TargetRegistry.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MC/TargetRegistry.h - Target Registration ----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file exposes the TargetRegistry interface, which tools can use to access
  15. // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
  16. // which have been registered.
  17. //
  18. // Target specific class implementations should register themselves using the
  19. // appropriate TargetRegistry interfaces.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_MC_TARGETREGISTRY_H
  23. #define LLVM_MC_TARGETREGISTRY_H
  24. #include "llvm-c/DisassemblerTypes.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/ADT/Triple.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include "llvm/MC/MCObjectFileInfo.h"
  29. #include "llvm/Support/CodeGen.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/FormattedStream.h"
  32. #include <cassert>
  33. #include <cstddef>
  34. #include <iterator>
  35. #include <memory>
  36. #include <optional>
  37. #include <string>
  38. namespace llvm {
  39. class AsmPrinter;
  40. class MCAsmBackend;
  41. class MCAsmInfo;
  42. class MCAsmParser;
  43. class MCCodeEmitter;
  44. class MCContext;
  45. class MCDisassembler;
  46. class MCInstPrinter;
  47. class MCInstrAnalysis;
  48. class MCInstrInfo;
  49. class MCObjectWriter;
  50. class MCRegisterInfo;
  51. class MCRelocationInfo;
  52. class MCStreamer;
  53. class MCSubtargetInfo;
  54. class MCSymbolizer;
  55. class MCTargetAsmParser;
  56. class MCTargetOptions;
  57. class MCTargetStreamer;
  58. class raw_ostream;
  59. class TargetMachine;
  60. class TargetOptions;
  61. namespace mca {
  62. class CustomBehaviour;
  63. class InstrPostProcess;
  64. class InstrumentManager;
  65. struct SourceMgr;
  66. } // namespace mca
  67. MCStreamer *createNullStreamer(MCContext &Ctx);
  68. // Takes ownership of \p TAB and \p CE.
  69. /// Create a machine code streamer which will print out assembly for the native
  70. /// target, suitable for compiling with a native assembler.
  71. ///
  72. /// \param InstPrint - If given, the instruction printer to use. If not given
  73. /// the MCInst representation will be printed. This method takes ownership of
  74. /// InstPrint.
  75. ///
  76. /// \param CE - If given, a code emitter to use to show the instruction
  77. /// encoding inline with the assembly. This method takes ownership of \p CE.
  78. ///
  79. /// \param TAB - If given, a target asm backend to use to show the fixup
  80. /// information in conjunction with encoding information. This method takes
  81. /// ownership of \p TAB.
  82. ///
  83. /// \param ShowInst - Whether to show the MCInst representation inline with
  84. /// the assembly.
  85. MCStreamer *
  86. createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
  87. bool isVerboseAsm, bool useDwarfDirectory,
  88. MCInstPrinter *InstPrint, std::unique_ptr<MCCodeEmitter> &&CE,
  89. std::unique_ptr<MCAsmBackend> &&TAB, bool ShowInst);
  90. MCStreamer *createELFStreamer(MCContext &Ctx,
  91. std::unique_ptr<MCAsmBackend> &&TAB,
  92. std::unique_ptr<MCObjectWriter> &&OW,
  93. std::unique_ptr<MCCodeEmitter> &&CE,
  94. bool RelaxAll);
  95. MCStreamer *createMachOStreamer(MCContext &Ctx,
  96. std::unique_ptr<MCAsmBackend> &&TAB,
  97. std::unique_ptr<MCObjectWriter> &&OW,
  98. std::unique_ptr<MCCodeEmitter> &&CE,
  99. bool RelaxAll, bool DWARFMustBeAtTheEnd,
  100. bool LabelSections = false);
  101. MCStreamer *createWasmStreamer(MCContext &Ctx,
  102. std::unique_ptr<MCAsmBackend> &&TAB,
  103. std::unique_ptr<MCObjectWriter> &&OW,
  104. std::unique_ptr<MCCodeEmitter> &&CE,
  105. bool RelaxAll);
  106. MCStreamer *createXCOFFStreamer(MCContext &Ctx,
  107. std::unique_ptr<MCAsmBackend> &&TAB,
  108. std::unique_ptr<MCObjectWriter> &&OW,
  109. std::unique_ptr<MCCodeEmitter> &&CE,
  110. bool RelaxAll);
  111. MCStreamer *createSPIRVStreamer(MCContext &Ctx,
  112. std::unique_ptr<MCAsmBackend> &&TAB,
  113. std::unique_ptr<MCObjectWriter> &&OW,
  114. std::unique_ptr<MCCodeEmitter> &&CE,
  115. bool RelaxAll);
  116. MCStreamer *createDXContainerStreamer(MCContext &Ctx,
  117. std::unique_ptr<MCAsmBackend> &&TAB,
  118. std::unique_ptr<MCObjectWriter> &&OW,
  119. std::unique_ptr<MCCodeEmitter> &&CE,
  120. bool RelaxAll);
  121. MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
  122. MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
  123. LLVMSymbolLookupCallback SymbolLookUp,
  124. void *DisInfo, MCContext *Ctx,
  125. std::unique_ptr<MCRelocationInfo> &&RelInfo);
  126. mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
  127. const mca::SourceMgr &SrcMgr,
  128. const MCInstrInfo &MCII);
  129. mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
  130. const MCInstrInfo &MCII);
  131. mca::InstrumentManager *createInstrumentManager(const MCSubtargetInfo &STI,
  132. const MCInstrInfo &MCII);
  133. /// Target - Wrapper for Target specific information.
  134. ///
  135. /// For registration purposes, this is a POD type so that targets can be
  136. /// registered without the use of static constructors.
  137. ///
  138. /// Targets should implement a single global instance of this class (which
  139. /// will be zero initialized), and pass that instance to the TargetRegistry as
  140. /// part of their initialization.
  141. class Target {
  142. public:
  143. friend struct TargetRegistry;
  144. using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
  145. using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
  146. const Triple &TT,
  147. const MCTargetOptions &Options);
  148. using MCObjectFileInfoCtorFnTy = MCObjectFileInfo *(*)(MCContext &Ctx,
  149. bool PIC,
  150. bool LargeCodeModel);
  151. using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
  152. using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
  153. using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
  154. using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
  155. StringRef CPU,
  156. StringRef Features);
  157. using TargetMachineCtorTy = TargetMachine
  158. *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
  159. const TargetOptions &Options, std::optional<Reloc::Model> RM,
  160. std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
  161. // If it weren't for layering issues (this header is in llvm/Support, but
  162. // depends on MC?) this should take the Streamer by value rather than rvalue
  163. // reference.
  164. using AsmPrinterCtorTy = AsmPrinter *(*)(
  165. TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
  166. using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
  167. const MCSubtargetInfo &STI,
  168. const MCRegisterInfo &MRI,
  169. const MCTargetOptions &Options);
  170. using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
  171. const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
  172. const MCTargetOptions &Options);
  173. using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
  174. const MCSubtargetInfo &STI,
  175. MCContext &Ctx);
  176. using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
  177. unsigned SyntaxVariant,
  178. const MCAsmInfo &MAI,
  179. const MCInstrInfo &MII,
  180. const MCRegisterInfo &MRI);
  181. using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
  182. MCContext &Ctx);
  183. using ELFStreamerCtorTy =
  184. MCStreamer *(*)(const Triple &T, MCContext &Ctx,
  185. std::unique_ptr<MCAsmBackend> &&TAB,
  186. std::unique_ptr<MCObjectWriter> &&OW,
  187. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
  188. using MachOStreamerCtorTy =
  189. MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
  190. std::unique_ptr<MCObjectWriter> &&OW,
  191. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
  192. bool DWARFMustBeAtTheEnd);
  193. using COFFStreamerCtorTy =
  194. MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
  195. std::unique_ptr<MCObjectWriter> &&OW,
  196. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
  197. bool IncrementalLinkerCompatible);
  198. using WasmStreamerCtorTy =
  199. MCStreamer *(*)(const Triple &T, MCContext &Ctx,
  200. std::unique_ptr<MCAsmBackend> &&TAB,
  201. std::unique_ptr<MCObjectWriter> &&OW,
  202. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
  203. using XCOFFStreamerCtorTy =
  204. MCStreamer *(*)(const Triple &T, MCContext &Ctx,
  205. std::unique_ptr<MCAsmBackend> &&TAB,
  206. std::unique_ptr<MCObjectWriter> &&OW,
  207. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
  208. using SPIRVStreamerCtorTy =
  209. MCStreamer *(*)(const Triple &T, MCContext &Ctx,
  210. std::unique_ptr<MCAsmBackend> &&TAB,
  211. std::unique_ptr<MCObjectWriter> &&OW,
  212. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
  213. using DXContainerStreamerCtorTy =
  214. MCStreamer *(*)(const Triple &T, MCContext &Ctx,
  215. std::unique_ptr<MCAsmBackend> &&TAB,
  216. std::unique_ptr<MCObjectWriter> &&OW,
  217. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
  218. using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
  219. using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
  220. MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
  221. bool IsVerboseAsm);
  222. using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
  223. MCStreamer &S, const MCSubtargetInfo &STI);
  224. using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
  225. MCContext &Ctx);
  226. using MCSymbolizerCtorTy = MCSymbolizer *(*)(
  227. const Triple &TT, LLVMOpInfoCallback GetOpInfo,
  228. LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
  229. std::unique_ptr<MCRelocationInfo> &&RelInfo);
  230. using CustomBehaviourCtorTy =
  231. mca::CustomBehaviour *(*)(const MCSubtargetInfo &STI,
  232. const mca::SourceMgr &SrcMgr,
  233. const MCInstrInfo &MCII);
  234. using InstrPostProcessCtorTy =
  235. mca::InstrPostProcess *(*)(const MCSubtargetInfo &STI,
  236. const MCInstrInfo &MCII);
  237. using InstrumentManagerCtorTy =
  238. mca::InstrumentManager *(*)(const MCSubtargetInfo &STI,
  239. const MCInstrInfo &MCII);
  240. private:
  241. /// Next - The next registered target in the linked list, maintained by the
  242. /// TargetRegistry.
  243. Target *Next;
  244. /// The target function for checking if an architecture is supported.
  245. ArchMatchFnTy ArchMatchFn;
  246. /// Name - The target name.
  247. const char *Name;
  248. /// ShortDesc - A short description of the target.
  249. const char *ShortDesc;
  250. /// BackendName - The name of the backend implementation. This must match the
  251. /// name of the 'def X : Target ...' in TableGen.
  252. const char *BackendName;
  253. /// HasJIT - Whether this target supports the JIT.
  254. bool HasJIT;
  255. /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
  256. /// registered.
  257. MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
  258. /// Constructor function for this target's MCObjectFileInfo, if registered.
  259. MCObjectFileInfoCtorFnTy MCObjectFileInfoCtorFn;
  260. /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
  261. /// if registered.
  262. MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
  263. /// MCInstrAnalysisCtorFn - Constructor function for this target's
  264. /// MCInstrAnalysis, if registered.
  265. MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
  266. /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
  267. /// if registered.
  268. MCRegInfoCtorFnTy MCRegInfoCtorFn;
  269. /// MCSubtargetInfoCtorFn - Constructor function for this target's
  270. /// MCSubtargetInfo, if registered.
  271. MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
  272. /// TargetMachineCtorFn - Construction function for this target's
  273. /// TargetMachine, if registered.
  274. TargetMachineCtorTy TargetMachineCtorFn;
  275. /// MCAsmBackendCtorFn - Construction function for this target's
  276. /// MCAsmBackend, if registered.
  277. MCAsmBackendCtorTy MCAsmBackendCtorFn;
  278. /// MCAsmParserCtorFn - Construction function for this target's
  279. /// MCTargetAsmParser, if registered.
  280. MCAsmParserCtorTy MCAsmParserCtorFn;
  281. /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
  282. /// if registered.
  283. AsmPrinterCtorTy AsmPrinterCtorFn;
  284. /// MCDisassemblerCtorFn - Construction function for this target's
  285. /// MCDisassembler, if registered.
  286. MCDisassemblerCtorTy MCDisassemblerCtorFn;
  287. /// MCInstPrinterCtorFn - Construction function for this target's
  288. /// MCInstPrinter, if registered.
  289. MCInstPrinterCtorTy MCInstPrinterCtorFn;
  290. /// MCCodeEmitterCtorFn - Construction function for this target's
  291. /// CodeEmitter, if registered.
  292. MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
  293. // Construction functions for the various object formats, if registered.
  294. COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
  295. MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
  296. ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
  297. WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
  298. XCOFFStreamerCtorTy XCOFFStreamerCtorFn = nullptr;
  299. SPIRVStreamerCtorTy SPIRVStreamerCtorFn = nullptr;
  300. DXContainerStreamerCtorTy DXContainerStreamerCtorFn = nullptr;
  301. /// Construction function for this target's null TargetStreamer, if
  302. /// registered (default = nullptr).
  303. NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
  304. /// Construction function for this target's asm TargetStreamer, if
  305. /// registered (default = nullptr).
  306. AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
  307. /// Construction function for this target's obj TargetStreamer, if
  308. /// registered (default = nullptr).
  309. ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
  310. /// MCRelocationInfoCtorFn - Construction function for this target's
  311. /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
  312. MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
  313. /// MCSymbolizerCtorFn - Construction function for this target's
  314. /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
  315. MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
  316. /// CustomBehaviourCtorFn - Construction function for this target's
  317. /// CustomBehaviour, if registered (default = nullptr).
  318. CustomBehaviourCtorTy CustomBehaviourCtorFn = nullptr;
  319. /// InstrPostProcessCtorFn - Construction function for this target's
  320. /// InstrPostProcess, if registered (default = nullptr).
  321. InstrPostProcessCtorTy InstrPostProcessCtorFn = nullptr;
  322. /// InstrumentManagerCtorFn - Construction function for this target's
  323. /// InstrumentManager, if registered (default = nullptr).
  324. InstrumentManagerCtorTy InstrumentManagerCtorFn = nullptr;
  325. public:
  326. Target() = default;
  327. /// @name Target Information
  328. /// @{
  329. // getNext - Return the next registered target.
  330. const Target *getNext() const { return Next; }
  331. /// getName - Get the target name.
  332. const char *getName() const { return Name; }
  333. /// getShortDescription - Get a short description of the target.
  334. const char *getShortDescription() const { return ShortDesc; }
  335. /// getBackendName - Get the backend name.
  336. const char *getBackendName() const { return BackendName; }
  337. /// @}
  338. /// @name Feature Predicates
  339. /// @{
  340. /// hasJIT - Check if this targets supports the just-in-time compilation.
  341. bool hasJIT() const { return HasJIT; }
  342. /// hasTargetMachine - Check if this target supports code generation.
  343. bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
  344. /// hasMCAsmBackend - Check if this target supports .o generation.
  345. bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
  346. /// hasMCAsmParser - Check if this target supports assembly parsing.
  347. bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
  348. /// @}
  349. /// @name Feature Constructors
  350. /// @{
  351. /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
  352. /// target triple.
  353. ///
  354. /// \param TheTriple This argument is used to determine the target machine
  355. /// feature set; it should always be provided. Generally this should be
  356. /// either the target triple from the module, or the target triple of the
  357. /// host if that does not exist.
  358. MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple,
  359. const MCTargetOptions &Options) const {
  360. if (!MCAsmInfoCtorFn)
  361. return nullptr;
  362. return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options);
  363. }
  364. /// Create a MCObjectFileInfo implementation for the specified target
  365. /// triple.
  366. ///
  367. MCObjectFileInfo *createMCObjectFileInfo(MCContext &Ctx, bool PIC,
  368. bool LargeCodeModel = false) const {
  369. if (!MCObjectFileInfoCtorFn) {
  370. MCObjectFileInfo *MOFI = new MCObjectFileInfo();
  371. MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
  372. return MOFI;
  373. }
  374. return MCObjectFileInfoCtorFn(Ctx, PIC, LargeCodeModel);
  375. }
  376. /// createMCInstrInfo - Create a MCInstrInfo implementation.
  377. ///
  378. MCInstrInfo *createMCInstrInfo() const {
  379. if (!MCInstrInfoCtorFn)
  380. return nullptr;
  381. return MCInstrInfoCtorFn();
  382. }
  383. /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
  384. ///
  385. MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
  386. if (!MCInstrAnalysisCtorFn)
  387. return nullptr;
  388. return MCInstrAnalysisCtorFn(Info);
  389. }
  390. /// createMCRegInfo - Create a MCRegisterInfo implementation.
  391. ///
  392. MCRegisterInfo *createMCRegInfo(StringRef TT) const {
  393. if (!MCRegInfoCtorFn)
  394. return nullptr;
  395. return MCRegInfoCtorFn(Triple(TT));
  396. }
  397. /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
  398. ///
  399. /// \param TheTriple This argument is used to determine the target machine
  400. /// feature set; it should always be provided. Generally this should be
  401. /// either the target triple from the module, or the target triple of the
  402. /// host if that does not exist.
  403. /// \param CPU This specifies the name of the target CPU.
  404. /// \param Features This specifies the string representation of the
  405. /// additional target features.
  406. MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
  407. StringRef Features) const {
  408. if (!MCSubtargetInfoCtorFn)
  409. return nullptr;
  410. return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
  411. }
  412. /// createTargetMachine - Create a target specific machine implementation
  413. /// for the specified \p Triple.
  414. ///
  415. /// \param TT This argument is used to determine the target machine
  416. /// feature set; it should always be provided. Generally this should be
  417. /// either the target triple from the module, or the target triple of the
  418. /// host if that does not exist.
  419. TargetMachine *createTargetMachine(
  420. StringRef TT, StringRef CPU, StringRef Features,
  421. const TargetOptions &Options, std::optional<Reloc::Model> RM,
  422. std::optional<CodeModel::Model> CM = std::nullopt,
  423. CodeGenOpt::Level OL = CodeGenOpt::Default, bool JIT = false) const {
  424. if (!TargetMachineCtorFn)
  425. return nullptr;
  426. return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
  427. CM, OL, JIT);
  428. }
  429. /// createMCAsmBackend - Create a target specific assembly parser.
  430. MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI,
  431. const MCRegisterInfo &MRI,
  432. const MCTargetOptions &Options) const {
  433. if (!MCAsmBackendCtorFn)
  434. return nullptr;
  435. return MCAsmBackendCtorFn(*this, STI, MRI, Options);
  436. }
  437. /// createMCAsmParser - Create a target specific assembly parser.
  438. ///
  439. /// \param Parser The target independent parser implementation to use for
  440. /// parsing and lexing.
  441. MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
  442. MCAsmParser &Parser,
  443. const MCInstrInfo &MII,
  444. const MCTargetOptions &Options) const {
  445. if (!MCAsmParserCtorFn)
  446. return nullptr;
  447. return MCAsmParserCtorFn(STI, Parser, MII, Options);
  448. }
  449. /// createAsmPrinter - Create a target specific assembly printer pass. This
  450. /// takes ownership of the MCStreamer object.
  451. AsmPrinter *createAsmPrinter(TargetMachine &TM,
  452. std::unique_ptr<MCStreamer> &&Streamer) const {
  453. if (!AsmPrinterCtorFn)
  454. return nullptr;
  455. return AsmPrinterCtorFn(TM, std::move(Streamer));
  456. }
  457. MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
  458. MCContext &Ctx) const {
  459. if (!MCDisassemblerCtorFn)
  460. return nullptr;
  461. return MCDisassemblerCtorFn(*this, STI, Ctx);
  462. }
  463. MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
  464. const MCAsmInfo &MAI,
  465. const MCInstrInfo &MII,
  466. const MCRegisterInfo &MRI) const {
  467. if (!MCInstPrinterCtorFn)
  468. return nullptr;
  469. return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
  470. }
  471. /// createMCCodeEmitter - Create a target specific code emitter.
  472. MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
  473. MCContext &Ctx) const {
  474. if (!MCCodeEmitterCtorFn)
  475. return nullptr;
  476. return MCCodeEmitterCtorFn(II, Ctx);
  477. }
  478. /// Create a target specific MCStreamer.
  479. ///
  480. /// \param T The target triple.
  481. /// \param Ctx The target context.
  482. /// \param TAB The target assembler backend object. Takes ownership.
  483. /// \param OW The stream object.
  484. /// \param Emitter The target independent assembler object.Takes ownership.
  485. /// \param RelaxAll Relax all fixups?
  486. MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
  487. std::unique_ptr<MCAsmBackend> &&TAB,
  488. std::unique_ptr<MCObjectWriter> &&OW,
  489. std::unique_ptr<MCCodeEmitter> &&Emitter,
  490. const MCSubtargetInfo &STI, bool RelaxAll,
  491. bool IncrementalLinkerCompatible,
  492. bool DWARFMustBeAtTheEnd) const {
  493. MCStreamer *S = nullptr;
  494. switch (T.getObjectFormat()) {
  495. case Triple::UnknownObjectFormat:
  496. llvm_unreachable("Unknown object format");
  497. case Triple::COFF:
  498. assert(T.isOSWindows() && "only Windows COFF is supported");
  499. S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
  500. std::move(Emitter), RelaxAll,
  501. IncrementalLinkerCompatible);
  502. break;
  503. case Triple::MachO:
  504. if (MachOStreamerCtorFn)
  505. S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
  506. std::move(Emitter), RelaxAll,
  507. DWARFMustBeAtTheEnd);
  508. else
  509. S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
  510. std::move(Emitter), RelaxAll,
  511. DWARFMustBeAtTheEnd);
  512. break;
  513. case Triple::ELF:
  514. if (ELFStreamerCtorFn)
  515. S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
  516. std::move(Emitter), RelaxAll);
  517. else
  518. S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
  519. std::move(Emitter), RelaxAll);
  520. break;
  521. case Triple::Wasm:
  522. if (WasmStreamerCtorFn)
  523. S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
  524. std::move(Emitter), RelaxAll);
  525. else
  526. S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
  527. std::move(Emitter), RelaxAll);
  528. break;
  529. case Triple::GOFF:
  530. report_fatal_error("GOFF MCObjectStreamer not implemented yet");
  531. case Triple::XCOFF:
  532. if (XCOFFStreamerCtorFn)
  533. S = XCOFFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
  534. std::move(Emitter), RelaxAll);
  535. else
  536. S = createXCOFFStreamer(Ctx, std::move(TAB), std::move(OW),
  537. std::move(Emitter), RelaxAll);
  538. break;
  539. case Triple::SPIRV:
  540. if (SPIRVStreamerCtorFn)
  541. S = SPIRVStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
  542. std::move(Emitter), RelaxAll);
  543. else
  544. S = createSPIRVStreamer(Ctx, std::move(TAB), std::move(OW),
  545. std::move(Emitter), RelaxAll);
  546. break;
  547. case Triple::DXContainer:
  548. if (DXContainerStreamerCtorFn)
  549. S = DXContainerStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
  550. std::move(Emitter), RelaxAll);
  551. else
  552. S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
  553. std::move(Emitter), RelaxAll);
  554. break;
  555. }
  556. if (ObjectTargetStreamerCtorFn)
  557. ObjectTargetStreamerCtorFn(*S, STI);
  558. return S;
  559. }
  560. MCStreamer *createAsmStreamer(MCContext &Ctx,
  561. std::unique_ptr<formatted_raw_ostream> OS,
  562. bool IsVerboseAsm, bool UseDwarfDirectory,
  563. MCInstPrinter *InstPrint,
  564. std::unique_ptr<MCCodeEmitter> &&CE,
  565. std::unique_ptr<MCAsmBackend> &&TAB,
  566. bool ShowInst) const {
  567. formatted_raw_ostream &OSRef = *OS;
  568. MCStreamer *S = llvm::createAsmStreamer(
  569. Ctx, std::move(OS), IsVerboseAsm, UseDwarfDirectory, InstPrint,
  570. std::move(CE), std::move(TAB), ShowInst);
  571. createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
  572. return S;
  573. }
  574. MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
  575. formatted_raw_ostream &OS,
  576. MCInstPrinter *InstPrint,
  577. bool IsVerboseAsm) const {
  578. if (AsmTargetStreamerCtorFn)
  579. return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
  580. return nullptr;
  581. }
  582. MCStreamer *createNullStreamer(MCContext &Ctx) const {
  583. MCStreamer *S = llvm::createNullStreamer(Ctx);
  584. createNullTargetStreamer(*S);
  585. return S;
  586. }
  587. MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
  588. if (NullTargetStreamerCtorFn)
  589. return NullTargetStreamerCtorFn(S);
  590. return nullptr;
  591. }
  592. /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
  593. ///
  594. /// \param TT The target triple.
  595. /// \param Ctx The target context.
  596. MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
  597. MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
  598. ? MCRelocationInfoCtorFn
  599. : llvm::createMCRelocationInfo;
  600. return Fn(Triple(TT), Ctx);
  601. }
  602. /// createMCSymbolizer - Create a target specific MCSymbolizer.
  603. ///
  604. /// \param TT The target triple.
  605. /// \param GetOpInfo The function to get the symbolic information for
  606. /// operands.
  607. /// \param SymbolLookUp The function to lookup a symbol name.
  608. /// \param DisInfo The pointer to the block of symbolic information for above
  609. /// call
  610. /// back.
  611. /// \param Ctx The target context.
  612. /// \param RelInfo The relocation information for this target. Takes
  613. /// ownership.
  614. MCSymbolizer *
  615. createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
  616. LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
  617. MCContext *Ctx,
  618. std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
  619. MCSymbolizerCtorTy Fn =
  620. MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
  621. return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
  622. std::move(RelInfo));
  623. }
  624. /// createCustomBehaviour - Create a target specific CustomBehaviour.
  625. /// This class is used by llvm-mca and requires backend functionality.
  626. mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
  627. const mca::SourceMgr &SrcMgr,
  628. const MCInstrInfo &MCII) const {
  629. if (CustomBehaviourCtorFn)
  630. return CustomBehaviourCtorFn(STI, SrcMgr, MCII);
  631. return nullptr;
  632. }
  633. /// createInstrPostProcess - Create a target specific InstrPostProcess.
  634. /// This class is used by llvm-mca and requires backend functionality.
  635. mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
  636. const MCInstrInfo &MCII) const {
  637. if (InstrPostProcessCtorFn)
  638. return InstrPostProcessCtorFn(STI, MCII);
  639. return nullptr;
  640. }
  641. /// createInstrumentManager - Create a target specific
  642. /// InstrumentManager. This class is used by llvm-mca and requires
  643. /// backend functionality.
  644. mca::InstrumentManager *
  645. createInstrumentManager(const MCSubtargetInfo &STI,
  646. const MCInstrInfo &MCII) const {
  647. if (InstrumentManagerCtorFn)
  648. return InstrumentManagerCtorFn(STI, MCII);
  649. return nullptr;
  650. }
  651. /// @}
  652. };
  653. /// TargetRegistry - Generic interface to target specific features.
  654. struct TargetRegistry {
  655. // FIXME: Make this a namespace, probably just move all the Register*
  656. // functions into Target (currently they all just set members on the Target
  657. // anyway, and Target friends this class so those functions can...
  658. // function).
  659. TargetRegistry() = delete;
  660. class iterator {
  661. friend struct TargetRegistry;
  662. const Target *Current = nullptr;
  663. explicit iterator(Target *T) : Current(T) {}
  664. public:
  665. using iterator_category = std::forward_iterator_tag;
  666. using value_type = Target;
  667. using difference_type = std::ptrdiff_t;
  668. using pointer = value_type *;
  669. using reference = value_type &;
  670. iterator() = default;
  671. bool operator==(const iterator &x) const { return Current == x.Current; }
  672. bool operator!=(const iterator &x) const { return !operator==(x); }
  673. // Iterator traversal: forward iteration only
  674. iterator &operator++() { // Preincrement
  675. assert(Current && "Cannot increment end iterator!");
  676. Current = Current->getNext();
  677. return *this;
  678. }
  679. iterator operator++(int) { // Postincrement
  680. iterator tmp = *this;
  681. ++*this;
  682. return tmp;
  683. }
  684. const Target &operator*() const {
  685. assert(Current && "Cannot dereference end iterator!");
  686. return *Current;
  687. }
  688. const Target *operator->() const { return &operator*(); }
  689. };
  690. /// printRegisteredTargetsForVersion - Print the registered targets
  691. /// appropriately for inclusion in a tool's version output.
  692. static void printRegisteredTargetsForVersion(raw_ostream &OS);
  693. /// @name Registry Access
  694. /// @{
  695. static iterator_range<iterator> targets();
  696. /// lookupTarget - Lookup a target based on a target triple.
  697. ///
  698. /// \param Triple - The triple to use for finding a target.
  699. /// \param Error - On failure, an error string describing why no target was
  700. /// found.
  701. static const Target *lookupTarget(const std::string &Triple,
  702. std::string &Error);
  703. /// lookupTarget - Lookup a target based on an architecture name
  704. /// and a target triple. If the architecture name is non-empty,
  705. /// then the lookup is done by architecture. Otherwise, the target
  706. /// triple is used.
  707. ///
  708. /// \param ArchName - The architecture to use for finding a target.
  709. /// \param TheTriple - The triple to use for finding a target. The
  710. /// triple is updated with canonical architecture name if a lookup
  711. /// by architecture is done.
  712. /// \param Error - On failure, an error string describing why no target was
  713. /// found.
  714. static const Target *lookupTarget(const std::string &ArchName,
  715. Triple &TheTriple, std::string &Error);
  716. /// @}
  717. /// @name Target Registration
  718. /// @{
  719. /// RegisterTarget - Register the given target. Attempts to register a
  720. /// target which has already been registered will be ignored.
  721. ///
  722. /// Clients are responsible for ensuring that registration doesn't occur
  723. /// while another thread is attempting to access the registry. Typically
  724. /// this is done by initializing all targets at program startup.
  725. ///
  726. /// @param T - The target being registered.
  727. /// @param Name - The target name. This should be a static string.
  728. /// @param ShortDesc - A short target description. This should be a static
  729. /// string.
  730. /// @param BackendName - The name of the backend. This should be a static
  731. /// string that is the same for all targets that share a backend
  732. /// implementation and must match the name used in the 'def X : Target ...' in
  733. /// TableGen.
  734. /// @param ArchMatchFn - The arch match checking function for this target.
  735. /// @param HasJIT - Whether the target supports JIT code
  736. /// generation.
  737. static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
  738. const char *BackendName,
  739. Target::ArchMatchFnTy ArchMatchFn,
  740. bool HasJIT = false);
  741. /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
  742. /// given target.
  743. ///
  744. /// Clients are responsible for ensuring that registration doesn't occur
  745. /// while another thread is attempting to access the registry. Typically
  746. /// this is done by initializing all targets at program startup.
  747. ///
  748. /// @param T - The target being registered.
  749. /// @param Fn - A function to construct a MCAsmInfo for the target.
  750. static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
  751. T.MCAsmInfoCtorFn = Fn;
  752. }
  753. /// Register a MCObjectFileInfo implementation for the given target.
  754. ///
  755. /// Clients are responsible for ensuring that registration doesn't occur
  756. /// while another thread is attempting to access the registry. Typically
  757. /// this is done by initializing all targets at program startup.
  758. ///
  759. /// @param T - The target being registered.
  760. /// @param Fn - A function to construct a MCObjectFileInfo for the target.
  761. static void RegisterMCObjectFileInfo(Target &T,
  762. Target::MCObjectFileInfoCtorFnTy Fn) {
  763. T.MCObjectFileInfoCtorFn = Fn;
  764. }
  765. /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
  766. /// given target.
  767. ///
  768. /// Clients are responsible for ensuring that registration doesn't occur
  769. /// while another thread is attempting to access the registry. Typically
  770. /// this is done by initializing all targets at program startup.
  771. ///
  772. /// @param T - The target being registered.
  773. /// @param Fn - A function to construct a MCInstrInfo for the target.
  774. static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
  775. T.MCInstrInfoCtorFn = Fn;
  776. }
  777. /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
  778. /// the given target.
  779. static void RegisterMCInstrAnalysis(Target &T,
  780. Target::MCInstrAnalysisCtorFnTy Fn) {
  781. T.MCInstrAnalysisCtorFn = Fn;
  782. }
  783. /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
  784. /// given target.
  785. ///
  786. /// Clients are responsible for ensuring that registration doesn't occur
  787. /// while another thread is attempting to access the registry. Typically
  788. /// this is done by initializing all targets at program startup.
  789. ///
  790. /// @param T - The target being registered.
  791. /// @param Fn - A function to construct a MCRegisterInfo for the target.
  792. static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
  793. T.MCRegInfoCtorFn = Fn;
  794. }
  795. /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
  796. /// the given target.
  797. ///
  798. /// Clients are responsible for ensuring that registration doesn't occur
  799. /// while another thread is attempting to access the registry. Typically
  800. /// this is done by initializing all targets at program startup.
  801. ///
  802. /// @param T - The target being registered.
  803. /// @param Fn - A function to construct a MCSubtargetInfo for the target.
  804. static void RegisterMCSubtargetInfo(Target &T,
  805. Target::MCSubtargetInfoCtorFnTy Fn) {
  806. T.MCSubtargetInfoCtorFn = Fn;
  807. }
  808. /// RegisterTargetMachine - Register a TargetMachine implementation for the
  809. /// given target.
  810. ///
  811. /// Clients are responsible for ensuring that registration doesn't occur
  812. /// while another thread is attempting to access the registry. Typically
  813. /// this is done by initializing all targets at program startup.
  814. ///
  815. /// @param T - The target being registered.
  816. /// @param Fn - A function to construct a TargetMachine for the target.
  817. static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
  818. T.TargetMachineCtorFn = Fn;
  819. }
  820. /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
  821. /// given target.
  822. ///
  823. /// Clients are responsible for ensuring that registration doesn't occur
  824. /// while another thread is attempting to access the registry. Typically
  825. /// this is done by initializing all targets at program startup.
  826. ///
  827. /// @param T - The target being registered.
  828. /// @param Fn - A function to construct an AsmBackend for the target.
  829. static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
  830. T.MCAsmBackendCtorFn = Fn;
  831. }
  832. /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
  833. /// the given target.
  834. ///
  835. /// Clients are responsible for ensuring that registration doesn't occur
  836. /// while another thread is attempting to access the registry. Typically
  837. /// this is done by initializing all targets at program startup.
  838. ///
  839. /// @param T - The target being registered.
  840. /// @param Fn - A function to construct an MCTargetAsmParser for the target.
  841. static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
  842. T.MCAsmParserCtorFn = Fn;
  843. }
  844. /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
  845. /// target.
  846. ///
  847. /// Clients are responsible for ensuring that registration doesn't occur
  848. /// while another thread is attempting to access the registry. Typically
  849. /// this is done by initializing all targets at program startup.
  850. ///
  851. /// @param T - The target being registered.
  852. /// @param Fn - A function to construct an AsmPrinter for the target.
  853. static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
  854. T.AsmPrinterCtorFn = Fn;
  855. }
  856. /// RegisterMCDisassembler - Register a MCDisassembler implementation for
  857. /// the given target.
  858. ///
  859. /// Clients are responsible for ensuring that registration doesn't occur
  860. /// while another thread is attempting to access the registry. Typically
  861. /// this is done by initializing all targets at program startup.
  862. ///
  863. /// @param T - The target being registered.
  864. /// @param Fn - A function to construct an MCDisassembler for the target.
  865. static void RegisterMCDisassembler(Target &T,
  866. Target::MCDisassemblerCtorTy Fn) {
  867. T.MCDisassemblerCtorFn = Fn;
  868. }
  869. /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
  870. /// given target.
  871. ///
  872. /// Clients are responsible for ensuring that registration doesn't occur
  873. /// while another thread is attempting to access the registry. Typically
  874. /// this is done by initializing all targets at program startup.
  875. ///
  876. /// @param T - The target being registered.
  877. /// @param Fn - A function to construct an MCInstPrinter for the target.
  878. static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
  879. T.MCInstPrinterCtorFn = Fn;
  880. }
  881. /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
  882. /// given target.
  883. ///
  884. /// Clients are responsible for ensuring that registration doesn't occur
  885. /// while another thread is attempting to access the registry. Typically
  886. /// this is done by initializing all targets at program startup.
  887. ///
  888. /// @param T - The target being registered.
  889. /// @param Fn - A function to construct an MCCodeEmitter for the target.
  890. static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
  891. T.MCCodeEmitterCtorFn = Fn;
  892. }
  893. static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
  894. T.COFFStreamerCtorFn = Fn;
  895. }
  896. static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
  897. T.MachOStreamerCtorFn = Fn;
  898. }
  899. static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
  900. T.ELFStreamerCtorFn = Fn;
  901. }
  902. static void RegisterSPIRVStreamer(Target &T, Target::SPIRVStreamerCtorTy Fn) {
  903. T.SPIRVStreamerCtorFn = Fn;
  904. }
  905. static void RegisterDXContainerStreamer(Target &T, Target::DXContainerStreamerCtorTy Fn) {
  906. T.DXContainerStreamerCtorFn = Fn;
  907. }
  908. static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
  909. T.WasmStreamerCtorFn = Fn;
  910. }
  911. static void RegisterXCOFFStreamer(Target &T, Target::XCOFFStreamerCtorTy Fn) {
  912. T.XCOFFStreamerCtorFn = Fn;
  913. }
  914. static void RegisterNullTargetStreamer(Target &T,
  915. Target::NullTargetStreamerCtorTy Fn) {
  916. T.NullTargetStreamerCtorFn = Fn;
  917. }
  918. static void RegisterAsmTargetStreamer(Target &T,
  919. Target::AsmTargetStreamerCtorTy Fn) {
  920. T.AsmTargetStreamerCtorFn = Fn;
  921. }
  922. static void
  923. RegisterObjectTargetStreamer(Target &T,
  924. Target::ObjectTargetStreamerCtorTy Fn) {
  925. T.ObjectTargetStreamerCtorFn = Fn;
  926. }
  927. /// RegisterMCRelocationInfo - Register an MCRelocationInfo
  928. /// implementation for the given target.
  929. ///
  930. /// Clients are responsible for ensuring that registration doesn't occur
  931. /// while another thread is attempting to access the registry. Typically
  932. /// this is done by initializing all targets at program startup.
  933. ///
  934. /// @param T - The target being registered.
  935. /// @param Fn - A function to construct an MCRelocationInfo for the target.
  936. static void RegisterMCRelocationInfo(Target &T,
  937. Target::MCRelocationInfoCtorTy Fn) {
  938. T.MCRelocationInfoCtorFn = Fn;
  939. }
  940. /// RegisterMCSymbolizer - Register an MCSymbolizer
  941. /// implementation for the given target.
  942. ///
  943. /// Clients are responsible for ensuring that registration doesn't occur
  944. /// while another thread is attempting to access the registry. Typically
  945. /// this is done by initializing all targets at program startup.
  946. ///
  947. /// @param T - The target being registered.
  948. /// @param Fn - A function to construct an MCSymbolizer for the target.
  949. static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
  950. T.MCSymbolizerCtorFn = Fn;
  951. }
  952. /// RegisterCustomBehaviour - Register a CustomBehaviour
  953. /// implementation for the given target.
  954. ///
  955. /// Clients are responsible for ensuring that registration doesn't occur
  956. /// while another thread is attempting to access the registry. Typically
  957. /// this is done by initializing all targets at program startup.
  958. ///
  959. /// @param T - The target being registered.
  960. /// @param Fn - A function to construct a CustomBehaviour for the target.
  961. static void RegisterCustomBehaviour(Target &T,
  962. Target::CustomBehaviourCtorTy Fn) {
  963. T.CustomBehaviourCtorFn = Fn;
  964. }
  965. /// RegisterInstrPostProcess - Register an InstrPostProcess
  966. /// implementation for the given target.
  967. ///
  968. /// Clients are responsible for ensuring that registration doesn't occur
  969. /// while another thread is attempting to access the registry. Typically
  970. /// this is done by initializing all targets at program startup.
  971. ///
  972. /// @param T - The target being registered.
  973. /// @param Fn - A function to construct an InstrPostProcess for the target.
  974. static void RegisterInstrPostProcess(Target &T,
  975. Target::InstrPostProcessCtorTy Fn) {
  976. T.InstrPostProcessCtorFn = Fn;
  977. }
  978. /// RegisterInstrumentManager - Register an InstrumentManager
  979. /// implementation for the given target.
  980. ///
  981. /// Clients are responsible for ensuring that registration doesn't occur
  982. /// while another thread is attempting to access the registry. Typically
  983. /// this is done by initializing all targets at program startup.
  984. ///
  985. /// @param T - The target being registered.
  986. /// @param Fn - A function to construct an InstrumentManager for the
  987. /// target.
  988. static void RegisterInstrumentManager(Target &T,
  989. Target::InstrumentManagerCtorTy Fn) {
  990. T.InstrumentManagerCtorFn = Fn;
  991. }
  992. /// @}
  993. };
  994. //===--------------------------------------------------------------------===//
  995. /// RegisterTarget - Helper template for registering a target, for use in the
  996. /// target's initialization function. Usage:
  997. ///
  998. ///
  999. /// Target &getTheFooTarget() { // The global target instance.
  1000. /// static Target TheFooTarget;
  1001. /// return TheFooTarget;
  1002. /// }
  1003. /// extern "C" void LLVMInitializeFooTargetInfo() {
  1004. /// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
  1005. /// description", "Foo" /* Backend Name */);
  1006. /// }
  1007. template <Triple::ArchType TargetArchType = Triple::UnknownArch,
  1008. bool HasJIT = false>
  1009. struct RegisterTarget {
  1010. RegisterTarget(Target &T, const char *Name, const char *Desc,
  1011. const char *BackendName) {
  1012. TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch,
  1013. HasJIT);
  1014. }
  1015. static bool getArchMatch(Triple::ArchType Arch) {
  1016. return Arch == TargetArchType;
  1017. }
  1018. };
  1019. /// RegisterMCAsmInfo - Helper template for registering a target assembly info
  1020. /// implementation. This invokes the static "Create" method on the class to
  1021. /// actually do the construction. Usage:
  1022. ///
  1023. /// extern "C" void LLVMInitializeFooTarget() {
  1024. /// extern Target TheFooTarget;
  1025. /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
  1026. /// }
  1027. template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
  1028. RegisterMCAsmInfo(Target &T) {
  1029. TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
  1030. }
  1031. private:
  1032. static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/, const Triple &TT,
  1033. const MCTargetOptions &Options) {
  1034. return new MCAsmInfoImpl(TT, Options);
  1035. }
  1036. };
  1037. /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
  1038. /// implementation. This invokes the specified function to do the
  1039. /// construction. Usage:
  1040. ///
  1041. /// extern "C" void LLVMInitializeFooTarget() {
  1042. /// extern Target TheFooTarget;
  1043. /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
  1044. /// }
  1045. struct RegisterMCAsmInfoFn {
  1046. RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
  1047. TargetRegistry::RegisterMCAsmInfo(T, Fn);
  1048. }
  1049. };
  1050. /// Helper template for registering a target object file info implementation.
  1051. /// This invokes the static "Create" method on the class to actually do the
  1052. /// construction. Usage:
  1053. ///
  1054. /// extern "C" void LLVMInitializeFooTarget() {
  1055. /// extern Target TheFooTarget;
  1056. /// RegisterMCObjectFileInfo<FooMCObjectFileInfo> X(TheFooTarget);
  1057. /// }
  1058. template <class MCObjectFileInfoImpl> struct RegisterMCObjectFileInfo {
  1059. RegisterMCObjectFileInfo(Target &T) {
  1060. TargetRegistry::RegisterMCObjectFileInfo(T, &Allocator);
  1061. }
  1062. private:
  1063. static MCObjectFileInfo *Allocator(MCContext &Ctx, bool PIC,
  1064. bool LargeCodeModel = false) {
  1065. return new MCObjectFileInfoImpl(Ctx, PIC, LargeCodeModel);
  1066. }
  1067. };
  1068. /// Helper template for registering a target object file info implementation.
  1069. /// This invokes the specified function to do the construction. Usage:
  1070. ///
  1071. /// extern "C" void LLVMInitializeFooTarget() {
  1072. /// extern Target TheFooTarget;
  1073. /// RegisterMCObjectFileInfoFn X(TheFooTarget, TheFunction);
  1074. /// }
  1075. struct RegisterMCObjectFileInfoFn {
  1076. RegisterMCObjectFileInfoFn(Target &T, Target::MCObjectFileInfoCtorFnTy Fn) {
  1077. TargetRegistry::RegisterMCObjectFileInfo(T, Fn);
  1078. }
  1079. };
  1080. /// RegisterMCInstrInfo - Helper template for registering a target instruction
  1081. /// info implementation. This invokes the static "Create" method on the class
  1082. /// to actually do the construction. Usage:
  1083. ///
  1084. /// extern "C" void LLVMInitializeFooTarget() {
  1085. /// extern Target TheFooTarget;
  1086. /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
  1087. /// }
  1088. template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
  1089. RegisterMCInstrInfo(Target &T) {
  1090. TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
  1091. }
  1092. private:
  1093. static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
  1094. };
  1095. /// RegisterMCInstrInfoFn - Helper template for registering a target
  1096. /// instruction info implementation. This invokes the specified function to
  1097. /// do the construction. Usage:
  1098. ///
  1099. /// extern "C" void LLVMInitializeFooTarget() {
  1100. /// extern Target TheFooTarget;
  1101. /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
  1102. /// }
  1103. struct RegisterMCInstrInfoFn {
  1104. RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
  1105. TargetRegistry::RegisterMCInstrInfo(T, Fn);
  1106. }
  1107. };
  1108. /// RegisterMCInstrAnalysis - Helper template for registering a target
  1109. /// instruction analyzer implementation. This invokes the static "Create"
  1110. /// method on the class to actually do the construction. Usage:
  1111. ///
  1112. /// extern "C" void LLVMInitializeFooTarget() {
  1113. /// extern Target TheFooTarget;
  1114. /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
  1115. /// }
  1116. template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
  1117. RegisterMCInstrAnalysis(Target &T) {
  1118. TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
  1119. }
  1120. private:
  1121. static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
  1122. return new MCInstrAnalysisImpl(Info);
  1123. }
  1124. };
  1125. /// RegisterMCInstrAnalysisFn - Helper template for registering a target
  1126. /// instruction analyzer implementation. This invokes the specified function
  1127. /// to do the construction. Usage:
  1128. ///
  1129. /// extern "C" void LLVMInitializeFooTarget() {
  1130. /// extern Target TheFooTarget;
  1131. /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
  1132. /// }
  1133. struct RegisterMCInstrAnalysisFn {
  1134. RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
  1135. TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
  1136. }
  1137. };
  1138. /// RegisterMCRegInfo - Helper template for registering a target register info
  1139. /// implementation. This invokes the static "Create" method on the class to
  1140. /// actually do the construction. Usage:
  1141. ///
  1142. /// extern "C" void LLVMInitializeFooTarget() {
  1143. /// extern Target TheFooTarget;
  1144. /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
  1145. /// }
  1146. template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
  1147. RegisterMCRegInfo(Target &T) {
  1148. TargetRegistry::RegisterMCRegInfo(T, &Allocator);
  1149. }
  1150. private:
  1151. static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
  1152. return new MCRegisterInfoImpl();
  1153. }
  1154. };
  1155. /// RegisterMCRegInfoFn - Helper template for registering a target register
  1156. /// info implementation. This invokes the specified function to do the
  1157. /// construction. Usage:
  1158. ///
  1159. /// extern "C" void LLVMInitializeFooTarget() {
  1160. /// extern Target TheFooTarget;
  1161. /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
  1162. /// }
  1163. struct RegisterMCRegInfoFn {
  1164. RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
  1165. TargetRegistry::RegisterMCRegInfo(T, Fn);
  1166. }
  1167. };
  1168. /// RegisterMCSubtargetInfo - Helper template for registering a target
  1169. /// subtarget info implementation. This invokes the static "Create" method
  1170. /// on the class to actually do the construction. Usage:
  1171. ///
  1172. /// extern "C" void LLVMInitializeFooTarget() {
  1173. /// extern Target TheFooTarget;
  1174. /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
  1175. /// }
  1176. template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
  1177. RegisterMCSubtargetInfo(Target &T) {
  1178. TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
  1179. }
  1180. private:
  1181. static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
  1182. StringRef /*FS*/) {
  1183. return new MCSubtargetInfoImpl();
  1184. }
  1185. };
  1186. /// RegisterMCSubtargetInfoFn - Helper template for registering a target
  1187. /// subtarget info implementation. This invokes the specified function to
  1188. /// do the construction. Usage:
  1189. ///
  1190. /// extern "C" void LLVMInitializeFooTarget() {
  1191. /// extern Target TheFooTarget;
  1192. /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
  1193. /// }
  1194. struct RegisterMCSubtargetInfoFn {
  1195. RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
  1196. TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
  1197. }
  1198. };
  1199. /// RegisterTargetMachine - Helper template for registering a target machine
  1200. /// implementation, for use in the target machine initialization
  1201. /// function. Usage:
  1202. ///
  1203. /// extern "C" void LLVMInitializeFooTarget() {
  1204. /// extern Target TheFooTarget;
  1205. /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
  1206. /// }
  1207. template <class TargetMachineImpl> struct RegisterTargetMachine {
  1208. RegisterTargetMachine(Target &T) {
  1209. TargetRegistry::RegisterTargetMachine(T, &Allocator);
  1210. }
  1211. private:
  1212. static TargetMachine *Allocator(const Target &T, const Triple &TT,
  1213. StringRef CPU, StringRef FS,
  1214. const TargetOptions &Options,
  1215. std::optional<Reloc::Model> RM,
  1216. std::optional<CodeModel::Model> CM,
  1217. CodeGenOpt::Level OL, bool JIT) {
  1218. return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
  1219. }
  1220. };
  1221. /// RegisterMCAsmBackend - Helper template for registering a target specific
  1222. /// assembler backend. Usage:
  1223. ///
  1224. /// extern "C" void LLVMInitializeFooMCAsmBackend() {
  1225. /// extern Target TheFooTarget;
  1226. /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
  1227. /// }
  1228. template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
  1229. RegisterMCAsmBackend(Target &T) {
  1230. TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
  1231. }
  1232. private:
  1233. static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI,
  1234. const MCRegisterInfo &MRI,
  1235. const MCTargetOptions &Options) {
  1236. return new MCAsmBackendImpl(T, STI, MRI);
  1237. }
  1238. };
  1239. /// RegisterMCAsmParser - Helper template for registering a target specific
  1240. /// assembly parser, for use in the target machine initialization
  1241. /// function. Usage:
  1242. ///
  1243. /// extern "C" void LLVMInitializeFooMCAsmParser() {
  1244. /// extern Target TheFooTarget;
  1245. /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
  1246. /// }
  1247. template <class MCAsmParserImpl> struct RegisterMCAsmParser {
  1248. RegisterMCAsmParser(Target &T) {
  1249. TargetRegistry::RegisterMCAsmParser(T, &Allocator);
  1250. }
  1251. private:
  1252. static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
  1253. MCAsmParser &P, const MCInstrInfo &MII,
  1254. const MCTargetOptions &Options) {
  1255. return new MCAsmParserImpl(STI, P, MII, Options);
  1256. }
  1257. };
  1258. /// RegisterAsmPrinter - Helper template for registering a target specific
  1259. /// assembly printer, for use in the target machine initialization
  1260. /// function. Usage:
  1261. ///
  1262. /// extern "C" void LLVMInitializeFooAsmPrinter() {
  1263. /// extern Target TheFooTarget;
  1264. /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
  1265. /// }
  1266. template <class AsmPrinterImpl> struct RegisterAsmPrinter {
  1267. RegisterAsmPrinter(Target &T) {
  1268. TargetRegistry::RegisterAsmPrinter(T, &Allocator);
  1269. }
  1270. private:
  1271. static AsmPrinter *Allocator(TargetMachine &TM,
  1272. std::unique_ptr<MCStreamer> &&Streamer) {
  1273. return new AsmPrinterImpl(TM, std::move(Streamer));
  1274. }
  1275. };
  1276. /// RegisterMCCodeEmitter - Helper template for registering a target specific
  1277. /// machine code emitter, for use in the target initialization
  1278. /// function. Usage:
  1279. ///
  1280. /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
  1281. /// extern Target TheFooTarget;
  1282. /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
  1283. /// }
  1284. template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
  1285. RegisterMCCodeEmitter(Target &T) {
  1286. TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
  1287. }
  1288. private:
  1289. static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
  1290. MCContext & /*Ctx*/) {
  1291. return new MCCodeEmitterImpl();
  1292. }
  1293. };
  1294. } // end namespace llvm
  1295. #endif // LLVM_MC_TARGETREGISTRY_H
  1296. #ifdef __GNUC__
  1297. #pragma GCC diagnostic pop
  1298. #endif