LLVM  7.0.0
WebAssemblyMCCodeEmitter.cpp
Go to the documentation of this file.
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 
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"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/LEB128.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,
45  const MCSubtargetInfo &STI) const;
46 
47  void encodeInstruction(const MCInst &MI, raw_ostream &OS,
49  const MCSubtargetInfo &STI) const override;
50 
51 public:
52  WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
53 };
54 } // end anonymous namespace
55 
57  return new WebAssemblyMCCodeEmitter(MCII);
58 }
59 
60 void WebAssemblyMCCodeEmitter::encodeInstruction(
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, one operand for each table entry, and the
76  // default operand.
77  if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
78  MI.getOpcode() == WebAssembly::BR_TABLE_I64)
79  encodeULEB128(MI.getNumOperands() - 2, OS);
80 
81  const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
82  for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
83  const MCOperand &MO = MI.getOperand(i);
84  if (MO.isReg()) {
85  /* nothing to encode */
86  } else if (MO.isImm()) {
87  if (i < Desc.getNumOperands()) {
88  assert(Desc.TSFlags == 0 &&
89  "WebAssembly non-variable_ops don't use TSFlags");
90  const MCOperandInfo &Info = Desc.OpInfo[i];
91  LLVM_DEBUG(dbgs() << "Encoding immediate: type="
92  << int(Info.OperandType) << "\n");
94  encodeSLEB128(int32_t(MO.getImm()), OS);
95  } else if (Info.OperandType == WebAssembly::OPERAND_OFFSET32) {
96  encodeULEB128(uint32_t(MO.getImm()), OS);
97  } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
98  encodeSLEB128(int64_t(MO.getImm()), OS);
99  } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
100  llvm_unreachable("wasm globals should only be accessed symbolicly");
101  } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
102  OS << uint8_t(MO.getImm());
103  } else {
104  encodeULEB128(uint64_t(MO.getImm()), OS);
105  }
106  } else {
109  encodeULEB128(uint64_t(MO.getImm()), OS);
110  }
111  } else if (MO.isFPImm()) {
112  assert(i < Desc.getNumOperands() &&
113  "Unexpected floating-point immediate as a non-fixed operand");
114  assert(Desc.TSFlags == 0 &&
115  "WebAssembly variable_ops floating point ops don't use TSFlags");
116  const MCOperandInfo &Info = Desc.OpInfo[i];
118  // TODO: MC converts all floating point immediate operands to double.
119  // This is fine for numeric values, but may cause NaNs to change bits.
120  float f = float(MO.getFPImm());
122  } else {
124  double d = MO.getFPImm();
126  }
127  } else if (MO.isExpr()) {
128  const MCOperandInfo &Info = Desc.OpInfo[i];
130  size_t PaddedSize = 5;
133  } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
135  PaddedSize = 10;
136  } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 ||
140  } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
142  } else {
143  llvm_unreachable("unexpected symbolic operand kind");
144  }
145  Fixups.push_back(MCFixup::create(
146  OS.tell() - Start, MO.getExpr(),
147  FixupKind, MI.getLoc()));
148  ++MCNumFixups;
149  encodeULEB128(0, OS, PaddedSize);
150  } else {
151  llvm_unreachable("unexpected operand kind");
152  }
153  }
154 
155  ++MCNumEmitted; // Keep track of the # of mi's emitted.
156 }
157 
158 #include "WebAssemblyGenMCCodeEmitter.inc"
32-bit floating-point immediates.
bool isImm() const
Definition: MCInst.h:59
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:162
32-bit unsigned memory offsets.
bool isReg() const
Definition: MCInst.h:58
STATISTIC(NumFunctions, "Total number of functions")
static Lanai::Fixups FixupKind(const MCExpr *Expr)
signature immediate for block/loop.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:209
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
uint8_t OperandType
Information about the type of the operand.
Definition: MCInstrDesc.h:79
const MCExpr * getExpr() const
Definition: MCInst.h:96
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
int64_t getImm() const
Definition: MCInst.h:76
MCCodeEmitter * createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII)
bool isFPImm() const
Definition: MCInst.h:60
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
This file provides WebAssembly-specific target descriptions.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
bool isExpr() const
Definition: MCInst.h:61
void write< float >(raw_ostream &os, float value, endianness endian)
Definition: EndianStream.h:34
unsigned getNumOperands() const
Definition: MCInst.h:184
type signature immediate for call_indirect.
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:90
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:81
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:24
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
SMLoc getLoc() const
Definition: MCInst.h:180
64-bit floating-point immediates.
32-bit unsigned function indices.
Generic base class for all target subtargets.
void write< double >(raw_ostream &os, double value, endianness endian)
Definition: EndianStream.h:39
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:173
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:100
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition: MCInstrDesc.h:67
unsigned getOpcode() const
Definition: MCInst.h:174
#define LLVM_DEBUG(X)
Definition: Debug.h:119
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
double getFPImm() const
Definition: MCInst.h:86