1 //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains a printer that converts from our internal
12 /// representation of machine-dependent LLVM code to the WebAssembly assembly
15 //===----------------------------------------------------------------------===//
17 #include "WebAssembly.h"
18 #include "InstPrinter/WebAssemblyInstPrinter.h"
19 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
20 #include "WebAssemblyMCInstLower.h"
21 #include "WebAssemblyMachineFunctionInfo.h"
22 #include "WebAssemblyRegisterInfo.h"
23 #include "WebAssemblySubtarget.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/CodeGen/Analysis.h"
27 #include "llvm/CodeGen/AsmPrinter.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.h"
40 #define DEBUG_TYPE "asm-printer"
44 class WebAssemblyAsmPrinter final : public AsmPrinter {
45 const MachineRegisterInfo *MRI;
46 const WebAssemblyFunctionInfo *MFI;
49 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
50 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
53 const char *getPassName() const override {
54 return "WebAssembly Assembly Printer";
57 //===------------------------------------------------------------------===//
58 // MachineFunctionPass Implementation.
59 //===------------------------------------------------------------------===//
61 bool runOnMachineFunction(MachineFunction &MF) override {
62 MRI = &MF.getRegInfo();
63 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
64 return AsmPrinter::runOnMachineFunction(MF);
67 //===------------------------------------------------------------------===//
68 // AsmPrinter Implementation.
69 //===------------------------------------------------------------------===//
71 void EmitJumpTableInfo() override;
72 void EmitConstantPool() override;
73 void EmitFunctionBodyStart() override;
74 void EmitInstruction(const MachineInstr *MI) override;
75 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
76 unsigned AsmVariant, const char *ExtraCode,
77 raw_ostream &OS) override;
78 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
79 unsigned AsmVariant, const char *ExtraCode,
80 raw_ostream &OS) override;
82 MVT getRegType(unsigned RegNo) const;
83 const char *toString(MVT VT) const;
84 std::string regToString(const MachineOperand &MO);
87 } // end anonymous namespace
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
93 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
94 const TargetRegisterClass *TRC =
95 TargetRegisterInfo::isVirtualRegister(RegNo) ?
96 MRI->getRegClass(RegNo) :
97 MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
98 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
101 DEBUG(errs() << "Unknown type for register number: " << RegNo);
102 llvm_unreachable("Unknown register type");
106 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
107 unsigned RegNo = MO.getReg();
108 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
109 "Unlowered physical register encountered during assembly printing");
110 assert(!MFI->isVRegStackified(RegNo));
111 unsigned WAReg = MFI->getWAReg(RegNo);
112 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
113 return '$' + utostr(WAReg);
116 const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
117 return WebAssembly::TypeToString(VT);
120 //===----------------------------------------------------------------------===//
121 // WebAssemblyAsmPrinter Implementation.
122 //===----------------------------------------------------------------------===//
124 void WebAssemblyAsmPrinter::EmitConstantPool() {
125 assert(MF->getConstantPool()->getConstants().empty() &&
126 "WebAssembly disables constant pools");
129 void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
130 // Nothing to do; jump tables are incorporated into the instruction stream.
133 static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
134 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
135 const DataLayout &DL(F.getParent()->getDataLayout());
136 const WebAssemblyTargetLowering &TLI =
137 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
138 SmallVector<EVT, 4> VTs;
139 ComputeValueVTs(TLI, DL, Ty, VTs);
142 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
143 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
144 for (unsigned i = 0; i != NumRegs; ++i)
145 ValueVTs.push_back(RegisterVT);
149 void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
150 if (!MFI->getParams().empty()) {
152 Param.setOpcode(WebAssembly::PARAM);
153 for (MVT VT : MFI->getParams())
154 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
155 EmitToStreamer(*OutStreamer, Param);
158 SmallVector<MVT, 4> ResultVTs;
159 const Function &F(*MF->getFunction());
160 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
161 // If the return type needs to be legalized it will get converted into
162 // passing a pointer.
163 if (ResultVTs.size() == 1) {
165 Result.setOpcode(WebAssembly::RESULT);
166 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
167 EmitToStreamer(*OutStreamer, Result);
170 bool AnyWARegs = false;
172 Local.setOpcode(WebAssembly::LOCAL);
173 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
174 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
175 unsigned WAReg = MFI->getWAReg(VReg);
176 // Don't declare unused registers.
177 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
179 // Don't redeclare parameters.
180 if (WAReg < MFI->getParams().size())
182 // Don't declare stackified registers.
185 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
188 auto &PhysRegs = MFI->getPhysRegs();
189 for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
190 if (PhysRegs[PReg] == -1U)
192 Local.addOperand(MCOperand::createImm(getRegType(PReg).SimpleTy));
196 EmitToStreamer(*OutStreamer, Local);
198 AsmPrinter::EmitFunctionBodyStart();
201 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
202 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
204 switch (MI->getOpcode()) {
205 case WebAssembly::ARGUMENT_I32:
206 case WebAssembly::ARGUMENT_I64:
207 case WebAssembly::ARGUMENT_F32:
208 case WebAssembly::ARGUMENT_F64:
209 // These represent values which are live into the function entry, so there's
210 // no instruction to emit.
212 case WebAssembly::LOOP_END:
213 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
214 // fallthrough which nevertheless requires a label for the destination here.
217 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
219 MCInstLowering.Lower(MI, TmpInst);
220 EmitToStreamer(*OutStreamer, TmpInst);
226 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
227 unsigned OpNo, unsigned AsmVariant,
228 const char *ExtraCode,
231 report_fatal_error("There are no defined alternate asm variants");
233 // First try the generic code, which knows about modifiers like 'c' and 'n'.
234 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
238 const MachineOperand &MO = MI->getOperand(OpNo);
239 switch (MO.getType()) {
240 case MachineOperand::MO_Immediate:
243 case MachineOperand::MO_Register:
244 OS << regToString(MO);
246 case MachineOperand::MO_GlobalAddress:
247 getSymbol(MO.getGlobal())->print(OS, MAI);
248 printOffset(MO.getOffset(), OS);
250 case MachineOperand::MO_ExternalSymbol:
251 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
252 printOffset(MO.getOffset(), OS);
254 case MachineOperand::MO_MachineBasicBlock:
255 MO.getMBB()->getSymbol()->print(OS, MAI);
265 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
268 const char *ExtraCode,
271 report_fatal_error("There are no defined alternate asm variants");
274 // TODO: For now, we just hard-code 0 as the constant offset; teach
275 // SelectInlineAsmMemoryOperand how to do address mode matching.
276 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
280 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
283 // Force static initialization.
284 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
285 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
286 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);