WebAssemblyMCCodeEmitter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //=- WebAssemblyMCCodeEmitter.cpp - Convert WebAssembly code to machine code -//
  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 WebAssemblyMCCodeEmitter class.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "MCTargetDesc/WebAssemblyFixupKinds.h"
  14. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/MC/MCCodeEmitter.h"
  18. #include "llvm/MC/MCFixup.h"
  19. #include "llvm/MC/MCInst.h"
  20. #include "llvm/MC/MCInstrInfo.h"
  21. #include "llvm/MC/MCRegisterInfo.h"
  22. #include "llvm/MC/MCSubtargetInfo.h"
  23. #include "llvm/MC/MCSymbol.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/EndianStream.h"
  26. #include "llvm/Support/LEB128.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. using namespace llvm;
  29. #define DEBUG_TYPE "mccodeemitter"
  30. STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
  31. STATISTIC(MCNumFixups, "Number of MC fixups created.");
  32. namespace {
  33. class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
  34. const MCInstrInfo &MCII;
  35. // Implementation generated by tablegen.
  36. uint64_t getBinaryCodeForInstr(const MCInst &MI,
  37. SmallVectorImpl<MCFixup> &Fixups,
  38. const MCSubtargetInfo &STI) const;
  39. void encodeInstruction(const MCInst &MI, raw_ostream &OS,
  40. SmallVectorImpl<MCFixup> &Fixups,
  41. const MCSubtargetInfo &STI) const override;
  42. public:
  43. WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) : MCII(MCII) {}
  44. };
  45. } // end anonymous namespace
  46. MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
  47. return new WebAssemblyMCCodeEmitter(MCII);
  48. }
  49. void WebAssemblyMCCodeEmitter::encodeInstruction(
  50. const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
  51. const MCSubtargetInfo &STI) const {
  52. uint64_t Start = OS.tell();
  53. uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
  54. if (Binary < (1 << 8)) {
  55. OS << uint8_t(Binary);
  56. } else if (Binary < (1 << 16)) {
  57. OS << uint8_t(Binary >> 8);
  58. encodeULEB128(uint8_t(Binary), OS);
  59. } else if (Binary < (1 << 24)) {
  60. OS << uint8_t(Binary >> 16);
  61. encodeULEB128(uint16_t(Binary), OS);
  62. } else {
  63. llvm_unreachable("Very large (prefix + 3 byte) opcodes not supported");
  64. }
  65. // For br_table instructions, encode the size of the table. In the MCInst,
  66. // there's an index operand (if not a stack instruction), one operand for
  67. // each table entry, and the default operand.
  68. if (MI.getOpcode() == WebAssembly::BR_TABLE_I32_S ||
  69. MI.getOpcode() == WebAssembly::BR_TABLE_I64_S)
  70. encodeULEB128(MI.getNumOperands() - 1, OS);
  71. if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
  72. MI.getOpcode() == WebAssembly::BR_TABLE_I64)
  73. encodeULEB128(MI.getNumOperands() - 2, OS);
  74. const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
  75. for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
  76. const MCOperand &MO = MI.getOperand(I);
  77. if (MO.isReg()) {
  78. /* nothing to encode */
  79. } else if (MO.isImm()) {
  80. if (I < Desc.getNumOperands()) {
  81. const MCOperandInfo &Info = Desc.operands()[I];
  82. LLVM_DEBUG(dbgs() << "Encoding immediate: type="
  83. << int(Info.OperandType) << "\n");
  84. switch (Info.OperandType) {
  85. case WebAssembly::OPERAND_I32IMM:
  86. encodeSLEB128(int32_t(MO.getImm()), OS);
  87. break;
  88. case WebAssembly::OPERAND_OFFSET32:
  89. encodeULEB128(uint32_t(MO.getImm()), OS);
  90. break;
  91. case WebAssembly::OPERAND_I64IMM:
  92. encodeSLEB128(int64_t(MO.getImm()), OS);
  93. break;
  94. case WebAssembly::OPERAND_SIGNATURE:
  95. case WebAssembly::OPERAND_VEC_I8IMM:
  96. support::endian::write<uint8_t>(OS, MO.getImm(), support::little);
  97. break;
  98. case WebAssembly::OPERAND_VEC_I16IMM:
  99. support::endian::write<uint16_t>(OS, MO.getImm(), support::little);
  100. break;
  101. case WebAssembly::OPERAND_VEC_I32IMM:
  102. support::endian::write<uint32_t>(OS, MO.getImm(), support::little);
  103. break;
  104. case WebAssembly::OPERAND_VEC_I64IMM:
  105. support::endian::write<uint64_t>(OS, MO.getImm(), support::little);
  106. break;
  107. case WebAssembly::OPERAND_GLOBAL:
  108. llvm_unreachable("wasm globals should only be accessed symbolicly");
  109. default:
  110. encodeULEB128(uint64_t(MO.getImm()), OS);
  111. }
  112. } else {
  113. encodeULEB128(uint64_t(MO.getImm()), OS);
  114. }
  115. } else if (MO.isSFPImm()) {
  116. uint32_t F = MO.getSFPImm();
  117. support::endian::write<uint32_t>(OS, F, support::little);
  118. } else if (MO.isDFPImm()) {
  119. uint64_t D = MO.getDFPImm();
  120. support::endian::write<uint64_t>(OS, D, support::little);
  121. } else if (MO.isExpr()) {
  122. const MCOperandInfo &Info = Desc.operands()[I];
  123. llvm::MCFixupKind FixupKind;
  124. size_t PaddedSize = 5;
  125. switch (Info.OperandType) {
  126. case WebAssembly::OPERAND_I32IMM:
  127. FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i32);
  128. break;
  129. case WebAssembly::OPERAND_I64IMM:
  130. FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i64);
  131. PaddedSize = 10;
  132. break;
  133. case WebAssembly::OPERAND_FUNCTION32:
  134. case WebAssembly::OPERAND_TABLE:
  135. case WebAssembly::OPERAND_OFFSET32:
  136. case WebAssembly::OPERAND_SIGNATURE:
  137. case WebAssembly::OPERAND_TYPEINDEX:
  138. case WebAssembly::OPERAND_GLOBAL:
  139. case WebAssembly::OPERAND_TAG:
  140. FixupKind = MCFixupKind(WebAssembly::fixup_uleb128_i32);
  141. break;
  142. case WebAssembly::OPERAND_OFFSET64:
  143. FixupKind = MCFixupKind(WebAssembly::fixup_uleb128_i64);
  144. PaddedSize = 10;
  145. break;
  146. default:
  147. llvm_unreachable("unexpected symbolic operand kind");
  148. }
  149. Fixups.push_back(MCFixup::create(OS.tell() - Start, MO.getExpr(),
  150. FixupKind, MI.getLoc()));
  151. ++MCNumFixups;
  152. encodeULEB128(0, OS, PaddedSize);
  153. } else {
  154. llvm_unreachable("unexpected operand kind");
  155. }
  156. }
  157. ++MCNumEmitted; // Keep track of the # of mi's emitted.
  158. }
  159. #include "WebAssemblyGenMCCodeEmitter.inc"