summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp
blob: d3c4861e1d60b8b202b6b07bf5a76fe395dc9459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Print MCInst instructions to wasm format.
///
//===----------------------------------------------------------------------===//

#include "InstPrinter/WebAssemblyInstPrinter.h"
#include "WebAssembly.h"
#include "WebAssemblyMachineFunctionInfo.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include <cctype>
using namespace llvm;

#define DEBUG_TYPE "asm-printer"

#include "WebAssemblyGenAsmWriter.inc"

WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
                                               const MCInstrInfo &MII,
                                               const MCRegisterInfo &MRI)
    : MCInstPrinter(MAI, MII, MRI) {}

void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
                                          unsigned RegNo) const {
  assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
  // 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");

  // FIXME: Revisit whether we actually print the set_local explicitly.
  if (NumDefs != 0) {
    unsigned WAReg = MI->getOperand(0).getReg();
    // Only print the set_local if the register is used.
    // TODO: Revisit this once the spec explains what should happen here.
    if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
      OS << "\n"
            "\t" "set_local " << WAReg << ", $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,
                                          raw_ostream &O) {
  const MCOperand &Op = MI->getOperand(OpNo);
  if (Op.isReg()) {
    if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
      printRegName(O, Op.getReg());
    else {
      if (Op.getReg() != WebAssemblyFunctionInfo::UnusedReg)
        O << "$push";
      else
        O << "$discard";
    }
  } else if (Op.isImm())
    O << Op.getImm();
  else if (Op.isFPImm())
    O << toString(APFloat(Op.getFPImm()));
  else {
    assert(Op.isExpr() && "unknown operand kind in printOperand");
    Op.getExpr()->print(O, &MAI);
  }
}
OpenPOWER on IntegriCloud