rucc-rules 0.3.4

The rewrite and lowering rule DSL compiler for the rucc C compiler.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! The matcher as Rust, for the compiler to link against.
//!
//! `spec/10-backend.md` section 10.2 asks for a generated automaton rather than a chain of
//! conditionals, and this is the half of that which leaves this crate. A build script reads a
//! rule file, builds the trie next door, and writes what this module produces into the build
//! directory of the crate that matches with it. Nothing here is a copy of anything: the rule
//! file is the only place the rules are written, and the table is regenerated whenever it
//! changes.
//!
//! # What comes out
//!
//! One Rust source file, holding the trie as an array of nodes, the rules as an array of
//! replacements, and one function per guard. It is data and not code, except for the guards,
//! which are the one part of a rule that has to be evaluated rather than looked up. The walk
//! over the table lives in the crate that includes the file, because the subject of a match
//! there is the compiler's own IR rather than a term, and because a walk written once is a
//! walk written once however many targets there are.
//!
//! The types the file names are the ones that crate defines, and it refers to them through
//! `super`, which is what makes the file includable and nothing else. That is the whole of the
//! contract between the two, and it is small on purpose.
//!
//! # Guards
//!
//! A guard is a condition on the constants a pattern matched, so it becomes a function of the
//! values the bindings hold. A binding that is not a constant at all makes the guard false
//! rather than an error, because a rule guarded by a claim about a number is a rule that does
//! not fire when the operand is not one.
//!
//! The language a guard may be written in is small and this module is where it ends. A head it
//! does not know is refused with the line it is on, rather than emitted and discovered as a
//! compile error in generated code, which is the sort of message nobody can act on.

use std::fmt::Write as _;

use crate::ast::{Rule, Term, TermKind};
use crate::error::Error;
use crate::matcher::{Matcher, Test};

/// The helpers a guard can call, and what each one needs emitted with it.
const HELPERS: &[(&str, &str)] = &[
    ("sign_extend", SIGN_EXTEND),
    ("zero_extend", ZERO_EXTEND),
    ("extract", EXTRACT),
    ("shifted", SHIFTED),
    ("low", LOW),
];

/// Turn a rule set and the trie it compiles into into Rust.
///
/// `source` is the rule file as it should be named in the generated file and in anything the
/// compiler says about a rule at run time, so it is the path a person could open rather than
/// wherever the build script happened to find it.
///
/// # Errors
///
/// A guard this module cannot compile, reported with the position of the term that was not
/// understood. Every other way a rule set can be wrong has been reported by the reader or by
/// the trie before anything gets here.
pub fn emit(source: &str, rules: &[Rule], matcher: &Matcher) -> Result<String, Vec<Error>> {
    let mut out = String::new();
    let mut errors = Vec::new();
    let mut wanted: Vec<&'static str> = Vec::new();

    let guards = compile_guards(source, rules, &mut wanted, &mut errors);
    if !errors.is_empty() {
        return Err(errors);
    }

    header(&mut out, source, rules, matcher);
    nodes(&mut out, matcher);
    lowerings(&mut out, source, rules, &guards);
    out.push_str(&guards.iter().flatten().map(String::as_str).collect::<String>());
    helpers(&mut out, &wanted);
    Ok(out)
}

/// The comment nobody reads until they have to, and the table itself.
fn header(out: &mut String, source: &str, rules: &[Rule], matcher: &Matcher) {
    let _ = write!(
        out,
        "\
// Generated from {source} by rucc-rules. Do not edit this file: edit the
// rule file and build again. It holds {} rules over {} trie nodes.
//
// The types are the ones the module that includes this file defines, and the walk over the
// table is there too. What is here is the table.

use super::{{Node, Piece, Rule, Table, Test}};

/// The rule file this table was built from, so that anything said about a rule can name a file
/// somebody can open.
pub const SOURCE: &str = {source:?};

/// The lowering rules of this target, as an automaton over their patterns.
pub static TABLE: Table = Table {{ source: SOURCE, nodes: NODES, rules: LOWERINGS }};
",
        rules.len(),
        matcher.nodes.len()
    );
}

