LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/Target/Mangler.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
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:277
static MCOperand CreateReg(unsigned Reg)
Definition: MCInst.h:111
MachineBasicBlock * getMBB() const
static const MCConstantExpr * Create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:152
XCoreMCInstLower(class AsmPrinter &asmprinter)
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
const char * getSymbolName() const
print alias Alias Set Printer
Address of indexed Jump Table for switch.
#define llvm_unreachable(msg)
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
GetJTISymbol - Return the symbol for the specified jump table entry.
unsigned getNumOperands() const
Definition: MachineInstr.h:265
int getOpcode() const
Definition: MachineInstr.h:261
int64_t getImm() const
Address of indexed Constant in Constant Pool.
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
void Initialize(Mangler *mang, MCContext *C)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
int64_t getOffset() const
MCBinaryExpr - Binary assembler expressions.
Definition: MCExpr.h:356
MCSymbol * getSymbol() const
void setOpcode(unsigned Op)
Definition: MCInst.h:157
static const MCBinaryExpr * CreateAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:396
MachineOperandType getType() const
static MCOperand CreateImm(int64_t Val)
Definition: MCInst.h:117
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Mask of preserved registers.
MCSymbol * GetCPISymbol(unsigned CPID) const
GetCPISymbol - Return the symbol for the specified constant pool entry.
unsigned getReg() const
getReg - Returns the register number.
Address of a basic block.
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
const BlockAddress * getBlockAddress() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
MachineBasicBlock reference.
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Address of a global value.
Name of external global symbol.