Fix reading Time zone rules using Julian days (#17672)
[platform/upstream/coreclr.git] / src / jit / unwindarm.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 XX                                                                           XX
8 XX                              UnwindInfo                                   XX
9 XX                                                                           XX
10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
11 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
12 */
13
14 #include "jitpch.h"
15 #ifdef _MSC_VER
16 #pragma hdrstop
17 #endif
18
19 #if defined(_TARGET_ARM_) && defined(_TARGET_UNIX_)
20 int Compiler::mapRegNumToDwarfReg(regNumber reg)
21 {
22     int dwarfReg = DWARF_REG_ILLEGAL;
23
24     switch (reg)
25     {
26         case REG_R0:
27             dwarfReg = 0;
28             break;
29         case REG_R1:
30             dwarfReg = 1;
31             break;
32         case REG_R2:
33             dwarfReg = 2;
34             break;
35         case REG_R3:
36             dwarfReg = 3;
37             break;
38         case REG_R4:
39             dwarfReg = 4;
40             break;
41         case REG_R5:
42             dwarfReg = 5;
43             break;
44         case REG_R6:
45             dwarfReg = 6;
46             break;
47         case REG_R7:
48             dwarfReg = 7;
49             break;
50         case REG_R8:
51             dwarfReg = 8;
52             break;
53         case REG_R9:
54             dwarfReg = 9;
55             break;
56         case REG_R10:
57             dwarfReg = 10;
58             break;
59         case REG_R11:
60             dwarfReg = 11;
61             break;
62         case REG_R12:
63             dwarfReg = 12;
64             break;
65         case REG_R13:
66             dwarfReg = 13;
67             break;
68         case REG_R14:
69             dwarfReg = 14;
70             break;
71         case REG_R15:
72             dwarfReg = 15;
73             break;
74         case REG_F0:
75             dwarfReg = 64;
76             break;
77         case REG_F1:
78             dwarfReg = 65;
79             break;
80         case REG_F2:
81             dwarfReg = 66;
82             break;
83         case REG_F3:
84             dwarfReg = 67;
85             break;
86         case REG_F4:
87             dwarfReg = 68;
88             break;
89         case REG_F5:
90             dwarfReg = 69;
91             break;
92         case REG_F6:
93             dwarfReg = 70;
94             break;
95         case REG_F7:
96             dwarfReg = 71;
97             break;
98         case REG_F8:
99             dwarfReg = 72;
100             break;
101         case REG_F9:
102             dwarfReg = 73;
103             break;
104         case REG_F10:
105             dwarfReg = 74;
106             break;
107         case REG_F11:
108             dwarfReg = 75;
109             break;
110         case REG_F12:
111             dwarfReg = 76;
112             break;
113         case REG_F13:
114             dwarfReg = 77;
115             break;
116         case REG_F14:
117             dwarfReg = 78;
118             break;
119         case REG_F15:
120             dwarfReg = 79;
121             break;
122         case REG_F16:
123             dwarfReg = 80;
124             break;
125         case REG_F17:
126             dwarfReg = 81;
127             break;
128         case REG_F18:
129             dwarfReg = 82;
130             break;
131         case REG_F19:
132             dwarfReg = 83;
133             break;
134         case REG_F20:
135             dwarfReg = 84;
136             break;
137         case REG_F21:
138             dwarfReg = 85;
139             break;
140         case REG_F22:
141             dwarfReg = 86;
142             break;
143         case REG_F23:
144             dwarfReg = 87;
145             break;
146         case REG_F24:
147             dwarfReg = 88;
148             break;
149         case REG_F25:
150             dwarfReg = 89;
151             break;
152         case REG_F26:
153             dwarfReg = 90;
154             break;
155         case REG_F27:
156             dwarfReg = 91;
157             break;
158         case REG_F28:
159             dwarfReg = 92;
160             break;
161         case REG_F29:
162             dwarfReg = 93;
163             break;
164         case REG_F30:
165             dwarfReg = 94;
166             break;
167         case REG_F31:
168             dwarfReg = 95;
169             break;
170         default:
171             noway_assert(!"unexpected REG_NUM");
172     }
173
174     return dwarfReg;
175 }
176 #endif // _TARGET_ARM_ && _TARGET_UNIX_
177
178 #ifdef _TARGET_ARMARCH_
179
180 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
181 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
182 XX                                                                           XX
183 XX  Unwind APIs                                                              XX
184 XX                                                                           XX
185 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
186 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
187 */
188
189 void Compiler::unwindBegProlog()
190 {
191     assert(compGeneratingProlog);
192
193 #if defined(_TARGET_UNIX_)
194     if (generateCFIUnwindCodes())
195     {
196         unwindBegPrologCFI();
197         return;
198     }
199 #endif // _TARGET_UNIX_
200
201     FuncInfoDsc* func = funCurrentFunc();
202
203     // There is only one prolog for a function/funclet, and it comes first. So now is
204     // a good time to initialize all the unwind data structures.
205
206     emitLocation* startLoc;
207     emitLocation* endLoc;
208     unwindGetFuncLocations(func, true, &startLoc, &endLoc);
209
210     func->uwi.InitUnwindInfo(this, startLoc, endLoc);
211     func->uwi.CaptureLocation();
212
213     func->uwiCold = NULL; // No cold data yet
214 }
215
216 void Compiler::unwindEndProlog()
217 {
218     assert(compGeneratingProlog);
219 }
220
221 void Compiler::unwindBegEpilog()
222 {
223     assert(compGeneratingEpilog);
224
225 #if defined(_TARGET_UNIX_)
226     if (generateCFIUnwindCodes())
227     {
228         return;
229     }
230 #endif // _TARGET_UNIX_
231
232     funCurrentFunc()->uwi.AddEpilog();
233 }
234
235 void Compiler::unwindEndEpilog()
236 {
237     assert(compGeneratingEpilog);
238 }
239
240 #if defined(_TARGET_ARM_)
241
242 void Compiler::unwindPushPopMaskInt(regMaskTP maskInt, bool useOpsize16)
243 {
244     // floating point registers cannot be specified in 'maskInt'
245     assert((maskInt & RBM_ALLFLOAT) == 0);
246
247     UnwindInfo* pu = &funCurrentFunc()->uwi;
248
249     if (useOpsize16)
250     {
251         // The 16-bit opcode only encode R0-R7 and LR
252         assert((maskInt & ~(RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3 | RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_LR)) == 0);
253
254         bool shortFormat = false;
255         BYTE val         = 0;
256
257         if ((maskInt & (RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3)) == 0)
258         {
259             regMaskTP matchMask = maskInt & (RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7);
260             regMaskTP valMask   = RBM_R4;
261             while (val < 4)
262             {
263                 if (matchMask == valMask)
264                 {
265                     shortFormat = true;
266                     break;
267                 }
268
269                 valMask <<= 1;
270                 valMask |= RBM_R4;
271
272                 val++;
273             }
274         }
275
276         if (shortFormat)
277         {
278             // D0-D7 : pop {r4-rX,lr} (X=4-7) (opsize 16)
279             pu->AddCode(0xD0 | ((maskInt >> 12) & 0x4) | val);
280         }
281         else
282         {
283             // EC-ED : pop {r0-r7,lr} (opsize 16)
284             pu->AddCode(0xEC | ((maskInt >> 14) & 0x1), (BYTE)maskInt);
285         }
286     }
287     else
288     {
289         assert((maskInt &
290                 ~(RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3 | RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8 | RBM_R9 | RBM_R10 |
291                   RBM_R11 | RBM_R12 | RBM_LR)) == 0);
292
293         bool shortFormat = false;
294         BYTE val         = 0;
295
296         if (((maskInt & (RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3)) == 0) &&
297             ((maskInt & (RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8)) == (RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8)))
298         {
299             regMaskTP matchMask = maskInt & (RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8 | RBM_R9 | RBM_R10 | RBM_R11);
300             regMaskTP valMask   = RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8;
301             while (val < 4)
302             {
303                 if (matchMask == valMask)
304                 {
305                     shortFormat = true;
306                     break;
307                 }
308
309                 valMask <<= 1;
310                 valMask |= RBM_R4;
311
312                 val++;
313             }
314         }
315
316         if (shortFormat)
317         {
318             // D8-DF : pop {r4-rX,lr} (X=8-11) (opsize 32)
319             pu->AddCode(0xD8 | ((maskInt >> 12) & 0x4) | val);
320         }
321         else
322         {
323             // 80-BF : pop {r0-r12,lr} (opsize 32)
324             pu->AddCode(0x80 | ((maskInt >> 8) & 0x1F) | ((maskInt >> 9) & 0x20), (BYTE)maskInt);
325         }
326     }
327 }
328
329 void Compiler::unwindPushPopMaskFloat(regMaskTP maskFloat)
330 {
331     // Only floating pointer registers can be specified in 'maskFloat'
332     assert((maskFloat & ~RBM_ALLFLOAT) == 0);
333
334     // If the maskFloat is zero there is no unwind code to emit
335     //
336     if (maskFloat == RBM_NONE)
337     {
338         return;
339     }
340
341     UnwindInfo* pu = &funCurrentFunc()->uwi;
342
343     BYTE      val     = 0;
344     regMaskTP valMask = (RBM_F16 | RBM_F17);
345
346     while (maskFloat != valMask)
347     {
348         valMask <<= 2;
349         valMask |= (RBM_F16 | RBM_F17);
350
351         val++;
352
353         if (val == 8)
354         {
355             noway_assert(!"Illegal maskFloat");
356         }
357     }
358
359     // E0-E7 : vpop {d8-dX} (X=8-15) (opsize 32)
360     assert(0 <= val && val <= 7);
361     pu->AddCode(0xE0 | val);
362 }
363
364 void Compiler::unwindPushMaskInt(regMaskTP maskInt)
365 {
366     // Only r0-r12 and lr are supported
367     assert((maskInt &
368             ~(RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3 | RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8 | RBM_R9 | RBM_R10 |
369               RBM_R11 | RBM_R12 | RBM_LR)) == 0);
370
371 #if defined(_TARGET_UNIX_)
372     if (generateCFIUnwindCodes())
373     {
374         return;
375     }
376 #endif // _TARGET_UNIX_
377
378     bool useOpsize16 = ((maskInt & (RBM_LOW_REGS | RBM_LR)) == maskInt); // Can PUSH use the 16-bit encoding?
379     unwindPushPopMaskInt(maskInt, useOpsize16);
380 }
381
382 void Compiler::unwindPushMaskFloat(regMaskTP maskFloat)
383 {
384     // Only floating point registers should be in maskFloat
385     assert((maskFloat & RBM_ALLFLOAT) == maskFloat);
386
387 #if defined(_TARGET_UNIX_)
388     if (generateCFIUnwindCodes())
389     {
390         return;
391     }
392 #endif // _TARGET_UNIX_
393
394     unwindPushPopMaskFloat(maskFloat);
395 }
396
397 void Compiler::unwindPopMaskInt(regMaskTP maskInt)
398 {
399 #if defined(_TARGET_UNIX_)
400     if (generateCFIUnwindCodes())
401     {
402         unwindPushPopMaskCFI(maskInt, false);
403         return;
404     }
405 #endif // _TARGET_UNIX_
406
407     // Only r0-r12 and lr and pc are supported (pc is mapped to lr when encoding)
408     assert((maskInt &
409             ~(RBM_R0 | RBM_R1 | RBM_R2 | RBM_R3 | RBM_R4 | RBM_R5 | RBM_R6 | RBM_R7 | RBM_R8 | RBM_R9 | RBM_R10 |
410               RBM_R11 | RBM_R12 | RBM_LR | RBM_PC)) == 0);
411
412     bool useOpsize16 = ((maskInt & (RBM_LOW_REGS | RBM_PC)) == maskInt); // Can POP use the 16-bit encoding?
413
414     // If we are popping PC, then we'll return from the function. In this case, we assume
415     // the first thing the prolog did was push LR, so give the unwind codes in terms of
416     // the LR that was pushed. Note that the epilog unwind codes are meant to reverse
417     // the effect of the prolog. For "pop {pc}", the prolog had "push {lr}", so we need
418     // an epilog code to model the reverse of that.
419     if (maskInt & RBM_PC)
420     {
421         maskInt = (maskInt & ~RBM_PC) | RBM_LR;
422     }
423     unwindPushPopMaskInt(maskInt, useOpsize16);
424 }
425
426 void Compiler::unwindPopMaskFloat(regMaskTP maskFloat)
427 {
428 #if defined(_TARGET_UNIX_)
429     if (generateCFIUnwindCodes())
430     {
431         unwindPushPopMaskCFI(maskFloat, true);
432         return;
433     }
434 #endif // _TARGET_UNIX_
435
436     // Only floating point registers should be in maskFloat
437     assert((maskFloat & RBM_ALLFLOAT) == maskFloat);
438     unwindPushPopMaskFloat(maskFloat);
439 }
440
441 void Compiler::unwindAllocStack(unsigned size)
442 {
443 #if defined(_TARGET_UNIX_)
444     if (generateCFIUnwindCodes())
445     {
446         if (compGeneratingEpilog)
447         {
448             unwindAllocStackCFI(size);
449         }
450         return;
451     }
452 #endif // _TARGET_UNIX_
453
454     UnwindInfo* pu = &funCurrentFunc()->uwi;
455
456     assert(size % 4 == 0);
457     size /= 4;
458
459     if (size <= 0x7F)
460     {
461         // 00-7F : add sp, sp, #X*4 (opsize 16)
462         pu->AddCode((BYTE)size);
463     }
464     else if (size <= 0x3FF)
465     {
466         // E8-EB : addw sp, sp, #X*4 (opsize 32)
467         pu->AddCode(0xE8 | (BYTE)(size >> 8), (BYTE)size);
468     }
469     else if (size <= 0xFFFF)
470     {
471         // F7 : add sp, sp, #X*4 (opsize 16)
472         // F9 : add sp, sp, #X*4 (opsize 32)
473         //
474         // For large stack size, the most significant bits
475         // are stored first (and next to the opCode (F9)) per the unwind spec.
476         unsigned instrSizeInBytes = pu->GetInstructionSize();
477         BYTE     b1               = (instrSizeInBytes == 2) ? 0xF7 : 0xF9;
478         pu->AddCode(b1,
479                     (BYTE)(size >> 8), // msb
480                     (BYTE)size);       // lsb
481     }
482     else
483     {
484         // F8 : add sp, sp, #X*4 (opsize 16)
485         // FA : add sp, sp, #X*4 (opsize 32)
486         //
487         // For large stack size, the most significant bits
488         // are stored first (and next to the opCode (FA)) per the unwind spec.
489         unsigned instrSizeInBytes = pu->GetInstructionSize();
490         BYTE     b1               = (instrSizeInBytes == 2) ? 0xF8 : 0xFA;
491         pu->AddCode(b1, (BYTE)(size >> 16), (BYTE)(size >> 8), (BYTE)size);
492     }
493 }
494
495 void Compiler::unwindSetFrameReg(regNumber reg, unsigned offset)
496 {
497 #if defined(_TARGET_UNIX_)
498     if (generateCFIUnwindCodes())
499     {
500         if (compGeneratingEpilog)
501         {
502             unwindSetFrameRegCFI(reg, offset);
503         }
504         return;
505     }
506 #endif // _TARGET_UNIX_
507
508     UnwindInfo* pu = &funCurrentFunc()->uwi;
509
510     // Arm unwind info does not allow offset
511     assert(offset == 0);
512     assert(0 <= reg && reg <= 15);
513
514     // C0-CF : mov sp, rX (opsize 16)
515     pu->AddCode((BYTE)(0xC0 + reg));
516 }
517
518 void Compiler::unwindSaveReg(regNumber reg, unsigned offset)
519 {
520     unreached();
521 }
522
523 void Compiler::unwindBranch16()
524 {
525 #if defined(_TARGET_UNIX_)
526     if (generateCFIUnwindCodes())
527     {
528         return;
529     }
530 #endif // _TARGET_UNIX_
531
532     UnwindInfo* pu = &funCurrentFunc()->uwi;
533
534     // TODO-CQ: need to handle changing the exit code from 0xFF to 0xFD. Currently, this will waste an extra 0xFF at the
535     // end, automatically added.
536     pu->AddCode(0xFD);
537 }
538
539 void Compiler::unwindNop(unsigned codeSizeInBytes) // codeSizeInBytes is 2 or 4 bytes for Thumb2 instruction
540 {
541 #if defined(_TARGET_UNIX_)
542     if (generateCFIUnwindCodes())
543     {
544         return;
545     }
546 #endif // _TARGET_UNIX_
547
548     UnwindInfo* pu = &funCurrentFunc()->uwi;
549
550 #ifdef DEBUG
551     if (verbose)
552     {
553         printf("unwindNop: adding NOP for %d byte instruction\n", codeSizeInBytes);
554     }
555 #endif
556
557     INDEBUG(pu->uwiAddingNOP = true);
558
559     if (codeSizeInBytes == 2)
560     {
561         // FB : nop (opsize 16)
562         pu->AddCode(0xFB);
563     }
564     else
565     {
566         noway_assert(codeSizeInBytes == 4);
567
568         // FC : nop (opsize 32)
569         pu->AddCode(0xFC);
570     }
571
572     INDEBUG(pu->uwiAddingNOP = false);
573 }
574
575 #endif // defined(_TARGET_ARM_)
576
577 // The instructions between the last captured "current state" and the current instruction
578 // are in the prolog but have no effect for unwinding. Emit the appropriate NOP unwind codes
579 // for them.
580 void Compiler::unwindPadding()
581 {
582 #if defined(_TARGET_UNIX_)
583     if (generateCFIUnwindCodes())
584     {
585         return;
586     }
587 #endif // _TARGET_UNIX_
588
589     UnwindInfo* pu = &funCurrentFunc()->uwi;
590     genEmitter->emitUnwindNopPadding(pu->GetCurrentEmitterLocation(), this);
591 }
592
593 // Ask the VM to reserve space for the unwind information for the function and
594 // all its funclets.
595 void Compiler::unwindReserve()
596 {
597     assert(!compGeneratingProlog);
598     assert(!compGeneratingEpilog);
599
600     assert(compFuncInfoCount > 0);
601     for (unsigned funcIdx = 0; funcIdx < compFuncInfoCount; funcIdx++)
602     {
603         unwindReserveFunc(funGetFunc(funcIdx));
604     }
605 }
606
607 void Compiler::unwindReserveFunc(FuncInfoDsc* func)
608 {
609     BOOL isFunclet          = (func->funKind == FUNC_ROOT) ? FALSE : TRUE;
610     bool funcHasColdSection = false;
611
612 #if defined(_TARGET_UNIX_)
613     if (generateCFIUnwindCodes())
614     {
615         DWORD unwindCodeBytes = 0;
616         if (fgFirstColdBlock != nullptr)
617         {
618             eeReserveUnwindInfo(isFunclet, true /*isColdCode*/, unwindCodeBytes);
619         }
620         unwindCodeBytes = (DWORD)(func->cfiCodes->size() * sizeof(CFI_CODE));
621         eeReserveUnwindInfo(isFunclet, false /*isColdCode*/, unwindCodeBytes);
622
623         return;
624     }
625 #endif // _TARGET_UNIX_
626
627     // If there is cold code, split the unwind data between the hot section and the
628     // cold section. This needs to be done before we split into fragments, as each
629     // of the hot and cold sections can have multiple fragments.
630
631     if (fgFirstColdBlock != NULL)
632     {
633         assert(!isFunclet); // TODO-CQ: support hot/cold splitting with EH
634
635         emitLocation* startLoc;
636         emitLocation* endLoc;
637         unwindGetFuncLocations(func, false, &startLoc, &endLoc);
638
639         func->uwiCold = new (this, CMK_UnwindInfo) UnwindInfo();
640         func->uwiCold->InitUnwindInfo(this, startLoc, endLoc);
641         func->uwiCold->HotColdSplitCodes(&func->uwi);
642
643         funcHasColdSection = true;
644     }
645
646     // First we need to split the function or funclet into fragments that are no larger
647     // than 512K, so the fragment size will fit in the unwind data "Function Length" field.
648     // The ARM Exception Data specification "Function Fragments" section describes this.
649     func->uwi.Split();
650
651     func->uwi.Reserve(isFunclet, true);
652
653     // After the hot section, split and reserve the cold section
654
655     if (funcHasColdSection)
656     {
657         assert(func->uwiCold != NULL);
658
659         func->uwiCold->Split();
660         func->uwiCold->Reserve(isFunclet, false);
661     }
662 }
663
664 // unwindEmit: Report all the unwind information to the VM.
665 // Arguments:
666 //      pHotCode:  Pointer to the beginning of the memory with the function and funclet hot  code
667 //      pColdCode: Pointer to the beginning of the memory with the function and funclet cold code.
668
669 void Compiler::unwindEmit(void* pHotCode, void* pColdCode)
670 {
671     assert(compFuncInfoCount > 0);
672     for (unsigned funcIdx = 0; funcIdx < compFuncInfoCount; funcIdx++)
673     {
674         unwindEmitFunc(funGetFunc(funcIdx), pHotCode, pColdCode);
675     }
676 }
677
678 void Compiler::unwindEmitFunc(FuncInfoDsc* func, void* pHotCode, void* pColdCode)
679 {
680     // Verify that the JIT enum is in sync with the JIT-EE interface enum
681     static_assert_no_msg(FUNC_ROOT == (FuncKind)CORJIT_FUNC_ROOT);
682     static_assert_no_msg(FUNC_HANDLER == (FuncKind)CORJIT_FUNC_HANDLER);
683     static_assert_no_msg(FUNC_FILTER == (FuncKind)CORJIT_FUNC_FILTER);
684
685 #if defined(_TARGET_UNIX_)
686     if (generateCFIUnwindCodes())
687     {
688         unwindEmitFuncCFI(func, pHotCode, pColdCode);
689         return;
690     }
691 #endif // _TARGET_UNIX_
692
693     func->uwi.Allocate((CorJitFuncKind)func->funKind, pHotCode, pColdCode, true);
694
695     if (func->uwiCold != NULL)
696     {
697         func->uwiCold->Allocate((CorJitFuncKind)func->funKind, pHotCode, pColdCode, false);
698     }
699 }
700
701 #if defined(_TARGET_ARM_)
702
703 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
704 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
705 XX                                                                           XX
706 XX  Unwind Info Debug helpers                                                XX
707 XX                                                                           XX
708 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
709 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
710 */
711
712 #ifdef DEBUG
713
714 // Return the opcode size of an instruction, in bytes, given the first byte of
715 // its corresponding unwind code.
716
717 unsigned GetOpcodeSizeFromUnwindHeader(BYTE b1)
718 {
719     static BYTE s_UnwindOpsize[256] = {
720         // array of opsizes, in bytes (as specified in the ARM unwind specification)
721         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 00-0F
722         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 10-1F
723         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 20-2F
724         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 30-3F
725         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 40-4F
726         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 50-5F
727         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 60-6F
728         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 70-7F
729         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 80-8F
730         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 90-9F
731         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // A0-AF
732         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // B0-BF
733         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0-CF
734         2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, // D0-DF
735         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 4, // E0-EF
736         0, 0, 0, 0, 0, 4, 4, 2, 2, 4, 4, 2, 4, 2, 4, 0  // F0-FF
737     };
738
739     BYTE opsize = s_UnwindOpsize[b1];
740     assert(opsize == 2 ||
741            opsize == 4); // We shouldn't get a code with no opsize (the 0xFF end code is handled specially)
742     return opsize;
743 }
744
745 // Return the size of the unwind code (from 1 to 4 bytes), given the first byte of the unwind bytes
746
747 unsigned GetUnwindSizeFromUnwindHeader(BYTE b1)
748 {
749     static BYTE s_UnwindSize[256] = {
750         // array of unwind sizes, in bytes (as specified in the ARM unwind specification)
751         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00-0F
752         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 10-1F
753         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 20-2F
754         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 30-3F
755         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40-4F
756         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 50-5F
757         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60-6F
758         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70-7F
759         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 80-8F
760         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 90-9F
761         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // A0-AF
762         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // B0-BF
763         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C0-CF
764         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D0-DF
765         1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, // E0-EF
766         1, 1, 1, 1, 1, 2, 2, 3, 4, 3, 4, 1, 1, 1, 1, 1  // F0-FF
767     };
768
769     unsigned size = s_UnwindSize[b1];
770     assert(1 <= size && size <= 4);
771     return size;
772 }
773
774 #endif // DEBUG
775
776 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
777 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
778 XX                                                                           XX
779 XX  Unwind Info Support Classes                                              XX
780 XX                                                                           XX
781 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
782 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
783 */
784
785 ///////////////////////////////////////////////////////////////////////////////
786 //
787 //  UnwindCodesBase
788 //
789 ///////////////////////////////////////////////////////////////////////////////
790
791 #ifdef DEBUG
792
793 // Walk the prolog codes and calculate the size of the prolog or epilog, in bytes.
794 // The 0xFD and 0xFE "end + NOP" codes need to be handled differently between
795 // the prolog and epilog. They count as pure "end" codes in a prolog, but they
796 // count as 16 and 32 bit NOPs (respectively), as well as an "end", in an epilog.
797 unsigned UnwindCodesBase::GetCodeSizeFromUnwindCodes(bool isProlog)
798 {
799     BYTE*    pCodesStart = GetCodes();
800     BYTE*    pCodes      = pCodesStart;
801     unsigned size        = 0;
802     for (;;)
803     {
804         BYTE b1 = *pCodes;
805         if (b1 >= 0xFD)
806         {
807             // 0xFD, 0xFE, 0xFF are "end" codes
808
809             if (!isProlog && (b1 == 0xFD || b1 == 0xFE))
810             {
811                 // Count the special "end + NOP" code size in the epilog
812                 size += GetOpcodeSizeFromUnwindHeader(b1);
813             }
814
815             break; // We hit an "end" code; we're done
816         }
817         size += GetOpcodeSizeFromUnwindHeader(b1);
818         pCodes += GetUnwindSizeFromUnwindHeader(b1);
819         assert(pCodes - pCodesStart < 256); // 255 is the absolute maximum number of code bytes allowed
820     }
821     return size;
822 }
823
824 #endif // DEBUG
825
826 #endif // defined(_TARGET_ARM_)
827
828 ///////////////////////////////////////////////////////////////////////////////
829 //
830 //  UnwindPrologCodes
831 //
832 ///////////////////////////////////////////////////////////////////////////////
833
834 // We're going to use the prolog codes memory to store the final unwind data.
835 // Ensure we have enough memory to store everything. If 'epilogBytes' > 0, then
836 // move the prolog codes so there are 'epilogBytes' bytes after the prolog codes.
837 // Set the header pointer for future use, adding the header bytes (this pointer
838 // is updated when a header byte is added), and remember the index that points
839 // to the beginning of the header.
840
841 void UnwindPrologCodes::SetFinalSize(int headerBytes, int epilogBytes)
842 {
843 #ifdef DEBUG
844     // We're done adding codes. Check that we didn't accidentally create a bigger prolog.
845     unsigned codeSize = GetCodeSizeFromUnwindCodes(true);
846     assert(codeSize <= MAX_PROLOG_SIZE_BYTES);
847 #endif // DEBUG
848
849     int prologBytes = Size();
850
851     EnsureSize(headerBytes + prologBytes + epilogBytes + 3); // 3 = padding bytes for alignment
852
853     upcUnwindBlockSlot = upcCodeSlot - headerBytes - epilogBytes; // Index of the first byte of the unwind header
854
855     assert(upcMemSize == upcUnwindBlockSlot + headerBytes + prologBytes + epilogBytes + 3);
856
857     upcHeaderSlot = upcUnwindBlockSlot - 1; // upcHeaderSlot is always incremented before storing
858     assert(upcHeaderSlot >= -1);
859
860     if (epilogBytes > 0)
861     {
862         // The prolog codes that are already at the end of the array need to get moved to the middle,
863         // with space for the non-matching epilog codes to follow.
864
865         memmove_s(&upcMem[upcUnwindBlockSlot + headerBytes], upcMemSize - (upcUnwindBlockSlot + headerBytes),
866                   &upcMem[upcCodeSlot], prologBytes);
867
868         // Note that the three UWC_END padding bytes still exist at the end of the array.
869         CLANG_FORMAT_COMMENT_ANCHOR;
870
871 #ifdef DEBUG
872         // Zero out the epilog codes memory, to ensure we've copied the right bytes. Don't zero the padding bytes.
873         memset(&upcMem[upcUnwindBlockSlot + headerBytes + prologBytes], 0, epilogBytes);
874 #endif // DEBUG
875
876         upcEpilogSlot =
877             upcUnwindBlockSlot + headerBytes + prologBytes; // upcEpilogSlot points to the next epilog location to fill
878
879         // Update upcCodeSlot to point at the new beginning of the prolog codes
880         upcCodeSlot = upcUnwindBlockSlot + headerBytes;
881     }
882 }
883
884 // Add a header word. Header words are added starting at the beginning, in order: first to last.
885 // This is in contrast to the prolog unwind codes, which are added in reverse order.
886 void UnwindPrologCodes::AddHeaderWord(DWORD d)
887 {
888     assert(-1 <= upcHeaderSlot);
889     assert(upcHeaderSlot + 4 < upcCodeSlot); // Don't collide with the unwind codes that are already there!
890
891     // Store it byte-by-byte in little-endian format. We've already ensured there is enough space
892     // in SetFinalSize().
893     upcMem[++upcHeaderSlot] = (BYTE)d;
894     upcMem[++upcHeaderSlot] = (BYTE)(d >> 8);
895     upcMem[++upcHeaderSlot] = (BYTE)(d >> 16);
896     upcMem[++upcHeaderSlot] = (BYTE)(d >> 24);
897 }
898
899 // AppendEpilog: copy the epilog bytes to the next epilog bytes slot
900 void UnwindPrologCodes::AppendEpilog(UnwindEpilogInfo* pEpi)
901 {
902     assert(upcEpilogSlot != -1);
903
904     int epiSize = pEpi->Size();
905     memcpy_s(&upcMem[upcEpilogSlot], upcMemSize - upcEpilogSlot - 3, pEpi->GetCodes(),
906              epiSize); // -3 to avoid writing to the alignment padding
907     assert(pEpi->GetStartIndex() ==
908            upcEpilogSlot - upcCodeSlot); // Make sure we copied it where we expected to copy it.
909
910     upcEpilogSlot += epiSize;
911     assert(upcEpilogSlot <= upcMemSize - 3);
912 }
913
914 // GetFinalInfo: return a pointer to the final unwind info to hand to the VM, and the size of this info in bytes
915 void UnwindPrologCodes::GetFinalInfo(/* OUT */ BYTE** ppUnwindBlock, /* OUT */ ULONG* pUnwindBlockSize)
916 {
917     assert(upcHeaderSlot + 1 == upcCodeSlot); // We better have filled in the header before asking for the final data!
918
919     *ppUnwindBlock = &upcMem[upcUnwindBlockSlot];
920
921     // We put 4 'end' codes at the end for padding, so we can ensure we have an
922     // unwind block that is a multiple of 4 bytes in size. Subtract off three 'end'
923     // codes (leave one), and then align the size up to a multiple of 4.
924     *pUnwindBlockSize = AlignUp((UINT)(upcMemSize - upcUnwindBlockSlot - 3), sizeof(DWORD));
925 }
926
927 // Do the argument unwind codes match our unwind codes?
928 // If they don't match, return -1. If they do, return the offset into
929 // our codes at which they match. Note that this means that the
930 // argument codes can match a subset of our codes. The subset needs to be at
931 // the end, for the "end" code to match.
932 //
933 // This is similar to UnwindEpilogInfo::Match().
934 //
935 #if defined(_TARGET_ARM_)
936 // Note that if we wanted to handle 0xFD and 0xFE codes, by converting
937 // an existing 0xFF code to one of those, we might do that here.
938 #endif // defined(_TARGET_ARM_)
939
940 int UnwindPrologCodes::Match(UnwindEpilogInfo* pEpi)
941 {
942     if (Size() < pEpi->Size())
943     {
944         return -1;
945     }
946
947     int matchIndex = Size() - pEpi->Size();
948
949     if (0 == memcmp(GetCodes() + matchIndex, pEpi->GetCodes(), pEpi->Size()))
950     {
951         return matchIndex;
952     }
953
954     return -1;
955 }
956
957 // Copy the prolog codes from another prolog. The only time this is legal is
958 // if we are at the initial state and no prolog codes have been added.
959 // This is used to create the 'phantom' prolog for non-first fragments.
960
961 void UnwindPrologCodes::CopyFrom(UnwindPrologCodes* pCopyFrom)
962 {
963     assert(uwiComp == pCopyFrom->uwiComp);
964     assert(upcMem == upcMemLocal);
965     assert(upcMemSize == UPC_LOCAL_COUNT);
966     assert(upcHeaderSlot == -1);
967     assert(upcEpilogSlot == -1);
968
969     // Copy the codes
970     EnsureSize(pCopyFrom->upcMemSize);
971     assert(upcMemSize == pCopyFrom->upcMemSize);
972     memcpy_s(upcMem, upcMemSize, pCopyFrom->upcMem, pCopyFrom->upcMemSize);
973
974     // Copy the other data
975     upcCodeSlot        = pCopyFrom->upcCodeSlot;
976     upcHeaderSlot      = pCopyFrom->upcHeaderSlot;
977     upcEpilogSlot      = pCopyFrom->upcEpilogSlot;
978     upcUnwindBlockSlot = pCopyFrom->upcUnwindBlockSlot;
979 }
980
981 void UnwindPrologCodes::EnsureSize(int requiredSize)
982 {
983     if (requiredSize > upcMemSize)
984     {
985         // Reallocate, and copy everything to a new array.
986
987         // Choose the next power of two size. This may or may not be the best choice.
988         noway_assert((requiredSize & 0xC0000000) == 0); // too big!
989         int newSize;
990         for (newSize = upcMemSize << 1; newSize < requiredSize; newSize <<= 1)
991         {
992             // do nothing
993         }
994
995         BYTE* newUnwindCodes = new (uwiComp, CMK_UnwindInfo) BYTE[newSize];
996         memcpy_s(newUnwindCodes + newSize - upcMemSize, upcMemSize, upcMem,
997                  upcMemSize); // copy the existing data to the end
998 #ifdef DEBUG
999         // Clear the old unwind codes; nobody should be looking at them
1000         memset(upcMem, 0xFF, upcMemSize);
1001 #endif                           // DEBUG
1002         upcMem = newUnwindCodes; // we don't free anything that used to be there since we have a no-release allocator
1003         upcCodeSlot += newSize - upcMemSize;
1004         upcMemSize = newSize;
1005     }
1006 }
1007
1008 #ifdef DEBUG
1009 void UnwindPrologCodes::Dump(int indent)
1010 {
1011     printf("%*sUnwindPrologCodes @0x%08p, size:%d:\n", indent, "", dspPtr(this), sizeof(*this));
1012     printf("%*s  uwiComp: 0x%08p\n", indent, "", dspPtr(uwiComp));
1013     printf("%*s  &upcMemLocal[0]: 0x%08p\n", indent, "", dspPtr(&upcMemLocal[0]));
1014     printf("%*s  upcMem: 0x%08p\n", indent, "", dspPtr(upcMem));
1015     printf("%*s  upcMemSize: %d\n", indent, "", upcMemSize);
1016     printf("%*s  upcCodeSlot: %d\n", indent, "", upcCodeSlot);
1017     printf("%*s  upcHeaderSlot: %d\n", indent, "", upcHeaderSlot);
1018     printf("%*s  upcEpilogSlot: %d\n", indent, "", upcEpilogSlot);
1019     printf("%*s  upcUnwindBlockSlot: %d\n", indent, "", upcUnwindBlockSlot);
1020
1021     if (upcMemSize > 0)
1022     {
1023         printf("%*s  codes:", indent, "");
1024         for (int i = 0; i < upcMemSize; i++)
1025         {
1026             printf(" %02x", upcMem[i]);
1027             if (i == upcCodeSlot)
1028                 printf(" <-C");
1029             else if (i == upcHeaderSlot)
1030                 printf(" <-H");
1031             else if (i == upcEpilogSlot)
1032                 printf(" <-E");
1033             else if (i == upcUnwindBlockSlot)
1034                 printf(" <-U");
1035         }
1036         printf("\n");
1037     }
1038 }
1039 #endif // DEBUG
1040
1041 ///////////////////////////////////////////////////////////////////////////////
1042 //
1043 //  UnwindEpilogCodes
1044 //
1045 ///////////////////////////////////////////////////////////////////////////////
1046
1047 void UnwindEpilogCodes::EnsureSize(int requiredSize)
1048 {
1049     if (requiredSize > uecMemSize)
1050     {
1051         // Reallocate, and copy everything to a new array.
1052
1053         // Choose the next power of two size. This may or may not be the best choice.
1054         noway_assert((requiredSize & 0xC0000000) == 0); // too big!
1055         int newSize;
1056         for (newSize = uecMemSize << 1; newSize < requiredSize; newSize <<= 1)
1057         {
1058             // do nothing
1059         }
1060
1061         BYTE* newUnwindCodes = new (uwiComp, CMK_UnwindInfo) BYTE[newSize];
1062         memcpy_s(newUnwindCodes, newSize, uecMem, uecMemSize);
1063 #ifdef DEBUG
1064         // Clear the old unwind codes; nobody should be looking at them
1065         memset(uecMem, 0xFF, uecMemSize);
1066 #endif                           // DEBUG
1067         uecMem = newUnwindCodes; // we don't free anything that used to be there since we have a no-release allocator
1068         // uecCodeSlot stays the same
1069         uecMemSize = newSize;
1070     }
1071 }
1072
1073 #ifdef DEBUG
1074 void UnwindEpilogCodes::Dump(int indent)
1075 {
1076     printf("%*sUnwindEpilogCodes @0x%08p, size:%d:\n", indent, "", dspPtr(this), sizeof(*this));
1077     printf("%*s  uwiComp: 0x%08p\n", indent, "", dspPtr(uwiComp));
1078     printf("%*s  &uecMemLocal[0]: 0x%08p\n", indent, "", dspPtr(&uecMemLocal[0]));
1079     printf("%*s  uecMem: 0x%08p\n", indent, "", dspPtr(uecMem));
1080     printf("%*s  uecMemSize: %d\n", indent, "", uecMemSize);
1081     printf("%*s  uecCodeSlot: %d\n", indent, "", uecCodeSlot);
1082     printf("%*s  uecFinalized: %s\n", indent, "", dspBool(uecFinalized));
1083
1084     if (uecMemSize > 0)
1085     {
1086         printf("%*s  codes:", indent, "");
1087         for (int i = 0; i < uecMemSize; i++)
1088         {
1089             printf(" %02x", uecMem[i]);
1090             if (i == uecCodeSlot)
1091                 printf(" <-C"); // Indicate the current pointer
1092         }
1093         printf("\n");
1094     }
1095 }
1096 #endif // DEBUG
1097
1098 ///////////////////////////////////////////////////////////////////////////////
1099 //
1100 //  UnwindEpilogInfo
1101 //
1102 ///////////////////////////////////////////////////////////////////////////////
1103
1104 // Do the current unwind codes match those of the argument epilog?
1105 // If they don't match, return -1. If they do, return the offset into
1106 // our codes at which the argument codes match. Note that this means that
1107 // the argument codes can match a subset of our codes. The subset needs to be at
1108 // the end, for the "end" code to match.
1109 //
1110 // Note that if we wanted to handle 0xFD and 0xFE codes, by converting
1111 // an existing 0xFF code to one of those, we might do that here.
1112
1113 int UnwindEpilogInfo::Match(UnwindEpilogInfo* pEpi)
1114 {
1115     if (Matches())
1116     {
1117         // We are already matched to someone else, and won't provide codes to the final layout
1118         return -1;
1119     }
1120
1121     if (Size() < pEpi->Size())
1122     {
1123         return -1;
1124     }
1125
1126     int matchIndex = Size() - pEpi->Size();
1127
1128     if (0 == memcmp(GetCodes() + matchIndex, pEpi->GetCodes(), pEpi->Size()))
1129     {
1130         return matchIndex;
1131     }
1132
1133     return -1;
1134 }
1135
1136 void UnwindEpilogInfo::CaptureEmitLocation()
1137 {
1138     noway_assert(epiEmitLocation == NULL); // This function is only called once per epilog
1139     epiEmitLocation = new (uwiComp, CMK_UnwindInfo) emitLocation();
1140     epiEmitLocation->CaptureLocation(uwiComp->genEmitter);
1141 }
1142
1143 void UnwindEpilogInfo::FinalizeOffset()
1144 {
1145     epiStartOffset = epiEmitLocation->CodeOffset(uwiComp->genEmitter);
1146 }
1147
1148 #ifdef DEBUG
1149 void UnwindEpilogInfo::Dump(int indent)
1150 {
1151     printf("%*sUnwindEpilogInfo @0x%08p, size:%d:\n", indent, "", dspPtr(this), sizeof(*this));
1152     printf("%*s  uwiComp: 0x%08p\n", indent, "", dspPtr(uwiComp));
1153     printf("%*s  epiNext: 0x%08p\n", indent, "", dspPtr(epiNext));
1154     printf("%*s  epiEmitLocation: 0x%08p\n", indent, "", dspPtr(epiEmitLocation));
1155     printf("%*s  epiStartOffset: 0x%x\n", indent, "", epiStartOffset);
1156     printf("%*s  epiMatches: %s\n", indent, "", dspBool(epiMatches));
1157     printf("%*s  epiStartIndex: %d\n", indent, "", epiStartIndex);
1158
1159     epiCodes.Dump(indent + 2);
1160 }
1161 #endif // DEBUG
1162
1163 ///////////////////////////////////////////////////////////////////////////////
1164 //
1165 //  UnwindFragmentInfo
1166 //
1167 ///////////////////////////////////////////////////////////////////////////////
1168
1169 UnwindFragmentInfo::UnwindFragmentInfo(Compiler* comp, emitLocation* emitLoc, bool hasPhantomProlog)
1170     : UnwindBase(comp)
1171     , ufiNext(NULL)
1172     , ufiEmitLoc(emitLoc)
1173     , ufiHasPhantomProlog(hasPhantomProlog)
1174     , ufiPrologCodes(comp)
1175     , ufiEpilogFirst(comp)
1176     , ufiEpilogList(NULL)
1177     , ufiEpilogLast(NULL)
1178     , ufiCurCodes(&ufiPrologCodes)
1179     , ufiSize(0)
1180     , ufiStartOffset(UFI_ILLEGAL_OFFSET)
1181 {
1182 #ifdef DEBUG
1183     ufiNum         = 1;
1184     ufiInProlog    = true;
1185     ufiInitialized = UFI_INITIALIZED_PATTERN;
1186 #endif // DEBUG
1187 }
1188
1189 void UnwindFragmentInfo::FinalizeOffset()
1190 {
1191     if (ufiEmitLoc == NULL)
1192     {
1193         // NULL emit location means the beginning of the code. This is to handle the first fragment prolog.
1194         ufiStartOffset = 0;
1195     }
1196     else
1197     {
1198         ufiStartOffset = ufiEmitLoc->CodeOffset(uwiComp->genEmitter);
1199     }
1200
1201     for (UnwindEpilogInfo* pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1202     {
1203         pEpi->FinalizeOffset();
1204     }
1205 }
1206
1207 void UnwindFragmentInfo::AddEpilog()
1208 {
1209     assert(ufiInitialized == UFI_INITIALIZED_PATTERN);
1210
1211 #ifdef DEBUG
1212     if (ufiInProlog)
1213     {
1214         assert(ufiEpilogList == NULL);
1215         ufiInProlog = false;
1216     }
1217     else
1218     {
1219         assert(ufiEpilogList != NULL);
1220     }
1221 #endif // DEBUG
1222
1223     // Either allocate a new epilog object, or, for the first one, use the
1224     // preallocated one that is a member of the UnwindFragmentInfo class.
1225
1226     UnwindEpilogInfo* newepi;
1227
1228     if (ufiEpilogList == NULL)
1229     {
1230         // Use the epilog that's in the class already. Be sure to initialize it!
1231         newepi = ufiEpilogList = &ufiEpilogFirst;
1232     }
1233     else
1234     {
1235         newepi = new (uwiComp, CMK_UnwindInfo) UnwindEpilogInfo(uwiComp);
1236     }
1237
1238     // Put the new epilog at the end of the epilog list
1239
1240     if (ufiEpilogLast != NULL)
1241     {
1242         ufiEpilogLast->epiNext = newepi;
1243     }
1244
1245     ufiEpilogLast = newepi;
1246
1247     // What is the starting code offset of the epilog? Store an emitter location
1248     // so we can ask the emitter later, after codegen.
1249
1250     newepi->CaptureEmitLocation();
1251
1252     // Put subsequent unwind codes in this new epilog
1253
1254     ufiCurCodes = &newepi->epiCodes;
1255 }
1256
1257 // Copy the prolog codes from the 'pCopyFrom' fragment. These prolog codes will
1258 // become 'phantom' prolog codes in this fragment. Note that this fragment should
1259 // not have any prolog codes currently; it is at the initial state.
1260
1261 void UnwindFragmentInfo::CopyPrologCodes(UnwindFragmentInfo* pCopyFrom)
1262 {
1263     ufiPrologCodes.CopyFrom(&pCopyFrom->ufiPrologCodes);
1264 #ifdef _TARGET_ARM64_
1265     ufiPrologCodes.AddCode(UWC_END_C);
1266 #endif
1267 }
1268
1269 // Split the epilog codes that currently exist in 'pSplitFrom'. The ones that represent
1270 // epilogs that start at or after the location represented by 'emitLoc' are removed
1271 // from 'pSplitFrom' and moved to this fragment. Note that this fragment should not have
1272 // any epilog codes currently; it is at the initial state.
1273
1274 void UnwindFragmentInfo::SplitEpilogCodes(emitLocation* emitLoc, UnwindFragmentInfo* pSplitFrom)
1275 {
1276     UnwindEpilogInfo* pEpiPrev;
1277     UnwindEpilogInfo* pEpi;
1278
1279     UNATIVE_OFFSET splitOffset = emitLoc->CodeOffset(uwiComp->genEmitter);
1280
1281     for (pEpiPrev = NULL, pEpi = pSplitFrom->ufiEpilogList; pEpi != NULL; pEpiPrev = pEpi, pEpi = pEpi->epiNext)
1282     {
1283         pEpi->FinalizeOffset(); // Get the offset of the epilog from the emitter so we can compare it
1284         if (pEpi->GetStartOffset() >= splitOffset)
1285         {
1286             // This epilog and all following epilogs, which must be in order of increasing offsets,
1287             // get moved to this fragment.
1288
1289             // Splice in the epilogs to this fragment. Set the head of the epilog
1290             // list to this epilog.
1291             ufiEpilogList = pEpi; // In this case, don't use 'ufiEpilogFirst'
1292             ufiEpilogLast = pSplitFrom->ufiEpilogLast;
1293
1294             // Splice out the tail of the list from the 'pSplitFrom' epilog list
1295             pSplitFrom->ufiEpilogLast = pEpiPrev;
1296             if (pSplitFrom->ufiEpilogLast == NULL)
1297             {
1298                 pSplitFrom->ufiEpilogList = NULL;
1299             }
1300             else
1301             {
1302                 pSplitFrom->ufiEpilogLast->epiNext = NULL;
1303             }
1304
1305             // No more codes should be added once we start splitting
1306             pSplitFrom->ufiCurCodes = NULL;
1307             ufiCurCodes             = NULL;
1308
1309             break;
1310         }
1311     }
1312 }
1313
1314 // Is this epilog at the end of an unwind fragment? Ask the emitter.
1315 // Note that we need to know this before all code offsets are finalized,
1316 // so we can determine whether we can omit an epilog scope word for a
1317 // single matching epilog.
1318
1319 bool UnwindFragmentInfo::IsAtFragmentEnd(UnwindEpilogInfo* pEpi)
1320 {
1321     return uwiComp->genEmitter->emitIsFuncEnd(pEpi->epiEmitLocation, (ufiNext == NULL) ? NULL : ufiNext->ufiEmitLoc);
1322 }
1323
1324 // Merge the unwind codes as much as possible.
1325 // This function is called before all offsets are final.
1326 // Also, compute the size of the final unwind block. Store this
1327 // and some other data for later, when we actually emit the
1328 // unwind block.
1329
1330 void UnwindFragmentInfo::MergeCodes()
1331 {
1332     assert(ufiInitialized == UFI_INITIALIZED_PATTERN);
1333
1334     unsigned epilogCount     = 0;
1335     unsigned epilogCodeBytes = 0; // The total number of unwind code bytes used by epilogs that don't match the
1336                                   // prolog codes
1337     unsigned epilogIndex = ufiPrologCodes.Size(); // The "Epilog Start Index" for the next non-matching epilog codes
1338     UnwindEpilogInfo* pEpi;
1339
1340     for (pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1341     {
1342         ++epilogCount;
1343
1344         pEpi->FinalizeCodes();
1345
1346         // Does this epilog match the prolog?
1347         // NOTE: for the purpose of matching, we don't handle the 0xFD and 0xFE end codes that allow slightly unequal
1348         // prolog and epilog codes.
1349
1350         int matchIndex;
1351
1352         matchIndex = ufiPrologCodes.Match(pEpi);
1353         if (matchIndex != -1)
1354         {
1355             pEpi->SetMatches();
1356             pEpi->SetStartIndex(matchIndex); // Prolog codes start at zero, so matchIndex is exactly the start index
1357         }
1358         else
1359         {
1360             // The epilog codes don't match the prolog codes. Do they match any of the epilogs
1361             // we've seen so far?
1362
1363             bool matched = false;
1364             for (UnwindEpilogInfo* pEpi2 = ufiEpilogList; pEpi2 != pEpi; pEpi2 = pEpi2->epiNext)
1365             {
1366                 matchIndex = pEpi2->Match(pEpi);
1367                 if (matchIndex != -1)
1368                 {
1369                     // Use the same epilog index as the one we matched, as it has already been set.
1370                     pEpi->SetMatches();
1371                     pEpi->SetStartIndex(pEpi2->GetStartIndex() + matchIndex); // We might match somewhere inside pEpi2's
1372                                                                               // codes, in which case matchIndex > 0
1373                     matched = true;
1374                     break;
1375                 }
1376             }
1377
1378             if (!matched)
1379             {
1380                 pEpi->SetStartIndex(epilogIndex); // We'll copy these codes to the next available location
1381                 epilogCodeBytes += pEpi->Size();
1382                 epilogIndex += pEpi->Size();
1383             }
1384         }
1385     }
1386
1387     DWORD codeBytes = ufiPrologCodes.Size() + epilogCodeBytes;
1388     codeBytes       = AlignUp(codeBytes, sizeof(DWORD));
1389
1390     DWORD codeWords =
1391         codeBytes / sizeof(DWORD); // This is how many words we need to store all the unwind codes in the unwind block
1392
1393     // Do we need the 2nd header word for "Extended Code Words" or "Extended Epilog Count"?
1394
1395     bool needExtendedCodeWordsEpilogCount =
1396         (codeWords > UW_MAX_CODE_WORDS_COUNT) || (epilogCount > UW_MAX_EPILOG_COUNT);
1397
1398     // How many epilog scope words do we need?
1399
1400     bool     setEBit      = false;       // do we need to set the E bit?
1401     unsigned epilogScopes = epilogCount; // Note that this could be zero if we have no epilogs!
1402
1403     if (epilogCount == 1)
1404     {
1405         assert(ufiEpilogList != NULL);
1406         assert(ufiEpilogList->epiNext == NULL);
1407
1408         if (ufiEpilogList->Matches() && (ufiEpilogList->GetStartIndex() == 0) && // The match is with the prolog
1409             !needExtendedCodeWordsEpilogCount && IsAtFragmentEnd(ufiEpilogList))
1410         {
1411             epilogScopes = 0; // Don't need any epilog scope words
1412             setEBit      = true;
1413         }
1414     }
1415
1416     DWORD headerBytes = (1                                            // Always need first header DWORD
1417                          + (needExtendedCodeWordsEpilogCount ? 1 : 0) // Do we need the 2nd DWORD for Extended Code
1418                                                                       // Words or Extended Epilog Count?
1419                          + epilogScopes                               // One DWORD per epilog scope, for EBit = 0
1420                          ) *
1421                         sizeof(DWORD); // convert it to bytes
1422
1423     DWORD finalSize = headerBytes + codeBytes; // Size of actual unwind codes, aligned up to 4-byte words,
1424                                                // including end padding if necessary
1425
1426     // Construct the final unwind information.
1427
1428     // We re-use the memory for the prolog unwind codes to construct the full unwind data. If all the epilogs
1429     // match the prolog, this is easy: we just prepend the header. If there are epilog codes that don't match
1430     // the prolog, we still use the prolog codes memory, but it's a little more complicated, since the
1431     // unwind info is ordered as: (a) header, (b) prolog codes, (c) non-matching epilog codes. And, the prolog
1432     // codes array is filled in from end-to-beginning. So, we compute the size of memory we need, ensure we
1433     // have that much memory, and then copy the prolog codes to the right place, appending the non-matching
1434     // epilog codes and prepending the header.
1435
1436     ufiPrologCodes.SetFinalSize(headerBytes, epilogCodeBytes);
1437
1438     if (epilogCodeBytes != 0)
1439     {
1440         // We need to copy the epilog code bytes to their final memory location
1441
1442         for (pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1443         {
1444             if (!pEpi->Matches())
1445             {
1446                 ufiPrologCodes.AppendEpilog(pEpi);
1447             }
1448         }
1449     }
1450
1451     // Save some data for later
1452
1453     ufiSize                             = finalSize;
1454     ufiSetEBit                          = setEBit;
1455     ufiNeedExtendedCodeWordsEpilogCount = needExtendedCodeWordsEpilogCount;
1456     ufiCodeWords                        = codeWords;
1457     ufiEpilogScopes                     = epilogScopes;
1458 }
1459
1460 // Finalize: Prepare the unwind information for the VM. Compute and prepend the unwind header.
1461
1462 void UnwindFragmentInfo::Finalize(UNATIVE_OFFSET functionLength)
1463 {
1464     assert(ufiInitialized == UFI_INITIALIZED_PATTERN);
1465
1466 #ifdef DEBUG
1467     if (0 && uwiComp->verbose)
1468     {
1469         printf("*************** Before fragment #%d finalize\n", ufiNum);
1470         Dump();
1471     }
1472 #endif
1473
1474 // Compute the header
1475
1476 #if defined(_TARGET_ARM_)
1477     noway_assert((functionLength & 1) == 0);
1478     DWORD headerFunctionLength = functionLength / 2;
1479 #elif defined(_TARGET_ARM64_)
1480     noway_assert((functionLength & 3) == 0);
1481     DWORD headerFunctionLength = functionLength / 4;
1482 #endif // _TARGET_ARM64_
1483
1484     DWORD headerVers = 0; // Version of the unwind info is zero. No other version number is currently defined.
1485     DWORD headerXBit = 0; // We never generate "exception data", but the VM might add some.
1486     DWORD headerEBit;
1487 #if defined(_TARGET_ARM_)
1488     DWORD headerFBit = ufiHasPhantomProlog ? 1 : 0; // Is this data a fragment in the sense of the unwind data
1489                                                     // specification? That is, do the prolog codes represent a real
1490                                                     // prolog or not?
1491 #endif                                              // defined(_TARGET_ARM_)
1492     DWORD headerEpilogCount;                        // This depends on how we set headerEBit.
1493     DWORD headerCodeWords;
1494     DWORD headerExtendedEpilogCount = 0; // This depends on how we set headerEBit.
1495     DWORD headerExtendedCodeWords   = 0;
1496
1497     if (ufiSetEBit)
1498     {
1499         headerEBit        = 1;
1500         headerEpilogCount = ufiEpilogList->GetStartIndex(); // probably zero -- the start of the prolog codes!
1501         headerCodeWords   = ufiCodeWords;
1502     }
1503     else
1504     {
1505         headerEBit = 0;
1506
1507         if (ufiNeedExtendedCodeWordsEpilogCount)
1508         {
1509             headerEpilogCount         = 0;
1510             headerCodeWords           = 0;
1511             headerExtendedEpilogCount = ufiEpilogScopes;
1512             headerExtendedCodeWords   = ufiCodeWords;
1513         }
1514         else
1515         {
1516             headerEpilogCount = ufiEpilogScopes;
1517             headerCodeWords   = ufiCodeWords;
1518         }
1519     }
1520
1521     // Start writing the header
1522
1523     noway_assert(headerFunctionLength <=
1524                  0x3FFFFU); // We create fragments to prevent this from firing, so if it hits, we have an internal error
1525
1526     if ((headerEpilogCount > UW_MAX_EPILOG_COUNT) || (headerCodeWords > UW_MAX_CODE_WORDS_COUNT))
1527     {
1528         IMPL_LIMITATION("unwind data too large");
1529     }
1530
1531 #if defined(_TARGET_ARM_)
1532     DWORD header = headerFunctionLength | (headerVers << 18) | (headerXBit << 20) | (headerEBit << 21) |
1533                    (headerFBit << 22) | (headerEpilogCount << 23) | (headerCodeWords << 28);
1534 #elif defined(_TARGET_ARM64_)
1535     DWORD header               = headerFunctionLength | (headerVers << 18) | (headerXBit << 20) | (headerEBit << 21) |
1536                    (headerEpilogCount << 22) | (headerCodeWords << 27);
1537 #endif // defined(_TARGET_ARM64_)
1538
1539     ufiPrologCodes.AddHeaderWord(header);
1540
1541     // Construct the second header word, if needed
1542
1543     if (ufiNeedExtendedCodeWordsEpilogCount)
1544     {
1545         noway_assert(headerEBit == 0);
1546         noway_assert(headerEpilogCount == 0);
1547         noway_assert(headerCodeWords == 0);
1548         noway_assert((headerExtendedEpilogCount > UW_MAX_EPILOG_COUNT) ||
1549                      (headerExtendedCodeWords > UW_MAX_CODE_WORDS_COUNT));
1550
1551         if ((headerExtendedEpilogCount > UW_MAX_EXTENDED_EPILOG_COUNT) ||
1552             (headerExtendedCodeWords > UW_MAX_EXTENDED_CODE_WORDS_COUNT))
1553         {
1554             IMPL_LIMITATION("unwind data too large");
1555         }
1556
1557         DWORD header2 = headerExtendedEpilogCount | (headerExtendedCodeWords << 16);
1558
1559         ufiPrologCodes.AddHeaderWord(header2);
1560     }
1561
1562     // Construct the epilog scope words, if needed
1563
1564     if (!ufiSetEBit)
1565     {
1566         for (UnwindEpilogInfo* pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1567         {
1568 #if defined(_TARGET_ARM_)
1569             DWORD headerCondition = 0xE; // The epilog is unconditional. We don't have epilogs under the IT instruction.
1570 #endif                                   // defined(_TARGET_ARM_)
1571
1572             // The epilog must strictly follow the prolog. The prolog is in the first fragment of
1573             // the hot section. If this epilog is at the start of a fragment, it can't be the
1574             // first fragment in the hot section. We actually don't know if we're processing
1575             // the hot or cold section (or a funclet), so we can't distinguish these cases. Thus,
1576             // we just assert that the epilog starts within the fragment.
1577             assert(pEpi->GetStartOffset() >= GetStartOffset());
1578
1579             // We report the offset of an epilog as the offset from the beginning of the function/funclet fragment,
1580             // NOT the offset from the beginning of the main function.
1581             DWORD headerEpilogStartOffset = pEpi->GetStartOffset() - GetStartOffset();
1582
1583 #if defined(_TARGET_ARM_)
1584             noway_assert((headerEpilogStartOffset & 1) == 0);
1585             headerEpilogStartOffset /= 2; // The unwind data stores the actual offset divided by 2 (since the low bit of
1586                                           // the actual offset is always zero)
1587 #elif defined(_TARGET_ARM64_)
1588             noway_assert((headerEpilogStartOffset & 3) == 0);
1589             headerEpilogStartOffset /= 4; // The unwind data stores the actual offset divided by 4 (since the low 2 bits
1590                                           // of the actual offset is always zero)
1591 #endif // defined(_TARGET_ARM64_)
1592
1593             DWORD headerEpilogStartIndex = pEpi->GetStartIndex();
1594
1595             if ((headerEpilogStartOffset > UW_MAX_EPILOG_START_OFFSET) ||
1596                 (headerEpilogStartIndex > UW_MAX_EPILOG_START_INDEX))
1597             {
1598                 IMPL_LIMITATION("unwind data too large");
1599             }
1600
1601 #if defined(_TARGET_ARM_)
1602             DWORD epilogScopeWord = headerEpilogStartOffset | (headerCondition << 20) | (headerEpilogStartIndex << 24);
1603 #elif defined(_TARGET_ARM64_)
1604             DWORD epilogScopeWord = headerEpilogStartOffset | (headerEpilogStartIndex << 22);
1605 #endif // defined(_TARGET_ARM64_)
1606
1607             ufiPrologCodes.AddHeaderWord(epilogScopeWord);
1608         }
1609     }
1610
1611     // The unwind code words are already here, following the header, so we're done!
1612 }
1613
1614 void UnwindFragmentInfo::Reserve(BOOL isFunclet, bool isHotCode)
1615 {
1616     assert(isHotCode || !isFunclet); // TODO-CQ: support hot/cold splitting in functions with EH
1617
1618     MergeCodes();
1619
1620     BOOL isColdCode = isHotCode ? FALSE : TRUE;
1621
1622     ULONG unwindSize = Size();
1623
1624 #ifdef DEBUG
1625     if (uwiComp->verbose)
1626     {
1627         if (ufiNum != 1)
1628             printf("reserveUnwindInfo: fragment #%d:\n", ufiNum);
1629     }
1630 #endif
1631
1632     uwiComp->eeReserveUnwindInfo(isFunclet, isColdCode, unwindSize);
1633 }
1634
1635 // Allocate the unwind info for a fragment with the VM.
1636 // Arguments:
1637 //      funKind:       funclet kind
1638 //      pHotCode:      hot section code buffer
1639 //      pColdCode:     cold section code buffer
1640 //      funcEndOffset: offset of the end of this function/funclet. Used if this fragment is the last one for a
1641 //                     function/funclet.
1642 //      isHotCode:     are we allocating the unwind info for the hot code section?
1643
1644 void UnwindFragmentInfo::Allocate(
1645     CorJitFuncKind funKind, void* pHotCode, void* pColdCode, UNATIVE_OFFSET funcEndOffset, bool isHotCode)
1646 {
1647     UNATIVE_OFFSET startOffset;
1648     UNATIVE_OFFSET endOffset;
1649     UNATIVE_OFFSET codeSize;
1650
1651     // We don't support hot/cold splitting with EH, so if there is cold code, this
1652     // better not be a funclet!
1653     // TODO-CQ: support funclets in cold code
1654
1655     noway_assert(isHotCode || funKind == CORJIT_FUNC_ROOT);
1656
1657     // Compute the final size, and start and end offsets of the fragment
1658
1659     startOffset = GetStartOffset();
1660
1661     if (ufiNext == NULL)
1662     {
1663         // This is the last fragment, so the fragment extends to the end of the function/fragment.
1664         assert(funcEndOffset != 0);
1665         endOffset = funcEndOffset;
1666     }
1667     else
1668     {
1669         // The fragment length is all the code between the beginning of this fragment
1670         // and the beginning of the next fragment. Note that all fragments have had their
1671         // offsets computed before any fragment is allocated.
1672         endOffset = ufiNext->GetStartOffset();
1673     }
1674
1675     assert(endOffset > startOffset);
1676     codeSize = endOffset - startOffset;
1677
1678     // Finalize the fragment unwind block to hand to the VM
1679
1680     Finalize(codeSize);
1681
1682     // Get the final unwind information and hand it to the VM
1683
1684     ULONG unwindBlockSize;
1685     BYTE* pUnwindBlock;
1686
1687     GetFinalInfo(&pUnwindBlock, &unwindBlockSize);
1688
1689 #ifdef DEBUG
1690     if (uwiComp->opts.dspUnwind)
1691     {
1692         DumpUnwindInfo(uwiComp, isHotCode, startOffset, endOffset, pUnwindBlock, unwindBlockSize);
1693     }
1694 #endif // DEBUG
1695
1696     // Adjust for cold or hot code:
1697     // 1. The VM doesn't want the cold code pointer unless this is cold code.
1698     // 2. The startOffset and endOffset need to be from the base of the hot section for hot code
1699     //    and from the base of the cold section for cold code
1700
1701     if (isHotCode)
1702     {
1703         assert(endOffset <= uwiComp->info.compTotalHotCodeSize);
1704         pColdCode = NULL;
1705     }
1706     else
1707     {
1708         assert(startOffset >= uwiComp->info.compTotalHotCodeSize);
1709         startOffset -= uwiComp->info.compTotalHotCodeSize;
1710         endOffset -= uwiComp->info.compTotalHotCodeSize;
1711     }
1712
1713 #ifdef DEBUG
1714     if (uwiComp->verbose)
1715     {
1716         if (ufiNum != 1)
1717             printf("unwindEmit: fragment #%d:\n", ufiNum);
1718     }
1719 #endif // DEBUG
1720
1721     uwiComp->eeAllocUnwindInfo((BYTE*)pHotCode, (BYTE*)pColdCode, startOffset, endOffset, unwindBlockSize, pUnwindBlock,
1722                                funKind);
1723 }
1724
1725 #ifdef DEBUG
1726 void UnwindFragmentInfo::Dump(int indent)
1727 {
1728     unsigned          count;
1729     UnwindEpilogInfo* pEpi;
1730
1731     count = 0;
1732     for (pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1733     {
1734         ++count;
1735     }
1736
1737     printf("%*sUnwindFragmentInfo #%d, @0x%08p, size:%d:\n", indent, "", ufiNum, dspPtr(this), sizeof(*this));
1738     printf("%*s  uwiComp: 0x%08p\n", indent, "", dspPtr(uwiComp));
1739     printf("%*s  ufiNext: 0x%08p\n", indent, "", dspPtr(ufiNext));
1740     printf("%*s  ufiEmitLoc: 0x%08p\n", indent, "", dspPtr(ufiEmitLoc));
1741     printf("%*s  ufiHasPhantomProlog: %s\n", indent, "", dspBool(ufiHasPhantomProlog));
1742     printf("%*s  %d epilog%s\n", indent, "", count, (count != 1) ? "s" : "");
1743     printf("%*s  ufiEpilogList: 0x%08p\n", indent, "", dspPtr(ufiEpilogList));
1744     printf("%*s  ufiEpilogLast: 0x%08p\n", indent, "", dspPtr(ufiEpilogLast));
1745     printf("%*s  ufiCurCodes: 0x%08p\n", indent, "", dspPtr(ufiCurCodes));
1746     printf("%*s  ufiSize: %u\n", indent, "", ufiSize);
1747     printf("%*s  ufiSetEBit: %s\n", indent, "", dspBool(ufiSetEBit));
1748     printf("%*s  ufiNeedExtendedCodeWordsEpilogCount: %s\n", indent, "", dspBool(ufiNeedExtendedCodeWordsEpilogCount));
1749     printf("%*s  ufiCodeWords: %u\n", indent, "", ufiCodeWords);
1750     printf("%*s  ufiEpilogScopes: %u\n", indent, "", ufiEpilogScopes);
1751     printf("%*s  ufiStartOffset: 0x%x\n", indent, "", ufiStartOffset);
1752     printf("%*s  ufiInProlog: %s\n", indent, "", dspBool(ufiInProlog));
1753     printf("%*s  ufiInitialized: 0x%08x\n", indent, "", ufiInitialized);
1754
1755     ufiPrologCodes.Dump(indent + 2);
1756
1757     for (pEpi = ufiEpilogList; pEpi != NULL; pEpi = pEpi->epiNext)
1758     {
1759         pEpi->Dump(indent + 2);
1760     }
1761 }
1762 #endif // DEBUG
1763
1764 ///////////////////////////////////////////////////////////////////////////////
1765 //
1766 //  UnwindInfo
1767 //
1768 ///////////////////////////////////////////////////////////////////////////////
1769
1770 void UnwindInfo::InitUnwindInfo(Compiler* comp, emitLocation* startLoc, emitLocation* endLoc)
1771 {
1772     uwiComp = comp;
1773
1774     // The first fragment is a member of UnwindInfo, so it doesn't need to be allocated.
1775     // However, its constructor needs to be explicitly called, since the constructor for
1776     // UnwindInfo is not called.
1777
1778     uwiFragmentFirst.UnwindFragmentInfo::UnwindFragmentInfo(comp, startLoc, false);
1779
1780     uwiFragmentLast = &uwiFragmentFirst;
1781
1782     uwiEndLoc = endLoc;
1783
1784     // Allocate an emitter location object. It is initialized to something
1785     // invalid: it has a null 'ig' that needs to get set before it can be used.
1786     // Note that when we create an UnwindInfo for the cold section, this never
1787     // gets initialized with anything useful, since we never add unwind codes
1788     // to the cold section; we simply distribute the existing (previously added) codes.
1789     uwiCurLoc = new (uwiComp, CMK_UnwindInfo) emitLocation();
1790
1791 #ifdef DEBUG
1792     uwiInitialized = UWI_INITIALIZED_PATTERN;
1793     uwiAddingNOP   = false;
1794 #endif // DEBUG
1795 }
1796
1797 // Split the unwind codes in 'puwi' into those that are in the hot section (leave them in 'puwi')
1798 // and those that are in the cold section (move them to 'this'). There is exactly one fragment
1799 // in each UnwindInfo; the fragments haven't been split for size, yet.
1800
1801 void UnwindInfo::HotColdSplitCodes(UnwindInfo* puwi)
1802 {
1803     // Ensure that there is exactly a single fragment in both the hot and the cold sections
1804     assert(&uwiFragmentFirst == uwiFragmentLast);
1805     assert(&puwi->uwiFragmentFirst == puwi->uwiFragmentLast);
1806     assert(uwiFragmentLast->ufiNext == NULL);
1807     assert(puwi->uwiFragmentLast->ufiNext == NULL);
1808
1809     // The real prolog is in the hot section, so this, cold, section has a phantom prolog
1810     uwiFragmentLast->ufiHasPhantomProlog = true;
1811     uwiFragmentLast->CopyPrologCodes(puwi->uwiFragmentLast);
1812
1813     // Now split the epilog codes
1814     uwiFragmentLast->SplitEpilogCodes(uwiFragmentLast->ufiEmitLoc, puwi->uwiFragmentLast);
1815 }
1816
1817 // Split the function or funclet into fragments that are no larger than 512K,
1818 // so the fragment size will fit in the unwind data "Function Length" field.
1819 // The ARM Exception Data specification "Function Fragments" section describes this.
1820 // We split the function so that it is no larger than 512K bytes, or the value of
1821 // the COMPlus_JitSplitFunctionSize value, if defined (and smaller). We must determine
1822 // how to split the function/funclet before we issue the instructions, so we can
1823 // reserve the unwind space with the VM. The instructions issued may shrink (but not
1824 // expand!) during issuing (although this is extremely rare in any case, and may not
1825 // actually occur on ARM), so we don't finalize actual sizes or offsets.
1826 //
1827 // ARM64 has very similar limitations, except functions can be up to 1MB. TODO-ARM64-Bug?: make sure this works!
1828 //
1829 // We don't split any prolog or epilog. Ideally, we might not split an instruction,
1830 // although that doesn't matter because the unwind at any point would still be
1831 // well-defined.
1832
1833 void UnwindInfo::Split()
1834 {
1835     UNATIVE_OFFSET maxFragmentSize; // The maximum size of a code fragment in bytes
1836
1837     maxFragmentSize = UW_MAX_FRAGMENT_SIZE_BYTES;
1838
1839 #ifdef DEBUG
1840     // Consider COMPlus_JitSplitFunctionSize
1841     unsigned splitFunctionSize = (unsigned)JitConfig.JitSplitFunctionSize();
1842
1843     if (splitFunctionSize != 0)
1844         if (splitFunctionSize < maxFragmentSize)
1845             maxFragmentSize = splitFunctionSize;
1846 #endif // DEBUG
1847
1848     // Now, there should be exactly one fragment.
1849
1850     assert(uwiFragmentLast != NULL);
1851     assert(uwiFragmentLast == &uwiFragmentFirst);
1852     assert(uwiFragmentLast->ufiNext == NULL);
1853
1854     // Find the code size of this function/funclet.
1855
1856     UNATIVE_OFFSET startOffset;
1857     UNATIVE_OFFSET endOffset;
1858     UNATIVE_OFFSET codeSize;
1859
1860     if (uwiFragmentLast->ufiEmitLoc == NULL)
1861     {
1862         // NULL emit location means the beginning of the code. This is to handle the first fragment prolog.
1863         startOffset = 0;
1864     }
1865     else
1866     {
1867         startOffset = uwiFragmentLast->ufiEmitLoc->CodeOffset(uwiComp->genEmitter);
1868     }
1869
1870     if (uwiEndLoc == NULL)
1871     {
1872         // Note that compTotalHotCodeSize and compTotalColdCodeSize are computed before issuing instructions
1873         // from the emitter instruction group offsets, and will be accurate unless the issued code shrinks.
1874         // compNativeCodeSize is precise, but is only set after instructions are issued, which is too late
1875         // for us, since we need to decide how many fragments we need before the code memory is allocated
1876         // (which is before instruction issuing).
1877         UNATIVE_OFFSET estimatedTotalCodeSize =
1878             uwiComp->info.compTotalHotCodeSize + uwiComp->info.compTotalColdCodeSize;
1879         assert(estimatedTotalCodeSize != 0);
1880         endOffset = estimatedTotalCodeSize;
1881     }
1882     else
1883     {
1884         endOffset = uwiEndLoc->CodeOffset(uwiComp->genEmitter);
1885     }
1886
1887     assert(endOffset > startOffset); // there better be at least 1 byte of code
1888     codeSize = endOffset - startOffset;
1889
1890     // Now that we know the code size for this section (main function hot or cold, or funclet),
1891     // figure out how many fragments we're going to need.
1892
1893     UNATIVE_OFFSET numberOfFragments = (codeSize + maxFragmentSize - 1) / maxFragmentSize; // round up
1894     assert(numberOfFragments > 0);
1895
1896     if (numberOfFragments == 1)
1897     {
1898         // No need to split; we're done
1899         return;
1900     }
1901
1902     // Now, we're going to commit to splitting the function into "numberOfFragments" fragments,
1903     // for the purpose of unwind information. We need to do the actual splits so we can figure out
1904     // the size of each piece of unwind data for the call to reserveUnwindInfo(). We won't know
1905     // the actual offsets of the splits since we haven't issued the instructions yet, so store
1906     // an emitter location instead of an offset, and "finalize" the offset in the unwindEmit() phase,
1907     // like we do for the function length and epilog offsets.
1908     CLANG_FORMAT_COMMENT_ANCHOR;
1909
1910 #ifdef DEBUG
1911     if (uwiComp->verbose)
1912     {
1913         printf("Split unwind info into %d fragments (function/funclet size: %d, maximum fragment size: %d)\n",
1914                numberOfFragments, codeSize, maxFragmentSize);
1915     }
1916 #endif // DEBUG
1917
1918     // Call the emitter to do the split, and call us back for every split point it chooses.
1919     uwiComp->genEmitter->emitSplit(uwiFragmentLast->ufiEmitLoc, uwiEndLoc, maxFragmentSize, (void*)this,
1920                                    EmitSplitCallback);
1921
1922 #ifdef DEBUG
1923     // Did the emitter split the function/funclet into as many fragments as we asked for?
1924     // It might be fewer if the COMPlus_JitSplitFunctionSize was used, but it better not
1925     // be fewer if we're splitting into 512K blocks!
1926
1927     unsigned fragCount = 0;
1928     for (UnwindFragmentInfo* pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
1929     {
1930         ++fragCount;
1931     }
1932     if (fragCount < numberOfFragments)
1933     {
1934         if (uwiComp->verbose)
1935         {
1936             printf("WARNING: asked the emitter for %d fragments, but only got %d\n", numberOfFragments, fragCount);
1937         }
1938
1939         // If this fires, then we split into fewer fragments than we asked for, and we are using
1940         // the default, unwind-data-defined 512K maximum fragment size. We won't be able to fit
1941         // this fragment into the unwind data! If you set COMPlus_JitSplitFunctionSize to something
1942         // small, we might not be able to split into as many fragments as asked for, because we
1943         // can't split prologs or epilogs.
1944         assert(maxFragmentSize != UW_MAX_FRAGMENT_SIZE_BYTES);
1945     }
1946 #endif // DEBUG
1947 }
1948
1949 /*static*/ void UnwindInfo::EmitSplitCallback(void* context, emitLocation* emitLoc)
1950 {
1951     UnwindInfo* puwi = (UnwindInfo*)context;
1952     puwi->AddFragment(emitLoc);
1953 }
1954
1955 // Reserve space for the unwind info for all fragments
1956
1957 void UnwindInfo::Reserve(BOOL isFunclet, bool isHotCode)
1958 {
1959     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
1960     assert(isHotCode || !isFunclet);
1961
1962     for (UnwindFragmentInfo* pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
1963     {
1964         pFrag->Reserve(isFunclet, isHotCode);
1965     }
1966 }
1967
1968 // Allocate and populate VM unwind info for all fragments
1969
1970 void UnwindInfo::Allocate(CorJitFuncKind funKind, void* pHotCode, void* pColdCode, bool isHotCode)
1971 {
1972     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
1973
1974     UnwindFragmentInfo* pFrag;
1975
1976     // First, finalize all the offsets (the location of the beginning of fragments, and epilogs),
1977     // so a fragment can use the finalized offset of the subsequent fragment to determine its code size.
1978
1979     UNATIVE_OFFSET endOffset;
1980
1981     if (uwiEndLoc == NULL)
1982     {
1983         assert(uwiComp->info.compNativeCodeSize != 0);
1984         endOffset = uwiComp->info.compNativeCodeSize;
1985     }
1986     else
1987     {
1988         endOffset = uwiEndLoc->CodeOffset(uwiComp->genEmitter);
1989     }
1990
1991     for (pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
1992     {
1993         pFrag->FinalizeOffset();
1994     }
1995
1996     for (pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
1997     {
1998         pFrag->Allocate(funKind, pHotCode, pColdCode, endOffset, isHotCode);
1999     }
2000 }
2001
2002 void UnwindInfo::AddEpilog()
2003 {
2004     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
2005     assert(uwiFragmentLast != NULL);
2006     uwiFragmentLast->AddEpilog();
2007     CaptureLocation();
2008 }
2009
2010 #if defined(_TARGET_ARM_)
2011
2012 unsigned UnwindInfo::GetInstructionSize()
2013 {
2014     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
2015     return uwiComp->genEmitter->emitGetInstructionSize(uwiCurLoc);
2016 }
2017
2018 #endif // defined(_TARGET_ARM_)
2019
2020 void UnwindInfo::CaptureLocation()
2021 {
2022     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
2023     assert(uwiCurLoc != NULL);
2024     uwiCurLoc->CaptureLocation(uwiComp->genEmitter);
2025 }
2026
2027 void UnwindInfo::AddFragment(emitLocation* emitLoc)
2028 {
2029     assert(uwiInitialized == UWI_INITIALIZED_PATTERN);
2030     assert(uwiFragmentLast != NULL);
2031
2032     UnwindFragmentInfo* newFrag = new (uwiComp, CMK_UnwindInfo) UnwindFragmentInfo(uwiComp, emitLoc, true);
2033
2034 #ifdef DEBUG
2035     newFrag->ufiNum = uwiFragmentLast->ufiNum + 1;
2036 #endif // DEBUG
2037
2038     newFrag->CopyPrologCodes(&uwiFragmentFirst);
2039     newFrag->SplitEpilogCodes(emitLoc, uwiFragmentLast);
2040
2041     // Link the new fragment in at the end of the fragment list
2042     uwiFragmentLast->ufiNext = newFrag;
2043     uwiFragmentLast          = newFrag;
2044 }
2045
2046 #ifdef DEBUG
2047
2048 #if defined(_TARGET_ARM_)
2049
2050 // Given the first byte of the unwind code, check that its opsize matches
2051 // the last instruction added in the emitter.
2052 void UnwindInfo::CheckOpsize(BYTE b1)
2053 {
2054     // Adding NOP padding goes through the same path, but doesn't update the location to indicate
2055     // the correct location of the instruction for which we are adding a NOP, so just skip the
2056     // assert. Should be ok, because the emitter is telling us the size of the instruction for
2057     // which we are adding the NOP.
2058     if (uwiAddingNOP)
2059         return;
2060
2061     unsigned opsizeInBytes    = GetOpcodeSizeFromUnwindHeader(b1);
2062     unsigned instrSizeInBytes = GetInstructionSize();
2063     assert(opsizeInBytes == instrSizeInBytes);
2064 }
2065
2066 #endif // defined(_TARGET_ARM_)
2067
2068 void UnwindInfo::Dump(bool isHotCode, int indent)
2069 {
2070     unsigned            count;
2071     UnwindFragmentInfo* pFrag;
2072
2073     count = 0;
2074     for (pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
2075     {
2076         ++count;
2077     }
2078
2079     printf("%*sUnwindInfo %s@0x%08p, size:%d:\n", indent, "", isHotCode ? "" : "COLD ", dspPtr(this), sizeof(*this));
2080     printf("%*s  uwiComp: 0x%08p\n", indent, "", dspPtr(uwiComp));
2081     printf("%*s  %d fragment%s\n", indent, "", count, (count != 1) ? "s" : "");
2082     printf("%*s  uwiFragmentLast: 0x%08p\n", indent, "", dspPtr(uwiFragmentLast));
2083     printf("%*s  uwiEndLoc: 0x%08p\n", indent, "", dspPtr(uwiEndLoc));
2084     printf("%*s  uwiInitialized: 0x%08x\n", indent, "", uwiInitialized);
2085
2086     for (pFrag = &uwiFragmentFirst; pFrag != NULL; pFrag = pFrag->ufiNext)
2087     {
2088         pFrag->Dump(indent + 2);
2089     }
2090 }
2091
2092 #endif // DEBUG
2093
2094 #if defined(_TARGET_ARM_)
2095
2096 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2097 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2098 XX                                                                           XX
2099 XX  Debug dumpers                                                            XX
2100 XX                                                                           XX
2101 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2102 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2103 */
2104
2105 #ifdef DEBUG
2106
2107 // start is 0-based index from LSB, length is number of bits
2108 DWORD ExtractBits(DWORD dw, DWORD start, DWORD length)
2109 {
2110     return (dw >> start) & ((1 << length) - 1);
2111 }
2112
2113 // Dump an integer register set. 'x' is an array of bits where bit 0 = r0, bit 1 = r1, etc.
2114 // The highest register considered is r12.
2115 // If 'lr' is non-zero, the "lr" register is emitted last.
2116 // Returns the number of characters printed.
2117 DWORD DumpIntRegSet(DWORD x, DWORD lr)
2118 {
2119     assert(x != 0 || lr != 0); // we must have one
2120     assert((x & 0xE000) == 0); // don't handle r13 (sp), r14 (lr), r15 (pc) in 'x'
2121     DWORD printed = 0;
2122
2123     printf("{");
2124     ++printed;
2125     bool  first   = true;
2126     DWORD bitMask = 1;
2127     for (DWORD bitNum = 0; bitNum < 12; bitNum++)
2128     {
2129         if (x & bitMask)
2130         {
2131             if (!first)
2132             {
2133                 printf(",");
2134                 ++printed;
2135             }
2136             printf("r%u", bitNum);
2137             printed += (bitNum < 10) ? 2 : 3;
2138             first = false;
2139         }
2140         bitMask <<= 1;
2141     }
2142     if (lr)
2143     {
2144         if (!first)
2145         {
2146             printf(",");
2147             ++printed;
2148         }
2149         printf("lr");
2150         printed += 2;
2151     }
2152     printf("}");
2153     ++printed;
2154
2155     return printed;
2156 }
2157
2158 // Dump a register set range from register 'start' to register 'end'.
2159 // rtype should be "r" or "d" to indicate register type.
2160 // If 'lr' is non-zero, the "lr" register is emitted last. (Note that
2161 // 'lr' should be zero for rtype == "d".)
2162 // Returns the number of characters printed.
2163 DWORD DumpRegSetRange(const char* const rtype, DWORD start, DWORD end, DWORD lr)
2164 {
2165     assert(start <= end);
2166     DWORD printed  = 0;
2167     DWORD rtypeLen = strlen(rtype);
2168
2169     printf("{");
2170     ++printed;
2171     bool first = true;
2172     for (DWORD reg = start; reg <= end; reg++)
2173     {
2174         if (!first)
2175         {
2176             printf(",");
2177             ++printed;
2178         }
2179         printf("%s%u", rtype, reg);
2180         printed += rtypeLen + ((reg < 10) ? 1 : 2);
2181         first = false;
2182     }
2183     if (lr)
2184     {
2185         assert(!first); // If 'lr' is set, it can't be first, since we require a non-empty range
2186         printf(",lr");
2187         printed += 3;
2188     }
2189     printf("}");
2190     ++printed;
2191
2192     return printed;
2193 }
2194
2195 // Dump the opsize.
2196 // Returns the number of characters printed.
2197 DWORD DumpOpsize(DWORD padding, DWORD opsize)
2198 {
2199     if (padding > 100) // underflow?
2200         padding   = 4;
2201     DWORD printed = padding;
2202     for (; padding > 0; padding--)
2203         printf(" ");
2204     printf("; opsize %d\n", opsize);
2205     return printed + 11; // assumes opsize is always 2 digits
2206 }
2207
2208 // Dump the unwind data.
2209 // Arguments:
2210 //      isHotCode:          true if this unwind data is for the hot section
2211 //      startOffset:        byte offset of the code start that this unwind data represents
2212 //      endOffset:          byte offset of the code end   that this unwind data represents
2213 //      pHeader:            pointer to the unwind data blob
2214 //      unwindBlockSize:    size in bytes of the unwind data blob
2215
2216 void DumpUnwindInfo(Compiler*         comp,
2217                     bool              isHotCode,
2218                     UNATIVE_OFFSET    startOffset,
2219                     UNATIVE_OFFSET    endOffset,
2220                     const BYTE* const pHeader,
2221                     ULONG             unwindBlockSize)
2222 {
2223     printf("Unwind Info%s:\n", isHotCode ? "" : " COLD");
2224
2225     // pHeader is not guaranteed to be aligned. We put four 0xFF end codes at the end
2226     // to provide padding, and round down to get a multiple of 4 bytes in size.
2227     DWORD UNALIGNED* pdw = (DWORD UNALIGNED*)pHeader;
2228     DWORD dw;
2229
2230     dw = *pdw++;
2231
2232     DWORD codeWords      = ExtractBits(dw, 28, 4);
2233     DWORD epilogCount    = ExtractBits(dw, 23, 5);
2234     DWORD FBit           = ExtractBits(dw, 22, 1);
2235     DWORD EBit           = ExtractBits(dw, 21, 1);
2236     DWORD XBit           = ExtractBits(dw, 20, 1);
2237     DWORD Vers           = ExtractBits(dw, 18, 2);
2238     DWORD functionLength = ExtractBits(dw, 0, 18);
2239
2240     printf("  >> Start offset   : 0x%06x (not in unwind data)\n", comp->dspOffset(startOffset));
2241     printf("  >>   End offset   : 0x%06x (not in unwind data)\n", comp->dspOffset(endOffset));
2242     printf("  Code Words        : %u\n", codeWords);
2243     printf("  Epilog Count      : %u\n", epilogCount);
2244     printf("  F bit             : %u\n", FBit);
2245     printf("  E bit             : %u\n", EBit);
2246     printf("  X bit             : %u\n", XBit);
2247     printf("  Vers              : %u\n", Vers);
2248     printf("  Function Length   : %u (0x%05x) Actual length = %u (0x%06x)\n", functionLength, functionLength,
2249            functionLength * 2, functionLength * 2);
2250
2251     assert(functionLength * 2 == endOffset - startOffset);
2252
2253     if (codeWords == 0 && epilogCount == 0)
2254     {
2255         // We have an extension word specifying a larger number of Code Words or Epilog Counts
2256         // than can be specified in the header word.
2257
2258         dw = *pdw++;
2259
2260         codeWords   = ExtractBits(dw, 16, 8);
2261         epilogCount = ExtractBits(dw, 0, 16);
2262         assert((dw & 0xF0000000) == 0); // reserved field should be zero
2263
2264         printf("  ---- Extension word ----\n");
2265         printf("  Extended Code Words        : %u\n", codeWords);
2266         printf("  Extended Epilog Count      : %u\n", epilogCount);
2267     }
2268
2269     bool epilogStartAt[256] = {}; // One byte per possible epilog start index; initialized to false
2270
2271     if (EBit == 0)
2272     {
2273         // We have an array of epilog scopes
2274
2275         printf("  ---- Epilog scopes ----\n");
2276         if (epilogCount == 0)
2277         {
2278             printf("  No epilogs\n");
2279         }
2280         else
2281         {
2282             for (DWORD scope = 0; scope < epilogCount; scope++)
2283             {
2284                 dw = *pdw++;
2285
2286                 DWORD epilogStartOffset = ExtractBits(dw, 0, 18);
2287                 DWORD res               = ExtractBits(dw, 18, 2);
2288                 DWORD condition         = ExtractBits(dw, 20, 4);
2289                 DWORD epilogStartIndex  = ExtractBits(dw, 24, 8);
2290
2291                 // Note that epilogStartOffset for a funclet is the offset from the beginning
2292                 // of the current funclet, not the offset from the beginning of the main function.
2293                 // To help find it when looking through JitDump output, also show the offset from
2294                 // the beginning of the main function.
2295                 DWORD epilogStartOffsetFromMainFunctionBegin = epilogStartOffset * 2 + startOffset;
2296
2297                 assert(res == 0);
2298
2299                 printf("  ---- Scope %d\n", scope);
2300                 printf("  Epilog Start Offset        : %u (0x%05x) Actual offset = %u (0x%06x) Offset from main "
2301                        "function begin = %u (0x%06x)\n",
2302                        comp->dspOffset(epilogStartOffset), comp->dspOffset(epilogStartOffset),
2303                        comp->dspOffset(epilogStartOffset * 2), comp->dspOffset(epilogStartOffset * 2),
2304                        comp->dspOffset(epilogStartOffsetFromMainFunctionBegin),
2305                        comp->dspOffset(epilogStartOffsetFromMainFunctionBegin));
2306                 printf("  Condition                  : %u (0x%x)%s\n", condition, condition,
2307                        (condition == 0xE) ? " (always)" : "");
2308                 printf("  Epilog Start Index         : %u (0x%02x)\n", epilogStartIndex, epilogStartIndex);
2309
2310                 epilogStartAt[epilogStartIndex] = true; // an epilog starts at this offset in the unwind codes
2311             }
2312         }
2313     }
2314     else
2315     {
2316         printf("  --- One epilog, unwind codes at %u\n", epilogCount);
2317         assert(epilogCount < _countof(epilogStartAt));
2318         epilogStartAt[epilogCount] = true; // the one and only epilog starts its unwind codes at this offset
2319     }
2320
2321     if (FBit)
2322     {
2323         printf("  ---- Note: 'F' bit is set. Prolog codes are for a 'phantom' prolog.\n");
2324     }
2325
2326     // Dump the unwind codes
2327
2328     printf("  ---- Unwind codes ----\n");
2329
2330     DWORD countOfUnwindCodes = codeWords * 4;
2331     PBYTE pUnwindCode        = (PBYTE)pdw;
2332     BYTE  b1, b2, b3, b4;
2333     DWORD x, y;
2334     DWORD opsize;
2335     DWORD opCol = 52;
2336     DWORD printed;
2337     for (DWORD i = 0; i < countOfUnwindCodes; i++)
2338     {
2339         // Does this byte start an epilog sequence? If so, note that fact.
2340         if (epilogStartAt[i])
2341         {
2342             printf("    ---- Epilog start at index %u ----\n", i);
2343         }
2344
2345         b1 = *pUnwindCode++;
2346
2347         if ((b1 & 0x80) == 0)
2348         {
2349             // 00-7F : add sp, sp, #X*4 (opsize 16)
2350             x = b1 & 0x7F;
2351             printf("    %02X          add sp, sp, #%-8d", b1, x * 4);
2352             DumpOpsize(opCol - 37, 16);
2353         }
2354         else if ((b1 & 0xC0) == 0x80)
2355         {
2356             // 80-BF : pop {r0-r12,lr} (X = bitmask) (opsize 32)
2357             assert(i + 1 < countOfUnwindCodes);
2358             b2 = *pUnwindCode++;
2359             i++;
2360
2361             DWORD LBit = ExtractBits(b1, 5, 1);
2362             x          = ((DWORD)(b1 & 0x1F) << 8) | (DWORD)b2;
2363
2364             printf("    %02X %02X       pop ", b1, b2);
2365             printed = 20;
2366             printed += DumpIntRegSet(x, LBit);
2367             DumpOpsize(opCol - printed, 32);
2368         }
2369         else if ((b1 & 0xF0) == 0xC0)
2370         {
2371             // C0-CF : mov sp, rX (X=0-15) (opsize 16)
2372             x = b1 & 0xF;
2373             printf("    %02X          mov sp, r%u", b1, x);
2374             printed = 25 + ((x > 10) ? 2 : 1);
2375             DumpOpsize(opCol - printed, 16);
2376         }
2377         else if ((b1 & 0xF8) == 0xD0)
2378         {
2379             // D0-D7 : pop {r4-rX,lr} (X=4-7) (opsize 16)
2380             x          = b1 & 0x3;
2381             DWORD LBit = b1 & 0x4;
2382             printf("    %02X          pop ", b1);
2383             printed = 20;
2384             printed += DumpRegSetRange("r", 4, x + 4, LBit);
2385             DumpOpsize(opCol - printed, 16);
2386         }
2387         else if ((b1 & 0xF8) == 0xD8)
2388         {
2389             // D8-DF : pop {r4-rX,lr} (X=8-11) (opsize 32)
2390             x          = b1 & 0x3;
2391             DWORD LBit = b1 & 0x4;
2392             printf("    %02X          pop ", b1);
2393             printed = 20;
2394             printed += DumpRegSetRange("r", 4, x + 8, LBit);
2395             DumpOpsize(opCol - printed, 32);
2396         }
2397         else if ((b1 & 0xF8) == 0xE0)
2398         {
2399             // E0-E7 : vpop {d8-dX} (X=8-15) (opsize 32)
2400             x = b1 & 0x7;
2401             printf("    %02X          vpop ", b1);
2402             printed = 21;
2403             printed += DumpRegSetRange("d", 8, x + 8, 0);
2404             DumpOpsize(opCol - printed, 32);
2405         }
2406         else if ((b1 & 0xFC) == 0xE8)
2407         {
2408             // E8-EB : addw sp, sp, #X*4 (opsize 32)
2409             assert(i + 1 < countOfUnwindCodes);
2410             b2 = *pUnwindCode++;
2411             i++;
2412
2413             x = ((DWORD)(b1 & 0x3) << 8) | (DWORD)b2;
2414
2415             printf("    %02X %02X       addw sp, sp, #%-8u", b1, b2, x * 4);
2416             DumpOpsize(opCol - 38, 32);
2417         }
2418         else if ((b1 & 0xFE) == 0xEC)
2419         {
2420             // EC-ED : pop {r0-r7,lr} (X = bitmask) (opsize 16)
2421             assert(i + 1 < countOfUnwindCodes);
2422             b2 = *pUnwindCode++;
2423             i++;
2424
2425             DWORD LBit = ExtractBits(b1, 0, 1);
2426             x          = (DWORD)b2;
2427
2428             printf("    %02X %02X       pop ", b1, b2);
2429             printed = 20;
2430             printed += DumpIntRegSet(x, LBit);
2431             DumpOpsize(opCol - printed, 16);
2432         }
2433         else if (b1 == 0xEE)
2434         {
2435             assert(i + 1 < countOfUnwindCodes);
2436             b2 = *pUnwindCode++;
2437             i++;
2438
2439             if ((b2 & 0xF0) == 0)
2440             {
2441                 // EE/0x (opsize 16)
2442                 x = b2 & 0xF;
2443                 printf("    %02X %02X       Microsoft-specific (x = %02X)", b1, b2, x);
2444                 DumpOpsize(4, 16);
2445             }
2446             else
2447             {
2448                 // EE/xy (opsize 16)
2449                 x = ExtractBits(b2, 4, 4);
2450                 y = ExtractBits(b2, 0, 4);
2451                 printf("    %02X %02X       Available (x = %02X, y = %02X)", b1, b2, x, y);
2452                 DumpOpsize(4, 16);
2453             }
2454         }
2455         else if (b1 == 0xEF)
2456         {
2457             assert(i + 1 < countOfUnwindCodes);
2458             b2 = *pUnwindCode++;
2459             i++;
2460
2461             if ((b2 & 0xF0) == 0)
2462             {
2463                 // EF/0x : ldr lr, [sp], #X*4 (opsize 32)
2464                 x = b2 & 0xF;
2465                 printf("    %02X %02X       ldr lr, [sp], #%-8u", b1, b2, x * 4);
2466                 DumpOpsize(opCol - 39, 32);
2467             }
2468             else
2469             {
2470                 // EF/xy (opsize 32)
2471                 x = ExtractBits(b2, 4, 4);
2472                 y = ExtractBits(b2, 0, 4);
2473                 printf("    %02X %02X       Available (x = %02X, y = %02X)", b1, b2, x, y);
2474                 DumpOpsize(4, 32);
2475             }
2476         }
2477         else if ((b1 & 0xF7) == 0xF0)
2478         {
2479             // F0-F4
2480             x = b1 & 0x7;
2481             printf("    %02X          Available (x = %02X)\n", b1, x);
2482         }
2483         else if (b1 == 0xF5)
2484         {
2485             // F5 : vpop {dS-dE} (opsize 32)
2486
2487             assert(i + 1 < countOfUnwindCodes);
2488             b2 = *pUnwindCode++;
2489             i++;
2490
2491             DWORD s = ExtractBits(b2, 4, 4);
2492             DWORD e = ExtractBits(b2, 0, 4);
2493
2494             printf("    %02X %02X       vpop ", b1, b2);
2495             printed = 21;
2496             printed += DumpRegSetRange("d", s, e, 0);
2497             DumpOpsize(opCol - printed, 32);
2498         }
2499         else if (b1 == 0xF6)
2500         {
2501             // F6 : vpop {d(S+16)-d(E+16)} (opsize 32)
2502
2503             assert(i + 1 < countOfUnwindCodes);
2504             b2 = *pUnwindCode++;
2505             i++;
2506
2507             DWORD s = ExtractBits(b2, 4, 4);
2508             DWORD e = ExtractBits(b2, 0, 4);
2509
2510             printf("    %02X %02X       vpop ", b1, b2);
2511             printed = 21;
2512             printed += DumpRegSetRange("d", s + 16, e + 16, 0);
2513             DumpOpsize(opCol - printed, 32);
2514         }
2515         else if (b1 == 0xF7 || b1 == 0xF9)
2516         {
2517             // F7, F9 : add sp, sp, #X*4
2518             // 0xF7 has opsize 16, 0xF9 has opsize 32
2519
2520             assert(i + 2 < countOfUnwindCodes);
2521             b2 = *pUnwindCode++;
2522             b3 = *pUnwindCode++;
2523             i += 2;
2524
2525             x = ((DWORD)b2 << 8) | (DWORD)b3;
2526
2527             opsize = (b1 == 0xF7) ? 16 : 32;
2528
2529             printf("    %02X %02X %02X    add sp, sp, #%-8u", b1, b2, b3, x * 4, opsize);
2530             DumpOpsize(opCol - 37, opsize);
2531         }
2532         else if (b1 == 0xF8 || b1 == 0xFA)
2533         {
2534             // F8, FA : add sp, sp, #X*4
2535             // 0xF8 has opsize 16, 0xFA has opsize 32
2536
2537             assert(i + 3 < countOfUnwindCodes);
2538             b2 = *pUnwindCode++;
2539             b3 = *pUnwindCode++;
2540             b4 = *pUnwindCode++;
2541             i += 3;
2542
2543             x = ((DWORD)b2 << 16) | ((DWORD)b3 << 8) | (DWORD)b4;
2544
2545             opsize = (b1 == 0xF8) ? 16 : 32;
2546
2547             printf("    %02X %02X %02X %02X add sp, sp, #%-8u", b1, b2, b3, b4, x * 4, opsize);
2548             DumpOpsize(opCol - 37, opsize);
2549         }
2550         else if (b1 == 0xFB || b1 == 0xFC)
2551         {
2552             // FB, FC : nop
2553             // 0xFB has opsize 16, 0xFC has opsize 32
2554
2555             opsize = (b1 == 0xFB) ? 16 : 32;
2556
2557             printf("    %02X          nop", b1, opsize);
2558             DumpOpsize(opCol - 19, opsize);
2559         }
2560         else if (b1 == 0xFD || b1 == 0xFE)
2561         {
2562             // FD, FE : end + nop
2563             // 0xFD has opsize 16, 0xFE has opsize 32
2564
2565             opsize = (b1 == 0xFD) ? 16 : 32;
2566
2567             printf("    %02X          end + nop", b1, opsize);
2568             DumpOpsize(opCol - 25, opsize);
2569         }
2570         else if (b1 == 0xFF)
2571         {
2572             // FF : end
2573
2574             printf("    %02X          end\n", b1);
2575         }
2576         else
2577         {
2578             assert(!"Internal error decoding unwind codes");
2579         }
2580     }
2581
2582     pdw += codeWords;
2583     assert((PBYTE)pdw == pUnwindCode);
2584     assert((PBYTE)pdw == pHeader + unwindBlockSize);
2585
2586     assert(XBit == 0); // We don't handle the case where exception data is present, such as the Exception Handler RVA
2587
2588     printf("\n");
2589 }
2590
2591 #endif // DEBUG
2592
2593 #endif // defined(_TARGET_ARM_)
2594
2595 #endif // _TARGET_ARMARCH_