-
Notifications
You must be signed in to change notification settings - Fork 20
/
gen_call.go
267 lines (227 loc) · 6.73 KB
/
gen_call.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
package main
type Emitter interface {
emit()
}
func (funcall *ExprFuncallOrConversion) getRettypes() []*Gtype {
if funcall.rel.gtype != nil {
// Conversion
return []*Gtype{funcall.rel.gtype}
}
return funcall.getFuncDef().rettypes
}
func (ast *ExprMethodcall) getUniqueName() string {
gtype := ast.receiver.getGtype()
return getMethodUniqueName(gtype, ast.fname)
}
func (methodCall *ExprMethodcall) getOrigType() *Gtype {
gtype := methodCall.receiver.getGtype()
assertNotNil(methodCall.receiver != nil, methodCall.token())
assertNotNil(gtype != nil, methodCall.tok)
assert(gtype.kind == G_NAMED || gtype.kind == G_POINTER || gtype.kind == G_INTERFACE, methodCall.tok, "method must be an interface or belong to a named type")
var typeToBeloing *Gtype
if gtype.kind == G_POINTER {
typeToBeloing = gtype.origType
assert(typeToBeloing != nil, methodCall.token(), "shoudl not be nil:%s", gtype.String())
} else {
typeToBeloing = gtype
}
assert(typeToBeloing.kind == G_NAMED, methodCall.tok, "method must belong to a named type")
origType := typeToBeloing.relation.gtype
assert(typeToBeloing.relation.gtype != nil, methodCall.token(), "origType should not be nil")
return origType
}
func (methodCall *ExprMethodcall) getRettypes() []*Gtype {
origType := methodCall.getOrigType()
if origType == nil {
errorft(methodCall.token(), "origType should not be nil")
}
if origType.getKind() == G_INTERFACE {
imethod, _ := imethodGet(origType.imethods, methodCall.fname)
return imethod.rettypes
} else {
funcref, ok := methodGet(origType.methods, methodCall.fname)
if !ok {
errorft(methodCall.token(), "method %s is not found in type %s", methodCall.fname, methodCall.receiver.getGtype().String())
}
return funcref.funcdef.rettypes
}
}
type IrInterfaceMethodCall struct {
receiver Expr
methodName identifier
args []Expr
callee *signature
}
func (methodCall *ExprMethodcall) interfaceMethodCall() Emitter {
iType := methodCall.getOrigType()
sig, ok := iType.imethods[methodCall.fname]
if !ok {
panic("signature not found in the interface")
}
call := &IrInterfaceMethodCall{
receiver: methodCall.receiver,
methodName: methodCall.fname,
args: methodCall.args,
callee:sig,
}
return call
}
func (methodCall *ExprMethodcall) dynamicTypeMethodCall() Emitter {
origType := methodCall.getOrigType()
funcref, ok := methodGet(origType.methods, methodCall.fname)
if !ok {
errorft(methodCall.token(), "method %s is not found in type %s", methodCall.fname, methodCall.receiver.getGtype().String())
}
name := methodCall.getUniqueName()
var staticCall Expr = &IrCall{
tok: methodCall.token(),
symbol: getFuncSymbol(funcref.funcdef.pkgPath, name),
callee: funcref.funcdef,
receiver: methodCall.receiver,
args: methodCall.args,
origExpr: methodCall,
}
return staticCall
}
func (methodCall *ExprMethodcall) emit() {
origType := methodCall.getOrigType()
var e Emitter
if origType.getKind() == G_INTERFACE {
e = methodCall.interfaceMethodCall()
} else {
e = methodCall.dynamicTypeMethodCall()
}
e.emit()
}
func (funcall *ExprFuncallOrConversion) getFuncDef() *DeclFunc {
relexpr := funcall.rel.expr
assert(relexpr != nil, funcall.token(), "relexpr should NOT be nil")
funcref, ok := relexpr.(*ExprFuncRef)
if !ok {
errorft(funcall.token(), "Compiler error: funcref is not *ExprFuncRef (%s)", funcall.fname)
}
assertNotNil(funcref.funcdef != nil, nil)
return funcref.funcdef
}
func (stmt *StmtGo) emit() {
call, ok := stmt.expr.(*ExprFuncallOrConversion)
if !ok {
errorft(stmt.expr.token(), "Unexpected expr for go routine")
}
declFunc := call.getFuncDef()
declFunc.emitLoadFuncRef() // load funcref
emit("movq %%rax, %%rsi # set task")
emit("leaq iruntime.mstart+0(%%rip), %%rax # load mstart")
emit("movq %%rax, %%rdi")
emit("call iruntime.startm")
}
func funcall2emitter(funcall *ExprFuncallOrConversion) Emitter {
assert(funcall.rel.expr != nil && funcall.rel.gtype == nil, funcall.token(), "this is conversion")
assert(funcall.getFuncDef() != nil, funcall.token(), "funcdef is nil")
declFunc := funcall.getFuncDef()
// check if it's a builtin function
switch declFunc {
case builtinDumpSlice:
arg := funcall.args[0]
return &builtinDumpSliceEmitter{
arg: arg,
}
case builtinDumpInterface:
arg := funcall.args[0]
return &builtinDumpInterfaceEmitter{
arg: arg,
}
case builtinAssertInterface:
arg := funcall.args[0]
return &builtinAssertInterfaceEmitter{
arg: arg,
}
case builtinAsComment:
arg := funcall.args[0]
return &builtinAsCommentEmitter{
arg: arg,
}
default:
return &IrCall{
tok: funcall.token(),
symbol: declFunc.getSymbol(),
callee: declFunc,
args: funcall.args,
origExpr: funcall,
}
}
}
func (funcall *ExprFuncallOrConversion) emit() {
e := funcall2emitter(funcall)
e.emit()
}
type IrCall struct {
// https://sourceware.org/binutils/docs-2.30/as/Symbol-Intro.html#Symbol-Intro
// A symbol is one or more characters chosen from the set of all letters (both upper and lower case), digits and the three characters ‘_.$’.
tok *Token
isInterfaceMethodCall bool
symbol string
icallee *signature
callee *DeclFunc
receiver Expr
args []Expr
origExpr Expr
}
func (ircall *IrCall) token() *Token {
return ircall.tok
}
func (ircall *IrCall) dump() {
ircall.origExpr.dump()
}
func (ircall *IrCall) getGtype() *Gtype {
return ircall.origExpr.getGtype()
}
type builtinDumpSliceEmitter struct {
arg Expr
}
func (em *builtinDumpSliceEmitter) emit() {
// @TODO Implement me
emit("# builtinDumpSliceEmitter is not implemented")
}
type builtinDumpInterfaceEmitter struct {
arg Expr
}
func (em *builtinDumpInterfaceEmitter) emit() {
// @TODO Implement me
emit("# builtinDumpInterfaceEmitter is not implemented")
}
type builtinAssertInterfaceEmitter struct {
arg Expr
}
func (em *builtinAssertInterfaceEmitter) emit() {
emit("# builtinAssertInterface")
labelEnd := makeLabel()
em.arg.emit() // rax=ptr, rbx=receverTypeId, rcx=dynamicTypeId
// (ptr != nil && rcx == nil) => Error
emit("CMP_NE_ZERO")
emit("cmpq $0, %%rax")
emit("je %s", labelEnd)
emit("movq %%rcx, %%rax")
emit("CMP_EQ_ZERO")
emit("cmpq $0, %%rax")
emit("je %s", labelEnd)
slabel := makeLabel()
emit(".data 0")
emitWithoutIndent("%s:", slabel)
emit(".string \"%s\"", "assertInterface failed")
emit(".text")
emit("leaq %s, %%rax", slabel)
emit("PUSH_8")
emit("POP_TO_ARG_0")
emit("FUNCALL %s", getFuncSymbol(IRuntimePath, "panic"))
emitWithoutIndent("%s:", labelEnd)
emitNewline()
}
type builtinAsCommentEmitter struct {
arg Expr
}
func (em *builtinAsCommentEmitter) emit() {
if stringLiteral, ok := em.arg.(*ExprStringLiteral); ok {
emitWithoutIndent("# %s", stringLiteral.val)
}
}