/// The trie, one array entry per node, with node zero the root.
fn nodes(out: &mut String, matcher: &Matcher) {
    out.push_str(
        "\n/// The trie over the patterns. A node holds the tests to try in order, the branch\n\
         /// that takes anything, and the rule that ends here if one does.\nstatic NODES: \
         &[Node] = &[\n",
    );
    for (index, node) in matcher.nodes.iter().enumerate() {
        let _ = writeln!(out, "    // {index}");
        out.push_str("    Node {\n        tests: &[");
        for (test, next) in &node.tests {
            match test {
                Test::App { head, arity } => {
                    let _ = write!(
                        out,
                        "\n            (Test::App {{ head: {head:?}, arity: {arity} }}, {next}),"
                    );
                }
                Test::Int(value) => {
                    let _ = write!(out, "\n            (Test::Int({value}), {next}),");
                }
            }
        }
        if !node.tests.is_empty() {
            out.push_str("\n        ");
        }
        out.push_str("],\n");
        match &node.wildcard {
            Some((name, next)) => {
                let _ = writeln!(out, "        wildcard: Some(({name:?}, {next})),");
            }
            None => out.push_str("        wildcard: None,\n"),
        }
        match node.accept {
            Some(rule) => {
                let _ = writeln!(out, "        accept: Some({rule}),");
            }
            None => out.push_str("        accept: None,\n"),
        }
        out.push_str("    },\n");
    }
    out.push_str("];\n");
}

/// The rules, one array entry each, in the order the file writes them.
fn lowerings(out: &mut String, source: &str, rules: &[Rule], guards: &[Option<String>]) {
    out.push_str(
        "\n/// The rules, in the order the rule file writes them, which is the order the\n\
         /// `accept` of a trie node names.\nstatic LOWERINGS: &[Rule] = &[\n",
    );
    for (index, rule) in rules.iter().enumerate() {
        let pattern = rule.pattern.to_string();
        let _ = writeln!(out, "    // {source}:{}", rule.line);
        out.push_str("    Rule {\n");
        let _ = writeln!(out, "        pattern: {pattern:?},");
        out.push_str("        replacement: &[");
        let bound = bound_names(&rule.pattern);
        for piece in pieces(&rule.replacement, &bound) {
            let _ = write!(out, "\n            {piece},");
        }
        out.push_str("\n        ],\n");
        match guards[index] {
            Some(_) => {
                let _ = writeln!(out, "        guard: Some(guard_{index}),");
            }
            None => out.push_str("        guard: None,\n"),
        }
        let _ = writeln!(out, "        line: {},", rule.line);
        out.push_str("    },\n");
    }
    out.push_str("];\n");
}

/// The names a pattern binds, in the order the matcher binds them, which is the pre-order it
/// walks the subject in. A replacement names one of them and the table holds the position,
/// because a position is what the match has and a name is what the reader has.
fn bound_names(pattern: &Term) -> Vec<String> {
    let mut out = Vec::new();
    pattern.walk(&mut |term| {
        if let TermKind::Var(name) = &term.kind {
            out.push(name.clone());
        }
    });
    out
}

/// One replacement term, flattened into the pieces that build it, in pre-order.
fn pieces(term: &Term, bound: &[String]) -> Vec<String> {
    let mut out = Vec::new();
    push_pieces(term, bound, &mut out);
    out
}

fn push_pieces(term: &Term, bound: &[String], out: &mut Vec<String>) {
    match &term.kind {
        TermKind::Var(name) => {
            // The reader has already refused a replacement naming something the pattern never
            // bound, so there is a position for every name that reaches here.
            let index = bound.iter().position(|have| have == name).unwrap_or_default();
            out.push(format!("Piece::Var {{ name: {name:?}, index: {index} }}"));
        }
        TermKind::Int(value) => out.push(format!("Piece::Int({value})")),
        TermKind::App { head, args } => {
            out.push(format!("Piece::App {{ head: {head:?}, arity: {} }}", args.len()));
            for arg in args {
                push_pieces(arg, bound, out);
            }
        }
    }
}

