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