From 16489c0df640786a9c2c95923daa7439d4fe383a Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 5 May 2018 19:18:53 +0200 Subject: [PATCH] compiler: Workaround for runtime.boundsCheck in runtime dependencies --- tgo.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tgo.go b/tgo.go index 05fec0f5..26b2a785 100644 --- a/tgo.go +++ b/tgo.go @@ -843,11 +843,16 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) { // Bounds check. // LLVM optimizes this away in most cases. - constZero := llvm.ConstInt(c.intType, 0, false) - isNegative := c.builder.CreateICmp(llvm.IntSLT, index, constZero, "") // index < 0 - isTooBig := c.builder.CreateICmp(llvm.IntSGE, index, buflen, "") // index >= len(value) - isOverflow := c.builder.CreateOr(isNegative, isTooBig, "") - c.builder.CreateCall(c.mod.NamedFunction("runtime.boundsCheck"), []llvm.Value{isOverflow}, "") + // TODO: runtime.boundsCheck is undefined in packages imported by + // package runtime, so we have to remove it. This should be fixed. + boundsCheck := c.mod.NamedFunction("runtime.boundsCheck") + if !boundsCheck.IsNil() { + constZero := llvm.ConstInt(c.intType, 0, false) + isNegative := c.builder.CreateICmp(llvm.IntSLT, index, constZero, "") // index < 0 + isTooBig := c.builder.CreateICmp(llvm.IntSGE, index, buflen, "") // index >= len(value) + isOverflow := c.builder.CreateOr(isNegative, isTooBig, "") + c.builder.CreateCall(boundsCheck, []llvm.Value{isOverflow}, "") + } indices := []llvm.Value{ llvm.ConstInt(llvm.Int32Type(), 0, false),