1353358Sdim//===-- llvm/Support/ARMWinEH.h - Windows on ARM EH Constants ---*- C++ -*-===//
2274955Ssvnmir//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6274955Ssvnmir//
7274955Ssvnmir//===----------------------------------------------------------------------===//
8274955Ssvnmir
9280031Sdim#ifndef LLVM_SUPPORT_ARMWINEH_H
10280031Sdim#define LLVM_SUPPORT_ARMWINEH_H
11274955Ssvnmir
12274955Ssvnmir#include "llvm/ADT/ArrayRef.h"
13274955Ssvnmir#include "llvm/Support/Endian.h"
14274955Ssvnmir
15274955Ssvnmirnamespace llvm {
16274955Ssvnmirnamespace ARM {
17274955Ssvnmirnamespace WinEH {
18274955Ssvnmirenum class RuntimeFunctionFlag {
19274955Ssvnmir  RFF_Unpacked,       /// unpacked entry
20274955Ssvnmir  RFF_Packed,         /// packed entry
21274955Ssvnmir  RFF_PackedFragment, /// packed entry representing a fragment
22274955Ssvnmir  RFF_Reserved,       /// reserved
23274955Ssvnmir};
24274955Ssvnmir
25274955Ssvnmirenum class ReturnType {
26274955Ssvnmir  RT_POP,             /// return via pop {pc} (L flag must be set)
27274955Ssvnmir  RT_B,               /// 16-bit branch
28274955Ssvnmir  RT_BW,              /// 32-bit branch
29274955Ssvnmir  RT_NoEpilogue,      /// no epilogue (fragment)
30274955Ssvnmir};
31274955Ssvnmir
32274955Ssvnmir/// RuntimeFunction - An entry in the table of procedure data (.pdata)
33274955Ssvnmir///
34274955Ssvnmir///  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
35274955Ssvnmir///  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
36274955Ssvnmir/// +---------------------------------------------------------------+
37274955Ssvnmir/// |                     Function Start RVA                        |
38274955Ssvnmir/// +-------------------+-+-+-+-----+-+---+---------------------+---+
39274955Ssvnmir/// |    Stack Adjust   |C|L|R| Reg |H|Ret|   Function Length   |Flg|
40274955Ssvnmir/// +-------------------+-+-+-+-----+-+---+---------------------+---+
41274955Ssvnmir///
42274955Ssvnmir/// Flag : 2-bit field with the following meanings:
43274955Ssvnmir///   - 00 = packed unwind data not used; reamining bits point to .xdata record
44274955Ssvnmir///   - 01 = packed unwind data
45274955Ssvnmir///   - 10 = packed unwind data, function assumed to have no prologue; useful
46274955Ssvnmir///          for function fragments that are discontiguous with the start of the
47274955Ssvnmir///          function
48274955Ssvnmir///   - 11 = reserved
49274955Ssvnmir/// Function Length : 11-bit field providing the length of the entire function
50274955Ssvnmir///                   in bytes, divided by 2; if the function is greater than
51274955Ssvnmir///                   4KB, a full .xdata record must be used instead
52274955Ssvnmir/// Ret : 2-bit field indicating how the function returns
53274955Ssvnmir///   - 00 = return via pop {pc} (the L bit must be set)
54274955Ssvnmir///   - 01 = return via 16-bit branch
55274955Ssvnmir///   - 10 = return via 32-bit branch
56274955Ssvnmir///   - 11 = no epilogue; useful for function fragments that may only contain a
57274955Ssvnmir///          prologue but the epilogue is elsewhere
58274955Ssvnmir/// H : 1-bit flag indicating whether the function "homes" the integer parameter
59274955Ssvnmir///     registers (r0-r3), allocating 16-bytes on the stack
60274955Ssvnmir/// Reg : 3-bit field indicating the index of the last saved non-volatile
61274955Ssvnmir///       register.  If the R bit is set to 0, then only integer registers are
62274955Ssvnmir///       saved (r4-rN, where N is 4 + Reg).  If the R bit is set to 1, then
63274955Ssvnmir///       only floating-point registers are being saved (d8-dN, where N is
64274955Ssvnmir///       8 + Reg).  The special case of the R bit being set to 1 and Reg equal
65274955Ssvnmir///       to 7 indicates that no registers are saved.
66274955Ssvnmir/// R : 1-bit flag indicating whether the non-volatile registers are integer or
67274955Ssvnmir///     floating-point.  0 indicates integer, 1 indicates floating-point.  The
68274955Ssvnmir///     special case of the R-flag being set and Reg being set to 7 indicates
69274955Ssvnmir///     that no non-volatile registers are saved.
70274955Ssvnmir/// L : 1-bit flag indicating whether the function saves/restores the link
71274955Ssvnmir///     register (LR)
72274955Ssvnmir/// C : 1-bit flag indicating whether the function includes extra instructions
73274955Ssvnmir///     to setup a frame chain for fast walking.  If this flag is set, r11 is
74274955Ssvnmir///     implicitly added to the list of saved non-volatile integer registers.
75274955Ssvnmir/// Stack Adjust : 10-bit field indicating the number of bytes of stack that are
76274955Ssvnmir///                allocated for this function.  Only values between 0x000 and
77274955Ssvnmir///                0x3f3 can be directly encoded.  If the value is 0x3f4 or
78274955Ssvnmir///                greater, then the low 4 bits have special meaning as follows:
79274955Ssvnmir///                - Bit 0-1
80274955Ssvnmir///                  indicate the number of words' of adjustment (1-4), minus 1
81274955Ssvnmir///                - Bit 2
82274955Ssvnmir///                  indicates if the prologue combined adjustment into push
83274955Ssvnmir///                - Bit 3
84274955Ssvnmir///                  indicates if the epilogue combined adjustment into pop
85274955Ssvnmir///
86274955Ssvnmir/// RESTRICTIONS:
87274955Ssvnmir///   - IF C is SET:
88274955Ssvnmir///     + L flag must be set since frame chaining requires r11 and lr
89274955Ssvnmir///     + r11 must NOT be included in the set of registers described by Reg
90274955Ssvnmir///   - IF Ret is 0:
91274955Ssvnmir///     + L flag must be set
92274955Ssvnmir
93274955Ssvnmir// NOTE: RuntimeFunction is meant to be a simple class that provides raw access
94274955Ssvnmir// to all fields in the structure.  The accessor methods reflect the names of
95274955Ssvnmir// the bitfields that they correspond to.  Although some obvious simplifications
96274955Ssvnmir// are possible via merging of methods, it would prevent the use of this class
97274955Ssvnmir// to fully inspect the contents of the data structure which is particularly
98274955Ssvnmir// useful for scenarios such as llvm-readobj to aid in testing.
99274955Ssvnmir
100274955Ssvnmirclass RuntimeFunction {
101274955Ssvnmirpublic:
102274955Ssvnmir  const support::ulittle32_t BeginAddress;
103274955Ssvnmir  const support::ulittle32_t UnwindData;
104274955Ssvnmir
105274955Ssvnmir  RuntimeFunction(const support::ulittle32_t *Data)
106274955Ssvnmir    : BeginAddress(Data[0]), UnwindData(Data[1]) {}
107274955Ssvnmir
108274955Ssvnmir  RuntimeFunction(const support::ulittle32_t BeginAddress,
109274955Ssvnmir                  const support::ulittle32_t UnwindData)
110274955Ssvnmir    : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
111274955Ssvnmir
112274955Ssvnmir  RuntimeFunctionFlag Flag() const {
113274955Ssvnmir    return RuntimeFunctionFlag(UnwindData & 0x3);
114274955Ssvnmir  }
115274955Ssvnmir
116274955Ssvnmir  uint32_t ExceptionInformationRVA() const {
117274955Ssvnmir    assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
118274955Ssvnmir           "unpacked form required for this operation");
119274955Ssvnmir    return (UnwindData & ~0x3);
120274955Ssvnmir  }
121274955Ssvnmir
122274955Ssvnmir  uint32_t PackedUnwindData() const {
123274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
124274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
125274955Ssvnmir           "packed form required for this operation");
126274955Ssvnmir    return (UnwindData & ~0x3);
127274955Ssvnmir  }
128274955Ssvnmir  uint32_t FunctionLength() const {
129274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
130274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
131274955Ssvnmir           "packed form required for this operation");
132274955Ssvnmir    return (((UnwindData & 0x00001ffc) >> 2) << 1);
133274955Ssvnmir  }
134274955Ssvnmir  ReturnType Ret() const {
135274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
136274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
137274955Ssvnmir           "packed form required for this operation");
138274955Ssvnmir    assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
139274955Ssvnmir    return ReturnType((UnwindData & 0x00006000) >> 13);
140274955Ssvnmir  }
141274955Ssvnmir  bool H() const {
142274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
143274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
144274955Ssvnmir           "packed form required for this operation");
145274955Ssvnmir    return ((UnwindData & 0x00008000) >> 15);
146274955Ssvnmir  }
147274955Ssvnmir  uint8_t Reg() const {
148274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
149274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
150274955Ssvnmir           "packed form required for this operation");
151274955Ssvnmir    return ((UnwindData & 0x00070000) >> 16);
152274955Ssvnmir  }
153274955Ssvnmir  bool R() const {
154274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
155274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
156274955Ssvnmir           "packed form required for this operation");
157274955Ssvnmir    return ((UnwindData & 0x00080000) >> 19);
158274955Ssvnmir  }
159274955Ssvnmir  bool L() const {
160274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
161274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
162274955Ssvnmir           "packed form required for this operation");
163274955Ssvnmir    return ((UnwindData & 0x00100000) >> 20);
164274955Ssvnmir  }
165274955Ssvnmir  bool C() const {
166274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
167274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
168274955Ssvnmir           "packed form required for this operation");
169274955Ssvnmir    assert(((~UnwindData & 0x00200000) || L()) &&
170274955Ssvnmir           "L flag must be set, chaining requires r11 and LR");
171274955Ssvnmir    assert(((~UnwindData & 0x00200000) || (Reg() < 7) || R()) &&
172274955Ssvnmir           "r11 must not be included in Reg; C implies r11");
173274955Ssvnmir    return ((UnwindData & 0x00200000) >> 21);
174274955Ssvnmir  }
175274955Ssvnmir  uint16_t StackAdjust() const {
176274955Ssvnmir    assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
177274955Ssvnmir            Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
178274955Ssvnmir           "packed form required for this operation");
179274955Ssvnmir    return ((UnwindData & 0xffc00000) >> 22);
180274955Ssvnmir  }
181274955Ssvnmir};
182274955Ssvnmir
183274955Ssvnmir/// PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the
184274955Ssvnmir/// prologue has stack adjustment combined into the push
185274955Ssvnmirinline bool PrologueFolding(const RuntimeFunction &RF) {
186274955Ssvnmir  return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x4);
187274955Ssvnmir}
188274955Ssvnmir/// Epilogue - pseudo-flag derived from Stack Adjust indicating that the
189274955Ssvnmir/// epilogue has stack adjustment combined into the pop
190274955Ssvnmirinline bool EpilogueFolding(const RuntimeFunction &RF) {
191274955Ssvnmir  return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x8);
192274955Ssvnmir}
193274955Ssvnmir/// StackAdjustment - calculated stack adjustment in words.  The stack
194274955Ssvnmir/// adjustment should be determined via this function to account for the special
195274955Ssvnmir/// handling the special encoding when the value is >= 0x3f4.
196274955Ssvnmirinline uint16_t StackAdjustment(const RuntimeFunction &RF) {
197274955Ssvnmir  uint16_t Adjustment = RF.StackAdjust();
198274955Ssvnmir  if (Adjustment >= 0x3f4)
199274955Ssvnmir    return (Adjustment & 0x3) ? ((Adjustment & 0x3) << 2) - 1 : 0;
200274955Ssvnmir  return Adjustment;
201274955Ssvnmir}
202274955Ssvnmir
203274955Ssvnmir/// SavedRegisterMask - Utility function to calculate the set of saved general
204274955Ssvnmir/// purpose (r0-r15) and VFP (d0-d31) registers.
205274955Ssvnmirstd::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF);
206274955Ssvnmir
207274955Ssvnmir/// ExceptionDataRecord - An entry in the table of exception data (.xdata)
208274955Ssvnmir///
209344779Sdim/// The format on ARM is:
210344779Sdim///
211274955Ssvnmir///  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
212274955Ssvnmir///  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
213274955Ssvnmir/// +-------+---------+-+-+-+---+-----------------------------------+
214274955Ssvnmir/// | C Wrd | Epi Cnt |F|E|X|Ver|         Function Length           |
215274955Ssvnmir/// +-------+--------+'-'-'-'---'---+-------------------------------+
216274955Ssvnmir/// |    Reserved    |Ex. Code Words|   (Extended Epilogue Count)   |
217274955Ssvnmir/// +-------+--------+--------------+-------------------------------+
218274955Ssvnmir///
219344779Sdim/// The format on ARM64 is:
220344779Sdim///
221344779Sdim///  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
222344779Sdim///  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
223344779Sdim/// +---------+---------+-+-+---+-----------------------------------+
224344779Sdim/// |  C Wrd  | Epi Cnt |E|X|Ver|         Function Length           |
225344779Sdim/// +---------+------+--'-'-'---'---+-------------------------------+
226344779Sdim/// |    Reserved    |Ex. Code Words|   (Extended Epilogue Count)   |
227344779Sdim/// +-------+--------+--------------+-------------------------------+
228344779Sdim///
229274955Ssvnmir/// Function Length : 18-bit field indicating the total length of the function
230274955Ssvnmir///                   in bytes divided by 2.  If a function is larger than
231274955Ssvnmir///                   512KB, then multiple pdata and xdata records must be used.
232274955Ssvnmir/// Vers : 2-bit field describing the version of the remaining structure.  Only
233274955Ssvnmir///        version 0 is currently defined (values 1-3 are not permitted).
234274955Ssvnmir/// X : 1-bit field indicating the presence of exception data
235274955Ssvnmir/// E : 1-bit field indicating that the single epilogue is packed into the
236274955Ssvnmir///     header
237274955Ssvnmir/// F : 1-bit field indicating that the record describes a function fragment
238274955Ssvnmir///     (implies that no prologue is present, and prologue processing should be
239344779Sdim///     skipped) (ARM only)
240274955Ssvnmir/// Epilogue Count : 5-bit field that differs in meaning based on the E field.
241274955Ssvnmir///
242274955Ssvnmir///                  If E is set, then this field specifies the index of the
243274955Ssvnmir///                  first unwind code describing the (only) epilogue.
244274955Ssvnmir///
245274955Ssvnmir///                  Otherwise, this field indicates the number of exception
246274955Ssvnmir///                  scopes.  If more than 31 scopes exist, then this field and
247274955Ssvnmir///                  the Code Words field must both be set to 0 to indicate that
248274955Ssvnmir///                  an extension word is required.
249344779Sdim/// Code Words : 4-bit (5-bit on ARM64) field that specifies the number of
250344779Sdim///              32-bit words needed to contain all the unwind codes.  If more
251344779Sdim///              than 15 words (31 words on ARM64) are required, then this field
252344779Sdim///              and the Epilogue Count field must both be set to 0 to indicate
253344779Sdim///              that an extension word is required.
254274955Ssvnmir/// Extended Epilogue Count, Extended Code Words :
255274955Ssvnmir///                          Valid only if Epilog Count and Code Words are both
256274955Ssvnmir///                          set to 0.  Provides an 8-bit extended code word
257274955Ssvnmir///                          count and 16-bits for epilogue count
258274955Ssvnmir///
259344779Sdim/// The epilogue scope format on ARM is:
260344779Sdim///
261274955Ssvnmir///  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
262274955Ssvnmir///  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
263274955Ssvnmir/// +----------------+------+---+---+-------------------------------+
264274955Ssvnmir/// |  Ep Start Idx  | Cond |Res|       Epilogue Start Offset       |
265274955Ssvnmir/// +----------------+------+---+-----------------------------------+
266274955Ssvnmir///
267344779Sdim/// The epilogue scope format on ARM64 is:
268344779Sdim///
269344779Sdim///  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
270344779Sdim///  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
271344779Sdim/// +-------------------+-------+---+-------------------------------+
272344779Sdim/// |  Ep Start Idx     |  Res  |   Epilogue Start Offset           |
273344779Sdim/// +-------------------+-------+-----------------------------------+
274344779Sdim///
275274955Ssvnmir/// If the E bit is unset in the header, the header is followed by a series of
276274955Ssvnmir/// epilogue scopes, which are sorted by their offset.
277274955Ssvnmir///
278274955Ssvnmir/// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
279274955Ssvnmir///                        to the start of the function in bytes divided by two
280274955Ssvnmir/// Res : 2-bit field reserved for future expansion (must be set to 0)
281344779Sdim/// Condition : (ARM only) 4-bit field providing the condition under which the
282344779Sdim///             epilogue is executed.  Unconditional epilogues should set this
283344779Sdim///             field to 0xe. Epilogues must be entirely conditional or
284344779Sdim///             unconditional, and in Thumb-2 mode.  The epilogue begins with
285344779Sdim///             the first instruction after the IT opcode.
286274955Ssvnmir/// Epilogue Start Index : 8-bit field indicating the byte index of the first
287274955Ssvnmir///                        unwind code describing the epilogue
288274955Ssvnmir///
289274955Ssvnmir///  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
290274955Ssvnmir///  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
291274955Ssvnmir/// +---------------+---------------+---------------+---------------+
292274955Ssvnmir/// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
293274955Ssvnmir/// +---------------+---------------+---------------+---------------+
294274955Ssvnmir///
295274955Ssvnmir/// Following the epilogue scopes, the byte code describing the unwinding
296274955Ssvnmir/// follows.  This is padded to align up to word alignment.  Bytes are stored in
297274955Ssvnmir/// little endian.
298274955Ssvnmir///
299274955Ssvnmir///  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
300274955Ssvnmir///  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
301274955Ssvnmir/// +---------------------------------------------------------------+
302274955Ssvnmir/// |           Exception Handler RVA (requires X = 1)              |
303274955Ssvnmir/// +---------------------------------------------------------------+
304274955Ssvnmir/// |  (possibly followed by data required for exception handler)   |
305274955Ssvnmir/// +---------------------------------------------------------------+
306274955Ssvnmir///
307274955Ssvnmir/// If the X bit is set in the header, the unwind byte code is followed by the
308274955Ssvnmir/// exception handler information.  This constants of one Exception Handler RVA
309274955Ssvnmir/// which is the address to the exception handler, followed immediately by the
310274955Ssvnmir/// variable length data associated with the exception handler.
311274955Ssvnmir///
312274955Ssvnmir
313274955Ssvnmirstruct EpilogueScope {
314274955Ssvnmir  const support::ulittle32_t ES;
315274955Ssvnmir
316274955Ssvnmir  EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
317344779Sdim  // Same for both ARM and AArch64.
318274955Ssvnmir  uint32_t EpilogueStartOffset() const {
319274955Ssvnmir    return (ES & 0x0003ffff);
320274955Ssvnmir  }
321344779Sdim
322344779Sdim  // Different implementations for ARM and AArch64.
323344779Sdim  uint8_t ResARM() const {
324274955Ssvnmir    return ((ES & 0x000c0000) >> 18);
325274955Ssvnmir  }
326344779Sdim
327344779Sdim  uint8_t ResAArch64() const {
328344779Sdim    return ((ES & 0x000f0000) >> 18);
329344779Sdim  }
330344779Sdim
331344779Sdim  // Condition is only applicable to ARM.
332274955Ssvnmir  uint8_t Condition() const {
333274955Ssvnmir    return ((ES & 0x00f00000) >> 20);
334274955Ssvnmir  }
335344779Sdim
336344779Sdim  // Different implementations for ARM and AArch64.
337344779Sdim  uint8_t EpilogueStartIndexARM() const {
338274955Ssvnmir    return ((ES & 0xff000000) >> 24);
339274955Ssvnmir  }
340344779Sdim
341344779Sdim  uint16_t EpilogueStartIndexAArch64() const {
342344779Sdim    return ((ES & 0xffc00000) >> 22);
343344779Sdim  }
344274955Ssvnmir};
345274955Ssvnmir
346274955Ssvnmirstruct ExceptionDataRecord;
347274955Ssvnmirinline size_t HeaderWords(const ExceptionDataRecord &XR);
348274955Ssvnmir
349274955Ssvnmirstruct ExceptionDataRecord {
350274955Ssvnmir  const support::ulittle32_t *Data;
351344779Sdim  bool isAArch64;
352274955Ssvnmir
353344779Sdim  ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64) :
354344779Sdim    Data(Data), isAArch64(isAArch64) {}
355274955Ssvnmir
356274955Ssvnmir  uint32_t FunctionLength() const {
357274955Ssvnmir    return (Data[0] & 0x0003ffff);
358274955Ssvnmir  }
359274955Ssvnmir
360344779Sdim  uint32_t FunctionLengthInBytesARM() const {
361344779Sdim    return FunctionLength() << 1;
362344779Sdim  }
363344779Sdim
364344779Sdim  uint32_t FunctionLengthInBytesAArch64() const {
365344779Sdim    return FunctionLength() << 2;
366344779Sdim  }
367344779Sdim
368274955Ssvnmir  uint8_t Vers() const {
369274955Ssvnmir    return (Data[0] & 0x000C0000) >> 18;
370274955Ssvnmir  }
371274955Ssvnmir
372274955Ssvnmir  bool X() const {
373274955Ssvnmir    return ((Data[0] & 0x00100000) >> 20);
374274955Ssvnmir  }
375274955Ssvnmir
376274955Ssvnmir  bool E() const {
377274955Ssvnmir    return ((Data[0] & 0x00200000) >> 21);
378274955Ssvnmir  }
379274955Ssvnmir
380274955Ssvnmir  bool F() const {
381344779Sdim    assert(!isAArch64 && "Fragments are only supported on ARMv7 WinEH");
382274955Ssvnmir    return ((Data[0] & 0x00400000) >> 22);
383274955Ssvnmir  }
384274955Ssvnmir
385353358Sdim  uint16_t EpilogueCount() const {
386344779Sdim    if (HeaderWords(*this) == 1) {
387344779Sdim      if (isAArch64)
388344779Sdim        return (Data[0] & 0x07C00000) >> 22;
389274955Ssvnmir      return (Data[0] & 0x0f800000) >> 23;
390344779Sdim    }
391274955Ssvnmir    return Data[1] & 0x0000ffff;
392274955Ssvnmir  }
393274955Ssvnmir
394274955Ssvnmir  uint8_t CodeWords() const {
395344779Sdim    if (HeaderWords(*this) == 1) {
396344779Sdim      if (isAArch64)
397344779Sdim        return (Data[0] & 0xf8000000) >> 27;
398274955Ssvnmir      return (Data[0] & 0xf0000000) >> 28;
399344779Sdim    }
400274955Ssvnmir    return (Data[1] & 0x00ff0000) >> 16;
401274955Ssvnmir  }
402274955Ssvnmir
403274955Ssvnmir  ArrayRef<support::ulittle32_t> EpilogueScopes() const {
404274955Ssvnmir    assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
405274955Ssvnmir    size_t Offset = HeaderWords(*this);
406280031Sdim    return makeArrayRef(&Data[Offset], EpilogueCount());
407274955Ssvnmir  }
408274955Ssvnmir
409280031Sdim  ArrayRef<uint8_t> UnwindByteCode() const {
410274955Ssvnmir    const size_t Offset = HeaderWords(*this)
411274955Ssvnmir                        + (E() ? 0 :  EpilogueCount());
412280031Sdim    const uint8_t *ByteCode =
413280031Sdim      reinterpret_cast<const uint8_t *>(&Data[Offset]);
414280031Sdim    return makeArrayRef(ByteCode, CodeWords() * sizeof(uint32_t));
415274955Ssvnmir  }
416274955Ssvnmir
417274955Ssvnmir  uint32_t ExceptionHandlerRVA() const {
418274955Ssvnmir    assert(X() && "Exception Handler RVA is only valid if the X bit is set");
419274955Ssvnmir    return Data[HeaderWords(*this) + EpilogueCount() + CodeWords()];
420274955Ssvnmir  }
421274955Ssvnmir
422274955Ssvnmir  uint32_t ExceptionHandlerParameter() const {
423274955Ssvnmir    assert(X() && "Exception Handler RVA is only valid if the X bit is set");
424274955Ssvnmir    return Data[HeaderWords(*this) + EpilogueCount() + CodeWords() + 1];
425274955Ssvnmir  }
426274955Ssvnmir};
427274955Ssvnmir
428274955Ssvnmirinline size_t HeaderWords(const ExceptionDataRecord &XR) {
429344779Sdim  if (XR.isAArch64)
430344779Sdim    return (XR.Data[0] & 0xffc00000) ? 1 : 2;
431274955Ssvnmir  return (XR.Data[0] & 0xff800000) ? 1 : 2;
432274955Ssvnmir}
433274955Ssvnmir}
434274955Ssvnmir}
435274955Ssvnmir}
436274955Ssvnmir
437274955Ssvnmir#endif
438