LLVM API Documentation
00001 //===-- DwarfException.h - Dwarf Exception Framework -----------*- C++ -*--===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for writing dwarf exception info into asm files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H 00015 #define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H 00016 00017 #include "DIE.h" 00018 #include "DwarfPrinter.h" 00019 #include "llvm/CodeGen/AsmPrinter.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include <string> 00022 00023 namespace llvm { 00024 00025 struct LandingPadInfo; 00026 class MachineModuleInfo; 00027 class TargetAsmInfo; 00028 class Timer; 00029 class raw_ostream; 00030 00031 //===----------------------------------------------------------------------===// 00032 /// DwarfException - Emits Dwarf exception handling directives. 00033 /// 00034 class VISIBILITY_HIDDEN DwarfException : public Dwarf { 00035 struct FunctionEHFrameInfo { 00036 std::string FnName; 00037 unsigned Number; 00038 unsigned PersonalityIndex; 00039 bool hasCalls; 00040 bool hasLandingPads; 00041 std::vector<MachineMove> Moves; 00042 const Function * function; 00043 00044 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P, 00045 bool hC, bool hL, 00046 const std::vector<MachineMove> &M, 00047 const Function *f): 00048 FnName(FN), Number(Num), PersonalityIndex(P), 00049 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { } 00050 }; 00051 00052 std::vector<FunctionEHFrameInfo> EHFrames; 00053 00054 /// shouldEmitTable - Per-function flag to indicate if EH tables should 00055 /// be emitted. 00056 bool shouldEmitTable; 00057 00058 /// shouldEmitMoves - Per-function flag to indicate if frame moves info 00059 /// should be emitted. 00060 bool shouldEmitMoves; 00061 00062 /// shouldEmitTableModule - Per-module flag to indicate if EH tables 00063 /// should be emitted. 00064 bool shouldEmitTableModule; 00065 00066 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves 00067 /// should be emitted. 00068 bool shouldEmitMovesModule; 00069 00070 /// ExceptionTimer - Timer for the Dwarf exception writer. 00071 Timer *ExceptionTimer; 00072 00073 /// EmitCommonEHFrame - Emit the common eh unwind frame. 00074 /// 00075 void EmitCommonEHFrame(const Function *Personality, unsigned Index); 00076 00077 /// EmitEHFrame - Emit function exception frame information. 00078 /// 00079 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo); 00080 00081 /// EmitExceptionTable - Emit landing pads and actions. 00082 /// 00083 /// The general organization of the table is complex, but the basic concepts 00084 /// are easy. First there is a header which describes the location and 00085 /// organization of the three components that follow. 00086 /// 1. The landing pad site information describes the range of code covered 00087 /// by the try. In our case it's an accumulation of the ranges covered 00088 /// by the invokes in the try. There is also a reference to the landing 00089 /// pad that handles the exception once processed. Finally an index into 00090 /// the actions table. 00091 /// 2. The action table, in our case, is composed of pairs of type ids 00092 /// and next action offset. Starting with the action index from the 00093 /// landing pad site, each type Id is checked for a match to the current 00094 /// exception. If it matches then the exception and type id are passed 00095 /// on to the landing pad. Otherwise the next action is looked up. This 00096 /// chain is terminated with a next action of zero. If no type id is 00097 /// found the the frame is unwound and handling continues. 00098 /// 3. Type id table contains references to all the C++ typeinfo for all 00099 /// catches in the function. This tables is reversed indexed base 1. 00100 00101 /// SharedTypeIds - How many leading type ids two landing pads have in common. 00102 static unsigned SharedTypeIds(const LandingPadInfo *L, 00103 const LandingPadInfo *R); 00104 00105 /// PadLT - Order landing pads lexicographically by type id. 00106 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R); 00107 00108 struct KeyInfo { 00109 static inline unsigned getEmptyKey() { return -1U; } 00110 static inline unsigned getTombstoneKey() { return -2U; } 00111 static unsigned getHashValue(const unsigned &Key) { return Key; } 00112 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; } 00113 static bool isPod() { return true; } 00114 }; 00115 00116 /// PadRange - Structure holding a try-range and the associated landing pad. 00117 struct PadRange { 00118 // The index of the landing pad. 00119 unsigned PadIndex; 00120 // The index of the begin and end labels in the landing pad's label lists. 00121 unsigned RangeIndex; 00122 }; 00123 00124 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType; 00125 00126 /// ActionEntry - Structure describing an entry in the actions table. 00127 struct ActionEntry { 00128 int ValueForTypeID; // The value to write - may not be equal to the type id. 00129 int NextAction; 00130 struct ActionEntry *Previous; 00131 }; 00132 00133 /// CallSiteEntry - Structure describing an entry in the call-site table. 00134 struct CallSiteEntry { 00135 // The 'try-range' is BeginLabel .. EndLabel. 00136 unsigned BeginLabel; // zero indicates the start of the function. 00137 unsigned EndLabel; // zero indicates the end of the function. 00138 00139 // The landing pad starts at PadLabel. 00140 unsigned PadLabel; // zero indicates that there is no landing pad. 00141 unsigned Action; 00142 }; 00143 00144 /// ComputeActionsTable - Compute the actions table and gather the first 00145 /// action index for each landing pad site. 00146 unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs, 00147 SmallVectorImpl<ActionEntry> &Actions, 00148 SmallVectorImpl<unsigned> &FirstActions); 00149 00150 /// ComputeCallSiteTable - Compute the call-site table. The entry for an 00151 /// invoke has a try-range containing the call, a non-zero landing pad and an 00152 /// appropriate action. The entry for an ordinary call has a try-range 00153 /// containing the call and zero for the landing pad and the action. Calls 00154 /// marked 'nounwind' have no entry and must not be contained in the try-range 00155 /// of any entry - they form gaps in the table. Entries must be ordered by 00156 /// try-range address. 00157 void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 00158 const RangeMapType &PadMap, 00159 const SmallVectorImpl<const LandingPadInfo *> &LPs, 00160 const SmallVectorImpl<unsigned> &FirstActions); 00161 void EmitExceptionTable(); 00162 00163 public: 00164 //===--------------------------------------------------------------------===// 00165 // Main entry points. 00166 // 00167 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T); 00168 virtual ~DwarfException(); 00169 00170 /// BeginModule - Emit all exception information that should come prior to the 00171 /// content. 00172 void BeginModule(Module *m, MachineModuleInfo *mmi) { 00173 this->M = m; 00174 this->MMI = mmi; 00175 } 00176 00177 /// EndModule - Emit all exception information that should come after the 00178 /// content. 00179 void EndModule(); 00180 00181 /// BeginFunction - Gather pre-function exception information. Assumes being 00182 /// emitted immediately after the function entry point. 00183 void BeginFunction(MachineFunction *MF); 00184 00185 /// EndFunction - Gather and emit post-function exception information. 00186 void EndFunction(); 00187 }; 00188 00189 } // End of namespace llvm 00190 00191 #endif