blob: 5f7f3d694cd4cc90cefdf6f3c5ddc7bb3d016d80 [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
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"
JF Bastienb9073fb2015-07-22 21:28:15 +000034#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000035#include "llvm/MC/MCSymbol.h"
Sam Clegg7736855d2017-06-13 01:42:21 +000036#include "llvm/MC/MCSymbolWasm.h"
37#include "llvm/MC/MCSymbolELF.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;
JF Bastien1d20a5e2015-10-16 00:53:49 +000056 DEBUG(errs() << "Unknown type for register number: " << RegNo);
57 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);
Sam Clegg9bf73c02017-07-05 20:25:08 +000087 getTargetStreamer()->emitIndirectFunctionType(getSymbol(&F), Params,
Dan Gohman2726b882016-10-06 22:29:32 +000088 Results);
Derek Schuff5859a9ed2016-06-03 18:34:36 +000089 }
90 }
Derek Schuff7747d703e2016-12-01 00:11:15 +000091 for (const auto &G : M.globals()) {
92 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohmand934cb82017-02-24 23:18:00 +000093 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
Dan Gohmanf7172f42017-12-05 17:21:57 +000094 if (TM.getTargetTriple().isOSBinFormatELF())
95 getTargetStreamer()->emitGlobalImport(G.getGlobalIdentifier());
Dan Gohmand934cb82017-02-24 23:18:00 +000096 OutStreamer->emitELFSize(getSymbol(&G),
97 MCConstantExpr::create(Size, OutContext));
Derek Schuff7747d703e2016-12-01 00:11:15 +000098 }
99 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000100}
101
102void WebAssemblyAsmPrinter::EmitConstantPool() {
103 assert(MF->getConstantPool()->getConstants().empty() &&
104 "WebAssembly disables constant pools");
105}
106
107void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
108 // Nothing to do; jump tables are incorporated into the instruction stream.
109}
110
JF Bastienb6091df2015-08-25 22:58:05 +0000111void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000112 getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000113
Derek Schuff46e33162015-11-16 21:12:41 +0000114 SmallVector<MVT, 4> ResultVTs;
115 const Function &F(*MF->getFunction());
Derek Schuffc64d7652016-08-01 22:25:02 +0000116
117 // Emit the function index.
118 if (MDNode *Idx = F.getMetadata("wasm.index")) {
119 assert(Idx->getNumOperands() == 1);
120
121 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
122 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
123 }
124
Derek Schuff46e33162015-11-16 21:12:41 +0000125 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000126
Derek Schuff46e33162015-11-16 21:12:41 +0000127 // If the return type needs to be legalized it will get converted into
128 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000129 if (ResultVTs.size() == 1)
Dan Gohmand934cb82017-02-24 23:18:00 +0000130 getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
131 else
132 getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
JF Bastienb6091df2015-08-25 22:58:05 +0000133
Dan Gohmand934cb82017-02-24 23:18:00 +0000134 if (TM.getTargetTriple().isOSBinFormatELF()) {
135 assert(MFI->getLocals().empty());
136 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
137 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
138 unsigned WAReg = MFI->getWAReg(VReg);
139 // Don't declare unused registers.
140 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
141 continue;
142 // Don't redeclare parameters.
143 if (WAReg < MFI->getParams().size())
144 continue;
145 // Don't declare stackified registers.
146 if (int(WAReg) < 0)
147 continue;
148 MFI->addLocal(getRegType(VReg));
149 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000150 }
Dan Gohman3acb1872016-10-24 23:27:49 +0000151
152 getTargetStreamer()->emitLocal(MFI->getLocals());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000153
Dan Gohmane51c0582015-10-06 00:27:55 +0000154 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000155}
156
Dan Gohman3469ee12016-01-12 20:30:51 +0000157void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000158 if (TM.getTargetTriple().isOSBinFormatELF())
159 getTargetStreamer()->emitEndFunc();
Dan Gohman3469ee12016-01-12 20:30:51 +0000160}
161
JF Bastienb9073fb2015-07-22 21:28:15 +0000162void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000163 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000164
Dan Gohmane51c0582015-10-06 00:27:55 +0000165 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000166 case WebAssembly::ARGUMENT_I32:
167 case WebAssembly::ARGUMENT_I64:
168 case WebAssembly::ARGUMENT_F32:
169 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000170 case WebAssembly::ARGUMENT_v16i8:
171 case WebAssembly::ARGUMENT_v8i16:
172 case WebAssembly::ARGUMENT_v4i32:
173 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000174 // These represent values which are live into the function entry, so there's
175 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000176 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000177 case WebAssembly::FALLTHROUGH_RETURN_I32:
178 case WebAssembly::FALLTHROUGH_RETURN_I64:
179 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000180 case WebAssembly::FALLTHROUGH_RETURN_F64:
181 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
182 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
183 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
184 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000185 // These instructions represent the implicit return at the end of a
186 // function body. The operand is always a pop.
187 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
188
189 if (isVerbose()) {
190 OutStreamer->AddComment("fallthrough-return: $pop" +
191 utostr(MFI->getWARegStackId(
192 MFI->getWAReg(MI->getOperand(0).getReg()))));
193 OutStreamer->AddBlankLine();
194 }
195 break;
196 }
197 case WebAssembly::FALLTHROUGH_RETURN_VOID:
198 // This instruction represents the implicit return at the end of a
199 // function body with no return value.
200 if (isVerbose()) {
201 OutStreamer->AddComment("fallthrough-return");
202 OutStreamer->AddBlankLine();
203 }
204 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000205 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000206 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
207 MCInst TmpInst;
208 MCInstLowering.Lower(MI, TmpInst);
209 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000210 break;
211 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000212 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000213}
214
Dan Gohman26c67652016-01-11 23:38:05 +0000215const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
216 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Sam Clegg7736855d2017-06-13 01:42:21 +0000217 if (GV->getValueType()->isFunctionTy()) {
Dan Gohman26c67652016-01-11 23:38:05 +0000218 return MCSymbolRefExpr::create(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000219 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
Sam Clegg7736855d2017-06-13 01:42:21 +0000220 }
Dan Gohman26c67652016-01-11 23:38:05 +0000221 return AsmPrinter::lowerConstant(CV);
222}
223
Dan Gohmanf19ed562015-11-13 01:42:29 +0000224bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
225 unsigned OpNo, unsigned AsmVariant,
226 const char *ExtraCode,
227 raw_ostream &OS) {
228 if (AsmVariant != 0)
229 report_fatal_error("There are no defined alternate asm variants");
230
Dan Gohman30a42bf2015-12-16 17:15:17 +0000231 // First try the generic code, which knows about modifiers like 'c' and 'n'.
232 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
233 return false;
234
Dan Gohmanf19ed562015-11-13 01:42:29 +0000235 if (!ExtraCode) {
236 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000237 switch (MO.getType()) {
238 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000239 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000240 return false;
241 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000242 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000243 return false;
244 case MachineOperand::MO_GlobalAddress:
245 getSymbol(MO.getGlobal())->print(OS, MAI);
246 printOffset(MO.getOffset(), OS);
247 return false;
248 case MachineOperand::MO_ExternalSymbol:
249 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
250 printOffset(MO.getOffset(), OS);
251 return false;
252 case MachineOperand::MO_MachineBasicBlock:
253 MO.getMBB()->getSymbol()->print(OS, MAI);
254 return false;
255 default:
256 break;
257 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000258 }
259
Dan Gohman30a42bf2015-12-16 17:15:17 +0000260 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000261}
262
263bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
264 unsigned OpNo,
265 unsigned AsmVariant,
266 const char *ExtraCode,
267 raw_ostream &OS) {
268 if (AsmVariant != 0)
269 report_fatal_error("There are no defined alternate asm variants");
270
Dan Gohmanb465aa02017-11-08 19:18:08 +0000271 // The current approach to inline asm is that "r" constraints are expressed
272 // as local indices, rather than values on the operand stack. This simplifies
273 // using "r" as it eliminates the need to push and pop the values in a
274 // particular order, however it also makes it impossible to have an "m"
275 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000276
277 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
278}
279
JF Bastienb9073fb2015-07-22 21:28:15 +0000280// Force static initialization.
281extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000282 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
283 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000284}