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 | #include <cctype> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | #define DEBUG_TYPE "asm-printer" |
| 31 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 32 | #include "WebAssemblyGenAsmWriter.inc" |
| 33 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 34 | WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI, |
| 35 | const MCInstrInfo &MII, |
| 36 | const MCRegisterInfo &MRI) |
| 37 | : MCInstPrinter(MAI, MII, MRI) {} |
| 38 | |
| 39 | void WebAssemblyInstPrinter::printRegName(raw_ostream &OS, |
| 40 | unsigned RegNo) const { |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 41 | assert(RegNo != WebAssemblyFunctionInfo::UnusedReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 42 | // Note that there's an implicit get_local/set_local here! |
| 43 | OS << "$" << RegNo; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS, |
| 47 | StringRef Annot, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame^] | 48 | const MCSubtargetInfo & /*STI*/) { |
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 | |
| 51 | const MCInstrDesc &Desc = MII.get(MI->getOpcode()); |
| 52 | if (Desc.isVariadic()) |
| 53 | for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; |
| 54 | ++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 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 60 | printAnnotation(OS, Annot); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static std::string toString(const APFloat &FP) { |
| 64 | static const size_t BufBytes = 128; |
| 65 | char buf[BufBytes]; |
| 66 | if (FP.isNaN()) |
| 67 | assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) || |
| 68 | FP.bitwiseIsEqual( |
| 69 | APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) && |
| 70 | "convertToHexString handles neither SNaN nor NaN payloads"); |
| 71 | // Use C99's hexadecimal floating-point representation. |
| 72 | auto Written = FP.convertToHexString( |
| 73 | buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven); |
| 74 | (void)Written; |
| 75 | assert(Written != 0); |
| 76 | assert(Written < BufBytes); |
| 77 | return buf; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 78 | } |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 79 | |
| 80 | void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 81 | raw_ostream &O) { |
| 82 | const MCOperand &Op = MI->getOperand(OpNo); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 83 | if (Op.isReg()) { |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 84 | unsigned WAReg = Op.getReg(); |
| 85 | if (int(WAReg) >= 0) |
| 86 | printRegName(O, WAReg); |
| 87 | else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs()) |
| 88 | O << "$pop" << (WAReg & INT32_MAX); |
| 89 | else if (WAReg != WebAssemblyFunctionInfo::UnusedReg) |
| 90 | O << "$push" << (WAReg & INT32_MAX); |
| 91 | else |
| 92 | O << "$discard"; |
Dan Gohman | 700515f | 2015-11-23 21:55:57 +0000 | [diff] [blame] | 93 | // Add a '=' suffix if this is a def. |
| 94 | if (OpNo < MII.get(MI->getOpcode()).getNumDefs()) |
| 95 | O << '='; |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 96 | } else if (Op.isImm()) { |
| 97 | switch (MI->getOpcode()) { |
| 98 | case WebAssembly::PARAM: |
| 99 | case WebAssembly::RESULT: |
| 100 | case WebAssembly::LOCAL: |
| 101 | switch (Op.getImm()) { |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame^] | 102 | case MVT::i32: |
| 103 | O << "i32"; |
| 104 | break; |
| 105 | case MVT::i64: |
| 106 | O << "i64"; |
| 107 | break; |
| 108 | case MVT::f32: |
| 109 | O << "f32"; |
| 110 | break; |
| 111 | case MVT::f64: |
| 112 | O << "f64"; |
| 113 | break; |
| 114 | default: |
| 115 | llvm_unreachable("unexpected type"); |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 116 | } |
| 117 | break; |
| 118 | default: |
| 119 | O << Op.getImm(); |
| 120 | break; |
| 121 | } |
| 122 | } else if (Op.isFPImm()) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 123 | O << toString(APFloat(Op.getFPImm())); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 124 | else { |
| 125 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
| 126 | Op.getExpr()->print(O, &MAI); |
| 127 | } |
| 128 | } |