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 &Inst, SmallVectorImpl<char> &CB, 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) 76 : MCAsmBackend(llvm::endianness::little) {} 77 ~DXILAsmBackend() override = default; 78 79 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, 80 const MCValue &Target, MutableArrayRef<char> Data, 81 uint64_t Value, bool IsResolved, 82 const MCSubtargetInfo *STI) const override {} 83 84 std::unique_ptr<MCObjectTargetWriter> 85 createObjectTargetWriter() const override { 86 return createDXContainerTargetObjectWriter(); 87 } 88 89 unsigned getNumFixupKinds() const override { return 0; } 90 91 bool writeNopData(raw_ostream &OS, uint64_t Count, 92 const MCSubtargetInfo *STI) const override { 93 return true; 94 } 95 }; 96 97 class DirectXMCAsmInfo : public MCAsmInfo { 98 public: 99 explicit DirectXMCAsmInfo(const Triple &TT, const MCTargetOptions &Options) 100 : MCAsmInfo() {} 101 }; 102 103 } // namespace 104 105 static MCInstPrinter *createDXILMCInstPrinter(const Triple &T, 106 unsigned SyntaxVariant, 107 const MCAsmInfo &MAI, 108 const MCInstrInfo &MII, 109 const MCRegisterInfo &MRI) { 110 if (SyntaxVariant == 0) 111 return new DXILInstPrinter(MAI, MII, MRI); 112 return nullptr; 113 } 114 115 MCCodeEmitter *createDXILMCCodeEmitter(const MCInstrInfo &MCII, 116 MCContext &Ctx) { 117 return new DXILMCCodeEmitter(); 118 } 119 120 MCAsmBackend *createDXILMCAsmBackend(const Target &T, 121 const MCSubtargetInfo &STI, 122 const MCRegisterInfo &MRI, 123 const MCTargetOptions &Options) { 124 return new DXILAsmBackend(STI); 125 } 126 127 static MCSubtargetInfo * 128 createDirectXMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { 129 return createDirectXMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS); 130 } 131 132 static MCRegisterInfo *createDirectXMCRegisterInfo(const Triple &Triple) { 133 return new MCRegisterInfo(); 134 } 135 136 static MCInstrInfo *createDirectXMCInstrInfo() { return new MCInstrInfo(); } 137 138 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTargetMC() { 139 Target &T = getTheDirectXTarget(); 140 RegisterMCAsmInfo<DirectXMCAsmInfo> X(T); 141 TargetRegistry::RegisterMCInstrInfo(T, createDirectXMCInstrInfo); 142 TargetRegistry::RegisterMCInstPrinter(T, createDXILMCInstPrinter); 143 TargetRegistry::RegisterMCRegInfo(T, createDirectXMCRegisterInfo); 144 TargetRegistry::RegisterMCSubtargetInfo(T, createDirectXMCSubtargetInfo); 145 TargetRegistry::RegisterMCCodeEmitter(T, createDXILMCCodeEmitter); 146 TargetRegistry::RegisterMCAsmBackend(T, createDXILMCAsmBackend); 147 } 148