WebAssemblyAsmPrinter.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // WebAssemblyAsmPrinter.h - WebAssembly implementation of AsmPrinter-*- C++ -*-
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
  9. #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
  10. #include "WebAssemblyMachineFunctionInfo.h"
  11. #include "WebAssemblySubtarget.h"
  12. #include "llvm/CodeGen/AsmPrinter.h"
  13. #include "llvm/MC/MCStreamer.h"
  14. #include "llvm/Target/TargetMachine.h"
  15. namespace llvm {
  16. class WebAssemblyTargetStreamer;
  17. class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter {
  18. const WebAssemblySubtarget *Subtarget;
  19. const MachineRegisterInfo *MRI;
  20. WebAssemblyFunctionInfo *MFI;
  21. // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter?
  22. std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
  23. std::vector<std::unique_ptr<std::string>> Names;
  24. bool signaturesEmitted = false;
  25. StringRef storeName(StringRef Name) {
  26. std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
  27. Names.push_back(std::move(N));
  28. return *Names.back();
  29. }
  30. public:
  31. explicit WebAssemblyAsmPrinter(TargetMachine &TM,
  32. std::unique_ptr<MCStreamer> Streamer)
  33. : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), MRI(nullptr),
  34. MFI(nullptr) {}
  35. StringRef getPassName() const override {
  36. return "WebAssembly Assembly Printer";
  37. }
  38. const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; }
  39. void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
  40. Signatures.push_back(std::move(Sig));
  41. }
  42. //===------------------------------------------------------------------===//
  43. // MachineFunctionPass Implementation.
  44. //===------------------------------------------------------------------===//
  45. bool runOnMachineFunction(MachineFunction &MF) override {
  46. Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
  47. MRI = &MF.getRegInfo();
  48. MFI = MF.getInfo<WebAssemblyFunctionInfo>();
  49. return AsmPrinter::runOnMachineFunction(MF);
  50. }
  51. //===------------------------------------------------------------------===//
  52. // AsmPrinter Implementation.
  53. //===------------------------------------------------------------------===//
  54. void emitEndOfAsmFile(Module &M) override;
  55. void EmitProducerInfo(Module &M);
  56. void EmitTargetFeatures(Module &M);
  57. void emitSymbolType(const MCSymbolWasm *Sym);
  58. void emitGlobalVariable(const GlobalVariable *GV) override;
  59. void emitJumpTableInfo() override;
  60. void emitConstantPool() override;
  61. void emitFunctionBodyStart() override;
  62. void emitInstruction(const MachineInstr *MI) override;
  63. bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  64. const char *ExtraCode, raw_ostream &OS) override;
  65. bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
  66. const char *ExtraCode, raw_ostream &OS) override;
  67. MVT getRegType(unsigned RegNo) const;
  68. std::string regToString(const MachineOperand &MO);
  69. WebAssemblyTargetStreamer *getTargetStreamer();
  70. MCSymbolWasm *getMCSymbolForFunction(const Function *F, bool EnableEmEH,
  71. wasm::WasmSignature *Sig,
  72. bool &InvokeDetected);
  73. MCSymbol *getOrCreateWasmSymbol(StringRef Name);
  74. void emitDecls(const Module &M);
  75. };
  76. } // end namespace llvm
  77. #endif