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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
| # translate.tcl --
#
# Tcl bytecode to quadcode conversion code, plus basic (no reasoning
# required) type assertions hooked off that generated quadcode.
#
# Copyright (c) 2014-2015 by Kevin B. Kenny
# Copyright (c) 2015 by Donal K. Fellows
# Copyright (c) 2017 by Kevin B. Kenny
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------
# bytecode-to-quads --
#
# Translates Tcl bytecode to quadruples: three-address instructions.
#
# Parameters:
# bytecodeVar - Name of a dictionary in caller's scope that contains
# a Tcl bytecode sequence as intrpreted by
# tcl::unsupported::getbytecode
#
# Results:
# Returns a dictionary. The dictionary will contain three keys:
# quadcode: The value is a list of three-address instructions
# links: Dictionary whose keys are variable names that may be
# linked by [variable], [upvar], [namespace upavar] and
# whose values are immaterial.
# vars: List of variable names found in the quadcode.
proc bytecode-to-quads {bytecodeVar} {
upvar 1 $bytecodeVar bytecode
variable unreachablewarning
set IMPURE_NUMERIC [::quadcode::dataType::typeUnion \
$::quadcode::dataType::IMPURE \
$::quadcode::dataType::NUMERIC]
set currentline 0
set currentscript {}
set originalscript [dict get $bytecode script]
set quads {}; # List of instructions under construction
set fixup {}; # Dictionary whose keys are jump targets
; # and the values are lists of quad program
; # counters that jump to them, used to fix up
; # forward jumps.
# Construct a header that sets parameters and provides a fake point
# to which live-on-entry variables can be attached.
set vars [lmap x [dict get $bytecode variables] {lindex $x 1}]
quads entry {temp @callframe} [list literal $vars]
set idx 0
foreach v [dict get $bytecode variables] {
if {{arg} in [lindex $v 0]} {
quads param [list var [lindex $v 1]] [list arg $idx]
}
incr idx
}
# Iterate the instruction list
dict for {pc insn} [dict get $bytecode instructions] {
if {![dict exists $bytecode stackState $pc]} {
if {$unreachablewarning} {
puts "warning: unreachable: $pc: $insn"
}
continue
}
lassign [dict get $bytecode stackState $pc] state depth
# Fix up any quads that jump to the current quad
dict set quadindex $pc [llength $quads]
if {[dict exists $fixup $pc]} {
foreach q [dict get $fixup $pc] {
lset quads $q 1 [list pc [llength $quads]]
}
dict unset fixup $pc
}
# Determine if the current source line has changed
set c {}
foreach cr [dict get $bytecode commands] {
if {[dict get $cr codefrom] > $pc} continue
if {[dict get $cr codeto] < $pc} continue
set c $cr
}
if {$c ne ""} {
# Count the number of newlines up to the start of the command.
set line [regexp -all \n \
[string range $originalscript 0 [dict get $c scriptfrom]]]
# Add the location of the first line of the script within its
# file, if that is known.
if {[dict exists $bytecode initiallinenumber]} {
incr line [dict get $bytecode initiallinenumber]
}
# Issue the directive if there has been a change in line number
if {$line != $currentline} {
set currentline $line
quads @debug-line {} [list literal $line]
}
# Issue the directive if there has been a change in script text
if {$currentscript ne [dict get $c script]} {
set currentscript [dict get $c script]
quads @debug-script {} [list literal [dict get $c script]]
}
}
# Translate the current bytecode
switch -exact -- [lindex $insn 0] {
add -
bitand -
bitor -
bitxor -
land -
lor -
lshift -
rshift -
mult -
sub { # Binary ops
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
generate-arith-domain-check [lindex $insn 0] $v0 $v1
quads purify {temp opd0} $v0
quads purify {temp opd1} $v1
quads [lindex $insn 0] $r {temp opd0} {temp opd1}
}
eq -
ge -
gt -
le -
lt -
neq {
# Force string and numeric comparisons along different
# branches. In most cases type checks will be
# optimized away, but string and numeric equality will
# be distinguished in the generated code. The advantage
# is that the typecheck will become loop-invariant and
# subject to code motion.
set t [list temp $depth]
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
set op [lindex $insn 0]
set n [llength $quads]
# are both operands numeric?
quads [list instanceOf $IMPURE_NUMERIC {IMPURE NUMERIC}] $t $v0
; # 0
quads jumpFalse [list pc [expr {$n+8}]] $t; # 1
quads [list instanceOf $IMPURE_NUMERIC {IMPURE NUMERIC}] $t $v1
; # 2
quads jumpFalse [list pc [expr {$n+8}]] $t; # 3
# both operands are numeric
quads purify {temp opd0} $v0; # 4
quads purify {temp opd1} $v1; # 5
quads $op $r {temp opd0} {temp opd1}; # 6
quads jump [list pc [expr {$n+10}]]; # 7
# at least one operand is not numeric
quads strcmp $t $v0 $v1; # 8
quads $op $r $t [list literal 0]; # 9
}
strcmp -
streq -
strfind -
strrfind { # Binary ops that don't need type checks
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
quads [lindex $insn 0] $r $v0 $v1
}
uplus -
uminus -
bitnot { # Unary ops
set value [list temp [incr depth -1]]
set r [list temp $depth]
generate-arith-domain-check [lindex $insn 0] $value
quads purify {temp opd0} $value
set op [lindex $insn 0]
quads $op $r {temp opd0}
}
listNotIn {
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
error-quads listIn $r $v0 $v1
quads not $r $r
}
div - expon - mod {
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
generate-arith-domain-check [lindex $insn 0] $v0 $v1
quads purify {temp opd0} $v0
quads purify {temp opd1} $v1
error-quads [lindex $insn 0] $r {temp opd0} {temp opd1}
}
listIn - strindex {
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
error-quads [lindex $insn 0] $r $v0 $v1
}
regexp {
set flag [list literal [lindex $insn 1]]
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
error-quads regexp $r $flag $v0 $v1
}
lsetFlat {
set old [list temp [incr depth -1]]
set elem [list temp [incr depth -1]]
set indices {}
for {set i 0} {$i < [lindex $insn 1]-2} {incr i} {
lappend indices [list temp [incr depth -1]]
}
set new [list temp $depth]
error-quads listSet $new $old $elem {*}[lreverse $indices]
}
lsetList {
set old [list temp [incr depth -1]]
set elem [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set new [list temp $depth]
error-quads listSet $new $old $elem $idx
}
strmatch {
set flag [list literal [lindex $insn 1]]
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
quads [lindex $insn 0] $r $flag $v0 $v1
}
strneq { # Negated binary ops
set v1 [list temp [incr depth -1]]
set v0 [list temp [incr depth -1]]
set r $v0
quads streq $r $v0 $v1
quads not $r $r
}
strcaseUpper {
set v [list temp [incr depth -1]]
quads strcase $v $v [list literal 0]
}
strcaseLower {
set v [list temp [incr depth -1]]
quads strcase $v $v [list literal 1]
}
strcaseTitle {
set v [list temp [incr depth -1]]
quads strcase $v $v [list literal 2]
}
strclass {
set v [list temp [incr depth -1]]
set classes {
=alnum 0 =alpha 1 =ascii 2 =control 3
=digit 4 =graph 5 =lower 6 =print 7
=punct 8 =space 9 =upper 10 =word 11
=xdigit 12
}
quads strclass $v $v [list literal \
[dict get $classes [lindex $insn 1]]]
}
strrangeImm {
set i1 [string range [lindex $insn 1] 1 end]
set i2 [string range [lindex $insn 2] 1 end]
set v [list temp [incr depth -1]]
error-quads strrange $v $v [list literal $i1] [list literal $i2]
}
strmap {
set v3 [list temp [incr depth -1]]
set v2 [list temp [incr depth -1]]
set v1 [list temp [incr depth -1]]
quads strmap $v1 $v1 $v2 $v3
}
strrange {
set i2 [list temp [incr depth -1]]
set i1 [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
error-quads strrange $v $v $i1 $i2
}
strreplace {
set substr [list temp [incr depth -1]]
set i2 [list temp [incr depth -1]]
set i1 [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
error-quads strreplace $v $v $i1 $i2 $substr
}
strtrim {
set chars [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
quads strtrim $v $v $chars [list literal 0]
}
strtrimLeft {
set chars [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
quads strtrim $v $v $chars [list literal -1]
}
strtrimRight {
set chars [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
quads strtrim $v $v $chars [list literal 1]
}
done { # End of bytecode
set v [list temp [incr depth -1]]
quads return {} {temp @callframe} $v
}
returnStk {
# NOTE! Opposite order to 'returnImm'
set v [list temp [incr depth -1]]
set opt [list temp [incr depth -1]]
set resultVar [list temp $depth]
error-quads initException $resultVar $v $opt
}
break {
generate-jump [exception-target loop break]
}
continue {
generate-jump [exception-target loop continue]
}
returnImm - syntax {
set code [expr [lindex $insn 1]]
set level [expr [lindex $insn 2]]
# NOTE! Opposite order to 'returnStk'
set opt [list temp [incr depth -1]]
set v [list temp [incr depth -1]]
set resultVar [list temp $depth]
# Optimizations
switch $code,$level {
0,0 {
quads copy $resultVar $v
continue
}
3,0 {
# TODO: Is this right?
lappend quads [list setReturnCode {} {literal 3}]
generate-jump [exception-target loop break]
continue
}
4,0 {
# TODO: Is this right?
lappend quads [list setReturnCode {} {literal 4}]
generate-jump [exception-target loop continue]
continue
}
}
quads initException $resultVar $v $opt \
[list literal $code] [list literal $level]
if {$code == 1 && $level == 0} {
# Simplified case: we know this is an error
generate-jump [exception-target catch]
} else {
generate-jump [exception-target catch] maybe $resultVar
quads extractMaybe $resultVar $resultVar
}
}
reverse {
set n [lindex $insn 1]
set d $depth
set tmps [set vs {}]
for {set i 0} {$i < $n} {incr i;incr d} {
lappend vs [list temp [incr depth -1]]
lappend tmps [list temp $d]
quads copy [lindex $tmps end] [lindex $vs end]
}
for {set i 0} {$i < $n} {incr i} {
quads copy [lindex $vs end-$i] [lindex $tmps $i]
}
}
foreach_start {
# Split up like this so that the quadcode engine understands
# all the assignments going on. This is a complex bytecode!
set auxNum [string range [lindex $insn 1] 1 end]
set aux [lindex [dict get $bytecode auxiliary] $auxNum]
if {[dict exists $aux loop]} {
# Workaround for bug in Tcl; wrong aux type issued
# sometimes!
dict set aux jumpOffset [dict get $aux loop]
}
dict with aux {}
set n [expr {$depth - [llength $assign] - 1}]
set lists {}
foreach group $assign {
lappend lists [list temp [incr n]]
}
set res [list temp $depth]
error-quads foreachStart $res [list literal $assign] {*}$lists
foreach list $lists {
quads unshareList $list $list
}
generate-jump [expr {$pc + 5 - $jumpOffset}]
}
foreach_step {
# Split up like this so that the quadcode engine understands
# all the assignments going on. This is a complex bytecode!
set auxNum [lindex $insn 1]
set aux [lindex [dict get $bytecode auxiliary] $auxNum]
if {[dict exists $aux loop]} {
# Workaround for bug in Tcl; wrong aux type issued
# sometimes!
dict set aux jumpOffset [dict get $aux loop]
}
dict with aux {}
set pair [list temp [expr {$depth - 2}]]
set res [list temp $depth]
quads foreachIter $res $pair
set n [expr {$depth - [llength $assign] - 3}]
set lists [lmap group $assign {
list temp [incr n]
}]
set idx [list temp [expr {$depth + 1}]]
foreach varGroup $assign list $lists {
if {[llength $varGroup] == 1} {
quads copy $idx $res
} else {
quads mult $idx $res [list literal [llength $varGroup]]
}
foreach varIndex $varGroup {
set var [index-to-var $varIndex]
quads listIndex $var $list $idx
quads extractMaybe $var $var
quads add $idx $idx [list literal 1]
}
}
quads foreachMayStep $res $pair
quads foreachAdvance $pair $pair
set target [expr {$pc + $jumpOffset}]
generate-jump $target true $res
}
foreach_end {
# No special action needed; just a fancy pop, and that's
# handled by the stack calculation code.
}
lmap_collect {
set auxNum [lindex $insn 1]
set aux [lindex [dict get $bytecode auxiliary] $auxNum]
dict with aux {}
set collector [list temp [expr {$depth - [llength $assign] - 4}]]
set value [list temp [incr depth -1]]
quads listAppend $collector $collector $value
quads extractMaybe $collector $collector
}
dictFirst {
set var [index-to-var [lindex $insn 1]]
set dict [list temp [incr depth -1]]
set value [list temp $depth]
set key [list temp [incr depth]]
set done [list temp [incr depth]]
error-quads dictIterStart $var $dict
quads dictIterKey $key $var
quads dictIterValue $value $var
quads dictIterDone $done $var
}
dictNext {
set var [index-to-var [lindex $insn 1]]
set value [list temp $depth]
set key [list temp [incr depth]]
set done [list temp [incr depth]]
quads dictIterNext $var $var
quads dictIterKey $key $var
quads dictIterValue $value $var
quads dictIterDone $done $var
}
dictUpdateStart {
# Consider doing a special opcode for this, but that's not
# necessary until we get [unset] working right.
set var [index-to-var [lindex $insn 1]]
set auxNum [string range [lindex $insn 2] 1 end]
set aux [lindex [dict get $bytecode auxiliary] $auxNum]
set mid [list temp $depth]
set val [list temp [incr depth -1]]
set idx 0
foreach v [dict get $aux variables] {
set r [index-to-var $v]
error-quads listIndex $mid $val [list literal $idx]
error-quads dictGet $r $var $mid
incr idx
}
}
dictUpdateEnd {
# Consider doing a special opcode for this, but that's not
# necessary until we get [unset] working right.
set var [index-to-var [lindex $insn 1]]
set auxNum [string range [lindex $insn 2] 1 end]
set aux [lindex [dict get $bytecode auxiliary] $auxNum]
set mid [list temp $depth]
set mid2 [list temp [expr {$depth + 1}]]
set val [list temp [incr depth -1]]
set idx 0
quads copy $mid2 $var
foreach v [dict get $aux variables] {
set r [index-to-var $v]
error-quads listIndex $mid $val [list literal $idx]
error-quads dictSet $mid2 $mid2 $r $mid
}
quads copy $var $mid2
}
unsetScalar {
set var [index-to-var [lindex $insn 2]]
quads unset $var
}
dictDone {
# Do nothing; general free will clean up.
}
verifyDict {
set r [list temp [incr depth -1]]
error-quads dictSize $r $r
# The result will be discarded
}
incrScalar1Imm {
set result [list temp $depth]
set var [index-to-var [lindex $insn 1]]
set delta [lindex $insn 2]
quads initIfNotExists $var $var {literal 0}
quads copy $result [list literal $delta]
generate-arith-domain-check incr $var $result
quads purify {temp opd0} $var
quads purify {temp opd1} $result
quads add $var {temp opd0} {temp opd1}
quads copy $result $var
}
incrScalar1 {
set result [list temp $depth]
set val [list temp [incr depth -1]]
set var [index-to-var [lindex $insn 1]]
quads initIfNotExists $var $var {literal 0}
generate-arith-domain-check incr $var $val
quads purify {temp opd0} $var
quads purify {temp opd1} $val
quads add $var {temp opd0} {temp opd1}
quads copy $result $var
}
incrArray1Imm {
set tmp [list temp $depth]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
set delta [list literal [lindex $insn 2]]
quads initIfNotExists $ary $ary {literal {}}
quads copy $tmp $delta
set res $idx
generate-arith-domain-check incr $tmp
quads purify {temp opd2} $tmp
error-quads dictIncr $ary $ary $idx {temp opd2}
error-quads dictGet $res $ary $idx
}
incrArray1 {
set delta [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
quads initIfNotExists $ary $ary {literal {}}
set res $idx
generate-arith-domain-check incr $delta
quads purify {temp opd2} $delta
error-quads dictIncr $ary $ary $idx {temp opd2}
error-quads dictGet $res $ary $idx
}
incrStkImm {
set var [list temp [incr depth -1]]
set delta [list literal [lindex $insn 1]]
# TODO: This assumes we're dealing with qualified names!
set val {temp opd2}
error-quads directGet $val $var
generate-arith-domain-check incr $val $delta
quads purify {temp opd0} $val
quads purify {temp opd1} $delta
quads add $val {temp opd0} {temp opd1}
error-quads directSet $var $var $val
}
incrStk {
set delta [list temp [incr depth -1]]
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
set val {temp opd2}
error-quads directGet $val $var
generate-arith-domain-check incr $val $delta
quads purify {temp opd0} $val
quads purify {temp opd1} $delta
quads add $val {temp opd0} {temp opd1}
error-quads directSet $var $var $val
}
appendStk {
set delta [list temp [incr depth -1]]
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
error-quads directAppend $var $var $delta
}
lappendStk {
set delta [list temp [incr depth -1]]
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
error-quads directLappend $var $var $delta
}
existStk {
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
quads directExists $var $var
}
loadStk {
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
error-quads directGet $var $var
}
storeStk {
set value [list temp [incr depth -1]]
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
error-quads directSet $var $var $value
}
unsetStk {
set flags [list literal [lindex $insn 1]]
set var [list temp [incr depth -1]]
# TODO: This assumes we're dealing with qualified names!
error-quads directUnset $var $var $flags
}
dictGet {
set idxNum [lindex $insn 1]
set q {}
for {set i 0} {$i < $idxNum} {incr i} {
# NOTE: Reversed
lappend q [list temp [incr depth -1]]
}
set val [list temp [incr depth -1]]
set r [list temp $depth]
error-quads dictGet $r $val {*}[lreverse $q]
}
dictExists {
set idxNum [lindex $insn 1]
set q {}
for {set i 0} {$i < $idxNum} {incr i} {
# NOTE: Reversed
lappend q [list temp [incr depth -1]]
}
set val [list temp [incr depth -1]]
set r [list temp $depth]
quads dictExists $r $val {*}[lreverse $q]
}
dictSet {
set idxNum [expr [lindex $insn 1]]
set var [index-to-var [lindex $insn 2]]
set val [list temp [incr depth -1]]
set q {}
for {set i 0} {$i < $idxNum} {incr i} {
# NOTE: Reversed
lappend q [list temp [incr depth -1]]
}
set r [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads dictSet $r $var $val {*}[lreverse $q]
quads copy $var $r
}
dictUnset {
set idxNum [expr [lindex $insn 1]]
set var [index-to-var [lindex $insn 2]]
set q {}
for {set i 0} {$i < $idxNum} {incr i} {
# NOTE: Reversed
lappend q [list temp [incr depth -1]]
}
set r [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads dictUnset $r $var {*}[lreverse $q]
quads copy $var $r
}
dictAppend - dictLappend {
set var [index-to-var [lindex $insn 1]]
set val [list temp [incr depth -1]]
set key [list temp [incr depth -1]]
set res [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads [lindex $insn 0] $res $var $key $val
quads copy $var $res
}
dictIncrImm {
set delta [list literal [lindex $insn 1]]
set var [index-to-var [lindex $insn 2]]
set key [list temp [incr depth -1]]
set res [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads dictIncr $res $var $key $delta
quads copy $var $res
}
list {
set acount [lindex $insn 1]
set depth [expr {$depth - $acount}]
set result [list temp $depth]
set qd {}
for {set i 0} {$i < $acount} {incr i} {
lappend qd [list temp [expr {$depth + $i}]]
}
quads list $result {*}$qd
}
listIndexImm {
set idx [list literal [string range [lindex $insn 1] 1 end]]
set val [list temp [incr depth -1]]
error-quads listIndex $val $val $idx
}
listIndex {
set idx [list temp [incr depth -1]]
set val [list temp [incr depth -1]]
error-quads listIndex $val $val $idx
}
lindexMulti {
set n [lindex $insn 1]
set val [list temp [incr depth -$n]]
for {set i 1} {$i < $n} {incr i} {
error-quads listIndex $val $val [list temp [incr depth]]
}
# Should we do this as a single operation? c.f. TclLindexFlat
}
listRangeImm {
set from [list literal [string range [lindex $insn 1] 1 end]]
set to [list literal [string range [lindex $insn 2] 1 end]]
set val [list temp [incr depth -1]]
error-quads listRange $val $val $from $to
}
listLength {
set value [list temp [incr depth -1]]
set r [list temp $depth]
error-quads [lindex $insn 0] $r $value
}
invokeReplace {
set acount [lindex $insn 1]
set rcount [lindex $insn 2]
set depth [expr {$depth - $acount - 1}]
set result [list temp $depth]
# FIXME - Simply do the replacement in place for
# now. We will probably eventually want the
# mechanism to rewrite the error stack if
# the invoked procedure raises an error
set qd [list [list temp [expr {$depth + $acount}]]]
for {set i $rcount} {$i < $acount} {incr i} {
lappend qd [list temp [expr {$depth + $i}]]
}
generate-function-param-check $qd
# generate the call itself
quads invoke {temp @callframe} {temp @callframe} {*}$qd
quads retrieveResult $result {temp @callframe}
quads extractCallFrame {temp @callframe} {temp @callframe}
generate-jump [exception-target catch] maybe $result
quads extractMaybe $result $result
}
invokeStk1 - invokeStk4 {
set acount [lindex $insn 1]
set depth [expr {$depth - $acount}]
set result [list temp $depth]
set qd {}
for {set i 0} {$i < $acount} {incr i} {
lappend qd [list temp [expr {$depth + $i}]]
}
generate-function-param-check $qd
# generate the call itself
quads invoke {temp @callframe} {temp @callframe} {*}$qd
quads retrieveResult $result {temp @callframe}
quads extractCallFrame {temp @callframe} {temp @callframe}
generate-jump [exception-target catch] maybe $result
quads extractMaybe $result $result
}
jump1 - jump4 {
switch -exact -- [lindex $insn 1 0] {
pc {
set to_pc [lindex $insn 1 1]
generate-jump $to_pc
}
default {
return -code error "I don't know what to do with\
[lindex $insn 1 0] as a jump target."
}
}
}
jumpFalse1 -
jumpFalse4 { # Conditional jump
incr depth -1
switch -exact -- [lindex $insn 1 0] {
pc {
set to_pc [lindex $insn 1 1]
set to_test [list temp $depth]
generate-jump $to_pc false $to_test
}
default {
return -code error "I don't know what to do with\
[lindex $insn 1 0] as a jump target."
}
}
}
jumpTrue1 -
jumpTrue4 { # Conditional jump
incr depth -1
switch -exact -- [lindex $insn 1 0] {
pc {
set to_pc [lindex $insn 1 1]
set to_test [list temp $depth]
generate-jump $to_pc true $to_test
}
default {
return -code error "I don't know what to do with\
[lindex $insn 1 0] as a jump target."
}
}
}
returnCodeBranch {
incr depth -1
set to_test [list temp $depth]
set test [list temp [expr {$depth + 1}]]
for {set i 1} {$i <= 4} {incr i} {
quads eq $test $to_test [list literal $i]
set to_pc [expr {$pc + $i*2 - 1}]
generate-jump $to_pc true $test
}
set to_pc [expr {$pc + 9}]
generate-jump $to_pc
}
jumpTable {
set jumpTableNum [string range [lindex $insn 1] 1 end]
set jumpTable [dict get [lindex \
[dict get $bytecode auxiliary] $jumpTableNum] mapping]
incr depth -1
set to_test [list temp $depth]
set test [list temp [expr {$depth + 1}]]
set offsets [dict values $jumpTable]
lappend offsets 1
set notThere [expr {[tcl::mathfunc::min {*}$offsets] - 1}]
quads maptoint $to_test $to_test [list literal $jumpTable] \
[list literal $notThere]
dict for {val offset} $jumpTable {
quads eq $test $to_test [list literal $offset]
set to_pc [expr {$pc + $offset}]
generate-jump $to_pc true $test
}
}
existScalar {
set result [list temp $depth]
set var [index-to-var [lindex $insn 1]]
quads exists $result $var
}
loadScalar1 -
loadScalar4 { # Load a variable
set result [list temp $depth]
set var [index-to-var [lindex $insn 1]]
generate-existence-check $var
quads copy $result $var
}
loadArray1 -
loadArray4 { # Load from an array
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
generate-existence-check $ary
set res $idx
error-quads dictGet $res $ary $idx
}
nop {
}
pop - expandDrop { # Pop and expandDrop generate no code, but
# only adjust stack
}
beginCatch4 - endCatch {
# These generate no code; the exception stack is resolved
# entirely at compile time.
}
push1 -
push4 { # Push a constant
set litIdx [string range [lindex $insn 1] 1 end]
set literal [lindex [dict get $bytecode literals] $litIdx]
set result [list temp $depth]
quads copy $result [list literal $literal]
}
pushResult - pushReturnCode {
# Push bits and pieces of interpreter state
quads [string map {pushR r} $insn] \
[list temp $depth] {temp @callframe}
}
pushReturnOpts {
set v [list temp $depth]
quads returnCode $v {temp @callframe}
quads returnOptions $v {temp @callframe} $v
}
dup { # Duplicate value on stack
set d [expr {$depth - 1}]
quads copy [list temp $depth] [list temp $d]
}
over { # Duplicate value from deeper in stack
set d [expr {$depth - 1 - [lindex $insn 1]}]
quads copy [list temp $depth] [list temp $d]
}
startCommand {
# For now, startCommand does nothing
# lappend quads startCommand
}
storeScalar1 -
storeScalar4 { # Store a variable
incr depth -1
set var [index-to-var [lindex $insn 1]]
quads copy $var [list temp $depth]
}
storeArray1 -
storeArray4 { # Store into an array
set val [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
set res $idx
quads initIfNotExists $ary $ary {literal {}}
error-quads dictSet $ary $ary $val $idx
quads copy $res $val
}
tryCvtToNumeric { # No effect on value
}
tryCvtToBoolean { # Push whether we're dealing with a boolean
set val [list temp [expr {$depth - 1}]]
set res [list temp $depth]
quads isBoolean $res $val
}
numericType -
resolveCmd -
strlen { # Unary operations
set v0 [list temp [incr depth -1]]
set r $v0
quads [lindex $insn 0] $r $v0
}
originCmd {
set v0 [list temp [incr depth -1]]
set r $v0
error-quads [lindex $insn 0] $r $v0
}
stringIsDouble -
stringIsDoubleStrict -
stringIsEntier -
stringIsEntierStrict -
stringIsInt -
stringIsIntStrict -
stringIsWide -
stringIsWideStrict {
regexp {^stringIs(Double|Entier|Int|Wide)((?:Strict)?)$} \
[lindex $insn 0] -> type strict
switch -exact $type {
Double {
set typecode $quadcode::dataType::NUMERIC
}
Entier {
set typecode $quadcode::dataType::ENTIER
}
Wide {
set typecode $quadcode::dataType::INT
}
Int {
set typecode $quadcode::dataType::INT32
}
}
set typecode [quadcode::dataType::typeUnion \
$quadcode::dataType::IMPURE $typecode]
if {$strict ne "Strict"} {
set typecode [quadcode::dataType::typeUnion \
$quadcode::dataType::EMPTY $typecode]
}
set v0 [list temp [incr depth -1]]
set r $v0
quads [list instanceOf $typecode \
[quadcode::nameOfType $typecode]] $r $v0
}
not {
set v0 [list temp [incr depth -1]]
set r $v0
error-quads [lindex $insn 0] $r $v0
}
appendScalar1 - appendScalar4 {
set val [list temp [incr depth -1]]
set var [index-to-var [lindex $insn 1]]
quads initIfNotExists $var $var {literal {}}
set result [list temp $depth]
quads strcat $var $var $val
quads copy $result $var
}
appendArray1 - appendArray4 {
set val [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
set res [list temp $depth]
quads initIfNotExists $ary $ary {literal {}}
error-quads dictGet $res $ary $idx
quads strcat $res $res $val
error-quads dictSet $ary $ary $res $idx
}
strcat - concatStk {
lassign $insn op count
set strcatvals {}
for {set i 0} {$i < $count} {incr i} {
lappend strcatvals [list temp [incr depth -1]]
}
set result [list temp $depth]
if {$op eq "concatStk"} {
set op "concat"
}
quads $op $result {*}[lreverse $strcatvals]
}
lappendScalar1 - lappendScalar4 {
set val [list temp [incr depth -1]]
set var [index-to-var [lindex $insn 1]]
set res [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads listAppend $res $var $val
quads copy $var $res
}
lappendList {
set val [list temp [incr depth -1]]
set var [index-to-var [lindex $insn 1]]
set res [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads listConcat $res $var $val
quads copy $var $res
}
lappendArray1 - lappendArray4 {
set val [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
set res [list temp $depth]
quads initIfNotExists $var $var {literal {}}
error-quads dictGet $res $ary $idx
error-quads listAppend $res $res $val
error-quads dictSet $ary $ary $res $idx
}
lappendListArray {
set val [list temp [incr depth -1]]
set idx [list temp [incr depth -1]]
set ary [index-to-var [lindex $insn 1]]
set res [list temp $depth]
quads initIfNotExists $ary $ary {literal {}}
error-quads dictGet $res $ary $idx
error-quads listConcat $res $res $val
error-quads dictSet $ary $ary $res $idx
}
listConcat {
set list2 [list temp [incr depth -1]]
set list1 [list temp [incr depth -1]]
set res [list temp $depth]
error-quads listConcat $res $list1 $list2
}
arrayExistsImm {
set ary [index-to-var [lindex $insn 1]]
set res [list temp $depth]
set tmp [list temp [expr {$depth + 1}]]
quads initIfNotExists $tmp $ary {literal \uf8ff}
quads dictExists $res $tmp
}
arrayMakeImm {
set ary [index-to-var [lindex $insn 1]]
quads initIfNotExists $ary $ary {literal {}}
}
variable {
set var [index-to-var [lindex $insn 1]]
set name [list temp [incr depth -1]]
quads [lindex $insn 0] {temp @callframe} {temp @callframe} \
[list literal [lindex $var 1]] $name
quads retrieveResult {temp @error} {temp @callframe}
quads extractCallFrame {temp @callframe} {temp @callframe}
generate-jump [exception-target catch] maybe {temp @error}
}
nsupvar - upvar {
set var [index-to-var [lindex $insn 1]]
set name [list temp [incr depth -1]]
set context [list temp [incr depth -1]]
quads [lindex $insn 0] {temp @callframe} {temp @callframe} \
[list literal [lindex $var 1]] $context $name
quads retrieveResult {temp @error} {temp @callframe}
quads extractCallFrame {temp @callframe} {temp @callframe}
generate-jump [exception-target catch] maybe {temp @error}
}
default {
# TODO - Many more instructions
return -code error "I don't know yet what to do about $insn"
}
}
}
if {[dict exists $fixup -1]} {
foreach q [dict get $fixup -1] {
lset quads $q 1 [list pc [llength $quads]]
}
# let the flow analysis figure out whether this is confluent!
# lappend quads confluence
quads returnCode [set code [list temp 0]] {temp @callframe}
quads returnException {} {temp @callframe} $code
dict unset fixup -1
}
if {[dict size $fixup] > 0} {
error "Failed to fix jumps at [join [dict keys $fixup] ,]"
}
return [dict create \
quadcode $quads \
links [dict get $bytecode links] \
vars [lmap v $vars {list var $v}]]
}
##############################################################################
#
# HELPER PROCEDURES FOR bytecode-to-quads
#
# index-to-var --
#
# Translates an index into Tcl's bytecode LVT into a quadcode variable
# reference. Assumes it is being called from [bytecode-to-quads]. Note
# that this handles nameless temporaries by creating variables with
# "illegal" names; merely using the empty string does not work because
# that ends up conflating temporaries when there are multiple in use at
# once (e.g., in [try] or with nested code).
#
# Parameters:
# varIndex -
# The index into the LVT. May be preceded by a "%" character.
#
# Results:
# Quadcode variable name; a two element list with the first being "var"
# and the second being the actual name of the variable.
proc index-to-var {varIndex} {
upvar 1 bytecode bytecode
if {[string match "%*" $varIndex]} {
set varIndex [string range $varIndex 1 end]
}
lassign [lindex [dict get $bytecode variables] $varIndex] flags name
if {"temp" in $flags} {
# Variable name is not legal for Tcl scalar local variables
return [list var ($varIndex)]
}
return [list var $name]
}
# exception-target --
#
# Locate where an exception should go to from the current PC. Do not
# call from anywhere other than bytecode-to-quads!
#
# Parameters:
# exceptionType - The type of exception that we are looking up. Should
# be 'catch' or 'loop'.
# exceptionField - The subtype of exception that we are looking up.
# Defaults to 'catch' which is the correct value for
# 'catch' exceptions; must be set to 'break' or
# 'continue' for 'loop' exceptions.
#
# Results:
# Returns the address that we should jump to on the given kind of
# exception, or -1 if we should jump to the generic exception handler.
proc exception-target {exceptionType {exceptionField "catch"}} {
upvar 1 bytecode bytecode pc pc
set to_pc -1
foreach exnRange [dict get $bytecode exception] {
dict with exnRange {}
if {$from > $pc} break
set field $exceptionField
if {($type eq $exceptionType || $type eq "catch") && $to >= $pc} {
set to_pc [expr {
$type eq $exceptionType
? [set $exceptionField]
: $catch
}]
}
}
return $to_pc
}
# generate-jump --
#
# Generates a jump instruction and appends it to the list of quadcodes
# being built. Do not call from anywhere other than bytecode-to-quads!
#
# Example uses:
# generate-jump $targetPC
# generate-jump $targetPC true $testVar
# generate-jump $targetPC false $testVar
# generate-jump $exceptionPC maybe $maybeVar
#
# Parameters:
# target - The absolute PC in the bytecode that we are jumping
# to.
# jumpType - The type of jump we are creating. Should be omitted
# when just generating a simple jump.
# conditionVar - The variable that is used to decide whether to jump or
# not. The decision to use based on the variable depends
# on the jumpType. Should be omitted when generating a
# simple jump.
#
# Results:
# None.
proc generate-jump {target args} {
upvar 1 quadindex quadindex fixup fixup quads quads
if {[dict exists $quadindex $target]} {
set target [list pc [dict get $quadindex $target]]
} else {
dict lappend fixup $target [llength $quads]
set target {}
}
if {[llength $args] == 2} {
lassign $args type var
quads jump[string totitle $type] $target $var
} else {
quads jump $target
}
}
# generate-existence-check --
#
# Generates a check to make sure that a variable exists, and
# appends it to the list of quadcodes being built. Do not call from
# anywhere but bytecode-to-quads!
#
# Parameters;
# var - The variable being tested for existence
#
# Results:
# None.
#
# Side effects:
# Emits a sequence including 'throwIfNotExists' and instructions that
# adjust the variable's type on the two paths.
proc generate-existence-check {var} {
upvar 1 quadindex quadindex fixup fixup quads quads
set target [uplevel 1 {exception-target catch}]
if {[dict exists $quadindex $target]} {
set tgt [list pc [dict get $quadindex $target]]
} else {
dict lappend fixup $target [llength $quads]
set tgt {}
}
quads throwIfNotExists $tgt $var [lreplace $var 0 0 literal]
# The narrowing pass will insert any necessary 'extractExists'
# instructions
}
# generate-arith-domain-check --
#
# Generates a check to make sure that a value is NUMERIC or INT
# for the instructions that require such a value. Do not call from
# anywhere but bytecode-to-quads!
#
# Parameters:
# q - The quadruple that will consume the value
#
# Results:
# None.
#
# Side effects:
# Emits a sequence including 'checkArithDomain' and instructions that
# adjust the value's type on the two paths. Note that the path on which
# the exception is thrown will most likely yield a value represented
# as a string; nevertheless, knowing that it is NOT a numeric value
# or an integer may yield useful clues for further analyis.
# TODO: bitand, bitor, bitxor, lshift, rshift, mod and bitnot should be
# restricted to ENTIER (and the shift count on lshift/rshift should
# be INT). This will depend on an implementation of ENTIER.
variable operator_info \
[dict create \
incr [list incr INT] \
add [list + NUMERIC] \
div [list / NUMERIC] \
expon [list ** NUMERIC] \
mult [list * NUMERIC] \
sub [list - NUMERIC] \
uplus [list + NUMERIC] \
uminus [list - NUMERIC] \
bitand [list & INT] \
bitor [list | INT] \
bitxor [list ^ INT] \
mod [list % INT] \
lshift [list << INT] \
rshift [list << INT] \
bitnot [list ~ INT]]
proc generate-arith-domain-check {operator args} {
variable operator_info
upvar 1 quadindex quadindex fixup fixup quads quads
set target [uplevel 1 {exception-target catch}]
if {![dict exists $operator_info $operator]} return
lassign [dict get $operator_info $operator] opname typename
namespace upvar quadcode::dataType $typename typecode
namespace upvar quadcode::dataType IMPURE impure
set impureTC [quadcode::dataType::typeUnion $typecode $impure]
set impureTN [list IMPURE $typename]
foreach val $args {
if {[dict exists $quadindex $target]} {
set tgt [list pc [dict get $quadindex $target]]
} else {
dict lappend fixup $target [llength $quads]
set tgt {}
}
quads [list checkArithDomain $impureTC $impureTN] \
$tgt $val [list literal $opname]
# A later pass will add type narrowing operations on both
# branches, after copy propagation has had a chance to run.
}
}
# generate-function-param-check --
#
# Generates a check to make sure that a value conforms with a function's
# requirement for its parameter type. Do not call from anywhere but
# bytecode-to-quads!
#
# Parameters:
# q - The quadruple that will consume the value
#
# Results:
# None.
#
# Side effects:
# Inserts a parameter type checking sequence.
#
# Unfortunately, when we are doing this, we don't actually know what
# that type is, so we emit a placeholder sequence for each parameter $i:
#
# {checkFunctionParam $i} {pc A} $functionName $value
# {narrowToParamType $i} $value $functionName $value
# A: jump {pc B}
# {narrowToNotParamType $i} $value $functionName $value
# jump {pc $catch}
# B: (next instruction)
#
# If, later on, we decide that we know what the parameters and result of
# $functionName are, we replace 'checkFunctionParam' with 'checkArithDomain',
# and 'narrowToParamType' and 'narrowToNotParamType' with the appropriate
# 'narrowToType' operations. If, instead, we cannot find the function or we
# find that it accepts STRING, we remove the check and the narrowing operations.
proc generate-function-param-check {q} {
upvar 1 quadindex quadindex fixup fixup quads quads
set argv [lassign $q functionName]
set target [uplevel 1 {exception-target catch}]
set i -1
foreach val $argv {
incr i
if {[dict exists $quadindex $target]} {
set tgt [list pc [dict get $quadindex $target]]
} else {
dict lappend fixup $target [llength $quads]
set tgt {}
}
quads [list checkFunctionParam $i] [list pc $target] $functionName $val
# A later pass will insert the narrowing operations
# quads [list narrowToParamType $i] $val $functionName $val; # 1
# quads jump [list pc [expr {[llength $quads] + 3}]]; # 2
# quads [list narrowToNotParamType $i] $val $functionName $val; # 3
# uplevel 1 [list generate-jump $target]; # 4
# # next instruction is position 5. 0 jumps to 3, 2 jumps to 5.
}
}
# quads --
#
# Generate the given quadcode.
#
# Parameters:
# opcode - The name of the quadcode opcode to generate.
# result - The "stack location" to write the result of the opcode.
# args - The arguments to the opcode.
#
# Results:
# None.
proc quads {opcode result args} {
upvar 1 quads quads
lappend quads [list $opcode $result {*}$args]
return
}
# error-quads --
#
# Generate the given quadcode and its error interception sequence.
#
# Parameters:
# opcode - The name of the quadcode opcode to generate.
# result - The "stack location" to write the result of the opcode.
# args - The inputs to the quadcode.
#
# Results:
# None.
proc error-quads {opcode result args} {
upvar 1 quads quads
lappend quads [list $opcode $result {*}$args]
set target [uplevel 1 {exception-target catch}]
uplevel 1 [list generate-jump $target maybe $result]
lappend quads [list extractMaybe $result $result]
return
}
# interp alias {} tcl::mathfunc::istype {} ::dataType::isa
# Local Variables:
# mode: tcl
# fill-column: 78
# auto-fill-function: nil
# End:
|