xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/CodeGen/AsmPrinter/WasmException.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file contains support for writing WebAssembly exception info into asm
107330f729Sjoerg // files.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #include "WasmException.h"
157330f729Sjoerg #include "llvm/IR/Mangler.h"
167330f729Sjoerg #include "llvm/MC/MCContext.h"
177330f729Sjoerg #include "llvm/MC/MCStreamer.h"
187330f729Sjoerg using namespace llvm;
197330f729Sjoerg 
endModule()207330f729Sjoerg void WasmException::endModule() {
21*82d56013Sjoerg   // This is the symbol used in 'throw' and 'catch' instruction to denote this
22*82d56013Sjoerg   // is a C++ exception. This symbol has to be emitted somewhere once in the
23*82d56013Sjoerg   // module.  Check if the symbol has already been created, i.e., we have at
24*82d56013Sjoerg   // least one 'throw' or 'catch' instruction in the module, and emit the symbol
25*82d56013Sjoerg   // only if so.
267330f729Sjoerg   SmallString<60> NameStr;
277330f729Sjoerg   Mangler::getNameWithPrefix(NameStr, "__cpp_exception", Asm->getDataLayout());
287330f729Sjoerg   if (Asm->OutContext.lookupSymbol(NameStr)) {
297330f729Sjoerg     MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol("__cpp_exception");
30*82d56013Sjoerg     Asm->OutStreamer->emitLabel(ExceptionSym);
317330f729Sjoerg   }
327330f729Sjoerg }
337330f729Sjoerg 
markFunctionEnd()347330f729Sjoerg void WasmException::markFunctionEnd() {
357330f729Sjoerg   // Get rid of any dead landing pads.
367330f729Sjoerg   if (!Asm->MF->getLandingPads().empty()) {
377330f729Sjoerg     auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
387330f729Sjoerg     // Wasm does not set BeginLabel and EndLabel information for landing pads,
397330f729Sjoerg     // so we should set the second argument false.
407330f729Sjoerg     NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
417330f729Sjoerg   }
427330f729Sjoerg }
437330f729Sjoerg 
endFunction(const MachineFunction * MF)447330f729Sjoerg void WasmException::endFunction(const MachineFunction *MF) {
457330f729Sjoerg   bool ShouldEmitExceptionTable = false;
467330f729Sjoerg   for (const LandingPadInfo &Info : MF->getLandingPads()) {
477330f729Sjoerg     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
487330f729Sjoerg       ShouldEmitExceptionTable = true;
497330f729Sjoerg       break;
507330f729Sjoerg     }
517330f729Sjoerg   }
527330f729Sjoerg   if (!ShouldEmitExceptionTable)
537330f729Sjoerg     return;
547330f729Sjoerg   MCSymbol *LSDALabel = emitExceptionTable();
557330f729Sjoerg   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
567330f729Sjoerg 
577330f729Sjoerg   // Wasm requires every data section symbol to have a .size set. So we emit an
587330f729Sjoerg   // end marker and set the size as the difference between the start end the end
597330f729Sjoerg   // marker.
607330f729Sjoerg   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
61*82d56013Sjoerg   Asm->OutStreamer->emitLabel(LSDAEndLabel);
627330f729Sjoerg   MCContext &OutContext = Asm->OutStreamer->getContext();
637330f729Sjoerg   const MCExpr *SizeExp = MCBinaryExpr::createSub(
647330f729Sjoerg       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
657330f729Sjoerg       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
667330f729Sjoerg   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
677330f729Sjoerg }
687330f729Sjoerg 
697330f729Sjoerg // Compute the call-site table for wasm EH. Even though we use the same function
707330f729Sjoerg // name to share the common routines, a call site entry in the table corresponds
717330f729Sjoerg // to not a call site for possibly-throwing functions but a landing pad. In wasm
727330f729Sjoerg // EH the VM is responsible for stack unwinding. After an exception occurs and
737330f729Sjoerg // the stack is unwound, the control flow is transferred to wasm 'catch'
747330f729Sjoerg // instruction by the VM, after which the personality function is called from
757330f729Sjoerg // the compiler-generated code. Refer to WasmEHPrepare pass for more
767330f729Sjoerg // information.
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> & CallSites,SmallVectorImpl<CallSiteRange> & CallSiteRanges,const SmallVectorImpl<const LandingPadInfo * > & LandingPads,const SmallVectorImpl<unsigned> & FirstActions)777330f729Sjoerg void WasmException::computeCallSiteTable(
787330f729Sjoerg     SmallVectorImpl<CallSiteEntry> &CallSites,
79*82d56013Sjoerg     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
807330f729Sjoerg     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
817330f729Sjoerg     const SmallVectorImpl<unsigned> &FirstActions) {
827330f729Sjoerg   MachineFunction &MF = *Asm->MF;
837330f729Sjoerg   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
847330f729Sjoerg     const LandingPadInfo *Info = LandingPads[I];
857330f729Sjoerg     MachineBasicBlock *LPad = Info->LandingPadBlock;
867330f729Sjoerg     // We don't emit LSDA for single catch (...).
877330f729Sjoerg     if (!MF.hasWasmLandingPadIndex(LPad))
887330f729Sjoerg       continue;
897330f729Sjoerg     // Wasm EH must maintain the EH pads in the order assigned to them by the
907330f729Sjoerg     // WasmEHPrepare pass.
917330f729Sjoerg     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
927330f729Sjoerg     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
937330f729Sjoerg     if (CallSites.size() < LPadIndex + 1)
947330f729Sjoerg       CallSites.resize(LPadIndex + 1);
957330f729Sjoerg     CallSites[LPadIndex] = Site;
967330f729Sjoerg   }
977330f729Sjoerg }
98