LLVM 20.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"
17#include "llvm/IR/Mangler.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCStreamer.h"
20using namespace llvm;
21
23 // These are symbols used to throw/catch C++ exceptions and C longjmps. These
24 // symbols have to be emitted somewhere once in the module. Check if each of
25 // the symbols has already been created, i.e., we have at least one 'throw' or
26 // 'catch' instruction with the symbol in the module, and emit the symbol only
27 // if so.
28 //
29 // But in dynamic linking, it is in general not possible to come up with a
30 // module instantiating order in which tag-defining modules are loaded before
31 // the importing modules. So we make them undefined symbols here, define tags
32 // in the JS side, and feed them to each importing module.
33 if (!Asm->isPositionIndependent()) {
34 for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
35 SmallString<60> NameStr;
36 Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
37 if (Asm->OutContext.lookupSymbol(NameStr)) {
38 MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
39 Asm->OutStreamer->emitLabel(ExceptionSym);
40 }
41 }
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 SmallVectorImpl<CallSiteRange> &CallSiteRanges,
82 const SmallVectorImpl<unsigned> &FirstActions) {
83 MachineFunction &MF = *Asm->MF;
84 for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
85 const LandingPadInfo *Info = LandingPads[I];
86 MachineBasicBlock *LPad = Info->LandingPadBlock;
87 // We don't emit LSDA for single catch (...).
88 if (!MF.hasWasmLandingPadIndex(LPad))
89 continue;
90 // Wasm EH must maintain the EH pads in the order assigned to them by the
91 // WasmEHPrepare pass.
92 unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
93 CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
94 if (CallSites.size() < LPadIndex + 1)
95 CallSites.resize(LPadIndex + 1);
96 CallSites[LPadIndex] = Site;
97 }
98}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:104
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
MCSymbol * createTempSymbol(const Twine &Name) const
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
bool isPositionIndependent() const
Definition: AsmPrinter.cpp:399
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:412
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:380
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:622
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * lookupSymbol(const Twine &Name) const
Get the symbol for Name, or null.
Definition: MCContext.cpp:412
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
unsigned getWasmLandingPadIndex(const MachineBasicBlock *LPad) const
Get the index in wasm EH for a given landing pad.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
bool hasWasmLandingPadIndex(const MachineBasicBlock *LPad) const
Returns true if the landing pad has an associate index in wasm EH.
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:121
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void resize(size_type N)
Definition: SmallVector.h:638
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.
void endModule() override
Emit all sections that should come after the content.
void endFunction(const MachineFunction *MF) override
Gather post-function debug information.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
#define N
Structure describing an entry in the call-site table.
Definition: EHStreamer.h:61
This structure is used to retain landing pad info for the current function.