blob: 94ea1085ff440a05b760682cc5c6bf1c8ff1d56a [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"
25#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000026#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000027#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DebugInfo.h"
30#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000031#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Support/raw_ostream.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "asm-printer"
39
40namespace {
41
42class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien73ff6af2015-08-31 22:24:11 +000043 bool hasAddr64;
JF Bastien600aee92015-07-31 17:53:38 +000044 const WebAssemblyInstrInfo *TII;
45
JF Bastienb9073fb2015-07-22 21:28:15 +000046public:
47 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
JF Bastien600aee92015-07-31 17:53:38 +000048 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000049
50private:
51 const char *getPassName() const override {
52 return "WebAssembly Assembly Printer";
53 }
54
55 //===------------------------------------------------------------------===//
56 // MachineFunctionPass Implementation.
57 //===------------------------------------------------------------------===//
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 AsmPrinter::getAnalysisUsage(AU);
61 }
62
JF Bastien600aee92015-07-31 17:53:38 +000063 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien73ff6af2015-08-31 22:24:11 +000064 const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
65 hasAddr64 = Subtarget.hasAddr64();
66 TII = Subtarget.getInstrInfo();
JF Bastien600aee92015-07-31 17:53:38 +000067 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000068 }
69
70 //===------------------------------------------------------------------===//
71 // AsmPrinter Implementation.
72 //===------------------------------------------------------------------===//
73
JF Bastien45479f62015-08-26 22:09:54 +000074 void EmitGlobalVariable(const GlobalVariable *GV) override;
75
Dan Gohman950a13c2015-09-16 16:51:30 +000076 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000077 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000078 void EmitFunctionEntryLabel() override;
79 void EmitFunctionBodyStart() override;
80 void EmitFunctionBodyEnd() override;
81
JF Bastienb9073fb2015-07-22 21:28:15 +000082 void EmitInstruction(const MachineInstr *MI) override;
83};
84
85} // end anonymous namespace
86
87//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000088// Helpers.
89//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000090
JF Bastien315cc062015-08-07 01:57:03 +000091// Untyped, lower-case version of the opcode's name matching the names
92// WebAssembly opcodes are expected to have. The tablegen names are uppercase
93// and suffixed with their type (after an underscore).
JF Bastien45479f62015-08-26 22:09:54 +000094static SmallString<32> OpcodeName(const WebAssemblyInstrInfo *TII,
95 const MachineInstr *MI) {
JF Bastien315cc062015-08-07 01:57:03 +000096 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
Dan Gohman69c4c762015-08-24 21:03:24 +000097 std::string::size_type End = N.rfind('_');
JF Bastien315cc062015-08-07 01:57:03 +000098 End = std::string::npos == End ? N.length() : End;
99 return SmallString<32>(&N[0], &N[End]);
100}
101
JF Bastienaf111db2015-08-24 22:16:48 +0000102static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
103
JF Bastien45479f62015-08-26 22:09:54 +0000104static std::string toString(const APFloat &FP) {
105 static const size_t BufBytes = 128;
106 char buf[BufBytes];
107 if (FP.isNaN())
108 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
109 FP.bitwiseIsEqual(
110 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
111 "convertToHexString handles neither SNaN nor NaN payloads");
112 // Use C99's hexadecimal floating-point representation.
113 auto Written = FP.convertToHexString(
114 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
115 (void)Written;
116 assert(Written != 0);
117 assert(Written < BufBytes);
118 return buf;
119}
120
JF Bastien73ff6af2015-08-31 22:24:11 +0000121static const char *toString(const Type *Ty, bool hasAddr64) {
122 switch (Ty->getTypeID()) {
123 default: break;
124 // Treat all pointers as the underlying integer into linear memory.
125 case Type::PointerTyID: return hasAddr64 ? "i64" : "i32";
126 case Type::FloatTyID: return "f32";
127 case Type::DoubleTyID: return "f64";
128 case Type::IntegerTyID:
129 switch (Ty->getIntegerBitWidth()) {
130 case 8: return "i8";
131 case 16: return "i16";
132 case 32: return "i32";
133 case 64: return "i64";
134 default: break;
135 }
136 }
137 DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
138 llvm_unreachable("invalid type");
139 return "<invalid>";
140}
141
142
JF Bastien45479f62015-08-26 22:09:54 +0000143//===----------------------------------------------------------------------===//
144// WebAssemblyAsmPrinter Implementation.
145//===----------------------------------------------------------------------===//
146
147void WebAssemblyAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
148 SmallString<128> Str;
149 raw_svector_ostream OS(Str);
150 StringRef Name = GV->getName();
151 DEBUG(dbgs() << "Global " << Name << '\n');
152
153 if (!GV->hasInitializer()) {
154 DEBUG(dbgs() << " Skipping declaration.\n");
155 return;
156 }
157
158 // Check to see if this is a special global used by LLVM.
159 static const char *Ignored[] = {"llvm.used", "llvm.metadata"};
160 for (const char *I : Ignored)
161 if (Name == I)
162 return;
163 // FIXME: Handle the following globals.
164 static const char *Unhandled[] = {"llvm.global_ctors", "llvm.global_dtors"};
165 for (const char *U : Unhandled)
166 if (Name == U)
167 report_fatal_error("Unhandled global");
168 if (Name.startswith("llvm."))
169 report_fatal_error("Unknown LLVM-internal global");
170
171 if (GV->isThreadLocal())
172 report_fatal_error("TLS isn't yet supported by WebAssembly");
173
174 const DataLayout &DL = getDataLayout();
175 const Constant *Init = GV->getInitializer();
176 if (isa<UndefValue>(Init))
177 Init = Constant::getNullValue(Init->getType());
178 unsigned Align = DL.getPrefTypeAlignment(Init->getType());
179
180 switch (GV->getLinkage()) {
181 case GlobalValue::InternalLinkage:
182 case GlobalValue::PrivateLinkage:
183 break;
184 case GlobalValue::AppendingLinkage:
185 case GlobalValue::LinkOnceAnyLinkage:
186 case GlobalValue::LinkOnceODRLinkage:
187 case GlobalValue::WeakAnyLinkage:
188 case GlobalValue::WeakODRLinkage:
189 case GlobalValue::ExternalLinkage:
190 case GlobalValue::CommonLinkage:
191 report_fatal_error("Linkage types other than internal and private aren't "
JF Bastienb1b61eb2015-08-26 23:03:07 +0000192 "supported by WebAssembly yet");
JF Bastien45479f62015-08-26 22:09:54 +0000193 default:
194 llvm_unreachable("Unknown linkage type");
195 return;
196 }
197
JF Bastien73ff6af2015-08-31 22:24:11 +0000198 OS << "(global " << toSymbol(Name) << ' '
199 << toString(Init->getType(), hasAddr64) << ' ';
JF Bastien45479f62015-08-26 22:09:54 +0000200 if (const auto *C = dyn_cast<ConstantInt>(Init)) {
201 assert(C->getBitWidth() <= 64 && "Printing wider types unimplemented");
202 OS << C->getZExtValue();
203 } else if (const auto *C = dyn_cast<ConstantFP>(Init)) {
204 OS << toString(C->getValueAPF());
205 } else {
206 assert(false && "Only integer and floating-point constants are supported");
207 }
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000208 OS << ") ;; align " << Align;
JF Bastien45479f62015-08-26 22:09:54 +0000209 OutStreamer->EmitRawText(OS.str());
210}
211
JF Bastien54be3b12015-08-25 23:19:49 +0000212void WebAssemblyAsmPrinter::EmitConstantPool() {
213 assert(MF->getConstantPool()->getConstants().empty() &&
214 "WebAssembly disables constant pools");
215}
216
Dan Gohman950a13c2015-09-16 16:51:30 +0000217void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
218 // Nothing to do; jump tables are incorporated into the instruction stream.
219}
220
JF Bastienb6091df2015-08-25 22:58:05 +0000221void WebAssemblyAsmPrinter::EmitFunctionEntryLabel() {
222 SmallString<128> Str;
223 raw_svector_ostream OS(Str);
224
225 CurrentFnSym->redefineIfPossible();
226
227 // The function label could have already been emitted if two symbols end up
228 // conflicting due to asm renaming. Detect this and emit an error.
229 if (CurrentFnSym->isVariable())
230 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
231 "' is a protected alias");
232 if (CurrentFnSym->isDefined())
233 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
234 "' label emitted multiple times to assembly file");
235
236 OS << "(func " << toSymbol(CurrentFnSym->getName());
237 OutStreamer->EmitRawText(OS.str());
238}
239
240void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
241 SmallString<128> Str;
242 raw_svector_ostream OS(Str);
243 const Function *F = MF->getFunction();
JF Bastienb6091df2015-08-25 22:58:05 +0000244 const Type *Rt = F->getReturnType();
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000245 if (!Rt->isVoidTy() || !F->arg_empty()) {
246 for (const Argument &A : F->args())
247 OS << " (param " << toString(A.getType(), hasAddr64) << ')';
248 if (!Rt->isVoidTy())
249 OS << " (result " << toString(Rt, hasAddr64) << ')';
250 OutStreamer->EmitRawText(OS.str());
251 }
JF Bastienb6091df2015-08-25 22:58:05 +0000252}
253
254void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
255 SmallString<128> Str;
256 raw_svector_ostream OS(Str);
Dan Gohman1ce7ba52015-09-09 15:13:36 +0000257 OS << ") ;; end func " << toSymbol(CurrentFnSym->getName());
JF Bastienb6091df2015-08-25 22:58:05 +0000258 OutStreamer->EmitRawText(OS.str());
259}
260
JF Bastienb9073fb2015-07-22 21:28:15 +0000261void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000262 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000263 SmallString<128> Str;
264 raw_svector_ostream OS(Str);
265
JF Bastien600aee92015-07-31 17:53:38 +0000266 unsigned NumDefs = MI->getDesc().getNumDefs();
267 assert(NumDefs <= 1 &&
268 "Instructions with multiple result values not implemented");
269
Dan Gohman12e19972015-08-24 21:19:48 +0000270 OS << '\t';
271
JF Bastien600aee92015-07-31 17:53:38 +0000272 if (NumDefs != 0) {
273 const MachineOperand &MO = MI->getOperand(0);
274 unsigned Reg = MO.getReg();
275 OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
276 }
277
Dan Gohman4f52e002015-09-09 00:52:47 +0000278 if (MI->getOpcode() == WebAssembly::COPY) {
279 OS << '@' << TargetRegisterInfo::virtReg2Index(MI->getOperand(1).getReg());
280 } else {
281 OS << '(' << OpcodeName(TII, MI);
282 for (const MachineOperand &MO : MI->uses())
283 switch (MO.getType()) {
284 default:
285 llvm_unreachable("unexpected machine operand type");
286 case MachineOperand::MO_Register: {
287 if (MO.isImplicit())
288 continue;
289 unsigned Reg = MO.getReg();
290 OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
291 } break;
292 case MachineOperand::MO_Immediate: {
293 OS << ' ' << MO.getImm();
294 } break;
295 case MachineOperand::MO_FPImmediate: {
296 OS << ' ' << toString(MO.getFPImm()->getValueAPF());
297 } break;
298 case MachineOperand::MO_GlobalAddress: {
299 OS << ' ' << toSymbol(MO.getGlobal()->getName());
300 } break;
Dan Gohman950a13c2015-09-16 16:51:30 +0000301 case MachineOperand::MO_MachineBasicBlock: {
302 OS << ' ' << toSymbol(MO.getMBB()->getSymbol()->getName());
303 } break;
Dan Gohman4f52e002015-09-09 00:52:47 +0000304 }
305 OS << ')';
306 }
JF Bastien600aee92015-07-31 17:53:38 +0000307
308 if (NumDefs != 0)
309 OS << ')';
310
JF Bastienb9073fb2015-07-22 21:28:15 +0000311 OutStreamer->EmitRawText(OS.str());
312}
313
314// Force static initialization.
315extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
316 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
317 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
318}