LLVM 22.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"
16#include "BPFISelLowering.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"
27using namespace llvm;
28
31 return Printer.getSymbol(MO.getGlobal());
32}
33
36 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
37}
38
40 MCSymbol *Sym) const {
41
42 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
43
44 if (!MO.isJTI() && MO.getOffset())
45 llvm_unreachable("unknown symbol op");
46
47 return MCOperand::createExpr(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(errs());
58 llvm_unreachable("unknown operand type");
60 // Ignore all implicit register operands.
61 if (MO.isImplicit())
62 continue;
63 MCOp = MCOperand::createReg(MO.getReg());
64 break;
66 MCOp = MCOperand::createImm(MO.getImm());
67 break;
70 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
71 break;
73 continue;
76 break;
79 break;
81 MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
82 break;
84 MCOp = LowerSymbolOperand(MO, Printer.getJTPublicSymbol(MO.getIndex()));
85 break;
86 }
87
88 OutMI.addOperand(MCOp);
89 }
90}
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:214
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.