Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 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 | |
| 15 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 17 | #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 Kleckner | 8f4bd1f | 2016-06-23 18:12:31 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/EndianStream.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "mccodeemitter" |
| 30 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 31 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); |
| 32 | STATISTIC(MCNumFixups, "Number of MC fixups created."); |
| 33 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | class WebAssemblyMCCodeEmitter final : public MCCodeEmitter { |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 36 | const MCInstrInfo &MCII; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 37 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 38 | // Implementation generated by tablegen. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 39 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
| 40 | SmallVectorImpl<MCFixup> &Fixups, |
| 41 | const MCSubtargetInfo &STI) const; |
| 42 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 43 | void encodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 44 | SmallVectorImpl<MCFixup> &Fixups, |
| 45 | const MCSubtargetInfo &STI) const override; |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
Dan Gohman | 8394756 | 2016-01-20 05:54:22 +0000 | [diff] [blame] | 48 | WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 49 | }; |
| 50 | } // end anonymous namespace |
| 51 | |
Dan Gohman | cff7983 | 2016-01-19 21:31:41 +0000 | [diff] [blame] | 52 | MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) { |
| 53 | return new WebAssemblyMCCodeEmitter(MCII); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void WebAssemblyMCCodeEmitter::encodeInstruction( |
| 57 | const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups, |
| 58 | const MCSubtargetInfo &STI) const { |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 59 | // FIXME: This is not the real binary encoding. This is an extremely |
| 60 | // over-simplified encoding where we just use uint64_t for everything. This |
| 61 | // is a temporary measure. |
| 62 | support::endian::Writer<support::little>(OS).write<uint64_t>(MI.getOpcode()); |
| 63 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
| 64 | if (Desc.isVariadic()) |
| 65 | support::endian::Writer<support::little>(OS).write<uint64_t>( |
| 66 | MI.getNumOperands() - Desc.NumOperands); |
| 67 | for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) { |
| 68 | const MCOperand &MO = MI.getOperand(i); |
| 69 | if (MO.isReg()) { |
| 70 | support::endian::Writer<support::little>(OS).write<uint64_t>(MO.getReg()); |
| 71 | } else if (MO.isImm()) { |
| 72 | support::endian::Writer<support::little>(OS).write<uint64_t>(MO.getImm()); |
| 73 | } else if (MO.isFPImm()) { |
| 74 | support::endian::Writer<support::little>(OS).write<double>(MO.getFPImm()); |
| 75 | } else if (MO.isExpr()) { |
| 76 | support::endian::Writer<support::little>(OS).write<uint64_t>(0); |
| 77 | Fixups.push_back(MCFixup::create( |
| 78 | (1 + MCII.get(MI.getOpcode()).isVariadic() + i) * sizeof(uint64_t), |
Dan Gohman | 8394756 | 2016-01-20 05:54:22 +0000 | [diff] [blame] | 79 | MO.getExpr(), |
| 80 | STI.getTargetTriple().isArch64Bit() ? FK_Data_8 : FK_Data_4, |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 81 | MI.getLoc())); |
| 82 | ++MCNumFixups; |
| 83 | } else { |
| 84 | llvm_unreachable("unexpected operand kind"); |
| 85 | } |
| 86 | } |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 88 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #include "WebAssemblyGenMCCodeEmitter.inc" |