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*349cc55cSDimitry Andric // These are symbols used to throw/catch C++ exceptions and C longjmps. These 22*349cc55cSDimitry Andric // symbols have to be emitted somewhere once in the module. Check if each of 23*349cc55cSDimitry Andric // the symbols has already been created, i.e., we have at least one 'throw' or 24*349cc55cSDimitry Andric // 'catch' instruction with the symbol in the module, and emit the symbol only 25*349cc55cSDimitry Andric // if so. 26*349cc55cSDimitry Andric // 27*349cc55cSDimitry Andric // But in dynamic linking, it is in general not possible to come up with a 28*349cc55cSDimitry Andric // module instantiating order in which tag-defining modules are loaded before 29*349cc55cSDimitry Andric // the importing modules. So we make them undefined symbols here, define tags 30*349cc55cSDimitry Andric // in the JS side, and feed them to each importing module. 31*349cc55cSDimitry Andric if (!Asm->isPositionIndependent()) { 32*349cc55cSDimitry Andric for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) { 330b57cec5SDimitry Andric SmallString<60> NameStr; 34*349cc55cSDimitry Andric Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout()); 350b57cec5SDimitry Andric if (Asm->OutContext.lookupSymbol(NameStr)) { 36*349cc55cSDimitry Andric MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName); 375ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(ExceptionSym); 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric } 40*349cc55cSDimitry Andric } 41*349cc55cSDimitry Andric } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric void WasmException::markFunctionEnd() { 440b57cec5SDimitry Andric // Get rid of any dead landing pads. 450b57cec5SDimitry Andric if (!Asm->MF->getLandingPads().empty()) { 460b57cec5SDimitry Andric auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF); 470b57cec5SDimitry Andric // Wasm does not set BeginLabel and EndLabel information for landing pads, 480b57cec5SDimitry Andric // so we should set the second argument false. 490b57cec5SDimitry Andric NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric void WasmException::endFunction(const MachineFunction *MF) { 540b57cec5SDimitry Andric bool ShouldEmitExceptionTable = false; 550b57cec5SDimitry Andric for (const LandingPadInfo &Info : MF->getLandingPads()) { 560b57cec5SDimitry Andric if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) { 570b57cec5SDimitry Andric ShouldEmitExceptionTable = true; 580b57cec5SDimitry Andric break; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric if (!ShouldEmitExceptionTable) 620b57cec5SDimitry Andric return; 630b57cec5SDimitry Andric MCSymbol *LSDALabel = emitExceptionTable(); 640b57cec5SDimitry Andric assert(LSDALabel && ".GCC_exception_table has not been emitted!"); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Wasm requires every data section symbol to have a .size set. So we emit an 670b57cec5SDimitry Andric // end marker and set the size as the difference between the start end the end 680b57cec5SDimitry Andric // marker. 690b57cec5SDimitry Andric MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end"); 705ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(LSDAEndLabel); 710b57cec5SDimitry Andric MCContext &OutContext = Asm->OutStreamer->getContext(); 720b57cec5SDimitry Andric const MCExpr *SizeExp = MCBinaryExpr::createSub( 730b57cec5SDimitry Andric MCSymbolRefExpr::create(LSDAEndLabel, OutContext), 740b57cec5SDimitry Andric MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext); 750b57cec5SDimitry Andric Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp); 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // Compute the call-site table for wasm EH. Even though we use the same function 790b57cec5SDimitry Andric // name to share the common routines, a call site entry in the table corresponds 800b57cec5SDimitry Andric // to not a call site for possibly-throwing functions but a landing pad. In wasm 810b57cec5SDimitry Andric // EH the VM is responsible for stack unwinding. After an exception occurs and 820b57cec5SDimitry Andric // the stack is unwound, the control flow is transferred to wasm 'catch' 830b57cec5SDimitry Andric // instruction by the VM, after which the personality function is called from 840b57cec5SDimitry Andric // the compiler-generated code. Refer to WasmEHPrepare pass for more 850b57cec5SDimitry Andric // information. 860b57cec5SDimitry Andric void WasmException::computeCallSiteTable( 870b57cec5SDimitry Andric SmallVectorImpl<CallSiteEntry> &CallSites, 88e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteRange> &CallSiteRanges, 890b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 900b57cec5SDimitry Andric const SmallVectorImpl<unsigned> &FirstActions) { 910b57cec5SDimitry Andric MachineFunction &MF = *Asm->MF; 920b57cec5SDimitry Andric for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) { 930b57cec5SDimitry Andric const LandingPadInfo *Info = LandingPads[I]; 940b57cec5SDimitry Andric MachineBasicBlock *LPad = Info->LandingPadBlock; 950b57cec5SDimitry Andric // We don't emit LSDA for single catch (...). 960b57cec5SDimitry Andric if (!MF.hasWasmLandingPadIndex(LPad)) 970b57cec5SDimitry Andric continue; 980b57cec5SDimitry Andric // Wasm EH must maintain the EH pads in the order assigned to them by the 990b57cec5SDimitry Andric // WasmEHPrepare pass. 1000b57cec5SDimitry Andric unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad); 1010b57cec5SDimitry Andric CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]}; 1020b57cec5SDimitry Andric if (CallSites.size() < LPadIndex + 1) 1030b57cec5SDimitry Andric CallSites.resize(LPadIndex + 1); 1040b57cec5SDimitry Andric CallSites[LPadIndex] = Site; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric } 107