LLVM  4.0.0
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 
33  MCSymbol *Sym) const {
34 
35  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
36 
37  if (!MO.isJTI() && MO.getOffset())
38  llvm_unreachable("unknown symbol op");
39 
40  return MCOperand::createExpr(Expr);
41 }
42 
43 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
44  OutMI.setOpcode(MI->getOpcode());
45 
46  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
47  const MachineOperand &MO = MI->getOperand(i);
48 
49  MCOperand MCOp;
50  switch (MO.getType()) {
51  default:
52  MI->dump();
53  llvm_unreachable("unknown operand type");
55  // Ignore all implicit register operands.
56  if (MO.isImplicit())
57  continue;
58  MCOp = MCOperand::createReg(MO.getReg());
59  break;
61  MCOp = MCOperand::createImm(MO.getImm());
62  break;
64  MCOp = MCOperand::createExpr(
66  break;
68  continue;
71  break;
72  }
73 
74  OutMI.addOperand(MCOp);
75  }
76 }
bool isImplicit() const
const GlobalValue * getGlobal() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:371
size_t i
MachineBasicBlock * getMBB() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MachineBasicBlock reference.
Mask of preserved registers.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:111
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:277
int64_t getImm() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:273
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Address of a global value.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
int64_t getOffset() const
Return the offset from the symbol in this operand.
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:158
MachineOperand class - Representation of each machine instruction operand.
void dump(const TargetInstrInfo *TII=nullptr) const
Representation of each machine instruction.
Definition: MachineInstr.h:52
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
unsigned getReg() const
getReg - Returns the register number.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117