1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===// 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 provides PowerPC specific target descriptions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PPCMCTargetDesc.h" 15 #include "InstPrinter/PPCInstPrinter.h" 16 #include "PPCMCAsmInfo.h" 17 #include "PPCTargetStreamer.h" 18 #include "llvm/MC/MCCodeGenInfo.h" 19 #include "llvm/MC/MCContext.h" 20 #include "llvm/MC/MCELFStreamer.h" 21 #include "llvm/MC/MCExpr.h" 22 #include "llvm/MC/MCInstrInfo.h" 23 #include "llvm/MC/MCRegisterInfo.h" 24 #include "llvm/MC/MCStreamer.h" 25 #include "llvm/MC/MCSubtargetInfo.h" 26 #include "llvm/MC/MCSymbolELF.h" 27 #include "llvm/MC/MachineLocation.h" 28 #include "llvm/Support/ELF.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/FormattedStream.h" 31 #include "llvm/Support/TargetRegistry.h" 32 33 using namespace llvm; 34 35 #define GET_INSTRINFO_MC_DESC 36 #include "PPCGenInstrInfo.inc" 37 38 #define GET_SUBTARGETINFO_MC_DESC 39 #include "PPCGenSubtargetInfo.inc" 40 41 #define GET_REGINFO_MC_DESC 42 #include "PPCGenRegisterInfo.inc" 43 44 // Pin the vtable to this file. 45 PPCTargetStreamer::~PPCTargetStreamer() {} 46 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {} 47 48 static MCInstrInfo *createPPCMCInstrInfo() { 49 MCInstrInfo *X = new MCInstrInfo(); 50 InitPPCMCInstrInfo(X); 51 return X; 52 } 53 54 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) { 55 bool isPPC64 = 56 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le); 57 unsigned Flavour = isPPC64 ? 0 : 1; 58 unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR; 59 60 MCRegisterInfo *X = new MCRegisterInfo(); 61 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour); 62 return X; 63 } 64 65 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT, 66 StringRef CPU, StringRef FS) { 67 return createPPCMCSubtargetInfoImpl(TT, CPU, FS); 68 } 69 70 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, 71 const Triple &TheTriple) { 72 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 || 73 TheTriple.getArch() == Triple::ppc64le); 74 75 MCAsmInfo *MAI; 76 if (TheTriple.isOSDarwin()) 77 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple); 78 else 79 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple); 80 81 // Initial state of the frame pointer is R1. 82 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1; 83 MCCFIInstruction Inst = 84 MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0); 85 MAI->addInitialFrameState(Inst); 86 87 return MAI; 88 } 89 90 static MCCodeGenInfo *createPPCMCCodeGenInfo(const Triple &TT, Reloc::Model RM, 91 CodeModel::Model CM, 92 CodeGenOpt::Level OL) { 93 MCCodeGenInfo *X = new MCCodeGenInfo(); 94 95 if (CM == CodeModel::Default) { 96 if (!TT.isOSDarwin() && 97 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le)) 98 CM = CodeModel::Medium; 99 } 100 X->initMCCodeGenInfo(RM, CM, OL); 101 return X; 102 } 103 104 namespace { 105 class PPCTargetAsmStreamer : public PPCTargetStreamer { 106 formatted_raw_ostream &OS; 107 108 public: 109 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS) 110 : PPCTargetStreamer(S), OS(OS) {} 111 void emitTCEntry(const MCSymbol &S) override { 112 OS << "\t.tc "; 113 OS << S.getName(); 114 OS << "[TC],"; 115 OS << S.getName(); 116 OS << '\n'; 117 } 118 void emitMachine(StringRef CPU) override { 119 OS << "\t.machine " << CPU << '\n'; 120 } 121 void emitAbiVersion(int AbiVersion) override { 122 OS << "\t.abiversion " << AbiVersion << '\n'; 123 } 124 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 125 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 126 127 OS << "\t.localentry\t"; 128 S->print(OS, MAI); 129 OS << ", "; 130 LocalOffset->print(OS, MAI); 131 OS << '\n'; 132 } 133 }; 134 135 class PPCTargetELFStreamer : public PPCTargetStreamer { 136 public: 137 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 138 MCELFStreamer &getStreamer() { 139 return static_cast<MCELFStreamer &>(Streamer); 140 } 141 void emitTCEntry(const MCSymbol &S) override { 142 // Creates a R_PPC64_TOC relocation 143 Streamer.EmitValueToAlignment(8); 144 Streamer.EmitSymbolValue(&S, 8); 145 } 146 void emitMachine(StringRef CPU) override { 147 // FIXME: Is there anything to do in here or does this directive only 148 // limit the parser? 149 } 150 void emitAbiVersion(int AbiVersion) override { 151 MCAssembler &MCA = getStreamer().getAssembler(); 152 unsigned Flags = MCA.getELFHeaderEFlags(); 153 Flags &= ~ELF::EF_PPC64_ABI; 154 Flags |= (AbiVersion & ELF::EF_PPC64_ABI); 155 MCA.setELFHeaderEFlags(Flags); 156 } 157 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 158 MCAssembler &MCA = getStreamer().getAssembler(); 159 160 int64_t Res; 161 if (!LocalOffset->evaluateAsAbsolute(Res, MCA)) 162 report_fatal_error(".localentry expression must be absolute."); 163 164 unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res); 165 if (Res != ELF::decodePPC64LocalEntryOffset(Encoded)) 166 report_fatal_error(".localentry expression cannot be encoded."); 167 168 unsigned Other = S->getOther(); 169 Other &= ~ELF::STO_PPC64_LOCAL_MASK; 170 Other |= Encoded; 171 S->setOther(Other); 172 173 // For GAS compatibility, unless we already saw a .abiversion directive, 174 // set e_flags to indicate ELFv2 ABI. 175 unsigned Flags = MCA.getELFHeaderEFlags(); 176 if ((Flags & ELF::EF_PPC64_ABI) == 0) 177 MCA.setELFHeaderEFlags(Flags | 2); 178 } 179 void emitAssignment(MCSymbol *S, const MCExpr *Value) override { 180 auto *Symbol = cast<MCSymbolELF>(S); 181 // When encoding an assignment to set symbol A to symbol B, also copy 182 // the st_other bits encoding the local entry point offset. 183 if (Value->getKind() != MCExpr::SymbolRef) 184 return; 185 const auto &RhsSym = cast<MCSymbolELF>( 186 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol()); 187 unsigned Other = Symbol->getOther(); 188 Other &= ~ELF::STO_PPC64_LOCAL_MASK; 189 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK; 190 Symbol->setOther(Other); 191 } 192 }; 193 194 class PPCTargetMachOStreamer : public PPCTargetStreamer { 195 public: 196 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 197 void emitTCEntry(const MCSymbol &S) override { 198 llvm_unreachable("Unknown pseudo-op: .tc"); 199 } 200 void emitMachine(StringRef CPU) override { 201 // FIXME: We should update the CPUType, CPUSubType in the Object file if 202 // the new values are different from the defaults. 203 } 204 void emitAbiVersion(int AbiVersion) override { 205 llvm_unreachable("Unknown pseudo-op: .abiversion"); 206 } 207 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 208 llvm_unreachable("Unknown pseudo-op: .localentry"); 209 } 210 }; 211 } 212 213 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, 214 formatted_raw_ostream &OS, 215 MCInstPrinter *InstPrint, 216 bool isVerboseAsm) { 217 return new PPCTargetAsmStreamer(S, OS); 218 } 219 220 static MCTargetStreamer * 221 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) { 222 const Triple &TT = STI.getTargetTriple(); 223 if (TT.isOSBinFormatELF()) 224 return new PPCTargetELFStreamer(S); 225 return new PPCTargetMachOStreamer(S); 226 } 227 228 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T, 229 unsigned SyntaxVariant, 230 const MCAsmInfo &MAI, 231 const MCInstrInfo &MII, 232 const MCRegisterInfo &MRI) { 233 return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin()); 234 } 235 236 extern "C" void LLVMInitializePowerPCTargetMC() { 237 for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) { 238 // Register the MC asm info. 239 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo); 240 241 // Register the MC codegen info. 242 TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo); 243 244 // Register the MC instruction info. 245 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo); 246 247 // Register the MC register info. 248 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo); 249 250 // Register the MC subtarget info. 251 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo); 252 253 // Register the MC Code Emitter 254 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter); 255 256 // Register the asm backend. 257 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend); 258 259 // Register the object target streamer. 260 TargetRegistry::RegisterObjectTargetStreamer(*T, 261 createObjectTargetStreamer); 262 263 // Register the asm target streamer. 264 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer); 265 266 // Register the MCInstPrinter. 267 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter); 268 } 269 } 270