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 
endModule()20 void WasmException::endModule() {
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   for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
27     SmallString<60> NameStr;
28     Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
29     if (Asm->OutContext.lookupSymbol(NameStr)) {
30       MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
31       Asm->OutStreamer->emitLabel(ExceptionSym);
32     }
33   }
34 }
35 
markFunctionEnd()36 void WasmException::markFunctionEnd() {
37   // Get rid of any dead landing pads.
38   if (!Asm->MF->getLandingPads().empty()) {
39     auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
40     // Wasm does not set BeginLabel and EndLabel information for landing pads,
41     // so we should set the second argument false.
42     NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
43   }
44 }
45 
endFunction(const MachineFunction * MF)46 void WasmException::endFunction(const MachineFunction *MF) {
47   bool ShouldEmitExceptionTable = false;
48   for (const LandingPadInfo &Info : MF->getLandingPads()) {
49     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
50       ShouldEmitExceptionTable = true;
51       break;
52     }
53   }
54   if (!ShouldEmitExceptionTable)
55     return;
56   MCSymbol *LSDALabel = emitExceptionTable();
57   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
58 
59   // Wasm requires every data section symbol to have a .size set. So we emit an
60   // end marker and set the size as the difference between the start end the end
61   // marker.
62   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
63   Asm->OutStreamer->emitLabel(LSDAEndLabel);
64   MCContext &OutContext = Asm->OutStreamer->getContext();
65   const MCExpr *SizeExp = MCBinaryExpr::createSub(
66       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
67       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
68   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
69 }
70 
71 // Compute the call-site table for wasm EH. Even though we use the same function
72 // name to share the common routines, a call site entry in the table corresponds
73 // to not a call site for possibly-throwing functions but a landing pad. In wasm
74 // EH the VM is responsible for stack unwinding. After an exception occurs and
75 // the stack is unwound, the control flow is transferred to wasm 'catch'
76 // instruction by the VM, after which the personality function is called from
77 // the compiler-generated code. Refer to WasmEHPrepare pass for more
78 // information.
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> & CallSites,SmallVectorImpl<CallSiteRange> & CallSiteRanges,const SmallVectorImpl<const LandingPadInfo * > & LandingPads,const SmallVectorImpl<unsigned> & FirstActions)79 void WasmException::computeCallSiteTable(
80     SmallVectorImpl<CallSiteEntry> &CallSites,
81     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
82     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
83     const SmallVectorImpl<unsigned> &FirstActions) {
84   MachineFunction &MF = *Asm->MF;
85   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
86     const LandingPadInfo *Info = LandingPads[I];
87     MachineBasicBlock *LPad = Info->LandingPadBlock;
88     // We don't emit LSDA for single catch (...).
89     if (!MF.hasWasmLandingPadIndex(LPad))
90       continue;
91     // Wasm EH must maintain the EH pads in the order assigned to them by the
92     // WasmEHPrepare pass.
93     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
94     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
95     if (CallSites.size() < LPadIndex + 1)
96       CallSites.resize(LPadIndex + 1);
97     CallSites[LPadIndex] = Site;
98   }
99 }
100