LLVM  6.0.0svn
PPCMCInstLower.cpp
Go to the documentation of this file.
1 //===-- PPCMCInstLower.cpp - Convert PPC 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 PPC MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "MCTargetDesc/PPCMCExpr.h"
16 #include "PPC.h"
17 #include "PPCSubtarget.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Twine.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/GlobalValue.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
31 using namespace llvm;
32 
35 }
36 
38  AsmPrinter &AP) {
39  const TargetMachine &TM = AP.TM;
40  Mangler &Mang = TM.getObjFileLowering()->getMangler();
41  const DataLayout &DL = AP.getDataLayout();
42  MCContext &Ctx = AP.OutContext;
43 
45  StringRef Suffix;
47  Suffix = "$non_lazy_ptr";
48 
49  if (!Suffix.empty())
50  Name += DL.getPrivateGlobalPrefix();
51 
52  if (!MO.isGlobal()) {
53  assert(MO.isSymbol() && "Isn't a symbol reference");
55  } else {
56  const GlobalValue *GV = MO.getGlobal();
57  TM.getNameWithPrefix(Name, GV, Mang);
58  }
59 
60  Name += Suffix;
61  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
62 
63  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
64  // then add the suffix.
67 
69 
70  if (!StubSym.getPointer()) {
71  assert(MO.isGlobal() && "Extern symbol not handled yet");
72  StubSym = MachineModuleInfoImpl::
74  !MO.getGlobal()->hasInternalLinkage());
75  }
76  return Sym;
77  }
78 
79  return Sym;
80 }
81 
83  AsmPrinter &Printer, bool isDarwin) {
84  MCContext &Ctx = Printer.OutContext;
86 
88 
89  switch (access) {
90  case PPCII::MO_TPREL_LO:
92  break;
93  case PPCII::MO_TPREL_HA:
95  break;
98  break;
99  case PPCII::MO_TLSLD_LO:
101  break;
102  case PPCII::MO_TOC_LO:
104  break;
105  case PPCII::MO_TLS:
106  RefKind = MCSymbolRefExpr::VK_PPC_TLS;
107  break;
108  }
109 
110  if (MO.getTargetFlags() == PPCII::MO_PLT)
111  RefKind = MCSymbolRefExpr::VK_PLT;
112 
113  const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
114 
115  if (!MO.isJTI() && MO.getOffset())
116  Expr = MCBinaryExpr::createAdd(Expr,
118  Ctx);
119 
120  // Subtract off the PIC base if required.
121  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
122  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
123 
124  const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
125  Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
126  }
127 
128  // Add ha16() / lo16() markers if required.
129  switch (access) {
130  case PPCII::MO_LO:
131  Expr = PPCMCExpr::createLo(Expr, isDarwin, Ctx);
132  break;
133  case PPCII::MO_HA:
134  Expr = PPCMCExpr::createHa(Expr, isDarwin, Ctx);
135  break;
136  }
137 
138  return MCOperand::createExpr(Expr);
139 }
140 
142  AsmPrinter &AP, bool isDarwin) {
143  OutMI.setOpcode(MI->getOpcode());
144 
145  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
146  const MachineOperand &MO = MI->getOperand(i);
147 
148  MCOperand MCOp;
149  switch (MO.getType()) {
150  default:
151  MI->print(errs());
152  llvm_unreachable("unknown operand type");
154  assert(!MO.getSubReg() && "Subregs should be eliminated!");
155  assert(MO.getReg() > PPC::NoRegister &&
156  MO.getReg() < PPC::NUM_TARGET_REGS &&
157  "Invalid register for this target!");
158  MCOp = MCOperand::createReg(MO.getReg());
159  break;
161  MCOp = MCOperand::createImm(MO.getImm());
162  break;
165  MO.getMBB()->getSymbol(), AP.OutContext));
166  break;
169  MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
170  break;
172  MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
173  break;
175  MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
176  break;
178  MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
179  isDarwin);
180  break;
182  continue;
183  }
184 
185  OutMI.addOperand(MCOp);
186  }
187 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineBasicBlock * getMBB() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
PointerTy getPointer() const
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
static MachineModuleInfoMachO & getMachOMMI(AsmPrinter &AP)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:88
Address of indexed Jump Table for switch.
unsigned getSubReg() const
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
MachineBasicBlock reference.
print alias Alias Set Printer
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:59
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:528
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:443
const char * Name
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:99
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
Address of a global value.
PointerIntPair - This class implements a pair of a pointer and small integer.
MO_NLP_FLAG - If this bit is set, the symbol reference is actually to the non_lazy_ptr for the global...
Definition: PPC.h:77
const GlobalValue * getGlobal() const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:80
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:76
bool hasInternalLinkage() const
Definition: GlobalValue.h:414
Address of a basic block.
Ty & getObjFileInfo()
Keep track of various per-function pieces of information for backends that would like to do so...
static const PPCMCExpr * createLo(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:49
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
The next are not flags but distinct values.
Definition: PPC.h:85
void setOpcode(unsigned Op)
Definition: MCInst.h:167
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, AsmPrinter &Printer, bool isDarwin)
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
virtual TargetLoweringObjectFile * getObjFileLowering() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:420
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:96
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
Representation of each machine instruction.
Definition: MachineInstr.h:59
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:121
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.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
static const PPCMCExpr * createHa(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:59
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:56
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP, bool isDarwin)
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:109
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:196
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Address of indexed Constant in Constant Pool.
static MCSymbol * GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
On a symbol operand "FOO", this indicates that the reference is actually to "FOO@plt".
Definition: PPC.h:69
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:159
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function&#39;s picbase...
Definition: PPC.h:73
This file describes how to lower LLVM code to machine code.