blob: f62cec9f59437b12ea1162a08cdfa8e754e17f12 [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
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"
23
24#include "llvm/ADT/SmallString.h"
Dan Gohmane51c0582015-10-06 00:27:55 +000025#include "llvm/ADT/StringExtras.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000026#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000027#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DebugInfo.h"
31#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000032#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/raw_ostream.h"
36
37using namespace llvm;
38
39#define DEBUG_TYPE "asm-printer"
40
41namespace {
42
43class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien600aee92015-07-31 17:53:38 +000044 const WebAssemblyInstrInfo *TII;
Dan Gohmane51c0582015-10-06 00:27:55 +000045 unsigned NumArgs;
JF Bastien600aee92015-07-31 17:53:38 +000046
JF Bastienb9073fb2015-07-22 21:28:15 +000047public:
48 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Dan Gohman979840d2015-09-23 16:59:10 +000049 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000050
51private:
52 const char *getPassName() const override {
53 return "WebAssembly Assembly Printer";
54 }
55
56 //===------------------------------------------------------------------===//
57 // MachineFunctionPass Implementation.
58 //===------------------------------------------------------------------===//
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AsmPrinter::getAnalysisUsage(AU);
62 }
63
JF Bastien600aee92015-07-31 17:53:38 +000064 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien73ff6af2015-08-31 22:24:11 +000065 const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
JF Bastien73ff6af2015-08-31 22:24:11 +000066 TII = Subtarget.getInstrInfo();
Dan Gohmane51c0582015-10-06 00:27:55 +000067 NumArgs = MF.getInfo<WebAssemblyFunctionInfo>()->getNumArguments();
JF Bastien600aee92015-07-31 17:53:38 +000068 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000069 }
70
71 //===------------------------------------------------------------------===//
72 // AsmPrinter Implementation.
73 //===------------------------------------------------------------------===//
74
Dan Gohman950a13c2015-09-16 16:51:30 +000075 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000076 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000077 void EmitFunctionBodyStart() override;
JF Bastienb6091df2015-08-25 22:58:05 +000078
JF Bastienb9073fb2015-07-22 21:28:15 +000079 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohman979840d2015-09-23 16:59:10 +000080
81 static std::string toString(const APFloat &APF);
82 const char *toString(Type *Ty) const;
Dan Gohmane51c0582015-10-06 00:27:55 +000083 std::string regToString(unsigned RegNo);
84 std::string argToString(unsigned ArgNo);
JF Bastienb9073fb2015-07-22 21:28:15 +000085};
86
87} // end anonymous namespace
88
89//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000090// Helpers.
91//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000092
JF Bastien315cc062015-08-07 01:57:03 +000093// Untyped, lower-case version of the opcode's name matching the names
94// WebAssembly opcodes are expected to have. The tablegen names are uppercase
95// and suffixed with their type (after an underscore).
JF Bastien45479f62015-08-26 22:09:54 +000096static SmallString<32> OpcodeName(const WebAssemblyInstrInfo *TII,
97 const MachineInstr *MI) {
JF Bastien315cc062015-08-07 01:57:03 +000098 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
Dan Gohman69c4c762015-08-24 21:03:24 +000099 std::string::size_type End = N.rfind('_');
JF Bastien315cc062015-08-07 01:57:03 +0000100 End = std::string::npos == End ? N.length() : End;
101 return SmallString<32>(&N[0], &N[End]);
102}
103
JF Bastienaf111db2015-08-24 22:16:48 +0000104static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
105
Dan Gohman979840d2015-09-23 16:59:10 +0000106std::string WebAssemblyAsmPrinter::toString(const APFloat &FP) {
JF Bastien45479f62015-08-26 22:09:54 +0000107 static const size_t BufBytes = 128;
108 char buf[BufBytes];
109 if (FP.isNaN())
110 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
111 FP.bitwiseIsEqual(
112 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
113 "convertToHexString handles neither SNaN nor NaN payloads");
114 // Use C99's hexadecimal floating-point representation.
115 auto Written = FP.convertToHexString(
116 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
117 (void)Written;
118 assert(Written != 0);
119 assert(Written < BufBytes);
120 return buf;
121}
122
Dan Gohmane51c0582015-10-06 00:27:55 +0000123std::string WebAssemblyAsmPrinter::regToString(unsigned RegNo) {
124 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
125 return WebAssemblyInstPrinter::getRegisterName(RegNo);
126
127 // WebAssembly arguments and local variables are in the same index space, and
128 // there are no explicit varargs, so we just add the number of arguments to
129 // the virtual register number to get the local variable number.
130 return '@' + utostr(TargetRegisterInfo::virtReg2Index(RegNo) + NumArgs);
131}
132
133std::string WebAssemblyAsmPrinter::argToString(unsigned ArgNo) {
134 // Same as above, but we don't need to add NumArgs here.
135 return '@' + utostr(ArgNo);
136}
137
Dan Gohman979840d2015-09-23 16:59:10 +0000138const char *WebAssemblyAsmPrinter::toString(Type *Ty) const {
JF Bastien73ff6af2015-08-31 22:24:11 +0000139 switch (Ty->getTypeID()) {
Dan Gohman979840d2015-09-23 16:59:10 +0000140 default:
141 break;
JF Bastien73ff6af2015-08-31 22:24:11 +0000142 // Treat all pointers as the underlying integer into linear memory.
Dan Gohman979840d2015-09-23 16:59:10 +0000143 case Type::PointerTyID:
144 switch (getPointerSize()) {
145 case 4:
146 return "i32";
147 case 8:
148 return "i64";
149 default:
150 llvm_unreachable("unsupported pointer size");
151 }
152 break;
153 case Type::FloatTyID:
154 return "f32";
155 case Type::DoubleTyID:
156 return "f64";
JF Bastien73ff6af2015-08-31 22:24:11 +0000157 case Type::IntegerTyID:
158 switch (Ty->getIntegerBitWidth()) {
Dan Gohman979840d2015-09-23 16:59:10 +0000159 case 8:
160 return "i8";
161 case 16:
162 return "i16";
163 case 32:
164 return "i32";
165 case 64:
166 return "i64";
167 default:
168 break;
JF Bastien73ff6af2015-08-31 22:24:11 +0000169 }
170 }
171 DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
172 llvm_unreachable("invalid type");
173 return "<invalid>";
174}
175
JF Bastien45479f62015-08-26 22:09:54 +0000176//===----------------------------------------------------------------------===//
177// WebAssemblyAsmPrinter Implementation.
178//===----------------------------------------------------------------------===//
179
JF Bastien54be3b12015-08-25 23:19:49 +0000180void WebAssemblyAsmPrinter::EmitConstantPool() {
181 assert(MF->getConstantPool()->getConstants().empty() &&
182 "WebAssembly disables constant pools");
183}
184
Dan Gohman950a13c2015-09-16 16:51:30 +0000185void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
186 // Nothing to do; jump tables are incorporated into the instruction stream.
187}
188
JF Bastienb6091df2015-08-25 22:58:05 +0000189void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
JF Bastienb6091df2015-08-25 22:58:05 +0000190 const Function *F = MF->getFunction();
Dan Gohman979840d2015-09-23 16:59:10 +0000191 Type *Rt = F->getReturnType();
Dan Gohmane51c0582015-10-06 00:27:55 +0000192
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000193 if (!Rt->isVoidTy() || !F->arg_empty()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000194 SmallString<128> Str;
195 raw_svector_ostream OS(Str);
196 bool First = true;
197 for (const Argument &A : F->args()) {
198 OS << (First ? "" : "\n") << "\t"
199 ".param "
200 << toString(A.getType());
201 First = false;
202 }
203 if (!Rt->isVoidTy()) {
204 OS << (First ? "" : "\n") << "\t"
205 ".result "
206 << toString(Rt);
207 First = false;
208 }
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000209 OutStreamer->EmitRawText(OS.str());
210 }
JF Bastienb6091df2015-08-25 22:58:05 +0000211
Dan Gohmane51c0582015-10-06 00:27:55 +0000212 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000213}
214
JF Bastienb9073fb2015-07-22 21:28:15 +0000215void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000216 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000217 SmallString<128> Str;
218 raw_svector_ostream OS(Str);
219
JF Bastien600aee92015-07-31 17:53:38 +0000220 unsigned NumDefs = MI->getDesc().getNumDefs();
221 assert(NumDefs <= 1 &&
222 "Instructions with multiple result values not implemented");
223
Dan Gohman12e19972015-08-24 21:19:48 +0000224 OS << '\t';
225
Dan Gohmane51c0582015-10-06 00:27:55 +0000226 switch (MI->getOpcode()) {
227 case TargetOpcode::COPY:
228 OS << regToString(MI->getOperand(1).getReg());
229 break;
230 case WebAssembly::GLOBAL:
231 // TODO: wasm64
232 OS << "i32.const " << toSymbol(MI->getOperand(1).getGlobal()->getName());
233 break;
234 case WebAssembly::ARGUMENT_I32:
235 case WebAssembly::ARGUMENT_I64:
236 case WebAssembly::ARGUMENT_F32:
237 case WebAssembly::ARGUMENT_F64:
238 OS << argToString(MI->getOperand(1).getImm());
239 break;
240 case WebAssembly::Immediate_I32:
241 OS << "i32.const " << MI->getOperand(1).getImm();
242 break;
243 case WebAssembly::Immediate_I64:
244 OS << "i64.const " << MI->getOperand(1).getImm();
245 break;
246 case WebAssembly::Immediate_F32:
247 OS << "f32.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
248 break;
249 case WebAssembly::Immediate_F64:
250 OS << "f64.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
251 break;
252 default: {
253 OS << OpcodeName(TII, MI);
254 bool NeedComma = false;
255 for (const MachineOperand &MO : MI->uses()) {
256 if (MO.isReg() && MO.isImplicit())
257 continue;
258 if (NeedComma)
259 OS << ',';
260 NeedComma = true;
261 OS << ' ';
Dan Gohman4f52e002015-09-09 00:52:47 +0000262 switch (MO.getType()) {
263 default:
264 llvm_unreachable("unexpected machine operand type");
Dan Gohmane51c0582015-10-06 00:27:55 +0000265 case MachineOperand::MO_Register:
266 OS << regToString(MO.getReg());
267 break;
268 case MachineOperand::MO_Immediate:
269 OS << MO.getImm();
270 break;
271 case MachineOperand::MO_FPImmediate:
272 OS << toString(MO.getFPImm()->getValueAPF());
273 break;
274 case MachineOperand::MO_GlobalAddress:
275 OS << toSymbol(MO.getGlobal()->getName());
276 break;
277 case MachineOperand::MO_MachineBasicBlock:
278 OS << toSymbol(MO.getMBB()->getSymbol()->getName());
279 break;
Dan Gohman4f52e002015-09-09 00:52:47 +0000280 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000281 }
282 break;
283 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000284 }
JF Bastien600aee92015-07-31 17:53:38 +0000285
JF Bastienb9073fb2015-07-22 21:28:15 +0000286 OutStreamer->EmitRawText(OS.str());
Dan Gohmane51c0582015-10-06 00:27:55 +0000287
288 if (NumDefs != 0) {
289 SmallString<128> Str;
290 raw_svector_ostream OS(Str);
291 OS << "\t" "set_local "
292 << regToString(MI->getOperand(0).getReg()) << ", "
293 "pop";
294 OutStreamer->EmitRawText(OS.str());
295 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000296}
297
298// Force static initialization.
299extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
300 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
301 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
302}