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 "MCTargetDesc/PPCMCExpr.h"
17 #include "PPCSubtarget.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/GlobalValue.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 using namespace llvm;
32 
getMachOMMI(AsmPrinter & AP)33 static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
34   return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
35 }
36 
37 
GetSymbolFromOperand(const MachineOperand & MO,AsmPrinter & AP)38 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
39   const TargetMachine &TM = AP.TM;
40   Mangler *Mang = AP.Mang;
41   const DataLayout *DL = TM.getDataLayout();
42   MCContext &Ctx = AP.OutContext;
43   bool isDarwin = Triple(TM.getTargetTriple()).isOSDarwin();
44 
45   SmallString<128> Name;
46   StringRef Suffix;
47   if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB) {
48     if (isDarwin)
49       Suffix = "$stub";
50   } else if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG)
51     Suffix = "$non_lazy_ptr";
52 
53   if (!Suffix.empty())
54     Name += DL->getPrivateGlobalPrefix();
55 
56   unsigned PrefixLen = Name.size();
57 
58   if (!MO.isGlobal()) {
59     assert(MO.isSymbol() && "Isn't a symbol reference");
60     Mang->getNameWithPrefix(Name, MO.getSymbolName());
61   } else {
62     const GlobalValue *GV = MO.getGlobal();
63     TM.getNameWithPrefix(Name, GV, *Mang);
64   }
65 
66   unsigned OrigLen = Name.size() - PrefixLen;
67 
68   Name += Suffix;
69   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
70   StringRef OrigName = StringRef(Name).substr(PrefixLen, OrigLen);
71 
72   // If the target flags on the operand changes the name of the symbol, do that
73   // before we return the symbol.
74   if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && isDarwin) {
75     MachineModuleInfoImpl::StubValueTy &StubSym =
76       getMachOMMI(AP).getFnStubEntry(Sym);
77     if (StubSym.getPointer())
78       return Sym;
79 
80     if (MO.isGlobal()) {
81       StubSym =
82       MachineModuleInfoImpl::
83       StubValueTy(AP.getSymbol(MO.getGlobal()),
84                   !MO.getGlobal()->hasInternalLinkage());
85     } else {
86       StubSym =
87       MachineModuleInfoImpl::
88       StubValueTy(Ctx.GetOrCreateSymbol(OrigName), false);
89     }
90     return Sym;
91   }
92 
93   // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
94   // then add the suffix.
95   if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
96     MachineModuleInfoMachO &MachO = getMachOMMI(AP);
97 
98     MachineModuleInfoImpl::StubValueTy &StubSym =
99       (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
100          MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
101 
102     if (!StubSym.getPointer()) {
103       assert(MO.isGlobal() && "Extern symbol not handled yet");
104       StubSym = MachineModuleInfoImpl::
105                    StubValueTy(AP.getSymbol(MO.getGlobal()),
106                                !MO.getGlobal()->hasInternalLinkage());
107     }
108     return Sym;
109   }
110 
111   return Sym;
112 }
113 
GetSymbolRef(const MachineOperand & MO,const MCSymbol * Symbol,AsmPrinter & Printer,bool isDarwin)114 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
115                               AsmPrinter &Printer, bool isDarwin) {
116   MCContext &Ctx = Printer.OutContext;
117   MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
118 
119   unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
120 
121   switch (access) {
122     case PPCII::MO_TPREL_LO:
123       RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
124       break;
125     case PPCII::MO_TPREL_HA:
126       RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
127       break;
128     case PPCII::MO_DTPREL_LO:
129       RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
130       break;
131     case PPCII::MO_TLSLD_LO:
132       RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
133       break;
134     case PPCII::MO_TOC_LO:
135       RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
136       break;
137     case PPCII::MO_TLS:
138       RefKind = MCSymbolRefExpr::VK_PPC_TLS;
139       break;
140   }
141 
142   if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && !isDarwin)
143     RefKind = MCSymbolRefExpr::VK_PLT;
144 
145   const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
146 
147   if (!MO.isJTI() && MO.getOffset())
148     Expr = MCBinaryExpr::CreateAdd(Expr,
149                                    MCConstantExpr::Create(MO.getOffset(), Ctx),
150                                    Ctx);
151 
152   // Subtract off the PIC base if required.
153   if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
154     const MachineFunction *MF = MO.getParent()->getParent()->getParent();
155 
156     const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
157     Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
158   }
159 
160   // Add ha16() / lo16() markers if required.
161   switch (access) {
162     case PPCII::MO_LO:
163       Expr = PPCMCExpr::CreateLo(Expr, isDarwin, Ctx);
164       break;
165     case PPCII::MO_HA:
166       Expr = PPCMCExpr::CreateHa(Expr, isDarwin, Ctx);
167       break;
168   }
169 
170   return MCOperand::CreateExpr(Expr);
171 }
172 
LowerPPCMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI,AsmPrinter & AP,bool isDarwin)173 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
174                                         AsmPrinter &AP, bool isDarwin) {
175   OutMI.setOpcode(MI->getOpcode());
176 
177   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178     const MachineOperand &MO = MI->getOperand(i);
179 
180     MCOperand MCOp;
181     switch (MO.getType()) {
182     default:
183       MI->dump();
184       llvm_unreachable("unknown operand type");
185     case MachineOperand::MO_Register:
186       assert(!MO.getSubReg() && "Subregs should be eliminated!");
187       assert(MO.getReg() > PPC::NoRegister &&
188              MO.getReg() < PPC::NUM_TARGET_REGS &&
189              "Invalid register for this target!");
190       MCOp = MCOperand::CreateReg(MO.getReg());
191       break;
192     case MachineOperand::MO_Immediate:
193       MCOp = MCOperand::CreateImm(MO.getImm());
194       break;
195     case MachineOperand::MO_MachineBasicBlock:
196       MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
197                                       MO.getMBB()->getSymbol(), AP.OutContext));
198       break;
199     case MachineOperand::MO_GlobalAddress:
200     case MachineOperand::MO_ExternalSymbol:
201       MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
202       break;
203     case MachineOperand::MO_JumpTableIndex:
204       MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
205       break;
206     case MachineOperand::MO_ConstantPoolIndex:
207       MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
208       break;
209     case MachineOperand::MO_BlockAddress:
210       MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
211                           isDarwin);
212       break;
213     case MachineOperand::MO_RegisterMask:
214       continue;
215     }
216 
217     OutMI.addOperand(MCOp);
218   }
219 }
220