blob: d58c83d14ae2a5537224e83caa55dfa93a44efc9 [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file implements the WebAssemblyMCCodeEmitter class.
Dan Gohman05ac43f2015-12-17 01:39:00 +000012///
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"
Sam Clegg685c5e82018-04-04 22:27:58 +000026#include "llvm/Support/Debug.h"
Reid Kleckner8f4bd1f2016-06-23 18:12:31 +000027#include "llvm/Support/EndianStream.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000028#include "llvm/Support/LEB128.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000029#include "llvm/Support/raw_ostream.h"
Sam Clegg685c5e82018-04-04 22:27:58 +000030
Dan Gohman05ac43f2015-12-17 01:39:00 +000031using namespace llvm;
32
33#define DEBUG_TYPE "mccodeemitter"
34
Dan Gohman1a427282016-01-12 03:32:29 +000035STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
36STATISTIC(MCNumFixups, "Number of MC fixups created.");
37
Dan Gohman05ac43f2015-12-17 01:39:00 +000038namespace {
39class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
Dan Gohman1a427282016-01-12 03:32:29 +000040 const MCInstrInfo &MCII;
Dan Gohman05ac43f2015-12-17 01:39:00 +000041
Dan Gohman1a427282016-01-12 03:32:29 +000042 // Implementation generated by tablegen.
Dan Gohman05ac43f2015-12-17 01:39:00 +000043 uint64_t getBinaryCodeForInstr(const MCInst &MI,
44 SmallVectorImpl<MCFixup> &Fixups,
45 const MCSubtargetInfo &STI) const;
46
Dan Gohman05ac43f2015-12-17 01:39:00 +000047 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
48 SmallVectorImpl<MCFixup> &Fixups,
49 const MCSubtargetInfo &STI) const override;
Dan Gohman1a427282016-01-12 03:32:29 +000050
51public:
Sam Clegg9d24fb72017-06-16 23:59:10 +000052 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000053};
54} // end anonymous namespace
55
Sam Clegg9d24fb72017-06-16 23:59:10 +000056MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
57 return new WebAssemblyMCCodeEmitter(MCII);
Dan Gohman05ac43f2015-12-17 01:39:00 +000058}
59
60void WebAssemblyMCCodeEmitter::encodeInstruction(
61 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const {
Dan Gohman4fc4e422016-10-24 19:49:43 +000063 uint64_t Start = OS.tell();
64
65 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Dan Gohmancdd48b82017-11-28 01:13:40 +000066 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 }
Dan Gohman4fc4e422016-10-24 19:49:43 +000073
Dan Gohmand934cb82017-02-24 23:18:00 +000074 // 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
Dan Gohman1a427282016-01-12 03:32:29 +000081 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Dan Gohman1a427282016-01-12 03:32:29 +000082 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
83 const MCOperand &MO = MI.getOperand(i);
84 if (MO.isReg()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000085 /* nothing to encode */
Dan Gohman1a427282016-01-12 03:32:29 +000086 } else if (MO.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +000087 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];
Nicola Zaghend34e60c2018-05-14 12:53:11 +000091 LLVM_DEBUG(dbgs() << "Encoding immediate: type="
92 << int(Info.OperandType) << "\n");
Dan Gohman3acb1872016-10-24 23:27:49 +000093 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
94 encodeSLEB128(int32_t(MO.getImm()), OS);
Sam Clegg685c5e82018-04-04 22:27:58 +000095 } else if (Info.OperandType == WebAssembly::OPERAND_OFFSET32) {
96 encodeULEB128(uint32_t(MO.getImm()), OS);
Dan Gohman3acb1872016-10-24 23:27:49 +000097 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
98 encodeSLEB128(int64_t(MO.getImm()), OS);
Dan Gohmand934cb82017-02-24 23:18:00 +000099 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000100 llvm_unreachable("wasm globals should only be accessed symbolicly");
Derek Schufff7a4f3d2017-04-17 20:28:28 +0000101 } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
Heejin Ahn0c69a3e2018-03-02 20:52:59 +0000102 OS << uint8_t(MO.getImm());
Dan Gohman3acb1872016-10-24 23:27:49 +0000103 } else {
104 encodeULEB128(uint64_t(MO.getImm()), OS);
105 }
Dan Gohman4fc4e422016-10-24 19:49:43 +0000106 } else {
Dan Gohman3acb1872016-10-24 23:27:49 +0000107 assert(Desc.TSFlags == (WebAssemblyII::VariableOpIsImmediate |
108 WebAssemblyII::VariableOpImmediateIsLabel));
Dan Gohman4fc4e422016-10-24 19:49:43 +0000109 encodeULEB128(uint64_t(MO.getImm()), OS);
110 }
Dan Gohman1a427282016-01-12 03:32:29 +0000111 } else if (MO.isFPImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000112 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];
117 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
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());
121 support::endian::Writer<support::little>(OS).write<float>(f);
122 } else {
123 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
124 double d = MO.getFPImm();
125 support::endian::Writer<support::little>(OS).write<double>(d);
126 }
Dan Gohman1a427282016-01-12 03:32:29 +0000127 } else if (MO.isExpr()) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000128 const MCOperandInfo &Info = Desc.OpInfo[i];
129 llvm::MCFixupKind FixupKind;
Sam Clegg66a99e42017-09-15 20:34:47 +0000130 size_t PaddedSize = 5;
Dan Gohmand934cb82017-02-24 23:18:00 +0000131 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
132 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i32);
Dan Gohmand934cb82017-02-24 23:18:00 +0000133 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
134 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i64);
135 PaddedSize = 10;
136 } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 ||
137 Info.OperandType == WebAssembly::OPERAND_OFFSET32 ||
138 Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
139 FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000140 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
141 FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index);
Dan Gohmand934cb82017-02-24 23:18:00 +0000142 } else {
143 llvm_unreachable("unexpected symbolic operand kind");
144 }
Dan Gohman1a427282016-01-12 03:32:29 +0000145 Fixups.push_back(MCFixup::create(
Dan Gohman4fc4e422016-10-24 19:49:43 +0000146 OS.tell() - Start, MO.getExpr(),
Dan Gohmand934cb82017-02-24 23:18:00 +0000147 FixupKind, MI.getLoc()));
Dan Gohman1a427282016-01-12 03:32:29 +0000148 ++MCNumFixups;
Sam Clegg66a99e42017-09-15 20:34:47 +0000149 encodeULEB128(0, OS, PaddedSize);
Dan Gohman1a427282016-01-12 03:32:29 +0000150 } else {
151 llvm_unreachable("unexpected operand kind");
152 }
153 }
Dan Gohman05ac43f2015-12-17 01:39:00 +0000154
Dan Gohman1a427282016-01-12 03:32:29 +0000155 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Dan Gohman05ac43f2015-12-17 01:39:00 +0000156}
157
158#include "WebAssemblyGenMCCodeEmitter.inc"