blob: 1b175a7f8d5b2e3fa6907d781126580ece4898bf [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"
JF Bastienb9073fb2015-07-22 21:28:15 +000037using namespace llvm;
38
39#define DEBUG_TYPE "asm-printer"
40
41namespace {
42
43class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien1d20a5e2015-10-16 00:53:49 +000044 const MachineRegisterInfo *MRI;
Dan Gohmancf4748f2015-11-12 17:04:33 +000045 const WebAssemblyFunctionInfo *MFI;
JF Bastien600aee92015-07-31 17:53:38 +000046
JF Bastienb9073fb2015-07-22 21:28:15 +000047public:
48 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Dan Gohmancf4748f2015-11-12 17:04:33 +000049 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000050
51private:
52 const char *getPassName() const override {
53 return "WebAssembly Assembly Printer";
54 }
55
56 //===------------------------------------------------------------------===//
57 // MachineFunctionPass Implementation.
58 //===------------------------------------------------------------------===//
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AsmPrinter::getAnalysisUsage(AU);
62 }
63
JF Bastien600aee92015-07-31 17:53:38 +000064 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien1d20a5e2015-10-16 00:53:49 +000065 MRI = &MF.getRegInfo();
Dan Gohmancf4748f2015-11-12 17:04:33 +000066 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
JF Bastien600aee92015-07-31 17:53:38 +000067 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000068 }
69
70 //===------------------------------------------------------------------===//
71 // AsmPrinter Implementation.
72 //===------------------------------------------------------------------===//
73
Dan Gohman950a13c2015-09-16 16:51:30 +000074 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000075 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000076 void EmitFunctionBodyStart() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000077 void EmitInstruction(const MachineInstr *MI) override;
JF Bastien1a59c6b2015-10-21 02:23:09 +000078 void EmitEndOfAsmFile(Module &M) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000079 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
80 unsigned AsmVariant, const char *ExtraCode,
81 raw_ostream &OS) override;
82 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
83 unsigned AsmVariant, const char *ExtraCode,
84 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000085
Dan Gohman53828fd2015-11-23 16:50:18 +000086 MVT getRegType(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000087 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000088 std::string regToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000089};
90
91} // end anonymous namespace
92
93//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000094// Helpers.
95//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000096
Dan Gohman53828fd2015-11-23 16:50:18 +000097MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
JF Bastien1d20a5e2015-10-16 00:53:49 +000098 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
99 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
100 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +0000101 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000102 DEBUG(errs() << "Unknown type for register number: " << RegNo);
103 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000104 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000105}
106
JF Bastien1d20a5e2015-10-16 00:53:49 +0000107std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
108 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000109 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
110 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000111 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000112 unsigned WAReg = MFI->getWAReg(RegNo);
113 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000114 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000115}
116
Dan Gohman754cd112015-11-11 01:33:02 +0000117const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
Dan Gohman5e0886b2015-12-06 19:42:29 +0000118 return WebAssembly::TypeToString(VT);
JF Bastien73ff6af2015-08-31 22:24:11 +0000119}
120
JF Bastien45479f62015-08-26 22:09:54 +0000121//===----------------------------------------------------------------------===//
122// WebAssemblyAsmPrinter Implementation.
123//===----------------------------------------------------------------------===//
124
JF Bastien54be3b12015-08-25 23:19:49 +0000125void WebAssemblyAsmPrinter::EmitConstantPool() {
126 assert(MF->getConstantPool()->getConstants().empty() &&
127 "WebAssembly disables constant pools");
128}
129
Dan Gohman950a13c2015-09-16 16:51:30 +0000130void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
131 // Nothing to do; jump tables are incorporated into the instruction stream.
132}
133
Dan Gohman7a6b9822015-11-29 22:32:02 +0000134static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
135 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
136 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000137 const WebAssemblyTargetLowering &TLI =
138 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
139 SmallVector<EVT, 4> VTs;
140 ComputeValueVTs(TLI, DL, Ty, VTs);
141
142 for (EVT VT : VTs) {
143 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
144 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
145 for (unsigned i = 0; i != NumRegs; ++i)
146 ValueVTs.push_back(RegisterVT);
147 }
148}
149
JF Bastienb6091df2015-08-25 22:58:05 +0000150void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman53828fd2015-11-23 16:50:18 +0000151 if (!MFI->getParams().empty()) {
152 MCInst Param;
153 Param.setOpcode(WebAssembly::PARAM);
154 for (MVT VT : MFI->getParams())
155 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
156 EmitToStreamer(*OutStreamer, Param);
157 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000158
Derek Schuff46e33162015-11-16 21:12:41 +0000159 SmallVector<MVT, 4> ResultVTs;
160 const Function &F(*MF->getFunction());
161 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
162 // If the return type needs to be legalized it will get converted into
163 // passing a pointer.
Dan Gohman53828fd2015-11-23 16:50:18 +0000164 if (ResultVTs.size() == 1) {
165 MCInst Result;
166 Result.setOpcode(WebAssembly::RESULT);
167 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
168 EmitToStreamer(*OutStreamer, Result);
169 }
JF Bastienb6091df2015-08-25 22:58:05 +0000170
Dan Gohman53828fd2015-11-23 16:50:18 +0000171 bool AnyWARegs = false;
172 MCInst Local;
173 Local.setOpcode(WebAssembly::LOCAL);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000174 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
175 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000176 unsigned WAReg = MFI->getWAReg(VReg);
177 // Don't declare unused registers.
178 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
179 continue;
180 // Don't redeclare parameters.
181 if (WAReg < MFI->getParams().size())
182 continue;
183 // Don't declare stackified registers.
184 if (int(WAReg) < 0)
185 continue;
Dan Gohman53828fd2015-11-23 16:50:18 +0000186 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
187 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000188 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000189 if (AnyWARegs)
190 EmitToStreamer(*OutStreamer, Local);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000191
Dan Gohmane51c0582015-10-06 00:27:55 +0000192 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000193}
194
JF Bastienb9073fb2015-07-22 21:28:15 +0000195void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000196 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000197
Dan Gohmane51c0582015-10-06 00:27:55 +0000198 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000199 case WebAssembly::ARGUMENT_I32:
200 case WebAssembly::ARGUMENT_I64:
201 case WebAssembly::ARGUMENT_F32:
202 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000203 // These represent values which are live into the function entry, so there's
204 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000205 break;
Dan Gohmanf6857222015-11-23 19:12:37 +0000206 case WebAssembly::LOOP_END:
207 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
208 // fallthrough which nevertheless requires a label for the destination here.
209 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000210 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000211 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
212 MCInst TmpInst;
213 MCInstLowering.Lower(MI, TmpInst);
214 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000215 break;
216 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000217 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000218}
219
JF Bastien1a59c6b2015-10-21 02:23:09 +0000220void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
Dan Gohman754cd112015-11-11 01:33:02 +0000221 const DataLayout &DL = M.getDataLayout();
222
JF Bastien5789a692015-10-30 16:41:21 +0000223 SmallString<128> Str;
224 raw_svector_ostream OS(Str);
JF Bastien1a59c6b2015-10-21 02:23:09 +0000225 for (const Function &F : M)
226 if (F.isDeclarationForLinker()) {
227 assert(F.hasName() && "imported functions must have a name");
Dan Gohman754cd112015-11-11 01:33:02 +0000228 if (F.isIntrinsic())
JF Bastien5789a692015-10-30 16:41:21 +0000229 continue;
JF Bastien1a59c6b2015-10-21 02:23:09 +0000230 if (Str.empty())
JF Bastien5789a692015-10-30 16:41:21 +0000231 OS << "\t.imports\n";
Dan Gohman754cd112015-11-11 01:33:02 +0000232
Dan Gohmancf4748f2015-11-12 17:04:33 +0000233 MCSymbol *Sym = OutStreamer->getContext().getOrCreateSymbol(F.getName());
234 OS << "\t.import " << *Sym << " \"\" " << *Sym;
Dan Gohman754cd112015-11-11 01:33:02 +0000235
236 const WebAssemblyTargetLowering &TLI =
237 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
238
239 // If we need to legalize the return type, it'll get converted into
240 // passing a pointer.
241 bool SawParam = false;
242 SmallVector<MVT, 4> ResultVTs;
Derek Schuff46e33162015-11-16 21:12:41 +0000243 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman754cd112015-11-11 01:33:02 +0000244 if (ResultVTs.size() > 1) {
245 ResultVTs.clear();
246 OS << " (param " << toString(TLI.getPointerTy(DL));
247 SawParam = true;
248 }
249
250 for (const Argument &A : F.args()) {
251 SmallVector<MVT, 4> ParamVTs;
Derek Schuff46e33162015-11-16 21:12:41 +0000252 ComputeLegalValueVTs(F, TM, A.getType(), ParamVTs);
253 for (MVT VT : ParamVTs) {
Dan Gohman754cd112015-11-11 01:33:02 +0000254 if (!SawParam) {
255 OS << " (param";
256 SawParam = true;
257 }
Derek Schuff46e33162015-11-16 21:12:41 +0000258 OS << ' ' << toString(VT);
Dan Gohman754cd112015-11-11 01:33:02 +0000259 }
260 }
261 if (SawParam)
262 OS << ')';
263
Derek Schuff46e33162015-11-16 21:12:41 +0000264 for (MVT VT : ResultVTs)
265 OS << " (result " << toString(VT) << ')';
Dan Gohman754cd112015-11-11 01:33:02 +0000266
JF Bastien1a59c6b2015-10-21 02:23:09 +0000267 OS << '\n';
JF Bastien5789a692015-10-30 16:41:21 +0000268 }
Dan Gohman754cd112015-11-11 01:33:02 +0000269
270 StringRef Text = OS.str();
271 if (!Text.empty())
272 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
Dan Gohman53828fd2015-11-23 16:50:18 +0000273
274 AsmPrinter::EmitEndOfAsmFile(M);
JF Bastien1a59c6b2015-10-21 02:23:09 +0000275}
276
Dan Gohmanf19ed562015-11-13 01:42:29 +0000277bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
278 unsigned OpNo, unsigned AsmVariant,
279 const char *ExtraCode,
280 raw_ostream &OS) {
281 if (AsmVariant != 0)
282 report_fatal_error("There are no defined alternate asm variants");
283
284 if (!ExtraCode) {
285 const MachineOperand &MO = MI->getOperand(OpNo);
286 if (MO.isImm())
287 OS << MO.getImm();
288 else
289 OS << regToString(MO);
290 return false;
291 }
292
293 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
294}
295
296bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
297 unsigned OpNo,
298 unsigned AsmVariant,
299 const char *ExtraCode,
300 raw_ostream &OS) {
301 if (AsmVariant != 0)
302 report_fatal_error("There are no defined alternate asm variants");
303
304 if (!ExtraCode) {
305 OS << regToString(MI->getOperand(OpNo));
306 return false;
307 }
308
309 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
310}
311
JF Bastienb9073fb2015-07-22 21:28:15 +0000312// Force static initialization.
313extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
314 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
315 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
316}