forked from go-llvm/llgo
/
llvmtypes.go
492 lines (429 loc) · 15.2 KB
/
llvmtypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
Copyright (c) 2011, 2012 Andrew Wilkins <axwalk@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package llgo
import (
"fmt"
"github.com/axw/gollvm/llvm"
"github.com/axw/llgo/types"
"go/ast"
"reflect"
)
type TypeMap struct {
module llvm.Module
target llvm.TargetData
types map[types.Type]llvm.Type // compile-time LLVM type
runtime map[types.Type]llvm.Value // runtime/reflect type representation
expr map[ast.Expr]types.Type // expression types
runtimeType,
runtimeCommonType,
runtimeUncommonType,
runtimeArrayType,
runtimeChanType,
runtimeFuncType,
runtimeInterfaceType,
runtimeMapType,
runtimePtrType,
runtimeSliceType,
runtimeStructType llvm.Type
hashAlgFunctionType,
equalAlgFunctionType,
printAlgFunctionType,
copyAlgFunctionType llvm.Type
}
func NewTypeMap(module llvm.Module, target llvm.TargetData, exprTypes map[ast.Expr]types.Type) *TypeMap {
tm := &TypeMap{module: module, target: target, expr: exprTypes}
tm.types = make(map[types.Type]llvm.Type)
tm.runtime = make(map[types.Type]llvm.Value)
// Load "reflect.go", and generate LLVM types for the runtime type
// structures.
pkg, err := parseReflect()
if err != nil {
panic(err) // FIXME return err
}
objToLLVMType := func(name string) llvm.Type {
obj := pkg.Scope.Lookup(name)
return tm.ToLLVM(obj.Type.(types.Type))
}
tm.runtimeType = objToLLVMType("runtimeType")
tm.runtimeCommonType = objToLLVMType("commonType")
tm.runtimeUncommonType = objToLLVMType("uncommonType")
tm.runtimeArrayType = objToLLVMType("arrayType")
tm.runtimeChanType = objToLLVMType("chanType")
tm.runtimeFuncType = objToLLVMType("funcType")
tm.runtimeInterfaceType = objToLLVMType("interfaceType")
tm.runtimeMapType = objToLLVMType("mapType")
tm.runtimePtrType = objToLLVMType("ptrType")
tm.runtimeSliceType = objToLLVMType("sliceType")
tm.runtimeStructType = objToLLVMType("structType")
// Types for algorithms. See 'runtime/runtime.h'.
uintptrType := tm.target.IntPtrType()
voidPtrType := llvm.PointerType(llvm.Int8Type(), 0)
boolType := llvm.Int1Type()
// Create runtime algorithm function types.
params := []llvm.Type{uintptrType, voidPtrType}
tm.hashAlgFunctionType = llvm.FunctionType(uintptrType, params, false)
params = []llvm.Type{uintptrType, voidPtrType, voidPtrType}
tm.equalAlgFunctionType = llvm.FunctionType(boolType, params, false)
params = []llvm.Type{uintptrType, voidPtrType}
tm.printAlgFunctionType = llvm.FunctionType(llvm.VoidType(), params, false)
params = []llvm.Type{uintptrType, voidPtrType, voidPtrType}
tm.copyAlgFunctionType = llvm.FunctionType(llvm.VoidType(), params, false)
return tm
}
func (tm *TypeMap) ToLLVM(t types.Type) llvm.Type {
t = types.Underlying(t)
lt, ok := tm.types[t]
if !ok {
lt = tm.makeLLVMType(t)
if lt.IsNil() {
panic(fmt.Sprint("Failed to create LLVM type for: ", t))
}
tm.types[t] = lt
}
return lt
}
func (tm *TypeMap) ToRuntime(t types.Type) llvm.Value {
t = types.Underlying(t)
r, ok := tm.runtime[t]
if !ok {
_, r = tm.makeRuntimeType(t)
if r.IsNil() {
panic(fmt.Sprint("Failed to create runtime type for: ", t))
}
tm.runtime[t] = r
}
return r
}
func (tm *TypeMap) makeLLVMType(t types.Type) llvm.Type {
switch t := t.(type) {
case *types.Bad:
return tm.badLLVMType(t)
case *types.Basic:
return tm.basicLLVMType(t)
case *types.Array:
return tm.arrayLLVMType(t)
case *types.Slice:
return tm.sliceLLVMType(t)
case *types.Struct:
return tm.structLLVMType(t)
case *types.Pointer:
return tm.pointerLLVMType(t)
case *types.Func:
return tm.funcLLVMType(t)
case *types.Interface:
return tm.interfaceLLVMType(t)
case *types.Map:
return tm.mapLLVMType(t)
case *types.Chan:
return tm.chanLLVMType(t)
case *types.Name:
return tm.nameLLVMType(t)
}
panic("unreachable")
}
func (tm *TypeMap) badLLVMType(b *types.Bad) llvm.Type {
return llvm.Type{nil}
}
func (tm *TypeMap) basicLLVMType(b *types.Basic) llvm.Type {
switch b.Kind {
case types.BoolKind:
return llvm.Int1Type()
case types.Int8Kind, types.Uint8Kind:
return llvm.Int8Type()
case types.Int16Kind, types.Uint16Kind:
return llvm.Int16Type()
case types.Int32Kind, types.Uint32Kind:
return llvm.Int32Type()
case types.Int64Kind, types.Uint64Kind:
return llvm.Int64Type()
case types.Float32Kind:
return llvm.FloatType()
case types.Float64Kind:
return llvm.DoubleType()
case types.UnsafePointerKind, types.UintptrKind,
types.UintKind, types.IntKind:
return tm.target.IntPtrType()
//case Complex64: TODO
//case Complex128:
//case UntypedInt:
//case UntypedFloat:
//case UntypedComplex:
case types.StringKind:
i8ptr := llvm.PointerType(llvm.Int8Type(), 0)
elements := []llvm.Type{i8ptr, llvm.Int32Type()}
return llvm.StructType(elements, false)
}
panic(fmt.Sprint("unhandled kind: ", b.Kind))
}
func (tm *TypeMap) arrayLLVMType(a *types.Array) llvm.Type {
return llvm.ArrayType(tm.ToLLVM(a.Elt), int(a.Len))
}
func (tm *TypeMap) sliceLLVMType(s *types.Slice) llvm.Type {
elements := []llvm.Type{
llvm.PointerType(tm.ToLLVM(s.Elt), 0),
llvm.Int32Type(),
llvm.Int32Type(),
}
return llvm.StructType(elements, false)
}
func (tm *TypeMap) structLLVMType(s *types.Struct) llvm.Type {
// Types may be circular, so we need to first create an empty
// struct type, then fill in its body after visiting its
// members.
typ, ok := tm.types[s]
if !ok {
typ = llvm.GlobalContext().StructCreateNamed("")
tm.types[s] = typ
elements := make([]llvm.Type, len(s.Fields))
for i, f := range s.Fields {
ft := f.Type.(types.Type)
elements[i] = tm.ToLLVM(ft)
}
typ.StructSetBody(elements, false)
}
return typ
}
func (tm *TypeMap) pointerLLVMType(p *types.Pointer) llvm.Type {
return llvm.PointerType(tm.ToLLVM(p.Base), 0)
}
func (tm *TypeMap) funcLLVMType(f *types.Func) llvm.Type {
param_types := make([]llvm.Type, 0)
// Add receiver parameter.
if f.Recv != nil {
recv_type := f.Recv.Type.(types.Type)
param_types = append(param_types, tm.ToLLVM(recv_type))
}
for _, param := range f.Params {
param_type := param.Type.(types.Type)
param_types = append(param_types, tm.ToLLVM(param_type))
}
var return_type llvm.Type
switch len(f.Results) {
case 0:
return_type = llvm.VoidType()
case 1:
return_type = tm.ToLLVM(f.Results[0].Type.(types.Type))
default:
elements := make([]llvm.Type, len(f.Results))
for i, result := range f.Results {
elements[i] = tm.ToLLVM(result.Type.(types.Type))
}
return_type = llvm.StructType(elements, false)
}
fn_type := llvm.FunctionType(return_type, param_types, false)
return llvm.PointerType(fn_type, 0)
}
func (tm *TypeMap) interfaceLLVMType(i *types.Interface) llvm.Type {
valptr_type := llvm.PointerType(llvm.Int8Type(), 0)
typptr_type := valptr_type // runtimeCommonType may not be defined yet
elements := make([]llvm.Type, 2+len(i.Methods))
elements[0] = valptr_type // value
elements[1] = typptr_type // type
for n, m := range i.Methods {
// Add an opaque pointer parameter to the function for the
// struct pointer.
fntype := m.Type.(*types.Func)
receiver_type := &types.Pointer{Base: types.Int8}
fntype.Recv = ast.NewObj(ast.Var, "")
fntype.Recv.Type = receiver_type
elements[n+2] = tm.ToLLVM(fntype)
}
return llvm.StructType(elements, false)
}
func (tm *TypeMap) mapLLVMType(m *types.Map) llvm.Type {
// XXX This map type will change in the future, when I get around to it.
// At the moment, it's representing a really dumb singly linked list.
list_type := llvm.GlobalContext().StructCreateNamed("")
list_ptr_type := llvm.PointerType(list_type, 0)
size_type := llvm.Int32Type()
element_types := []llvm.Type{size_type, list_type}
typ := llvm.StructType(element_types, false)
tm.types[m] = typ
list_element_types := []llvm.Type{
list_ptr_type, tm.ToLLVM(m.Key), tm.ToLLVM(m.Elt)}
list_type.StructSetBody(list_element_types, false)
return typ
}
func (tm *TypeMap) chanLLVMType(c *types.Chan) llvm.Type {
panic("unimplemented")
}
func (tm *TypeMap) nameLLVMType(n *types.Name) llvm.Type {
return tm.ToLLVM(n.Underlying)
}
///////////////////////////////////////////////////////////////////////////////
func (tm *TypeMap) makeRuntimeType(t types.Type) (global, ptr llvm.Value) {
switch t := t.(type) {
case *types.Bad:
return tm.badRuntimeType(t)
case *types.Basic:
return tm.basicRuntimeType(t)
case *types.Array:
return tm.arrayRuntimeType(t)
case *types.Slice:
return tm.sliceRuntimeType(t)
case *types.Struct:
return tm.structRuntimeType(t)
case *types.Pointer:
return tm.pointerRuntimeType(t)
case *types.Func:
return tm.funcRuntimeType(t)
case *types.Interface:
return tm.interfaceRuntimeType(t)
case *types.Map:
return tm.mapRuntimeType(t)
case *types.Chan:
return tm.chanRuntimeType(t)
case *types.Name:
return tm.nameRuntimeType(t)
}
panic("unreachable")
}
func (tm *TypeMap) makeAlgorithmTable(t types.Type) llvm.Value {
// TODO set these to actual functions.
hashAlg := llvm.ConstNull(llvm.PointerType(tm.hashAlgFunctionType, 0))
printAlg := llvm.ConstNull(llvm.PointerType(tm.printAlgFunctionType, 0))
copyAlg := llvm.ConstNull(llvm.PointerType(tm.copyAlgFunctionType, 0))
equalAlgName := "runtime.memequal"
equalAlg := tm.module.NamedFunction(equalAlgName)
if equalAlg.IsNil() {
equalAlg = llvm.AddFunction(
tm.module, equalAlgName, tm.equalAlgFunctionType)
}
elems := []llvm.Value{hashAlg, equalAlg, printAlg, copyAlg}
return llvm.ConstStruct(elems, false)
}
func (tm *TypeMap) makeRuntimeTypeGlobal(v llvm.Value) (global, ptr llvm.Value) {
runtimeTypeValue := llvm.ConstNull(tm.runtimeType)
initType := llvm.StructType([]llvm.Type{tm.runtimeType, v.Type()}, false)
global = llvm.AddGlobal(tm.module, initType, "")
ptr = llvm.ConstBitCast(global, llvm.PointerType(tm.runtimeType, 0))
// Set ptrToThis in v's commonType.
if v.Type() == tm.runtimeCommonType {
v = llvm.ConstInsertValue(v, ptr, []uint32{9})
} else {
commonType := llvm.ConstExtractValue(v, []uint32{0})
commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{9})
v = llvm.ConstInsertValue(v, commonType, []uint32{0})
}
init := llvm.Undef(initType)
//runtimeTypeValue = llvm.ConstInsertValue() TODO
init = llvm.ConstInsertValue(init, runtimeTypeValue, []uint32{0})
init = llvm.ConstInsertValue(init, v, []uint32{1})
global.SetInitializer(init)
return
}
func (tm *TypeMap) makeCommonType(t types.Type, k reflect.Kind) llvm.Value {
// Not sure if there's an easier way to do this, but if you just
// use ConstStruct, you end up getting a different llvm.Type.
lt := tm.ToLLVM(t)
typ := llvm.ConstNull(tm.runtimeCommonType)
elementTypes := tm.runtimeCommonType.StructElementTypes()
// Size.
size := llvm.SizeOf(lt)
if size.Type().IntTypeWidth() > elementTypes[0].IntTypeWidth() {
size = llvm.ConstTrunc(size, elementTypes[0])
}
typ = llvm.ConstInsertValue(typ, size, []uint32{0})
// TODO hash
// TODO padding
// Alignment.
align := llvm.ConstTrunc(llvm.AlignOf(lt), llvm.Int8Type())
typ = llvm.ConstInsertValue(typ, align, []uint32{3}) // var
typ = llvm.ConstInsertValue(typ, align, []uint32{4}) // field
// Kind.
kind := llvm.ConstInt(llvm.Int8Type(), uint64(k), false)
typ = llvm.ConstInsertValue(typ, kind, []uint32{5})
// Algorithm table.
alg := tm.makeAlgorithmTable(t)
algptr := llvm.AddGlobal(tm.module, alg.Type(), "")
algptr.SetInitializer(alg)
algptr = llvm.ConstBitCast(algptr, elementTypes[6])
typ = llvm.ConstInsertValue(typ, algptr, []uint32{6})
// TODO string
return typ
}
func (tm *TypeMap) badRuntimeType(b *types.Bad) (global, ptr llvm.Value) {
panic("bad type")
}
func (tm *TypeMap) basicRuntimeType(b *types.Basic) (global, ptr llvm.Value) {
commonType := tm.makeCommonType(b, reflect.Kind(b.Kind))
return tm.makeRuntimeTypeGlobal(commonType)
}
func (tm *TypeMap) arrayRuntimeType(a *types.Array) (global, ptr llvm.Value) {
panic("unimplemented")
}
func (tm *TypeMap) sliceRuntimeType(s *types.Slice) (global, ptr llvm.Value) {
commonType := tm.makeCommonType(s, reflect.Slice)
elemRuntimeType := tm.ToRuntime(s.Elt)
sliceType := llvm.ConstNull(tm.runtimeSliceType)
sliceType = llvm.ConstInsertValue(sliceType, commonType, []uint32{0})
sliceType = llvm.ConstInsertValue(sliceType, elemRuntimeType, []uint32{1})
return tm.makeRuntimeTypeGlobal(sliceType)
}
func (tm *TypeMap) structRuntimeType(s *types.Struct) (global, ptr llvm.Value) {
commonType := tm.makeCommonType(s, reflect.Struct)
structType := llvm.ConstNull(tm.runtimeStructType)
structType = llvm.ConstInsertValue(structType, commonType, []uint32{0})
// TODO set fields
return tm.makeRuntimeTypeGlobal(structType)
}
func (tm *TypeMap) pointerRuntimeType(p *types.Pointer) (global, ptr llvm.Value) {
panic("unimplemented")
}
func (tm *TypeMap) funcRuntimeType(f *types.Func) (global, ptr llvm.Value) {
panic("unimplemented")
}
func (tm *TypeMap) interfaceRuntimeType(i *types.Interface) (global, ptr llvm.Value) {
panic("unimplemented")
}
func (tm *TypeMap) mapRuntimeType(m *types.Map) (global, ptr llvm.Value) {
commonType := tm.makeCommonType(m, reflect.Map)
mapType := llvm.ConstNull(tm.runtimeMapType)
mapType = llvm.ConstInsertValue(mapType, commonType, []uint32{0})
// TODO set key, elem
return tm.makeRuntimeTypeGlobal(mapType)
}
func (tm *TypeMap) chanRuntimeType(c *types.Chan) (global, ptr llvm.Value) {
panic("unimplemented")
}
func (tm *TypeMap) nameRuntimeType(n *types.Name) (global, ptr llvm.Value) {
global, ptr = tm.makeRuntimeType(n.Underlying)
globalInit := global.Initializer()
// Locate the common type.
underlyingRuntimeType := llvm.ConstExtractValue(globalInit, []uint32{1})
commonType := underlyingRuntimeType
if _, ok := n.Underlying.(*types.Basic); !ok {
commonType = llvm.ConstExtractValue(commonType, []uint32{0})
}
// Insert the uncommon type.
uncommonTypeInit := llvm.ConstNull(tm.runtimeUncommonType)
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
commonType = llvm.ConstInsertValue(commonType, uncommonType, []uint32{8})
// Update the global's initialiser.
if _, ok := n.Underlying.(*types.Basic); !ok {
underlyingRuntimeType = llvm.ConstInsertValue(underlyingRuntimeType, commonType, []uint32{0})
} else {
underlyingRuntimeType = commonType
}
globalInit = llvm.ConstInsertValue(globalInit, underlyingRuntimeType, []uint32{1})
global.SetName("__llgo.reflect." + n.Obj.Name)
return global, ptr
}
// vim: set ft=go :