Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/ARMException.cpp - ARM EHABI 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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/ADT/Twine.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 23 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCContext.h" |
| 27 | #include "llvm/MC/MCExpr.h" |
| 28 | #include "llvm/MC/MCSection.h" |
| 29 | #include "llvm/MC/MCStreamer.h" |
| 30 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Dwarf.h" |
| 33 | #include "llvm/Support/FormattedStream.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetFrameLowering.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetOptions.h" |
| 36 | #include "llvm/Target/TargetRegisterInfo.h" |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
| 39 | ARMException::ARMException(AsmPrinter *A) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 40 | : DwarfException(A), |
| 41 | shouldEmitCFI(false) {} |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 42 | |
| 43 | ARMException::~ARMException() {} |
| 44 | |
Rafael Espindola | 320296a | 2013-10-08 13:08:17 +0000 | [diff] [blame] | 45 | ARMTargetStreamer &ARMException::getTargetStreamer() { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 46 | MCTargetStreamer &TS = *Asm->OutStreamer.getTargetStreamer(); |
Rafael Espindola | 320296a | 2013-10-08 13:08:17 +0000 | [diff] [blame] | 47 | return static_cast<ARMTargetStreamer &>(TS); |
| 48 | } |
| 49 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 50 | /// endModule - Emit all exception information that should come after the |
| 51 | /// content. |
| 52 | void ARMException::endModule() { |
| 53 | if (shouldEmitCFI) |
| 54 | Asm->OutStreamer.EmitCFISections(false, true); |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 57 | /// beginFunction - Gather pre-function exception information. Assumes it's |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 58 | /// being emitted immediately after the function entry point. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 59 | void ARMException::beginFunction(const MachineFunction *MF) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 60 | if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM) |
| 61 | getTargetStreamer().emitFnStart(); |
| 62 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin", |
| 63 | Asm->getFunctionNumber())); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 64 | // See if we need call frame info. |
| 65 | AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); |
| 66 | assert(MoveType != AsmPrinter::CFI_M_EH && |
| 67 | "non-EH CFI not yet supported in prologue with EHABI lowering"); |
| 68 | if (MoveType == AsmPrinter::CFI_M_Debug) { |
| 69 | shouldEmitCFI = true; |
| 70 | Asm->OutStreamer.EmitCFIStartProc(false); |
| 71 | } |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 74 | /// endFunction - Gather and emit post-function exception information. |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 75 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 76 | void ARMException::endFunction(const MachineFunction *) { |
| 77 | if (shouldEmitCFI) |
| 78 | Asm->OutStreamer.EmitCFIEndProc(); |
| 79 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 80 | // Map all labels and get rid of any dead landing pads. |
| 81 | MMI->TidyLandingPads(); |
| 82 | |
Rafael Espindola | 320296a | 2013-10-08 13:08:17 +0000 | [diff] [blame] | 83 | ARMTargetStreamer &ATS = getTargetStreamer(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 84 | if (!Asm->MF->getFunction()->needsUnwindTableEntry() && |
| 85 | MMI->getLandingPads().empty()) |
Rafael Espindola | 320296a | 2013-10-08 13:08:17 +0000 | [diff] [blame] | 86 | ATS.emitCantUnwind(); |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 87 | else { |
| 88 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end", |
| 89 | Asm->getFunctionNumber())); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 90 | if (!MMI->getLandingPads().empty()) { |
| 91 | // Emit references to personality. |
| 92 | if (const Function * Personality = |
| 93 | MMI->getPersonalities()[MMI->getPersonalityIndex()]) { |
| 94 | MCSymbol *PerSym = Asm->getSymbol(Personality); |
| 95 | Asm->OutStreamer.EmitSymbolAttribute(PerSym, MCSA_Global); |
| 96 | ATS.emitPersonality(PerSym); |
Anton Korobeynikov | 062a6c8 | 2012-11-14 19:13:30 +0000 | [diff] [blame] | 97 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 98 | |
| 99 | // Emit .handlerdata directive. |
| 100 | ATS.emitHandlerData(); |
| 101 | |
| 102 | // Emit actual exception table |
| 103 | EmitExceptionTable(); |
Evgeniy Stepanov | 53fa1ae | 2012-01-24 13:05:33 +0000 | [diff] [blame] | 104 | } |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 107 | if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM) |
| 108 | ATS.emitFnEnd(); |
Anton Korobeynikov | b5e16af | 2011-03-05 18:43:15 +0000 | [diff] [blame] | 109 | } |
Anton Korobeynikov | 2386fc8 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 110 | |
| 111 | void ARMException::EmitTypeInfos(unsigned TTypeEncoding) { |
| 112 | const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
| 113 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 114 | |
| 115 | bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); |
| 116 | |
| 117 | int Entry = 0; |
| 118 | // Emit the Catch TypeInfos. |
| 119 | if (VerboseAsm && !TypeInfos.empty()) { |
| 120 | Asm->OutStreamer.AddComment(">> Catch TypeInfos <<"); |
| 121 | Asm->OutStreamer.AddBlankLine(); |
| 122 | Entry = TypeInfos.size(); |
| 123 | } |
| 124 | |
| 125 | for (std::vector<const GlobalVariable *>::const_reverse_iterator |
| 126 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { |
| 127 | const GlobalVariable *GV = *I; |
| 128 | if (VerboseAsm) |
| 129 | Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--)); |
Anton Korobeynikov | 239938f | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 130 | Asm->EmitTTypeReference(GV, TTypeEncoding); |
Anton Korobeynikov | 2386fc8 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Emit the Exception Specifications. |
| 134 | if (VerboseAsm && !FilterIds.empty()) { |
| 135 | Asm->OutStreamer.AddComment(">> Filter TypeInfos <<"); |
| 136 | Asm->OutStreamer.AddBlankLine(); |
| 137 | Entry = 0; |
| 138 | } |
| 139 | for (std::vector<unsigned>::const_iterator |
| 140 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { |
| 141 | unsigned TypeID = *I; |
| 142 | if (VerboseAsm) { |
| 143 | --Entry; |
| 144 | if (TypeID != 0) |
| 145 | Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry)); |
| 146 | } |
| 147 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 148 | Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]), |
Anton Korobeynikov | 239938f | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 149 | TTypeEncoding); |
Anton Korobeynikov | 2386fc8 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 150 | } |
| 151 | } |