blob: fda95a5e6a5d7fe7a1bbe42fc2e09e48152f8704 [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"
Dan Gohmane51c0582015-10-06 00:27:55 +000025#include "llvm/ADT/StringExtras.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000026#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000027#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DebugInfo.h"
31#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000032#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/raw_ostream.h"
36
37using namespace llvm;
38
39#define DEBUG_TYPE "asm-printer"
40
41namespace {
42
43class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien600aee92015-07-31 17:53:38 +000044 const WebAssemblyInstrInfo *TII;
JF Bastien1d20a5e2015-10-16 00:53:49 +000045 const MachineRegisterInfo *MRI;
Dan Gohmane51c0582015-10-06 00:27:55 +000046 unsigned NumArgs;
JF Bastien600aee92015-07-31 17:53:38 +000047
JF Bastienb9073fb2015-07-22 21:28:15 +000048public:
49 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
JF Bastien1d20a5e2015-10-16 00:53:49 +000050 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr), MRI(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000051
52private:
53 const char *getPassName() const override {
54 return "WebAssembly Assembly Printer";
55 }
56
57 //===------------------------------------------------------------------===//
58 // MachineFunctionPass Implementation.
59 //===------------------------------------------------------------------===//
60
61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AsmPrinter::getAnalysisUsage(AU);
63 }
64
JF Bastien600aee92015-07-31 17:53:38 +000065 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien73ff6af2015-08-31 22:24:11 +000066 const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
JF Bastien73ff6af2015-08-31 22:24:11 +000067 TII = Subtarget.getInstrInfo();
JF Bastien1d20a5e2015-10-16 00:53:49 +000068 MRI = &MF.getRegInfo();
Dan Gohmane51c0582015-10-06 00:27:55 +000069 NumArgs = MF.getInfo<WebAssemblyFunctionInfo>()->getNumArguments();
JF Bastien600aee92015-07-31 17:53:38 +000070 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000071 }
72
73 //===------------------------------------------------------------------===//
74 // AsmPrinter Implementation.
75 //===------------------------------------------------------------------===//
76
Dan Gohman950a13c2015-09-16 16:51:30 +000077 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000078 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000079 void EmitFunctionBodyStart() override;
JF Bastienb6091df2015-08-25 22:58:05 +000080
JF Bastienb9073fb2015-07-22 21:28:15 +000081 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohman979840d2015-09-23 16:59:10 +000082
JF Bastien1d20a5e2015-10-16 00:53:49 +000083 std::string getRegTypeName(unsigned RegNo) const;
Dan Gohman979840d2015-09-23 16:59:10 +000084 static std::string toString(const APFloat &APF);
85 const char *toString(Type *Ty) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000086 std::string regToString(const MachineOperand &MO);
87 std::string argToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000088};
89
90} // end anonymous namespace
91
92//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000093// Helpers.
94//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000095
JF Bastien1d20a5e2015-10-16 00:53:49 +000096// Operand type (if any), followed by the lower-case version of the opcode's
97// name matching the names WebAssembly opcodes are expected to have. The
98// tablegen names are uppercase and suffixed with their type (after an
99// underscore).
100static std::string OpcodeName(const WebAssemblyInstrInfo *TII,
101 const MachineInstr *MI) {
JF Bastien315cc062015-08-07 01:57:03 +0000102 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000103 std::string::size_type Len = N.length();
104 std::string::size_type Under = N.rfind('_');
105 bool HasType = std::string::npos != Under;
106 std::string::size_type NameEnd = HasType ? Under : Len;
107 std::string Name(&N[0], &N[NameEnd]);
108 return HasType ? (std::string(&N[NameEnd + 1], &N[Len]) + '.' + Name) : Name;
JF Bastien315cc062015-08-07 01:57:03 +0000109}
110
JF Bastienaf111db2015-08-24 22:16:48 +0000111static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
112
JF Bastien1d20a5e2015-10-16 00:53:49 +0000113std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const {
114 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
115 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
116 if (TRC->hasType(T))
117 return EVT(T).getEVTString();
118 DEBUG(errs() << "Unknown type for register number: " << RegNo);
119 llvm_unreachable("Unknown register type");
120 return "?";
121}
122
Dan Gohman979840d2015-09-23 16:59:10 +0000123std::string WebAssemblyAsmPrinter::toString(const APFloat &FP) {
JF Bastien45479f62015-08-26 22:09:54 +0000124 static const size_t BufBytes = 128;
125 char buf[BufBytes];
126 if (FP.isNaN())
127 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
128 FP.bitwiseIsEqual(
129 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
130 "convertToHexString handles neither SNaN nor NaN payloads");
131 // Use C99's hexadecimal floating-point representation.
132 auto Written = FP.convertToHexString(
133 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
134 (void)Written;
135 assert(Written != 0);
136 assert(Written < BufBytes);
137 return buf;
138}
139
JF Bastien1d20a5e2015-10-16 00:53:49 +0000140std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
141 unsigned RegNo = MO.getReg();
Dan Gohmane51c0582015-10-06 00:27:55 +0000142 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
143 return WebAssemblyInstPrinter::getRegisterName(RegNo);
144
145 // WebAssembly arguments and local variables are in the same index space, and
146 // there are no explicit varargs, so we just add the number of arguments to
147 // the virtual register number to get the local variable number.
JF Bastien1d20a5e2015-10-16 00:53:49 +0000148 return utostr(TargetRegisterInfo::virtReg2Index(RegNo) + NumArgs);
Dan Gohmane51c0582015-10-06 00:27:55 +0000149}
150
JF Bastien1d20a5e2015-10-16 00:53:49 +0000151std::string WebAssemblyAsmPrinter::argToString(const MachineOperand &MO) {
152 unsigned ArgNo = MO.getImm();
Dan Gohmane51c0582015-10-06 00:27:55 +0000153 // Same as above, but we don't need to add NumArgs here.
JF Bastien1d20a5e2015-10-16 00:53:49 +0000154 return utostr(ArgNo);
Dan Gohmane51c0582015-10-06 00:27:55 +0000155}
156
Dan Gohman979840d2015-09-23 16:59:10 +0000157const char *WebAssemblyAsmPrinter::toString(Type *Ty) const {
JF Bastien73ff6af2015-08-31 22:24:11 +0000158 switch (Ty->getTypeID()) {
Dan Gohman979840d2015-09-23 16:59:10 +0000159 default:
160 break;
JF Bastien73ff6af2015-08-31 22:24:11 +0000161 // Treat all pointers as the underlying integer into linear memory.
Dan Gohman979840d2015-09-23 16:59:10 +0000162 case Type::PointerTyID:
163 switch (getPointerSize()) {
164 case 4:
165 return "i32";
166 case 8:
167 return "i64";
168 default:
169 llvm_unreachable("unsupported pointer size");
170 }
171 break;
172 case Type::FloatTyID:
173 return "f32";
174 case Type::DoubleTyID:
175 return "f64";
JF Bastien73ff6af2015-08-31 22:24:11 +0000176 case Type::IntegerTyID:
177 switch (Ty->getIntegerBitWidth()) {
Dan Gohman979840d2015-09-23 16:59:10 +0000178 case 8:
179 return "i8";
180 case 16:
181 return "i16";
182 case 32:
183 return "i32";
184 case 64:
185 return "i64";
186 default:
187 break;
JF Bastien73ff6af2015-08-31 22:24:11 +0000188 }
189 }
190 DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
191 llvm_unreachable("invalid type");
192 return "<invalid>";
193}
194
JF Bastien45479f62015-08-26 22:09:54 +0000195//===----------------------------------------------------------------------===//
196// WebAssemblyAsmPrinter Implementation.
197//===----------------------------------------------------------------------===//
198
JF Bastien54be3b12015-08-25 23:19:49 +0000199void WebAssemblyAsmPrinter::EmitConstantPool() {
200 assert(MF->getConstantPool()->getConstants().empty() &&
201 "WebAssembly disables constant pools");
202}
203
Dan Gohman950a13c2015-09-16 16:51:30 +0000204void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
205 // Nothing to do; jump tables are incorporated into the instruction stream.
206}
207
JF Bastienb6091df2015-08-25 22:58:05 +0000208void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
JF Bastienb6091df2015-08-25 22:58:05 +0000209 const Function *F = MF->getFunction();
Dan Gohman979840d2015-09-23 16:59:10 +0000210 Type *Rt = F->getReturnType();
JF Bastien1d20a5e2015-10-16 00:53:49 +0000211 SmallString<128> Str;
212 raw_svector_ostream OS(Str);
213 bool First = true;
Dan Gohmane51c0582015-10-06 00:27:55 +0000214
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000215 if (!Rt->isVoidTy() || !F->arg_empty()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000216 for (const Argument &A : F->args()) {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000217 OS << (First ? "" : "\n") << "\t.param " << toString(A.getType());
Dan Gohmane51c0582015-10-06 00:27:55 +0000218 First = false;
219 }
220 if (!Rt->isVoidTy()) {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000221 OS << (First ? "" : "\n") << "\t.result " << toString(Rt);
Dan Gohmane51c0582015-10-06 00:27:55 +0000222 First = false;
223 }
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000224 }
JF Bastienb6091df2015-08-25 22:58:05 +0000225
JF Bastien1d20a5e2015-10-16 00:53:49 +0000226 bool FirstVReg = true;
227 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
228 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
JF Bastien3428ed42015-10-17 00:25:38 +0000229 // FIXME: Don't skip dead virtual registers for now: that would require
230 // remapping all locals' numbers.
231 //if (!MRI->use_empty(VReg)) {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000232 if (FirstVReg) {
233 OS << (First ? "" : "\n") << "\t.local ";
234 First = false;
235 }
236 OS << (FirstVReg ? "" : ", ") << getRegTypeName(VReg);
237 FirstVReg = false;
JF Bastien3428ed42015-10-17 00:25:38 +0000238 //}
JF Bastien1d20a5e2015-10-16 00:53:49 +0000239 }
240
241 if (!First)
242 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000243 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000244}
245
JF Bastienb9073fb2015-07-22 21:28:15 +0000246void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000247 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000248 SmallString<128> Str;
249 raw_svector_ostream OS(Str);
250
JF Bastien600aee92015-07-31 17:53:38 +0000251 unsigned NumDefs = MI->getDesc().getNumDefs();
252 assert(NumDefs <= 1 &&
253 "Instructions with multiple result values not implemented");
254
Dan Gohman12e19972015-08-24 21:19:48 +0000255 OS << '\t';
256
Dan Gohmane51c0582015-10-06 00:27:55 +0000257 switch (MI->getOpcode()) {
258 case TargetOpcode::COPY:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000259 OS << "get_local " << regToString(MI->getOperand(1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000260 break;
261 case WebAssembly::GLOBAL:
262 // TODO: wasm64
263 OS << "i32.const " << toSymbol(MI->getOperand(1).getGlobal()->getName());
264 break;
265 case WebAssembly::ARGUMENT_I32:
266 case WebAssembly::ARGUMENT_I64:
267 case WebAssembly::ARGUMENT_F32:
268 case WebAssembly::ARGUMENT_F64:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000269 OS << "get_local " << argToString(MI->getOperand(1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000270 break;
271 case WebAssembly::Immediate_I32:
272 OS << "i32.const " << MI->getOperand(1).getImm();
273 break;
274 case WebAssembly::Immediate_I64:
275 OS << "i64.const " << MI->getOperand(1).getImm();
276 break;
277 case WebAssembly::Immediate_F32:
278 OS << "f32.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
279 break;
280 case WebAssembly::Immediate_F64:
281 OS << "f64.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
282 break;
283 default: {
284 OS << OpcodeName(TII, MI);
285 bool NeedComma = false;
286 for (const MachineOperand &MO : MI->uses()) {
287 if (MO.isReg() && MO.isImplicit())
288 continue;
289 if (NeedComma)
290 OS << ',';
291 NeedComma = true;
292 OS << ' ';
Dan Gohman4f52e002015-09-09 00:52:47 +0000293 switch (MO.getType()) {
294 default:
295 llvm_unreachable("unexpected machine operand type");
Dan Gohmane51c0582015-10-06 00:27:55 +0000296 case MachineOperand::MO_Register:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000297 OS << "(get_local " << regToString(MO) << ')';
Dan Gohmane51c0582015-10-06 00:27:55 +0000298 break;
299 case MachineOperand::MO_Immediate:
300 OS << MO.getImm();
301 break;
302 case MachineOperand::MO_FPImmediate:
303 OS << toString(MO.getFPImm()->getValueAPF());
304 break;
305 case MachineOperand::MO_GlobalAddress:
306 OS << toSymbol(MO.getGlobal()->getName());
307 break;
308 case MachineOperand::MO_MachineBasicBlock:
309 OS << toSymbol(MO.getMBB()->getSymbol()->getName());
310 break;
Dan Gohman4f52e002015-09-09 00:52:47 +0000311 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000312 }
313 break;
314 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000315 }
JF Bastien600aee92015-07-31 17:53:38 +0000316
JF Bastienb9073fb2015-07-22 21:28:15 +0000317 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000318
319 if (NumDefs != 0) {
320 SmallString<128> Str;
321 raw_svector_ostream OS(Str);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000322 const MachineOperand &Operand = MI->getOperand(0);
323 OS << "\tset_local " << regToString(Operand) << ", pop";
Dan Gohmane51c0582015-10-06 00:27:55 +0000324 OutStreamer->EmitRawText(OS.str());
325 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000326}
327
328// Force static initialization.
329extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
330 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
331 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
332}