LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/Target/Mangler.h"
27 using namespace llvm;
28 
31 }
32 
33 
35  MCContext &Ctx = AP.OutContext;
36 
38  if (!MO.isGlobal()) {
39  assert(MO.isSymbol() && "Isn't a symbol reference");
40  Name += AP.MAI->getGlobalPrefix();
41  Name += MO.getSymbolName();
42  } else {
43  const GlobalValue *GV = MO.getGlobal();
44  bool isImplicitlyPrivate = false;
47  isImplicitlyPrivate = true;
48 
49  AP.Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
50  }
51 
52  // If the target flags on the operand changes the name of the symbol, do that
53  // before we return the symbol.
55  Name += "$stub";
56  const char *PGP = AP.MAI->getPrivateGlobalPrefix();
57  const char *Prefix = "";
58  if (!Name.startswith(PGP)) {
59  // http://llvm.org/bugs/show_bug.cgi?id=15763
60  // all stubs and lazy_ptrs should be local symbols, which need leading 'L'
61  Prefix = PGP;
62  }
63  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Twine(Prefix) + Twine(Name));
65  getMachOMMI(AP).getFnStubEntry(Sym);
66  if (StubSym.getPointer())
67  return Sym;
68 
69  if (MO.isGlobal()) {
70  StubSym =
73  !MO.getGlobal()->hasInternalLinkage());
74  } else {
75  Name.erase(Name.end()-5, Name.end());
76  StubSym =
78  StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
79  }
80  return Sym;
81  }
82 
83  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
84  // then add the suffix.
86  Name += "$non_lazy_ptr";
87  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
88 
90 
93  MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
94 
95  if (StubSym.getPointer() == 0) {
96  assert(MO.isGlobal() && "Extern symbol not handled yet");
97  StubSym = MachineModuleInfoImpl::
99  !MO.getGlobal()->hasInternalLinkage());
100  }
101  return Sym;
102  }
103 
104  return Ctx.GetOrCreateSymbol(Name.str());
105 }
106 
108  AsmPrinter &Printer, bool isDarwin) {
109  MCContext &Ctx = Printer.OutContext;
111 
112  unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
113 
114  switch (access) {
115  case PPCII::MO_TPREL_LO:
117  break;
118  case PPCII::MO_TPREL_HA:
120  break;
121  case PPCII::MO_DTPREL_LO:
123  break;
124  case PPCII::MO_TLSLD_LO:
126  break;
127  case PPCII::MO_TOC_LO:
129  break;
130  case PPCII::MO_TLS:
131  RefKind = MCSymbolRefExpr::VK_PPC_TLS;
132  break;
133  }
134 
135  const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
136 
137  if (!MO.isJTI() && MO.getOffset())
138  Expr = MCBinaryExpr::CreateAdd(Expr,
140  Ctx);
141 
142  // Subtract off the PIC base if required.
143  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
144  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
145 
146  const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
147  Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
148  }
149 
150  // Add ha16() / lo16() markers if required.
151  switch (access) {
152  case PPCII::MO_LO:
153  Expr = PPCMCExpr::CreateLo(Expr, isDarwin, Ctx);
154  break;
155  case PPCII::MO_HA:
156  Expr = PPCMCExpr::CreateHa(Expr, isDarwin, Ctx);
157  break;
158  }
159 
160  return MCOperand::CreateExpr(Expr);
161 }
162 
164  AsmPrinter &AP, bool isDarwin) {
165  OutMI.setOpcode(MI->getOpcode());
166 
167  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168  const MachineOperand &MO = MI->getOperand(i);
169 
170  MCOperand MCOp;
171  switch (MO.getType()) {
172  default:
173  MI->dump();
174  llvm_unreachable("unknown operand type");
176  assert(!MO.getSubReg() && "Subregs should be eliminated!");
177  MCOp = MCOperand::CreateReg(MO.getReg());
178  break;
180  MCOp = MCOperand::CreateImm(MO.getImm());
181  break;
184  MO.getMBB()->getSymbol(), AP.OutContext));
185  break;
188  MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
189  break;
191  MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
192  break;
194  MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
195  break;
197  MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
198  isDarwin);
199  break;
201  continue;
202  }
203 
204  OutMI.addOperand(MCOp);
205  }
206 }
static const PPCMCExpr * CreateHa(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:58
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
StubValueTy & getHiddenGVStubEntry(MCSymbol *Sym)
const MachineFunction * getParent() const
MachineInstr * getParent()
const GlobalValue * getGlobal() const
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
MCContext & OutContext
Definition: AsmPrinter.h:72
static MachineModuleInfoMachO & getMachOMMI(AsmPrinter &AP)
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, bool isImplicitlyPrivate, bool UseGlobalPrefix=true)
Definition: Mangler.cpp:90
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:78
const char * getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:434
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
const char * getSymbolName() const
print alias Alias Set Printer
Address of indexed Jump Table for switch.
static const PPCMCExpr * CreateLo(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:48
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
int access(const char *path, int amode);
MCSymbol * GetOrCreateSymbol(StringRef Name)
Definition: MCContext.cpp:118
#define llvm_unreachable(msg)
bool hasInternalLinkage() const
Definition: GlobalValue.h:205
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
GetJTISymbol - Return the symbol for the specified jump table entry.
static const MCBinaryExpr * CreateSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:460
unsigned getNumOperands() const
Definition: MachineInstr.h:265
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Mangler * Mang
Definition: AsmPrinter.h:88
int getOpcode() const
Definition: MachineInstr.h:261
int64_t getImm() const
Address of indexed Constant in Constant Pool.
MachineModuleInfo * MMI
MMI - This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:84
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
unsigned getTargetFlags() const
const MCAsmInfo * MAI
Definition: AsmPrinter.h:66
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
int64_t getOffset() const
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:133
unsigned getSubReg() const
iterator erase(iterator I)
Definition: SmallVector.h:478
MCSymbol * getSymbol() const
StubValueTy & getGVStubEntry(MCSymbol *Sym)
The next are not flags but distinct values.
Definition: PPC.h:75
PointerTy getPointer() const
void setOpcode(unsigned Op)
Definition: MCInst.h:157
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, AsmPrinter &Printer, bool isDarwin)
MCSymbol * getPICBaseSymbol() const
const char * getGlobalPrefix() const
Definition: MCAsmInfo.h:431
void dump() const
StubValueTy & getFnStubEntry(MCSymbol *Sym)
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
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 LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP, bool isDarwin)
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
const BlockAddress * getBlockAddress() const
static MCSymbol * GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP)
MachineBasicBlock reference.
Address of a global value.
Name of external global symbol.