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 "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/Mangler.h"
23 #include "llvm/ADT/SmallString.h"
24 using namespace llvm;
25
getMachOMMI(AsmPrinter & AP)26 static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
27 return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
28 }
29
30
GetSymbolFromOperand(const MachineOperand & MO,AsmPrinter & AP)31 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
32 MCContext &Ctx = AP.OutContext;
33
34 SmallString<128> Name;
35 if (!MO.isGlobal()) {
36 assert(MO.isSymbol() && "Isn't a symbol reference");
37 Name += AP.MAI->getGlobalPrefix();
38 Name += MO.getSymbolName();
39 } else {
40 const GlobalValue *GV = MO.getGlobal();
41 bool isImplicitlyPrivate = false;
42 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB ||
43 (MO.getTargetFlags() & PPCII::MO_NLP_FLAG))
44 isImplicitlyPrivate = true;
45
46 AP.Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
47 }
48
49 // If the target flags on the operand changes the name of the symbol, do that
50 // before we return the symbol.
51 if (MO.getTargetFlags() == PPCII::MO_DARWIN_STUB) {
52 Name += "$stub";
53 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
54 MachineModuleInfoImpl::StubValueTy &StubSym =
55 getMachOMMI(AP).getFnStubEntry(Sym);
56 if (StubSym.getPointer())
57 return Sym;
58
59 if (MO.isGlobal()) {
60 StubSym =
61 MachineModuleInfoImpl::
62 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
63 !MO.getGlobal()->hasInternalLinkage());
64 } else {
65 Name.erase(Name.end()-5, Name.end());
66 StubSym =
67 MachineModuleInfoImpl::
68 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
69 }
70 return Sym;
71 }
72
73 // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
74 // then add the suffix.
75 if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
76 Name += "$non_lazy_ptr";
77 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
78
79 MachineModuleInfoMachO &MachO = getMachOMMI(AP);
80
81 MachineModuleInfoImpl::StubValueTy &StubSym =
82 (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
83 MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
84
85 if (StubSym.getPointer() == 0) {
86 assert(MO.isGlobal() && "Extern symbol not handled yet");
87 StubSym = MachineModuleInfoImpl::
88 StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
89 !MO.getGlobal()->hasInternalLinkage());
90 }
91 return Sym;
92 }
93
94 return Ctx.GetOrCreateSymbol(Name.str());
95 }
96
GetSymbolRef(const MachineOperand & MO,const MCSymbol * Symbol,AsmPrinter & Printer,bool isDarwin)97 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
98 AsmPrinter &Printer, bool isDarwin) {
99 MCContext &Ctx = Printer.OutContext;
100 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
101
102 if (MO.getTargetFlags() & PPCII::MO_LO16)
103 RefKind = isDarwin ? MCSymbolRefExpr::VK_PPC_DARWIN_LO16 : MCSymbolRefExpr::VK_PPC_GAS_LO16;
104 else if (MO.getTargetFlags() & PPCII::MO_HA16)
105 RefKind = isDarwin ? MCSymbolRefExpr::VK_PPC_DARWIN_HA16 : MCSymbolRefExpr::VK_PPC_GAS_HA16;
106
107 // FIXME: This isn't right, but we don't have a good way to express this in
108 // the MC Level, see below.
109 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG)
110 RefKind = MCSymbolRefExpr::VK_None;
111
112 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
113
114 if (!MO.isJTI() && MO.getOffset())
115 Expr = MCBinaryExpr::CreateAdd(Expr,
116 MCConstantExpr::Create(MO.getOffset(), Ctx),
117 Ctx);
118
119 // Subtract off the PIC base if required.
120 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
121 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
122
123 const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
124 Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
125 // FIXME: We have no way to make the result be VK_PPC_LO16/VK_PPC_HA16,
126 // since it is not a symbol!
127 }
128
129 return MCOperand::CreateExpr(Expr);
130 }
131
LowerPPCMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI,AsmPrinter & AP,bool isDarwin)132 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
133 AsmPrinter &AP, bool isDarwin) {
134 OutMI.setOpcode(MI->getOpcode());
135
136 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
137 const MachineOperand &MO = MI->getOperand(i);
138
139 MCOperand MCOp;
140 switch (MO.getType()) {
141 default:
142 MI->dump();
143 assert(0 && "unknown operand type");
144 case MachineOperand::MO_Register:
145 assert(!MO.getSubReg() && "Subregs should be eliminated!");
146 MCOp = MCOperand::CreateReg(MO.getReg());
147 break;
148 case MachineOperand::MO_Immediate:
149 MCOp = MCOperand::CreateImm(MO.getImm());
150 break;
151 case MachineOperand::MO_MachineBasicBlock:
152 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
153 MO.getMBB()->getSymbol(), AP.OutContext));
154 break;
155 case MachineOperand::MO_GlobalAddress:
156 case MachineOperand::MO_ExternalSymbol:
157 MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
158 break;
159 case MachineOperand::MO_JumpTableIndex:
160 MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
161 break;
162 case MachineOperand::MO_ConstantPoolIndex:
163 MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
164 break;
165 case MachineOperand::MO_BlockAddress:
166 MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
167 isDarwin);
168 break;
169 }
170
171 OutMI.addOperand(MCOp);
172 }
173 }
174