Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// |
| 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 | |
| 14 | #include "DwarfException.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/CodeGen/AsmPrinter.h" |
| 17 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineLocation.h" |
| 21 | #include "llvm/MC/MCAsmInfo.h" |
| 22 | #include "llvm/MC/MCContext.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCSection.h" |
| 25 | #include "llvm/MC/MCStreamer.h" |
| 26 | #include "llvm/MC/MCSymbol.h" |
| 27 | #include "llvm/Target/Mangler.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetFrameLowering.h" |
| 30 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/Target/TargetOptions.h" |
| 33 | #include "llvm/Target/TargetRegisterInfo.h" |
| 34 | #include "llvm/Support/Dwarf.h" |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 36 | #include "llvm/Support/FormattedStream.h" |
| 37 | #include "llvm/ADT/SmallString.h" |
| 38 | #include "llvm/ADT/StringExtras.h" |
| 39 | #include "llvm/ADT/Twine.h" |
| 40 | using namespace llvm; |
| 41 | |
| 42 | DwarfCFIException::DwarfCFIException(AsmPrinter *A) |
| 43 | : DwarfException(A), |
| 44 | shouldEmitTable(false), shouldEmitMoves(false), shouldEmitTableModule(false) |
| 45 | {} |
| 46 | |
| 47 | DwarfCFIException::~DwarfCFIException() {} |
| 48 | |
| 49 | /// EndModule - Emit all exception information that should come after the |
| 50 | /// content. |
| 51 | void DwarfCFIException::EndModule() { |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame] | 52 | if (!Asm->MAI->isExceptionHandlingDwarf()) |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 53 | return; |
| 54 | |
| 55 | if (!shouldEmitTableModule) |
| 56 | return; |
| 57 | |
| 58 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 59 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
| 60 | |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 61 | if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel) |
| 62 | return; |
| 63 | |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 64 | // Emit references to all used personality functions |
| 65 | const std::vector<const Function*> &Personalities = MMI->getPersonalities(); |
| 66 | for (size_t i = 0, e = Personalities.size(); i != e; ++i) { |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 67 | const MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 68 | TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym); |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | /// BeginFunction - Gather pre-function exception information. Assumes it's |
| 73 | /// being emitted immediately after the function entry point. |
| 74 | void DwarfCFIException::BeginFunction(const MachineFunction *MF) { |
| 75 | shouldEmitTable = shouldEmitMoves = false; |
| 76 | |
| 77 | // If any landing pads survive, we need an EH table. |
| 78 | shouldEmitTable = !MMI->getLandingPads().empty(); |
| 79 | |
| 80 | // See if we need frame move info. |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 81 | shouldEmitMoves = MMI->hasDebugInfo() || |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 82 | !Asm->MF->getFunction()->doesNotThrow() || UnwindTablesMandatory; |
| 83 | |
| 84 | if (shouldEmitMoves || shouldEmitTable) |
| 85 | // Assumes in correct section after the entry point. |
| 86 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin", |
| 87 | Asm->getFunctionNumber())); |
| 88 | |
| 89 | shouldEmitTableModule |= shouldEmitTable; |
| 90 | |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 91 | if (shouldEmitMoves || shouldEmitTable) |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 92 | Asm->OutStreamer.EmitCFIStartProc(); |
| 93 | |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 94 | if (!shouldEmitTable) |
| 95 | return; |
| 96 | |
| 97 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 98 | |
| 99 | // Provide LSDA information. |
| 100 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 101 | if (LSDAEncoding != dwarf::DW_EH_PE_omit) |
| 102 | Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception", |
| 103 | Asm->getFunctionNumber()), |
| 104 | LSDAEncoding); |
| 105 | |
| 106 | // Indicate personality routine, if any. |
| 107 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 108 | const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()]; |
| 109 | if (PerEncoding == dwarf::DW_EH_PE_omit || !Per) |
| 110 | return; |
| 111 | |
| 112 | const MCSymbol *Sym; |
| 113 | switch (PerEncoding & 0x70) { |
| 114 | default: |
| 115 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Benjamin Kramer | e506098 | 2011-04-16 10:30:47 +0000 | [diff] [blame] | 116 | case dwarf::DW_EH_PE_absptr: |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 117 | Sym = Asm->Mang->getSymbol(Per); |
| 118 | break; |
Benjamin Kramer | e506098 | 2011-04-16 10:30:47 +0000 | [diff] [blame] | 119 | case dwarf::DW_EH_PE_pcrel: |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 120 | Sym = TLOF.getPersonalityPICSymbol(Per->getName()); |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 123 | Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding); |
Anton Korobeynikov | 9a1ef4e | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /// EndFunction - Gather and emit post-function exception information. |
| 127 | /// |
| 128 | void DwarfCFIException::EndFunction() { |
| 129 | if (!shouldEmitMoves && !shouldEmitTable) return; |
| 130 | |
| 131 | if (shouldEmitMoves) |
| 132 | Asm->OutStreamer.EmitCFIEndProc(); |
| 133 | |
| 134 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end", |
| 135 | Asm->getFunctionNumber())); |
| 136 | |
| 137 | // Map all labels and get rid of any dead landing pads. |
| 138 | MMI->TidyLandingPads(); |
| 139 | |
| 140 | if (shouldEmitTable) |
| 141 | EmitExceptionTable(); |
| 142 | } |