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 "BPFAsmPrinter.h"
16#include "BPFISelLowering.h"
17#include "llvm/CodeGen/AsmPrinter.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
27using namespace llvm;
28
29MCSymbol *
30BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
31 return Printer.getSymbol(GV: MO.getGlobal());
32}
33
34MCSymbol *
35BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {
36 return Printer.GetExternalSymbolSymbol(Sym: MO.getSymbolName());
37}
38
39MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
40 MCSymbol *Sym) const {
41
42 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol: Sym, Ctx);
43
44 if (!MO.isJTI() && MO.getOffset())
45 llvm_unreachable("unknown symbol op");
46
47 return MCOperand::createExpr(Val: Expr);
48}
49
50void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
51 OutMI.setOpcode(MI->getOpcode());
52
53 for (const MachineOperand &MO : MI->operands()) {
54 MCOperand MCOp;
55 switch (MO.getType()) {
56 default:
57 MI->print(OS&: errs());
58 llvm_unreachable("unknown operand type");
59 case MachineOperand::MO_Register:
60 // Ignore all implicit register operands.
61 if (MO.isImplicit())
62 continue;
63 MCOp = MCOperand::createReg(Reg: MO.getReg());
64 break;
65 case MachineOperand::MO_Immediate:
66 MCOp = MCOperand::createImm(Val: MO.getImm());
67 break;
68 case MachineOperand::MO_MachineBasicBlock:
69 MCOp = MCOperand::createExpr(
70 Val: MCSymbolRefExpr::create(Symbol: MO.getMBB()->getSymbol(), Ctx));
71 break;
72 case MachineOperand::MO_RegisterMask:
73 continue;
74 case MachineOperand::MO_ExternalSymbol:
75 MCOp = LowerSymbolOperand(MO, Sym: GetExternalSymbolSymbol(MO));
76 break;
77 case MachineOperand::MO_GlobalAddress:
78 MCOp = LowerSymbolOperand(MO, Sym: GetGlobalAddressSymbol(MO));
79 break;
80 case MachineOperand::MO_ConstantPoolIndex:
81 MCOp = LowerSymbolOperand(MO, Sym: Printer.GetCPISymbol(CPID: MO.getIndex()));
82 break;
83 case MachineOperand::MO_JumpTableIndex:
84 MCOp = LowerSymbolOperand(MO, Sym: Printer.getJTPublicSymbol(JTI: MO.getIndex()));
85 break;
86 }
87
88 OutMI.addOperand(Op: MCOp);
89 }
90}
91