blob: 5c8fe57084714d441545ca404b22d9020dab882e [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);
229 if (!MRI->use_empty(VReg)) {
230 if (FirstVReg) {
231 OS << (First ? "" : "\n") << "\t.local ";
232 First = false;
233 }
234 OS << (FirstVReg ? "" : ", ") << getRegTypeName(VReg);
235 FirstVReg = false;
236 }
237 }
238
239 if (!First)
240 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000241 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000242}
243
JF Bastienb9073fb2015-07-22 21:28:15 +0000244void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000245 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000246 SmallString<128> Str;
247 raw_svector_ostream OS(Str);
248
JF Bastien600aee92015-07-31 17:53:38 +0000249 unsigned NumDefs = MI->getDesc().getNumDefs();
250 assert(NumDefs <= 1 &&
251 "Instructions with multiple result values not implemented");
252
Dan Gohman12e19972015-08-24 21:19:48 +0000253 OS << '\t';
254
Dan Gohmane51c0582015-10-06 00:27:55 +0000255 switch (MI->getOpcode()) {
256 case TargetOpcode::COPY:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000257 OS << "get_local " << regToString(MI->getOperand(1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000258 break;
259 case WebAssembly::GLOBAL:
260 // TODO: wasm64
261 OS << "i32.const " << toSymbol(MI->getOperand(1).getGlobal()->getName());
262 break;
263 case WebAssembly::ARGUMENT_I32:
264 case WebAssembly::ARGUMENT_I64:
265 case WebAssembly::ARGUMENT_F32:
266 case WebAssembly::ARGUMENT_F64:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000267 OS << "get_local " << argToString(MI->getOperand(1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000268 break;
269 case WebAssembly::Immediate_I32:
270 OS << "i32.const " << MI->getOperand(1).getImm();
271 break;
272 case WebAssembly::Immediate_I64:
273 OS << "i64.const " << MI->getOperand(1).getImm();
274 break;
275 case WebAssembly::Immediate_F32:
276 OS << "f32.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
277 break;
278 case WebAssembly::Immediate_F64:
279 OS << "f64.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
280 break;
281 default: {
282 OS << OpcodeName(TII, MI);
283 bool NeedComma = false;
284 for (const MachineOperand &MO : MI->uses()) {
285 if (MO.isReg() && MO.isImplicit())
286 continue;
287 if (NeedComma)
288 OS << ',';
289 NeedComma = true;
290 OS << ' ';
Dan Gohman4f52e002015-09-09 00:52:47 +0000291 switch (MO.getType()) {
292 default:
293 llvm_unreachable("unexpected machine operand type");
Dan Gohmane51c0582015-10-06 00:27:55 +0000294 case MachineOperand::MO_Register:
JF Bastien1d20a5e2015-10-16 00:53:49 +0000295 OS << "(get_local " << regToString(MO) << ')';
Dan Gohmane51c0582015-10-06 00:27:55 +0000296 break;
297 case MachineOperand::MO_Immediate:
298 OS << MO.getImm();
299 break;
300 case MachineOperand::MO_FPImmediate:
301 OS << toString(MO.getFPImm()->getValueAPF());
302 break;
303 case MachineOperand::MO_GlobalAddress:
304 OS << toSymbol(MO.getGlobal()->getName());
305 break;
306 case MachineOperand::MO_MachineBasicBlock:
307 OS << toSymbol(MO.getMBB()->getSymbol()->getName());
308 break;
Dan Gohman4f52e002015-09-09 00:52:47 +0000309 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000310 }
311 break;
312 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000313 }
JF Bastien600aee92015-07-31 17:53:38 +0000314
JF Bastienb9073fb2015-07-22 21:28:15 +0000315 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000316
317 if (NumDefs != 0) {
318 SmallString<128> Str;
319 raw_svector_ostream OS(Str);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000320 const MachineOperand &Operand = MI->getOperand(0);
321 OS << "\tset_local " << regToString(Operand) << ", pop";
Dan Gohmane51c0582015-10-06 00:27:55 +0000322 OutStreamer->EmitRawText(OS.str());
323 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000324}
325
326// Force static initialization.
327extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
328 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
329 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
330}