blob: 400e85748eba7c84b1b1586bbfe3be1983e52f04 [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"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "InstPrinter/WebAssemblyInstPrinter.h"
19#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
20#include "WebAssemblyMCInstLower.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000021#include "WebAssemblyMachineFunctionInfo.h"
22#include "WebAssemblyRegisterInfo.h"
23#include "WebAssemblySubtarget.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000024#include "llvm/ADT/SmallString.h"
Dan Gohmane51c0582015-10-06 00:27:55 +000025#include "llvm/ADT/StringExtras.h"
Dan Gohman754cd112015-11-11 01:33:02 +000026#include "llvm/CodeGen/Analysis.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000027#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000028#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/IR/DataLayout.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000031#include "llvm/MC/MCContext.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000033#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/raw_ostream.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "asm-printer"
41
42namespace {
43
44class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien1d20a5e2015-10-16 00:53:49 +000045 const MachineRegisterInfo *MRI;
Dan Gohmancf4748f2015-11-12 17:04:33 +000046 const WebAssemblyFunctionInfo *MFI;
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)
Dan Gohmancf4748f2015-11-12 17:04:33 +000050 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(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 Bastien1d20a5e2015-10-16 00:53:49 +000066 MRI = &MF.getRegInfo();
Dan Gohmancf4748f2015-11-12 17:04:33 +000067 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
JF Bastien600aee92015-07-31 17:53:38 +000068 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000069 }
70
71 //===------------------------------------------------------------------===//
72 // AsmPrinter Implementation.
73 //===------------------------------------------------------------------===//
74
Dan Gohman950a13c2015-09-16 16:51:30 +000075 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000076 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000077 void EmitFunctionBodyStart() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000078 void EmitInstruction(const MachineInstr *MI) override;
JF Bastien1a59c6b2015-10-21 02:23:09 +000079 void EmitEndOfAsmFile(Module &M) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000080 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
81 unsigned AsmVariant, const char *ExtraCode,
82 raw_ostream &OS) override;
83 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
84 unsigned AsmVariant, const char *ExtraCode,
85 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000086
JF Bastien1d20a5e2015-10-16 00:53:49 +000087 std::string getRegTypeName(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000088 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000089 std::string regToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000090};
91
92} // end anonymous namespace
93
94//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000095// Helpers.
96//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000097
JF Bastien1d20a5e2015-10-16 00:53:49 +000098std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const {
99 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
100 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
101 if (TRC->hasType(T))
102 return EVT(T).getEVTString();
103 DEBUG(errs() << "Unknown type for register number: " << RegNo);
104 llvm_unreachable("Unknown register type");
105 return "?";
106}
107
JF Bastien1d20a5e2015-10-16 00:53:49 +0000108std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
109 unsigned RegNo = MO.getReg();
Dan Gohmane51c0582015-10-06 00:27:55 +0000110 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
111 return WebAssemblyInstPrinter::getRegisterName(RegNo);
112
Dan Gohman4ba48162015-11-18 16:12:01 +0000113 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000114 unsigned WAReg = MFI->getWAReg(RegNo);
115 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000116 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000117}
118
Dan Gohman754cd112015-11-11 01:33:02 +0000119const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
120 switch (VT.SimpleTy) {
Dan Gohman979840d2015-09-23 16:59:10 +0000121 default:
122 break;
Dan Gohman754cd112015-11-11 01:33:02 +0000123 case MVT::f32:
Dan Gohman979840d2015-09-23 16:59:10 +0000124 return "f32";
Dan Gohman754cd112015-11-11 01:33:02 +0000125 case MVT::f64:
Dan Gohman979840d2015-09-23 16:59:10 +0000126 return "f64";
Dan Gohman754cd112015-11-11 01:33:02 +0000127 case MVT::i32:
128 return "i32";
129 case MVT::i64:
130 return "i64";
JF Bastien73ff6af2015-08-31 22:24:11 +0000131 }
Dan Gohman754cd112015-11-11 01:33:02 +0000132 DEBUG(dbgs() << "Invalid type " << EVT(VT).getEVTString() << '\n');
JF Bastien73ff6af2015-08-31 22:24:11 +0000133 llvm_unreachable("invalid type");
134 return "<invalid>";
135}
136
JF Bastien45479f62015-08-26 22:09:54 +0000137//===----------------------------------------------------------------------===//
138// WebAssemblyAsmPrinter Implementation.
139//===----------------------------------------------------------------------===//
140
JF Bastien54be3b12015-08-25 23:19:49 +0000141void WebAssemblyAsmPrinter::EmitConstantPool() {
142 assert(MF->getConstantPool()->getConstants().empty() &&
143 "WebAssembly disables constant pools");
144}
145
Dan Gohman950a13c2015-09-16 16:51:30 +0000146void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
147 // Nothing to do; jump tables are incorporated into the instruction stream.
148}
149
Derek Schuff46e33162015-11-16 21:12:41 +0000150static void ComputeLegalValueVTs(const Function &F,
151 const TargetMachine &TM,
152 Type *Ty,
153 SmallVectorImpl<MVT> &ValueVTs) {
154 const DataLayout& DL(F.getParent()->getDataLayout());
155 const WebAssemblyTargetLowering &TLI =
156 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
157 SmallVector<EVT, 4> VTs;
158 ComputeValueVTs(TLI, DL, Ty, VTs);
159
160 for (EVT VT : VTs) {
161 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
162 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
163 for (unsigned i = 0; i != NumRegs; ++i)
164 ValueVTs.push_back(RegisterVT);
165 }
166}
167
JF Bastienb6091df2015-08-25 22:58:05 +0000168void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000169 SmallString<128> Str;
170 raw_svector_ostream OS(Str);
Dan Gohmane51c0582015-10-06 00:27:55 +0000171
Dan Gohmancf4748f2015-11-12 17:04:33 +0000172 for (MVT VT : MFI->getParams())
173 OS << "\t" ".param " << toString(VT) << '\n';
Derek Schuff46e33162015-11-16 21:12:41 +0000174
175 SmallVector<MVT, 4> ResultVTs;
176 const Function &F(*MF->getFunction());
177 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
178 // If the return type needs to be legalized it will get converted into
179 // passing a pointer.
180 if (ResultVTs.size() == 1)
181 OS << "\t" ".result " << toString(ResultVTs.front()) << '\n';
JF Bastienb6091df2015-08-25 22:58:05 +0000182
Dan Gohman4ba48162015-11-18 16:12:01 +0000183 bool FirstWAReg = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000184 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
185 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000186 unsigned WAReg = MFI->getWAReg(VReg);
187 // Don't declare unused registers.
188 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
189 continue;
190 // Don't redeclare parameters.
191 if (WAReg < MFI->getParams().size())
192 continue;
193 // Don't declare stackified registers.
194 if (int(WAReg) < 0)
195 continue;
196 if (FirstWAReg)
197 OS << "\t" ".local ";
198 else
199 OS << ", ";
200 OS << getRegTypeName(VReg);
201 FirstWAReg = false;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000202 }
Dan Gohman4ba48162015-11-18 16:12:01 +0000203 if (!FirstWAReg)
Dan Gohman754cd112015-11-11 01:33:02 +0000204 OS << '\n';
JF Bastien1d20a5e2015-10-16 00:53:49 +0000205
Dan Gohman754cd112015-11-11 01:33:02 +0000206 // EmitRawText appends a newline, so strip off the last newline.
207 StringRef Text = OS.str();
208 if (!Text.empty())
209 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000210 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000211}
212
JF Bastienb9073fb2015-07-22 21:28:15 +0000213void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000214 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000215
Dan Gohmane51c0582015-10-06 00:27:55 +0000216 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000217 case WebAssembly::ARGUMENT_I32:
218 case WebAssembly::ARGUMENT_I64:
219 case WebAssembly::ARGUMENT_F32:
220 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000221 // These represent values which are live into the function entry, so there's
222 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000223 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000224 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000225 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
226 MCInst TmpInst;
227 MCInstLowering.Lower(MI, TmpInst);
228 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000229 break;
230 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000231 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000232}
233
JF Bastien1a59c6b2015-10-21 02:23:09 +0000234void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
Dan Gohman754cd112015-11-11 01:33:02 +0000235 const DataLayout &DL = M.getDataLayout();
236
JF Bastien5789a692015-10-30 16:41:21 +0000237 SmallString<128> Str;
238 raw_svector_ostream OS(Str);
JF Bastien1a59c6b2015-10-21 02:23:09 +0000239 for (const Function &F : M)
240 if (F.isDeclarationForLinker()) {
241 assert(F.hasName() && "imported functions must have a name");
Dan Gohman754cd112015-11-11 01:33:02 +0000242 if (F.isIntrinsic())
JF Bastien5789a692015-10-30 16:41:21 +0000243 continue;
JF Bastien1a59c6b2015-10-21 02:23:09 +0000244 if (Str.empty())
JF Bastien5789a692015-10-30 16:41:21 +0000245 OS << "\t.imports\n";
Dan Gohman754cd112015-11-11 01:33:02 +0000246
Dan Gohmancf4748f2015-11-12 17:04:33 +0000247 MCSymbol *Sym = OutStreamer->getContext().getOrCreateSymbol(F.getName());
248 OS << "\t.import " << *Sym << " \"\" " << *Sym;
Dan Gohman754cd112015-11-11 01:33:02 +0000249
250 const WebAssemblyTargetLowering &TLI =
251 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
252
253 // If we need to legalize the return type, it'll get converted into
254 // passing a pointer.
255 bool SawParam = false;
256 SmallVector<MVT, 4> ResultVTs;
Derek Schuff46e33162015-11-16 21:12:41 +0000257 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman754cd112015-11-11 01:33:02 +0000258 if (ResultVTs.size() > 1) {
259 ResultVTs.clear();
260 OS << " (param " << toString(TLI.getPointerTy(DL));
261 SawParam = true;
262 }
263
264 for (const Argument &A : F.args()) {
265 SmallVector<MVT, 4> ParamVTs;
Derek Schuff46e33162015-11-16 21:12:41 +0000266 ComputeLegalValueVTs(F, TM, A.getType(), ParamVTs);
267 for (MVT VT : ParamVTs) {
Dan Gohman754cd112015-11-11 01:33:02 +0000268 if (!SawParam) {
269 OS << " (param";
270 SawParam = true;
271 }
Derek Schuff46e33162015-11-16 21:12:41 +0000272 OS << ' ' << toString(VT);
Dan Gohman754cd112015-11-11 01:33:02 +0000273 }
274 }
275 if (SawParam)
276 OS << ')';
277
Derek Schuff46e33162015-11-16 21:12:41 +0000278 for (MVT VT : ResultVTs)
279 OS << " (result " << toString(VT) << ')';
Dan Gohman754cd112015-11-11 01:33:02 +0000280
JF Bastien1a59c6b2015-10-21 02:23:09 +0000281 OS << '\n';
JF Bastien5789a692015-10-30 16:41:21 +0000282 }
Dan Gohman754cd112015-11-11 01:33:02 +0000283
284 StringRef Text = OS.str();
285 if (!Text.empty())
286 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
JF Bastien1a59c6b2015-10-21 02:23:09 +0000287}
288
Dan Gohmanf19ed562015-11-13 01:42:29 +0000289bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
290 unsigned OpNo, unsigned AsmVariant,
291 const char *ExtraCode,
292 raw_ostream &OS) {
293 if (AsmVariant != 0)
294 report_fatal_error("There are no defined alternate asm variants");
295
296 if (!ExtraCode) {
297 const MachineOperand &MO = MI->getOperand(OpNo);
298 if (MO.isImm())
299 OS << MO.getImm();
300 else
301 OS << regToString(MO);
302 return false;
303 }
304
305 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
306}
307
308bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
309 unsigned OpNo,
310 unsigned AsmVariant,
311 const char *ExtraCode,
312 raw_ostream &OS) {
313 if (AsmVariant != 0)
314 report_fatal_error("There are no defined alternate asm variants");
315
316 if (!ExtraCode) {
317 OS << regToString(MI->getOperand(OpNo));
318 return false;
319 }
320
321 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
322}
323
JF Bastienb9073fb2015-07-22 21:28:15 +0000324// Force static initialization.
325extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
326 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
327 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
328}