blob: a224ae7316af15cdf84b45cba4383af936bdf771 [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file contains a printer that converts from our internal
JF Bastienb9073fb2015-07-22 21:28:15 +000012/// representation of machine-dependent LLVM code to the WebAssembly assembly
13/// language.
14///
15//===----------------------------------------------------------------------===//
16
Dan Gohmand934cb82017-02-24 23:18:00 +000017#include "WebAssemblyAsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "InstPrinter/WebAssemblyInstPrinter.h"
19#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000020#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Derek Schuffc64d7652016-08-01 22:25:02 +000021#include "WebAssembly.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000022#include "WebAssemblyMCInstLower.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000023#include "WebAssemblyMachineFunctionInfo.h"
24#include "WebAssemblyRegisterInfo.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"
Dan Gohman82607f52017-02-24 23:46:05 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000031#include "llvm/IR/DataLayout.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000032#include "llvm/IR/GlobalVariable.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000033#include "llvm/MC/MCContext.h"
Sam Cleggcfd44a22018-04-05 17:01:39 +000034#include "llvm/MC/MCSectionWasm.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000035#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000036#include "llvm/MC/MCSymbol.h"
Sam Clegg7736855d2017-06-13 01:42:21 +000037#include "llvm/MC/MCSymbolELF.h"
Sam Cleggcfd44a22018-04-05 17:01:39 +000038#include "llvm/MC/MCSymbolWasm.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000042using namespace llvm;
43
44#define DEBUG_TYPE "asm-printer"
45
JF Bastienb9073fb2015-07-22 21:28:15 +000046//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000047// Helpers.
48//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000049
Dan Gohman53828fd2015-11-23 16:50:18 +000050MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000051 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Dan Gohman0cfb5f82016-05-10 04:24:02 +000052 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
Derek Schuff39bf39f2016-08-02 23:16:09 +000053 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
54 MVT::v4i32, MVT::v4f32})
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000055 if (TRI->isTypeLegalForClass(*TRC, T))
Dan Gohman53828fd2015-11-23 16:50:18 +000056 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +000057 DEBUG(errs() << "Unknown type for register number: " << RegNo);
58 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +000059 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +000060}
61
JF Bastien1d20a5e2015-10-16 00:53:49 +000062std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
63 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +000064 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
65 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +000066 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +000067 unsigned WAReg = MFI->getWAReg(RegNo);
68 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000069 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +000070}
71
Dan Gohmanec977b02016-01-25 15:12:05 +000072WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +000073 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
74 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +000075}
76
JF Bastien45479f62015-08-26 22:09:54 +000077//===----------------------------------------------------------------------===//
78// WebAssemblyAsmPrinter Implementation.
79//===----------------------------------------------------------------------===//
Derek Schuff46e33162015-11-16 21:12:41 +000080
Derek Schuff5859a9ed2016-06-03 18:34:36 +000081void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
82 for (const auto &F : M) {
83 // Emit function type info for all undefined functions
84 if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
Dan Gohman2726b882016-10-06 22:29:32 +000085 SmallVector<MVT, 4> Results;
86 SmallVector<MVT, 4> Params;
87 ComputeSignatureVTs(F, TM, Params, Results);
Jacob Gravelleca358da2018-02-12 21:41:12 +000088 MCSymbol *Sym = getSymbol(&F);
Dan Gohmandb1916a2018-02-09 23:13:22 +000089 getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
90
Jacob Gravelleca358da2018-02-12 21:41:12 +000091 if (TM.getTargetTriple().isOSBinFormatWasm() &&
92 F.hasFnAttribute("wasm-import-module")) {
93 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
Dan Gohmandb1916a2018-02-09 23:13:22 +000094 StringRef Name = F.getFnAttribute("wasm-import-module")
95 .getValueAsString();
Jacob Gravelleca358da2018-02-12 21:41:12 +000096 getTargetStreamer()->emitImportModule(WasmSym, Name);
Dan Gohmandb1916a2018-02-09 23:13:22 +000097 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +000098 }
99 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000100 for (const auto &G : M.globals()) {
101 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohman5cf64732017-12-07 00:14:30 +0000102 if (G.getValueType()->isSized()) {
103 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
104 if (TM.getTargetTriple().isOSBinFormatELF())
105 getTargetStreamer()->emitGlobalImport(G.getGlobalIdentifier());
106 OutStreamer->emitELFSize(getSymbol(&G),
107 MCConstantExpr::create(Size, OutContext));
108 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000109 }
110 }
Sam Cleggcfd44a22018-04-05 17:01:39 +0000111
112 if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
113 for (const Metadata *MD : Named->operands()) {
114 const MDTuple *Tuple = dyn_cast<MDTuple>(MD);
115 if (!Tuple || Tuple->getNumOperands() != 2)
116 continue;
117 const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
118 const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
119 if (!Name || !Contents)
120 continue;
121
122 OutStreamer->PushSection();
123 std::string SectionName = (".custom_section." + Name->getString()).str();
124 MCSectionWasm *mySection =
125 OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
126 OutStreamer->SwitchSection(mySection);
127 OutStreamer->EmitBytes(Contents->getString());
128 OutStreamer->PopSection();
129 }
130 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000131}
132
133void WebAssemblyAsmPrinter::EmitConstantPool() {
134 assert(MF->getConstantPool()->getConstants().empty() &&
135 "WebAssembly disables constant pools");
136}
137
138void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
139 // Nothing to do; jump tables are incorporated into the instruction stream.
140}
141
JF Bastienb6091df2015-08-25 22:58:05 +0000142void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000143 getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000144
Derek Schuff46e33162015-11-16 21:12:41 +0000145 SmallVector<MVT, 4> ResultVTs;
David Blaikie21109242017-12-15 23:52:06 +0000146 const Function &F = MF->getFunction();
Derek Schuffc64d7652016-08-01 22:25:02 +0000147
148 // Emit the function index.
149 if (MDNode *Idx = F.getMetadata("wasm.index")) {
150 assert(Idx->getNumOperands() == 1);
151
152 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
153 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
154 }
155
Derek Schuff46e33162015-11-16 21:12:41 +0000156 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000157
Derek Schuff46e33162015-11-16 21:12:41 +0000158 // If the return type needs to be legalized it will get converted into
159 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000160 if (ResultVTs.size() == 1)
Dan Gohmand934cb82017-02-24 23:18:00 +0000161 getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
162 else
163 getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
JF Bastienb6091df2015-08-25 22:58:05 +0000164
Dan Gohmand934cb82017-02-24 23:18:00 +0000165 if (TM.getTargetTriple().isOSBinFormatELF()) {
166 assert(MFI->getLocals().empty());
167 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
168 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
169 unsigned WAReg = MFI->getWAReg(VReg);
170 // Don't declare unused registers.
171 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
172 continue;
173 // Don't redeclare parameters.
174 if (WAReg < MFI->getParams().size())
175 continue;
176 // Don't declare stackified registers.
177 if (int(WAReg) < 0)
178 continue;
179 MFI->addLocal(getRegType(VReg));
180 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000181 }
Dan Gohman3acb1872016-10-24 23:27:49 +0000182
183 getTargetStreamer()->emitLocal(MFI->getLocals());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000184
Dan Gohmane51c0582015-10-06 00:27:55 +0000185 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000186}
187
Dan Gohman3469ee12016-01-12 20:30:51 +0000188void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000189 if (TM.getTargetTriple().isOSBinFormatELF())
190 getTargetStreamer()->emitEndFunc();
Dan Gohman3469ee12016-01-12 20:30:51 +0000191}
192
JF Bastienb9073fb2015-07-22 21:28:15 +0000193void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000194 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000195
Dan Gohmane51c0582015-10-06 00:27:55 +0000196 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000197 case WebAssembly::ARGUMENT_I32:
198 case WebAssembly::ARGUMENT_I64:
199 case WebAssembly::ARGUMENT_F32:
200 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000201 case WebAssembly::ARGUMENT_v16i8:
202 case WebAssembly::ARGUMENT_v8i16:
203 case WebAssembly::ARGUMENT_v4i32:
204 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000205 // These represent values which are live into the function entry, so there's
206 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000207 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000208 case WebAssembly::FALLTHROUGH_RETURN_I32:
209 case WebAssembly::FALLTHROUGH_RETURN_I64:
210 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000211 case WebAssembly::FALLTHROUGH_RETURN_F64:
212 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
213 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
214 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
215 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000216 // These instructions represent the implicit return at the end of a
217 // function body. The operand is always a pop.
218 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
219
220 if (isVerbose()) {
221 OutStreamer->AddComment("fallthrough-return: $pop" +
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000222 Twine(MFI->getWARegStackId(
Dan Gohmanb7c24002016-05-21 00:21:56 +0000223 MFI->getWAReg(MI->getOperand(0).getReg()))));
224 OutStreamer->AddBlankLine();
225 }
226 break;
227 }
228 case WebAssembly::FALLTHROUGH_RETURN_VOID:
229 // This instruction represents the implicit return at the end of a
230 // function body with no return value.
231 if (isVerbose()) {
232 OutStreamer->AddComment("fallthrough-return");
233 OutStreamer->AddBlankLine();
234 }
235 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000236 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000237 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
238 MCInst TmpInst;
239 MCInstLowering.Lower(MI, TmpInst);
240 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000241 break;
242 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000243 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000244}
245
Dan Gohman26c67652016-01-11 23:38:05 +0000246const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
247 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Sam Clegg7736855d2017-06-13 01:42:21 +0000248 if (GV->getValueType()->isFunctionTy()) {
Dan Gohman26c67652016-01-11 23:38:05 +0000249 return MCSymbolRefExpr::create(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000250 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
Sam Clegg7736855d2017-06-13 01:42:21 +0000251 }
Dan Gohman26c67652016-01-11 23:38:05 +0000252 return AsmPrinter::lowerConstant(CV);
253}
254
Dan Gohmanf19ed562015-11-13 01:42:29 +0000255bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
256 unsigned OpNo, unsigned AsmVariant,
257 const char *ExtraCode,
258 raw_ostream &OS) {
259 if (AsmVariant != 0)
260 report_fatal_error("There are no defined alternate asm variants");
261
Dan Gohman30a42bf2015-12-16 17:15:17 +0000262 // First try the generic code, which knows about modifiers like 'c' and 'n'.
263 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
264 return false;
265
Dan Gohmanf19ed562015-11-13 01:42:29 +0000266 if (!ExtraCode) {
267 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000268 switch (MO.getType()) {
269 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000270 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000271 return false;
272 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000273 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000274 return false;
275 case MachineOperand::MO_GlobalAddress:
276 getSymbol(MO.getGlobal())->print(OS, MAI);
277 printOffset(MO.getOffset(), OS);
278 return false;
279 case MachineOperand::MO_ExternalSymbol:
280 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
281 printOffset(MO.getOffset(), OS);
282 return false;
283 case MachineOperand::MO_MachineBasicBlock:
284 MO.getMBB()->getSymbol()->print(OS, MAI);
285 return false;
286 default:
287 break;
288 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000289 }
290
Dan Gohman30a42bf2015-12-16 17:15:17 +0000291 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000292}
293
294bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
295 unsigned OpNo,
296 unsigned AsmVariant,
297 const char *ExtraCode,
298 raw_ostream &OS) {
299 if (AsmVariant != 0)
300 report_fatal_error("There are no defined alternate asm variants");
301
Dan Gohmanb465aa02017-11-08 19:18:08 +0000302 // The current approach to inline asm is that "r" constraints are expressed
303 // as local indices, rather than values on the operand stack. This simplifies
304 // using "r" as it eliminates the need to push and pop the values in a
305 // particular order, however it also makes it impossible to have an "m"
306 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000307
308 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
309}
310
JF Bastienb9073fb2015-07-22 21:28:15 +0000311// Force static initialization.
312extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000313 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
314 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000315}