From 911ce3a4bcaaed7140bd1922c3c5f5645d87f08d Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 13 Jan 2023 19:39:09 +0100 Subject: [PATCH] compiler: update golang.org/x/tools/ssa This package needs to be updated to support Go 1.20. There were a few backwards incompatible changes that required updates to the compiler package. --- compiler/compiler.go | 103 +++++++++++++++++++++---------------------- go.mod | 4 +- go.sum | 10 ++--- 3 files changed, 58 insertions(+), 59 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 903031fb..4abfdd55 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -818,7 +818,7 @@ func (c *compilerContext) createPackage(irbuilder llvm.Builder, pkg *ssa.Package member := pkg.Members[name] switch member := member.(type) { case *ssa.Function: - if member.Synthetic == "generic function" { + if member.TypeParams() != nil { // Do not try to build generic (non-instantiated) functions. continue } @@ -1949,28 +1949,55 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) { case *ssa.Global: panic("global is not an expression") case *ssa.Index: - array := b.getValue(expr.X) + collection := b.getValue(expr.X) index := b.getValue(expr.Index) - // Extend index to at least uintptr size, because getelementptr assumes - // index is a signed integer. - index = b.extendInteger(index, expr.Index.Type(), b.uintptrType) + switch xType := expr.X.Type().Underlying().(type) { + case *types.Basic: // extract byte from string + // Value type must be a string, which is a basic type. + if xType.Info()&types.IsString == 0 { + panic("lookup on non-string?") + } - // Check bounds. - arrayLen := expr.X.Type().Underlying().(*types.Array).Len() - arrayLenLLVM := llvm.ConstInt(b.uintptrType, uint64(arrayLen), false) - b.createLookupBoundsCheck(arrayLenLLVM, index) + // Sometimes, the index can be e.g. an uint8 or int8, and we have to + // correctly extend that type for two reasons: + // 1. The lookup bounds check expects an index of at least uintptr + // size. + // 2. getelementptr has signed operands, and therefore s[uint8(x)] + // can be lowered as s[int8(x)]. That would be a bug. + index = b.extendInteger(index, expr.Index.Type(), b.uintptrType) - // Can't load directly from array (as index is non-constant), so have to - // do it using an alloca+gep+load. - arrayType := array.Type() - alloca, allocaPtr, allocaSize := b.createTemporaryAlloca(arrayType, "index.alloca") - b.CreateStore(array, alloca) - zero := llvm.ConstInt(b.ctx.Int32Type(), 0, false) - ptr := b.CreateInBoundsGEP(arrayType, alloca, []llvm.Value{zero, index}, "index.gep") - result := b.CreateLoad(arrayType.ElementType(), ptr, "index.load") - b.emitLifetimeEnd(allocaPtr, allocaSize) - return result, nil + // Bounds check. + length := b.CreateExtractValue(collection, 1, "len") + b.createLookupBoundsCheck(length, index) + + // Lookup byte + buf := b.CreateExtractValue(collection, 0, "") + bufElemType := b.ctx.Int8Type() + bufPtr := b.CreateInBoundsGEP(bufElemType, buf, []llvm.Value{index}, "") + return b.CreateLoad(bufElemType, bufPtr, ""), nil + case *types.Array: // extract element from array + // Extend index to at least uintptr size, because getelementptr + // assumes index is a signed integer. + index = b.extendInteger(index, expr.Index.Type(), b.uintptrType) + + // Check bounds. + arrayLen := llvm.ConstInt(b.uintptrType, uint64(xType.Len()), false) + b.createLookupBoundsCheck(arrayLen, index) + + // Can't load directly from array (as index is non-constant), so + // have to do it using an alloca+gep+load. + arrayType := collection.Type() + alloca, allocaPtr, allocaSize := b.createTemporaryAlloca(arrayType, "index.alloca") + b.CreateStore(collection, alloca) + zero := llvm.ConstInt(b.ctx.Int32Type(), 0, false) + ptr := b.CreateInBoundsGEP(arrayType, alloca, []llvm.Value{zero, index}, "index.gep") + result := b.CreateLoad(arrayType.ElementType(), ptr, "index.load") + b.emitLifetimeEnd(allocaPtr, allocaSize) + return result, nil + default: + panic("unknown *ssa.Index type") + } case *ssa.IndexAddr: val := b.getValue(expr.X) index := b.getValue(expr.Index) @@ -2023,42 +2050,14 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) { default: panic("unreachable") } - case *ssa.Lookup: + case *ssa.Lookup: // map lookup value := b.getValue(expr.X) index := b.getValue(expr.Index) - switch xType := expr.X.Type().Underlying().(type) { - case *types.Basic: - // Value type must be a string, which is a basic type. - if xType.Info()&types.IsString == 0 { - panic("lookup on non-string?") - } - - // Sometimes, the index can be e.g. an uint8 or int8, and we have to - // correctly extend that type for two reasons: - // 1. The lookup bounds check expects an index of at least uintptr - // size. - // 2. getelementptr has signed operands, and therefore s[uint8(x)] - // can be lowered as s[int8(x)]. That would be a bug. - index = b.extendInteger(index, expr.Index.Type(), b.uintptrType) - - // Bounds check. - length := b.CreateExtractValue(value, 1, "len") - b.createLookupBoundsCheck(length, index) - - // Lookup byte - buf := b.CreateExtractValue(value, 0, "") - bufElemType := b.ctx.Int8Type() - bufPtr := b.CreateInBoundsGEP(bufElemType, buf, []llvm.Value{index}, "") - return b.CreateLoad(bufElemType, bufPtr, ""), nil - case *types.Map: - valueType := expr.Type() - if expr.CommaOk { - valueType = valueType.(*types.Tuple).At(0).Type() - } - return b.createMapLookup(xType.Key(), valueType, value, index, expr.CommaOk, expr.Pos()) - default: - panic("unknown lookup type: " + expr.String()) + valueType := expr.Type() + if expr.CommaOk { + valueType = valueType.(*types.Tuple).At(0).Type() } + return b.createMapLookup(expr.X.Type().Underlying().(*types.Map).Key(), valueType, value, index, expr.CommaOk, expr.Pos()) case *ssa.MakeChan: return b.createMakeChan(expr), nil case *ssa.MakeClosure: diff --git a/go.mod b/go.mod index e0a7d9f0..aa7e2c47 100644 --- a/go.mod +++ b/go.mod @@ -14,8 +14,8 @@ require ( github.com/mattn/go-colorable v0.1.8 github.com/mattn/go-tty v0.0.4 go.bug.st/serial v1.3.5 - golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 - golang.org/x/tools v0.1.11 + golang.org/x/sys v0.4.0 + golang.org/x/tools v0.5.0 gopkg.in/yaml.v2 v2.4.0 tinygo.org/x/go-llvm v0.0.0-20221028183034-8341240c0b32 ) diff --git a/go.sum b/go.sum index 0332ad3d..6a53cceb 100644 --- a/go.sum +++ b/go.sum @@ -46,7 +46,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= go.bug.st/serial v1.3.5 h1:k50SqGZCnHZ2MiBQgzccXWG+kd/XpOs1jUljpDDKzaE= go.bug.st/serial v1.3.5/go.mod h1:z8CesKorE90Qr/oRSJiEuvzYRKol9r/anJZEb5kt304= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -54,10 +54,10 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= -golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY= -golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4= +golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=