-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirX64.c
1603 lines (1453 loc) · 50.8 KB
/
CirX64.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 "cir_internal.h"
#include <sys/mman.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#define GLOBAL_MEM_SIZE (1024 * 1024 * 1)
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#define REG_RAX 0
#define REG_RCX 1
#define REG_RDX 2
#define REG_RBX 3
#define REG_RSP 4
#define REG_RBP 5
#define REG_RSI 6
#define REG_RDI 7
#define REG_R8 8
#define REG_R9 9
#define REG_R10 10
#define REG_R11 11
#define REG_R12 12
#define REG_R13 13
#define REG_R14 14
#define REG_R15 15
// Cond
#define COND_B 0x02 // Jump if below (unsigned), CF = 1
#define COND_BE 0x06 // Jump if below or equal (unsigned), CF = 1 or ZF = 1
#define COND_L 0x0c // Jump if lesser (signed), SF != OF
#define COND_LE 0x0e // Jump if lesser or equal (signed), SF != OF or ZF = 1
#define COND_AE 0x03 // Jump if above or equal (unsigned), CF = 0
#define COND_A 0x07 // Jump if above (unsigned), CF = 0 and ZF = 0
#define COND_GE 0x0d // Jump if greater or equal (signed), SF = OF
#define COND_G 0x0f // Jump if greater (signed), ((ZF = 0) and SF = OF)
#define COND_E 0x04 // Jump if equal, ZF = 1
#define COND_NE 0x05 // Jump if not equal, ZF = 0
#define COND_O 0x00 // Jump if overflow, OF = 1
// Windows scratch registers: RAX, RCX, RDX, R8, R9, R10, R11
// Register used to store the start of our global memory pool
// This should maintain the same value throughout the function,
// otherwise we would need to movaps it in every time we wish to store a global var.
// NOTE: Important that this is not an argument register!
#define REG_GLOBAL_BASE REG_R10
// NOTE: Important that this is not an argument register!
#define REG_MEM_ADDR REG_R11
#define REG_OPERAND1 REG_RCX
#define REG_OPERAND2 REG_RDX
typedef struct VarInfo {
enum {
ALLOC_NONE, // not allocated yet
ALLOC_STACK, // assigned a stack position, offset stored in offset
ALLOC_GLOBAL, // assigned a position in our global memory pool, offset stored in offset
ALLOC_EXTERNAL, // global location, stored in ptr
ALLOC_COMPILING // still generating code for it
} allocStatus;
int32_t offset;
void *ptr;
size_t codeOffset;
} VarInfo;
static CirArray(VarInfo) varinfos;
static CirArray(CirVarId) compileQueue;
static CirArray(size_t) needPatch;
static CirArray(size_t) needStmtPatch;
static CirBBuf codebuf;
static CirArray(size_t) stmtLocs;
// NOTE: This memory MUST be zeroed by default!
static uint8_t globalMem[GLOBAL_MEM_SIZE] __attribute__((aligned(16)));
static size_t globalMemSize;
static void *currentPage; // Current memory page(s) we have mmaped
static size_t currentPageLen; // Number of bytes we have filled up
static size_t currentPageAlloc; // Total number of bytes we have allocated. If we don't have enough, we need to allocate a new page.
static const uint8_t callStubCode[] = {
0x49, 0x89, 0xfa, // mov r10, rdi
0x49, 0x8b, 0x7a, 0x08, // mov rdi, QWORD PTR [r10+0x8]
0x49, 0x8b, 0x72, 0x10, // mov rsi, QWORD PTR [r10+0x10]
0x49, 0x8b, 0x52, 0x18, // mov rdx, QWORD PTR [r10+0x18]
0x49, 0x8b, 0x4a, 0x20, // mov rcx, QWORD PTR [r10+0x20]
0x4d, 0x8b, 0x42, 0x28, // mov r8,QWORD PTR [r10+0x28]
0x4d, 0x8b, 0x4a, 0x30, // mov r9,QWORD PTR [r10+0x30]
0x4d, 0x8b, 0x12, // mov r10,QWORD PTR [r10]
0x41, 0xff, 0xe2 // jmp r10
};
static uint64_t (*callStub)(void *mem);
static const char *reg64ToStr[] = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
static const char *reg32ToStr[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" };
__attribute__((format(printf, 1, 2)))
static void
logInstr(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
#if 0
vfprintf(stderr, fmt, va);
#endif
va_end(va);
}
static VarInfo *
getVarInfo(CirVarId var_id)
{
if (varinfos.len > var_id)
return &varinfos.items[var_id];
// We should have already allocated enough memory.
// See the start of CirX64_call()
assert(varinfos.alloc > var_id);
for (size_t i = varinfos.len; i <= var_id; i++)
varinfos.items[i].allocStatus = ALLOC_NONE;
varinfos.len = var_id + 1;
return &varinfos.items[var_id];
}
// Rounds up `nrbits` to the nearest multiple of `roundto`.
// `roundto` must be a power of two.
static uint64_t
addTrailing(uint64_t nrbits, uint64_t roundto) {
uint64_t x = (nrbits + roundto - 1) & (~roundto + 1);
assert(x % roundto == 0);
return x;
}
static int32_t
globalMemAlloc(size_t n, size_t align)
{
globalMemSize = addTrailing(globalMemSize, align);
if (globalMemSize > INT_MAX)
cir_fatal("out of global memory");
int32_t offset = globalMemSize;
globalMemSize += n;
return offset;
}
// If space for var has not been allocated yet, resolve it or allocate it.
// After this, the allocStatus will never be ALLOC_NONE
static VarInfo *
resolveVar(CirVarId var_id)
{
VarInfo *varinfo = getVarInfo(var_id);
if (varinfo->allocStatus != ALLOC_NONE)
return varinfo;
const CirType *type = CirVar_getType(var_id);
assert(type);
if (CirType_isFun(type)) {
// Does it have code?
CirCodeId code_id = CirVar_getCode(var_id);
if (code_id) {
// Yes, it has. It's just not compiled yet. Mark it for compilation.
CirArray_push(&compileQueue, &var_id);
varinfo->allocStatus = ALLOC_COMPILING;
varinfo->ptr = NULL;
return varinfo;
} else {
// No, it does not.
// If storage is static it MUST have a code.
CirStorage storage = CirVar_getStorage(var_id);
if (storage == CIR_STATIC) {
CirLog_begin(CIRLOG_FATAL);
CirLog_print("was called but there is no definition for: ");
CirVar_logNameAndType(var_id);
CirLog_end();
exit(1);
}
CirName name = CirVar_getName(var_id);
void *ptr;
if (!CirDl_findSymbol(CirName_cstr(name), &ptr))
cir_fatal("could not find symbol: %s", CirName_cstr(name));
varinfo->allocStatus = ALLOC_EXTERNAL;
varinfo->ptr = ptr;
return varinfo;
}
} else {
// If storage is static it MUST be allocated locally.
CirStorage storage = CirVar_getStorage(var_id);
if (storage != CIR_STATIC) {
CirName name = CirVar_getName(var_id);
void *ptr;
if (CirDl_findSymbol(CirName_cstr(name), &ptr)) {
varinfo->allocStatus = ALLOC_EXTERNAL;
varinfo->ptr = ptr;
return varinfo;
}
// If storage is EXTERN is must NOT be allocated locally
if (storage == CIR_EXTERN) {
cir_fatal("could not find symbol: %s", CirName_cstr(name));
}
}
// Allocate memory
size_t align = CirType_alignof(type, &CirMachine__build);
size_t size = CirType_sizeof(type, &CirMachine__build);
int32_t offset = globalMemAlloc(size, align);
varinfo->allocStatus = ALLOC_GLOBAL;
varinfo->offset = offset;
return varinfo;
}
}
// ======
static void
emitU8(uint8_t x)
{
CirBBuf_grow(&codebuf, 1);
codebuf.items[codebuf.len++] = x;
}
static void
emitI8(int8_t x)
{
union { int8_t i8; uint8_t u8; } u = { .i8 = x };
emitU8(u.u8);
}
static void
emitU32(uint32_t x)
{
CirBBuf_grow(&codebuf, 4);
size_t i = codebuf.len;
codebuf.items[i++] = x & 0xff;
codebuf.items[i++] = (x >> 8) & 0xff;
codebuf.items[i++] = (x >> 16) & 0xff;
codebuf.items[i++] = (x >> 24) & 0xff;
codebuf.len = i;
}
static void
emitI32(int32_t x)
{
union { int32_t i32; uint32_t u32; } u = { .i32 = x };
emitU32(u.u32);
}
static void
emitU64(uint64_t x)
{
CirBBuf_grow(&codebuf, 8);
size_t i = codebuf.len;
codebuf.items[i++] = x & 0xff;
codebuf.items[i++] = (x >> 8) & 0xff;
codebuf.items[i++] = (x >> 16) & 0xff;
codebuf.items[i++] = (x >> 24) & 0xff;
codebuf.items[i++] = (x >> 32) & 0xff;
codebuf.items[i++] = (x >> 40) & 0xff;
codebuf.items[i++] = (x >> 48) & 0xff;
codebuf.items[i++] = (x >> 56) & 0xff;
codebuf.len = i;
}
static void
emitREX(uint8_t W, uint8_t R, uint8_t X, uint8_t B)
{
assert(W < 2); assert(R < 2); assert(X < 2); assert(B < 2);
uint8_t val = 64 | (W << 3) | (R << 2) | (X << 1) | B;
emitU8(val);
}
static void
emitModRM(uint8_t mod, uint8_t reg, uint8_t rm)
{
assert(mod < 4); assert(reg < 8); assert(rm < 8);
uint8_t val = (mod << 6) | (reg << 3) | rm;
emitU8(val);
}
static void
emitSIB(uint8_t scale, uint8_t index, uint8_t base)
{
assert(scale < 4); assert(index < 8); assert(base < 8);
uint8_t val = (scale << 6) | (index << 3) | base;
emitU8(val);
}
// emit LEA dstReg, [baseReg + indexReg * (2 ** scale)]
static void
emitLea(unsigned dstReg, unsigned baseReg, unsigned indexReg, unsigned scale)
{
assert(dstReg < 16); assert(baseReg < 16); assert(indexReg < 16); assert(scale < 4);
assert(baseReg != REG_RBP && baseReg != REG_R13); // We don't support this encoding yet
assert(indexReg != REG_RSP); // We don't support this encoding yet
emitREX(1, dstReg > 7, indexReg > 7, baseReg > 7);
emitU8(0x8d);
emitModRM(0, dstReg & 0x07, 4 /* use SIB byte */);
emitSIB(scale, indexReg & 0x07, baseReg & 0x07);
}
// emit [srcReg64 + disp]
static void
emitMemDisp(unsigned dstReg64, unsigned srcReg64, int32_t disp)
{
if (disp == 0 && srcReg64 != REG_RSP && srcReg64 != REG_RBP && srcReg64 != REG_R12 && srcReg64 != REG_R13) {
emitModRM(0, dstReg64 & 0x07, srcReg64 & 0x07);
} else if (disp <= SCHAR_MAX && disp >= SCHAR_MIN) {
// [r/m + disp8]
emitModRM(1, dstReg64 & 0x07, srcReg64 & 0x07);
if (srcReg64 == REG_RSP || srcReg64 == REG_R12)
emitSIB(0, 4, srcReg64 & 0x07);
emitI8(disp);
} else {
// [r/m + disp32]
emitModRM(2, dstReg64 & 0x07, srcReg64 & 0x07);
if (srcReg64 == REG_RSP || srcReg64 == REG_R12)
emitSIB(0, 4, srcReg64 & 0x07);
emitI32(disp);
}
}
// 64-bit add
static void
emitAdd64(unsigned dstReg64, unsigned srcReg64)
{
assert(dstReg64 < 16); assert(srcReg64 < 16);
logInstr("add\t%s, %s\n", reg64ToStr[dstReg64], reg64ToStr[dstReg64]);
emitREX(1, srcReg64 > 7, 0, dstReg64 > 7);
emitU8(0x01);
emitModRM(3, srcReg64 & 0x07, dstReg64 & 0x07);
}
static void
emitAddImmI32(unsigned dstReg64, int32_t imm)
{
assert(dstReg64 < 16);
logInstr("add\t%s, %d\n", reg64ToStr[dstReg64], imm);
emitREX(1, 0, 0, dstReg64 > 7);
emitU8(0x81);
emitModRM(3, 0, dstReg64 & 0x07);
emitU32(imm);
}
static void
emitSub64(unsigned dstReg64, unsigned srcReg64)
{
assert(dstReg64 < 16); assert(srcReg64 < 16);
logInstr("sub\t%s, %s\n", reg64ToStr[dstReg64], reg64ToStr[srcReg64]);
emitREX(1, dstReg64 > 7, 0, srcReg64 > 7);
emitU8(0x2B);
emitModRM(3, dstReg64 & 0x07, srcReg64 & 0x07);
}
static void
emitMulImm32(unsigned dstReg, unsigned srcReg, int32_t imm)
{
assert(dstReg < 16); assert(srcReg < 16);
bool fitsInOneByte = imm >= SCHAR_MIN && imm <= SCHAR_MAX;
emitREX(1, dstReg > 7, 0, srcReg > 7);
emitU8(fitsInOneByte ? 0x6b : 0x69);
emitModRM(3, dstReg & 0x07, srcReg & 0x07);
if (fitsInOneByte)
emitI8(imm);
else
emitI32(imm);
}
static void
emitImul(unsigned dstReg, unsigned srcReg)
{
assert(dstReg < 16); assert(srcReg < 16);
emitREX(1, dstReg > 7, 0, srcReg > 7);
emitU8(0x0f);
emitU8(0xaf);
emitModRM(3, dstReg & 0x07, srcReg & 0x07);
}
static void
emitDiv64(unsigned reg)
{
assert(reg < 16);
emitREX(1, 0, 0, reg > 7);
emitU8(0xf7);
emitModRM(3, 6, reg & 0x07);
}
static void
emitIdiv64(unsigned reg)
{
assert(reg < 16);
emitREX(1, 0, 0, reg > 7);
emitU8(0xf7);
emitModRM(3, 7, reg & 0x07);
}
static void
emitDiv32(unsigned reg)
{
assert(reg < 16);
if (reg > 7)
emitREX(0, 0, 0, 1);
emitU8(0xf7);
emitModRM(3, 6, reg & 0x07);
}
static void
emitIdiv32(unsigned reg)
{
assert(reg < 16);
if (reg > 7)
emitREX(0, 0, 0, 1);
emitU8(0xf7);
emitModRM(3, 7, reg & 0x07);
}
static void
emitXor(unsigned dstReg, unsigned srcReg)
{
assert(dstReg < 16); assert(srcReg < 16);
emitREX(1, dstReg > 7, 0, srcReg > 7);
emitU8(0x33);
emitModRM(3, dstReg & 0x07, srcReg & 0x07);
}
// Move immediate value into register
static void
emitMovImmU64(unsigned reg64, uint64_t value)
{
assert(reg64 < 16);
logInstr("movabs\t%s, 0x%lx\n", reg64ToStr[reg64], value);
emitREX(1, 0, 0, reg64 > 7);
emitU8(0xB8 + (reg64 & 0x07)); // 0xB8 + op1_reg
emitU64(value); // op2_imm64
}
// Move immediate value into register
static void
emitMovImmI64(unsigned reg64, int64_t value)
{
union { uint64_t u64; int64_t i64; } u = { .i64 = value };
emitMovImmU64(reg64, u.u64);
}
static void
emitMovImmPtr(unsigned reg, const void *ptr)
{
union { uint64_t u64; const void *ptr; } u = { .ptr = ptr };
emitMovImmU64(reg, u.u64);
}
static void
emitMovReg3264(unsigned dstReg64, unsigned srcReg64, bool wide)
{
assert(dstReg64 < 16); assert(srcReg64 < 16);
if (wide || dstReg64 > 7 || srcReg64 > 7)
emitREX(1, dstReg64 > 7, 0, srcReg64 > 7);
emitU8(0x8B);
emitModRM(3, dstReg64 & 0x07, srcReg64 & 0x07);
}
static void
emitMovReg32(unsigned dstReg32, unsigned srcReg32)
{
logInstr("mov\t%s, %s\n", reg32ToStr[dstReg32], reg32ToStr[dstReg32]);
emitMovReg3264(dstReg32, srcReg32, false);
}
static void
emitMovReg64(unsigned dstReg64, unsigned srcReg64)
{
logInstr("mov\t%s, %s\n", reg64ToStr[dstReg64], reg64ToStr[dstReg64]);
emitMovReg3264(dstReg64, srcReg64, true);
}
// Load memory into register
static void
emitLoad3264(unsigned dstReg64, unsigned srcReg64, int32_t disp, bool wide, bool _signed)
{
assert(dstReg64 < 16); assert(srcReg64 < 16);
if (wide || dstReg64 > 7 || srcReg64 > 7)
emitREX(wide, dstReg64 > 7, 0, srcReg64 > 7);
emitU8(_signed ? 0x63 : 0x8B);
emitMemDisp(dstReg64, srcReg64, disp);
}
static void
emitLoad8(unsigned dstReg, unsigned srcReg, int32_t disp, bool _signed)
{
assert(dstReg < 16); assert(srcReg < 16);
emitREX(1, dstReg > 7, 0, srcReg > 7);
if (_signed) {
emitU8(0x0f);
emitU8(0xbe);
} else {
// movzx
emitU8(0x0f);
emitU8(0xb6);
}
emitMemDisp(dstReg, srcReg, disp);
}
static void
emitLoad16(unsigned dstReg, unsigned srcReg, int32_t disp, bool _signed)
{
assert(dstReg < 16); assert(srcReg < 16);
if (!_signed)
emitU8(0x66); // size override
emitREX(1, dstReg > 7, 0, srcReg > 7);
if (_signed) {
emitU8(0x0f);
emitU8(0xbf);
} else {
// movzx
emitU8(0x0f);
emitU8(0xb7);
}
emitMemDisp(dstReg, srcReg, disp);
}
static void
emitLoad64(unsigned dstReg64, unsigned srcReg64, int32_t disp)
{
emitLoad3264(dstReg64, srcReg64, disp, true, false);
}
static void
emitLoad32(unsigned dstReg32, unsigned srcReg32, int32_t disp, bool _signed)
{
emitLoad3264(dstReg32, srcReg32, disp, _signed, _signed);
}
static void
emitLoadIkind(uint32_t ikind, unsigned dstReg, unsigned memReg, int32_t disp)
{
switch (ikind) {
case CIR_ICHAR:
if (CirMachine__build.charIsUnsigned)
emitLoadIkind(CIR_IUCHAR, dstReg, memReg, disp);
else
emitLoadIkind(CIR_ISCHAR, dstReg, memReg, disp);
break;
case CIR_ISCHAR:
case CIR_IUCHAR:
case CIR_IBOOL:
assert(sizeof(char) == 1);
assert(sizeof(bool) == 1);
emitLoad8(dstReg, memReg, disp, ikind == CIR_ISCHAR);
break;
case CIR_ISHORT:
case CIR_IUSHORT:
assert(sizeof(short) == 2);
emitLoad16(dstReg, memReg, disp, ikind == CIR_ISHORT);
break;
case CIR_IINT:
case CIR_IUINT:
assert(sizeof(int) == 4);
emitLoad32(dstReg, memReg, disp, ikind == CIR_IINT);
break;
case CIR_ILONG:
case CIR_IULONG:
case CIR_ILONGLONG:
case CIR_IULONGLONG:
assert(sizeof(long) == 8);
assert(sizeof(long long) == 8);
emitLoad64(dstReg, memReg, disp);
break;
default:
cir_bug("unsupported ikind");
}
}
static void
emitLoad(unsigned dstReg, const CirValue *value)
{
if (CirValue_isInt(value)) {
// TODO: maybe use appropriate encoding based on imm
emitMovImmU64(dstReg, CirValue_getU64(value));
return;
}
if (CirValue_isString(value)) {
emitMovImmPtr(dstReg, CirValue_getString(value));
return;
}
assert(CirValue_isLval(value));
bool deref = CirValue_isMem(value);
CirVarId var_id = CirValue_getVar(value);
const VarInfo *varinfo = resolveVar(var_id);
uint64_t bitsOffset = 0;
const CirType *type = CirValue_computeTypeAndBitsOffset(value, &bitsOffset, &CirMachine__build);
assert(type);
type = CirType_unroll(type);
bitsOffset /= 8;
if (bitsOffset > INT_MAX)
cir_fatal("field offset too large");
int32_t fieldOffset = bitsOffset;
uint32_t ikind = CirType_isInt(type);
if (CirType_isPtr(type))
ikind = CIR_IULONG;
if (deref) {
// First we need to load the address from var
emitLoad(dstReg, CirValue_ofVar(var_id));
// Then we load the value
if (ikind)
emitLoadIkind(ikind, dstReg, dstReg, fieldOffset);
else if (CirType_isArray(type))
emitAddImmI32(dstReg, fieldOffset);
else
cir_fatal("emitLoad called on non-int/ptr/array var");
} else if (varinfo->allocStatus == ALLOC_STACK) {
if (ikind) {
emitLoadIkind(ikind, dstReg, REG_RSP, varinfo->offset + fieldOffset);
} else if (CirType_isArray(type)) {
// TODO: Probably can optimize into a single instruction
emitMovReg64(dstReg, REG_RSP);
emitAddImmI32(dstReg, varinfo->offset + fieldOffset);
} else {
cir_fatal("emitLoad called on non-int/ptr/array var");
}
} else if (varinfo->allocStatus == ALLOC_GLOBAL) {
if (ikind) {
emitLoadIkind(ikind, dstReg, REG_GLOBAL_BASE, varinfo->offset + fieldOffset);
} else if (CirType_isArray(type)) {
// TODO: Probably can optimize into a single instruction
emitMovReg64(dstReg, REG_GLOBAL_BASE);
emitAddImmI32(dstReg, varinfo->offset + fieldOffset);
} else {
cir_fatal("emitLoad called on non-int/ptr/array var");
}
} else {
assert(varinfo->allocStatus == ALLOC_EXTERNAL);
uint64_t ptr = (uint64_t)varinfo->ptr;
ptr += fieldOffset;
if (ikind) {
emitMovImmU64(dstReg, ptr);
emitLoadIkind(ikind, dstReg, dstReg, fieldOffset);
} else if (CirType_isArray(type)) {
assert(!fieldOffset);
emitMovImmU64(dstReg, ptr);
} else {
cir_fatal("emitLoad called on non-int/ptr/array var");
}
}
}
static void
emitLoadVarAddress(unsigned dstReg, CirVarId var_id, int32_t disp)
{
const VarInfo *varinfo = resolveVar(var_id);
if (varinfo->allocStatus == ALLOC_GLOBAL) {
emitMovReg64(dstReg, REG_GLOBAL_BASE);
emitAddImmI32(dstReg, varinfo->offset + disp);
} else if (varinfo->allocStatus == ALLOC_STACK) {
emitMovReg64(dstReg, REG_RSP);
emitAddImmI32(dstReg, varinfo->offset + disp);
} else if (varinfo->allocStatus == ALLOC_EXTERNAL) {
uint64_t ptr = (uint64_t)varinfo->ptr;
emitMovImmU64(dstReg, ptr + disp);
} else if (varinfo->allocStatus == ALLOC_COMPILING) {
// Need to backpatch later
// emitMovImmU64 with placeholder constant
assert(!disp);
assert(dstReg < 16);
emitREX(1, 0, 0, dstReg > 7);
emitU8(0xB8 + (dstReg & 0x07));
CirArray_push(&needPatch, &codebuf.len);
emitU64(var_id);
} else {
cir_bug("boo");
}
}
static void
emitLoadAddress(unsigned dstReg, const CirValue *value)
{
uint32_t ikind = CirValue_isInt(value);
if (ikind)
cir_fatal("cannot get address of an integer constant");
if (CirValue_isString(value)) {
emitMovImmPtr(dstReg, CirValue_getString(value));
return;
}
assert(CirValue_isLval(value));
bool deref = CirValue_isMem(value);
CirVarId var_id = CirValue_getVar(value);
uint64_t bitsOffset = CirValue_computeBitsOffset(value, &CirMachine__build);
bitsOffset /= 8;
if (bitsOffset > INT_MAX)
cir_fatal("field offset too large");
int32_t fieldOffset = bitsOffset;
if (deref) {
// First we need to load the address from var
emitLoad(dstReg, CirValue_ofVar(var_id));
// Then, we add the fieldOffset to the value
emitAddImmI32(dstReg, fieldOffset);
} else {
emitLoadVarAddress(dstReg, var_id, fieldOffset);
}
}
// Store register into memory
static void
emitStore163264(unsigned memReg64, int32_t disp, unsigned srcReg64, bool wide, bool size_override)
{
assert(memReg64 < 16); assert(srcReg64 < 16);
if (size_override)
emitU8(0x66);
if (wide || memReg64 > 7 || srcReg64 > 7)
emitREX(wide, srcReg64 > 7, 0, memReg64 > 7);
emitU8(0x89);
emitMemDisp(srcReg64, memReg64, disp);
}
static void
emitStore8(unsigned memReg, int32_t disp, unsigned srcReg)
{
assert(memReg < 16); assert(srcReg < 16);
if (memReg > 7 || srcReg > 7)
emitREX(0, srcReg > 7, 0, memReg > 7);
emitU8(0x88);
emitMemDisp(srcReg, memReg, disp);
}
static void
emitStore16(unsigned memReg64, int32_t disp, unsigned srcReg64)
{
emitStore163264(memReg64, disp, srcReg64, false, true);
}
static void
emitStore32(unsigned memReg64, int32_t disp, unsigned srcReg64)
{
emitStore163264(memReg64, disp, srcReg64, false, false);
}
static void
emitStore64(unsigned memReg64, int32_t disp, unsigned srcReg64)
{
emitStore163264(memReg64, disp, srcReg64, true, false);
}
static void
emitStoreIkind(uint32_t ikind, unsigned memReg, int32_t disp, unsigned srcReg)
{
switch (ikind) {
case CIR_ICHAR:
case CIR_ISCHAR:
case CIR_IUCHAR:
case CIR_IBOOL:
assert(sizeof(char) == 1);
assert(sizeof(bool) == 1);
emitStore8(memReg, disp, srcReg);
return;
case CIR_ISHORT:
case CIR_IUSHORT:
assert(sizeof(short) == 2);
emitStore16(memReg, disp, srcReg);
return;
case CIR_IINT:
case CIR_IUINT:
assert(sizeof(int) == 4);
emitStore32(memReg, disp, srcReg);
return;
case CIR_ILONG:
case CIR_IULONG:
case CIR_ILONGLONG:
case CIR_IULONGLONG:
assert(sizeof(long) == 8);
assert(sizeof(long long) == 8);
emitStore64(memReg, disp, srcReg);
return;
default:
cir_bug("unsupported ikind");
}
}
static void
emitStore(const CirValue *value, unsigned srcReg)
{
if (!CirValue_isLval(value))
cir_bug("emitStore called on non-lval");
bool deref = CirValue_isMem(value);
CirVarId var_id = CirValue_getVar(value);
const VarInfo *varinfo = resolveVar(var_id);
uint64_t bitsOffset = 0;
const CirType *type = CirValue_computeTypeAndBitsOffset(value, &bitsOffset, &CirMachine__build);
assert(type);
type = CirType_unroll(type);
bitsOffset /= 8;
if (bitsOffset > INT_MAX)
cir_fatal("emitStore: offset too large");
int32_t fieldOffset = bitsOffset;
uint32_t ikind = CirType_isInt(type);
if (CirType_isPtr(type))
ikind = CIR_IULONG;
if (!ikind) {
CirLog_begin(CIRLOG_BUG);
CirLog_print("emitStore called on non-int/ptr var: ");
CirVar_logNameAndType(var_id);
CirLog_end();
abort();
}
if (deref) {
// First we need to load the address from var into REG_MEM_ADDR
emitLoad(REG_MEM_ADDR, CirValue_ofVar(var_id));
// Then we store the value
emitStoreIkind(ikind, REG_MEM_ADDR, fieldOffset, srcReg);
} else {
if (varinfo->allocStatus == ALLOC_STACK) {
emitStoreIkind(ikind, REG_RSP, varinfo->offset + fieldOffset, srcReg);
} else if (varinfo->allocStatus == ALLOC_GLOBAL) {
emitStoreIkind(ikind, REG_GLOBAL_BASE, varinfo->offset + fieldOffset, srcReg);
} else {
assert(varinfo->allocStatus == ALLOC_EXTERNAL);
uint64_t ptr = (uint64_t)varinfo->ptr;
ptr += fieldOffset;
emitMovImmU64(REG_MEM_ADDR, ptr);
emitStoreIkind(ikind, REG_MEM_ADDR, fieldOffset, srcReg);
}
}
}
static void
emitCall(unsigned reg)
{
if (reg > 7)
emitREX(0, 0, 0, reg > 7);
emitU8(0xFF);
emitModRM(3, 2, reg & 0x07);
}
static void
emitRet(void)
{
emitU8(0xc3);
}
static void
emitJumpToStmt(CirStmtId stmt_id)
{
emitU8(0xE9);
assert(sizeof(stmt_id) == 4);
CirArray_push(&needStmtPatch, &codebuf.len);
emitU32(stmt_id);
}
static void
emitCondJumpToStmt(uint8_t cond, CirStmtId stmt_id)
{
assert(cond < 16);
emitU8(0x0f);
emitU8(0x80 + cond);
assert(sizeof(stmt_id) == 4);
CirArray_push(&needStmtPatch, &codebuf.len);
emitU32(stmt_id);
}
static void
emitCmp(unsigned reg1, unsigned reg2)
{
assert(reg1 < 16); assert(reg2 < 16);
emitREX(1, reg1 > 7, 0, reg2 > 7);
emitU8(0x3b);
emitModRM(3, reg1 & 0x07, reg2 & 0x07);
}
static void
emitCqo(void)
{
emitREX(1, 0, 0, 0);
emitU8(0x99);
}
static void
emitInt3(void)
{
emitU8(0xcc);
}
// =====
static void
emitAddPtrInt(const CirValue *dst, const CirValue *ptrValue, const CirType *ptrType, const CirValue *intValue, const CirType *intType)
{
static unsigned sizeToFactor[] = { [1] = 0, [2] = 1, [4] = 2, [8] = 3 };
// Code sequence depends on whether the size of each element is in {1, 2, 4, 8}.
const CirType *baseType = CirType_getBaseType(ptrType);
uint64_t baseTypeSize = CirType_sizeof(baseType, &CirMachine__build);
if (CirValue_isInt(intValue)) {
// Can use ADD + pre-computed offset
uint32_t ikind = CirType_isInt(CirType_unroll(intType));
assert(ikind);
emitLoad(REG_OPERAND1, ptrValue);
int32_t imm;
if (CirIkind_isSigned(ikind, &CirMachine__build)) {
int64_t val = CirValue_getI64(intValue) * baseTypeSize;
assert(val >= INT_MIN && val <= INT_MAX);
imm = val;
} else {
uint64_t val = CirValue_getU64(intValue) * baseTypeSize;
assert(val <= INT_MAX);
imm = val;
}
emitAddImmI32(REG_OPERAND1, imm);
emitStore(dst, REG_OPERAND1);
} else if (baseTypeSize == 1) {
// Can use ADD
emitLoad(REG_OPERAND1, ptrValue);
emitLoad(REG_OPERAND2, intValue);
emitAdd64(REG_OPERAND1, REG_OPERAND2);
emitStore(dst, REG_OPERAND1);
} else if (baseTypeSize == 2 || baseTypeSize == 4 || baseTypeSize == 8) {
// Can use LEA
emitLoad(REG_OPERAND1, ptrValue);
emitLoad(REG_OPERAND2, intValue);
emitLea(REG_OPERAND1, REG_OPERAND1, REG_OPERAND2, sizeToFactor[baseTypeSize]);
emitStore(dst, REG_OPERAND1);
} else {
// Compute offset manually
assert(baseTypeSize <= INT_MAX);
emitLoad(REG_OPERAND1, ptrValue);
emitLoad(REG_OPERAND2, intValue);
emitMulImm32(REG_OPERAND2, REG_OPERAND2, baseTypeSize);
emitAdd64(REG_OPERAND1, REG_OPERAND2);
emitStore(dst, REG_OPERAND1);
}
}
static void
emitBinop(CirStmtId stmt_id)
{
assert(CirStmt_isBinOp(stmt_id));
uint32_t op = CirStmt_getOp(stmt_id);
const CirValue *dst = CirStmt_getDst(stmt_id);
const CirValue *operand1 = CirStmt_getOperand1(stmt_id);
const CirValue *operand2 = CirStmt_getOperand2(stmt_id);
if (!operand1)
cir_fatal("emitBinop: operand1 has no value");
if (!operand2)
cir_fatal("emitBinop: operand2 has no value");
const CirType *operand1Type = CirValue_getType(operand1);
if (!operand1Type)
cir_fatal("emitBinop: operand1 has no type");
const CirType *operand2Type = CirValue_getType(operand2);
if (!operand2Type)
cir_fatal("emitBinop: operand2 has no type");
operand1Type = CirType_lvalConv(operand1Type);
operand2Type = CirType_lvalConv(operand2Type);
// Perform operation
bool isMod = false;
switch (op) {
case CIR_BINOP_PLUS: {
// Depends on the type of the operands
const CirType *operand1UnrolledType = CirType_unroll(operand1Type);
const CirType *operand2UnrolledType = CirType_unroll(operand2Type);
uint32_t ikind;
if (CirType_isArithmetic(operand1UnrolledType) && CirType_isArithmetic(operand2UnrolledType)) {
// TODO: support floats here
emitLoad(REG_OPERAND1, operand1);
emitLoad(REG_OPERAND2, operand2);
emitAdd64(REG_OPERAND1, REG_OPERAND2);
emitStore(dst, REG_OPERAND1);
} else if (CirType_isPtr(operand1UnrolledType) && (ikind = CirType_isInt(operand2UnrolledType))) {
// ptr + int
emitAddPtrInt(dst, operand1, operand1UnrolledType, operand2, operand2UnrolledType);
} else if ((ikind = CirType_isInt(operand1UnrolledType)) && CirType_isPtr(operand2UnrolledType)) {
// int + ptr
emitAddPtrInt(dst, operand2, operand2Type, operand1, operand1Type);
} else {
cir_fatal("CIR_BINOP_PLUS: invalid operand types");
}
break;
}
case CIR_BINOP_MINUS: {
// TODO: Depends on the type of the operands
// Need to support ptr - int, ptr - ptr
emitLoad(REG_OPERAND1, operand1);
emitLoad(REG_OPERAND2, operand2);
emitSub64(REG_OPERAND1, REG_OPERAND2);
emitStore(dst, REG_OPERAND1);
break;
}
case CIR_BINOP_MUL: {
emitLoad(REG_OPERAND1, operand1);