forked from go-llvm/llgo
/
runtime.go
146 lines (128 loc) · 4 KB
/
runtime.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
// Copyright 2012 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"
"github.com/axw/gollvm/llvm"
"go/ast"
"go/build"
"go/parser"
"go/token"
"path"
"strings"
)
type FunctionCache struct {
*compiler
functions map[string]llvm.Value
}
func NewFunctionCache(c *compiler) *FunctionCache {
return &FunctionCache{c, make(map[string]llvm.Value)}
}
func (c *FunctionCache) NamedFunction(name string, signature string) llvm.Value {
f, _ := c.functions[name+":"+signature]
if !f.IsNil() {
return f
}
if strings.HasPrefix(name, c.module.Name+".") {
obj := c.pkg.Scope().Lookup(name[len(c.module.Name)+1:])
if obj == nil {
panic("Missing function: " + name)
}
value := c.Resolve(c.objectdata[obj].Ident)
f = llvm.ConstExtractValue(value.LLVMValue(), []uint32{0})
} else {
if c.runtimetypespkg == nil {
// Parse the runtime package, since we may need to refer to
// its types.
buildpkg, err := build.Import("github.com/axw/llgo/pkg/runtime", "", 0)
if err != nil {
panic(err)
}
// All types visible to the compiler are in "types.go".
runtimefiles := []string{path.Join(buildpkg.Dir, "types.go")}
fset := token.NewFileSet()
files, err := parseFiles(fset, runtimefiles)
if err != nil {
panic(err)
}
c.runtimetypespkg, err = c.typecheck("runtime", fset, files)
if err != nil {
panic(err)
}
}
pkg := c.runtimetypespkg
scope := pkg.Scope().Child(0)
ftype, _, err := types.Eval(signature+"{panic()}", pkg, scope)
if err != nil {
panic(err)
}
llvmfntyp := c.types.ToLLVM(ftype).StructElementTypes()[0].ElementType()
f = llvm.AddFunction(c.module.Module, name, llvmfntyp)
}
c.functions[name+":"+signature] = f
return f
}
func parseFile(fset *token.FileSet, name string) (*ast.File, error) {
return parser.ParseFile(fset, name, nil, parser.DeclarationErrors)
}
func parseFiles(fset *token.FileSet, filenames []string) (files []*ast.File, err error) {
for _, filename := range filenames {
var file *ast.File
file, err = parseFile(fset, filename)
if err != nil {
return
} else if file != nil {
files = append(files, file)
}
}
return
}
// parseReflect parses the reflect package and type-checks its AST.
// This is used to generate runtime type structures.
func (c *compiler) parseReflect() (*types.Package, error) {
buildpkg, err := build.Import("reflect", "", 0)
if err != nil {
return nil, err
}
filenames := make([]string, len(buildpkg.GoFiles))
for i, f := range buildpkg.GoFiles {
filenames[i] = path.Join(buildpkg.Dir, f)
}
fset := token.NewFileSet()
files, err := parseFiles(fset, filenames)
if err != nil {
return nil, err
}
pkg, err := c.typecheck("reflect", fset, files)
if err != nil {
return nil, err
}
return pkg, nil
}
func (c *compiler) createMalloc(size llvm.Value) llvm.Value {
malloc := c.NamedFunction("runtime.malloc", "func(uintptr) unsafe.Pointer")
switch n := size.Type().IntTypeWidth() - c.target.IntPtrType().IntTypeWidth(); {
case n < 0:
size = c.builder.CreateZExt(size, c.target.IntPtrType(), "")
case n > 0:
size = c.builder.CreateTrunc(size, c.target.IntPtrType(), "")
}
return c.builder.CreateCall(malloc, []llvm.Value{size}, "")
}
func (c *compiler) createTypeMalloc(t llvm.Type) llvm.Value {
ptr := c.createMalloc(llvm.SizeOf(t))
return c.builder.CreateIntToPtr(ptr, llvm.PointerType(t, 0), "")
}
func (c *compiler) memsetZero(ptr llvm.Value, size llvm.Value) {
memset := c.NamedFunction("runtime.memset", "func(dst unsafe.Pointer, fill byte, size uintptr)")
switch n := size.Type().IntTypeWidth() - c.target.IntPtrType().IntTypeWidth(); {
case n < 0:
size = c.builder.CreateZExt(size, c.target.IntPtrType(), "")
case n > 0:
size = c.builder.CreateTrunc(size, c.target.IntPtrType(), "")
}
ptr = c.builder.CreatePtrToInt(ptr, c.target.IntPtrType(), "")
fill := llvm.ConstNull(llvm.Int8Type())
c.builder.CreateCall(memset, []llvm.Value{ptr, fill, size}, "")
}