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"
37using namespace llvm;
38
39ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
40
41ARMException::~ARMException() {}
42
43ARMTargetStreamer &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.
50void ARMException::endModule() {
51  if (shouldEmitCFI)
52    Asm->OutStreamer->EmitCFISections(false, true);
53}
54
55void 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///
70void 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  bool forceEmitPersonality =
77    F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
78    F->needsUnwindTableEntry();
79  bool shouldEmitPersonality = forceEmitPersonality ||
80    !MMI->getLandingPads().empty();
81  if (!Asm->MF->getFunction()->needsUnwindTableEntry() &&
82      !shouldEmitPersonality)
83    ATS.emitCantUnwind();
84  else if (shouldEmitPersonality) {
85    // Emit references to personality.
86    if (Per) {
87      MCSymbol *PerSym = Asm->getSymbol(Per);
88      Asm->OutStreamer->EmitSymbolAttribute(PerSym, MCSA_Global);
89      ATS.emitPersonality(PerSym);
90    }
91
92    // Emit .handlerdata directive.
93    ATS.emitHandlerData();
94
95    // Emit actual exception table
96    emitExceptionTable();
97  }
98
99  if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
100    ATS.emitFnEnd();
101}
102
103void ARMException::emitTypeInfos(unsigned TTypeEncoding) {
104  const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos();
105  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
106
107  bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
108
109  int Entry = 0;
110  // Emit the Catch TypeInfos.
111  if (VerboseAsm && !TypeInfos.empty()) {
112    Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
113    Asm->OutStreamer->AddBlankLine();
114    Entry = TypeInfos.size();
115  }
116
117  for (const GlobalValue *GV : reverse(TypeInfos)) {
118    if (VerboseAsm)
119      Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
120    Asm->EmitTTypeReference(GV, TTypeEncoding);
121  }
122
123  // Emit the Exception Specifications.
124  if (VerboseAsm && !FilterIds.empty()) {
125    Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
126    Asm->OutStreamer->AddBlankLine();
127    Entry = 0;
128  }
129  for (std::vector<unsigned>::const_iterator
130         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
131    unsigned TypeID = *I;
132    if (VerboseAsm) {
133      --Entry;
134      if (TypeID != 0)
135        Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
136    }
137
138    Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]),
139                            TTypeEncoding);
140  }
141}
142