blob: a53a7cd2ed6ec6bf5eab2f4a2a38ab082e9074ab [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"
Dan Gohman058fce52015-11-13 00:21:05 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman53828fd2015-11-23 16:50:18 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000019#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"
Dan Gohmane9361d52015-11-05 19:28:16 +000026#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000027#include <cctype>
28using namespace llvm;
29
30#define DEBUG_TYPE "asm-printer"
31
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "WebAssemblyGenAsmWriter.inc"
33
Dan Gohman10e730a2015-06-29 23:51:55 +000034WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
35 const MCInstrInfo &MII,
36 const MCRegisterInfo &MRI)
37 : MCInstPrinter(MAI, MII, MRI) {}
38
39void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
40 unsigned RegNo) const {
Dan Gohman058fce52015-11-13 00:21:05 +000041 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000042 // Note that there's an implicit get_local/set_local here!
43 OS << "$" << RegNo;
Dan Gohman10e730a2015-06-29 23:51:55 +000044}
45
46void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
47 StringRef Annot,
48 const MCSubtargetInfo &STI) {
JF Bastienb9073fb2015-07-22 21:28:15 +000049 printInstruction(MI, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000050
51 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
52 if (Desc.isVariadic())
53 for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
54 ++i) {
Dan Gohman53828fd2015-11-23 16:50:18 +000055 if (i != 0)
56 OS << ", ";
Dan Gohmancf4748f2015-11-12 17:04:33 +000057 printOperand(MI, i, OS);
58 }
59
JF Bastienb9073fb2015-07-22 21:28:15 +000060 printAnnotation(OS, Annot);
Dan Gohmancf4748f2015-11-12 17:04:33 +000061}
62
63static std::string toString(const APFloat &FP) {
64 static const size_t BufBytes = 128;
65 char buf[BufBytes];
66 if (FP.isNaN())
67 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
68 FP.bitwiseIsEqual(
69 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
70 "convertToHexString handles neither SNaN nor NaN payloads");
71 // Use C99's hexadecimal floating-point representation.
72 auto Written = FP.convertToHexString(
73 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
74 (void)Written;
75 assert(Written != 0);
76 assert(Written < BufBytes);
77 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +000078}
JF Bastienaf111db2015-08-24 22:16:48 +000079
80void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
81 raw_ostream &O) {
82 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +000083 if (Op.isReg()) {
Dan Gohman4ba48162015-11-18 16:12:01 +000084 unsigned WAReg = Op.getReg();
85 if (int(WAReg) >= 0)
86 printRegName(O, WAReg);
87 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
88 O << "$pop" << (WAReg & INT32_MAX);
89 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
90 O << "$push" << (WAReg & INT32_MAX);
91 else
92 O << "$discard";
Dan Gohman53828fd2015-11-23 16:50:18 +000093 } else if (Op.isImm()) {
94 switch (MI->getOpcode()) {
95 case WebAssembly::PARAM:
96 case WebAssembly::RESULT:
97 case WebAssembly::LOCAL:
98 switch (Op.getImm()) {
99 case MVT::i32: O << "i32"; break;
100 case MVT::i64: O << "i64"; break;
101 case MVT::f32: O << "f32"; break;
102 case MVT::f64: O << "f64"; break;
103 default: llvm_unreachable("unexpected type");
104 }
105 break;
106 default:
107 O << Op.getImm();
108 break;
109 }
110 } else if (Op.isFPImm())
Dan Gohmancf4748f2015-11-12 17:04:33 +0000111 O << toString(APFloat(Op.getFPImm()));
JF Bastienaf111db2015-08-24 22:16:48 +0000112 else {
113 assert(Op.isExpr() && "unknown operand kind in printOperand");
114 Op.getExpr()->print(O, &MAI);
115 }
116}