LLVM  8.0.0svn
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 //===----------------------------------------------------------------------===//
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"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCExpr.h"
29 #include "llvm/MC/MCInst.h"
31 using namespace llvm;
35 }
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;
45  StringRef Suffix;
47  Suffix = "$non_lazy_ptr";
49  if (!Suffix.empty())
50  Name += DL.getPrivateGlobalPrefix();
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  }
60  Name += Suffix;
61  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
63  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
64  // then add the suffix.
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  }
79  return Sym;
80 }
83  AsmPrinter &Printer, bool isDarwin) {
84  MCContext &Ctx = Printer.OutContext;
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  }
110  if (MO.getTargetFlags() == PPCII::MO_PLT)
111  RefKind = MCSymbolRefExpr::VK_PLT;
113  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
114  const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
115  const TargetMachine &TM = Printer.TM;
116  const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
117  // -msecure-plt option works only in PIC mode. If secure plt mode
118  // is on add 32768 to symbol.
119  if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
121  Expr = MCBinaryExpr::createAdd(Expr,
122  MCConstantExpr::create(32768, Ctx),
123  Ctx);
125  if (!MO.isJTI() && MO.getOffset())
126  Expr = MCBinaryExpr::createAdd(Expr,
128  Ctx);
130  // Subtract off the PIC base if required.
131  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
132  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
134  const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
135  Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
136  }
138  // Add ha16() / lo16() markers if required.
139  switch (access) {
140  case PPCII::MO_LO:
141  Expr = PPCMCExpr::createLo(Expr, isDarwin, Ctx);
142  break;
143  case PPCII::MO_HA:
144  Expr = PPCMCExpr::createHa(Expr, isDarwin, Ctx);
145  break;
146  }
148  return MCOperand::createExpr(Expr);
149 }
152  AsmPrinter &AP, bool isDarwin) {
153  OutMI.setOpcode(MI->getOpcode());
155  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156  MCOperand MCOp;
157  if (LowerPPCMachineOperandToMCOperand(MI->getOperand(i), MCOp, AP,
158  isDarwin))
159  OutMI.addOperand(MCOp);
160  }
161 }
164  MCOperand &OutMO, AsmPrinter &AP,
165  bool isDarwin) {
166  switch (MO.getType()) {
167  default:
168  llvm_unreachable("unknown operand type");
170  assert(!MO.getSubReg() && "Subregs should be eliminated!");
171  assert(MO.getReg() > PPC::NoRegister &&
172  MO.getReg() < PPC::NUM_TARGET_REGS &&
173  "Invalid register for this target!");
174  OutMO = MCOperand::createReg(MO.getReg());
175  return true;
177  OutMO = MCOperand::createImm(MO.getImm());
178  return true;
180  OutMO = MCOperand::createExpr(
182  return true;
185  OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
186  return true;
188  OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
189  return true;
191  OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
192  return true;
194  OutMO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
195  isDarwin);
196  return true;
198  return false;
199  }
200 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
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:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
PointerTy getPointer() const
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:294
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:89
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:98
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.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
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:409
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:63
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:546
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:461
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:100
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Address of a global value.
bool isSecurePlt() const
Definition: PPCSubtarget.h:262
PointerIntPair - This class implements a pair of a pointer and small integer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MO_NLP_FLAG - If this bit is set, the symbol reference is actually to the non_lazy_ptr for the global...
Definition: PPC.h:87
const GlobalValue * getGlobal() const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:82
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
bool hasInternalLinkage() const
Definition: GlobalValue.h:434
Address of a basic block.
static bool isDarwin(object::Archive::Kind Kind)
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:51
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:95
void setOpcode(unsigned Op)
Definition: MCInst.h:173
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:430
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:106
bool LowerPPCMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &OutMO, AsmPrinter &AP, bool isDarwin)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:254
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:123
bool isPositionIndependent() const
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.
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:61
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
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:112
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:216
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
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:414
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:79
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:164
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function&#39;s picbase...
Definition: PPC.h:83
This file describes how to lower LLVM code to machine code.