forked from go-llvm/llgo
/
decl.go
451 lines (417 loc) · 12.9 KB
/
decl.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
// Copyright 2011 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package llgo
import (
"code.google.com/p/go.tools/go/types"
"fmt"
"github.com/axw/gollvm/llvm"
"go/ast"
"go/scanner"
"go/token"
"reflect"
)
func (c *compiler) makeFunc(ident *ast.Ident, ftyp *types.Signature) *LLVMValue {
fname := ident.String()
if ftyp.Recv() == nil && fname == "init" {
// Make "init" functions anonymous.
fname = ""
} else {
var pkgname string
if recv := ftyp.Recv(); recv != nil {
var recvname string
switch recvtyp := recv.Type().(type) {
case *types.Pointer:
if named, ok := recvtyp.Elem().(*types.Named); ok {
obj := named.Obj()
recvname = "*" + obj.Name()
pkgname = pkgpath(c.objectdata[obj].Package)
}
case *types.Named:
named := recvtyp
obj := named.Obj()
recvname = obj.Name()
pkgname = pkgpath(c.objectdata[obj].Package)
}
if recvname != "" {
fname = fmt.Sprintf("%s.%s", recvname, fname)
} else {
// If the receiver is an unnamed struct, we're
// synthesising a method for an unnamed struct
// type. There's no meaningful name to give the
// function, so leave it up to LLVM.
fname = ""
}
} else {
obj := c.objects[ident]
pkgname = pkgpath(c.objectdata[obj].Package)
}
if fname != "" {
fname = pkgname + "." + fname
}
}
// gcimporter may produce multiple AST objects for the same function.
llvmftyp := c.types.ToLLVM(ftyp)
var fn llvm.Value
if fname != "" {
fn = c.module.Module.NamedFunction(fname)
}
if fn.IsNil() {
llvmfptrtyp := llvmftyp.StructElementTypes()[0].ElementType()
fn = llvm.AddFunction(c.module.Module, fname, llvmfptrtyp)
}
fn = llvm.ConstInsertValue(llvm.ConstNull(llvmftyp), fn, []uint32{0})
return c.NewValue(fn, ftyp)
}
// promoteStackVar takes a stack variable Value, and promotes it to the heap,
// replacing all uses of the stack-allocated value in the process.
func (stackvar *LLVMValue) promoteStackVar() {
c := stackvar.compiler
stackptrval := stackvar.pointer.value
currblock := c.builder.GetInsertBlock()
defer c.builder.SetInsertPointAtEnd(currblock)
c.builder.SetInsertPointBefore(stackptrval)
typ := stackptrval.Type().ElementType()
heapptrval := c.createTypeMalloc(typ)
heapptrval.SetName(stackptrval.Name())
stackptrval.ReplaceAllUsesWith(heapptrval)
stackvar.pointer.value = heapptrval
stackvar.stack = nil
}
// buildFunction takes a function Value, a list of parameters, and a body,
// and generates code for the function.
func (c *compiler) buildFunction(f *LLVMValue, context, params, results *types.Tuple, body *ast.BlockStmt, isvariadic bool) {
if currblock := c.builder.GetInsertBlock(); !currblock.IsNil() {
defer c.builder.SetInsertPointAtEnd(currblock)
}
llvm_fn := llvm.ConstExtractValue(f.LLVMValue(), []uint32{0})
entry := llvm.AddBasicBlock(llvm_fn, "entry")
c.builder.SetInsertPointAtEnd(entry)
// For closures, context is the captured context values.
var paramoffset int
if context != nil {
paramoffset++
// Store the existing values. We're going to temporarily
// replace the values with offsets into the context param.
oldvalues := make([]*LLVMValue, context.Len())
for i := range oldvalues {
v := context.At(i)
oldvalues[i] = c.objectdata[v].Value
}
defer func() {
for i := range oldvalues {
v := context.At(i)
c.objectdata[v].Value = oldvalues[i]
}
}()
// The context parameter is a pointer to a struct
// whose elements are pointers to captured values.
arg0 := llvm_fn.Param(0)
for i := range oldvalues {
v := context.At(i)
argptr := c.builder.CreateStructGEP(arg0, i, "")
argptr = c.builder.CreateLoad(argptr, "")
ptrtyp := oldvalues[i].pointer.Type()
newvalue := c.NewValue(argptr, ptrtyp)
c.objectdata[v].Value = newvalue.makePointee()
}
}
// Bind receiver, arguments and return values to their
// identifiers/objects. We'll store each parameter on the stack so
// they're addressable.
nparams := int(params.Len())
for i := 0; i < nparams; i++ {
v := params.At(i)
name := v.Name()
if name != "" {
value := llvm_fn.Param(i + paramoffset)
typ := v.Type()
if isvariadic && i == nparams-1 {
typ = types.NewSlice(typ)
}
stackvalue := c.builder.CreateAlloca(c.types.ToLLVM(typ), name)
c.builder.CreateStore(value, stackvalue)
ptrvalue := c.NewValue(stackvalue, types.NewPointer(typ))
stackvar := ptrvalue.makePointee()
stackvar.stack = f
c.objectdata[v].Value = stackvar
}
}
funcstate := &function{LLVMValue: f, results: results}
c.functions.push(funcstate)
hasdefer := hasDefer(funcstate, body)
// Allocate space on the stack for named results.
results.ForEach(func(v *types.Var) {
name := v.Name()
allocstack := name != ""
if !allocstack && hasdefer {
c.objectdata[v] = &ObjectData{}
allocstack = true
}
if allocstack {
typ := v.Type()
llvmtyp := c.types.ToLLVM(typ)
stackptr := c.builder.CreateAlloca(llvmtyp, name)
c.builder.CreateStore(llvm.ConstNull(llvmtyp), stackptr)
ptrvalue := c.NewValue(stackptr, types.NewPointer(typ))
stackvar := ptrvalue.makePointee()
stackvar.stack = f
c.objectdata[v].Value = stackvar
}
})
// Create the function body.
if hasdefer {
c.makeDeferBlock(funcstate, body)
}
c.VisitBlockStmt(body, false)
c.functions.pop()
// If the last instruction in the function is not a terminator, then
// we either have unreachable code or a missing optional return statement
// (the latter case is allowable only for functions without results).
//
// Use GetInsertBlock rather than LastBasicBlock, since the
// last basic block might actually be a "defer" block.
last := c.builder.GetInsertBlock()
if in := last.LastInstruction(); in.IsNil() || in.IsATerminatorInst().IsNil() {
c.builder.SetInsertPointAtEnd(last)
if results.Len() == 0 {
if funcstate.deferblock.IsNil() {
c.builder.CreateRetVoid()
} else {
c.builder.CreateBr(funcstate.deferblock)
}
} else {
c.builder.CreateUnreachable()
}
}
}
func (c *compiler) VisitFuncDecl(f *ast.FuncDecl) Value {
fn := c.Resolve(f.Name).(*LLVMValue)
attributes := parseAttributes(f.Doc)
for _, attr := range attributes {
attr.Apply(fn)
}
if f.Body == nil {
return fn
}
var paramVars []*types.Var
ftyp := fn.Type().(*types.Signature)
if recv := ftyp.Recv(); recv != nil {
paramVars = append(paramVars, recv)
}
if ftyp.Params != nil {
ftyp.Params().ForEach(func(p *types.Var) {
paramVars = append(paramVars, p)
})
}
paramVarsTuple := types.NewTuple(paramVars...)
c.buildFunction(fn, nil, paramVarsTuple, ftyp.Results(), f.Body, ftyp.IsVariadic())
if f.Recv == nil && f.Name.Name == "init" {
// Is it an 'init' function? Then record it.
fnptr := llvm.ConstExtractValue(fn.value, []uint32{0})
c.initfuncs = append(c.initfuncs, fnptr)
}
return fn
}
// Create a constructor function which initialises a global.
// TODO collapse all global inits into one init function?
func (c *compiler) createGlobals(idents []*ast.Ident, values []ast.Expr, pkg string) {
globals := make([]*LLVMValue, len(idents))
for i, ident := range idents {
if ident.Name != "_" {
t := c.objects[ident].Type()
llvmtyp := c.types.ToLLVM(t)
gv := llvm.AddGlobal(c.module.Module, llvmtyp, pkg+"."+ident.Name)
g := c.NewValue(gv, types.NewPointer(t)).makePointee()
globals[i] = g
c.objectdata[c.objects[ident]].Value = g
}
}
if len(values) == 0 {
for _, g := range globals {
if g != nil {
typ := g.pointer.value.Type().ElementType()
initializer := llvm.ConstNull(typ)
g.pointer.value.SetInitializer(initializer)
}
}
return
} else if len(values) == len(idents) {
// Non-compound. Initialise global variables with constant
// values (if any). If all expressions are constant, return
// immediately after, to avoid the unnecessary function
// below.
allconst := true
for i, expr := range values {
constinfo := c.types.expr[expr]
if constinfo.Value != nil {
if globals[i] != nil {
gv := globals[i].pointer.value
value := c.VisitExpr(expr)
value = value.Convert(globals[i].Type())
gv.SetInitializer(value.LLVMValue())
}
} else {
allconst = false
}
}
if allconst {
return
}
}
// There are non-const expressions, so we must create an init()
// function to evaluate the expressions and initialise the globals.
if block := c.builder.GetInsertBlock(); !block.IsNil() {
defer c.builder.SetInsertPointAtEnd(block)
}
llvmfntype := llvm.FunctionType(llvm.VoidType(), nil, false)
fn := llvm.AddFunction(c.module.Module, "", llvmfntype)
entry := llvm.AddBasicBlock(fn, "entry")
c.builder.SetInsertPointAtEnd(entry)
if len(values) == 1 && len(idents) > 1 {
values := c.destructureExpr(values[0])
for i, v := range values {
if globals[i] != nil {
//v := values[i].Convert(ident.Obj.Type.(types.Type))
gv := globals[i].pointer.value
gv.SetInitializer(llvm.Undef(gv.Type().ElementType()))
c.builder.CreateStore(v.LLVMValue(), gv)
}
}
} else {
for i, expr := range values {
constval := c.types.expr[expr].Value
if constval == nil {
// Must evaluate regardless of whether value is
// assigned, in event of side-effects.
v := c.VisitExpr(expr)
if globals[i] != nil {
gv := globals[i].pointer.value
gv.SetInitializer(llvm.Undef(gv.Type().ElementType()))
v = v.Convert(globals[i].Type())
c.builder.CreateStore(v.LLVMValue(), gv)
}
}
}
}
c.builder.CreateRetVoid()
c.varinitfuncs = append(c.varinitfuncs, fn)
}
func (c *compiler) VisitValueSpec(valspec *ast.ValueSpec) {
// Check if the value-spec has already been visited (referenced
// before definition visited.)
for _, name := range valspec.Names {
if name != nil && name.Name != "_" {
obj := c.objects[name]
if c.objectdata[obj].Value != nil {
return
}
}
}
if len(valspec.Values) == len(valspec.Names) {
for i, name := range valspec.Names {
if name.Name == "" {
continue
}
typ := c.objects[name].Type()
c.convertUntyped(valspec.Values[i], typ)
}
}
// If the ValueSpec exists at the package level, create globals.
if obj, ok := c.objects[valspec.Names[0]]; ok {
if c.pkg.Scope().Lookup(valspec.Names[0].Name) == obj {
c.createGlobals(valspec.Names, valspec.Values, pkgpath(c.pkg))
return
}
}
var values []Value
if len(valspec.Values) == 1 && len(valspec.Names) > 1 {
values = c.destructureExpr(valspec.Values[0])
} else if len(valspec.Values) > 0 {
values = make([]Value, len(valspec.Names))
for i, x := range valspec.Values {
values[i] = c.VisitExpr(x)
}
}
for i, name := range valspec.Names {
if name.Name == "_" {
continue
}
// The variable should be allocated on the stack if it's
// declared inside a function.
//
// FIXME currently allocating all variables on the heap.
// Change this to allocate on the stack, and perform
// escape analysis to determine whether to promote.
obj := c.objects[name]
typ := obj.Type()
llvmtyp := c.types.ToLLVM(typ)
ptr := c.createTypeMalloc(llvmtyp)
if values != nil && values[i] != nil {
// FIXME we need to revisit how aggregate types
// are initialised/copied/etc. A CreateStore will
// try to do everything in registers, which is
// going to hurt when the aggregate is large.
llvmInit := values[i].Convert(typ).LLVMValue()
c.builder.CreateStore(llvmInit, ptr)
}
stackvar := c.NewValue(ptr, types.NewPointer(typ)).makePointee()
stackvar.stack = c.functions.top().LLVMValue
c.objectdata[c.objects[name]].Value = stackvar
}
}
func (c *compiler) VisitGenDecl(decl *ast.GenDecl) {
switch decl.Tok {
case token.IMPORT:
// Already handled in type-checking.
break
case token.TYPE:
// Export runtime type information.
for _, spec := range decl.Specs {
typspec := spec.(*ast.TypeSpec)
typ := c.objects[typspec.Name].Type()
c.types.ToRuntime(typ)
}
case token.CONST:
// Nothing to do; constants are evaluated by go/types.
// They are converted to LLVM constant values at the
// site of use.
case token.VAR:
// Global variable attributes
// TODO only parse attributes for package-level var's.
attributes := parseAttributes(decl.Doc)
for _, spec := range decl.Specs {
valspec, _ := spec.(*ast.ValueSpec)
c.VisitValueSpec(valspec)
for _, attr := range attributes {
for _, name := range valspec.Names {
obj := c.objects[name]
attr.Apply(c.objectdata[obj].Value)
}
}
}
}
}
func (c *compiler) VisitDecl(decl ast.Decl) Value {
// This is temporary. We'll return errors later, rather than panicking.
if c.Logger != nil {
c.Logger.Println("Compile declaration:", c.fileset.Position(decl.Pos()))
}
defer func() {
if e := recover(); e != nil {
elist := new(scanner.ErrorList)
elist.Add(c.fileset.Position(decl.Pos()), fmt.Sprint(e))
panic(elist)
}
}()
switch x := decl.(type) {
case *ast.FuncDecl:
return c.VisitFuncDecl(x)
case *ast.GenDecl:
c.VisitGenDecl(x)
return nil
}
panic(fmt.Sprintf("Unhandled decl (%s) at %s\n",
reflect.TypeOf(decl),
c.fileset.Position(decl.Pos())))
}