LLVM  5.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 /// \brief 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"
27 #include "llvm/Support/LEB128.h"
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "mccodeemitter"
32 
33 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
34 STATISTIC(MCNumFixups, "Number of MC fixups created.");
35 
36 namespace {
37 class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
38  const MCInstrInfo &MCII;
39 
40  // Implementation generated by tablegen.
41  uint64_t getBinaryCodeForInstr(const MCInst &MI,
43  const MCSubtargetInfo &STI) const;
44 
45  void encodeInstruction(const MCInst &MI, raw_ostream &OS,
47  const MCSubtargetInfo &STI) const override;
48 
49 public:
50  WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
51 };
52 } // end anonymous namespace
53 
55  return new WebAssemblyMCCodeEmitter(MCII);
56 }
57 
58 void WebAssemblyMCCodeEmitter::encodeInstruction(
60  const MCSubtargetInfo &STI) const {
61  uint64_t Start = OS.tell();
62 
63  uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
64  assert(Binary < UINT8_MAX && "Multi-byte opcodes not supported yet");
65  OS << uint8_t(Binary);
66 
67  // For br_table instructions, encode the size of the table. In the MCInst,
68  // there's an index operand, one operand for each table entry, and the
69  // default operand.
70  if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
71  MI.getOpcode() == WebAssembly::BR_TABLE_I64)
72  encodeULEB128(MI.getNumOperands() - 2, OS);
73 
74  const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
75  for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
76  const MCOperand &MO = MI.getOperand(i);
77  if (MO.isReg()) {
78  /* nothing to encode */
79  } else if (MO.isImm()) {
80  if (i < Desc.getNumOperands()) {
81  assert(Desc.TSFlags == 0 &&
82  "WebAssembly non-variable_ops don't use TSFlags");
83  const MCOperandInfo &Info = Desc.OpInfo[i];
85  encodeSLEB128(int32_t(MO.getImm()), OS);
86  } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
87  encodeSLEB128(int64_t(MO.getImm()), OS);
88  } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
89  llvm_unreachable("wasm globals should only be accessed symbolicly");
90  } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
91  encodeSLEB128(int64_t(MO.getImm()), OS);
92  } else {
93  encodeULEB128(uint64_t(MO.getImm()), OS);
94  }
95  } else {
98  encodeULEB128(uint64_t(MO.getImm()), OS);
99  }
100  } else if (MO.isFPImm()) {
101  assert(i < Desc.getNumOperands() &&
102  "Unexpected floating-point immediate as a non-fixed operand");
103  assert(Desc.TSFlags == 0 &&
104  "WebAssembly variable_ops floating point ops don't use TSFlags");
105  const MCOperandInfo &Info = Desc.OpInfo[i];
107  // TODO: MC converts all floating point immediate operands to double.
108  // This is fine for numeric values, but may cause NaNs to change bits.
109  float f = float(MO.getFPImm());
110  support::endian::Writer<support::little>(OS).write<float>(f);
111  } else {
113  double d = MO.getFPImm();
114  support::endian::Writer<support::little>(OS).write<double>(d);
115  }
116  } else if (MO.isExpr()) {
117  const MCOperandInfo &Info = Desc.OpInfo[i];
119  size_t PaddedSize;
122  PaddedSize = 5;
123  } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
125  PaddedSize = 10;
126  } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 ||
130  PaddedSize = 5;
131  } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
133  PaddedSize = 5;
134  } else {
135  llvm_unreachable("unexpected symbolic operand kind");
136  }
137  Fixups.push_back(MCFixup::create(
138  OS.tell() - Start, MO.getExpr(),
139  FixupKind, MI.getLoc()));
140  ++MCNumFixups;
141  encodeULEB128(0, OS, PaddedSize - 1);
142  } else {
143  llvm_unreachable("unexpected operand kind");
144  }
145  }
146 
147  ++MCNumEmitted; // Keep track of the # of mi's emitted.
148 }
149 
150 #include "WebAssemblyGenMCCodeEmitter.inc"
32-bit floating-point immediates.
double getFPImm() const
Definition: MCInst.h:86
STATISTIC(NumFunctions,"Total number of functions")
bool isReg() const
Definition: MCInst.h:58
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
32-bit unsigned memory offsets.
static Lanai::Fixups FixupKind(const MCExpr *Expr)
signature immediate for block/loop.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:98
uint8_t OperandType
Information about the type of the operand.
Definition: MCInstrDesc.h:82
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
bool isImm() const
Definition: MCInst.h:59
const MCExpr * getExpr() const
Definition: MCInst.h:96
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
type signature immediate for call_indirect.
SMLoc getLoc() const
Definition: MCInst.h:171
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:82
unsigned getOpcode() const
Definition: MCInst.h:168
64-bit floating-point immediates.
32-bit unsigned function indices.
int64_t getImm() const
Definition: MCInst.h:76
llvm_unreachable("Unknown RegBank!")
unsigned getNumOperands() const
Definition: MCInst.h:175
MCSubtargetInfo - Generic base class for all target subtargets.
void encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:210
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:174
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:73
IRTranslator LLVM IR MI
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition: MCInstrDesc.h:70
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173