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" 15 #include "llvm/ADT/Twine.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/Mangler.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/MC/MCAsmInfo.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCSection.h" 24 #include "llvm/MC/MCStreamer.h" 25 #include "llvm/MC/MCSymbol.h" 26 #include "llvm/Support/FormattedStream.h" 27 #include "llvm/Target/TargetOptions.h" 28 using namespace llvm; 29 30 ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {} 31 32 ARMException::~ARMException() {} 33 34 ARMTargetStreamer &ARMException::getTargetStreamer() { 35 MCTargetStreamer &TS = *Asm->OutStreamer->getTargetStreamer(); 36 return static_cast<ARMTargetStreamer &>(TS); 37 } 38 39 void ARMException::beginFunction(const MachineFunction *MF) { 40 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM) 41 getTargetStreamer().emitFnStart(); 42 // See if we need call frame info. 43 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); 44 assert(MoveType != AsmPrinter::CFI_M_EH && 45 "non-EH CFI not yet supported in prologue with EHABI lowering"); 46 47 if (MoveType == AsmPrinter::CFI_M_Debug) { 48 if (!hasEmittedCFISections) { 49 if (Asm->needsOnlyDebugCFIMoves()) 50 Asm->OutStreamer->EmitCFISections(false, true); 51 hasEmittedCFISections = true; 52 } 53 54 shouldEmitCFI = true; 55 Asm->OutStreamer->EmitCFIStartProc(false); 56 } 57 } 58 59 /// endFunction - Gather and emit post-function exception information. 60 /// 61 void ARMException::endFunction(const MachineFunction *MF) { 62 ARMTargetStreamer &ATS = getTargetStreamer(); 63 const Function &F = MF->getFunction(); 64 const Function *Per = nullptr; 65 if (F.hasPersonalityFn()) 66 Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); 67 bool forceEmitPersonality = 68 F.hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) && 69 F.needsUnwindTableEntry(); 70 bool shouldEmitPersonality = forceEmitPersonality || 71 !MF->getLandingPads().empty(); 72 if (!Asm->MF->getFunction().needsUnwindTableEntry() && 73 !shouldEmitPersonality) 74 ATS.emitCantUnwind(); 75 else if (shouldEmitPersonality) { 76 // Emit references to personality. 77 if (Per) { 78 MCSymbol *PerSym = Asm->getSymbol(Per); 79 Asm->OutStreamer->EmitSymbolAttribute(PerSym, MCSA_Global); 80 ATS.emitPersonality(PerSym); 81 } 82 83 // Emit .handlerdata directive. 84 ATS.emitHandlerData(); 85 86 // Emit actual exception table 87 emitExceptionTable(); 88 } 89 90 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM) 91 ATS.emitFnEnd(); 92 } 93 94 void ARMException::emitTypeInfos(unsigned TTypeEncoding) { 95 const MachineFunction *MF = Asm->MF; 96 const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 97 const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 98 99 bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 100 101 int Entry = 0; 102 // Emit the Catch TypeInfos. 103 if (VerboseAsm && !TypeInfos.empty()) { 104 Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); 105 Asm->OutStreamer->AddBlankLine(); 106 Entry = TypeInfos.size(); 107 } 108 109 for (const GlobalValue *GV : reverse(TypeInfos)) { 110 if (VerboseAsm) 111 Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); 112 Asm->EmitTTypeReference(GV, TTypeEncoding); 113 } 114 115 // Emit the Exception Specifications. 116 if (VerboseAsm && !FilterIds.empty()) { 117 Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); 118 Asm->OutStreamer->AddBlankLine(); 119 Entry = 0; 120 } 121 for (std::vector<unsigned>::const_iterator 122 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 123 unsigned TypeID = *I; 124 if (VerboseAsm) { 125 --Entry; 126 if (TypeID != 0) 127 Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); 128 } 129 130 Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]), 131 TTypeEncoding); 132 } 133 } 134