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& imports() { return imports_; }
48 /// Yields a `fs::path*` if not already added that you can use in Loc%ation; returns `nullptr` otherwise.
49 const fs::path* add_import(fs::path, Sym);
50 ///@}
51
52 /// @name Load Plugin
53 /// Finds and loads a shared object file that implements the MimIR Plugin @p name.
54 /// If \a name is an absolute path to a `.so`/`.dll` file, this is used.
55 /// Otherwise, "name", "libmim_name.so" (Linux, Mac), "mim_name.dll" (Win)
56 /// are searched for in Driver::search_paths().
57 ///@{
58 void load(Sym name);
59 void load(const std::string& name) { return load(sym(name)); }
60 bool is_loaded(Sym sym) const { return lookup(plugins_, sym); }
61 void* get_fun_ptr(Sym plugin, const char* name);
62 template<class F> auto get_fun_ptr(Sym plugin, const char* name) {
63 return reinterpret_cast<F*>(get_fun_ptr(plugin, name));
64 }
65 template<class F> auto get_fun_ptr(const char* plugin, const char* name) {
66 return get_fun_ptr<F>(sym(plugin), name);
67 }
68 ///@}
69
70 /// @name Manage Plugins
71 /// All these lookups yield `nullptr` if the key has not been found.
72 ///@{
73 auto pass(flags_t flags) { return lookup(passes_, flags); }
74 auto normalizer(flags_t flags) const { return lookup(normalizers_, flags); }
75 auto normalizer(plugin_t d, tag_t t, sub_t s) const { return normalizer(d | flags_t(t << 8u) | s); }
76 auto backend(std::string_view name) { return lookup(backends_, name); }
77 ///@}
78
79private:
80 // This must go *first* so plugins will be unloaded *last* in the d'tor; otherwise funny things might happen ...
81 absl::node_hash_map<Sym, Plugin::Handle> plugins_;
82 Flags flags_;
83 Log log_;
84 World world_;
85 std::list<fs::path> search_paths_;
86 std::list<fs::path>::iterator insert_ = search_paths_.end();
87 Backends backends_;
88 Passes passes_;
89 Normalizers normalizers_;
90 std::deque<std::pair<fs::path, Sym>> imports_;
91};
92
93#define GET_FUN_PTR(plugin, f) get_fun_ptr<decltype(f)>(plugin, #f)
94
95} // namespace mim
Some "global" variables needed all over the place.
Definition driver.h:17
auto pass(flags_t flags)
Definition driver.h:73
void load(Sym name)
Definition driver.cpp:53
void add_search_path(fs::path path)
Definition driver.h:37
auto backend(std::string_view name)
Definition driver.h:76
Log & log()
Definition driver.h:24
auto normalizer(plugin_t d, tag_t t, sub_t s) const
Definition driver.h:75
World & world()
Definition driver.h:25
bool is_loaded(Sym sym) const
Definition driver.h:60
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:74
void load(const std::string &name)
Definition driver.h:59
Flags & flags()
Definition driver.h:23
auto get_fun_ptr(Sym plugin, const char *name)
Definition driver.h:62
auto get_fun_ptr(const char *plugin, const char *name)
Definition driver.h:65
const auto & search_paths() const
Definition driver.h:36
const auto & imports()
Definition driver.h:47
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 cfg.h:11
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:93
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