[WebAssembly] Reapply r252858, with svn add for the new file.
[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   // FIXME: Revisit whether we actually print the get_local explicitly.
40   OS << "(get_local " << RegNo << ")";
41 }
42
43 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
44                                        StringRef Annot,
45                                        const MCSubtargetInfo &STI) {
46   printInstruction(MI, OS);
47
48   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
49   if (Desc.isVariadic())
50     for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
51          ++i) {
52       OS << ", ";
53       printOperand(MI, i, OS);
54     }
55
56   printAnnotation(OS, Annot);
57
58   unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
59   assert(NumDefs <= 1 &&
60          "Instructions with multiple result values not implemented");
61
62   // FIXME: Revisit whether we actually print the set_local explicitly.
63   if (NumDefs != 0)
64     OS << "\n"
65           "\t" "set_local " << MI->getOperand(0).getReg() << ", $pop";
66 }
67
68 static std::string toString(const APFloat &FP) {
69   static const size_t BufBytes = 128;
70   char buf[BufBytes];
71   if (FP.isNaN())
72     assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
73             FP.bitwiseIsEqual(
74                 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
75            "convertToHexString handles neither SNaN nor NaN payloads");
76   // Use C99's hexadecimal floating-point representation.
77   auto Written = FP.convertToHexString(
78       buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
79   (void)Written;
80   assert(Written != 0);
81   assert(Written < BufBytes);
82   return buf;
83 }
84
85 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
86                                           raw_ostream &O) {
87   const MCOperand &Op = MI->getOperand(OpNo);
88   if (Op.isReg()) {
89     if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
90       O << "$push";
91     else
92       printRegName(O, Op.getReg());
93   } else if (Op.isImm())
94     O << Op.getImm();
95   else if (Op.isFPImm())
96     O << toString(APFloat(Op.getFPImm()));
97   else {
98     assert(Op.isExpr() && "unknown operand kind in printOperand");
99     Op.getExpr()->print(O, &MAI);
100   }
101 }