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