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:
DXILInstPrinter(const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)49 DXILInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
50 const MCRegisterInfo &MRI)
51 : MCInstPrinter(MAI, MII, MRI) {}
52
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & O)53 void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
54 const MCSubtargetInfo &STI, raw_ostream &O) override {}
55
getMnemonic(const MCInst * MI)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:
DXILMCCodeEmitter()65 DXILMCCodeEmitter() {}
66
encodeInstruction(const MCInst & Inst,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const67 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:
DXILAsmBackend(const MCSubtargetInfo & STI)75 DXILAsmBackend(const MCSubtargetInfo &STI)
76 : MCAsmBackend(llvm::endianness::little) {}
77 ~DXILAsmBackend() override = default;
78
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved,const MCSubtargetInfo * STI) const79 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>
createObjectTargetWriter() const85 createObjectTargetWriter() const override {
86 return createDXContainerTargetObjectWriter();
87 }
88
getNumFixupKinds() const89 unsigned getNumFixupKinds() const override { return 0; }
90
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const91 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:
DirectXMCAsmInfo(const Triple & TT,const MCTargetOptions & Options)99 explicit DirectXMCAsmInfo(const Triple &TT, const MCTargetOptions &Options)
100 : MCAsmInfo() {}
101 };
102
103 } // namespace
104
createDXILMCInstPrinter(const Triple & T,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)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
createDXILMCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)115 MCCodeEmitter *createDXILMCCodeEmitter(const MCInstrInfo &MCII,
116 MCContext &Ctx) {
117 return new DXILMCCodeEmitter();
118 }
119
createDXILMCAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions & Options)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 *
createDirectXMCSubtargetInfo(const Triple & TT,StringRef CPU,StringRef FS)128 createDirectXMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
129 return createDirectXMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
130 }
131
createDirectXMCRegisterInfo(const Triple & Triple)132 static MCRegisterInfo *createDirectXMCRegisterInfo(const Triple &Triple) {
133 return new MCRegisterInfo();
134 }
135
createDirectXMCInstrInfo()136 static MCInstrInfo *createDirectXMCInstrInfo() { return new MCInstrInfo(); }
137
LLVMInitializeDirectXTargetMC()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