LLVM  10.0.0svn
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"
24 using namespace llvm;
25 
26 MCSymbol *
28  return Printer.getSymbol(MO.getGlobal());
29 }
30 
31 MCSymbol *
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 
47 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
48  OutMI.setOpcode(MI->getOpcode());
49 
50  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
51  const MachineOperand &MO = MI->getOperand(i);
52 
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;
68  MCOp = MCOperand::createExpr(
70  break;
72  continue;
75  break;
78  break;
79  }
80 
81  OutMI.addOperand(MCOp);
82  }
83 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineBasicBlock * getMBB() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:327
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
MachineBasicBlock reference.
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:414
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
const char * getSymbolName() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
const GlobalValue * getGlobal() const
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:170
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MachineOperand class - Representation of each machine instruction operand.
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
int64_t getImm() const
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
Representation of each machine instruction.
Definition: MachineInstr.h:64
int64_t getOffset() const
Return the offset from the symbol in this operand.
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
bool isImplicit() const