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 "llvm/ADT/SmallString.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Target/Mangler.h"
27 static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
28 return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
32 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
33 MCContext &Ctx = AP.OutContext;
35 SmallString<128> Name;
37 assert(MO.isSymbol() && "Isn't a symbol reference");
38 Name += AP.MAI->getGlobalPrefix();
39 Name += MO.getSymbolName();
41 const GlobalValue *GV = MO.getGlobal();
42 bool isImplicitlyPrivate = false;
43 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB ||
44 (MO.getTargetFlags() & PPCII::MO_NLP_FLAG))
45 isImplicitlyPrivate = true;
47 AP.Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
50 // If the target flags on the operand changes the name of the symbol, do that
51 // before we return the symbol.
52 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB) {
54 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
55 MachineModuleInfoImpl::StubValueTy &StubSym =
56 getMachOMMI(AP).getFnStubEntry(Sym);
57 if (StubSym.getPointer())
62 MachineModuleInfoImpl::
63 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
64 !MO.getGlobal()->hasInternalLinkage());
66 Name.erase(Name.end()-5, Name.end());
68 MachineModuleInfoImpl::
69 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
74 // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
75 // then add the suffix.
76 if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
77 Name += "$non_lazy_ptr";
78 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
80 MachineModuleInfoMachO &MachO = getMachOMMI(AP);
82 MachineModuleInfoImpl::StubValueTy &StubSym =
83 (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
84 MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
86 if (StubSym.getPointer() == 0) {
87 assert(MO.isGlobal() && "Extern symbol not handled yet");
88 StubSym = MachineModuleInfoImpl::
89 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
90 !MO.getGlobal()->hasInternalLinkage());
95 return Ctx.GetOrCreateSymbol(Name.str());
98 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
99 AsmPrinter &Printer, bool isDarwin) {
100 MCContext &Ctx = Printer.OutContext;
101 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
103 unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
106 case PPCII::MO_HA16: RefKind = isDarwin ?
107 MCSymbolRefExpr::VK_PPC_DARWIN_HA16 :
108 MCSymbolRefExpr::VK_PPC_GAS_HA16;
110 case PPCII::MO_LO16: RefKind = isDarwin ?
111 MCSymbolRefExpr::VK_PPC_DARWIN_LO16 :
112 MCSymbolRefExpr::VK_PPC_GAS_LO16;
114 case PPCII::MO_TPREL16_HA: RefKind = MCSymbolRefExpr::VK_PPC_TPREL16_HA;
116 case PPCII::MO_TPREL16_LO: RefKind = MCSymbolRefExpr::VK_PPC_TPREL16_LO;
118 case PPCII::MO_DTPREL16_LO: RefKind = MCSymbolRefExpr::VK_PPC_DTPREL16_LO;
120 case PPCII::MO_TLSLD16_LO: RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO;
122 case PPCII::MO_TOC16_LO: RefKind = MCSymbolRefExpr::VK_PPC_TOC16_LO;
126 // FIXME: This isn't right, but we don't have a good way to express this in
127 // the MC Level, see below.
128 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG)
129 RefKind = MCSymbolRefExpr::VK_None;
131 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
133 if (!MO.isJTI() && MO.getOffset())
134 Expr = MCBinaryExpr::CreateAdd(Expr,
135 MCConstantExpr::Create(MO.getOffset(), Ctx),
138 // Subtract off the PIC base if required.
139 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
140 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
142 const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
143 Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
144 // FIXME: We have no way to make the result be VK_PPC_LO16/VK_PPC_HA16,
145 // since it is not a symbol!
148 return MCOperand::CreateExpr(Expr);
151 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
152 AsmPrinter &AP, bool isDarwin) {
153 OutMI.setOpcode(MI->getOpcode());
155 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156 const MachineOperand &MO = MI->getOperand(i);
159 switch (MO.getType()) {
162 llvm_unreachable("unknown operand type");
163 case MachineOperand::MO_Register:
164 assert(!MO.getSubReg() && "Subregs should be eliminated!");
165 MCOp = MCOperand::CreateReg(MO.getReg());
167 case MachineOperand::MO_Immediate:
168 MCOp = MCOperand::CreateImm(MO.getImm());
170 case MachineOperand::MO_MachineBasicBlock:
171 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
172 MO.getMBB()->getSymbol(), AP.OutContext));
174 case MachineOperand::MO_GlobalAddress:
175 case MachineOperand::MO_ExternalSymbol:
176 MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
178 case MachineOperand::MO_JumpTableIndex:
179 MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
181 case MachineOperand::MO_ConstantPoolIndex:
182 MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
184 case MachineOperand::MO_BlockAddress:
185 MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
188 case MachineOperand::MO_RegisterMask:
192 OutMI.addOperand(MCOp);