[WebAssembly] Reapply r252858, with svn add for the new file.
[oota-llvm.git] / lib / Target / WebAssembly / InstPrinter / WebAssemblyInstPrinter.cpp
index bb10f20fd6d069814d2fdfeaee58ed611306fbbf..cc77f56dce351191dcc835d4dbd13c20e45cd914 100644 (file)
@@ -36,28 +36,50 @@ WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
 
 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
                                           unsigned RegNo) const {
-  if (TargetRegisterInfo::isPhysicalRegister(RegNo))
-    OS << getRegisterName(RegNo);
-  else
-    OS << TargetRegisterInfo::virtReg2Index(RegNo);
+  // FIXME: Revisit whether we actually print the get_local explicitly.
+  OS << "(get_local " << RegNo << ")";
 }
 
 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
                                        StringRef Annot,
                                        const MCSubtargetInfo &STI) {
   printInstruction(MI, OS);
+
+  const MCInstrDesc &Desc = MII.get(MI->getOpcode());
+  if (Desc.isVariadic())
+    for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
+         ++i) {
+      OS << ", ";
+      printOperand(MI, i, OS);
+    }
+
   printAnnotation(OS, Annot);
 
   unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
   assert(NumDefs <= 1 &&
          "Instructions with multiple result values not implemented");
 
-  if (NumDefs != 0) {
+  // FIXME: Revisit whether we actually print the set_local explicitly.
+  if (NumDefs != 0)
     OS << "\n"
-          "\t" "set_local ";
-    printRegName(OS, MI->getOperand(0).getReg());
-    OS << ", pop";
-  }
+          "\t" "set_local " << MI->getOperand(0).getReg() << ", $pop";
+}
+
+static std::string toString(const APFloat &FP) {
+  static const size_t BufBytes = 128;
+  char buf[BufBytes];
+  if (FP.isNaN())
+    assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
+            FP.bitwiseIsEqual(
+                APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
+           "convertToHexString handles neither SNaN nor NaN payloads");
+  // Use C99's hexadecimal floating-point representation.
+  auto Written = FP.convertToHexString(
+      buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
+  (void)Written;
+  assert(Written != 0);
+  assert(Written < BufBytes);
+  return buf;
 }
 
 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
@@ -65,13 +87,13 @@ void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
-      O << "push";
+      O << "$push";
     else
       printRegName(O, Op.getReg());
   } else if (Op.isImm())
-    O << '#' << Op.getImm();
+    O << Op.getImm();
   else if (Op.isFPImm())
-    O << '#' << Op.getFPImm();
+    O << toString(APFloat(Op.getFPImm()));
   else {
     assert(Op.isExpr() && "unknown operand kind in printOperand");
     Op.getExpr()->print(O, &MAI);