blob: d3c4861e1d60b8b202b6b07bf5a76fe395dc9459 [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 Gohman10e730a2015-06-29 23:51:55 +000018#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/FormattedStream.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000025#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000026#include <cctype>
27using namespace llvm;
28
29#define DEBUG_TYPE "asm-printer"
30
JF Bastienb9073fb2015-07-22 21:28:15 +000031#include "WebAssemblyGenAsmWriter.inc"
32
Dan Gohman10e730a2015-06-29 23:51:55 +000033WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
34 const MCInstrInfo &MII,
35 const MCRegisterInfo &MRI)
36 : MCInstPrinter(MAI, MII, MRI) {}
37
38void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
39 unsigned RegNo) const {
Dan Gohman058fce52015-11-13 00:21:05 +000040 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohmancf4748f2015-11-12 17:04:33 +000041 // FIXME: Revisit whether we actually print the get_local explicitly.
42 OS << "(get_local " << RegNo << ")";
Dan Gohman10e730a2015-06-29 23:51:55 +000043}
44
45void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
46 StringRef Annot,
47 const MCSubtargetInfo &STI) {
JF Bastienb9073fb2015-07-22 21:28:15 +000048 printInstruction(MI, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000049
50 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
51 if (Desc.isVariadic())
52 for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
53 ++i) {
54 OS << ", ";
55 printOperand(MI, i, OS);
56 }
57
JF Bastienb9073fb2015-07-22 21:28:15 +000058 printAnnotation(OS, Annot);
Dan Gohmane9361d52015-11-05 19:28:16 +000059
60 unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
61 assert(NumDefs <= 1 &&
62 "Instructions with multiple result values not implemented");
63
Dan Gohmancf4748f2015-11-12 17:04:33 +000064 // FIXME: Revisit whether we actually print the set_local explicitly.
Dan Gohman058fce52015-11-13 00:21:05 +000065 if (NumDefs != 0) {
66 unsigned WAReg = MI->getOperand(0).getReg();
67 // Only print the set_local if the register is used.
68 // TODO: Revisit this once the spec explains what should happen here.
69 if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
70 OS << "\n"
71 "\t" "set_local " << WAReg << ", $pop";
72 }
Dan Gohmancf4748f2015-11-12 17:04:33 +000073}
74
75static std::string toString(const APFloat &FP) {
76 static const size_t BufBytes = 128;
77 char buf[BufBytes];
78 if (FP.isNaN())
79 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
80 FP.bitwiseIsEqual(
81 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
82 "convertToHexString handles neither SNaN nor NaN payloads");
83 // Use C99's hexadecimal floating-point representation.
84 auto Written = FP.convertToHexString(
85 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
86 (void)Written;
87 assert(Written != 0);
88 assert(Written < BufBytes);
89 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +000090}
JF Bastienaf111db2015-08-24 22:16:48 +000091
92void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
93 raw_ostream &O) {
94 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +000095 if (Op.isReg()) {
Dan Gohman058fce52015-11-13 00:21:05 +000096 if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmane9361d52015-11-05 19:28:16 +000097 printRegName(O, Op.getReg());
Dan Gohman058fce52015-11-13 00:21:05 +000098 else {
99 if (Op.getReg() != WebAssemblyFunctionInfo::UnusedReg)
100 O << "$push";
101 else
102 O << "$discard";
103 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000104 } else if (Op.isImm())
Dan Gohmancf4748f2015-11-12 17:04:33 +0000105 O << Op.getImm();
Dan Gohmane9361d52015-11-05 19:28:16 +0000106 else if (Op.isFPImm())
Dan Gohmancf4748f2015-11-12 17:04:33 +0000107 O << toString(APFloat(Op.getFPImm()));
JF Bastienaf111db2015-08-24 22:16:48 +0000108 else {
109 assert(Op.isExpr() && "unknown operand kind in printOperand");
110 Op.getExpr()->print(O, &MAI);
111 }
112}