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 const Flags& flags() const { return flags_; }
25 Log& log() const { return log_; }
26 World& world() { return world_; }
27 ///@}
28
29 /// @name Manage Search Paths
30 /// Search paths for plugins are in the following order:
31 /// 1. The empty path. Used as prefix to look into current working directory without resorting to an absolute path.
32 /// 2. All further user-specified paths via Driver::add_search_path; paths added first will also be searched first.
33 /// 3. All paths specified in the environment variable `MIM_PLUGIN_PATH`.
34 /// 4. `path/to/mim.exe/../../lib/mim`
35 /// 5. `CMAKE_INSTALL_PREFIX/lib/mim`
36 ///@{
37 const auto& search_paths() const { return search_paths_; }
38 void add_search_path(fs::path path) {
39 if (fs::exists(path) && fs::is_directory(path)) search_paths_.insert(insert_, std::move(path));
40 }
41 ///@}
42
43 /// @name Manage Imports
44 /// This is a list of pairs where each pair contains:
45 /// 1. The `fs::path` used during import,
46 /// 2. The name as Sym%bol used in the `import` directive or in Parser::import.
47 ///@{
48 const auto& import_path2sym() { return import_path2sym_; }
49 auto import_paths() { return import_path2sym_ | std::views::keys; }
50 auto import_syms() { return import_path2sym_ | std::views::values; }
51 /// Yields a `fs::path*` if not already added that you can use in Loc%ation; returns `nullptr` otherwise.
52 const fs::path* add_import(fs::path, Sym);
53 ///@}
54
55 /// @name Load Plugin
56 /// Finds and loads a shared object file that implements the MimIR Plugin @p name.
57 /// If \a name is an absolute path to a `.so`/`.dll` file, this is used.
58 /// Otherwise, "name", "libmim_name.so" (Linux, Mac), "mim_name.dll" (Win)
59 /// are searched for in Driver::search_paths().
60 ///@{
61 void load(Sym name);
62 void load(const std::string& name) { return load(sym(name)); }
63 bool is_loaded(Sym sym) const { return lookup(plugins_, sym); }
64 void* get_fun_ptr(Sym plugin, const char* name);
65
66 template<class F>
67 auto get_fun_ptr(Sym plugin, const char* name) {
68 return reinterpret_cast<F*>(get_fun_ptr(plugin, name));
69 }
70
71 template<class F>
72 auto get_fun_ptr(const char* plugin, const char* name) {
73 return get_fun_ptr<F>(sym(plugin), name);
74 }
75 ///@}
76
77 /// @name Manage Plugins
78 /// All these lookups yield `nullptr` if the key has not been found.
79 ///@{
80 auto stage(flags_t flags) { return lookup(stages_, flags); }
81 const auto& stages() const { return stages_; }
82 auto normalizer(flags_t flags) const { return lookup(normalizers_, flags); }
83 auto normalizer(plugin_t d, tag_t t, sub_t s) const { return normalizer(d | flags_t(t << 8u) | s); }
84 auto backend(std::string_view name) { return lookup(backends_, name); }
85 ///@}
86
87private:
88 // This must go *first* so plugins will be unloaded *last* in the d'tor; otherwise funny things might happen ...
89 absl::node_hash_map<Sym, Plugin::Handle> plugins_;
90 Flags flags_;
91 mutable Log log_;
92 World world_;
93 std::list<fs::path> search_paths_;
94 std::list<fs::path>::iterator insert_ = search_paths_.end();
95 Backends backends_;
96 Flags2Stages stages_;
97 Normalizers normalizers_;
98 std::deque<std::pair<fs::path, Sym>> import_path2sym_;
99};
100
101#define GET_FUN_PTR(plugin, f) get_fun_ptr<decltype(f)>(plugin, #f)
102
103} // namespace mim
const auto & stages() const
Definition driver.h:81
void load(Sym name)
Definition driver.cpp:45
const auto & import_path2sym()
Definition driver.h:48
void add_search_path(fs::path path)
Definition driver.h:38
auto backend(std::string_view name)
Definition driver.h:84
auto normalizer(plugin_t d, tag_t t, sub_t s) const
Definition driver.h:83
World & world()
Definition driver.h:26
Log & log() const
Definition driver.h:25
auto import_paths()
Definition driver.h:49
const Flags & flags() const
Definition driver.h:24
bool is_loaded(Sym sym) const
Definition driver.h:63
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:37
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:83
auto normalizer(flags_t flags) const
Definition driver.h:82
void load(const std::string &name)
Definition driver.h:62
Flags & flags()
Definition driver.h:23
auto get_fun_ptr(Sym plugin, const char *name)
Definition driver.h:67
auto get_fun_ptr(const char *plugin, const char *name)
Definition driver.h:72
auto import_syms()
Definition driver.h:50
auto stage(flags_t flags)
Definition driver.h:80
const auto & search_paths() const
Definition driver.h:37
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:32
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:100
absl::btree_map< std::string, void(*)(World &, std::ostream &)> Backends
Definition plugin.h:24
absl::flat_hash_map< flags_t, std::function< std::unique_ptr< Stage >(World &)> > Flags2Stages
Maps an an axiom of a Stage to a function that creates one.
Definition plugin.h:22
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