Skip to content

Commit

Permalink
Merge pull request #3761 from onflow/bastian/improve-comparison-and-a…
Browse files Browse the repository at this point in the history
…rithmetic-instructions

[Compiler] Generalize arithmetic instructions for all number values
  • Loading branch information
turbolent authored Feb 6, 2025
2 parents 6443aa9 + ad9ba17 commit d861cc4
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 256 deletions.
18 changes: 9 additions & 9 deletions bbq/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,27 +1093,27 @@ func (c *Compiler[_]) VisitBinaryExpression(expression *ast.BinaryExpression) (_

switch expression.Operation {
case ast.OperationPlus:
c.codeGen.Emit(opcode.InstructionIntAdd{})
c.codeGen.Emit(opcode.InstructionAdd{})
case ast.OperationMinus:
c.codeGen.Emit(opcode.InstructionIntSubtract{})
c.codeGen.Emit(opcode.InstructionSubtract{})
case ast.OperationMul:
c.codeGen.Emit(opcode.InstructionIntMultiply{})
c.codeGen.Emit(opcode.InstructionMultiply{})
case ast.OperationDiv:
c.codeGen.Emit(opcode.InstructionIntDivide{})
c.codeGen.Emit(opcode.InstructionDivide{})
case ast.OperationMod:
c.codeGen.Emit(opcode.InstructionIntMod{})
c.codeGen.Emit(opcode.InstructionMod{})
case ast.OperationEqual:
c.codeGen.Emit(opcode.InstructionEqual{})
case ast.OperationNotEqual:
c.codeGen.Emit(opcode.InstructionNotEqual{})
case ast.OperationLess:
c.codeGen.Emit(opcode.InstructionIntLess{})
c.codeGen.Emit(opcode.InstructionLess{})
case ast.OperationLessEqual:
c.codeGen.Emit(opcode.InstructionIntLessOrEqual{})
c.codeGen.Emit(opcode.InstructionLessOrEqual{})
case ast.OperationGreater:
c.codeGen.Emit(opcode.InstructionIntGreater{})
c.codeGen.Emit(opcode.InstructionGreater{})
case ast.OperationGreaterEqual:
c.codeGen.Emit(opcode.InstructionIntGreaterOrEqual{})
c.codeGen.Emit(opcode.InstructionGreaterOrEqual{})
default:
panic(errors.NewUnreachableError())
}
Expand Down
22 changes: 11 additions & 11 deletions bbq/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,26 @@ func TestCompileRecursionFib(t *testing.T) {
// if n < 2
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 6},
// then return n
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionReturnValue{},
// fib(n - 1)
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntSubtract{},
opcode.InstructionSubtract{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionGetGlobal{GlobalIndex: 0x0},
opcode.InstructionInvoke{},
// fib(n - 2)
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntSubtract{},
opcode.InstructionSubtract{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionGetGlobal{GlobalIndex: 0x0},
opcode.InstructionInvoke{},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
// assign to temp $result
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x1},
Expand Down Expand Up @@ -148,12 +148,12 @@ func TestCompileImperativeFib(t *testing.T) {
// while i < n
opcode.InstructionGetLocal{LocalIndex: 0x4},
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 33},
// fibonacci = fib1 + fib2
opcode.InstructionGetLocal{LocalIndex: 0x1},
opcode.InstructionGetLocal{LocalIndex: 0x2},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x3},
// fib1 = fib2
Expand All @@ -167,7 +167,7 @@ func TestCompileImperativeFib(t *testing.T) {
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x4},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x4},
// continue loop
Expand Down Expand Up @@ -234,14 +234,14 @@ func TestCompileBreak(t *testing.T) {
// if i > 3
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntGreater{},
opcode.InstructionGreater{},
opcode.InstructionJumpIfFalse{Target: 10},
// break
opcode.InstructionJump{Target: 16},
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x2},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x0},
// repeat
Expand Down Expand Up @@ -313,13 +313,13 @@ func TestCompileContinue(t *testing.T) {
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x0},
// if i < 3
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x2},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 15},
// continue
opcode.InstructionJump{Target: 3},
Expand Down
2 changes: 2 additions & 0 deletions bbq/opcode/gen/instructions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"value",
"string",
"array",
"dictionary",
"integer",
"number",
"bool",
"path",
"int",
Expand Down
Loading

0 comments on commit d861cc4

Please sign in to comment.