From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Mon, 8 Oct 2018 03:05:54 +0000 (+0800) Subject: optimise ifstatements jumpif condition check (#13) X-Git-Tag: v0.1.1~20 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=c472eb9be5ffd4db6e8d04ece79bc61163b4db17;p=bytom%2Fequity.git optimise ifstatements jumpif condition check (#13) * optimise ifstatements jumpif condition check * fix unit test * restore --- diff --git a/compiler/builder.go b/compiler/builder.go index e8dca4e..b00d4ec 100644 --- a/compiler/builder.go +++ b/compiler/builder.go @@ -44,6 +44,10 @@ func (b *builder) addInt64(stk stack, n int64) stack { return b.add(s, stk.add(s)) } +func (b *builder) addNot(stk stack, desc string) stack { + return b.add("NOT", stk.drop().add(desc)) +} + func (b *builder) addEqual(stk stack, desc string) stack { return b.add("EQUAL", stk.dropN(2).add(desc)) } diff --git a/compiler/compile.go b/compiler/compile.go index 76913a2..5a98641 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -373,10 +373,9 @@ func compileStatement(b *builder, stk stack, contract *Contract, env *environ, c } // jump to falseBody when condition is false, while the JUMPIF instruction will be run success when - // the value of dataStack is true, therefore add this check + // the value of dataStack is true, therefore add this negation conditionExpr := stk.str - stk = b.addBoolean(stk, false) - stk = b.addEqual(stk, fmt.Sprintf("(%s == false)", conditionExpr)) // stack: [... ] + stk = b.addNot(stk, fmt.Sprintf("!%s", conditionExpr)) // add label var label string diff --git a/compiler/compile_test.go b/compiler/compile_test.go index 3fc02d9..5360de8 100644 --- a/compiler/compile_test.go +++ b/compiler/compile_test.go @@ -74,17 +74,17 @@ func TestCompile(t *testing.T) { { "TestSigIf", equitytest.TestSigIf, - "53797b879169765379a00087641c00000052795279a0696321000000765279a069", + "53797b879169765379a091641b00000052795279a0696320000000765279a069", }, { "TestIfAndMultiClause", equitytest.TestIfAndMultiClause, - "7b641f0000007087916976547aa00087641a000000765379a06963240000007b7bae7cac", + "7b641e0000007087916976547aa0916419000000765379a06963230000007b7bae7cac", }, { "TestIfNesting", equitytest.TestIfNesting, - "7b644400000054795279879169765579a00087643500000052795479a000876429000000765379a06952795579879169633a000000765479a06953797b8791635c0000007654798791695279a000876459000000527978a0697d8791", + "7b644200000054795279879169765579a091643300000052795479a0916427000000765379a069527955798791696338000000765479a06953797b879163590000007654798791695279a0916456000000527978a0697d8791", }, { "TestConstantMath",