7#include <initializer_list>
15#include <absl/container/flat_hash_map.h>
30using Flags2Phases = absl::flat_hash_map<flags_t, std::function<std::unique_ptr<Phase>(
World&)>>;
53inline std::optional<std::string_view> arg_split(std::string_view arg, std::string_view key) {
54 if (!arg.starts_with(key))
return {};
55 auto val = arg.substr(key.size());
56 if (val.empty())
return val;
57 if (val.front() ==
'=')
return val.substr(1);
63template<
class...
Keys>
64std::optional<std::string_view>
arg_value(fe::View<std::string> args,
Keys... keys) {
65 std::optional<std::string_view> res;
66 for (std::string_view arg : args)
67 for (std::string_view key : {std::string_view(keys)...})
68 if (
auto val = detail::arg_split(arg, key); val && !val->empty()) res = val;
73inline std::optional<bool>
arg_bool(fe::View<std::string> args,
74 std::initializer_list<std::string_view> on,
75 std::initializer_list<std::string_view> off) {
76 std::optional<bool> res;
77 for (std::string_view arg : args) {
79 if (arg == key) res =
true;
81 if (arg == key) res =
false;
87template<
class...
Keys>
89 for (std::string_view arg : args)
90 for (std::string_view key : {std::string_view(keys)...})
91 if (arg == key)
return true;
102 : name_(std::move(
name)) {}
104 std::string&
name() {
return name_; }
109 if (name_.empty())
return nullptr;
110 if (name_ ==
"-")
return &std::cout;
111 if (!ofs_.is_open()) {
113 if (!ofs_) fe::throwf(
"cannot open output file `{}`", name_);
131 auto cmp = std::tie(
major,
minor) <=> std::tie(other.major, other.minor);
132 if (cmp != 0)
return cmp;
134 return std::strcmp(
suffix, other.suffix) <=> 0;
139 return major == other.major &&
minor == other.minor && std::strcmp(
suffix, other.suffix) == 0;
150 Version { MIM_VER_MAJOR, MIM_VER_MINOR, MIM_VER_SUFFIX, MIM_GIT_HASH }
155 using Handle = std::unique_ptr<void, void (*)(
void*)>;
208 static std::optional<plugin_t>
mangle(std::string_view plugin);
247 template<
class Id>
static constexpr size_t Num = size_t(-1);
249 template<
class Id>
static consteval size_t num () {
return Num <Id>; }
258template<>
struct std::formatter<
mim::Version> : fe::ostream_formatter {};
Some "global" variables needed all over the place.
std::string & name()
Bound to a fe::Cli option, e.g. --output-mim.
std::ostream * os()
The stream to write to; nullptr if this output was not requested, std::cout for "-".
A Phase performs one self-contained task over the whole World.
The World represents the whole program and manages creation of MimIR nodes (Defs).
const char * descr
What it does; one sentence, Markdown.
bool arg_flag(fe::View< std::string > args, Keys... keys)
Whether any of keys occurs.
absl::flat_hash_map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
mim::Plugin mim_get_plugin()
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
fe::SymTab< ast::Tok::Tag, ast::Num_Keys+ast::Num_Subst > Keys
The reserved words the ast::Lexer looks up, keyed by the Sym it has just interned.
const char * descr
What it does; one sentence, Markdown.
const char * name
Name of the variable, e.g. "CUDA_HOME".
const char * syntax
How to spell the argument, e.g. "o=<file>, output=<file>".
std::optional< bool > arg_bool(fe::View< std::string > args, std::initializer_list< std::string_view > on, std::initializer_list< std::string_view > off)
An on key ↦ true, an off key ↦ false; std::nullopt if neither occurs.
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Value of <key>=<value>; std::nullopt if none of keys carries one.
One -X <plugin>:<arg> a Plugin understands; see Arguments.
One environment variable a Plugin reads; see Environment Variables.
static constexpr flags_t flags(plugin_t p, tag_t t, sub_t s=0)
Assembles the full flags from its plugin, tag, and sub fields.
static constexpr plugin_t Global_Plugin
static std::string demangle(plugin_t plugin)
Reverts an Axm::mangled plugin back to its name; never longer than Annex::Max_Plugin_Size.
static std::optional< plugin_t > mangle(std::string_view plugin)
Mangles s into a dense 48-bit representation.
static constexpr tag_t flags2tag(flags_t f)
Yields the tag part of the name as integer.
static constexpr size_t Max_Plugin_Size
static constexpr sub_t flags2sub(flags_t f)
Yields the sub part of the name as integer.
static constexpr plugin_t flags2plugin(flags_t f)
static consteval size_t num()
static constexpr size_t Num
Number of Axm::subtags.
static consteval flags_t base()
static constexpr flags_t Base
static constexpr flags_t flags2base(flags_t f)
Includes Axm::plugin() and Axm::tag() but not Axm::sub.
Basic info and registration function pointer to be returned from a specific plugin.
const PluginEnv * envs
The environment variables this Plugin reads; see PluginEnv.
size_t num_envs
Number of Plugin::envs.
size_t num_args
Number of Plugin::args.
const PluginArg * args
The -X arguments this Plugin understands; see PluginArg.
void(*) register_normalizers(Normalizers &)
Callback for registering the mapping from axm ids to normalizer functions in the given normalizers ma...
std::unique_ptr< void, void(*)(void *)> Handle
const char * name
Name of the Plugin.
void(*) register_phases(Flags2Phases &)
Callback for registering the Plugin's callbacks for Phases.
Version version
Version of the Plugin.
friend std::ostream & operator<<(std::ostream &os, const Version &v)
constexpr bool operator==(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
constexpr auto operator<=>(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.