-
Notifications
You must be signed in to change notification settings - Fork 20
/
gen_load.go
424 lines (385 loc) · 11.2 KB
/
gen_load.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
// gen_load handles loading of expressions
package main
func (ast *ExprNumberLiteral) emit() {
emit("LOAD_NUMBER %d", ast.val)
}
func loadStructField(strct Expr, field *Gtype, offset int) {
strct = unwrapRel(strct)
emit("# loadStructField")
switch strct.(type) {
case *ExprVariable:
variable := strct.(*ExprVariable)
if field.getKind() == G_ARRAY {
variable.emitAddress(field.offset)
} else {
if variable.isGlobal {
emit("LOAD_8_FROM_GLOBAL %s, %d+%d", variable.globalSymbol(), field.offset, offset)
} else {
emit("LOAD_8_FROM_LOCAL %d+%d+%d", variable.offset, field.offset, offset)
}
}
case *ExprStructField: // strct.field.field
a := strct.(*ExprStructField)
strcttype := a.strct.getGtype().Underlying()
assert(strcttype.size > 0, a.token(), "struct size should be > 0")
field2 := strcttype.getField(a.fieldname)
loadStructField(a.strct, field2, offset+field.offset)
case *ExprIndex: // array[1].field
indexExpr := strct.(*ExprIndex)
indexExpr.emitOffsetLoad(offset + field.offset)
default:
// funcall().field
// methodcall().field
// *ptr.field
// (MyStruct{}).field
// (&MyStruct{}).field
TBI(strct.token(), "unable to handle %T", strct)
}
}
func (a *ExprStructField) emitAddress() {
strcttype := a.strct.getGtype().origType.relation.gtype
field := strcttype.getField(a.fieldname)
a.strct.emit()
emit("ADD_NUMBER %d", field.offset)
}
func (a *ExprStructField) emit() {
emit("# LOAD ExprStructField")
switch a.strct.getGtype().getKind() {
case G_POINTER: // pointer to struct
strcttype := a.strct.getGtype().origType.relation.gtype
field := strcttype.getField(a.fieldname)
a.strct.emit()
emit("ADD_NUMBER %d", field.offset)
switch field.is24WidthType() {
case true:
emit("LOAD_24_BY_DEREF")
default:
switch field.getKind() {
case G_BYTE:
emit("LOAD_1_BY_DEREF")
case G_UINT_16:
emit("LOAD_2_BY_DEREF")
default:
emit("LOAD_8_BY_DEREF")
}
}
case G_STRUCT:
strcttype := a.strct.getGtype().relation.gtype
assert(strcttype.size > 0, a.token(), "struct size should be > 0")
field := strcttype.getField(a.fieldname)
loadStructField(a.strct, field, 0)
default:
errorft(a.token(), "internal error: bad gtype %s", a.strct.getGtype().String())
}
}
func (e *ExprStructField) emitOffsetLoad(size int, offset int) {
strct := e.strct
strct = unwrapRel(strct)
vr, ok := strct.(*ExprVariable)
assert(ok, e.tok, "should be *ExprVariable")
assert(vr.gtype.kind == G_NAMED, e.tok, "expect G_NAMED, but got ", vr.gtype.String())
field := vr.gtype.relation.gtype.getField(e.fieldname)
vr.emitOffsetLoad(size, field.offset+offset)
}
func (ast *ExprVariable) emit() {
emit("# load variable \"%s\" %s", ast.varname, ast.getGtype().String())
if ast.isGlobal {
symbol := ast.globalSymbol()
if ast.gtype.getKind() == G_ARRAY {
ast.emitAddress(0)
} else if ast.getGtype().is24WidthType() {
emit("LOAD_24_FROM_GLOBAL %s", symbol)
} else if ast.getGtype().getSize() == 1 {
emit("LOAD_1_FROM_GLOBAL_CAST %s", symbol)
} else {
emit("LOAD_8_FROM_GLOBAL %s", symbol)
}
} else {
if ast.offset == 0 {
errorft(ast.token(), "offset should not be zero for localvar %s", ast.varname)
}
if ast.gtype.getKind() == G_ARRAY {
ast.emitAddress(0)
} else if ast.gtype.is24WidthType() {
emit("LOAD_24_FROM_LOCAL %d", ast.offset)
} else if ast.getGtype().getSize() == 1 {
emit("LOAD_1_FROM_LOCAL_CAST %d", ast.offset)
} else {
emit("LOAD_8_FROM_LOCAL %d", ast.offset)
}
}
}
func (variable *ExprVariable) emitAddress(offset int) {
if variable.isGlobal {
emit("LOAD_GLOBAL_ADDR %s, %d", variable.globalSymbol(), offset)
} else {
if variable.offset == 0 {
errorft(variable.token(), "offset should not be zero for localvar %s", variable.varname)
}
emit("LOAD_LOCAL_ADDR %d+%d", variable.offset, offset)
}
}
func (rel *Relation) emit() {
assert(rel.expr != nil, rel.token(), "rel.expr is nil")
rel.expr.emit()
}
func (ast *ExprConstVariable) emit() {
emit("# *ExprConstVariable.emit() name=%s iotaindex=%d", ast.name, ast.iotaIndex)
assert(ast.iotaIndex < 10000, ast.token(), "iotaindex is too large")
assert(ast.val != nil, ast.token(), "const.val for should not be nil:%s", ast.name)
if ast.hasIotaValue() {
emit("# const is iota")
val := &ExprNumberLiteral{
val: ast.iotaIndex,
}
val.emit()
} else {
emit("# const is not iota")
ast.val.emit()
}
}
func (ast *ExprUop) emit() {
operand := unwrapRel(ast.operand)
ast.operand = operand
emit("# emitting ExprUop")
op := string(ast.op)
switch op {
case "&":
switch ast.operand.(type) {
case *ExprVariable:
vr := ast.operand.(*ExprVariable)
vr.emitAddress(0)
case *ExprStructLiteral:
e := ast.operand.(*ExprStructLiteral)
assert(e.invisiblevar.offset != 0, nil, "ExprStructLiteral's invisible var has offset")
ivv := e.invisiblevar
assignToStruct(ivv, e)
emitCallMalloc(e.getGtype().getSize())
emit("PUSH_8") // to:ptr addr
e.invisiblevar.emitAddress(0)
emit("PUSH_8") // from:address of invisible var
emitCopyStructFromStack(e.getGtype().getSize())
// emit address
case *ExprStructField:
e := ast.operand.(*ExprStructField)
e.emitAddress()
case *ExprIndex:
e := ast.operand.(*ExprIndex)
emitAddress(e)
default:
errorft(ast.token(), "Unknown type: %T", ast.operand)
}
case "*":
ast.operand.emit()
emit("LOAD_8_BY_DEREF")
case "!":
ast.operand.emit()
emit("CMP_EQ_ZERO")
case "-":
// delegate to biop
// -(x) -> (-1) * (x)
left := &ExprNumberLiteral{val: -1}
binop := &ExprBinop{
op: "*",
left: left,
right: ast.operand,
}
binop.emit()
default:
errorft(ast.token(), "unable to handle uop %s", ast.op)
}
//debugf(S("end of emitting ExprUop"))
}
func (variable *ExprVariable) emitOffsetLoad(size int, offset int) {
assert(0 <= size && size <= 8, variable.token(), "invalid size")
if variable.isGlobal {
emit("LOAD_%d_FROM_GLOBAL %s %d", size, variable.globalSymbol(), offset)
} else {
emit("LOAD_%d_FROM_LOCAL %d+%d", size, variable.offset, offset)
}
}
// rax: address
// rbx: len
// rcx: cap
func (e *ExprSliceLiteral) emit() {
emit("# (*ExprSliceLiteral).emit()")
var length int = len(e.values)
//debugf(S("slice literal %s: underlyingarray size = %d (should be %d)"), e.getGtype(), e.gtype.getSize(), e.gtype.elementType.getSize() * length)
emitCallMalloc(e.gtype.getSize() * length)
emit("PUSH_8 # ptr")
for i, value := range e.values {
if e.gtype.elementType.getKind() == G_INTERFACE && value.getGtype().getKind() != G_INTERFACE {
emitConversionToInterface(value)
} else {
value.emit()
}
emit("popq %%r10 # ptr")
var baseOffset int = IntSize * 3 * i
if e.gtype.elementType.is24WidthType() {
emit("movq %%rax, %d+%d(%%r10)", baseOffset, offset0)
emit("movq %%rbx, %d+%d(%%r10)", baseOffset, offset8)
emit("movq %%rcx, %d+%d(%%r10)", baseOffset, offset16)
} else if e.gtype.elementType.getSize() <= 8 {
var offset int = IntSize * i
emit("movq %%rax, %d(%%r10)", offset)
} else {
TBI(e.token(), "ExprSliceLiteral emit")
}
emit("pushq %%r10 # ptr")
}
emit("popq %%rax # ptr")
emit("movq $%d, %%rbx # len", length)
emit("movq $%d, %%rcx # cap", length)
}
func emitAddress(e Expr) {
e = unwrapRel(e)
switch e.(type) {
case *ExprVariable:
e.(*ExprVariable).emitAddress(0)
case *ExprIndex:
e.(*ExprIndex).emitAddress()
default:
TBI(e.token(), "")
}
}
func emitOffsetLoad(lhs Expr, size int, offset int) {
lhs = unwrapRel(lhs)
emit("# emitOffsetLoad(offset %d)", offset)
switch lhs.(type) {
case *ExprVariable:
variable := lhs.(*ExprVariable)
variable.emitOffsetLoad(size, offset)
case *ExprStructField:
structfield := lhs.(*ExprStructField)
fieldType := structfield.getGtype()
if structfield.strct.getGtype().getKind() == G_POINTER {
structfield.strct.emit() // emit address of the struct
var sum int = fieldType.offset + offset
emit("# offset %d + %d = %d", fieldType.offset, offset, sum)
emit("ADD_NUMBER %d+%d", fieldType.offset, offset)
//reg := getReg(size)
emit("LOAD_8_BY_DEREF")
} else {
emitOffsetLoad(structfield.strct, size, fieldType.offset+offset)
}
case *ExprIndex:
// e.g. arrayLiteral.values[i].getGtype().getKind()
indexExpr := lhs.(*ExprIndex)
indexExpr.emitOffsetLoad(offset)
case *ExprMethodcall:
// @TODO this logic is temporarly. Need to be verified.
mcall := lhs.(*ExprMethodcall)
rettypes := mcall.getRettypes()
assert(len(rettypes) == 1, lhs.token(), "rettype should be single")
rettype := rettypes[0]
assert(rettype.getKind() == G_POINTER, lhs.token(), "only pointer is supported")
mcall.emit()
emit("ADD_NUMBER %d", offset)
emit("LOAD_8_BY_DEREF")
default:
errorft(lhs.token(), "unkonwn type %T", lhs)
}
}
func (e *ExprIndex) emitAddressOfArrayOrSliceIndex() {
collection := e.collection
index := e.index
elmType := collection.getGtype().Underlying().elementType
assert(elmType != nil, collection.token(), "elmType should not be nil")
elmSize := elmType.getSize()
assert(elmSize > 0, nil, "elmSize > 0")
collection.emit()
emit("PUSH_8 # head")
index.emit()
emit("IMUL_NUMBER %d", elmSize)
emit("PUSH_8 # index * elmSize")
emit("SUM_FROM_STACK # (index * elmSize) + head")
}
func (e *ExprIndex) loadArrayOrSliceIndex(offset int) {
elmType := e.collection.getGtype().Underlying().elementType
elmSize := elmType.getSize()
e.emitAddressOfArrayOrSliceIndex()
emit("ADD_NUMBER %d", offset)
// dereference the content of an emelment
if elmType.is24WidthType() {
emit("LOAD_24_BY_DEREF")
} else if elmSize == 1 {
emit("LOAD_1_BY_DEREF")
} else {
emit("LOAD_8_BY_DEREF")
}
}
func (e *ExprIndex) emitAddress() {
switch e.collection.getGtype().getKind() {
case G_ARRAY, G_SLICE:
e.emitAddressOfArrayOrSliceIndex()
default:
TBI(e.collection.token(), "")
}
}
func (e *ExprIndex) emitOffsetLoad(offset int) {
emit("# ExprIndex.emitOffsetLoad")
switch e.collection.getGtype().getKind() {
case G_ARRAY, G_SLICE, G_STRING:
e.loadArrayOrSliceIndex(offset)
return
case G_MAP:
loadMapIndexExpr(e)
default:
TBI(e.collection.token(), "unable to handle kind %d", e.collection.getGtype().String())
}
}
// Slice expressions construct a substring or slice from a string, array, pointer to array, or slice.
func (e *ExprSlice) emit() {
elmType := e.collection.getGtype().Underlying().elementType
assert(elmType != nil, e.token(), "type should not be nil:T %s", e.collection.getGtype().String())
size := elmType.getSize()
assert(size > 0, nil, "size > 0")
emit("# assign to a slice")
emit("# emit address of the array")
e.collection.emit()
emit("PUSH_8 # head of the array")
e.low.emit()
emit("PUSH_8 # low index")
emit("LOAD_NUMBER %d", size)
emit("PUSH_8")
emit("IMUL_FROM_STACK")
emit("PUSH_8")
emit("SUM_FROM_STACK")
emit("PUSH_8")
emit("# calc and set len")
if e.high == nil {
e.high = &ExprLen{
tok: e.token(),
arg: e.collection,
}
}
calcLen := &ExprBinop{
op: "-",
left: e.high,
right: e.low,
}
calcLen.emit()
emit("PUSH_8")
if e.collection.getGtype().isString() {
emit("PUSH_8")
} else {
emit("# calc and set cap")
var max Expr
if e.max != nil {
max = e.max
} else {
max = &ExprCap{
tok: e.token(),
arg: e.collection,
}
}
calcCap := &ExprBinop{
op: "-",
left: max,
right: e.low,
}
calcCap.emit()
emit("PUSH_8")
}
emit("POP_SLICE")
}