LLVM  3.7.0
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 "PPC.h"
16 #include "MCTargetDesc/PPCMCExpr.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 
37 
39  const TargetMachine &TM = AP.TM;
40  Mangler *Mang = AP.Mang;
41  const DataLayout *DL = TM.getDataLayout();
42  MCContext &Ctx = AP.OutContext;
43  bool isDarwin = TM.getTargetTriple().isOSDarwin();
44 
46  StringRef Suffix;
48  if (isDarwin)
49  Suffix = "$stub";
50  } else if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG)
51  Suffix = "$non_lazy_ptr";
52 
53  if (!Suffix.empty())
54  Name += DL->getPrivateGlobalPrefix();
55 
56  unsigned PrefixLen = Name.size();
57 
58  if (!MO.isGlobal()) {
59  assert(MO.isSymbol() && "Isn't a symbol reference");
61  } else {
62  const GlobalValue *GV = MO.getGlobal();
63  TM.getNameWithPrefix(Name, GV, *Mang);
64  }
65 
66  unsigned OrigLen = Name.size() - PrefixLen;
67 
68  Name += Suffix;
69  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
70  StringRef OrigName = StringRef(Name).substr(PrefixLen, OrigLen);
71 
72  // If the target flags on the operand changes the name of the symbol, do that
73  // before we return the symbol.
74  if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && isDarwin) {
76  getMachOMMI(AP).getFnStubEntry(Sym);
77  if (StubSym.getPointer())
78  return Sym;
79 
80  if (MO.isGlobal()) {
81  StubSym =
84  !MO.getGlobal()->hasInternalLinkage());
85  } else {
86  StubSym =
88  StubValueTy(Ctx.getOrCreateSymbol(OrigName), false);
89  }
90  return Sym;
91  }
92 
93  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
94  // then add the suffix.
97 
100  MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
101 
102  if (!StubSym.getPointer()) {
103  assert(MO.isGlobal() && "Extern symbol not handled yet");
104  StubSym = MachineModuleInfoImpl::
106  !MO.getGlobal()->hasInternalLinkage());
107  }
108  return Sym;
109  }
110 
111  return Sym;
112 }
113 
115  AsmPrinter &Printer, bool isDarwin) {
116  MCContext &Ctx = Printer.OutContext;
118 
119  unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
120 
121  switch (access) {
122  case PPCII::MO_TPREL_LO:
124  break;
125  case PPCII::MO_TPREL_HA:
127  break;
128  case PPCII::MO_DTPREL_LO:
130  break;
131  case PPCII::MO_TLSLD_LO:
133  break;
134  case PPCII::MO_TOC_LO:
136  break;
137  case PPCII::MO_TLS:
138  RefKind = MCSymbolRefExpr::VK_PPC_TLS;
139  break;
140  }
141 
142  if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && !isDarwin)
143  RefKind = MCSymbolRefExpr::VK_PLT;
144 
145  const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
146 
147  if (!MO.isJTI() && MO.getOffset())
148  Expr = MCBinaryExpr::createAdd(Expr,
150  Ctx);
151 
152  // Subtract off the PIC base if required.
153  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
154  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
155 
156  const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
157  Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
158  }
159 
160  // Add ha16() / lo16() markers if required.
161  switch (access) {
162  case PPCII::MO_LO:
163  Expr = PPCMCExpr::createLo(Expr, isDarwin, Ctx);
164  break;
165  case PPCII::MO_HA:
166  Expr = PPCMCExpr::createHa(Expr, isDarwin, Ctx);
167  break;
168  }
169 
170  return MCOperand::createExpr(Expr);
171 }
172 
174  AsmPrinter &AP, bool isDarwin) {
175  OutMI.setOpcode(MI->getOpcode());
176 
177  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178  const MachineOperand &MO = MI->getOperand(i);
179 
180  MCOperand MCOp;
181  switch (MO.getType()) {
182  default:
183  MI->dump();
184  llvm_unreachable("unknown operand type");
186  assert(!MO.getSubReg() && "Subregs should be eliminated!");
187  assert(MO.getReg() > PPC::NoRegister &&
188  MO.getReg() < PPC::NUM_TARGET_REGS &&
189  "Invalid register for this target!");
190  MCOp = MCOperand::createReg(MO.getReg());
191  break;
193  MCOp = MCOperand::createImm(MO.getImm());
194  break;
197  MO.getMBB()->getSymbol(), AP.OutContext));
198  break;
201  MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
202  break;
204  MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
205  break;
207  MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
208  break;
210  MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
211  isDarwin);
212  break;
214  continue;
215  }
216 
217  OutMI.addOperand(MCOp);
218  }
219 }
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
StubValueTy & getHiddenGVStubEntry(MCSymbol *Sym)
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
const GlobalValue * getGlobal() const
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
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
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:78
static MachineModuleInfoMachO & getMachOMMI(AsmPrinter &AP)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:84
Address of indexed Jump Table for switch.
const char * getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
MachineBasicBlock reference.
const char * getSymbolName() const
print alias Alias Set Printer
Mask of preserved registers.
const Triple & getTargetTriple() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MO_PLT_OR_STUB - On a symbol operand "FOO", this indicates that the reference is actually to the "FOO...
Definition: PPC.h:65
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
bool hasInternalLinkage() const
Definition: GlobalValue.h:278
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Name of external global symbol.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Context object for machine code objects.
Definition: MCContext.h:48
Mangler * Mang
Name-mangler for global names.
Definition: AsmPrinter.h:93
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:514
MO_NLP_HIDDEN_FLAG - If this bit is set, the symbol reference is to a symbol with hidden visibility...
Definition: PPC.h:78
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:446
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:89
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
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:120
Address of a global value.
unsigned getTargetFlags() const
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:73
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:70
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Address of a basic block.
int64_t getOffset() const
Return the offset from the symbol in this operand.
Ty & getObjFileInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
static const PPCMCExpr * createLo(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:49
unsigned getSubReg() const
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
The next are not flags but distinct values.
Definition: PPC.h:81
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
Definition: Triple.h:404
PointerTy getPointer() const
void setOpcode(unsigned Op)
Definition: MCInst.h:158
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, AsmPrinter &Printer, bool isDarwin)
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
MachineOperand class - Representation of each machine instruction operand.
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:92
void dump() const
StubValueTy & getFnStubEntry(MCSymbol *Sym)
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:111
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:108
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
unsigned getReg() const
getReg - Returns the register number.
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.
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 addOperand(const MCOperand &Op)
Definition: MCInst.h:168
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
static MCSymbol * GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP)
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function's picbase...
Definition: PPC.h:69
This file describes how to lower LLVM code to machine code.
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110