/// One function per guarded rule, or nothing for a rule with no guard.
fn compile_guards(
    source: &str,
    rules: &[Rule],
    wanted: &mut Vec<&'static str>,
    errors: &mut Vec<Error>,
) -> Vec<Option<String>> {
    let mut out = Vec::with_capacity(rules.len());
    for (index, rule) in rules.iter().enumerate() {
        let Some(guard) = &rule.guard else {
            out.push(None);
            continue;
        };
        let bound = bound_names(&rule.pattern);
        let mut used = Vec::new();
        let condition = match condition(source, guard, &bound, wanted, &mut used) {
            Ok(text) => text,
            Err(error) => {
                errors.push(error);
                out.push(None);
                continue;
            }
        };
        let mut text = format!(
            "\n/// `{guard}`, which is the guard of the rule on line {}.\nfn guard_{index}(bound: \
             &[Option<i128>]) -> bool {{\n",
            rule.line
        );
        used.sort_unstable();
        used.dedup();
        for at in used {
            let _ = writeln!(
                text,
                "    // {}\n    let Some(Some(v{at})) = bound.get({at}).copied() else {{ return \
                 false }};",
                bound[at]
            );
        }
        let _ = writeln!(text, "    {}\n}}", bare(&condition));
        out.push(Some(text));
    }
    out
}

/// An expression without the parentheses that wrap the whole of it.
///
/// Every condition is emitted parenthesised, because an operand of one has to be. The outermost
/// one is nobody's operand, and Rust warns about the parentheses around it, which in a generated
/// file is a warning the reader of it can do nothing with.
fn bare(text: &str) -> &str {
    let Some(inner) = text.strip_prefix('(').and_then(|text| text.strip_suffix(')')) else {
        return text;
    };
    let mut depth = 0i32;
    for c in inner.chars() {
        match c {
            '(' => depth += 1,
            ')' => depth -= 1,
            _ => {}
        }
        // The pair that opened the string closed before the end of it, so the two ends are not
        // a pair and taking them off would be taking off two different people's parentheses.
        if depth < 0 {
            return text;
        }
    }
    inner
}

/// A guard as a Rust expression of type `bool`.
fn condition(
    source: &str,
    term: &Term,
    bound: &[String],
    wanted: &mut Vec<&'static str>,
    used: &mut Vec<usize>,
) -> Result<String, Error> {
    let TermKind::App { head, args } = &term.kind else {
        return Err(refused(source, term, "a guard is a condition, and this is not one"));
    };
    let arity = args.len();
    match (head.as_str(), arity) {
        ("and" | "or", 1..) => {
            let joint = if head == "and" { " && " } else { " || " };
            let mut parts = Vec::with_capacity(arity);
            for arg in args {
                parts.push(condition(source, arg, bound, wanted, used)?);
            }
            Ok(format!("({})", parts.join(joint)))
        }
        ("not", 1) => Ok(format!("!{}", condition(source, &args[0], bound, wanted, used)?)),
        ("=" | "!=" | "<" | "<=" | ">" | ">=", 2) => {
            let operator = if head == "=" { "==" } else { head.as_str() };
            let left = value(source, &args[0], bound, wanted, used)?;
            let right = value(source, &args[1], bound, wanted, used)?;
            Ok(format!("({left} {operator} {right})"))
        }
        _ => Err(refused(
            source,
            term,
            &format!(
                "`{head}` of {arity} is not a condition a guard can be compiled to. A guard is \
                 `and`, `or`, `not`, or a comparison of two numbers"
            ),
        )),
    }
}

