[WebAssembly] Use TSFlags instead of keeping a list of special-case opcodes.
[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 "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
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"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "asm-printer"
30
31 #include "WebAssemblyGenAsmWriter.inc"
32
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 {
40   assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
41   // Note that there's an implicit get_local/set_local here!
42   OS << "$" << RegNo;
43 }
44
45 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
46                                        StringRef Annot,
47                                        const MCSubtargetInfo & /*STI*/) {
48   // Print the instruction (this uses the AsmStrings from the .td files).
49   printInstruction(MI, OS);
50
51   // Print any additional variadic operands.
52   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
53   if (Desc.isVariadic())
54     for (auto i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; ++i) {
55       if (i != 0)
56         OS << ", ";
57       printOperand(MI, i, OS);
58     }
59
60   // Print any added annotation.
61   printAnnotation(OS, Annot);
62 }
63
64 static std::string toString(const APFloat &FP) {
65   static const size_t BufBytes = 128;
66   char buf[BufBytes];
67   if (FP.isNaN())
68     assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
69             FP.bitwiseIsEqual(
70                 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
71            "convertToHexString handles neither SNaN nor NaN payloads");
72   // Use C99's hexadecimal floating-point representation.
73   auto Written = FP.convertToHexString(
74       buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
75   (void)Written;
76   assert(Written != 0);
77   assert(Written < BufBytes);
78   return buf;
79 }
80
81 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
82                                           raw_ostream &O) {
83   const MCOperand &Op = MI->getOperand(OpNo);
84   if (Op.isReg()) {
85     assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
86             MII.get(MI->getOpcode()).TSFlags == 0) &&
87            "WebAssembly variable_ops register ops don't use TSFlags");
88     unsigned WAReg = Op.getReg();
89     if (int(WAReg) >= 0)
90       printRegName(O, WAReg);
91     else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
92       O << "$pop" << (WAReg & INT32_MAX);
93     else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
94       O << "$push" << (WAReg & INT32_MAX);
95     else
96       O << "$discard";
97     // Add a '=' suffix if this is a def.
98     if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
99       O << '=';
100   } else if (Op.isImm()) {
101     assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
102             (MII.get(MI->getOpcode()).TSFlags &
103                      WebAssemblyII::VariableOpIsImmediate)) &&
104            "WebAssemblyII::VariableOpIsImmediate should be set for "
105            "variable_ops immediate ops");
106     if (MII.get(MI->getOpcode()).TSFlags &
107         WebAssemblyII::VariableOpImmediateIsType)
108       // The immediates represent types.
109       O << WebAssembly::TypeToString(MVT::SimpleValueType(Op.getImm()));
110     else
111       O << Op.getImm();
112   } else if (Op.isFPImm()) {
113     assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
114             MII.get(MI->getOpcode()).TSFlags == 0) &&
115            "WebAssembly variable_ops floating point ops don't use TSFlags");
116     O << toString(APFloat(Op.getFPImm()));
117   } else {
118     assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
119             (MII.get(MI->getOpcode()).TSFlags &
120                      WebAssemblyII::VariableOpIsImmediate)) &&
121            "WebAssemblyII::VariableOpIsImmediate should be set for "
122            "variable_ops expr ops");
123     assert(Op.isExpr() && "unknown operand kind in printOperand");
124     Op.getExpr()->print(O, &MAI);
125   }
126 }
127
128 const char *llvm::WebAssembly::TypeToString(MVT Ty) {
129   switch (Ty.SimpleTy) {
130   case MVT::i32:
131     return "i32";
132   case MVT::i64:
133     return "i64";
134   case MVT::f32:
135     return "f32";
136   case MVT::f64:
137     return "f64";
138   default:
139     llvm_unreachable("unsupported type");
140   }
141 }