forked from go-llvm/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
187 lines (170 loc) · 4.93 KB
/
debug.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
// 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"
"github.com/axw/gollvm/llvm"
"go/ast"
"go/token"
)
// llgo constants.
const (
LLGOAuthor = "Andrew Wilkins <[email protected]>"
LLGOProducer = "llgo " + LLGOVersion + " (" + LLGOAuthor + ")"
)
// Go builtin types.
var (
int32_debug_type = &llvm.BasicTypeDescriptor{
Name: "int32",
Size: 32,
Alignment: 32,
TypeEncoding: llvm.DW_ATE_signed,
}
void_debug_type = &llvm.BasicTypeDescriptor{
Name: "void",
Size: 0,
Alignment: 0,
}
)
func (c *compiler) tollvmDebugDescriptor(t types.Type) llvm.DebugDescriptor {
switch t := t.(type) {
case *types.Pointer:
return llvm.NewPointerDerivedType(c.tollvmDebugDescriptor(t.Elem()))
case nil:
return void_debug_type
}
bt := &llvm.BasicTypeDescriptor{
Name: t.String(),
Size: uint64(c.types.Sizeof(t) * 8),
Alignment: uint64(c.types.Alignof(t) * 8),
}
if basic, ok := t.(*types.Basic); ok {
switch bi := basic.Info(); {
case bi&types.IsBoolean != 0:
bt.TypeEncoding = llvm.DW_ATE_boolean
case bi&types.IsUnsigned != 0:
bt.TypeEncoding = llvm.DW_ATE_unsigned
case bi&types.IsInteger != 0:
bt.TypeEncoding = llvm.DW_ATE_signed
case bi&types.IsFloat != 0:
bt.TypeEncoding = llvm.DW_ATE_float
}
}
return bt
}
func (c *compiler) pushDebugContext(d llvm.DebugDescriptor) {
c.debug_context = append(c.debug_context, d)
}
func (c *compiler) popDebugContext() (top llvm.DebugDescriptor) {
n := len(c.debug_context)
top, c.debug_context = c.debug_context[n-1], c.debug_context[:n-1]
return top
}
func (c *compiler) currentDebugContext() llvm.DebugDescriptor {
if len(c.debug_context) == 0 {
return nil
}
return c.debug_context[len(c.debug_context)-1]
}
func (c *compiler) setDebugLine(pos token.Pos) {
ctx := c.currentDebugContext()
if ctx == nil {
return
}
file := c.fileset.File(pos)
ld := &llvm.LineDescriptor{
Line: uint32(file.Line(pos)),
Context: ctx,
}
c.builder.SetCurrentDebugLocation(c.debug_info.MDNode(ld))
}
var uniqueId uint32
func (c *compiler) createBlockMetadata(stmt *ast.BlockStmt) llvm.DebugDescriptor {
ctx := c.currentDebugContext()
if ctx == nil {
return nil
}
uniqueId++
file := c.fileset.File(stmt.Pos())
fd := llvm.FileDescriptor(file.Name())
return &llvm.BlockDescriptor{
File: &fd,
Line: uint32(file.Line(stmt.Pos())),
Context: ctx,
Id: uniqueId,
}
}
func (c *compiler) createFunctionMetadata(f *ast.FuncDecl, fn *LLVMValue) llvm.DebugDescriptor {
if len(c.debug_context) == 0 {
return nil
}
file := c.fileset.File(f.Pos())
fnptr := fn.value
fun := fnptr.IsAFunction()
if fun.IsNil() {
fnptr = llvm.ConstExtractValue(fn.value, []uint32{0})
}
meta := &llvm.SubprogramDescriptor{
Name: fnptr.Name(),
DisplayName: f.Name.Name,
Path: llvm.FileDescriptor(file.Name()),
Line: uint32(file.Line(f.Pos())),
ScopeLine: uint32(file.Line(f.Body.Pos())),
Context: &llvm.ContextDescriptor{llvm.FileDescriptor(file.Name())},
Function: fnptr}
var result types.Type
var metaparams []llvm.DebugDescriptor
if ftyp, ok := fn.Type().(*types.Signature); ok {
if recv := ftyp.Recv(); recv != nil {
metaparams = append(metaparams, c.tollvmDebugDescriptor(recv.Type()))
}
if ftyp.Params() != nil {
for i := 0; i < ftyp.Params().Len(); i++ {
p := ftyp.Params().At(i)
metaparams = append(metaparams, c.tollvmDebugDescriptor(p.Type()))
}
}
if ftyp.Results() != nil {
result = ftyp.Results().At(0).Type()
// TODO: what to do with multiple returns?
for i := 1; i < ftyp.Results().Len(); i++ {
p := ftyp.Results().At(i)
metaparams = append(metaparams, c.tollvmDebugDescriptor(p.Type()))
}
}
}
meta.Type = llvm.NewSubroutineCompositeType(
c.tollvmDebugDescriptor(result),
metaparams,
)
// compile unit is the first context object pushed
compileUnit := c.debug_context[0].(*llvm.CompileUnitDescriptor)
compileUnit.Subprograms = append(compileUnit.Subprograms, meta)
return meta
}
// createLocalVariableMetadata creates and returns a debug descriptor for
// a local variable in a function.
//
// obj is the go/types object for the var.
// paramIndex is 0 for "auto" variables (non-parameter stack vars), and a
// 1-based index for parameter vars.
func (c *compiler) createLocalVariableMetadata(obj types.Object, paramIndex int) llvm.DebugDescriptor {
ctx := c.currentDebugContext()
if ctx == nil {
return nil
}
tag := llvm.DW_TAG_auto_variable
if paramIndex > 0 {
tag = llvm.DW_TAG_arg_variable
}
position := c.fileset.Position(obj.Pos())
ld := llvm.NewLocalVariableDescriptor(tag)
ld.Argument = uint32(paramIndex)
ld.Line = uint32(position.Line)
ld.Name = obj.Name()
ld.File = &llvm.ContextDescriptor{llvm.FileDescriptor(position.Filename)}
ld.Type = c.tollvmDebugDescriptor(obj.Type())
ld.Context = ctx
return ld
}