P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4tools/common/core/target.h
1#ifndef BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_
2#define BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_
3
4#include <cstdlib>
5#include <map>
6#include <optional>
7#include <string>
8
9#include "ir/ir.h"
10#include "lib/compile_context.h"
11#include "lib/exceptions.h"
12
13namespace P4::P4Tools {
14
19class Target {
20 public:
22 struct Spec {
23 // TODO: Prefix the variables here and in target with "_" to avoid ambiguities.
24 std::string deviceName;
25 std::string archName;
26
28 Spec(std::string_view deviceName, std::string_view archName);
29
31 bool operator<(const Spec &) const;
32 };
33
38 static bool init(std::string_view deviceName, std::string_view archName);
39
46 static bool setDevice(std::string_view deviceName);
47
54 static bool setArch(std::string_view archName);
55
57 std::string toolName;
58
61
62 // A virtual destructor makes this class polymorphic, so that the dynamic cast in get() can
63 // work.
64 virtual ~Target() = default;
65
70 virtual const IR::Expression *createTargetUninitialized(const IR::Type *type,
71 bool forceTaint) const;
72
75 static std::optional<ICompileContext *> initializeTarget(std::string_view toolName,
76 const std::vector<const char *> &args);
77 static std::optional<ICompileContext *> initializeTarget(std::string_view toolName,
78 std::string_view target,
79 std::string_view arch);
80
81 protected:
84 Target(std::string_view toolName, const std::string &deviceName, const std::string &archName);
85
87 [[nodiscard]] virtual ICompileContext *makeContext() const = 0;
88
90 //
91 // Implemented here because of limitations of templates.
92 template <class TargetImpl>
93 static const TargetImpl &get(std::string_view toolName) {
94 if (curTarget == std::nullopt) {
95 FATAL_ERROR(
96 "Target not initialized. Please provide a target using the --target option.");
97 }
98
99 const auto &instances = registry.at(*curTarget);
100 auto instance = instances.find(toolName);
101 BUG_CHECK(instance != instances.end(),
102 "Architecture %1% on device %2% not supported for %3%", curTarget->archName,
103 curTarget->deviceName, toolName);
104
105 const auto *casted = dynamic_cast<const TargetImpl *>(instance->second);
106 BUG_CHECK(casted, "%1%/%2% implementation for %3% has wrong type", curTarget->deviceName,
107 curTarget->archName, toolName);
108 return *casted;
109 }
110
111 private:
113 static std::optional<Spec> curTarget;
114
116 static std::map<Spec, std::map<std::string, const Target *, std::less<>>> registry;
117
119 static std::map<std::string, std::string, std::less<>> defaultArchByDevice;
120
122 static std::map<std::string, std::string, std::less<>> defaultDeviceByArch;
123};
124
125} // namespace P4::P4Tools
126
127#endif /* BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_ */
Definition compile_context.h:32
Definition p4tools/common/core/target.h:19
static const TargetImpl & get(std::string_view toolName)
Definition p4tools/common/core/target.h:93
static bool setDevice(std::string_view deviceName)
Definition p4tools/common/core/target.cpp:114
std::string toolName
The name of the tool supported by this instance.
Definition p4tools/common/core/target.h:57
virtual ICompileContext * makeContext() const =0
static bool setArch(std::string_view archName)
Definition p4tools/common/core/target.cpp:124
static bool init(std::string_view deviceName, std::string_view archName)
Definition p4tools/common/core/target.cpp:35
Spec spec
The device and architecture supported by this instance.
Definition p4tools/common/core/target.h:60
static std::optional< ICompileContext * > initializeTarget(std::string_view toolName, const std::vector< const char * > &args)
Definition p4tools/common/core/target.cpp:70
virtual const IR::Expression * createTargetUninitialized(const IR::Type *type, bool forceTaint) const
Definition p4tools/common/core/target.cpp:135
Definition common/compiler/compiler_result.cpp:3
Specifies a target device and architecture by their names in lower case.
Definition p4tools/common/core/target.h:22
bool operator<(const Spec &) const
Lexicographic ordering on (deviceName, archName).
Definition p4tools/common/core/target.cpp:19