MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
plugin.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include <absl/container/btree_map.h>
7#include <absl/container/flat_hash_map.h>
8
9#include "mim/config.h"
10#include "mim/def.h"
11
12namespace mim {
13
14class Driver;
15class Stage;
16
17/// @name Plugin Interface
18///@{
19using Normalizers = absl::flat_hash_map<flags_t, NormalizeFn>;
20
21/// Maps an an axiom of a Stage to a function that creates one.
22using Flags2Stages = absl::flat_hash_map<flags_t, std::function<std::unique_ptr<Stage>(World&)>>;
23
24using Backends = absl::btree_map<std::string, void (*)(World&, std::ostream&)>;
25///@}
26
27extern "C" {
28/// Basic info and registration function pointer to be returned from a specific plugin.
29/// Use Driver to load such a plugin.
30struct Plugin {
31 using Handle = std::unique_ptr<void, void (*)(void*)>;
32
33 const char* plugin_name; ///< Name of the Plugin.
34
35 /// Callback for registering the mapping from axm ids to normalizer functions in the given @p normalizers map.
37 /// Callback for registering the Plugin's callbacks for Pass%es and Phase%s.
39 /// Callback for registering the mapping from backend names to emission functions in the given @p backends map.
41};
42
43/// @name Plugin Interface
44/// @see Plugin
45///@{
46/// To be implemented and exported by a plugin.
47/// @returns a filled Plugin.
49///@}
50}
51
52/// Holds info about an entity defined within a Plugin (called *Annex*).
53struct Annex {
54 Annex() = delete;
55
56 /// @name Mangling Plugin Name
57 ///@{
58 static constexpr size_t Max_Plugin_Size = 8;
59 static constexpr plugin_t Global_Plugin = 0xffff'ffff'ffff'0000_u64;
60
61 /// Mangles @p s into a dense 48-bit representation.
62 /// The layout is as follows:
63 /// ```
64 /// |---7--||---6--||---5--||---4--||---3--||---2--||---1--||---0--|
65 /// 7654321076543210765432107654321076543210765432107654321076543210
66 /// Char67Char66Char65Char64Char63Char62Char61Char60|---reserved---|
67 /// ```
68 /// The `reserved` part is used for the Axm::tag and the Axm::sub.
69 /// Each `Char6x` is 6-bit wide and hence a plugin name has at most Axm::Max_Plugin_Size = 8 chars.
70 /// It uses this encoding:
71 /// | `Char6` | ASCII |
72 /// |---------|---------|
73 /// | 1: | `_` |
74 /// | 2-27: | `a`-`z` |
75 /// | 28-53: | `A`-`Z` |
76 /// | 54-63: | `0`-`9` |
77 /// The 0 is special and marks the end of the name if the name has less than 8 chars.
78 /// @returns `std::nullopt` if encoding is not possible.
79 static std::optional<plugin_t> mangle(Sym plugin);
80
81 /// Reverts an Axm::mangle%d string to a Sym.
82 /// Ignores lower 16-bit of @p u.
83 static Sym demangle(Driver&, plugin_t plugin);
84
85 static std::tuple<Sym, Sym, Sym> split(Driver&, Sym);
86 ///@}
87
88 /// @name Annex Name
89 /// @anchor annex_name
90 /// Anatomy of an Annex name:
91 /// ```
92 /// %plugin.tag.sub
93 /// | 48 | 8 | 8 | <-- Number of bits per field.
94 /// ```
95 /// * Def::name() retrieves the full name as Sym.
96 /// * Def::flags() retrieves the full name as Axm::mangle%d 64-bit integer.
97 ///@{
98 /// Yields the `plugin` part of the name as integer.
99 /// It consists of 48 relevant bits that are returned in the highest 6 bytes of a 64-bit integer.
100 static plugin_t flags2plugin(flags_t f) { return f & Global_Plugin; }
101
102 /// Yields the `tag` part of the name as integer.
103 static tag_t flags2tag(flags_t f) { return tag_t((f & 0x0000'0000'0000'ff00_u64) >> 8_u64); }
104
105 /// Yields the `sub` part of the name as integer.
106 static sub_t flags2sub(flags_t f) { return sub_t(f & 0x0000'0000'0000'00ff_u64); }
107
108 /// Includes Axm::plugin() and Axm::tag() but **not** Axm::sub.
109 static flags_t flags2base(flags_t f) { return f & ~0xff_u64; }
110 ///@}
111
112 /// @name Helpers for Matching
113 /// These are set via template specialization.
114 ///@{
115 // clang-format off
116 template<class Id> static constexpr size_t Num = size_t(-1); ///< Number of Axm::sub%tags.
117 template<class Id> static constexpr flags_t Base = flags_t(-1); ///< @see Axm::base.
118 template<class Id> static consteval size_t num () { return Num <Id>; }
119 template<class Id> static consteval flags_t base() { return Base<Id>; }
120 // clang-format of
121 ///@}
122};
123
124} // namespace mim
Some "global" variables needed all over the place.
Definition driver.h:17
Common base for Phase and Pass.
Definition pass.h:26
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
#define MIM_EXPORT
Definition config.h:16
Definition ast.h:14
u8 sub_t
Definition types.h:48
u64 flags_t
Definition types.h:45
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
mim::Plugin mim_get_plugin()
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
static sub_t flags2sub(flags_t f)
Yields the sub part of the name as integer.
Definition plugin.h:106
static std::tuple< Sym, Sym, Sym > split(Driver &, Sym)
Definition plugin.cpp:58
Annex()=delete
static constexpr plugin_t Global_Plugin
Definition plugin.h:59
static plugin_t flags2plugin(flags_t f)
Definition plugin.h:100
static constexpr size_t Max_Plugin_Size
Definition plugin.h:58
static flags_t flags2base(flags_t f)
Includes Axm::plugin() and Axm::tag() but not Axm::sub.
Definition plugin.h:109
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axm::mangled string to a Sym.
Definition plugin.cpp:37
static std::optional< plugin_t > mangle(Sym plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9
static consteval size_t num()
Definition plugin.h:118
static constexpr size_t Num
Number of Axm::subtags.
Definition plugin.h:116
static consteval flags_t base()
Definition plugin.h:119
static constexpr flags_t Base
Definition plugin.h:117
static tag_t flags2tag(flags_t f)
Yields the tag part of the name as integer.
Definition plugin.h:103
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:30
const char * plugin_name
Name of the Plugin.
Definition plugin.h:33
void(* register_backends)(Backends &)
Callback for registering the mapping from backend names to emission functions in the given backends m...
Definition plugin.h:40
void(* register_stages)(Flags2Stages &)
Callback for registering the Plugin's callbacks for Passes and Phases.
Definition plugin.h:38
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:31
void(* register_normalizers)(Normalizers &)
Callback for registering the mapping from axm ids to normalizer functions in the given normalizers ma...
Definition plugin.h:36