Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Target / BPF / BPFAsmPrinter.cpp
blobab2886682ddea46facb60e7a4cc9e473a6803105
1 //===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
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 //
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to the BPF assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "BPF.h"
15 #include "BPFInstrInfo.h"
16 #include "BPFMCInstLower.h"
17 #include "BPFTargetMachine.h"
18 #include "BTFDebug.h"
19 #include "InstPrinter/BPFInstPrinter.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm;
33 #define DEBUG_TYPE "asm-printer"
35 namespace {
36 class BPFAsmPrinter : public AsmPrinter {
37 public:
38 explicit BPFAsmPrinter(TargetMachine &TM,
39 std::unique_ptr<MCStreamer> Streamer)
40 : AsmPrinter(TM, std::move(Streamer)) {}
42 StringRef getPassName() const override { return "BPF Assembly Printer"; }
43 bool doInitialization(Module &M) override;
44 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
45 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
46 unsigned AsmVariant, const char *ExtraCode,
47 raw_ostream &O) override;
48 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
49 unsigned AsmVariant, const char *ExtraCode,
50 raw_ostream &O) override;
52 void EmitInstruction(const MachineInstr *MI) override;
54 } // namespace
56 bool BPFAsmPrinter::doInitialization(Module &M) {
57 AsmPrinter::doInitialization(M);
59 if (MAI->doesSupportDebugInformation()) {
60 Handlers.push_back(HandlerInfo(new BTFDebug(this), "emit",
61 "Debug Info Emission", "BTF",
62 "BTF Emission"));
65 return false;
68 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
69 raw_ostream &O) {
70 const MachineOperand &MO = MI->getOperand(OpNum);
72 switch (MO.getType()) {
73 case MachineOperand::MO_Register:
74 O << BPFInstPrinter::getRegisterName(MO.getReg());
75 break;
77 case MachineOperand::MO_Immediate:
78 O << MO.getImm();
79 break;
81 case MachineOperand::MO_MachineBasicBlock:
82 O << *MO.getMBB()->getSymbol();
83 break;
85 case MachineOperand::MO_GlobalAddress:
86 O << *getSymbol(MO.getGlobal());
87 break;
89 case MachineOperand::MO_BlockAddress: {
90 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
91 O << BA->getName();
92 break;
95 case MachineOperand::MO_ExternalSymbol:
96 O << *GetExternalSymbolSymbol(MO.getSymbolName());
97 break;
99 case MachineOperand::MO_JumpTableIndex:
100 case MachineOperand::MO_ConstantPoolIndex:
101 default:
102 llvm_unreachable("<unknown operand type>");
106 bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
107 unsigned /*AsmVariant*/,
108 const char *ExtraCode, raw_ostream &O) {
109 if (ExtraCode && ExtraCode[0])
110 return true; // BPF does not have special modifiers
112 printOperand(MI, OpNo, O);
113 return false;
116 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
117 unsigned OpNum, unsigned AsmVariant,
118 const char *ExtraCode,
119 raw_ostream &O) {
120 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
121 const MachineOperand &BaseMO = MI->getOperand(OpNum);
122 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
123 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
124 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
125 int Offset = OffsetMO.getImm();
127 if (ExtraCode)
128 return true; // Unknown modifier.
130 if (Offset < 0)
131 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
132 else
133 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
135 return false;
138 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
140 BPFMCInstLower MCInstLowering(OutContext, *this);
142 MCInst TmpInst;
143 MCInstLowering.Lower(MI, TmpInst);
144 EmitToStreamer(*OutStreamer, TmpInst);
147 // Force static initialization.
148 extern "C" void LLVMInitializeBPFAsmPrinter() {
149 RegisterAsmPrinter<BPFAsmPrinter> X(getTheBPFleTarget());
150 RegisterAsmPrinter<BPFAsmPrinter> Y(getTheBPFbeTarget());
151 RegisterAsmPrinter<BPFAsmPrinter> Z(getTheBPFTarget());