|
| 1 | +package gctype |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + |
| 8 | + "tinygo.org/x/go-llvm" |
| 9 | +) |
| 10 | + |
| 11 | +// getPointerBitmap scans the given LLVM type for pointers and sets bits in a |
| 12 | +// bigint at the word offset that contains a pointer. This scan is recursive. |
| 13 | +func getPointerBitmap(targetData llvm.TargetData, typ llvm.Type, name string) *big.Int { |
| 14 | + alignment := targetData.PrefTypeAlignment(llvm.PointerType(typ.Context().Int8Type(), 0)) |
| 15 | + switch typ.TypeKind() { |
| 16 | + case llvm.IntegerTypeKind, llvm.FloatTypeKind, llvm.DoubleTypeKind: |
| 17 | + return big.NewInt(0) |
| 18 | + case llvm.PointerTypeKind: |
| 19 | + return big.NewInt(1) |
| 20 | + case llvm.StructTypeKind: |
| 21 | + ptrs := big.NewInt(0) |
| 22 | + for i, subtyp := range typ.StructElementTypes() { |
| 23 | + subptrs := getPointerBitmap(targetData, subtyp, name) |
| 24 | + if subptrs.BitLen() == 0 { |
| 25 | + continue |
| 26 | + } |
| 27 | + offset := targetData.ElementOffset(typ, i) |
| 28 | + if offset%uint64(alignment) != 0 { |
| 29 | + panic("precise GC: global contains unaligned pointer: " + name) |
| 30 | + } |
| 31 | + subptrs.Lsh(subptrs, uint(offset)/uint(alignment)) |
| 32 | + ptrs.Or(ptrs, subptrs) |
| 33 | + } |
| 34 | + return ptrs |
| 35 | + case llvm.ArrayTypeKind: |
| 36 | + subtyp := typ.ElementType() |
| 37 | + subptrs := getPointerBitmap(targetData, subtyp, name) |
| 38 | + ptrs := big.NewInt(0) |
| 39 | + if subptrs.BitLen() == 0 { |
| 40 | + return ptrs |
| 41 | + } |
| 42 | + elementSize := targetData.TypeAllocSize(subtyp) |
| 43 | + for i := 0; i < typ.ArrayLength(); i++ { |
| 44 | + ptrs.Lsh(ptrs, uint(elementSize)/uint(alignment)) |
| 45 | + ptrs.Or(ptrs, subptrs) |
| 46 | + } |
| 47 | + return ptrs |
| 48 | + default: |
| 49 | + panic("unknown type kind: " + name) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// NewTyper creates a Typer. |
| 54 | +func NewTyper(ctx llvm.Context, mod llvm.Module, td llvm.TargetData) *Typer { |
| 55 | + ptr := llvm.PointerType(ctx.Int8Type(), 0) |
| 56 | + return &Typer{ |
| 57 | + tcache: make(map[llvm.Type]llvm.Value), |
| 58 | + td: td, |
| 59 | + ctx: ctx, |
| 60 | + mod: mod, |
| 61 | + uintptr: ctx.IntType(int(td.TypeSizeInBits(ptr))), |
| 62 | + i8: ctx.Int8Type(), |
| 63 | + ptrSize: td.TypeAllocSize(ptr), |
| 64 | + ptrAlign: uint64(td.ABITypeAlignment(ptr)), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Typer creates GC types. |
| 69 | +type Typer struct { |
| 70 | + // tcache is a cache of GC types by LLVM type. |
| 71 | + tcache map[llvm.Type]llvm.Value |
| 72 | + |
| 73 | + // td is the target platform data. |
| 74 | + td llvm.TargetData |
| 75 | + |
| 76 | + ctx llvm.Context |
| 77 | + |
| 78 | + mod llvm.Module |
| 79 | + |
| 80 | + uintptr, i8 llvm.Type |
| 81 | + |
| 82 | + ptrSize, ptrAlign uint64 |
| 83 | +} |
| 84 | + |
| 85 | +// Create a GC type for the given LLVM type. |
| 86 | +func (t *Typer) Create(typ llvm.Type) (llvm.Value, error) { |
| 87 | + // Check the cache before attempting to create the global. |
| 88 | + if g, ok := t.tcache[typ]; ok { |
| 89 | + return g, nil |
| 90 | + } |
| 91 | + |
| 92 | + // Find the type size. |
| 93 | + size := t.td.TypeAllocSize(typ) |
| 94 | + |
| 95 | + // Compute a pointer bitmap. |
| 96 | + // TODO: clean this up and maybe use error handling? |
| 97 | + b := getPointerBitmap(t.td, typ, "") |
| 98 | + if b.Cmp(big.NewInt(0)) == 0 { |
| 99 | + // The type has no pointers. |
| 100 | + return llvm.ConstNull(llvm.PointerType(t.uintptr, 0)), nil |
| 101 | + } |
| 102 | + |
| 103 | + // Use some limited sanity-checking. |
| 104 | + align := uint64(t.td.ABITypeAlignment(typ)) |
| 105 | + switch { |
| 106 | + case size < t.ptrSize: |
| 107 | + return llvm.Value{}, errors.New("type has pointers but is smaller than a pointer") |
| 108 | + case align%t.ptrAlign != 0: |
| 109 | + return llvm.Value{}, errors.New("alignment of pointery type is not a multiple of pointer alignment") |
| 110 | + case size%align != 0: |
| 111 | + return llvm.Value{}, errors.New("type violates the array alignment invariant") |
| 112 | + } |
| 113 | + |
| 114 | + // Convert size into increments of pointer-align. |
| 115 | + size /= t.ptrAlign |
| 116 | + |
| 117 | + // Create a global for the type. |
| 118 | + g := t.createGlobal(size, b.Bytes()) |
| 119 | + |
| 120 | + // Save the global to the cache. |
| 121 | + t.tcache[typ] = g |
| 122 | + |
| 123 | + return g, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (t *Typer) createGlobal(size uint64, layout []byte) llvm.Value { |
| 127 | + // TODO: compression? |
| 128 | + |
| 129 | + // Generate the name of the global. |
| 130 | + name := fmt.Sprintf("tinygo.gc.type.%d.%x", size, layout) |
| 131 | + |
| 132 | + // Create the global if it does not exist. |
| 133 | + g := t.mod.NamedGlobal(name) |
| 134 | + if g.IsNil() { |
| 135 | + // Convert the encoded layout to a byte array. |
| 136 | + bitmapValues := make([]llvm.Value, len(layout)) |
| 137 | + for i, b := range layout { |
| 138 | + bitmapValues[len(layout)-i-1] = llvm.ConstInt(t.i8, uint64(b), false) |
| 139 | + } |
| 140 | + bitmapArray := llvm.ConstArray(t.i8, bitmapValues) |
| 141 | + |
| 142 | + // Construct a tuple of the size + the array. |
| 143 | + tuple := t.ctx.ConstStruct([]llvm.Value{ |
| 144 | + llvm.ConstInt(t.uintptr, size, false), |
| 145 | + bitmapArray, |
| 146 | + }, false) |
| 147 | + |
| 148 | + // Create a global constant initialized with the tuple. |
| 149 | + g = llvm.AddGlobal(t.mod, tuple.Type(), name) |
| 150 | + g.SetInitializer(tuple) |
| 151 | + g.SetGlobalConstant(true) |
| 152 | + g.SetUnnamedAddr(true) |
| 153 | + g.SetLinkage(llvm.InternalLinkage) |
| 154 | + } |
| 155 | + |
| 156 | + // Get a pointer to the size component of the global. |
| 157 | + // This is used because different globals will end up with different sizes. |
| 158 | + g = llvm.ConstBitCast(g, llvm.PointerType(t.uintptr, 0)) |
| 159 | + |
| 160 | + return g |
| 161 | +} |
0 commit comments