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/DataLayout.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/Mangler.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
32 static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
33 return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
37 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
38 const TargetMachine &TM = AP.TM;
39 Mangler *Mang = AP.Mang;
40 const DataLayout *DL = TM.getDataLayout();
41 MCContext &Ctx = AP.OutContext;
43 SmallString<128> Name;
45 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB)
47 else if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG)
48 Suffix = "$non_lazy_ptr";
51 Name += DL->getPrivateGlobalPrefix();
53 unsigned PrefixLen = Name.size();
56 assert(MO.isSymbol() && "Isn't a symbol reference");
57 Mang->getNameWithPrefix(Name, MO.getSymbolName());
59 const GlobalValue *GV = MO.getGlobal();
60 TM.getTargetLowering()->getNameWithPrefix(Name, GV, *Mang);
63 unsigned OrigLen = Name.size() - PrefixLen;
66 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
67 StringRef OrigName = StringRef(Name).substr(PrefixLen, OrigLen);
69 // If the target flags on the operand changes the name of the symbol, do that
70 // before we return the symbol.
71 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB) {
72 MachineModuleInfoImpl::StubValueTy &StubSym =
73 getMachOMMI(AP).getFnStubEntry(Sym);
74 if (StubSym.getPointer())
79 MachineModuleInfoImpl::
80 StubValueTy(AP.getSymbol(MO.getGlobal()),
81 !MO.getGlobal()->hasInternalLinkage());
84 MachineModuleInfoImpl::
85 StubValueTy(Ctx.GetOrCreateSymbol(OrigName), false);
90 // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
91 // then add the suffix.
92 if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
93 MachineModuleInfoMachO &MachO = getMachOMMI(AP);
95 MachineModuleInfoImpl::StubValueTy &StubSym =
96 (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
97 MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
99 if (StubSym.getPointer() == 0) {
100 assert(MO.isGlobal() && "Extern symbol not handled yet");
101 StubSym = MachineModuleInfoImpl::
102 StubValueTy(AP.getSymbol(MO.getGlobal()),
103 !MO.getGlobal()->hasInternalLinkage());
111 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
112 AsmPrinter &Printer, bool isDarwin) {
113 MCContext &Ctx = Printer.OutContext;
114 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
116 unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
119 case PPCII::MO_TPREL_LO:
120 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
122 case PPCII::MO_TPREL_HA:
123 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
125 case PPCII::MO_DTPREL_LO:
126 RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
128 case PPCII::MO_TLSLD_LO:
129 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
131 case PPCII::MO_TOC_LO:
132 RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
135 RefKind = MCSymbolRefExpr::VK_PPC_TLS;
139 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
141 if (!MO.isJTI() && MO.getOffset())
142 Expr = MCBinaryExpr::CreateAdd(Expr,
143 MCConstantExpr::Create(MO.getOffset(), Ctx),
146 // Subtract off the PIC base if required.
147 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
148 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
150 const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
151 Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
154 // Add ha16() / lo16() markers if required.
157 Expr = PPCMCExpr::CreateLo(Expr, isDarwin, Ctx);
160 Expr = PPCMCExpr::CreateHa(Expr, isDarwin, Ctx);
164 return MCOperand::CreateExpr(Expr);
167 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
168 AsmPrinter &AP, bool isDarwin) {
169 OutMI.setOpcode(MI->getOpcode());
171 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
172 const MachineOperand &MO = MI->getOperand(i);
175 switch (MO.getType()) {
178 llvm_unreachable("unknown operand type");
179 case MachineOperand::MO_Register:
180 assert(!MO.getSubReg() && "Subregs should be eliminated!");
181 MCOp = MCOperand::CreateReg(MO.getReg());
183 case MachineOperand::MO_Immediate:
184 MCOp = MCOperand::CreateImm(MO.getImm());
186 case MachineOperand::MO_MachineBasicBlock:
187 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
188 MO.getMBB()->getSymbol(), AP.OutContext));
190 case MachineOperand::MO_GlobalAddress:
191 case MachineOperand::MO_ExternalSymbol:
192 MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
194 case MachineOperand::MO_JumpTableIndex:
195 MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
197 case MachineOperand::MO_ConstantPoolIndex:
198 MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
200 case MachineOperand::MO_BlockAddress:
201 MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
204 case MachineOperand::MO_RegisterMask:
208 OutMI.addOperand(MCOp);