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 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/WebAssemblyFixupKinds.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 18 | #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 Kleckner | 8f4bd1f | 2016-06-23 18:12:31 +0000 | [diff] [blame] | 26 | #include "llvm/Support/EndianStream.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LEB128.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | using namespace llvm; |
| 30 | |
| 31 | #define DEBUG_TYPE "mccodeemitter" |
| 32 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 33 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); |
| 34 | STATISTIC(MCNumFixups, "Number of MC fixups created."); |
| 35 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | class WebAssemblyMCCodeEmitter final : public MCCodeEmitter { |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 38 | const MCInstrInfo &MCII; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 39 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 40 | // Implementation generated by tablegen. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 41 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
| 42 | SmallVectorImpl<MCFixup> &Fixups, |
| 43 | const MCSubtargetInfo &STI) const; |
| 44 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 45 | void encodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 46 | SmallVectorImpl<MCFixup> &Fixups, |
| 47 | const MCSubtargetInfo &STI) const override; |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 50 | WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 51 | }; |
| 52 | } // end anonymous namespace |
| 53 | |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 54 | MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) { |
| 55 | return new WebAssemblyMCCodeEmitter(MCII); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void WebAssemblyMCCodeEmitter::encodeInstruction( |
| 59 | const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups, |
| 60 | const MCSubtargetInfo &STI) const { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 61 | uint64_t Start = OS.tell(); |
| 62 | |
| 63 | uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI); |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 64 | assert(Binary < UINT8_MAX && "Multi-byte opcodes not supported yet"); |
| 65 | OS << uint8_t(Binary); |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 66 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 67 | // For br_table instructions, encode the size of the table. In the MCInst, |
| 68 | // there's an index operand, one operand for each table entry, and the |
| 69 | // default operand. |
| 70 | if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 || |
| 71 | MI.getOpcode() == WebAssembly::BR_TABLE_I64) |
| 72 | encodeULEB128(MI.getNumOperands() - 2, OS); |
| 73 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 74 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 75 | for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) { |
| 76 | const MCOperand &MO = MI.getOperand(i); |
| 77 | if (MO.isReg()) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 78 | /* nothing to encode */ |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 79 | } else if (MO.isImm()) { |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 80 | if (i < Desc.getNumOperands()) { |
| 81 | assert(Desc.TSFlags == 0 && |
| 82 | "WebAssembly non-variable_ops don't use TSFlags"); |
| 83 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 84 | if (Info.OperandType == WebAssembly::OPERAND_I32IMM) { |
| 85 | encodeSLEB128(int32_t(MO.getImm()), OS); |
| 86 | } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) { |
| 87 | encodeSLEB128(int64_t(MO.getImm()), OS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 88 | } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) { |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 89 | llvm_unreachable("wasm globals should only be accessed symbolicly"); |
Derek Schuff | f7a4f3d | 2017-04-17 20:28:28 +0000 | [diff] [blame] | 90 | } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) { |
| 91 | encodeSLEB128(int64_t(MO.getImm()), OS); |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 92 | } else { |
| 93 | encodeULEB128(uint64_t(MO.getImm()), OS); |
| 94 | } |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 95 | } else { |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 96 | assert(Desc.TSFlags == (WebAssemblyII::VariableOpIsImmediate | |
| 97 | WebAssemblyII::VariableOpImmediateIsLabel)); |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 98 | encodeULEB128(uint64_t(MO.getImm()), OS); |
| 99 | } |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 100 | } else if (MO.isFPImm()) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 101 | assert(i < Desc.getNumOperands() && |
| 102 | "Unexpected floating-point immediate as a non-fixed operand"); |
| 103 | assert(Desc.TSFlags == 0 && |
| 104 | "WebAssembly variable_ops floating point ops don't use TSFlags"); |
| 105 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 106 | if (Info.OperandType == WebAssembly::OPERAND_F32IMM) { |
| 107 | // TODO: MC converts all floating point immediate operands to double. |
| 108 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 109 | float f = float(MO.getFPImm()); |
| 110 | support::endian::Writer<support::little>(OS).write<float>(f); |
| 111 | } else { |
| 112 | assert(Info.OperandType == WebAssembly::OPERAND_F64IMM); |
| 113 | double d = MO.getFPImm(); |
| 114 | support::endian::Writer<support::little>(OS).write<double>(d); |
| 115 | } |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 116 | } else if (MO.isExpr()) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 117 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 118 | llvm::MCFixupKind FixupKind; |
| 119 | size_t PaddedSize; |
| 120 | if (Info.OperandType == WebAssembly::OPERAND_I32IMM) { |
| 121 | FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i32); |
| 122 | PaddedSize = 5; |
| 123 | } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) { |
| 124 | FixupKind = MCFixupKind(WebAssembly::fixup_code_sleb128_i64); |
| 125 | PaddedSize = 10; |
| 126 | } else if (Info.OperandType == WebAssembly::OPERAND_FUNCTION32 || |
| 127 | Info.OperandType == WebAssembly::OPERAND_OFFSET32 || |
| 128 | Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { |
| 129 | FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32); |
| 130 | PaddedSize = 5; |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 131 | } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) { |
| 132 | FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index); |
| 133 | PaddedSize = 5; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 134 | } else { |
| 135 | llvm_unreachable("unexpected symbolic operand kind"); |
| 136 | } |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 137 | Fixups.push_back(MCFixup::create( |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 138 | OS.tell() - Start, MO.getExpr(), |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 139 | FixupKind, MI.getLoc())); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 140 | ++MCNumFixups; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 141 | encodeULEB128(0, OS, PaddedSize - 1); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 142 | } else { |
| 143 | llvm_unreachable("unexpected operand kind"); |
| 144 | } |
| 145 | } |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 146 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 147 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #include "WebAssemblyGenMCCodeEmitter.inc" |