/// A term inside a guard that stands for a number.
fn value(
    source: &str,
    term: &Term,
    bound: &[String],
    wanted: &mut Vec<&'static str>,
    used: &mut Vec<usize>,
) -> Result<String, Error> {
    match &term.kind {
        TermKind::Int(number) => Ok(format!("{number}")),
        TermKind::Var(name) => {
            // The reader has already refused a guard naming something the pattern never bound.
            let at = bound.iter().position(|have| have == name).unwrap_or_default();
            used.push(at);
            Ok(format!("v{at}"))
        }
        TermKind::App { head, args } => {
            let arity = args.len();
            match (head.as_str(), arity) {
                ("sign_extend" | "zero_extend" | "extract", 3) => {
                    let first = width(source, &args[0])?;
                    let second = width(source, &args[1])?;
                    let inner = value(source, &args[2], bound, wanted, used)?;
                    let name = match head.as_str() {
                        "sign_extend" => "sign_extend",
                        "zero_extend" => "zero_extend",
                        _ => "extract",
                    };
                    want(wanted, name);
                    Ok(format!("{name}({first}, {second}, {inner})"))
                }
                _ => Err(refused(
                    source,
                    term,
                    &format!(
                        "`{head}` of {arity} is not a number a guard can be compiled to. The \
                         ones that are are `sign_extend`, `zero_extend` and `extract`"
                    ),
                )),
            }
        }
    }
}

/// A width, which has to be written out rather than computed, because it is how many bits a
/// machine instruction has room for and not something a program is allowed to vary.
fn width(source: &str, term: &Term) -> Result<String, Error> {
    match &term.kind {
        TermKind::Int(number) if (0..=128).contains(number) => Ok(format!("{number}")),
        _ => Err(refused(source, term, "a width has to be a number from 0 to 128")),
    }
}

/// Remember a helper, and everything it is written in terms of.
fn want(wanted: &mut Vec<&'static str>, name: &'static str) {
    if wanted.contains(&name) {
        return;
    }
    wanted.push(name);
    match name {
        "sign_extend" => want(wanted, "shifted"),
        "zero_extend" | "extract" => want(wanted, "low"),
        _ => {}
    }
}

/// The helpers the guards used, in a fixed order so that the file does not move about between
/// builds for no reason.
fn helpers(out: &mut String, wanted: &[&str]) {
    for (name, text) in HELPERS {
        if wanted.contains(name) {
            out.push_str(text);
        }
    }
}

fn refused(source: &str, term: &Term, message: &str) -> Error {
    Error {
        path: source.to_owned(),
        line: term.line,
        column: term.column,
        message: message.to_owned(),
    }
}

const SIGN_EXTEND: &str = "
/// The low `from` bits of `value`, sign extended to `to` bits.
fn sign_extend(from: u32, to: u32, value: i128) -> i128 {
    shifted(to, shifted(from, value))
}
";

const ZERO_EXTEND: &str = "
/// The low `from` bits of `value`, read as a number and not sign extended.
fn zero_extend(from: u32, to: u32, value: i128) -> i128 {
    low(to, low(from, value))
}
";

const EXTRACT: &str = "
/// The bits from `hi` down to `lo` of `value`, read as a number.
fn extract(hi: u32, lo: u32, value: i128) -> i128 {
    if lo >= 128 || hi < lo {
        return 0;
    }
    low(hi - lo + 1, value >> lo)
}
";

const SHIFTED: &str = "
/// `value` read as a signed number that many bits wide.
fn shifted(bits: u32, value: i128) -> i128 {
    match 128u32.checked_sub(bits) {
        Some(room) if room > 0 => (value << room) >> room,
        _ => value,
    }
}
";

