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

Opbinaryop and other minor optimizations #157

Merged
merged 9 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 15 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (c *Compiler) Compile(node ast.Node) error {
return err
}

c.emit(node, OpBinaryOp)
c.emit(node, OpGreaterThan)

return nil
Expand All @@ -130,6 +131,7 @@ func (c *Compiler) Compile(node ast.Node) error {
return err
}

c.emit(node, OpBinaryOp)
c.emit(node, OpGreaterThanEqual)

return nil
Expand All @@ -144,34 +146,47 @@ func (c *Compiler) Compile(node ast.Node) error {

switch node.Token {
case token.Add:
c.emit(node, OpBinaryOp)
c.emit(node, OpAdd)
case token.Sub:
c.emit(node, OpBinaryOp)
c.emit(node, OpSub)
case token.Mul:
c.emit(node, OpBinaryOp)
c.emit(node, OpMul)
case token.Quo:
c.emit(node, OpBinaryOp)
c.emit(node, OpDiv)
case token.Rem:
c.emit(node, OpBinaryOp)
c.emit(node, OpRem)
case token.Greater:
c.emit(node, OpBinaryOp)
c.emit(node, OpGreaterThan)
case token.GreaterEq:
c.emit(node, OpBinaryOp)
c.emit(node, OpGreaterThanEqual)
case token.Equal:
c.emit(node, OpEqual)
case token.NotEqual:
c.emit(node, OpNotEqual)
case token.And:
c.emit(node, OpBinaryOp)
c.emit(node, OpBAnd)
case token.Or:
c.emit(node, OpBinaryOp)
c.emit(node, OpBOr)
case token.Xor:
c.emit(node, OpBinaryOp)
c.emit(node, OpBXor)
case token.AndNot:
c.emit(node, OpBinaryOp)
c.emit(node, OpBAndNot)
case token.Shl:
c.emit(node, OpBinaryOp)
c.emit(node, OpBShiftLeft)
case token.Shr:
c.emit(node, OpBinaryOp)
c.emit(node, OpBShiftRight)
default:
return c.errorf(node, "invalid binary operator: %s", node.Token.String())
Expand Down
11 changes: 11 additions & 0 deletions compiler/compiler_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,37 @@ func (c *Compiler) compileAssign(node ast.Node, lhs, rhs []ast.Expr, op token.To

switch op {
case token.AddAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpAdd)
case token.SubAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpSub)
case token.MulAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpMul)
case token.QuoAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpDiv)
case token.RemAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpRem)
case token.AndAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBAnd)
case token.OrAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBOr)
case token.AndNotAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBAndNot)
case token.XorAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBXor)
case token.ShlAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBShiftLeft)
case token.ShrAssign:
c.emit(node, OpBinaryOp)
c.emit(node, OpBShiftRight)
}

Expand Down
32 changes: 30 additions & 2 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -40,6 +41,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpSub),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -51,6 +53,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpMul),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -62,6 +65,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpDiv),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -87,6 +91,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpGreaterThan),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -98,6 +103,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpGreaterThan),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand Down Expand Up @@ -204,6 +210,7 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand All @@ -219,6 +226,7 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpSetGlobal, 1),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpGetGlobal, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpSetGlobal, 0)),
objectsArray(
Expand All @@ -234,6 +242,7 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpSetGlobal, 1),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpGetGlobal, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpDiv),
compiler.MakeInstruction(compiler.OpSetGlobal, 0)),
objectsArray(
Expand Down Expand Up @@ -265,12 +274,15 @@ func TestCompiler_Compile(t *testing.T) {
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpSub),
compiler.MakeInstruction(compiler.OpConstant, 4),
compiler.MakeInstruction(compiler.OpConstant, 5),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpMul),
compiler.MakeInstruction(compiler.OpArray, 3),
compiler.MakeInstruction(compiler.OpPop)),
Expand Down Expand Up @@ -314,10 +326,12 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpConstant, 4),
compiler.MakeInstruction(compiler.OpConstant, 5),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpMul),
compiler.MakeInstruction(compiler.OpMap, 4),
compiler.MakeInstruction(compiler.OpPop)),
Expand All @@ -338,6 +352,7 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpArray, 3),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpConstant, 4),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpIndex),
compiler.MakeInstruction(compiler.OpPop)),
Expand All @@ -356,6 +371,7 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpMap, 2),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpSub),
compiler.MakeInstruction(compiler.OpIndex),
compiler.MakeInstruction(compiler.OpPop)),
Expand Down Expand Up @@ -444,6 +460,7 @@ func TestCompiler_Compile(t *testing.T) {
compiledFunction(0, 0,
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpReturnValue)))))

