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 "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"
27using namespace llvm;
28
29static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
30  return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
31}
32
33
34static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
35  MCContext &Ctx = AP.OutContext;
36
37  SmallString<128> Name;
38  if (!MO.isGlobal()) {
39    assert(MO.isSymbol() && "Isn't a symbol reference");
40    Name += AP.MAI->getGlobalPrefix();
41    Name += MO.getSymbolName();
42  } else {
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;
48
49    AP.Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
50  }
51
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) {
55    Name += "$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'
61      Prefix = PGP;
62    }
63    MCSymbol *Sym = Ctx.GetOrCreateSymbol(Twine(Prefix) + Twine(Name));
64    MachineModuleInfoImpl::StubValueTy &StubSym =
65      getMachOMMI(AP).getFnStubEntry(Sym);
66    if (StubSym.getPointer())
67      return Sym;
68
69    if (MO.isGlobal()) {
70      StubSym =
71      MachineModuleInfoImpl::
72      StubValueTy(AP.Mang->getSymbol(MO.getGlobal()),
73                  !MO.getGlobal()->hasInternalLinkage());
74    } else {
75      Name.erase(Name.end()-5, Name.end());
76      StubSym =
77      MachineModuleInfoImpl::
78      StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
79    }
80    return Sym;
81  }
82
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());
88
89    MachineModuleInfoMachO &MachO = getMachOMMI(AP);
90
91    MachineModuleInfoImpl::StubValueTy &StubSym =
92      (MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
93         MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
94
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());
100    }
101    return Sym;
102  }
103
104  return Ctx.GetOrCreateSymbol(Name.str());
105}
106
107static 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;
111
112  unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
113
114  switch (access) {
115    case PPCII::MO_TPREL_LO:
116      RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
117      break;
118    case PPCII::MO_TPREL_HA:
119      RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
120      break;
121    case PPCII::MO_DTPREL_LO:
122      RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
123      break;
124    case PPCII::MO_TLSLD_LO:
125      RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
126      break;
127    case PPCII::MO_TOC_LO:
128      RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
129      break;
130    case PPCII::MO_TLS:
131      RefKind = MCSymbolRefExpr::VK_PPC_TLS;
132      break;
133  }
134
135  const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
136
137  if (!MO.isJTI() && MO.getOffset())
138    Expr = MCBinaryExpr::CreateAdd(Expr,
139                                   MCConstantExpr::Create(MO.getOffset(), Ctx),
140                                   Ctx);
141
142  // Subtract off the PIC base if required.
143  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
144    const MachineFunction *MF = MO.getParent()->getParent()->getParent();
145
146    const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
147    Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
148  }
149
150  // Add ha16() / lo16() markers if required.
151  switch (access) {
152    case PPCII::MO_LO:
153      Expr = PPCMCExpr::CreateLo(Expr, isDarwin, Ctx);
154      break;
155    case PPCII::MO_HA:
156      Expr = PPCMCExpr::CreateHa(Expr, isDarwin, Ctx);
157      break;
158  }
159
160  return MCOperand::CreateExpr(Expr);
161}
162
163void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
164                                        AsmPrinter &AP, bool isDarwin) {
165  OutMI.setOpcode(MI->getOpcode());
166
167  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168    const MachineOperand &MO = MI->getOperand(i);
169
170    MCOperand MCOp;
171    switch (MO.getType()) {
172    default:
173      MI->dump();
174      llvm_unreachable("unknown operand type");
175    case MachineOperand::MO_Register:
176      assert(!MO.getSubReg() && "Subregs should be eliminated!");
177      MCOp = MCOperand::CreateReg(MO.getReg());
178      break;
179    case MachineOperand::MO_Immediate:
180      MCOp = MCOperand::CreateImm(MO.getImm());
181      break;
182    case MachineOperand::MO_MachineBasicBlock:
183      MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
184                                      MO.getMBB()->getSymbol(), AP.OutContext));
185      break;
186    case MachineOperand::MO_GlobalAddress:
187    case MachineOperand::MO_ExternalSymbol:
188      MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
189      break;
190    case MachineOperand::MO_JumpTableIndex:
191      MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
192      break;
193    case MachineOperand::MO_ConstantPoolIndex:
194      MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
195      break;
196    case MachineOperand::MO_BlockAddress:
197      MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
198                          isDarwin);
199      break;
200    case MachineOperand::MO_RegisterMask:
201      continue;
202    }
203
204    OutMI.addOperand(MCOp);
205  }
206}
207