blob: 2baa135ebd921224b8769fb9643fd0e7e73a03d1 [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 Gohman05ac43f2015-12-17 01:39:00 +000039
Dan Gohman1a427282016-01-12 03:32:29 +000040 // Implementation generated by tablegen.
Dan Gohman05ac43f2015-12-17 01:39:00 +000041 uint64_t getBinaryCodeForInstr(const MCInst &MI,
42 SmallVectorImpl<MCFixup> &Fixups,
43 const MCSubtargetInfo &STI) const;
44
Dan Gohman05ac43f2015-12-17 01:39:00 +000045 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
46 SmallVectorImpl<MCFixup> &Fixups,
47 const MCSubtargetInfo &STI) const override;
Dan Gohman1a427282016-01-12 03:32:29 +000048
49public:
Sam Clegg9d24fb72017-06-16 23:59:10 +000050 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000051};
52} // end anonymous namespace
53
Sam Clegg9d24fb72017-06-16 23:59:10 +000054MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
55 return new WebAssemblyMCCodeEmitter(MCII);
Dan Gohman05ac43f2015-12-17 01:39:00 +000056}
57
58void WebAssemblyMCCodeEmitter::encodeInstruction(
59 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &STI) const {
Dan Gohman4fc4e422016-10-24 19:49:43 +000061 uint64_t Start = OS.tell();
62
63 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Dan Gohmancdd48b82017-11-28 01:13:40 +000064 if (Binary <= UINT8_MAX) {
65 OS << uint8_t(Binary);
66 } else {
67 assert(Binary <= UINT16_MAX && "Several-byte opcodes not supported yet");
68 OS << uint8_t(Binary >> 8)
69 << uint8_t(Binary);
70 }
Dan Gohman4fc4e422016-10-24 19:49:43 +000071
Dan Gohmand934cb82017-02-24 23:18:00 +000072 // For br_table instructions, encode the size of the table. In the MCInst,
73 // there's an index operand, one operand for each table entry, and the
74 // default operand.
75 if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
76 MI.getOpcode() == WebAssembly::BR_TABLE_I64)
77 encodeULEB128(MI.getNumOperands() - 2, OS);
78
Dan Gohman1a427282016-01-12 03:32:29 +000079 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Dan Gohman1a427282016-01-12 03:32:29 +000080 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
81 const MCOperand &MO = MI.getOperand(i);
82 if (MO.isReg()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000083 /* nothing to encode */
Dan Gohman1a427282016-01-12 03:32:29 +000084 } else if (MO.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +000085 if (i < Desc.getNumOperands()) {
86 assert(Desc.TSFlags == 0 &&
87 "WebAssembly non-variable_ops don't use TSFlags");
88 const MCOperandInfo &Info = Desc.OpInfo[i];
89 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
90 encodeSLEB128(int32_t(MO.getImm()), OS);
91 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
92 encodeSLEB128(int64_t(MO.getImm()), OS);
Dan Gohmand934cb82017-02-24 23:18:00 +000093 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000094 llvm_unreachable("wasm globals should only be accessed symbolicly");
Derek Schufff7a4f3d2017-04-17 20:28:28 +000095 } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
Heejin Ahn0c69a3e2018-03-02 20:52:59 +000096 OS << uint8_t(MO.getImm());
Dan Gohman3acb1872016-10-24 23:27:49 +000097 } else {
98 encodeULEB128(uint64_t(MO.getImm()), OS);
99 }
Dan Gohman4fc4e422016-10-24 19:49:43 +0000100 } else {
Dan Gohman3acb1872016-10-24 23:27:49 +0000101 assert(Desc.TSFlags == (WebAssemblyII::VariableOpIsImmediate |
102 WebAssemblyII::VariableOpImmediateIsLabel));
Dan Gohman4fc4e422016-10-24 19:49:43 +0000103 encodeULEB128(uint64_t(MO.getImm()), OS);
104 }
Dan Gohman1a427282016-01-12 03:32:29 +0000105 } else if (MO.isFPImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000106 assert(i < Desc.getNumOperands() &&
107 "Unexpected floating-point immediate as a non-fixed operand");
108 assert(Desc.TSFlags == 0 &&
109 "WebAssembly variable_ops floating point ops don't use TSFlags");
110 const MCOperandInfo &Info = Desc.OpInfo[i];
111 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
112 // TODO: MC converts all floating point immediate operands to double.
113 // This is fine for numeric values, but may cause NaNs to change bits.
114 float f = float(MO.getFPImm());
115 support::endian::Writer<support::little>(OS).write<float>(f);
116 } else {
117 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
118 double d = MO.getFPImm();
119 support::endian::Writer<support::little>(OS).write<double>(d);
120 }
Dan Gohman1a427282016-01-12 03:32:29 +0000121 } else if (MO.isExpr()) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000122 const MCOperandInfo &Info = Desc.OpInfo[i];
123 llvm::MCFixupKind FixupKind;
Sam Clegg66a99e42017-09-15 20:34:47 +0000124 size_t PaddedSize = 5;
Dan Gohmand934cb82017-02-24 23:18:00 +0000125 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
126 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i32);
Dan Gohmand934cb82017-02-24 23:18:00 +0000127 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
128 FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i64);
129 PaddedSize = 10;
130 } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 ||
131 Info.OperandType == WebAssembly::OPERAND_OFFSET32 ||
132 Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
133 FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000134 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
135 FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index);
Dan Gohmand934cb82017-02-24 23:18:00 +0000136 } else {
137 llvm_unreachable("unexpected symbolic operand kind");
138 }
Dan Gohman1a427282016-01-12 03:32:29 +0000139 Fixups.push_back(MCFixup::create(
Dan Gohman4fc4e422016-10-24 19:49:43 +0000140 OS.tell() - Start, MO.getExpr(),
Dan Gohmand934cb82017-02-24 23:18:00 +0000141 FixupKind, MI.getLoc()));
Dan Gohman1a427282016-01-12 03:32:29 +0000142 ++MCNumFixups;
Sam Clegg66a99e42017-09-15 20:34:47 +0000143 encodeULEB128(0, OS, PaddedSize);
Dan Gohman1a427282016-01-12 03:32:29 +0000144 } else {
145 llvm_unreachable("unexpected operand kind");
146 }
147 }
Dan Gohman05ac43f2015-12-17 01:39:00 +0000148
Dan Gohman1a427282016-01-12 03:32:29 +0000149 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Dan Gohman05ac43f2015-12-17 01:39:00 +0000150}
151
152#include "WebAssemblyGenMCCodeEmitter.inc"