-
Notifications
You must be signed in to change notification settings - Fork 3
/
i.c
4967 lines (4435 loc) · 133 KB
/
i.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
/* i.c -- instructions */
#include "n.h"
#include "i.h"
/* imports */
extern obj cx__2Aglobals_2A;
extern obj cx__2Atransformers_2A;
extern obj cx__2Adynamic_2Dstate_2A;
extern obj cx_continuation_2Dadapter_2Dcode;
extern obj cx_callmv_2Dadapter_2Dclosure;
extern obj cx__2Acurrent_2Dinput_2A;
extern obj cx__2Acurrent_2Doutput_2A;
extern obj cx__2Acurrent_2Derror_2A;
//#define istagged(o, t) istagged_inlined(o, t)
/* forwards */
static struct intgtab_entry *lookup_integrable(int sym);
static int intgtab_count(void);
static int isintegrable(obj x);
static struct intgtab_entry *integrabledata(obj x);
static obj mkintegrable(struct intgtab_entry *);
static int integrable_type(struct intgtab_entry *pi);
static const char *integrable_global(struct intgtab_entry *pi);
static const char *integrable_code(struct intgtab_entry *pi, int n);
static obj *rds_intgtab(obj *r, obj *sp, obj *hp);
static obj *rds_stox(obj *r, obj *sp, obj *hp);
static obj *rds_stoc(obj *r, obj *sp, obj *hp);
static obj *init_modules(obj *r, obj *sp, obj *hp);
/* platform-dependent optimizations */
#if defined(__clang__)
#define unlikely(x) __builtin_expect(x, 0)
#define likely(x) __builtin_expect(x, 1)
#if __clang_major__ >= 13
#define VM_AC_IN_REG
#define outofline __attribute__((noinline))
#define VM_MUSTTAIL_GUARANTEE
#define musttail __attribute__((musttail))
#define regcall __regcall
#define noalias restrict
#else
#define outofline
#define musttail
#define regcall
#define noalias
#endif
/* #pragma GCC optimize ("align-functions=16") */
#define nochecks __attribute__((no_stack_protector, aligned(8)))
/* __attribute__((nocf_check)) */
#define VM_INS_CODE_ALIGNED
#else
#define unlikely(x) (x)
#define likely(x) (x)
#define outofline
#define musttail
#define regcall
#define noalias
#define nochecks
#endif
/* copying objects */
#ifndef OBJCOPY_LOOP
#define objcpy(dst, src, cnt) \
memcpy((dst), (src), (cnt)*sizeof(obj))
#define objmove_left(dst, src, cnt) \
memmove((dst), (src), (cnt)*sizeof(obj))
#define objmove(dst, src, cnt) \
memmove((dst), (src), (cnt)*sizeof(obj))
#else
#define objcpy(dst, src, cnt) \
do { obj *noalias dp__ = (dst), *noalias sp__ = (src); int n__ = (cnt); \
while (n__-- > 0) *dp__++ = *sp__++; } while(0)
#define objmove_left(dst, src, cnt) \
do { int n__ = (cnt); obj *dp__ = (dst), *sp__ = (src); \
while (n__-- > 0) *dp__++ = *sp__++; } while(0)
#define objmove_right(dst, src, cnt) \
do { int n__ = (cnt); obj *dp__ = (dst)+n__, *sp__ = (src)+n__; \
while (n__-- > 0) *--dp__ = *--sp__; } while(0)
#define objmove(dst, src, cnt) \
do { int n__ = (cnt); obj *dp__ = (dst), *sp__ = (src); \
if (dp__ > sp__) { dp__ += n__, sp__ += n; while (n__-- > 0) *--dp__ = *--sp__; } \
else if (dp__ < sp__) while (n__-- > 0) *dp__++ = *sp__++; } while(0)
#endif
#ifdef VM_INS_CODE_ALIGNED /* direct representation */
#define ins_from_obj(x) ((ins_t)(x))
#else /* indirect representation (alignment needed) */
#define ins_from_obj(x) (*(ins_t*)(x))
#endif
/* instruction calling convention (platform-dependent) */
#ifdef VM_AC_IN_REG
#define ILOCALS obj ac, *r, *ip, *sp, *hp
#define IPARAMS obj ac, obj *noalias r, obj *noalias ip, obj *noalias sp, obj *noalias hp
#define IARGS ac, r, ip, sp, hp
#define IARGS1 ac, r, ip+1, sp, hp
#define unload_ac() (ra = ac)
#define reload_ac() (ac = ra)
#else
#define ILOCALS obj *r, *ip, *sp, *hp
#define IPARAMS obj *r, obj *noalias ip, obj *noalias sp, obj *noalias hp
#define IARGS r, ip, sp, hp
#define IARGS1 r, ip+1, sp, hp
#define ac ra
#define unload_ac() (0)
#define reload_ac() (0)
#endif
/* gc-safe obj registers; some of them used for saving/restoring during gc */
#define rk (r[0]) /* #f or any non-false value for unwindi; run result */
#define ra (r[1]) /* shadow reg for the accumulator (argc on call) */
#define rx (r[2]) /* next instruction (index in closure's code) */
#define rd (r[3]) /* current closure/display (vector of [0]=code, display) */
#define rs (r[4]) /* shadow reg for stack pointer */
#define rz (r[5]) /* red zone for stack pointer (r + len - rsz) */
/* the rest of the register file is used as a stack */
#define VM_REGC 6 /* r[0] ... r[5] */
#define VM_STACK_LEN 256000 /* r[6] ... r[256005] */
#define VM_STACK_RSZ 256 /* red zone for overflow checks */
#define VM_STACK_GSZ (VM_STACK_LEN-VM_STACK_RSZ)
/* faster non-debug type testing */
#ifdef NDEBUG /* quick */
static int istagged_inline(obj o, int t) { return isobjptr(o) && hblkref(o, 0) == obj_from_size(t); }
#define istagged(o, t) istagged_inline(o, t)
#endif
/* box representation extras */
#define boxbsz() hbsz(1+1)
#define hend_box() (*--hp = obj_from_size(BOX_BTAG), hendblk(1+1))
/* pair representation extras */
#define pairbsz() hbsz(2+1)
#define hend_pair() (*--hp = obj_from_size(PAIR_BTAG), hendblk(2+1))
/* vector representation extras */
#define vecbsz(n) hbsz((n)+1)
#define hend_vec(n) (*--hp = obj_from_size(VECTOR_BTAG), hendblk((n)+1))
/* record representation extras */
#define recbsz(c) hbsz((c)+1)
#define hend_rec(rtd, c) (*--hp = rtd, hendblk((c)+1))
/* vm closure representation */
#ifdef NDEBUG /* quick */
#define isvmclo(x) (isobjptr(x) && isobjptr(hblkref(x, 0)))
#define vmcloref(x,i) hblkref(x, i)
#define vmclolen(x) hblklen(x)
#define vmclobsz(c) hbsz(c)
#define hend_vmclo(c) hendblk(c)
#else /* slow but thorough */
#define isvmclo isprocedure
#define vmcloref *procedureref
#define vmclolen procedurelen
#define vmclobsz(c) hbsz(c)
#define hend_vmclo(c) hendblk(c)
#endif
/* vm tuple representation (c != 1) */
#define istuple(x) istagged(x, 0)
#define tupleref(x,i) *taggedref(x, 0, i)
#define tuplelen(x) taggedlen(x, 0)
#define tuplebsz(c) hbsz((c)+1)
#define hend_tuple(c) (*--hp = obj_from_size(0), hendblk((c)+1))
/* in/re-loading gc-save shadow registers */
#define unload_ip() (rx = obj_from_fixnum(ip - &vectorref(vmcloref(rd, 0), 0)))
#define reload_ip() (ip = &vectorref(vmcloref(rd, 0), fixnum_from_obj(rx)))
#define unload_sp() (rs = obj_from_fixnum(sp - r))
#define reload_sp() (sp = r + fixnum_from_obj(rs))
/* access to stack, display, global cells */
#define sref(i) (sp[-(i)-1])
#define dref(i) (vmcloref(rd, (i)+1))
#define gref(p) (boxref(p))
#ifdef _DEBUG
static void _sck(obj *s) {
assert(s != NULL);
assert(s >= cxg_regs + VM_REGC);
assert(s < cxg_rend);
}
#define spush(o) (_sck(sp), *sp++ = o)
#define spop() (_sck(sp), *--sp)
#define sdrop(n) (_sck(sp), sp -= (n))
#define sgrow(n) (_sck(sp), sp += (n))
#else
#define spush(o) (*sp++ = o)
#define spop() (*--sp)
#define sdrop(n) (sp -= (n))
#define sgrow(n) (sp += (n))
#endif
/* forcing gc explicitly */
#define hp_collect() do { \
unload_ac(); unload_ip(); \
hp = cxm_hgc(r, sp, hp, 0); \
reload_ac(); reload_ip(); \
} while (0)
/* reserving heap memory inside instructions */
#define hp_reserve(n) do { \
if (unlikely(hp < cxg_heap + (n))) { \
unload_ac(); unload_ip(); \
hp = cxm_hgc(r, sp, hp, n); \
reload_ac(); reload_ip(); \
} } while (0)
#define hp_reserve_inline(n) \
((hp < cxg_heap + (n)) \
? (unload_ac(), unload_ip(), hp = cxm_hgc(r, sp, hp, (n)), reload_ac(), reload_ip(), hp) \
: hp)
#define hp_pushptr(p, pt) \
(hp_reserve_inline(2), *--hp = (obj)(p), *--hp = (obj)(pt), (obj)(hp+1))
/* object representation extras */
#define bool_obj(b) obj_from_bool(b)
#define is_bool(o) is_bool_obj(o)
#define get_bool(o) bool_from_obj(o)
#define char_obj(b) obj_from_char(b)
#define is_char(o) is_char_obj(o)
#define get_char(o) char_from_obj(o)
#define void_obj() obj_from_void(0)
#define is_void(o) (o == obj_from_void(0))
#define is_shebang(o) isshebang(o)
#define get_shebang(o) getshebang(o)
#define shebang_obj(i) mkshebang(i)
#define unit_obj() obj_from_unit()
#define is_unit(o) (o == obj_from_unit())
#define null_obj() mknull()
#define is_null(o) isnull(o)
#define eof_obj() mkeof()
#define is_eof(o) ((o) == mkeof())
#define fixnum_obj(x) obj_from_fixnum(x)
#define is_fixnum(o) is_fixnum_obj(o)
#define are_fixnums(o1, o2) (is_fixnum(o1) && is_fixnum(o2))
#define get_fixnum(o) fixnum_from_obj(o)
#define is_byte(o) is_byte_obj(o)
#define byte_obj(x) obj_from_fixnum((unsigned char)(x))
#define get_byte(o) ((unsigned char)fixnum_from_obj(o))
#ifdef FLONUMS_BOXED
#define flonum_obj(x) hp_pushptr(dupflonum(x), FLONUM_NTAG)
#define is_flonum(o) is_flonum_obj(o)
#define get_flonum(o) flonum_from_obj(o)
#else
#define flonum_obj(x) obj_from_flonum(0, x)
#define is_flonum(o) is_flonum_obj(o)
#define get_flonum(o) flonum_from_obj(o)
#endif
#define is_symbol(o) issymbol(o)
#define get_symbol(o) getsymbol(o)
#define symbol_obj(i) mksymbol(i)
#define is_pair(o) ispair(o)
#define pair_car(o) car(o)
#define pair_cdr(o) cdr(o)
#define is_list(o) islist(o)
#define is_circular(o) iscircular(o)
#define is_noncircular(o) (!iscircular(o))
#define is_vector(o) isvector(o)
#define vector_len(o) vectorlen(o)
#define vector_ref(o, i) vectorref(o, i)
#define string_obj(s) hp_pushptr((s), STRING_NTAG)
#define is_string(o) isstring(o)
#define string_len(o) stringlen(o)
#define string_ref(o, i) (*stringref(o, i))
#define bytevector_obj(s) hp_pushptr((s), BYTEVECTOR_NTAG)
#define is_bytevector(o) isbytevector(o)
#define bytevector_len(o) bytevectorlen(o)
#define bytevector_ref(o, i) (*bytevectorref(o, i))
#define iport_file_obj(fp) hp_pushptr((fp), IPORT_FILE_NTAG)
#define oport_file_obj(fp) hp_pushptr((fp), OPORT_FILE_NTAG)
#define iport_string_obj(fp) hp_pushptr((fp), IPORT_STRING_NTAG)
#define oport_string_obj(fp) hp_pushptr((fp), OPORT_STRING_NTAG)
#define iport_bytevector_obj(fp) hp_pushptr((fp), IPORT_BYTEVECTOR_NTAG)
#define oport_bytevector_obj(fp) hp_pushptr((fp), OPORT_BYTEVECTOR_NTAG)
#define is_iport(o) isiport(o)
#define is_oport(o) isoport(o)
#define is_box(o) isbox(o)
#define box_ref(o) boxref(o)
#define is_proc(o) isvmclo(o)
#define is_tuple(o) (isrecord(o) && recordrtd(o) == 0)
#define tuple_len(o) tuplelen(o)
#define tuple_ref(o, i) tupleref(o, i)
#define is_record(o) (isrecord(o) && recordrtd(o) != 0)
#define record_rtd(o) recordrtd(o)
#define record_len(o) recordlen(o)
#define record_ref(o, i) recordref(o, i)
/* cxi instructions protocol; retval is new hp: */
typedef obj* regcall (*ins_t)(IPARAMS);
/* calling next instruction and returning from instruction */
#define goih(ih) musttail return ((ih)(IARGS))
#define goi(name) musttail return (glue(cxi_, name)(IARGS))
#define gonexti() musttail return ((ins_from_obj(*ip))(IARGS1))
#define unwindi(c) return (unload_ac(), unload_ip(), rd = (c), hp)
#define check_sp() if (unlikely(sp > (obj*)rz)) fail("stack overflow")
#ifdef VM_MUSTTAIL_GUARANTEE
#define callsubi() check_sp(); reload_ip(); gonexti()
#define retfromi() reload_ip(); gonexti()
#define trampcnd() (0)
#else
#define callsubi() check_sp(); unload_sp(); return hp
#define retfromi() unload_sp(); return hp
#define trampcnd() (rd)
#endif
/* defining instruction helpers */
#define define_instrhelper(name) \
static obj* regcall outofline nochecks name(IPARAMS)
/* defining and binding instructions */
#define define_instruction(name) \
obj* regcall nochecks glue(cxi_, name)(IPARAMS)
#ifdef VM_INS_CODE_ALIGNED
#define declare_instruction_global(name) \
obj glue(cx_ins_2D, name) = (obj)(&glue(cxi_, name));
#else
#define declare_instruction_global(name) \
static ins_t glue(cxib_, name)[1] = { &glue(cxi_, name) }; \
obj glue(cx_ins_2D, name) = (obj)(&glue(cxib_, name)[0]);
#endif
static obj vmhost(obj);
obj vmcases[13] = {
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
(obj)vmhost
};
/* vmhost procedure */
static obj vmhost(obj pc)
{
ILOCALS;
int rc = cxg_rc, i;
r = cxg_regs; hp = cxg_hp;
jump:
switch (objptr_from_obj(pc)-vmcases) {
case 0: /* execute-thunk-closure */
/* r[0] = self, r[1] = k, r[2] = closure */
{ obj k, arg;
assert(rc == 3);
k = r[1]; arg = r[2];
r = cxm_rgc(NULL, VM_REGC + VM_STACK_LEN);
rk = k; /* continuation, kept there */
ra = obj_from_fixnum(0); /* argc, shadow ac */
rx = obj_from_fixnum(0); /* shadow ip */
rd = arg; /* thunk closure to execute */
rs = obj_from_fixnum(VM_REGC); /* sp */
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
do { /* unwindi trampoline */
reload_ac(); /* ra => ac */
reload_ip(); /* rd/rx => ip */
reload_sp(); /* rs => sp */
hp = (ins_from_obj(*ip))(IARGS1);
} while (likely(trampcnd()));
/* r[0] = k, r[1] = result */
r[2] = r[1];
r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 1: /* make-closure */
/* r[0] = clo, r[1] = k, r[2] = code */
{ assert(rc == 3);
r[0] = r[1]; r[1] = r[2];
/* r[0] = k; r[1] = code */
hreserve(vmclobsz(1), 2); /* 2 live regs */
*--hp = r[1];
r[2] = hend_vmclo(1);
r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 2: /* decode-sexp */
/* r[0] = clo, r[1] = k, r[2] = xstr */
{ assert(rc == 3);
r[0] = r[1]; r[1] = r[2];
/* r[0] = k; r[1] = xstr */
for (i = 3; i < VM_REGC; ++i) r[i] = 0;
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
hp = rds_stox(r, r+VM_REGC, hp); /* r[1] -> r[1] */
r[2] = r[1]; r[1] = obj_from_ktrap();
/* r[0] = k; r[1] = ek; r[2] = code */
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 3: /* decode-code */
/* r[0] = clo, r[1] = k, r[2] = cstr */
{ assert(rc == 3);
r[0] = r[1]; r[1] = r[2];
/* r[0] = k; r[1] = cstr */
for (i = 3; i < VM_REGC; ++i) r[i] = 0;
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
hp = rds_stoc(r, r+VM_REGC, hp); /* r[1] -> r[1] */
r[2] = r[1]; r[1] = obj_from_ktrap();
/* r[0] = k; r[1] = ek; r[2] = code */
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 4: /* find-integrable-encoding */
/* r[0] = clo, r[1] = k, r[2] = id, r[3] = argc */
{ assert(rc == 4);
r[2] = obj_from_bool(0);;
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 5: /* encode-integrable */
/* r[0] = clo, r[1] = k, r[2] = argc, r[3] = pe, r[4] = port */
{ assert(rc == 5);
assert(0);
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 6: /* install-global-lambdas */
/* r[0] = clo, r[1] = k */
{ assert(rc == 2);
r[0] = r[1];
for (i = 2; i < VM_REGC; ++i) r[i] = 0;
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
hp = rds_intgtab(r, r+VM_REGC, hp);
r[1] = obj_from_ktrap();
r[2] = obj_from_void(0);
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 7: /* initialize-modules */
/* r[0] = clo, r[1] = k */
{ assert(rc == 2);
r[0] = r[1];
r = cxm_rgc(NULL, VM_REGC + VM_STACK_LEN);
for (i = 2; i < VM_REGC; ++i) r[i] = 0;
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
hp = init_modules(r, r+VM_REGC, hp);
r[1] = obj_from_ktrap();
r[2] = obj_from_void(0);
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 8: /* integrable? */
/* r[0] = clo, r[1] = k, r[2] = obj */
{ assert(rc == 3);
r[2] = obj_from_bool(isintegrable(r[2]));
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 9: /* lookup-integrable */
/* r[0] = clo, r[1] = k, r[2] = id */
{ assert(rc == 3);
if (issymbol(r[2])) {
int sym = getsymbol(r[2]);
struct intgtab_entry *pe = lookup_integrable(sym);
r[2] = pe ? mkintegrable(pe) : obj_from_bool(0);
} else r[2] = obj_from_bool(0);
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 10: /* integrable-type */
/* r[0] = clo, r[1] = k, r[2] = ig */
{ assert(rc == 3);
if (isintegrable(r[2])) {
int it = integrable_type(integrabledata(r[2]));
r[2] = it ? obj_from_char(it) : obj_from_bool(0);
} else r[2] = obj_from_bool(0);
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 11: /* integrable-global */
/* r[0] = clo, r[1] = k, r[2] = ig */
{ assert(rc == 3);
if (isintegrable(r[2])) {
const char *igs = integrable_global(integrabledata(r[2]));
r[2] = igs ? mksymbol(internsym((char*)igs)) : obj_from_bool(0);
} else r[2] = obj_from_bool(0);
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
case 12: /* integrable-code */
/* r[0] = clo, r[1] = k, r[2] = ig, r[3] = i */
{ assert(rc == 4);
if (isintegrable(r[2]) && is_fixnum(r[3])) {
const char *cs = integrable_code(integrabledata(r[2]), get_fixnum(r[3]));
r[2] = cs ? hpushstr(3, newstring((char*)cs)) : obj_from_bool(0);
} else r[2] = obj_from_bool(0);
r[0] = r[1]; r[1] = obj_from_ktrap();
pc = objptr_from_obj(r[0])[0];
rc = 3;
goto jump; }
default: /* inter-host call */
cxg_hp = hp;
cxm_rgc(r, 1);
cxg_rc = rc;
return pc;
}
}
/* instructions for basic vm machinery */
define_instrhelper(cxi_fail) {
fprintf(stderr, "run-time failure: %s\n", (char*)ac);
ac = void_obj(); /* so it is not printed by repl */
unwindi(0);
}
define_instrhelper(cxi_failactype) {
char *msg = (char*)spop(); obj p;
fprintf(stderr, "run-time failure: argument is not a %s:\n", msg);
p = oport_file_obj(stderr); spush(p);
oportputcircular(ac, p, 0);
fputc('\n', stderr);
spop();
ac = void_obj(); /* so it is not printed by repl */
unwindi(0);
}
#define fail(msg) do { ac = (obj)msg; musttail return cxi_fail(IARGS); } while (0)
#define failtype(x, msg) do { ac = (x); spush((obj)msg); musttail return cxi_failactype(IARGS); } while (0)
#define failactype(msg) do { spush((obj)msg); musttail return cxi_failactype(IARGS); } while (0)
#define ckp(x) do { obj _x = (x); if (unlikely(!is_pair(_x))) \
{ ac = _x; spush((obj)"pair"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckl(x) do { obj _x = (x); if (unlikely(!is_list(_x))) \
{ ac = _x; spush((obj)"list"); musttail return cxi_failactype(IARGS); } } while (0)
#define cku(x) do { obj _x = (x); if (unlikely(!is_null(_x))) \
{ ac = _x; spush((obj)"proper list"); musttail return cxi_failactype(IARGS); } } while (0)
#define cko(x) do { obj _x = (x); if (unlikely(!is_record(_x))) \
{ ac = _x; spush((obj)"record"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckv(x) do { obj _x = (x); if (unlikely(!is_vector(_x))) \
{ ac = _x; spush((obj)"vector"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckc(x) do { obj _x = (x); if (unlikely(!is_char(_x))) \
{ ac = _x; spush((obj)"char"); musttail return cxi_failactype(IARGS); } } while (0)
#define cks(x) do { obj _x = (x); if (unlikely(!is_string(_x))) \
{ ac = _x; spush((obj)"string"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckb(x) do { obj _x = (x); if (unlikely(!is_bytevector(_x))) \
{ ac = _x; spush((obj)"bytevector"); musttail return cxi_failactype(IARGS); } } while (0)
#define cki(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x))) \
{ ac = _x; spush((obj)"fixnum"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckj(x) do { obj _x = (x); if (unlikely(!is_flonum(_x))) \
{ ac = _x; spush((obj)"flonum"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckn(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x) && !is_flonum(_x))) \
{ ac = _x; spush((obj)"number"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckk(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x) || get_fixnum(_x) < 0)) \
{ ac = _x; spush((obj)"nonnegative fixnum"); musttail return cxi_failactype(IARGS); } } while (0)
#define ck8(x) do { obj _x = (x); if (unlikely(!is_byte(_x))) \
{ ac = _x; spush((obj)"byte"); musttail return cxi_failactype(IARGS); } } while (0)
#define cky(x) do { obj _x = (x); if (unlikely(!is_symbol(_x))) \
{ ac = _x; spush((obj)"symbol"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckr(x) do { obj _x = (x); if (unlikely(!is_iport(_x))) \
{ ac = _x; spush((obj)"input port"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckw(x) do { obj _x = (x); if (unlikely(!is_oport(_x))) \
{ ac = _x; spush((obj)"output port"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckx(x) do { obj _x = (x); if (unlikely(!is_proc(_x))) \
{ ac = _x; spush((obj)"procedure"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckz(x) do { obj _x = (x); if (unlikely(!is_box(_x))) \
{ ac = _x; spush((obj)"box, cell, or promise"); musttail return cxi_failactype(IARGS); } } while (0)
#define ckg(x) do { obj _x = (x); if (unlikely(!isintegrable(_x))) \
{ ac = _x; spush((obj)"integrable entry"); musttail return cxi_failactype(IARGS); } } while (0)
#define cksb(x) do { obj _x = (x); if (unlikely(!is_shebang(_x))) \
{ ac = _x; spush((obj)"directive"); musttail return cxi_failactype(IARGS); } } while (0)
define_instruction(halt) {
unwindi(0);
}
define_instruction(panic) {
obj l, p; cks(ac); ckl(sref(0));
p = oport_file_obj(stderr);
fprintf(stderr, "error: %s", stringchars(ac));
if (is_pair(sref(0))) fputs(":\n", stderr);
else fputs("\n", stderr);
for (l = sref(0); is_pair(l); l = pair_cdr(l)) {
oportputcircular(pair_car(l), p, 0);
fputc('\n', stderr);
}
sdrop(1);
ac = void_obj();
unwindi(0);
}
define_instruction(abort) {
/* exit code ignored */
abort();
unwindi(0);
}
define_instruction(exit) {
int excode;
if (ac == bool_obj(0)) excode = 1;
else if (ac == bool_obj(1)) excode = 0;
else if (is_fixnum(ac)) excode = (int)get_fixnum(ac);
else excode = 1;
exit(excode);
unwindi(0);
}
define_instruction(lit) {
ac = *ip++;
gonexti();
}
define_instruction(sref) {
int i = get_fixnum(*ip++);
ac = sref(i);
gonexti();
}
define_instruction(dref) {
int i = get_fixnum(*ip++);
ac = dref(i);
gonexti();
}
define_instruction(gref) {
obj p = *ip++;
ac = gref(p);
gonexti();
}
define_instruction(iref) {
ac = box_ref(ac);
gonexti();
}
define_instruction(iset) {
box_ref(ac) = spop();
gonexti();
}
define_instruction(dclose) {
int i, n = get_fixnum(*ip++), c = n+1;
hp_reserve(vmclobsz(c));
for (i = n-1; i >= 0; --i) *--hp = sref(i); /* display */
*--hp = *ip++; /* code */
ac = hend_vmclo(c); /* closure */
sdrop(n);
gonexti();
}
define_instruction(sbox) {
int i = get_fixnum(*ip++);
hp_reserve(boxbsz());
*--hp = sref(i);
sref(i) = hend_box();
gonexti();
}
define_instruction(br) {
int dx = get_fixnum(*ip++);
ip += dx;
gonexti();
}
define_instruction(brt) {
int dx = get_fixnum(*ip++);
if (ac) ip += dx;
gonexti();
}
define_instruction(brnot) {
int dx = get_fixnum(*ip++);
if (!ac) ip += dx;
gonexti();
}
define_instruction(andbo) {
if (ac) { /* go to next binary instruction w/2 args */
ac = spop();
} else { /* skip the next instruction, drop its args */
sdrop(2);
ip += 1;
}
gonexti();
}
define_instruction(sseti) {
int i = get_fixnum(*ip++);
box_ref(sref(i)) = ac;
gonexti();
}
define_instruction(dseti) {
int i = get_fixnum(*ip++);
box_ref(dref(i)) = ac;
gonexti();
}
define_instruction(gloc) {
ac = *ip++;
gonexti();
}
define_instruction(gset) {
obj p = *ip++;
gref(p) = ac;
gonexti();
}
define_instruction(appl) {
int n, i; obj l = spop(), t = l;
for (n = 0; is_pair(t); t = pair_cdr(t)) ++n; sgrow(n);
for (i = 0; i < n; ++i, l = pair_cdr(l)) sref(i) = pair_car(l);
ckx(ac); rd = ac; rx = fixnum_obj(0);
ac = fixnum_obj(n); /* argc */
callsubi();
}
define_instruction(cwmv) {
obj t = ac, x = spop();
ckx(t); ckx(x);
/* we can run in constant space in some situations */
if (vmcloref(x, 0) == cx_continuation_2Dadapter_2Dcode
&& vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
/* arrange call of t with x as continuation */
/* [0] adapter_code, [1] dynamic_state */
int n = vmclolen(x) - 2;
assert((cxg_rend - cxg_regs - VM_REGC) > n);
sp = r + VM_REGC; /* stack is empty */
objcpy(sp, &vmcloref(x, 2), n);
sp += n; /* contains n elements now */
rd = t; rx = fixnum_obj(0);
ac = fixnum_obj(0);
callsubi();
} else {
/* arrange return to cwmv code w/x */
spush(x);
spush(cx_callmv_2Dadapter_2Dclosure);
spush(fixnum_obj(0));
/* call the producer */
rd = t; rx = fixnum_obj(0); ac = fixnum_obj(0);
callsubi();
}
}
define_instruction(rcmv) {
/* single-value producer call returns here with result in ac, cns on stack */
obj val = ac, x = spop();
/* tail-call the consumer with the returned value(s) */
if (is_unit(val)) { /* (values) in improper context */
ac = fixnum_obj(0);
} else if (is_tuple(val)) { /* (values a1 a2 a ...) in improper context */
int n = tuple_len(val), i;
for (i = n-1; i >= 0; --i) spush(tuple_ref(val, i));
ac = fixnum_obj(n);
} else { /* regular single value */
spush(val); ac = fixnum_obj(1);
}
rd = x; rx = fixnum_obj(0);
callsubi();
}
define_instruction(sdmv) {
/* sending values on stack, ac contains argc */
if (ac == fixnum_obj(1)) {
/* can return anywhere, including rcmv */
ac = spop();
rx = spop();
rd = spop();
retfromi();
} else {
/* can only pseudo-return to rcmv */
int n = get_fixnum(ac), m = 3, i;
if (sref(n) == fixnum_obj(0) && sref(n+1) == cx_callmv_2Dadapter_2Dclosure) {
/* tail-call the consumer with the produced values */
rd = sref(n+2); rx = fixnum_obj(0); /* cns */
/* NB: can be sped up for popular cases: n == 0, n == 2 */
objmove_left(sp-n-m, sp-n, n);
sdrop(m); callsubi();
} else if (n == 0) { /* return unit (n = 0) */
ac = unit_obj();
rx = spop();
rd = spop();
retfromi();
} else { /* return args as tuple (n > 1) */
hp_reserve(tuplebsz(n));
for (i = n-1; i >= 0; --i) *--hp = sref(i);
ac = hend_tuple(n);
sdrop(n);
rx = spop();
rd = spop();
retfromi();
}
}
}
define_instruction(lck) {
int m = get_fixnum(*ip++);
int n; cki(sref(m)); ckx(sref(m+1));
n = (int)(sp-m-(r+VM_REGC));
hp_reserve(vmclobsz(n+2));
hp -= n; objcpy(hp, sp-n-m, n);
/* [0] adapter_code, [1] dynamic_state */
*--hp = cx__2Adynamic_2Dstate_2A;
*--hp = cx_continuation_2Dadapter_2Dcode;
ac = hend_vmclo(n+2);
gonexti();
}
define_instruction(lck0) {
int n; cki(sref(0)); ckx(sref(1));
n = (int)(sp-(r+VM_REGC));
hp_reserve(vmclobsz(n+2));
hp -= n; objcpy(hp, sp-n, n);
/* [0] adapter_code, [1] dynamic_state */
*--hp = cx__2Adynamic_2Dstate_2A;
*--hp = cx_continuation_2Dadapter_2Dcode;
ac = hend_vmclo(n+2);
gonexti();
}
define_instruction(wck) {
obj x = ac, t = spop(); ckx(t); ckx(x);
if (vmcloref(x, 0) != cx_continuation_2Dadapter_2Dcode)
failactype("continuation");
/* [0] adapter_code, [1] dynamic_state */
if (vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
/* restore cont stack and invoke t there */
int n = vmclolen(x) - 2;
assert((cxg_rend - cxg_regs - VM_REGC) > n);
sp = r + VM_REGC; /* stack is empty */
objcpy(sp, &vmcloref(x, 2), n);
sp += n; /* contains n elements now */
rd = t; rx = fixnum_obj(0);
ac = fixnum_obj(0);
callsubi();
} else {
/* have to arrange call of cont adapter */
spush(x);
spush(cx_callmv_2Dadapter_2Dclosure);
spush(fixnum_obj(0));
/* call the thunk as producer */
rd = t; rx = fixnum_obj(0);
ac = fixnum_obj(0);
callsubi();
}
}
define_instruction(wckr) {
obj x = ac, o = spop(); ckx(x);
if (vmcloref(x, 0) != cx_continuation_2Dadapter_2Dcode)
failactype("continuation");
/* [0] adapter_code, [1] dynamic_state */
if (vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
/* restore cont stack and return o there */
int n = vmclolen(x) - 2;
assert((cxg_rend - cxg_regs - VM_REGC) > n);
sp = r + VM_REGC; /* stack is empty */
objcpy(sp, &vmcloref(x, 2), n);
sp += n;
ac = o;
rx = spop();
rd = spop();
retfromi();
} else {
/* have to arrange call of cont adapter */
spush(o);
rd = x; rx = fixnum_obj(0);
ac = fixnum_obj(1);
callsubi();
}
}
define_instruction(rck) {
/* called with continuation as rd:
* in: ac:argc, args on stack, rd display is dys, saved stack */
if (vmcloref(rd, 1) != cx__2Adynamic_2Dstate_2A) {
/* need to run the rest of the code to unwind/rewind on the
* old stack; rck will be called again when done */
gonexti();
} else if (ac == fixnum_obj(1)) { /* easy, popular case */
ac = rd;
goi(wckr);
} else { /* multiple results case */
/* rd[0] adapter_code, rd[1] dynamic_state */
int c = get_fixnum(ac), n = vmclolen(rd) - 2, i;
obj *ks = &vmcloref(rd, 2), *ke = ks + n;
if (ke-ks > 3 && *--ke == fixnum_obj(0) && *--ke == cx_callmv_2Dadapter_2Dclosure) {
obj *sb = r + VM_REGC;
rd = *--ke; rx = fixnum_obj(0); n = (int)(ke - ks); /* cns */
/* arrange stack as follows: [ks..ke] [arg ...] */
assert((cxg_rend - cxg_regs - VM_REGC) > n + c);
if (c) objmove(sb+n, sp-c, c);
objcpy(sb, ks, n);
sp = sb+n+c; callsubi();
} else if (c == 0) { /* return unit (n = 0) */
spush(unit_obj());
ac = rd;
goi(wckr);
} else { /* return args as tuple (n > 1) */
hp_reserve(tuplebsz(c));
for (i = c-1; i >= 0; --i) *--hp = sref(i);
ac = hend_tuple(c);
sdrop(c);
spush(ac);
ac = rd;
goi(wckr);
}
}
}
define_instruction(dys) {
ac = cx__2Adynamic_2Dstate_2A;
gonexti();
}
define_instruction(setdys) {
cx__2Adynamic_2Dstate_2A = ac;
gonexti();
}
define_instruction(save) {
int dx = get_fixnum(*ip++);
spush(rd);
spush(fixnum_obj(ip + dx - &vector_ref(vmcloref(rd, 0), 0)));
gonexti();
}
define_instruction(push) { spush(ac); gonexti(); }
define_instruction(jdceq) {
obj v = *ip++, i = *ip++;
if (ac == v) {
rd = dref(get_fixnum(i));
rx = fixnum_obj(0);
callsubi();
}
gonexti();
}
define_instruction(jdcge) {
obj v = *ip++, i = *ip++;
if (ac >= v) { /* unsigned tagged fixnums can be compared as-is */
rd = dref(get_fixnum(i));
rx = fixnum_obj(0);
callsubi();
}
gonexti();
}
define_instruction(jdref) {
int i = get_fixnum(*ip++);
rd = dref(i);
rx = fixnum_obj(0);
callsubi();
}
define_instruction(call) {
int n = get_fixnum(*ip++);
ckx(ac); rd = ac; rx = fixnum_obj(0);
ac = fixnum_obj(n); /* argc */
callsubi();
}
define_instruction(scall) {
int m = get_fixnum(*ip++), n = get_fixnum(*ip++);
ckx(ac); rd = ac; rx = fixnum_obj(0);
ac = fixnum_obj(n); /* argc */
objmove_left(sp-n-m, sp-n, n);
sdrop(m);
callsubi();
}
define_instruction(return) {
rx = spop();
rd = spop();
retfromi();
}
define_instruction(sreturn) {
int n = get_fixnum(*ip++);
sdrop(n);
rx = spop();
rd = spop();
retfromi();
}