LLVM 20.0.0git
BPFMCInstLower.cpp
Go to the documentation of this file.
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"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
24using namespace llvm;
25
28 return Printer.getSymbol(MO.getGlobal());
29}
30
33 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
34}
35
37 MCSymbol *Sym) const {
38
39 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
40
41 if (!MO.isJTI() && MO.getOffset())
42 llvm_unreachable("unknown symbol op");
43
44 return MCOperand::createExpr(Expr);
45}
46
47void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
48 OutMI.setOpcode(MI->getOpcode());
49
50 for (const MachineOperand &MO : MI->operands()) {
51 MCOperand MCOp;
52 switch (MO.getType()) {
53 default:
54 MI->print(errs());
55 llvm_unreachable("unknown operand type");
57 // Ignore all implicit register operands.
58 if (MO.isImplicit())
59 continue;
60 MCOp = MCOperand::createReg(MO.getReg());
61 break;
63 MCOp = MCOperand::createImm(MO.getImm());
64 break;
67 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
68 break;
70 continue;
73 break;
76 break;
78 MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
79 break;
80 }
81
82 OutMI.addOperand(MCOp);
83 }
84}
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:701
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
void setOpcode(unsigned Op)
Definition: MCInst.h:198
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:163
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:142
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const char * getSymbolName() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
int64_t getOffset() const
Return the offset from the symbol in this operand.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.