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 "MCTargetDesc/WebAssemblyTargetStreamer.h"
21 #include "WebAssemblyMCInstLower.h"
22 #include "WebAssemblyMachineFunctionInfo.h"
23 #include "WebAssemblyRegisterInfo.h"
24 #include "WebAssemblySubtarget.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/MachineInstr.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
39 #define DEBUG_TYPE "asm-printer"
43 class WebAssemblyAsmPrinter final : public AsmPrinter {
44 const MachineRegisterInfo *MRI;
45 const WebAssemblyFunctionInfo *MFI;
48 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
49 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
52 const char *getPassName() const override {
53 return "WebAssembly Assembly Printer";
56 //===------------------------------------------------------------------===//
57 // MachineFunctionPass Implementation.
58 //===------------------------------------------------------------------===//
60 bool runOnMachineFunction(MachineFunction &MF) override {
61 MRI = &MF.getRegInfo();
62 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
63 return AsmPrinter::runOnMachineFunction(MF);
66 //===------------------------------------------------------------------===//
67 // AsmPrinter Implementation.
68 //===------------------------------------------------------------------===//
70 void EmitJumpTableInfo() override;
71 void EmitConstantPool() override;
72 void EmitFunctionBodyStart() override;
73 void EmitFunctionBodyEnd() override;
74 void EmitInstruction(const MachineInstr *MI) override;
75 const MCExpr *lowerConstant(const Constant *CV) override;
76 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
77 unsigned AsmVariant, const char *ExtraCode,
78 raw_ostream &OS) override;
79 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
80 unsigned AsmVariant, const char *ExtraCode,
81 raw_ostream &OS) override;
83 MVT getRegType(unsigned RegNo) const;
84 const char *toString(MVT VT) const;
85 std::string regToString(const MachineOperand &MO);
86 WebAssemblyTargetStreamer *getTargetStreamer();
89 } // end anonymous namespace
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
96 const TargetRegisterClass *TRC =
97 TargetRegisterInfo::isVirtualRegister(RegNo)
98 ? MRI->getRegClass(RegNo)
99 : MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
100 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
103 DEBUG(errs() << "Unknown type for register number: " << RegNo);
104 llvm_unreachable("Unknown register type");
108 const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
109 return WebAssembly::TypeToString(VT);
112 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
113 unsigned RegNo = MO.getReg();
114 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
115 "Unlowered physical register encountered during assembly printing");
116 assert(!MFI->isVRegStackified(RegNo));
117 unsigned WAReg = MFI->getWAReg(RegNo);
118 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
119 return '$' + utostr(WAReg);
122 WebAssemblyTargetStreamer *
123 WebAssemblyAsmPrinter::getTargetStreamer() {
124 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
125 return static_cast<WebAssemblyTargetStreamer *>(TS);
128 //===----------------------------------------------------------------------===//
129 // WebAssemblyAsmPrinter Implementation.
130 //===----------------------------------------------------------------------===//
132 void WebAssemblyAsmPrinter::EmitConstantPool() {
133 assert(MF->getConstantPool()->getConstants().empty() &&
134 "WebAssembly disables constant pools");
137 void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
138 // Nothing to do; jump tables are incorporated into the instruction stream.
141 static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
142 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
143 const DataLayout &DL(F.getParent()->getDataLayout());
144 const WebAssemblyTargetLowering &TLI =
145 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
146 SmallVector<EVT, 4> VTs;
147 ComputeValueVTs(TLI, DL, Ty, VTs);
150 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
151 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
152 for (unsigned i = 0; i != NumRegs; ++i)
153 ValueVTs.push_back(RegisterVT);
157 void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
158 if (!MFI->getParams().empty())
159 getTargetStreamer()->emitParam(MFI->getParams());
161 SmallVector<MVT, 4> ResultVTs;
162 const Function &F(*MF->getFunction());
163 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
165 // If the return type needs to be legalized it will get converted into
166 // passing a pointer.
167 if (ResultVTs.size() == 1)
168 getTargetStreamer()->emitResult(ResultVTs);
170 bool AnyWARegs = false;
171 SmallVector<MVT, 16> LocalTypes;
172 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
173 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
174 unsigned WAReg = MFI->getWAReg(VReg);
175 // Don't declare unused registers.
176 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
178 // Don't redeclare parameters.
179 if (WAReg < MFI->getParams().size())
181 // Don't declare stackified registers.
184 LocalTypes.push_back(getRegType(VReg));
187 auto &PhysRegs = MFI->getPhysRegs();
188 for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
189 if (PhysRegs[PReg] == -1U)
191 LocalTypes.push_back(getRegType(PReg));
195 getTargetStreamer()->emitLocal(LocalTypes);
197 AsmPrinter::EmitFunctionBodyStart();
200 void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
201 getTargetStreamer()->emitEndFunc();
204 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
205 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
207 switch (MI->getOpcode()) {
208 case WebAssembly::ARGUMENT_I32:
209 case WebAssembly::ARGUMENT_I64:
210 case WebAssembly::ARGUMENT_F32:
211 case WebAssembly::ARGUMENT_F64:
212 // These represent values which are live into the function entry, so there's
213 // no instruction to emit.
216 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
218 MCInstLowering.Lower(MI, TmpInst);
219 EmitToStreamer(*OutStreamer, TmpInst);
225 const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
226 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
227 if (GV->getValueType()->isFunctionTy())
228 return MCSymbolRefExpr::create(
229 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
230 return AsmPrinter::lowerConstant(CV);
233 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
234 unsigned OpNo, unsigned AsmVariant,
235 const char *ExtraCode,
238 report_fatal_error("There are no defined alternate asm variants");
240 // First try the generic code, which knows about modifiers like 'c' and 'n'.
241 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
245 const MachineOperand &MO = MI->getOperand(OpNo);
246 switch (MO.getType()) {
247 case MachineOperand::MO_Immediate:
250 case MachineOperand::MO_Register:
251 OS << regToString(MO);
253 case MachineOperand::MO_GlobalAddress:
254 getSymbol(MO.getGlobal())->print(OS, MAI);
255 printOffset(MO.getOffset(), OS);
257 case MachineOperand::MO_ExternalSymbol:
258 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
259 printOffset(MO.getOffset(), OS);
261 case MachineOperand::MO_MachineBasicBlock:
262 MO.getMBB()->getSymbol()->print(OS, MAI);
272 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
275 const char *ExtraCode,
278 report_fatal_error("There are no defined alternate asm variants");
281 // TODO: For now, we just hard-code 0 as the constant offset; teach
282 // SelectInlineAsmMemoryOperand how to do address mode matching.
283 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
287 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
290 // Force static initialization.
291 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
292 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
293 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);