-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharm64.c
More file actions
3188 lines (2950 loc) · 99 KB
/
Copy patharm64.c
File metadata and controls
3188 lines (2950 loc) · 99 KB
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
/*
* AArch64 (ARM64) code generation for DILL
* Minimal implementation to get t1 test working
*/
#include "config.h"
#include "dill.h"
#include "dill_internal.h"
#include "arm64.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <libkern/OSCacheControl.h>
#endif
#ifndef CLEAR_CACHE_DEFINED
extern void __clear_cache(void *, void *);
#endif
/* Type sizes for AArch64 (LP64 model) */
int arm64_type_size[] = {
1, /* DILL_C */
1, /* DILL_UC */
2, /* DILL_S */
2, /* DILL_US */
4, /* DILL_I */
4, /* DILL_U */
8, /* DILL_L */
8, /* DILL_UL */
8, /* DILL_P */
4, /* DILL_F */
8, /* DILL_D */
0, /* DILL_V */
0, /* DILL_B */
8, /* DILL_EC */
};
/* Type alignments for AArch64 */
int arm64_type_align[] = {
1, /* DILL_C */
1, /* DILL_UC */
2, /* DILL_S */
2, /* DILL_US */
4, /* DILL_I */
4, /* DILL_U */
8, /* DILL_L */
8, /* DILL_UL */
8, /* DILL_P */
4, /* DILL_F */
8, /* DILL_D */
0, /* DILL_V */
0, /* DILL_B */
8, /* DILL_EC */
};
/* Forward declaration */
static void arm64_set64(dill_stream s, int rd, unsigned long imm);
/*
* AArch64 Instruction Encoding Helpers
*/
/* MOV (wide immediate) - MOVZ variant
* Encoding: sf=1 (64-bit), opc=10, hw=0, imm16, Rd
* 1 10 100101 00 imm16[15:0] Rd[4:0]
* 0xd2800000 | (imm16 << 5) | rd
*/
static void
arm64_movz(dill_stream s, int rd, unsigned long imm16)
{
unsigned int insn = 0xd2800000 | ((imm16 & 0xffff) << 5) | (rd & 0x1f);
INSN_OUT(s, insn);
}
/* MOV (wide immediate) - MOVZ with shift
* hw = shift / 16 (0, 1, 2, or 3 for 64-bit)
*/
static void
arm64_movz_shift(dill_stream s, int rd, unsigned long imm16, int shift)
{
int hw = shift / 16;
unsigned int insn = 0xd2800000 | (hw << 21) | ((imm16 & 0xffff) << 5) | (rd & 0x1f);
INSN_OUT(s, insn);
}
/* MOVK - move with keep
* 1 11 100101 hw imm16 Rd
* 0xf2800000 | (hw << 21) | (imm16 << 5) | rd
*/
static void
arm64_movk(dill_stream s, int rd, unsigned long imm16, int shift)
{
int hw = shift / 16;
unsigned int insn = 0xf2800000 | (hw << 21) | ((imm16 & 0xffff) << 5) | (rd & 0x1f);
INSN_OUT(s, insn);
}
/* RET - return from subroutine
* Encoding: 1101011 0010 11111 000000 Rn[4:0] 00000
* Default Rn = x30 (link register)
* 0xd65f0000 | (rn << 5)
*/
static void
arm64_ret_insn(dill_stream s)
{
/* RET x30 = 0xd65f03c0 */
unsigned int insn = 0xd65f0000 | (_lr << 5);
INSN_OUT(s, insn);
}
/* NOP instruction */
static void
arm64_nop(dill_stream s)
{
INSN_OUT(s, 0xd503201f);
}
/* Simple return - emits return with nops for potential patching */
static void
arm64_simple_ret(dill_stream s)
{
dill_mark_ret_location(s);
/* Emit frame teardown epilogue:
* mov sp, x29 ; Restore SP to frame pointer (deallocate save area)
* ldp x29, x30, [sp], #16 ; Restore FP and LR
* ldp d14, d15, [sp], #16 ; Restore float callee-saved regs
* ldp d12, d13, [sp], #16
* ldp d10, d11, [sp], #16
* ldp d8, d9, [sp], #16
* ldp x27, x28, [sp], #16 ; Restore integer callee-saved regs
* ldp x25, x26, [sp], #16
* ldp x23, x24, [sp], #16
* ldp x21, x22, [sp], #16
* ldp x19, x20, [sp], #16
* ret
*/
/* MOV sp, x29 (ADD sp, x29, #0) = 0x910003BF */
INSN_OUT(s, 0x910003BF);
/* LDP x29, x30, [sp], #16 = 0xA8C17BFD */
INSN_OUT(s, 0xA8C17BFD);
/* Restore float callee-saved registers d8-d15 */
/* LDP d14, d15, [sp], #16 = 0x6CC13FEE */
INSN_OUT(s, 0x6CC13FEE);
/* LDP d12, d13, [sp], #16 = 0x6CC137EC */
INSN_OUT(s, 0x6CC137EC);
/* LDP d10, d11, [sp], #16 = 0x6CC12FEA */
INSN_OUT(s, 0x6CC12FEA);
/* LDP d8, d9, [sp], #16 = 0x6CC127E8 */
INSN_OUT(s, 0x6CC127E8);
/* Restore integer callee-saved registers x19-x28 */
/* LDP x27, x28, [sp], #16 = 0xA8C173FB */
INSN_OUT(s, 0xA8C173FB);
/* LDP x25, x26, [sp], #16 = 0xA8C16BF9 */
INSN_OUT(s, 0xA8C16BF9);
/* LDP x23, x24, [sp], #16 = 0xA8C163F7 */
INSN_OUT(s, 0xA8C163F7);
/* LDP x21, x22, [sp], #16 = 0xA8C15BF5 */
INSN_OUT(s, 0xA8C15BF5);
/* LDP x19, x20, [sp], #16 = 0xA8C153F3 */
INSN_OUT(s, 0xA8C153F3);
arm64_ret_insn(s);
arm64_nop(s); /* nops for potential epilogue patching */
arm64_nop(s);
}
/* Flush instruction cache */
static void
arm64_flush(void *base, void *limit)
{
#if defined(__APPLE__) && defined(HOST_ARM64)
/* On macOS ARM64, use sys_icache_invalidate */
sys_icache_invalidate(base, (size_t)((char*)limit - (char*)base));
#elif defined(HOST_ARM64)
__clear_cache(base, limit);
#endif
}
/* Link data labels - currently unused */
static void
arm64_data_link(dill_stream s)
{
/* TODO: Implement if needed for data label support */
}
/* Link branch targets */
static void
arm64_branch_link(dill_stream s)
{
struct branch_table *t = &s->p->branch_table;
int i;
for (i = 0; i < t->branch_count; i++) {
int label = t->branch_locs[i].label;
int label_offset = t->label_locs[label] - t->branch_locs[i].loc;
int *branch_addr = (int*)((char *)s->p->code_base +
t->branch_locs[i].loc);
/* ARM64 branch offset is in instructions (divide by 4) */
label_offset = label_offset >> 2;
/* Check instruction type by top byte */
int insn = *branch_addr;
if ((insn & 0xff000000) == 0x54000000) {
/* B.cond: imm19 in bits 5-23, cond in bits 0-3 */
*branch_addr &= 0xff00001f; /* Keep opcode and condition */
*branch_addr |= ((label_offset & 0x7ffff) << 5);
} else {
/* B/BL: imm26 in bits 0-25 */
*branch_addr &= 0xfc000000;
*branch_addr |= (label_offset & 0x03ffffff);
}
}
}
extern void arm64_rt_call_link(char *code, call_t *t);
/* Link call targets */
static void
arm64_call_link(dill_stream s)
{
arm64_rt_call_link(s->p->code_base, &s->p->call_table);
}
/*
* On ARM64, we need a procedure linkage table to manage
* calls to addresses that are more than 26 bits away (128MB).
* We emit a PLT that loads the address into a register and branches.
*/
extern void
arm64_PLT_emit(dill_stream s, int package)
{
call_t *t = &s->p->call_table;
int i;
for (i = 0; i < t->call_count; i++) {
int *call_addr = (int*)((unsigned long)s->p->code_base +
t->call_locs[i].loc);
long call_offset = (unsigned long)t->call_locs[i].xfer_addr -
(unsigned long)call_addr;
/* Check if offset exceeds 26-bit signed range (128MB) */
call_offset = call_offset >> 2;
long high_bits = call_offset >> 25;
#ifdef HOST_ARM64
if (((high_bits != 0) && (high_bits != -1)) || package) {
t->call_locs[i].mach_info = (void*)
((long)s->p->cur_ip - (long)s->p->code_base);
/* Load address into x16 (IP0) and branch */
arm64_set64(s, _x16, (unsigned long)t->call_locs[i].xfer_addr);
/* BR x16 = 0xd61f0200 */
INSN_OUT(s, 0xd61f0000 | (_x16 << 5));
}
#endif
}
}
/* MOVN - move wide with NOT
* 1 00 100101 hw imm16 Rd
* 0x92800000 | (hw << 21) | (imm16 << 5) | rd
*/
static void
arm64_movn(dill_stream s, int rd, unsigned long imm16, int shift)
{
int hw = shift / 16;
unsigned int insn = 0x92800000 | (hw << 21) | ((imm16 & 0xffff) << 5) | (rd & 0x1f);
INSN_OUT(s, insn);
}
/* Load a 64-bit immediate value into a register
* Uses MOVZ/MOVK sequence, or MOVN for values near -1
*/
static void
arm64_set64(dill_stream s, int rd, unsigned long imm)
{
unsigned long inv = ~imm;
int i;
int first_chunk = -1;
int num_zero_chunks = 0;
int num_ffff_chunks = 0;
/* Count zero and 0xffff chunks to decide between MOVZ and MOVN */
for (i = 0; i < 4; i++) {
unsigned long chunk = (imm >> (i * 16)) & 0xffff;
if (chunk == 0) num_zero_chunks++;
if (chunk == 0xffff) num_ffff_chunks++;
}
if (num_ffff_chunks > num_zero_chunks) {
/* Use MOVN - more 0xffff chunks means fewer instructions with NOT */
/* Find first chunk that isn't 0xffff */
for (i = 0; i < 4; i++) {
unsigned long chunk = (inv >> (i * 16)) & 0xffff;
if (chunk != 0) {
first_chunk = i;
break;
}
}
if (first_chunk == -1) {
/* All 0xffff - value is -1 */
arm64_movn(s, rd, 0, 0);
return;
}
/* MOVN with first non-zero chunk of inverted value */
arm64_movn(s, rd, (inv >> (first_chunk * 16)) & 0xffff, first_chunk * 16);
/* MOVK for remaining chunks that aren't 0xffff in original */
for (i = first_chunk + 1; i < 4; i++) {
unsigned long chunk = (imm >> (i * 16)) & 0xffff;
if (chunk != 0xffff) {
arm64_movk(s, rd, chunk, i * 16);
}
}
} else {
/* Use MOVZ - find first non-zero chunk */
for (i = 0; i < 4; i++) {
unsigned long chunk = (imm >> (i * 16)) & 0xffff;
if (chunk != 0) {
first_chunk = i;
break;
}
}
if (first_chunk == -1) {
/* All zero */
arm64_movz(s, rd, 0);
return;
}
/* MOVZ with first non-zero chunk */
arm64_movz_shift(s, rd, (imm >> (first_chunk * 16)) & 0xffff, first_chunk * 16);
/* MOVK for remaining non-zero chunks */
for (i = first_chunk + 1; i < 4; i++) {
unsigned long chunk = (imm >> (i * 16)) & 0xffff;
if (chunk != 0) {
arm64_movk(s, rd, chunk, i * 16);
}
}
}
}
/*
* Register initialization
* Sets up available registers for allocation
*/
#define bit_R(x) ((unsigned long)1 << (x))
static void
arm64_reg_init(dill_stream s)
{
/* Variable registers (callee-saved) - x19-x28 */
s->p->var_i.init_avail[0] = (bit_R(_s0) | bit_R(_s1) | bit_R(_s2) | bit_R(_s3) |
bit_R(_s4) | bit_R(_s5) | bit_R(_s6) | bit_R(_s7) |
bit_R(_s8) | bit_R(_s9));
s->p->var_i.members[0] = s->p->var_i.init_avail[0];
/* Temporary registers (caller-saved) - x9-x15 */
s->p->tmp_i.init_avail[0] = (bit_R(_t0) | bit_R(_t1) | bit_R(_t2) | bit_R(_t3) |
bit_R(_t4) | bit_R(_t5) | bit_R(_t6));
s->p->tmp_i.members[0] = s->p->tmp_i.init_avail[0] |
(bit_R(_a0) | bit_R(_a1) | bit_R(_a2) | bit_R(_a3) |
bit_R(_a4) | bit_R(_a5) | bit_R(_a6) | bit_R(_a7));
/* Float variable registers (callee-saved lower 64 bits) - v8-v15 */
s->p->var_f.init_avail[0] = (bit_R(_v8) | bit_R(_v9) | bit_R(_v10) |
bit_R(_v11) | bit_R(_v12) | bit_R(_v13) |
bit_R(_v14) | bit_R(_v15));
s->p->var_f.members[0] = s->p->var_f.init_avail[0];
/* Float temporary registers - v16-v31 */
s->p->tmp_f.init_avail[0] = (bit_R(_v16) | bit_R(_v17) | bit_R(_v18) |
bit_R(_v19) | bit_R(_v20) | bit_R(_v21) |
bit_R(_v22) | bit_R(_v23) | bit_R(_v24) |
bit_R(_v25) | bit_R(_v26) | bit_R(_v27) |
bit_R(_v28) | bit_R(_v29) | bit_R(_v30) |
bit_R(_v31));
s->p->tmp_f.members[0] = s->p->tmp_f.init_avail[0] |
(bit_R(_v0) | bit_R(_v1) | bit_R(_v2) | bit_R(_v3) |
bit_R(_v4) | bit_R(_v5) | bit_R(_v6) | bit_R(_v7));
}
/*
* Create machine info structure
*/
void *
gen_arm64_mach_info(dill_stream s)
{
arm64_mach_info ami = malloc(sizeof(struct arm64_mach_info));
if (s->p->mach_info != NULL) {
free(s->p->mach_info);
s->p->mach_info = NULL;
s->p->native.mach_info = NULL;
}
arm64_reg_init(s);
memset(ami, 0, sizeof(struct arm64_mach_info));
ami->stack_align = 16; /* AArch64 requires 16-byte stack alignment */
ami->act_rec_size = 0;
ami->cur_arg_offset = 0;
ami->next_core_register = _x0;
ami->next_float_register = _v0;
ami->gp_save_offset = 0;
ami->fp_save_offset = 0;
ami->max_arg_size = 0;
return ami;
}
/*
* Procedure start
* Sets up parameter handling and function prologue
*/
void
arm64_proc_start(dill_stream s, char *subr_name, int arg_count,
arg_info_list args, dill_reg *arglist)
{
arm64_mach_info ami = (arm64_mach_info)s->p->mach_info;
int i;
int next_core_reg = _x0;
int next_float_reg = _v0;
int cur_arg_offset = 0;
/* Reset machine info */
/* Start act_rec_size at 224 to account for the save area allocated in prologue.
* Save area layout (FP-16 to FP-224):
* - Integer arg regs x0-x7: FP-16 to FP-80 (64 bytes)
* - Integer tmp regs x9-x15: FP-96 to FP-152 (56 bytes)
* - Float regs v0-v7: FP-160 to FP-224 (64 bytes)
* Local variables will be allocated below FP-224.
*/
ami->act_rec_size = 224;
ami->cur_arg_offset = 0;
ami->max_arg_size = 0;
/* Mark the function entry point */
s->p->fp = s->p->cur_ip;
/* Emit frame setup prologue:
* First save callee-saved registers (x19-x28, d8-d15), then FP/LR:
* stp x19, x20, [sp, #-16]! ; Save integer callee-saved regs (80 bytes)
* stp x21, x22, [sp, #-16]!
* stp x23, x24, [sp, #-16]!
* stp x25, x26, [sp, #-16]!
* stp x27, x28, [sp, #-16]!
* stp d8, d9, [sp, #-16]! ; Save float callee-saved regs (64 bytes)
* stp d10, d11, [sp, #-16]!
* stp d12, d13, [sp, #-16]!
* stp d14, d15, [sp, #-16]!
* stp x29, x30, [sp, #-16]! ; Save FP and LR
* mov x29, sp ; Set up frame pointer
* sub sp, sp, #224 ; Allocate save area
*
* Stack layout after prologue (offsets from FP):
* [FP + 160] = caller's stack (incoming stack args start here)
* [FP + 144] = saved x19/x20
* [FP + 128] = saved x21/x22
* [FP + 112] = saved x23/x24
* [FP + 96] = saved x25/x26
* [FP + 80] = saved x27/x28
* [FP + 64] = saved d8/d9
* [FP + 48] = saved d10/d11
* [FP + 32] = saved d12/d13
* [FP + 16] = saved d14/d15
* [FP + 8] = saved x30 (LR)
* [FP + 0] = saved x29 (old FP)
* [FP - 16] to [FP - 80] = integer arg reg save area (x0-x7)
* [FP - 96] to [FP - 152] = integer tmp reg save area (x9-x15)
* [FP - 160] to [FP - 224] = float reg save area (v0-v7)
* [SP] = bottom of save area (FP - 224)
*/
/* Save callee-saved integer registers x19-x28 */
/* STP x19, x20, [sp, #-16]! = 0xA9BF53F3 */
INSN_OUT(s, 0xA9BF53F3);
/* STP x21, x22, [sp, #-16]! = 0xA9BF5BF5 */
INSN_OUT(s, 0xA9BF5BF5);
/* STP x23, x24, [sp, #-16]! = 0xA9BF63F7 */
INSN_OUT(s, 0xA9BF63F7);
/* STP x25, x26, [sp, #-16]! = 0xA9BF6BF9 */
INSN_OUT(s, 0xA9BF6BF9);
/* STP x27, x28, [sp, #-16]! = 0xA9BF73FB */
INSN_OUT(s, 0xA9BF73FB);
/* Save callee-saved float registers d8-d15 (lower 64 bits of v8-v15) */
/* STP d8, d9, [sp, #-16]! = 0x6DBF27E8 */
INSN_OUT(s, 0x6DBF27E8);
/* STP d10, d11, [sp, #-16]! = 0x6DBF2FEA */
INSN_OUT(s, 0x6DBF2FEA);
/* STP d12, d13, [sp, #-16]! = 0x6DBF37EC */
INSN_OUT(s, 0x6DBF37EC);
/* STP d14, d15, [sp, #-16]! = 0x6DBF3FEE */
INSN_OUT(s, 0x6DBF3FEE);
/* STP x29, x30, [sp, #-16]! = 0xA9BF7BFD */
INSN_OUT(s, 0xA9BF7BFD);
/* MOV x29, sp (ADD x29, sp, #0) = 0x910003FD */
INSN_OUT(s, 0x910003FD);
/* SUB sp, sp, #224 = 0xD10383FF (imm12 = 224 = 0x0E0) */
INSN_OUT(s, 0xD10383FF);
/* Process arguments - determine register/stack location for each */
for (i = 0; i < arg_count; i++) {
int type = args[i].type;
int slot_size;
switch (type) {
case DILL_F:
/* Float arguments go in v0-v7 */
if (next_float_reg <= _v7) {
args[i].is_register = 1;
args[i].in_reg = next_float_reg;
args[i].out_reg = next_float_reg;
next_float_reg++;
} else {
args[i].is_register = 0;
args[i].offset = cur_arg_offset;
#ifdef __APPLE__
cur_arg_offset += 4;
#else
cur_arg_offset += 8; /* AAPCS64: 8-byte stack slots */
#endif
}
break;
case DILL_D:
/* Double arguments go in v0-v7 */
if (next_float_reg <= _v7) {
args[i].is_register = 1;
args[i].in_reg = next_float_reg;
args[i].out_reg = next_float_reg;
next_float_reg++;
} else {
/* 64-bit double needs 8-byte alignment */
if (cur_arg_offset & 7) cur_arg_offset = (cur_arg_offset + 7) & ~7;
args[i].is_register = 0;
args[i].offset = cur_arg_offset;
cur_arg_offset += 8;
}
break;
case DILL_C:
case DILL_UC:
case DILL_S:
case DILL_US:
case DILL_I:
case DILL_U:
/* Small integer arguments go in x0-x7 */
if (next_core_reg <= _x7) {
args[i].is_register = 1;
args[i].in_reg = next_core_reg;
args[i].out_reg = next_core_reg;
next_core_reg++;
} else {
args[i].is_register = 0;
args[i].offset = cur_arg_offset;
#ifdef __APPLE__
cur_arg_offset += 4;
#else
cur_arg_offset += 8; /* AAPCS64: 8-byte stack slots */
#endif
}
break;
default:
/* 64-bit types (L, UL, P) go in x0-x7 */
if (next_core_reg <= _x7) {
args[i].is_register = 1;
args[i].in_reg = next_core_reg;
args[i].out_reg = next_core_reg;
next_core_reg++;
} else {
/* 64-bit needs 8-byte alignment */
if (cur_arg_offset & 7) cur_arg_offset = (cur_arg_offset + 7) & ~7;
args[i].is_register = 0;
args[i].offset = cur_arg_offset;
cur_arg_offset += 8;
}
break;
}
}
/* Move parameters from argument registers to variable registers if needed */
for (i = 0; i < arg_count; i++) {
int tmp_reg;
if (args[i].is_register) {
/* Try to get a variable register to hold this parameter */
if (!dill_raw_getreg(s, &tmp_reg, args[i].type, DILL_VAR)) {
/* No variable register available, leave in argument register */
if (arglist != NULL) arglist[i] = args[i].in_reg;
continue;
}
/* Move from argument register to variable register */
arm64_mov(s, args[i].type, 0, tmp_reg, args[i].in_reg);
args[i].in_reg = tmp_reg;
args[i].out_reg = tmp_reg;
if (arglist != NULL) arglist[i] = tmp_reg;
} else {
/* Parameter is on stack - load it into a register */
int type = args[i].type;
if (!dill_raw_getreg(s, &tmp_reg, type, DILL_VAR)) {
/* No variable register available */
if (arglist != NULL) arglist[i] = -1;
continue;
}
/* Load from stack: incoming stack args are at [FP + 160 + offset]
* (+16 for FP/LR, +64 for d8-d15, +80 for x19-x28)
*/
arm64_ploadi(s, type, 0, tmp_reg, _fp, args[i].offset + 160);
args[i].in_reg = tmp_reg;
args[i].out_reg = tmp_reg;
args[i].is_register = 1;
if (arglist != NULL) arglist[i] = tmp_reg;
}
}
}
void
arm64_emit_save(dill_stream s)
{
arm64_mach_info ami = (arm64_mach_info)s->p->mach_info;
void *save_ip = s->p->cur_ip;
int ar_size = ami->act_rec_size;
/* Align to 16 bytes (ARM64 SP must be 16-byte aligned) */
ar_size = (ar_size + 15) & ~15;
s->p->fp = (char*)s->p->code_base;
/* Patch the SUB SP instruction in the prologue.
* The SUB SP, SP, #imm is the 12th instruction (index 11) after
* the 10 STP saves + MOV FP,SP.
* SUB SP, SP, #imm = 0xD10003FF | (imm12 << 10)
*/
unsigned int *prologue = (unsigned int*)s->p->fp;
prologue[11] = 0xD10003FF | ((ar_size & 0xFFF) << 10);
s->p->cur_ip = save_ip;
}
/*
* Procedure end
*/
void
arm64_end(dill_stream s)
{
arm64_nop(s);
arm64_simple_ret(s);
arm64_PLT_emit(s, 0); /* must be done before linking */
arm64_branch_link(s);
arm64_call_link(s);
arm64_data_link(s);
arm64_emit_save(s);
arm64_flush(s->p->code_base, s->p->code_limit);
}
/*
* Package end (for serializable code)
*/
void
arm64_package_end(dill_stream s)
{
arm64_nop(s);
arm64_simple_ret(s);
arm64_PLT_emit(s, 1); /* package=1 means emit PLT for all calls */
arm64_branch_link(s);
arm64_emit_save(s);
}
/*
* Clone code to new location
*/
void *
arm64_clone_code(dill_stream s, void *new_base, int available_size)
{
int size = dill_code_size(s);
void *old_base = s->p->code_base;
void *native_base = s->p->code_base;
if (available_size < size) {
return NULL;
}
if (native_base == NULL)
native_base = s->p->native.code_base;
memcpy(new_base, native_base, size);
s->p->code_base = new_base;
s->p->cur_ip = (char*)new_base + size;
s->p->fp = new_base;
arm64_branch_link(s);
arm64_call_link(s);
arm64_data_link(s);
s->p->code_base = old_base;
s->p->cur_ip = (char*)old_base + size;
s->p->fp = old_base;
arm64_flush(new_base, (char*)new_base + size);
return new_base;
}
/*
* Return a value in a register
*/
void
arm64_ret(dill_stream s, int data1, int data2, int src)
{
switch (data1) {
case DILL_C:
case DILL_UC:
case DILL_S:
case DILL_US:
case DILL_I:
case DILL_U:
case DILL_L:
case DILL_UL:
case DILL_P:
/* Move src to x0 (return register) if needed */
if (src != _x0) {
/* MOV x0, src using ORR */
/* ORR Xd, XZR, Xm : 1 01 01010 00 0 Xm 000000 11111 Xd */
unsigned int insn = 0xaa0003e0 | (src << 16) | _x0;
INSN_OUT(s, insn);
}
break;
case DILL_F:
/* Move to v0/s0 if needed */
if (src != _v0) {
/* FMOV Sd, Sn: 0001 1110 0010 0000 0100 00 Sn Sd */
unsigned int insn = 0x1e204000 | (src << 5) | _v0;
INSN_OUT(s, insn);
}
break;
case DILL_D:
/* Move to v0/d0 if needed */
if (src != _v0) {
/* FMOV Dd, Dn: 0001 1110 0110 0000 0100 00 Dn Dd */
unsigned int insn = 0x1e604000 | (src << 5) | _v0;
INSN_OUT(s, insn);
}
break;
}
arm64_simple_ret(s);
}
/*
* Return an immediate value - THIS IS THE KEY FUNCTION FOR T1
*/
void
arm64_reti(dill_stream s, int data1, int data2, IMM_TYPE imm)
{
switch (data1) {
case DILL_C:
case DILL_UC:
case DILL_S:
case DILL_US:
case DILL_I:
case DILL_U:
case DILL_L:
case DILL_UL:
case DILL_P:
/* Load immediate into x0 (return register) */
arm64_set64(s, _x0, (unsigned long)imm);
break;
case DILL_F:
case DILL_D:
break; /* no return immediate of floats */
}
arm64_simple_ret(s);
}
/*
* Return a floating point immediate
*/
void
arm64_retf(dill_stream s, int data1, int data2, double imm)
{
union {
float f;
unsigned int i;
} a;
union {
double d;
unsigned long l;
} b;
switch (data1) {
case DILL_F:
a.f = (float)imm;
/* Load float bits into w0, then move to s0 */
arm64_set64(s, _x0, a.i);
/* FMOV Sd, Wn: 0001 1110 0010 0111 0000 00 Wn Sd */
INSN_OUT(s, 0x1e270000 | (_x0 << 5) | _v0);
break;
case DILL_D:
b.d = imm;
/* Load double bits into x0, then move to d0 */
arm64_set64(s, _x0, b.l);
/* FMOV Dd, Xn: 1001 1110 0110 0111 0000 00 Xn Dd */
INSN_OUT(s, 0x9e670000 | (_x0 << 5) | _v0);
break;
}
arm64_simple_ret(s);
}
/*
* Stub implementations for other required functions
*/
void
arm64_ploadi(dill_stream s, int type, int junk, int dest, int src, IMM_TYPE offset)
{
unsigned int insn;
switch (type) {
case DILL_C:
/* LDRSB (signed byte, sign-extend to 64-bit): load and sign extend */
if (offset >= 0 && offset < 4096) {
/* LDRSB Xt (unsigned offset): 0x39800000 */
insn = 0x39800000 | ((offset & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDURSB Xt (unscaled): 0x38800000 */
insn = 0x38800000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_UC:
/* LDRB (unsigned byte, zero-extend) */
if (offset >= 0 && offset < 4096) {
/* LDRB Wt (unsigned offset): 0x39400000 */
insn = 0x39400000 | ((offset & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDURB (unscaled): 0x38400000 */
insn = 0x38400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_S:
/* LDRSH (signed halfword, sign-extend to 64-bit) */
if (offset >= 0 && (offset & 1) == 0 && offset < 8192) {
/* LDRSH Xt (unsigned offset, scaled): 0x79800000 */
insn = 0x79800000 | (((offset >> 1) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDURSH Xt (unscaled): 0x78800000 */
insn = 0x78800000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_US:
/* LDRH (unsigned halfword, zero-extend) */
if (offset >= 0 && (offset & 1) == 0 && offset < 8192) {
/* LDRH Wt (unsigned offset, scaled): 0x79400000 */
insn = 0x79400000 | (((offset >> 1) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDURH (unscaled): 0x78400000 */
insn = 0x78400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_I:
/* LDRSW (sign-extend 32-bit to 64-bit) for signed int */
if (offset >= 0 && (offset & 3) == 0 && offset < 16384) {
/* LDRSW Xt (unsigned offset, scaled by 4): 0xB9800000 */
insn = 0xB9800000 | (((offset >> 2) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDURSW Xt (unscaled): 0xB8800000 */
insn = 0xB8800000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_U:
/* LDR Wt (zero-extend 32-bit to 64-bit) for unsigned int */
if (offset >= 0 && (offset & 3) == 0 && offset < 16384) {
/* LDR Wt (unsigned offset, scaled by 4): 0xB9400000 */
insn = 0xB9400000 | (((offset >> 2) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDUR Wt (unscaled): 0xB8400000 */
insn = 0xB8400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_L:
case DILL_UL:
case DILL_P:
/* LDR Xt (64-bit) */
if (offset >= 0 && (offset & 7) == 0 && offset < 32768) {
/* LDR Xt (unsigned offset, scaled): 0xF9400000 */
insn = 0xF9400000 | (((offset >> 3) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDUR Xt (unscaled): 0xF8400000 */
insn = 0xF8400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_F:
/* LDR St (32-bit float) */
if (offset >= 0 && (offset & 3) == 0 && offset < 16384) {
/* LDR St (unsigned offset, scaled): 0xBD400000 */
insn = 0xBD400000 | (((offset >> 2) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDUR St (unscaled): 0xBC400000 */
insn = 0xBC400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
case DILL_D:
/* LDR Dt (64-bit double) */
if (offset >= 0 && (offset & 7) == 0 && offset < 32768) {
/* LDR Dt (unsigned offset, scaled): 0xFD400000 */
insn = 0xFD400000 | (((offset >> 3) & 0xfff) << 10) | (src << 5) | dest;
} else if (offset >= -256 && offset < 256) {
/* LDUR Dt (unscaled): 0xFC400000 */
insn = 0xFC400000 | ((offset & 0x1ff) << 12) | (src << 5) | dest;
} else {
arm64_set64(s, _x16, offset);
arm64_pload(s, type, junk, dest, src, _x16);
return;
}
break;
default:
return;
}
INSN_OUT(s, insn);
}
void
arm64_pload(dill_stream s, int type, int junk, int dest, int src1, int src2)
{
/* Load with register offset: LDR Rt, [Xn, Xm] */
unsigned int insn;
switch (type) {
case DILL_C:
/* LDRSB Xt, [Xn, Xm] (sign extend byte to 64-bit): 0x38A06800 */
insn = 0x38A06800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_UC:
/* LDRB Wt, [Xn, Xm] (zero extend byte): 0x38606800 */
insn = 0x38606800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_S:
/* LDRSH Xt, [Xn, Xm] (sign extend halfword to 64-bit): 0x78A06800 */
insn = 0x78A06800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_US:
/* LDRH Wt, [Xn, Xm] (zero extend halfword): 0x78606800 */
insn = 0x78606800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_I:
/* LDRSW Xt, [Xn, Xm] (sign extend 32-bit to 64-bit): 0xB8A06800 */
insn = 0xB8A06800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_U:
/* LDR Wt, [Xn, Xm] (zero extend 32-bit): 0xB8606800 */
insn = 0xB8606800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_L:
case DILL_UL:
case DILL_P:
/* LDR Xt, [Xn, Xm] (64-bit): 0xF8606800 */
insn = 0xF8606800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_F:
/* LDR St, [Xn, Xm] (32-bit float): 0xBC606800 */
insn = 0xBC606800 | (src2 << 16) | (src1 << 5) | dest;
break;
case DILL_D:
/* LDR Dt, [Xn, Xm] (64-bit double): 0xFC606800 */
insn = 0xFC606800 | (src2 << 16) | (src1 << 5) | dest;
break;
default:
return;
}
INSN_OUT(s, insn);
}