xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/WasmException.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains support for writing WebAssembly exception info into asm
100b57cec5SDimitry Andric // files.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "WasmException.h"
150b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric void WasmException::endModule() {
21*e8d8bef9SDimitry Andric   // This is the symbol used in 'throw' and 'catch' instruction to denote this
22*e8d8bef9SDimitry Andric   // is a C++ exception. This symbol has to be emitted somewhere once in the
23*e8d8bef9SDimitry Andric   // module.  Check if the symbol has already been created, i.e., we have at
24*e8d8bef9SDimitry Andric   // least one 'throw' or 'catch' instruction in the module, and emit the symbol
25*e8d8bef9SDimitry Andric   // only if so.
260b57cec5SDimitry Andric   SmallString<60> NameStr;
270b57cec5SDimitry Andric   Mangler::getNameWithPrefix(NameStr, "__cpp_exception", Asm->getDataLayout());
280b57cec5SDimitry Andric   if (Asm->OutContext.lookupSymbol(NameStr)) {
290b57cec5SDimitry Andric     MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol("__cpp_exception");
305ffd83dbSDimitry Andric     Asm->OutStreamer->emitLabel(ExceptionSym);
310b57cec5SDimitry Andric   }
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric void WasmException::markFunctionEnd() {
350b57cec5SDimitry Andric   // Get rid of any dead landing pads.
360b57cec5SDimitry Andric   if (!Asm->MF->getLandingPads().empty()) {
370b57cec5SDimitry Andric     auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
380b57cec5SDimitry Andric     // Wasm does not set BeginLabel and EndLabel information for landing pads,
390b57cec5SDimitry Andric     // so we should set the second argument false.
400b57cec5SDimitry Andric     NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric void WasmException::endFunction(const MachineFunction *MF) {
450b57cec5SDimitry Andric   bool ShouldEmitExceptionTable = false;
460b57cec5SDimitry Andric   for (const LandingPadInfo &Info : MF->getLandingPads()) {
470b57cec5SDimitry Andric     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
480b57cec5SDimitry Andric       ShouldEmitExceptionTable = true;
490b57cec5SDimitry Andric       break;
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric   if (!ShouldEmitExceptionTable)
530b57cec5SDimitry Andric     return;
540b57cec5SDimitry Andric   MCSymbol *LSDALabel = emitExceptionTable();
550b57cec5SDimitry Andric   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // Wasm requires every data section symbol to have a .size set. So we emit an
580b57cec5SDimitry Andric   // end marker and set the size as the difference between the start end the end
590b57cec5SDimitry Andric   // marker.
600b57cec5SDimitry Andric   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
615ffd83dbSDimitry Andric   Asm->OutStreamer->emitLabel(LSDAEndLabel);
620b57cec5SDimitry Andric   MCContext &OutContext = Asm->OutStreamer->getContext();
630b57cec5SDimitry Andric   const MCExpr *SizeExp = MCBinaryExpr::createSub(
640b57cec5SDimitry Andric       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
650b57cec5SDimitry Andric       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
660b57cec5SDimitry Andric   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric // Compute the call-site table for wasm EH. Even though we use the same function
700b57cec5SDimitry Andric // name to share the common routines, a call site entry in the table corresponds
710b57cec5SDimitry Andric // to not a call site for possibly-throwing functions but a landing pad. In wasm
720b57cec5SDimitry Andric // EH the VM is responsible for stack unwinding. After an exception occurs and
730b57cec5SDimitry Andric // the stack is unwound, the control flow is transferred to wasm 'catch'
740b57cec5SDimitry Andric // instruction by the VM, after which the personality function is called from
750b57cec5SDimitry Andric // the compiler-generated code. Refer to WasmEHPrepare pass for more
760b57cec5SDimitry Andric // information.
770b57cec5SDimitry Andric void WasmException::computeCallSiteTable(
780b57cec5SDimitry Andric     SmallVectorImpl<CallSiteEntry> &CallSites,
79*e8d8bef9SDimitry Andric     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
800b57cec5SDimitry Andric     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
810b57cec5SDimitry Andric     const SmallVectorImpl<unsigned> &FirstActions) {
820b57cec5SDimitry Andric   MachineFunction &MF = *Asm->MF;
830b57cec5SDimitry Andric   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
840b57cec5SDimitry Andric     const LandingPadInfo *Info = LandingPads[I];
850b57cec5SDimitry Andric     MachineBasicBlock *LPad = Info->LandingPadBlock;
860b57cec5SDimitry Andric     // We don't emit LSDA for single catch (...).
870b57cec5SDimitry Andric     if (!MF.hasWasmLandingPadIndex(LPad))
880b57cec5SDimitry Andric       continue;
890b57cec5SDimitry Andric     // Wasm EH must maintain the EH pads in the order assigned to them by the
900b57cec5SDimitry Andric     // WasmEHPrepare pass.
910b57cec5SDimitry Andric     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
920b57cec5SDimitry Andric     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
930b57cec5SDimitry Andric     if (CallSites.size() < LPadIndex + 1)
940b57cec5SDimitry Andric       CallSites.resize(LPadIndex + 1);
950b57cec5SDimitry Andric     CallSites[LPadIndex] = Site;
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric }
98