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