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