MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
driver.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4
5#include <absl/container/node_hash_map.h>
6
7#include "mim/flags.h"
8#include "mim/plugin.h"
9#include "mim/world.h"
10
11#include "mim/util/log.h"
12
13namespace mim {
14
15/// Some "global" variables needed all over the place.
16/// Well, there are not really global - that's the point of this class.
17class Driver : public fe::SymPool {
18public:
19 Driver();
20
21 /// @name Getters
22 ///@{
23 Flags& flags() { return flags_; }
24 Log& log() { return log_; }
25 World& world() { return world_; }
26 ///@}
27
28 /// @name Manage Search Paths
29 /// Search paths for plugins are in the following order:
30 /// 1. The empty path. Used as prefix to look into current working directory without resorting to an absolute path.
31 /// 2. All further user-specified paths via Driver::add_search_path; paths added first will also be searched first.
32 /// 3. All paths specified in the environment variable `MIM_PLUGIN_PATH`.
33 /// 4. `path/to/mim.exe/../../lib/mim`
34 /// 5. `CMAKE_INSTALL_PREFIX/lib/mim`
35 ///@{
36 const auto& search_paths() const { return search_paths_; }
37 void add_search_path(fs::path path) {
38 if (fs::exists(path) && fs::is_directory(path)) search_paths_.insert(insert_, std::move(path));
39 }
40 ///@}
41
42 /// @name Manage Imports
43 /// This is a list of pairs where each pair contains:
44 /// 1. The `fs::path` used during import,
45 /// 2. The name as Sym%bol used in the `import` directive or in Parser::import.
46 ///@{
47 const auto& import_path2sym() { return import_path2sym_; }
48 auto import_paths() { return import_path2sym_ | std::views::keys; }
49 auto import_syms() { return import_path2sym_ | std::views::values; }
50 /// Yields a `fs::path*` if not already added that you can use in Loc%ation; returns `nullptr` otherwise.
51 const fs::path* add_import(fs::path, Sym);
52 ///@}
53
54 /// @name Load Plugin
55 /// Finds and loads a shared object file that implements the MimIR Plugin @p name.
56 /// If \a name is an absolute path to a `.so`/`.dll` file, this is used.
57 /// Otherwise, "name", "libmim_name.so" (Linux, Mac), "mim_name.dll" (Win)
58 /// are searched for in Driver::search_paths().
59 ///@{
60 void load(Sym name);
61 void load(const std::string& name) { return load(sym(name)); }
62 bool is_loaded(Sym sym) const { return lookup(plugins_, sym); }
63 void* get_fun_ptr(Sym plugin, const char* name);
64 template<class F> auto get_fun_ptr(Sym plugin, const char* name) {
65 return reinterpret_cast<F*>(get_fun_ptr(plugin, name));
66 }
67 template<class F> auto get_fun_ptr(const char* plugin, const char* name) {
68 return get_fun_ptr<F>(sym(plugin), name);
69 }
70 ///@}
71
72 /// @name Manage Plugins
73 /// All these lookups yield `nullptr` if the key has not been found.
74 ///@{
75 auto pass(flags_t flags) { return lookup(passes_, flags); }
76 auto normalizer(flags_t flags) const { return lookup(normalizers_, flags); }
77 auto normalizer(plugin_t d, tag_t t, sub_t s) const { return normalizer(d | flags_t(t << 8u) | s); }
78 auto backend(std::string_view name) { return lookup(backends_, name); }
79 ///@}
80
81private:
82 // This must go *first* so plugins will be unloaded *last* in the d'tor; otherwise funny things might happen ...
83 absl::node_hash_map<Sym, Plugin::Handle> plugins_;
84 Flags flags_;
85 Log log_;
86 World world_;
87 std::list<fs::path> search_paths_;
88 std::list<fs::path>::iterator insert_ = search_paths_.end();
89 Backends backends_;
90 Passes passes_;
91 Normalizers normalizers_;
92 std::deque<std::pair<fs::path, Sym>> import_path2sym_;
93};
94
95#define GET_FUN_PTR(plugin, f) get_fun_ptr<decltype(f)>(plugin, #f)
96
97} // namespace mim
Some "global" variables needed all over the place.
Definition driver.h:17
auto pass(flags_t flags)
Definition driver.h:75
void load(Sym name)
Definition driver.cpp:53
const auto & import_path2sym()
Definition driver.h:47
void add_search_path(fs::path path)
Definition driver.h:37
auto backend(std::string_view name)
Definition driver.h:78
Log & log()
Definition driver.h:24
auto normalizer(plugin_t d, tag_t t, sub_t s) const
Definition driver.h:77
World & world()
Definition driver.h:25
auto import_paths()
Definition driver.h:48
bool is_loaded(Sym sym) const
Definition driver.h:62
const fs::path * add_import(fs::path, Sym)
Yields a fs::path* if not already added that you can use in Location; returns nullptr otherwise.
Definition driver.cpp:45
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:91
auto normalizer(flags_t flags) const
Definition driver.h:76
void load(const std::string &name)
Definition driver.h:61
Flags & flags()
Definition driver.h:23
auto get_fun_ptr(Sym plugin, const char *name)
Definition driver.h:64
auto get_fun_ptr(const char *plugin, const char *name)
Definition driver.h:67
auto import_syms()
Definition driver.h:49
const auto & search_paths() const
Definition driver.h:36
Facility to log what you are doing.
Definition log.h:17
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
Definition ast.h:14
u8 sub_t
Definition types.h:48
u64 flags_t
Definition types.h:45
auto lookup(const C &container, const K &key)
Yields pointer to element (or the element itself if it is already a pointer), if found and nullptr ot...
Definition util.h:95
absl::flat_hash_map< flags_t, std::function< void(World &, PipelineBuilder &, const Def *)> > Passes
axiom ↦ (pipeline part) × (axiom application) → () The function should inspect Application to const...
Definition plugin.h:22
absl::btree_map< std::string, void(*)(World &, std::ostream &)> Backends
Definition plugin.h:23
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:19
u64 plugin_t
Definition types.h:46
u8 tag_t
Definition types.h:47
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11