1 //===-- 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" 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" 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "asm-printer" 37 38 namespace { 39 40 class WebAssemblyAsmPrinter final : public AsmPrinter { 41 public: 42 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) 43 : AsmPrinter(TM, std::move(Streamer)) {} 44 45 private: 46 const char *getPassName() const override { 47 return "WebAssembly Assembly Printer"; 48 } 49 50 //===------------------------------------------------------------------===// 51 // MachineFunctionPass Implementation. 52 //===------------------------------------------------------------------===// 53 54 void getAnalysisUsage(AnalysisUsage &AU) const override { 55 AsmPrinter::getAnalysisUsage(AU); 56 } 57 58 bool runOnMachineFunction(MachineFunction &F) override { 59 return AsmPrinter::runOnMachineFunction(F); 60 } 61 62 //===------------------------------------------------------------------===// 63 // AsmPrinter Implementation. 64 //===------------------------------------------------------------------===// 65 66 void EmitInstruction(const MachineInstr *MI) override; 67 }; 68 69 } // end anonymous namespace 70 71 //===----------------------------------------------------------------------===// 72 73 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { 74 SmallString<128> Str; 75 raw_svector_ostream OS(Str); 76 77 switch (MI->getOpcode()) { 78 default: 79 DEBUG(MI->print(dbgs())); 80 llvm_unreachable("Unhandled instruction"); 81 break; 82 } 83 84 OutStreamer->EmitRawText(OS.str()); 85 } 86 87 // Force static initialization. 88 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() { 89 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32); 90 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64); 91 } 92