LLVM  8.0.1
BPFMCInstLower.cpp
Go to the documentation of this file.
1 //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to lower BPF MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "BPFMCInstLower.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"
25 using namespace llvm;
26 
27 MCSymbol *
29  return Printer.getSymbol(MO.getGlobal());
30 }
31 
32 MCSymbol *
34  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
35 }
36 
38  MCSymbol *Sym) const {
39 
40  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
41 
42  if (!MO.isJTI() && MO.getOffset())
43  llvm_unreachable("unknown symbol op");
44 
45  return MCOperand::createExpr(Expr);
46 }
47 
48 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
49  OutMI.setOpcode(MI->getOpcode());
50 
51  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
52  const MachineOperand &MO = MI->getOperand(i);
53 
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;
69  MCOp = MCOperand::createExpr(
71  break;
73  continue;
76  break;
79  break;
80  }
81 
82  OutMI.addOperand(MCOp);
83  }
84 }
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:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
MachineBasicBlock reference.
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
const char * getSymbolName() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
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:173
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:430
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:186
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
bool isImplicit() const