diff options
author | Ayke van Laethem <[email protected]> | 2022-09-22 13:33:00 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-10-19 22:23:19 +0200 |
commit | 62df1d7490ccf495dc08837ec72ee1d6042bc374 (patch) | |
tree | 379a9e4e5f46dc8293beeb3ba1f386f08bdf53c9 /interp | |
parent | 229746b71ef8c6ab095f0e97aa722959c6f07268 (diff) | |
download | tinygo-62df1d7490ccf495dc08837ec72ee1d6042bc374.tar.gz tinygo-62df1d7490ccf495dc08837ec72ee1d6042bc374.zip |
all: remove pointer ElementType calls
This is needed for opaque pointers, which are enabled by default in
LLVM 15.
Diffstat (limited to 'interp')
-rw-r--r-- | interp/compiler.go | 21 | ||||
-rw-r--r-- | interp/interp.go | 10 | ||||
-rw-r--r-- | interp/interpreter.go | 2 | ||||
-rw-r--r-- | interp/memory.go | 23 |
4 files changed, 33 insertions, 23 deletions
diff --git a/interp/compiler.go b/interp/compiler.go index 20723c646..ffef69e56 100644 --- a/interp/compiler.go +++ b/interp/compiler.go @@ -209,7 +209,7 @@ func (r *runner) compileFunction(llvmFn llvm.Value) *function { case llvm.Alloca: // Alloca allocates stack space for local variables. numElements := r.getValue(inst.llvmInst.Operand(0)).(literalValue).value.(uint32) - elementSize := r.targetData.TypeAllocSize(inst.llvmInst.Type().ElementType()) + elementSize := r.targetData.TypeAllocSize(inst.llvmInst.AllocatedType()) inst.operands = []value{ literalValue{elementSize * uint64(numElements)}, } @@ -218,17 +218,17 @@ func (r *runner) compileFunction(llvmFn llvm.Value) *function { inst.name = llvmInst.Name() ptr := llvmInst.Operand(0) n := llvmInst.OperandsCount() - elementType := ptr.Type().ElementType() + elementType := llvmInst.GEPSourceElementType() // gep: [source ptr, dest value size, pairs of indices...] inst.operands = []value{ r.getValue(ptr), - literalValue{r.targetData.TypeAllocSize(llvmInst.Type().ElementType())}, r.getValue(llvmInst.Operand(1)), literalValue{r.targetData.TypeAllocSize(elementType)}, } for i := 2; i < n; i++ { operand := r.getValue(llvmInst.Operand(i)) - if elementType.TypeKind() == llvm.StructTypeKind { + switch elementType.TypeKind() { + case llvm.StructTypeKind: index := operand.(literalValue).value.(uint32) elementOffset := r.targetData.ElementOffset(elementType, int(index)) // Encode operands in a special way. The elementOffset @@ -242,12 +242,15 @@ func (r *runner) compileFunction(llvmFn llvm.Value) *function { // runtime. inst.operands = append(inst.operands, literalValue{elementOffset}, literalValue{^uint64(index)}) elementType = elementType.StructElementTypes()[index] - } else { + case llvm.ArrayTypeKind: elementType = elementType.ElementType() elementSize := r.targetData.TypeAllocSize(elementType) elementSizeOperand := literalValue{elementSize} // Add operand * elementSizeOperand bytes to the pointer. inst.operands = append(inst.operands, operand, elementSizeOperand) + default: + // This should be unreachable. + panic("unknown type: " + elementType.String()) } } case llvm.BitCast, llvm.IntToPtr, llvm.PtrToInt: @@ -267,10 +270,12 @@ func (r *runner) compileFunction(llvmFn llvm.Value) *function { case llvm.StructTypeKind: offset += r.targetData.ElementOffset(indexingType, int(index)) indexingType = indexingType.StructElementTypes()[index] - default: // ArrayTypeKind + case llvm.ArrayTypeKind: indexingType = indexingType.ElementType() elementSize := r.targetData.TypeAllocSize(indexingType) offset += elementSize * uint64(index) + default: + panic("unknown type kind") // unreachable } } size := r.targetData.TypeAllocSize(inst.llvmInst.Type()) @@ -290,10 +295,12 @@ func (r *runner) compileFunction(llvmFn llvm.Value) *function { case llvm.StructTypeKind: offset += r.targetData.ElementOffset(indexingType, int(index)) indexingType = indexingType.StructElementTypes()[index] - default: // ArrayTypeKind + case llvm.ArrayTypeKind: indexingType = indexingType.ElementType() elementSize := r.targetData.TypeAllocSize(indexingType) offset += elementSize * uint64(index) + default: + panic("unknown type kind") // unreachable } } // insertvalue [agg, elt, byteOffset] diff --git a/interp/interp.go b/interp/interp.go index 856f08b18..7833dfe93 100644 --- a/interp/interp.go +++ b/interp/interp.go @@ -156,7 +156,7 @@ func Run(mod llvm.Module, timeout time.Duration, debug bool) error { if obj.constant { continue // constant buffers can't have been modified } - initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.Type().ElementType(), &mem) + initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.GlobalValueType(), &mem) if err == errInvalidPtrToIntSize { // This can happen when a previous interp run did not have the // correct LLVM type for a global and made something up. In that @@ -190,7 +190,7 @@ func Run(mod llvm.Module, timeout time.Duration, debug bool) error { if err != nil { return err } - if checks && initializer.Type() != obj.llvmGlobal.Type().ElementType() { + if checks && initializer.Type() != obj.llvmGlobal.GlobalValueType() { panic("initializer type mismatch") } obj.llvmGlobal.SetInitializer(initializer) @@ -213,7 +213,7 @@ func RunFunc(fn llvm.Value, timeout time.Duration, debug bool) error { r.pkgName = initName[:len(initName)-len(".init")] // Create new function with the interp result. - newFn := llvm.AddFunction(mod, fn.Name()+".tmp", fn.Type().ElementType()) + newFn := llvm.AddFunction(mod, fn.Name()+".tmp", fn.GlobalValueType()) newFn.SetLinkage(fn.Linkage()) newFn.SetVisibility(fn.Visibility()) entry := mod.Context().AddBasicBlock(newFn, "entry") @@ -263,11 +263,11 @@ func RunFunc(fn llvm.Value, timeout time.Duration, debug bool) error { if obj.constant { continue // constant, so can't have been modified } - initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.Type().ElementType(), &mem) + initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.GlobalValueType(), &mem) if err != nil { return err } - if checks && initializer.Type() != obj.llvmGlobal.Type().ElementType() { + if checks && initializer.Type() != obj.llvmGlobal.GlobalValueType() { panic("initializer type mismatch") } obj.llvmGlobal.SetInitializer(initializer) diff --git a/interp/interpreter.go b/interp/interpreter.go index 8c783a706..c438b4b61 100644 --- a/interp/interpreter.go +++ b/interp/interpreter.go @@ -655,7 +655,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent // GetElementPtr does pointer arithmetic, changing the offset of the // pointer into the underlying object. var offset uint64 - for i := 2; i < len(operands); i += 2 { + for i := 1; i < len(operands); i += 2 { index := operands[i].Uint() elementSize := operands[i+1].Uint() if int64(elementSize) < 0 { diff --git a/interp/memory.go b/interp/memory.go index 6a537bad8..82ab716d4 100644 --- a/interp/memory.go +++ b/interp/memory.go @@ -808,14 +808,17 @@ func (v rawValue) rawLLVMValue(mem *memoryView) (llvm.Value, error) { if err != nil { return llvm.Value{}, err } - elementType := field.Type().ElementType() - if elementType.TypeKind() == llvm.StructTypeKind { - // There are some special pointer types that should be used as a - // ptrtoint, so that they can be used in certain optimizations. - name := elementType.StructName() - if name == "runtime.typecodeID" || name == "runtime.funcValueWithSignature" { - uintptrType := ctx.IntType(int(mem.r.pointerSize) * 8) - field = llvm.ConstPtrToInt(field, uintptrType) + if !field.IsAGlobalVariable().IsNil() { + elementType := field.GlobalValueType() + if elementType.TypeKind() == llvm.StructTypeKind { + // There are some special pointer types that should be used + // as a ptrtoint, so that they can be used in certain + // optimizations. + name := elementType.StructName() + if name == "runtime.typecodeID" || name == "runtime.funcValueWithSignature" { + uintptrType := ctx.IntType(int(mem.r.pointerSize) * 8) + field = llvm.ConstPtrToInt(field, uintptrType) + } } } structFields = append(structFields, field) @@ -998,7 +1001,7 @@ func (v *rawValue) set(llvmValue llvm.Value, r *runner) { ptr := llvmValue.Operand(0) index := llvmValue.Operand(1) numOperands := llvmValue.OperandsCount() - elementType := ptr.Type().ElementType() + elementType := llvmValue.GEPSourceElementType() totalOffset := r.targetData.TypeAllocSize(elementType) * index.ZExtValue() for i := 2; i < numOperands; i++ { indexValue := llvmValue.Operand(i) @@ -1173,7 +1176,7 @@ func (r *runner) getValue(llvmValue llvm.Value) value { r.globals[llvmValue] = index r.objects = append(r.objects, obj) if !llvmValue.IsAGlobalVariable().IsNil() { - obj.size = uint32(r.targetData.TypeAllocSize(llvmValue.Type().ElementType())) + obj.size = uint32(r.targetData.TypeAllocSize(llvmValue.GlobalValueType())) if initializer := llvmValue.Initializer(); !initializer.IsNil() { obj.buffer = r.getValue(initializer) obj.constant = llvmValue.IsGlobalConstant() |