# Commands covered: assemble if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest 2.2 namespace import -force ::tcltest::* } namespace eval tcl::unsupported {namespace export assemble} namespace import tcl::unsupported::assemble # Procedure to make code that fills the literal and local variable tables, # to force instructions to spill to four bytes. proc fillTables {} { set s {} set sep {} for {set i 0} {$i < 256} {incr i} { append s $sep [list set v$i literal$i] set sep \n } return $s } # assemble-1 - TclNRAssembleObjCmd test assemble-1.1 {wrong # args, direct eval} { -body { eval [list assemble] } -returnCodes error -result {wrong # args*} -match glob } test assemble-1.2 {wrong # args, direct eval} { -body { eval [list assemble too many] } -returnCodes error -result {wrong # args*} -match glob } test assemble-1.3 {error reporting, direct eval} { -body { list [catch { eval [list assemble { # bad opcode rubbish }] } result] $result $errorInfo } -match glob -result {1 {bad instruction "rubbish":*} {bad instruction "rubbish":* while executing "rubbish" ("assemble" body, line 3)*}} -cleanup {unset result} } test assemble-1.4 {simple direct eval} { -body { eval [list assemble {push {this is a test}}] } -result {this is a test} } # assemble-2 - CompileAssembleObj test assemble-2.1 {bytecode reuse, direct eval} { -body { set x {push "this is a test"} list [eval [list assemble $x]] \ [eval [list assemble $x]] } -result {{this is a test} {this is a test}} } test assemble-2.2 {bytecode discard, direct eval} { -body { set x {load value} proc p1 {x} { set value value1 assemble $x } proc p2 {x} { set a b set value value2 assemble $x } list [p1 $x] [p2 $x] } -result {value1 value2} -cleanup { unset x rename p1 {} rename p2 {} } } test assemble-2.3 {null script, direct eval} { -body { set x {} assemble $x } -result {} -cleanup {unset x} } # assemble-3 - TclCompileAssembleCmd test assemble-3.1 {wrong # args, compiled path} { -body { proc x {} { assemble } x } -returnCodes error -match glob -result {wrong # args:*} } test assemble-3.2 {wrong # args, compiled path} { -body { proc x {} { assemble too many } x } -returnCodes error -match glob -result {wrong # args:*} -cleanup { rename x {} } } # assemble-4 - TclAssembleCode mainline test assemble-4.1 {syntax error} { -body { proc x {} { assemble { {}extra } } list [catch x result] $result $::errorInfo } -cleanup { rename x {} unset result } -match glob -result {1 {extra characters after close-brace} {extra characters after close-brace while executing "{}extra " ("assemble" body, line 2)*}} } test assemble-4.2 {null command} { -body { proc x {} { assemble { push hello; pop;;push goodbye } } x } -result goodbye -cleanup { rename x {} } } # assemble-5 - GetNextOperand off-nominal cases test assemble-5.1 {unsupported expansion} { -body { proc x {y} { assemble { {*}$y } } list [catch {x {push hello}} result] $result $::errorCode } -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}} -cleanup { rename x {} unset result } } test assemble-5.2 {unsupported substitution} { -body { proc x {y} { assemble { $y } } list [catch {x {nop}} result] $result $::errorCode } -cleanup { rename x {} unset result } -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}} } test assemble-5.3 {unsupported substitution} { -body { proc x {} { assemble { [x] } } list [catch {x} result] $result $::errorCode } -result {1 {assembly code may not contain substitutions} {TCL ASSEM NOSUBST}} } test assemble-5.4 {backslash substitution} { -body { proc x {} { assemble { p\x75sh\ hello\ world } } x } -cleanup { rename x {} } -result {hello world} } # assemble-6 - ASSEM_PUSH test assemble-6.1 {push, wrong # args} { -body { assemble push } -returnCodes error -match glob -result {wrong # args*} } test assemble-6.2 {push, wrong # args} { -body { assemble {push too many} } -returnCodes error -match glob -result {wrong # args*} } test assemble-6.3 {push} { -body { eval [list assemble {push hello}] } -result hello } test assemble-6.4 {push4} { -body { proc x {} " [fillTables] assemble {push hello} " x } -cleanup { rename x {} } -result hello } # assemble-7 - ASSEM_1BYTE test assemble-7.1 {add, wrong # args} { -body { assemble {add excess} } -returnCodes error -match glob -result {wrong # args*} } test assemble-7.2 {add} { -body { assemble { push 2 push 2 add } } -result {4} } test assemble-7.3 {appendArrayStk} { -body { set a(b) {hello, } assemble { push a push b push world appendArrayStk } set a(b) } -result {hello, world} -cleanup {unset a} } test assemble-7.4 {appendStk} { -body { set a {hello, } assemble { push a push world appendStk } set a } -result {hello, world} -cleanup {unset a} } test assemble-7.5 {bitwise ops} { -body { list \ [assemble {push 0b1100; push 0b1010; bitand}] \ [assemble {push 0b1100; bitnot}] \ [assemble {push 0b1100; push 0b1010; bitor}] \ [assemble {push 0b1100; push 0b1010; bitxor}] } -result {8 -13 14 6} } test assemble-7.6 {div} { -body { assemble {push 999999; push 7; div} } -result 142857 } test assemble-7.7 {dup} { -body { assemble { push 1; dup; dup; add; dup; add; dup; add; add } } -result 9 } test assemble-7.8 {eq} { -body { list \ [assemble {push able; push baker; eq}] \ [assemble {push able; push able; eq}] } -result {0 1} } test assemble-7.9 {evalStk} { -body { assemble { push {concat test 7.3} evalStk } } -result {test 7.3} } test assemble-7.9a {evalStk, syntax} { -body { assemble { push {{}bad} evalStk } } -returnCodes error -result {extra characters after close-brace} } test assemble-7.9b {evalStk, backtrace} { -body { proc y {z} { error testing } proc x {} { assemble { push { # test error in evalStk y asd } evalStk } } list [catch x result] $result $errorInfo } -result {1 testing {testing while executing "error testing" (procedure "y" line 2) invoked from within "y asd"*}} -match glob -cleanup { rename y {} rename x {} } } test assemble-7.10 {existArrayStk} { -body { proc x {name key} { set a(b) c assemble { load name; load key; existArrayStk } } list [x a a] [x a b] [x b a] [x b b] } -result {0 1 0 0} -cleanup {rename x {}} } test assemble-7.11 {existStk} { -body { proc x {name} { set a b assemble { load name; existStk } } list [x a] [x b] } -result {1 0} -cleanup {rename x {}} } test assemble-7.12 {expon} { -body { assemble {push 3; push 4; expon} } -result 81 } test assemble-7.13 {exprStk} { -body { assemble { push {acos(-1)} exprStk } } -result 3.141592653589793 } test assemble-7.13a {exprStk, syntax} { -body { assemble { push {2+} exprStk } } -returnCodes error -result {missing operand at _@_ in expression "2+_@_"} } test assemble-7.13b {exprStk, backtrace} { -body { proc y {z} { error testing } proc x {} { assemble { push {[y asd]} exprStk } } list [catch x result] $result $errorInfo } -result {1 testing {testing while executing "error testing" (procedure "y" line 2) invoked from within "y asd"*}} -match glob -cleanup { rename y {} rename x {} } } test assemble-7.14 {ge gt le lt} { -body { proc x {a b} { list [assemble {load a; load b; ge}] \ [assemble {load a; load b; gt}] \ [assemble {load a; load b; le}] \ [assemble {load a; load b; lt}] } list [x 0 0] [x 0 1] [x 1 0] } -result {{1 0 1 0} {0 0 1 1} {1 1 0 0}} -cleanup {rename x {}} } test assemble-7.15 {incrArrayStk} { -body { proc x {} { set a(b) 5 assemble { push a; push b; push 7; incrArrayStk } } x } -result 12 -cleanup {rename x {}} } test assemble-7.16 {incrStk} { -body { proc x {} { set a 5 assemble { push a; push 7; incrStk } } x } -result 12 -cleanup {rename x {}} } test assemble-7.17 {land/lor} { -body { proc x {a b} { list \ [assemble {load a; load b; land}] \ [assemble {load a; load b; lor}] } list [x 0 0] [x 0 23] [x 35 0] [x 47 59] } -result {{0 0} {0 1} {0 1} {1 1}} -cleanup {rename x {}} } test assemble-7.18 {lappendArrayStk} { -body { proc x {} { set able(baker) charlie assemble { push able push baker push dog lappendArrayStk } } x } -result {charlie dog} -cleanup {rename x {}} } test assemble-7.19 {lappendStk} { -body { proc x {} { set able baker assemble { push able push charlie lappendStk } } x } -result {baker charlie} -cleanup {rename x {}} } test assemble-7.20 {listIndex} { -body { assemble { push {a b c d} push 2 listIndex } } -result c } test assemble-7.21 {listLength} { -body { assemble { push {a b c d} listLength } } -result 4 } test assemble-7.22 {loadArrayStk} { -body { proc x {} { set able(baker) charlie assemble { push able push baker loadArrayStk } } x } -result charlie -cleanup {rename x {}} } test assemble-7.23 {loadStk} { -body { proc x {} { set able baker assemble { push able loadStk } } x } -result baker -cleanup {rename x {}} } test assemble-7.24 {lsetList} { -body { proc x {} { set l {{a b} {c d} {e f} {g h}} assemble { push {2 1}; push i; load l; lsetList } } x } -result {{a b} {c d} {e i} {g h}} } test assemble-7.25 {lshift} { -body { assemble {push 16; push 4; lshift} } -result 256 } test assemble-7.26 {mod} { -body { assemble {push 123456; push 1000; mod} } -result 456 } test assemble-7.27 {mult} { -body { assemble {push 12345679; push 9; mult} } -result 111111111 } test assemble-7.28 {neq} { -body { list \ [assemble {push able; push baker; neq}] \ [assemble {push able; push able; neq}] } -result {1 0} } test assemble-7.29 {not} { -body { list \ [assemble {push 17; not}] \ [assemble {push 0; not}] } -result {0 1} } test assemble-7.30 {pop} { -body { assemble {push this; pop; push that} } -result that } test assemble-7.31 {rshift} { -body { assemble {push 257; push 4; rshift} } -result 16 } test assemble-7.32 {storeArrayStk} { -body { proc x {} { assemble { push able; push baker; push charlie; storeArrayStk } array get able } x } -result {baker charlie} -cleanup {rename x {}} } test assemble-7.33 {storeStk} { -body { proc x {} { assemble { push able; push baker; storeStk } set able } x } -result {baker} -cleanup {rename x {}} } test assemble-7,34 {strcmp} { -body { proc x {a b} { assemble { load a; load b; strcmp } } list [x able baker] [x baker able] [x baker baker] } -result {-1 1 0} -cleanup {rename x {}} } test assemble-7.35 {streq/strneq} { -body { proc x {a b} { list \ [assemble {load a; load b; streq}] \ [assemble {load a; load b; strneq}] } list [x able able] [x able baker] } -result {{1 0} {0 1}} -cleanup {rename x {}} } test assemble-7.36 {strindex} { -body { assemble {push testing; push 4; strindex} } -result i } test assemble-7.37 {strlen} { -body { assemble {push testing; strlen} } -result 7 } test assemble-7.38 {sub} { -body { assemble {push 42; push 17; sub} } -result 25 } test assemble-7.39 {uminus} { -body { assemble { push 42; uminus } } -result -42 } test assemble-7.40 {uplus} { -body { assemble { push 42; uplus } } -result 42 } test assemble-7.8 {exist} { -body { proc x {} { set y z list [assemble {exist y}] \ [assemble {exist z}] } x } -result {1 0} -cleanup {rename x {}} } # assemble-8 ASSEM_LVT and FindLocalVar test assemble-8.1 {load, wrong # args} { -body { assemble load } -returnCodes error -match glob -result {wrong # args*} } test assemble-8.2 {load, wrong # args} { -body { assemble {load too many} } -returnCodes error -match glob -result {wrong # args*} } test assemble-8.3 {nonlocal var} { -body { list [catch {assemble {load ::env}} result] $result $errorCode } -result {1 {variable "::env" is not local} {TCL ASSEM NONLOCAL ::env}} -cleanup {unset result} } test assemble-8.4 {bad context} { -body { set x 1 list [catch {assemble {load x}} result] $result $errorCode } -result {1 {cannot use this instruction to create a variable in a non-proc context} {TCL ASSEM LVT}} -cleanup {unset result} } test assemble-8.5 {bad context} { -body { namespace eval assem { set x 1 list [catch {assemble {load x}} result] $result $errorCode } } -result {1 {cannot use this instruction to create a variable in a non-proc context} {TCL ASSEM LVT}} -cleanup {namespace delete assem} } test assemble-8.6 {load1} { -body { proc x {a} { assemble { load a } } x able } -result able -cleanup {rename x {}} } test assemble-8.7 {load4} { -body { proc x {a} " [fillTables] set b \$a assemble {load b} " x able } -result able -cleanup {rename x {}} } test assemble-8.8 {loadArray1} { -body { proc x {} { set able(baker) charlie assemble { push baker loadArray able } } x } -result charlie -cleanup {rename x {}} } test assemble-8.9 {loadArray4} { -body " proc x {} { [fillTables] set able(baker) charlie assemble { push baker loadArray able } } x " -result charlie -cleanup {rename x {}} } test assemble-8.10 {append1} { -body { proc x {} { set y {hello, } assemble { push world; append y } } x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.11 {append4} { -body { proc x {} " [fillTables] set y {hello, } assemble { push world; append y } " x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.12 {appendArray1} { -body { proc x {} { set y(z) {hello, } assemble { push z; push world; appendArray y } } x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.13 {appendArray4} { -body { proc x {} " [fillTables] set y(z) {hello, } assemble { push z; push world; appendArray y } " x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.14 {lappend1} { -body { proc x {} { set y {hello,} assemble { push world; lappend y } } x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.15 {lappend4} { -body { proc x {} " [fillTables] set y {hello,} assemble { push world; lappend y } " x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.16 {lappendArray1} { -body { proc x {} { set y(z) {hello,} assemble { push z; push world; lappendArray y } } x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.17 {lappendArray4} { -body { proc x {} " [fillTables] set y(z) {hello,} assemble { push z; push world; lappendArray y } " x } -result {hello, world} -cleanup {rename x {}} } test assemble-8.18 {store1} { -body { proc x {} { assemble { push test; store y } set y } x } -result {test} -cleanup {rename x {}} } test assemble-8.19 {store4} { -body { proc x {} " [fillTables] assemble { push test; store y } set y " x } -result test -cleanup {rename x {}} } test assemble-8.20 {storeArray1} { -body { proc x {} { assemble { push z; push test; storeArray y } set y(z) } x } -result test -cleanup {rename x {}} } test assemble-8.21 {storeArray4} { -body { proc x {} " [fillTables] assemble { push z; push test; storeArray y } " x } -result test -cleanup {rename x {}} } # assemble-9 - ASSEM_CONCAT1, GetIntegerOperand, CheckOneByte test assemble-9.1 {wrong # args} { -body {assemble concat} -result {wrong # args*} -match glob -returnCodes error } test assemble-9.2 {wrong # args} { -body {assemble {concat too many}} -result {wrong # args*} -match glob -returnCodes error } test assemble-9.3 {not a number} { -body {assemble {concat rubbish}} -result {expected integer but got "rubbish"} -returnCodes error } test assemble-9.4 {too small} { -body {assemble {concat -1}} -result {operand does not fit in one byte} -returnCodes error } test assemble-9.5 {too small} { -body {assemble {concat 256}} -result {operand does not fit in one byte} -returnCodes error } test assemble-9.6 {concat} { -body { assemble {push h; push e; push l; push l; push o; concat 5} } -result hello } test assemble-9.7 {concat} { -body { list [catch {assemble {concat 0}} result] $result $::errorCode } -result {1 {cannot concatenate zero objects} {TCL ASSEM EMPTYCONCAT}} -cleanup {unset result} } test assemble-1.6 {Testing push, dup, add} { -body { assemble { push 2 dup add } } -result {4} } test assemble-1.6a {wrong # args} { -body { catch { assemble { push 2 2 dup add } } set ::errorInfo } -match glob -result {wrong # args: should be "push value" while executing "push 2 2" ("assemble" body, line 2)* } } test assemble-1.7 {longer sequence} { -body { assemble { push 3 dup mult push 4 dup mult expon } } -result {1853020188851841} } test assemble-1.8 {unbalanced stack} { -body { list \ [catch { assemble { push 3 dup mult push 4 dup mult pop expon } } result] $result $::errorInfo } -result {1 {stack underflow} {stack underflow in assembly code between lines 1 and end of assembly code*}} -match glob -returnCodes ok } test assemble-1.8a {unbalanced stack} {*}{ -body { list \ [catch { assemble { label a push {} label b pop label c pop label d push {} } } result] $result $::errorInfo } -result {1 {stack underflow} {stack underflow in assembly code between lines 6 and 8*}} -match glob -returnCodes ok } # Tests for load and store test assemble-1.9 { Testing load within a proc } { -body { proc x y { assemble { load y dup mult } } x 10 } -result {100} } test assemble-1.10 {store and load again} { -body { proc x arg { assemble { push aString store arg pop load arg } } x "not_aString" } -result {aString} } test assemble-1.11 {Testing storeArray and loadArray} { -body { set anArray(1,2) "not_aString" proc x arg { upvar $arg anArray assemble { push 1,2 push aString storeArray anArray pop push 1,2 loadArray anArray } } x anArray } -cleanup {unset anArray} -result {aString} } test assemble-1.12 {Testing loadStk with a variable} { -body { set vara 10 assemble { push vara loadStk dup mult push 4 dup mult add } } -cleanup {unset vara} -result {116} } test assemble-1.13 {Testing loadStk with an array variable} { -body { set vararr(1,2) 10 assemble { push vararr(1,2) loadStk dup mult push 4 dup mult add } } -cleanup {unset vararr} -result {116} } test assemble-1.14 {Testing loadStk and storeStk} { -body { set aVar 5 assemble { push aVar push aString storeStk pop push aVar loadStk } } -cleanup {unset aVar} -result {aString} } test assemble-1.15 {Testing loadArrayStk and storeArrayStk} { -body { set aVar(1,2) 5 assemble { push aVar push 1,2 push aString storeArrayStk pop push aVar push 1,2 loadArrayStk } } -cleanup {unset aVar} -result {aString} } # Tests for incr instructions test assemble-1.16 { Testing incr } { -body { proc x arg { set i 5 assemble { push 5 incr i } } x 1 } -result {10} } test assemble-1.17 { Testing incrImm } { -body { proc x arg { set i 5 assemble { incrImm i 5 } } x 1 } -result {10} } test assemble-1.18 { Testing incrStk } { -body { proc x arg { set i 5 assemble { push i push 5 incrStk } } x 1 } -result {10} } test assemble-1.19 { Testing incrStkImm } { -body { proc x arg { set i 5 assemble { push i incrStkImm 5 } } x 1 } -result {10} } test assemble-1.20 { Testing incrStkImm } { -body { proc x arg { set i 5 assemble { push i incrStkImm 5 } } x 1 } -result {10} } test assemble-1.21 { Testing incrArray } { -body { proc x arg { set i(1,2) 5 assemble { push 1,2 push 5 incrArray i } } x 1 } -result {10} } test assemble-1.22 { Testing incrArrayImm } { -body { proc x arg { set i(1,2) 5 assemble { push 1,2 incrArrayImm i 5 } } x 1 } -result {10} } test assemble-1.23 { Testing incrArrayStk } { -body { proc x arg { set i(1,2) 5 assemble { push i push 1,2 push 5 incrArrayStk } } x 1 } -result {10} } test assemble-1.24 { Testing incrArrayStkImm } { -body { proc x arg { set i(1,2) 5 assemble { push i push 1,2 incrArrayStkImm 5 } } x 1 } -result {10} } # Tests for STRs -NOT HERE YET # Tests for LIST BYTECODES -NOT HERE YET # Tests for label and jump {push NotEqual} test assemble-1.25 { Testing label and jumpTrue } { -body { proc x {arg1 arg2} { assemble { label a load arg2 push 2 mult store arg2 pop load arg1 push 1 sub store arg1 push 0 neq jumpTrue a load arg2 } } x 4 2 } -result {32} } test assemble-1.26 { Testing label and jumpFalse } { -body { proc x {arg1 arg2} { assemble { label a load arg2 push 2 mult store arg2 pop load arg1 push 1 sub store arg1 push 0 eq jumpFalse a load arg2 } } x 4 2 } -result {32} } test assemble-1.27 {forward jumps} { -body { proc x {arg1 arg2 arg3} { assemble { jump a push aString store arg1 pop label a jump b push aString store arg2 label b jump c push aString store arg3 label c load arg1 } } x 1 2 3 } -result {1} } test assemble-1.28 {forward jumps} { -body { proc x {arg1 arg2 arg3} { assemble { jump a push aString store arg1 pop label a jump b push aString store arg2 label b jump c push aString store arg3 label c load arg2 } } x 1 2 3 } -result {2} } test assemble-1.29 {forward jumps} { -body { proc x {arg1 arg2 arg3} { assemble { jump a push aString store arg1 pop label a jump b push aString store arg2 label b jump c push aString store arg3 label c load arg3 } } x 1 2 3 } -result {3} } test assemble-1.30 {Inconsistent stack usage} {*}{ -body { proc x {y} { assemble { load y jumpFalse else push 0 jump then label else push 1 push 2 label then pop } } catch {x 1} set errorInfo } -match glob -result {inconsistent stack depths on two execution paths ("assemble" body, line 9)*} } test assemble-1.31 {unset, exists, lappend - smoke test} { -body { proc x {y} { assemble { exist result store result pop exist result lappend result pop push result push y existStk lappendStk pop push y unsetStk true exist y lappend result pop push {} store foo pop exist foo lappend result pop unset true foo exist foo lappend result pop push a existArray z lappend result pop push a push b lappendArray z lappend result pop push z push a existArrayStk lappend result pop push z push a push c lappendArrayStk lappend result } } x 1 } -result {0 1 1 0 1 0 0 b 1 {b c}} } test assemble-2.1 {concat} { -body { assemble { push a push b push c concat 3 } } -result abc } test assemble-3.1 {expr} { -body { assemble { push {1+2+3+4+5} exprStk } } -result 15 } test assemble-4.1 {eval} { -body { assemble { push {join [list [expr {1+2+3+4+5}] a] {}} evalStk } } -result 15a } test assemble-4.2 {eval} { -body { proc x {} { assemble { push 3 store n pop eval {expr {3*$n + 1}} push 1 add } } x } -result 11 } test assemble-4.3 {expr} { -body { proc x {} { assemble { push 3 store n pop expr {3*$n + 1} push 1 add } } x } -result 11 } test assemble-30.1 {Ulam's 3n+1 problem, TAL implementation} { -body { proc ulam {n} { assemble { load n; # max dup; # max n jump start; # max n label loop; # max n over 1; # max n max over 1; # max in max n ge; # man n max>=n jumpTrue skip; # max n reverse 2; # n max pop; # n dup; # n n label skip; # max n dup; # max n n push 2; # max n n 2 mod; # max n n%2 jumpTrue odd; # max n push 2; # max n 2 div; # max n/2 -> max n jump start; # max n label odd; # max n push 3; # max n 3 mult; # max 3*n push 1; # max 3*n 1 add; # max 3*n+1 label start; # max n dup; # max n n push 1; # max n n 1 neq; # max n n>1 jumpTrue loop; # max n pop; # max } } set result {} for {set i 1} {$i < 30} {incr i} { lappend result [ulam $i] } set result } -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} } rename fillTables {} rename assemble {} ::tcltest::cleanupTests return # Local Variables: # mode: tcl # End: