LLVM 20.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/MCContext.h"
20#include "llvm/MC/MCFixup.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Support/Debug.h"
28#include "llvm/Support/LEB128.h"
29#include "llvm/Support/SMLoc.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "mccodeemitter"
35
36STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
37STATISTIC(MCNumFixups, "Number of MC fixups created.");
38
39namespace {
40class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
41 const MCInstrInfo &MCII;
42 MCContext &Ctx;
43 // Implementation generated by tablegen.
44 uint64_t getBinaryCodeForInstr(const MCInst &MI,
46 const MCSubtargetInfo &STI) const;
47
50 const MCSubtargetInfo &STI) const override;
51
52public:
53 WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
54 : MCII(MCII), Ctx{Ctx} {}
55};
56} // end anonymous namespace
57
59 MCContext &Ctx) {
60 return new WebAssemblyMCCodeEmitter(MCII, Ctx);
61}
62
63void WebAssemblyMCCodeEmitter::encodeInstruction(
65 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
67 uint64_t Start = OS.tell();
68
69 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
70 if (Binary < (1 << 8)) {
71 OS << uint8_t(Binary);
72 } else if (Binary < (1 << 16)) {
73 OS << uint8_t(Binary >> 8);
74 encodeULEB128(uint8_t(Binary), OS);
75 } else if (Binary < (1 << 24)) {
76 OS << uint8_t(Binary >> 16);
77 encodeULEB128(uint16_t(Binary), OS);
78 } else {
79 llvm_unreachable("Very large (prefix + 3 byte) opcodes not supported");
80 }
81
82 // For br_table instructions, encode the size of the table. In the MCInst,
83 // there's an index operand (if not a stack instruction), one operand for
84 // each table entry, and the default operand.
85 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32_S ||
86 MI.getOpcode() == WebAssembly::BR_TABLE_I64_S)
87 encodeULEB128(MI.getNumOperands() - 1, OS);
88 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
89 MI.getOpcode() == WebAssembly::BR_TABLE_I64)
90 encodeULEB128(MI.getNumOperands() - 2, OS);
91
92 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
93 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
94 const MCOperand &MO = MI.getOperand(I);
95 if (MO.isReg()) {
96 /* nothing to encode */
97
98 } else if (MO.isImm()) {
99 if (I < Desc.getNumOperands()) {
100 const MCOperandInfo &Info = Desc.operands()[I];
101 LLVM_DEBUG(dbgs() << "Encoding immediate: type="
102 << int(Info.OperandType) << "\n");
103 switch (Info.OperandType) {
105 encodeSLEB128(int32_t(MO.getImm()), OS);
106 break;
109 break;
111 encodeSLEB128(int64_t(MO.getImm()), OS);
112 break;
115 support::endian::write<uint8_t>(OS, MO.getImm(),
117 break;
119 support::endian::write<uint16_t>(OS, MO.getImm(),
121 break;
123 support::endian::write<uint32_t>(OS, MO.getImm(),
125 break;
127 support::endian::write<uint64_t>(OS, MO.getImm(),
129 break;
131 Ctx.reportError(
132 SMLoc(),
133 Twine("Wasm globals should only be accessed symbolically!"));
134 break;
135 default:
137 }
138 } else {
140 }
141
142 } else if (MO.isSFPImm()) {
143 uint32_t F = MO.getSFPImm();
144 support::endian::write<uint32_t>(OS, F, llvm::endianness::little);
145 } else if (MO.isDFPImm()) {
146 uint64_t D = MO.getDFPImm();
147 support::endian::write<uint64_t>(OS, D, llvm::endianness::little);
148 } else if (MO.isExpr()) {
149 const MCOperandInfo &Info = Desc.operands()[I];
151 size_t PaddedSize = 5;
152 switch (Info.OperandType) {
155 break;
158 PaddedSize = 10;
159 break;
168 break;
171 PaddedSize = 10;
172 break;
173 default:
174 llvm_unreachable("unexpected symbolic operand kind");
175 }
176 Fixups.push_back(MCFixup::create(OS.tell() - Start, MO.getExpr(),
177 FixupKind, MI.getLoc()));
178 ++MCNumFixups;
179 encodeULEB128(0, OS, PaddedSize);
180 } else {
181 llvm_unreachable("unexpected operand kind");
182 }
183 }
184
185 ++MCNumEmitted; // Keep track of the # of mi's emitted.
186}
187
188#include "WebAssemblyGenMCCodeEmitter.inc"
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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:166
This file provides WebAssembly-specific target descriptions.
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
Context object for machine code objects.
Definition: MCContext.h:83
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
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.
Represents a location in source code.
Definition: SMLoc.h:23
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:147
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
#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, MCContext &Ctx)
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
Description of the encoding of one expression Op.