Use an enum class.
[oota-llvm.git] / include / llvm / Support / ARMWinEH.h
1 //===-- llvm/Support/WinARMEH.h - Windows on ARM EH Constants ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_WINARMEH_H
11 #define LLVM_SUPPORT_WINARMEH_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/Support/Endian.h"
15
16 namespace llvm {
17 namespace ARM {
18 namespace WinEH {
19 enum class RuntimeFunctionFlag {
20   RFF_Unpacked,       /// unpacked entry
21   RFF_Packed,         /// packed entry
22   RFF_PackedFragment, /// packed entry representing a fragment
23   RFF_Reserved,       /// reserved
24 };
25
26 enum class ReturnType {
27   RT_POP,             /// return via pop {pc} (L flag must be set)
28   RT_B,               /// 16-bit branch
29   RT_BW,              /// 32-bit branch
30   RT_NoEpilogue,      /// no epilogue (fragment)
31 };
32
33 /// RuntimeFunction - An entry in the table of procedure data (.pdata)
34 ///
35 ///  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
36 ///  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
37 /// +---------------------------------------------------------------+
38 /// |                     Function Start RVA                        |
39 /// +-------------------+-+-+-+-----+-+---+---------------------+---+
40 /// |    Stack Adjust   |C|L|R| Reg |H|Ret|   Function Length   |Flg|
41 /// +-------------------+-+-+-+-----+-+---+---------------------+---+
42 ///
43 /// Flag : 2-bit field with the following meanings:
44 ///   - 00 = packed unwind data not used; reamining bits point to .xdata record
45 ///   - 01 = packed unwind data
46 ///   - 10 = packed unwind data, function assumed to have no prologue; useful
47 ///          for function fragments that are discontiguous with the start of the
48 ///          function
49 ///   - 11 = reserved
50 /// Function Length : 11-bit field providing the length of the entire function
51 ///                   in bytes, divided by 2; if the function is greater than
52 ///                   4KB, a full .xdata record must be used instead
53 /// Ret : 2-bit field indicating how the function returns
54 ///   - 00 = return via pop {pc} (the L bit must be set)
55 ///   - 01 = return via 16-bit branch
56 ///   - 10 = return via 32-bit branch
57 ///   - 11 = no epilogue; useful for function fragments that may only contain a
58 ///          prologue but the epilogue is elsewhere
59 /// H : 1-bit flag indicating whether the function "homes" the integer parameter
60 ///     registers (r0-r3), allocating 16-bytes on the stack
61 /// Reg : 3-bit field indicating the index of the last saved non-volatile
62 ///       register.  If the R bit is set to 0, then only integer registers are
63 ///       saved (r4-rN, where N is 4 + Reg).  If the R bit is set to 1, then
64 ///       only floating-point registers are being saved (d8-dN, where N is
65 ///       8 + Reg).  The special case of the R bit being set to 1 and Reg equal
66 ///       to 7 indicates that no registers are saved.
67 /// R : 1-bit flag indicating whether the non-volatile registers are integer or
68 ///     floating-point.  0 indicates integer, 1 indicates floating-point.  The
69 ///     special case of the R-flag being set and Reg being set to 7 indicates
70 ///     that no non-volatile registers are saved.
71 /// L : 1-bit flag indicating whether the function saves/restores the link
72 ///     register (LR)
73 /// C : 1-bit flag indicating whether the function includes extra instructions
74 ///     to setup a frame chain for fast walking.  If this flag is set, r11 is
75 ///     implicitly added to the list of saved non-volatile integer registers.
76 /// Stack Adjust : 10-bit field indicating the number of bytes of stack that are
77 ///                allocated for this function.  Only values between 0x000 and
78 ///                0x3f3 can be directly encoded.  If the value is 0x3f4 or
79 ///                greater, then the low 4 bits have special meaning as follows:
80 ///                - Bit 0-1
81 ///                  indicate the number of words' of adjustment (1-4), minus 1
82 ///                - Bit 2
83 ///                  indicates if the prologue combined adjustment into push
84 ///                - Bit 3
85 ///                  indicates if the epilogue combined adjustment into pop
86 ///
87 /// RESTRICTIONS:
88 ///   - IF C is SET:
89 ///     + L flag must be set since frame chaining requires r11 and lr
90 ///     + r11 must NOT be included in the set of registers described by Reg
91 ///   - IF Ret is 0:
92 ///     + L flag must be set
93 class RuntimeFunction {
94 public:
95   const support::ulittle32_t BeginAddress;
96   const support::ulittle32_t UnwindData;
97
98   RuntimeFunction(const support::ulittle32_t *Data)
99     : BeginAddress(Data[0]), UnwindData(Data[1]) {}
100
101   RuntimeFunction(const support::ulittle32_t BeginAddress,
102                   const support::ulittle32_t UnwindData)
103     : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
104
105   RuntimeFunctionFlag Flag() const {
106     return RuntimeFunctionFlag(UnwindData & 0x3);
107   }
108
109   uint32_t ExceptionInformationRVA() const {
110     assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
111            "unpacked form required for this operation");
112     return (UnwindData & ~0x3);
113   }
114
115   uint32_t PackedUnwindData() const {
116     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
117             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
118            "packed form required for this operation");
119     return (UnwindData & ~0x3);
120   }
121   uint32_t FunctionLength() const {
122     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
123             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
124            "packed form required for this operation");
125     return (((UnwindData & 0x00001ffc) >> 2) << 1);
126   }
127   ReturnType Ret() const {
128     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
129             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
130            "packed form required for this operation");
131     assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
132     return ReturnType((UnwindData & 0x00006000) >> 13);
133   }
134   bool H() const {
135     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
136             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
137            "packed form required for this operation");
138     return ((UnwindData & 0x00008000) >> 15);
139   }
140   uint8_t Reg() const {
141     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
142             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
143            "packed form required for this operation");
144     return ((UnwindData & 0x00070000) >> 16);
145   }
146   bool R() const {
147     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
148             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
149            "packed form required for this operation");
150     return ((UnwindData & 0x00080000) >> 19);
151   }
152   bool L() const {
153     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
154             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
155            "packed form required for this operation");
156     return ((UnwindData & 0x00100000) >> 20);
157   }
158   bool C() const {
159     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
160             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
161            "packed form required for this operation");
162     assert(((~UnwindData & 0x00200000) || L()) &&
163            "L flag must be set, chaining requires r11 and LR");
164     assert(((~UnwindData & 0x00200000) || (Reg() < 7) || R()) &&
165            "r11 must not be included in Reg; C implies r11");
166     return ((UnwindData & 0x00200000) >> 21);
167   }
168   uint16_t StackAdjust() const {
169     assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
170             Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
171            "packed form required for this operation");
172     return ((UnwindData & 0xffc00000) >> 22);
173   }
174 };
175
176 /// PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the
177 /// prologue has stack adjustment combined into the push
178 inline bool PrologueFolding(const RuntimeFunction &RF) {
179   return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x4);
180 }
181 /// Epilogue - pseudo-flag derived from Stack Adjust indicating that the
182 /// epilogue has stack adjustment combined into the pop
183 inline bool EpilogueFolding(const RuntimeFunction &RF) {
184   return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x8);
185 }
186 /// StackAdjustment - calculated stack adjustment in words.  The stack
187 /// adjustment should be determined via this function to account for the special
188 /// handling the special encoding when the value is ≥ 0x3f4.
189 inline uint16_t StackAdjustment(const RuntimeFunction &RF) {
190   uint16_t Adjustment = RF.StackAdjust();
191   if (Adjustment >= 0x3f4)
192     return (Adjustment & 0x3) ? ((Adjustment & 0x3) << 2) - 1 : 0;
193   return Adjustment;
194 }
195
196 /// SavedRegisterMask - Utility function to calculate the set of saved general
197 /// purpose (r0-r15) and VFP (d0-d31) registers.
198 std::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF);
199
200 /// ExceptionDataRecord - An entry in the table of exception data (.xdata)
201 ///
202 ///  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
203 ///  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
204 /// +-------+---------+-+-+-+---+-----------------------------------+
205 /// | C Wrd | Epi Cnt |F|E|X|Ver|         Function Length           |
206 /// +-------+--------+'-'-'-'---'---+-------------------------------+
207 /// |    Reserved    |Ex. Code Words|   (Extended Epilogue Count)   |
208 /// +-------+--------+--------------+-------------------------------+
209 ///
210 /// Function Length : 18-bit field indicating the total length of the function
211 ///                   in bytes divided by 2.  If a function is larger than
212 ///                   512KB, then multiple pdata and xdata records must be used.
213 /// Vers : 2-bit field describing the version of the remaining structure.  Only
214 ///        version 0 is currently defined (values 1-3 are not permitted).
215 /// X : 1-bit field indicating the presence of exception data
216 /// E : 1-bit field indicating that the single epilogue is packed into the
217 ///     header
218 /// F : 1-bit field indicating that the record describes a function fragment
219 ///     (implies that no prologue is present, and prologue processing should be
220 ///     skipped)
221 /// Epilogue Count : 5-bit field that differs in meaning based on the E field.
222 ///
223 ///                  If E is set, then this field specifies the index of the
224 ///                  first unwind code describing the (only) epilogue.
225 ///
226 ///                  Otherwise, this field indicates the number of exception
227 ///                  scopes.  If more than 31 scopes exist, then this field and
228 ///                  the Code Words field must both be set to 0 to indicate that
229 ///                  an extension word is required.
230 /// Code Words : 4-bit field that species the number of 32-bit words needed to
231 ///              contain all the unwind codes.  If more than 15 words (63 code
232 ///              bytes) are required, then this field and the Epilogue Count
233 ///              field must both be set to 0 to indicate that an extension word
234 ///              is required.
235 /// Extended Epilogue Count, Extended Code Words :
236 ///                          Valid only if Epilog Count and Code Words are both
237 ///                          set to 0.  Provides an 8-bit extended code word
238 ///                          count and 16-bits for epilogue count
239 ///
240 ///  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
241 ///  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
242 /// +----------------+------+---+---+-------------------------------+
243 /// |  Ep Start Idx  | Cond |Res|       Epilogue Start Offset       |
244 /// +----------------+------+---+-----------------------------------+
245 ///
246 /// If the E bit is unset in the header, the header is followed by a series of
247 /// epilogue scopes, which are sorted by their offset.
248 ///
249 /// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
250 ///                        to the start of the function in bytes divided by two
251 /// Res : 2-bit field reserved for future expansion (must be set to 0)
252 /// Condition : 4-bit field providing the condition under which the epilogue is
253 ///             executed.  Unconditional epilogues should set this field to 0xe.
254 ///             Epilogues must be entirely conditional or unconditional, and in
255 ///             Thumb-2 mode.  The epilogue beings with the first instruction
256 ///             after the IT opcode.
257 /// Epilogue Start Index : 8-bit field indicating the byte index of the first
258 ///                        unwind code describing the epilogue
259 ///
260 ///  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
261 ///  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
262 /// +---------------+---------------+---------------+---------------+
263 /// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
264 /// +---------------+---------------+---------------+---------------+
265 ///
266 /// Following the epilogue scopes, the byte code describing the unwinding
267 /// follows.  This is padded to align up to word alignment.  Bytes are stored in
268 /// little endian.
269 ///
270 ///  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
271 ///  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
272 /// +---------------------------------------------------------------+
273 /// |           Exception Handler RVA (requires X = 1)              |
274 /// +---------------------------------------------------------------+
275 /// |  (possibly followed by data required for exception handler)   |
276 /// +---------------------------------------------------------------+
277 ///
278 /// If the X bit is set in the header, the unwind byte code is followed by the
279 /// exception handler information.  This constants of one Exception Handler RVA
280 /// which is the address to the exception handler, followed immediately by the
281 /// variable length data associated with the exception handler.
282 ///
283
284 struct EpilogueScope {
285   const support::ulittle32_t ES;
286
287   EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
288   uint32_t EpilogueStartOffset() const {
289     return (ES & 0x0003ffff);
290   }
291   uint8_t Res() const {
292     return ((ES & 0x000c0000) >> 18);
293   }
294   uint8_t Condition() const {
295     return ((ES & 0x00f00000) >> 20);
296   }
297   uint8_t EpilogueStartIndex() const {
298     return ((ES & 0xff000000) >> 24);
299   }
300 };
301
302 struct ExceptionDataRecord;
303 inline size_t HeaderWords(const ExceptionDataRecord &XR);
304
305 struct ExceptionDataRecord {
306   const support::ulittle32_t *Data;
307
308   ExceptionDataRecord(const support::ulittle32_t *Data) : Data(Data) {}
309
310   uint32_t FunctionLength() const {
311     return (Data[0] & 0x0003ffff);
312   }
313
314   uint8_t Vers() const {
315     return (Data[0] & 0x000C0000) >> 18;
316   }
317
318   bool X() const {
319     return ((Data[0] & 0x00100000) >> 20);
320   }
321
322   bool E() const {
323     return ((Data[0] & 0x00200000) >> 21);
324   }
325
326   bool F() const {
327     return ((Data[0] & 0x00400000) >> 22);
328   }
329
330   uint8_t EpilogueCount() const {
331     if (HeaderWords(*this) == 1)
332       return (Data[0] & 0x0f800000) >> 23;
333     return Data[1] & 0x0000ffff;
334   }
335
336   uint8_t CodeWords() const {
337     if (HeaderWords(*this) == 1)
338       return (Data[0] & 0xf0000000) >> 28;
339     return (Data[1] & 0x00ff0000) >> 16;
340   }
341
342   ArrayRef<support::ulittle32_t> EpilogueScopes() const {
343     assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
344     size_t Offset = HeaderWords(*this);
345     return ArrayRef<support::ulittle32_t>(&Data[Offset], EpilogueCount());
346   }
347
348   ArrayRef<support::ulittle8_t> UnwindByteCode() const {
349     const size_t Offset = HeaderWords(*this)
350                         + (E() ? 0 :  EpilogueCount());
351     const support::ulittle8_t *ByteCode =
352       reinterpret_cast<const support::ulittle8_t *>(&Data[Offset]);
353     return ArrayRef<support::ulittle8_t>(ByteCode,
354                                          CodeWords() * sizeof(uint32_t));
355   }
356
357   uint32_t ExceptionHandlerRVA() const {
358     assert(X() && "Exception Handler RVA is only valid if the X bit is set");
359     return Data[HeaderWords(*this) + EpilogueCount() + CodeWords()];
360   }
361
362   uint32_t ExceptionHandlerParameter() const {
363     assert(X() && "Exception Handler RVA is only valid if the X bit is set");
364     return Data[HeaderWords(*this) + EpilogueCount() + CodeWords() + 1];
365   }
366 };
367
368 inline size_t HeaderWords(const ExceptionDataRecord &XR) {
369   return (XR.Data[0] & 0xff800000) ? 1 : 2;
370 }
371 }
372 }
373 }
374
375 #endif
376