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