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 32 static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, 33 AsmPrinter &AP) { 34 if (MO.isGlobal()) { 35 // Get the symbol from the global, accounting for XCOFF-specific 36 // intricacies (see TargetLoweringObjectFileXCOFF::getTargetSymbol). 37 const GlobalValue *GV = MO.getGlobal(); 38 return AP.getSymbol(GV); 39 } 40 41 assert(MO.isSymbol() && "Isn't a symbol reference"); 42 43 SmallString<128> Name; 44 const DataLayout &DL = AP.getDataLayout(); 45 Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL); 46 47 MCContext &Ctx = AP.OutContext; 48 MCSymbol *Sym = Ctx.getOrCreateSymbol(Name); 49 return Sym; 50 } 51 52 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, 53 AsmPrinter &Printer) { 54 MCContext &Ctx = Printer.OutContext; 55 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None; 56 57 unsigned access = MO.getTargetFlags(); 58 59 switch (access) { 60 case PPCII::MO_TPREL_LO: 61 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO; 62 break; 63 case PPCII::MO_TPREL_HA: 64 RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA; 65 break; 66 case PPCII::MO_DTPREL_LO: 67 RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO; 68 break; 69 case PPCII::MO_TLSLD_LO: 70 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO; 71 break; 72 case PPCII::MO_TOC_LO: 73 RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO; 74 break; 75 case PPCII::MO_TLS: 76 RefKind = MCSymbolRefExpr::VK_PPC_TLS; 77 break; 78 case PPCII::MO_TLS_PCREL_FLAG: 79 RefKind = MCSymbolRefExpr::VK_PPC_TLS_PCREL; 80 break; 81 } 82 83 const TargetMachine &TM = Printer.TM; 84 85 if (MO.getTargetFlags() == PPCII::MO_PLT) 86 RefKind = MCSymbolRefExpr::VK_PLT; 87 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG) 88 RefKind = MCSymbolRefExpr::VK_PCREL; 89 else if (MO.getTargetFlags() == PPCII::MO_GOT_PCREL_FLAG) 90 RefKind = MCSymbolRefExpr::VK_PPC_GOT_PCREL; 91 else if (MO.getTargetFlags() == PPCII::MO_TPREL_PCREL_FLAG) 92 RefKind = MCSymbolRefExpr::VK_TPREL; 93 else if (MO.getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG) 94 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL; 95 else if (MO.getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG) 96 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_PCREL; 97 else if (MO.getTargetFlags() == PPCII::MO_GOT_TPREL_PCREL_FLAG) 98 RefKind = MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL; 99 else if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG) { 100 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!"); 101 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal()); 102 // For the local-exec TLS model, we may generate the offset from the TLS 103 // base as an immediate operand (instead of using a TOC entry). 104 // Set the relocation type in case the result is used for purposes other 105 // than a TOC reference. In TOC reference cases, this result is discarded. 106 if (Model == TLSModel::LocalExec) 107 RefKind = MCSymbolRefExpr::VK_PPC_AIX_TLSLE; 108 } 109 110 const MachineInstr *MI = MO.getParent(); 111 const MachineFunction *MF = MI->getMF(); 112 const Module *M = MF->getFunction().getParent(); 113 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>()); 114 115 unsigned MIOpcode = MI->getOpcode(); 116 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) && 117 "BL8_NOTOC is only valid when using PC Relative Calls."); 118 if (Subtarget->isUsingPCRelativeCalls()) { 119 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 || 120 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 || 121 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) { 122 RefKind = MCSymbolRefExpr::VK_PPC_NOTOC; 123 } 124 if (MO.getTargetFlags() == PPCII::MO_PCREL_OPT_FLAG) 125 RefKind = MCSymbolRefExpr::VK_PPC_PCREL_OPT; 126 } 127 128 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx); 129 // If -msecure-plt -fPIC, add 32768 to symbol. 130 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() && 131 M->getPICLevel() == PICLevel::BigPIC && 132 MO.getTargetFlags() == PPCII::MO_PLT) 133 Expr = 134 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx); 135 136 if (!MO.isJTI() && MO.getOffset()) 137 Expr = MCBinaryExpr::createAdd(Expr, 138 MCConstantExpr::create(MO.getOffset(), Ctx), 139 Ctx); 140 141 // Subtract off the PIC base if required. 142 if (MO.getTargetFlags() == PPCII::MO_PIC_FLAG || 143 MO.getTargetFlags() == PPCII::MO_PIC_HA_FLAG || 144 MO.getTargetFlags() == PPCII::MO_PIC_LO_FLAG) { 145 const MachineFunction *MF = MO.getParent()->getParent()->getParent(); 146 147 const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx); 148 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx); 149 } 150 151 // Add ha16() / lo16() markers if required. 152 switch (access) { 153 case PPCII::MO_LO: 154 case PPCII::MO_PIC_LO_FLAG: 155 Expr = PPCMCExpr::createLo(Expr, Ctx); 156 break; 157 case PPCII::MO_HA: 158 case PPCII::MO_PIC_HA_FLAG: 159 Expr = PPCMCExpr::createHa(Expr, Ctx); 160 break; 161 } 162 163 return MCOperand::createExpr(Expr); 164 } 165 166 void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, 167 AsmPrinter &AP) { 168 OutMI.setOpcode(MI->getOpcode()); 169 170 for (const MachineOperand &MO : MI->operands()) { 171 MCOperand MCOp; 172 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP)) 173 OutMI.addOperand(MCOp); 174 } 175 } 176 177 bool llvm::LowerPPCMachineOperandToMCOperand(const MachineOperand &MO, 178 MCOperand &OutMO, AsmPrinter &AP) { 179 switch (MO.getType()) { 180 default: 181 llvm_unreachable("unknown operand type"); 182 case MachineOperand::MO_Register: 183 assert(!MO.getSubReg() && "Subregs should be eliminated!"); 184 assert(MO.getReg() > PPC::NoRegister && 185 MO.getReg() < PPC::NUM_TARGET_REGS && 186 "Invalid register for this target!"); 187 // Ignore all implicit register operands. 188 if (MO.isImplicit()) 189 return false; 190 OutMO = MCOperand::createReg(MO.getReg()); 191 return true; 192 case MachineOperand::MO_Immediate: 193 OutMO = MCOperand::createImm(MO.getImm()); 194 return true; 195 case MachineOperand::MO_MachineBasicBlock: 196 OutMO = MCOperand::createExpr( 197 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext)); 198 return true; 199 case MachineOperand::MO_GlobalAddress: 200 case MachineOperand::MO_ExternalSymbol: 201 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP); 202 return true; 203 case MachineOperand::MO_JumpTableIndex: 204 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP); 205 return true; 206 case MachineOperand::MO_ConstantPoolIndex: 207 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP); 208 return true; 209 case MachineOperand::MO_BlockAddress: 210 OutMO = 211 GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP); 212 return true; 213 case MachineOperand::MO_MCSymbol: 214 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP); 215 return true; 216 case MachineOperand::MO_RegisterMask: 217 return false; 218 } 219 } 220