fix deadcode optimization (#378)
This commit is contained in:
parent
f90d433d21
commit
71459c44f6
2 changed files with 28 additions and 3 deletions
|
@ -1220,14 +1220,14 @@ func (c *Compiler) optimizeFunc(node parser.Node) {
|
||||||
iterateInstructions(c.scopes[c.scopeIndex].Instructions,
|
iterateInstructions(c.scopes[c.scopeIndex].Instructions,
|
||||||
func(pos int, opcode parser.Opcode, operands []int) bool {
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
||||||
switch {
|
switch {
|
||||||
|
case dsts[pos]:
|
||||||
|
dstIdx++
|
||||||
|
deadCode = false
|
||||||
case opcode == parser.OpReturn:
|
case opcode == parser.OpReturn:
|
||||||
if deadCode {
|
if deadCode {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
deadCode = true
|
deadCode = true
|
||||||
case dsts[pos]:
|
|
||||||
dstIdx++
|
|
||||||
deadCode = false
|
|
||||||
case deadCode:
|
case deadCode:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -1242,6 +1242,7 @@ func (c *Compiler) optimizeFunc(node parser.Node) {
|
||||||
var appendReturn bool
|
var appendReturn bool
|
||||||
endPos := len(c.scopes[c.scopeIndex].Instructions)
|
endPos := len(c.scopes[c.scopeIndex].Instructions)
|
||||||
newEndPost := len(newInsts)
|
newEndPost := len(newInsts)
|
||||||
|
|
||||||
iterateInstructions(newInsts,
|
iterateInstructions(newInsts,
|
||||||
func(pos int, opcode parser.Opcode, operands []int) bool {
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
||||||
switch opcode {
|
switch opcode {
|
||||||
|
|
|
@ -1159,6 +1159,30 @@ func() {
|
||||||
tengo.MakeInstruction(parser.OpReturn, 1),
|
tengo.MakeInstruction(parser.OpReturn, 1),
|
||||||
tengo.MakeInstruction(parser.OpConstant, 1),
|
tengo.MakeInstruction(parser.OpConstant, 1),
|
||||||
tengo.MakeInstruction(parser.OpReturn, 1)))))
|
tengo.MakeInstruction(parser.OpReturn, 1)))))
|
||||||
|
|
||||||
|
expectCompile(t, `
|
||||||
|
func() {
|
||||||
|
if true {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
return 123
|
||||||
|
}`, bytecode(
|
||||||
|
concatInsts(
|
||||||
|
tengo.MakeInstruction(parser.OpConstant, 1),
|
||||||
|
tengo.MakeInstruction(parser.OpPop),
|
||||||
|
tengo.MakeInstruction(parser.OpSuspend)),
|
||||||
|
objectsArray(
|
||||||
|
intObject(123),
|
||||||
|
compiledFunction(0, 0,
|
||||||
|
tengo.MakeInstruction(parser.OpTrue),
|
||||||
|
tengo.MakeInstruction(parser.OpJumpFalsy, 6),
|
||||||
|
tengo.MakeInstruction(parser.OpReturn, 0),
|
||||||
|
tengo.MakeInstruction(parser.OpReturn, 0),
|
||||||
|
tengo.MakeInstruction(parser.OpConstant, 0),
|
||||||
|
tengo.MakeInstruction(parser.OpReturn, 1)))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCompilerScopes(t *testing.T) {
|
func TestCompilerScopes(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue