P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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 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 toolName, std::string deviceName, std::string archName);
74
76 //
77 // Implemented here because of limitations of templates.
78 template <class TargetImpl>
79 static const TargetImpl &get(const std::string &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 BUG_CHECK(instances.count(toolName), "Architecture %1% on device %2% not supported for %3%",
87 curTarget->archName, curTarget->deviceName, toolName);
88
89 const auto *instance = instances.at(toolName);
90 const auto *casted = dynamic_cast<const TargetImpl *>(instance);
91 BUG_CHECK(casted, "%1%/%2% implementation for %3% has wrong type", curTarget->deviceName,
92 curTarget->archName, toolName);
93 return *casted;
94 }
95
96 private:
98 static std::optional<Spec> curTarget;
99
101 static std::map<Spec, std::map<std::string, const Target *>> registry;
102
104 static std::map<std::string, std::string> defaultArchByDevice;
105
107 static std::map<std::string, std::string> defaultDeviceByArch;
108};
109
110} // namespace P4Tools
111
112#endif /* BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_ */
Definition target.h:17
virtual const IR::Expression * createTargetUninitialized(const IR::Type *type, bool forceTaint) const
Definition target.cpp:71
static bool setDevice(std::string deviceName)
Definition target.cpp:51
static bool setArch(std::string archName)
Definition target.cpp:61
Spec spec
The device and architecture supported by this instance.
Definition target.h:57
Target(std::string toolName, std::string deviceName, std::string archName)
Definition target.cpp:79
static bool init(std::string deviceName, std::string archName)
Definition target.cpp:40
std::string toolName
The name of the tool supported by this instance.
Definition target.h:54
static const TargetImpl & get(const std::string &toolName)
Definition target.h:79
Definition compiler_target.cpp:19
Specifies a target device and architecture by their names in lower case.
Definition target.h:20
bool operator<(const Spec &) const
Lexicographic ordering on (deviceName, archName).
Definition target.cpp:24
Spec(std::string deviceName, std::string archName)
Names provided to this constructor are converted to lower case.
Definition target.cpp:16