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