LLVM  3.7.0
XCoreMCInstLower.cpp
Go to the documentation of this file.
1 //===-- XCoreMCInstLower.cpp - Convert XCore MachineInstr to 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 /// \file
11 /// \brief This file contains code to lower XCore MachineInstrs to their
12 /// corresponding MCInst records.
13 ///
14 //===----------------------------------------------------------------------===//
15 #include "XCoreMCInstLower.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 
25 using namespace llvm;
26 
28 : Printer(asmprinter) {}
29 
31  Mang = M;
32  Ctx = C;
33 }
34 
35 MCOperand XCoreMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
36  MachineOperandType MOTy,
37  unsigned Offset) const {
39  const MCSymbol *Symbol;
40 
41  switch (MOTy) {
43  Symbol = MO.getMBB()->getSymbol();
44  break;
46  Symbol = Printer.getSymbol(MO.getGlobal());
47  Offset += MO.getOffset();
48  break;
50  Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
51  Offset += MO.getOffset();
52  break;
54  Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
55  Offset += MO.getOffset();
56  break;
58  Symbol = Printer.GetJTISymbol(MO.getIndex());
59  break;
61  Symbol = Printer.GetCPISymbol(MO.getIndex());
62  Offset += MO.getOffset();
63  break;
64  default:
65  llvm_unreachable("<unknown operand type>");
66  }
67 
68  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
69 
70  if (!Offset)
71  return MCOperand::createExpr(MCSym);
72 
73  // Assume offset is never negative.
74  assert(Offset > 0);
75 
76  const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
77  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
78  return MCOperand::createExpr(Add);
79 }
80 
82  unsigned offset) const {
83  MachineOperandType MOTy = MO.getType();
84 
85  switch (MOTy) {
86  default: llvm_unreachable("unknown operand type");
88  // Ignore all implicit register operands.
89  if (MO.isImplicit()) break;
90  return MCOperand::createReg(MO.getReg());
92  return MCOperand::createImm(MO.getImm() + offset);
99  return LowerSymbolOperand(MO, MOTy, offset);
101  break;
102  }
103 
104  return MCOperand();
105 }
106 
107 void XCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
108  OutMI.setOpcode(MI->getOpcode());
109 
110  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
111  const MachineOperand &MO = MI->getOperand(i);
112  MCOperand MCOp = LowerOperand(MO);
113 
114  if (MCOp.isValid())
115  OutMI.addOperand(MCOp);
116  }
117 }
bool isImplicit() const
const GlobalValue * getGlobal() const
bool isValid() const
Definition: MCInst.h:55
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
XCoreMCInstLower(class AsmPrinter &asmprinter)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
Address of indexed Jump Table for switch.
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
MachineBasicBlock reference.
const char * getSymbolName() const
print alias Alias Set Printer
Mask of preserved registers.
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
Name of external global symbol.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:159
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
Context object for machine code objects.
Definition: MCContext.h:48
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:446
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
Address of a global value.
void Initialize(Mangler *mang, MCContext *C)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
Address of a basic block.
int64_t getOffset() const
Return the offset from the symbol in this operand.
Binary assembler expressions.
Definition: MCExpr.h:405
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.
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
unsigned getReg() const
getReg - Returns the register number.
const ARM::ArchExtKind Kind
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
void Lower(const MachineInstr *MI, MCInst &OutMI) const