-
Notifications
You must be signed in to change notification settings - Fork 327
/
module.go
504 lines (450 loc) · 13.8 KB
/
module.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
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codegen
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/google/gapid/core/os/device"
"llvm/bindings/go/llvm"
)
// Module is a JIT module.
type Module struct {
Types Types
llvm llvm.Module
ctx llvm.Context
target *device.ABI
triple Triple
name string
funcs map[string]*Function
strings map[string]llvm.Value
memcpy *Function
memset *Function
exceptions exceptions
dbg *dbg
}
// NewModule returns a new module with the specified name.
func NewModule(name string, target *device.ABI) *Module {
if target == nil {
panic("NewModule must be passed a non-nil target")
}
layout := target.MemoryLayout
intSize := 8 * int(layout.Integer.Size)
ptrSize := 8 * int(layout.Pointer.Size)
sizeSize := 8 * int(layout.Size.Size)
triple := TargetTriple(target)
ctx := llvm.NewContext()
module := ctx.NewModule(name)
module.SetTarget(triple.String())
if dl := DataLayout(target); dl != "" {
module.SetDataLayout(dl)
}
bt := func(name string, dtl *device.DataTypeLayout, llvm llvm.Type) basicType {
return basicType{name, 8 * int(dtl.Size), 8 * int(dtl.Alignment), llvm}
}
m := &Module{
Types: Types{
Void: basicType{"void", 0, 0, ctx.VoidType()},
Bool: Integer{false, basicType{"bool", 1, 8, ctx.Int1Type()}},
Int: Integer{true, bt("int", layout.Integer, ctx.IntType(intSize))},
Int8: Integer{true, bt("int8", layout.I8, ctx.Int8Type())},
Int16: Integer{true, bt("int16", layout.I16, ctx.Int16Type())},
Int32: Integer{true, bt("int32", layout.I32, ctx.Int32Type())},
Int64: Integer{true, bt("int64", layout.I64, ctx.Int64Type())},
Uint: Integer{false, bt("uint", layout.Integer, ctx.IntType(intSize))},
Uint8: Integer{false, bt("uint8", layout.I8, ctx.Int8Type())},
Uint16: Integer{false, bt("uint16", layout.I16, ctx.Int16Type())},
Uint32: Integer{false, bt("uint32", layout.I32, ctx.Int32Type())},
Uint64: Integer{false, bt("uint64", layout.I64, ctx.Int64Type())},
Uintptr: Integer{false, bt("uintptr", layout.Pointer, ctx.IntType(ptrSize))},
Size: Integer{false, bt("size", layout.Size, ctx.IntType(sizeSize))},
Float32: Float{bt("float32", layout.F32, ctx.FloatType())},
Float64: Float{bt("float64", layout.F64, ctx.DoubleType())},
pointers: map[Type]Pointer{},
arrays: map[typeInt]*Array{},
structs: map[string]*Struct{},
funcs: map[string]*FunctionType{},
enums: map[string]Enum{},
aliases: map[string]Alias{},
named: map[string]Type{},
},
llvm: module,
ctx: ctx,
target: target,
triple: triple,
name: name,
funcs: map[string]*Function{},
strings: map[string]llvm.Value{},
}
m.Types.emptyStructField = Field{"empty-struct", m.Types.Int8}
m.Types.m = m
voidPtr := m.Types.Pointer(m.Types.Void)
// void llvm.memcpy.p0i8.p0i8.i32(i8 * <dest>, i8 * <src>, i32 <len>, i32 <align>, i1 <isvolatile>)
m.memcpy = m.Function(m.Types.Void, "llvm.memcpy.p0i8.p0i8.i32",
voidPtr, voidPtr, m.Types.Uint32, m.Types.Bool)
// void @llvm.memset.p0i8.i32(i8* <dest>, i8 <val>, i32 <len>, i1 <isvolatile>)
m.memset = m.Function(m.Types.Void, "llvm.memset.p0i8.i32",
voidPtr, m.Types.Uint8, m.Types.Uint32, m.Types.Bool)
m.exceptions.init(m)
return m
}
// EmitDebug enables debug info generation for the module.
func (m *Module) EmitDebug() {
if m.dbg == nil {
m.dbg = &dbg{
files: map[string]file{},
tys: map[Type]llvm.Metadata{},
m: m,
}
}
}
// Verify checks correctness of the module.
func (m *Module) Verify() error {
m.dbg.finalize()
for f := m.llvm.FirstFunction(); !f.IsNil(); f = llvm.NextFunction(f) {
if err := llvm.VerifyFunction(f, llvm.ReturnStatusAction); err != nil {
f.Dump()
modErr := slashHexToRune(llvm.VerifyModule(m.llvm, llvm.ReturnStatusAction).Error())
return fmt.Errorf("Function '%s' verification failed:\n%v\n%v", f.Name(), err, modErr)
}
}
if err := llvm.VerifyModule(m.llvm, llvm.ReturnStatusAction); err != nil {
return fmt.Errorf("Module verification failed:\n%v\n%v", err, m.String())
}
return nil
}
func hex(r rune) byte {
switch {
case r >= '0' && r <= '9':
return byte(r - '0')
case r >= 'A' && r <= 'Z':
return byte(r-'A') + 10
case r >= 'a' && r <= 'z':
return byte(r-'a') + 10
default:
return 0
}
}
func slashHexToRune(ir string) string {
runes := ([]rune)(ir)
out := bytes.Buffer{}
for {
for len(runes) >= 3 && runes[0] == '\\' {
out.WriteByte(hex(runes[1])<<4 | hex(runes[2]))
runes = runes[3:]
}
if len(runes) == 0 {
break
}
out.WriteRune(runes[0])
runes = runes[1:]
}
return out.String()
}
func (m *Module) String() string {
return slashHexToRune(m.llvm.String())
}
var parseFuncRE = regexp.MustCompile(`(\w+\s*\**)\s*(\w+)\((.*)\)`)
// ParseFunctionSignature returns a function parsed from a C-style signature.
// Example:
// "void* Foo(uint8_t i, bool b)"
func (m *Module) ParseFunctionSignature(sig string) *Function {
parts := parseFuncRE.FindStringSubmatch(sig)
if len(parts) != 4 {
fail("'%v' is not a valid function signature", sig)
}
ret := m.parseType(parts[1])
name := parts[2]
args := m.parseTypes(strings.Split(parts[3], ","))
return m.Function(ret, name, args...)
}
func (m *Module) parseTypes(l []string) []Type {
out := make([]Type, len(l))
for i, s := range l {
out[i] = m.parseType(s)
}
return out
}
var parseTypeRE = regexp.MustCompile(`^\s*(\w+|\.\.\.)\s*([\*\s]*)`)
func (m *Module) parseType(s string) Type {
parts := parseTypeRE.FindStringSubmatch(s)
if len(parts) != 3 {
fail("'%v' is not a valid type", s)
}
name := parts[1]
ptrs := parts[2]
ty := m.parseTypeName(name)
for _, r := range ptrs {
if r == '*' {
ty = m.Types.Pointer(ty)
}
}
return ty
}
func (m *Module) parseTypeName(name string) Type {
switch name {
case "void":
return m.Types.Void
case "bool":
return m.Types.Bool
case "char":
return m.Types.Uint8
case "int_t":
return m.Types.Int
case "int8_t":
return m.Types.Int8
case "int16_t":
return m.Types.Int16
case "int32_t":
return m.Types.Int32
case "int64_t":
return m.Types.Int64
case "uint_t":
return m.Types.Uint
case "uint8_t":
return m.Types.Uint8
case "uint16_t":
return m.Types.Uint16
case "uint32_t":
return m.Types.Uint32
case "uint64_t":
return m.Types.Uint64
case "float":
return m.Types.Float32
case "double":
return m.Types.Float64
case "uintptr_t":
return m.Types.Uintptr
case "...":
return Variadic
default:
if ty, ok := m.Types.named[name]; ok {
return ty
}
fail("'%v' is not a valid type name", name)
return nil
}
}
// Function creates a new function with the given name, result type and parameters.
func (m *Module) Function(resTy Type, name string, paramTys ...Type) *Function {
ty := m.Types.Function(resTy, paramTys...)
f := llvm.AddFunction(m.llvm, name, ty.llvm)
out := &Function{Name: name, Type: ty, llvm: f, m: m}
if name != "" {
if _, existing := m.funcs[name]; existing {
fail("Duplicate function with name: '%s'", name)
}
m.funcs[name] = out
}
return out
}
// Ctor creates and builds a new module constructor.
func (m *Module) Ctor(priority int32, cb func(*Builder)) {
ctor := m.Function(m.Types.Void, "ctor")
ctor.Build(cb)
// TODO: Glob together multiple calls into a single table.
entryTy := m.ctx.StructType([]llvm.Type{
m.Types.Int32.llvmTy(),
llvm.PointerType(llvm.FunctionType(m.Types.Void.llvmTy(), []llvm.Type{}, false), 0),
}, false)
entries := []llvm.Value{
llvm.ConstStruct([]llvm.Value{
m.Scalar(priority).llvm,
ctor.llvm,
}, false),
}
table := llvm.ConstArray(entryTy, entries)
g := llvm.AddGlobal(m.llvm, table.Type(), "llvm.global_ctors")
g.SetInitializer(table)
g.SetLinkage(llvm.AppendingLinkage)
}
// Global is a global value.
type Global struct {
Type Type
llvm llvm.Value
}
// Value returns a Value for the global.
func (g Global) Value(b *Builder) *Value {
return b.val(g.Type, g.llvm)
}
// LinkPrivate makes this global use private linkage.
func (g Global) LinkPrivate() Global {
g.llvm.SetLinkage(llvm.PrivateLinkage)
return g
}
// LinkPublic makes this global use public linkage.
func (g Global) LinkPublic() Global {
g.llvm.SetLinkage(llvm.ExternalLinkage)
return g
}
// SetConstant makes this global constant.
func (g Global) SetConstant(constant bool) Global {
g.llvm.SetGlobalConstant(constant)
return g
}
// ZeroGlobal returns a zero-initialized new global variable with the specified
// name and type.
func (m *Module) ZeroGlobal(name string, ty Type) Global {
v := llvm.AddGlobal(m.llvm, ty.llvmTy(), name)
v.SetInitializer(llvm.ConstNull(ty.llvmTy()))
v.SetLinkage(llvm.PrivateLinkage)
return Global{m.Types.Pointer(ty), v}
}
// Global returns a new global variable intiailized with the specified constant
// value.
func (m *Module) Global(name string, val Const) Global {
v := llvm.AddGlobal(m.llvm, val.Type.llvmTy(), name)
v.SetInitializer(val.llvm)
v.SetLinkage(llvm.PrivateLinkage)
return Global{m.Types.Pointer(val.Type), v}
}
// Extern returns a global variable declared externally with the given name and
// type.
func (m *Module) Extern(name string, ty Type) Global {
v := llvm.AddGlobal(m.llvm, ty.llvmTy(), name)
return Global{m.Types.Pointer(ty), v}
}
// Const is an immutable value.
type Const struct {
Type Type
llvm llvm.Value
}
// Value returns a Value for the constant.
func (c Const) Value(b *Builder) *Value {
return b.val(c.Type, c.llvm)
}
// Scalar returns an inferred type constant scalar with the value v.
func (m *Module) Scalar(v interface{}) Const {
ty := m.Types.TypeOf(v)
return m.ScalarOfType(v, ty)
}
// Array returns an constant array with the value v and element type ty.
func (m *Module) Array(v interface{}, elTy Type) Const {
ty := m.Types.Array(elTy, reflect.ValueOf(v).Len())
return m.ScalarOfType(v, ty)
}
// ScalarOfType returns a constant scalar with the value v with the given type.
func (m *Module) ScalarOfType(v interface{}, ty Type) Const {
if ty == nil {
fail("ScalarOfType passed nil type")
}
var val llvm.Value
switch v := v.(type) {
case *Value:
val = v.llvm
case Const:
val = v.llvm
case Global:
val = v.llvm
case *Function:
if v != nil {
ty, val = m.Types.Pointer(v.Type), v.llvm
} else {
ty = m.Types.Pointer(m.Types.Void)
val = llvm.ConstNull(ty.llvmTy())
}
default:
switch reflect.TypeOf(v).Kind() {
case reflect.Array, reflect.Slice:
arrTy, ok := ty.(*Array)
if !ok {
fail("Slice must have an array type. Got %v", ty)
}
rv := reflect.ValueOf(v)
vals := make([]llvm.Value, rv.Len())
for i := range vals {
vals[i] = m.ScalarOfType(rv.Index(i).Interface(), arrTy.Element).llvm
}
val = llvm.ConstArray(arrTy.Element.llvmTy(), vals)
case reflect.Bool:
if reflect.ValueOf(v).Bool() {
val = llvm.ConstInt(ty.llvmTy(), 1, false)
} else {
val = llvm.ConstInt(ty.llvmTy(), 0, false)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val = llvm.ConstInt(ty.llvmTy(), uint64(reflect.ValueOf(v).Int()), false)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val = llvm.ConstInt(ty.llvmTy(), reflect.ValueOf(v).Uint(), false)
case reflect.Float32, reflect.Float64:
val = llvm.ConstFloat(ty.llvmTy(), float64(reflect.ValueOf(v).Float()))
case reflect.Struct:
strTy, ok := ty.(*Struct)
if !ok {
fail("Struct must have an struct type. Got %v", ty)
}
r := reflect.ValueOf(v)
fields := make([]llvm.Value, r.NumField())
for i := range fields {
fields[i] = m.Scalar(r.Field(i).Interface()).llvm
}
val = llvm.ConstNamedStruct(strTy.llvm, fields)
case reflect.String:
s := v.(string)
var ok bool
val, ok = m.strings[s]
if !ok {
arr := llvm.ConstString(s, true)
buf := llvm.AddGlobal(m.llvm, arr.Type(), "str")
buf.SetInitializer(arr)
buf.SetLinkage(llvm.PrivateLinkage)
ptrTy := m.Types.Pointer(m.Types.Uint8)
val = llvm.ConstPointerCast(buf, ptrTy.llvmTy())
m.strings[s] = val
}
default:
fail("Scalar does not support type %T", v)
}
}
if val.Type().C != ty.llvmTy().C {
fail("Value type mismatch for %T: value: %v, type: %v", v, val.Type(), ty.llvmTy())
}
val.SetName(fmt.Sprintf("%v", v))
return Const{ty, val}
}
// ConstStruct returns a constant struct with the value v.
func (m *Module) ConstStruct(ty *Struct, fields map[string]interface{}) Const {
vals := make([]llvm.Value, len(ty.Fields()))
for i, f := range ty.Fields() {
if v := fields[f.Name]; v == nil {
vals[i] = llvm.ConstNull(f.Type.llvmTy())
} else {
vals[i] = m.ScalarOfType(v, f.Type).llvm
}
}
val := llvm.ConstNamedStruct(ty.llvm, vals)
return Const{ty, val}
}
// Zero returns a constant zero value of type ty.
func (m *Module) Zero(ty Type) Const {
return Const{ty, llvm.ConstNull(ty.llvmTy())}
}
// SizeOf returns the size of the type in bytes as a uint64.
func (m *Module) SizeOf(ty Type) Const {
return m.Scalar(uint64((ty.SizeInBits() + 7) / 8))
}
// AlignOf returns the alignment of the type in bytes.
func (m *Module) AlignOf(ty Type) Const {
return m.Scalar(uint64((ty.AlignInBits() + 7) / 8))
}
// OffsetOf returns the field offset in bytes.
func (m *Module) OffsetOf(ty *Struct, name string) Const {
idx := ty.FieldIndex(name)
if idx == -1 {
fail("'%v' is not a field of %v", name, ty)
}
return m.Scalar(uint64((ty.FieldOffsetInBits(idx) + 7) / 8))
}