Revert r252858: "[WebAssembly] Switch to MC for instruction printing."
[oota-llvm.git] / lib / Target / WebAssembly / InstPrinter / WebAssemblyInstPrinter.cpp
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"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include <cctype>
26 using namespace llvm;
27
28 #define DEBUG_TYPE "asm-printer"
29
30 #include "WebAssemblyGenAsmWriter.inc"
31
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 {
39   if (TargetRegisterInfo::isPhysicalRegister(RegNo))
40     OS << getRegisterName(RegNo);
41   else
42     OS << TargetRegisterInfo::virtReg2Index(RegNo);
43 }
44
45 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
46                                        StringRef Annot,
47                                        const MCSubtargetInfo &STI) {
48   printInstruction(MI, OS);
49   printAnnotation(OS, Annot);
50
51   unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
52   assert(NumDefs <= 1 &&
53          "Instructions with multiple result values not implemented");
54
55   if (NumDefs != 0) {
56     OS << "\n"
57           "\t" "set_local ";
58     printRegName(OS, MI->getOperand(0).getReg());
59     OS << ", pop";
60   }
61 }
62
63 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
64                                           raw_ostream &O) {
65   const MCOperand &Op = MI->getOperand(OpNo);
66   if (Op.isReg()) {
67     if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
68       O << "push";
69     else
70       printRegName(O, Op.getReg());
71   } else if (Op.isImm())
72     O << '#' << Op.getImm();
73   else if (Op.isFPImm())
74     O << '#' << Op.getFPImm();
75   else {
76     assert(Op.isExpr() && "unknown operand kind in printOperand");
77     Op.getExpr()->print(O, &MAI);
78   }
79 }