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/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/raw_ostream.h"
36 #define DEBUG_TYPE "asm-printer"
40 class WebAssemblyAsmPrinter final : public AsmPrinter {
42 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43 : AsmPrinter(TM, std::move(Streamer)) {}
46 const char *getPassName() const override {
47 return "WebAssembly Assembly Printer";
50 //===------------------------------------------------------------------===//
51 // MachineFunctionPass Implementation.
52 //===------------------------------------------------------------------===//
54 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 AsmPrinter::getAnalysisUsage(AU);
58 bool runOnMachineFunction(MachineFunction &F) override {
59 return AsmPrinter::runOnMachineFunction(F);
62 //===------------------------------------------------------------------===//
63 // AsmPrinter Implementation.
64 //===------------------------------------------------------------------===//
66 void EmitInstruction(const MachineInstr *MI) override;
69 } // end anonymous namespace
71 //===----------------------------------------------------------------------===//
73 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
75 raw_svector_ostream OS(Str);
77 switch (MI->getOpcode()) {
79 DEBUG(MI->print(dbgs()));
80 llvm_unreachable("Unhandled instruction");
84 OutStreamer->EmitRawText(OS.str());
87 // Force static initialization.
88 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
89 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
90 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);