LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MipsMCInstLower.cpp
Go to the documentation of this file.
1 //===-- MipsMCInstLower.cpp - Convert Mips 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 // This file contains code to lower Mips MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "MipsMCInstLower.h"
16 #include "MipsAsmPrinter.h"
17 #include "MipsInstrInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Target/Mangler.h"
25 
26 using namespace llvm;
27 
29  : AsmPrinter(asmprinter) {}
30 
32  Ctx = C;
33 }
34 
35 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
36  MachineOperandType MOTy,
37  unsigned Offset) const {
39  const MCSymbol *Symbol;
40 
41  switch(MO.getTargetFlags()) {
42  default: llvm_unreachable("Invalid target flag!");
43  case MipsII::MO_NO_FLAG: Kind = MCSymbolRefExpr::VK_None; break;
47  case MipsII::MO_GOT: Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
68  }
69 
70  switch (MOTy) {
72  Symbol = MO.getMBB()->getSymbol();
73  break;
74 
76  Symbol = AsmPrinter.getSymbol(MO.getGlobal());
77  Offset += MO.getOffset();
78  break;
79 
82  Offset += MO.getOffset();
83  break;
84 
87  Offset += MO.getOffset();
88  break;
89 
91  Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
92  break;
93 
95  Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
96  Offset += MO.getOffset();
97  break;
98 
99  default:
100  llvm_unreachable("<unknown operand type>");
101  }
102 
103  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, *Ctx);
104 
105  if (!Offset)
106  return MCOperand::CreateExpr(MCSym);
107 
108  // Assume offset is never negative.
109  assert(Offset > 0);
110 
111  const MCConstantExpr *OffsetExpr = MCConstantExpr::Create(Offset, *Ctx);
112  const MCBinaryExpr *Add = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, *Ctx);
113  return MCOperand::CreateExpr(Add);
114 }
115 
116 /*
117 static void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand &Opnd0,
118  const MCOperand &Opnd1,
119  const MCOperand &Opnd2 = MCOperand()) {
120  Inst.setOpcode(Opc);
121  Inst.addOperand(Opnd0);
122  Inst.addOperand(Opnd1);
123  if (Opnd2.isValid())
124  Inst.addOperand(Opnd2);
125 }
126 */
127 
129  unsigned offset) const {
130  MachineOperandType MOTy = MO.getType();
131 
132  switch (MOTy) {
133  default: llvm_unreachable("unknown operand type");
135  // Ignore all implicit register operands.
136  if (MO.isImplicit()) break;
137  return MCOperand::CreateReg(MO.getReg());
139  return MCOperand::CreateImm(MO.getImm() + offset);
146  return LowerSymbolOperand(MO, MOTy, offset);
148  break;
149  }
150 
151  return MCOperand();
152 }
153 
154 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
155  OutMI.setOpcode(MI->getOpcode());
156 
157  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
158  const MachineOperand &MO = MI->getOperand(i);
159  MCOperand MCOp = LowerOperand(MO);
160 
161  if (MCOp.isValid())
162  OutMI.addOperand(MCOp);
163  }
164 }
165 
bool isImplicit() const
const GlobalValue * getGlobal() const
bool isValid() const
Definition: MCInst.h:55
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:277
MO_TLSLDM - Represents the offset into the global offset table at which.
Definition: MipsBaseInfo.h:64
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
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MO_GOT_HI16/LO16, MO_CALL_HI16/LO16 - Relocations used for large GOTs.
Definition: MipsBaseInfo.h:90
MO_TLSGD - Represents the offset into the global offset table at which.
Definition: MipsBaseInfo.h:59
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
const char * getSymbolName() const
void Initialize(MCContext *C)
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
unsigned getTargetFlags() const
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
MO_GOTTPREL - Represents the offset from the thread pointer (Initial.
Definition: MipsBaseInfo.h:70
void setOpcode(unsigned Op)
Definition: MCInst.h:157
MipsMCInstLower(MipsAsmPrinter &asmprinter)
static const MCBinaryExpr * CreateAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:396
MO_TPREL_HI/LO - Represents the hi and low part of the offset from.
Definition: MipsBaseInfo.h:74
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.
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
const BlockAddress * getBlockAddress() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
MachineBasicBlock reference.
Address of a global value.
Name of external global symbol.