blob: 6fe75ad82d26b095bbd8dffb8fcd04cfaa12f729 [file] [log] [blame]
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +00001//===-- 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 Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000018#include "llvm/CodeGen/AsmPrinter.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000025#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 Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Dwarf.h"
33#include "llvm/Support/FormattedStream.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000034#include "llvm/Target/TargetFrameLowering.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000035#include "llvm/Target/TargetOptions.h"
36#include "llvm/Target/TargetRegisterInfo.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000037using namespace llvm;
38
39ARMException::ARMException(AsmPrinter *A)
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000040 : EHStreamer(A), shouldEmitCFI(false) {}
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000041
42ARMException::~ARMException() {}
43
Rafael Espindolaa17151a2013-10-08 13:08:17 +000044ARMTargetStreamer &ARMException::getTargetStreamer() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +000045 MCTargetStreamer &TS = *Asm->OutStreamer.getTargetStreamer();
Rafael Espindolaa17151a2013-10-08 13:08:17 +000046 return static_cast<ARMTargetStreamer &>(TS);
47}
48
Artyom Skrobovf6830f42014-02-14 17:19:07 +000049/// endModule - Emit all exception information that should come after the
50/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000051void ARMException::endModule() {
Artyom Skrobovf6830f42014-02-14 17:19:07 +000052 if (shouldEmitCFI)
53 Asm->OutStreamer.EmitCFISections(false, true);
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000054}
55
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000056/// beginFunction - Gather pre-function exception information. Assumes it's
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000057/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000058void ARMException::beginFunction(const MachineFunction *MF) {
Joerg Sonnenberger3c108172014-04-30 22:43:13 +000059 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
60 getTargetStreamer().emitFnStart();
Logan Chien95188b92014-05-14 16:38:30 +000061 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
62 Asm->getFunctionNumber()));
Artyom Skrobovf6830f42014-02-14 17:19:07 +000063 // 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;
Oliver Stannardcf6bfb12014-11-03 12:19:03 +000069 Asm->OutStreamer.EmitCFIStartProc(false);
Artyom Skrobovf6830f42014-02-14 17:19:07 +000070 }
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000071}
72
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000073/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000074///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +000075void ARMException::endFunction(const MachineFunction *) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +000076 if (shouldEmitCFI)
77 Asm->OutStreamer.EmitCFIEndProc();
78
Logan Chien95188b92014-05-14 16:38:30 +000079 // Map all labels and get rid of any dead landing pads.
80 MMI->TidyLandingPads();
81
Rafael Espindolaa17151a2013-10-08 13:08:17 +000082 ARMTargetStreamer &ATS = getTargetStreamer();
Logan Chien95188b92014-05-14 16:38:30 +000083 if (!Asm->MF->getFunction()->needsUnwindTableEntry() &&
84 MMI->getLandingPads().empty())
Rafael Espindolaa17151a2013-10-08 13:08:17 +000085 ATS.emitCantUnwind();
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000086 else {
87 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
88 Asm->getFunctionNumber()));
Renato Golin8cea6e82014-01-29 11:50:56 +000089 if (!MMI->getLandingPads().empty()) {
90 // Emit references to personality.
Reid Klecknere80a0a72015-01-14 22:47:54 +000091 if (const Function *Personality = MMI->getPersonality()) {
Renato Golin8cea6e82014-01-29 11:50:56 +000092 MCSymbol *PerSym = Asm->getSymbol(Personality);
93 Asm->OutStreamer.EmitSymbolAttribute(PerSym, MCSA_Global);
94 ATS.emitPersonality(PerSym);
Anton Korobeynikovb619a412012-11-14 19:13:30 +000095 }
Renato Golin8cea6e82014-01-29 11:50:56 +000096
97 // Emit .handlerdata directive.
98 ATS.emitHandlerData();
99
100 // Emit actual exception table
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000101 emitExceptionTable();
Evgeniy Stepanov33a7e2f2012-01-24 13:05:33 +0000102 }
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +0000103 }
104
Joerg Sonnenberger3c108172014-04-30 22:43:13 +0000105 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
106 ATS.emitFnEnd();
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +0000107}
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000108
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000109void ARMException::emitTypeInfos(unsigned TTypeEncoding) {
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000110 const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos();
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000111 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
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000123 for (std::vector<const GlobalValue *>::const_reverse_iterator
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000124 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000125 const GlobalValue *GV = *I;
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000126 if (VerboseAsm)
127 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000128 Asm->EmitTTypeReference(GV, TTypeEncoding);
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000129 }
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
Craig Topper353eda42014-04-24 06:44:33 +0000146 Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]),
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000147 TTypeEncoding);
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000148 }
149}