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