LLVM  3.7.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 #include "llvm/ADT/SmallString.h"
26 using namespace llvm;
27 
28 MCSymbol *
30  return Printer.getSymbol(MO.getGlobal());
31 }
32 
34  MCSymbol *Sym) const {
35 
36  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
37 
38  if (!MO.isJTI() && MO.getOffset())
39  llvm_unreachable("unknown symbol op");
40 
41  return MCOperand::createExpr(Expr);
42 }
43 
44 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
45  OutMI.setOpcode(MI->getOpcode());
46 
47  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
48  const MachineOperand &MO = MI->getOperand(i);
49 
50  MCOperand MCOp;
51  switch (MO.getType()) {
52  default:
53  MI->dump();
54  llvm_unreachable("unknown operand type");
56  // Ignore all implicit register operands.
57  if (MO.isImplicit())
58  continue;
59  MCOp = MCOperand::createReg(MO.getReg());
60  break;
62  MCOp = MCOperand::createImm(MO.getImm());
63  break;
65  MCOp = MCOperand::createExpr(
67  break;
69  continue;
72  break;
73  }
74 
75  OutMI.addOperand(MCOp);
76  }
77 }
bool isImplicit() const
const GlobalValue * getGlobal() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:339
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
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:267
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Address of a global value.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
int64_t getOffset() const
Return the offset from the symbol in this operand.
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
void setOpcode(unsigned Op)
Definition: MCInst.h:158
MachineOperand class - Representation of each machine instruction operand.
void dump() const
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
unsigned getReg() const
getReg - Returns the register number.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
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