MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
gen.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string_view>
5
6#include <mim/driver.h>
7#include <mim/phase.h>
8
10
11namespace mim::plug::mach {
12
13class Gen;
14
15/// Gen::start, compiled once into `libmim_mach`; Gen reaches it via `GET_FUN_PTR`.
16extern "C" MIM_EXPORT void mim_mach_gen(Gen&);
17
18/// Writes the closed rules among the annexes of a backend's isel plugin as the C++ matchers and builders of a
19/// rules::Table in `mim::plug::<backend>::rules`, to `-X <isel>:o=<file>` or stdout.
20/// The isel plugin derives from it and states the machine facts of its backend.
21class Gen : public Phase {
22public:
23 /// The annexes of the isel plugin that Gen knows; `0` for one it does not have.
24 struct Annexes {
25 flags_t addr = 0; ///< `addr p`: the address `p` as an address operand, Want::Addr.
26 flags_t reg_imm = 0; ///< `reg_imm x`: Want::RegImm.
27 flags_t reg_sh = 0; ///< `reg_sh x`: Want::RegSh.
28 flags_t scaled = 0; ///< `scaled i`: Want::Scaled.
29 flags_t zx = 0; ///< `zx x`: Want::Zx of a meta variable, else the non-terminal Zx of a rhs or conversion.
30 flags_t imm = 0; ///< The guard `imm x`: Want::Imm.
31 flags_t lit = 0; ///< The guard `lit x`: a literal.
32 flags_t commutes; ///< The tuple of the operations whose two operands a rule also matches swapped.
33 flags_t formats; ///< The values of a family parameter of type `«2; Nat»`.
34 flags_t sizes; ///< The values of a family parameter of type `Nat`.
35 };
36
37 /// @p backend names the plugin whose instructions the rules of the plugin @p isel select.
40 std::string_view backend,
42 std::string_view isel,
45 : Phase(world, annex)
46 , backend_(backend)
47 , backend_id_(backend_id)
48 , isel_(isel)
49 , isel_id_(isel_id)
50 , annexes_(annexes) {}
51
52 void start() final { driver().GET_FUN_PTR("mach", mim_mach_gen)(*this); }
53
54 std::string_view backend() const { return backend_; }
55 plugin_t backend_id() const { return backend_id_; }
56 std::string_view isel() const { return isel_; }
57 plugin_t isel_id() const { return isel_id_; }
58 const Annexes& annexes() const { return annexes_; }
59
60 /// @name Machine Facts
61 ///@{
62 /// The cost of the instruction @p insn of the backend, which a rule's cost sums up.
63 virtual u32 cost(const App* insn) const = 0;
64 /// The operand whose register the result of the two-address instruction @p insn takes.
65 virtual std::optional<size_t> tied(const App*) const { return {}; }
66 /// What an operand of the backend's address type @p type is: Want::Base, Want::Index, or Want::Addr.
67 virtual std::optional<mach::rules::Want> classify(const Def*) const { return {}; }
68 ///@}
69
70private:
71 std::string_view backend_;
72 plugin_t backend_id_;
73 std::string_view isel_;
74 plugin_t isel_id_;
75 Annexes annexes_;
76};
77
78} // namespace mim::plug::mach
Base class for all Defs.
Definition def.h:285
flags_t annex() const
Definition phase.h:64
Phase(World &world, std::string name)
Definition phase.h:29
Driver & driver()
Definition phase.h:61
World & world()
Definition phase.h:60
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
Writes the closed rules among the annexes of a backend's isel plugin as the C++ matchers and builders...
Definition gen.h:21
virtual std::optional< mach::rules::Want > classify(const Def *) const
What an operand of the backend's address type type is: Want::Base, Want::Index, or Want::Addr.
Definition gen.h:67
flags_t sizes
The values of a family parameter of type Nat.
Definition gen.h:34
std::string_view backend() const
Definition gen.h:54
flags_t addr
addr p: the address p as an address operand, Want::Addr.
Definition gen.h:25
flags_t reg_sh
reg_sh x: Want::RegSh.
Definition gen.h:27
flags_t reg_imm
reg_imm x: Want::RegImm.
Definition gen.h:26
void start() final
Actual entry.
Definition gen.h:52
flags_t lit
The guard lit x: a literal.
Definition gen.h:31
plugin_t isel_id() const
Definition gen.h:57
flags_t commutes
The tuple of the operations whose two operands a rule also matches swapped.
Definition gen.h:32
virtual std::optional< size_t > tied(const App *) const
The operand whose register the result of the two-address instruction insn takes.
Definition gen.h:65
flags_t zx
zx x: Want::Zx of a meta variable, else the non-terminal Zx of a rhs or conversion.
Definition gen.h:29
std::string_view isel() const
Definition gen.h:56
Gen(World &world, flags_t annex, std::string_view backend, plugin_t backend_id, std::string_view isel, plugin_t isel_id, Annexes annexes)
backend names the plugin whose instructions the rules of the plugin isel select.
Definition gen.h:38
flags_t scaled
scaled i: Want::Scaled.
Definition gen.h:28
plugin_t backend_id() const
Definition gen.h:55
virtual u32 cost(const App *insn) const =0
flags_t imm
The guard imm x: Want::Imm.
Definition gen.h:30
flags_t formats
The values of a family parameter of type «2; Nat».
Definition gen.h:33
const Annexes & annexes() const
Definition gen.h:58
The annexes of the isel plugin that Gen knows; 0 for one it does not have.
Definition gen.h:24
#define MIM_EXPORT
Definition config.h:21
The mach Plugin
Definition asm.h:15
void mim_mach_gen(Gen &)
Gen::start, compiled once into libmim_mach; Gen reaches it via GET_FUN_PTR.
Definition gen.cpp:1006
u64 flags_t
Definition types.h:39
u64 plugin_t
Definition types.h:40
uint32_t u32
Definition types.h:27