1 //===-- CodeGen/AsmPrinter/ARMException.cpp - ARM EHABI Exception Impl ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing DWARF exception info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfException.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/Support/Dwarf.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Target/TargetFrameLowering.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
37 ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
39 ARMException::~ARMException() {}
41 ARMTargetStreamer &ARMException::getTargetStreamer() {
42 MCTargetStreamer &TS = *Asm->OutStreamer->getTargetStreamer();
43 return static_cast<ARMTargetStreamer &>(TS);
46 void ARMException::beginFunction(const MachineFunction *MF) {
47 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
48 getTargetStreamer().emitFnStart();
49 // See if we need call frame info.
50 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
51 assert(MoveType != AsmPrinter::CFI_M_EH &&
52 "non-EH CFI not yet supported in prologue with EHABI lowering");
54 if (MoveType == AsmPrinter::CFI_M_Debug) {
55 if (!hasEmittedCFISections) {
56 if (Asm->needsOnlyDebugCFIMoves())
57 Asm->OutStreamer->EmitCFISections(false, true);
58 hasEmittedCFISections = true;
62 Asm->OutStreamer->EmitCFIStartProc(false);
66 /// endFunction - Gather and emit post-function exception information.
68 void ARMException::endFunction(const MachineFunction *MF) {
69 ARMTargetStreamer &ATS = getTargetStreamer();
70 const Function *F = MF->getFunction();
71 const Function *Per = nullptr;
72 if (F->hasPersonalityFn())
73 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
74 bool forceEmitPersonality =
75 F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
76 F->needsUnwindTableEntry();
77 bool shouldEmitPersonality = forceEmitPersonality ||
78 !MF->getLandingPads().empty();
79 if (!Asm->MF->getFunction()->needsUnwindTableEntry() &&
80 !shouldEmitPersonality)
82 else if (shouldEmitPersonality) {
83 // Emit references to personality.
85 MCSymbol *PerSym = Asm->getSymbol(Per);
86 Asm->OutStreamer->EmitSymbolAttribute(PerSym, MCSA_Global);
87 ATS.emitPersonality(PerSym);
90 // Emit .handlerdata directive.
91 ATS.emitHandlerData();
93 // Emit actual exception table
97 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
101 void ARMException::emitTypeInfos(unsigned TTypeEncoding) {
102 const MachineFunction *MF = Asm->MF;
103 const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
104 const std::vector<unsigned> &FilterIds = MF->getFilterIds();
106 bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
109 // Emit the Catch TypeInfos.
110 if (VerboseAsm && !TypeInfos.empty()) {
111 Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
112 Asm->OutStreamer->AddBlankLine();
113 Entry = TypeInfos.size();
116 for (const GlobalValue *GV : reverse(TypeInfos)) {
118 Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
119 Asm->EmitTTypeReference(GV, TTypeEncoding);
122 // Emit the Exception Specifications.
123 if (VerboseAsm && !FilterIds.empty()) {
124 Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
125 Asm->OutStreamer->AddBlankLine();
128 for (std::vector<unsigned>::const_iterator
129 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
130 unsigned TypeID = *I;
134 Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
137 Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]),