blob: 6fc309f02615e1f604f71ca465437fb05c1dd62e [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 Gohman979840d2015-09-23 16:59:10 +000080
JF Bastien1d20a5e2015-10-16 00:53:49 +000081 std::string getRegTypeName(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000082 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000083 std::string regToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000084};
85
86} // end anonymous namespace
87
88//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000089// Helpers.
90//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000091
JF Bastien1d20a5e2015-10-16 00:53:49 +000092std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const {
93 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
94 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
95 if (TRC->hasType(T))
96 return EVT(T).getEVTString();
97 DEBUG(errs() << "Unknown type for register number: " << RegNo);
98 llvm_unreachable("Unknown register type");
99 return "?";
100}
101
JF Bastien1d20a5e2015-10-16 00:53:49 +0000102std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
103 unsigned RegNo = MO.getReg();
Dan Gohmane51c0582015-10-06 00:27:55 +0000104 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
105 return WebAssemblyInstPrinter::getRegisterName(RegNo);
106
Dan Gohmancf4748f2015-11-12 17:04:33 +0000107 return utostr(MFI->getWAReg(RegNo));
Dan Gohmane51c0582015-10-06 00:27:55 +0000108}
109
Dan Gohman754cd112015-11-11 01:33:02 +0000110const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
111 switch (VT.SimpleTy) {
Dan Gohman979840d2015-09-23 16:59:10 +0000112 default:
113 break;
Dan Gohman754cd112015-11-11 01:33:02 +0000114 case MVT::f32:
Dan Gohman979840d2015-09-23 16:59:10 +0000115 return "f32";
Dan Gohman754cd112015-11-11 01:33:02 +0000116 case MVT::f64:
Dan Gohman979840d2015-09-23 16:59:10 +0000117 return "f64";
Dan Gohman754cd112015-11-11 01:33:02 +0000118 case MVT::i32:
119 return "i32";
120 case MVT::i64:
121 return "i64";
JF Bastien73ff6af2015-08-31 22:24:11 +0000122 }
Dan Gohman754cd112015-11-11 01:33:02 +0000123 DEBUG(dbgs() << "Invalid type " << EVT(VT).getEVTString() << '\n');
JF Bastien73ff6af2015-08-31 22:24:11 +0000124 llvm_unreachable("invalid type");
125 return "<invalid>";
126}
127
JF Bastien45479f62015-08-26 22:09:54 +0000128//===----------------------------------------------------------------------===//
129// WebAssemblyAsmPrinter Implementation.
130//===----------------------------------------------------------------------===//
131
JF Bastien54be3b12015-08-25 23:19:49 +0000132void WebAssemblyAsmPrinter::EmitConstantPool() {
133 assert(MF->getConstantPool()->getConstants().empty() &&
134 "WebAssembly disables constant pools");
135}
136
Dan Gohman950a13c2015-09-16 16:51:30 +0000137void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
138 // Nothing to do; jump tables are incorporated into the instruction stream.
139}
140
JF Bastienb6091df2015-08-25 22:58:05 +0000141void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
JF Bastien1d20a5e2015-10-16 00:53:49 +0000142 SmallString<128> Str;
143 raw_svector_ostream OS(Str);
Dan Gohmane51c0582015-10-06 00:27:55 +0000144
Dan Gohmancf4748f2015-11-12 17:04:33 +0000145 for (MVT VT : MFI->getParams())
146 OS << "\t" ".param " << toString(VT) << '\n';
147 for (MVT VT : MFI->getResults())
148 OS << "\t" ".result " << toString(VT) << '\n';
JF Bastienb6091df2015-08-25 22:58:05 +0000149
JF Bastien1d20a5e2015-10-16 00:53:49 +0000150 bool FirstVReg = true;
151 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
152 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohmancf4748f2015-11-12 17:04:33 +0000153 if (!MRI->use_empty(VReg)) {
154 if (FirstVReg)
155 OS << "\t" ".local ";
156 else
157 OS << ", ";
158 OS << getRegTypeName(VReg);
159 FirstVReg = false;
160 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000161 }
Dan Gohman754cd112015-11-11 01:33:02 +0000162 if (!FirstVReg)
163 OS << '\n';
JF Bastien1d20a5e2015-10-16 00:53:49 +0000164
Dan Gohman754cd112015-11-11 01:33:02 +0000165 // EmitRawText appends a newline, so strip off the last newline.
166 StringRef Text = OS.str();
167 if (!Text.empty())
168 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
Dan Gohmane51c0582015-10-06 00:27:55 +0000169 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000170}
171
JF Bastienb9073fb2015-07-22 21:28:15 +0000172void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000173 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000174
David Blaikieb0311c52015-11-12 19:07:43 +0000175 assert(MI->getDesc().getNumDefs() <= 1 &&
JF Bastien600aee92015-07-31 17:53:38 +0000176 "Instructions with multiple result values not implemented");
177
Dan Gohmane51c0582015-10-06 00:27:55 +0000178 switch (MI->getOpcode()) {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000179 case TargetOpcode::COPY: {
180 // TODO: Figure out a way to lower COPY instructions to MCInst form.
181 SmallString<128> Str;
182 raw_svector_ostream OS(Str);
183 OS << "\t" "set_local " << regToString(MI->getOperand(0)) << ", "
184 "(get_local " << regToString(MI->getOperand(1)) << ")";
185 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000186 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000187 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000188 case WebAssembly::ARGUMENT_I32:
189 case WebAssembly::ARGUMENT_I64:
190 case WebAssembly::ARGUMENT_F32:
191 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000192 // These represent values which are live into the function entry, so there's
193 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000194 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000195 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000196 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
197 MCInst TmpInst;
198 MCInstLowering.Lower(MI, TmpInst);
199 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000200 break;
201 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000202 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000203}
204
Dan Gohman754cd112015-11-11 01:33:02 +0000205static void ComputeLegalValueVTs(LLVMContext &Context,
206 const WebAssemblyTargetLowering &TLI,
207 const DataLayout &DL, Type *Ty,
208 SmallVectorImpl<MVT> &ValueVTs) {
209 SmallVector<EVT, 4> VTs;
210 ComputeValueVTs(TLI, DL, Ty, VTs);
211
212 for (EVT VT : VTs) {
213 unsigned NumRegs = TLI.getNumRegisters(Context, VT);
214 MVT RegisterVT = TLI.getRegisterType(Context, VT);
215 for (unsigned i = 0; i != NumRegs; ++i)
216 ValueVTs.push_back(RegisterVT);
217 }
218}
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;
243 ComputeLegalValueVTs(M.getContext(), TLI, DL, F.getReturnType(),
244 ResultVTs);
245 if (ResultVTs.size() > 1) {
246 ResultVTs.clear();
247 OS << " (param " << toString(TLI.getPointerTy(DL));
248 SawParam = true;
249 }
250
251 for (const Argument &A : F.args()) {
252 SmallVector<MVT, 4> ParamVTs;
253 ComputeLegalValueVTs(M.getContext(), TLI, DL, A.getType(), ParamVTs);
254 for (EVT VT : ParamVTs) {
255 if (!SawParam) {
256 OS << " (param";
257 SawParam = true;
258 }
259 OS << ' ' << toString(VT.getSimpleVT());
260 }
261 }
262 if (SawParam)
263 OS << ')';
264
265 for (EVT VT : ResultVTs)
266 OS << " (result " << toString(VT.getSimpleVT()) << ')';
267
JF Bastien1a59c6b2015-10-21 02:23:09 +0000268 OS << '\n';
JF Bastien5789a692015-10-30 16:41:21 +0000269 }
Dan Gohman754cd112015-11-11 01:33:02 +0000270
271 StringRef Text = OS.str();
272 if (!Text.empty())
273 OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
JF Bastien1a59c6b2015-10-21 02:23:09 +0000274}
275
JF Bastienb9073fb2015-07-22 21:28:15 +0000276// Force static initialization.
277extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
278 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
279 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
280}