WebAssemblyAsmBackend.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
  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. /// \file
  10. /// This file implements the WebAssemblyAsmBackend class.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "MCTargetDesc/WebAssemblyFixupKinds.h"
  14. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  15. #include "llvm/MC/MCAsmBackend.h"
  16. #include "llvm/MC/MCAssembler.h"
  17. #include "llvm/MC/MCDirectives.h"
  18. #include "llvm/MC/MCExpr.h"
  19. #include "llvm/MC/MCFixupKindInfo.h"
  20. #include "llvm/MC/MCObjectWriter.h"
  21. #include "llvm/MC/MCSubtargetInfo.h"
  22. #include "llvm/MC/MCSymbol.h"
  23. #include "llvm/MC/MCWasmObjectWriter.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. namespace {
  28. class WebAssemblyAsmBackend final : public MCAsmBackend {
  29. bool Is64Bit;
  30. bool IsEmscripten;
  31. public:
  32. explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten)
  33. : MCAsmBackend(support::little), Is64Bit(Is64Bit),
  34. IsEmscripten(IsEmscripten) {}
  35. unsigned getNumFixupKinds() const override {
  36. return WebAssembly::NumTargetFixupKinds;
  37. }
  38. const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
  39. void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
  40. const MCValue &Target, MutableArrayRef<char> Data,
  41. uint64_t Value, bool IsPCRel,
  42. const MCSubtargetInfo *STI) const override;
  43. std::unique_ptr<MCObjectTargetWriter>
  44. createObjectTargetWriter() const override;
  45. // No instruction requires relaxation
  46. bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
  47. const MCRelaxableFragment *DF,
  48. const MCAsmLayout &Layout) const override {
  49. return false;
  50. }
  51. bool writeNopData(raw_ostream &OS, uint64_t Count,
  52. const MCSubtargetInfo *STI) const override;
  53. };
  54. const MCFixupKindInfo &
  55. WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
  56. const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
  57. // This table *must* be in the order that the fixup_* kinds are defined in
  58. // WebAssemblyFixupKinds.h.
  59. //
  60. // Name Offset (bits) Size (bits) Flags
  61. {"fixup_sleb128_i32", 0, 5 * 8, 0},
  62. {"fixup_sleb128_i64", 0, 10 * 8, 0},
  63. {"fixup_uleb128_i32", 0, 5 * 8, 0},
  64. {"fixup_uleb128_i64", 0, 10 * 8, 0},
  65. };
  66. if (Kind < FirstTargetFixupKind)
  67. return MCAsmBackend::getFixupKindInfo(Kind);
  68. assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
  69. "Invalid kind!");
  70. return Infos[Kind - FirstTargetFixupKind];
  71. }
  72. bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
  73. const MCSubtargetInfo *STI) const {
  74. for (uint64_t I = 0; I < Count; ++I)
  75. OS << char(WebAssembly::Nop);
  76. return true;
  77. }
  78. void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
  79. const MCFixup &Fixup,
  80. const MCValue &Target,
  81. MutableArrayRef<char> Data,
  82. uint64_t Value, bool IsPCRel,
  83. const MCSubtargetInfo *STI) const {
  84. const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
  85. assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
  86. unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
  87. if (Value == 0)
  88. return; // Doesn't change encoding.
  89. // Shift the value into position.
  90. Value <<= Info.TargetOffset;
  91. unsigned Offset = Fixup.getOffset();
  92. assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
  93. // For each byte of the fragment that the fixup touches, mask in the
  94. // bits from the fixup value.
  95. for (unsigned I = 0; I != NumBytes; ++I)
  96. Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
  97. }
  98. std::unique_ptr<MCObjectTargetWriter>
  99. WebAssemblyAsmBackend::createObjectTargetWriter() const {
  100. return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
  101. }
  102. } // end anonymous namespace
  103. MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
  104. return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
  105. }