From 2d5c2573b1a7e01c58bd68c685c30127e99c67cb Mon Sep 17 00:00:00 2001 From: deelawn Date: Wed, 21 Feb 2024 13:52:50 -0800 Subject: [PATCH] added more opcode functions --- examples/gno.land/r/x/benchmark/ops.gno | 202 +++++++++++++++++++----- gnovm/pkg/gnolang/op_binary.go | 23 ++- gnovm/pkg/gnolang/op_call.go | 5 +- gnovm/pkg/gnolang/op_exec.go | 4 + 4 files changed, 186 insertions(+), 48 deletions(-) diff --git a/examples/gno.land/r/x/benchmark/ops.gno b/examples/gno.land/r/x/benchmark/ops.gno index 7d8e282bb61..4a3e268ee6e 100644 --- a/examples/gno.land/r/x/benchmark/ops.gno +++ b/examples/gno.land/r/x/benchmark/ops.gno @@ -284,13 +284,11 @@ func OpDefer() int { }() return b } + /* func OpUnary() - - */ -func OpUnary(){ - +func OpUnary() { a := 1 b := -a b = +a @@ -298,8 +296,8 @@ func OpUnary(){ c := true d := !c - } + /* func OpBinary() @@ -456,8 +454,6 @@ OpExec, bodyStmt[0/0/23]=(end) OpExec, return OpReturnFromBlock, [FRAME FUNC:OpBinary RECV:(undefined) (0 args) 1/0/0/0/1 LASTPKG:main LASTRLM:Realm(nil)] OpHalt - - */ func OpBinary() { a := 1_000_000 @@ -469,41 +465,40 @@ func OpBinary() { c = true || false c = true && false c = a == b - c = a != b + c = a != b c = a < b c = a <= b c = a > b c = a >= b // - d = a + b // 1 - d = a - b // 0 - d = a | b // 1 - d = a ^ b // 1 - d = a * b // 1,000,001,000,000 + d = a + b // 1 + d = a - b // 0 + d = a | b // 1 + d = a ^ b // 1 + d = a * b // 1,000,001,000,000 d = a / b d = a % b d = 63 << a d = a >> 63 d = a & b d = a &^ b - } -type foo struct{ - i int +type foo struct { + i int } -func (f foo) bark() { +func (f foo) bark() { } -type dog interface{ - - bark() +type dog interface { + bark() } -type foofighter struct{ +type foofighter struct { f foo } + /* func ExprOps() OpEval, [(const (2 int))](const-type int){(const (0 int)), (const (1 int))} OpEval, [(const (2 int))](const-type int) @@ -642,29 +637,158 @@ OpHalt */ func ExprOps() { - a := [2]int{0,1} // OpArrayLit + a := [2]int{0, 1} // OpArrayLit + + a2 := [...]int{0, 1} // same as [2]int + m := make(map[int]int) // OpMapLit + s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} // OpSliceLit - a2 := [...]int{0,1} // same as [2]int - m := make(map[int]int) // OpMapLit - s := []int{0,1,2,3,4,5,6,7,8,9} // OpSliceLit + s2 := []int{9: 90} // OpSliceLit2 + f := foo{i: 1} // OpStructLit + ff := foofighter{f: f} // OpCompositeLit - s2 := []int{9:90} // OpSliceLit2 - f :=foo {i: 1} // OpStructLit - ff :=foofighter{f: f} // OpCompositeLit + b := a[0] // OpIndex1 + b, _ = m[0] // OpIndex2 + b = f.i // OpSelector - b := a[0] // OpIndex1 - b, _ = m[0] // OpIndex2 - b = f.i // OpSelector + subs := s[1:5:10] // OpSlice - subs := s[1:5:10] // OpSlice + ptr := &a2[0] // OpRef + b = *ptr // OpStar + + var d dog + d = f + f = d.(foo) // OpTypeAssert1 concrete type + // d = f.(d) // OpTypeAssert1 interface + f, ok := d.(foo) // OpTypeAssert2 + +} + +/* +OpEval, (const (true bool)) +OpDefine, a := (const (true bool)) +OpExec, bodyStmt[0/0/1]=b := (const (false bool)) +OpEval, (const (false bool)) +OpDefine, b := (const (false bool)) +OpExec, bodyStmt[0/0/2]=b || a +OpEval, b || a +OpEval, b +OpEval, a +OpLor, (false bool), (true bool) +OpExec, bodyStmt[0/0/3]=a || b +OpEval, a || b +OpEval, a +OpExec, bodyStmt[0/0/4]=(end) +OpExec, return +OpReturnFromBlock, [FRAME FUNC:OpLor RECV:(undefined) (0 args) 1/0/0/0/1 LASTPKG:main LASTRLM:Realm(nil)] +OpHalt +*/ +func OpLor() { + a := true + b := false + b || a + a || b +} - ptr := &a2[0] //OpRef - b = *ptr // OpStar +/* +OpEval, (const (true bool)) +OpDefine, a := (const (true bool)) +OpExec, bodyStmt[0/0/1]=b := (const (false bool)) +OpEval, (const (false bool)) +OpDefine, b := (const (false bool)) +OpExec, bodyStmt[0/0/2]=a && b +OpEval, a && b +OpEval, a +OpEval, b +OpLand, (true bool), (false bool) +OpExec, bodyStmt[0/0/3]=(end) +OpExec, return +OpReturnFromBlock, [FRAME FUNC:OpLand RECV:(undefined) (0 args) 1/0/0/0/1 LASTPKG:main LASTRLM:Realm(nil)] +OpHalt +*/ +func OpLand() { + a := true + b := false + a && b +} - var d dog - d = f - f = d.(foo) // OpTypeAssert1 concrete type -// d = f.(d) // OpTypeAssert1 interface - f, ok := d.(foo) //OpTypeAssert2 +/* +OpEval, (const (recover func()(exception interface{}))) +OpDefer, defer (const (recover func()(exception interface{})))() +OpExec, bodyStmt[0/0/1]=panic((const ("panic" string))) +OpEval, (const ("panic" string)) +OpPanic1 +OpReturnCallDefers +OpReturnCallDefers +OpPanic2 +OpReturnCallDefers +OpReturnFromBlock, [FRAME FUNC:OpPanic RECV:(undefined) (0 args) 1/0/0/0/1 LASTPKG:main LASTRLM:Realm(nil)] +OpHalt +*/ +func OpPanic() { + defer recover() + panic("panic") +} +/* +OpEval, []interface { }{(const (1 int)), (const ("hello" string)), (const (1 float64)), interface { }} +OpEval, []interface { } +OpEval, interface { } +OpEval, (const (1 int)) +OpEval, (const ("hello" string)) +OpEval, (const (1 float64)) +OpEval, interface { } +OpDefine, values := []interface { }{(const (1 int)), (const ("hello" string)), (const (1 float64)), interface { }} +OpExec, bodyStmt[0/0/1]=for _, v := range values { switch v { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: } } +OpEval, values +OpExec, bodyStmt[0/0/-2]=(init) +OpEval, v +OpTypeSwitch, switch v { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: }, (1 int) +OpPopBlock, Block(ID:0000000000000000000000000000000000000000:0,Addr:0xc00946c960,Source:switch v { case (c...,Parent:0xc00946c780) +OpPopFrameAndReset, [FRAME LABEL: 4/2/0/3/3] +OpExec, bodyStmt[4/0/1]=(end) +OpExec, bodyStmt[4/1/-1]=(init) +OpEval, v +OpTypeSwitch, switch v { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: }, ("hello" string) +OpPopBlock, Block(ID:0000000000000000000000000000000000000000:0,Addr:0xc00946cb40,Source:switch v { case (c...,Parent:0xc00946c780) +OpPopFrameAndReset, [FRAME LABEL: 4/2/0/3/3] +OpExec, bodyStmt[4/1/1]=(end) +OpExec, bodyStmt[4/2/-1]=(init) +OpEval, v +OpTypeSwitch, switch v { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: }, (1 float64) +OpPopBlock, Block(ID:0000000000000000000000000000000000000000:0,Addr:0xc00946cd20,Source:switch v { case (c...,Parent:0xc00946c780) +OpPopFrameAndReset, [FRAME LABEL: 4/2/0/3/3] +OpExec, bodyStmt[4/2/1]=(end) +OpExec, bodyStmt[4/3/-1]=(init) +OpEval, v +OpTypeSwitch, switch v { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: }, (typeval{interface{} (0xc00cfd6780)} type{}) +OpPopBlock, Block(ID:0000000000000000000000000000000000000000:0,Addr:0xc00946cf00,Source:switch v { case (c...,Parent:0xc00946c780) +OpPopFrameAndReset, [FRAME LABEL: 4/2/0/3/3] +OpExec, bodyStmt[4/3/1]=(end) +OpPopFrameAndReset, [FRAME LABEL: 3/1/0/2/2] +OpExec, bodyStmt[0/0/2]=(end) +OpExec, return +OpReturnFromBlock, [FRAME FUNC:OpTypeSwitch RECV:(undefined) (0 args) 1/0/0/0/1 LASTPKG:main LASTRLM:Realm(nil)] +OpHalt +*/ +func OpTypeSwitch() { + values := []interface{}{1, "hello", 1.0, interface{}} + for _, v := range values { + switch v.(type) { + case int: + // ... + case string: + // ... + case float64: + // ... + default: + // ... + } + } } + +// func OpCallDeferNativeBody() { +// defer func() { +// s := strconv.Itoa(1) +// }() +// } diff --git a/gnovm/pkg/gnolang/op_binary.go b/gnovm/pkg/gnolang/op_binary.go index 51f86ac84e8..a627a820017 100644 --- a/gnovm/pkg/gnolang/op_binary.go +++ b/gnovm/pkg/gnolang/op_binary.go @@ -50,6 +50,9 @@ func (m *Machine) doOpLor() { // get right and left operands. rv := m.PopValue() lv := m.PeekValue(1) // also the result + if bm.OpCodeDetails && bm.Start { + log.Printf("benchmark.OpLor, %v, %v\n", lv, rv) + } if debug { assertSameTypes(lv.T, rv.T) } @@ -67,6 +70,10 @@ func (m *Machine) doOpLand() { // get right and left operands. rv := m.PopValue() lv := m.PeekValue(1) // also the result + if bm.OpCodeDetails && bm.Start { + log.Printf("benchmark.OpLand, %v, %v\n", lv, rv) + } + if debug { assertSameTypes(lv.T, rv.T) } @@ -214,7 +221,7 @@ func (m *Machine) doOpAdd() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpAdd, %v + %v\n", lv , rv) + log.Printf("benchmark.OpAdd, %v + %v\n", lv, rv) } // add rv to lv. addAssign(m.Alloc, lv, rv) @@ -230,7 +237,7 @@ func (m *Machine) doOpSub() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpSub, %v - %v\n", lv , rv) + log.Printf("benchmark.OpSub, %v - %v\n", lv, rv) } // sub rv from lv. subAssign(lv, rv) @@ -247,7 +254,7 @@ func (m *Machine) doOpBor() { } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpBor, %v | %v\n", lv , rv) + log.Printf("benchmark.OpBor, %v | %v\n", lv, rv) } // lv | rv borAssign(lv, rv) @@ -263,7 +270,7 @@ func (m *Machine) doOpXor() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpXor, %v ^ %v\n", lv , rv) + log.Printf("benchmark.OpXor, %v ^ %v\n", lv, rv) } // lv ^ rv xorAssign(lv, rv) @@ -279,7 +286,7 @@ func (m *Machine) doOpMul() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpMul, %v * %v\n", lv , rv) + log.Printf("benchmark.OpMul, %v * %v\n", lv, rv) } // lv * rv mulAssign(lv, rv) @@ -295,7 +302,7 @@ func (m *Machine) doOpQuo() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpQuo, %v / %v\n", lv , rv) + log.Printf("benchmark.OpQuo, %v / %v\n", lv, rv) } // lv / rv quoAssign(lv, rv) @@ -311,7 +318,7 @@ func (m *Machine) doOpRem() { assertSameTypes(lv.T, rv.T) } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpRem, %v %% %v\n", lv , rv) + log.Printf("benchmark.OpRem, %v %% %v\n", lv, rv) } // lv % rv remAssign(lv, rv) @@ -329,7 +336,7 @@ func (m *Machine) doOpShl() { } } if bm.OpCodeDetails && bm.Start { - log.Printf("benchmark.OpShl, %v << %v\n", lv , rv) + log.Printf("benchmark.OpShl, %v << %v\n", lv, rv) } // lv << rv diff --git a/gnovm/pkg/gnolang/op_call.go b/gnovm/pkg/gnolang/op_call.go index 086858bb0ab..c9a707f00e1 100644 --- a/gnovm/pkg/gnolang/op_call.go +++ b/gnovm/pkg/gnolang/op_call.go @@ -182,7 +182,10 @@ func (m *Machine) doOpCallNativeBody() { func (m *Machine) doOpCallDeferNativeBody() { fv := m.PopValue().V.(*FuncValue) - log.Printf("benchmark.OpCallDeferNativeBody, %v\n", fv) + if bm.OpCodeDetails && bm.Start { + log.Printf("benchmark.OpCallDeferNativeBody, %v\n", fv) + } + fv.nativeBody(m) } diff --git a/gnovm/pkg/gnolang/op_exec.go b/gnovm/pkg/gnolang/op_exec.go index a7b45c88323..496a07be35a 100644 --- a/gnovm/pkg/gnolang/op_exec.go +++ b/gnovm/pkg/gnolang/op_exec.go @@ -842,6 +842,10 @@ func (m *Machine) doOpIfCond() { func (m *Machine) doOpTypeSwitch() { ss := m.PopStmt().(*SwitchStmt) xv := m.PopValue() + if bm.OpCodeDetails && bm.Start { + log.Printf("benchmark.OpTypeSwitch, %v, %v\n", ss, xv) + } + xtid := TypeID("") if xv.T != nil { xtid = xv.T.TypeID()