forked from go-llvm/llgo
/
expr.go
490 lines (451 loc) · 14.1 KB
/
expr.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
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/token"
"reflect"
"sort"
"strconv"
)
func isNilIdent(x ast.Expr) bool {
ident, ok := x.(*ast.Ident)
return ok && ident.Obj == types.Nil
}
// Binary logical operators are handled specially, outside of the Value
// type, because of the need to perform lazy evaluation.
//
// Binary logical operators are implemented using a Phi node, which takes
// on the appropriate value depending on which basic blocks branch to it.
func (c *compiler) compileLogicalOp(op token.Token, lhs Value, rhsFunc func() Value) Value {
lhsBlock := c.builder.GetInsertBlock()
resultBlock := llvm.AddBasicBlock(lhsBlock.Parent(), "")
resultBlock.MoveAfter(lhsBlock)
rhsBlock := llvm.InsertBasicBlock(resultBlock, "")
falseBlock := llvm.InsertBasicBlock(resultBlock, "")
if op == token.LOR {
c.builder.CreateCondBr(lhs.LLVMValue(), resultBlock, rhsBlock)
} else {
c.builder.CreateCondBr(lhs.LLVMValue(), rhsBlock, falseBlock)
}
c.builder.SetInsertPointAtEnd(rhsBlock)
rhs := rhsFunc()
rhsBlock = c.builder.GetInsertBlock() // rhsFunc may create blocks
c.builder.CreateCondBr(rhs.LLVMValue(), resultBlock, falseBlock)
c.builder.SetInsertPointAtEnd(falseBlock)
c.builder.CreateBr(resultBlock)
c.builder.SetInsertPointAtEnd(resultBlock)
result := c.builder.CreatePHI(llvm.Int1Type(), "")
trueValue := llvm.ConstAllOnes(llvm.Int1Type())
falseValue := llvm.ConstNull(llvm.Int1Type())
var values []llvm.Value
var blocks []llvm.BasicBlock
if op == token.LOR {
values = []llvm.Value{trueValue, trueValue, falseValue}
blocks = []llvm.BasicBlock{lhsBlock, rhsBlock, falseBlock}
} else {
values = []llvm.Value{trueValue, falseValue}
blocks = []llvm.BasicBlock{rhsBlock, falseBlock}
}
result.AddIncoming(values, blocks)
return c.NewLLVMValue(result, types.Bool)
}
func (c *compiler) VisitBinaryExpr(expr *ast.BinaryExpr) Value {
lhs := c.VisitExpr(expr.X)
switch expr.Op {
case token.LOR, token.LAND:
if lhs, ok := lhs.(ConstValue); ok {
lhsvalue := lhs.Const.Val.(bool)
switch expr.Op {
case token.LOR:
if lhsvalue {
return lhs
}
case token.LAND:
if !lhsvalue {
return lhs
}
}
return c.VisitExpr(expr.Y)
}
return c.compileLogicalOp(expr.Op, lhs, func() Value { return c.VisitExpr(expr.Y) })
case token.SHL, token.SHR:
rhs := c.VisitExpr(expr.Y)
if _, ok := lhs.(ConstValue); ok {
typ := c.types.expr[expr]
lhs = lhs.Convert(typ)
}
return lhs.BinaryOp(expr.Op, rhs)
}
return lhs.BinaryOp(expr.Op, c.VisitExpr(expr.Y))
}
func (c *compiler) VisitUnaryExpr(expr *ast.UnaryExpr) Value {
value := c.VisitExpr(expr.X)
return value.UnaryOp(expr.Op)
}
func (c *compiler) VisitCallExpr(expr *ast.CallExpr) Value {
switch x := (expr.Fun).(type) {
case *ast.Ident:
switch x.String() {
case "print":
return c.VisitPrint(expr, false)
case "println":
return c.VisitPrint(expr, true)
case "len":
return c.VisitLen(expr)
case "new":
return c.VisitNew(expr)
case "make":
return c.VisitMake(expr)
case "append":
return c.VisitAppend(expr)
case "delete":
m := c.VisitExpr(expr.Args[0]).(*LLVMValue)
key := c.VisitExpr(expr.Args[1])
c.mapDelete(m, key)
return nil
case "panic":
// TODO
return nil
}
case *ast.SelectorExpr:
// Handle unsafe functions specially.
if pkgobj, ok := x.X.(*ast.Ident); ok && pkgobj.Obj.Data == types.Unsafe.Data {
var value int
switch x.Sel.Name {
case "Alignof", "Offsetof":
panic("unimplemented")
case "Sizeof":
argtype := c.types.expr[expr.Args[0]]
value = c.sizeofType(argtype)
value := c.NewConstValue(token.INT, strconv.Itoa(value))
value.typ = types.Uintptr
return value
}
}
}
lhs := c.VisitExpr(expr.Fun)
// Is it a type conversion?
if len(expr.Args) == 1 {
if _, ok := lhs.(TypeValue); ok {
typ := lhs.Type()
value := c.VisitExpr(expr.Args[0])
return value.Convert(typ)
}
}
// Not a type conversion, so must be a function call.
fn := lhs.(*LLVMValue)
fn_type := types.Underlying(fn.Type()).(*types.Func)
args := make([]llvm.Value, 0)
if fn_type.Recv != nil {
// Don't dereference the receiver here. It'll have been worked out in
// the selector.
receiver := fn.receiver
args = append(args, receiver.LLVMValue())
}
if nparams := len(fn_type.Params); nparams > 0 {
if fn_type.IsVariadic {
nparams--
}
for i := 0; i < nparams; i++ {
value := c.VisitExpr(expr.Args[i])
param_type := fn_type.Params[i].Type.(types.Type)
args = append(args, value.Convert(param_type).LLVMValue())
}
if fn_type.IsVariadic {
param_type := fn_type.Params[nparams].Type.(*types.Slice).Elt
varargs := make([]llvm.Value, 0)
for i := nparams; i < len(expr.Args); i++ {
value := c.VisitExpr(expr.Args[i])
value = value.Convert(param_type)
varargs = append(varargs, value.LLVMValue())
}
slice_value := c.makeLiteralSlice(varargs, param_type)
args = append(args, slice_value)
}
}
var result_type types.Type
switch len(fn_type.Results) {
case 0: // no-op
case 1:
result_type = fn_type.Results[0].Type.(types.Type)
default:
fields := make([]*ast.Object, len(fn_type.Results))
for i, result := range fn_type.Results {
fields[i] = result
}
result_type = &types.Struct{Fields: fields}
}
// After calling the function, we must bitcast to the computed LLVM
// type. This is a no-op, and exists just to satisfy LLVM's type
// comparisons.
result := c.builder.CreateCall(fn.LLVMValue(), args, "")
if len(fn_type.Results) == 1 {
result = c.builder.CreateBitCast(result, c.types.ToLLVM(result_type), "")
}
return c.NewLLVMValue(result, result_type)
}
func isIntType(t types.Type) bool {
for {
switch x := t.(type) {
case *types.Name:
t = x.Underlying
case *types.Basic:
switch x.Kind {
case types.UintKind, types.Uint8Kind, types.Uint16Kind,
types.Uint32Kind, types.Uint64Kind, types.IntKind,
types.Int8Kind, types.Int16Kind, types.Int32Kind,
types.Int64Kind:
return true
default:
return false
}
default:
return false
}
}
panic("unreachable")
}
func (c *compiler) VisitIndexExpr(expr *ast.IndexExpr) Value {
value := c.VisitExpr(expr.X).(*LLVMValue)
index := c.VisitExpr(expr.Index)
typ := value.Type()
if typ == types.String {
ptr := c.builder.CreateExtractValue(value.LLVMValue(), 0, "")
gepindices := []llvm.Value{index.LLVMValue()}
ptr = c.builder.CreateGEP(ptr, gepindices, "")
result := c.NewLLVMValue(ptr, &types.Pointer{Base: types.Byte})
return result.makePointee()
}
// We can index a pointer to an array.
if _, ok := types.Underlying(typ).(*types.Pointer); ok {
value = value.makePointee()
typ = value.Type()
}
switch types.Underlying(typ).(type) {
case *types.Array, *types.Slice:
var gep_indices []llvm.Value
var ptr llvm.Value
var result_type types.Type
switch typ := types.Underlying(typ).(type) {
case *types.Array:
// FIXME what to do if value is not addressable?
// Do we have to load the array onto the stack?
result_type = typ.Elt
ptr = value.pointer.LLVMValue()
gep_indices = append(gep_indices, llvm.ConstNull(llvm.Int32Type()))
case *types.Slice:
result_type = typ.Elt
ptr = c.builder.CreateExtractValue(value.LLVMValue(), 0, "")
}
gep_indices = append(gep_indices, index.LLVMValue())
element := c.builder.CreateGEP(ptr, gep_indices, "")
result := c.NewLLVMValue(element, &types.Pointer{Base: result_type})
return result.makePointee()
case *types.Map:
value, _ = c.mapLookup(value, index, false)
return value
}
panic(fmt.Sprintf("unreachable (%s)", typ))
}
type selectorCandidate struct {
Indices []int
Type types.Type
}
func (c *compiler) VisitSelectorExpr(expr *ast.SelectorExpr) Value {
lhs := c.VisitExpr(expr.X)
if lhs == nil {
// The only time we should get a nil result is if the object is
// a package.
obj := expr.Sel.Obj
if obj.Kind == ast.Typ {
return TypeValue{obj.Type.(types.Type)}
}
return c.Resolve(obj)
}
// TODO(?) record path to field/method during typechecking, so we don't
// have to search again here.
name := expr.Sel.Name
if iface, ok := types.Underlying(lhs.Type()).(*types.Interface); ok {
i := sort.Search(len(iface.Methods), func(i int) bool {
return iface.Methods[i].Name >= name
})
structValue := lhs.LLVMValue()
receiver := c.builder.CreateExtractValue(structValue, 0, "")
f := c.builder.CreateExtractValue(structValue, i+2, "")
ftype := c.ObjGetType(iface.Methods[i]).(*types.Func)
method := c.NewLLVMValue(c.builder.CreateBitCast(f, c.types.ToLLVM(ftype), ""), ftype)
method.receiver = c.NewLLVMValue(receiver, ftype.Recv.Type.(types.Type))
return method
}
// Search through embedded types for field/method.
var result selectorCandidate
curr := []selectorCandidate{{nil, lhs.Type()}}
for result.Type == nil && len(curr) > 0 {
var next []selectorCandidate
for _, candidate := range curr {
indices := candidate.Indices[0:]
t := candidate.Type
if p, ok := types.Underlying(t).(*types.Pointer); ok {
if _, ok := types.Underlying(p.Base).(*types.Struct); ok {
t = p.Base
}
}
if n, ok := t.(*types.Name); ok {
i := sort.Search(len(n.Methods), func(i int) bool {
return n.Methods[i].Name >= name
})
if i < len(n.Methods) && n.Methods[i].Name == name {
result.Indices = indices
result.Type = t
}
}
if t, ok := types.Underlying(t).(*types.Struct); ok {
if i, ok := t.FieldIndices[name]; ok {
result.Indices = append(indices, int(i))
result.Type = t
} else {
// Add embedded types to the next set of types
// to check.
for i, field := range t.Fields {
if field.Name == "" {
indices = append(indices[0:], i)
t := field.Type.(types.Type)
candidate := selectorCandidate{indices, t}
next = append(next, candidate)
}
}
}
}
}
curr = next
}
// Get a pointer to the field/receiver.
recvValue := lhs.(*LLVMValue)
if _, ok := types.Underlying(lhs.Type()).(*types.Pointer); !ok {
recvValue = recvValue.pointer
}
recvValue = c.NewLLVMValue(recvValue.LLVMValue(), recvValue.Type())
if len(result.Indices) > 0 {
for _, v := range result.Indices {
ptr := recvValue.LLVMValue()
field := types.Underlying(types.Deref(recvValue.typ)).(*types.Struct).Fields[v]
fieldPtr := c.builder.CreateStructGEP(ptr, v, "")
fieldPtrTyp := &types.Pointer{Base: field.Type.(types.Type)}
recvValue = c.NewLLVMValue(fieldPtr, fieldPtrTyp)
// GEP returns a pointer; if the field is a pointer,
// we must load our pointer-to-a-pointer.
if _, ok := field.Type.(*types.Pointer); ok {
recvValue = recvValue.makePointee()
}
}
}
if !types.Identical(recvValue.typ, expr.Sel.Obj.Type.(types.Type)) {
recvValue = recvValue.makePointee()
}
// Method?
if expr.Sel.Obj.Kind == ast.Fun {
method := c.Resolve(expr.Sel.Obj).(*LLVMValue)
methodType := expr.Sel.Obj.Type.(*types.Func)
receiverType := methodType.Recv.Type.(types.Type)
if types.Identical(recvValue.Type(), receiverType) {
method.receiver = recvValue
} else if types.Identical(&types.Pointer{Base: recvValue.Type()}, receiverType) {
method.receiver = recvValue.pointer
} else {
method.receiver = recvValue.makePointee()
}
return method
} else {
return recvValue
}
panic("unreachable")
}
func (c *compiler) VisitStarExpr(expr *ast.StarExpr) Value {
switch operand := c.VisitExpr(expr.X).(type) {
case TypeValue:
return TypeValue{&types.Pointer{Base: operand.Type()}}
case *LLVMValue:
// We don't want to immediately load the value, as we might be doing an
// assignment rather than an evaluation. Instead, we return the pointer
// and tell the caller to load it on demand.
return operand.makePointee()
}
panic("unreachable")
}
func (c *compiler) VisitTypeAssertExpr(expr *ast.TypeAssertExpr) Value {
if expr.Type == nil {
// .(type) switch
// XXX this will probably be handled in the switch statement.
panic("TODO")
} else {
lhs := c.VisitExpr(expr.X)
typ := c.GetType(expr.Type)
return lhs.Convert(typ)
}
return nil
}
func (c *compiler) VisitExpr(expr ast.Expr) Value {
switch x := expr.(type) {
case *ast.BasicLit:
return c.VisitBasicLit(x)
case *ast.BinaryExpr:
return c.VisitBinaryExpr(x)
case *ast.FuncLit:
return c.VisitFuncLit(x)
case *ast.CompositeLit:
return c.VisitCompositeLit(x)
case *ast.UnaryExpr:
return c.VisitUnaryExpr(x)
case *ast.CallExpr:
return c.VisitCallExpr(x)
case *ast.IndexExpr:
return c.VisitIndexExpr(x)
case *ast.SelectorExpr:
return c.VisitSelectorExpr(x)
case *ast.StarExpr:
return c.VisitStarExpr(x)
case *ast.ParenExpr:
return c.VisitExpr(x.X)
case *ast.TypeAssertExpr:
return c.VisitTypeAssertExpr(x)
case *ast.SliceExpr:
return c.VisitSliceExpr(x)
case *ast.FuncType:
return c.VisitFuncType(x)
case *ast.MapType:
return c.VisitMapType(x)
case *ast.ArrayType:
return c.VisitArrayType(x)
case *ast.StructType:
return c.VisitStructType(x)
case *ast.InterfaceType:
return c.VisitInterfaceType(x)
case *ast.Ident:
if x.Obj == nil {
x.Obj = c.LookupObj(x.Name)
}
return c.Resolve(x.Obj)
}
panic(fmt.Sprintf("Unhandled Expr node: %s", reflect.TypeOf(expr)))
}
// vim: set ft=go :