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 <map>
5#include <optional>
6#include <string>
7
8#include "ir/ir.h"
9#include "lib/exceptions.h"
10
11namespace P4::P4Tools {
12
17class Target {
18 public:
20 struct Spec {
21 std::string deviceName;
22 std::string archName;
23
25 Spec(std::string deviceName, std::string archName);
26
28 bool operator<(const Spec &) const;
29 };
30
35 static bool init(std::string deviceName, std::string archName);
36
43 static bool setDevice(std::string deviceName);
44
51 static bool setArch(std::string archName);
52
54 std::string toolName;
55
58
59 // A virtual destructor makes this class polymorphic, so that the dynamic cast in get() can
60 // work.
61 virtual ~Target() = default;
62
67 virtual const IR::Expression *createTargetUninitialized(const IR::Type *type,
68 bool forceTaint) const;
69
70 protected:
73 Target(std::string_view toolName, const std::string &deviceName, const std::string &archName);
74
76 //
77 // Implemented here because of limitations of templates.
78 template <class TargetImpl>
79 static const TargetImpl &get(std::string_view toolName) {
80 if (curTarget == std::nullopt) {
81 FATAL_ERROR(
82 "Target not initialized. Please provide a target using the --target option.");
83 }
84
85 const auto &instances = registry.at(*curTarget);
86 auto instance = instances.find(toolName);
87 BUG_CHECK(instance != instances.end(),
88 "Architecture %1% on device %2% not supported for %3%", curTarget->archName,
89 curTarget->deviceName, toolName);
90
91 const auto *casted = dynamic_cast<const TargetImpl *>(instance->second);
92 BUG_CHECK(casted, "%1%/%2% implementation for %3% has wrong type", curTarget->deviceName,
93 curTarget->archName, toolName);
94 return *casted;
95 }
96
97 private:
99 static std::optional<Spec> curTarget;
100
102 static std::map<Spec, std::map<std::string, const Target *, std::less<>>> registry;
103
105 static std::map<std::string, std::string, std::less<>> defaultArchByDevice;
106
108 static std::map<std::string, std::string, std::less<>> defaultDeviceByArch;
109};
110
111} // namespace P4::P4Tools
112
113#endif /* BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_ */
Definition p4tools/common/core/target.h:17
static const TargetImpl & get(std::string_view toolName)
Definition p4tools/common/core/target.h:79
static bool init(std::string deviceName, std::string archName)
Definition p4tools/common/core/target.cpp:40
std::string toolName
The name of the tool supported by this instance.
Definition p4tools/common/core/target.h:54
static bool setDevice(std::string deviceName)
Definition p4tools/common/core/target.cpp:51
static bool setArch(std::string archName)
Definition p4tools/common/core/target.cpp:61
Spec spec
The device and architecture supported by this instance.
Definition p4tools/common/core/target.h:57
virtual const IR::Expression * createTargetUninitialized(const IR::Type *type, bool forceTaint) const
Definition p4tools/common/core/target.cpp:71
Target(std::string_view toolName, const std::string &deviceName, const std::string &archName)
Definition p4tools/common/core/target.cpp:79
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:20
Spec(std::string deviceName, std::string archName)
Names provided to this constructor are converted to lower case.
Definition p4tools/common/core/target.cpp:16
bool operator<(const Spec &) const
Lexicographic ordering on (deviceName, archName).
Definition p4tools/common/core/target.cpp:24