xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/WasmException.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
15*81ad6265SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
16*81ad6265SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
170b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
endModule()220b57cec5SDimitry Andric void WasmException::endModule() {
23349cc55cSDimitry Andric   // These are symbols used to throw/catch C++ exceptions and C longjmps. These
24349cc55cSDimitry Andric   // symbols have to be emitted somewhere once in the module. Check if each of
25349cc55cSDimitry Andric   // the symbols has already been created, i.e., we have at least one 'throw' or
26349cc55cSDimitry Andric   // 'catch' instruction with the symbol in the module, and emit the symbol only
27349cc55cSDimitry Andric   // if so.
28349cc55cSDimitry Andric   //
29349cc55cSDimitry Andric   // But in dynamic linking, it is in general not possible to come up with a
30349cc55cSDimitry Andric   // module instantiating order in which tag-defining modules are loaded before
31349cc55cSDimitry Andric   // the importing modules. So we make them undefined symbols here, define tags
32349cc55cSDimitry Andric   // in the JS side, and feed them to each importing module.
33349cc55cSDimitry Andric   if (!Asm->isPositionIndependent()) {
34349cc55cSDimitry Andric     for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
350b57cec5SDimitry Andric       SmallString<60> NameStr;
36349cc55cSDimitry Andric       Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
370b57cec5SDimitry Andric       if (Asm->OutContext.lookupSymbol(NameStr)) {
38349cc55cSDimitry Andric         MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
395ffd83dbSDimitry Andric         Asm->OutStreamer->emitLabel(ExceptionSym);
400b57cec5SDimitry Andric       }
410b57cec5SDimitry Andric     }
42349cc55cSDimitry Andric   }
43349cc55cSDimitry Andric }
440b57cec5SDimitry Andric 
endFunction(const MachineFunction * MF)450b57cec5SDimitry Andric void WasmException::endFunction(const MachineFunction *MF) {
460b57cec5SDimitry Andric   bool ShouldEmitExceptionTable = false;
470b57cec5SDimitry Andric   for (const LandingPadInfo &Info : MF->getLandingPads()) {
480b57cec5SDimitry Andric     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
490b57cec5SDimitry Andric       ShouldEmitExceptionTable = true;
500b57cec5SDimitry Andric       break;
510b57cec5SDimitry Andric     }
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric   if (!ShouldEmitExceptionTable)
540b57cec5SDimitry Andric     return;
550b57cec5SDimitry Andric   MCSymbol *LSDALabel = emitExceptionTable();
560b57cec5SDimitry Andric   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // Wasm requires every data section symbol to have a .size set. So we emit an
590b57cec5SDimitry Andric   // end marker and set the size as the difference between the start end the end
600b57cec5SDimitry Andric   // marker.
610b57cec5SDimitry Andric   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
625ffd83dbSDimitry Andric   Asm->OutStreamer->emitLabel(LSDAEndLabel);
630b57cec5SDimitry Andric   MCContext &OutContext = Asm->OutStreamer->getContext();
640b57cec5SDimitry Andric   const MCExpr *SizeExp = MCBinaryExpr::createSub(
650b57cec5SDimitry Andric       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
660b57cec5SDimitry Andric       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
670b57cec5SDimitry Andric   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric // Compute the call-site table for wasm EH. Even though we use the same function
710b57cec5SDimitry Andric // name to share the common routines, a call site entry in the table corresponds
720b57cec5SDimitry Andric // to not a call site for possibly-throwing functions but a landing pad. In wasm
730b57cec5SDimitry Andric // EH the VM is responsible for stack unwinding. After an exception occurs and
740b57cec5SDimitry Andric // the stack is unwound, the control flow is transferred to wasm 'catch'
750b57cec5SDimitry Andric // instruction by the VM, after which the personality function is called from
760b57cec5SDimitry Andric // the compiler-generated code. Refer to WasmEHPrepare pass for more
770b57cec5SDimitry Andric // information.
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> & CallSites,SmallVectorImpl<CallSiteRange> & CallSiteRanges,const SmallVectorImpl<const LandingPadInfo * > & LandingPads,const SmallVectorImpl<unsigned> & FirstActions)780b57cec5SDimitry Andric void WasmException::computeCallSiteTable(
790b57cec5SDimitry Andric     SmallVectorImpl<CallSiteEntry> &CallSites,
80e8d8bef9SDimitry Andric     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
810b57cec5SDimitry Andric     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
820b57cec5SDimitry Andric     const SmallVectorImpl<unsigned> &FirstActions) {
830b57cec5SDimitry Andric   MachineFunction &MF = *Asm->MF;
840b57cec5SDimitry Andric   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
850b57cec5SDimitry Andric     const LandingPadInfo *Info = LandingPads[I];
860b57cec5SDimitry Andric     MachineBasicBlock *LPad = Info->LandingPadBlock;
870b57cec5SDimitry Andric     // We don't emit LSDA for single catch (...).
880b57cec5SDimitry Andric     if (!MF.hasWasmLandingPadIndex(LPad))
890b57cec5SDimitry Andric       continue;
900b57cec5SDimitry Andric     // Wasm EH must maintain the EH pads in the order assigned to them by the
910b57cec5SDimitry Andric     // WasmEHPrepare pass.
920b57cec5SDimitry Andric     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
930b57cec5SDimitry Andric     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
940b57cec5SDimitry Andric     if (CallSites.size() < LPadIndex + 1)
950b57cec5SDimitry Andric       CallSites.resize(LPadIndex + 1);
960b57cec5SDimitry Andric     CallSites[LPadIndex] = Site;
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric }
99