LLVM  8.0.1
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 /// 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 
32 MCOperand XCoreMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
33  MachineOperandType MOTy,
34  unsigned Offset) const {
36  const MCSymbol *Symbol;
37 
38  switch (MOTy) {
40  Symbol = MO.getMBB()->getSymbol();
41  break;
43  Symbol = Printer.getSymbol(MO.getGlobal());
44  Offset += MO.getOffset();
45  break;
47  Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
48  Offset += MO.getOffset();
49  break;
51  Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
52  Offset += MO.getOffset();
53  break;
55  Symbol = Printer.GetJTISymbol(MO.getIndex());
56  break;
58  Symbol = Printer.GetCPISymbol(MO.getIndex());
59  Offset += MO.getOffset();
60  break;
61  default:
62  llvm_unreachable("<unknown operand type>");
63  }
64 
65  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
66 
67  if (!Offset)
68  return MCOperand::createExpr(MCSym);
69 
70  // Assume offset is never negative.
71  assert(Offset > 0);
72 
73  const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
74  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
75  return MCOperand::createExpr(Add);
76 }
77 
79  unsigned offset) const {
80  MachineOperandType MOTy = MO.getType();
81 
82  switch (MOTy) {
83  default: llvm_unreachable("unknown operand type");
85  // Ignore all implicit register operands.
86  if (MO.isImplicit()) break;
87  return MCOperand::createReg(MO.getReg());
89  return MCOperand::createImm(MO.getImm() + offset);
96  return LowerSymbolOperand(MO, MOTy, offset);
98  break;
99  }
100 
101  return MCOperand();
102 }
103 
104 void XCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
105  OutMI.setOpcode(MI->getOpcode());
106 
107  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
108  const MachineOperand &MO = MI->getOperand(i);
109  MCOperand MCOp = LowerOperand(MO);
110 
111  if (MCOp.isValid())
112  OutMI.addOperand(MCOp);
113  }
114 }
uint64_t CallInst * C
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
XCoreMCInstLower(class AsmPrinter &asmprinter)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
print alias Alias Set Printer
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
Name of external global symbol.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:63
void Initialize(MCContext *C)
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Address of a global value.
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
Address of a basic block.
Binary assembler expressions.
Definition: MCExpr.h:417
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:173
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:430
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
Representation of each machine instruction.
Definition: MachineInstr.h:64
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
bool isValid() const
Definition: MCInst.h:57
Address of indexed Constant in Constant Pool.
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
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
void Lower(const MachineInstr *MI, MCInst &OutMI) const
bool isImplicit() const