forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnock.c
3168 lines (2801 loc) · 81.9 KB
/
nock.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
/// @file
#include "nock.h"
#include "allocate.h"
#include "hashtable.h"
#include "imprison.h"
#include "jets.h"
#include "jets/k.h"
#include "jets/q.h"
#include "manage.h"
#include "options.h"
#include "retrieve.h"
#include "trace.h"
#include "vortex.h"
#include "xtract.h"
#include "zave.h"
// define to have each opcode printed as it executes,
// along with some other debugging info
# undef VERBOSE_BYTECODE
#if 0
// Retained for debugging purposes.
static u3_noun _n_nock_on(u3_noun bus, u3_noun fol);
/* _n_hint(): process hint.
*/
static u3_noun
_n_hint(u3_noun zep,
u3_noun hod,
u3_noun bus,
u3_noun nex)
{
switch ( zep ) {
default: {
// u3m_p("weird zep", zep);
u3a_lose(zep);
u3a_lose(hod);
return _n_nock_on(bus, nex);
}
case c3__hunk:
case c3__lose:
case c3__mean:
case c3__spot: {
u3_noun tac = u3nc(zep, hod);
u3_noun pro;
u3t_push(tac);
#if 0
{
static int low_i;
if ( !low_i ) {
low_i = 1;
if ( 0 == (u3R->pro.nox_d % 65536ULL) ) {
if ( c3__spot == zep ) {
u3l_log("spot %d/%d : %d/%d",
u3h(u3h(u3t(hod))),
u3t(u3h(u3t(hod))),
u3h(u3t(u3t(hod))),
u3t(u3t(u3t(hod))));
}
}
low_i = 0;
}
}
#endif
pro = _n_nock_on(bus, nex);
u3t_drop();
return pro;
}
case c3__live: {
if ( c3y == u3ud(hod) ) {
u3t_off(noc_o);
u3t_heck(hod);
u3t_on(noc_o);
} else {
u3z(hod);
}
return _n_nock_on(bus, nex);
}
case c3__slog: {
if ( !(u3C.wag_w & u3o_quiet) ) {
u3t_off(noc_o);
u3t_slog(hod);
u3t_on(noc_o);
}
return _n_nock_on(bus, nex);
}
case c3__germ: {
u3_noun pro = _n_nock_on(bus, nex);
if ( c3y == u3r_sing(pro, hod) ) {
u3z(pro); return hod;
} else {
u3z(hod); return pro;
}
}
case c3__fast: {
u3_noun pro = _n_nock_on(bus, nex);
u3t_off(noc_o);
u3j_mine(hod, u3k(pro));
u3t_on(noc_o);
return pro;
}
case c3__memo: {
u3z(hod);
#if 0
return _n_nock_on(bus, nex);
#else
{
u3_noun pro = u3z_find_2(144 + c3__nock, bus, nex);
if ( pro != u3_none ) {
u3z(bus); u3z(nex);
return pro;
}
pro = _n_nock_on(u3k(bus), u3k(nex));
if ( &(u3H->rod_u) != u3R ) {
u3z_save_2(144 + c3__nock, bus, nex, pro);
}
u3z(bus); u3z(nex);
return pro;
}
#endif
}
case c3__sole: {
u3z(hod);
{
u3_noun pro = _n_nock_on(bus, nex);
// return u3z_uniq(pro);
return pro;
}
}
}
}
/* _n_nock_on(): produce .*(bus fol). Do not virtualize.
*/
static u3_noun
_n_nock_on(u3_noun bus, u3_noun fol)
{
u3_noun hib, gal;
while ( 1 ) {
hib = u3h(fol);
gal = u3t(fol);
#ifdef U3_CPU_DEBUG
u3R->pro.nox_d += 1;
#endif
if ( c3y == u3du(hib) ) {
u3_noun poz, riv;
poz = _n_nock_on(u3k(bus), u3k(hib));
riv = _n_nock_on(bus, u3k(gal));
u3a_lose(fol);
return u3i_cell(poz, riv);
}
else switch ( hib ) {
default: return u3m_bail(c3__exit);
case 0: {
if ( c3n == u3ud(gal) ) {
return u3m_bail(c3__exit);
}
else {
u3_noun pro = u3k(u3at(gal, bus));
u3a_lose(bus); u3a_lose(fol);
return pro;
}
}
u3_assert(!"not reached");
case 1: {
u3_noun pro = u3k(gal);
u3a_lose(bus); u3a_lose(fol);
return pro;
}
u3_assert(!"not reached");
case 2: {
u3_noun nex = _n_nock_on(u3k(bus), u3k(u3t(gal)));
u3_noun seb = _n_nock_on(bus, u3k(u3h(gal)));
u3a_lose(fol);
bus = seb;
fol = nex;
continue;
}
u3_assert(!"not reached");
case 3: {
u3_noun gof, pro;
gof = _n_nock_on(bus, u3k(gal));
pro = u3du(gof);
u3a_lose(gof); u3a_lose(fol);
return pro;
}
u3_assert(!"not reached");
case 4: {
u3_noun gof, pro;
gof = _n_nock_on(bus, u3k(gal));
pro = u3i_vint(gof);
u3a_lose(fol);
return pro;
}
u3_assert(!"not reached");
case 5: {
u3_noun wim = _n_nock_on(bus, u3k(gal));
u3_noun pro = u3r_sing(u3h(wim), u3t(wim));
u3a_lose(wim); u3a_lose(fol);
return pro;
}
u3_assert(!"not reached");
case 6: {
u3_noun b_gal, c_gal, d_gal;
u3x_trel(gal, &b_gal, &c_gal, &d_gal);
{
u3_noun tys = _n_nock_on(u3k(bus), u3k(b_gal));
u3_noun nex;
if ( 0 == tys ) {
nex = u3k(c_gal);
} else if ( 1 == tys ) {
nex = u3k(d_gal);
} else return u3m_bail(c3__exit);
u3a_lose(fol);
fol = nex;
continue;
}
}
u3_assert(!"not reached");
case 7: {
u3_noun b_gal, c_gal;
u3x_cell(gal, &b_gal, &c_gal);
{
u3_noun bod = _n_nock_on(bus, u3k(b_gal));
u3_noun nex = u3k(c_gal);
u3a_lose(fol);
bus = bod;
fol = nex;
continue;
}
}
u3_assert(!"not reached");
case 8: {
u3_noun b_gal, c_gal;
u3x_cell(gal, &b_gal, &c_gal);
{
u3_noun heb = _n_nock_on(u3k(bus), u3k(b_gal));
u3_noun bod = u3nc(heb, bus);
u3_noun nex = u3k(c_gal);
u3a_lose(fol);
bus = bod;
fol = nex;
continue;
}
}
u3_assert(!"not reached");
case 9: {
u3_noun b_gal, c_gal;
u3x_cell(gal, &b_gal, &c_gal);
{
u3_noun seb = _n_nock_on(bus, u3k(c_gal));
u3_noun pro;
u3t_off(noc_o);
pro = u3j_kick(seb, b_gal);
u3t_on(noc_o);
if ( u3_none != pro ) {
u3a_lose(fol);
return pro;
}
else {
if ( c3n == u3ud(b_gal) ) {
return u3m_bail(c3__exit);
}
else {
u3_noun nex = u3k(u3at(b_gal, seb));
u3a_lose(fol);
bus = seb;
fol = nex;
continue;
}
}
}
}
u3_assert(!"not reached");
case 10: {
u3_noun p_gal, q_gal;
u3x_cell(gal, &p_gal, &q_gal);
{
u3_noun zep, hod, nex;
if ( c3y == u3du(p_gal) ) {
u3_noun b_gal = u3h(p_gal);
u3_noun c_gal = u3t(p_gal);
u3_noun d_gal = q_gal;
zep = u3k(b_gal);
hod = _n_nock_on(u3k(bus), u3k(c_gal));
nex = u3k(d_gal);
}
else {
u3_noun b_gal = p_gal;
u3_noun c_gal = q_gal;
zep = u3k(b_gal);
hod = u3_nul;
nex = u3k(c_gal);
}
u3a_lose(fol);
return _n_hint(zep, hod, bus, nex);
}
}
case 11: {
u3_noun ref = _n_nock_on(u3k(bus), u3k(u3h(gal)));
u3_noun gof = _n_nock_on(bus, u3k(u3t(gal)));
u3_noun val;
u3t_off(noc_o);
val = u3m_soft_esc(u3k(ref), u3k(gof));
u3t_on(noc_o);
if ( !_(u3du(val)) ) {
u3m_bail(u3nt(1, gof, 0));
}
if ( !_(u3du(u3t(val))) ) {
//
// replace with proper error stack push
//
u3t_push(u3nt(c3__hunk, ref, gof));
return u3m_bail(c3__exit);
}
else {
u3_noun pro;
u3z(ref);
u3z(gof);
u3z(fol);
pro = u3k(u3t(u3t(val)));
u3z(val);
return pro;
}
}
u3_assert(!"not reached");
}
}
}
#endif
// Several opcodes "overflow" (from byte to short index) to their successor, so
// order can matter here.
// Note that we use an X macro (https://en.wikipedia.org/wiki/X_Macro) to unify
// the opcode's enum name, string representation, and computed goto into a
// single structure.
#define OPCODES \
/* non-nock bytecodes */ \
X(HALT, "halt", &&do_halt), /* 0: terminator, end of bytcode program */ \
X(BAIL, "bail", &&do_bail), /* 1: deterministic crash */ \
/* stack manipulation */ \
X(COPY, "copy", &&do_copy), /* 2 */ \
X(SWAP, "swap", &&do_swap), /* 3 */ \
X(TOSS, "toss", &&do_toss), /* 4 */ \
/* auto-cons */ \
X(AUTO, "auto", &&do_auto), /* 5: keep */ \
X(AULT, "ault", &&do_ault), /* 6: lose */ \
/* general purposes */ \
X(SNOC, "snoc", &&do_snoc), /* 7: keep */ \
X(SNOL, "snol", &&do_snol), /* 8: lose */ \
/* nock 0: head */ \
X(HEAD, "head", &&do_head), /* 9: keep */ \
X(HELD, "held", &&do_held), /* 10: lose */ \
/* nock 0: tail */ \
X(TAIL, "tail", &&do_tail), /* 11: keep */ \
X(TALL, "tall", &&do_tall), /* 12: lose */ \
/* nock 0: fragment (keep) */ \
X(FABK, "fabk", &&do_fabk), /* 13: c3_y */ \
X(FASK, "fask", &&do_fask), /* 14: c3_s */ \
X(FIBK, "fibk", &&do_fibk), /* 15: c3_y */ \
X(FISK, "fisk", &&do_fisk), /* 16: c3_s */ \
/* nock 0: fragment (lose) */ \
X(FABL, "fabl", &&do_fabl), /* 17: c3_y */ \
X(FASL, "fasl", &&do_fasl), /* 18: c3_s */ \
X(FIBL, "fibl", &&do_fibl), /* 19: c3_y */ \
X(FISL, "fisl", &&do_fisl), /* 20: c3_s */ \
/* nock 1: literal (keep) */ \
X(LIT0, "lit0", &&do_lit0), /* 21: a literal 0 */ \
X(LIT1, "lit1", &&do_lit1), /* 22: a literal 1 */ \
X(LITB, "litb", &&do_litb), /* 23: c3_y */ \
X(LITS, "lits", &&do_lits), /* 24: c3_s */ \
X(LIBK, "libk", &&do_libk), /* 25: c3_y */ \
X(LISK, "lisk", &&do_lisk), /* 26: c3_s */ \
/* nock 1: literal (lose) */ \
X(LIL0, "lil0", &&do_lil0), /* 27: a literal 0 */ \
X(LIL1, "lil1", &&do_lil1), /* 28: a literal 1 */ \
X(LILB, "lilb", &&do_lilb), /* 29: c3_y */ \
X(LILS, "lils", &&do_lils), /* 30: c3_s */ \
X(LIBL, "libl", &&do_libl), /* 31: c3_y */ \
X(LISL, "lisl", &&do_lisl), /* 32: c3_s */ \
/* nock 2: nock (lose) */ \
X(NOLK, "nolk", &&do_nolk), /* 33 */ \
X(NOCT, "noct", &&do_noct), /* 34 */ \
X(NOCK, "nock", &&do_nock), /* 35 */ \
/* nock 3 & 4 */ \
X(DEEP, "deep", &&do_deep), /* 36 */ \
X(BUMP, "bump", &&do_bump), /* 37 */ \
/* nock 5: equality */ \
X(SAM0, "sam0", &&do_sam0), /* 38: test that it is equal to 0 */ \
X(SAM1, "sam1", &&do_sam1), /* 39: test that it is equal to 1 */ \
X(SAMB, "samb", &&do_samb), /* 40: test equality for vars size c3_b */ \
X(SAMS, "sams", &&do_sams), /* 41: test equality for vars size c3_s */ \
X(SANB, "sanb", &&do_sanb), /* 42: test equality for vars size c3_b */ \
X(SANS, "sans", &&do_sans), /* 43: test equality for vars size c3_s */ \
X(SAME, "same", &&do_same), /* 44 */ \
X(SALM, "salm", &&do_salm), /* 45 */ \
X(SAMC, "samc", &&do_samc), /* 46 */ \
/* related to nock 6: unconditional skips */ \
X(SBIP, "sbip", &&do_sbip), /* 47: c3_b */ \
X(SIPS, "sips", &&do_sips), /* 48: c3_s */ \
X(SWIP, "swip", &&do_swip), /* 49: c3_l */ \
/* related to nock 6: conditional skips */ \
X(SBIN, "sbin", &&do_sbin), /* 50: c3_b */ \
X(SINS, "sins", &&do_sins), /* 51: c3_s */ \
X(SWIN, "swin", &&do_swin), /* 52: c3_l */ \
/* nock 9 */ \
X(KICB, "kicb", &&do_kicb), /* 53: c3_b */ \
X(KICS, "kics", &&do_kics), /* 54: c3_s */ \
X(TICB, "ticb", &&do_ticb), /* 55: c3_b */ \
X(TICS, "tics", &&do_tics), /* 56: c3_s */ \
/* nock 12: scry (only defined in arvo, not in base nock spec) */ \
X(WILS, "wils", &&do_wils), /* 57 */ \
X(WISH, "wish", &&do_wish), /* 58 */ \
/* nock 11: hint processing */ \
X(BUSH, "bush", &&do_bush), /* 59: c3_b */ \
X(SUSH, "sush", &&do_sush), /* 60: c3_s */ \
X(DROP, "drop", &&do_drop), /* 61 */ \
X(HECK, "heck", &&do_heck), /* 62 */ \
X(SLOG, "slog", &&do_slog), /* 63 */ \
/* nock 11: fast (keep) */ \
X(BAST, "bast", &&do_bast), /* 64: c3_b */ \
X(SAST, "sast", &&do_sast), /* 65: c3_s */ \
/* nock 11: fast (lose) */ \
X(BALT, "balt", &&do_balt), /* 66: c3_b */ \
X(SALT, "salt", &&do_salt), /* 67: c3_s */ \
/* nock 11: memo (keep) */ \
X(SKIB, "skib", &&do_skib), /* 68: c3_b */ \
X(SKIS, "skis", &&do_skis), /* 69: c3_s */ \
/* nock 11: memo (lose) */ \
X(SLIB, "slib", &&do_slib), /* 70: c3_b */ \
X(SLIS, "slis", &&do_slis), /* 71: c3_s */ \
X(SAVE, "save", &&do_save), /* 72 */ \
/* nock 11: before formula */ \
X(HILB, "hilb", &&do_hilb), /* 73: atomic, byte */ \
X(HILS, "hils", &&do_hils), /* 74: atomic, short */ \
X(HINB, "hinb", &&do_hinb), /* 75: arbitrary, byte */ \
X(HINS, "hins", &&do_hins), /* 76: arbitrary, short */ \
/* nock 11: after formula */ \
X(HILK, "hilk", &&do_hilk), /* 77: atomic, keep */ \
X(HILL, "hill", &&do_hill), /* 78: atomic, lose */ \
X(HINK, "hink", &&do_hink), /* 79: arbitrary, keep */ \
X(HINL, "hinl", &&do_hinl), /* 80: arbitrary, lose */ \
/* nock 10 */ \
X(MUTH, "muth", &&do_muth), /* 81 */ \
X(KUTH, "kuth", &&do_kuth), /* 82 */ \
X(MUTT, "mutt", &&do_mutt), /* 83 */ \
X(KUTT, "kutt", &&do_kutt), /* 84 */ \
X(MUSM, "musm", &&do_musm), /* 85 */ \
X(KUSM, "kusm", &&do_kusm), /* 86 */ \
X(MUTB, "mutb", &&do_mutb), /* 87: c3_b */ \
X(MUTS, "muts", &&do_muts), /* 88: c3_s */ \
X(MITB, "mitb", &&do_mitb), /* 89: c3_b */ \
X(MITS, "mits", &&do_mits), /* 90: c3_s */ \
X(KUTB, "kutb", &&do_kutb), /* 91: c3_b */ \
X(KUTS, "kuts", &&do_kuts), /* 92: c3_s */ \
X(KITB, "kitb", &&do_kitb), /* 93: c3_b */ \
X(KITS, "kits", &&do_kits), /* 94: c3_s */ \
X(LAST, NULL, NULL), /* 95 */
// Opcodes. Define X to select the enum name from OPCODES.
#define X(opcode, name, indirect_jump) opcode
enum { OPCODES };
#undef X
/* _n_arg(): return the size (in bytes) of an opcode's argument
*/
static inline c3_y
_n_arg(c3_y cod_y)
{
switch ( cod_y ) {
case FABK: case FABL: case FIBL: case FIBK:
case LILB: case LITB: case LIBL: case LIBK:
case SAMB: case SANB: case SBIP: case SBIN:
case SLIB: case SKIB: case KICB: case TICB:
case BUSH: case BAST: case BALT:
case MUTB: case KUTB: case MITB: case KITB:
case HILB: case HINB:
return sizeof(c3_y);
case FASK: case FASL: case FISL: case FISK:
case LILS: case LITS: case LISL: case LISK:
case SAMS: case SANS: case SIPS: case SINS:
case SLIS: case SKIS: case KICS: case TICS:
case SUSH: case SAST: case SALT:
case MUTS: case KUTS: case MITS: case KITS:
case HILS: case HINS:
return sizeof(c3_s);
case SWIP: case SWIN:
return sizeof(c3_l);
default:
u3_assert( cod_y < LAST );
return 0;
}
}
/* _n_melt(): measure space for list of ops (from _n_comp) */
static u3_noun
_n_melt(u3_noun ops, c3_w* byc_w, c3_w* cal_w,
c3_w* reg_w, c3_w* lit_w, c3_w* mem_w)
{
c3_w len_w = u3qb_lent(ops),
i_w = len_w - 1,
a_w;
c3_y cod_y;
c3_y* siz_y = u3a_malloc(len_w);
u3_noun op, sip = u3_nul;
while ( u3_nul != ops ) {
op = u3h(ops);
if ( c3n == u3du(op) ) {
switch ( op ) {
default:
siz_y[i_w] = 1;
break;
case BAST: case BALT:
a_w = (*reg_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 registration sites.\r\n");
u3_assert(0);
}
break;
}
}
else {
cod_y = u3h(op);
switch ( cod_y ) {
default:
siz_y[i_w] = 1 + _n_arg(cod_y);
break;
case SBIP: case SBIN: {
c3_l tot_l = 0,
sip_l = u3t(op);
c3_w j_w, k_w = i_w;
for ( j_w = 0; j_w < sip_l; ++j_w ) {
tot_l += siz_y[++k_w];
}
sip = u3nc(tot_l, sip);
siz_y[i_w] = tot_l <= 0xFF ? 2 : tot_l <= 0xFFFF ? 3 : 5;
break;
}
case SKIB: case SLIB: {
c3_l tot_l = 0,
sip_l = u3h(u3t(u3t(op)));
c3_w j_w, k_w = i_w;
for ( j_w = 0; j_w < sip_l; ++j_w ) {
tot_l += siz_y[++k_w];
}
sip = u3nc(tot_l, sip);
a_w = (*mem_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 memos.\r\n");
u3_assert(0);
}
break;
}
case SIPS: case SINS: case SWIP: case SWIN:
case SAST: case SALT: case KICS: case TICS:
case FISK: case FISL: case SUSH: case SANS:
case LISL: case LISK: case SKIS: case SLIS:
case HILS: case HINS:
u3_assert(0); //overflows
break;
case KICB: case TICB:
a_w = (*cal_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 call sites.\r\n");
u3_assert(0);
}
break;
case BUSH: case FIBK: case FIBL:
case SANB: case LIBL: case LIBK:
case KITB: case MITB:
case HILB: case HINB:
a_w = (*lit_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 literals.\r\n");
u3_assert(0);
}
break;
}
}
*(byc_w) += siz_y[i_w--];
ops = u3t(ops);
}
u3a_free(siz_y);
return u3kb_flop(sip);
}
/* _n_prog_dat(): return pointer to program's data segment
*/
static void*
_n_prog_dat(u3n_prog* pog_u)
{
return ((void*) pog_u) + sizeof(u3n_prog);
}
/* _n_prog_new(): allocate and set up pointers for u3n_prog
*/
static u3n_prog*
_n_prog_new(c3_w byc_w, c3_w cal_w,
c3_w reg_w, c3_w lit_w, c3_w mem_w)
{
c3_w cab_w = (sizeof(u3j_site) * cal_w),
reb_w = (sizeof(u3j_rite) * reg_w),
lib_w = (sizeof(u3_noun) * lit_w),
meb_w = (sizeof(u3n_memo) * mem_w),
dat_w = byc_w + cab_w + reb_w + lib_w + meb_w;
u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w);
pog_u->byc_u.own_o = c3y;
pog_u->byc_u.len_w = byc_w;
pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u);
pog_u->lit_u.len_w = lit_w;
pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w);
pog_u->mem_u.len_w = mem_w;
pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w);
pog_u->cal_u.len_w = cal_w;
pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w);
pog_u->reg_u.len_w = reg_w;
pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w);
return pog_u;
}
/* _n_prog_old(): as _n_prog_new(),
* but leech off senior program's data segment
*/
static u3n_prog*
_n_prog_old(u3n_prog* sep_u)
{
c3_w cab_w = sizeof(u3j_site) * sep_u->cal_u.len_w,
reb_w = sizeof(u3j_rite) * sep_u->reg_u.len_w,
lib_w = sizeof(u3_noun) * sep_u->lit_u.len_w,
meb_w = sizeof(u3n_memo) * sep_u->mem_u.len_w,
dat_w = cab_w + reb_w + lib_w + meb_w;
u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w);
pog_u->byc_u.own_o = c3n;
pog_u->byc_u.len_w = sep_u->byc_u.len_w;
pog_u->byc_u.ops_y = sep_u->byc_u.ops_y;
pog_u->lit_u.len_w = sep_u->lit_u.len_w;
pog_u->lit_u.non = (u3_noun*) _n_prog_dat(pog_u);
pog_u->mem_u.len_w = sep_u->mem_u.len_w;
pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w);
pog_u->cal_u.len_w = sep_u->cal_u.len_w;
pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w);
pog_u->reg_u.len_w = sep_u->reg_u.len_w;
pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w);
memcpy(pog_u->lit_u.non, sep_u->lit_u.non, dat_w);
return pog_u;
}
/* _n_prog_asm_inx(): write an index to the bytestream with overflow
*/
static void
_n_prog_asm_inx(c3_y* buf_y, c3_w* i_w, c3_s inx_s, c3_y cod)
{
if ( inx_s <= 0xFF ) {
buf_y[(*i_w)--] = (c3_y) (inx_s);
buf_y[*i_w] = (c3_y) cod;
}
else {
buf_y[(*i_w)--] = (c3_y) (inx_s >> 8);
buf_y[(*i_w)--] = (c3_y) (inx_s);
// the short-index versions of these opcodes must immediately
// follow the byte-index versions because of this convention
buf_y[(*i_w)] = cod + 1;
}
}
/* _n_prog_asm(): assemble list of ops (from _n_comp) into u3n_prog
*/
static void
_n_prog_asm(u3_noun ops, u3n_prog* pog_u, u3_noun sip)
{
u3_noun top = ops;
c3_y* buf_y = pog_u->byc_u.ops_y;
c3_s lit_s = 0,
cal_s = 0,
mem_s = 0,
reg_s = 0;
c3_w i_w = pog_u->byc_u.len_w-1;
buf_y[i_w] = HALT;
while ( i_w-- > 0 ) {
u3_noun op = u3h(ops);
if ( c3y == u3ud(op) ) {
switch ( op ) {
default:
buf_y[i_w] = (c3_y) op;
break;
/* registration site index args */
case BAST: case BALT: {
_n_prog_asm_inx(buf_y, &i_w, reg_s, op);
u3j_rite* rit_u = &(pog_u->reg_u.rit_u[reg_s++]);
rit_u->own_o = c3n;
rit_u->clu = u3_none;
rit_u->fin_p = 0;
break;
}
}
}
else {
u3_noun cod = u3h(op);
switch ( cod ) {
default:
u3_assert(0);
return;
/* memo index args */
case SKIB: case SLIB: {
u3n_memo* mem_u;
c3_l sip_l = u3h(sip);
u3_noun tmp = sip;
sip = u3k(u3t(sip));
u3z(tmp);
_n_prog_asm_inx(buf_y, &i_w, mem_s, cod);
mem_u = &(pog_u->mem_u.sot_u[mem_s++]);
mem_u->sip_l = sip_l;
// [op_y, cid, mem_w, nef]
mem_u->key = u3k(u3t(u3t(u3t(op))));
mem_u->cid = u3h(u3t(op));
break;
}
/* skips */
case SBIP: case SBIN: {
c3_l sip_l = u3h(sip);
u3_noun tmp = sip;
sip = u3k(u3t(sip));
u3z(tmp);
if ( sip_l <= 0xFF ) {
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod;
}
else if ( sip_l <= 0xFFFF ) {
buf_y[i_w--] = (c3_y) (sip_l >> 8);
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod + 1;
}
else {
buf_y[i_w--] = (c3_y) (sip_l >> 24);
buf_y[i_w--] = (c3_y) (sip_l >> 16);
buf_y[i_w--] = (c3_y) (sip_l >> 8);
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod + 2;
}
break;
}
/* 8-bit direct args */
case FABK: case FABL:
case LITB: case LILB:
case MUTB: case KUTB:
case SAMB:
buf_y[i_w--] = (c3_y) u3t(op);
buf_y[i_w] = (c3_y) cod;
break;
/* 16-bit direct args */
case FASK: case FASL:
case LILS: case LITS:
case MUTS: case KUTS:
case SAMS: case SIPS: case SINS: {
c3_s off_s = u3t(op);
buf_y[i_w--] = (c3_y) (off_s >> 8);
buf_y[i_w--] = (c3_y) off_s;
buf_y[i_w] = (c3_y) cod;
break;
}
/* 31-bit direct args */
case SWIP: case SWIN: {
c3_w off_l = u3t(op);
buf_y[i_w--] = (c3_y) (off_l >> 24);
buf_y[i_w--] = (c3_y) (off_l >> 16);
buf_y[i_w--] = (c3_y) (off_l >> 8);
buf_y[i_w--] = (c3_y) off_l;
buf_y[i_w] = (c3_y) cod;
break;
}
/* literal index args */
case FIBK: case FIBL:
case LIBK: case LIBL:
case BUSH: case SANB:
case KITB: case MITB:
case HILB: case HINB:
_n_prog_asm_inx(buf_y, &i_w, lit_s, cod);
pog_u->lit_u.non[lit_s++] = u3k(u3t(op));
break;
/* call site index args */
case TICB: case KICB: {
_n_prog_asm_inx(buf_y, &i_w, cal_s, cod);
u3j_site* sit_u = &(pog_u->cal_u.sit_u[cal_s++]);
sit_u->axe = u3k(u3t(op));
sit_u->pog_p = 0;
sit_u->bat = u3_none;
sit_u->bas = u3_none;
sit_u->loc = u3_none;
sit_u->lab = u3_none;
sit_u->jet_o = c3n;
sit_u->fon_o = c3n;
sit_u->cop_u = NULL;
sit_u->ham_u = NULL;
sit_u->fin_p = 0;
break;
}
}
}
ops = u3t(ops);
}
u3z(top);
// this assert will fail if we overflow a c3_w worth of instructions
u3_assert(u3_nul == ops);
// this is just a sanity check
u3_assert(u3_nul == sip);
}
/* _n_prog_from_ops(): new program from _n_comp() product
*/
static u3n_prog*
_n_prog_from_ops(u3_noun ops)
{
u3_noun sip;
u3n_prog* pog_u;
c3_w byc_w = 1, // HALT
cal_w = 0,
reg_w = 0,
lit_w = 0,
mem_w = 0;
sip = _n_melt(ops, &byc_w, &cal_w, ®_w, &lit_w, &mem_w);
pog_u = _n_prog_new(byc_w, cal_w, reg_w, lit_w, mem_w);
_n_prog_asm(ops, pog_u, sip);
return pog_u;
}
#if 0
/* _n_print_stack(): print out the cap stack up to a designated "empty"
* used only for debugging
*/
static void _n_print_stack(u3p(u3_noun) empty) {
c3_w cur_p = u3R->cap_p;
fprintf(stderr, "[");
int first = 1;
while ( cur_p != empty ) {
if ( first ) {
first = 0;
}
else {
fprintf(stderr, " ");
}
if ( c3y == u3a_is_north(u3R) ) {
fprintf(stderr, "%u", *(u3to(u3_noun, cur_p)));
cur_p++;
}
else {
fprintf(stderr, "%u", *(u3to(u3_noun, cur_p-1)));
cur_p--;
}
}
fprintf(stderr, "]\r\n");
}
#endif
// Define X to select the opcode string representation from OPCODES.
# define X(opcode, name, indirect_jump) name
static c3_c* opcode_names[] = { OPCODES };
# undef X
/* _n_apen(): emit the instructions contained in src to dst
*/
static inline void
_n_apen(u3_noun* dst, u3_noun src)
{
*dst = u3kb_weld(src, *dst);
}
/* _n_emit(): emit a single instruction to ops
*/
static inline void
_n_emit(u3_noun *ops, u3_noun op)
{
*ops = u3nc(op, *ops);
}