1 //===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower PPC MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/PPCMCExpr.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfoImpls.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"
29 static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
30 return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
34 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
35 MCContext &Ctx = AP.OutContext;
37 SmallString<128> Name;
39 assert(MO.isSymbol() && "Isn't a symbol reference");
40 Name += AP.MAI->getGlobalPrefix();
41 Name += MO.getSymbolName();
43 const GlobalValue *GV = MO.getGlobal();
44 bool isImplicitlyPrivate = false;
45 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB ||
46 (MO.getTargetFlags() & PPCII::MO_NLP_FLAG))
47 isImplicitlyPrivate = true;
49 AP.Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
52 // If the target flags on the operand changes the name of the symbol, do that
53 // before we return the symbol.
54 if (MO.getTargetFlags() == PPCII::MO_DARWIN_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'
63 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Twine(Prefix) + Twine(Name));
64 MachineModuleInfoImpl::StubValueTy &StubSym =
65 getMachOMMI(AP).getFnStubEntry(Sym);
66 if (StubSym.getPointer())
71 MachineModuleInfoImpl::
72 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
73 !MO.getGlobal()->hasInternalLinkage());
75 Name.erase(Name.end()-5, Name.end());
77 MachineModuleInfoImpl::
78 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
83 // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
84 // then add the suffix.
85 if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
86 Name += "$non_lazy_ptr";
87 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
89 MachineModuleInfoMachO &MachO = getMachOMMI(AP);
91 MachineModuleInfoImpl::StubValueTy &StubSym =
92 (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
93 MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
95 if (StubSym.getPointer() == 0) {
96 assert(MO.isGlobal() && "Extern symbol not handled yet");
97 StubSym = MachineModuleInfoImpl::
98 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
99 !MO.getGlobal()->hasInternalLinkage());
104 return Ctx.GetOrCreateSymbol(Name.str());
107 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
108 AsmPrinter &Printer, bool isDarwin) {
109 MCContext &Ctx = Printer.OutContext;
110 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
112 unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
115 case PPCII::MO_TPREL16_HA:
116 RefKind = MCSymbolRefExpr::VK_PPC_TPREL16_HA;
118 case PPCII::MO_TPREL16_LO:
119 RefKind = MCSymbolRefExpr::VK_PPC_TPREL16_LO;
121 case PPCII::MO_DTPREL16_LO:
122 RefKind = MCSymbolRefExpr::VK_PPC_DTPREL16_LO;
124 case PPCII::MO_TLSLD16_LO:
125 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO;
127 case PPCII::MO_TOC16_LO:
128 RefKind = MCSymbolRefExpr::VK_PPC_TOC16_LO;
132 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
134 if (!MO.isJTI() && MO.getOffset())
135 Expr = MCBinaryExpr::CreateAdd(Expr,
136 MCConstantExpr::Create(MO.getOffset(), Ctx),
139 // Subtract off the PIC base if required.
140 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
141 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
143 const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
144 Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
147 // Add ha16() / lo16() markers if required.
150 Expr = PPCMCExpr::CreateHa16(Expr, Ctx);
153 Expr = PPCMCExpr::CreateLo16(Expr, Ctx);
157 return MCOperand::CreateExpr(Expr);
160 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
161 AsmPrinter &AP, bool isDarwin) {
162 OutMI.setOpcode(MI->getOpcode());
164 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
165 const MachineOperand &MO = MI->getOperand(i);
168 switch (MO.getType()) {
171 llvm_unreachable("unknown operand type");
172 case MachineOperand::MO_Register:
173 assert(!MO.getSubReg() && "Subregs should be eliminated!");
174 MCOp = MCOperand::CreateReg(MO.getReg());
176 case MachineOperand::MO_Immediate:
177 MCOp = MCOperand::CreateImm(MO.getImm());
179 case MachineOperand::MO_MachineBasicBlock:
180 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
181 MO.getMBB()->getSymbol(), AP.OutContext));
183 case MachineOperand::MO_GlobalAddress:
184 case MachineOperand::MO_ExternalSymbol:
185 MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
187 case MachineOperand::MO_JumpTableIndex:
188 MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
190 case MachineOperand::MO_ConstantPoolIndex:
191 MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
193 case MachineOperand::MO_BlockAddress:
194 MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
197 case MachineOperand::MO_RegisterMask:
201 OutMI.addOperand(MCOp);