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 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 450b57cec5SDimitry Andric void WasmException::markFunctionEnd() { 460b57cec5SDimitry Andric // Get rid of any dead landing pads. 470b57cec5SDimitry Andric if (!Asm->MF->getLandingPads().empty()) { 480b57cec5SDimitry Andric auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF); 490b57cec5SDimitry Andric // Wasm does not set BeginLabel and EndLabel information for landing pads, 500b57cec5SDimitry Andric // so we should set the second argument false. 510b57cec5SDimitry Andric NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric void WasmException::endFunction(const MachineFunction *MF) { 560b57cec5SDimitry Andric bool ShouldEmitExceptionTable = false; 570b57cec5SDimitry Andric for (const LandingPadInfo &Info : MF->getLandingPads()) { 580b57cec5SDimitry Andric if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) { 590b57cec5SDimitry Andric ShouldEmitExceptionTable = true; 600b57cec5SDimitry Andric break; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric if (!ShouldEmitExceptionTable) 640b57cec5SDimitry Andric return; 650b57cec5SDimitry Andric MCSymbol *LSDALabel = emitExceptionTable(); 660b57cec5SDimitry Andric assert(LSDALabel && ".GCC_exception_table has not been emitted!"); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // Wasm requires every data section symbol to have a .size set. So we emit an 690b57cec5SDimitry Andric // end marker and set the size as the difference between the start end the end 700b57cec5SDimitry Andric // marker. 710b57cec5SDimitry Andric MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end"); 725ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(LSDAEndLabel); 730b57cec5SDimitry Andric MCContext &OutContext = Asm->OutStreamer->getContext(); 740b57cec5SDimitry Andric const MCExpr *SizeExp = MCBinaryExpr::createSub( 750b57cec5SDimitry Andric MCSymbolRefExpr::create(LSDAEndLabel, OutContext), 760b57cec5SDimitry Andric MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext); 770b57cec5SDimitry Andric Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Compute the call-site table for wasm EH. Even though we use the same function 810b57cec5SDimitry Andric // name to share the common routines, a call site entry in the table corresponds 820b57cec5SDimitry Andric // to not a call site for possibly-throwing functions but a landing pad. In wasm 830b57cec5SDimitry Andric // EH the VM is responsible for stack unwinding. After an exception occurs and 840b57cec5SDimitry Andric // the stack is unwound, the control flow is transferred to wasm 'catch' 850b57cec5SDimitry Andric // instruction by the VM, after which the personality function is called from 860b57cec5SDimitry Andric // the compiler-generated code. Refer to WasmEHPrepare pass for more 870b57cec5SDimitry Andric // information. 880b57cec5SDimitry Andric void WasmException::computeCallSiteTable( 890b57cec5SDimitry Andric SmallVectorImpl<CallSiteEntry> &CallSites, 90e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteRange> &CallSiteRanges, 910b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 920b57cec5SDimitry Andric const SmallVectorImpl<unsigned> &FirstActions) { 930b57cec5SDimitry Andric MachineFunction &MF = *Asm->MF; 940b57cec5SDimitry Andric for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) { 950b57cec5SDimitry Andric const LandingPadInfo *Info = LandingPads[I]; 960b57cec5SDimitry Andric MachineBasicBlock *LPad = Info->LandingPadBlock; 970b57cec5SDimitry Andric // We don't emit LSDA for single catch (...). 980b57cec5SDimitry Andric if (!MF.hasWasmLandingPadIndex(LPad)) 990b57cec5SDimitry Andric continue; 1000b57cec5SDimitry Andric // Wasm EH must maintain the EH pads in the order assigned to them by the 1010b57cec5SDimitry Andric // WasmEHPrepare pass. 1020b57cec5SDimitry Andric unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad); 1030b57cec5SDimitry Andric CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]}; 1040b57cec5SDimitry Andric if (CallSites.size() < LPadIndex + 1) 1050b57cec5SDimitry Andric CallSites.resize(LPadIndex + 1); 1060b57cec5SDimitry Andric CallSites[LPadIndex] = Site; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric } 109