Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added more opcode functions #6

Merged
merged 1 commit into from
Mar 8, 2024
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
202 changes: 163 additions & 39 deletions examples/gno.land/r/x/benchmark/ops.gno
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,20 @@ func OpDefer() int {
}()
return b
}

/*
func OpUnary()


*/
func OpUnary(){

func OpUnary() {
a := 1
b := -a
b = +a
b = ^a

c := true
d := !c

}

/*
func OpBinary()

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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<VPBlock(1,0)> := (const (true bool))
OpExec, bodyStmt[0/0/1]=b<VPBlock(1,1)> := (const (false bool))
OpEval, (const (false bool))
OpDefine, b<VPBlock(1,1)> := (const (false bool))
OpExec, bodyStmt[0/0/2]=b<VPBlock(1,1)> || a<VPBlock(1,0)>
OpEval, b<VPBlock(1,1)> || a<VPBlock(1,0)>
OpEval, b<VPBlock(1,1)>
OpEval, a<VPBlock(1,0)>
OpLor, (false bool), (true bool)
OpExec, bodyStmt[0/0/3]=a<VPBlock(1,0)> || b<VPBlock(1,1)>
OpEval, a<VPBlock(1,0)> || b<VPBlock(1,1)>
OpEval, a<VPBlock(1,0)>
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<VPBlock(1,0)> := (const (true bool))
OpExec, bodyStmt[0/0/1]=b<VPBlock(1,1)> := (const (false bool))
OpEval, (const (false bool))
OpDefine, b<VPBlock(1,1)> := (const (false bool))
OpExec, bodyStmt[0/0/2]=a<VPBlock(1,0)> && b<VPBlock(1,1)>
OpEval, a<VPBlock(1,0)> && b<VPBlock(1,1)>
OpEval, a<VPBlock(1,0)>
OpEval, b<VPBlock(1,1)>
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" <untyped> string)))
OpEval, (const ("panic" <untyped> 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<VPBlock(1,0)> := []interface { }{(const (1 int)), (const ("hello" string)), (const (1 float64)), interface { }}
OpExec, bodyStmt[0/0/1]=for _<VPBlock(0,0)>, v<VPBlock(1,0)> := range values<VPBlock(2,0)> { switch v<VPBlock(2,0)> { case (const-type int): ; case (const-type string): ; case (const-type float64): ; default: } }
OpEval, values<VPBlock(2,0)>
OpExec, bodyStmt[0/0/-2]=(init)
OpEval, v<VPBlock(2,0)>
OpTypeSwitch, switch v<VPBlock(2,0)> { 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<VPBlock(2,0)> { 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<VPBlock(2,0)>
OpTypeSwitch, switch v<VPBlock(2,0)> { 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<VPBlock(2,0)> { 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<VPBlock(2,0)>
OpTypeSwitch, switch v<VPBlock(2,0)> { 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<VPBlock(2,0)> { 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<VPBlock(2,0)>
OpTypeSwitch, switch v<VPBlock(2,0)> { 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<VPBlock(2,0)> { 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)
// }()
// }
23 changes: 15 additions & 8 deletions gnovm/pkg/gnolang/op_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion gnovm/pkg/gnolang/op_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions gnovm/pkg/gnolang/op_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading