MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <iterator>
3#include <numeric>
4#include <ranges>
5#include <vector>
6
8#include <fe/assert.h>
9
10#include "mim/axm.h"
11#include "mim/def.h"
12#include "mim/tuple.h"
13#include "mim/world.h"
14
15#include "mim/util/dbg.h"
16#include "mim/util/log.h"
17
20
23
24namespace mim::plug::regex {
25
26template<quant id>
27const Def* normalize_quant(const Def* type, const Def* callee, const Def* arg) {
28 auto& world = type->world();
29
30 // quantifiers are idempotent
31 if (Axm::isa(id, arg)) return arg;
32
33 if constexpr (id == quant::plus) {
34 // (\d?)+ and (\d*)+ == \d*
35 if (auto optional_app = Axm::isa(quant::optional, arg))
36 return world.call(quant::star, optional_app->arg());
37 else if (auto star_app = Axm::isa(quant::star, arg))
38 return arg;
39 } else if constexpr (id == quant::star) {
40 // (\d?)* and (\d+)* == \d*
41 if (auto quant_app = Axm::isa<quant>(arg)) return world.app(callee, quant_app->arg());
42 } else if constexpr (id == quant::optional) {
43 // (\d*)? and (\d+)? == \d*
44 if (auto star_app = Axm::isa(quant::star, arg))
45 return arg;
46 else if (auto plus_app = Axm::isa(quant::plus, arg))
47 return world.call(quant::star, plus_app->arg());
48 }
49
50 return {};
51}
52
53template<class ConjOrDisj>
55 assert(!args.empty());
56 auto& world = args.front()->world();
57 return std::accumulate(args.begin() + 1, args.end(), args.front(), [&world](const Def* lhs, const Def* rhs) {
58 return world.call<ConjOrDisj, false>(Defs{lhs, rhs});
59 });
60}
61
62const Def* normalize_conj(const Def* type, const Def* callee, const Def* arg) {
63 auto& world = type->world();
64 world.DLOG("conj {}:{} ({})", type, callee, arg);
65
66 if (auto a = Lit::isa(arg->arity())) {
67 switch (*a) {
68 case 0: return world.lit_tt();
69 case 1: return arg;
70 default:
71 if (auto args = detail::flatten_in_arg<conj>(arg); !args.empty())
72 return make_binary_tree<conj>(args);
73 else
74 return world.annex<empty>();
75 }
76 }
77
78 return {};
79}
80
81bool compare_re(const Def* lhs, const Def* rhs) {
82 auto lhs_range = Axm::isa<range>(lhs);
83 auto rhs_range = Axm::isa<range>(rhs);
84 // sort ranges by increasing lower bound
85 if (lhs_range && rhs_range) return Lit::as(lhs_range->arg()->proj(0)) < Lit::as(rhs_range->arg()->proj(0));
86 // ranges to the end
87 if (lhs_range) return false;
88 if (rhs_range) return true;
89
90 return lhs->gid() < rhs->gid(); // make irreflexive
91}
92
94 std::stable_sort(args.begin(), args.end(), &compare_re);
95 {
96 auto new_end = std::unique(args.begin(), args.end());
97 args.erase(new_end, args.end());
98 }
99}
100
101bool is_in_range(Range range, nat_t needle) { return needle >= range.first && needle <= range.second; }
102
103auto get_range(const Def* rng) -> Range {
104 auto rng_match = Axm::isa<range, false>(rng);
105 return {Lit::as<std::uint8_t>(rng_match->arg(0)), Lit::as<std::uint8_t>(rng_match->arg(1))};
106}
107
108struct app_range {
110 const Def* operator()(Range rng) { return w.call<range>(Defs{w.lit_i8(rng.first), w.lit_i8(rng.second)}); }
111};
112
113void merge_ranges(DefVec& args) {
114 auto ranges_begin = args.begin();
115 while (ranges_begin != args.end() && !Axm::isa<range>(*ranges_begin))
116 ranges_begin++;
117 if (ranges_begin == args.end()) return;
118
119 Ranges old_ranges;
120 auto& world = (*ranges_begin)->world();
121
122 std::transform(ranges_begin, args.end(), std::back_inserter(old_ranges), get_range);
123
124 auto new_ranges = automaton::merge_ranges(old_ranges, [&world](std::string_view msg) { world.DLOG("{}", msg); });
125
126 // invalidates ranges_begin
127 args.erase(ranges_begin, args.end());
128 std::transform(new_ranges.begin(), new_ranges.end(), std::back_inserter(args), app_range{world});
129
130 make_vector_unique(args);
131}
132
133template<cls A, cls B>
134bool equals_any(const Def* cls0, const Def* cls1) {
135 return (Axm::isa(A, cls0) && Axm::isa(B, cls1)) || (Axm::isa(A, cls1) && Axm::isa(B, cls0));
136}
137
138bool equals_any(const Def* lhs, const Def* rhs) {
139 auto check_arg_equiv = [](const Def* lhs, const Def* rhs) {
140 if (auto rng_lhs = Axm::isa<range>(lhs))
141 if (auto not_rhs = Axm::isa<not_>(rhs)) {
142 if (auto rng_rhs = Axm::isa<range>(not_rhs->arg())) return rng_lhs == rng_rhs;
143 }
144 return false;
145 };
146
147 return check_arg_equiv(lhs, rhs) || check_arg_equiv(rhs, lhs);
148}
149
150bool equals_any(Defs lhs, Defs negated_rhs) {
151 auto is_range = [](const Def* d) { return Axm::isa<range>(d); };
152 auto to_range = std::views::filter(is_range) | std::views::transform(get_range);
153 auto rhs_view = negated_rhs | to_range;
154
155 if (std::ranges::distance(rhs_view) != std::ranges::distance(negated_rhs)) return false;
156
157 return std::ranges::includes(lhs | to_range, rhs_view);
158}
159
160const Def* normalize_disj(const Def* type, const Def*, const Def* arg) {
161 auto& world = type->world();
162 if (auto a = Lit::isa(arg->arity())) {
163 switch (*a) {
164 case 0: return world.lit_ff();
165 case 1: return arg;
166 default:
167 auto new_args = detail::flatten_in_arg<disj>(arg);
168
169 const bool contains_any
170 = std::ranges::find_if(new_args, [](const Def* ax) -> bool { return Axm::isa<any>(ax); })
171 != new_args.end();
172 const bool contains_empty
173 = std::ranges::find_if(new_args, [](const Def* ax) -> bool { return Axm::isa<empty>(ax); })
174 != new_args.end();
175
176 auto make_any = [&world, contains_empty]() {
177 if (contains_empty)
178 // (any|) matches everything, including empty string
179 // don't normalize again, as we'd just run into this normalizer again..
180 return world.call<disj, false>(Defs{world.annex<any>(), world.annex<empty>()});
181 else
182 return world.annex<any>();
183 };
184
185 if (contains_any) return make_any();
186 make_vector_unique(new_args);
187 merge_ranges(new_args);
188
189 const Def* to_remove = nullptr;
190 for (const auto* cls0 : new_args) {
191 for (const auto* cls1 : new_args)
192 if (equals_any(cls0, cls1)) return make_any();
193
194 if (auto not_rhs = Axm::isa<not_>(cls0)) {
195 if (auto disj_rhs = Axm::isa<disj>(not_rhs->arg())) {
196 auto rngs = detail::flatten_in_arg<disj>(disj_rhs->arg());
197 make_vector_unique(rngs);
198 if (equals_any(new_args, rngs)) return make_any();
199 }
200 }
201 }
202
203 erase(new_args, to_remove);
204 world.DLOG("final ranges {}", fe::Join(new_args));
205
206 if (new_args.size() > 2) return make_binary_tree<disj>(new_args);
207 if (new_args.size() > 1) return world.call<disj, false>(new_args);
208 return new_args.back();
209 }
210 }
211 return {};
212}
213
214const Def* normalize_range(const Def* type, const Def* callee, const Def* arg) {
215 auto& world = type->world();
216 auto [lhs, rhs] = arg->projs<2>();
217
218 if (!lhs->isa<Var>() && !rhs->isa<Var>()) // before first PE.
219 if (lhs->as<Lit>()->get() > rhs->as<Lit>()->get()) return world.raw_app(type, callee, {rhs, lhs});
220
221 return {};
222}
223
224const Def* any_unwanted_for_not(const Def* arg) {
225 if (auto ax = Axm::isa<regex::range>(arg)) return nullptr;
226 if (auto ax = Axm::isa<regex::not_>(arg)) return nullptr;
227 if (auto ax = Axm::isa<regex::any>(arg)) return nullptr;
228 if (auto disj = Axm::isa<regex::disj>(arg)) {
229 for (const auto* disj_arg : disj->args())
230 if (auto ret = any_unwanted_for_not(disj_arg)) return ret;
231 return nullptr;
232 }
233 return arg;
234}
235
236const Def* normalize_not(const Def*, const Def* callee, const Def* arg) {
237 if (auto unwanted = any_unwanted_for_not(arg)) {
238 throw Error()
239 .error(arg->loc(),
240 "regex.not_ must only be used with regex.disj, regex.range, regex.any and regex.not_: {} {}", callee,
241 arg)
242 .error(unwanted->loc(), "found unwanted: {}", unwanted);
243 }
244 return {};
245}
246
248
249} // namespace mim::plug::regex
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:402
Loc loc() const
Definition def.h:557
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:282
virtual const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
Definition def.cpp:597
Error & error(Loc loc, std::format_string< Args... > s, Args &&... args)
Definition dbg.h:63
static std::optional< T > isa(const Def *def)
Definition def.h:878
T get() const
Definition def.h:865
static T as(const Def *def)
Definition def.h:884
A variable introduced by a binder (mutable).
Definition def.h:756
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:18
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
automaton::Range Range
Vector< Range > Ranges
std::pair< std::uint64_t, std::uint64_t > Range
std::optional< Range > merge_ranges(Range a, Range b) noexcept
The regex Plugin
Definition lower_regex.h:5
void merge_ranges(DefVec &args)
void make_vector_unique(DefVec &args)
auto get_range(const Def *rng) -> Range
bool is_in_range(Range range, nat_t needle)
const Def * normalize_conj(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_range(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_disj(const Def *type, const Def *, const Def *arg)
const Def * normalize_quant(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_not(const Def *, const Def *callee, const Def *arg)
bool compare_re(const Def *lhs, const Def *rhs)
const Def * make_binary_tree(Defs args)
const Def * any_unwanted_for_not(const Def *arg)
bool equals_any(const Def *cls0, const Def *cls1)
View< const Def * > Defs
Definition def.h:78
u64 nat_t
Definition types.h:37
Vector< const Def * > DefVec
Definition def.h:79
Vector< T, N, A >::size_type erase(Vector< T, N, A > &c, const U &value) noexcept
Definition vector.h:85
#define MIM_regex_NORMALIZER_IMPL
Definition autogen.h:107
const Def * operator()(Range rng)