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" |
| 16 | #include "WebAssembly.h" |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 17 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
| 19 | #include "llvm/MC/MCInst.h" |
| 20 | #include "llvm/MC/MCInstrInfo.h" |
| 21 | #include "llvm/MC/MCSubtargetInfo.h" |
| 22 | #include "llvm/MC/MCSymbol.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/FormattedStream.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 26 | #include <cctype> |
| 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, |
| 47 | const MCSubtargetInfo &STI) { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 48 | printInstruction(MI, OS); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 49 | |
| 50 | const MCInstrDesc &Desc = MII.get(MI->getOpcode()); |
| 51 | if (Desc.isVariadic()) |
| 52 | for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; |
| 53 | ++i) { |
| 54 | OS << ", "; |
| 55 | printOperand(MI, i, OS); |
| 56 | } |
| 57 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 58 | printAnnotation(OS, Annot); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static std::string toString(const APFloat &FP) { |
| 62 | static const size_t BufBytes = 128; |
| 63 | char buf[BufBytes]; |
| 64 | if (FP.isNaN()) |
| 65 | assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) || |
| 66 | FP.bitwiseIsEqual( |
| 67 | APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) && |
| 68 | "convertToHexString handles neither SNaN nor NaN payloads"); |
| 69 | // Use C99's hexadecimal floating-point representation. |
| 70 | auto Written = FP.convertToHexString( |
| 71 | buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven); |
| 72 | (void)Written; |
| 73 | assert(Written != 0); |
| 74 | assert(Written < BufBytes); |
| 75 | return buf; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 76 | } |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 77 | |
| 78 | void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 79 | raw_ostream &O) { |
| 80 | const MCOperand &Op = MI->getOperand(OpNo); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 81 | if (Op.isReg()) { |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame^] | 82 | unsigned WAReg = Op.getReg(); |
| 83 | if (int(WAReg) >= 0) |
| 84 | printRegName(O, WAReg); |
| 85 | else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs()) |
| 86 | O << "$pop" << (WAReg & INT32_MAX); |
| 87 | else if (WAReg != WebAssemblyFunctionInfo::UnusedReg) |
| 88 | O << "$push" << (WAReg & INT32_MAX); |
| 89 | else |
| 90 | O << "$discard"; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 91 | } else if (Op.isImm()) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 92 | O << Op.getImm(); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 93 | else if (Op.isFPImm()) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 94 | O << toString(APFloat(Op.getFPImm())); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 95 | else { |
| 96 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
| 97 | Op.getExpr()->print(O, &MAI); |
| 98 | } |
| 99 | } |