const LOW: &str = "
/// The low `bits` bits of `value`, read as a number.
fn low(bits: u32, value: i128) -> i128 {
    if bits >= 128 {
        return value;
    }
    #[allow(clippy::cast_possible_wrap)]
    let masked = (value as u128 & ((1u128 << bits) - 1)) as i128;
    masked
}
";

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse;

    fn built(text: &str) -> String {
        let rules = parse("rules/test.rules", text).expect("the rules read");
        let matcher = Matcher::build("rules/test.rules", &rules).expect("the matcher builds");
        emit("rules/test.rules", &rules, &matcher).expect("the table is emitted")
    }

    /// The shape of the file, which is what the module that includes it is written against.
    #[test]
    fn a_rule_set_comes_out_as_a_table_of_nodes_and_a_table_of_rules() {
        let out = built(
            "(rule (lower (add.i64 (value.i64 x) (value.i64 y)))\n\
             (x64.add_rr_64 x y)\n\
             (spec (= (bvadd x y) (result))))\n",
        );
        assert!(out.contains("use super::{Node, Piece, Rule, Table, Test};"), "{out}");
        assert!(out.contains("pub const SOURCE: &str = \"rules/test.rules\";"), "{out}");
        assert!(out.contains("(Test::App { head: \"add.i64\", arity: 2 }, 1),"), "{out}");
        assert!(out.contains("wildcard: Some((\"x\", 3)),"), "{out}");
        assert!(out.contains("accept: Some(0),"), "{out}");
        assert!(out.contains("Piece::App { head: \"x64.add_rr_64\", arity: 2 }"), "{out}");
        assert!(out.contains("Piece::Var { name: \"x\", index: 0 }"), "{out}");
        assert!(out.contains("Piece::Var { name: \"y\", index: 1 }"), "{out}");
        assert!(out.contains("guard: None,"), "{out}");
    }

    /// A guard becomes a function of the constants the pattern matched, and the helpers it
    /// calls come with it. A binding it reads that is not a constant makes it false, which is
    /// what the `let ... else` in it is for.
    #[test]
    fn a_guard_comes_out_as_a_function_of_the_bindings() {
        let out = built(
            "(rule (lower (shl.i64 (value.i64 x) (iconst.i64 k)))\n\
             (if (and (>= k 0) (< k 64)))\n\
             (x64.shl_ri_64 x k)\n\
             (spec (= (bvshl x k) (result))))\n",
        );
        assert!(out.contains("guard: Some(guard_0),"), "{out}");
        assert!(out.contains("fn guard_0(bound: &[Option<i128>]) -> bool {"), "{out}");
        assert!(
            out.contains("let Some(Some(v1)) = bound.get(1).copied() else { return false };"),
            "{out}"
        );
        assert!(out.contains("(v1 >= 0) && (v1 < 64)"), "{out}");
        // Nothing this guard does not use is emitted, because an unused function in a
        // generated file is a warning in the crate that includes it.
        assert!(!out.contains("fn sign_extend"), "{out}");
        assert!(!out.contains("fn low"), "{out}");
    }

    /// The immediate guard, which is the one that needs the arithmetic helpers, and which is
    /// what pulls `shifted` and `low` in behind them.
    #[test]
    fn a_guard_that_reads_bits_brings_the_helpers_it_needs() {
        let out = built(
            "(rule (lower (add.i64 (value.i64 x) (iconst.i64 k)))\n\
             (if (= k (sign_extend 32 64 (extract 31 0 k))))\n\
             (x64.add_ri_64 x k)\n\
             (spec (= (bvadd x k) (result))))\n",
        );
        assert!(out.contains("v1 == sign_extend(32, 64, extract(31, 0, v1))"), "{out}");
        assert!(out.contains("fn sign_extend(from: u32, to: u32, value: i128) -> i128 {"), "{out}");
        assert!(out.contains("fn shifted(bits: u32, value: i128) -> i128 {"), "{out}");
        assert!(out.contains("fn extract(hi: u32, lo: u32, value: i128) -> i128 {"), "{out}");
        assert!(out.contains("fn low(bits: u32, value: i128) -> i128 {"), "{out}");
        assert!(!out.contains("fn zero_extend"), "{out}");
    }

    /// A guard written in something this module does not compile is refused here, with the
    /// position of the term, rather than emitted and found later as a compile error in a
    /// generated file that nobody wrote.
    #[test]
    fn a_guard_nothing_can_be_made_of_is_refused_where_it_is_written() {
        let rules = parse(
            "rules/test.rules",
            "(rule (lower (add.i64 (value.i64 x) (iconst.i64 k)))\n\
             (if (fits_in_a_byte k))\n\
             (x64.add_ri_64 x k)\n\
             (spec (= (bvadd x k) (result))))\n",
        )
        .expect("the rules read");
        let matcher = Matcher::build("rules/test.rules", &rules).expect("the matcher builds");
        let errors = emit("rules/test.rules", &rules, &matcher).expect_err("the guard is refused");
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].line, 2);
        assert!(
            errors[0].message.contains("`fits_in_a_byte` of 1 is not a condition"),
            "{}",
            errors[0]
        );
    }
}