LLVM  14.0.0git
WasmException.cpp
Go to the documentation of this file.
1 //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing WebAssembly exception info into asm
10 // files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "WasmException.h"
15 #include "llvm/IR/Mangler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCStreamer.h"
18 using namespace llvm;
19 
21  // These are symbols used to throw/catch C++ exceptions and C longjmps. These
22  // symbols have to be emitted somewhere once in the module. Check if each of
23  // the symbols has already been created, i.e., we have at least one 'throw' or
24  // 'catch' instruction with the symbol in the module, and emit the symbol only
25  // if so.
26  //
27  // But in dynamic linking, it is in general not possible to come up with a
28  // module instantiating order in which tag-defining modules are loaded before
29  // the importing modules. So we make them undefined symbols here, define tags
30  // in the JS side, and feed them to each importing module.
31  if (!Asm->isPositionIndependent()) {
32  for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
33  SmallString<60> NameStr;
34  Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
35  if (Asm->OutContext.lookupSymbol(NameStr)) {
36  MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
37  Asm->OutStreamer->emitLabel(ExceptionSym);
38  }
39  }
40  }
41 }
42 
44  // Get rid of any dead landing pads.
45  if (!Asm->MF->getLandingPads().empty()) {
46  auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
47  // Wasm does not set BeginLabel and EndLabel information for landing pads,
48  // so we should set the second argument false.
49  NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
50  }
51 }
52 
54  bool ShouldEmitExceptionTable = false;
55  for (const LandingPadInfo &Info : MF->getLandingPads()) {
56  if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
57  ShouldEmitExceptionTable = true;
58  break;
59  }
60  }
61  if (!ShouldEmitExceptionTable)
62  return;
63  MCSymbol *LSDALabel = emitExceptionTable();
64  assert(LSDALabel && ".GCC_exception_table has not been emitted!");
65 
66  // Wasm requires every data section symbol to have a .size set. So we emit an
67  // end marker and set the size as the difference between the start end the end
68  // marker.
69  MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
70  Asm->OutStreamer->emitLabel(LSDAEndLabel);
71  MCContext &OutContext = Asm->OutStreamer->getContext();
72  const MCExpr *SizeExp = MCBinaryExpr::createSub(
73  MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
74  MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
75  Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
76 }
77 
78 // Compute the call-site table for wasm EH. Even though we use the same function
79 // name to share the common routines, a call site entry in the table corresponds
80 // to not a call site for possibly-throwing functions but a landing pad. In wasm
81 // EH the VM is responsible for stack unwinding. After an exception occurs and
82 // the stack is unwound, the control flow is transferred to wasm 'catch'
83 // instruction by the VM, after which the personality function is called from
84 // the compiler-generated code. Refer to WasmEHPrepare pass for more
85 // information.
88  SmallVectorImpl<CallSiteRange> &CallSiteRanges,
89  const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
90  const SmallVectorImpl<unsigned> &FirstActions) {
91  MachineFunction &MF = *Asm->MF;
92  for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
93  const LandingPadInfo *Info = LandingPads[I];
94  MachineBasicBlock *LPad = Info->LandingPadBlock;
95  // We don't emit LSDA for single catch (...).
96  if (!MF.hasWasmLandingPadIndex(LPad))
97  continue;
98  // Wasm EH must maintain the EH pads in the order assigned to them by the
99  // WasmEHPrepare pass.
100  unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
101  CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
102  if (CallSites.size() < LPadIndex + 1)
103  CallSites.resize(LPadIndex + 1);
104  CallSites[LPadIndex] = Site;
105  }
106 }
llvm::AsmPrinter::isPositionIndependent
bool isPositionIndependent() const
Definition: AsmPrinter.cpp:214
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::AsmPrinter::createTempSymbol
MCSymbol * createTempSymbol(const Twine &Name) const
Definition: AsmPrinter.cpp:3151
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::MCContext::lookupSymbol
MCSymbol * lookupSymbol(const Twine &Name) const
Get the symbol for Name, or null.
Definition: MCContext.cpp:344
llvm::WasmException::computeCallSiteTable
void computeCallSiteTable(SmallVectorImpl< CallSiteEntry > &CallSites, SmallVectorImpl< CallSiteRange > &CallSiteRanges, const SmallVectorImpl< const LandingPadInfo * > &LandingPads, const SmallVectorImpl< unsigned > &FirstActions) override
Compute the call-site table and the call-site ranges.
Definition: WasmException.cpp:86
WasmException.h
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
MCContext.h
llvm::EHStreamer::emitExceptionTable
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:376
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:609
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::MachineFunction::tidyLandingPads
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr, bool TidyIfNoBeginLabels=true)
Remap landing pad labels and remove any deleted landing pads.
Definition: MachineFunction.cpp:789
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
llvm::WasmException::endModule
void endModule() override
Emit all sections that should come after the content.
Definition: WasmException.cpp:20
I
#define I(x, y, z)
Definition: MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCBinaryExpr::createSub
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:610
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:100
llvm::MachineFunction::getLandingPads
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
Definition: MachineFunction.h:1079
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:92
llvm::WasmException::markFunctionEnd
virtual void markFunctionEnd() override
Definition: WasmException.cpp:43
llvm::MachineFunction
Definition: MachineFunction.h:241
Mangler.h
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3211
llvm::EHStreamer::CallSiteEntry
Structure describing an entry in the call-site table.
Definition: EHStreamer.h:61
llvm::EHStreamer::Asm
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
llvm::MachineFunction::hasWasmLandingPadIndex
bool hasWasmLandingPadIndex(const MachineBasicBlock *LPad) const
Returns true if the landing pad has an associate index in wasm EH.
Definition: MachineFunction.h:1125
N
#define N
MCStreamer.h
llvm::WasmException::endFunction
void endFunction(const MachineFunction *MF) override
Gather post-function debug information.
Definition: WasmException.cpp:53
llvm::AsmPrinter::getDataLayout
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:227
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::LandingPadInfo
This structure is used to retain landing pad info for the current function.
Definition: MachineFunction.h:229
llvm::MachineFunction::getWasmLandingPadIndex
unsigned getWasmLandingPadIndex(const MachineBasicBlock *LPad) const
Get the index in wasm EH for a given landing pad.
Definition: MachineFunction.h:1130
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35