Skip to content

Commit

Permalink
Merge pull request #3718 from onflow/supun/improve-instructions
Browse files Browse the repository at this point in the history
[Compiler+VM POC] Improve the custom instructions set
  • Loading branch information
SupunS authored Dec 17, 2024
2 parents b2c56c9 + 4ac394b commit a24debb
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 68 deletions.
28 changes: 18 additions & 10 deletions bbq/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,14 @@ func (c *Compiler[_]) addConstant(kind constantkind.ConstantKind, data []byte) *
}

func (c *Compiler[_]) stringConstLoad(str string) {
constant := c.addConstant(constantkind.String, []byte(str))
constant := c.addStringConst(str)
c.codeGen.Emit(opcode.InstructionGetConstant{ConstantIndex: constant.index})
}

func (c *Compiler[_]) addStringConst(str string) *constant {
return c.addConstant(constantkind.String, []byte(str))
}

func (c *Compiler[_]) emitJump(target int) int {
if target >= math.MaxUint16 {
panic(errors.NewDefaultUserError("invalid jump"))
Expand Down Expand Up @@ -648,8 +652,8 @@ func (c *Compiler[_]) VisitAssignmentStatement(statement *ast.AssignmentStatemen

case *ast.MemberExpression:
c.compileExpression(target.Expression)
c.stringConstLoad(target.Identifier.Identifier)
c.codeGen.Emit(opcode.InstructionSetField{})
constant := c.addStringConst(target.Identifier.Identifier)
c.codeGen.Emit(opcode.InstructionSetField{FieldNameIndex: constant.index})

case *ast.IndexExpression:
c.compileExpression(target.TargetExpression)
Expand Down Expand Up @@ -829,11 +833,12 @@ func (c *Compiler[_]) VisitInvocationExpression(expression *ast.InvocationExpres
panic(errors.NewDefaultUserError("invalid number of arguments"))
}

funcNameConst := c.addStringConst(funcName)
c.codeGen.Emit(
opcode.InstructionInvokeDynamic{
Name: funcName,
TypeArgs: typeArgs,
ArgCount: uint16(argumentCount),
NameIndex: funcNameConst.index,
TypeArgs: typeArgs,
ArgCount: uint16(argumentCount),
},
)

Expand Down Expand Up @@ -914,8 +919,8 @@ func (c *Compiler[_]) loadTypeArguments(expression *ast.InvocationExpression) []

func (c *Compiler[_]) VisitMemberExpression(expression *ast.MemberExpression) (_ struct{}) {
c.compileExpression(expression.Expression)
c.stringConstLoad(expression.Identifier.Identifier)
c.codeGen.Emit(opcode.InstructionGetField{})
constant := c.addStringConst(expression.Identifier.Identifier)
c.codeGen.Emit(opcode.InstructionGetField{FieldNameIndex: constant.index})
return
}

Expand Down Expand Up @@ -1063,10 +1068,13 @@ func (c *Compiler[_]) VisitPathExpression(expression *ast.PathExpression) (_ str
if len(identifier) >= math.MaxUint16 {
panic(errors.NewDefaultUserError("invalid identifier"))
}

identifierConst := c.addStringConst(identifier)

c.codeGen.Emit(
opcode.InstructionPath{
Domain: domain,
Identifier: identifier,
Domain: domain,
IdentifierIndex: identifierConst.index,
},
)
return
Expand Down
12 changes: 9 additions & 3 deletions bbq/opcode/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,19 @@ func instructionStringFuncDecl(ins instruction) *dst.FuncDecl {
)

for _, operand := range ins.Operands {
var funcName string
switch operand.Type {
case operandTypeIndices:
funcName = "printfUInt16ArrayArgument"
default:
funcName = "printfArgument"
}
stmts = append(
stmts,
&dst.ExprStmt{
X: &dst.CallExpr{
Fun: &dst.Ident{
Name: "Fprintf",
Path: "fmt",
Name: funcName,
},
Args: []dst.Expr{
&dst.UnaryExpr{
Expand All @@ -488,7 +494,7 @@ func instructionStringFuncDecl(ins instruction) *dst.FuncDecl {
},
&dst.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf(`" %s:%%s"`, operand.Name),
Value: fmt.Sprintf(`"%s"`, operand.Name),
},
&dst.SelectorExpr{
X: dst.NewIdent("i"),
Expand Down
21 changes: 21 additions & 0 deletions bbq/opcode/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package opcode

import (
"fmt"
"strings"

"github.com/onflow/cadence/common"
)

Expand Down Expand Up @@ -161,3 +164,21 @@ func DecodeInstructions(code []byte) []Instruction {
}
return instructions
}

// Instruction pretty print

func printfUInt16ArrayArgument(sb *strings.Builder, argName string, values []uint16) {
_, _ = fmt.Fprintf(sb, " %s:[", argName)
for i, value := range values {
if i > 0 {
_, _ = fmt.Fprint(sb, ", ")
}
_, _ = fmt.Fprintf(sb, "%d", value)
}

sb.WriteString("]")
}

func printfArgument(sb *strings.Builder, fieldName string, v any) {
_, _ = fmt.Fprintf(sb, " %s:%v", fieldName, v)
}
Loading

0 comments on commit a24debb

Please sign in to comment.