WasmException.cpp 4.6 KB

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