xref: /freebsd/contrib/llvm-project/llvm/lib/Target/DirectX/MCTargetDesc/DirectXMCTargetDesc.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
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   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
97                             const MCRelaxableFragment *DF,
98                             const MCAsmLayout &Layout) const override {
99     return true;
100   }
101 };
102 
103 class DirectXMCAsmInfo : public MCAsmInfo {
104 public:
105   explicit DirectXMCAsmInfo(const Triple &TT, const MCTargetOptions &Options)
106       : MCAsmInfo() {}
107 };
108 
109 } // namespace
110 
111 static MCInstPrinter *createDXILMCInstPrinter(const Triple &T,
112                                               unsigned SyntaxVariant,
113                                               const MCAsmInfo &MAI,
114                                               const MCInstrInfo &MII,
115                                               const MCRegisterInfo &MRI) {
116   if (SyntaxVariant == 0)
117     return new DXILInstPrinter(MAI, MII, MRI);
118   return nullptr;
119 }
120 
121 MCCodeEmitter *createDXILMCCodeEmitter(const MCInstrInfo &MCII,
122                                        MCContext &Ctx) {
123   return new DXILMCCodeEmitter();
124 }
125 
126 MCAsmBackend *createDXILMCAsmBackend(const Target &T,
127                                      const MCSubtargetInfo &STI,
128                                      const MCRegisterInfo &MRI,
129                                      const MCTargetOptions &Options) {
130   return new DXILAsmBackend(STI);
131 }
132 
133 static MCSubtargetInfo *
134 createDirectXMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
135   return createDirectXMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
136 }
137 
138 static MCRegisterInfo *createDirectXMCRegisterInfo(const Triple &Triple) {
139   return new MCRegisterInfo();
140 }
141 
142 static MCInstrInfo *createDirectXMCInstrInfo() { return new MCInstrInfo(); }
143 
144 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTargetMC() {
145   Target &T = getTheDirectXTarget();
146   RegisterMCAsmInfo<DirectXMCAsmInfo> X(T);
147   TargetRegistry::RegisterMCInstrInfo(T, createDirectXMCInstrInfo);
148   TargetRegistry::RegisterMCInstPrinter(T, createDXILMCInstPrinter);
149   TargetRegistry::RegisterMCRegInfo(T, createDirectXMCRegisterInfo);
150   TargetRegistry::RegisterMCSubtargetInfo(T, createDirectXMCSubtargetInfo);
151   TargetRegistry::RegisterMCCodeEmitter(T, createDXILMCCodeEmitter);
152   TargetRegistry::RegisterMCAsmBackend(T, createDXILMCAsmBackend);
153 }
154