1 //===- DirectXMCTargetDesc.cpp - DirectX Target Implementation --*- C++ -*-===// 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 /// \file 10 /// This file contains DirectX target initializer. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "DirectXMCTargetDesc.h" 15 #include "DirectXContainerObjectWriter.h" 16 #include "TargetInfo/DirectXTargetInfo.h" 17 #include "llvm/MC/LaneBitmask.h" 18 #include "llvm/MC/MCAsmBackend.h" 19 #include "llvm/MC/MCAsmInfo.h" 20 #include "llvm/MC/MCCodeEmitter.h" 21 #include "llvm/MC/MCDXContainerWriter.h" 22 #include "llvm/MC/MCInstPrinter.h" 23 #include "llvm/MC/MCInstrInfo.h" 24 #include "llvm/MC/MCRegisterInfo.h" 25 #include "llvm/MC/MCSchedule.h" 26 #include "llvm/MC/MCSubtargetInfo.h" 27 #include "llvm/MC/TargetRegistry.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/TargetParser/Triple.h" 30 #include <memory> 31 32 using namespace llvm; 33 34 #define GET_INSTRINFO_MC_DESC 35 #define GET_INSTRINFO_MC_HELPERS 36 #include "DirectXGenInstrInfo.inc" 37 38 #define GET_SUBTARGETINFO_MC_DESC 39 #include "DirectXGenSubtargetInfo.inc" 40 41 #define GET_REGINFO_MC_DESC 42 #include "DirectXGenRegisterInfo.inc" 43 44 namespace { 45 46 // DXILInstPrinter is a null stub because DXIL instructions aren't printed. 47 class DXILInstPrinter : public MCInstPrinter { 48 public: 49 DXILInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, 50 const MCRegisterInfo &MRI) 51 : MCInstPrinter(MAI, MII, MRI) {} 52 53 void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, 54 const MCSubtargetInfo &STI, raw_ostream &O) override {} 55 56 std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override { 57 return std::make_pair<const char *, uint64_t>("", 0ull); 58 } 59 60 private: 61 }; 62 63 class DXILMCCodeEmitter : public MCCodeEmitter { 64 public: 65 DXILMCCodeEmitter() {} 66 67 void encodeInstruction(const MCInst &MI, raw_ostream &OS, 68 SmallVectorImpl<MCFixup> &Fixups, 69 const MCSubtargetInfo &STI) const override {} 70 }; 71 72 class DXILAsmBackend : public MCAsmBackend { 73 74 public: 75 DXILAsmBackend(const MCSubtargetInfo &STI) : MCAsmBackend(support::little) {} 76 ~DXILAsmBackend() override = default; 77 78 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, 79 const MCValue &Target, MutableArrayRef<char> Data, 80 uint64_t Value, bool IsResolved, 81 const MCSubtargetInfo *STI) const override {} 82 83 std::unique_ptr<MCObjectTargetWriter> 84 createObjectTargetWriter() const override { 85 return createDXContainerTargetObjectWriter(); 86 } 87 88 unsigned getNumFixupKinds() const override { return 0; } 89 90 bool writeNopData(raw_ostream &OS, uint64_t Count, 91 const MCSubtargetInfo *STI) const override { 92 return true; 93 } 94 95 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 96 const MCRelaxableFragment *DF, 97 const MCAsmLayout &Layout) const override { 98 return true; 99 } 100 }; 101 102 class DirectXMCAsmInfo : public MCAsmInfo { 103 public: 104 explicit DirectXMCAsmInfo(const Triple &TT, const MCTargetOptions &Options) 105 : MCAsmInfo() {} 106 }; 107 108 } // namespace 109 110 static MCInstPrinter *createDXILMCInstPrinter(const Triple &T, 111 unsigned SyntaxVariant, 112 const MCAsmInfo &MAI, 113 const MCInstrInfo &MII, 114 const MCRegisterInfo &MRI) { 115 if (SyntaxVariant == 0) 116 return new DXILInstPrinter(MAI, MII, MRI); 117 return nullptr; 118 } 119 120 MCCodeEmitter *createDXILMCCodeEmitter(const MCInstrInfo &MCII, 121 MCContext &Ctx) { 122 return new DXILMCCodeEmitter(); 123 } 124 125 MCAsmBackend *createDXILMCAsmBackend(const Target &T, 126 const MCSubtargetInfo &STI, 127 const MCRegisterInfo &MRI, 128 const MCTargetOptions &Options) { 129 return new DXILAsmBackend(STI); 130 } 131 132 static MCSubtargetInfo * 133 createDirectXMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { 134 return createDirectXMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS); 135 } 136 137 static MCRegisterInfo *createDirectXMCRegisterInfo(const Triple &Triple) { 138 return new MCRegisterInfo(); 139 } 140 141 static MCInstrInfo *createDirectXMCInstrInfo() { return new MCInstrInfo(); } 142 143 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTargetMC() { 144 Target &T = getTheDirectXTarget(); 145 RegisterMCAsmInfo<DirectXMCAsmInfo> X(T); 146 TargetRegistry::RegisterMCInstrInfo(T, createDirectXMCInstrInfo); 147 TargetRegistry::RegisterMCInstPrinter(T, createDXILMCInstPrinter); 148 TargetRegistry::RegisterMCRegInfo(T, createDirectXMCRegisterInfo); 149 TargetRegistry::RegisterMCSubtargetInfo(T, createDirectXMCSubtargetInfo); 150 TargetRegistry::RegisterMCCodeEmitter(T, createDXILMCCodeEmitter); 151 TargetRegistry::RegisterMCAsmBackend(T, createDXILMCAsmBackend); 152 } 153