forked from avsm/EpiVM
/
closure.c
1113 lines (941 loc) · 24.2 KB
/
closure.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "closure.h"
#include "emalloc.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
VAL one;
VAL* zcon;
int v_argc;
VAL* v_argv;
VMState* vm;
extern func _do___U__main();
ALLOCATOR allocate;
REALLOCATOR reallocate;
pool_t** pools = NULL;
pool_t* pool = NULL;
//void* blob = NULL;
//int blobnext = 0;
/*void* FASTMALLOC(int size) {
if (blob == NULL) { blob = malloc(10001000); }
void* newblock = blob+blobnext;
blobnext+=((size+4) & 0xfffffffc);
if (blobnext>10000000) blobnext=0;
return newblock;
}*/
void dumpClosureA(Closure* c, int rec);
void dumpCon(con* c, int rec) {
int x,arity;
if (!rec) { printf("TAG(%d)", c->tag & 65535); }
arity = c->tag >> 16;
if (arity>0 && !rec) { printf(": "); }
for(x=0; x<arity; ++x) {
dumpClosureA(c->args[x], rec);
if (x!=(arity-1)) { printf(", "); }
}
}
void dumpRecord(Closure* r) {
dumpClosureA(r, 1);
}
void dumpClosureA(Closure* c, int rec) {
c = DO_EVAL(c,0);
switch(GETTY(c)) {
case FUN:
printf("FUN[");
break;
case THUNK:
printf("THUNK[");
break;
case CON:
if (!rec) { printf("CON["); } else { printf("["); }
dumpCon((con*)c->info, rec);
break;
case INT:
if (!rec) { printf("INT[%ld", ((eint)c)>>1); } else { printf("[%ld", ((eint)c)>>1); }
break;
case BIGINT:
printf("BIGINT[");
break;
case FLOAT:
printf("FLOAT[");
break;
case BIGFLOAT:
printf("BIGFLOAT[");
break;
case STRING:
printf("STRING[%s", ((char*)c->info));
break;
case UNIT:
printf("UNIT[");
break;
case PTR:
printf("PTR[");
break;
case FREEVAR:
printf("FREEVAR[");
break;
default:
printf("[%d,%ld", GETTY(c), (eint)c->info);
}
printf("]");
}
void dumpClosure(Closure* c) {
dumpClosureA(c,0);
printf("\n");
}
void assertConR(Closure* c)
{
if (c==NULL) { printf("Null constructor\n"); assert(0); }
if (!ISCON(c)) { dumpClosure(c); assert(0); }
}
void assertIntR(Closure* c)
{
if (!ISINT(c)) { dumpClosure(c); assert(0); }
}
void* pool_malloc(size_t size) {
if ((size & 7)!=0) {
size = 8 + ((size >> 3) << 3);
}
*((size_t*)(pool->block_loc)) = size;
void* mem = (void*)(((size_t*)(pool->block_loc))+2);
pool->block_loc = pool->block_loc+size+sizeof(size_t)*2;
return mem;
}
void* pool_realloc(void* ptr, size_t size) {
if ((size & 7)!=0) {
size = 8 + ((size >> 3) << 3);
}
*((size_t*)(pool->block_loc)) = size;
void* mem = (void*)(((size_t*)(pool->block_loc))+2);
pool->block_loc = pool->block_loc+size+sizeof(size_t)*2;
size_t orig_size = *(((size_t*)ptr)-2);
memcpy(mem, ptr, orig_size);
return mem;
}
void* pool_grow_malloc(size_t size) {
// TODO: if we're out of space, make a new pool, with a pointer to
// the old pool so we can free it when we're ready.
if ((size & 7)!=0) {
size = 8 + ((size >> 3) << 3);
}
*((size_t*)(pool->block_loc)) = size;
void* mem = (void*)(((size_t*)(pool->block_loc))+2);
pool->block_loc = pool->block_loc+size+sizeof(size_t)*2;
return mem;
}
void* pool_grow_realloc(void* ptr, size_t size) {
if ((size & 7)!=0) {
size = 8 + ((size >> 3) << 3);
}
*((size_t*)(pool->block_loc)) = size;
void* mem = (void*)(((size_t*)(pool->block_loc))+2);
pool->block_loc = pool->block_loc+size+sizeof(size_t)*2;
size_t orig_size = *(((size_t*)ptr)-2);
memcpy(mem, ptr, orig_size);
return mem;
}
void freePool(pool_t* pool)
{
free(pool->block);
if (pool->grow!=NULL) {
freePool((pool_t*)(pool->grow));
}
free(pool);
}
VAL copyFun(fun* f, pool_t* oldpool) {
VAL c = EMALLOC(sizeof(Closure)+sizeof(fun));
fun* fn = (fun*)(c+1);
fn->fn = f->fn;
fn->arity = f->arity;
int args = f->arg_end - f->args;
fn->args = MKARGS(args);
fn->arg_end = fn->args + args;
void** p = fn->args;
void** a;
for(a = f->args; a < f->arg_end; ++a, ++p) {
*p = copy((VAL)(*a), oldpool);
}
SETTY(c, FUN);
c->info = (void*)fn;
EREADY(c);
return c;
}
VAL copyThunk(thunk* t, pool_t* oldpool) {
VAL c = EMALLOC(sizeof(Closure)+sizeof(fun));
thunk* fn = (thunk*)(c+1);
fn->fn = t->fn;
fn->args = MKARGS(t->numargs);
fn->numargs = t->numargs;
void** a = t->args;
void** p = fn->args;
int i;
for(i=0; i < t->numargs; ++i, ++a, ++p) {
*p = copy((VAL)(*a), oldpool);
}
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
VAL copyCon(con* c, pool_t* oldpool) {
int arity = c->tag >> 16;
// printf("COPY CON %d %d\n", c->tag & 65535, arity);
VAL nc = EMALLOC(sizeof(Closure)+sizeof(con));
con* cn = (con*)(nc+1);
cn->tag = c->tag;
cn->args = MKARGS(arity);
void** a = c->args;
void** p = cn->args;
int i;
for(i=0; i<arity; ++i, ++a, ++p) {
// printf("COPY ARG %d\n", *a);
*p = copy((VAL)(*a), oldpool);
}
SETTY(nc, CON);
nc->info = (void*)cn;
EREADY(nc);
return nc;
}
// TODO: Preserve sharing. But this function isn't really intended for that
// sort of thing.
VAL copy(VAL x, pool_t* oldpool) {
// only copy things that were allocated in the given pool.
// TODO: also need to check whether it was allocated in pool->grow
if (x>=(VAL)(oldpool->block) && x<(VAL)(oldpool->block_end)) {
switch(GETTY(x)) {
case FUN:
return copyFun((fun*)x->info, oldpool);
case THUNK:
return copyThunk((thunk*)x->info, oldpool);
case CON:
return copyCon((con*)x->info, oldpool);
case INT:
return x;
case BIGINT:
return MKBIGINT((mpz_t*)(x->info));
case FLOAT:
return MKFLOAT(*((double*)x->info));
case BIGFLOAT:
assert(0); // NOT IMPLEMENTED YET
case STRING:
return MKSTR((char*)x->info);
case UNIT:
return MKUNIT;
case PTR:
return MKPTR(x->info);
case FREEVAR:
assert(0); // NOT IMPLEMENTED
}
return x;
}
else {
return x;
}
}
inline VAL CLOSURE(func x, int arity, int args, void** block)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(fun)); // MKCLOSURE;
fun* fn = (fun*)(c+1);
fn->fn = x;
fn->arity = arity;
if (args==0) {
fn->args = 0;
fn->arg_end = 0;
} else {
fn->args = MKARGS(args);
fn->arg_end=fn->args+args;
memcpy((void*)(fn->args), (void*)block, args*sizeof(VAL));
}
SETTY(c, FUN);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTORn(int tag, int arity, void** block)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (arity << 16);
if (arity==0) {
cn->args = 0;
} else {
cn->args = MKARGS(arity);
memcpy((void*)(cn->args), (void*)block, arity*sizeof(VAL));
}
SETTY(c, CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTOR1(int tag, VAL a1)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)+sizeof(VAL)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (1 << 16);
cn->args = (void*)c+sizeof(Closure)+sizeof(con); // MKARGS(1);
cn->args[0] = a1;
SETTY(c,CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTOR2(int tag, VAL a1, VAL a2)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)+2*sizeof(VAL)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (2 << 16);
cn->args = (void*)c+sizeof(Closure)+sizeof(con); //MKARGS(2);
cn->args[0] = a1;
cn->args[1] = a2;
SETTY(c,CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTOR3(int tag, VAL a1, VAL a2, VAL a3)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)+3*sizeof(VAL)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (3 << 16);
cn->args = (void*)c+sizeof(Closure)+sizeof(con); //MKARGS(3);
cn->args[0] = a1;
cn->args[1] = a2;
cn->args[2] = a3;
SETTY(c,CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTOR4(int tag, VAL a1, VAL a2, VAL a3, VAL a4)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)+4*sizeof(VAL)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (4 << 16);
cn->args = (void*)c+sizeof(Closure)+sizeof(con); //MKARGS(2);
cn->args[0] = a1;
cn->args[1] = a2;
cn->args[2] = a3;
cn->args[3] = a4;
SETTY(c,CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
inline VAL CONSTRUCTOR5(int tag, VAL a1, VAL a2, VAL a3, VAL a4, VAL a5)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(con)+5*sizeof(VAL)); // MKCLOSURE;
con* cn = (con*)(c+1);
cn->tag = tag + (5 << 16);
cn->args = (void*)c+sizeof(Closure)+sizeof(con); //MKARGS(5);
cn->args[0] = a1;
cn->args[1] = a2;
cn->args[2] = a3;
cn->args[3] = a4;
cn->args[4] = a5;
SETTY(c,CON);
c->info = (void*)cn;
EREADY(c);
return c;
}
// This needs to make a copy
inline VAL CLOSURE_ADDN(VAL xin, int args, void** block)
{
assert(GETTY(xin) == FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, args + diff);
fn->arg_end = fn->args + diff;
memcpy((void*)(fn->arg_end), (void*)block, args*sizeof(VAL));
fn->arg_end += args;
return x;
}
/*
VAL CLOSURE_ADDN(VAL xin, int args, void** block)
{
switch(args) {
case 1: return CLOSURE_ADD1(xin,block[0]);
case 2: return CLOSURE_ADD2(xin,block[0],block[1]);
case 3: return CLOSURE_ADD3(xin,block[0],block[1],block[2]);
case 4: return CLOSURE_ADD4(xin,block[0],block[1],block[2],block[3]);
case 5: return CLOSURE_ADD5(xin,block[0],block[1],block[2],block[3],block[4]);
default: return aux_CLOSURE_ADDN(xin,args,block);
}
}
*/
inline VAL CLOSURE_ADD1(VAL xin, VAL a1)
{
assert(GETTY(xin)==FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, diff + 1);
fn->arg_end = fn->args + diff;
fn->arg_end[0] = a1;
fn->arg_end+=1;
return x;
}
inline VAL CLOSURE_ADD2(VAL xin, VAL a1, VAL a2)
{
assert(GETTY(xin)==FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, diff + 2);
fn->arg_end = fn->args + diff;
fn->arg_end[0] = a1;
fn->arg_end[1] = a2;
fn->arg_end+=2;
return x;
}
inline VAL CLOSURE_ADD3(VAL xin, VAL a1, VAL a2, VAL a3)
{
assert(GETTY(xin)==FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, diff + 3);
fn->arg_end = fn->args + diff;
fn->arg_end[0] = a1;
fn->arg_end[1] = a2;
fn->arg_end[2] = a3;
fn->arg_end+=3;
return x;
}
inline VAL CLOSURE_ADD4(VAL xin, VAL a1, VAL a2, VAL a3, VAL a4)
{
assert(GETTY(xin)==FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, diff + 4);
fn->arg_end = fn->args + diff;
fn->arg_end[0] = a1;
fn->arg_end[1] = a2;
fn->arg_end[2] = a3;
fn->arg_end[3] = a4;
fn->arg_end+=4;
return x;
}
inline VAL CLOSURE_ADD5(VAL xin, VAL a1, VAL a2, VAL a3, VAL a4, VAL a5)
{
assert(GETTY(xin)==FUN);
fun* finf = (fun*)xin->info;
VAL x = CLOSURE(finf->fn, finf->arity,
finf->arg_end-finf->args, finf->args);
fun* fn = (fun*)(x->info);
int diff = fn->arg_end - fn->args;
fn->args = MOREARGS(fn->args, diff + 5);
fn->arg_end = fn->args + diff;
fn->arg_end[0] = a1;
fn->arg_end[1] = a2;
fn->arg_end[2] = a3;
fn->arg_end[3] = a4;
fn->arg_end[4] = a5;
fn->arg_end+=5;
return x;
}
inline VAL CLOSURE_APPLY(VAL f, int args, void** block)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return CLOSURE_ADDN(f,args,block);
}
fn->fn = (void*)f;
fn->numargs = args;
if (args==0) {
fn->args = 0;
} else {
fn->args = MKARGS(args);
memcpy((void*)(fn->args), (void*)block, args*sizeof(VAL));
}
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL aux_CLOSURE_APPLY1(VAL f, VAL a1)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return CLOSURE_ADD1(f,a1);
}
fn->fn = (void*)f;
fn->numargs = 1;
fn->args = MKARGS(1);
fn->args[0] = a1;
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL aux_CLOSURE_APPLY2(VAL f, VAL a1, VAL a2)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return NULL; //CLOSURE_ADD2(f,a1,a2);
}
fn->fn = (void*)f;
fn->numargs = 2;
fn->args = MKARGS(2);
fn->args[0] = a1;
fn->args[1] = a2;
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL aux_CLOSURE_APPLY3(VAL f, VAL a1, VAL a2, VAL a3)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return CLOSURE_ADD3(f,a1,a2,a3);
}
fn->fn = (void*)f;
fn->numargs = 2;
fn->args = MKARGS(3);
fn->args[0] = a1;
fn->args[1] = a2;
fn->args[2] = a3;
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL aux_CLOSURE_APPLY4(VAL f, VAL a1, VAL a2, VAL a3, VAL a4)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return CLOSURE_ADD4(f,a1,a2,a3,a4);
}
fn->fn = (void*)f;
fn->numargs = 2;
fn->args = MKARGS(4);
fn->args[0] = a1;
fn->args[1] = a2;
fn->args[2] = a3;
fn->args[3] = a4;
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL aux_CLOSURE_APPLY5(VAL f, VAL a1, VAL a2, VAL a3, VAL a4, VAL a5)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(thunk)); // MKCLOSURE;
thunk* fn = (thunk*)(c+1);
if (ISFUN(f)) {
return CLOSURE_ADD5(f,a1,a2,a3,a4,a5);
}
fn->fn = (void*)f;
fn->numargs = 2;
fn->args = MKARGS(5);
fn->args[0] = a1;
fn->args[1] = a2;
fn->args[2] = a3;
fn->args[3] = a4;
fn->args[4] = a5;
SETTY(c,THUNK);
c->info = (void*)fn;
EREADY(c);
return c;
}
inline VAL CLOSURE_APPLY1(VAL f, VAL a1)
{
if (ISFUN(f)) {
fun* finf = (fun*)(f->info);
int got = finf->arg_end-finf->args;
if (finf->arity == (got+1)) {
void* block[got+1];
memcpy(block, finf->args, got*sizeof(VAL));
block[got] = a1;
return (VAL)(finf->fn(block));
}
else if (finf->arity < (got+1)) {
return (VAL) DO_EVAL(CLOSURE_ADD1(f,a1), 0);
} else return CLOSURE_ADD1(f,a1);
}
else return aux_CLOSURE_APPLY1(f,a1);
}
void* block[1024]; // Yes. I know. Better check below that this is big enough.
inline VAL CLOSURE_APPLY2(VAL f, VAL a1, VAL a2)
{
int i;
if (ISFUN(f)) {
fun* finf = (fun*)(f->info);
int got = finf->arg_end-finf->args;
if (finf->arity == (got+2)) {
// memcpy(block, finf->args, got*sizeof(VAL));
for(i=0; i<got; ++i) {
block[i] = finf->args[i];
}
block[got] = a1;
block[got+1] = a2;
return (VAL)(finf->fn(block));
} else if (finf->arity < (got+2)) {
return (VAL) DO_EVAL(CLOSURE_ADD2(f,a1,a2), 0);
} else return CLOSURE_ADD2(f,a1,a2);
}
else return aux_CLOSURE_APPLY2(f,a1,a2);
}
inline VAL CLOSURE_APPLY3(VAL f, VAL a1, VAL a2, VAL a3)
{
if (ISFUN(f)) {
fun* finf = (fun*)(f->info);
int got = finf->arg_end-finf->args;
if (finf->arity == (got+3)) {
void* block[got+3];
memcpy(block, finf->args, got*sizeof(VAL));
block[got] = a1;
block[got+1] = a2;
block[got+2] = a3;
return (VAL)(finf->fn(block));
}
else if (finf->arity < (got+3)) {
return (VAL) DO_EVAL(CLOSURE_ADD3(f,a1,a2,a3), 0);
} else return CLOSURE_ADD3(f,a1,a2,a3);
}
else return aux_CLOSURE_APPLY3(f,a1,a2,a3);
}
inline VAL CLOSURE_APPLY4(VAL f, VAL a1, VAL a2, VAL a3, VAL a4)
{
if (ISFUN(f)) {
fun* finf = (fun*)(f->info);
int got = finf->arg_end-finf->args;
if (finf->arity == (got+4)) {
void* block[got+4];
memcpy(block, finf->args, got*sizeof(VAL));
block[got] = a1;
block[got+1] = a2;
block[got+2] = a3;
block[got+3] = a4;
return (VAL)(finf->fn(block));
}
else if (finf->arity < (got+4)) {
return (VAL) DO_EVAL(CLOSURE_ADD4(f,a1,a2,a3,a4), 0);
} else return CLOSURE_ADD4(f,a1,a2,a3,a4);
}
else return aux_CLOSURE_APPLY4(f,a1,a2,a3,a4);
}
inline VAL CLOSURE_APPLY5(VAL f, VAL a1, VAL a2, VAL a3, VAL a4, VAL a5)
{
if (ISFUN(f)) {
fun* finf = (fun*)(f->info);
int got = finf->arg_end-finf->args;
if (finf->arity == (got+5)) {
void* block[got+5];
memcpy(block, finf->args, got*sizeof(VAL));
block[got] = a1;
block[got+1] = a2;
block[got+2] = a3;
block[got+3] = a4;
block[got+4] = a5;
return (VAL)(finf->fn(block));
}
else if (finf->arity < (got+5)) {
return (VAL) DO_EVAL(CLOSURE_ADD5(f,a1,a2,a3,a4,a5), 0);
} else return CLOSURE_ADD5(f,a1,a2,a3,a4,a5);
}
else return aux_CLOSURE_APPLY5(f,a1,a2,a3,a4,a5);
}
VAL DO_EVAL(VAL x, int update) {
// dummy value we'll never inspect, leave it alone.
if (x==NULL) return x;
VAL result;
// VAL x = (VAL)(*xin);
fun* fn;
thunk* th;
int excess;
// dumpClosure(x);
switch(GETTY(x)) {
case CON:
case INT:
case BIGINT:
case FLOAT:
case STRING:
case PTR:
case UNIT:
return x; // Already evaluated
case FUN:
// If the number of arguments is right, run it.
fn = (fun*)(x->info);
excess = (fn->arg_end - fn->args) - fn->arity;
if (excess == 0) {
result = fn->fn(fn->args);
// If the result is still a function, better eval again to make
// more progress.
// It could reasonably be null though, so be careful. It's null
// if it was a foreign/io call in particular.
if (result) {
if (GETTY(result)==FUN || GETTY(result)==THUNK) {
result=DO_EVAL(result, update);
}
/* if (ISINT(result)) {
printf("Updating with %d\n", x);
} else {
printf("Updating %d %d with %d\n", x, GETTY(x), result);
}*/
if (update) { UPDATE(x,result); } else { return result; }
}
else {
if (update) { SETTY(x, INT); x->info=(void*)42; } else { return NULL; }
}
}
// If there are too many arguments, run it with the right number
// then apply the remaining arguments to the resulting closure
else if (excess > 0) {
result = fn->fn(fn->args);
result = CLOSURE_APPLY(result, excess, fn->args + fn->arity);
result = DO_EVAL(result, update);
if (update) { UPDATE(x,result); } else { return result; }
return x;
}
break;
case THUNK:
th = (thunk*)(x->info);
// Evaluate inner thunk, which should give us a function
VAL nextfn = DO_EVAL((VAL)(th->fn), update);
// Apply this thunk's arguments to it
CLOSURE_APPLY((VAL)nextfn, th->numargs, th->args);
// And off we go again...
nextfn = DO_EVAL(nextfn, update);
if (update) { UPDATE(x, nextfn); } else { return nextfn; }
return x;
break;
default:
assert(0); // Can't happen
}
EREADY(x);
return x;
}
/*
void* DO_PROJECT(VAL x, int arg)
{
assert(x->ty == CON);
con* cn = (con*)x->info;
return cn->args[arg];
}
*/
/*void* MKINT(int x)
{
return (void*)((x<<1)+1);
// VAL c = MKCLOSURE;
// SETTY(c, INT);
// c->info = (void*)x;
// return c;
}*/
mpz_t* NEWBIGINTI(int val)
{
mpz_t* bigint = EMALLOC(sizeof(mpz_t));
mpz_init(*bigint);
mpz_set_si(*bigint, val);
return bigint;
}
void* NEWBIGINTVALI(int val)
{
mpz_t* bigint;
VAL c = EMALLOC(sizeof(Closure)+sizeof(mpz_t));
bigint = (mpz_t*)(c+1);
mpz_init(*bigint);
mpz_set_si(*bigint, val);
SETTY(c, BIGINT);
c->info = (void*)bigint;
EREADY(c);
return c;
}
void* NEWBIGINT(char* intstr)
{
mpz_t* bigint;
VAL c = EMALLOC(sizeof(Closure)+sizeof(mpz_t));
bigint = (mpz_t*)(c+1);
mpz_init(*bigint);
mpz_set_str(*bigint, intstr, 10);
SETTY(c, BIGINT);
c->info = (void*)bigint;
EREADY(c);
return c;
}
void* MKBIGINT(mpz_t* big)
{
mpz_t* bigint;
VAL c = EMALLOC(sizeof(Closure)+sizeof(mpz_t));
bigint = (mpz_t*)(c+1);
mpz_init(*bigint);
mpz_set(*bigint, *big);
SETTY(c, BIGINT);
c->info = (void*)bigint;
EREADY(c);
return c;
}
void* MKFLOAT(double f)
{
VAL c = EMALLOC(sizeof(Closure)+sizeof(double));
double* num = (double*)(c+1);
*num = f;
SETTY(c, FLOAT);
c->info = (void*)num;
EREADY(c);
return c;
}
/*
int GETINT(void* x)
{
return ((eint)x)>>1;
}
*/
mpz_t* GETBIGINT(void* x)
{
if (ISINT(x)) {
return NEWBIGINTI(GETINT(x));
} else {
return (mpz_t*)(((VAL)x)->info);
}
}
double GETFLOAT(void* x)
{
return *((double*)(((VAL)x)->info));
}
void* MKSTR(const char* x)
{
// VAL c = EMALLOC(sizeof(Closure)+strlen(x)+sizeof(char)+1); //MKCLOSURE;
VAL c = EMALLOC(sizeof(Closure));
SETTY(c, STRING);
// c->info = ((void*)c)+sizeof(Closure);// (void*)(EMALLOC(strlen(x)*sizeof(char)+1));
// strcpy(c->info,x);
// Since MKSTR is used to build strings from foreign calls, the string
// itself will already have been allocated so we just want the closure.
c->info=(void*)x;
EREADY(c);
return c;
}
void* MKPTR(void* x)
{
VAL c = MKCLOSURE;
SETTY(c, PTR);
c->info = x;
EREADY(c);
return c;
}
/* void* GETPTR(void* x) */
/* { */
/* return (void*)(((VAL)x)->info); */
/* } */
void ERROR(char* msg)
{
printf("*** error : %s ***\n",msg);
assert(0);
exit(1);
}
void* MKFREE(int x)
{
VAL c = MKCLOSURE;
SETTY(c, FREEVAR);
c->info = (void*)x;
EREADY(c);
return c;