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