MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
driver.cpp
Go to the documentation of this file.
1#include "mim/driver.h"
2
3#include "mim/plugin.h"
4
5#include "mim/util/dl.h"
6#include "mim/util/sys.h"
7
8namespace mim {
9
11 : log_(flags_)
12 , world_(this) {
13 // prepend empty path
14 search_paths_.emplace_front(fs::path{});
15
16 // paths from env
17 if (auto env_path = std::getenv("MIM_PLUGIN_PATH")) {
18 std::stringstream env_path_stream{env_path};
19 std::string sub_path;
20 while (std::getline(env_path_stream, sub_path, ':'))
21 add_search_path(sub_path);
22 }
23
24 // add path/to/mim.exe/../../lib/mim
25 if (auto path = sys::path_to_curr_exe()) add_search_path(path->parent_path().parent_path() / "lib" / "mim");
26
27 // add install path if different from above
28 if (auto install_path = fs::path{MIM_INSTALL_PREFIX} / "lib" / "mim"; fs::exists(install_path)) {
29 if (search_paths().empty() || !fs::equivalent(install_path, search_paths().back()))
30 add_search_path(std::move(install_path));
31 }
32
33 // all other user paths are placed just behind the first path (the empty path)
34 insert_ = ++search_paths_.begin();
35}
36
37const fs::path* Driver::add_import(fs::path path, Sym sym) {
38 for (auto p : import_paths())
39 if (fs::equivalent(p, path)) return nullptr;
40
41 import_path2sym_.emplace_back(std::pair(std::move(path), sym));
42 return &import_path2sym_.back().first;
43}
44
45void Driver::load(Sym name) {
46 ILOG("loading plugin: '{}'", name);
47
48 if (is_loaded(name)) {
49 WLOG("mim/plugin '{}' already loaded", name);
50 return;
51 }
52
53 auto handle = Plugin::Handle{nullptr, dl::close};
54 if (auto path = fs::path{name.view()}; path.is_absolute() && fs::is_regular_file(path))
55 handle.reset(dl::open(name.c_str()));
56 if (!handle) {
57 for (const auto& path : search_paths()) {
58 auto full_path = path / fmt("libmim_{}.{}", name, dl::extension);
59 std::error_code ignore;
60 if (bool reg_file = fs::is_regular_file(full_path, ignore); reg_file && !ignore) {
61 auto path_str = full_path.string();
62 if (handle.reset(dl::open(path_str.c_str())); handle) break;
63 }
64 if (handle) break;
65 }
66 }
67
68 if (!handle) error("cannot open plugin '{}'", name);
69
70 if (auto get_info = reinterpret_cast<decltype(&mim_get_plugin)>(dl::get(handle.get(), "mim_get_plugin"))) {
71 assert_emplace(plugins_, name, std::move(handle));
72 auto info = get_info();
73 // clang-format off
74 if (auto reg = info.register_normalizers) reg(normalizers_);
75 if (auto reg = info.register_stages) reg(phases_, passes_);
76 if (auto reg = info.register_backends) reg(backends_);
77 // clang-format on
78 } else {
79 error("mim/plugin has no 'mim_get_plugin()'");
80 }
81}
82
83void* Driver::get_fun_ptr(Sym plugin, const char* name) {
84 if (auto handle = lookup(plugins_, plugin)) return dl::get(handle->get(), name);
85 return nullptr;
86}
87
88} // namespace mim
void load(Sym name)
Definition driver.cpp:45
void add_search_path(fs::path path)
Definition driver.h:37
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:37
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:83
const auto & search_paths() const
Definition driver.h:36
#define MIM_INSTALL_PREFIX
Definition config.h:10
#define ILOG(...)
Definition log.h:91
#define WLOG(...)
Definition log.h:90
void * get(void *handle, const char *symbol_name)
Definition dl.cpp:36
void close(void *handle)
Definition dl.cpp:55
void * open(const char *filename)
Definition dl.cpp:17
static constexpr auto extension
Definition dl.h:8
std::optional< fs::path > path_to_curr_exe()
Yields std::nullopt if an error occurred.
Definition sys.cpp:28
Definition ast.h:14
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:110
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
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:163
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:125
mim::Plugin mim_get_plugin()
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:33