blob: 0ab8888015e2dc3d696a4b2751a131f1ad6b95b2 [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 Gohman058fce52015-11-13 00:21:05 +0000113 unsigned WAReg = MFI->getWAReg(RegNo);
114 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
115 return utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000116}
117
Dan Gohman754cd112015-11-11 01:33:02 +0000118const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
119 switch (VT.SimpleTy) {
Dan Gohman979840d2015-09-23 16:59:10 +0000120 default:
121 break;
Dan Gohman754cd112015-11-11 01:33:02 +0000122 case MVT::f32:
Dan Gohman979840d2015-09-23 16:59:10 +0000123 return "f32";
Dan Gohman754cd112015-11-11 01:33:02 +0000124 case MVT::f64:
Dan Gohman979840d2015-09-23 16:59:10 +0000125 return "f64";
Dan Gohman754cd112015-11-11 01:33:02 +0000126 case MVT::i32:
127 return "i32";
128 case MVT::i64:
129 return "i64";
JF Bastien73ff6af2015-08-31 22:24:11 +0000130 }
Dan Gohman754cd112015-11-11 01:33:02 +0000131 DEBUG(dbgs() << "Invalid type " << EVT(VT).getEVTString() << '\n');
JF Bastien73ff6af2015-08-31 22:24:11 +0000132 llvm_unreachable("invalid type");
133 return "<invalid>";
134}
135
JF Bastien45479f62015-08-26 22:09:54 +0000136//===----------------------------------------------------------------------===//
137// WebAssemblyAsmPrinter Implementation.
138//===----------------------------------------------------------------------===//
139
JF Bastien54be3b12015-08-25 23:19:49 +0000140void WebAssemblyAsmPrinter::EmitConstantPool() {
141 assert(MF->getConstantPool()->getConstants().empty() &&
142 "WebAssembly disables constant pools");
143}
144
Dan Gohman950a13c2015-09-16 16:51:30 +0000145void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
146 // Nothing to do; jump tables are incorporated into the instruction stream.
147}
148
JF Bastienb6091df2015-08-25 22:58:05 +0000149void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000150 SmallString<128> Str;
151 raw_svector_ostream OS(Str);
Dan Gohmane51c0582015-10-06 00:27:55 +0000152
Dan Gohmancf4748f2015-11-12 17:04:33 +0000153 for (MVT VT : MFI->getParams())
154 OS << "\t" ".param " << toString(VT) << '\n';
155 for (MVT VT : MFI->getResults())
156 OS << "\t" ".result " << toString(VT) << '\n';
JF Bastienb6091df2015-08-25 22:58:05 +0000157
JF Bastien1d20a5e2015-10-16 00:53:49 +0000158 bool FirstVReg = true;
159 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
160 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohmancf4748f2015-11-12 17:04:33 +0000161 if (!MRI->use_empty(VReg)) {
162 if (FirstVReg)
163 OS << "\t" ".local ";
164 else
165 OS << ", ";
166 OS << getRegTypeName(VReg);
167 FirstVReg = false;
168 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000169 }
Dan Gohman754cd112015-11-11 01:33:02 +0000170 if (!FirstVReg)
171 OS << '\n';
JF Bastien1d20a5e2015-10-16 00:53:49 +0000172
Dan Gohman754cd112015-11-11 01:33:02 +0000173 // EmitRawText appends a newline, so strip off the last newline.
174 StringRef Text = OS.str();
175 if (!Text.empty())
176 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000177 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000178}
179
JF Bastienb9073fb2015-07-22 21:28:15 +0000180void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000181 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000182
David Blaikieb0311c52015-11-12 19:07:43 +0000183 assert(MI->getDesc().getNumDefs() <= 1 &&
JF Bastien600aee92015-07-31 17:53:38 +0000184 "Instructions with multiple result values not implemented");
185
Dan Gohmane51c0582015-10-06 00:27:55 +0000186 switch (MI->getOpcode()) {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000187 case TargetOpcode::COPY: {
188 // TODO: Figure out a way to lower COPY instructions to MCInst form.
189 SmallString<128> Str;
190 raw_svector_ostream OS(Str);
191 OS << "\t" "set_local " << regToString(MI->getOperand(0)) << ", "
192 "(get_local " << regToString(MI->getOperand(1)) << ")";
193 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000194 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000195 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000196 case WebAssembly::ARGUMENT_I32:
197 case WebAssembly::ARGUMENT_I64:
198 case WebAssembly::ARGUMENT_F32:
199 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000200 // These represent values which are live into the function entry, so there's
201 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000202 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000203 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000204 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
205 MCInst TmpInst;
206 MCInstLowering.Lower(MI, TmpInst);
207 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000208 break;
209 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000210 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000211}
212
Dan Gohman754cd112015-11-11 01:33:02 +0000213static void ComputeLegalValueVTs(LLVMContext &Context,
214 const WebAssemblyTargetLowering &TLI,
215 const DataLayout &DL, Type *Ty,
216 SmallVectorImpl<MVT> &ValueVTs) {
217 SmallVector<EVT, 4> VTs;
218 ComputeValueVTs(TLI, DL, Ty, VTs);
219
220 for (EVT VT : VTs) {
221 unsigned NumRegs = TLI.getNumRegisters(Context, VT);
222 MVT RegisterVT = TLI.getRegisterType(Context, VT);
223 for (unsigned i = 0; i != NumRegs; ++i)
224 ValueVTs.push_back(RegisterVT);
225 }
226}
227
JF Bastien1a59c6b2015-10-21 02:23:09 +0000228void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
Dan Gohman754cd112015-11-11 01:33:02 +0000229 const DataLayout &DL = M.getDataLayout();
230
JF Bastien5789a692015-10-30 16:41:21 +0000231 SmallString<128> Str;
232 raw_svector_ostream OS(Str);
JF Bastien1a59c6b2015-10-21 02:23:09 +0000233 for (const Function &F : M)
234 if (F.isDeclarationForLinker()) {
235 assert(F.hasName() && "imported functions must have a name");
Dan Gohman754cd112015-11-11 01:33:02 +0000236 if (F.isIntrinsic())
JF Bastien5789a692015-10-30 16:41:21 +0000237 continue;
JF Bastien1a59c6b2015-10-21 02:23:09 +0000238 if (Str.empty())
JF Bastien5789a692015-10-30 16:41:21 +0000239 OS << "\t.imports\n";
Dan Gohman754cd112015-11-11 01:33:02 +0000240
Dan Gohmancf4748f2015-11-12 17:04:33 +0000241 MCSymbol *Sym = OutStreamer->getContext().getOrCreateSymbol(F.getName());
242 OS << "\t.import " << *Sym << " \"\" " << *Sym;
Dan Gohman754cd112015-11-11 01:33:02 +0000243
244 const WebAssemblyTargetLowering &TLI =
245 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
246
247 // If we need to legalize the return type, it'll get converted into
248 // passing a pointer.
249 bool SawParam = false;
250 SmallVector<MVT, 4> ResultVTs;
251 ComputeLegalValueVTs(M.getContext(), TLI, DL, F.getReturnType(),
252 ResultVTs);
253 if (ResultVTs.size() > 1) {
254 ResultVTs.clear();
255 OS << " (param " << toString(TLI.getPointerTy(DL));
256 SawParam = true;
257 }
258
259 for (const Argument &A : F.args()) {
260 SmallVector<MVT, 4> ParamVTs;
261 ComputeLegalValueVTs(M.getContext(), TLI, DL, A.getType(), ParamVTs);
262 for (EVT VT : ParamVTs) {
263 if (!SawParam) {
264 OS << " (param";
265 SawParam = true;
266 }
267 OS << ' ' << toString(VT.getSimpleVT());
268 }
269 }
270 if (SawParam)
271 OS << ')';
272
273 for (EVT VT : ResultVTs)
274 OS << " (result " << toString(VT.getSimpleVT()) << ')';
275
JF Bastien1a59c6b2015-10-21 02:23:09 +0000276 OS << '\n';
JF Bastien5789a692015-10-30 16:41:21 +0000277 }
Dan Gohman754cd112015-11-11 01:33:02 +0000278
279 StringRef Text = OS.str();
280 if (!Text.empty())
281 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
JF Bastien1a59c6b2015-10-21 02:23:09 +0000282}
283
Dan Gohmanf19ed562015-11-13 01:42:29 +0000284bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
285 unsigned OpNo, unsigned AsmVariant,
286 const char *ExtraCode,
287 raw_ostream &OS) {
288 if (AsmVariant != 0)
289 report_fatal_error("There are no defined alternate asm variants");
290
291 if (!ExtraCode) {
292 const MachineOperand &MO = MI->getOperand(OpNo);
293 if (MO.isImm())
294 OS << MO.getImm();
295 else
296 OS << regToString(MO);
297 return false;
298 }
299
300 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
301}
302
303bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
304 unsigned OpNo,
305 unsigned AsmVariant,
306 const char *ExtraCode,
307 raw_ostream &OS) {
308 if (AsmVariant != 0)
309 report_fatal_error("There are no defined alternate asm variants");
310
311 if (!ExtraCode) {
312 OS << regToString(MI->getOperand(OpNo));
313 return false;
314 }
315
316 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
317}
318
JF Bastienb9073fb2015-07-22 21:28:15 +0000319// Force static initialization.
320extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
321 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
322 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
323}