Thorin 1.9.0
The Higher ORder 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 "thorin/flags.h"
6#include "thorin/plugin.h"
7#include "thorin/world.h"
8
9#include "thorin/util/log.h"
10
11#include "absl/container/node_hash_map.h"
12
13namespace thorin {
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 ///@{
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 `THORIN_PLUGIN_PATH`.
34 /// 4. `path/to/thorin.exe/../../lib/thorin`
35 /// 5. `CMAKE_INSTALL_PREFIX/lib/thorin`
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 ///@{
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 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 ///@{
54 /// Finds and loads a shared object file that implements the Thorin Plugin @p name.
55 /// If \a name is an absolute path to a `.so`/`.dll` file, this is used.
56 /// Otherwise, "name", "libthorin_name.so" (Linux, Mac), "thorin_name.dll" (Win)
57 /// are searched for in Driver::search_paths().
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 ///@{
72 /// All these lookups yield `nullptr` if the key has not been found.
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
79 /// @name Manage Annex
80 ///@{
81 const auto& plugin2annxes(Sym plugin) { return plugin2annexes_[plugin]; }
82 std::pair<Annex&, bool> name2annex(Sym sym, Sym plugin, Sym tag, Loc loc);
83 ///@}
84
85private:
86 // This must go *first* so plugins will be unloaded *last* in the d'tor; otherwise funny things might happen ...
87 absl::node_hash_map<Sym, Plugin::Handle> plugins_;
88 Flags flags_;
89 Log log_;
90 World world_;
91 std::list<fs::path> search_paths_;
92 std::list<fs::path>::iterator insert_ = search_paths_.end();
93 Backends backends_;
94 Passes passes_;
95 Normalizers normalizers_;
96 std::deque<std::pair<fs::path, Sym>> imports_;
97 fe::SymMap<fe::SymMap<Annex>> plugin2annexes_;
98};
99
100#define GET_FUN_PTR(plugin, f) get_fun_ptr<decltype(f)>(plugin, #f)
101
102} // namespace thorin
Some "global" variables needed all over the place.
Definition driver.h:17
auto get_fun_ptr(const char *plugin, const char *name)
Definition driver.h:65
std::pair< Annex &, bool > name2annex(Sym sym, Sym plugin, Sym tag, Loc loc)
Definition driver.cpp:96
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:91
const auto & plugin2annxes(Sym plugin)
Definition driver.h:81
void load(const std::string &name)
Definition driver.h:59
World & world()
Definition driver.h:25
void load(Sym name)
Definition driver.cpp:53
Log & log()
Definition driver.h:24
const auto & search_paths() const
Definition driver.h:36
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
bool is_loaded(Sym sym) const
Definition driver.h:60
Flags & flags()
Definition driver.h:23
auto normalizer(plugin_t d, tag_t t, sub_t s) const
Definition driver.h:75
auto backend(std::string_view name)
Definition driver.h:76
const auto & imports()
Definition driver.h:47
auto pass(flags_t flags)
Definition driver.h:73
auto normalizer(flags_t flags) const
Definition driver.h:74
void add_search_path(fs::path path)
Definition driver.h:37
auto get_fun_ptr(Sym plugin, const char *name)
Definition driver.h:62
Facility to log what you are doing.
Definition log.h:14
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Definition cfg.h:11
u8 sub_t
Definition types.h:49
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
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, NormalizeFn > Normalizers
Definition plugin.h:19
absl::btree_map< std::string, void(*)(World &, std::ostream &)> Backends
Definition plugin.h:23
u8 tag_t
Definition types.h:48
u64 plugin_t
Definition types.h:47
u64 flags_t
Definition types.h:46
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11