From e01259ba7772435329e4c17b460b96fe35b6ffe2 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 30 Aug 2018 05:54:40 +0200 Subject: [PATCH] interpreter: string concatenation Sometimes strings are concatenated in a way that isn't const-propagated by the SSA transformation (e.g. the result of a function call). Concatenate them during init() interpretation. --- interpreter.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/interpreter.go b/interpreter.go index 62471e70..f137f530 100644 --- a/interpreter.go +++ b/interpreter.go @@ -43,6 +43,24 @@ func (p *Program) interpret(instrs []ssa.Instruction, paramKeys []*ssa.Parameter return i, err } locals[instr] = &PointerValue{nil, &alloc} + case *ssa.BinOp: + if typ, ok := instr.Type().(*types.Basic); ok && typ.Kind() == types.String { + // Concatenate two strings. + // This happens in the time package, for example. + x, err := p.getValue(instr.X, locals) + if err != nil { + return i, err + } + y, err := p.getValue(instr.Y, locals) + if err != nil { + return i, err + } + xstr := constant.StringVal(x.(*ConstValue).Expr.Value) + ystr := constant.StringVal(y.(*ConstValue).Expr.Value) + locals[instr] = &ConstValue{ssa.NewConst(constant.MakeString(xstr+ystr), types.Typ[types.String])} + } else { + return i, errors.New("init: unknown binop: " + instr.String()) + } case *ssa.Call: common := instr.Common() callee := common.StaticCallee()