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/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 EmitInstruction(const MachineInstr *MI) override;
74 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode,
76 raw_ostream &OS) override;
77 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
78 unsigned AsmVariant, const char *ExtraCode,
79 raw_ostream &OS) override;
81 MVT getRegType(unsigned RegNo) const;
82 const char *toString(MVT VT) const;
83 std::string regToString(const MachineOperand &MO);
86 } // end anonymous namespace
88 //===----------------------------------------------------------------------===//
90 //===----------------------------------------------------------------------===//
92 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
93 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
94 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
97 DEBUG(errs() << "Unknown type for register number: " << RegNo);
98 llvm_unreachable("Unknown register type");
102 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
103 unsigned RegNo = MO.getReg();
104 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
105 "Unlowered physical register encountered during assembly printing");
106 assert(!MFI->isVRegStackified(RegNo));
107 unsigned WAReg = MFI->getWAReg(RegNo);
108 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
109 return '$' + utostr(WAReg);
112 const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
113 return WebAssembly::TypeToString(VT);
116 //===----------------------------------------------------------------------===//
117 // WebAssemblyAsmPrinter Implementation.
118 //===----------------------------------------------------------------------===//
120 void WebAssemblyAsmPrinter::EmitConstantPool() {
121 assert(MF->getConstantPool()->getConstants().empty() &&
122 "WebAssembly disables constant pools");
125 void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
126 // Nothing to do; jump tables are incorporated into the instruction stream.
129 static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
130 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
131 const DataLayout &DL(F.getParent()->getDataLayout());
132 const WebAssemblyTargetLowering &TLI =
133 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
134 SmallVector<EVT, 4> VTs;
135 ComputeValueVTs(TLI, DL, Ty, VTs);
138 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
139 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
140 for (unsigned i = 0; i != NumRegs; ++i)
141 ValueVTs.push_back(RegisterVT);
145 void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
146 if (!MFI->getParams().empty()) {
148 Param.setOpcode(WebAssembly::PARAM);
149 for (MVT VT : MFI->getParams())
150 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
151 EmitToStreamer(*OutStreamer, Param);
154 SmallVector<MVT, 4> ResultVTs;
155 const Function &F(*MF->getFunction());
156 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
157 // If the return type needs to be legalized it will get converted into
158 // passing a pointer.
159 if (ResultVTs.size() == 1) {
161 Result.setOpcode(WebAssembly::RESULT);
162 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
163 EmitToStreamer(*OutStreamer, Result);
166 bool AnyWARegs = false;
168 Local.setOpcode(WebAssembly::LOCAL);
169 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
170 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
171 unsigned WAReg = MFI->getWAReg(VReg);
172 // Don't declare unused registers.
173 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
175 // Don't redeclare parameters.
176 if (WAReg < MFI->getParams().size())
178 // Don't declare stackified registers.
181 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
185 EmitToStreamer(*OutStreamer, Local);
187 AsmPrinter::EmitFunctionBodyStart();
190 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
191 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
193 switch (MI->getOpcode()) {
194 case WebAssembly::ARGUMENT_I32:
195 case WebAssembly::ARGUMENT_I64:
196 case WebAssembly::ARGUMENT_F32:
197 case WebAssembly::ARGUMENT_F64:
198 // These represent values which are live into the function entry, so there's
199 // no instruction to emit.
201 case WebAssembly::LOOP_END:
202 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
203 // fallthrough which nevertheless requires a label for the destination here.
206 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
208 MCInstLowering.Lower(MI, TmpInst);
209 EmitToStreamer(*OutStreamer, TmpInst);
215 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
216 unsigned OpNo, unsigned AsmVariant,
217 const char *ExtraCode,
220 report_fatal_error("There are no defined alternate asm variants");
222 // First try the generic code, which knows about modifiers like 'c' and 'n'.
223 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
227 const MachineOperand &MO = MI->getOperand(OpNo);
228 switch (MO.getType()) {
229 case MachineOperand::MO_Immediate:
232 case MachineOperand::MO_Register:
233 OS << regToString(MO);
235 case MachineOperand::MO_GlobalAddress:
236 getSymbol(MO.getGlobal())->print(OS, MAI);
237 printOffset(MO.getOffset(), OS);
239 case MachineOperand::MO_ExternalSymbol:
240 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
241 printOffset(MO.getOffset(), OS);
243 case MachineOperand::MO_MachineBasicBlock:
244 MO.getMBB()->getSymbol()->print(OS, MAI);
254 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
257 const char *ExtraCode,
260 report_fatal_error("There are no defined alternate asm variants");
263 OS << regToString(MI->getOperand(OpNo));
267 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
270 // Force static initialization.
271 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
272 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
273 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);