124faf859SHeejin Ahn //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
224faf859SHeejin Ahn //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
624faf859SHeejin Ahn //
724faf859SHeejin Ahn //===----------------------------------------------------------------------===//
824faf859SHeejin Ahn //
924faf859SHeejin Ahn // This file contains support for writing WebAssembly exception info into asm
1024faf859SHeejin Ahn // files.
1124faf859SHeejin Ahn //
1224faf859SHeejin Ahn //===----------------------------------------------------------------------===//
1324faf859SHeejin Ahn 
1424faf859SHeejin Ahn #include "WasmException.h"
15*ed98c1b3Sserge-sans-paille #include "llvm/CodeGen/AsmPrinter.h"
16*ed98c1b3Sserge-sans-paille #include "llvm/CodeGen/MachineFunction.h"
17da419bdbSHeejin Ahn #include "llvm/IR/Mangler.h"
18da419bdbSHeejin Ahn #include "llvm/MC/MCContext.h"
1924faf859SHeejin Ahn #include "llvm/MC/MCStreamer.h"
2024faf859SHeejin Ahn using namespace llvm;
2124faf859SHeejin Ahn 
endModule()22da419bdbSHeejin Ahn void WasmException::endModule() {
2328780e59SHeejin Ahn   // These are symbols used to throw/catch C++ exceptions and C longjmps. These
2428780e59SHeejin Ahn   // symbols have to be emitted somewhere once in the module. Check if each of
2528780e59SHeejin Ahn   // the symbols has already been created, i.e., we have at least one 'throw' or
2628780e59SHeejin Ahn   // 'catch' instruction with the symbol in the module, and emit the symbol only
2728780e59SHeejin Ahn   // if so.
289261ee32SHeejin Ahn   //
299261ee32SHeejin Ahn   // But in dynamic linking, it is in general not possible to come up with a
309261ee32SHeejin Ahn   // module instantiating order in which tag-defining modules are loaded before
319261ee32SHeejin Ahn   // the importing modules. So we make them undefined symbols here, define tags
329261ee32SHeejin Ahn   // in the JS side, and feed them to each importing module.
339261ee32SHeejin Ahn   if (!Asm->isPositionIndependent()) {
3428780e59SHeejin Ahn     for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
35da419bdbSHeejin Ahn       SmallString<60> NameStr;
3628780e59SHeejin Ahn       Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
37da419bdbSHeejin Ahn       if (Asm->OutContext.lookupSymbol(NameStr)) {
3828780e59SHeejin Ahn         MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
396d2d589bSFangrui Song         Asm->OutStreamer->emitLabel(ExceptionSym);
40da419bdbSHeejin Ahn       }
41da419bdbSHeejin Ahn     }
4228780e59SHeejin Ahn   }
439261ee32SHeejin Ahn }
44da419bdbSHeejin Ahn 
markFunctionEnd()4524faf859SHeejin Ahn void WasmException::markFunctionEnd() {
4624faf859SHeejin Ahn   // Get rid of any dead landing pads.
4724faf859SHeejin Ahn   if (!Asm->MF->getLandingPads().empty()) {
4824faf859SHeejin Ahn     auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
4924faf859SHeejin Ahn     // Wasm does not set BeginLabel and EndLabel information for landing pads,
5024faf859SHeejin Ahn     // so we should set the second argument false.
5124faf859SHeejin Ahn     NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
5224faf859SHeejin Ahn   }
5324faf859SHeejin Ahn }
5424faf859SHeejin Ahn 
endFunction(const MachineFunction * MF)5524faf859SHeejin Ahn void WasmException::endFunction(const MachineFunction *MF) {
5624faf859SHeejin Ahn   bool ShouldEmitExceptionTable = false;
5724faf859SHeejin Ahn   for (const LandingPadInfo &Info : MF->getLandingPads()) {
5824faf859SHeejin Ahn     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
5924faf859SHeejin Ahn       ShouldEmitExceptionTable = true;
6024faf859SHeejin Ahn       break;
6124faf859SHeejin Ahn     }
6224faf859SHeejin Ahn   }
6324faf859SHeejin Ahn   if (!ShouldEmitExceptionTable)
6424faf859SHeejin Ahn     return;
6524faf859SHeejin Ahn   MCSymbol *LSDALabel = emitExceptionTable();
6624faf859SHeejin Ahn   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
6724faf859SHeejin Ahn 
6824faf859SHeejin Ahn   // Wasm requires every data section symbol to have a .size set. So we emit an
6924faf859SHeejin Ahn   // end marker and set the size as the difference between the start end the end
7024faf859SHeejin Ahn   // marker.
7124faf859SHeejin Ahn   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
726d2d589bSFangrui Song   Asm->OutStreamer->emitLabel(LSDAEndLabel);
7324faf859SHeejin Ahn   MCContext &OutContext = Asm->OutStreamer->getContext();
7424faf859SHeejin Ahn   const MCExpr *SizeExp = MCBinaryExpr::createSub(
7524faf859SHeejin Ahn       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
7624faf859SHeejin Ahn       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
7724faf859SHeejin Ahn   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
7824faf859SHeejin Ahn }
7924faf859SHeejin Ahn 
8024faf859SHeejin Ahn // Compute the call-site table for wasm EH. Even though we use the same function
8124faf859SHeejin Ahn // name to share the common routines, a call site entry in the table corresponds
8224faf859SHeejin Ahn // to not a call site for possibly-throwing functions but a landing pad. In wasm
8324faf859SHeejin Ahn // EH the VM is responsible for stack unwinding. After an exception occurs and
8424faf859SHeejin Ahn // the stack is unwound, the control flow is transferred to wasm 'catch'
8524faf859SHeejin Ahn // instruction by the VM, after which the personality function is called from
8624faf859SHeejin Ahn // the compiler-generated code. Refer to WasmEHPrepare pass for more
8724faf859SHeejin Ahn // information.
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> & CallSites,SmallVectorImpl<CallSiteRange> & CallSiteRanges,const SmallVectorImpl<const LandingPadInfo * > & LandingPads,const SmallVectorImpl<unsigned> & FirstActions)8824faf859SHeejin Ahn void WasmException::computeCallSiteTable(
8924faf859SHeejin Ahn     SmallVectorImpl<CallSiteEntry> &CallSites,
908955950cSRahman Lavaee     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
9124faf859SHeejin Ahn     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
9224faf859SHeejin Ahn     const SmallVectorImpl<unsigned> &FirstActions) {
9324faf859SHeejin Ahn   MachineFunction &MF = *Asm->MF;
9424faf859SHeejin Ahn   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
9524faf859SHeejin Ahn     const LandingPadInfo *Info = LandingPads[I];
9624faf859SHeejin Ahn     MachineBasicBlock *LPad = Info->LandingPadBlock;
9724faf859SHeejin Ahn     // We don't emit LSDA for single catch (...).
9824faf859SHeejin Ahn     if (!MF.hasWasmLandingPadIndex(LPad))
9924faf859SHeejin Ahn       continue;
10024faf859SHeejin Ahn     // Wasm EH must maintain the EH pads in the order assigned to them by the
10124faf859SHeejin Ahn     // WasmEHPrepare pass.
10224faf859SHeejin Ahn     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
10324faf859SHeejin Ahn     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
10424faf859SHeejin Ahn     if (CallSites.size() < LPadIndex + 1)
10524faf859SHeejin Ahn       CallSites.resize(LPadIndex + 1);
10624faf859SHeejin Ahn     CallSites[LPadIndex] = Site;
10724faf859SHeejin Ahn   }
10824faf859SHeejin Ahn }
109