-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathins3.mud
More file actions
1128 lines (982 loc) · 33.9 KB
/
Copy pathins3.mud
File metadata and controls
1128 lines (982 loc) · 33.9 KB
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
/*
* Copyright (c) 1993-2012 David Gay
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
* THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY HAVE BEEN ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND DAVID
* GAY HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
// 3-address (not really) instructions for intermediate representation
library ins3
requires compiler, dlist, misc, sequences, vars
defines
mc:i_class, mc:i_compute, mc:i_aop, mc:i_adest, mc:i_aargs, mc:i_atypes,
mc:i_asizeinfo,
mc:i_branch, mc:i_bop, mc:i_bdest, mc:i_bargs, mc:i_btypes, mc:i_trap,
mc:i_top, mc:i_tdest, mc:i_targs, mc:i_ttypes, mc:i_memory, mc:i_mop,
mc:i_marray, mc:i_mindex, mc:i_mscalar, mc:i_closure, mc:i_fdest,
mc:i_ffunction,
mc:i_call, mc:i_cdest, mc:i_cargs, mc:i_cfunction, mc:i_ctypes,
mc:i_csizeinfo,
mc:i_return, mc:i_rvalue, mc:i_rtype, mc:i_vref, mc:i_vdest, mc:i_varg,
mc:branch_never, mc:branch_always, mc:branch_true, mc:branch_false,
mc:branch_or, mc:branch_nor, mc:branch_and, mc:branch_nand, mc:branch_bitand,
mc:branch_nbitand, mc:branch_eq, mc:branch_ne, mc:branch_lt, mc:branch_ge,
mc:branch_le, mc:branch_gt, mc:branch_slength, mc:branch_vlength,
mc:branch_bitset, mc:branch_bitclear, mc:branch_equal, mc:branch_nequal,
mc:branch_immutable, mc:branch_mutable, mc:branch_readonly,
mc:branch_writable, mc:branch_any_prim, mc:branch_not_prim, mc:branch_type?,
mc:branch_ntype?,
mc:trap_never, mc:trap_always, mc:trap_argcheck, mc:trap_loop, mc:trap_type,
mc:trap_typeset, mc:trap_global_write, mc:trap_global_read,
mc:memory_read, mc:memory_write, mc:memory_write_safe,
mc:il_label, mc:il_ins, mc:il_node, mc:il_number, mc:il_loc,
mc:il_defined_var, mc:il_arguments, mc:il_live_in, mc:il_live_out,
mc:l_ilist, mc:l_alias, mc:l_number, mc:l_mclabel,
mc:itype_typeset, mc:itypemap, mc:itypemap_inverse,
itype_none, itype_function, itype_integer, itype_string, itype_vector,
itype_null, itype_symbol, itype_table, itype_pair, itype_other, itype_any,
itype_float, itype_bigint, itype_reference, itype_non_zero, itype_zero,
itype_list, itype_names,
mc:stype_typesets, mc:type_typeset,
mc:types_from_typeset, mc:itypeset_from_typeset,
mc:new_fncode, mc:set_instruction, mc:get_instructions, mc:remove_branches,
mc:remove_aliases, mc:remove_labels, mc:remove_var_aliases, mc:new_local,
mc:ins_return, mc:ins_trap, mc:ins_branch, mc:make_compute_ins,
mc:ins_compute, mc:ins_closure, mc:ins_vref, mc:ins_call, mc:ins_memory,
mc:make_memory_ins, mc:ins_assign,
mc:new_label, mc:ins_label, mc:set_label,
mc:start_block, mc:end_block, mc:exit_block, mc:defined_var,
mc:closure_vars, mc:arguments, mc:set_closure_vars!, mc:barguments,
mc:replace_dest, mc:replace_args, mc:ins_list, mc:ins_list1, mc:print_ins,
mc:slabel, mc:fncode_fn, mc:new_varset, mc:set_vars!, mc:call_escapes?,
mc:my_protected_global?, mc:make_trap_ins,
mc:skip_label_alias,
mc:reset_ins_count
reads mc:this_module
[
// x := y op z, x := op y, x := y
// if x op y goto l, if op x goto l, goto l
// if x op y trap t, if op x trap t, trap t
// x[i] := y, y := x[i] (these are not the mudlle [] operator)
// x := closure y, z1, ..., zn
// x := call y, z1, z2, ..., zn
// return x
// All values in variables are mudlle values -> no data size considerations
// Intermediate code rep. Each instruction is a vector:
mc:i_class = 0; // Class of instruction:
// mc:i_compute, mc:i_branch, mc:i_memory,
// mc:i_closure, mc:i_call, mc:i_trap, mc:i_return
mc:i_compute = 0; // no side effects in the actual op
mc:i_aop = 1; // operator, determines # of args
mc:i_adest = 2; // destination variable
mc:i_aargs = 3; // arguments (list of var)
mc:i_atypes = 4; // types of arguments (list of itype) or false
mc:i_asizeinfo = 5; // known min. size of ref argument, or null
mc:i_branch = 1;
mc:i_bop = 1; // branch operator
mc:i_bdest = 2; // destination label
mc:i_bargs = 3; // arguments (list of var)
mc:i_btypes = 4; // types of arguments (list of itype)
mc:i_trap = 5; // runtime error checks
mc:i_top = 1; // trap operator
mc:i_tdest = 2; // destination trap
mc:i_targs = 3; // arguments (list of var)
mc:i_ttypes = 4; // types of arguments (list of itype)
mc:i_memory = 2;
mc:i_mop = 1; // read or write
mc:i_marray = 2; // the array variable
mc:i_mindex = 3; // the index (int)
mc:i_mscalar = 4; // source or destination variable
mc:i_closure = 3;
mc:i_fdest = 1; // where to place the closure
mc:i_ffunction = 2; // the actual function (component)
mc:i_call = 4;
mc:i_cdest = 1; // function result (var)
mc:i_cargs = 2; // arguments, first is function (list of var)
mc:i_cfunction = 3; // an mc:c_closure (for module locals), or null
// x0 . x1 for apply-like function x0 calling x1
mc:i_ctypes = 4; // itype_xxx bitfield list, or false
mc:i_csizeinfo = 5; // known min. size of set! argument, or null
mc:i_return = 6;
mc:i_rvalue = 1;
mc:i_rtype = 2;
mc:i_vref = 7;
mc:i_vdest = 1; // result variable
mc:i_varg = 2; // source variable
// branch ops
mc:branch_never = 0; // simplifies constant folding
mc:branch_always = 1;
mc:branch_true = 2;
mc:branch_false = 3;
mc:branch_or = 4;
mc:branch_nor = 5;
mc:branch_and = 6;
mc:branch_nand = 7;
mc:branch_bitand = 8;
mc:branch_nbitand = 9;
mc:branch_bitset = 10;
mc:branch_bitclear = 11;
mc:branch_eq = 12;
mc:branch_ne = 13;
mc:branch_lt = 14; // lt, ge, le, gt toggle meaning by
mc:branch_ge = 15; // flipping the lowest bit
mc:branch_le = 16;
mc:branch_gt = 17;
mc:branch_slength = 18; // eq, ne, ...
mc:branch_vlength = 24; // eq, ne, ...
mc:branch_equal = 30;
mc:branch_nequal = 31;
mc:branch_immutable = 32;
mc:branch_mutable = 33;
mc:branch_readonly = 34;
mc:branch_writable = 35;
mc:branch_any_prim = 36;
mc:branch_not_prim = 37;
mc:branch_type? = 38;
mc:branch_ntype? = mc:branch_type? + last_synthetic_type;
| branch_names |
branch_names = '[
"never" "always" "true" "false" "or" "nor" "and" "nand" "bitand" "!bitand"
"bitset?" "bitclear?" "==" "!=" "<" ">=" "<=" ">"
"slength ==" "slength !=" "slength <" "slength >=" "slength <=" "slength >"
"vlength ==" "vlength !=" "vlength <" "vlength >=" "vlength <=" "vlength >"
"equal?" "!equal?" "immutable?"
"!immutable?" "readonly?" "!readonly?" "any_primitive?"
"!any_primitive?"];
assert(vlength(branch_names) == mc:branch_type?);
// traps
mc:trap_never = 0; // simplifies constant folding
mc:trap_always = 1;
mc:trap_argcheck = 2;
mc:trap_loop = 3; // check for infinite loops (implicit at
// function entry)
mc:trap_type = 4; // arg1 is value, arg2 is type (constant)
mc:trap_typeset = 5; // arg1 is value, arg2 is typeset (constant)
mc:trap_global_write = 6; // check that arg1 is not readonly (arg1 is global)
// and that the calling code is allowed to write it
mc:trap_global_read = 7; // check that reading global arg1 is allowed
| trap_names |
trap_names = '["never" 0 "argcheck" "loop" "!type" "!typeset"
"!writable" "!readable"];
// memory ops
mc:memory_read = 1;
mc:memory_write = 2;
mc:memory_write_safe = 3; // write with check for read-only
// An instruction list is a list of the following vectors:
mc:il_label = 0; // label of this instruction, or false
mc:il_ins = 1; // the actual instruction
mc:il_node = 2; // the basic block to which this instruction belongs
mc:il_number = 3; // a unique number (for display)
mc:il_loc = 4; // line number of this instruction
mc:il_defined_var = 5; // number of variable defined here (or false)
mc:il_arguments = 6; // varset of arguments used
mc:il_live_in = 5; // data-flow information
mc:il_live_out = 6;
// labels:
mc:l_ilist = 0; // instruction list pointed to
mc:l_alias = 1; // we are an alias to this label
mc:l_number = 2; // unique number (for display)
mc:l_mclabel = 3; // corresponding machine code label
itype_none = 0; // no type
itype_function = 1;
itype_non_zero = 2;
itype_string = 4;
itype_vector = 8;
itype_null = 16;
itype_symbol = 32;
itype_table = 64;
itype_pair = 128;
itype_bigint = 256;
itype_float = 512;
itype_reference = 1024;
itype_other = 2048;
itype_zero = 4096;
itype_integer = itype_non_zero | itype_zero;
itype_list = itype_null | itype_pair;
itype_any = 8191; // "any" type
itype_names = '[
"function" "non-zero integer" "string" "vector" "null"
"symbol" "table" "pair" "bigint" "float" "reference" "\"other\"" "false"
];
assert((1 << vlength(itype_names)) - 1 == itype_any);
// map from itype_xxx bit -> type_xxx bitset
mc:itype_typeset =
'[// itype_function
,((1 << type_closure) | (1 << type_primitive) | (1 << type_varargs)
| (1 << type_secure))
,(1 << type_integer) // non-zero
,(1 << type_string)
,(1 << type_vector)
,(1 << type_null)
,(1 << type_symbol)
,(1 << type_table)
,(1 << type_pair)
,(1 << type_bigint)
,(1 << type_float)
,(1 << type_reference)
// itype_other
,((1 << type_code) | (1 << type_variable) | (1 << type_internal)
| (1 << type_private) | (1 << type_object) | (1 << type_character)
| (1 << type_gone) | (1 << type_oport) | (1 << type_mcode))
,(1 << type_integer) // zero
];
assert(vlength(mc:itype_typeset) == vlength(itype_names));
mc:itypemap = sequence // map from type_xxx/stype_xxx -> itype typesets
(itype_other, // type_code
itype_function, // type_closure
itype_other, // type_variable
itype_other, // type_internal
itype_function, // type_primitive
itype_function, // type_varargs
itype_function, // type_secure
itype_integer, // type_integer
itype_string, // type_string
itype_vector, // type_vector
itype_pair, // type_pair
itype_symbol, // type_symbol
itype_table, // type_table
itype_other, // type_private
itype_other, // type_object
itype_other, // type_character
itype_other, // type_gone
itype_other, // type_oport
itype_other, // type_mcode
itype_float, // type_float
itype_bigint, // type_bigint
itype_reference, // type_reference
itype_null, // type_null
itype_none, // stype_none
itype_any, // stype_any
itype_function, // stype_function
itype_list); // stype_list
assert(vlength(mc:itypemap) == last_synthetic_type);
vforeachi(fn (i, ts) [
| it |
it = 1 << i;
for (| t | t = 0; ts; [ ts >>= 1; ++t ])
if (ts & 1)
assert(mc:itypemap[t] & it);
], mc:itype_typeset);
mc:itypemap_inverse = sequence // map from type_xxx/stype_xxx -> itype typesets
(itype_any, // type_code
itype_any, // type_closure
itype_any, // type_variable
itype_any, // type_internal
itype_any, // type_primitive
itype_any, // type_varargs
itype_any, // type_secure
itype_any & ~itype_integer, // type_integer
itype_any & ~itype_string, // type_string
itype_any & ~itype_vector, // type_vector
itype_any & ~itype_pair, // type_pair
itype_any & ~itype_symbol, // type_symbol
itype_any & ~itype_table, // type_table
itype_any, // type_private
itype_any, // type_object
itype_any, // type_character
itype_any, // type_gone
itype_any, // type_oport
itype_any, // type_mcode
itype_any & ~itype_float, // type_float
itype_any & ~itype_bigint, // type_bigint
itype_any & ~itype_reference,// type_reference
itype_any & ~itype_null, // type_null
itype_any, // stype_none
itype_none, // stype_any
itype_any & ~itype_function, // stype_function
itype_any & ~itype_list); // stype_list
assert(vlength(mc:itypemap) == last_synthetic_type);
mc:stype_typesets =
'[ 0 // stype_none
,typeset_any // stype_any
,typeset_function // stype_function
,typeset_list ]; // stype_list
assert(vlength(mc:stype_typesets) == last_synthetic_type - last_type);
mc:type_typeset = fn (int type)
if (type >= last_type)
mc:stype_typesets[type - last_type]
else
1 << type;
// save on frequent allocations
| static_type_pairs |
static_type_pairs = make_vector(last_synthetic_type);
for (|t| t = 0; t < last_synthetic_type; ++t)
static_type_pairs[t] = '(,t);
protect(static_type_pairs);
mc:types_from_typeset = list fn (int typeset)
[
if (typeset == typeset_any)
exit<function> static_type_pairs[stype_any];
if (typeset == 0)
exit<function> static_type_pairs[stype_none];
| r, t |
t = last_synthetic_type;
loop
[
--t;
| ts |
ts = mc:type_typeset(t);
if (ts != 0 && ((typeset & ts) == ts))
[
typeset &= ~ts;
if (r == null)
r = static_type_pairs[t]
else
r = t . r;
if (typeset == 0)
exit r;
]
]
];
mc:itypeset_from_typeset = fn (ts)
[
| is, types |
types = mc:types_from_typeset(ts);
is = 0;
loop
[
if (types == null) exit is;
is |= mc:itypemap[car(types)];
types = cdr(types)
]
];
[
| label_index, ins_index, add_ins, print_if, print_op, is_temp? |
mc:new_fncode = fn "component -> fncode. Returns a structure to use for\n\
generating instructions for function component" (top)
[
// ilpos, nextlabel, top, blockstack
vector(null, false, top, null)
];
mc:fncode_fn = fn "fncode -> component. Return compoenent we are generating for" (fcode) fcode[2];
mc:set_instruction = fn "fncode ilist -> . Sets the current instruction insert position to ilist" (fcode, pos)
[
if (fcode[1] || fcode[3] != null) fail();
fcode[0] = pos;
];
mc:get_instructions = fn "fncode -> ilist. Returns instruction list of ilist" (fcode)
[
if (fcode[1] || fcode[3] != null) fail();
fcode[0]
];
mc:remove_branches = fn (ilist)
// Types: ilist : instruction list
// Returns: the list of instructions of fcode, with
// branches to the next instruction destroyed.
[
| old |
old = ilist;
loop
[
| ins, iold, next, label, nextins |
next = dnext(old);
if (next == ilist) exit ilist;
iold = dget(old);
ins = iold[mc:il_ins];
// remove branch to next instruction
if (ins[mc:i_class] == mc:i_branch)
[
label = mc:skip_label_alias(ins[mc:i_bdest]);
nextins = dget(next);
if (label[mc:l_ilist] == nextins)
[
// did removed instruction have a label ?
if (label = iold[mc:il_label])
mc:set_label(label, nextins);
dremove!(old, old);
];
];
old = next
]
];
mc:remove_aliases = fn (ilist)
// Types: ilist: list of instructions
// Requires: mc:remove_labels be called on ilist just after
// The split is present to allow use even when the code of the
// function is split between the flow graph nodes.
// Returns: the list of instructions of fcode, with aliases removed.
[
| scan |
// first remove all aliases
scan = ilist;
loop
[
| ins, label |
ins = dget(scan)[mc:il_ins];
if (ins[mc:i_class] == mc:i_branch)
[
// branch removal can create more than one depth of aliasing
label = mc:skip_label_alias(ins[mc:i_bdest]);
ins[mc:i_bdest] = label;
// label is used
label[mc:l_alias] = true;
];
scan = dnext(scan);
if (scan == ilist) exit ilist
]
];
mc:remove_labels = fn (ilist)
// Types: ilist: list of instructions
// Requires: mc:remove_aliases be called on ilist just before
// Effects: removes useless labels (is this really necessary here?)
[
| scan |
scan = ilist;
loop
[
| label, il |
il = dget(scan);
if (label = il[mc:il_label])
if (!label[mc:l_alias]) // unused
il[mc:il_label] = false
else
label[mc:l_alias] = false; // reset to usual state
scan = dnext(scan);
if (scan == ilist) exit ilist
]
];
mc:remove_var_aliases = fn (ilist)
// Types: ilist: list of instructions
// Returns: the list of instructions of fcode, with variable aliases removed.
[
| scan, rep, replist |
rep = mc:alias_base;
replist = fn (s) lmap!(rep, s);
scan = ilist;
loop
[
| ins, class |
ins = dget(scan)[mc:il_ins];
class = ins[mc:i_class];
if (class == mc:i_compute)
[
ins[mc:i_adest] = rep(ins[mc:i_adest]);
replist(ins[mc:i_aargs])
]
else if (class == mc:i_branch)
replist(ins[mc:i_bargs])
else if (class == mc:i_trap)
replist(ins[mc:i_targs])
else if (class == mc:i_memory)
[
ins[mc:i_marray] = rep(ins[mc:i_marray]);
ins[mc:i_mscalar] = rep(ins[mc:i_mscalar])
]
else if (class == mc:i_call)
[
ins[mc:i_fdest] = rep(ins[mc:i_fdest]);
replist(ins[mc:i_cargs])
]
else if (class == mc:i_closure)
ins[mc:i_cdest] = rep(ins[mc:i_cdest])
else if (class == mc:i_vref)
ins[mc:i_vdest] = rep(ins[mc:i_vdest])
else if (class == mc:i_return)
ins[mc:i_rvalue] = rep(ins[mc:i_rvalue])
else
[
dformat("unhandled class %d\n", class);
fail()
];
scan = dnext(scan);
if (scan == ilist) exit ilist
]
];
mc:new_local = fn "fncode -> var. Creates a new local variable for fncode" (fcode)
[
| local |
local = mc:var_make_local("");
//fcode[2][mc:c_flocals] = local . fcode[2][mc:c_flocals];
local
];
is_temp? = fn "var -> b. True if var was created by mc:new_local" (v)
v[mc:v_class] == mc:v_local && string_length(v[mc:v_name]) == 0;
ins_index = 0;
mc:reset_ins_count = fn () ins_index = 0;
add_ins = fn (fcode, ins)
// Types: fcode : fncode
// ins : instruction
// Effects: Adds ins to the instructions in fcode, setting the label
// if necessary.
// Clears the current label
[
| newins |
assert(pair?(mc:get_loc()));
// Add instruction
newins = vector(fcode[1], ins, null, ins_index = ins_index + 1,
mc:get_loc(), false, false);
// This is a strange hack:
// When code is initially generated, fcode[0] starts at null,
// then gets set to the first instruction with subsequent
// instructions inserted before it (and because of the circular
// nature of the list, actually at the end).
// Later, when code needs patching, mc:set_instruction is called
// to set the insertion point, before which new instructions are
// added.
if (fcode[0] == null) fcode[0] = dcons!(newins, null)
else dcons!(newins, fcode[0]); // insert before fcode[0]
// Set label if any
if (fcode[1]) fcode[1][mc:l_ilist] = newins;
fcode[1] = false;
];
mc:ins_return = fn "fncode var -> . Adds 'return var' to fncode" (fcode, v)
[
add_ins(fcode, vector(mc:i_return, v, null));
];
mc:make_trap_ins = fn (op, trap, args)
vector(mc:i_trap, op, trap, args, false);
mc:ins_trap = fn "fncode op n l -> . Adds 'if op(l) trap n' to fncode"
(fcode, op, trap, args)
add_ins(fcode, mc:make_trap_ins(op, trap, args));
mc:ins_branch = fn "fncode op label l -> . Adds 'if op(l) goto label' to fncode"
(fcode, op, label, args)
add_ins(fcode, vector(mc:i_branch, op, label, args, false));
mc:make_compute_ins = fn (op, dest, args)
vector(mc:i_compute, op, dest, args, false, null);
mc:ins_compute = fn "fncode op var l -> . Adds 'var := op(l)' to fncode"
(fcode, op, dest, args)
add_ins(fcode, mc:make_compute_ins(op, dest, args));
mc:ins_closure = fn "fncode var component -> . Adds 'var := closure(component)' to fncode"
(fcode, dest, f)
add_ins(fcode, vector(mc:i_closure, dest, f));
mc:ins_vref = fn "fncode var0 var1 -> Adds 'var0 := vref(var1)'"
(fcode, dest, var)
add_ins(fcode, vector(mc:i_vref, dest, var));
mc:ins_call = fn "fncode var l -> . Adds 'var := call l' to fncode"
(fcode, dest, args)
add_ins(fcode, vector(mc:i_call, dest, args, null, false, null));
mc:make_memory_ins = fn (op, array, index, scalar)
vector(mc:i_memory, op, array, index, scalar);
mc:ins_memory = fn "fncode op var1 n var2-> Adds 'var2 := var1[n] / var1[n] := var2' to fncode"
(fcode, op, array, index, scalar)
add_ins(fcode, mc:make_memory_ins(op, array, index, scalar));
mc:ins_assign = fn "fncode var1 var2 -> . 'var1 := var2', using aliases if possible" (fcode, v1, v2)
[
| v2b |
v2b = mc:alias_base(v2);
if (is_temp?(v2b)) mc:alias(v2b, v1)
else mc:ins_compute(fcode, mc:b_assign, v1, list(v2))
];
// labels
label_index = 0;
mc:new_label = fn "-> label. Returns a new unassigned label" ()
vector(false, false, ++label_index, null);
mc:ins_label = fn "fncode label -> . Makes label point at the next instruction to\n\
be generated in fncode" (fcode, label)
[
if (fcode[1]) label[mc:l_alias] = fcode[1]
else fcode[1] = label;
];
mc:set_label = fn "label ilist -> . Sets label to point to ilist. Might make it an alias of existing label" (vector l, vector il)
[
| lab |
if (lab = il[mc:il_label]) // make it an alias
[
l[mc:l_alias] = lab;
l[mc:l_ilist] = false;
]
else
[
l[mc:l_ilist] = il;
il[mc:il_label] = l;
];
null
];
// labeled blocks
mc:start_block = fn "fncode s -> . Starts a new block called s in fncode" (fcode, name)
fcode[3] = vector(name, mc:new_label(), mc:new_local(fcode)) . fcode[3];
mc:end_block = fn "fncode var1 -> var2. End of block, with value var1. Returns block value var2" (fcode, v)
[
| block |
block = car(fcode[3]);
fcode[3] = cdr(fcode[3]);
if (v) mc:ins_assign(fcode, block[2], v);
mc:ins_label(fcode, block[1]);
block[2]
];
mc:exit_block = fn "fncode s var -> b. Exit block s with result var. Returns false if block unknown" (fcode, name, v)
[
| block |
if (block = lexists?(if (name == null) fn (b) b[0] == null
else fn (b) b[0] != null && string_icmp(b[0], name) == 0,
fcode[3]))
[
mc:ins_assign(fcode, block[2], v);
mc:ins_branch(fcode, mc:branch_always, block[1], null);
true
]
else
false
];
// varsets
mc:new_varset = fn (ifn)
// Returns: A new empty bitset of variables of ifn
new_bitset(ifn[mc:c_fnvars]);
mc:set_vars! = fn (varset, vars)
[
while (vars != null)
[
set_bit!(varset, car(vars)[mc:v_number]);
vars = cdr(vars);
];
varset
];
// instruction examination
mc:defined_var = fn (vector ins)
[
| class |
class = ins[mc:i_class];
if (class == mc:i_compute) ins[mc:i_adest]
else if (class == mc:i_memory &&
ins[mc:i_mop] == mc:memory_read) ins[mc:i_mscalar]
else if (class == mc:i_closure) ins[mc:i_fdest]
else if (class == mc:i_call) ins[mc:i_cdest]
else if (class == mc:i_vref) ins[mc:i_vdest]
else false
];
mc:closure_vars = fn (ins)
lmap(fn (v) v[mc:v_cparent], ins[mc:i_ffunction][mc:c_fclosure]);
mc:arguments = fn (vector ins, ambiguous)
[
| class |
class = ins[mc:i_class];
if (class == mc:i_compute) ins[mc:i_aargs]
else if (class == mc:i_branch) ins[mc:i_bargs]
else if (class == mc:i_trap) ins[mc:i_targs]
else if (class == mc:i_memory)
if (ins[mc:i_mop] == mc:memory_write ||
ins[mc:i_mop] == mc:memory_write_safe)
list(ins[mc:i_marray],
ins[mc:i_mscalar])
else
list(ins[mc:i_marray])
else if (class == mc:i_closure)
mc:closure_vars(ins)
else if (class == mc:i_call)
if (ambiguous == null || !mc:call_escapes?(ins)) ins[mc:i_cargs]
else lappend(ins[mc:i_cargs], ambiguous)
else if (class == mc:i_vref)
list(ins[mc:i_varg])
else if (class == mc:i_return)
ins[mc:i_rvalue] . ambiguous
else fail()
];
mc:set_closure_vars! = fn (ins, rwmask, b)
[
| vars |
vars = ins[mc:i_ffunction][mc:c_fclosure];
while (vars != null)
[
| v |
v = car(vars);
if (rwmask == (mc:closure_read | mc:closure_write) ||
(mc:var_base(v)[mc:v_lclosure_uses] & rwmask))
set_bit!(b, v[mc:v_cparent][mc:v_number]);
vars = cdr(vars);
]
];
mc:my_protected_global? = fn (var)
[
| mod, mname |
(var[mc:v_class] == mc:v_global_define
&& string?(mod = module_vstatus(var[mc:v_goffset]))
&& string?(mname = mc:this_module[mc:m_name])
&& string_cmp(mod, mname) == 0
&& module_status(mod) == module_protected)
];
mc:call_escapes? = fn (vector ins)
// Returns: True if call may escape
[
| fun |
fun = ins[mc:i_cfunction];
if (pair?(fun)) // apply-like?
fun = cdr(fun);
if (vector?(fun))
exit<function> !fun[mc:c_fnoescape];
| escapes? |
escapes? = fn (f, args)
[
| fclass |
fclass = if (mc:my_protected_global?(f))
mc:v_global_constant
else
f[mc:v_class];
if (fclass == mc:v_global_define)
[
| mod |
mod = module_vstatus(f[mc:v_goffset]);
if (string?(mod)
&& equal?(mod, mc:this_module[mc:m_name])
&& module_status(mod) == module_protected)
// pretend this is a global constant
fclass = mc:v_global_constant;
];
if (fclass == mc:v_global_constant)
if (closure?(fun = global_value(f[mc:v_goffset])))
~closure_flags(fun) & clf_noescape
else if (function?(fun))
[
| flags |
flags = primitive_flags(fun);
if (flags & OP_APPLY)
[
| fidx |
@[ _ fidx _] = vexists?(fn (x) x[0] == fun,
mc:apply_functions);
if (llength(args) > fidx)
escapes?(nth(fidx + 1, args), null)
else
true
]
else
~flags & OP_NOESCAPE
]
else
true
else
true
];
escapes?(car(ins[mc:i_cargs]), cdr(ins[mc:i_cargs]))
];
mc:barguments = fn (il, ambiguous)
[
| ins, class, args |
ins = il[mc:il_ins];
class = ins[mc:i_class];
args = il[mc:il_arguments];
if (class == mc:i_call && mc:call_escapes?(ins) ||
class == mc:i_return) bunion(args, ambiguous)
else args
];
// instruction update
mc:replace_dest = fn (vector ins, newdest)
[
| class |
class = ins[mc:i_class];
if (class == mc:i_compute) ins[mc:i_adest] = newdest
else if (class == mc:i_memory &&
ins[mc:i_mop] == mc:memory_read) ins[mc:i_mscalar] = newdest
else if (class == mc:i_closure) ins[mc:i_fdest] = newdest
else if (class == mc:i_vref) ins[mc:i_vdest] = newdest
else if (class == mc:i_call) ins[mc:i_cdest] = newdest
else fail()
];
mc:replace_args = fn (vector ins, replacements)
[
| rep, replist, class |
rep = fn (v)
[
| r |
if (r = assq(v, replacements)) cdr(r)
else v
];
replist = fn (s)
while (s != null)
[
| r |
if (r = assq(car(s), replacements)) set_car!(s, cdr(r));
s = cdr(s);
];
class = ins[mc:i_class];
if (class == mc:i_compute)
replist(ins[mc:i_aargs])
else if (class == mc:i_branch)
replist(ins[mc:i_bargs])
else if (class == mc:i_trap)
replist(ins[mc:i_targs])
else if (class == mc:i_memory)
[
ins[mc:i_marray] = rep(ins[mc:i_marray]);
if (ins[mc:i_mop] == mc:memory_write ||
ins[mc:i_mop] == mc:memory_write_safe)
ins[mc:i_mscalar] = rep(ins[mc:i_mscalar])
]
else if (class == mc:i_call)
replist(ins[mc:i_cargs])
else if (class == mc:i_vref)
ins[mc:i_varg] = rep(ins[mc:i_varg])
else if (class == mc:i_return)
ins[mc:i_rvalue] = rep(ins[mc:i_rvalue])
else
fail()
];
// code display
mc:ins_list = fn "component -> . Prints instruction list of function component" (ifn)
[
| closures |
closures = ifn . null;
while (closures != null)
[
| cl |
cl = car(closures);
closures = cdr(closures);
dformat("closure %s:\n", cl[mc:c_fnumber]);
closures = lappend(closures, mc:ins_list1(cl[mc:c_fvalue]));
]
];
mc:ins_list1 = fn "ilist -> l. Prints instruction list and returns list of its closures" (ilist)
[
| scan, closures |
closures = null;
scan = ilist;
loop
[
| il |
il = dget(scan);
if (il[mc:il_label])
dformat("%s:", mc:slabel(il[mc:il_label]));
| loc |
loc = il[mc:il_loc];
dformat("\t%d:%d\t(%s) ", mc:loc_line(loc), mc:loc_column(loc),
il[mc:il_number]);
| ins |
ins = il[mc:il_ins];
if (ins == null)
display("<removed>")
else
closures = mc:print_ins(il[mc:il_ins], closures);
newline();
scan = dnext(scan);
if (scan == ilist) exit closures
]
];
mc:print_ins = fn (vector ins, closures)
[
| print_sz |
print_sz = fn (sz)
if (integer?(sz) && sz > 0)
dformat(" (size >= %d)", sz);
| class |
class = ins[mc:i_class];