-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
3051 lines (2882 loc) · 96.6 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
#define _GNU_SOURCE // ask stdio to include asprintf
#include "codegen.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "platform.h"
/*
Think how we would compile the following programs
1) let main () = 42
2) let main () = let f x = x * x in f 5
3) let main () = let f x y = x + y in f 5 6
4) let f z = let g x = z * x in g let main () = f 5 5
1) # Probably something like this:
.text
.global _start
_start:
mov eax, 42
int h80
2)
_start:
mov eax, 5 # argument
call f # result in eax
int h80
main__f:
imul eax, eax
ret
3) # not sure.. something ugly I bet
*/
/*
Activation records:
|-----------------|
| Result | 0
|-----------------|
| &Closure | 1
|-----------------|
| Argument | 2
|-----------------|
| Local 1 | 3
|-----------------|
| Local 2 | 4
|-----------------|
| ... |
The closure for a function is just a struct containing the values
the function references from above it
*/
/*
Struct ABI (x86_64)
----------
For multi-word values (e.g. func objects), use the argument registers
to return the subsequent bytes
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| rax | rdi | rsi | rdx | rcx | r8 | r9 |
For storage in memory, words are ordered in increase offsets
For storage on the stack they are stored in decreasin offsets... (until we fix)
Struct ABI (arm)
---------
Use the arm convention of r0-r3 for arguments and return values
| r0 | r1 | r2 | r3 |
*/
/*
* Some constants
*/
#define ENTRY_SYMBOL "start"
#define PFX "codegen: "
#define EPFX "codegen: error: "
#define WORD_SIZE (sizeof(void*))
/*
* flag to enable or disable verbose debugging statements
*/
int debug_codegen = 0;
/*
* File to write assembly code to
*/
FILE* cgenout = NULL;
/*
* temp file where data segment is written until it is appended to the end
*/
FILE* dataout = NULL;
#ifdef _WIN32
char dataoutname[L_tmpnam];
#endif
typedef struct Function {
Symbol name;
TypeExpr* type; // Param Type = type->left, ReturnType = type->right
ParamList* closure;
struct Function* parent; // The enclosing function where this is
// defined
// Some temporary data for while we are walking the tree
// and working out activation records and generating code
int var_stack_start; // The index in the variable stack which
// divides the scope of the current function with
// it's parent
} Function;
#define MAX_FNS 1024
static Function function_table[MAX_FNS];
static int fn_table_count = 0;
/*
* A fifo of the function bodies so that we can emit them sequentially
* rather than nested
*/
typedef struct BodiesToEmit {
Function* function;
Expr* body;
} BodiesToEmit;
static BodiesToEmit bodies[MAX_FNS];
BodiesToEmit* bodies_begin = bodies;
BodiesToEmit* bodies_end = bodies;
#define VAR_MAX 1024
static struct Var {
Symbol name;
TypeExpr* type;
int var_id; // index into the variable_table where extra into stored
int arity;
Function* function; // can be NULL. ptr into function table for direct call
} variable_stack[VAR_MAX];
typedef struct Var Var;
/* points to next free slot in the variable stack */
static Var* var_stack_ptr = variable_stack;
static struct VarEx {
Symbol name;
TypeExpr* type;
Function* func; // Function where this is a local
int stack_offset; // offset from start of locals
int size; // size of space used on stack
} variable_table[VAR_MAX];
typedef struct VarEx VarEx;
static int var_table_count = 0;
#define TYPE_NAMES_MAX 1024
static struct TypeSize {
Symbol name;
size_t size;
} type_sizes[TYPE_NAMES_MAX];
static int type_sizes_count = 0;
static Declaration* tagged_decls[TYPE_NAMES_MAX];
static int tagged_decls_count = 0;
/* Some pre-declarations */
static size_t stack_size_of_type(TypeExpr* type);
static Function* add_function_w_parent(Symbol name, TypeExpr* type, Function* parent)
{
if (fn_table_count >= MAX_FNS) {
assert(0 && "shouldn't have made it this far...");
}
function_table[fn_table_count] = (Function){
.name = name, .type = type, .parent = parent,
.var_stack_start = var_stack_ptr - variable_stack
};
return &function_table[fn_table_count++];
}
static Function* add_function(Symbol name, TypeExpr* type)
{
return add_function_w_parent(name, type, NULL);
}
static void print_function_table(FILE* out)
{
fprintf(out, "function table:\n");
fprintf(out, "----------------------------------------\n");
Function* fn = function_table;
for (int i = 0; i < fn_table_count; i++, fn++) {
fprintf(out, "%s : ", fn->name);
print_typexpr(out, fn->type);
if (fn->parent) {
fprintf(out, " <- %s\n", fn->parent->name);
} else {
fputs("\n", out);
}
fprintf(out, "\tlocals1:\n");
for (int j = 0; j < var_table_count; j++) {
if (variable_table[j].func == fn) {
VarEx* v = &variable_table[j];
fprintf(out, "\t-%04x:%04x %s : ", -v->stack_offset, v->size, v->name);
print_typexpr(out, v->type);
fputs("\n", out);
}
}
fprintf(out, "\tclosure:\n");
for (ParamList* c = fn->closure; c; c = c->next) {
fprintf(out, "\t\t%s : ", c->param->name);
print_typexpr(out, c->param->type);
fputs("\n", out);
}
}
fprintf(out, "----------------------------------------\n");
}
static void push_var(Symbol name, TypeExpr* type, Function* thisfn)
{
assert(var_stack_ptr < variable_stack + VAR_MAX);
assert(type != NULL); // We should be fully typed at this point
// thisfn can be null
*var_stack_ptr++ = (Var){
.name = name, .type = type, .var_id = -1, .function = thisfn
};
}
static Var* push_var2(Symbol name, TypeExpr* type, Var* var_stack_ptr, _Bool arity)
{
assert(var_stack_ptr < variable_stack + VAR_MAX);
assert(type != NULL); // We should be fully typed at this point
*var_stack_ptr++ = (Var){
.name = name, .type = type, .var_id = -1, .arity = arity
};
return var_stack_ptr;
}
static Var* lookup_var(Symbol name, Var* var_stack_ptr)
{
Var* end = var_stack_ptr;
while (--end >= variable_stack) {
if (end->name == name) {
return end;
}
}
return NULL;
}
/*
* Find a variable declaration only in current function
*/
static Var* lookup_var_in_func(Function* func, Symbol name)
{
Var* begin = variable_stack + func->var_stack_start;
Var* end = var_stack_ptr;
while (--end >= begin) {
if (end->name == name) {
return end;
}
}
if (debug_codegen) {
fprintf(stderr, PFX"var %s not found in function %s\n", name, func->name);
fprintf(stderr, PFX"func->var_stack_start = %d\n", func->var_stack_start);
}
return NULL;
}
static void add_var_to_closure(Function* func, Symbol name, TypeExpr* type)
{
// first check if it's already in there.
// Note that the same name must be the same var in a given closure
for (ParamList* c = func->closure; c; c = c->next) {
if (c->param->name == name)
return;
}
func->closure = add_param(func->closure, param_with_type(name, type));
assert(func->parent); // must have parent as we've closed over something
if (!lookup_var_in_func(func->parent, name)) {
add_var_to_closure(func->parent, name, type);
}
}
static void add_var_to_locals(Function* func, Symbol name, TypeExpr* type, Function* thisfn)
{
assert(var_table_count < VAR_MAX);
//int var_id = var_table_count;
if (debug_codegen) {
fprintf(stderr, PFX"Adding local (%s : ", name);
print_typexpr(stderr, type);
fprintf(stderr, ") to function %s\n", func->name);
}
/*
* Iterate over other locals which can be in scope
*/
Var* begin = variable_stack + func->var_stack_start;
Var* end = var_stack_ptr;
int stack_offset = -WORD_SIZE // Saved base pointer value
// Result
- stack_size_of_type(func->type->right)
// &Closure
- WORD_SIZE;
for (Var* it = begin; it != end; ++it) {
stack_offset -= stack_size_of_type(it->type);
}
// point at the first word of the data
stack_offset -= (stack_size_of_type(type) - WORD_SIZE);
variable_table[var_table_count++] = (VarEx) {
.name = name,
.type = type,
.func = func,
.stack_offset = stack_offset,
.size = stack_size_of_type(type)
};
push_var(name, type, thisfn);
(var_stack_ptr - 1)->var_id = var_table_count - 1;
}
static void add_type_size(Symbol name, size_t size)
{
if (type_sizes_count >= TYPE_NAMES_MAX) {
fprintf(stderr, EPFX"too many types, > %d", TYPE_NAMES_MAX);
exit(EXIT_FAILURE);
}
type_sizes[type_sizes_count++] = (struct TypeSize){
.name = name,
.size = size
};
}
// These functions are designed to look a bit like ARM intructions so
// that we can easily port there
typedef const char* reg;
#if defined(__x86_64__)
static reg r0 = "%rax";
static reg r1 = "%rdi"; // first argument or second word of return
static reg r2 = "%rsi";
static reg r3 = "%rdx";
static reg t0 = "%r10";
static reg t1 = "%r11";
static reg sp = "%rsp";
static reg bp = "%rbp";
static reg cs0 = "%r12";
static reg cs1 = "%r13";
static reg cs2 = "%r14";
static reg cs3 = "%r15";
#elif defined(__arm__)
static reg r0 = "r0";
static reg r1 = "r1"; // first argument or second word of return
static reg r2 = "r2";
static reg r3 = "r3";
static reg t0 = "r4"; // we are saving this on stack in our fn prologue
static reg t1 = "ip";
static reg sp = "sp";
static reg bp = "fp";
static reg cs0 = "r5"; // callee-saved
static reg cs1 = "r6";
static reg cs2 = "r7";
static reg cs3 = "r8";
#elif defined(__arm64__) || defined(__aarch64__)
static reg r0 = "x0";
static reg r1 = "x1"; // first argument or second word of return
static reg r2 = "x2";
static reg r3 = "x3";
static reg t0 = "x16"; // inter procedure scratch registers aka x16, ip0
static reg t1 = "x17"; // aka ip1
static reg sp = "sp"; // aka x31
static reg bp = "fp"; // aka x29
static reg cs0 = "x19"; // callee-saved
static reg cs1 = "x20";
static reg cs2 = "x21";
static reg cs3 = "x22"; // x23 - x29 are also callee-saved
#else
#error "Unknown platform"
#endif
static inline void ins2(const char* instr, const char* lop, const char* rop)
{
fprintf(cgenout, "\t%s\t%s, %s\n", instr, lop, rop);
}
#if defined(__x86_64__)
static inline void ins2_imm(const char* instr, long long immval, const char* rop)
{
#ifdef _WIN32
fprintf(cgenout,"\t%s\t$%I64d, %s\n", instr, immval, rop);
#else
fprintf(cgenout,"\t%s\t$%lld, %s\n", instr, immval, rop);
#endif
}
#endif
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
static inline void ins2_imm_rtl(const char* instr, const char* lop, int immval)
{
fprintf(cgenout, " %s %s, #%d\n", instr, lop, immval);
}
static inline void ins3(
const char* instr, const char* dst, const char* lop, const char* rop)
{
fprintf(cgenout, " %s %s, %s, %s\n", instr, dst, lop, rop);
}
static inline void ins3_imm_rtl(
const char* instr, const char* dst, const char* lop, int immval)
{
fprintf(cgenout, " %s %s, %s, #%d\n", instr, dst, lop, immval);
}
#endif
#if defined(__x86_64__)
static inline void ins1(const char* instr, const char* op)
{
fprintf(cgenout, "\t%s\t%s\n", instr, op);
}
#endif
static void add(reg dst, reg op1, reg op2)
{
// FIXME: we are conflating the idea of the target architecture with the
// architecture of the machine the compiler is running on.
// Instead, we should either use a struct of function pointers, a runtime
// loaded shared object library (e.g. dll), static library (with choice
// when building), or just use c `if` statements instead of preprocessor
// `if` statements
#ifdef __x86_64__
if (dst == op1) {
ins2("addq", op2, dst); // left to right because we are AT&T syntax
} else if (dst == op2) {
ins2("addq", op1, dst);
} else { // what if dst == op2?
ins2("movq", op1, dst);
ins2("addq", op2, dst);
}
#else
ins3("add", dst, op1, op2);
#endif
}
static inline void add_imm(reg dst, long long amount)
{
#ifdef __x86_64__
ins2_imm("addq", amount, dst);
#elif defined(__arm__)
ins2_imm_rtl("add", dst, amount);
#elif defined(__arm64__) || defined(__aarch64__)
ins3_imm_rtl("add", dst, dst, amount);
#else
#error "Unknown platform"
#endif
}
static void mul(reg dst, reg op1, reg op2)
{
#ifdef __x86_64__
if (dst == op1) {
ins2("imulq", op2, dst);
} else if (dst == op2) {
ins2("imulq", op1, dst);
} else { // what if dst == op2?
ins2("movq", op1, dst);
ins2("imulq", op2, dst);
}
#else
ins3("mul", dst, op1, op2);
#endif
}
static void sub(reg dst, reg minuend, reg amount)
{
#ifdef __x86_64__
if (dst == minuend) {
ins2("subq", amount, dst);
} else if (dst == amount) {
ins2("subq", minuend, dst);
ins1("negq", dst);
} else {
ins2("movq", minuend, dst);
ins2("subq", amount, dst);
}
#else
ins3("sub", dst, minuend, amount);
#endif
}
static inline void sub_imm(reg dst, long long amount)
{
#ifdef __x86_64__
ins2_imm("subq", amount, dst);
#elif defined(__arm__)
ins2_imm_rtl("sub", dst, amount);
#elif defined(__arm64__) || defined(__aarch64__)
ins3_imm_rtl("sub", dst, dst, amount);
#else
#error "Unknown platform"
#endif
}
static void mov(reg dst, reg src);
static void sub_imm2(reg dst, reg minuend, int immediate)
{
#ifdef __x86_64__
if (dst == minuend) {
sub_imm(dst, immediate);
} else {
mov(dst, minuend);
sub_imm(dst, immediate);
}
#else
fprintf(cgenout, " sub %s, %s, #%d\n", dst, minuend, immediate);
#endif
}
// load but with a comment to help debugging
static void loadc(reg dst, reg src, int off, const char* comment)
{
#ifdef __x86_64__
fprintf(cgenout,"\tmovq\t%d(%s), %s", off, src, dst);
if (comment) fprintf(cgenout,"\t\t# %s\n", comment);
else fputs("\n", cgenout);
#elif defined(__arm__)
fprintf(cgenout, " ldr %s, [%s, #%d]", dst, src, off);
if (comment) fprintf(cgenout, "\t\t@@ %s\n", comment);
else fputs("\n", cgenout);
#elif defined(__APPLE__) && defined(__arm64__)
fprintf(cgenout, " ldr %s, [%s, #%d]", dst, src, off);
if (comment) fprintf(cgenout, "\t\t; %s\n", comment);
else fputs("\n", cgenout);
#elif defined(__linux__) && defined(__aarch64__)
fprintf(cgenout, " ldr %s, [%s, #%d]", dst, src, off);
if (comment) fprintf(cgenout, "\t\t// %s\n", comment);
else fputs("\n", cgenout);
#else
#error "Unknown platform"
#endif
}
static void load_regoff(reg dst, reg src, reg off, _Bool negoff)
{
#ifdef __x86_64__
char* sign = negoff ? "-1" : "1";
fprintf(cgenout," movq (%s, %s, %s), %s\n", off, src, sign, dst);
#else
char* sign = negoff ? "-" : "";
fprintf(cgenout, " ldr %s, [%s, %s%s]\n", dst, src, sign, off);
#endif
}
static void load(reg dst, reg src, int off)
{
loadc(dst, src, off, NULL);
}
static void load2(reg base, reg op0, reg op1)
{
#ifdef __x86_64__
load(op0, base, 0);
load(op1, base, WORD_SIZE);
#elif defined(__arm__)
fprintf(cgenout, " ldm %s, {%s,%s}\n", base, op0, op1);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, " ldp %s, %s, [%s]\n", op0, op1, base);
#else
#error "Unknown platform"
#endif
}
static void load3(reg base, reg op0, reg op1, reg op2)
{
#ifdef __x86_64__
load(op0, base, 0 * WORD_SIZE);
load(op1, base, 1 * WORD_SIZE);
load(op2, base, 2 * WORD_SIZE);
#elif defined(__arm__)
fprintf(cgenout, " ldm %s, {%s,%s,%s}\n", base, op0, op1, op2);
#elif defined(__arm64__) || defined(__aarch64__)
load2(base, op0, op1);
load(op2, base, 2 * WORD_SIZE);
#else
#error "Unknown platform"
#endif
}
static void load4(reg base, reg op0, reg op1, reg op2, reg op3)
{
#ifdef __x86_64__
load(op0, base, 0 * WORD_SIZE);
load(op1, base, 1 * WORD_SIZE);
load(op2, base, 2 * WORD_SIZE);
load(op3, base, 3 * WORD_SIZE);
#elif defined(__arm__)
fprintf(cgenout, " ldm %s, {%s,%s,%s,%s}\n", base, op0, op1, op2, op3);
#elif defined(__arm64__) || defined(__aarch64__)
load2(base, op0, op1);
fprintf(cgenout, " ldp %s, %s, [%s, #%lu]\n", op0, op1, base,
2 * WORD_SIZE);
#else
#error "Unknown platform"
#endif
}
static void store(int off, reg dst, reg src)
{
#ifdef __x86_64__
fprintf(cgenout,"\tmovq\t%s, %d(%s)\n", src, off, dst);
#elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
// TODO: need to handle case where offset is not a rotated byte
fprintf(cgenout, " str %s, [%s, #%d]\n", src, dst, off);
#else
#error "Unknown platform"
#endif
}
static void store2(reg base, reg op0, reg op1)
{
#ifdef __x86_64__
store(0 * WORD_SIZE, base, op0);
store(1 * WORD_SIZE, base, op1);
#elif defined(__arm__)
fprintf(cgenout, " stm %s, {%s, %s}\n", base, op0, op1);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, " stp %s, %s, [%s]\n", op0, op1, base);
#else
#error "Unknown platform"
#endif
}
static void store3(reg base, reg op0, reg op1, reg op2)
{
#ifdef __x86_64__
store(0 * WORD_SIZE, base, op0);
store(1 * WORD_SIZE, base, op1);
store(2 * WORD_SIZE, base, op2);
#elif defined(__arm__)
fprintf(cgenout, " stm %s, {%s, %s, %s}\n", base, op0, op1, op2);
#elif defined(__arm64__) || defined(__aarch64__)
store2(base, op0, op1);
store(2 * WORD_SIZE, base, op2);
#else
#error "Unknown platform"
#endif
}
static void store4(reg base, reg op0, reg op1, reg op2, reg op3)
{
#ifdef __x86_64__
store(0 * WORD_SIZE, base, op0);
store(1 * WORD_SIZE, base, op1);
store(2 * WORD_SIZE, base, op2);
store(3 * WORD_SIZE, base, op3);
#else
assert(0 && "TODO");
fprintf(cgenout, " stm %s, {%s, %s, %s, %s}\n",
base, op0, op1, op2, op3);
#endif
}
static void mov_imm(reg dst, long long intval)
{
#ifdef __x86_64__
ins2_imm("movq", intval, dst);
#else
// arm can only hold rotated single byte in immediate
// use ldr and = to have assembler pick automatically whether
// immediate or store in constant pool
char* s; asprintf(&s, "=%lld", intval);
ins2("ldr", dst, s);
free(s);
#endif
}
static void mov(reg dst, reg src)
{
#ifdef __x86_64__
ins2("movq", src, dst);
#else
ins2("mov", dst, src);
#endif
}
#ifdef __x86_64__
static void pop(reg dst)
{
#ifdef __x86_64__
ins1("popq", dst);
#elif defined(__arm__)
fprintf(cgenout, "\tpop\t{%s}\n", dst);
#elif defined(__arm64__) || defined(__aarch64__)
#error "No unaligned pop on arm64"
#else
#error "Unknown platform"
#endif
}
#endif
static void pop_aligned(reg dst)
{
#ifdef __x86_64__
assert(! "TODO");
#elif defined(__arm__)
fprintf(cgenout, "\tpop\t{%s}\n", dst);
add_imm(sp, WORD_SIZE);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tldr\t%s, [sp], #16\n", dst);
#else
#error "Unknown platform"
#endif
}
static void pop2(reg dst0, reg dst1)
{
#ifdef __x86_64__
pop(dst0); pop(dst1);
#elif defined(__arm__)
fprintf(cgenout, "\tpop\t{%s,%s}\n", dst0, dst1);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tldp\t%s, %s, [sp], #16\n", dst0, dst1);
#else
#error "Unknown platform"
#endif
}
/* opposite of push4 */
static void pop4(reg dst0, reg dst1, reg dst2, reg dst3)
{
#ifdef __x86_64__
pop(dst0); pop(dst1); pop(dst2); pop(dst3);
#elif defined(__arm__)
fprintf(cgenout, "\tpop\t{%s,%s,%s,%s}\n", dst0, dst1, dst2, dst3);
#elif defined(__arm64__) || defined(__aarch64__)
// I'm not sure about this...
pop2(dst2, dst3);
pop2(dst0, dst1);
#else
#error "Unknown platform"
#endif
}
/* equivalent of pop(dst1),...,pop(dst0) */
static void pop2_rev(reg dst0, reg dst1)
{
#ifdef __x86_64__
pop(dst1); pop(dst0);
#elif defined(__arm__)
load(dst1, sp, 0 * WORD_SIZE);
load(dst0, sp, 1 * WORD_SIZE);
add_imm(sp, 2 * WORD_SIZE);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tldp\t%s, %s, [sp], #16\n", dst1, dst0);
#else
#error "Unknown platform"
#endif
}
/* opposite of push4_rev */
/* equivalent of pop(dst3),...,pop(dst0) */
static void pop4_rev(reg dst0, reg dst1, reg dst2, reg dst3)
{
#ifdef __x86_64__
pop(dst3); pop(dst2); pop(dst1); pop(dst0);
#elif defined(__arm__)
load(dst3, sp, 0 * WORD_SIZE);
load(dst2, sp, 1 * WORD_SIZE);
load(dst1, sp, 2 * WORD_SIZE);
load(dst0, sp, 3 * WORD_SIZE);
add_imm(sp, 4 * WORD_SIZE);
#elif defined(__arm64__) || defined(__aarch64__)
// I'm not sure about this...
pop2_rev(dst2, dst3);
pop2_rev(dst0, dst2);
#else
#error "Unknown platform"
#endif
}
#ifdef __x86_64__
static void push(reg op0)
{
#ifdef __x86_64__
ins1("pushq", op0);
#elif defined(__arm__)
fprintf(cgenout, "\tpush\t{%s}\n", op0);
#elif defined(__arm64__) || defined(__aarch64__)
#error "No unaligned push on arm64"
#else
#error "Unknown platform"
#endif
}
#endif
static void push_aligned(reg op0)
{
#ifdef __x86_64__
assert(! "TODO");
#elif defined(__arm__)
sub_imm(sp, WORD_SIZE);
fprintf(cgenout, "\tpush\t{%s}\n", op0);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tstr\t%s, [sp, #-16]!\n", op0);
#else
#error "Unknown platform"
#endif
}
/* equivalent to push(op1) and then push(op0) */
static void push2(reg op0, reg op1)
{
#ifdef __x86_64__
push(op1); push(op0);
#elif defined(__arm__)
fprintf(cgenout, "\tpush\t{%s,%s}\n", op0, op1);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tstp\t%s, %s, [sp, #-16]!\n", op0, op1);
#else
#error "Unknown platform"
#endif
}
/* equivalent to push(op3)...push(op0) */
static void push4(reg op0, reg op1, reg op2, reg op3)
{
#ifdef __x86_64__
push(op3); push(op2); push(op1); push(op0);
#elif defined(__arm__)
fprintf(cgenout, "\tpush\t{%s,%s,%s,%s}\n", op0, op1, op2, op3);
#elif defined(__arm64__) || defined(__aarch64__)
// I'm not sure about this...
push2(op2, op3);
push2(op0, op1);
#else
#error "Unknown platform"
#endif
}
/* equivalent to push(op0), push(op1) */
static void push2_rev(reg op0, reg op1)
{
#ifdef __x86_64__
push(op0); push(op1);
#elif defined(__arm__)
// Cannot use single instruction since registers are not
// in order
sub_imm(sp, 2 * WORD_SIZE);
store(0 * WORD_SIZE, sp, op1);
store(1 * WORD_SIZE, sp, op0);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tstp\t%s, %s, [sp, #-16]!\n", op1, op0);
#else
#error "Unknown platform"
#endif
}
/* equivalent to push(op0)...push(op3) */
static void push4_rev(reg op0, reg op1, reg op2, reg op3)
{
#ifdef __x86_64__
push(op0); push(op1); push(op2); push(op3);
#elif defined(__arm__)
// We use multiple instructions since the register numbers
// are not in order
sub_imm(sp, 4 * WORD_SIZE);
store(0 * WORD_SIZE, sp, op3);
store(1 * WORD_SIZE, sp, op2);
store(2 * WORD_SIZE, sp, op1);
store(3 * WORD_SIZE, sp, op0);
#elif defined(__arm64__) || defined(__aarch64__)
push2_rev(op0, op1);
push2_rev(op2, op3);
#else
#error "Unknown platform"
#endif
}
static void cmp(reg left, reg right)
{
#ifdef __x86_64__
ins2("cmpq", right, left); // do these
#else
ins2("cmp", left, right); // do these
#endif
}
static void cmp_imm(reg left, long long imm)
{
#ifdef __x86_64__
ins2_imm("cmpq", imm, left);
#else
ins2_imm_rtl("cmp", left, imm);
#endif
}
/*
* Temporary labels look like L0, L1, L2
*/
static int request_label()
{
static int next_label = 0;
return next_label++;
}
static void label(int label_number)
{
fprintf(cgenout, "L%d:\n", label_number);
}
static void load_label(reg dst, const char* lbl)
{
#ifdef __x86_64__
fprintf(cgenout, "\tleaq\t%s(%s), %s\n", lbl, "%rip", dst);
#elif defined(__arm__)
fprintf(cgenout, " ldr %s, =%s\n", dst, lbl);
#elif defined(__arm64__)
// Maybe adr would work ok too ...
fprintf(cgenout, " adrp %s, %s@PAGE\n", dst, lbl);
fprintf(cgenout, " add %s, %s, %s@PAGEOFF\n", dst, dst, lbl);
#elif defined(__aarch64__)
fprintf(cgenout, " adrp %s, %s\n", dst, lbl);
fprintf(cgenout, " add %s, %s, :lo12:%s\n", dst, dst, lbl);
#else
fprintf(cgenout, " adr %s, %s\n", dst, lbl);
#error "Unknown platform"
#endif
}
static void load_tmplabel(reg dst, int label_number)
{
char lbl[12] = {}; // 1 byte for L, 10 for largest possible unsigned, 1 for \0
snprintf(lbl, 12, "L%u", (unsigned)label_number);
load_label(dst, lbl);
}
static void beq(int label) // branch equal
{
#ifdef __x86_64__
fprintf(cgenout, "\tje\tL%d\n", label);
#elif defined(__arm__)
fprintf(cgenout, "\tbeq\tL%d\n", label);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tb.eq\tL%d\n", label);
#else
#error "Unknown platform"
#endif
}
static void bne(int label) // branch equal
{
#ifdef __x86_64__
fprintf(cgenout, "\tjne\tL%d\n", label);
#elif defined(__arm__)
fprintf(cgenout, "\tbne\tL%d\n", label);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tb.ne\tL%d\n", label);
#else
#error "Unknown platform"
#endif
}
static void bgt(int label) // branch equal
{
#ifdef __x86_64__
fprintf(cgenout, "\tjg\tL%d\n", label);
#elif defined(__arm__)
fprintf(cgenout, "\tbgt\tL%d\n", label);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tb.gt\tL%d\n", label);
#else
#error "Unknown platform"
#endif
}
static void bge(int label) // branch equal
{
#ifdef __x86_64__
fprintf(cgenout, "\tjge\tL%d\n", label);
fprintf(cgenout, "\tbge\tL%d\n", label);
#elif defined(__arm__)
fprintf(cgenout, "\tbge\tL%d\n", label);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tb.ge\tL%d\n", label);
#else
#error "Unknown platform"
#endif
}
static void b(int label)
{
#ifdef __x86_64__
fprintf(cgenout, "\tjmp\tL%d\n", label);
#elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, "\tb\tL%d\n", label);
#else
#error "Unknown platform"
#endif
}
// These work fine on arm but not on x86
#ifdef __arm__
static void moveq_imm(reg dst, int intval)
{
ins2_imm_rtl("moveq", dst, intval);
}
static void movne_imm(reg dst, int intval)
{
ins2_imm_rtl("movne", dst, intval);
}
#endif // __arm__
static void movne(reg dst, reg src)
{
#ifdef __x86_64__
ins2("cmovneq", src, dst);
#elif defined(__arm__)
ins2("movne", dst, src);
#elif defined(__arm64__) || defined(__aarch64__)
fprintf(cgenout, " csel %s, %s, %s, ne\n", dst, src, dst);