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 "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyRegisterInfo.h"
20 #include "WebAssemblySubtarget.h"
21 #include "InstPrinter/WebAssemblyInstPrinter.h"
22 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DebugInfo.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
39 #define DEBUG_TYPE "asm-printer"
43 class WebAssemblyAsmPrinter final : public AsmPrinter {
44 const WebAssemblyInstrInfo *TII;
45 const MachineRegisterInfo *MRI;
49 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
50 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr), MRI(nullptr) {}
53 const char *getPassName() const override {
54 return "WebAssembly Assembly Printer";
57 //===------------------------------------------------------------------===//
58 // MachineFunctionPass Implementation.
59 //===------------------------------------------------------------------===//
61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AsmPrinter::getAnalysisUsage(AU);
65 bool runOnMachineFunction(MachineFunction &MF) override {
66 const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
67 TII = Subtarget.getInstrInfo();
68 MRI = &MF.getRegInfo();
69 NumArgs = MF.getInfo<WebAssemblyFunctionInfo>()->getNumArguments();
70 return AsmPrinter::runOnMachineFunction(MF);
73 //===------------------------------------------------------------------===//
74 // AsmPrinter Implementation.
75 //===------------------------------------------------------------------===//
77 void EmitJumpTableInfo() override;
78 void EmitConstantPool() override;
79 void EmitFunctionBodyStart() override;
80 void EmitInstruction(const MachineInstr *MI) override;
81 void EmitEndOfAsmFile(Module &M) override;
83 std::string getRegTypeName(unsigned RegNo) const;
84 static std::string toString(const APFloat &APF);
85 const char *toString(Type *Ty) const;
86 std::string regToString(const MachineOperand &MO);
87 std::string argToString(const MachineOperand &MO);
90 } // end anonymous namespace
92 //===----------------------------------------------------------------------===//
94 //===----------------------------------------------------------------------===//
96 // Operand type (if any), followed by the lower-case version of the opcode's
97 // name matching the names WebAssembly opcodes are expected to have. The
98 // tablegen names are uppercase and suffixed with their type (after an
99 // underscore). Conversions are additionally prefixed with their input type
100 // (before a double underscore).
101 static std::string OpcodeName(const WebAssemblyInstrInfo *TII,
102 const MachineInstr *MI) {
103 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
104 std::string::size_type Len = N.length();
105 std::string::size_type Under = N.rfind('_');
106 bool HasType = std::string::npos != Under;
107 std::string::size_type NameEnd = HasType ? Under : Len;
108 std::string Name(&N[0], &N[NameEnd]);
111 for (const char *typelessOpcode : { "return", "call", "br_if" })
112 if (Name == typelessOpcode)
114 std::string Type(&N[NameEnd + 1], &N[Len]);
115 std::string::size_type DoubleUnder = Name.find("__");
116 bool IsConv = std::string::npos != DoubleUnder;
118 return Type + '.' + Name;
119 std::string InType(&Name[0], &Name[DoubleUnder]);
120 return Type + '.' + std::string(&Name[DoubleUnder + 2], &Name[NameEnd]) +
124 static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
126 std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const {
127 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
128 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
130 return EVT(T).getEVTString();
131 DEBUG(errs() << "Unknown type for register number: " << RegNo);
132 llvm_unreachable("Unknown register type");
136 std::string WebAssemblyAsmPrinter::toString(const APFloat &FP) {
137 static const size_t BufBytes = 128;
140 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
142 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
143 "convertToHexString handles neither SNaN nor NaN payloads");
144 // Use C99's hexadecimal floating-point representation.
145 auto Written = FP.convertToHexString(
146 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
148 assert(Written != 0);
149 assert(Written < BufBytes);
153 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
154 unsigned RegNo = MO.getReg();
155 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
156 return WebAssemblyInstPrinter::getRegisterName(RegNo);
158 // WebAssembly arguments and local variables are in the same index space, and
159 // there are no explicit varargs, so we just add the number of arguments to
160 // the virtual register number to get the local variable number.
161 return utostr(TargetRegisterInfo::virtReg2Index(RegNo) + NumArgs);
164 std::string WebAssemblyAsmPrinter::argToString(const MachineOperand &MO) {
165 unsigned ArgNo = MO.getImm();
166 // Same as above, but we don't need to add NumArgs here.
167 return utostr(ArgNo);
170 const char *WebAssemblyAsmPrinter::toString(Type *Ty) const {
171 switch (Ty->getTypeID()) {
174 // Treat all pointers as the underlying integer into linear memory.
175 case Type::PointerTyID:
176 switch (getPointerSize()) {
182 llvm_unreachable("unsupported pointer size");
185 case Type::FloatTyID:
187 case Type::DoubleTyID:
189 case Type::IntegerTyID:
190 switch (Ty->getIntegerBitWidth()) {
203 DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
204 llvm_unreachable("invalid type");
208 //===----------------------------------------------------------------------===//
209 // WebAssemblyAsmPrinter Implementation.
210 //===----------------------------------------------------------------------===//
212 void WebAssemblyAsmPrinter::EmitConstantPool() {
213 assert(MF->getConstantPool()->getConstants().empty() &&
214 "WebAssembly disables constant pools");
217 void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
218 // Nothing to do; jump tables are incorporated into the instruction stream.
221 void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
222 const Function *F = MF->getFunction();
223 Type *Rt = F->getReturnType();
224 SmallString<128> Str;
225 raw_svector_ostream OS(Str);
228 if (!Rt->isVoidTy() || !F->arg_empty()) {
229 for (const Argument &A : F->args()) {
230 OS << (First ? "" : "\n") << "\t.param " << toString(A.getType());
233 if (!Rt->isVoidTy()) {
234 OS << (First ? "" : "\n") << "\t.result " << toString(Rt);
239 bool FirstVReg = true;
240 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
241 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
242 // FIXME: Don't skip dead virtual registers for now: that would require
243 // remapping all locals' numbers.
244 //if (!MRI->use_empty(VReg)) {
246 OS << (First ? "" : "\n") << "\t.local ";
249 OS << (FirstVReg ? "" : ", ") << getRegTypeName(VReg);
255 OutStreamer->EmitRawText(OS.str());
256 AsmPrinter::EmitFunctionBodyStart();
259 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
260 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
261 SmallString<128> Str;
262 raw_svector_ostream OS(Str);
264 unsigned NumDefs = MI->getDesc().getNumDefs();
265 assert(NumDefs <= 1 &&
266 "Instructions with multiple result values not implemented");
270 switch (MI->getOpcode()) {
271 case TargetOpcode::COPY:
272 OS << "get_local " << regToString(MI->getOperand(1));
274 case WebAssembly::GLOBAL:
276 OS << "i32.const " << toSymbol(MI->getOperand(1).getGlobal()->getName());
278 case WebAssembly::ARGUMENT_I32:
279 case WebAssembly::ARGUMENT_I64:
280 case WebAssembly::ARGUMENT_F32:
281 case WebAssembly::ARGUMENT_F64:
282 OS << "get_local " << argToString(MI->getOperand(1));
284 case WebAssembly::Immediate_I32:
285 OS << "i32.const " << MI->getOperand(1).getImm();
287 case WebAssembly::Immediate_I64:
288 OS << "i64.const " << MI->getOperand(1).getImm();
290 case WebAssembly::Immediate_F32:
291 OS << "f32.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
293 case WebAssembly::Immediate_F64:
294 OS << "f64.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
297 OS << OpcodeName(TII, MI);
298 bool NeedComma = false;
299 for (const MachineOperand &MO : MI->uses()) {
300 if (MO.isReg() && MO.isImplicit())
306 switch (MO.getType()) {
308 llvm_unreachable("unexpected machine operand type");
309 case MachineOperand::MO_Register:
310 OS << "(get_local " << regToString(MO) << ')';
312 case MachineOperand::MO_Immediate:
315 case MachineOperand::MO_FPImmediate:
316 OS << toString(MO.getFPImm()->getValueAPF());
318 case MachineOperand::MO_GlobalAddress:
319 OS << toSymbol(MO.getGlobal()->getName());
321 case MachineOperand::MO_MachineBasicBlock:
322 OS << toSymbol(MO.getMBB()->getSymbol()->getName());
330 OutStreamer->EmitRawText(OS.str());
333 SmallString<128> Str;
334 raw_svector_ostream OS(Str);
335 const MachineOperand &Operand = MI->getOperand(0);
336 OS << "\tset_local " << regToString(Operand) << ", pop";
337 OutStreamer->EmitRawText(OS.str());
341 void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
342 SmallString<128> Str;
343 raw_svector_ostream OS(Str);
344 for (const Function &F : M)
345 if (F.isDeclarationForLinker()) {
346 assert(F.hasName() && "imported functions must have a name");
347 if (F.getName().startswith("llvm."))
350 OS << "\t.imports\n";
351 Type *Rt = F.getReturnType();
352 OS << "\t.import " << toSymbol(F.getName()) << " \"\" \"" << F.getName()
354 for (const Argument &A : F.args())
355 OS << " (param " << toString(A.getType()) << ')';
357 OS << " (result " << toString(Rt) << ')';
360 OutStreamer->EmitRawText(OS.str());
363 // Force static initialization.
364 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
365 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
366 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);