-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
unwindarm64.cpp
1102 lines (927 loc) · 35.6 KB
/
unwindarm64.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX UnwindInfo XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if defined(TARGET_ARM64)
#if defined(FEATURE_CFI_SUPPORT)
short Compiler::mapRegNumToDwarfReg(regNumber reg)
{
short dwarfReg = DWARF_REG_ILLEGAL;
switch (reg)
{
case REG_R0:
dwarfReg = 0;
break;
case REG_R1:
dwarfReg = 1;
break;
case REG_R2:
dwarfReg = 2;
break;
case REG_R3:
dwarfReg = 3;
break;
case REG_R4:
dwarfReg = 4;
break;
case REG_R5:
dwarfReg = 5;
break;
case REG_R6:
dwarfReg = 6;
break;
case REG_R7:
dwarfReg = 7;
break;
case REG_R8:
dwarfReg = 8;
break;
case REG_R9:
dwarfReg = 9;
break;
case REG_R10:
dwarfReg = 10;
break;
case REG_R11:
dwarfReg = 11;
break;
case REG_R12:
dwarfReg = 12;
break;
case REG_R13:
dwarfReg = 13;
break;
case REG_R14:
dwarfReg = 14;
break;
case REG_R15:
dwarfReg = 15;
break;
case REG_R16:
dwarfReg = 16;
break;
case REG_R17:
dwarfReg = 17;
break;
case REG_R18:
dwarfReg = 18;
break;
case REG_R19:
dwarfReg = 19;
break;
case REG_R20:
dwarfReg = 20;
break;
case REG_R21:
dwarfReg = 21;
break;
case REG_R22:
dwarfReg = 22;
break;
case REG_R23:
dwarfReg = 23;
break;
case REG_R24:
dwarfReg = 24;
break;
case REG_R25:
dwarfReg = 25;
break;
case REG_R26:
dwarfReg = 26;
break;
case REG_R27:
dwarfReg = 27;
break;
case REG_R28:
dwarfReg = 28;
break;
case REG_R29:
dwarfReg = 29;
break;
case REG_R30:
dwarfReg = 30;
break;
case REG_SP:
dwarfReg = 31;
break;
case REG_V0:
dwarfReg = 64;
break;
case REG_V1:
dwarfReg = 65;
break;
case REG_V2:
dwarfReg = 66;
break;
case REG_V3:
dwarfReg = 67;
break;
case REG_V4:
dwarfReg = 68;
break;
case REG_V5:
dwarfReg = 69;
break;
case REG_V6:
dwarfReg = 70;
break;
case REG_V7:
dwarfReg = 71;
break;
case REG_V8:
dwarfReg = 72;
break;
case REG_V9:
dwarfReg = 73;
break;
case REG_V10:
dwarfReg = 74;
break;
case REG_V11:
dwarfReg = 75;
break;
case REG_V12:
dwarfReg = 76;
break;
case REG_V13:
dwarfReg = 77;
break;
case REG_V14:
dwarfReg = 78;
break;
case REG_V15:
dwarfReg = 79;
break;
case REG_V16:
dwarfReg = 80;
break;
case REG_V17:
dwarfReg = 81;
break;
case REG_V18:
dwarfReg = 82;
break;
case REG_V19:
dwarfReg = 83;
break;
case REG_V20:
dwarfReg = 84;
break;
case REG_V21:
dwarfReg = 85;
break;
case REG_V22:
dwarfReg = 86;
break;
case REG_V23:
dwarfReg = 87;
break;
case REG_V24:
dwarfReg = 88;
break;
case REG_V25:
dwarfReg = 89;
break;
case REG_V26:
dwarfReg = 90;
break;
case REG_V27:
dwarfReg = 91;
break;
case REG_V28:
dwarfReg = 92;
break;
case REG_V29:
dwarfReg = 93;
break;
case REG_V30:
dwarfReg = 94;
break;
case REG_V31:
dwarfReg = 95;
break;
default:
NYI("CFI codes");
}
return dwarfReg;
}
#endif // FEATURE_CFI_SUPPORT
void Compiler::unwindPush(regNumber reg)
{
unreached(); // use one of the unwindSaveReg* functions instead.
}
void Compiler::unwindAllocStack(unsigned size)
{
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
unwindAllocStackCFI(size);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
assert(size % 16 == 0);
unsigned x = size / 16;
if (x <= 0x1F)
{
// alloc_s: 000xxxxx: allocate small stack with size < 128 (2^5 * 16)
// TODO-Review: should say size < 512
pu->AddCode((BYTE)x);
}
else if (x <= 0x7FF)
{
// alloc_m: 11000xxx | xxxxxxxx: allocate large stack with size < 16k (2^11 * 16)
// TODO-Review: should say size < 32K
pu->AddCode(0xC0 | (BYTE)(x >> 8), (BYTE)x);
}
else
{
// alloc_l: 11100000 | xxxxxxxx | xxxxxxxx | xxxxxxxx : allocate large stack with size < 256M (2^24 * 16)
//
// For large stack size, the most significant bits
// are stored first (and next to the opCode) per the unwind spec.
pu->AddCode(0xE0, (BYTE)(x >> 16), (BYTE)(x >> 8), (BYTE)x);
}
}
void Compiler::unwindSetFrameReg(regNumber reg, unsigned offset)
{
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
unwindSetFrameRegCFI(reg, offset);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
if (offset == 0)
{
assert(reg == REG_FP);
// set_fp: 11100001 : set up r29 : with : mov r29, sp
pu->AddCode(0xE1);
}
else
{
// add_fp: 11100010 | xxxxxxxx : set up r29 with : add r29, sp, #x * 8
assert(reg == REG_FP);
assert((offset % 8) == 0);
unsigned x = offset / 8;
assert(x <= 0xFF);
pu->AddCode(0xE2, (BYTE)x);
}
}
void Compiler::unwindSaveReg(regNumber reg, unsigned offset)
{
unreached();
}
void Compiler::unwindNop()
{
UnwindInfo* pu = &funCurrentFunc()->uwi;
#ifdef DEBUG
if (verbose)
{
printf("unwindNop: adding NOP\n");
}
#endif
INDEBUG(pu->uwiAddingNOP = true);
// nop: 11100011: no unwind operation is required.
pu->AddCode(0xE3);
INDEBUG(pu->uwiAddingNOP = false);
}
// unwindSaveRegPair: save a register pair to the stack at the specified byte offset (which must be positive,
// a multiple of 8 from 0 to 504). Note that for ARM64 unwind codes, reg2 must be exactly one register higher than reg1,
// except for the case of a pair including LR, in which case reg1 must be either FP or R19/R21/R23/R25/R27 (note that it
// can't be even, such as R20, because that would mean R19 was saved separately, instead of saving <R19,R20> as a pair,
// which we should do instead).
void Compiler::unwindSaveRegPair(regNumber reg1, regNumber reg2, int offset)
{
// stp reg1, reg2, [sp, #offset]
// offset for store pair in prolog must be positive and a multiple of 8.
assert(0 <= offset && offset <= 504);
assert((offset % 8) == 0);
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
FuncInfoDsc* func = funCurrentFunc();
UNATIVE_OFFSET cbProlog = unwindGetCurrentOffset(func);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg1), offset);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg2), offset + 8);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
int z = offset / 8;
assert(0 <= z && z <= 0x3F);
if (reg1 == REG_FP)
{
// save_fplr: 01zzzzzz: save <r29,lr> pair at [sp+#Z*8], offset <= 504
assert(reg2 == REG_LR);
pu->AddCode(0x40 | (BYTE)z);
}
else if (reg2 == REG_LR)
{
// save_lrpair: 1101011x | xxzzzzzz: save pair <r19 + 2 * #X, lr> at [sp + #Z * 8], offset <= 504
assert(REG_R19 <= reg1 && // first legal pair: R19, LR
reg1 <= REG_R27); // last legal pair: R27, LR
BYTE x = (BYTE)(reg1 - REG_R19);
assert((x % 2) == 0); // only legal reg1: R19, R21, R23, R25, R27
x /= 2;
assert(0 <= x && x <= 0x7);
pu->AddCode(0xD6 | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
else if (emitter::isGeneralRegister(reg1))
{
// save_regp: 110010xx | xxzzzzzz: save r(19 + #X) pair at [sp + #Z * 8], offset <= 504
assert(REG_NEXT(reg1) == reg2);
assert(REG_R19 <= reg1 && // first legal pair: R19, R20
reg1 <= REG_R27); // last legal pair: R27, R28 (FP is never saved without LR)
BYTE x = (BYTE)(reg1 - REG_R19);
assert(0 <= x && x <= 0xF);
pu->AddCode(0xC8 | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
else
{
// save_fregp: 1101100x | xxzzzzzz : save pair d(8 + #X) at [sp + #Z * 8], offset <= 504
assert(REG_NEXT(reg1) == reg2);
assert(REG_V8 <= reg1 && // first legal pair: V8, V9
reg1 <= REG_V14); // last legal pair: V14, V15
BYTE x = (BYTE)(reg1 - REG_V8);
assert(0 <= x && x <= 0x7);
pu->AddCode(0xD8 | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
}
// unwindSaveRegPairPreindexed: save a register pair to the stack at the specified byte offset (which must be negative,
// a multiple of 8 from -512 to -8). Note that for ARM64 unwind codes, reg2 must be exactly one register higher than
// reg1.
void Compiler::unwindSaveRegPairPreindexed(regNumber reg1, regNumber reg2, int offset)
{
// stp reg1, reg2, [sp, #offset]!
// pre-indexed offset in prolog must be negative and a multiple of 8.
assert(offset < 0);
assert((offset % 8) == 0);
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
FuncInfoDsc* func = funCurrentFunc();
UNATIVE_OFFSET cbProlog = unwindGetCurrentOffset(func);
createCfiCode(func, cbProlog, CFI_ADJUST_CFA_OFFSET, DWARF_REG_ILLEGAL, -offset);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg1), 0);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg2), 8);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
if (reg1 == REG_FP)
{
// save_fplr_x: 10zzzzzz: save <r29,lr> pair at [sp-(#Z+1)*8]!, pre-indexed offset >= -512
assert(-512 <= offset);
int z = (-offset) / 8 - 1;
assert(0 <= z && z <= 0x3F);
assert(reg2 == REG_LR);
pu->AddCode(0x80 | (BYTE)z);
}
else if ((reg1 == REG_R19) && (-256 <= offset)) // If the offset is between -512 and -256, we use the save_regp_x
// unwind code.
{
// save_r19r20_x: 001zzzzz: save <r19,r20> pair at [sp-#Z*8]!, pre-indexed offset >= -248
// NOTE: I'm not sure why we allow Z==0 here; seems useless, and the calculation of offset is different from the
// other cases.
int z = (-offset) / 8;
assert(0 <= z && z <= 0x1F);
assert(reg2 == REG_R20);
pu->AddCode(0x20 | (BYTE)z);
}
else if (emitter::isGeneralRegister(reg1))
{
// save_regp_x: 110011xx | xxzzzzzz: save pair r(19 + #X) at [sp - (#Z + 1) * 8]!, pre-indexed offset >= -512
assert(-512 <= offset);
int z = (-offset) / 8 - 1;
assert(0 <= z && z <= 0x3F);
assert(REG_NEXT(reg1) == reg2);
assert(REG_R19 <= reg1 && // first legal pair: R19, R20
reg1 <= REG_R27); // last legal pair: R27, R28 (FP is never saved without LR)
BYTE x = (BYTE)(reg1 - REG_R19);
assert(0 <= x && x <= 0xF);
pu->AddCode(0xCC | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
else
{
// save_fregp_x: 1101101x | xxzzzzzz : save pair d(8 + #X), at [sp - (#Z + 1) * 8]!, pre-indexed offset >= -512
assert(-512 <= offset);
int z = (-offset) / 8 - 1;
assert(0 <= z && z <= 0x3F);
assert(REG_NEXT(reg1) == reg2);
assert(REG_V8 <= reg1 && // first legal pair: V8, V9
reg1 <= REG_V14); // last legal pair: V14, V15
BYTE x = (BYTE)(reg1 - REG_V8);
assert(0 <= x && x <= 0x7);
pu->AddCode(0xDA | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
}
void Compiler::unwindSaveReg(regNumber reg, int offset)
{
// str reg, [sp, #offset]
// offset for store in prolog must be positive and a multiple of 8.
assert(0 <= offset && offset <= 504);
assert((offset % 8) == 0);
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
FuncInfoDsc* func = funCurrentFunc();
UNATIVE_OFFSET cbProlog = unwindGetCurrentOffset(func);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg), offset);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
int z = offset / 8;
assert(0 <= z && z <= 0x3F);
UnwindInfo* pu = &funCurrentFunc()->uwi;
if (emitter::isGeneralRegister(reg))
{
// save_reg: 110100xx | xxzzzzzz: save reg r(19 + #X) at [sp + #Z * 8], offset <= 504
assert(REG_R19 <= reg && // first legal register: R19
reg <= REG_LR); // last legal register: LR
BYTE x = (BYTE)(reg - REG_R19);
assert(0 <= x && x <= 0xF);
pu->AddCode(0xD0 | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
else
{
// save_freg: 1101110x | xxzzzzzz : save reg d(8 + #X) at [sp + #Z * 8], offset <= 504
assert(REG_V8 <= reg && // first legal register: V8
reg <= REG_V15); // last legal register: V15
BYTE x = (BYTE)(reg - REG_V8);
assert(0 <= x && x <= 0x7);
pu->AddCode(0xDC | (BYTE)(x >> 2), (BYTE)(x << 6) | (BYTE)z);
}
}
void Compiler::unwindSaveRegPreindexed(regNumber reg, int offset)
{
// str reg, [sp, #offset]!
// pre-indexed offset in prolog must be negative and a multiple of 8.
assert(-256 <= offset && offset < 0);
assert((offset % 8) == 0);
#if defined(FEATURE_CFI_SUPPORT)
if (generateCFIUnwindCodes())
{
if (compGeneratingProlog)
{
FuncInfoDsc* func = funCurrentFunc();
UNATIVE_OFFSET cbProlog = unwindGetCurrentOffset(func);
createCfiCode(func, cbProlog, CFI_ADJUST_CFA_OFFSET, DWARF_REG_ILLEGAL, -offset);
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg), 0);
}
return;
}
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
int z = (-offset) / 8 - 1;
assert(0 <= z && z <= 0x1F);
if (emitter::isGeneralRegister(reg))
{
// save_reg_x: 1101010x | xxxzzzzz: save reg r(19 + #X) at [sp - (#Z + 1) * 8]!, pre-indexed offset >= -256
assert(REG_R19 <= reg && // first legal register: R19
reg <= REG_LR); // last legal register: LR
BYTE x = (BYTE)(reg - REG_R19);
assert(0 <= x && x <= 0xF);
pu->AddCode(0xD4 | (BYTE)(x >> 3), (BYTE)(x << 5) | (BYTE)z);
}
else
{
// save_freg_x: 11011110 | xxxzzzzz : save reg d(8 + #X) at [sp - (#Z + 1) * 8]!, pre - indexed offset >= -256
assert(REG_V8 <= reg && // first legal register: V8
reg <= REG_V15); // last legal register: V15
BYTE x = (BYTE)(reg - REG_V8);
assert(0 <= x && x <= 0x7);
pu->AddCode(0xDE, (BYTE)(x << 5) | (BYTE)z);
}
}
void Compiler::unwindSaveNext()
{
#if defined(FEATURE_CFI_SUPPORT)
// do not use unwindSaveNext when generating CFI codes as there is no code for this
assert(!generateCFIUnwindCodes());
#endif // FEATURE_CFI_SUPPORT
UnwindInfo* pu = &funCurrentFunc()->uwi;
// We're saving the next register pair. The caller is responsible for ensuring this is correct!
// save_next: 11100110 : save next non - volatile Int or FP register pair.
pu->AddCode(0xE6);
}
void Compiler::unwindReturn(regNumber reg)
{
// Nothing to do; we will always have at least one trailing "end" opcode in our padding.
}
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Unwind Info Debug helpers XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#ifdef DEBUG
// Return the size of the unwind code (from 1 to 4 bytes), given the first byte of the unwind bytes
unsigned GetUnwindSizeFromUnwindHeader(BYTE b1)
{
static BYTE s_UnwindSize[256] = {
// array of unwind sizes, in bytes (as specified in the ARM unwind specification)
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00-0F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 10-1F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 20-2F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 30-3F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40-4F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 50-5F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60-6F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70-7F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80-8F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 90-9F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A0-AF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B0-BF
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0-CF
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, // D0-DF
4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E0-EF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F0-FF
};
unsigned size = s_UnwindSize[b1];
assert(1 <= size && size <= 4);
return size;
}
#endif // DEBUG
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Unwind Info Support Classes XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
///////////////////////////////////////////////////////////////////////////////
//
// UnwindCodesBase
//
///////////////////////////////////////////////////////////////////////////////
#ifdef DEBUG
// Walk the prolog codes and calculate the size of the prolog or epilog, in bytes.
unsigned UnwindCodesBase::GetCodeSizeFromUnwindCodes(bool isProlog)
{
BYTE* pCodesStart = GetCodes();
BYTE* pCodes = pCodesStart;
unsigned size = 0;
for (;;)
{
BYTE b1 = *pCodes;
if (IsEndCode(b1))
{
break; // We hit an "end" code; we're done
}
size += 4; // All codes represent 4 byte instructions.
pCodes += GetUnwindSizeFromUnwindHeader(b1);
assert(pCodes - pCodesStart < 256); // 255 is the absolute maximum number of code bytes allowed
}
return size;
}
#endif // DEBUG
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Debug dumpers XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#ifdef DEBUG
// start is 0-based index from LSB, length is number of bits
DWORD ExtractBits(DWORD dw, DWORD start, DWORD length)
{
return (dw >> start) & ((1 << length) - 1);
}
// Dump the unwind data.
// Arguments:
// isHotCode: true if this unwind data is for the hot section
// startOffset: byte offset of the code start that this unwind data represents
// endOffset: byte offset of the code end that this unwind data represents
// pHeader: pointer to the unwind data blob
// unwindBlockSize: size in bytes of the unwind data blob
void DumpUnwindInfo(Compiler* comp,
bool isHotCode,
UNATIVE_OFFSET startOffset,
UNATIVE_OFFSET endOffset,
const BYTE* const pHeader,
ULONG unwindBlockSize)
{
printf("Unwind Info%s:\n", isHotCode ? "" : " COLD");
// pHeader is not guaranteed to be aligned. We put four 0xFF end codes at the end
// to provide padding, and round down to get a multiple of 4 bytes in size.
DWORD UNALIGNED* pdw = (DWORD UNALIGNED*)pHeader;
DWORD dw;
dw = *pdw++;
DWORD codeWords = ExtractBits(dw, 27, 5);
DWORD epilogCount = ExtractBits(dw, 22, 5);
DWORD EBit = ExtractBits(dw, 21, 1);
DWORD XBit = ExtractBits(dw, 20, 1);
DWORD Vers = ExtractBits(dw, 18, 2);
DWORD functionLength = ExtractBits(dw, 0, 18);
printf(" >> Start offset : 0x%06x (not in unwind data)\n", comp->dspOffset(startOffset));
printf(" >> End offset : 0x%06x (not in unwind data)\n", comp->dspOffset(endOffset));
printf(" Code Words : %u\n", codeWords);
printf(" Epilog Count : %u\n", epilogCount);
printf(" E bit : %u\n", EBit);
printf(" X bit : %u\n", XBit);
printf(" Vers : %u\n", Vers);
printf(" Function Length : %u (0x%05x) Actual length = %u (0x%06x)\n", functionLength, functionLength,
functionLength * 4, functionLength * 4);
assert(functionLength * 4 == endOffset - startOffset);
if (codeWords == 0 && epilogCount == 0)
{
// We have an extension word specifying a larger number of Code Words or Epilog Counts
// than can be specified in the header word.
dw = *pdw++;
codeWords = ExtractBits(dw, 16, 8);
epilogCount = ExtractBits(dw, 0, 16);
assert((dw & 0xF0000000) == 0); // reserved field should be zero
printf(" ---- Extension word ----\n");
printf(" Extended Code Words : %u\n", codeWords);
printf(" Extended Epilog Count : %u\n", epilogCount);
}
bool epilogStartAt[1024] = {}; // One byte per possible epilog start index; initialized to false
if (EBit == 0)
{
// We have an array of epilog scopes
printf(" ---- Epilog scopes ----\n");
if (epilogCount == 0)
{
printf(" No epilogs\n");
}
else
{
for (DWORD scope = 0; scope < epilogCount; scope++)
{
dw = *pdw++;
DWORD epilogStartOffset = ExtractBits(dw, 0, 18);
DWORD res = ExtractBits(dw, 18, 4);
DWORD epilogStartIndex = ExtractBits(dw, 22, 10);
// Note that epilogStartOffset for a funclet is the offset from the beginning
// of the current funclet, not the offset from the beginning of the main function.
// To help find it when looking through JitDump output, also show the offset from
// the beginning of the main function.
DWORD epilogStartOffsetFromMainFunctionBegin = epilogStartOffset * 4 + startOffset;
assert(res == 0);
printf(" ---- Scope %d\n", scope);
printf(" Epilog Start Offset : %u (0x%05x) Actual offset = %u (0x%06x) Offset from main "
"function begin = %u (0x%06x)\n",
comp->dspOffset(epilogStartOffset), comp->dspOffset(epilogStartOffset),
comp->dspOffset(epilogStartOffset * 4), comp->dspOffset(epilogStartOffset * 4),
comp->dspOffset(epilogStartOffsetFromMainFunctionBegin),
comp->dspOffset(epilogStartOffsetFromMainFunctionBegin));
printf(" Epilog Start Index : %u (0x%02x)\n", epilogStartIndex, epilogStartIndex);
epilogStartAt[epilogStartIndex] = true; // an epilog starts at this offset in the unwind codes
}
}
}
else
{
printf(" --- One epilog, unwind codes at %u\n", epilogCount);
assert(epilogCount < ArrLen(epilogStartAt));
epilogStartAt[epilogCount] = true; // the one and only epilog starts its unwind codes at this offset
}
// Dump the unwind codes
printf(" ---- Unwind codes ----\n");
DWORD countOfUnwindCodes = codeWords * 4;
PBYTE pUnwindCode = (PBYTE)pdw;
BYTE b1, b2, b3, b4;
DWORD x, z;
for (DWORD i = 0; i < countOfUnwindCodes; i++)
{
// Does this byte start an epilog sequence? If so, note that fact.
if (epilogStartAt[i])
{
printf(" ---- Epilog start at index %u ----\n", i);
}
b1 = *pUnwindCode++;
if ((b1 & 0xE0) == 0)
{
// alloc_s: 000xxxxx: allocate small stack with size < 128 (2^5 * 16)
// TODO-Review:should say size < 512
x = b1 & 0x1F;
printf(" %02X alloc_s #%u (0x%02X); sub sp, sp, #%u (0x%03X)\n", b1, x, x, x * 16, x * 16);
}
else if ((b1 & 0xE0) == 0x20)
{
// save_r19r20_x: 001zzzzz: save <r19,r20> pair at [sp-#Z*8]!, pre-indexed offset >= -248
z = b1 & 0x1F;
printf(" %02X save_r19r20_x #%u (0x%02X); stp %s, %s, [sp, #-%u]!\n", b1, z, z,
getRegName(REG_R19), getRegName(REG_R20), z * 8);
}
else if ((b1 & 0xC0) == 0x40)
{
// save_fplr: 01zzzzzz: save <r29,lr> pair at [sp+#Z*8], offset <= 504
z = b1 & 0x3F;
printf(" %02X save_fplr #%u (0x%02X); stp %s, %s, [sp, #%u]\n", b1, z, z, getRegName(REG_FP),
getRegName(REG_LR), z * 8);
}
else if ((b1 & 0xC0) == 0x80)
{
// save_fplr_x: 10zzzzzz: save <r29,lr> pair at [sp-(#Z+1)*8]!, pre-indexed offset >= -512
z = b1 & 0x3F;
printf(" %02X save_fplr_x #%u (0x%02X); stp %s, %s, [sp, #-%u]!\n", b1, z, z,
getRegName(REG_FP), getRegName(REG_LR), (z + 1) * 8);
}
else if ((b1 & 0xF8) == 0xC0)
{
// alloc_m: 11000xxx | xxxxxxxx: allocate large stack with size < 16k (2^11 * 16)
// TODO-Review: should save size < 32K
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x7) << 8) | (DWORD)b2;
printf(" %02X %02X alloc_m #%u (0x%03X); sub sp, sp, #%u (0x%04X)\n", b1, b2, x, x, x * 16,
x * 16);
}
else if ((b1 & 0xFC) == 0xC8)
{
// save_regp: 110010xx | xxzzzzzz: save r(19 + #X) pair at [sp + #Z * 8], offset <= 504
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x3) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_regp X#%u Z#%u (0x%02X); stp %s, %s, [sp, #%u]\n", b1, b2, x, z, z,
getRegName(REG_R19 + x), getRegName(REG_R19 + x + 1), z * 8);
}
else if ((b1 & 0xFC) == 0xCC)
{
// save_regp_x: 110011xx | xxzzzzzz: save pair r(19 + #X) at [sp - (#Z + 1) * 8]!, pre-indexed offset >=
// -512
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x3) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_regp_x X#%u Z#%u (0x%02X); stp %s, %s, [sp, #-%u]!\n", b1, b2, x, z, z,
getRegName(REG_R19 + x), getRegName(REG_R19 + x + 1), (z + 1) * 8);
}
else if ((b1 & 0xFC) == 0xD0)
{
// save_reg: 110100xx | xxzzzzzz: save reg r(19 + #X) at [sp + #Z * 8], offset <= 504
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x3) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_reg X#%u Z#%u (0x%02X); str %s, [sp, #%u]\n", b1, b2, x, z, z,
getRegName(REG_R19 + x), z * 8);
}
else if ((b1 & 0xFE) == 0xD4)
{
// save_reg_x: 1101010x | xxxzzzzz: save reg r(19 + #X) at [sp - (#Z + 1) * 8]!, pre-indexed offset >= -256
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x1) << 3) | (DWORD)(b2 >> 5);
z = (DWORD)(b2 & 0x1F);
printf(" %02X %02X save_reg_x X#%u Z#%u (0x%02X); str %s, [sp, #-%u]!\n", b1, b2, x, z, z,
getRegName(REG_R19 + x), (z + 1) * 8);
}
else if ((b1 & 0xFE) == 0xD6)
{
// save_lrpair: 1101011x | xxzzzzzz: save pair <r19 + 2 * #X, lr> at [sp + #Z * 8], offset <= 504
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x1) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_lrpair X#%u Z#%u (0x%02X); stp %s, %s, [sp, #%u]\n", b1, b2, x, z, z,
getRegName(REG_R19 + 2 * x), getRegName(REG_LR), z * 8);
}
else if ((b1 & 0xFE) == 0xD8)
{
// save_fregp: 1101100x | xxzzzzzz : save pair d(8 + #X) at [sp + #Z * 8], offset <= 504
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x1) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_fregp X#%u Z#%u (0x%02X); stp %s, %s, [sp, #%u]\n", b1, b2, x, z, z,
getRegName(REG_V8 + x), getRegName(REG_V8 + x + 1), z * 8);
}
else if ((b1 & 0xFE) == 0xDA)
{
// save_fregp_x: 1101101x | xxzzzzzz : save pair d(8 + #X), at [sp - (#Z + 1) * 8]!, pre-indexed offset >=
// -512
assert(i + 1 < countOfUnwindCodes);
b2 = *pUnwindCode++;
i++;
x = ((DWORD)(b1 & 0x1) << 2) | (DWORD)(b2 >> 6);
z = (DWORD)(b2 & 0x3F);
printf(" %02X %02X save_fregp_x X#%u Z#%u (0x%02X); stp %s, %s, [sp, #-%u]!\n", b1, b2, x, z, z,
getRegName(REG_V8 + x), getRegName(REG_V8 + x + 1), (z + 1) * 8);
}