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