Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/transactions/logic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ Account fields used in the `acct_params_get` opcode.
| `assert` | immediately fail unless A is a non-zero number |
| `callsub target` | branch unconditionally to TARGET, saving the next instruction on the call stack |
| `retsub` | pop the top instruction from the call stack and branch to it |
| `switchi target ...` | branch to the Ath label. Continue at following instruction if index A exceeds the number of labels. |
| `switch target ...` | branch to the Ath label. Continue at following instruction if index A exceeds the number of labels. |

### State Access

Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/TEAL_opcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ The call stack is separate from the data stack. Only `callsub` and `retsub` mani

The call stack is separate from the data stack. Only `callsub` and `retsub` manipulate it.

## switchi target ...
## switch target ...

- Opcode: 0x8a {uint8 branch count} [{int16 branch offset, big-endian}, ...]
- Stack: ..., A: uint64 → ...
Expand Down
20 changes: 10 additions & 10 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ replace3
const switchNonsense = `
switch_label0:
pushint 1
switchi switch_label0 switch_label1
switch switch_label0 switch_label1
switch_label1:
pushint 1
`
Expand Down Expand Up @@ -2774,29 +2774,29 @@ func TestAssembleSwitch(t *testing.T) {
// fail when target doesn't correspond to existing label
source := `
pushint 1
switchi label1 label2
switch label1 label2
label1:
`
testProg(t, source, AssemblerMaxVersion, NewExpect(3, "reference to undefined label \"label2\""))

// fail when target index != uint64
testProg(t, `
byte "fail"
switchi label1
switch label1
labe11:
`, AssemblerMaxVersion, Expect{3, "switchi label1 arg 0 wanted type uint64..."})
`, AssemblerMaxVersion, Expect{3, "switch label1 arg 0 wanted type uint64..."})

// No labels is pretty degenerate, but ok, I suppose. It's just a no-op
testProg(t, `
int 0
switchi
switch
int 1
`, AssemblerMaxVersion)

// confirm arg limit
source = `
pushint 1
switchi label1 label2
switch label1 label2
label1:
label2:
`
Expand All @@ -2811,7 +2811,7 @@ int 1
// test that 255 labels is ok
source = fmt.Sprintf(`
pushint 1
switchi %s
switch %s
%s
`, strings.Join(labels, " "), strings.Join(labels, ":\n")+":\n")
ops = testProg(t, source, AssemblerMaxVersion)
Expand All @@ -2820,15 +2820,15 @@ int 1
// 256 is too many
source = fmt.Sprintf(`
pushint 1
switchi %s extra
switch %s extra
%s
`, strings.Join(labels, " "), strings.Join(labels, ":\n")+":\n")
ops = testProg(t, source, AssemblerMaxVersion, Expect{3, "switchi cannot take more than 255 labels"})
ops = testProg(t, source, AssemblerMaxVersion, Expect{3, "switch cannot take more than 255 labels"})

// allow duplicate label reference
source = `
pushint 1
switchi label1 label1
switch label1 label1
label1:
`
testProg(t, source, AssemblerMaxVersion)
Expand Down
6 changes: 3 additions & 3 deletions data/transactions/logic/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var opDocByName = map[string]string{
"vrf_verify": "Verify the proof B of message A against pubkey C. Returns vrf output and verification flag.",
"block": "field F of block A. Fail unless A falls between txn.LastValid-1002 and txn.FirstValid (exclusive)",

"switchi": "branch to the Ath label. Continue at following instruction if index A exceeds the number of labels.",
"switch": "branch to the Ath label. Continue at following instruction if index A exceeds the number of labels.",
}

// OpDoc returns a description of the op
Expand Down Expand Up @@ -264,7 +264,7 @@ var opcodeImmediateNotes = map[string]string{
"vrf_verify": "{uint8 parameters index}",
"block": "{uint8 block field}",

"switchi": "{uint8 branch count} [{int16 branch offset, big-endian}, ...]",
"switch": "{uint8 branch count} [{int16 branch offset, big-endian}, ...]",
}

// OpImmediateNote returns a short string about immediate data which follows the op byte
Expand Down Expand Up @@ -343,7 +343,7 @@ var OpGroups = map[string][]string{
"Byte Array Arithmetic": {"b+", "b-", "b/", "b*", "b<", "b>", "b<=", "b>=", "b==", "b!=", "b%", "bsqrt"},
"Byte Array Logic": {"b|", "b&", "b^", "b~"},
"Loading Values": {"intcblock", "intc", "intc_0", "intc_1", "intc_2", "intc_3", "pushint", "bytecblock", "bytec", "bytec_0", "bytec_1", "bytec_2", "bytec_3", "pushbytes", "bzero", "arg", "arg_0", "arg_1", "arg_2", "arg_3", "args", "txn", "gtxn", "txna", "txnas", "gtxna", "gtxnas", "gtxns", "gtxnsa", "gtxnsas", "global", "load", "loads", "store", "stores", "gload", "gloads", "gloadss", "gaid", "gaids"},
"Flow Control": {"err", "bnz", "bz", "b", "return", "pop", "dup", "dup2", "dig", "cover", "uncover", "swap", "select", "assert", "callsub", "retsub", "switchi"},
"Flow Control": {"err", "bnz", "bz", "b", "return", "pop", "dup", "dup2", "dig", "cover", "uncover", "swap", "select", "assert", "callsub", "retsub", "switch"},
"State Access": {"balance", "min_balance", "app_opted_in", "app_local_get", "app_local_get_ex", "app_global_get", "app_global_get_ex", "app_local_put", "app_global_put", "app_local_del", "app_global_del", "asset_holding_get", "asset_params_get", "app_params_get", "acct_params_get", "log", "block"},
"Inner Transactions": {"itxn_begin", "itxn_next", "itxn_field", "itxn_submit", "itxn", "itxna", "itxnas", "gitxn", "gitxna", "gitxnas"},
}
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,7 @@ func opB(cx *EvalContext) error {
return nil
}

func opSwitchInt(cx *EvalContext) error {
func opSwitch(cx *EvalContext) error {
last := len(cx.stack) - 1
branchIdx := cx.stack[last].Uint

Expand Down
18 changes: 9 additions & 9 deletions data/transactions/logic/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5535,7 +5535,7 @@ func TestSwitchInt(t *testing.T) {
// take the 0th label
testAccepts(t, `
int 0
switchi zero one
switch zero one
err
zero: int 1; return
one: int 0;
Expand All @@ -5544,7 +5544,7 @@ one: int 0;
// take the 1th label
testRejects(t, `
int 1
switchi zero one
switch zero one
err
zero: int 1; return
one: int 0;
Expand All @@ -5553,15 +5553,15 @@ one: int 0;
// same, but jumping to end of program
testAccepts(t, `
int 1; dup
switchi zero one
switch zero one
zero: err
one:
`, 8)

// no match
testAccepts(t, `
int 2
switchi zero one
switch zero one
int 1; return // falls through to here
zero: int 0; return
one: int 0; return
Expand All @@ -5576,7 +5576,7 @@ int 1
dup
int 1
-
switchi start end
switch start end
err
end:
int 2
Expand All @@ -5588,23 +5588,23 @@ int 1
// 0 labels are allowed, but weird!
testAccepts(t, `
int 0
switchi
switch
int 1
`, 8)

testPanics(t, notrack("switchi; int 1"), 8)
testPanics(t, notrack("switch; int 1"), 8)

// make the switch the final instruction
testAccepts(t, `
int 1
int 0
switchi done1 done2; done1: ; done2: ;
switch done1 done2; done1: ; done2: ;
`, 8)

// make the switch the final instruction, and don't match
testAccepts(t, `
int 1
int 88
switchi done1 done2; done1: ; done2: ;
switch done1 done2; done1: ; done2: ;
`, 8)
}
2 changes: 1 addition & 1 deletion data/transactions/logic/langspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@
},
{
"Opcode": 138,
"Name": "switchi",
"Name": "switch",
"Args": "U",
"Size": 0,
"Doc": "branch to the Ath label. Continue at following instruction if index A exceeds the number of labels.",
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ var OpSpecs = []OpSpec{
// "Function oriented"
{0x88, "callsub", opCallSub, proto(":"), 4, detBranch()},
{0x89, "retsub", opRetSub, proto(":"), 4, detDefault()},
{0x8a, "switchi", opSwitchInt, proto("i:"), 8, detSwitch()},
{0x8a, "switch", opSwitch, proto("i:"), 8, detSwitch()},
// 0x8b will likely be a switch on pairs of values/targets

// More math
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/teal.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
{
"name": "keyword.control.teal",
"match": "^(assert|b|bnz|bz|callsub|cover|dig|dup|dup2|err|pop|retsub|return|select|swap|switchi|uncover)\\b"
"match": "^(assert|b|bnz|bz|callsub|cover|dig|dup|dup2|err|pop|retsub|return|select|swap|switch|uncover)\\b"
},
{
"name": "keyword.other.teal",
Expand Down