forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
1588 lines (1415 loc) · 42.7 KB
/
codegen.c
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "chibicc.h"
#define GP_MAX 6
#define FP_MAX 8
static FILE *output_file;
static int depth;
static char *argreg8[] = {"%dil", "%sil", "%dl", "%cl", "%r8b", "%r9b"};
static char *argreg16[] = {"%di", "%si", "%dx", "%cx", "%r8w", "%r9w"};
static char *argreg32[] = {"%edi", "%esi", "%edx", "%ecx", "%r8d", "%r9d"};
static char *argreg64[] = {"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"};
static Obj *current_fn;
static void gen_expr(Node *node);
static void gen_stmt(Node *node);
__attribute__((format(printf, 1, 2)))
static void println(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(output_file, fmt, ap);
va_end(ap);
fprintf(output_file, "\n");
}
static int count(void) {
static int i = 1;
return i++;
}
static void push(void) {
println(" push %%rax");
depth++;
}
static void pop(char *arg) {
println(" pop %s", arg);
depth--;
}
static void pushf(void) {
println(" sub $8, %%rsp");
println(" movsd %%xmm0, (%%rsp)");
depth++;
}
static void popf(int reg) {
println(" movsd (%%rsp), %%xmm%d", reg);
println(" add $8, %%rsp");
depth--;
}
// Round up `n` to the nearest multiple of `align`. For instance,
// align_to(5, 8) returns 8 and align_to(11, 8) returns 16.
int align_to(int n, int align) {
return (n + align - 1) / align * align;
}
static char *reg_dx(int sz) {
switch (sz) {
case 1: return "%dl";
case 2: return "%dx";
case 4: return "%edx";
case 8: return "%rdx";
}
unreachable();
}
static char *reg_ax(int sz) {
switch (sz) {
case 1: return "%al";
case 2: return "%ax";
case 4: return "%eax";
case 8: return "%rax";
}
unreachable();
}
// Compute the absolute address of a given node.
// It's an error if a given node does not reside in memory.
static void gen_addr(Node *node) {
switch (node->kind) {
case ND_VAR:
// Variable-length array, which is always local.
if (node->var->ty->kind == TY_VLA) {
println(" mov %d(%%rbp), %%rax", node->var->offset);
return;
}
// Local variable
if (node->var->is_local) {
println(" lea %d(%%rbp), %%rax", node->var->offset);
return;
}
if (opt_fpic) {
// Thread-local variable
if (node->var->is_tls) {
println(" data16 lea %s@tlsgd(%%rip), %%rdi", node->var->name);
println(" .value 0x6666");
println(" rex64");
println(" call __tls_get_addr@PLT");
return;
}
// Function or global variable
println(" mov %s@GOTPCREL(%%rip), %%rax", node->var->name);
return;
}
// Thread-local variable
if (node->var->is_tls) {
println(" mov %%fs:0, %%rax");
println(" add $%s@tpoff, %%rax", node->var->name);
return;
}
// Here, we generate an absolute address of a function or a global
// variable. Even though they exist at a certain address at runtime,
// their addresses are not known at link-time for the following
// two reasons.
//
// - Address randomization: Executables are loaded to memory as a
// whole but it is not known what address they are loaded to.
// Therefore, at link-time, relative address in the same
// exectuable (i.e. the distance between two functions in the
// same executable) is known, but the absolute address is not
// known.
//
// - Dynamic linking: Dynamic shared objects (DSOs) or .so files
// are loaded to memory alongside an executable at runtime and
// linked by the runtime loader in memory. We know nothing
// about addresses of global stuff that may be defined by DSOs
// until the runtime relocation is complete.
//
// In order to deal with the former case, we use RIP-relative
// addressing, denoted by `(%rip)`. For the latter, we obtain an
// address of a stuff that may be in a shared object file from the
// Global Offset Table using `@GOTPCREL(%rip)` notation.
// Function
if (node->ty->kind == TY_FUNC) {
if (node->var->is_definition)
println(" lea %s(%%rip), %%rax", node->var->name);
else
println(" mov %s@GOTPCREL(%%rip), %%rax", node->var->name);
return;
}
// Global variable
println(" lea %s(%%rip), %%rax", node->var->name);
return;
case ND_DEREF:
gen_expr(node->lhs);
return;
case ND_COMMA:
gen_expr(node->lhs);
gen_addr(node->rhs);
return;
case ND_MEMBER:
gen_addr(node->lhs);
println(" add $%d, %%rax", node->member->offset);
return;
case ND_FUNCALL:
if (node->ret_buffer) {
gen_expr(node);
return;
}
break;
case ND_VLA_PTR:
println(" lea %d(%%rbp), %%rax", node->var->offset);
return;
}
error_tok(node->tok, "not an lvalue");
}
// Load a value from where %rax is pointing to.
static void load(Type *ty) {
switch (ty->kind) {
case TY_ARRAY:
case TY_STRUCT:
case TY_UNION:
case TY_FUNC:
case TY_VLA:
// If it is an array, do not attempt to load a value to the
// register because in general we can't load an entire array to a
// register. As a result, the result of an evaluation of an array
// becomes not the array itself but the address of the array.
// This is where "array is automatically converted to a pointer to
// the first element of the array in C" occurs.
return;
case TY_FLOAT:
println(" movss (%%rax), %%xmm0");
return;
case TY_DOUBLE:
println(" movsd (%%rax), %%xmm0");
return;
case TY_LDOUBLE:
println(" fldt (%%rax)");
return;
}
char *insn = ty->is_unsigned ? "movz" : "movs";
// When we load a char or a short value to a register, we always
// extend them to the size of int, so we can assume the lower half of
// a register always contains a valid value. The upper half of a
// register for char, short and int may contain garbage. When we load
// a long value to a register, it simply occupies the entire register.
if (ty->size == 1)
println(" %sbl (%%rax), %%eax", insn);
else if (ty->size == 2)
println(" %swl (%%rax), %%eax", insn);
else if (ty->size == 4)
println(" movsxd (%%rax), %%rax");
else
println(" mov (%%rax), %%rax");
}
// Store %rax to an address that the stack top is pointing to.
static void store(Type *ty) {
pop("%rdi");
switch (ty->kind) {
case TY_STRUCT:
case TY_UNION:
for (int i = 0; i < ty->size; i++) {
println(" mov %d(%%rax), %%r8b", i);
println(" mov %%r8b, %d(%%rdi)", i);
}
return;
case TY_FLOAT:
println(" movss %%xmm0, (%%rdi)");
return;
case TY_DOUBLE:
println(" movsd %%xmm0, (%%rdi)");
return;
case TY_LDOUBLE:
println(" fstpt (%%rdi)");
return;
}
if (ty->size == 1)
println(" mov %%al, (%%rdi)");
else if (ty->size == 2)
println(" mov %%ax, (%%rdi)");
else if (ty->size == 4)
println(" mov %%eax, (%%rdi)");
else
println(" mov %%rax, (%%rdi)");
}
static void cmp_zero(Type *ty) {
switch (ty->kind) {
case TY_FLOAT:
println(" xorps %%xmm1, %%xmm1");
println(" ucomiss %%xmm1, %%xmm0");
return;
case TY_DOUBLE:
println(" xorpd %%xmm1, %%xmm1");
println(" ucomisd %%xmm1, %%xmm0");
return;
case TY_LDOUBLE:
println(" fldz");
println(" fucomip");
println(" fstp %%st(0)");
return;
}
if (is_integer(ty) && ty->size <= 4)
println(" cmp $0, %%eax");
else
println(" cmp $0, %%rax");
}
enum { I8, I16, I32, I64, U8, U16, U32, U64, F32, F64, F80 };
static int getTypeId(Type *ty) {
switch (ty->kind) {
case TY_CHAR:
return ty->is_unsigned ? U8 : I8;
case TY_SHORT:
return ty->is_unsigned ? U16 : I16;
case TY_INT:
return ty->is_unsigned ? U32 : I32;
case TY_LONG:
return ty->is_unsigned ? U64 : I64;
case TY_FLOAT:
return F32;
case TY_DOUBLE:
return F64;
case TY_LDOUBLE:
return F80;
}
return U64;
}
// The table for type casts
static char i32i8[] = "movsbl %al, %eax";
static char i32u8[] = "movzbl %al, %eax";
static char i32i16[] = "movswl %ax, %eax";
static char i32u16[] = "movzwl %ax, %eax";
static char i32f32[] = "cvtsi2ssl %eax, %xmm0";
static char i32i64[] = "movsxd %eax, %rax";
static char i32f64[] = "cvtsi2sdl %eax, %xmm0";
static char i32f80[] = "mov %eax, -4(%rsp); fildl -4(%rsp)";
static char u32f32[] = "mov %eax, %eax; cvtsi2ssq %rax, %xmm0";
static char u32i64[] = "mov %eax, %eax";
static char u32f64[] = "mov %eax, %eax; cvtsi2sdq %rax, %xmm0";
static char u32f80[] = "mov %eax, %eax; mov %rax, -8(%rsp); fildll -8(%rsp)";
static char i64f32[] = "cvtsi2ssq %rax, %xmm0";
static char i64f64[] = "cvtsi2sdq %rax, %xmm0";
static char i64f80[] = "movq %rax, -8(%rsp); fildll -8(%rsp)";
static char u64f32[] = "cvtsi2ssq %rax, %xmm0";
static char u64f64[] =
"test %rax,%rax; js 1f; pxor %xmm0,%xmm0; cvtsi2sd %rax,%xmm0; jmp 2f; "
"1: mov %rax,%rdi; and $1,%eax; pxor %xmm0,%xmm0; shr %rdi; "
"or %rax,%rdi; cvtsi2sd %rdi,%xmm0; addsd %xmm0,%xmm0; 2:";
static char u64f80[] =
"mov %rax, -8(%rsp); fildq -8(%rsp); test %rax, %rax; jns 1f;"
"mov $1602224128, %eax; mov %eax, -4(%rsp); fadds -4(%rsp); 1:";
static char f32i8[] = "cvttss2sil %xmm0, %eax; movsbl %al, %eax";
static char f32u8[] = "cvttss2sil %xmm0, %eax; movzbl %al, %eax";
static char f32i16[] = "cvttss2sil %xmm0, %eax; movswl %ax, %eax";
static char f32u16[] = "cvttss2sil %xmm0, %eax; movzwl %ax, %eax";
static char f32i32[] = "cvttss2sil %xmm0, %eax";
static char f32u32[] = "cvttss2siq %xmm0, %rax";
static char f32i64[] = "cvttss2siq %xmm0, %rax";
static char f32u64[] = "cvttss2siq %xmm0, %rax";
static char f32f64[] = "cvtss2sd %xmm0, %xmm0";
static char f32f80[] = "movss %xmm0, -4(%rsp); flds -4(%rsp)";
static char f64i8[] = "cvttsd2sil %xmm0, %eax; movsbl %al, %eax";
static char f64u8[] = "cvttsd2sil %xmm0, %eax; movzbl %al, %eax";
static char f64i16[] = "cvttsd2sil %xmm0, %eax; movswl %ax, %eax";
static char f64u16[] = "cvttsd2sil %xmm0, %eax; movzwl %ax, %eax";
static char f64i32[] = "cvttsd2sil %xmm0, %eax";
static char f64u32[] = "cvttsd2siq %xmm0, %rax";
static char f64i64[] = "cvttsd2siq %xmm0, %rax";
static char f64u64[] = "cvttsd2siq %xmm0, %rax";
static char f64f32[] = "cvtsd2ss %xmm0, %xmm0";
static char f64f80[] = "movsd %xmm0, -8(%rsp); fldl -8(%rsp)";
#define FROM_F80_1 \
"fnstcw -10(%rsp); movzwl -10(%rsp), %eax; or $12, %ah; " \
"mov %ax, -12(%rsp); fldcw -12(%rsp); "
#define FROM_F80_2 " -24(%rsp); fldcw -10(%rsp); "
static char f80i8[] = FROM_F80_1 "fistps" FROM_F80_2 "movsbl -24(%rsp), %eax";
static char f80u8[] = FROM_F80_1 "fistps" FROM_F80_2 "movzbl -24(%rsp), %eax";
static char f80i16[] = FROM_F80_1 "fistps" FROM_F80_2 "movzbl -24(%rsp), %eax";
static char f80u16[] = FROM_F80_1 "fistpl" FROM_F80_2 "movswl -24(%rsp), %eax";
static char f80i32[] = FROM_F80_1 "fistpl" FROM_F80_2 "mov -24(%rsp), %eax";
static char f80u32[] = FROM_F80_1 "fistpl" FROM_F80_2 "mov -24(%rsp), %eax";
static char f80i64[] = FROM_F80_1 "fistpq" FROM_F80_2 "mov -24(%rsp), %rax";
static char f80u64[] = FROM_F80_1 "fistpq" FROM_F80_2 "mov -24(%rsp), %rax";
static char f80f32[] = "fstps -8(%rsp); movss -8(%rsp), %xmm0";
static char f80f64[] = "fstpl -8(%rsp); movsd -8(%rsp), %xmm0";
static char *cast_table[][11] = {
// i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 f80
{NULL, NULL, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80}, // i8
{i32i8, NULL, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80}, // i16
{i32i8, i32i16, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80}, // i32
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, i64f32, i64f64, i64f80}, // i64
{i32i8, NULL, NULL, i32i64, NULL, NULL, NULL, i32i64, i32f32, i32f64, i32f80}, // u8
{i32i8, i32i16, NULL, i32i64, i32u8, NULL, NULL, i32i64, i32f32, i32f64, i32f80}, // u16
{i32i8, i32i16, NULL, u32i64, i32u8, i32u16, NULL, u32i64, u32f32, u32f64, u32f80}, // u32
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, u64f32, u64f64, u64f80}, // u64
{f32i8, f32i16, f32i32, f32i64, f32u8, f32u16, f32u32, f32u64, NULL, f32f64, f32f80}, // f32
{f64i8, f64i16, f64i32, f64i64, f64u8, f64u16, f64u32, f64u64, f64f32, NULL, f64f80}, // f64
{f80i8, f80i16, f80i32, f80i64, f80u8, f80u16, f80u32, f80u64, f80f32, f80f64, NULL}, // f80
};
static void cast(Type *from, Type *to) {
if (to->kind == TY_VOID)
return;
if (to->kind == TY_BOOL) {
cmp_zero(from);
println(" setne %%al");
println(" movzx %%al, %%eax");
return;
}
int t1 = getTypeId(from);
int t2 = getTypeId(to);
if (cast_table[t1][t2])
println(" %s", cast_table[t1][t2]);
}
// Structs or unions equal or smaller than 16 bytes are passed
// using up to two registers.
//
// If the first 8 bytes contains only floating-point type members,
// they are passed in an XMM register. Otherwise, they are passed
// in a general-purpose register.
//
// If a struct/union is larger than 8 bytes, the same rule is
// applied to the the next 8 byte chunk.
//
// This function returns true if `ty` has only floating-point
// members in its byte range [lo, hi).
static bool has_flonum(Type *ty, int lo, int hi, int offset) {
if (ty->kind == TY_STRUCT || ty->kind == TY_UNION) {
for (Member *mem = ty->members; mem; mem = mem->next)
if (!has_flonum(mem->ty, lo, hi, offset + mem->offset))
return false;
return true;
}
if (ty->kind == TY_ARRAY) {
for (int i = 0; i < ty->array_len; i++)
if (!has_flonum(ty->base, lo, hi, offset + ty->base->size * i))
return false;
return true;
}
return offset < lo || hi <= offset || ty->kind == TY_FLOAT || ty->kind == TY_DOUBLE;
}
static bool has_flonum1(Type *ty) {
return has_flonum(ty, 0, 8, 0);
}
static bool has_flonum2(Type *ty) {
return has_flonum(ty, 8, 16, 0);
}
static void push_struct(Type *ty) {
int sz = align_to(ty->size, 8);
println(" sub $%d, %%rsp", sz);
depth += sz / 8;
for (int i = 0; i < ty->size; i++) {
println(" mov %d(%%rax), %%r10b", i);
println(" mov %%r10b, %d(%%rsp)", i);
}
}
static void push_args2(Node *args, bool first_pass) {
if (!args)
return;
push_args2(args->next, first_pass);
if ((first_pass && !args->pass_by_stack) || (!first_pass && args->pass_by_stack))
return;
gen_expr(args);
switch (args->ty->kind) {
case TY_STRUCT:
case TY_UNION:
push_struct(args->ty);
break;
case TY_FLOAT:
case TY_DOUBLE:
pushf();
break;
case TY_LDOUBLE:
println(" sub $16, %%rsp");
println(" fstpt (%%rsp)");
depth += 2;
break;
default:
push();
}
}
// Load function call arguments. Arguments are already evaluated and
// stored to the stack as local variables. What we need to do in this
// function is to load them to registers or push them to the stack as
// specified by the x86-64 psABI. Here is what the spec says:
//
// - Up to 6 arguments of integral type are passed using RDI, RSI,
// RDX, RCX, R8 and R9.
//
// - Up to 8 arguments of floating-point type are passed using XMM0 to
// XMM7.
//
// - If all registers of an appropriate type are already used, push an
// argument to the stack in the right-to-left order.
//
// - Each argument passed on the stack takes 8 bytes, and the end of
// the argument area must be aligned to a 16 byte boundary.
//
// - If a function is variadic, set the number of floating-point type
// arguments to RAX.
static int push_args(Node *node) {
int stack = 0, gp = 0, fp = 0;
// If the return type is a large struct/union, the caller passes
// a pointer to a buffer as if it were the first argument.
if (node->ret_buffer && node->ty->size > 16)
gp++;
// Load as many arguments to the registers as possible.
for (Node *arg = node->args; arg; arg = arg->next) {
Type *ty = arg->ty;
switch (ty->kind) {
case TY_STRUCT:
case TY_UNION:
if (ty->size > 16) {
arg->pass_by_stack = true;
stack += align_to(ty->size, 8) / 8;
} else {
bool fp1 = has_flonum1(ty);
bool fp2 = has_flonum2(ty);
if (fp + fp1 + fp2 < FP_MAX && gp + !fp1 + !fp2 < GP_MAX) {
fp = fp + fp1 + fp2;
gp = gp + !fp1 + !fp2;
} else {
arg->pass_by_stack = true;
stack += align_to(ty->size, 8) / 8;
}
}
break;
case TY_FLOAT:
case TY_DOUBLE:
if (fp++ >= FP_MAX) {
arg->pass_by_stack = true;
stack++;
}
break;
case TY_LDOUBLE:
arg->pass_by_stack = true;
stack += 2;
break;
default:
if (gp++ >= GP_MAX) {
arg->pass_by_stack = true;
stack++;
}
}
}
if ((depth + stack) % 2 == 1) {
println(" sub $8, %%rsp");
depth++;
stack++;
}
push_args2(node->args, true);
push_args2(node->args, false);
// If the return type is a large struct/union, the caller passes
// a pointer to a buffer as if it were the first argument.
if (node->ret_buffer && node->ty->size > 16) {
println(" lea %d(%%rbp), %%rax", node->ret_buffer->offset);
push();
}
return stack;
}
static void copy_ret_buffer(Obj *var) {
Type *ty = var->ty;
int gp = 0, fp = 0;
if (has_flonum1(ty)) {
assert(ty->size == 4 || 8 <= ty->size);
if (ty->size == 4)
println(" movss %%xmm0, %d(%%rbp)", var->offset);
else
println(" movsd %%xmm0, %d(%%rbp)", var->offset);
fp++;
} else {
for (int i = 0; i < MIN(8, ty->size); i++) {
println(" mov %%al, %d(%%rbp)", var->offset + i);
println(" shr $8, %%rax");
}
gp++;
}
if (ty->size > 8) {
if (has_flonum2(ty)) {
assert(ty->size == 12 || ty->size == 16);
if (ty->size == 12)
println(" movss %%xmm%d, %d(%%rbp)", fp, var->offset + 8);
else
println(" movsd %%xmm%d, %d(%%rbp)", fp, var->offset + 8);
} else {
char *reg1 = (gp == 0) ? "%al" : "%dl";
char *reg2 = (gp == 0) ? "%rax" : "%rdx";
for (int i = 8; i < MIN(16, ty->size); i++) {
println(" mov %s, %d(%%rbp)", reg1, var->offset + i);
println(" shr $8, %s", reg2);
}
}
}
}
static void copy_struct_reg(void) {
Type *ty = current_fn->ty->return_ty;
int gp = 0, fp = 0;
println(" mov %%rax, %%rdi");
if (has_flonum(ty, 0, 8, 0)) {
assert(ty->size == 4 || 8 <= ty->size);
if (ty->size == 4)
println(" movss (%%rdi), %%xmm0");
else
println(" movsd (%%rdi), %%xmm0");
fp++;
} else {
println(" mov $0, %%rax");
for (int i = MIN(8, ty->size) - 1; i >= 0; i--) {
println(" shl $8, %%rax");
println(" mov %d(%%rdi), %%al", i);
}
gp++;
}
if (ty->size > 8) {
if (has_flonum(ty, 8, 16, 0)) {
assert(ty->size == 12 || ty->size == 16);
if (ty->size == 4)
println(" movss 8(%%rdi), %%xmm%d", fp);
else
println(" movsd 8(%%rdi), %%xmm%d", fp);
} else {
char *reg1 = (gp == 0) ? "%al" : "%dl";
char *reg2 = (gp == 0) ? "%rax" : "%rdx";
println(" mov $0, %s", reg2);
for (int i = MIN(16, ty->size) - 1; i >= 8; i--) {
println(" shl $8, %s", reg2);
println(" mov %d(%%rdi), %s", i, reg1);
}
}
}
}
static void copy_struct_mem(void) {
Type *ty = current_fn->ty->return_ty;
Obj *var = current_fn->params;
println(" mov %d(%%rbp), %%rdi", var->offset);
for (int i = 0; i < ty->size; i++) {
println(" mov %d(%%rax), %%dl", i);
println(" mov %%dl, %d(%%rdi)", i);
}
}
static void builtin_alloca(void) {
// Align size to 16 bytes.
println(" add $15, %%rdi");
println(" and $0xfffffff0, %%edi");
// Shift the temporary area by %rdi.
println(" mov %d(%%rbp), %%rcx", current_fn->alloca_bottom->offset);
println(" sub %%rsp, %%rcx");
println(" mov %%rsp, %%rax");
println(" sub %%rdi, %%rsp");
println(" mov %%rsp, %%rdx");
println("1:");
println(" cmp $0, %%rcx");
println(" je 2f");
println(" mov (%%rax), %%r8b");
println(" mov %%r8b, (%%rdx)");
println(" inc %%rdx");
println(" inc %%rax");
println(" dec %%rcx");
println(" jmp 1b");
println("2:");
// Move alloca_bottom pointer.
println(" mov %d(%%rbp), %%rax", current_fn->alloca_bottom->offset);
println(" sub %%rdi, %%rax");
println(" mov %%rax, %d(%%rbp)", current_fn->alloca_bottom->offset);
}
// Generate code for a given node.
static void gen_expr(Node *node) {
println(" .loc %d %d", node->tok->file->file_no, node->tok->line_no);
switch (node->kind) {
case ND_NULL_EXPR:
return;
case ND_NUM: {
switch (node->ty->kind) {
case TY_FLOAT: {
union { float f32; uint32_t u32; } u = { node->fval };
println(" mov $%u, %%eax # float %Lf", u.u32, node->fval);
println(" movq %%rax, %%xmm0");
return;
}
case TY_DOUBLE: {
union { double f64; uint64_t u64; } u = { node->fval };
println(" mov $%lu, %%rax # double %Lf", u.u64, node->fval);
println(" movq %%rax, %%xmm0");
return;
}
case TY_LDOUBLE: {
union { long double f80; uint64_t u64[2]; } u;
memset(&u, 0, sizeof(u));
u.f80 = node->fval;
println(" mov $%lu, %%rax # long double %Lf", u.u64[0], node->fval);
println(" mov %%rax, -16(%%rsp)");
println(" mov $%lu, %%rax", u.u64[1]);
println(" mov %%rax, -8(%%rsp)");
println(" fldt -16(%%rsp)");
return;
}
}
println(" mov $%ld, %%rax", node->val);
return;
}
case ND_NEG:
gen_expr(node->lhs);
switch (node->ty->kind) {
case TY_FLOAT:
println(" mov $1, %%rax");
println(" shl $31, %%rax");
println(" movq %%rax, %%xmm1");
println(" xorps %%xmm1, %%xmm0");
return;
case TY_DOUBLE:
println(" mov $1, %%rax");
println(" shl $63, %%rax");
println(" movq %%rax, %%xmm1");
println(" xorpd %%xmm1, %%xmm0");
return;
case TY_LDOUBLE:
println(" fchs");
return;
}
println(" neg %%rax");
return;
case ND_VAR:
gen_addr(node);
load(node->ty);
return;
case ND_MEMBER: {
gen_addr(node);
load(node->ty);
Member *mem = node->member;
if (mem->is_bitfield) {
println(" shl $%d, %%rax", 64 - mem->bit_width - mem->bit_offset);
if (mem->ty->is_unsigned)
println(" shr $%d, %%rax", 64 - mem->bit_width);
else
println(" sar $%d, %%rax", 64 - mem->bit_width);
}
return;
}
case ND_DEREF:
gen_expr(node->lhs);
load(node->ty);
return;
case ND_ADDR:
gen_addr(node->lhs);
return;
case ND_ASSIGN:
gen_addr(node->lhs);
push();
gen_expr(node->rhs);
if (node->lhs->kind == ND_MEMBER && node->lhs->member->is_bitfield) {
println(" mov %%rax, %%r8");
// If the lhs is a bitfield, we need to read the current value
// from memory and merge it with a new value.
Member *mem = node->lhs->member;
println(" mov %%rax, %%rdi");
println(" and $%ld, %%rdi", (1L << mem->bit_width) - 1);
println(" shl $%d, %%rdi", mem->bit_offset);
println(" mov (%%rsp), %%rax");
load(mem->ty);
long mask = ((1L << mem->bit_width) - 1) << mem->bit_offset;
println(" mov $%ld, %%r9", ~mask);
println(" and %%r9, %%rax");
println(" or %%rdi, %%rax");
store(node->ty);
println(" mov %%r8, %%rax");
return;
}
store(node->ty);
return;
case ND_STMT_EXPR:
for (Node *n = node->body; n; n = n->next)
gen_stmt(n);
return;
case ND_COMMA:
gen_expr(node->lhs);
gen_expr(node->rhs);
return;
case ND_CAST:
gen_expr(node->lhs);
cast(node->lhs->ty, node->ty);
return;
case ND_MEMZERO:
// `rep stosb` is equivalent to `memset(%rdi, %al, %rcx)`.
println(" mov $%d, %%rcx", node->var->ty->size);
println(" lea %d(%%rbp), %%rdi", node->var->offset);
println(" mov $0, %%al");
println(" rep stosb");
return;
case ND_COND: {
int c = count();
gen_expr(node->cond);
cmp_zero(node->cond->ty);
println(" je .L.else.%d", c);
gen_expr(node->then);
println(" jmp .L.end.%d", c);
println(".L.else.%d:", c);
gen_expr(node->els);
println(".L.end.%d:", c);
return;
}
case ND_NOT:
gen_expr(node->lhs);
cmp_zero(node->lhs->ty);
println(" sete %%al");
println(" movzx %%al, %%rax");
return;
case ND_BITNOT:
gen_expr(node->lhs);
println(" not %%rax");
return;
case ND_LOGAND: {
int c = count();
gen_expr(node->lhs);
cmp_zero(node->lhs->ty);
println(" je .L.false.%d", c);
gen_expr(node->rhs);
cmp_zero(node->rhs->ty);
println(" je .L.false.%d", c);
println(" mov $1, %%rax");
println(" jmp .L.end.%d", c);
println(".L.false.%d:", c);
println(" mov $0, %%rax");
println(".L.end.%d:", c);
return;
}
case ND_LOGOR: {
int c = count();
gen_expr(node->lhs);
cmp_zero(node->lhs->ty);
println(" jne .L.true.%d", c);
gen_expr(node->rhs);
cmp_zero(node->rhs->ty);
println(" jne .L.true.%d", c);
println(" mov $0, %%rax");
println(" jmp .L.end.%d", c);
println(".L.true.%d:", c);
println(" mov $1, %%rax");
println(".L.end.%d:", c);
return;
}
case ND_FUNCALL: {
if (node->lhs->kind == ND_VAR && !strcmp(node->lhs->var->name, "alloca")) {
gen_expr(node->args);
println(" mov %%rax, %%rdi");
builtin_alloca();
return;
}
int stack_args = push_args(node);
gen_expr(node->lhs);
int gp = 0, fp = 0;
// If the return type is a large struct/union, the caller passes
// a pointer to a buffer as if it were the first argument.
if (node->ret_buffer && node->ty->size > 16)
pop(argreg64[gp++]);
for (Node *arg = node->args; arg; arg = arg->next) {
Type *ty = arg->ty;
switch (ty->kind) {
case TY_STRUCT:
case TY_UNION:
if (ty->size > 16)
continue;
bool fp1 = has_flonum1(ty);
bool fp2 = has_flonum2(ty);
if (fp + fp1 + fp2 < FP_MAX && gp + !fp1 + !fp2 < GP_MAX) {
if (fp1)
popf(fp++);
else
pop(argreg64[gp++]);
if (ty->size > 8) {
if (fp2)
popf(fp++);
else
pop(argreg64[gp++]);
}
}
break;
case TY_FLOAT:
case TY_DOUBLE:
if (fp < FP_MAX)
popf(fp++);
break;
case TY_LDOUBLE:
break;
default:
if (gp < GP_MAX)
pop(argreg64[gp++]);
}
}
println(" mov %%rax, %%r10");
println(" mov $%d, %%rax", fp);
println(" call *%%r10");
println(" add $%d, %%rsp", stack_args * 8);
depth -= stack_args;
// It looks like the most significant 48 or 56 bits in RAX may
// contain garbage if a function return type is short or bool/char,
// respectively. We clear the upper bits here.
switch (node->ty->kind) {
case TY_BOOL:
println(" movzx %%al, %%eax");
return;
case TY_CHAR:
if (node->ty->is_unsigned)
println(" movzbl %%al, %%eax");
else
println(" movsbl %%al, %%eax");
return;
case TY_SHORT:
if (node->ty->is_unsigned)
println(" movzwl %%ax, %%eax");
else
println(" movswl %%ax, %%eax");
return;
}
// If the return type is a small struct, a value is returned
// using up to two registers.
if (node->ret_buffer && node->ty->size <= 16) {
copy_ret_buffer(node->ret_buffer);
println(" lea %d(%%rbp), %%rax", node->ret_buffer->offset);
}
return;
}
case ND_LABEL_VAL:
println(" lea %s(%%rip), %%rax", node->unique_label);
return;
case ND_CAS: {
gen_expr(node->cas_addr);
push();
gen_expr(node->cas_new);
push();
gen_expr(node->cas_old);
println(" mov %%rax, %%r8");
load(node->cas_old->ty->base);
pop("%rdx"); // new
pop("%rdi"); // addr
int sz = node->cas_addr->ty->base->size;
println(" lock cmpxchg %s, (%%rdi)", reg_dx(sz));
println(" sete %%cl");
println(" je 1f");
println(" mov %s, (%%r8)", reg_ax(sz));
println("1:");
println(" movzbl %%cl, %%eax");
return;
}
case ND_EXCH: {
gen_expr(node->lhs);
push();
gen_expr(node->rhs);
pop("%rdi");
int sz = node->lhs->ty->base->size;
println(" xchg %s, (%%rdi)", reg_ax(sz));
return;
}
}
switch (node->lhs->ty->kind) {
case TY_FLOAT:
case TY_DOUBLE: {