LLVM 22.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 bool ShouldEmitExceptionTable = false;
24 for (const LandingPadInfo &Info : MF->getLandingPads()) {
25 if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
26 ShouldEmitExceptionTable = true;
27 break;
28 }
29 }
30 if (!ShouldEmitExceptionTable)
31 return;
32 MCSymbol *LSDALabel = emitExceptionTable();
33 assert(LSDALabel && ".GCC_exception_table has not been emitted!");
34
35 // Wasm requires every data section symbol to have a .size set. So we emit an
36 // end marker and set the size as the difference between the start end the end
37 // marker.
38 MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
39 Asm->OutStreamer->emitLabel(LSDAEndLabel);
40 MCContext &OutContext = Asm->OutStreamer->getContext();
41 const MCExpr *SizeExp = MCBinaryExpr::createSub(
42 MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
43 MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
44 Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
45}
46
47// Compute the call-site table for wasm EH. Even though we use the same function
48// name to share the common routines, a call site entry in the table corresponds
49// to not a call site for possibly-throwing functions but a landing pad. In wasm
50// EH the VM is responsible for stack unwinding. After an exception occurs and
51// the stack is unwound, the control flow is transferred to wasm 'catch'
52// instruction by the VM, after which the personality function is called from
53// the compiler-generated code. Refer to WasmEHPrepare pass for more
54// information.
57 SmallVectorImpl<CallSiteRange> &CallSiteRanges,
59 const SmallVectorImpl<unsigned> &FirstActions) {
60 MachineFunction &MF = *Asm->MF;
61 for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
62 const LandingPadInfo *Info = LandingPads[I];
63 MachineBasicBlock *LPad = Info->LandingPadBlock;
64 // We don't emit LSDA for single catch (...).
65 if (!MF.hasWasmLandingPadIndex(LPad))
66 continue;
67 // Wasm EH must maintain the EH pads in the order assigned to them by the
68 // WasmEHPrepare pass.
69 unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
70 CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
71 if (CallSites.size() < LPadIndex + 1)
72 CallSites.resize(LPadIndex + 1);
73 CallSites[LPadIndex] = Site;
74 }
75}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:58
AsmPrinter * Asm
Target of directive emission.
Definition EHStreamer.h:33
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:428
Context object for machine code objects.
Definition MCContext.h:83
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, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
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.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize(size_type N)
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 endFunction(const MachineFunction *MF) override
Gather post-function debug information.
This is an optimization pass for GlobalISel generic memory operations.
#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.