LLVM 24.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"
15#include "BPFAsmPrinter.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCStreamer.h"
26using namespace llvm;
27
30 return Printer.getSymbol(MO.getGlobal());
31}
32
35 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
36}
37
39 MCSymbol *Sym) const {
40
41 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
42
43 if (!MO.isJTI() && MO.getOffset())
44 llvm_unreachable("unknown symbol op");
45
46 return MCOperand::createExpr(Expr);
47}
48
49void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
50 OutMI.setOpcode(MI->getOpcode());
51
52 for (const MachineOperand &MO : MI->operands()) {
53 MCOperand MCOp;
54 switch (MO.getType()) {
55 default:
56 MI->print(errs());
57 llvm_unreachable("unknown operand type");
59 // Ignore all implicit register operands.
60 if (MO.isImplicit())
61 continue;
62 MCOp = MCOperand::createReg(MO.getReg());
63 break;
65 MCOp = MCOperand::createImm(MO.getImm());
66 break;
69 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
70 break;
72 continue;
75 break;
78 break;
80 MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
81 break;
83 MCOp = LowerSymbolOperand(MO, Printer.getJTPublicSymbol(MO.getIndex()));
84 break;
85 }
86
87 OutMI.addOperand(MCOp);
88 }
89}
IRTranslator LLVM IR MI
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:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Representation of each machine instruction.
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.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
#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.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.