From 4ed04309a36800c6e6c3185ed459c8cec5f36f40 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 3 Sep 2018 00:22:55 +0200 Subject: [PATCH] compiler: truncate shift amount when needed --- compiler.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler.go b/compiler.go index 4f901acb..552cb589 100644 --- a/compiler.go +++ b/compiler.go @@ -2108,6 +2108,13 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error // x and y must have equal sizes, make Y bigger in this case. // y is unsigned, this has been checked by the Go type checker. y = c.builder.CreateZExt(y, x.Type(), "") + } else if sizeX < sizeY { + // What about shifting more than the integer width? + // I'm not entirely sure what the Go spec is on that, but as + // Intel CPUs have undefined behavior when shifting more + // than the integer width I'm assuming it is also undefined + // in Go. + y = c.builder.CreateTrunc(y, x.Type(), "") } switch binop.Op { case token.SHL: // <<