blob: cc77f56dce351191dcc835d4dbd13c20e45cd914 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- 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 Gohmane9361d52015-11-05 19:28:16 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000025#include <cctype>
26using namespace llvm;
27
28#define DEBUG_TYPE "asm-printer"
29
JF Bastienb9073fb2015-07-22 21:28:15 +000030#include "WebAssemblyGenAsmWriter.inc"
31
Dan Gohman10e730a2015-06-29 23:51:55 +000032WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
33 const MCInstrInfo &MII,
34 const MCRegisterInfo &MRI)
35 : MCInstPrinter(MAI, MII, MRI) {}
36
37void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
Dan Gohmancf4748f2015-11-12 17:04:33 +000039 // FIXME: Revisit whether we actually print the get_local explicitly.
40 OS << "(get_local " << RegNo << ")";
Dan Gohman10e730a2015-06-29 23:51:55 +000041}
42
43void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
44 StringRef Annot,
45 const MCSubtargetInfo &STI) {
JF Bastienb9073fb2015-07-22 21:28:15 +000046 printInstruction(MI, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000047
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
JF Bastienb9073fb2015-07-22 21:28:15 +000056 printAnnotation(OS, Annot);
Dan Gohmane9361d52015-11-05 19:28:16 +000057
58 unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
59 assert(NumDefs <= 1 &&
60 "Instructions with multiple result values not implemented");
61
Dan Gohmancf4748f2015-11-12 17:04:33 +000062 // FIXME: Revisit whether we actually print the set_local explicitly.
63 if (NumDefs != 0)
Dan Gohmane9361d52015-11-05 19:28:16 +000064 OS << "\n"
Dan Gohmancf4748f2015-11-12 17:04:33 +000065 "\t" "set_local " << MI->getOperand(0).getReg() << ", $pop";
66}
67
68static 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;
Dan Gohman10e730a2015-06-29 23:51:55 +000083}
JF Bastienaf111db2015-08-24 22:16:48 +000084
85void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
86 raw_ostream &O) {
87 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +000088 if (Op.isReg()) {
89 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmancf4748f2015-11-12 17:04:33 +000090 O << "$push";
Dan Gohmane9361d52015-11-05 19:28:16 +000091 else
92 printRegName(O, Op.getReg());
93 } else if (Op.isImm())
Dan Gohmancf4748f2015-11-12 17:04:33 +000094 O << Op.getImm();
Dan Gohmane9361d52015-11-05 19:28:16 +000095 else if (Op.isFPImm())
Dan Gohmancf4748f2015-11-12 17:04:33 +000096 O << toString(APFloat(Op.getFPImm()));
JF Bastienaf111db2015-08-24 22:16:48 +000097 else {
98 assert(Op.isExpr() && "unknown operand kind in printOperand");
99 Op.getExpr()->print(O, &MAI);
100 }
101}