-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgen_builtin.go
125 lines (118 loc) · 2.91 KB
/
gen_builtin.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
package main
import "fmt"
func (e *ExprLen) emit() {
emit("# emit len()")
arg := unwrapRel(e.arg)
gtype := arg.getGtype()
assert(gtype != nil, e.token(), "gtype should not be nil:\n")
switch gtype.getKind() {
case G_ARRAY:
emit("LOAD_NUMBER %d", gtype.length)
case G_SLICE, G_STRING:
emit("# len(slice|string)")
switch arg.(type) {
case *ExprStringLiteral:
sLiteral := arg.(*ExprStringLiteral)
length := countStrlen(sLiteral.val)
emit("LOAD_NUMBER %d", length)
case *ExprVariable, *ExprStructField, *ExprIndex:
emitOffsetLoad(arg, 8, ptrSize)
case *ExprSliceLiteral:
emit("# ExprSliceLiteral")
_arg := arg.(*ExprSliceLiteral)
var length int = len(_arg.values)
emit("LOAD_NUMBER %d", length)
case *ExprSlice:
sliceExpr := arg.(*ExprSlice)
uop := &ExprBinop{
op: "-",
left: sliceExpr.high,
right: sliceExpr.low,
}
uop.emit()
case *IrExprConversion:
conv := arg.(*IrExprConversion)
e.arg = conv.arg
e.emit()
default:
bs := fmt.Sprintf("unable to handle %T", arg)
TBI(arg.token(), string(bs))
}
case G_MAP:
emit("# emit len(map)")
arg.emit()
// if not nil
// then 0
// else len
labelNil := makeLabel()
labelEnd := makeLabel()
emit("cmpq $0, %%rax # map && map (check if map is nil)")
emit("je %s # jump if map is nil", labelNil)
// not nil case
emit("movq 8(%%rax), %%rax # load map len")
emit("jmp %s", labelEnd)
// nil case
emit("%s:", labelNil)
emit("LOAD_NUMBER 0")
emit("%s:", labelEnd)
default:
TBI(arg.token(), "unable to handle %s", gtype)
}
}
type IrLowLevelCall struct {
token *Token
symbol string
argsFromStack int // args are taken from the stack
}
func (e *IrLowLevelCall) emit() {
var i int
for i = e.argsFromStack - 1; i >= 0; i-- {
emit("POP_TO_ARG_%d", i)
}
emit("FUNCALL %s", e.symbol)
}
func (e *ExprCap) emit() {
emit("# emit cap()")
arg := unwrapRel(e.arg)
gtype := arg.getGtype()
switch gtype.getKind() {
case G_ARRAY:
emit("LOAD_NUMBER %d", gtype.length)
case G_SLICE:
switch arg.(type) {
case *ExprVariable, *ExprStructField, *ExprIndex:
emitOffsetLoad(arg, 8, ptrSize*2)
case *ExprSliceLiteral:
emit("# ExprSliceLiteral")
_arg := arg.(*ExprSliceLiteral)
var length int = len(_arg.values)
emit("LOAD_NUMBER %d", length)
case *ExprSlice:
sliceExpr := arg.(*ExprSlice)
if sliceExpr.collection.getGtype().getKind() == G_ARRAY {
cp := &ExprBinop{
tok: e.tok,
op: "-",
left: &ExprLen{
tok: e.tok,
arg: sliceExpr.collection,
},
right: sliceExpr.low,
}
cp.emit()
} else {
TBI(arg.token(), "unable to handle %T", arg)
}
case *IrExprConversion:
conv := arg.(*IrExprConversion)
e.arg = conv.arg
e.emit()
default:
TBI(arg.token(), "unable to handle %T", arg)
}
case G_MAP:
errorft(arg.token(), "invalid argument for cap")
default:
TBI(e.token(), "unable to handle %s", gtype.String())
}
}