1 //===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code to lower PPC MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MCTargetDesc/PPCMCExpr.h"
15 #include "PPC.h"
16 #include "PPCSubtarget.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/CodeGen/TargetLowering.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/TargetLoweringObjectFile.h"
30 using namespace llvm;
31
GetSymbolFromOperand(const MachineOperand & MO,AsmPrinter & AP)32 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO,
33 AsmPrinter &AP) {
34 const TargetMachine &TM = AP.TM;
35 Mangler &Mang = TM.getObjFileLowering()->getMangler();
36 const DataLayout &DL = AP.getDataLayout();
37 MCContext &Ctx = AP.OutContext;
38
39 SmallString<128> Name;
40 if (!MO.isGlobal()) {
41 assert(MO.isSymbol() && "Isn't a symbol reference");
42 Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL);
43 } else {
44 const GlobalValue *GV = MO.getGlobal();
45 TM.getNameWithPrefix(Name, GV, Mang);
46 }
47
48 MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
49
50 return Sym;
51 }
52
GetSymbolRef(const MachineOperand & MO,const MCSymbol * Symbol,AsmPrinter & Printer)53 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
54 AsmPrinter &Printer) {
55 MCContext &Ctx = Printer.OutContext;
56 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
57
58 unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
59
60 switch (access) {
61 case PPCII::MO_TPREL_LO:
62 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
63 break;
64 case PPCII::MO_TPREL_HA:
65 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
66 break;
67 case PPCII::MO_DTPREL_LO:
68 RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
69 break;
70 case PPCII::MO_TLSLD_LO:
71 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
72 break;
73 case PPCII::MO_TOC_LO:
74 RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
75 break;
76 case PPCII::MO_TLS:
77 RefKind = MCSymbolRefExpr::VK_PPC_TLS;
78 break;
79 }
80
81 if (MO.getTargetFlags() == PPCII::MO_PLT)
82 RefKind = MCSymbolRefExpr::VK_PLT;
83 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
84 RefKind = MCSymbolRefExpr::VK_PCREL;
85 else if (MO.getTargetFlags() == (PPCII::MO_PCREL_FLAG | PPCII::MO_GOT_FLAG))
86 RefKind = MCSymbolRefExpr::VK_PPC_GOT_PCREL;
87
88 const MachineInstr *MI = MO.getParent();
89 const MachineFunction *MF = MI->getMF();
90 const Module *M = MF->getFunction().getParent();
91 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
92 const TargetMachine &TM = Printer.TM;
93
94 unsigned MIOpcode = MI->getOpcode();
95 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
96 "BL8_NOTOC is only valid when using PC Relative Calls.");
97 if (Subtarget->isUsingPCRelativeCalls()) {
98 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
99 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
100 MIOpcode == PPC::BL8_NOTOC) {
101 RefKind = MCSymbolRefExpr::VK_PPC_NOTOC;
102 }
103 }
104
105 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
106 // If -msecure-plt -fPIC, add 32768 to symbol.
107 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
108 M->getPICLevel() == PICLevel::BigPIC &&
109 MO.getTargetFlags() == PPCII::MO_PLT)
110 Expr =
111 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
112
113 if (!MO.isJTI() && MO.getOffset())
114 Expr = MCBinaryExpr::createAdd(Expr,
115 MCConstantExpr::create(MO.getOffset(), Ctx),
116 Ctx);
117
118 // Subtract off the PIC base if required.
119 if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
120 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
121
122 const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
123 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
124 }
125
126 // Add ha16() / lo16() markers if required.
127 switch (access) {
128 case PPCII::MO_LO:
129 Expr = PPCMCExpr::createLo(Expr, Ctx);
130 break;
131 case PPCII::MO_HA:
132 Expr = PPCMCExpr::createHa(Expr, Ctx);
133 break;
134 }
135
136 return MCOperand::createExpr(Expr);
137 }
138
LowerPPCMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI,AsmPrinter & AP)139 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
140 AsmPrinter &AP) {
141 OutMI.setOpcode(MI->getOpcode());
142
143 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
144 MCOperand MCOp;
145 if (LowerPPCMachineOperandToMCOperand(MI->getOperand(i), MCOp, AP))
146 OutMI.addOperand(MCOp);
147 }
148 }
149
LowerPPCMachineOperandToMCOperand(const MachineOperand & MO,MCOperand & OutMO,AsmPrinter & AP)150 bool llvm::LowerPPCMachineOperandToMCOperand(const MachineOperand &MO,
151 MCOperand &OutMO, AsmPrinter &AP) {
152 switch (MO.getType()) {
153 default:
154 llvm_unreachable("unknown operand type");
155 case MachineOperand::MO_Register:
156 assert(!MO.getSubReg() && "Subregs should be eliminated!");
157 assert(MO.getReg() > PPC::NoRegister &&
158 MO.getReg() < PPC::NUM_TARGET_REGS &&
159 "Invalid register for this target!");
160 // Ignore all implicit register operands.
161 if (MO.isImplicit())
162 return false;
163 OutMO = MCOperand::createReg(MO.getReg());
164 return true;
165 case MachineOperand::MO_Immediate:
166 OutMO = MCOperand::createImm(MO.getImm());
167 return true;
168 case MachineOperand::MO_MachineBasicBlock:
169 OutMO = MCOperand::createExpr(
170 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext));
171 return true;
172 case MachineOperand::MO_GlobalAddress:
173 case MachineOperand::MO_ExternalSymbol:
174 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
175 return true;
176 case MachineOperand::MO_JumpTableIndex:
177 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
178 return true;
179 case MachineOperand::MO_ConstantPoolIndex:
180 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
181 return true;
182 case MachineOperand::MO_BlockAddress:
183 OutMO =
184 GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
185 return true;
186 case MachineOperand::MO_MCSymbol:
187 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
188 return true;
189 case MachineOperand::MO_RegisterMask:
190 return false;
191 }
192 }
193