1 // WebAssemblyAsmPrinter.h - WebAssembly implementation of AsmPrinter-*- C++ -*-
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
10 #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
12 #include "WebAssemblyMachineFunctionInfo.h"
13 #include "WebAssemblySubtarget.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Target/TargetMachine.h"
19 class WebAssemblyTargetStreamer
;
21 class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final
: public AsmPrinter
{
22 const WebAssemblySubtarget
*Subtarget
;
23 const MachineRegisterInfo
*MRI
;
24 WebAssemblyFunctionInfo
*MFI
;
25 // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter?
26 std::vector
<std::unique_ptr
<wasm::WasmSignature
>> Signatures
;
27 std::vector
<std::unique_ptr
<std::string
>> Names
;
28 bool signaturesEmitted
= false;
30 StringRef
storeName(StringRef Name
) {
31 std::unique_ptr
<std::string
> N
= std::make_unique
<std::string
>(Name
);
32 Names
.push_back(std::move(N
));
37 explicit WebAssemblyAsmPrinter(TargetMachine
&TM
,
38 std::unique_ptr
<MCStreamer
> Streamer
)
39 : AsmPrinter(TM
, std::move(Streamer
)), Subtarget(nullptr), MRI(nullptr),
42 StringRef
getPassName() const override
{
43 return "WebAssembly Assembly Printer";
46 const WebAssemblySubtarget
&getSubtarget() const { return *Subtarget
; }
47 void addSignature(std::unique_ptr
<wasm::WasmSignature
> &&Sig
) {
48 Signatures
.push_back(std::move(Sig
));
51 //===------------------------------------------------------------------===//
52 // MachineFunctionPass Implementation.
53 //===------------------------------------------------------------------===//
55 bool runOnMachineFunction(MachineFunction
&MF
) override
{
56 Subtarget
= &MF
.getSubtarget
<WebAssemblySubtarget
>();
57 MRI
= &MF
.getRegInfo();
58 MFI
= MF
.getInfo
<WebAssemblyFunctionInfo
>();
59 return AsmPrinter::runOnMachineFunction(MF
);
62 //===------------------------------------------------------------------===//
63 // AsmPrinter Implementation.
64 //===------------------------------------------------------------------===//
66 void emitEndOfAsmFile(Module
&M
) override
;
67 void EmitProducerInfo(Module
&M
);
68 void EmitTargetFeatures(Module
&M
);
69 void emitSymbolType(const MCSymbolWasm
*Sym
);
70 void emitGlobalVariable(const GlobalVariable
*GV
) override
;
71 void emitJumpTableInfo() override
;
72 void emitConstantPool() override
;
73 void emitFunctionBodyStart() override
;
74 void emitInstruction(const MachineInstr
*MI
) override
;
75 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
76 const char *ExtraCode
, raw_ostream
&OS
) override
;
77 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
78 const char *ExtraCode
, raw_ostream
&OS
) override
;
80 MVT
getRegType(unsigned RegNo
) const;
81 std::string
regToString(const MachineOperand
&MO
);
82 WebAssemblyTargetStreamer
*getTargetStreamer();
83 MCSymbolWasm
*getMCSymbolForFunction(const Function
*F
, bool EnableEmEH
,
84 wasm::WasmSignature
*Sig
,
85 bool &InvokeDetected
);
86 MCSymbol
*getOrCreateWasmSymbol(StringRef Name
);
87 void emitExternalDecls(const Module
&M
);
90 } // end namespace llvm