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/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/IR/Mangler.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCStreamer.h"
20using namespace llvm;
21
22void WasmException::endFunction(const MachineFunction *MF) {
23 bool ShouldEmitExceptionTable = false;
24 for (const LandingPadInfo &Info : MF->getLandingPads()) {
25 if (MF->hasWasmLandingPadIndex(LPad: 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(Name: "GCC_except_table_end");
39 Asm->OutStreamer->emitLabel(Symbol: LSDAEndLabel);
40 MCContext &OutContext = Asm->OutStreamer->getContext();
41 const MCExpr *SizeExp = MCBinaryExpr::createSub(
42 LHS: MCSymbolRefExpr::create(Symbol: LSDAEndLabel, Ctx&: OutContext),
43 RHS: MCSymbolRefExpr::create(Symbol: LSDALabel, Ctx&: OutContext), Ctx&: OutContext);
44 Asm->OutStreamer->emitELFSize(Symbol: LSDALabel, Value: 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.
55void WasmException::computeCallSiteTable(
56 SmallVectorImpl<CallSiteEntry> &CallSites,
57 SmallVectorImpl<CallSiteRange> &CallSiteRanges,
58 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
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 = {.BeginLabel: nullptr, .EndLabel: nullptr, .LPad: Info, .Action: FirstActions[I]};
71 if (CallSites.size() < LPadIndex + 1)
72 CallSites.resize(N: LPadIndex + 1);
73 CallSites[LPadIndex] = Site;
74 }
75}
76