blob: 1f280e1d13fc4bcb8b744c9ba235a5d517ffcb38 [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 Cleggcfd44a22018-04-05 17:01:39 +000037#include "llvm/MC/MCSymbolWasm.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000038#include "llvm/Support/Debug.h"
39#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000041using namespace llvm;
42
43#define DEBUG_TYPE "asm-printer"
44
JF Bastienb9073fb2015-07-22 21:28:15 +000045//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000046// Helpers.
47//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000048
Dan Gohman53828fd2015-11-23 16:50:18 +000049MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000050 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Dan Gohman0cfb5f82016-05-10 04:24:02 +000051 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
Derek Schuff39bf39f2016-08-02 23:16:09 +000052 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
53 MVT::v4i32, MVT::v4f32})
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000054 if (TRI->isTypeLegalForClass(*TRC, T))
Dan Gohman53828fd2015-11-23 16:50:18 +000055 return T;
Nicola Zaghend34e60c2018-05-14 12:53:11 +000056 LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
JF Bastien1d20a5e2015-10-16 00:53:49 +000057 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +000058 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +000059}
60
JF Bastien1d20a5e2015-10-16 00:53:49 +000061std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
62 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +000063 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
64 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +000065 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +000066 unsigned WAReg = MFI->getWAReg(RegNo);
67 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000068 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +000069}
70
Dan Gohmanec977b02016-01-25 15:12:05 +000071WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +000072 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
73 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +000074}
75
JF Bastien45479f62015-08-26 22:09:54 +000076//===----------------------------------------------------------------------===//
77// WebAssemblyAsmPrinter Implementation.
78//===----------------------------------------------------------------------===//
Derek Schuff46e33162015-11-16 21:12:41 +000079
Derek Schuff5859a9ed2016-06-03 18:34:36 +000080void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
81 for (const auto &F : M) {
82 // Emit function type info for all undefined functions
83 if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
Dan Gohman2726b882016-10-06 22:29:32 +000084 SmallVector<MVT, 4> Results;
85 SmallVector<MVT, 4> Params;
86 ComputeSignatureVTs(F, TM, Params, Results);
Jacob Gravelleca358da2018-02-12 21:41:12 +000087 MCSymbol *Sym = getSymbol(&F);
Dan Gohmandb1916a2018-02-09 23:13:22 +000088 getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
89
Jacob Gravelleca358da2018-02-12 21:41:12 +000090 if (TM.getTargetTriple().isOSBinFormatWasm() &&
91 F.hasFnAttribute("wasm-import-module")) {
92 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
Dan Gohmandb1916a2018-02-09 23:13:22 +000093 StringRef Name = F.getFnAttribute("wasm-import-module")
94 .getValueAsString();
Jacob Gravelleca358da2018-02-12 21:41:12 +000095 getTargetStreamer()->emitImportModule(WasmSym, Name);
Dan Gohmandb1916a2018-02-09 23:13:22 +000096 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +000097 }
98 }
Derek Schuff7747d703e2016-12-01 00:11:15 +000099 for (const auto &G : M.globals()) {
100 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohman5cf64732017-12-07 00:14:30 +0000101 if (G.getValueType()->isSized()) {
102 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
Dan Gohman5cf64732017-12-07 00:14:30 +0000103 OutStreamer->emitELFSize(getSymbol(&G),
104 MCConstantExpr::create(Size, OutContext));
105 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000106 }
107 }
Sam Cleggcfd44a22018-04-05 17:01:39 +0000108
109 if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
110 for (const Metadata *MD : Named->operands()) {
111 const MDTuple *Tuple = dyn_cast<MDTuple>(MD);
112 if (!Tuple || Tuple->getNumOperands() != 2)
113 continue;
114 const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
115 const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
116 if (!Name || !Contents)
117 continue;
118
119 OutStreamer->PushSection();
120 std::string SectionName = (".custom_section." + Name->getString()).str();
121 MCSectionWasm *mySection =
122 OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
123 OutStreamer->SwitchSection(mySection);
124 OutStreamer->EmitBytes(Contents->getString());
125 OutStreamer->PopSection();
126 }
127 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000128}
129
130void WebAssemblyAsmPrinter::EmitConstantPool() {
131 assert(MF->getConstantPool()->getConstants().empty() &&
132 "WebAssembly disables constant pools");
133}
134
135void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
136 // Nothing to do; jump tables are incorporated into the instruction stream.
137}
138
JF Bastienb6091df2015-08-25 22:58:05 +0000139void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000140 getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000141
Derek Schuff46e33162015-11-16 21:12:41 +0000142 SmallVector<MVT, 4> ResultVTs;
David Blaikie21109242017-12-15 23:52:06 +0000143 const Function &F = MF->getFunction();
Derek Schuffc64d7652016-08-01 22:25:02 +0000144
145 // Emit the function index.
146 if (MDNode *Idx = F.getMetadata("wasm.index")) {
147 assert(Idx->getNumOperands() == 1);
148
149 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
150 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
151 }
152
Derek Schuff46e33162015-11-16 21:12:41 +0000153 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000154
Derek Schuff46e33162015-11-16 21:12:41 +0000155 // If the return type needs to be legalized it will get converted into
156 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000157 if (ResultVTs.size() == 1)
Dan Gohmand934cb82017-02-24 23:18:00 +0000158 getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
159 else
160 getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
JF Bastienb6091df2015-08-25 22:58:05 +0000161
Dan Gohman3acb1872016-10-24 23:27:49 +0000162 getTargetStreamer()->emitLocal(MFI->getLocals());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000163
Dan Gohmane51c0582015-10-06 00:27:55 +0000164 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000165}
166
JF Bastienb9073fb2015-07-22 21:28:15 +0000167void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000168 LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000169
Dan Gohmane51c0582015-10-06 00:27:55 +0000170 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000171 case WebAssembly::ARGUMENT_I32:
172 case WebAssembly::ARGUMENT_I64:
173 case WebAssembly::ARGUMENT_F32:
174 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000175 case WebAssembly::ARGUMENT_v16i8:
176 case WebAssembly::ARGUMENT_v8i16:
177 case WebAssembly::ARGUMENT_v4i32:
178 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000179 // These represent values which are live into the function entry, so there's
180 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000181 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000182 case WebAssembly::FALLTHROUGH_RETURN_I32:
183 case WebAssembly::FALLTHROUGH_RETURN_I64:
184 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000185 case WebAssembly::FALLTHROUGH_RETURN_F64:
186 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
187 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
188 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
Wouter van Oortmerssena90d24d2018-07-27 23:19:51 +0000189 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000190 // These instructions represent the implicit return at the end of a
Wouter van Oortmerssena90d24d2018-07-27 23:19:51 +0000191 // function body. The operand is always a pop.
192 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
193
Dan Gohmanb7c24002016-05-21 00:21:56 +0000194 if (isVerbose()) {
Wouter van Oortmerssena90d24d2018-07-27 23:19:51 +0000195 OutStreamer->AddComment("fallthrough-return: $pop" +
196 Twine(MFI->getWARegStackId(
197 MFI->getWAReg(MI->getOperand(0).getReg()))));
Dan Gohmanb7c24002016-05-21 00:21:56 +0000198 OutStreamer->AddBlankLine();
199 }
200 break;
201 }
202 case WebAssembly::FALLTHROUGH_RETURN_VOID:
203 // This instruction represents the implicit return at the end of a
204 // function body with no return value.
205 if (isVerbose()) {
Wouter van Oortmerssena90d24d2018-07-27 23:19:51 +0000206 OutStreamer->AddComment("fallthrough-return");
Dan Gohmanb7c24002016-05-21 00:21:56 +0000207 OutStreamer->AddBlankLine();
208 }
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
Dan Gohman26c67652016-01-11 23:38:05 +0000220const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
221 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Sam Clegg7736855d2017-06-13 01:42:21 +0000222 if (GV->getValueType()->isFunctionTy()) {
Dan Gohman26c67652016-01-11 23:38:05 +0000223 return MCSymbolRefExpr::create(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000224 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
Sam Clegg7736855d2017-06-13 01:42:21 +0000225 }
Dan Gohman26c67652016-01-11 23:38:05 +0000226 return AsmPrinter::lowerConstant(CV);
227}
228
Dan Gohmanf19ed562015-11-13 01:42:29 +0000229bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
230 unsigned OpNo, unsigned AsmVariant,
231 const char *ExtraCode,
232 raw_ostream &OS) {
233 if (AsmVariant != 0)
234 report_fatal_error("There are no defined alternate asm variants");
235
Dan Gohman30a42bf2015-12-16 17:15:17 +0000236 // First try the generic code, which knows about modifiers like 'c' and 'n'.
237 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
238 return false;
239
Dan Gohmanf19ed562015-11-13 01:42:29 +0000240 if (!ExtraCode) {
241 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000242 switch (MO.getType()) {
243 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000244 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000245 return false;
246 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000247 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000248 return false;
249 case MachineOperand::MO_GlobalAddress:
250 getSymbol(MO.getGlobal())->print(OS, MAI);
251 printOffset(MO.getOffset(), OS);
252 return false;
253 case MachineOperand::MO_ExternalSymbol:
254 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
255 printOffset(MO.getOffset(), OS);
256 return false;
257 case MachineOperand::MO_MachineBasicBlock:
258 MO.getMBB()->getSymbol()->print(OS, MAI);
259 return false;
260 default:
261 break;
262 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000263 }
264
Dan Gohman30a42bf2015-12-16 17:15:17 +0000265 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000266}
267
268bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
269 unsigned OpNo,
270 unsigned AsmVariant,
271 const char *ExtraCode,
272 raw_ostream &OS) {
273 if (AsmVariant != 0)
274 report_fatal_error("There are no defined alternate asm variants");
275
Dan Gohmanb465aa02017-11-08 19:18:08 +0000276 // The current approach to inline asm is that "r" constraints are expressed
277 // as local indices, rather than values on the operand stack. This simplifies
278 // using "r" as it eliminates the need to push and pop the values in a
279 // particular order, however it also makes it impossible to have an "m"
280 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000281
282 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
283}
284
JF Bastienb9073fb2015-07-22 21:28:15 +0000285// Force static initialization.
286extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000287 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
288 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000289}