/
idris_rts.c
244 lines (204 loc) · 6.31 KB
/
idris_rts.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "idris_rts.h"
#include "idris_gc.h"
VM* init_vm(int stack_size, size_t heap_size) {
VAL* valstack = malloc(stack_size*sizeof(VAL));
int* intstack = malloc(stack_size*sizeof(int));
double* floatstack = malloc(stack_size*sizeof(double));
VM* vm = malloc(sizeof(VM));
vm -> valstack = valstack;
vm -> valstack_top = valstack;
vm -> valstack_base = valstack;
vm -> intstack = intstack;
vm -> intstack_ptr = intstack;
vm -> floatstack = floatstack;
vm -> floatstack_ptr = floatstack;
vm -> stack_max = stack_size;
vm -> heap = malloc(heap_size);
vm -> oldheap = NULL;
vm -> heap_next = vm -> heap;
vm -> heap_end = vm -> heap + heap_size;
vm -> heap_size = heap_size;
vm -> collections = 0;
vm -> allocations = 0;
vm -> heap_growth = heap_size;
vm -> ret = NULL;
return vm;
}
void* allocate(VM* vm, size_t size) {
if ((size & 7)!=0) {
size = 8 + ((size >> 3) << 3);
}
vm->allocations += size+sizeof(size_t)*2;
if (vm -> heap_next + size < vm -> heap_end) {
void* ptr = (void*)(((size_t*)(vm->heap_next))+2);
*((size_t*)(vm->heap_next)) = size+sizeof(size_t)*2;
vm -> heap_next += size+sizeof(size_t)*2;
return ptr;
} else {
gc(vm);
return allocate(vm, size);
}
}
void* allocCon(VM* vm, int arity) {
Closure* cl = allocate(vm, sizeof(ClosureType) +
sizeof(con) + sizeof(VAL)*arity);
cl -> ty = CON;
if (arity == 0) {
cl -> info.c.args = NULL;
} else {
cl -> info.c.args = (void*)((char*)cl + sizeof(ClosureType)
+ sizeof(con));
}
return (void*)cl;
}
VAL MKFLOAT(VM* vm, double val) {
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(double));
cl -> ty = FLOAT;
cl -> info.f = val;
return cl;
}
VAL MKSTR(VM* vm, char* str) {
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
sizeof(char)*strlen(str)+1);
cl -> ty = STRING;
cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
strcpy(cl -> info.str, str);
return cl;
}
VAL MKPTR(VM* vm, void* ptr) {
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(void*));
cl -> ty = PTR;
cl -> info.ptr = ptr;
return cl;
}
VAL MKCON(VM* vm, int tag, int arity, ...) {
Closure* cl;
int i;
va_list args;
va_start(args, arity);
cl = allocCon(vm, arity);
cl -> info.c.tag = tag;
cl -> info.c.arity = arity;
VAL* argptr = (VAL*)(cl -> info.c.args);
for (i = 0; i < arity; ++i) {
VAL v = va_arg(args, VAL);
*argptr = v;
argptr++;
}
va_end(args);
return cl;
}
void PROJECT(VM* vm, VAL r, int loc, int arity) {
int i;
VAL* argptr = (VAL*)(r -> info.c.args);
for(i = 0; i < arity; ++i) {
LOC(i+loc) = *argptr++;
}
}
void SLIDE(VM* vm, int args) {
int i;
for(i = 0; i < args; ++i) {
LOC(i) = TOP(i);
}
}
void dumpVal(VAL v) {
int i;
switch(v->ty) {
case CON:
printf("%d[", v->info.c.tag);
for(i = 0; i < v->info.c.arity; ++i) {
VAL* args = (VAL*)v->info.c.args;
dumpVal(args[i]);
}
printf("] ");
}
}
VAL idris_castIntStr(VM* vm, VAL i) {
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) + sizeof(char)*16);
cl -> ty = STRING;
cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
sprintf(cl -> info.str, "%ld", GETINT(i));
return cl;
}
VAL idris_castStrInt(VM* vm, VAL i) {
char *end;
i_int v = strtol(GETSTR(i), &end, 10);
if (*end != '\0') return MKINT(0); else return MKINT(v);
}
VAL idris_castFloatStr(VM* vm, VAL i) {
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) + sizeof(char)*32);
cl -> ty = STRING;
cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
sprintf(cl -> info.str, "%g", GETFLOAT(i));
return cl;
}
VAL idris_castStrFloat(VM* vm, VAL i) {
return MKFLOAT(vm, strtod(GETSTR(i), NULL));
}
VAL idris_concat(VM* vm, VAL l, VAL r) {
char *ls = GETSTR(l);
char *rs = GETSTR(r);
Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
strlen(ls) + strlen(rs) + 1);
// Oops! problem if the second allocate triggers a gc because cl has
// to be a root. Fix by allocating all in one go.
// Also note that l/r may be in from space, so don't delete after collection,
// rather, delete just before the next collection.
cl -> ty = STRING;
cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
strcpy(cl -> info.str, ls);
strcat(cl -> info.str, rs);
return cl;
}
VAL idris_strlt(VM* vm, VAL l, VAL r) {
char *ls = GETSTR(l);
char *rs = GETSTR(r);
return MKINT((i_int)(strcmp(ls, rs) < 0));
}
VAL idris_streq(VM* vm, VAL l, VAL r) {
char *ls = GETSTR(l);
char *rs = GETSTR(r);
return MKINT((i_int)(strcmp(ls, rs) == 0));
}
VAL idris_strlen(VM* vm, VAL l) {
return MKINT((i_int)(strlen(GETSTR(l))));
}
#define BUFSIZE 256
VAL idris_readStr(VM* vm, FILE* h) {
// Modified from 'safe-fgets.c' in the gdb distribution.
// (see http://www.gnu.org/software/gdb/current/)
char *line_ptr;
char* line_buf = (char *) malloc (BUFSIZE);
int line_buf_size = BUFSIZE;
/* points to last byte */
line_ptr = line_buf + line_buf_size - 1;
/* so we can see if fgets put a 0 there */
*line_ptr = 1;
if (fgets (line_buf, line_buf_size, h) == 0)
return MKSTR(vm, "");
/* we filled the buffer? */
while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
{
/* Make the buffer bigger and read more of the line */
line_buf_size += BUFSIZE;
line_buf = (char *) realloc (line_buf, line_buf_size);
/* points to last byte again */
line_ptr = line_buf + line_buf_size - 1;
/* so we can see if fgets put a 0 there */
*line_ptr = 1;
if (fgets (line_buf + line_buf_size - BUFSIZE - 1, BUFSIZE + 1, h) == 0)
return MKSTR(vm, "");
}
VAL str = MKSTR(vm, line_buf);
free(line_buf);
return str;
}
void stackOverflow() {
fprintf(stderr, "Stack overflow");
exit(-1);
}