-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtigerlibrary_x86_64.c
113 lines (99 loc) · 1.84 KB
/
tigerlibrary_x86_64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
enum {INT_SIZE = 8};
void *__getmem(int64_t size)
{
return malloc(size);
}
void *__getmem_fill(int64_t count, int64_t value)
{
int64_t *result = (int64_t *)malloc(count*INT_SIZE);
int64_t *dest = result;
if (count > 0)
while (count-- != 0)
*dest++ = value;
return result;
}
void __print(char *s)
{
int i;
uint64_t len = *((uint64_t *)s);
s += INT_SIZE;
while (len-- != 0)
fputc(*s++, stdout);
}
void __flush()
{
fflush(stdout);
}
static void *chr_impl(char c)
{
char *res = (char *)malloc(INT_SIZE+1);
*((int64_t *)res) = 1;
res[INT_SIZE] = c;
return res;
}
void *__getchar()
{
return chr_impl(fgetc(stdin));
}
int64_t __ord(unsigned char *s)
{
if (*((int64_t *)s) == 0)
return -1;
else
return s[INT_SIZE];
}
void *__chr(int64_t i)
{
return chr_impl(i);
}
int64_t __size(char *s)
{
return *((int64_t *)s);
}
void *__substring(char *s, int64_t first, int64_t n)
{
int64_t len = *((int64_t *)s);
if (first + n > len)
n = len - first;
if (n < 0)
n = 0;
char *res = (char *)malloc(n+INT_SIZE);
*((int64_t *)res) = n;
memmove(res+INT_SIZE, s+INT_SIZE+first, n);
return res;
}
void *__concat(char *s1, char *s2)
{
int64_t len1 = *((int64_t *)s1);
int64_t len2 = *((int64_t *)s2);
char *res = (char *)malloc(INT_SIZE+len1+len2);
*((int64_t *)res) = len1+len2;
memmove(res+INT_SIZE, s1+INT_SIZE, len1);
memmove(res+INT_SIZE+len1, s2+INT_SIZE, len2);
return res;
}
int64_t __strcmp(char *s1, char *s2)
{
int64_t len1 = *((int64_t *)s1);
int64_t len2 = *((int64_t *)s2);
int64_t minlen = len1;
if (len2 < minlen)
minlen = len2;
int ret = strncmp(s1+INT_SIZE, s2+INT_SIZE, minlen);
if (ret != 0)
return ret;
else if (len1 == len2)
return 0;
else if (len1 < len2)
return -1;
else
return 1;
}
int64_t __not(int64_t i)
{
return !i;
}