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" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCInst.h" |
| 19 | #include "llvm/MC/MCInstrInfo.h" |
| 20 | #include "llvm/MC/MCSubtargetInfo.h" |
| 21 | #include "llvm/MC/MCSymbol.h" |
| 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/FormattedStream.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 25 | #include <cctype> |
| 26 | using namespace llvm; |
| 27 | |
| 28 | #define DEBUG_TYPE "asm-printer" |
| 29 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 30 | #include "WebAssemblyGenAsmWriter.inc" |
| 31 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 32 | WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI, |
| 33 | const MCInstrInfo &MII, |
| 34 | const MCRegisterInfo &MRI) |
| 35 | : MCInstPrinter(MAI, MII, MRI) {} |
| 36 | |
| 37 | void WebAssemblyInstPrinter::printRegName(raw_ostream &OS, |
| 38 | unsigned RegNo) const { |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 39 | if (TargetRegisterInfo::isPhysicalRegister(RegNo)) |
| 40 | OS << getRegisterName(RegNo); |
| 41 | else |
| 42 | OS << TargetRegisterInfo::virtReg2Index(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); |
| 49 | printAnnotation(OS, Annot); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 50 | |
| 51 | unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs(); |
| 52 | assert(NumDefs <= 1 && |
| 53 | "Instructions with multiple result values not implemented"); |
| 54 | |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 55 | if (NumDefs != 0) { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 56 | OS << "\n" |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 57 | "\t" "set_local "; |
| 58 | printRegName(OS, MI->getOperand(0).getReg()); |
| 59 | OS << ", pop"; |
| 60 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 61 | } |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 62 | |
| 63 | void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 64 | raw_ostream &O) { |
| 65 | const MCOperand &Op = MI->getOperand(OpNo); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 66 | if (Op.isReg()) { |
| 67 | if (OpNo < MII.get(MI->getOpcode()).getNumDefs()) |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 68 | O << "push"; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 69 | else |
| 70 | printRegName(O, Op.getReg()); |
| 71 | } else if (Op.isImm()) |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 72 | O << '#' << Op.getImm(); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 73 | else if (Op.isFPImm()) |
Hans Wennborg | 7384a2d | 2015-11-12 14:37:56 +0000 | [diff] [blame] | 74 | O << '#' << Op.getFPImm(); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 75 | else { |
| 76 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
| 77 | Op.getExpr()->print(O, &MAI); |
| 78 | } |
| 79 | } |