Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains support for writing WebAssembly exception info into asm |
| 10 | // files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "WasmException.h" |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Mangler.h" |
| 16 | #include "llvm/MC/MCContext.h" |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCStreamer.h" |
| 18 | using namespace llvm; |
| 19 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 20 | void WasmException::endModule() { |
| 21 | // This is the symbol used in 'throw' and 'if_except' instruction to denote |
| 22 | // this is a C++ exception. This symbol has to be emitted somewhere once in |
| 23 | // the module. Check if the symbol has already been created, i.e., we have at |
| 24 | // least one 'throw' or 'if_except' instruction in the module, and emit the |
| 25 | // symbol only if so. |
| 26 | SmallString<60> NameStr; |
| 27 | Mangler::getNameWithPrefix(NameStr, "__cpp_exception", Asm->getDataLayout()); |
| 28 | if (Asm->OutContext.lookupSymbol(NameStr)) { |
| 29 | MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol("__cpp_exception"); |
| 30 | Asm->OutStreamer->EmitLabel(ExceptionSym); |
| 31 | } |
| 32 | } |
| 33 | |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 34 | void WasmException::markFunctionEnd() { |
| 35 | // Get rid of any dead landing pads. |
| 36 | if (!Asm->MF->getLandingPads().empty()) { |
| 37 | auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF); |
| 38 | // Wasm does not set BeginLabel and EndLabel information for landing pads, |
| 39 | // so we should set the second argument false. |
| 40 | NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void WasmException::endFunction(const MachineFunction *MF) { |
| 45 | bool ShouldEmitExceptionTable = false; |
| 46 | for (const LandingPadInfo &Info : MF->getLandingPads()) { |
| 47 | if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) { |
| 48 | ShouldEmitExceptionTable = true; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | if (!ShouldEmitExceptionTable) |
| 53 | return; |
| 54 | MCSymbol *LSDALabel = emitExceptionTable(); |
| 55 | assert(LSDALabel && ".GCC_exception_table has not been emitted!"); |
| 56 | |
| 57 | // Wasm requires every data section symbol to have a .size set. So we emit an |
| 58 | // end marker and set the size as the difference between the start end the end |
| 59 | // marker. |
| 60 | MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end"); |
| 61 | Asm->OutStreamer->EmitLabel(LSDAEndLabel); |
| 62 | MCContext &OutContext = Asm->OutStreamer->getContext(); |
| 63 | const MCExpr *SizeExp = MCBinaryExpr::createSub( |
| 64 | MCSymbolRefExpr::create(LSDAEndLabel, OutContext), |
| 65 | MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext); |
| 66 | Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp); |
| 67 | } |
| 68 | |
| 69 | // Compute the call-site table for wasm EH. Even though we use the same function |
| 70 | // name to share the common routines, a call site entry in the table corresponds |
| 71 | // to not a call site for possibly-throwing functions but a landing pad. In wasm |
| 72 | // EH the VM is responsible for stack unwinding. After an exception occurs and |
| 73 | // the stack is unwound, the control flow is transferred to wasm 'catch' |
| 74 | // instruction by the VM, after which the personality function is called from |
| 75 | // the compiler-generated code. Refer to WasmEHPrepare pass for more |
| 76 | // information. |
| 77 | void WasmException::computeCallSiteTable( |
| 78 | SmallVectorImpl<CallSiteEntry> &CallSites, |
| 79 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 80 | const SmallVectorImpl<unsigned> &FirstActions) { |
| 81 | MachineFunction &MF = *Asm->MF; |
| 82 | for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) { |
| 83 | const LandingPadInfo *Info = LandingPads[I]; |
| 84 | MachineBasicBlock *LPad = Info->LandingPadBlock; |
| 85 | // We don't emit LSDA for single catch (...). |
| 86 | if (!MF.hasWasmLandingPadIndex(LPad)) |
| 87 | continue; |
| 88 | // Wasm EH must maintain the EH pads in the order assigned to them by the |
| 89 | // WasmEHPrepare pass. |
| 90 | unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad); |
| 91 | CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]}; |
| 92 | if (CallSites.size() < LPadIndex + 1) |
| 93 | CallSites.resize(LPadIndex + 1); |
| 94 | CallSites[LPadIndex] = Site; |
| 95 | } |
| 96 | } |