forked from go-llvm/llgo
/
compiler.go
422 lines (375 loc) · 12.3 KB
/
compiler.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
// 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/exact"
"code.google.com/p/go.tools/go/types"
"fmt"
"github.com/axw/gollvm/llvm"
"go/ast"
"go/token"
"log"
"strings"
)
type Module struct {
llvm.Module
Name string
Disposed bool
}
func (m Module) Dispose() {
if !m.Disposed {
m.Disposed = true
m.Module.Dispose()
}
}
// TODO get rid of this, change compiler to Compiler.
type Compiler interface {
Compile(fset *token.FileSet, files []*ast.File, importpath string) (*Module, error)
Dispose()
}
type compiler struct {
CompilerOptions
builder *Builder
module *Module
machine llvm.TargetMachine
target llvm.TargetData
functions functionStack
breakblocks []llvm.BasicBlock
continueblocks []llvm.BasicBlock
initfuncs []llvm.Value
varinitfuncs []llvm.Value
pkg *types.Package
fileset *token.FileSet
typeinfo types.Info
objectdata map[types.Object]*ObjectData
methodsets map[types.Type]*methodset
exportedtypes []types.Type
// lastlabel, if non-nil, is a LabeledStmt immediately
// preceding an unprocessed ForStmt, SwitchStmt or SelectStmt.
// Upon processing the statement, the label data will be updated,
// and forlabel set to nil.
lastlabel *ast.Ident
*FunctionCache
llvmtypes *LLVMTypeMap
types *TypeMap
// runtimetypespkg is the type-checked runtime/types.go file,
// which is used for evaluating the types of runtime functions.
runtimetypespkg *types.Package
// pnacl is set to true if the target triple was originally
// specified as "pnacl". This is necessary, as the TargetTriple
// field will have been updated to the true triple used to
// compile PNaCl modules.
pnacl bool
debug_context []llvm.DebugDescriptor
compile_unit *llvm.CompileUnitDescriptor
debug_info *llvm.DebugInfo
}
func (c *compiler) archinfo() (intsize, ptrsize int64) {
ptrsize = int64(c.target.PointerSize())
if ptrsize >= 8 {
intsize = 8
} else {
intsize = 4
}
return
}
func (c *compiler) Resolve(ident *ast.Ident) Value {
obj := c.typeinfo.Objects[ident]
data := c.objectdata[obj]
if data.Value != nil {
return data.Value
}
var value *LLVMValue
switch obj := obj.(type) {
case *types.Func:
value = c.makeFunc(ident, obj.Type().(*types.Signature))
case *synthFunc:
value = c.makeFunc(ident, obj.Type().(*types.Signature))
case *types.Var:
if data.Ident.Obj != nil {
switch decl := data.Ident.Obj.Decl.(type) {
case *ast.ValueSpec:
c.VisitValueSpec(decl)
case *ast.Field:
// No-op. Fields will be yielded for function
// arg/recv/ret. We update the .Data field of the
// object when we enter the function definition.
if data.Value == nil {
panic("expected object value")
}
}
}
// If it's an external variable, we'll need to create a global
// value reference here. It may be possible for multiple objects
// to refer to the same variable.
value = data.Value
if value == nil {
module := c.module.Module
t := obj.Type()
name := obj.Pkg().Path() + "." + obj.Name()
g := module.NamedGlobal(name)
if g.IsNil() {
g = llvm.AddGlobal(module, c.types.ToLLVM(t), name)
}
value = c.NewValue(g, types.NewPointer(t)).makePointee()
}
case *types.Const:
value = c.NewConstValue(obj.Val(), obj.Type())
default:
panic(fmt.Sprintf("unreachable (%T)", obj))
}
data.Value = value
return value
}
///////////////////////////////////////////////////////////////////////////////
type CompilerOptions struct {
// TargetTriple is the LLVM triple for the target.
TargetTriple string
// Logger is a logger used for tracing compilation.
Logger *log.Logger
}
// Based on parseArch from LLVM's lib/Support/Triple.cpp.
// This is used to match the target machine type.
func parseArch(arch string) string {
switch arch {
case "i386", "i486", "i586", "i686", "i786", "i886", "i986":
return "x86"
case "amd64", "x86_64":
return "x86-64"
case "powerpc":
return "ppc"
case "powerpc64", "ppu":
return "ppc64"
case "mblaze":
return "mblaze"
case "arm", "xscale":
return "arm"
case "thumb":
return "thumb"
case "spu", "cellspu":
return "cellspu"
case "msp430":
return "msp430"
case "mips", "mipseb", "mipsallegrex":
return "mips"
case "mipsel", "mipsallegrexel":
return "mipsel"
case "mips64", "mips64eb":
return "mips64"
case "mipsel64":
return "mipsel64"
case "r600", "hexagon", "sparc", "sparcv9", "tce",
"xcore", "nvptx", "nvptx64", "le32", "amdil":
return arch
}
if strings.HasPrefix(arch, "armv") {
return "arm"
} else if strings.HasPrefix(arch, "thumbv") {
return "thumb"
}
return "unknown"
}
func NewCompiler(opts CompilerOptions) (Compiler, error) {
compiler := &compiler{CompilerOptions: opts}
if strings.ToLower(compiler.TargetTriple) == "pnacl" {
compiler.TargetTriple = PNaClTriple
compiler.pnacl = true
}
// Triples are several fields separated by '-' characters.
// The first field is the architecture. The architecture's
// canonical form may include a '-' character, which would
// have been translated to '_' for inclusion in a triple.
triple := compiler.TargetTriple
arch := triple[:strings.IndexRune(triple, '-')]
arch = parseArch(arch)
var machine llvm.TargetMachine
for target := llvm.FirstTarget(); target.C != nil; target = target.NextTarget() {
if arch == target.Name() {
machine = target.CreateTargetMachine(triple, "", "",
llvm.CodeGenLevelDefault,
llvm.RelocDefault,
llvm.CodeModelDefault)
compiler.machine = machine
break
}
}
if machine.C == nil {
return nil, fmt.Errorf("Invalid target triple: %s", triple)
}
compiler.target = machine.TargetData()
return compiler, nil
}
func (compiler *compiler) Dispose() {
if compiler.machine.C != nil {
compiler.machine.Dispose()
compiler.machine.C = nil
}
}
func (compiler *compiler) Compile(fset *token.FileSet, files []*ast.File, importpath string) (m *Module, err error) {
// FIXME create a compilation state, rather than storing in 'compiler'.
compiler.fileset = fset
compiler.initfuncs = nil
compiler.varinitfuncs = nil
// If no import path is specified, or the package's
// name (not path) is "main", then set the import
// path to be the same as the package's name.
if importpath == "" || files[0].Name.String() == "main" {
importpath = files[0].Name.String()
}
// Type-check, and store object data.
compiler.typeinfo.Types = make(map[ast.Expr]types.Type)
compiler.typeinfo.Values = make(map[ast.Expr]exact.Value)
compiler.typeinfo.Objects = make(map[*ast.Ident]types.Object)
compiler.typeinfo.Implicits = make(map[ast.Node]types.Object)
compiler.typeinfo.Selections = make(map[*ast.SelectorExpr]*types.Selection)
compiler.objectdata = make(map[types.Object]*ObjectData)
compiler.methodsets = make(map[types.Type]*methodset)
compiler.exportedtypes = nil
compiler.llvmtypes = NewLLVMTypeMap(compiler.target)
pkg, err := compiler.typecheck(importpath, fset, files)
if err != nil {
return nil, err
}
compiler.pkg = pkg
// Create a Module, which contains the LLVM bitcode. Dispose it on panic,
// otherwise we'll set a finalizer at the end. The caller may invoke
// Dispose manually, which will render the finalizer a no-op.
modulename := importpath
compiler.module = &Module{llvm.NewModule(modulename), modulename, false}
compiler.module.SetTarget(compiler.TargetTriple)
compiler.module.SetDataLayout(compiler.target.String())
defer func() {
if e := recover(); e != nil {
compiler.module.Dispose()
panic(e)
}
}()
// Create a struct responsible for mapping static types to LLVM types,
// and to runtime/dynamic type values.
var resolver Resolver = compiler
compiler.FunctionCache = NewFunctionCache(compiler)
compiler.types = NewTypeMap(compiler.llvmtypes, compiler.module.Module, importpath, compiler.FunctionCache, resolver)
// Create a Builder, for building LLVM instructions.
compiler.builder = newBuilder(compiler.types)
defer compiler.builder.Dispose()
compiler.debug_info = &llvm.DebugInfo{}
// Compile each file in the package.
for _, file := range files {
compiler.compile_unit = &llvm.CompileUnitDescriptor{
Language: llvm.DW_LANG_Go,
Path: llvm.FileDescriptor(fset.File(file.Pos()).Name()),
Producer: LLGOProducer,
Runtime: LLGORuntimeVersion,
}
compiler.pushDebugContext(&compiler.compile_unit.Path)
for _, decl := range file.Decls {
compiler.VisitDecl(decl)
}
compiler.popDebugContext()
if len(compiler.debug_context) > 0 {
log.Panicln(compiler.debug_context)
}
compiler.module.AddNamedMetadataOperand("llvm.dbg.cu", compiler.debug_info.MDNode(compiler.compile_unit))
}
// Export runtime type information.
compiler.exportRuntimeTypes()
// Wrap "main.main" in a call to runtime.main.
if importpath == "main" {
err = compiler.createMainFunction()
if err != nil {
return nil, err
}
} else {
var e = exporter{compiler: compiler}
if err := e.Export(pkg); err != nil {
return nil, err
}
}
// Create global constructors. The initfuncs/varinitfuncs
// slices are in the order of visitation; we generate the
// list of constructors in the reverse order.
//
// The llgo linker will link modules in the order of
// package dependency, i.e. if A requires B, then llgo-link
// will link the modules in the order A, B. The "runtime"
// package is always last.
//
// At program initialisation, the runtime initialisation
// function (runtime.main) will invoke the constructors
// in reverse order.
var initfuncs [][]llvm.Value
if compiler.varinitfuncs != nil {
initfuncs = append(initfuncs, compiler.varinitfuncs)
}
if compiler.initfuncs != nil {
initfuncs = append(initfuncs, compiler.initfuncs)
}
if initfuncs != nil {
ctortype := llvm.PointerType(llvm.Int8Type(), 0)
var ctors []llvm.Value
var index int = 0
for _, initfuncs := range initfuncs {
for _, fnptr := range initfuncs {
name := fmt.Sprintf("__llgo.ctor.%s.%d", importpath, index)
fnptr.SetName(name)
fnptr = llvm.ConstBitCast(fnptr, ctortype)
ctors = append(ctors, fnptr)
index++
}
}
for i, n := 0, len(ctors); i < n/2; i++ {
ctors[i], ctors[n-i-1] = ctors[n-i-1], ctors[i]
}
ctorsInit := llvm.ConstArray(ctortype, ctors)
ctorsVar := llvm.AddGlobal(compiler.module.Module, ctorsInit.Type(), "runtime.ctors")
ctorsVar.SetInitializer(ctorsInit)
ctorsVar.SetLinkage(llvm.AppendingLinkage)
}
// Create debug metadata.
//compiler.createMetadata()
return compiler.module, nil
}
func (c *compiler) createMainFunction() error {
// In a PNaCl program (plugin), there should not be a "main.main";
// instead, we expect a "main.CreateModule" function.
// See pkg/nacl/ppapi/ppapi.go for more details.
mainMain := c.module.NamedFunction("main.main")
if c.pnacl {
// PNaCl's libppapi_stub.a implements "main", which simply
// calls through to PpapiPluginMain. We define our own "main"
// so that we can capture argc/argv.
if !mainMain.IsNil() {
return fmt.Errorf("Found main.main")
}
pluginMain := c.NamedFunction("PpapiPluginMain", "func() int32")
// Synthesise a main which has no return value. We could cast
// PpapiPluginMain, but this is potentially unsafe as its
// calling convention is unspecified.
ftyp := llvm.FunctionType(llvm.VoidType(), nil, false)
mainMain = llvm.AddFunction(c.module.Module, "main.main", ftyp)
entry := llvm.AddBasicBlock(mainMain, "entry")
c.builder.SetInsertPointAtEnd(entry)
c.builder.CreateCall(pluginMain, nil, "")
c.builder.CreateRetVoid()
} else {
mainMain = c.module.NamedFunction("main.main")
}
if mainMain.IsNil() {
return fmt.Errorf("Could not find main.main")
}
// runtime.main is called by main, with argc, argv, argp,
// and a pointer to main.main, which must be a niladic
// function with no result.
runtimeMain := c.NamedFunction("runtime.main", "func(int32, **byte, **byte, *int8) int32")
main := c.NamedFunction("main", "func(int32, **byte, **byte) int32")
c.builder.SetCurrentDebugLocation(c.debug_info.MDNode(nil))
entry := llvm.AddBasicBlock(main, "entry")
c.builder.SetInsertPointAtEnd(entry)
mainMain = c.builder.CreateBitCast(mainMain, runtimeMain.Type().ElementType().ParamTypes()[3], "")
args := []llvm.Value{main.Param(0), main.Param(1), main.Param(2), mainMain}
result := c.builder.CreateCall(runtimeMain, args, "")
c.builder.CreateRet(result)
return nil
}
// vim: set ft=go :