blob: ce832bee2af908fe3d81237263f75d1a1733726c [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
15#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman1a427282016-01-12 03:32:29 +000016#include "llvm/ADT/STLExtras.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCFixup.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
Reid Kleckner8f4bd1f2016-06-23 18:12:31 +000025#include "llvm/Support/EndianStream.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000026#include "llvm/Support/LEB128.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000027#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "mccodeemitter"
31
Dan Gohman1a427282016-01-12 03:32:29 +000032STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
33STATISTIC(MCNumFixups, "Number of MC fixups created.");
34
Dan Gohman05ac43f2015-12-17 01:39:00 +000035namespace {
36class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
Dan Gohman1a427282016-01-12 03:32:29 +000037 const MCInstrInfo &MCII;
Dan Gohmandf4f4d42017-02-10 00:14:42 +000038 MCContext &Ctx;
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:
Dan Gohmandf4f4d42017-02-10 00:14:42 +000050 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
51 : MCII(mcii), Ctx(ctx) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000052};
53} // end anonymous namespace
54
Dan Gohmandf4f4d42017-02-10 00:14:42 +000055MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
56 MCContext &Ctx) {
57 return new WebAssemblyMCCodeEmitter(MCII, Ctx);
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 Gohman3acb1872016-10-24 23:27:49 +000066 assert(Binary < UINT8_MAX && "Multi-byte opcodes not supported yet");
67 OS << uint8_t(Binary);
Dan Gohman4fc4e422016-10-24 19:49:43 +000068
Dan Gohman1a427282016-01-12 03:32:29 +000069 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Dan Gohman1a427282016-01-12 03:32:29 +000070 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
71 const MCOperand &MO = MI.getOperand(i);
72 if (MO.isReg()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000073 /* nothing to encode */
Dan Gohman1a427282016-01-12 03:32:29 +000074 } else if (MO.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +000075 if (i < Desc.getNumOperands()) {
76 assert(Desc.TSFlags == 0 &&
77 "WebAssembly non-variable_ops don't use TSFlags");
78 const MCOperandInfo &Info = Desc.OpInfo[i];
79 if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
80 encodeSLEB128(int32_t(MO.getImm()), OS);
81 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
82 encodeSLEB128(int64_t(MO.getImm()), OS);
83 } else {
84 encodeULEB128(uint64_t(MO.getImm()), OS);
85 }
Dan Gohman4fc4e422016-10-24 19:49:43 +000086 } else {
Dan Gohman3acb1872016-10-24 23:27:49 +000087 assert(Desc.TSFlags == (WebAssemblyII::VariableOpIsImmediate |
88 WebAssemblyII::VariableOpImmediateIsLabel));
Dan Gohman4fc4e422016-10-24 19:49:43 +000089 encodeULEB128(uint64_t(MO.getImm()), OS);
90 }
Dan Gohman1a427282016-01-12 03:32:29 +000091 } else if (MO.isFPImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000092 assert(i < Desc.getNumOperands() &&
93 "Unexpected floating-point immediate as a non-fixed operand");
94 assert(Desc.TSFlags == 0 &&
95 "WebAssembly variable_ops floating point ops don't use TSFlags");
96 const MCOperandInfo &Info = Desc.OpInfo[i];
97 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
98 // TODO: MC converts all floating point immediate operands to double.
99 // This is fine for numeric values, but may cause NaNs to change bits.
100 float f = float(MO.getFPImm());
101 support::endian::Writer<support::little>(OS).write<float>(f);
102 } else {
103 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
104 double d = MO.getFPImm();
105 support::endian::Writer<support::little>(OS).write<double>(d);
106 }
Dan Gohman1a427282016-01-12 03:32:29 +0000107 } else if (MO.isExpr()) {
Dan Gohman1a427282016-01-12 03:32:29 +0000108 Fixups.push_back(MCFixup::create(
Dan Gohman4fc4e422016-10-24 19:49:43 +0000109 OS.tell() - Start, MO.getExpr(),
Dan Gohman83947562016-01-20 05:54:22 +0000110 STI.getTargetTriple().isArch64Bit() ? FK_Data_8 : FK_Data_4,
Dan Gohman1a427282016-01-12 03:32:29 +0000111 MI.getLoc()));
112 ++MCNumFixups;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000113 encodeULEB128(STI.getTargetTriple().isArch64Bit() ? UINT64_MAX
114 : uint64_t(UINT32_MAX),
115 OS);
Dan Gohman1a427282016-01-12 03:32:29 +0000116 } else {
117 llvm_unreachable("unexpected operand kind");
118 }
119 }
Dan Gohman05ac43f2015-12-17 01:39:00 +0000120
Dan Gohman1a427282016-01-12 03:32:29 +0000121 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Dan Gohman05ac43f2015-12-17 01:39:00 +0000122}
123
124#include "WebAssemblyGenMCCodeEmitter.inc"