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