LLVM 17.0.0git
WebAssemblyMCCodeEmitter.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/Statistic.h"
19#include "llvm/MC/MCFixup.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/Support/Debug.h"
27#include "llvm/Support/LEB128.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "mccodeemitter"
33
34STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
35STATISTIC(MCNumFixups, "Number of MC fixups created.");
36
37namespace {
38class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
39 const MCInstrInfo &MCII;
40
41 // Implementation generated by tablegen.
42 uint64_t getBinaryCodeForInstr(const MCInst &MI,
44 const MCSubtargetInfo &STI) const;
45
48 const MCSubtargetInfo &STI) const override;
49
50public:
51 WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) : MCII(MCII) {}
52};
53} // end anonymous namespace
54
56 return new WebAssemblyMCCodeEmitter(MCII);
57}
58
59void WebAssemblyMCCodeEmitter::encodeInstruction(
61 const MCSubtargetInfo &STI) const {
62 uint64_t Start = OS.tell();
63
64 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
65 if (Binary < (1 << 8)) {
66 OS << uint8_t(Binary);
67 } else if (Binary < (1 << 16)) {
68 OS << uint8_t(Binary >> 8);
69 encodeULEB128(uint8_t(Binary), OS);
70 } else if (Binary < (1 << 24)) {
71 OS << uint8_t(Binary >> 16);
72 encodeULEB128(uint16_t(Binary), OS);
73 } else {
74 llvm_unreachable("Very large (prefix + 3 byte) opcodes not supported");
75 }
76
77 // For br_table instructions, encode the size of the table. In the MCInst,
78 // there's an index operand (if not a stack instruction), one operand for
79 // each table entry, and the default operand.
80 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32_S ||
81 MI.getOpcode() == WebAssembly::BR_TABLE_I64_S)
82 encodeULEB128(MI.getNumOperands() - 1, OS);
83 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
84 MI.getOpcode() == WebAssembly::BR_TABLE_I64)
85 encodeULEB128(MI.getNumOperands() - 2, OS);
86
87 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
88 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
89 const MCOperand &MO = MI.getOperand(I);
90 if (MO.isReg()) {
91 /* nothing to encode */
92
93 } else if (MO.isImm()) {
94 if (I < Desc.getNumOperands()) {
95 const MCOperandInfo &Info = Desc.operands()[I];
96 LLVM_DEBUG(dbgs() << "Encoding immediate: type="
97 << int(Info.OperandType) << "\n");
98 switch (Info.OperandType) {
100 encodeSLEB128(int32_t(MO.getImm()), OS);
101 break;
104 break;
106 encodeSLEB128(int64_t(MO.getImm()), OS);
107 break;
110 support::endian::write<uint8_t>(OS, MO.getImm(), support::little);
111 break;
113 support::endian::write<uint16_t>(OS, MO.getImm(), support::little);
114 break;
116 support::endian::write<uint32_t>(OS, MO.getImm(), support::little);
117 break;
119 support::endian::write<uint64_t>(OS, MO.getImm(), support::little);
120 break;
122 llvm_unreachable("wasm globals should only be accessed symbolicly");
123 default:
125 }
126 } else {
128 }
129
130 } else if (MO.isSFPImm()) {
131 uint32_t F = MO.getSFPImm();
132 support::endian::write<uint32_t>(OS, F, support::little);
133 } else if (MO.isDFPImm()) {
134 uint64_t D = MO.getDFPImm();
135 support::endian::write<uint64_t>(OS, D, support::little);
136 } else if (MO.isExpr()) {
137 const MCOperandInfo &Info = Desc.operands()[I];
139 size_t PaddedSize = 5;
140 switch (Info.OperandType) {
143 break;
146 PaddedSize = 10;
147 break;
156 break;
159 PaddedSize = 10;
160 break;
161 default:
162 llvm_unreachable("unexpected symbolic operand kind");
163 }
164 Fixups.push_back(MCFixup::create(OS.tell() - Start, MO.getExpr(),
165 FixupKind, MI.getLoc()));
166 ++MCNumFixups;
167 encodeULEB128(0, OS, PaddedSize);
168 } else {
169 llvm_unreachable("unexpected operand kind");
170 }
171 }
172
173 ++MCNumEmitted; // Keep track of the # of mi's emitted.
174}
175
176#include "WebAssemblyGenMCCodeEmitter.inc"
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
This file provides WebAssembly-specific target descriptions.
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
Definition: MCCodeEmitter.h:28
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:237
ArrayRef< MCOperandInfo > operands() const
Definition: MCInstrDesc.h:239
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition: MCInstrDesc.h:85
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
bool isSFPImm() const
Definition: MCInst.h:63
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
bool isReg() const
Definition: MCInst.h:61
bool isDFPImm() const
Definition: MCInst.h:64
const MCExpr * getExpr() const
Definition: MCInst.h:114
uint32_t getSFPImm() const
Definition: MCInst.h:90
uint64_t getDFPImm() const
Definition: MCInst.h:100
bool isExpr() const
Definition: MCInst.h:65
Generic base class for all target subtargets.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:134
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ OPERAND_GLOBAL
Global index.
@ OPERAND_OFFSET64
64-bit unsigned memory offsets.
@ OPERAND_I32IMM
32-bit integer immediates.
@ OPERAND_TABLE
32-bit unsigned table number.
@ OPERAND_VEC_I64IMM
64-bit vector lane immediate
@ OPERAND_VEC_I16IMM
16-bit vector lane immediate
@ OPERAND_TYPEINDEX
type signature immediate for call_indirect.
@ OPERAND_FUNCTION32
32-bit unsigned function indices.
@ OPERAND_VEC_I32IMM
32-bit vector lane immediate
@ OPERAND_VEC_I8IMM
8-bit vector lane immediate
@ OPERAND_SIGNATURE
signature immediate for block/loop.
@ OPERAND_I64IMM
64-bit integer immediates.
@ OPERAND_OFFSET32
32-bit unsigned memory offsets.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCCodeEmitter * createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80