Debian Codegraph @

tests/assemble.test

raw
1 # assemble.test --
2 #
3 # Test suite for the 'tcl::unsupported::assemble' command
4 #
5 # Copyright (c) 2010 by Ozgur Dogan Ugurlu.
6 # Copyright (c) 2010 by Kevin B. Kenny.
7 #
8 # See the file "license.terms" for information on usage and redistribution of
9 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 #-----------------------------------------------------------------------------
11
12 # Commands covered: assemble
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18 namespace eval tcl::unsupported {namespace export assemble}
19 namespace import tcl::unsupported::assemble
20
21 # Procedure to make code that fills the literal and local variable tables, to
22 # force instructions to spill to four bytes.
23
24 proc fillTables {} {
25 set s {}
26 set sep {}
27 for {set i 0} {$i < 256} {incr i} {
28 append s $sep [list set v$i literal$i]
29 set sep \n
30 }
31 return $s
32 }
33
34 testConstraint memory [llength [info commands memory]]
35 if {[testConstraint memory]} {
36 proc getbytes {} {
37 set lines [split [memory info] \n]
38 return [lindex $lines 3 3]
39 }
40 proc leaktest {script {iterations 3}} {
41 set end [getbytes]
42 for {set i 0} {$i < $iterations} {incr i} {
43 uplevel 1 $script
44 set tmp $end
45 set end [getbytes]
46 }
47 return [expr {$end - $tmp}]
48 }
49 }
50
51 # assemble-1 - TclNRAssembleObjCmd
52
53 test assemble-1.1 {wrong # args, direct eval} {
54 -body {
55 eval [list assemble]
56 }
57 -returnCodes error
58 -result {wrong # args*}
59 -match glob
60 }
61 test assemble-1.2 {wrong # args, direct eval} {
62 -body {
63 eval [list assemble too many]
64 }
65 -returnCodes error
66 -result {wrong # args*}
67 -match glob
68 }
69 test assemble-1.3 {error reporting, direct eval} {
70 -body {
71 list [catch {
72 eval [list assemble {
73 # bad opcode
74 rubbish
75 }]
76 } result] $result $errorInfo
77 }
78 -match glob
79 -result {1 {bad instruction "rubbish":*} {bad instruction "rubbish":*
80 while executing
81 "rubbish"
82 ("assemble" body, line 3)*}}
83 -cleanup {unset result}
84 }
85 test assemble-1.4 {simple direct eval} {
86 -body {
87 eval [list assemble {push {this is a test}}]
88 }
89 -result {this is a test}
90 }
91
92 # assemble-2 - CompileAssembleObj
93
94 test assemble-2.1 {bytecode reuse, direct eval} {
95 -body {
96 set x {push "this is a test"}
97 list [eval [list assemble $x]] \
98 [eval [list assemble $x]]
99 }
100 -result {{this is a test} {this is a test}}
101 }
102 test assemble-2.2 {bytecode discard, direct eval} {
103 -body {
104 set x {load value}
105 proc p1 {x} {
106 set value value1
107 assemble $x
108 }
109 proc p2 {x} {
110 set a b
111 set value value2
112 assemble $x
113 }
114 list [p1 $x] [p2 $x]
115 }
116 -result {value1 value2}
117 -cleanup {
118 unset x
119 rename p1 {}
120 rename p2 {}
121 }
122 }
123 test assemble-2.3 {null script, direct eval} {
124 -body {
125 set x {}
126 assemble $x
127 }
128 -result {}
129 -cleanup {unset x}
130 }
131
132 # assemble-3 - TclCompileAssembleCmd
133
134 test assemble-3.1 {wrong # args, compiled path} {
135 -body {
136 proc x {} {
137 assemble
138 }
139 x
140 }
141 -returnCodes error
142 -match glob
143 -result {wrong # args:*}
144 -cleanup {rename x {}}
145 }
146 test assemble-3.2 {wrong # args, compiled path} {
147 -body {
148 proc x {} {
149 assemble too many
150 }
151 x
152 }
153 -returnCodes error
154 -match glob
155 -result {wrong # args:*}
156 -cleanup {
157 rename x {}
158 }
159 }
160
161 # assemble-4 - TclAssembleCode mainline
162
163 test assemble-4.1 {syntax error} {
164 -body {
165 proc x {} {
166 assemble {
167 {}extra
168 }
169 }
170 list [catch x result] $result $::errorInfo
171 }
172 -cleanup {
173 rename x {}
174 unset result
175 }
176 -match glob
177 -result {1 {extra characters after close-brace} {extra characters after close-brace
178 while executing
179 "{}e"
180 ("assemble" body, line 2)*}}
181 }
182 test assemble-4.2 {null command} {
183 -body {
184 proc x {} {
185 assemble {
186 push hello; pop;;push goodbye
187 }
188 }
189 x
190 }
191 -result goodbye
192 -cleanup {
193 rename x {}
194 }
195 }
196
197 # assemble-5 - GetNextOperand off-nominal cases
198
199 test assemble-5.1 {unsupported expansion} {
200 -body {
201 proc x {y} {
202 assemble {
203 {*}$y
204 }
205 }
206 list [catch {x {push hello}} result] $result $::errorCode
207 }
208 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
209 -cleanup {
210 rename x {}
211 unset result
212 }
213 }
214 test assemble-5.2 {unsupported substitution} {
215 -body {
216 proc x {y} {
217 assemble {
218 $y
219 }
220 }
221 list [catch {x {nop}} result] $result $::errorCode
222 }
223 -cleanup {
224 rename x {}
225 unset result
226 }
227 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
228 }
229 test assemble-5.3 {unsupported substitution} {
230 -body {
231 proc x {} {
232 assemble {
233 [x]
234 }
235 }
236 list [catch {x} result] $result $::errorCode
237 }
238 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
239 -cleanup {rename x {}}
240 }
241 test assemble-5.4 {backslash substitution} {
242 -body {
243 proc x {} {
244 assemble {
245 p\x75sh\
246 hello\ world
247 }
248 }
249 x
250 }
251 -cleanup {
252 rename x {}
253 }
254 -result {hello world}
255 }
256
257 # assemble-6 - ASSEM_PUSH
258
259 test assemble-6.1 {push, wrong # args} {
260 -body {
261 assemble push
262 }
263 -returnCodes error
264 -match glob
265 -result {wrong # args*}
266 }
267 test assemble-6.2 {push, wrong # args} {
268 -body {
269 assemble {push too many}
270 }
271 -returnCodes error
272 -match glob
273 -result {wrong # args*}
274 }
275 test assemble-6.3 {push} {
276 -body {
277 eval [list assemble {push hello}]
278 }
279 -result hello
280 }
281 test assemble-6.4 {push4} {
282 -body {
283 proc x {} "
284 [fillTables]
285 assemble {push hello}
286 "
287 x
288 }
289 -cleanup {
290 rename x {}
291 }
292 -result hello
293 }
294
295 # assemble-7 - ASSEM_1BYTE
296
297 test assemble-7.1 {add, wrong # args} {
298 -body {
299 assemble {add excess}
300 }
301 -returnCodes error
302 -match glob
303 -result {wrong # args*}
304 }
305 test assemble-7.2 {add} {
306 -body {
307 assemble {
308 push 2
309 push 2
310 add
311 }
312 }
313 -result {4}
314 }
315 test assemble-7.3 {appendArrayStk} {
316 -body {
317 set a(b) {hello, }
318 assemble {
319 push a
320 push b
321 push world
322 appendArrayStk
323 }
324 set a(b)
325 }
326 -result {hello, world}
327 -cleanup {unset a}
328 }
329 test assemble-7.4 {appendStk} {
330 -body {
331 set a {hello, }
332 assemble {
333 push a
334 push world
335 appendStk
336 }
337 set a
338 }
339 -result {hello, world}
340 -cleanup {unset a}
341 }
342 test assemble-7.5 {bitwise ops} {
343 -body {
344 list \
345 [assemble {push 0b1100; push 0b1010; bitand}] \
346 [assemble {push 0b1100; bitnot}] \
347 [assemble {push 0b1100; push 0b1010; bitor}] \
348 [assemble {push 0b1100; push 0b1010; bitxor}]
349 }
350 -result {8 -13 14 6}
351 }
352 test assemble-7.6 {div} {
353 -body {
354 assemble {push 999999; push 7; div}
355 }
356 -result 142857
357 }
358 test assemble-7.7 {dup} {
359 -body {
360 assemble {
361 push 1; dup; dup; add; dup; add; dup; add; add
362 }
363 }
364 -result 9
365 }
366 test assemble-7.8 {eq} {
367 -body {
368 list \
369 [assemble {push able; push baker; eq}] \
370 [assemble {push able; push able; eq}]
371 }
372 -result {0 1}
373 }
374 test assemble-7.9 {evalStk} {
375 -body {
376 assemble {
377 push {concat test 7.3}
378 evalStk
379 }
380 }
381 -result {test 7.3}
382 }
383 test assemble-7.9a {evalStk, syntax} {
384 -body {
385 assemble {
386 push {{}bad}
387 evalStk
388 }
389 }
390 -returnCodes error
391 -result {extra characters after close-brace}
392 }
393 test assemble-7.9b {evalStk, backtrace} {
394 -body {
395 proc y {z} {
396 error testing
397 }
398 proc x {} {
399 assemble {
400 push {
401 # test error in evalStk
402 y asd
403 }
404 evalStk
405 }
406 }
407 list [catch x result] $result $errorInfo
408 }
409 -result {1 testing {testing
410 while executing
411 "error testing"
412 (procedure "y" line 2)
413 invoked from within
414 "y asd"*}}
415 -match glob
416 -cleanup {
417 rename y {}
418 rename x {}
419 }
420 }
421 test assemble-7.10 {existArrayStk} {
422 -body {
423 proc x {name key} {
424 set a(b) c
425 assemble {
426 load name; load key; existArrayStk
427 }
428 }
429 list [x a a] [x a b] [x b a] [x b b]
430 }
431 -result {0 1 0 0}
432 -cleanup {rename x {}}
433 }
434 test assemble-7.11 {existStk} {
435 -body {
436 proc x {name} {
437 set a b
438 assemble {
439 load name; existStk
440 }
441 }
442 list [x a] [x b]
443 }
444 -result {1 0}
445 -cleanup {rename x {}}
446 }
447 test assemble-7.12 {expon} {
448 -body {
449 assemble {push 3; push 4; expon}
450 }
451 -result 81
452 }
453 test assemble-7.13 {exprStk} {
454 -body {
455 assemble {
456 push {acos(-1)}
457 exprStk
458 }
459 }
460 -result 3.141592653589793
461 }
462 test assemble-7.13a {exprStk, syntax} {
463 -body {
464 assemble {
465 push {2+}
466 exprStk
467 }
468 }
469 -returnCodes error
470 -result {missing operand at _@_
471 in expression "2+_@_"}
472 }
473 test assemble-7.13b {exprStk, backtrace} {
474 -body {
475 proc y {z} {
476 error testing
477 }
478 proc x {} {
479 assemble {
480 push {[y asd]}
481 exprStk
482 }
483 }
484 list [catch x result] $result $errorInfo
485 }
486 -result {1 testing {testing
487 while executing
488 "error testing"
489 (procedure "y" line 2)
490 invoked from within
491 "y asd"*}}
492 -match glob
493 -cleanup {
494 rename y {}
495 rename x {}
496 }
497 }
498 test assemble-7.14 {ge gt le lt} {
499 -body {
500 proc x {a b} {
501 list [assemble {load a; load b; ge}] \
502 [assemble {load a; load b; gt}] \
503 [assemble {load a; load b; le}] \
504 [assemble {load a; load b; lt}]
505 }
506 list [x 0 0] [x 0 1] [x 1 0]
507 }
508 -result {{1 0 1 0} {0 0 1 1} {1 1 0 0}}
509 -cleanup {rename x {}}
510 }
511 test assemble-7.15 {incrArrayStk} {
512 -body {
513 proc x {} {
514 set a(b) 5
515 assemble {
516 push a; push b; push 7; incrArrayStk
517 }
518 }
519 x
520 }
521 -result 12
522 -cleanup {rename x {}}
523 }
524 test assemble-7.16 {incrStk} {
525 -body {
526 proc x {} {
527 set a 5
528 assemble {
529 push a; push 7; incrStk
530 }
531 }
532 x
533 }
534 -result 12
535 -cleanup {rename x {}}
536 }
537 test assemble-7.17 {land/lor} {
538 -body {
539 proc x {a b} {
540 list \
541 [assemble {load a; load b; land}] \
542 [assemble {load a; load b; lor}]
543 }
544 list [x 0 0] [x 0 23] [x 35 0] [x 47 59]
545 }
546 -result {{0 0} {0 1} {0 1} {1 1}}
547 -cleanup {rename x {}}
548 }
549 test assemble-7.18 {lappendArrayStk} {
550 -body {
551 proc x {} {
552 set able(baker) charlie
553 assemble {
554 push able
555 push baker
556 push dog
557 lappendArrayStk
558 }
559 }
560 x
561 }
562 -result {charlie dog}
563 -cleanup {rename x {}}
564 }
565 test assemble-7.19 {lappendStk} {
566 -body {
567 proc x {} {
568 set able baker
569 assemble {
570 push able
571 push charlie
572 lappendStk
573 }
574 }
575 x
576 }
577 -result {baker charlie}
578 -cleanup {rename x {}}
579 }
580 test assemble-7.20 {listIndex} {
581 -body {
582 assemble {
583 push {a b c d}
584 push 2
585 listIndex
586 }
587 }
588 -result c
589 }
590 test assemble-7.21 {listLength} {
591 -body {
592 assemble {
593 push {a b c d}
594 listLength
595 }
596 }
597 -result 4
598 }
599 test assemble-7.22 {loadArrayStk} {
600 -body {
601 proc x {} {
602 set able(baker) charlie
603 assemble {
604 push able
605 push baker
606 loadArrayStk
607 }
608 }
609 x
610 }
611 -result charlie
612 -cleanup {rename x {}}
613 }
614 test assemble-7.23 {loadStk} {
615 -body {
616 proc x {} {
617 set able baker
618 assemble {
619 push able
620 loadStk
621 }
622 }
623 x
624 }
625 -result baker
626 -cleanup {rename x {}}
627 }
628 test assemble-7.24 {lsetList} {
629 -body {
630 proc x {} {
631 set l {{a b} {c d} {e f} {g h}}
632 assemble {
633 push {2 1}; push i; load l; lsetList
634 }
635 }
636 x
637 }
638 -result {{a b} {c d} {e i} {g h}}
639 -cleanup {rename x {}}
640 }
641 test assemble-7.25 {lshift} {
642 -body {
643 assemble {push 16; push 4; lshift}
644 }
645 -result 256
646 }
647 test assemble-7.26 {mod} {
648 -body {
649 assemble {push 123456; push 1000; mod}
650 }
651 -result 456
652 }
653 test assemble-7.27 {mult} {
654 -body {
655 assemble {push 12345679; push 9; mult}
656 }
657 -result 111111111
658 }
659 test assemble-7.28 {neq} {
660 -body {
661 list \
662 [assemble {push able; push baker; neq}] \
663 [assemble {push able; push able; neq}]
664 }
665 -result {1 0}
666 }
667 test assemble-7.29 {not} {
668 -body {
669 list \
670 [assemble {push 17; not}] \
671 [assemble {push 0; not}]
672 }
673 -result {0 1}
674 }
675 test assemble-7.30 {pop} {
676 -body {
677 assemble {push this; pop; push that}
678 }
679 -result that
680 }
681 test assemble-7.31 {rshift} {
682 -body {
683 assemble {push 257; push 4; rshift}
684 }
685 -result 16
686 }
687 test assemble-7.32 {storeArrayStk} {
688 -body {
689 proc x {} {
690 assemble {
691 push able; push baker; push charlie; storeArrayStk
692 }
693 array get able
694 }
695 x
696 }
697 -result {baker charlie}
698 -cleanup {rename x {}}
699 }
700 test assemble-7.33 {storeStk} {
701 -body {
702 proc x {} {
703 assemble {
704 push able; push baker; storeStk
705 }
706 set able
707 }
708 x
709 }
710 -result {baker}
711 -cleanup {rename x {}}
712 }
713 test assemble-7,34 {strcmp} {
714 -body {
715 proc x {a b} {
716 assemble {
717 load a; load b; strcmp
718 }
719 }
720 list [x able baker] [x baker able] [x baker baker]
721 }
722 -result {-1 1 0}
723 -cleanup {rename x {}}
724 }
725 test assemble-7.35 {streq/strneq} {
726 -body {
727 proc x {a b} {
728 list \
729 [assemble {load a; load b; streq}] \
730 [assemble {load a; load b; strneq}]
731 }
732 list [x able able] [x able baker]
733 }
734 -result {{1 0} {0 1}}
735 -cleanup {rename x {}}
736 }
737 test assemble-7.36 {strindex} {
738 -body {
739 assemble {push testing; push 4; strindex}
740 }
741 -result i
742 }
743 test assemble-7.37 {strlen} {
744 -body {
745 assemble {push testing; strlen}
746 }
747 -result 7
748 }
749 test assemble-7.38 {sub} {
750 -body {
751 assemble {push 42; push 17; sub}
752 }
753 -result 25
754 }
755 test assemble-7.39 {tryCvtToNumeric} {
756 -body {
757 assemble {
758 push 42; tryCvtToNumeric
759 }
760 }
761 -result 42
762 }
763 # assemble-7.40 absent
764 test assemble-7.41 {uminus} {
765 -body {
766 assemble {
767 push 42; uminus
768 }
769 }
770 -result -42
771 }
772 test assemble-7.42 {uplus} {
773 -body {
774 assemble {
775 push 42; uplus
776 }
777 }
778 -result 42
779 }
780 test assemble-7.43 {uplus} {
781 -body {
782 assemble {
783 push NaN; uplus
784 }
785 }
786 -returnCodes error
787 -result {can't use non-numeric floating-point value as operand of "+"}
788 }
789 test assemble-7.43.1 {tryCvtToNumeric} {
790 -body {
791 assemble {
792 push NaN; tryCvtToNumeric
793 }
794 }
795 -returnCodes error
796 -result {domain error: argument not in valid range}
797 }
798 test assemble-7.44 {listIn} {
799 -body {
800 assemble {
801 push b; push {a b c}; listIn
802 }
803 }
804 -result 1
805 }
806 test assemble-7.45 {listNotIn} {
807 -body {
808 assemble {
809 push d; push {a b c}; listNotIn
810 }
811 }
812 -result 1
813 }
814 test assemble-7.46 {nop} {
815 -body {
816 assemble { push x; nop; nop; nop}
817 }
818 -result x
819 }
820
821 # assemble-8 ASSEM_LVT and FindLocalVar
822
823 test assemble-8.1 {load, wrong # args} {
824 -body {
825 assemble load
826 }
827 -returnCodes error
828 -match glob
829 -result {wrong # args*}
830 }
831 test assemble-8.2 {load, wrong # args} {
832 -body {
833 assemble {load too many}
834 }
835 -returnCodes error
836 -match glob
837 -result {wrong # args*}
838 }
839 test assemble-8.3 {nonlocal var} {
840 -body {
841 list [catch {assemble {load ::env}} result] $result $errorCode
842 }
843 -result {1 {variable "::env" is not local} {TCL ASSEM NONLOCAL ::env}}
844 -cleanup {unset result}
845 }
846 test assemble-8.4 {bad context} {
847 -body {
848 set x 1
849 list [catch {assemble {load x}} result] $result $errorCode
850 }
851 -result {1 {cannot use this instruction to create a variable in a non-proc context} {TCL ASSEM LVT}}
852 -cleanup {unset result}
853 }
854 test assemble-8.5 {bad context} {
855 -body {
856 namespace eval assem {
857 set x 1
858 list [catch {assemble {load x}} result opts] $result [dict get $opts -errorcode]
859 }
860 }
861 -result {1 {cannot use this instruction to create a variable in a non-proc context} {TCL ASSEM LVT}}
862 -cleanup {namespace delete assem}
863 }
864 test assemble-8.6 {load1} {
865 -body {
866 proc x {a} {
867 assemble {
868 load a
869 }
870 }
871 x able
872 }
873 -result able
874 -cleanup {rename x {}}
875 }
876 test assemble-8.7 {load4} {
877 -body {
878 proc x {a} "
879 [fillTables]
880 set b \$a
881 assemble {load b}
882 "
883 x able
884 }
885 -result able
886 -cleanup {rename x {}}
887 }
888 test assemble-8.8 {loadArray1} {
889 -body {
890 proc x {} {
891 set able(baker) charlie
892 assemble {
893 push baker
894 loadArray able
895 }
896 }
897 x
898 }
899 -result charlie
900 -cleanup {rename x {}}
901 }
902 test assemble-8.9 {loadArray4} {
903 -body "
904 proc x {} {
905 [fillTables]
906 set able(baker) charlie
907 assemble {
908 push baker
909 loadArray able
910 }
911 }
912 x
913 "
914 -result charlie
915 -cleanup {rename x {}}
916 }
917 test assemble-8.10 {append1} {
918 -body {
919 proc x {} {
920 set y {hello, }
921 assemble {
922 push world; append y
923 }
924 }
925 x
926 }
927 -result {hello, world}
928 -cleanup {rename x {}}
929 }
930 test assemble-8.11 {append4} {
931 -body {
932 proc x {} "
933 [fillTables]
934 set y {hello, }
935 assemble {
936 push world; append y
937 }
938 "
939 x
940 }
941 -result {hello, world}
942 -cleanup {rename x {}}
943 }
944 test assemble-8.12 {appendArray1} {
945 -body {
946 proc x {} {
947 set y(z) {hello, }
948 assemble {
949 push z; push world; appendArray y
950 }
951 }
952 x
953 }
954 -result {hello, world}
955 -cleanup {rename x {}}
956 }
957 test assemble-8.13 {appendArray4} {
958 -body {
959 proc x {} "
960 [fillTables]
961 set y(z) {hello, }
962 assemble {
963 push z; push world; appendArray y
964 }
965 "
966 x
967 }
968 -result {hello, world}
969 -cleanup {rename x {}}
970 }
971 test assemble-8.14 {lappend1} {
972 -body {
973 proc x {} {
974 set y {hello,}
975 assemble {
976 push world; lappend y
977 }
978 }
979 x
980 }
981 -result {hello, world}
982 -cleanup {rename x {}}
983 }
984 test assemble-8.15 {lappend4} {
985 -body {
986 proc x {} "
987 [fillTables]
988 set y {hello,}
989 assemble {
990 push world; lappend y
991 }
992 "
993 x
994 }
995 -result {hello, world}
996 -cleanup {rename x {}}
997 }
998 test assemble-8.16 {lappendArray1} {
999 -body {
1000 proc x {} {
1001 set y(z) {hello,}
1002 assemble {
1003 push z; push world; lappendArray y
1004 }
1005 }
1006 x
1007 }
1008 -result {hello, world}
1009 -cleanup {rename x {}}
1010 }
1011 test assemble-8.17 {lappendArray4} {
1012 -body {
1013 proc x {} "
1014 [fillTables]
1015 set y(z) {hello,}
1016 assemble {
1017 push z; push world; lappendArray y
1018 }
1019 "
1020 x
1021 }
1022 -result {hello, world}
1023 -cleanup {rename x {}}
1024 }
1025 test assemble-8.18 {store1} {
1026 -body {
1027 proc x {} {
1028 assemble {
1029 push test; store y
1030 }
1031 set y
1032 }
1033 x
1034 }
1035 -result {test}
1036 -cleanup {rename x {}}
1037 }
1038 test assemble-8.19 {store4} {
1039 -body {
1040 proc x {} "
1041 [fillTables]
1042 assemble {
1043 push test; store y
1044 }
1045 set y
1046 "
1047 x
1048 }
1049 -result test
1050 -cleanup {rename x {}}
1051 }
1052 test assemble-8.20 {storeArray1} {
1053 -body {
1054 proc x {} {
1055 assemble {
1056 push z; push test; storeArray y
1057 }
1058 set y(z)
1059 }
1060 x
1061 }
1062 -result test
1063 -cleanup {rename x {}}
1064 }
1065 test assemble-8.21 {storeArray4} {
1066 -body {
1067 proc x {} "
1068 [fillTables]
1069 assemble {
1070 push z; push test; storeArray y
1071 }
1072 "
1073 x
1074 }
1075 -result test
1076 -cleanup {rename x {}}
1077 }
1078
1079 # assemble-9 - ASSEM_CONCAT1, GetIntegerOperand, CheckOneByte
1080
1081 test assemble-9.1 {wrong # args} {
1082 -body {assemble concat}
1083 -result {wrong # args*}
1084 -match glob
1085 -returnCodes error
1086 }
1087 test assemble-9.2 {wrong # args} {
1088 -body {assemble {concat too many}}
1089 -result {wrong # args*}
1090 -match glob
1091 -returnCodes error
1092 }
1093 test assemble-9.3 {not a number} {
1094 -body {assemble {concat rubbish}}
1095 -result {expected integer but got "rubbish"}
1096 -returnCodes error
1097 }
1098 test assemble-9.4 {too small} {
1099 -body {assemble {concat -1}}
1100 -result {operand does not fit in one byte}
1101 -returnCodes error
1102 }
1103 test assemble-9.5 {too small} {
1104 -body {assemble {concat 256}}
1105 -result {operand does not fit in one byte}
1106 -returnCodes error
1107 }
1108 test assemble-9.6 {concat} {
1109 -body {
1110 assemble {push h; push e; push l; push l; push o; concat 5}
1111 }
1112 -result hello
1113 }
1114 test assemble-9.7 {concat} {
1115 -body {
1116 list [catch {assemble {concat 0}} result] $result $::errorCode
1117 }
1118 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
1119 -cleanup {unset result}
1120 }
1121
1122 # assemble-10 -- eval and expr
1123
1124 test assemble-10.1 {eval - wrong # args} {
1125 -body {
1126 assemble {eval}
1127 }
1128 -returnCodes error
1129 -match glob
1130 -result {wrong # args*}
1131 }
1132 test assemble-10.2 {eval - wrong # args} {
1133 -body {
1134 assemble {eval too many}
1135 }
1136 -returnCodes error
1137 -match glob
1138 -result {wrong # args*}
1139 }
1140 test assemble-10.3 {eval} {
1141 -body {
1142 proc x {} {
1143 assemble {
1144 push 3
1145 store n
1146 pop
1147 eval {expr {3*$n + 1}}
1148 push 1
1149 add
1150 }
1151 }
1152 x
1153 }
1154 -result 11
1155 -cleanup {rename x {}}
1156 }
1157 test assemble-10.4 {expr} {
1158 -body {
1159 proc x {} {
1160 assemble {
1161 push 3
1162 store n
1163 pop
1164 expr {3*$n + 1}
1165 push 1
1166 add
1167 }
1168 }
1169 x
1170 }
1171 -result 11
1172 -cleanup {rename x {}}
1173 }
1174 test assemble-10.5 {eval and expr - nonsimple} {
1175 -body {
1176 proc x {} {
1177 assemble {
1178 eval "s\x65t n 3"
1179 pop
1180 expr "\x33*\$n + 1"
1181 push 1
1182 add
1183 }
1184 }
1185 x
1186 }
1187 -result 11
1188 -cleanup {
1189 rename x {}
1190 }
1191 }
1192 test assemble-10.6 {eval - noncompilable} {
1193 -body {
1194 list [catch {assemble {eval $x}} result] $result $::errorCode
1195 }
1196 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
1197 }
1198 test assemble-10.7 {expr - noncompilable} {
1199 -body {
1200 list [catch {assemble {expr $x}} result] $result $::errorCode
1201 }
1202 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
1203 }
1204
1205 # assemble-11 - ASSEM_LVT4 (exist, existArray, dictAppend, dictLappend,
1206 # nsupvar, variable, upvar)
1207
1208 test assemble-11.1 {exist - wrong # args} {
1209 -body {
1210 assemble {exist}
1211 }
1212 -returnCodes error
1213 -match glob
1214 -result {wrong # args*}
1215 }
1216 test assemble-11.2 {exist - wrong # args} {
1217 -body {
1218 assemble {exist too many}
1219 }
1220 -returnCodes error
1221 -match glob
1222 -result {wrong # args*}
1223 }
1224 test assemble-11.3 {nonlocal var} {
1225 -body {
1226 list [catch {assemble {exist ::env}} result] $result $errorCode
1227 }
1228 -result {1 {variable "::env" is not local} {TCL ASSEM NONLOCAL ::env}}
1229 -cleanup {unset result}
1230 }
1231 test assemble-11.4 {exist} {
1232 -body {
1233 proc x {} {
1234 set y z
1235 list [assemble {exist y}] \
1236 [assemble {exist z}]
1237 }
1238 x
1239 }
1240 -result {1 0}
1241 -cleanup {rename x {}}
1242 }
1243 test assemble-11.5 {existArray} {
1244 -body {
1245 proc x {} {
1246 set a(b) c
1247 list [assemble {push b; existArray a}] \
1248 [assemble {push c; existArray a}] \
1249 [assemble {push a; existArray b}]
1250 }
1251 x
1252 }
1253 -result {1 0 0}
1254 -cleanup {rename x {}}
1255 }
1256 test assemble-11.6 {dictAppend} {
1257 -body {
1258 proc x {} {
1259 set dict {a 1 b 2 c 3}
1260 assemble {push b; push 22; dictAppend dict}
1261 }
1262 x
1263 }
1264 -result {a 1 b 222 c 3}
1265 -cleanup {rename x {}}
1266 }
1267 test assemble-11.7 {dictLappend} {
1268 -body {
1269 proc x {} {
1270 set dict {a 1 b 2 c 3}
1271 assemble {push b; push 2; dictLappend dict}
1272 }
1273 x
1274 }
1275 -result {a 1 b {2 2} c 3}
1276 -cleanup {rename x {}}
1277 }
1278 test assemble-11.8 {upvar} {
1279 -body {
1280 proc x {v} {
1281 assemble {push 1; load v; upvar w; pop; load w}
1282 }
1283 proc y {} {
1284 set z 123
1285 x z
1286 }
1287 y
1288 }
1289 -result 123
1290 -cleanup {rename x {}; rename y {}}
1291 }
1292 test assemble-11.9 {nsupvar} {
1293 -body {
1294 namespace eval q { variable v 123 }
1295 proc x {} {
1296 assemble {push q; push v; nsupvar y; pop; load y}
1297 }
1298 x
1299 }
1300 -result 123
1301 -cleanup {namespace delete q; rename x {}}
1302 }
1303 test assemble-11.10 {variable} {
1304 -body {
1305 namespace eval q { namespace eval r {variable v 123}}
1306 proc x {} {
1307 assemble {push q::r::v; variable y; load y}
1308 }
1309 x
1310 }
1311 -result 123
1312 -cleanup {namespace delete q; rename x {}}
1313 }
1314
1315 # assemble-12 - ASSEM_LVT1 (incr and incrArray)
1316
1317 test assemble-12.1 {incr - wrong # args} {
1318 -body {
1319 assemble {incr}
1320 }
1321 -returnCodes error
1322 -match glob
1323 -result {wrong # args*}
1324 }
1325 test assemble-12.2 {incr - wrong # args} {
1326 -body {
1327 assemble {incr too many}
1328 }
1329 -returnCodes error
1330 -match glob
1331 -result {wrong # args*}
1332 }
1333 test assemble-12.3 {incr nonlocal var} {
1334 -body {
1335 list [catch {assemble {incr ::env}} result] $result $errorCode
1336 }
1337 -result {1 {variable "::env" is not local} {TCL ASSEM NONLOCAL ::env}}
1338 -cleanup {unset result}
1339 }
1340 test assemble-12.4 {incr} {
1341 -body {
1342 proc x {} {
1343 set y 5
1344 assemble {push 3; incr y}
1345 }
1346 x
1347 }
1348 -result 8
1349 -cleanup {rename x {}}
1350 }
1351 test assemble-12.5 {incrArray} {
1352 -body {
1353 proc x {} {
1354 set a(b) 5
1355 assemble {push b; push 3; incrArray a}
1356 }
1357 x
1358 }
1359 -result 8
1360 -cleanup {rename x {}}
1361 }
1362 test assemble-12.6 {incr, stupid stack restriction} {
1363 -body {
1364 proc x {} "
1365 [fillTables]
1366 set y 5
1367 assemble {push 3; incr y}
1368 "
1369 list [catch {x} result] $result $errorCode
1370 }
1371 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1372 -cleanup {unset result; rename x {}}
1373 }
1374
1375 # assemble-13 -- ASSEM_LVT1_SINT1 - incrImm and incrArrayImm
1376
1377 test assemble-13.1 {incrImm - wrong # args} {
1378 -body {
1379 assemble {incrImm x}
1380 }
1381 -returnCodes error
1382 -match glob
1383 -result {wrong # args*}
1384 }
1385 test assemble-13.2 {incrImm - wrong # args} {
1386 -body {
1387 assemble {incrImm too many args}
1388 }
1389 -returnCodes error
1390 -match glob
1391 -result {wrong # args*}
1392 }
1393 test assemble-13.3 {incrImm nonlocal var} {
1394 -body {
1395 list [catch {assemble {incrImm ::env 2}} result] $result $errorCode
1396 }
1397 -result {1 {variable "::env" is not local} {TCL ASSEM NONLOCAL ::env}}
1398 -cleanup {unset result}
1399 }
1400 test assemble-13.4 {incrImm not a number} {
1401 -body {
1402 proc x {} {
1403 assemble {incrImm x rubbish}
1404 }
1405 x
1406 }
1407 -returnCodes error
1408 -result {expected integer but got "rubbish"}
1409 -cleanup {rename x {}}
1410 }
1411 test assemble-13.5 {incrImm too big} {
1412 -body {
1413 proc x {} {
1414 assemble {incrImm x 0x80}
1415 }
1416 list [catch x result] $result $::errorCode
1417 }
1418 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1419 -cleanup {rename x {}; unset result}
1420 }
1421 test assemble-13.6 {incrImm too small} {
1422 -body {
1423 proc x {} {
1424 assemble {incrImm x -0x81}
1425 }
1426 list [catch x result] $result $::errorCode
1427 }
1428 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1429 -cleanup {rename x {}; unset result}
1430 }
1431 test assemble-13.7 {incrImm} {
1432 -body {
1433 proc x {} {
1434 set y 1
1435 list [assemble {incrImm y -0x80}] [assemble {incrImm y 0x7f}]
1436 }
1437 x
1438 }
1439 -result {-127 0}
1440 -cleanup {rename x {}}
1441 }
1442 test assemble-13.8 {incrArrayImm} {
1443 -body {
1444 proc x {} {
1445 set a(b) 5
1446 assemble {push b; incrArrayImm a 3}
1447 }
1448 x
1449 }
1450 -result 8
1451 -cleanup {rename x {}}
1452 }
1453 test assemble-13.9 {incrImm, stupid stack restriction} {
1454 -body {
1455 proc x {} "
1456 [fillTables]
1457 set y 5
1458 assemble {incrImm y 3}
1459 "
1460 list [catch {x} result] $result $errorCode
1461 }
1462 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1463 -cleanup {unset result; rename x {}}
1464 }
1465
1466 # assemble-14 -- ASSEM_SINT1 (incrArrayStkImm and incrStkImm)
1467
1468 test assemble-14.1 {incrStkImm - wrong # args} {
1469 -body {
1470 assemble {incrStkImm}
1471 }
1472 -returnCodes error
1473 -match glob
1474 -result {wrong # args*}
1475 }
1476 test assemble-14.2 {incrStkImm - wrong # args} {
1477 -body {
1478 assemble {incrStkImm too many}
1479 }
1480 -returnCodes error
1481 -match glob
1482 -result {wrong # args*}
1483 }
1484 test assemble-14.3 {incrStkImm not a number} {
1485 -body {
1486 proc x {} {
1487 assemble {incrStkImm rubbish}
1488 }
1489 x
1490 }
1491 -returnCodes error
1492 -result {expected integer but got "rubbish"}
1493 -cleanup {rename x {}}
1494 }
1495 test assemble-14.4 {incrStkImm too big} {
1496 -body {
1497 proc x {} {
1498 assemble {incrStkImm 0x80}
1499 }
1500 list [catch x result] $result $::errorCode
1501 }
1502 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1503 -cleanup {rename x {}; unset result}
1504 }
1505 test assemble-14.5 {incrStkImm too small} {
1506 -body {
1507 proc x {} {
1508 assemble {incrStkImm -0x81}
1509 }
1510 list [catch x result] $result $::errorCode
1511 }
1512 -result {1 {operand does not fit in one byte} {TCL ASSEM 1BYTE}}
1513 -cleanup {rename x {}; unset result}
1514 }
1515 test assemble-14.6 {incrStkImm} {
1516 -body {
1517 proc x {} {
1518 set y 1
1519 list [assemble {push y; incrStkImm -0x80}] \
1520 [assemble {push y; incrStkImm 0x7f}]
1521 }
1522 x
1523 }
1524 -result {-127 0}
1525 -cleanup {rename x {}}
1526 }
1527 test assemble-14.7 {incrArrayStkImm} {
1528 -body {
1529 proc x {} {
1530 set a(b) 5
1531 assemble {push a; push b; incrArrayStkImm 3}
1532 }
1533 x
1534 }
1535 -result 8
1536 -cleanup {rename x {}}
1537 }
1538
1539 # assemble-15 - listIndexImm
1540
1541 test assemble-15.1 {listIndexImm - wrong # args} -body {
1542 assemble {listIndexImm}
1543 } -returnCodes error -match glob -result {wrong # args*}
1544 test assemble-15.2 {listIndexImm - wrong # args} -body {
1545 assemble {listIndexImm too many}
1546 } -returnCodes error -match glob -result {wrong # args*}
1547 test assemble-15.3 {listIndexImm - bad substitution} -body {
1548 list [catch {assemble {listIndexImm $foo}} result] $result $::errorCode
1549 } -cleanup {
1550 unset result
1551 } -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
1552 test assemble-15.4 {listIndexImm - invalid index} -body {
1553 assemble {listIndexImm rubbish}
1554 } -returnCodes error -match glob -result {bad index "rubbish"*}
1555 test assemble-15.5 {listIndexImm} -body {
1556 assemble {push {a b c}; listIndexImm 2}
1557 } -result c
1558 test assemble-15.6 {listIndexImm} -body {
1559 assemble {push {a b c}; listIndexImm end-1}
1560 } -result b
1561 test assemble-15.7 {listIndexImm} -body {
1562 assemble {push {a b c}; listIndexImm end}
1563 } -result c
1564 test assemble-15.8 {listIndexImm} -body {
1565 assemble {push {a b c}; listIndexImm end+2}
1566 } -result {}
1567 test assemble-15.9 {listIndexImm} -body {
1568 assemble {push {a b c}; listIndexImm -1-1}
1569 } -result {}
1570
1571 # assemble-16 - invokeStk
1572
1573 test assemble-16.1 {invokeStk - wrong # args} {
1574 -body {
1575 assemble {invokeStk}
1576 }
1577 -returnCodes error
1578 -match glob
1579 -result {wrong # args*}
1580 }
1581 test assemble-16.2 {invokeStk - wrong # args} {
1582 -body {
1583 assemble {invokeStk too many}
1584 }
1585 -returnCodes error
1586 -match glob
1587 -result {wrong # args*}
1588 }
1589 test assemble-16.3 {invokeStk - not a number} {
1590 -body {
1591 proc x {} {
1592 assemble {invokeStk rubbish}
1593 }
1594 x
1595 }
1596 -returnCodes error
1597 -result {expected integer but got "rubbish"}
1598 -cleanup {rename x {}}
1599 }
1600 test assemble-16.4 {invokeStk - no operands} {
1601 -body {
1602 proc x {} {
1603 assemble {invokeStk 0}
1604 }
1605 list [catch x result] $result $::errorCode
1606 }
1607 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
1608 -cleanup {rename x {}; unset result}
1609 }
1610 test assemble-16.5 {invokeStk1} {
1611 -body {
1612 tcl::unsupported::assemble {push concat; push 1; push 2; invokeStk 3}
1613 }
1614 -result {1 2}
1615 }
1616 test assemble-16.6 {invokeStk4} {
1617 -body {
1618 proc x {n} {
1619 set code {push concat}
1620 set shouldbe {}
1621 for {set i 1} {$i < $n} {incr i} {
1622 append code \n {push a} $i
1623 lappend shouldbe a$i
1624 }
1625 append code \n {invokeStk} { } $n
1626 set is [assemble $code]
1627 expr {$is eq $shouldbe}
1628 }
1629 list [x 254] [x 255] [x 256] [x 257]
1630 }
1631 -result {1 1 1 1}
1632 -cleanup {rename x {}}
1633 }
1634
1635 # assemble-17 -- jumps and labels
1636
1637 test assemble-17.1 {label, wrong # args} {
1638 -body {
1639 assemble {label}
1640 }
1641 -returnCodes error
1642 -match glob
1643 -result {wrong # args*}
1644 }
1645 test assemble-17.2 {label, wrong # args} {
1646 -body {
1647 assemble {label too many}
1648 }
1649 -returnCodes error
1650 -match glob
1651 -result {wrong # args*}
1652 }
1653 test assemble-17.3 {label, bad subst} {
1654 -body {
1655 list [catch {assemble {label $foo}} result] $result $::errorCode
1656 }
1657 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
1658 -cleanup {unset result}
1659 }
1660 test assemble-17.4 {duplicate label} {
1661 -body {
1662 list [catch {assemble {label foo; label foo}} result] \
1663 $result $::errorCode
1664 }
1665 -result {1 {duplicate definition of label "foo"} {TCL ASSEM DUPLABEL foo}}
1666 }
1667 test assemble-17.5 {jump, wrong # args} {
1668 -body {
1669 assemble {jump}
1670 }
1671 -returnCodes error
1672 -match glob
1673 -result {wrong # args*}
1674 }
1675 test assemble-17.6 {jump, wrong # args} {
1676 -body {
1677 assemble {jump too many}
1678 }
1679 -returnCodes error
1680 -match glob
1681 -result {wrong # args*}
1682 }
1683 test assemble-17.7 {jump, bad subst} {
1684 -body {
1685 list [catch {assemble {jump $foo}} result] $result $::errorCode
1686 }
1687 -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}}
1688 -cleanup {unset result}
1689 }
1690 test assemble-17.8 {jump - ahead and back} {
1691 -body {
1692 assemble {
1693 jump three
1694
1695 label one
1696 push a
1697 jump four
1698
1699 label two
1700 push b
1701 jump six
1702
1703 label three
1704 push c
1705 jump five
1706
1707 label four
1708 push d
1709 jump two
1710
1711 label five
1712 push e
1713 jump one
1714
1715 label six
1716 push f
1717 concat 6
1718 }
1719 }
1720 -result ceadbf
1721 }
1722 test assemble-17.9 {jump - resolve a label multiple times} {
1723 -body {
1724 proc x {} {
1725 set case 0
1726 set result {}
1727 assemble {
1728 jump common
1729
1730 label zero
1731 pop
1732 incrImm case 1
1733 pop
1734 push a
1735 append result
1736 pop
1737 jump common
1738
1739 label one
1740 pop
1741 incrImm case 1
1742 pop
1743 push b
1744 append result
1745 pop
1746 jump common
1747
1748 label common
1749 load case
1750 dup
1751 push 0
1752 eq
1753 jumpTrue zero
1754 dup
1755 push 1
1756 eq
1757 jumpTrue one
1758 dup
1759 push 2
1760 eq
1761 jumpTrue two
1762 dup
1763 push 3
1764 eq
1765 jumpTrue three
1766
1767 label two
1768 pop
1769 incrImm case 1
1770 pop
1771 push c
1772 append result
1773 pop
1774 jump common
1775
1776 label three
1777 pop
1778 incrImm case 1
1779 pop
1780 push d
1781 append result
1782 }
1783 }
1784 x
1785 }
1786 -result abcd
1787 -cleanup {rename x {}}
1788 }
1789 test assemble-17.10 {jump4 needed} {
1790 -body {
1791 assemble "push x; jump one; label two; [string repeat {dup; pop;} 128]
1792 jump three; label one; jump two; label three"
1793 }
1794 -result x
1795 }
1796 test assemble-17.11 {jumpTrue} {
1797 -body {
1798 proc x {y} {
1799 assemble {
1800 load y
1801 jumpTrue then
1802 push no
1803 jump else
1804 label then
1805 push yes
1806 label else
1807 }
1808 }
1809 list [x 0] [x 1]
1810 }
1811 -result {no yes}
1812 -cleanup {rename x {}}
1813 }
1814 test assemble-17.12 {jumpFalse} {
1815 -body {
1816 proc x {y} {
1817 assemble {
1818 load y
1819 jumpFalse then
1820 push no
1821 jump else
1822 label then
1823 push yes
1824 label else
1825 }
1826 }
1827 list [x 0] [x 1]
1828 }
1829 -result {yes no}
1830 -cleanup {rename x {}}
1831 }
1832 test assemble-17.13 {jump to undefined label} {
1833 -body {
1834 list [catch {assemble {jump nowhere}} result] $result $::errorCode
1835 }
1836 -result {1 {undefined label "nowhere"} {TCL ASSEM NOLABEL nowhere}}
1837 }
1838 test assemble-17.14 {jump to undefined label, line number correct?} {
1839 -body {
1840 catch {assemble {#1
1841 #2
1842 #3
1843 jump nowhere
1844 #5
1845 #6
1846 }}
1847 set ::errorInfo
1848 }
1849 -match glob
1850 -result {*"assemble" body, line 4*}
1851 }
1852 test assemble-17.15 {multiple passes of code resizing} {
1853 -setup {
1854 set body {
1855 push -
1856 }
1857 for {set i 0} {$i < 14} {incr i} {
1858 append body "label a" $i \
1859 "; push a; concat 2; nop; nop; jump b" \
1860 $i \n
1861 }
1862 append body {label a14; push a; concat 2; push 1; jumpTrue b14} \n
1863 append body {label a15; push a; concat 2; push 0; jumpFalse b15} \n
1864 for {set i 0} {$i < 15} {incr i} {
1865 append body "label b" $i \
1866 "; push b; concat 2; nop; nop; jump a" \
1867 [expr {$i+1}] \n
1868 }
1869 append body {label c; push -; concat 2; nop; nop; nop; jump d} \n
1870 append body {label b15; push b; concat 2; nop; nop; jump c} \n
1871 append body {label d}
1872 proc x {} [list assemble $body]
1873 }
1874 -body {
1875 x
1876 }
1877 -cleanup {
1878 catch {unset body}
1879 catch {rename x {}}
1880 }
1881 -result -abababababababababababababababab-
1882 }
1883
1884 # assemble-18 - lindexMulti
1885
1886 test assemble-18.1 {lindexMulti - wrong # args} {
1887 -body {
1888 assemble {lindexMulti}
1889 }
1890 -returnCodes error
1891 -match glob
1892 -result {wrong # args*}
1893 }
1894 test assemble-18.2 {lindexMulti - wrong # args} {
1895 -body {
1896 assemble {lindexMulti too many}
1897 }
1898 -returnCodes error
1899 -match glob
1900 -result {wrong # args*}
1901 }
1902 test assemble-18.3 {lindexMulti - bad subst} {
1903 -body {
1904 assemble {lindexMulti $foo}
1905 }
1906 -returnCodes error
1907 -match glob
1908 -result {assembly code may not contain substitutions}
1909 }
1910 test assemble-18.4 {lindexMulti - not a number} {
1911 -body {
1912 proc x {} {
1913 assemble {lindexMulti rubbish}
1914 }
1915 x
1916 }
1917 -returnCodes error
1918 -result {expected integer but got "rubbish"}
1919 -cleanup {rename x {}}
1920 }
1921 test assemble-18.5 {lindexMulti - bad operand count} {
1922 -body {
1923 proc x {} {
1924 assemble {lindexMulti 0}
1925 }
1926 list [catch x result] $result $::errorCode
1927 }
1928 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
1929 -cleanup {rename x {}; unset result}
1930 }
1931 test assemble-18.6 {lindexMulti} {
1932 -body {
1933 assemble {push {{a b c} {d e f} {g h j}}; lindexMulti 1}
1934 }
1935 -result {{a b c} {d e f} {g h j}}
1936 }
1937 test assemble-18.7 {lindexMulti} {
1938 -body {
1939 assemble {push {{a b c} {d e f} {g h j}}; push 1; lindexMulti 2}
1940 }
1941 -result {d e f}
1942 }
1943 test assemble-18.8 {lindexMulti} {
1944 -body {
1945 assemble {push {{a b c} {d e f} {g h j}}; push 2; push 1; lindexMulti 3}
1946 }
1947 -result h
1948 }
1949
1950 # assemble-19 - list
1951
1952 test assemble-19.1 {list - wrong # args} {
1953 -body {
1954 assemble {list}
1955 }
1956 -returnCodes error
1957 -match glob
1958 -result {wrong # args*}
1959 }
1960 test assemble-19.2 {list - wrong # args} {
1961 -body {
1962 assemble {list too many}
1963 }
1964 -returnCodes error
1965 -match glob
1966 -result {wrong # args*}
1967 }
1968 test assemble-19.3 {list - bad subst} {
1969 -body {
1970 assemble {list $foo}
1971 }
1972 -returnCodes error
1973 -match glob
1974 -result {assembly code may not contain substitutions}
1975 }
1976 test assemble-19.4 {list - not a number} {
1977 -body {
1978 proc x {} {
1979 assemble {list rubbish}
1980 }
1981 x
1982 }
1983 -returnCodes error
1984 -result {expected integer but got "rubbish"}
1985 -cleanup {rename x {}}
1986 }
1987 test assemble-19.5 {list - negative operand count} {
1988 -body {
1989 proc x {} {
1990 assemble {list -1}
1991 }
1992 list [catch x result] $result $::errorCode
1993 }
1994 -result {1 {operand must be nonnegative} {TCL ASSEM NONNEGATIVE}}
1995 -cleanup {rename x {}; unset result}
1996 }
1997 test assemble-19.6 {list - no args} {
1998 -body {
1999 assemble {list 0}
2000 }
2001 -result {}
2002 }
2003 test assemble-19.7 {list - 1 arg} {
2004 -body {
2005 assemble {push hello; list 1}
2006 }
2007 -result hello
2008 }
2009 test assemble-19.8 {list - 2 args} {
2010 -body {
2011 assemble {push hello; push world; list 2}
2012 }
2013 -result {hello world}
2014 }
2015
2016 # assemble-20 - lsetFlat
2017
2018 test assemble-20.1 {lsetFlat - wrong # args} {
2019 -body {
2020 assemble {lsetFlat}
2021 }
2022 -returnCodes error
2023 -match glob
2024 -result {wrong # args*}
2025 }
2026 test assemble-20.2 {lsetFlat - wrong # args} {
2027 -body {
2028 assemble {lsetFlat too many}
2029 }
2030 -returnCodes error
2031 -match glob
2032 -result {wrong # args*}
2033 }
2034 test assemble-20.3 {lsetFlat - bad subst} {
2035 -body {
2036 assemble {lsetFlat $foo}
2037 }
2038 -returnCodes error
2039 -match glob
2040 -result {assembly code may not contain substitutions}
2041 }
2042 test assemble-20.4 {lsetFlat - not a number} {
2043 -body {
2044 proc x {} {
2045 assemble {lsetFlat rubbish}
2046 }
2047 x
2048 }
2049 -returnCodes error
2050 -result {expected integer but got "rubbish"}
2051 -cleanup {rename x {}}
2052 }
2053 test assemble-20.5 {lsetFlat - negative operand count} {
2054 -body {
2055 proc x {} {
2056 assemble {lsetFlat 1}
2057 }
2058 list [catch x result] $result $::errorCode
2059 }
2060 -result {1 {operand must be >=2} {TCL ASSEM OPERAND>=2}}
2061 -cleanup {rename x {}; unset result}
2062 }
2063 test assemble-20.6 {lsetFlat} {
2064 -body {
2065 assemble {push b; push a; lsetFlat 2}
2066 }
2067 -result b
2068 }
2069 test assemble-20.7 {lsetFlat} {
2070 -body {
2071 assemble {push 1; push d; push {a b c}; lsetFlat 3}
2072 }
2073 -result {a d c}
2074 }
2075
2076 # assemble-21 - over
2077
2078 test assemble-21.1 {over - wrong # args} {
2079 -body {
2080 assemble {over}
2081 }
2082 -returnCodes error
2083 -match glob
2084 -result {wrong # args*}
2085 }
2086 test assemble-21.2 {over - wrong # args} {
2087 -body {
2088 assemble {over too many}
2089 }
2090 -returnCodes error
2091 -match glob
2092 -result {wrong # args*}
2093 }
2094 test assemble-21.3 {over - bad subst} {
2095 -body {
2096 assemble {over $foo}
2097 }
2098 -returnCodes error
2099 -match glob
2100 -result {assembly code may not contain substitutions}
2101 }
2102 test assemble-21.4 {over - not a number} {
2103 -body {
2104 proc x {} {
2105 assemble {over rubbish}
2106 }
2107 x
2108 }
2109 -returnCodes error
2110 -result {expected integer but got "rubbish"}
2111 -cleanup {rename x {}}
2112 }
2113 test assemble-21.5 {over - negative operand count} {
2114 -body {
2115 proc x {} {
2116 assemble {over -1}
2117 }
2118 list [catch x result] $result $::errorCode
2119 }
2120 -result {1 {operand must be nonnegative} {TCL ASSEM NONNEGATIVE}}
2121 -cleanup {rename x {}; unset result}
2122 }
2123 test assemble-21.6 {over} {
2124 -body {
2125 proc x {} {
2126 assemble {
2127 push 1
2128 push 2
2129 push 3
2130 over 0
2131 store x
2132 pop
2133 pop
2134 pop
2135 pop
2136 load x
2137 }
2138 }
2139 x
2140 }
2141 -result 3
2142 -cleanup {rename x {}}
2143 }
2144 test assemble-21.7 {over} {
2145 -body {
2146 proc x {} {
2147 assemble {
2148 push 1
2149 push 2
2150 push 3
2151 over 2
2152 store x
2153 pop
2154 pop
2155 pop
2156 pop
2157 load x
2158 }
2159 }
2160 x
2161 }
2162 -result 1
2163 -cleanup {rename x {}}
2164 }
2165
2166 # assemble-22 - reverse
2167
2168 test assemble-22.1 {reverse - wrong # args} {
2169 -body {
2170 assemble {reverse}
2171 }
2172 -returnCodes error
2173 -match glob
2174 -result {wrong # args*}
2175 }
2176 test assemble-22.2 {reverse - wrong # args} {
2177 -body {
2178 assemble {reverse too many}
2179 }
2180 -returnCodes error
2181 -match glob
2182 -result {wrong # args*}
2183 }
2184
2185 test assemble-22.3 {reverse - bad subst} {
2186 -body {
2187 assemble {reverse $foo}
2188 }
2189 -returnCodes error
2190 -match glob
2191 -result {assembly code may not contain substitutions}
2192 }
2193
2194 test assemble-22.4 {reverse - not a number} {
2195 -body {
2196 proc x {} {
2197 assemble {reverse rubbish}
2198 }
2199 x
2200 }
2201 -returnCodes error
2202 -result {expected integer but got "rubbish"}
2203 -cleanup {rename x {}}
2204 }
2205 test assemble-22.5 {reverse - negative operand count} {
2206 -body {
2207 proc x {} {
2208 assemble {reverse -1}
2209 }
2210 list [catch x result] $result $::errorCode
2211 }
2212 -result {1 {operand must be nonnegative} {TCL ASSEM NONNEGATIVE}}
2213 -cleanup {rename x {}; unset result}
2214 }
2215 test assemble-22.6 {reverse - zero operand count} {
2216 -body {
2217 proc x {} {
2218 assemble {push 1; reverse 0}
2219 }
2220 x
2221 }
2222 -result 1
2223 -cleanup {rename x {}}
2224 }
2225 test assemble-22.7 {reverse} {
2226 -body {
2227 proc x {} {
2228 assemble {
2229 push 1
2230 push 2
2231 push 3
2232 reverse 1
2233 store x
2234 pop
2235 pop
2236 pop
2237 load x
2238 }
2239 }
2240 x
2241 }
2242 -result 3
2243 -cleanup {rename x {}}
2244 }
2245 test assemble-22.8 {reverse} {
2246 -body {
2247 proc x {} {
2248 assemble {
2249 push 1
2250 push 2
2251 push 3
2252 reverse 3
2253 store x
2254 pop
2255 pop
2256 pop
2257 load x
2258 }
2259 }
2260 x
2261 }
2262 -result 1
2263 -cleanup {rename x {}}
2264 }
2265
2266 # assemble-23 - ASSEM_BOOL (strmatch, unsetStk, unsetArrayStk)
2267
2268 test assemble-23.1 {strmatch - wrong # args} {
2269 -body {
2270 assemble {strmatch}
2271 }
2272 -returnCodes error
2273 -match glob
2274 -result {wrong # args*}
2275 }
2276 test assemble-23.2 {strmatch - wrong # args} {
2277 -body {
2278 assemble {strmatch too many}
2279 }
2280 -returnCodes error
2281 -match glob
2282 -result {wrong # args*}
2283 }
2284 test assemble-23.3 {strmatch - bad subst} {
2285 -body {
2286 assemble {strmatch $foo}
2287 }
2288 -returnCodes error
2289 -match glob
2290 -result {assembly code may not contain substitutions}
2291 }
2292 test assemble-23.4 {strmatch - not a boolean} {
2293 -body {
2294 proc x {} {
2295 assemble {strmatch rubbish}
2296 }
2297 x
2298 }
2299 -returnCodes error
2300 -result {expected boolean value but got "rubbish"}
2301 -cleanup {rename x {}}
2302 }
2303 test assemble-23.5 {strmatch} {
2304 -body {
2305 proc x {a b} {
2306 list [assemble {load a; load b; strmatch 0}] \
2307 [assemble {load a; load b; strmatch 1}]
2308 }
2309 list [x foo*.grill fengbar.grill] [x foo*.grill foobar.grill] [x foo*.grill FOOBAR.GRILL]
2310 }
2311 -result {{0 0} {1 1} {0 1}}
2312 -cleanup {rename x {}}
2313 }
2314 test assemble-23.6 {unsetStk} {
2315 -body {
2316 proc x {} {
2317 set a {}
2318 assemble {push a; unsetStk false}
2319 info exists a
2320 }
2321 x
2322 }
2323 -result 0
2324 -cleanup {rename x {}}
2325 }
2326 test assemble-23.7 {unsetStk} {
2327 -body {
2328 proc x {} {
2329 assemble {push a; unsetStk false}
2330 info exists a
2331 }
2332 x
2333 }
2334 -result 0
2335 -cleanup {rename x {}}
2336 }
2337 test assemble-23.8 {unsetStk} {
2338 -body {
2339 proc x {} {
2340 assemble {push a; unsetStk true}
2341 info exists a
2342 }
2343 x
2344 }
2345 -returnCodes error
2346 -result {can't unset "a": no such variable}
2347 -cleanup {rename x {}}
2348 }
2349 test assemble-23.9 {unsetArrayStk} {
2350 -body {
2351 proc x {} {
2352 set a(b) {}
2353 assemble {push a; push b; unsetArrayStk false}
2354 info exists a(b)
2355 }
2356 x
2357 }
2358 -result 0
2359 -cleanup {rename x {}}
2360 }
2361 test assemble-23.10 {unsetArrayStk} {
2362 -body {
2363 proc x {} {
2364 assemble {push a; push b; unsetArrayStk false}
2365 info exists a(b)
2366 }
2367 x
2368 }
2369 -result 0
2370 -cleanup {rename x {}}
2371 }
2372 test assemble-23.11 {unsetArrayStk} {
2373 -body {
2374 proc x {} {
2375 assemble {push a; push b; unsetArrayStk true}
2376 info exists a(b)
2377 }
2378 x
2379 }
2380 -returnCodes error
2381 -result {can't unset "a(b)": no such variable}
2382 -cleanup {rename x {}}
2383 }
2384
2385 # assemble-24 -- ASSEM_BOOL_LVT4 (unset; unsetArray)
2386
2387 test assemble-24.1 {unset - wrong # args} {
2388 -body {
2389 assemble {unset one}
2390 }
2391 -returnCodes error
2392 -match glob
2393 -result {wrong # args*}
2394 }
2395 test assemble-24.2 {unset - wrong # args} {
2396 -body {
2397 assemble {unset too many args}
2398 }
2399 -returnCodes error
2400 -match glob
2401 -result {wrong # args*}
2402 }
2403 test assemble-24.3 {unset - bad subst -arg 1} {
2404 -body {
2405 assemble {unset $foo bar}
2406 }
2407 -returnCodes error
2408 -match glob
2409 -result {assembly code may not contain substitutions}
2410 }
2411 test assemble-24.4 {unset - not a boolean} {
2412 -body {
2413 proc x {} {
2414 assemble {unset rubbish trash}
2415 }
2416 x
2417 }
2418 -returnCodes error
2419 -result {expected boolean value but got "rubbish"}
2420 -cleanup {rename x {}}
2421 }
2422 test assemble-24.5 {unset - bad subst - arg 2} {
2423 -body {
2424 assemble {unset true $bar}
2425 }
2426 -returnCodes error
2427 -result {assembly code may not contain substitutions}
2428 }
2429 test assemble-24.6 {unset - nonlocal var} {
2430 -body {
2431 assemble {unset true ::foo::bar}
2432 }
2433 -returnCodes error
2434 -result {variable "::foo::bar" is not local}
2435 }
2436 test assemble-24.7 {unset} {
2437 -body {
2438 proc x {} {
2439 set a {}
2440 assemble {unset false a}
2441 info exists a
2442 }
2443 x
2444 }
2445 -result 0
2446 -cleanup {rename x {}}
2447 }
2448 test assemble-24.8 {unset} {
2449 -body {
2450 proc x {} {
2451 assemble {unset false a}
2452 info exists a
2453 }
2454 x
2455 }
2456 -result 0
2457 -cleanup {rename x {}}
2458 }
2459 test assemble-24.9 {unset} {
2460 -body {
2461 proc x {} {
2462 assemble {unset true a}
2463 info exists a
2464 }
2465 x
2466 }
2467 -returnCodes error
2468 -result {can't unset "a": no such variable}
2469 -cleanup {rename x {}}
2470 }
2471 test assemble-24.10 {unsetArray} {
2472 -body {
2473 proc x {} {
2474 set a(b) {}
2475 assemble {push b; unsetArray false a}
2476 info exists a(b)
2477 }
2478 x
2479 }
2480 -result 0
2481 -cleanup {rename x {}}
2482 }
2483 test assemble-24.11 {unsetArray} {
2484 -body {
2485 proc x {} {
2486 assemble {push b; unsetArray false a}
2487 info exists a(b)
2488 }
2489 x
2490 }
2491 -result 0
2492 -cleanup {rename x {}}
2493 }
2494 test assemble-24.12 {unsetArray} {
2495 -body {
2496 proc x {} {
2497 assemble {push b; unsetArray true a}
2498 info exists a(b)
2499 }
2500 x
2501 }
2502 -returnCodes error
2503 -result {can't unset "a(b)": no such variable}
2504 -cleanup {rename x {}}
2505 }
2506
2507 # assemble-25 - dict get
2508
2509 test assemble-25.1 {dict get - wrong # args} {
2510 -body {
2511 assemble {dictGet}
2512 }
2513 -returnCodes error
2514 -match glob
2515 -result {wrong # args*}
2516 }
2517 test assemble-25.2 {dict get - wrong # args} {
2518 -body {
2519 assemble {dictGet too many}
2520 }
2521 -returnCodes error
2522 -match glob
2523 -result {wrong # args*}
2524 }
2525 test assemble-25.3 {dictGet - bad subst} {
2526 -body {
2527 assemble {dictGet $foo}
2528 }
2529 -returnCodes error
2530 -match glob
2531 -result {assembly code may not contain substitutions}
2532 }
2533 test assemble-25.4 {dict get - not a number} {
2534 -body {
2535 proc x {} {
2536 assemble {dictGet rubbish}
2537 }
2538 x
2539 }
2540 -returnCodes error
2541 -result {expected integer but got "rubbish"}
2542 -cleanup {rename x {}}
2543 }
2544 test assemble-25.5 {dictGet - negative operand count} {
2545 -body {
2546 proc x {} {
2547 assemble {dictGet 0}
2548 }
2549 list [catch x result] $result $::errorCode
2550 }
2551 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
2552 -cleanup {rename x {}; unset result}
2553 }
2554 test assemble-25.6 {dictGet - 1 index} {
2555 -body {
2556 assemble {push {a 1 b 2}; push a; dictGet 1}
2557 }
2558 -result 1
2559 }
2560
2561 # assemble-26 - dict set
2562
2563 test assemble-26.1 {dict set - wrong # args} {
2564 -body {
2565 assemble {dictSet 1}
2566 }
2567 -returnCodes error
2568 -match glob
2569 -result {wrong # args*}
2570 }
2571 test assemble-26.2 {dict get - wrong # args} {
2572 -body {
2573 assemble {dictSet too many args}
2574 }
2575 -returnCodes error
2576 -match glob
2577 -result {wrong # args*}
2578 }
2579 test assemble-26.3 {dictSet - bad subst} {
2580 -body {
2581 assemble {dictSet 1 $foo}
2582 }
2583 -returnCodes error
2584 -match glob
2585 -result {assembly code may not contain substitutions}
2586 }
2587 test assemble-26.4 {dictSet - not a number} {
2588 -body {
2589 proc x {} {
2590 assemble {dictSet rubbish foo}
2591 }
2592 x
2593 }
2594 -returnCodes error
2595 -result {expected integer but got "rubbish"}
2596 -cleanup {rename x {}}
2597 }
2598 test assemble-26.5 {dictSet - zero operand count} {
2599 -body {
2600 proc x {} {
2601 assemble {dictSet 0 foo}
2602 }
2603 list [catch x result] $result $::errorCode
2604 }
2605 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
2606 -cleanup {rename x {}; unset result}
2607 }
2608 test assemble-26.6 {dictSet - bad local} {
2609 -body {
2610 proc x {} {
2611 assemble {dictSet 1 ::foo::bar}
2612 }
2613 list [catch x result] $result $::errorCode
2614 }
2615 -result {1 {variable "::foo::bar" is not local} {TCL ASSEM NONLOCAL ::foo::bar}}
2616 -cleanup {rename x {}; unset result}
2617 }
2618 test assemble-26.7 {dictSet} {
2619 -body {
2620 proc x {} {
2621 set dict {a 1 b 2 c 3}
2622 assemble {push b; push 4; dictSet 1 dict}
2623 }
2624 x
2625 }
2626 -result {a 1 b 4 c 3}
2627 -cleanup {rename x {}}
2628 }
2629
2630 # assemble-27 - dictUnset
2631
2632 test assemble-27.1 {dictUnset - wrong # args} {
2633 -body {
2634 assemble {dictUnset 1}
2635 }
2636 -returnCodes error
2637 -match glob
2638 -result {wrong # args*}
2639 }
2640 test assemble-27.2 {dictUnset - wrong # args} {
2641 -body {
2642 assemble {dictUnset too many args}
2643 }
2644 -returnCodes error
2645 -match glob
2646 -result {wrong # args*}
2647 }
2648 test assemble-27.3 {dictUnset - bad subst} {
2649 -body {
2650 assemble {dictUnset 1 $foo}
2651 }
2652 -returnCodes error
2653 -match glob
2654 -result {assembly code may not contain substitutions}
2655 }
2656 test assemble-27.4 {dictUnset - not a number} {
2657 -body {
2658 proc x {} {
2659 assemble {dictUnset rubbish foo}
2660 }
2661 x
2662 }
2663 -returnCodes error
2664 -result {expected integer but got "rubbish"}
2665 -cleanup {rename x {}}
2666 }
2667 test assemble-27.5 {dictUnset - zero operand count} {
2668 -body {
2669 proc x {} {
2670 assemble {dictUnset 0 foo}
2671 }
2672 list [catch x result] $result $::errorCode
2673 }
2674 -result {1 {operand must be positive} {TCL ASSEM POSITIVE}}
2675 -cleanup {rename x {}; unset result}
2676 }
2677 test assemble-27.6 {dictUnset - bad local} {
2678 -body {
2679 proc x {} {
2680 assemble {dictUnset 1 ::foo::bar}
2681 }
2682 list [catch x result] $result $::errorCode
2683 }
2684 -result {1 {variable "::foo::bar" is not local} {TCL ASSEM NONLOCAL ::foo::bar}}
2685 -cleanup {rename x {}; unset result}
2686 }
2687 test assemble-27.7 {dictUnset} {
2688 -body {
2689 proc x {} {
2690 set dict {a 1 b 2 c 3}
2691 assemble {push b; dictUnset 1 dict}
2692 }
2693 x
2694 }
2695 -result {a 1 c 3}
2696 -cleanup {rename x {}}
2697 }
2698
2699 # assemble-28 - dictIncrImm
2700
2701 test assemble-28.1 {dictIncrImm - wrong # args} {
2702 -body {
2703 assemble {dictIncrImm 1}
2704 }
2705 -returnCodes error
2706 -match glob
2707 -result {wrong # args*}
2708 }
2709 test assemble-28.2 {dictIncrImm - wrong # args} {
2710 -body {
2711 assemble {dictIncrImm too many args}
2712 }
2713 -returnCodes error
2714 -match glob
2715 -result {wrong # args*}
2716 }
2717 test assemble-28.3 {dictIncrImm - bad subst} {
2718 -body {
2719 assemble {dictIncrImm 1 $foo}
2720 }
2721 -returnCodes error
2722 -match glob
2723 -result {assembly code may not contain substitutions}
2724 }
2725 test assemble-28.4 {dictIncrImm - not a number} {
2726 -body {
2727 proc x {} {
2728 assemble {dictIncrImm rubbish foo}
2729 }
2730 x
2731 }
2732 -returnCodes error
2733 -result {expected integer but got "rubbish"}
2734 -cleanup {rename x {}}
2735 }
2736 test assemble-28.5 {dictIncrImm - bad local} {
2737 -body {
2738 proc x {} {
2739 assemble {dictIncrImm 1 ::foo::bar}
2740 }
2741 list [catch x result] $result $::errorCode
2742 }
2743 -result {1 {variable "::foo::bar" is not local} {TCL ASSEM NONLOCAL ::foo::bar}}
2744 -cleanup {rename x {}; unset result}
2745 }
2746 test assemble-28.6 {dictIncrImm} {
2747 -body {
2748 proc x {} {
2749 set dict {a 1 b 2 c 3}
2750 assemble {push b; dictIncrImm 42 dict}
2751 }
2752 x
2753 }
2754 -result {a 1 b 44 c 3}
2755 -cleanup {rename x {}}
2756 }
2757
2758 # assemble-29 - ASSEM_REGEXP
2759
2760 test assemble-29.1 {regexp - wrong # args} {
2761 -body {
2762 assemble {regexp}
2763 }
2764 -returnCodes error
2765 -match glob
2766 -result {wrong # args*}
2767 }
2768 test assemble-29.2 {regexp - wrong # args} {
2769 -body {
2770 assemble {regexp too many}
2771 }
2772 -returnCodes error
2773 -match glob
2774 -result {wrong # args*}
2775 }
2776 test assemble-29.3 {regexp - bad subst} {
2777 -body {
2778 assemble {regexp $foo}
2779 }
2780 -returnCodes error
2781 -match glob
2782 -result {assembly code may not contain substitutions}
2783 }
2784 test assemble-29.4 {regexp - not a boolean} {
2785 -body {
2786 proc x {} {
2787 assemble {regexp rubbish}
2788 }
2789 x
2790 }
2791 -returnCodes error
2792 -result {expected boolean value but got "rubbish"}
2793 -cleanup {rename x {}}
2794 }
2795 test assemble-29.5 {regexp} {
2796 -body {
2797 assemble {push br.*br; push abracadabra; regexp false}
2798 }
2799 -result 1
2800 }
2801 test assemble-29.6 {regexp} {
2802 -body {
2803 assemble {push br.*br; push aBRacadabra; regexp false}
2804 }
2805 -result 0
2806 }
2807 test assemble-29.7 {regexp} {
2808 -body {
2809 assemble {push br.*br; push aBRacadabra; regexp true}
2810 }
2811 -result 1
2812 }
2813
2814 # assemble-30 - Catches
2815
2816 test assemble-30.1 {simplest possible catch} {
2817 -body {
2818 proc x {} {
2819 assemble {
2820 beginCatch @bad
2821 push error
2822 push testing
2823 invokeStk 2
2824 pop
2825 push 0
2826 jump @ok
2827 label @bad
2828 push 1; # should be pushReturnCode
2829 label @ok
2830 endCatch
2831 }
2832 }
2833 x
2834 }
2835 -result 1
2836 -cleanup {rename x {}}
2837 }
2838 test assemble-30.2 {catch in external catch conntext} {
2839 -body {
2840 proc x {} {
2841 list [catch {
2842 assemble {
2843 beginCatch @bad
2844 push error
2845 push testing
2846 invokeStk 2
2847 pop
2848 push 0
2849 jump @ok
2850 label @bad
2851 pushReturnCode
2852 label @ok
2853 endCatch
2854 }
2855 } result] $result
2856 }
2857 x
2858 }
2859 -result {0 1}
2860 -cleanup {rename x {}}
2861 }
2862 test assemble-30.3 {embedded catches} {
2863 -body {
2864 proc x {} {
2865 list [catch {
2866 assemble {
2867 beginCatch @bad
2868 push error
2869 eval { list [catch {error whatever} result] $result }
2870 invokeStk 2
2871 push 0
2872 reverse 2
2873 jump @done
2874 label @bad
2875 pushReturnCode
2876 pushResult
2877 label @done
2878 endCatch
2879 list 2
2880 }
2881 } result2] $result2
2882 }
2883 x
2884 }
2885 -result {0 {1 {1 whatever}}}
2886 -cleanup {rename x {}}
2887 }
2888 test assemble-30.4 {throw in wrong context} {
2889 -body {
2890 proc x {} {
2891 list [catch {
2892 assemble {
2893 beginCatch @bad
2894 push error
2895 eval { list [catch {error whatever} result] $result }
2896 invokeStk 2
2897 push 0
2898 reverse 2
2899 jump @done
2900
2901 label @bad
2902 load x
2903 pushResult
2904
2905 label @done
2906 endCatch
2907 list 2
2908 }
2909 } result] $result $::errorCode [split $::errorInfo \n]
2910 }
2911 x
2912 }
2913 -match glob
2914 -result {1 {"loadScalar1" instruction may not appear in a context where an exception has been caught and not disposed of.} {TCL ASSEM BADTHROW} {{"loadScalar1" instruction may not appear in a context where an exception has been caught and not disposed of.} { in assembly code between lines 10 and 15}*}}
2915 -cleanup {rename x {}}
2916 }
2917 test assemble-30.5 {unclosed catch} {
2918 -body {
2919 proc x {} {
2920 assemble {
2921 beginCatch @error
2922 push 0
2923 jump @done
2924 label @error
2925 push 1
2926 label @done
2927 push ""
2928 pop
2929 }
2930 }
2931 list [catch {x} result] $result $::errorCode $::errorInfo
2932 }
2933 -match glob
2934 -result {1 {catch still active on exit from assembly code} {TCL ASSEM UNCLOSEDCATCH} {catch still active on exit from assembly code
2935 ("assemble" body, line 2)*}}
2936 -cleanup {rename x {}}
2937 }
2938 test assemble-30.6 {inconsistent catch contexts} {
2939 -body {
2940 proc x {y} {
2941 assemble {
2942 load y
2943 jumpTrue @inblock
2944 beginCatch @error
2945 label @inblock
2946 push 0
2947 jump @done
2948 label @error
2949 push 1
2950 label @done
2951 }
2952 }
2953 list [catch {x 2} result] $::errorCode $::errorInfo
2954 }
2955 -match glob
2956 -result {1 {TCL ASSEM BADCATCH} {execution reaches an instruction in inconsistent exception contexts
2957 ("assemble" body, line 5)*}}
2958 -cleanup {rename x {}}
2959 }
2960
2961 # assemble-31 - Jump tables
2962
2963 test assemble-31.1 {jumpTable, wrong # args} {
2964 -body {
2965 assemble {jumpTable}
2966 }
2967 -returnCodes error
2968 -match glob
2969 -result {wrong # args*}
2970 }
2971 test assemble-31.2 {jumpTable, wrong # args} {
2972 -body {
2973 assemble {jumpTable too many}
2974 }
2975 -returnCodes error
2976 -match glob
2977 -result {wrong # args*}
2978 }
2979 test assemble-31.3 {jumpTable - bad subst} {
2980 -body {
2981 assemble {jumpTable $foo}
2982 }
2983 -returnCodes error
2984 -match glob
2985 -result {assembly code may not contain substitutions}
2986 }
2987 test assemble-31.4 {jumptable - not a list} {
2988 -body {
2989 assemble {jumpTable \{rubbish}
2990 }
2991 -returnCodes error
2992 -result {unmatched open brace in list}
2993 }
2994 test assemble-31.5 {jumpTable, badly structured} {
2995 -body {
2996 list [catch {assemble {
2997 # line 2
2998 jumpTable {one two three};# line 3
2999 }} result] \
3000 $result $::errorCode $::errorInfo
3001 }
3002 -match glob
3003 -result {1 {jump table must have an even number of list elements} {TCL ASSEM BADJUMPTABLE} {jump table must have an even number of list elements*("assemble" body, line 3)*}}
3004 }
3005 test assemble-31.6 {jumpTable, missing symbol} {
3006 -body {
3007 list [catch {assemble {
3008 # line 2
3009 jumpTable {1 a};# line 3
3010 }} result] \
3011 $result $::errorCode $::errorInfo
3012 }
3013 -match glob
3014 -result {1 {undefined label "a"} {TCL ASSEM NOLABEL a} {undefined label "a"*("assemble" body, line 3)*}}
3015 }
3016 test assemble-31.7 {jumptable, actual example} {
3017 -setup {
3018 proc x {} {
3019 set result {}
3020 for {set i 0} {$i < 5} {incr i} {
3021 lappend result [assemble {
3022 load i
3023 jumpTable {1 @one 2 @two 3 @three}
3024 push {none of the above}
3025 jump @done
3026 label @one
3027 push one
3028 jump @done
3029 label @two
3030 push two
3031 jump @done
3032 label @three
3033 push three
3034 label @done
3035 }]
3036 }
3037 set tcl_traceCompile 2
3038 set result
3039 }
3040 }
3041 -body x
3042 -result {{none of the above} one two three {none of the above}}
3043 -cleanup {set tcl_traceCompile 0; rename x {}}
3044 }
3045
3046 test assemble-40.1 {unbalanced stack} {
3047 -body {
3048 list \
3049 [catch {
3050 assemble {
3051 push 3
3052 dup
3053 mult
3054 push 4
3055 dup
3056 mult
3057 pop
3058 expon
3059 }
3060 } result] $result $::errorInfo
3061 }
3062 -result {1 {stack underflow} {stack underflow
3063 in assembly code between lines 1 and end of assembly code*}}
3064 -match glob
3065 -returnCodes ok
3066 }
3067 test assemble-40.2 {unbalanced stack} {*}{
3068 -body {
3069 list \
3070 [catch {
3071 assemble {
3072 label a
3073 push {}
3074 label b
3075 pop
3076 label c
3077 pop
3078 label d
3079 push {}
3080 }
3081 } result] $result $::errorInfo
3082 }
3083 -result {1 {stack underflow} {stack underflow
3084 in assembly code between lines 7 and 9*}}
3085 -match glob
3086 -returnCodes ok
3087 }
3088
3089 test assemble-41.1 {Inconsistent stack usage} {*}{
3090 -body {
3091 proc x {y} {
3092 assemble {
3093 load y
3094 jumpFalse else
3095 push 0
3096 jump then
3097 label else
3098 push 1
3099 push 2
3100 label then
3101 pop
3102 }
3103 }
3104 catch {x 1}
3105 set errorInfo
3106 }
3107 -match glob
3108 -result {inconsistent stack depths on two execution paths
3109 ("assemble" body, line 10)*}
3110 -cleanup {rename x {}}
3111 }
3112 test assemble-41.2 {Inconsistent stack, jumptable and default} {
3113 -body {
3114 proc x {y} {
3115 assemble {
3116 load y
3117 jumpTable {0 else}
3118 push 0
3119 label else
3120 pop
3121 }
3122 }
3123 catch {x 1}
3124 set errorInfo
3125 }
3126 -match glob
3127 -result {inconsistent stack depths on two execution paths
3128 ("assemble" body, line 6)*}
3129 -cleanup {rename x {}}
3130 }
3131 test assemble-41.3 {Inconsistent stack, two legs of jumptable} {
3132 -body {
3133 proc x {y} {
3134 assemble {
3135 load y
3136 jumpTable {0 no 1 yes}
3137 label no
3138 push 0
3139 label yes
3140 pop
3141 }
3142 }
3143 catch {x 1}
3144 set errorInfo
3145 }
3146 -match glob
3147 -result {inconsistent stack depths on two execution paths
3148 ("assemble" body, line 7)*}
3149 -cleanup {rename x {}}
3150 }
3151
3152 test assemble-50.1 {Ulam's 3n+1 problem, TAL implementation} {
3153 -body {
3154 proc ulam {n} {
3155 assemble {
3156 load n; # max
3157 dup; # max n
3158 jump start; # max n
3159
3160 label loop; # max n
3161 over 1; # max n max
3162 over 1; # max in max n
3163 ge; # man n max>=n
3164 jumpTrue skip; # max n
3165
3166 reverse 2; # n max
3167 pop; # n
3168 dup; # n n
3169
3170 label skip; # max n
3171 dup; # max n n
3172 push 2; # max n n 2
3173 mod; # max n n%2
3174 jumpTrue odd; # max n
3175
3176 push 2; # max n 2
3177 div; # max n/2 -> max n
3178 jump start; # max n
3179
3180 label odd; # max n
3181 push 3; # max n 3
3182 mult; # max 3*n
3183 push 1; # max 3*n 1
3184 add; # max 3*n+1
3185
3186 label start; # max n
3187 dup; # max n n
3188 push 1; # max n n 1
3189 neq; # max n n>1
3190 jumpTrue loop; # max n
3191
3192 pop; # max
3193 }
3194 }
3195 set result {}
3196 for {set i 1} {$i < 30} {incr i} {
3197 lappend result [ulam $i]
3198 }
3199 set result
3200 }
3201 -result {1 2 16 4 16 16 52 8 52 16 52 16 40 52 160 16 52 52 88 20 64 52 160 24 88 40 9232 52 88}
3202 -cleanup {rename ulam {}}
3203 }
3204
3205 test assemble-51.1 {memory leak testing} memory {
3206 leaktest {
3207 apply {{} {assemble {push hello}}}
3208 }
3209 } 0
3210 test assemble-51.2 {memory leak testing} memory {
3211 leaktest {
3212 apply {{{x 0}} {assemble {incrImm x 1}}}
3213 }
3214 } 0
3215 test assemble-51.3 {memory leak testing} memory {
3216 leaktest {
3217 apply {{n} {
3218 assemble {
3219 load n; # max
3220 dup; # max n
3221 jump start; # max n
3222
3223 label loop; # max n
3224 over 1; # max n max
3225 over 1; # max in max n
3226 ge; # man n max>=n
3227 jumpTrue skip; # max n
3228
3229 reverse 2; # n max
3230 pop; # n
3231 dup; # n n
3232
3233 label skip; # max n
3234 dup; # max n n
3235 push 2; # max n n 2
3236 mod; # max n n%2
3237 jumpTrue odd; # max n
3238
3239 push 2; # max n 2
3240 div; # max n/2 -> max n
3241 jump start; # max n
3242
3243 label odd; # max n
3244 push 3; # max n 3
3245 mult; # max 3*n
3246 push 1; # max 3*n 1
3247 add; # max 3*n+1
3248
3249 label start; # max n
3250 dup; # max n n
3251 push 1; # max n n 1
3252 neq; # max n n>1
3253 jumpTrue loop; # max n
3254
3255 pop; # max
3256 }
3257 }} 1
3258 }
3259 } 0
3260 test assemble-51.4 {memory leak testing} memory {
3261 leaktest {
3262 catch {
3263 apply {{} {
3264 assemble {reverse polish notation}
3265 }}
3266 }
3267 }
3268 } 0
3269
3270 test assemble-52.1 {Bug 3154ea2759} {
3271 apply {{} {
3272 # Needs six exception ranges to force the range allocations to use the
3273 # malloced store.
3274 ::tcl::unsupported::assemble {
3275 beginCatch @badLabel
3276 push error
3277 push testing
3278 invokeStk 2
3279 pop
3280 push 0
3281 jump @okLabel
3282 label @badLabel
3283 push 1; # should be pushReturnCode
3284 label @okLabel
3285 endCatch
3286 pop
3287
3288 beginCatch @badLabel2
3289 push error
3290 push testing
3291 invokeStk 2
3292 pop
3293 push 0
3294 jump @okLabel2
3295 label @badLabel2
3296 push 1; # should be pushReturnCode
3297 label @okLabel2
3298 endCatch
3299 pop
3300
3301 beginCatch @badLabel3
3302 push error
3303 push testing
3304 invokeStk 2
3305 pop
3306 push 0
3307 jump @okLabel3
3308 label @badLabel3
3309 push 1; # should be pushReturnCode
3310 label @okLabel3
3311 endCatch
3312 pop
3313
3314 beginCatch @badLabel4
3315 push error
3316 push testing
3317 invokeStk 2
3318 pop
3319 push 0
3320 jump @okLabel4
3321 label @badLabel4
3322 push 1; # should be pushReturnCode
3323 label @okLabel4
3324 endCatch
3325 pop
3326
3327 beginCatch @badLabel5
3328 push error
3329 push testing
3330 invokeStk 2
3331 pop
3332 push 0
3333 jump @okLabel5
3334 label @badLabel5
3335 push 1; # should be pushReturnCode
3336 label @okLabel5
3337 endCatch
3338 pop
3339
3340 beginCatch @badLabel6
3341 push error
3342 push testing
3343 invokeStk 2
3344 pop
3345 push 0
3346 jump @okLabel6
3347 label @badLabel6
3348 push 1; # should be pushReturnCode
3349 label @okLabel6
3350 endCatch
3351 pop
3352 }
3353 }}
3354 } {}; # must not crash
3355
3356 rename fillTables {}
3357 rename assemble {}
3358 if {[testConstraint memory]} {
3359 rename getbytes {}
3360 rename leaktest {}
3361 }
3362
3363 ::tcltest::cleanupTests
3364 return
3365
3366 # Local Variables:
3367 # mode: tcl
3368 # fill-column: 78
3369 # End: