@@ -20,7 +20,7 @@ VM* init_vm(int stack_size, size_t heap_size) {
20 vm -> intstack_ptr = intstack;
20 vm -> intstack_ptr = intstack;
21 vm -> floatstack = floatstack;
21 vm -> floatstack = floatstack;
22 vm -> floatstack_ptr = floatstack;
22 vm -> floatstack_ptr = floatstack;
23− vm -> stack_max = stack_size;
23+ vm -> stack_max = valstack + stack_size;
24 vm -> heap = malloc(heap_size);
24 vm -> heap = malloc(heap_size);
25 vm -> oldheap = NULL;
25 vm -> oldheap = NULL;
26 vm -> heap_next = vm -> heap;
26 vm -> heap_next = vm -> heap;
@@ -30,18 +30,21 @@ VM* init_vm(int stack_size, size_t heap_size) {
30 vm -> allocations = 0;
30 vm -> allocations = 0;
31 vm -> heap_growth = heap_size;
31 vm -> heap_growth = heap_size;
32 vm -> ret = NULL;
32 vm -> ret = NULL;
33 return vm;
34 return vm;
36void* allocate(VM* vm, size_t size) {
37void* allocate(VM* vm, size_t size) {
38+// return malloc(size);
37 if ((size & 7)!=0) {
39 if ((size & 7)!=0) {
38 size = 8 + ((size >> 3) << 3);
40 size = 8 + ((size >> 3) << 3);
40− vm->allocations += size+sizeof(size_t)*2;
42+ vm->allocations += size + sizeof(size_t);
41 if (vm -> heap_next + size < vm -> heap_end) {
43 if (vm -> heap_next + size < vm -> heap_end) {
42− void* ptr = (void*)(((size_t*)(vm->heap_next))+2);
44+ void* ptr = (void*)(vm->heap_next + sizeof(size_t));
43− *((size_t*)(vm->heap_next)) = size+sizeof(size_t)*2;
45+ *((size_t*)(vm->heap_next)) = size + sizeof(size_t);
44− vm -> heap_next += size+sizeof(size_t)*2;
46+ vm -> heap_next += size + sizeof(size_t);
45 return ptr;
48 return ptr;
@@ -57,6 +60,9 @@ void* allocCon(VM* vm, int arity) {
58 cl -> info.c.args = (void*)((char*)cl + sizeof(Closure));
61 cl -> info.c.args = (void*)((char*)cl + sizeof(Closure));
63+ cl -> info.c.arity = arity;
64+// cl -> info.c.tag = 42424242;
65+// printf("%p\n", cl);
60 return (void*)cl;
66 return (void*)cl;
@@ -84,17 +90,17 @@ VAL MKPTR(VM* vm, void* ptr) {
84 return cl;
90 return cl;
87−VAL MKCON(VM* vm, int tag, int arity, ...) {
93+VAL MKCON(VM* vm, VAL cl, int tag, int arity, ...) {
90 va_list args;
95 va_list args;
92 va_start(args, arity);
97 va_start(args, arity);
94− cl = allocCon(vm, arity);
99+// Closure* cl = allocCon(vm, arity);
95 cl -> info.c.tag = tag;
100 cl -> info.c.tag = tag;
96 cl -> info.c.arity = arity;
101 cl -> info.c.arity = arity;
97 VAL* argptr = (VAL*)(cl -> info.c.args);
102 VAL* argptr = (VAL*)(cl -> info.c.args);
103+ printf("... %p %p\n", cl, argptr);
99 for (i = 0; i < arity; ++i) {
105 for (i = 0; i < arity; ++i) {
100 VAL v = va_arg(args, VAL);
106 VAL v = va_arg(args, VAL);
@@ -102,7 +108,6 @@ VAL MKCON(VM* vm, int tag, int arity, ...) {
102 argptr++;
108 argptr++;
104 va_end(args);
110 va_end(args);
106 return cl;
111 return cl;
@@ -122,7 +127,23 @@ void SLIDE(VM* vm, int args) {
130+void dumpStack(VM* vm) {
134+ for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
137+ if (*root >= (VAL)(vm->heap) && *root < (VAL)(vm->heap_end)) { printf("OK"); }
125void dumpVal(VAL v) {
145void dumpVal(VAL v) {
146+ if (v==NULL) return;
127 if (ISINT(v)) {
148 if (ISINT(v)) {
128 printf("%ld ", GETINT(v));
149 printf("%ld ", GETINT(v));
@@ -137,6 +158,10 @@ void dumpVal(VAL v) {
138 printf("] ");
159 printf("] ");
163+ dumpVal((VAL)(v->info.ptr));
141 printf("val");
166 printf("val");
@@ -154,7 +179,10 @@ VAL idris_castIntStr(VM* vm, VAL i) {
154VAL idris_castStrInt(VM* vm, VAL i) {
179VAL idris_castStrInt(VM* vm, VAL i) {
155 char *end;
180 char *end;
156 i_int v = strtol(GETSTR(i), &end, 10);
181 i_int v = strtol(GETSTR(i), &end, 10);
182+ if (*end == '\0' || *end == '\n' || *end == '\r')
157− if (*end != '\0') return MKINT(0); else return MKINT(v);
183+ return MKINT(v);
160VAL idris_castFloatStr(VM* vm, VAL i) {
188VAL idris_castFloatStr(VM* vm, VAL i) {
@@ -170,8 +198,10 @@ VAL idris_castStrFloat(VM* vm, VAL i) {
172VAL idris_concat(VM* vm, VAL l, VAL r) {
200VAL idris_concat(VM* vm, VAL l, VAL r) {
173− char *ls = GETSTR(l);
174 char *rs = GETSTR(r);
201 char *rs = GETSTR(r);
202+ char *ls = GETSTR(l);
175 Closure* cl = allocate(vm, sizeof(Closure) + strlen(ls) + strlen(rs) + 1);
205 Closure* cl = allocate(vm, sizeof(Closure) + strlen(ls) + strlen(rs) + 1);
176 cl -> ty = STRING;
206 cl -> ty = STRING;
177 cl -> info.str = (char*)cl + sizeof(Closure);
207 cl -> info.str = (char*)cl + sizeof(Closure);
@@ -246,10 +276,10 @@ VAL idris_strTail(VM* vm, VAL str) {
247VAL idris_strCons(VM* vm, VAL x, VAL xs) {
277VAL idris_strCons(VM* vm, VAL x, VAL xs) {
248 char *xstr = GETSTR(xs);
278 char *xstr = GETSTR(xs);
249− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
279+ Closure* cl = allocate(vm, sizeof(Closure) +
250 strlen(xstr) + 2);
280 strlen(xstr) + 2);
251 cl -> ty = STRING;
281 cl -> ty = STRING;
252− cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
282+ cl -> info.str = (char*)cl + sizeof(Closure);
253 cl -> info.str[0] = (char)(GETINT(x));
283 cl -> info.str[0] = (char)(GETINT(x));
254 strcpy(cl -> info.str+1, xstr);
284 strcpy(cl -> info.str+1, xstr);
255 return cl;
285 return cl;
@@ -261,10 +291,10 @@ VAL idris_strIndex(VM* vm, VAL str, VAL i) {
262VAL idris_strRev(VM* vm, VAL str) {
292VAL idris_strRev(VM* vm, VAL str) {
263 char *xstr = GETSTR(str);
293 char *xstr = GETSTR(str);
264− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
294+ Closure* cl = allocate(vm, sizeof(Closure) +
265 strlen(xstr) + 1);
295 strlen(xstr) + 1);
266 cl -> ty = STRING;
296 cl -> ty = STRING;
267− cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
297+ cl -> info.str = (char*)cl + sizeof(Closure);
268 int y = 0;
298 int y = 0;
269 int x = strlen(xstr);
299 int x = strlen(xstr);