blob: f6ef85a5b78f1ad9f19549ee318a1d26362f0920 [file] [log] [blame]
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +00001//===-- CodeGen/AsmPrinter/ARMException.cpp - ARM EHABI Exception Impl ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing DWARF exception info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/Twine.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000015#include "llvm/CodeGen/AsmPrinter.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000016#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000018#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000020#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCSection.h"
23#include "llvm/MC/MCStreamer.h"
24#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/FormattedStream.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000026#include "llvm/Target/TargetOptions.h"
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000027using namespace llvm;
28
Rafael Espindolaa6001792015-03-09 18:29:12 +000029ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000030
31ARMException::~ARMException() {}
32
Rafael Espindolaa17151a2013-10-08 13:08:17 +000033ARMTargetStreamer &ARMException::getTargetStreamer() {
Lang Hames9ff69c82015-04-24 19:11:51 +000034 MCTargetStreamer &TS = *Asm->OutStreamer->getTargetStreamer();
Rafael Espindolaa17151a2013-10-08 13:08:17 +000035 return static_cast<ARMTargetStreamer &>(TS);
36}
37
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000038void ARMException::beginFunction(const MachineFunction *MF) {
Joerg Sonnenberger3c108172014-04-30 22:43:13 +000039 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
40 getTargetStreamer().emitFnStart();
Artyom Skrobovf6830f42014-02-14 17:19:07 +000041 // See if we need call frame info.
42 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
43 assert(MoveType != AsmPrinter::CFI_M_EH &&
44 "non-EH CFI not yet supported in prologue with EHABI lowering");
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000045
Artyom Skrobovf6830f42014-02-14 17:19:07 +000046 if (MoveType == AsmPrinter::CFI_M_Debug) {
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000047 if (!hasEmittedCFISections) {
Joerg Sonnenberger83963992017-01-05 20:55:28 +000048 if (Asm->needsOnlyDebugCFIMoves())
49 Asm->OutStreamer->EmitCFISections(false, true);
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000050 hasEmittedCFISections = true;
51 }
52
Artyom Skrobovf6830f42014-02-14 17:19:07 +000053 shouldEmitCFI = true;
Lang Hames9ff69c82015-04-24 19:11:51 +000054 Asm->OutStreamer->EmitCFIStartProc(false);
Artyom Skrobovf6830f42014-02-14 17:19:07 +000055 }
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000056}
57
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000058/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000059///
Rafael Espindolaa6001792015-03-09 18:29:12 +000060void ARMException::endFunction(const MachineFunction *MF) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +000061 ARMTargetStreamer &ATS = getTargetStreamer();
Matthias Braunf1caa282017-12-15 22:22:58 +000062 const Function &F = MF->getFunction();
Keno Fischeraff703a2015-07-14 19:22:51 +000063 const Function *Per = nullptr;
Matthias Braunf1caa282017-12-15 22:22:58 +000064 if (F.hasPersonalityFn())
65 Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Keno Fischeraff703a2015-07-14 19:22:51 +000066 bool forceEmitPersonality =
Matthias Braunf1caa282017-12-15 22:22:58 +000067 F.hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
68 F.needsUnwindTableEntry();
Keno Fischeraff703a2015-07-14 19:22:51 +000069 bool shouldEmitPersonality = forceEmitPersonality ||
Matthias Braund0ee66c2016-12-01 19:32:15 +000070 !MF->getLandingPads().empty();
Matthias Braunf1caa282017-12-15 22:22:58 +000071 if (!Asm->MF->getFunction().needsUnwindTableEntry() &&
Keno Fischeraff703a2015-07-14 19:22:51 +000072 !shouldEmitPersonality)
Rafael Espindolaa17151a2013-10-08 13:08:17 +000073 ATS.emitCantUnwind();
Keno Fischeraff703a2015-07-14 19:22:51 +000074 else if (shouldEmitPersonality) {
75 // Emit references to personality.
76 if (Per) {
77 MCSymbol *PerSym = Asm->getSymbol(Per);
78 Asm->OutStreamer->EmitSymbolAttribute(PerSym, MCSA_Global);
79 ATS.emitPersonality(PerSym);
Evgeniy Stepanov33a7e2f2012-01-24 13:05:33 +000080 }
Keno Fischeraff703a2015-07-14 19:22:51 +000081
82 // Emit .handlerdata directive.
83 ATS.emitHandlerData();
84
85 // Emit actual exception table
86 emitExceptionTable();
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000087 }
88
Joerg Sonnenberger3c108172014-04-30 22:43:13 +000089 if (Asm->MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
90 ATS.emitFnEnd();
Anton Korobeynikova7ec2dc2011-03-05 18:43:15 +000091}
Anton Korobeynikovf65a6382012-11-19 21:06:26 +000092
Rafael Espindolad09b4162018-02-09 17:00:25 +000093void ARMException::emitTypeInfos(unsigned TTypeEncoding,
94 MCSymbol *TTBaseLabel) {
Matthias Braund0ee66c2016-12-01 19:32:15 +000095 const MachineFunction *MF = Asm->MF;
96 const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
97 const std::vector<unsigned> &FilterIds = MF->getFilterIds();
Anton Korobeynikovf65a6382012-11-19 21:06:26 +000098
Lang Hames9ff69c82015-04-24 19:11:51 +000099 bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000100
101 int Entry = 0;
102 // Emit the Catch TypeInfos.
103 if (VerboseAsm && !TypeInfos.empty()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000104 Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
105 Asm->OutStreamer->AddBlankLine();
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000106 Entry = TypeInfos.size();
107 }
108
Pete Cooperf3159f32015-07-29 22:19:09 +0000109 for (const GlobalValue *GV : reverse(TypeInfos)) {
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000110 if (VerboseAsm)
Lang Hames9ff69c82015-04-24 19:11:51 +0000111 Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000112 Asm->EmitTTypeReference(GV, TTypeEncoding);
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000113 }
114
Rafael Espindolad09b4162018-02-09 17:00:25 +0000115 Asm->OutStreamer->EmitLabel(TTBaseLabel);
116
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000117 // Emit the Exception Specifications.
118 if (VerboseAsm && !FilterIds.empty()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000119 Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
120 Asm->OutStreamer->AddBlankLine();
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000121 Entry = 0;
122 }
123 for (std::vector<unsigned>::const_iterator
124 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
125 unsigned TypeID = *I;
126 if (VerboseAsm) {
127 --Entry;
128 if (TypeID != 0)
Lang Hames9ff69c82015-04-24 19:11:51 +0000129 Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000130 }
131
Craig Topper353eda42014-04-24 06:44:33 +0000132 Asm->EmitTTypeReference((TypeID == 0 ? nullptr : TypeInfos[TypeID - 1]),
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000133 TTypeEncoding);
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000134 }
135}