065ef87fb35a1e9f5638f4a6b2948a240c771e2a
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyAsmPrinter.cpp
1 //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
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 This file contains a printer that converts from our internal
12 /// representation of machine-dependent LLVM code to the WebAssembly assembly
13 /// language.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyRegisterInfo.h"
20 #include "WebAssemblySubtarget.h"
21 #include "InstPrinter/WebAssemblyInstPrinter.h"
22 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
23
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "asm-printer"
37
38 namespace {
39
40 class WebAssemblyAsmPrinter final : public AsmPrinter {
41   const WebAssemblyInstrInfo *TII;
42
43 public:
44   WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45       : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
46
47 private:
48   const char *getPassName() const override {
49     return "WebAssembly Assembly Printer";
50   }
51
52   //===------------------------------------------------------------------===//
53   // MachineFunctionPass Implementation.
54   //===------------------------------------------------------------------===//
55
56   void getAnalysisUsage(AnalysisUsage &AU) const override {
57     AsmPrinter::getAnalysisUsage(AU);
58   }
59
60   bool runOnMachineFunction(MachineFunction &MF) override {
61     TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
62     return AsmPrinter::runOnMachineFunction(MF);
63   }
64
65   //===------------------------------------------------------------------===//
66   // AsmPrinter Implementation.
67   //===------------------------------------------------------------------===//
68
69   void EmitInstruction(const MachineInstr *MI) override;
70 };
71
72 } // end anonymous namespace
73
74 //===----------------------------------------------------------------------===//
75
76 // Untyped, lower-case version of the opcode's name matching the names
77 // WebAssembly opcodes are expected to have. The tablegen names are uppercase
78 // and suffixed with their type (after an underscore).
79 static SmallString<32> Name(const WebAssemblyInstrInfo *TII,
80                             const MachineInstr *MI) {
81   std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
82   std::string::size_type End = N.rfind('_');
83   End = std::string::npos == End ? N.length() : End;
84   return SmallString<32>(&N[0], &N[End]);
85 }
86
87 static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
88
89 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
90   DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
91   SmallString<128> Str;
92   raw_svector_ostream OS(Str);
93
94   unsigned NumDefs = MI->getDesc().getNumDefs();
95   assert(NumDefs <= 1 &&
96          "Instructions with multiple result values not implemented");
97
98   OS << '\t';
99
100   if (NumDefs != 0) {
101     const MachineOperand &MO = MI->getOperand(0);
102     unsigned Reg = MO.getReg();
103     OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
104   }
105
106   OS << '(' << Name(TII, MI);
107   for (const MachineOperand &MO : MI->uses())
108     switch (MO.getType()) {
109     default:
110       llvm_unreachable("unexpected machine operand type");
111     case MachineOperand::MO_Register: {
112       if (MO.isImplicit())
113         continue;
114       unsigned Reg = MO.getReg();
115       OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
116     } break;
117     case MachineOperand::MO_Immediate: {
118       OS << ' ' << MO.getImm();
119     } break;
120     case MachineOperand::MO_FPImmediate: {
121       static const size_t BufBytes = 128;
122       char buf[BufBytes];
123       APFloat FP = MO.getFPImm()->getValueAPF();
124       if (FP.isNaN())
125         assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
126                 FP.bitwiseIsEqual(
127                     APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
128                "convertToHexString handles neither SNaN nor NaN payloads");
129       // Use C99's hexadecimal floating-point representation.
130       auto Written =
131           FP.convertToHexString(buf, /*hexDigits=*/0, /*upperCase=*/false,
132                                 APFloat::rmNearestTiesToEven);
133       (void)Written;
134       assert(Written != 0);
135       assert(Written < BufBytes);
136       OS << ' ' << buf;
137     } break;
138     case MachineOperand::MO_GlobalAddress: {
139       OS << ' ' << toSymbol(MO.getGlobal()->getName());
140     } break;
141     }
142   OS << ')';
143
144   if (NumDefs != 0)
145     OS << ')';
146
147   OS << '\n';
148
149   OutStreamer->EmitRawText(OS.str());
150 }
151
152 // Force static initialization.
153 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
154   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
155   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
156 }