Expand All @@ -458,6 +475,7 @@ func TestCompiler_Compile(t *testing.T) {
compiledFunction(0, 0,
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpPop),
compiler.MakeInstruction(compiler.OpReturn)))))
Expand Down Expand Up @@ -636,6 +654,7 @@ func TestCompiler_Compile(t *testing.T) {
compiler.MakeInstruction(compiler.OpDefineLocal, 1),
compiler.MakeInstruction(compiler.OpGetLocal, 0),
compiler.MakeInstruction(compiler.OpGetLocal, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpReturnValue)))))

Expand Down Expand Up @@ -722,6 +741,7 @@ func TestCompiler_Compile(t *testing.T) {
compiledFunction(1, 1,
compiler.MakeInstruction(compiler.OpGetFree, 0),
compiler.MakeInstruction(compiler.OpGetLocal, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpReturnValue)),
compiledFunction(1, 1,
Expand All @@ -746,8 +766,10 @@ func(a) {
compiledFunction(1, 1,
compiler.MakeInstruction(compiler.OpGetFree, 0),
compiler.MakeInstruction(compiler.OpGetFree, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpGetLocal, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpReturnValue)),
compiledFunction(1, 1,
Expand Down Expand Up @@ -792,10 +814,13 @@ func() {
compiler.MakeInstruction(compiler.OpDefineLocal, 0),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpGetFree, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpGetFree, 1),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpGetLocal, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpReturnValue)),
compiledFunction(1, 0,
Expand All @@ -819,10 +844,12 @@ func() {
compiler.MakeInstruction(compiler.OpSetGlobal, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpGreaterThan),
compiler.MakeInstruction(compiler.OpJumpFalsy, 29),
compiler.MakeInstruction(compiler.OpJumpFalsy, 31),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpAdd),
compiler.MakeInstruction(compiler.OpSetGlobal, 0),
compiler.MakeInstruction(compiler.OpJump, 6)),
Expand Down Expand Up @@ -863,9 +890,10 @@ func() {
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpNotEqual),
compiler.MakeInstruction(compiler.OpOrJump, 33),
compiler.MakeInstruction(compiler.OpOrJump, 34),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpBinaryOp),
compiler.MakeInstruction(compiler.OpGreaterThan),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
Expand Down
24 changes: 24 additions & 0 deletions compiler/opcodes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package compiler

import (
"github.com/d5/tengo/compiler/token"
)

// Opcode represents a single byte operation code.
type Opcode = byte

Expand Down Expand Up @@ -59,6 +63,7 @@ const (
OpIteratorNext // Iterator next
OpIteratorKey // Iterator key
OpIteratorValue // Iterator value
OpBinaryOp // Iterator value
)

// OpcodeNames is opcode names.
Expand Down Expand Up @@ -117,6 +122,7 @@ var OpcodeNames = [...]string{
OpIteratorNext: "ITNXT",
OpIteratorKey: "ITKEY",
OpIteratorValue: "ITVAL",
OpBinaryOp: "BINARYOP",
}

// OpcodeOperands is the number of operands.
Expand Down Expand Up @@ -175,6 +181,24 @@ var OpcodeOperands = [...][]int{
OpIteratorNext: {},
OpIteratorKey: {},
OpIteratorValue: {},
OpBinaryOp: {},
}

// BinaryOpTokens is the mapping of opcodes to tokens.
var BinaryOpTokens = [...]token.Token{
OpAdd: token.Add,
OpSub: token.Sub,
OpMul: token.Mul,
OpDiv: token.Quo,
OpRem: token.Rem,
OpBAnd: token.And,
OpBOr: token.Or,
OpBXor: token.Xor,
OpBAndNot: token.AndNot,
OpBShiftLeft: token.Shl,
OpBShiftRight: token.Shr,
OpGreaterThan: token.Greater,
OpGreaterThanEqual: token.GreaterEq,
}

// ReadOperands reads operands from the bytecode.
Expand Down
11 changes: 11 additions & 0 deletions objects/compiled_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ func (o *CompiledFunction) IsFalsy() bool {
func (o *CompiledFunction) Equals(x Object) bool {
return false
}

// SourcePos returns the source position of the instruction at ip.
func (o *CompiledFunction) SourcePos(ip int) source.Pos {
for ip >= 0 {
if p, ok := o.SourceMap[ip]; ok {
return p
}
ip--
}
return source.NoPos
}
Loading