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
8 changes: 8 additions & 0 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,14 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
c.currentSwitch = label
c.pushStackLabel(label, 1)

last := len(n.Body.List) - 1
for i := range last {
if n.Body.List[i].(*ast.CaseClause).List == nil { // early default
n.Body.List[i], n.Body.List[last] = n.Body.List[last], n.Body.List[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check if this substitution affects generated debug info. Lines info stored in debug info for substituted default should match the source code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can i check this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See how saveSequencePoint works, ensure that proper statement boundaries will be stored in the debug info file on compilation:

func (c *codegen) saveSequencePoint(n ast.Node) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestSwitch(t *testing.T) {
	_, di, err := compiler.CompileWithOptions("contract.go", strings.NewReader(`
package contract

func F() int {
  switch {
  case true:
    return 1
  default:
    return 0
  }
}
`), nil)
	require.NoError(t, err)

	for _, m := range di.Methods {
		for _, p := range m.SeqPoints {
			j, err := json.MarshalIndent(p, "", " ")
			require.NoError(t, err)
			fmt.Println(string(j))
		}
	}
}

w/ default going after case (more usual):

{
 "Opcode": 8,
 "Document": 0,
 "StartLine": 7,
 "StartCol": 5,
 "EndLine": 7,
 "EndCol": 13
}
{
 "Opcode": 13,
 "Document": 0,
 "StartLine": 9,
 "StartCol": 5,
 "EndLine": 9,
 "EndCol": 13
}
{
 "Opcode": 15,
 "Document": 0,
 "StartLine": 4,
 "StartCol": 14,
 "EndLine": 11,
 "EndCol": 2
}

and before:

{
 "Opcode": 8,
 "Document": 0,
 "StartLine": 9,
 "StartCol": 5,
 "EndLine": 9,
 "EndCol": 13
}
{
 "Opcode": 13,
 "Document": 0,
 "StartLine": 7,
 "StartCol": 5,
 "EndLine": 7,
 "EndCol": 13
}
{
 "Opcode": 15,
 "Document": 0,
 "StartLine": 4,
 "StartCol": 14,
 "EndLine": 11,
 "EndCol": 2
}

anything that needs to be fixed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, the following program gives as 3 sequence points with RET opcodes, every opcode matches return line:

			f, di, err := CompileWithOptions("contract.go", strings.NewReader(` // 1
			package contract			// 2
										// 3
			func F(b bool) int {		// 4	18: RET (lines 4-11)
			  switch b {				// 5
			  case true:				// 6
			    return 1				// 7	11: RET	(line 7)
			  default:					// 8
			    return 0				// 9	16: RET	(line 9)
			  }							// 10
			}							// 11
		`), nil)
			require.NoError(t, err)

The bytecode is:

NEO-GO-VM 0 > loadbase64 VwABeEoIlyYHRRFAIgVFEEBFQA==
READY: loaded 19 instructions
NEO-GO-VM 0 > ops
INDEX    OPCODE      PARAMETER
0        INITSLOT    0 local, 1 arg    <<
3        LDARG0
4        DUP
5        PUSHT
6        EQUAL
7        JMPIFNOT    14 (7/07)
9        DROP
10       PUSH1
11       RET
12       JMP         17 (5/05)
14       DROP
15       PUSH0
16       RET
17       DROP
18       RET

The updated version of the program with default going before case gives the following sequence points:

			f, di, err := CompileWithOptions("contract.go", strings.NewReader(` // 1
			package contract			// 2
										// 3
			func F(b bool) int {		// 4	18: RET (lines 4-11)
			  switch b {				// 5
			  default:					// 6
			    return 0				// 7	16: RET	(line 7)
			  case true:				// 8
			    return 1				// 9	11: RET	(line 9)
			  }							// 10
			}							// 11
		`), nil)
			require.NoError(t, err)

The generated bytecode is not changed (as expected).

From the last output we see that 11: RET bounds match exactly the return 1 case, so everything works as expected.

break
}
}

startLabels := make([]uint16, len(n.Body.List))
for i := range startLabels {
startLabels[i] = c.newLabel()
Expand Down
40 changes: 40 additions & 0 deletions pkg/compiler/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,46 @@ var switchTestCases = []testCase{
`,
big.NewInt(1),
},
{
"case after first default",
`func F%d() int {
a := 5
switch a {
default: return 4
case 5: return 2
}
}
`,
big.NewInt(2),
},
{
"case after intermediate default",
`func F%d() int {
a := 6
switch a {
case 5: return 2
default: return 4
case 6:
}
return 1
}
`,
big.NewInt(1),
},
{
"intermediate default",
`func F%d() int {
a := 3
switch a {
case 5: return 2
default: return 4
case 6:
}
return 1
}
`,
big.NewInt(4),
},
{
"expression in case clause",
`func F%d() int {
Expand Down
Loading