|
11 | 11 | // |
12 | 12 | //===----------------------------------------------------------------------===// |
13 | 13 |
|
| 14 | +#include "BPFAsmPrinter.h" |
14 | 15 | #include "BPF.h" |
15 | 16 | #include "BPFInstrInfo.h" |
16 | 17 | #include "BPFMCInstLower.h" |
17 | 18 | #include "BTFDebug.h" |
18 | 19 | #include "MCTargetDesc/BPFInstPrinter.h" |
19 | 20 | #include "TargetInfo/BPFTargetInfo.h" |
| 21 | +#include "llvm/BinaryFormat/ELF.h" |
20 | 22 | #include "llvm/CodeGen/AsmPrinter.h" |
21 | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
22 | 24 | #include "llvm/CodeGen/MachineInstr.h" |
| 25 | +#include "llvm/CodeGen/MachineJumpTableInfo.h" |
23 | 26 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 27 | +#include "llvm/CodeGen/TargetLowering.h" |
24 | 28 | #include "llvm/IR/Module.h" |
25 | 29 | #include "llvm/MC/MCAsmInfo.h" |
| 30 | +#include "llvm/MC/MCExpr.h" |
26 | 31 | #include "llvm/MC/MCInst.h" |
27 | 32 | #include "llvm/MC/MCStreamer.h" |
28 | 33 | #include "llvm/MC/MCSymbol.h" |
| 34 | +#include "llvm/MC/MCSymbolELF.h" |
29 | 35 | #include "llvm/MC/TargetRegistry.h" |
30 | 36 | #include "llvm/Support/Compiler.h" |
31 | 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | +#include "llvm/Target/TargetLoweringObjectFile.h" |
32 | 39 | using namespace llvm; |
33 | 40 |
|
34 | 41 | #define DEBUG_TYPE "asm-printer" |
35 | 42 |
|
36 | | -namespace { |
37 | | -class BPFAsmPrinter : public AsmPrinter { |
38 | | -public: |
39 | | - explicit BPFAsmPrinter(TargetMachine &TM, |
40 | | - std::unique_ptr<MCStreamer> Streamer) |
41 | | - : AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr) {} |
42 | | - |
43 | | - StringRef getPassName() const override { return "BPF Assembly Printer"; } |
44 | | - bool doInitialization(Module &M) override; |
45 | | - void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O); |
46 | | - bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
47 | | - const char *ExtraCode, raw_ostream &O) override; |
48 | | - bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, |
49 | | - const char *ExtraCode, raw_ostream &O) override; |
50 | | - |
51 | | - void emitInstruction(const MachineInstr *MI) override; |
52 | | - |
53 | | - static char ID; |
54 | | - |
55 | | -private: |
56 | | - BTFDebug *BTF; |
57 | | -}; |
58 | | -} // namespace |
59 | | - |
60 | 43 | bool BPFAsmPrinter::doInitialization(Module &M) { |
61 | 44 | AsmPrinter::doInitialization(M); |
62 | 45 |
|
@@ -150,6 +133,47 @@ void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) { |
150 | 133 | EmitToStreamer(*OutStreamer, TmpInst); |
151 | 134 | } |
152 | 135 |
|
| 136 | +MCSymbol *BPFAsmPrinter::getJTPublicSymbol(unsigned JTI) { |
| 137 | + SmallString<60> Name; |
| 138 | + raw_svector_ostream(Name) |
| 139 | + << "BPF.JT." << MF->getFunctionNumber() << '.' << JTI; |
| 140 | + MCSymbol *S = OutContext.getOrCreateSymbol(Name); |
| 141 | + if (auto *ES = dyn_cast<MCSymbolELF>(S)) |
| 142 | + ES->setBinding(ELF::STB_GLOBAL); |
| 143 | + return S; |
| 144 | +} |
| 145 | + |
| 146 | +void BPFAsmPrinter::emitJumpTableInfo() { |
| 147 | + const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); |
| 148 | + if (!MJTI) |
| 149 | + return; |
| 150 | + |
| 151 | + const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 152 | + if (JT.empty()) |
| 153 | + return; |
| 154 | + |
| 155 | + const TargetLoweringObjectFile &TLOF = getObjFileLowering(); |
| 156 | + const Function &F = MF->getFunction(); |
| 157 | + MCSection *JTS = TLOF.getSectionForJumpTable(F, TM); |
| 158 | + assert(MJTI->getEntryKind() == MachineJumpTableInfo::EK_BlockAddress); |
| 159 | + unsigned EntrySize = MJTI->getEntrySize(getDataLayout()); |
| 160 | + OutStreamer->switchSection(JTS); |
| 161 | + for (unsigned JTI = 0; JTI < JT.size(); JTI++) { |
| 162 | + ArrayRef<MachineBasicBlock *> JTBBs = JT[JTI].MBBs; |
| 163 | + if (JTBBs.empty()) |
| 164 | + continue; |
| 165 | + |
| 166 | + MCSymbol *JTStart = getJTPublicSymbol(JTI); |
| 167 | + OutStreamer->emitLabel(JTStart); |
| 168 | + for (const MachineBasicBlock *MBB : JTBBs) { |
| 169 | + const MCExpr *LHS = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); |
| 170 | + OutStreamer->emitValue(LHS, EntrySize); |
| 171 | + } |
| 172 | + const MCExpr *JTSize = MCConstantExpr::create(JTBBs.size() * 8, OutContext); |
| 173 | + OutStreamer->emitELFSize(JTStart, JTSize); |
| 174 | + } |
| 175 | +} |
| 176 | + |
153 | 177 | char BPFAsmPrinter::ID = 0; |
154 | 178 |
|
155 | 179 | INITIALIZE_PASS(BPFAsmPrinter, "bpf-asm-printer", "BPF Assembly Printer", false, |
|
0 commit comments