-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgen_abi.go
357 lines (316 loc) · 8.32 KB
/
gen_abi.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
package main
/**
Intel® 64 and IA-32 Architectures Software Developer’s Manual
Combined Volumes: 1, 2A, 2B, 2C, 2D, 3A, 3B, 3C, 3D and 4
3.4.1.1 General-Purpose Registers in 64-Bit Mode
In 64-bit mode, there are 16 general purpose registers and the default operand size is 32 bits.
However, general-purpose registers are able to work with either 32-bit or 64-bit operands.
If a 32-bit operand size is specified: EAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP, R8D - R15D are available.
If a 64-bit operand size is specified: RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, R8-R15 are available.
R8D-R15D/R8-R15 represent eight new general-purpose registers.
All of these registers can be accessed at the byte, word, dword, and qword level.
REX prefixes are used to generate 64-bit operand sizes or to reference registers R8-R15.
*/
var retRegi [14]string = [14]string{
"rax",
"rbx",
"rcx",
"rdx",
"rdi",
"rsi",
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
}
var RegsForArguments [12]string = [12]string{
"rdi",
"rsi",
"rdx",
"rcx",
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
}
func (f *DeclFunc) prepare() Emitter {
var params []*ExprVariable
// prepend receiver to params
if f.receiver != nil {
params = []*ExprVariable{f.receiver}
for _, param := range f.params {
params = append(params, param)
}
} else {
params = f.params
}
var regIndex int
// offset for params and local variables
var offset int
var argRegisters []int
for _, param := range params {
var width int
switch param.getGtype().is24WidthType() {
case true:
width = 3
regIndex += width
offset -= IntSize * width
param.offset = offset
argRegisters = append(argRegisters, regIndex-1)
argRegisters = append(argRegisters, regIndex-2)
argRegisters = append(argRegisters, regIndex-3)
default:
width = 1
regIndex += width
offset -= IntSize * width
param.offset = offset
argRegisters = append(argRegisters, regIndex-width)
}
}
var localarea int
var i int
var lvar *ExprVariable
for i, lvar = range f.localvars {
if lvar.gtype == nil {
errorft(lvar.token(), "i=%d %s has nil gtype", i, lvar.varname)
}
size := lvar.gtype.getSize()
assert(size != 0, lvar.token(), "size should not be zero:%s", lvar.gtype.String())
loff := align(size, 8)
localarea -= loff
offset -= loff
lvar.offset = offset
//debugf(S("set offset %d to lvar %s, type=%s"), lvar.offset, lvar.varname, lvar.gtype)
}
return &funcPrologueEmitter{
token: f.token(),
symbol: f.getSymbol(),
argRegisters: argRegisters,
localvars: f.localvars,
localarea: localarea,
}
}
type funcPrologueEmitter struct {
token *Token
symbol string
argRegisters []int
localvars []*ExprVariable
localarea int
}
func (fe *funcPrologueEmitter) emit() {
setPos(fe.token)
emitWithoutIndent("%s:", fe.symbol)
emit("FUNC_PROLOGUE")
if len(fe.argRegisters) > 0 {
emit("# set params")
}
for _, regi := range fe.argRegisters {
emit("PUSH_ARG_%d", regi)
}
if len(fe.localvars) > 0 {
//emit("# Allocating stack for localvars len=%d", len(fe.localvars))
for i := len(fe.localvars) - 1; i >= 0; i-- {
lvar := fe.localvars[i]
emit("# offset %d variable \"%s\" %s", lvar.offset, lvar.varname, lvar.gtype.String())
}
var localarea int = -fe.localarea
emit("subq $%d, %%rsp # total stack size", localarea)
}
emitNewline()
}
func (call *IrCall) emit() {
receiver := call.receiver
var params []*ExprVariable
var numRegs int
if call.isInterfaceMethodCall {
emit("# emit interface method call")
params = call.icallee.params
// interface method call
receiver.emit()
emit("LOAD_8_BY_DEREF # dereference: convert an interface value to a concrete value")
emit("PUSH_8 # receiver")
numRegs = 1
emitCallInner(numRegs, call.args, params)
emit("POP_8 # funcref")
emit("call *%%rax")
} else {
emit("# emit static call: %s", call.symbol)
params = call.callee.params
if receiver != nil {
// method call of a dynamic type
receiver.emit()
emitPush(receiver.getGtype())
if receiver.getGtype().is24WidthType() {
numRegs = 3
} else {
numRegs = 1
}
}
emitCallInner(numRegs, call.args, params)
emit("FUNCALL %s", call.symbol)
}
emitNewline()
}
func emitCallInner(numRegs int, args []Expr, params []*ExprVariable) {
var variadicArgs []Expr // nil means there is no varadic in funcdecl
var arg Expr
var argIndex int
var param *ExprVariable
for argIndex, arg = range args {
if argIndex < len(params) {
param = params[argIndex]
if param.isVariadic {
if _, ok := arg.(*ExprVaArg); !ok {
variadicArgs = make([]Expr, 0, len(args) - argIndex)
}
}
}
if variadicArgs != nil {
variadicArgs = append(variadicArgs, arg)
continue
}
var doConvertToInterface bool
var fromGtype string
if arg.getGtype() != nil {
emit("# get fromGtype")
fromGtype = arg.getGtype().String()
}
emit("# from %s", fromGtype)
if param != nil {
emit("# has a corresponding param")
var fromGtype *Gtype
if arg.getGtype() != nil {
fromGtype = arg.getGtype()
emit("# fromGtype:%s", fromGtype.String())
}
var toGtype *Gtype
if param.getGtype() != nil {
toGtype = param.getGtype()
emit("# toGtype:%s", toGtype.String())
}
if toGtype != nil && toGtype.getKind() == G_INTERFACE && fromGtype != nil && fromGtype.getKind() != G_INTERFACE {
doConvertToInterface = true
}
}
emit("# arg %d, doConvertToInterface=%s, collectVariadicArgs=%s",
argIndex, bool2string(doConvertToInterface), bool2string(variadicArgs != nil))
if doConvertToInterface {
emit("# doConvertToInterface !!!")
emitConversionToInterface(arg)
} else {
arg.emit()
}
var width int
if doConvertToInterface || arg.getGtype().is24WidthType() {
emit("PUSH_24")
width = 3
} else {
emit("PUSH_8")
width = 1
}
numRegs += width
}
// check if callee has a variadic
// https://golang.org/ref/spec#Passing_arguments_to_..._parameters
// If f is invoked with no actual arguments for p, the value passed to p is nil.
if argIndex < len(params) - 1 && params[argIndex+1].isVariadic {
// pass nil as vaargs
variadicArgs = make([]Expr, 0, 0)
}
if variadicArgs != nil {
numRegs += 3 // pass one slice
emit("# pass variadic = true")
emit("# make an empty slice")
emit("LOAD_EMPTY_SLICE")
emit("PUSH_SLICE")
// pass a []interface{}
for _, varg := range variadicArgs {
emit("# emit variadic arg")
// conversion : var ifc = x
if varg.getGtype().getKind() == G_INTERFACE {
varg.emit()
} else {
emitConversionToInterface(varg)
}
emit("PUSH_INTERFACE")
emit("# calling append24")
emit("POP_TO_ARG_5 # ifc_c")
emit("POP_TO_ARG_4 # ifc_b")
emit("POP_TO_ARG_3 # ifc_a")
emit("POP_TO_ARG_2 # cap")
emit("POP_TO_ARG_1 # len")
emit("POP_TO_ARG_0 # ptr")
emit("FUNCALL %s", getFuncSymbol(IRuntimePath, "append24"))
emit("PUSH_SLICE")
}
}
emit("# numRegs=%d", numRegs)
for i := numRegs - 1; i >= 0; i-- {
if i >= len(RegsForArguments) {
errorft(args[0].token(), "too many arguments")
}
var j int = i
emit("POP_TO_ARG_%d", j)
}
}
func (stmt *StmtReturn) emit() {
if len(stmt.exprs) == 0 {
// return void
emit("LOAD_EMPTY_8")
stmt.emitDeferAndReturn()
return
}
if len(stmt.exprs) > 7 {
TBI(stmt.token(), "too many number of arguments")
}
var retRegiIndex int
if len(stmt.exprs) == 1 {
expr := stmt.exprs[0]
rettype := stmt.rettypes[0]
if rettype.getKind() == G_INTERFACE && expr.getGtype().getKind() != G_INTERFACE {
if expr.getGtype() == nil {
emit("LOAD_EMPTY_INTERFACE")
} else {
emitConversionToInterface(expr)
}
} else {
expr.emit()
if expr.getGtype() == nil && stmt.rettypes[0].getKind() == G_SLICE {
emit("LOAD_EMPTY_SLICE")
}
}
stmt.emitDeferAndReturn()
return
}
for i, rettype := range stmt.rettypes {
expr := stmt.exprs[i]
expr.emit()
// rettype := stmt.rettypes[i]
if expr.getGtype() == nil && rettype.getKind() == G_SLICE {
emit("LOAD_EMPTY_SLICE")
}
size := rettype.getSize()
if size < 8 {
size = 8
}
var num64bit int = size / 8 // @TODO odd size
for j := 0; j < num64bit; j++ {
reg := retRegi[num64bit-1-j]
emit("pushq %%%s", reg)
retRegiIndex++
}
}
for i := 0; i < retRegiIndex; i++ {
reg := retRegi[retRegiIndex-1-i]
emit("popq %%%s", reg)
}
stmt.emitDeferAndReturn()
}