WasmException.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for writing WebAssembly exception info into asm
  10. // files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "WasmException.h"
  14. #include "llvm/CodeGen/AsmPrinter.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/IR/Mangler.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCStreamer.h"
  19. using namespace llvm;
  20. void WasmException::endModule() {
  21. // These are symbols used to throw/catch C++ exceptions and C longjmps. These
  22. // symbols have to be emitted somewhere once in the module. Check if each of
  23. // the symbols has already been created, i.e., we have at least one 'throw' or
  24. // 'catch' instruction with the symbol in the module, and emit the symbol only
  25. // if so.
  26. //
  27. // But in dynamic linking, it is in general not possible to come up with a
  28. // module instantiating order in which tag-defining modules are loaded before
  29. // the importing modules. So we make them undefined symbols here, define tags
  30. // in the JS side, and feed them to each importing module.
  31. if (!Asm->isPositionIndependent()) {
  32. for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
  33. SmallString<60> NameStr;
  34. Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
  35. if (Asm->OutContext.lookupSymbol(NameStr)) {
  36. MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
  37. Asm->OutStreamer->emitLabel(ExceptionSym);
  38. }
  39. }
  40. }
  41. }
  42. void WasmException::endFunction(const MachineFunction *MF) {
  43. bool ShouldEmitExceptionTable = false;
  44. for (const LandingPadInfo &Info : MF->getLandingPads()) {
  45. if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
  46. ShouldEmitExceptionTable = true;
  47. break;
  48. }
  49. }
  50. if (!ShouldEmitExceptionTable)
  51. return;
  52. MCSymbol *LSDALabel = emitExceptionTable();
  53. assert(LSDALabel && ".GCC_exception_table has not been emitted!");
  54. // Wasm requires every data section symbol to have a .size set. So we emit an
  55. // end marker and set the size as the difference between the start end the end
  56. // marker.
  57. MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
  58. Asm->OutStreamer->emitLabel(LSDAEndLabel);
  59. MCContext &OutContext = Asm->OutStreamer->getContext();
  60. const MCExpr *SizeExp = MCBinaryExpr::createSub(
  61. MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
  62. MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
  63. Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
  64. }
  65. // Compute the call-site table for wasm EH. Even though we use the same function
  66. // name to share the common routines, a call site entry in the table corresponds
  67. // to not a call site for possibly-throwing functions but a landing pad. In wasm
  68. // EH the VM is responsible for stack unwinding. After an exception occurs and
  69. // the stack is unwound, the control flow is transferred to wasm 'catch'
  70. // instruction by the VM, after which the personality function is called from
  71. // the compiler-generated code. Refer to WasmEHPrepare pass for more
  72. // information.
  73. void WasmException::computeCallSiteTable(
  74. SmallVectorImpl<CallSiteEntry> &CallSites,
  75. SmallVectorImpl<CallSiteRange> &CallSiteRanges,
  76. const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
  77. const SmallVectorImpl<unsigned> &FirstActions) {
  78. MachineFunction &MF = *Asm->MF;
  79. for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
  80. const LandingPadInfo *Info = LandingPads[I];
  81. MachineBasicBlock *LPad = Info->LandingPadBlock;
  82. // We don't emit LSDA for single catch (...).
  83. if (!MF.hasWasmLandingPadIndex(LPad))
  84. continue;
  85. // Wasm EH must maintain the EH pads in the order assigned to them by the
  86. // WasmEHPrepare pass.
  87. unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
  88. CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
  89. if (CallSites.size() < LPadIndex + 1)
  90. CallSites.resize(LPadIndex + 1);
  91. CallSites[LPadIndex] = Site;
  92. }
  93. }