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