blob: aa7734118a60cfd3d6fa7781841cb9597345336b [file] [log] [blame]
JF Bastienb9073fb2015-07-22 21:28:15 +00001//===-- 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"
JF Bastien54be3b12015-08-25 23:19:49 +000026#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000027#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DebugInfo.h"
30#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000031#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Support/raw_ostream.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "asm-printer"
39
40namespace {
41
42class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien600aee92015-07-31 17:53:38 +000043 const WebAssemblyInstrInfo *TII;
44
JF Bastienb9073fb2015-07-22 21:28:15 +000045public:
46 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
JF Bastien600aee92015-07-31 17:53:38 +000047 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000048
49private:
50 const char *getPassName() const override {
51 return "WebAssembly Assembly Printer";
52 }
53
54 //===------------------------------------------------------------------===//
55 // MachineFunctionPass Implementation.
56 //===------------------------------------------------------------------===//
57
58 void getAnalysisUsage(AnalysisUsage &AU) const override {
59 AsmPrinter::getAnalysisUsage(AU);
60 }
61
JF Bastien600aee92015-07-31 17:53:38 +000062 bool runOnMachineFunction(MachineFunction &MF) override {
Dan Gohmane419a7c2015-08-24 16:46:31 +000063 TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
JF Bastien600aee92015-07-31 17:53:38 +000064 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000065 }
66
67 //===------------------------------------------------------------------===//
68 // AsmPrinter Implementation.
69 //===------------------------------------------------------------------===//
70
JF Bastien54be3b12015-08-25 23:19:49 +000071 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000072 void EmitFunctionEntryLabel() override;
73 void EmitFunctionBodyStart() override;
74 void EmitFunctionBodyEnd() override;
75
JF Bastienb9073fb2015-07-22 21:28:15 +000076 void EmitInstruction(const MachineInstr *MI) override;
77};
78
79} // end anonymous namespace
80
81//===----------------------------------------------------------------------===//
82
JF Bastien315cc062015-08-07 01:57:03 +000083// Untyped, lower-case version of the opcode's name matching the names
84// WebAssembly opcodes are expected to have. The tablegen names are uppercase
85// and suffixed with their type (after an underscore).
86static SmallString<32> Name(const WebAssemblyInstrInfo *TII,
87 const MachineInstr *MI) {
88 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
Dan Gohman69c4c762015-08-24 21:03:24 +000089 std::string::size_type End = N.rfind('_');
JF Bastien315cc062015-08-07 01:57:03 +000090 End = std::string::npos == End ? N.length() : End;
91 return SmallString<32>(&N[0], &N[End]);
92}
93
JF Bastienaf111db2015-08-24 22:16:48 +000094static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
95
JF Bastienb6091df2015-08-25 22:58:05 +000096static const char *toType(const Type *Ty) {
97 switch (Ty->getTypeID()) {
98 default: break;
99 case Type::FloatTyID: return "f32";
100 case Type::DoubleTyID: return "f64";
101 case Type::IntegerTyID:
102 switch (Ty->getIntegerBitWidth()) {
103 case 32: return "i32";
104 case 64: return "i64";
105 default: break;
106 }
107 }
108 DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
109 llvm_unreachable("invalid type");
110 return "<invalid>";
111}
112
JF Bastien54be3b12015-08-25 23:19:49 +0000113void WebAssemblyAsmPrinter::EmitConstantPool() {
114 assert(MF->getConstantPool()->getConstants().empty() &&
115 "WebAssembly disables constant pools");
116}
117
JF Bastienb6091df2015-08-25 22:58:05 +0000118void WebAssemblyAsmPrinter::EmitFunctionEntryLabel() {
119 SmallString<128> Str;
120 raw_svector_ostream OS(Str);
121
122 CurrentFnSym->redefineIfPossible();
123
124 // The function label could have already been emitted if two symbols end up
125 // conflicting due to asm renaming. Detect this and emit an error.
126 if (CurrentFnSym->isVariable())
127 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
128 "' is a protected alias");
129 if (CurrentFnSym->isDefined())
130 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
131 "' label emitted multiple times to assembly file");
132
133 OS << "(func " << toSymbol(CurrentFnSym->getName());
134 OutStreamer->EmitRawText(OS.str());
135}
136
137void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
138 SmallString<128> Str;
139 raw_svector_ostream OS(Str);
140 const Function *F = MF->getFunction();
141 for (const Argument &A : F->args())
142 OS << " (param " << toType(A.getType()) << ')';
143 const Type *Rt = F->getReturnType();
144 if (!Rt->isVoidTy())
145 OS << " (result " << toType(Rt) << ')';
146 OS << '\n';
147 OutStreamer->EmitRawText(OS.str());
148}
149
150void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
151 SmallString<128> Str;
152 raw_svector_ostream OS(Str);
153 OS << ") ;; end func " << toSymbol(CurrentFnSym->getName()) << '\n';
154 OutStreamer->EmitRawText(OS.str());
155}
156
JF Bastienb9073fb2015-07-22 21:28:15 +0000157void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000158 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000159 SmallString<128> Str;
160 raw_svector_ostream OS(Str);
161
JF Bastien600aee92015-07-31 17:53:38 +0000162 unsigned NumDefs = MI->getDesc().getNumDefs();
163 assert(NumDefs <= 1 &&
164 "Instructions with multiple result values not implemented");
165
Dan Gohman12e19972015-08-24 21:19:48 +0000166 OS << '\t';
167
JF Bastien600aee92015-07-31 17:53:38 +0000168 if (NumDefs != 0) {
169 const MachineOperand &MO = MI->getOperand(0);
170 unsigned Reg = MO.getReg();
171 OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
172 }
173
JF Bastien4a642252015-08-10 22:36:48 +0000174 OS << '(' << Name(TII, MI);
175 for (const MachineOperand &MO : MI->uses())
176 switch (MO.getType()) {
177 default:
178 llvm_unreachable("unexpected machine operand type");
179 case MachineOperand::MO_Register: {
180 if (MO.isImplicit())
JF Bastien600aee92015-07-31 17:53:38 +0000181 continue;
182 unsigned Reg = MO.getReg();
183 OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
JF Bastien4a642252015-08-10 22:36:48 +0000184 } break;
185 case MachineOperand::MO_Immediate: {
186 OS << ' ' << MO.getImm();
187 } break;
188 case MachineOperand::MO_FPImmediate: {
189 static const size_t BufBytes = 128;
190 char buf[BufBytes];
191 APFloat FP = MO.getFPImm()->getValueAPF();
JF Bastiene73ce682015-08-11 00:49:20 +0000192 if (FP.isNaN())
193 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
194 FP.bitwiseIsEqual(
195 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
196 "convertToHexString handles neither SNaN nor NaN payloads");
JF Bastien4a642252015-08-10 22:36:48 +0000197 // Use C99's hexadecimal floating-point representation.
198 auto Written =
199 FP.convertToHexString(buf, /*hexDigits=*/0, /*upperCase=*/false,
200 APFloat::rmNearestTiesToEven);
JF Bastien11bf0da2015-08-11 04:52:24 +0000201 (void)Written;
JF Bastien4a642252015-08-10 22:36:48 +0000202 assert(Written != 0);
203 assert(Written < BufBytes);
204 OS << ' ' << buf;
205 } break;
JF Bastienaf111db2015-08-24 22:16:48 +0000206 case MachineOperand::MO_GlobalAddress: {
207 OS << ' ' << toSymbol(MO.getGlobal()->getName());
208 } break;
JF Bastien600aee92015-07-31 17:53:38 +0000209 }
210 OS << ')';
211
212 if (NumDefs != 0)
213 OS << ')';
214
215 OS << '\n';
216
JF Bastienb9073fb2015-07-22 21:28:15 +0000217 OutStreamer->EmitRawText(OS.str());
218}
219
220// Force static initialization.
221extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
222 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
223 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
224}