-
Notifications
You must be signed in to change notification settings - Fork 8
/
gen.go
761 lines (716 loc) · 15.4 KB
/
gen.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package main
import "unsafe"
import "runtime"
import "fmt"
import "strings"
var REGS = []string{"rdi", "rsi", "rdx", "rcx", "r8", "r9"}
var stackpos int
func emit(format string, args ...interface{}) {
emit_int("\t"+format, args...)
}
func emit_label(fmt string, args ...interface{}) {
emit_int(fmt, args...)
}
func emit_int(format string, args ...interface{}) {
code := fmt.Sprintf(format, args...)
pc, _, no, ok := runtime.Caller(3)
if !ok {
errorf("Unable to get caller")
}
details := runtime.FuncForPC(pc)
callerName := (strings.Split(details.Name(), "."))[1]
caller := fmt.Sprintf(" %s %d", callerName, no)
numSpaces := 27 - len(code)
printf("%s %*c %s\n", code, numSpaces, '#', caller)
}
func get_int_reg(ctype *Ctype, r byte) string {
assert(r == 'a' || r == 'c')
switch ctype.size {
case 1:
if r == 'a' {
return "al"
} else {
return "cl"
}
case 2:
if r == 'a' {
return "ax"
} else {
return "cx"
}
case 4:
if r == 'a' {
return "eax"
} else {
return "ecx"
}
case 8:
if r == 'a' {
return "rax"
} else {
return "rcx"
}
default:
errorf("Unknown data size: %s: %d", ctype, ctype.size)
}
return ""
}
func push_xmm(reg int) {
emit("sub $8, %%rsp")
emit("movsd %%xmm%d, (%%rsp)", reg)
stackpos += 8
}
func pop_xmm(reg int) {
emit("movsd (%%rsp), %%xmm%d", reg)
emit("add $8, %%rsp")
stackpos -= 8
assert(stackpos >= 8)
}
func push(reg string) {
emit("push %%%s", reg)
stackpos += 8
}
func pop(reg string) {
emit("pop %%%s", reg)
stackpos -= 8
assert(stackpos >= 8)
}
func emit_gload(ctype *Ctype, label string, off int) {
if ctype.typ == CTYPE_ARRAY {
if off != 0 {
emit("lea %s+%d(%%rip), %%rax", label, off)
} else {
emit("lea %s(%%rip), %%rax", label)
}
return
}
reg := get_int_reg(ctype, 'a')
if ctype.size < 4 {
emit("mov $0, %%eax")
}
if off != 0 {
emit("mov %s+%d(%%rip), %%%s", label, off, reg)
} else {
emit("mov %s(%%rip), %%%s", label, reg)
}
}
func emit_toint(ctype *Ctype) {
if !is_flotype(ctype) {
return
}
emit("cvttsd2si %%xmm0, %%eax")
}
func emit_todouble(ctype *Ctype) {
if is_flotype(ctype) {
return
}
emit("cvtsi2sd %%eax, %%xmm0")
}
func emit_lload(ctype *Ctype, off int) {
if ctype.typ == CTYPE_ARRAY {
emit("lea %d(%%rbp), %%rax", off)
} else if ctype.typ == CTYPE_FLOAT {
emit("cvtps2pd %d(%%rbp), %%xmm0", off)
} else if ctype.typ == CTYPE_DOUBLE || ctype.typ == CTYPE_LDOUBLE {
emit("movsd %d(%%rbp), %%xmm0", off)
} else {
reg := get_int_reg(ctype, 'a')
if ctype.size < 4 {
emit("mov $0, %%eax")
}
emit("mov %d(%%rbp), %%%s", off, reg)
}
}
func emit_gsave(varname string, ctype *Ctype, off int) {
assert(ctype.typ != CTYPE_ARRAY)
reg := get_int_reg(ctype, 'a')
if off != 0 {
emit("mov %%%s, %s+%d(%%rip)", reg, varname, off)
} else {
emit("mov %%%s, %s(%%rip)", reg, varname)
}
}
func emit_lsave(ctype *Ctype, off int) {
if ctype.typ == CTYPE_FLOAT {
push_xmm(0)
emit("cvtpd2ps %%xmm0, %%xmm0")
emit("movss %%xmm0, %d(%%rbp)", off)
pop_xmm(0)
} else if ctype.typ == CTYPE_DOUBLE || ctype.typ == CTYPE_LDOUBLE {
emit("movsd %%xmm0, %d(%%rbp)", off)
} else {
reg := get_int_reg(ctype, 'a')
emit("mov %%%s, %d(%%rbp)", reg, off)
}
}
func emit_assign_deref_int(ctype *Ctype, off int) {
emit("mov (%%rsp), %%rcx")
reg := get_int_reg(ctype, 'c')
if off != 0 {
emit("mov %%%s, %d(%%rax)", reg, off)
} else {
emit("mov %%%s, (%%rax)", reg)
}
pop("rax")
}
func emit_assign_deref(variable *Ast) {
push("rax")
emit_expr(variable.operand)
emit_assign_deref_int(variable.operand.ctype.ptr, 0)
}
func emit_pointer_arith(_ byte, left *Ast, right *Ast) {
emit_expr(left)
push("rax")
emit_expr(right)
size := left.ctype.ptr.size
if size > 1 {
emit("imul $%d, %%rax", size)
}
emit("mov %%rax, %%rcx")
pop("rax")
emit("add %%rcx, %%rax")
}
func emit_assign_struct_ref(struc *Ast, field *Ctype, off int) {
switch struc.typ {
case AST_LVAR:
emit_lsave(field, struc.loff+field.offset+off)
case AST_GVAR:
emit_gsave(struc.varname, field, field.offset+off)
case AST_STRUCT_REF:
emit_assign_struct_ref(struc.struc, field, off+struc.ctype.offset)
case AST_DEREF:
v := struc
push("rax")
emit_expr(v.operand)
emit_assign_deref_int(field, field.offset+off)
default:
errorf("internal error: %s", struc)
}
}
func emit_load_struct_ref(struc *Ast, field *Ctype, off int) {
switch struc.typ {
case AST_LVAR:
emit_lload(field, struc.loff+field.offset+off)
case AST_GVAR:
emit_gload(field, struc.glabel, field.offset+off)
case AST_STRUCT_REF:
emit_load_struct_ref(struc.struc, field, struc.ctype.offset+off)
case AST_DEREF:
emit_expr(struc.operand)
emit_load_deref(struc.ctype, field, field.offset+off)
default:
errorf("internal error: %s", struc)
}
}
func emit_assign(variable *Ast) {
if variable.typ == AST_DEREF {
emit_assign_deref(variable)
return
}
switch variable.typ {
case AST_DEREF:
emit_assign_deref(variable)
case AST_STRUCT_REF:
emit_assign_struct_ref(variable.struc, variable.ctype, 0)
case AST_LVAR:
emit_lsave(variable.ctype, variable.loff)
case AST_GVAR:
emit_gsave(variable.varname, variable.ctype, 0)
default:
errorf("internal error")
}
}
func emit_comp(inst string, ast *Ast) {
if is_flotype(ast.ctype) {
emit_expr(ast.left)
emit_todouble(ast.left.ctype)
push_xmm(0)
emit_expr(ast.right)
emit_todouble(ast.right.ctype)
pop_xmm(1)
emit("ucomisd %%xmm0, %%xmm1")
} else {
emit_expr(ast.left)
emit_toint(ast.left.ctype)
push("rax")
emit_expr(ast.right)
emit_toint(ast.right.ctype)
pop("rcx")
emit("cmp %%rax, %%rcx")
emit("%s %%al", inst)
emit("movzb %%al, %%eax")
}
}
func emit_bion_int_arith(ast *Ast) {
var op string
switch ast.typ {
case '+':
op = "add"
case '-':
op = "sub"
case '*':
op = "imul"
case '/':
break
default:
errorf("invalid operator '%d", ast.typ)
}
emit_expr(ast.left)
emit_toint(ast.left.ctype)
push("rax")
emit_expr(ast.right)
emit_toint(ast.right.ctype)
emit("mov %%rax, %%rcx")
pop("rax")
if ast.typ == '/' {
emit("mov $0, %%edx")
emit("idiv %%rcx")
} else {
emit("%s %%rcx, %%rax", op)
}
}
func emit_binop_float_arith(ast *Ast) {
var op string
switch ast.typ {
case '+':
op = "addsd"
case '-':
op = "subsd"
case '*':
op = "mulsd"
case '/':
op = "divsd"
default:
errorf("invalid operator '%d'", ast.typ)
}
emit_expr(ast.left)
emit_todouble(ast.left.ctype)
push_xmm(0)
emit_expr(ast.right)
emit_todouble(ast.right.ctype)
emit("movsd %%xmm0, %%xmm1")
pop_xmm(0)
emit("%s %%xmm1, %%xmm0", op)
}
func emit_load_convert(to *Ctype, from *Ctype) {
if is_flotype(to) {
emit_todouble(from)
} else {
emit_toint(from)
}
}
func emit_save_convert(to *Ctype, from *Ctype) {
if is_inttype(from) && to.typ == CTYPE_FLOAT {
emit("cvtsi2ss %%eax, %%xmm0")
} else if is_flotype(from) && to.typ == CTYPE_FLOAT {
emit("cvtpd2ps %%xmm0, %%xmm0")
} else if is_inttype(from) && (to.typ == CTYPE_DOUBLE || to.typ == CTYPE_LDOUBLE) {
emit("cvtsi2sd %%eax, %%xmm0")
} else if !(is_flotype(from) && (to.typ == CTYPE_DOUBLE || to.typ == CTYPE_LDOUBLE)) {
emit_load_convert(to, from)
}
}
func emit_binop(ast *Ast) {
if ast.typ == '=' {
emit_expr(ast.right)
emit_load_convert(ast.ctype, ast.right.ctype)
emit_assign(ast.left)
return
}
if ast.ctype.typ == CTYPE_PTR {
emit_pointer_arith(byte(ast.typ), ast.left, ast.right)
return
}
switch ast.typ {
case '<':
emit_comp("setl", ast)
return
case '>':
emit_comp("setg", ast)
return
case OP_EQ:
emit_comp("sete", ast)
return
case OP_GE:
emit_comp("setge", ast)
return
case OP_LE:
emit_comp("setle", ast)
return
case OP_NE:
emit_comp("setne", ast)
return
}
if is_inttype(ast.ctype) {
emit_bion_int_arith(ast)
} else if is_flotype(ast.ctype) {
emit_binop_float_arith(ast)
} else {
errorf("internal error")
}
}
func emit_inc_dec(ast *Ast, op string) {
emit_expr(ast.operand)
push("rax")
emit("%s $1, %%rax", op)
emit_assign(ast.operand)
pop("rax")
}
func emit_load_deref(result_type *Ctype, operand_type *Ctype, off int) {
if operand_type.typ == CTYPE_PTR &&
operand_type.ptr.typ == CTYPE_ARRAY {
return
}
if result_type.size < 4 {
emit("mov $0, %%ecx")
}
reg := get_int_reg(result_type, 'c')
if off != 0 {
emit("mov %d(%%rax), %%%s", off, reg)
} else {
emit("mov (%%rax), %%%s", reg)
}
emit("mov %%rcx, %%rax")
}
func get_arg_types(ast *Ast) []*Ctype {
var r []*Ctype
for i, v := range ast.args {
var ptype *Ctype
if len(ast.paramtypes) > i {
ptype = ast.paramtypes[i]
} else {
ptype = result_type('=', v.ctype, ctype_int)
}
r = append(r, ptype)
}
return r
}
func emit_expr(ast *Ast) {
switch ast.typ {
case AST_LITERAL:
switch ast.ctype.typ {
case CTYPE_CHAR:
emit("mov $%d, %%rax", ast.ival)
case CTYPE_INT:
emit("mov $%d, %%eax", ast.ival)
case CTYPE_LONG:
emit("mov $%d, %%rax", ast.ival)
case CTYPE_FLOAT, CTYPE_DOUBLE, CTYPE_LDOUBLE:
emit("movsd %s(%%rip), %%xmm0", ast.flabel)
default:
errorf("internal error")
}
case AST_STRING:
emit("lea %s(%%rip), %%rax", ast.slabel)
case AST_LVAR:
emit_lload(ast.ctype, ast.loff)
case AST_GVAR:
emit_gload(ast.ctype, ast.glabel, 0)
case AST_FUNCALL:
ireg := 0
xreg := 0
argtypes := get_arg_types(ast)
for _, v := range argtypes {
if is_flotype(v) {
if xreg > 0 {
push_xmm(xreg)
}
xreg++
} else {
push(REGS[ireg])
ireg++
}
}
for i, v := range ast.args {
emit_expr(v)
ptype := argtypes[i]
emit_save_convert(ptype, v.ctype)
if is_flotype(ptype) {
push_xmm(0)
} else {
push("rax")
}
}
ir := ireg
xr := xreg
var reversed_args []*Ctype
for i := len(argtypes) - 1; i >= 0; i-- {
reversed_args = append(reversed_args, argtypes[i])
}
for _, v := range reversed_args {
if is_flotype(v) {
xr--
pop_xmm(xr)
} else {
ir--
pop(REGS[ir])
}
}
emit("mov $%d, %%eax", xreg)
if stackpos%16 != 0 {
emit("sub $8, %%rsp")
}
emit("call %s", ast.fname)
if stackpos%16 != 0 {
emit("add $8, %%rsp")
}
for _, v := range reversed_args {
if is_flotype(v) {
if xreg != 1 {
xreg--
pop_xmm(xreg)
}
} else {
ireg--
pop(REGS[ireg])
}
}
if ast.ctype.typ == CTYPE_FLOAT {
emit("cvtps2pd %%xmm0, %%xmm0")
}
case AST_DECL:
if ast.declinit == nil {
return
}
if ast.declinit.typ == AST_INIT_LIST {
off := 0
for _, v := range ast.declinit.initlist {
elem := v
emit_expr(elem)
emit_lsave(elem.totype, ast.declvar.loff+off)
off += elem.totype.size
}
} else if ast.declvar.ctype.typ == CTYPE_ARRAY {
assert(ast.declinit.typ == AST_STRING)
var i int
for i, char := range ast.declinit.val {
emit("movb $%d, %d(%%rbp)", char, ast.declvar.loff+i)
}
emit("movb $0, %d(%%rbp)", ast.declvar.loff+i)
} else if ast.declinit.typ == AST_STRING {
emit_gload(ast.declinit.ctype, ast.declinit.slabel, 0)
emit_lsave(ast.declvar.ctype, ast.declvar.loff)
} else {
emit_expr(ast.declinit)
emit_lsave(ast.declvar.ctype, ast.declvar.loff)
}
case AST_ADDR:
switch ast.operand.typ {
case AST_LVAR:
emit("lea %d(%%rbp), %%rax", ast.operand.loff)
case AST_GVAR:
emit("lea %s(%%rip), %%rax", ast.operand.glabel)
default:
errorf("internal error")
}
case AST_DEREF:
emit_expr(ast.operand)
emit_load_deref(ast.ctype, ast.operand.ctype, 0)
case AST_IF, AST_TERNARY:
emit_expr(ast.cond)
ne := make_label()
emit("test %%rax, %%rax")
emit("je %s", ne)
emit_expr(ast.then)
if ast.els != nil {
end := make_label()
emit("jmp %s", end)
emit("%s:", ne)
emit_expr(ast.els)
emit("%s:", end)
} else {
emit("%s:", ne)
}
case AST_FOR:
if ast.init != nil {
emit_expr(ast.init)
}
begin := make_label()
end := make_label()
emit("%s:", begin)
if ast.cond != nil {
emit_expr(ast.cond)
emit("test %%rax, %%rax")
emit("je %s", end)
}
emit_expr(ast.body)
if ast.step != nil {
emit_expr(ast.step)
}
emit("jmp %s", begin)
emit("%s:", end)
case AST_RETURN:
if ast.retval != nil {
emit_expr(ast.retval)
emit_save_convert(ast.ctype, ast.retval.ctype)
}
emit("leave")
emit("ret")
break
case AST_COMPOUND_STMT:
for _, v := range ast.stmts {
emit_expr(v)
}
case AST_STRUCT_REF:
emit_load_struct_ref(ast.struc, ast.ctype, 0)
case OP_INC:
emit_inc_dec(ast, "add")
case OP_DEC:
emit_inc_dec(ast, "sub")
case '!':
emit_expr(ast.operand)
emit("cmp $0, %%rax")
emit("sete %%al")
emit("movzb %%al, %%eax")
case '&':
emit_expr(ast.left)
push("rax")
emit_expr(ast.right)
pop("rcx")
emit("and %%rcx, %%rax")
case '|':
emit_expr(ast.left)
push("rax")
emit_expr(ast.right)
pop("rcx")
emit("or %%rcx, %%rax")
case OP_LOGAND:
end := make_label()
emit_expr(ast.left)
emit("test %%rax, %%rax")
emit("mov $0, %%rax")
emit("je %s", end)
emit_expr(ast.right)
emit("test %%rax, %%rax")
emit("mov $0, %%rax")
emit("je %s", end)
emit("mov $1, %%rax")
emit("%s:", end)
case OP_LOGOR:
end := make_label()
emit_expr(ast.left)
emit("test %%rax, %%rax")
emit("mov $1, %%rax")
emit("jne %s", end)
emit_expr(ast.right)
emit("test %%rax, %%rax")
emit("mov $1, %%rax")
emit("jne %s", end)
emit("mov $0, %%rax")
emit("%s:", end)
default:
emit_binop(ast)
}
}
func emit_data_section() {
emit(".data")
for _, v := range gstrings {
emit_label("%s:", v.slabel)
emit(".string \"%s\"", quote_cstring(v.val))
}
for _, v := range flonums {
label := make_label()
v.flabel = label
emit_label("%s:", label)
up1 := unsafe.Pointer(&v.fval)
up2 := unsafe.Pointer(uintptr(up1) + 4) // 4 means the size of int32
i1 := *(*int32)(up1)
i2 := *(*int32)(up2)
emit(".long %d", i1)
emit(".long %d", i2)
}
}
func align(n int, m int) int {
rem := n % m
if rem == 0 {
return n
} else {
return n - rem + m
}
}
func emit_data_int(data *Ast) {
assert(data.ctype.typ != CTYPE_ARRAY)
switch data.ctype.size {
case 1:
emit(".byte %d", data.ival)
case 4:
emit(".long %d", data.ival)
case 8:
emit(".quad %d", data.ival)
default:
errorf("internal error")
}
}
func emit_data(v *Ast) {
emit_label(".global %s", v.declvar.varname)
emit_label("%s:", v.declvar.varname)
if v.declinit.typ == AST_INIT_LIST {
for _, v := range v.declinit.initlist {
emit_data_int(v)
}
return
}
assert(v.declinit.typ == AST_LITERAL && is_inttype(v.declinit.ctype))
emit_data_int(v.declinit)
}
func emit_bss(v *Ast) {
emit(".lcomm %s, %d", v.declvar.varname, v.declvar.ctype.size)
}
func emit_global_var(v *Ast) {
if v.declinit != nil {
emit_data(v)
} else {
emit_bss(v)
}
}
func emit_func_prologue(fn *Ast) {
emit(".text")
emit_label(".global %s\n", fn.fname)
emit_label("%s:", fn.fname)
push("rbp")
emit("mov %%rsp, %%rbp")
off := 0
ireg := 0
xreg := 0
for _, v := range fn.params {
if is_flotype(v.ctype) {
push_xmm(xreg)
xreg++
} else if v.ctype.typ == CTYPE_DOUBLE || v.ctype.typ == CTYPE_LDOUBLE {
push_xmm(xreg)
xreg++
} else {
push(REGS[ireg])
ireg++
}
off -= align(v.ctype.size, 8)
v.loff = off
}
localarea := 0
for _, v := range fn.localvars {
a := align(v.ctype.size, 8)
off -= a
v.loff = off
localarea -= a
}
if localarea != 0 {
emit("sub $%d, %%rsp", -localarea)
}
stackpos += -(off - 8)
}
func emit_func_epilogue() {
emit("leave")
emit("ret")
}
func emit_toplevel(v *Ast) {
stackpos = 0
if v.typ == AST_FUNC {
emit_func_prologue(v)
emit_expr(v.body)
emit_func_epilogue()
} else if v.typ == AST_DECL {
emit_global_var(v)
} else {
errorf("internal error")
}
}