blob: c56c591def361943705f797a94b632d1b02b3a20 [file] [log] [blame]
Dan Gohman05ac43f2015-12-17 01:39:00 +00001//=- 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
Dan Gohmand934cb82017-02-24 23:18:00 +000015#include "MCTargetDesc/WebAssemblyFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman1a427282016-01-12 03:32:29 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000018#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"
Reid Kleckner8f4bd1f2016-06-23 18:12:31 +000026#include "llvm/Support/EndianStream.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000027#include "llvm/Support/LEB128.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000028#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
30
31#define DEBUG_TYPE "mccodeemitter"
32
Dan Gohman1a427282016-01-12 03:32:29 +000033STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
34STATISTIC(MCNumFixups, "Number of MC fixups created.");
35
Dan Gohman05ac43f2015-12-17 01:39:00 +000036namespace {
37class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
Dan Gohman1a427282016-01-12 03:32:29 +000038 const MCInstrInfo &MCII;
Dan Gohmandf4f4d42017-02-10 00:14:42 +000039 MCContext &Ctx;
Dan Gohman05ac43f2015-12-17 01:39:00 +000040
Dan Gohman1a427282016-01-12 03:32:29 +000041 // Implementation generated by tablegen.
Dan Gohman05ac43f2015-12-17 01:39:00 +000042 uint64_t getBinaryCodeForInstr(const MCInst &MI,
43 SmallVectorImpl<MCFixup> &Fixups,
44 const MCSubtargetInfo &STI) const;
45
Dan Gohman05ac43f2015-12-17 01:39:00 +000046 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
47 SmallVectorImpl<MCFixup> &Fixups,
48 const MCSubtargetInfo &STI) const override;
Dan Gohman1a427282016-01-12 03:32:29 +000049
50public:
Dan Gohmandf4f4d42017-02-10 00:14:42 +000051 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
Dan Gohmand934cb82017-02-24 23:18:00 +000052 : MCII(mcii), Ctx(ctx) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000053};
54} // end anonymous namespace
55
Dan Gohmandf4f4d42017-02-10 00:14:42 +000056MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
57 MCContext &Ctx) {
58 return new WebAssemblyMCCodeEmitter(MCII, Ctx);
Dan Gohman05ac43f2015-12-17 01:39:00 +000059}
60
61void WebAssemblyMCCodeEmitter::encodeInstruction(
62 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
63 const MCSubtargetInfo &STI) const {
Dan Gohman4fc4e422016-10-24 19:49:43 +000064 uint64_t Start = OS.tell();
65
66 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Dan Gohman3acb1872016-10-24 23:27:49 +000067 assert(Binary < UINT8_MAX && "Multi-byte opcodes not supported yet");
68 OS << uint8_t(Binary);
Dan Gohman4fc4e422016-10-24 19:49:43 +000069
Dan Gohmand934cb82017-02-24 23:18:00 +000070 // For br_table instructions, encode the size of the table. In the MCInst,
71 // there's an index operand, one operand for each table entry, and the
72 // default operand.
73 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
74 MI.getOpcode() == WebAssembly::BR_TABLE_I64)
75 encodeULEB128(MI.getNumOperands() - 2, OS);
76
Dan Gohman1a427282016-01-12 03:32:29 +000077 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Dan Gohman1a427282016-01-12 03:32:29 +000078 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
79 const MCOperand &MO = MI.getOperand(i);
80 if (MO.isReg()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000081 /* nothing to encode */
Dan Gohman1a427282016-01-12 03:32:29 +000082 } else if (MO.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +000083 if (i < Desc.getNumOperands()) {
84 assert(Desc.TSFlags == 0 &&
85 "WebAssembly non-variable_ops don't use TSFlags");
86 const MCOperandInfo &Info = Desc.OpInfo[i];
87 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
88 encodeSLEB128(int32_t(MO.getImm()), OS);
89 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
90 encodeSLEB128(int64_t(MO.getImm()), OS);
Dan Gohmand934cb82017-02-24 23:18:00 +000091 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
92 Fixups.push_back(MCFixup::create(
93 OS.tell() - Start, MCConstantExpr::create(MO.getImm(), Ctx),
94 MCFixupKind(WebAssembly::fixup_code_global_index), MI.getLoc()));
95 ++MCNumFixups;
96 encodeULEB128(uint64_t(MO.getImm()), OS);
Derek Schufff7a4f3d2017-04-17 20:28:28 +000097 } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
98 encodeSLEB128(int64_t(MO.getImm()), OS);
Dan Gohman3acb1872016-10-24 23:27:49 +000099 } else {
100 encodeULEB128(uint64_t(MO.getImm()), OS);
101 }
Dan Gohman4fc4e422016-10-24 19:49:43 +0000102 } else {
Dan Gohman3acb1872016-10-24 23:27:49 +0000103 assert(Desc.TSFlags == (WebAssemblyII::VariableOpIsImmediate |
104 WebAssemblyII::VariableOpImmediateIsLabel));
Dan Gohman4fc4e422016-10-24 19:49:43 +0000105 encodeULEB128(uint64_t(MO.getImm()), OS);
106 }
Dan Gohman1a427282016-01-12 03:32:29 +0000107 } else if (MO.isFPImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000108 assert(i < Desc.getNumOperands() &&
109 "Unexpected floating-point immediate as a non-fixed operand");
110 assert(Desc.TSFlags == 0 &&
111 "WebAssembly variable_ops floating point ops don't use TSFlags");
112 const MCOperandInfo &Info = Desc.OpInfo[i];
113 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
114 // TODO: MC converts all floating point immediate operands to double.
115 // This is fine for numeric values, but may cause NaNs to change bits.
116 float f = float(MO.getFPImm());
117 support::endian::Writer<support::little>(OS).write<float>(f);
118 } else {
119 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
120 double d = MO.getFPImm();
121 support::endian::Writer<support::little>(OS).write<double>(d);
122 }
Dan Gohman1a427282016-01-12 03:32:29 +0000123 } else if (MO.isExpr()) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000124 const MCOperandInfo &Info = Desc.OpInfo[i];
125 llvm::MCFixupKind FixupKind;
126 size_t PaddedSize;
127 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
128 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i32);
129 PaddedSize = 5;
130 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
131 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i64);
132 PaddedSize = 10;
133 } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 ||
134 Info.OperandType == WebAssembly::OPERAND_OFFSET32 ||
135 Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
136 FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32);
137 PaddedSize = 5;
138 } else {
139 llvm_unreachable("unexpected symbolic operand kind");
140 }
Dan Gohman1a427282016-01-12 03:32:29 +0000141 Fixups.push_back(MCFixup::create(
Dan Gohman4fc4e422016-10-24 19:49:43 +0000142 OS.tell() - Start, MO.getExpr(),
Dan Gohmand934cb82017-02-24 23:18:00 +0000143 FixupKind, MI.getLoc()));
Dan Gohman1a427282016-01-12 03:32:29 +0000144 ++MCNumFixups;
Dan Gohmand934cb82017-02-24 23:18:00 +0000145 encodeULEB128(0, OS, PaddedSize - 1);
Dan Gohman1a427282016-01-12 03:32:29 +0000146 } else {
147 llvm_unreachable("unexpected operand kind");
148 }
149 }
Dan Gohman05ac43f2015-12-17 01:39:00 +0000150
Dan Gohman1a427282016-01-12 03:32:29 +0000151 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Dan Gohman05ac43f2015-12-17 01:39:00 +0000152}
153
154#include "WebAssemblyGenMCCodeEmitter.inc"