1 //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=//
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 // This file contains code to lower BPF MachineInstrs to their corresponding
12 //===----------------------------------------------------------------------===//
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"
27 BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
28 return Printer
.getSymbol(MO
.getGlobal());
32 BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand
&MO
) const {
33 return Printer
.GetExternalSymbolSymbol(MO
.getSymbolName());
36 MCOperand
BPFMCInstLower::LowerSymbolOperand(const MachineOperand
&MO
,
37 MCSymbol
*Sym
) const {
39 const MCExpr
*Expr
= MCSymbolRefExpr::create(Sym
, Ctx
);
41 if (!MO
.isJTI() && MO
.getOffset())
42 llvm_unreachable("unknown symbol op");
44 return MCOperand::createExpr(Expr
);
47 void BPFMCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
48 OutMI
.setOpcode(MI
->getOpcode());
50 for (const MachineOperand
&MO
: MI
->operands()) {
52 switch (MO
.getType()) {
55 llvm_unreachable("unknown operand type");
56 case MachineOperand::MO_Register
:
57 // Ignore all implicit register operands.
60 MCOp
= MCOperand::createReg(MO
.getReg());
62 case MachineOperand::MO_Immediate
:
63 MCOp
= MCOperand::createImm(MO
.getImm());
65 case MachineOperand::MO_MachineBasicBlock
:
66 MCOp
= MCOperand::createExpr(
67 MCSymbolRefExpr::create(MO
.getMBB()->getSymbol(), Ctx
));
69 case MachineOperand::MO_RegisterMask
:
71 case MachineOperand::MO_ExternalSymbol
:
72 MCOp
= LowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
74 case MachineOperand::MO_GlobalAddress
:
75 MCOp
= LowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
77 case MachineOperand::MO_ConstantPoolIndex
:
78 MCOp
= LowerSymbolOperand(MO
, Printer
.GetCPISymbol(MO
.getIndex()));
82 OutMI
.addOperand(MCOp
);