/src/llvm-project/llvm/lib/Target/BPF/BPFMCInstLower.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=// |
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 code to lower BPF MachineInstrs to their corresponding |
10 | | // MCInst records. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "BPFMCInstLower.h" |
15 | | #include "llvm/CodeGen/AsmPrinter.h" |
16 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
17 | | #include "llvm/CodeGen/MachineInstr.h" |
18 | | #include "llvm/MC/MCAsmInfo.h" |
19 | | #include "llvm/MC/MCContext.h" |
20 | | #include "llvm/MC/MCExpr.h" |
21 | | #include "llvm/MC/MCInst.h" |
22 | | #include "llvm/Support/ErrorHandling.h" |
23 | | #include "llvm/Support/raw_ostream.h" |
24 | | using namespace llvm; |
25 | | |
26 | | MCSymbol * |
27 | 2 | BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
28 | 2 | return Printer.getSymbol(MO.getGlobal()); |
29 | 2 | } |
30 | | |
31 | | MCSymbol * |
32 | 0 | BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const { |
33 | 0 | return Printer.GetExternalSymbolSymbol(MO.getSymbolName()); |
34 | 0 | } |
35 | | |
36 | | MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
37 | 2 | MCSymbol *Sym) const { |
38 | | |
39 | 2 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); |
40 | | |
41 | 2 | if (!MO.isJTI() && MO.getOffset()) |
42 | 0 | llvm_unreachable("unknown symbol op"); |
43 | | |
44 | 2 | return MCOperand::createExpr(Expr); |
45 | 2 | } |
46 | | |
47 | 14 | void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { |
48 | 14 | OutMI.setOpcode(MI->getOpcode()); |
49 | | |
50 | 35 | for (const MachineOperand &MO : MI->operands()) { |
51 | 35 | MCOperand MCOp; |
52 | 35 | switch (MO.getType()) { |
53 | 0 | default: |
54 | 0 | MI->print(errs()); |
55 | 0 | llvm_unreachable("unknown operand type"); |
56 | 26 | case MachineOperand::MO_Register: |
57 | | // Ignore all implicit register operands. |
58 | 26 | if (MO.isImplicit()) |
59 | 2 | continue; |
60 | 24 | MCOp = MCOperand::createReg(MO.getReg()); |
61 | 24 | break; |
62 | 7 | case MachineOperand::MO_Immediate: |
63 | 7 | MCOp = MCOperand::createImm(MO.getImm()); |
64 | 7 | break; |
65 | 0 | case MachineOperand::MO_MachineBasicBlock: |
66 | 0 | MCOp = MCOperand::createExpr( |
67 | 0 | MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); |
68 | 0 | break; |
69 | 0 | case MachineOperand::MO_RegisterMask: |
70 | 0 | continue; |
71 | 0 | case MachineOperand::MO_ExternalSymbol: |
72 | 0 | MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); |
73 | 0 | break; |
74 | 2 | case MachineOperand::MO_GlobalAddress: |
75 | 2 | MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
76 | 2 | break; |
77 | 35 | } |
78 | | |
79 | 33 | OutMI.addOperand(MCOp); |
80 | 33 | } |
81 | 14 | } |