Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 1 | //===-- DwarfException.h - Dwarf Exception Framework -----------*- 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 | // This file contains support for writing dwarf exception info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 6f37f8f | 2009-07-17 20:32:07 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H |
| 15 | #define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 16 | |
| 17 | #include "DIE.h" |
| 18 | #include "DwarfPrinter.h" |
| 19 | #include "llvm/CodeGen/AsmPrinter.h" |
| 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #include <string> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | struct LandingPadInfo; |
| 26 | class MachineModuleInfo; |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 27 | class MCAsmInfo; |
Bill Wendling | 7378b1b | 2009-11-17 01:23:53 +0000 | [diff] [blame] | 28 | class MCExpr; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 29 | class Timer; |
| 30 | class raw_ostream; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | /// DwarfException - Emits Dwarf exception handling directives. |
| 34 | /// |
Chris Lattner | 066c9ac | 2010-01-22 22:23:57 +0000 | [diff] [blame] | 35 | class DwarfException : public DwarfPrinter { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 36 | struct FunctionEHFrameInfo { |
Chris Lattner | 3a9be0e | 2010-01-23 05:51:36 +0000 | [diff] [blame^] | 37 | MCSymbol *FunctionEHSym; // L_foo.eh |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 38 | unsigned Number; |
| 39 | unsigned PersonalityIndex; |
| 40 | bool hasCalls; |
| 41 | bool hasLandingPads; |
| 42 | std::vector<MachineMove> Moves; |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 43 | const Function *function; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 3a9be0e | 2010-01-23 05:51:36 +0000 | [diff] [blame^] | 45 | FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P, |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 46 | bool hC, bool hL, |
| 47 | const std::vector<MachineMove> &M, |
| 48 | const Function *f): |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 49 | FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P), |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 50 | hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { } |
| 51 | }; |
| 52 | |
| 53 | std::vector<FunctionEHFrameInfo> EHFrames; |
| 54 | |
Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 55 | /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index |
| 56 | /// uses an LSDA. If so, then we need to encode that information in the CIE's |
| 57 | /// augmentation. |
| 58 | DenseMap<unsigned, bool> UsesLSDA; |
| 59 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 60 | /// shouldEmitTable - Per-function flag to indicate if EH tables should |
| 61 | /// be emitted. |
| 62 | bool shouldEmitTable; |
| 63 | |
| 64 | /// shouldEmitMoves - Per-function flag to indicate if frame moves info |
| 65 | /// should be emitted. |
| 66 | bool shouldEmitMoves; |
| 67 | |
| 68 | /// shouldEmitTableModule - Per-module flag to indicate if EH tables |
| 69 | /// should be emitted. |
| 70 | bool shouldEmitTableModule; |
| 71 | |
| 72 | /// shouldEmitFrameModule - Per-module flag to indicate if frame moves |
| 73 | /// should be emitted. |
| 74 | bool shouldEmitMovesModule; |
| 75 | |
| 76 | /// ExceptionTimer - Timer for the Dwarf exception writer. |
| 77 | Timer *ExceptionTimer; |
| 78 | |
Bill Wendling | bb3e299 | 2009-09-10 00:04:48 +0000 | [diff] [blame] | 79 | /// SizeOfEncodedValue - Return the size of the encoding in bytes. |
Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 80 | unsigned SizeOfEncodedValue(unsigned Encoding); |
| 81 | |
Bill Wendling | 7ccda0f | 2009-08-25 08:08:33 +0000 | [diff] [blame] | 82 | /// EmitCIE - Emit a Common Information Entry (CIE). This holds information |
| 83 | /// that is shared among many Frame Description Entries. There is at least |
| 84 | /// one CIE in every non-empty .debug_frame section. |
| 85 | void EmitCIE(const Function *Personality, unsigned Index); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 86 | |
Bill Wendling | 7ccda0f | 2009-08-25 08:08:33 +0000 | [diff] [blame] | 87 | /// EmitFDE - Emit the Frame Description Entry (FDE) for the function. |
| 88 | void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 89 | |
| 90 | /// EmitExceptionTable - Emit landing pads and actions. |
| 91 | /// |
| 92 | /// The general organization of the table is complex, but the basic concepts |
| 93 | /// are easy. First there is a header which describes the location and |
| 94 | /// organization of the three components that follow. |
| 95 | /// 1. The landing pad site information describes the range of code covered |
| 96 | /// by the try. In our case it's an accumulation of the ranges covered |
| 97 | /// by the invokes in the try. There is also a reference to the landing |
| 98 | /// pad that handles the exception once processed. Finally an index into |
| 99 | /// the actions table. |
| 100 | /// 2. The action table, in our case, is composed of pairs of type ids |
| 101 | /// and next action offset. Starting with the action index from the |
| 102 | /// landing pad site, each type Id is checked for a match to the current |
| 103 | /// exception. If it matches then the exception and type id are passed |
| 104 | /// on to the landing pad. Otherwise the next action is looked up. This |
| 105 | /// chain is terminated with a next action of zero. If no type id is |
| 106 | /// found the the frame is unwound and handling continues. |
| 107 | /// 3. Type id table contains references to all the C++ typeinfo for all |
| 108 | /// catches in the function. This tables is reversed indexed base 1. |
| 109 | |
| 110 | /// SharedTypeIds - How many leading type ids two landing pads have in common. |
| 111 | static unsigned SharedTypeIds(const LandingPadInfo *L, |
| 112 | const LandingPadInfo *R); |
| 113 | |
| 114 | /// PadLT - Order landing pads lexicographically by type id. |
| 115 | static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R); |
| 116 | |
| 117 | struct KeyInfo { |
| 118 | static inline unsigned getEmptyKey() { return -1U; } |
| 119 | static inline unsigned getTombstoneKey() { return -2U; } |
| 120 | static unsigned getHashValue(const unsigned &Key) { return Key; } |
| 121 | static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 124 | /// PadRange - Structure holding a try-range and the associated landing pad. |
| 125 | struct PadRange { |
| 126 | // The index of the landing pad. |
| 127 | unsigned PadIndex; |
| 128 | // The index of the begin and end labels in the landing pad's label lists. |
| 129 | unsigned RangeIndex; |
| 130 | }; |
| 131 | |
| 132 | typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType; |
| 133 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 134 | /// ActionEntry - Structure describing an entry in the actions table. |
| 135 | struct ActionEntry { |
| 136 | int ValueForTypeID; // The value to write - may not be equal to the type id. |
| 137 | int NextAction; |
| 138 | struct ActionEntry *Previous; |
| 139 | }; |
| 140 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 141 | /// CallSiteEntry - Structure describing an entry in the call-site table. |
| 142 | struct CallSiteEntry { |
| 143 | // The 'try-range' is BeginLabel .. EndLabel. |
| 144 | unsigned BeginLabel; // zero indicates the start of the function. |
| 145 | unsigned EndLabel; // zero indicates the end of the function. |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 146 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 147 | // The landing pad starts at PadLabel. |
| 148 | unsigned PadLabel; // zero indicates that there is no landing pad. |
| 149 | unsigned Action; |
| 150 | }; |
| 151 | |
Bill Wendling | d460962 | 2009-07-28 23:23:00 +0000 | [diff] [blame] | 152 | /// ComputeActionsTable - Compute the actions table and gather the first |
| 153 | /// action index for each landing pad site. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 154 | unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs, |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 155 | SmallVectorImpl<ActionEntry> &Actions, |
| 156 | SmallVectorImpl<unsigned> &FirstActions); |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 157 | |
Bill Wendling | ed060dc | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 158 | /// CallToNoUnwindFunction - Return `true' if this is a call to a function |
| 159 | /// marked `nounwind'. Return `false' otherwise. |
| 160 | bool CallToNoUnwindFunction(const MachineInstr *MI); |
| 161 | |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 162 | /// ComputeCallSiteTable - Compute the call-site table. The entry for an |
| 163 | /// invoke has a try-range containing the call, a non-zero landing pad and an |
| 164 | /// appropriate action. The entry for an ordinary call has a try-range |
| 165 | /// containing the call and zero for the landing pad and the action. Calls |
| 166 | /// marked 'nounwind' have no entry and must not be contained in the try-range |
| 167 | /// of any entry - they form gaps in the table. Entries must be ordered by |
| 168 | /// try-range address. |
| 169 | void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, |
| 170 | const RangeMapType &PadMap, |
| 171 | const SmallVectorImpl<const LandingPadInfo *> &LPs, |
| 172 | const SmallVectorImpl<unsigned> &FirstActions); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 173 | void EmitExceptionTable(); |
| 174 | |
Bill Wendling | 7378b1b | 2009-11-17 01:23:53 +0000 | [diff] [blame] | 175 | /// CreateLabelDiff - Emit a label and subtract it from the expression we |
| 176 | /// already have. This is equivalent to emitting "foo - .", but we have to |
| 177 | /// emit the label for "." directly. |
| 178 | const MCExpr *CreateLabelDiff(const MCExpr *ExprRef, const char *LabelName, |
| 179 | unsigned Index); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 180 | public: |
| 181 | //===--------------------------------------------------------------------===// |
| 182 | // Main entry points. |
| 183 | // |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 184 | DwarfException(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 185 | virtual ~DwarfException(); |
| 186 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 187 | /// BeginModule - Emit all exception information that should come prior to the |
| 188 | /// content. |
Devang Patel | 208622d | 2009-06-25 22:36:02 +0000 | [diff] [blame] | 189 | void BeginModule(Module *m, MachineModuleInfo *mmi) { |
| 190 | this->M = m; |
| 191 | this->MMI = mmi; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /// EndModule - Emit all exception information that should come after the |
| 195 | /// content. |
| 196 | void EndModule(); |
| 197 | |
| 198 | /// BeginFunction - Gather pre-function exception information. Assumes being |
| 199 | /// emitted immediately after the function entry point. |
| 200 | void BeginFunction(MachineFunction *MF); |
| 201 | |
| 202 | /// EndFunction - Gather and emit post-function exception information. |
| 203 | void EndFunction(); |
| 204 | }; |
| 205 | |
| 206 | } // End of namespace llvm |
| 207 | |
| 208 | #endif |