vm: fix codegen for APPEND

This commit is contained in:
Evgenii Stratonikov 2019-09-12 17:39:23 +03:00
parent d0ffd165fd
commit 56ec097d76
2 changed files with 15 additions and 1 deletions

View file

@ -203,6 +203,10 @@ func isSyscall(fun *funcScope) bool {
return ok return ok
} }
func isByteArrayType(t types.Type) bool {
return t.String() == "[]byte"
}
func isStringType(t types.Type) bool { func isStringType(t types.Type) bool {
return t.String() == "string" return t.String() == "string"
} }

View file

@ -614,7 +614,17 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
emitOpcode(c.prog, vm.ARRAYSIZE) emitOpcode(c.prog, vm.ARRAYSIZE)
} }
case "append": case "append":
emitOpcode(c.prog, vm.APPEND) arg := expr.Args[0]
typ := c.typeInfo.Types[arg].Type
if isByteArrayType(typ) {
emitOpcode(c.prog, vm.CAT)
} else {
emitOpcode(c.prog, vm.SWAP)
emitOpcode(c.prog, vm.DUP)
emitOpcode(c.prog, vm.PUSH2)
emitOpcode(c.prog, vm.XSWAP)
emitOpcode(c.prog, vm.APPEND)
}
case "SHA256": case "SHA256":
emitOpcode(c.prog, vm.SHA256) emitOpcode(c.prog, vm.SHA256)
case "SHA1": case "SHA1":