Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=// |
| 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 Print MCInst instructions to wasm format. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "InstPrinter/WebAssemblyInstPrinter.h" |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 17 | #include "WebAssembly.h" |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 18 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCExpr.h" |
| 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include "llvm/MC/MCInstrInfo.h" |
| 22 | #include "llvm/MC/MCSubtargetInfo.h" |
| 23 | #include "llvm/MC/MCSymbol.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/FormattedStream.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "asm-printer" |
| 30 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 31 | #include "WebAssemblyGenAsmWriter.inc" |
| 32 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 33 | WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI, |
| 34 | const MCInstrInfo &MII, |
| 35 | const MCRegisterInfo &MRI) |
| 36 | : MCInstPrinter(MAI, MII, MRI) {} |
| 37 | |
| 38 | void WebAssemblyInstPrinter::printRegName(raw_ostream &OS, |
| 39 | unsigned RegNo) const { |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 40 | assert(RegNo != WebAssemblyFunctionInfo::UnusedReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 41 | // Note that there's an implicit get_local/set_local here! |
| 42 | OS << "$" << RegNo; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS, |
| 46 | StringRef Annot, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 47 | const MCSubtargetInfo & /*STI*/) { |
Dan Gohman | dd20c70 | 2015-12-21 16:50:41 +0000 | [diff] [blame] | 48 | // Print the instruction (this uses the AsmStrings from the .td files). |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 49 | printInstruction(MI, OS); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 50 | |
Dan Gohman | dd20c70 | 2015-12-21 16:50:41 +0000 | [diff] [blame] | 51 | // Print any additional variadic operands. |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 52 | const MCInstrDesc &Desc = MII.get(MI->getOpcode()); |
| 53 | if (Desc.isVariadic()) |
Dan Gohman | dd20c70 | 2015-12-21 16:50:41 +0000 | [diff] [blame] | 54 | for (auto i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; ++i) { |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 55 | if (i != 0) |
| 56 | OS << ", "; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 57 | printOperand(MI, i, OS); |
| 58 | } |
| 59 | |
Dan Gohman | dd20c70 | 2015-12-21 16:50:41 +0000 | [diff] [blame] | 60 | // Print any added annotation. |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 61 | printAnnotation(OS, Annot); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static std::string toString(const APFloat &FP) { |
| 65 | static const size_t BufBytes = 128; |
| 66 | char buf[BufBytes]; |
| 67 | if (FP.isNaN()) |
| 68 | assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) || |
| 69 | FP.bitwiseIsEqual( |
| 70 | APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) && |
| 71 | "convertToHexString handles neither SNaN nor NaN payloads"); |
| 72 | // Use C99's hexadecimal floating-point representation. |
| 73 | auto Written = FP.convertToHexString( |
| 74 | buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven); |
| 75 | (void)Written; |
| 76 | assert(Written != 0); |
| 77 | assert(Written < BufBytes); |
| 78 | return buf; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 79 | } |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 80 | |
| 81 | void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 82 | raw_ostream &O) { |
| 83 | const MCOperand &Op = MI->getOperand(OpNo); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 84 | if (Op.isReg()) { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 85 | assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() || |
| 86 | MII.get(MI->getOpcode()).TSFlags == 0) && |
| 87 | "WebAssembly variable_ops register ops don't use TSFlags"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 88 | unsigned WAReg = Op.getReg(); |
| 89 | if (int(WAReg) >= 0) |
| 90 | printRegName(O, WAReg); |
| 91 | else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs()) |
| 92 | O << "$pop" << (WAReg & INT32_MAX); |
| 93 | else if (WAReg != WebAssemblyFunctionInfo::UnusedReg) |
| 94 | O << "$push" << (WAReg & INT32_MAX); |
| 95 | else |
| 96 | O << "$discard"; |
Dan Gohman | 700515f | 2015-11-23 21:55:57 +0000 | [diff] [blame] | 97 | // Add a '=' suffix if this is a def. |
| 98 | if (OpNo < MII.get(MI->getOpcode()).getNumDefs()) |
| 99 | O << '='; |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 100 | } else if (Op.isImm()) { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 101 | assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() || |
| 102 | (MII.get(MI->getOpcode()).TSFlags & |
| 103 | WebAssemblyII::VariableOpIsImmediate)) && |
| 104 | "WebAssemblyII::VariableOpIsImmediate should be set for " |
| 105 | "variable_ops immediate ops"); |
| 106 | if (MII.get(MI->getOpcode()).TSFlags & |
| 107 | WebAssemblyII::VariableOpImmediateIsType) |
| 108 | // The immediates represent types. |
Dan Gohman | 5e0886b | 2015-12-06 19:42:29 +0000 | [diff] [blame] | 109 | O << WebAssembly::TypeToString(MVT::SimpleValueType(Op.getImm())); |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 110 | else |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 111 | O << Op.getImm(); |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 112 | } else if (Op.isFPImm()) { |
| 113 | assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() || |
| 114 | MII.get(MI->getOpcode()).TSFlags == 0) && |
| 115 | "WebAssembly variable_ops floating point ops don't use TSFlags"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 116 | O << toString(APFloat(Op.getFPImm())); |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 117 | } else { |
| 118 | assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() || |
| 119 | (MII.get(MI->getOpcode()).TSFlags & |
| 120 | WebAssemblyII::VariableOpIsImmediate)) && |
| 121 | "WebAssemblyII::VariableOpIsImmediate should be set for " |
| 122 | "variable_ops expr ops"); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 123 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
| 124 | Op.getExpr()->print(O, &MAI); |
| 125 | } |
| 126 | } |
Dan Gohman | 5e0886b | 2015-12-06 19:42:29 +0000 | [diff] [blame] | 127 | |
| 128 | const char *llvm::WebAssembly::TypeToString(MVT Ty) { |
| 129 | switch (Ty.SimpleTy) { |
| 130 | case MVT::i32: |
| 131 | return "i32"; |
| 132 | case MVT::i64: |
| 133 | return "i64"; |
| 134 | case MVT::f32: |
| 135 | return "f32"; |
| 136 | case MVT::f64: |
| 137 | return "f64"; |
| 138 | default: |
| 139 | llvm_unreachable("unsupported type"); |
| 140 | } |
| 141 | } |