forked from go-llvm/llgo
/
decl.go
362 lines (331 loc) · 10.5 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
/*
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"
"go/scanner"
"go/token"
"reflect"
"strconv"
)
func (c *compiler) VisitFuncProtoDecl(f *ast.FuncDecl) *LLVMValue {
if f.Name.Obj != nil {
if result, ok := f.Name.Obj.Data.(*LLVMValue); ok {
return result
}
}
var fn_type *types.Func
fn_name := f.Name.String()
if f.Recv == nil && fn_name == "init" {
// Make "init" functions anonymous.
fn_name = ""
// "init" functions aren't recorded by the parser, so f.Name.Obj is
// not set.
fn_type = &types.Func{ /* no params or result */}
} else {
fn_type = f.Name.Obj.Type.(*types.Func)
if c.module.Name != "main" || fn_name != "main" {
if fn_type.Recv != nil {
recv := fn_type.Recv
if recvtyp, ok := recv.Type.(*types.Pointer); ok {
recv = recvtyp.Base.(*types.Name).Obj
} else {
recv = recv.Type.(*types.Name).Obj
}
pkgname := c.pkgmap[recv]
fn_name = pkgname + "." + recv.Name + "." + fn_name
} else {
pkgname := c.pkgmap[f.Name.Obj]
fn_name = pkgname + "." + fn_name
}
}
}
llvm_fn_type := c.types.ToLLVM(fn_type).ElementType()
// gcimporter may produce multiple AST objects for the same function.
fn := c.module.Module.NamedFunction(fn_name)
if fn.IsNil() {
fn = llvm.AddFunction(c.module.Module, fn_name, llvm_fn_type)
}
result := c.NewLLVMValue(fn, fn_type)
if f.Name.Obj != nil {
f.Name.Obj.Data = result
f.Name.Obj.Type = fn_type
}
return result
}
// buildFunction takes a function Value, a list of parameters, and a body,
// and generates code for the function.
func (c *compiler) buildFunction(f *LLVMValue, params []*ast.Object, body *ast.BlockStmt) {
ftyp := f.Type().(*types.Func)
llvm_fn := f.LLVMValue()
entry := llvm.AddBasicBlock(llvm_fn, "entry")
c.builder.SetInsertPointAtEnd(entry)
// Bind receiver, arguments and return values to their identifiers/objects.
// We'll store each parameter on the stack so they're addressable.
for i, obj := range params {
if obj.Name != "" {
value := llvm_fn.Param(i)
typ := obj.Type.(types.Type)
stackvalue := c.builder.CreateAlloca(c.types.ToLLVM(typ), obj.Name)
c.builder.CreateStore(value, stackvalue)
ptrvalue := c.NewLLVMValue(stackvalue, &types.Pointer{Base: typ})
obj.Data = ptrvalue.makePointee()
}
}
// Allocate space on the stack for named results.
for _, obj := range ftyp.Results {
if obj.Name != "" {
typ := obj.Type.(types.Type)
llvmtyp := c.types.ToLLVM(typ)
stackptr := c.builder.CreateAlloca(llvmtyp, obj.Name)
c.builder.CreateStore(llvm.ConstNull(llvmtyp), stackptr)
ptrvalue := c.NewLLVMValue(stackptr, &types.Pointer{Base: typ})
obj.Data = ptrvalue.makePointee()
}
}
c.functions = append(c.functions, f)
c.VisitBlockStmt(body, false)
c.functions = c.functions[0 : len(c.functions)-1]
last := llvm_fn.LastBasicBlock()
if in := last.LastInstruction(); in.IsNil() || in.IsATerminatorInst().IsNil() {
// Assume nil return type, AST should be checked first.
c.builder.SetInsertPointAtEnd(last)
c.builder.CreateRetVoid()
}
}
func (c *compiler) VisitFuncDecl(f *ast.FuncDecl) Value {
var fn *LLVMValue
if f.Name.Obj != nil {
fn = c.Resolve(f.Name.Obj).(*LLVMValue)
} else {
fn = c.VisitFuncProtoDecl(f)
}
attributes := parseAttributes(f.Doc)
for _, attr := range attributes {
attr.Apply(fn)
}
if f.Body == nil {
return fn
}
fn_type := fn.Type().(*types.Func)
paramObjects := fn_type.Params
if f.Recv != nil {
paramObjects = append([]*ast.Object{fn_type.Recv}, paramObjects...)
}
c.buildFunction(fn, paramObjects, f.Body)
// Is it an 'init' function? Then record it.
if f.Recv == nil && f.Name.Name == "init" {
c.initfuncs = append(c.initfuncs, fn)
}
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 := ident.Obj.Type.(types.Type)
llvmtyp := c.types.ToLLVM(t)
gv := llvm.AddGlobal(c.module.Module, llvmtyp, pkg+"."+ident.Name)
g := c.NewLLVMValue(gv, &types.Pointer{Base: t}).makePointee()
globals[i] = g
ident.Obj.Data = g
}
}
if len(values) == 0 {
for _, g := range globals {
if g != nil {
initializer := llvm.ConstNull(g.pointer.value.Type().ElementType())
g.pointer.value.SetInitializer(initializer)
}
}
return
}
// FIXME Once we have constant folding, we can check first if the value is
// a constant. For now we'll create a function and then erase it if the
// computed value is a constant.
if block := c.builder.GetInsertBlock(); !block.IsNil() {
defer c.builder.SetInsertPointAtEnd(block)
}
fntype := &types.Func{}
llvmfntype := c.types.ToLLVM(fntype).ElementType()
fn := llvm.AddFunction(c.module.Module, "", llvmfntype)
entry := llvm.AddBasicBlock(fn, "entry")
c.builder.SetInsertPointAtEnd(entry)
if len(values) == 1 && len(idents) > 1 {
// Compound values are always non-constant.
values := c.destructureExpr(values[0])
for i, ident := range idents {
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 {
allconst := true
for i, expr := range values {
if globals[i] != nil {
gv := globals[i].pointer.value
ident := idents[i]
value := c.VisitExpr(expr)
value = value.Convert(ident.Obj.Type.(types.Type))
_, isconst := value.(ConstValue)
if isconst {
gv.SetInitializer(value.LLVMValue())
} else {
allconst = false
gv.SetInitializer(llvm.Undef(gv.Type().ElementType()))
c.builder.CreateStore(value.LLVMValue(), gv)
}
}
}
if allconst {
fn.EraseFromParentAsFunction()
fn = llvm.Value{nil}
}
}
// FIXME order global ctors
if !fn.IsNil() {
c.builder.CreateRetVoid()
fnvalue := c.NewLLVMValue(fn, fntype)
c.varinitfuncs = append(c.varinitfuncs, fnvalue)
}
}
func (c *compiler) VisitValueSpec(valspec *ast.ValueSpec, isconst bool) {
// Check if the value-spec has already been visited (referenced
// before definition visited.)
if len(valspec.Names) > 0 {
if _, ok := valspec.Names[0].Obj.Data.(Value); ok {
return
}
}
var iotaObj *ast.Object = types.Universe.Lookup("iota")
defer func(data interface{}) {
iotaObj.Data = data
}(iotaObj.Data)
pkgname, ispackagelevel := c.pkgmap[valspec.Names[0].Obj]
if ispackagelevel && !isconst {
c.createGlobals(valspec.Names, valspec.Values, pkgname)
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, name_ := range valspec.Names {
if isconst {
if iota_, isint := (name_.Obj.Data).(int); isint {
iotaValue := c.NewConstValue(token.INT, strconv.Itoa(iota_))
iotaObj.Data = iotaValue
}
}
values[i] = c.VisitExpr(valspec.Values[i])
}
}
for i, name := range valspec.Names {
if name.Name == "_" {
continue
}
// For constants, we just pass the ConstValue around. Otherwise, we
// will convert it to an LLVMValue.
var value Value
if isconst {
value = values[i].Convert(name.Obj.Type.(types.Type))
} else {
// The variable should be allocated on the stack if it's
// declared inside a function.
var llvmInit llvm.Value
typ := name.Obj.Type.(types.Type)
ptr := c.builder.CreateAlloca(c.types.ToLLVM(typ), name.Name)
if values == nil || values[i] == nil {
// If no initialiser was specified, set it to the
// zero value.
llvmInit = llvm.ConstNull(c.types.ToLLVM(typ))
} else {
llvmInit = values[i].Convert(typ).LLVMValue()
}
c.builder.CreateStore(llvmInit, ptr)
value = c.NewLLVMValue(ptr, &types.Pointer{Base: typ}).makePointee()
}
name.Obj.Data = value
}
}
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 := typspec.Name.Obj.Type.(types.Type)
c.types.ToRuntime(typ)
}
case token.CONST:
for _, spec := range decl.Specs {
valspec := spec.(*ast.ValueSpec)
c.VisitValueSpec(valspec, true)
}
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, false)
for _, attr := range attributes {
for _, name := range valspec.Names {
attr.Apply(name.Obj.Data.(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())))
}
// vim: set ft=go :