Skip to main content

rucc_codegen/select/
x86_64.rs

1//! The x86-64 lowering table.
2//!
3//! Everything below the module comment is generated from `rules/x86-64.rules` by `rucc-rules`
4//! when this crate is built, and none of it is in the repository. The rule file is the only
5//! place the rules are written, which is what makes the table that is matched with and the
6//! table `rucc-verify` proves things about the same table.
7//!
8//! To read the rules, read the rule file. To read the automaton they compile into, build the
9//! crate and read `x86-64.rs` under the build directory, which is a file worth looking at once
10//! for the shape of it and never again.
11
12// A guard is emitted as the comparison the rule writes, so a rule saying a shift count is at
13// least zero and less than the width comes out as two comparisons rather than as a range. That
14// is deliberate: the generated line and the rule it came from should read the same, and the
15// suggestion to write it another way is advice for somebody editing code, which nobody here is.
16#![allow(clippy::manual_range_contains)]
17
18include!(concat!(env!("OUT_DIR"), "/x86-64.rs"));
19
20#[cfg(test)]
21mod tests {
22    use rucc_target::x86_64;
23
24    use super::TABLE;
25    use crate::select::Piece;
26
27    /// The prefix a rule file puts in front of a machine term, which is how it says which target
28    /// the term belongs to. It is not part of the opcode.
29    const PREFIX: &str = "x64.";
30
31    /// The two address constructors, which are not instructions. An addressing mode is an
32    /// argument to `lea` and to every memory operand after it, so it is written as a term in the
33    /// rule file and built by the selector into the instruction that takes it.
34    const AMODES: &[&str] =
35        &["amode_base_index_scale", "amode_index_scale", "amode_base", "amode_base_offset"];
36
37    /// Every head this table can write, in and under the replacements.
38    fn heads() -> Vec<&'static str> {
39        let mut found: Vec<&'static str> = TABLE
40            .rules
41            .iter()
42            .flat_map(|rule| rule.replacement.iter())
43            .filter_map(|piece| match piece {
44                Piece::App { head, .. } => Some(*head),
45                _ => None,
46            })
47            .collect();
48        found.sort_unstable();
49        found.dedup();
50        found
51    }
52
53    #[test]
54    fn every_instruction_the_table_writes_is_described() {
55        for head in heads() {
56            if AMODES.contains(&head) {
57                continue;
58            }
59            let opcode = head.strip_prefix(PREFIX).unwrap_or_else(|| {
60                panic!("{head} is neither an x86-64 term nor an addressing mode")
61            });
62            assert!(
63                x86_64::form(opcode).is_some(),
64                "{head} is selected by a rule and `rucc_target::x86_64` does not say what it \
65                 does with its operands"
66            );
67        }
68    }
69
70    /// The order the operands of a store are written in, which is the IR's and not a choice this
71    /// file makes.
72    ///
73    /// A pattern is matched against an instruction's operand list by position, so a rule that
74    /// names the address where the IR holds the value is a rule that stores to the value and
75    /// writes the address into memory. Nothing in a proof would catch it, because a proof is
76    /// about the rule file agreeing with itself, and both halves would be wrong in the same way.
77    /// `rucc_ir::Builder::store` takes the value first and the machine instruction takes it last,
78    /// which is why the two halves of one of these rules read in opposite orders.
79    #[test]
80    fn a_store_is_written_with_the_value_first_because_that_is_where_the_ir_keeps_it() {
81        let mut seen = 0;
82        for rule in TABLE.rules {
83            let Some(rest) = rule.pattern.strip_prefix("(store.") else { continue };
84            let (width, operands) = rest.split_once(' ').expect("a store takes operands");
85            assert!(
86                operands.starts_with(&format!("(value.{width} ")),
87                "line {}: {} binds something other than the value it is storing first",
88                rule.line,
89                rule.pattern
90            );
91            assert!(
92                operands.contains("(value.i64 "),
93                "line {}: {} reaches no address",
94                rule.line,
95                rule.pattern
96            );
97            seen += 1;
98        }
99        assert_eq!(seen, 16, "the store rules moved and this test did not follow them");
100    }
101
102    /// Every comparison can be made against a constant as well as against a register.
103    ///
104    /// Four comparisons in five in the corpus are against a constant, and without a rule for one
105    /// the constant is loaded into a register first, which is an instruction and a register the
106    /// machine never needed. A missing width or a missing condition would not fail anything else:
107    /// the register rule still matches, the output is still correct, and the only sign is code
108    /// that is one instruction longer in a place nobody is looking. So the two lists are counted
109    /// against each other here.
110    ///
111    /// What this cannot check is that the condition on the immediate rule is the right one, since
112    /// both halves of a wrong pair would be a consistent pair. That is what the `spec` clause is
113    /// for, and `rucc-verify` is what reads it.
114    #[test]
115    fn a_comparison_against_a_constant_is_written_for_every_one_against_a_register() {
116        let mut against_register = Vec::new();
117        let mut against_constant = Vec::new();
118        for rule in TABLE.rules {
119            let Some(rest) = rule.pattern.strip_prefix("(icmp_") else { continue };
120            let (condition, operands) = rest.split_once(".i1 ").expect("a comparison takes two");
121            let width = operands
122                .strip_prefix("(value.")
123                .and_then(|rest| rest.split_once(' '))
124                .map(|(width, _)| width)
125                .expect("a comparison reads a value first");
126            let named = format!("{condition}.{width}");
127            if operands.contains("(iconst.") {
128                // The constant is the second operand and never the first, because a comparison is
129                // not symmetric and the same condition on the other side means the opposite.
130                assert!(
131                    !operands.starts_with("(iconst."),
132                    "line {}: {} compares a constant against a value",
133                    rule.line,
134                    rule.pattern
135                );
136                against_constant.push(named);
137            } else {
138                against_register.push(named);
139            }
140        }
141        against_register.sort_unstable();
142        against_constant.sort_unstable();
143        assert_eq!(against_register, against_constant);
144        assert_eq!(against_register.len(), 40, "ten conditions at four widths");
145    }
146
147    /// The instructions the calling convention writes rather than a rule.
148    ///
149    /// Three kinds of them. Naming the register an argument arrived in, where an argument is
150    /// depends on its position in the signature and on the classification of every argument before
151    /// it, and a rule pattern sees one term and has no way to say any of that, so `crate::abi`
152    /// builds these from the convention instead. Calling a name is the same the other way round:
153    /// what its operands are is whatever the signature made them, and a call through an address is
154    /// the same instruction with one operand more.
155    ///
156    /// The second half of a value that comes back in two registers is the third. A return of one
157    /// value is a rule, because where that value goes depends on nothing but the value, which is
158    /// exactly what a rule can say. A return of two is not, because which register the second half
159    /// is in depends on the first half: the two register files are counted separately, so a
160    /// `double` and a `long` both come back at place zero and two `long`s do not.
161    const CONVENTION: &[&str] = &[
162        "arg_val_8",
163        "arg_val_16",
164        "arg_val_32",
165        "arg_val_64",
166        "arg_val_f32",
167        "arg_val_f64",
168        "arg_val_f128",
169        "ret_val2_8",
170        "ret_val2_16",
171        "ret_val2_32",
172        "ret_val2_64",
173        "ret_val2_f32",
174        "ret_val2_f64",
175        "ret_val2_f128",
176        "call",
177        "call_reg",
178    ];
179
180    /// The instructions the block layout writes rather than a rule.
181    ///
182    /// A rule sees one branch and the layout is about the order of every block in the function, so
183    /// which arm falls through is not something any pattern could say. That answer is what decides
184    /// whether the jump goes to the arm the condition is true for or the other one, and whether
185    /// there is a second jump after it, so all of these are written where the answer is.
186    ///
187    /// The comparisons are here for a second reason on top of that one. A branch on a comparison
188    /// is a comparison and a jump on the flags it set, and the flags are not a value: no pattern
189    /// could bind one and no `spec` clause could say anything about one. So the pair is put
190    /// together by the layout, out of a comparison a rule did select and the branch behind it,
191    /// which is the same argument `rucc_target::x86_64::Form::CmpSet` is one form rather than two
192    /// under.
193    const LAYOUT: &[&str] = &[
194        "test_rr_8",
195        "cmp_rr_8",
196        "cmp_rr_16",
197        "cmp_rr_32",
198        "cmp_rr_64",
199        "cmp_ri_8",
200        "cmp_ri_16",
201        "cmp_ri_32",
202        "cmp_ri_64",
203        "jcc_e",
204        "jcc_ne",
205        "jcc_l",
206        "jcc_le",
207        "jcc_g",
208        "jcc_ge",
209        "jcc_b",
210        "jcc_be",
211        "jcc_a",
212        "jcc_ae",
213        "jmp",
214    ];
215
216    /// The instructions the compare pass writes rather than a rule.
217    ///
218    /// The other half of the argument the comparisons above are here under. A rule selects a
219    /// comparison that keeps its answer in a byte, because that is the shape a value has. What is
220    /// left of one when the machine has already made the comparison is the byte with no comparison
221    /// in front of it, and there is no pattern for that: the term it would compute is the same term
222    /// the full comparison computes, and what makes the short one right is the instruction three
223    /// places back rather than anything about the value. So `crate::compare` writes them by name,
224    /// in place of a comparison it found was already made.
225    const COMPARE: &[&str] = &[
226        "set_e", "set_ne", "set_l", "set_le", "set_g", "set_ge", "set_b", "set_be", "set_a",
227        "set_ae",
228    ];
229
230    /// The instruction a computed `goto` is written as rather than a rule.
231    ///
232    /// The one branch `crate::lower` writes by name, and the one the block layout does not write
233    /// either. What it reads is the address, which a pattern could have bound, so it is not
234    /// exempt for the reason the branches above are. What no pattern can say is the rest of it:
235    /// how many arms the block has, which is every label of the function the program took the
236    /// address of, and a rule says what an instruction reads rather than where a block goes.
237    const LABELS: &[&str] = &["jmp_reg"];
238
239    /// The instruction the memory model writes rather than a rule.
240    ///
241    /// A barrier computes nothing, so there is no equality for the solver to discharge and no
242    /// pattern for a rule to be written as. What makes it the right answer is what the machine
243    /// promises about the order two other instructions become visible in, which is a claim about
244    /// the program around it rather than about any value. `crate::lower` writes it by name, at the
245    /// strongest ordering and nowhere else, and `crate::expand` says why the strongest is the only
246    /// one that costs anything here.
247    const BARRIER: &[&str] = &["mfence"];
248
249    /// The instruction a program stops on, which `crate::lower` writes rather than a rule.
250    ///
251    /// The first half of the barrier's reason and not the second. It computes nothing, so there is
252    /// no equality for the solver and no pattern for a rule. What makes it right is not a claim
253    /// about the order anything becomes visible in either: it is what the operating system does
254    /// with the fault, which is a fact about neither the values nor the program around it.
255    const STOP: &[&str] = &["ud2"];
256
257    /// The instructions that are a hint rather than a computation.
258    ///
259    /// The same shape of exemption the barrier gets and for a reason one step further out. A
260    /// barrier computes nothing and still has to be where it is, so there is at least a claim about
261    /// the program around it. A prefetch does not even have that: a machine that drops the whole
262    /// instruction runs the program correctly, because the only thing it can change is how long the
263    /// program takes.
264    ///
265    /// So there is no equality for the solver and no pattern for a rule, and which of the four a
266    /// program gets is decided by a number in the builtin's own arguments rather than by anything
267    /// about the value being prefetched. `crate::lower` writes them by name, out of the hint the IR
268    /// carries beside the instruction.
269    const HINT: &[&str] = &["prefetch_nta", "prefetch_t0", "prefetch_t1", "prefetch_t2"];
270
271    /// The instructions nothing but an `asm` statement asks for.
272    ///
273    /// One step further out again. A prefetch is a hint and is still something the compiler decides
274    /// to write, out of a builtin the program called. These are instructions the program wrote down
275    /// itself, by name, in a template, and nothing else in the language reaches them: there is no
276    /// builtin for either, no rule could match a term that produces one, and `crate::lower` writes
277    /// them only because [`rucc_target::x86_64::read`] found the name in a template and said which
278    /// opcode that is.
279    ///
280    /// `pause` is the hint a spin lock writes between two tries at the lock. `cpuid` is how a
281    /// program asks the processor what it can do, which there is no other way to ask, so every
282    /// program that takes a faster path on some machines than on others has one of these in it.
283    ///
284    /// The alignment is the third, and it is on this list rather than one of its own because it
285    /// meets the claim below outright: an instruction is exempt for this reason exactly when there
286    /// is nothing about it for a rule to name, and an opcode with no operands and no addressing mode
287    /// has nothing. It is not an instruction at all, which is more than the test asks and is the
288    /// reason no rule could have been written for it however the rule language grew.
289    const TEMPLATE: &[&str] = &["cpuid", "pause", "align"];
290
291    /// The instructions a template asks for that are right because of the line above them.
292    ///
293    /// These are exempt for the reason the ten bytes in [`COMPARE`] are, one step further out. A
294    /// rule selects a conditional move with its comparison in front of it, because that pair is the
295    /// shape a select has. The move on its own computes the same term and what makes it right is the
296    /// comparison somewhere behind it rather than anything about its own operands, so no pattern
297    /// could say what it means. The compare pass does not write one either, because it replaces a
298    /// comparison it found was already made and there is no earlier move here to replace: what
299    /// writes one is a program that put the comparison on one line of a template and the move on the
300    /// next, which is what zstd does to keep a bounds check from becoming a branch.
301    ///
302    /// So these have operands a rule could have named, unlike everything in [`TEMPLATE`], and they
303    /// are still not instructions a rule could have been written for.
304    const CONDITIONAL: &[&str] = &[
305        "cmov_e_16",
306        "cmov_e_32",
307        "cmov_e_64",
308        "cmov_ne_16",
309        "cmov_ne_32",
310        "cmov_ne_64",
311        "cmov_l_16",
312        "cmov_l_32",
313        "cmov_l_64",
314        "cmov_le_16",
315        "cmov_le_32",
316        "cmov_le_64",
317        "cmov_g_16",
318        "cmov_g_32",
319        "cmov_g_64",
320        "cmov_ge_16",
321        "cmov_ge_32",
322        "cmov_ge_64",
323        "cmov_b_16",
324        "cmov_b_32",
325        "cmov_b_64",
326        "cmov_be_16",
327        "cmov_be_32",
328        "cmov_be_64",
329        "cmov_a_16",
330        "cmov_a_32",
331        "cmov_a_64",
332        "cmov_ae_16",
333        "cmov_ae_32",
334        "cmov_ae_64",
335    ];
336
337    /// The instructions that produce two values, which is one more than a rule can name.
338    ///
339    /// A rule replaces a term with a term, and a term is the value one instruction computes. A
340    /// compare and exchange computes two: what it found at the address, and whether what it found
341    /// was what the program expected. There is no way to write the second one down in the rule
342    /// language, and inventing one would be inventing a language for a single instruction.
343    ///
344    /// So `crate::lower` writes it by name, the way it writes the barrier by name, and for a reason
345    /// that is about the rule language rather than about the machine. What the solver would have
346    /// been asked to prove about it is the easy half in any case: the arithmetic is a comparison
347    /// and a select, and what is hard is that the whole of it happens at once, which is the same
348    /// claim about the program around it that a barrier makes.
349    const ATOMIC: &[&str] = &["cmpxchg_8", "cmpxchg_16", "cmpxchg_32", "cmpxchg_64"];
350
351    /// The instructions whose operation is in the payload rather than in the head.
352    ///
353    /// A different exemption from the one above, on instructions that produce one value each and so
354    /// could be named by a rule if the rule had anything to match on. The head a pattern matches is
355    /// an opcode and a type, and every read modify write in the IR is the one opcode `atomic_rmw`.
356    /// Which of the thirteen operations it performs is carried beside the instruction rather than in
357    /// its name, so a pattern written for the exchange would match the add and the nand as well, and
358    /// the rule language has no way to look at what a rule matched to tell them apart.
359    ///
360    /// Giving each operation its own opcode is the other way out and is a worse trade: it is
361    /// thirteen opcodes at four widths where the IR wants one, and every pass that treats a read
362    /// modify write as one thing would then have a list of fifty two.
363    ///
364    /// So `crate::lower` writes these by name too. Three operations here, out of the thirteen: the
365    /// bitwise ones need a loop around a compare and exchange, which is control flow and so is built
366    /// before selection rather than during it, and they are the rest of `tamnd/rucc#311`.
367    const PAYLOAD: &[&str] =
368        &["xchg_8", "xchg_16", "xchg_32", "xchg_64", "xadd_8", "xadd_16", "xadd_32", "xadd_64"];
369
370    /// The instructions a frame writes rather than a rule.
371    ///
372    /// A prologue, an epilogue, a copy, a spill and a reload are not in the program. They are what
373    /// the allocator's answer costs, so they are written after it, by `crate::finish` reading
374    /// `x86_64::FRAME`. Six of the names that describes are already reachable from a rule, since a
375    /// prologue taking its frame is a subtraction and a spill is a store, and those are not here:
376    /// this is only the ones nothing else can reach.
377    const FRAME: &[&str] = &[
378        "push_64",
379        "pop_64",
380        "ret",
381        "mov_rr_64",
382        "movaps_rr",
383        // The touch a probing prologue puts on each page as it reaches it, the landing pad a
384        // prologue opens with, and the byte that does nothing which one reserves room with. All
385        // three are written by a frame and none on a command line that did not ask for it.
386        "or_mi_8",
387        "endbr64",
388        "nop",
389    ];
390
391    /// The instructions that reach the x87 stack, which are selected but not from here.
392    ///
393    /// A third kind of exemption, and the same reason all the way down the list.
394    ///
395    /// Every one of these is written by `crate::lower`, as part of a group rather than on its own.
396    /// What one of them leaves behind and the next picks up is the top of the x87 stack, which is
397    /// not a register anything allocates from and not a value a pattern could bind, so a rule
398    /// could neither match the middle of a group nor name what its replacement produced. And an
399    /// add here reads two addresses and writes a third, where one machine IR instruction carries
400    /// one addressing mode, so the group cannot be folded into a single opcode the way
401    /// `ucomisd_set_e` folds a comparison and a `setcc` either.
402    ///
403    /// So these are exempt for the reason `FRAME` is exempt rather than for the reason the list
404    /// below is, and they will stay exempt. Two of them are not reached by anything yet all the
405    /// same: `fsub_p` and `fdiv_p` are the other direction of the subtraction and the division,
406    /// which a code generator that pushed its operands the other way round would need and this one
407    /// does not. `fabs` is a third, since C spells that as a call to a library function.
408    const X87: &[&str] = &[
409        "fld_t",
410        "fstp_t",
411        "fld_s",
412        "fld_l",
413        "fild_l",
414        "fild_ll",
415        "fstp_s",
416        "fstp_l",
417        "fistp_l",
418        "fistp_ll",
419        "fnstcw",
420        "fldcw",
421        "fadd_p",
422        "fsub_p",
423        "fsubr_p",
424        "fmul_p",
425        "fdiv_p",
426        "fdivr_p",
427        "fchs",
428        "fabs",
429        "fucomip_set_a",
430        "fucomip_set_ae",
431        "fucomip_set_b",
432        "fucomip_set_be",
433        "fucomip_set_e",
434        "fucomip_set_ne",
435        "fucomip_set_p",
436        "fucomip_set_np",
437        "fucomip_set_e_and_np",
438        "fucomip_set_ne_or_p",
439    ];
440
441    /// The instructions no rule selects yet, because the rules that selected them were taken out.
442    ///
443    /// A different kind of exemption from the three above. Those say an instruction is written
444    /// somewhere a rule cannot reach and always will be. These say nobody reaches one at all right
445    /// now, and name the work that puts the rules back.
446    ///
447    /// The rules went out under `tamnd/rucc#368`. C promotes the operands of an arithmetic
448    /// operator to `int`, so a byte add and a two byte compare are things no C program asks the
449    /// back end for, and the rules at those widths sat proved and never selected over the whole
450    /// torture corpus at every optimization level. The width narrowing pass in `tamnd/rucc#375` is
451    /// what asks for them, and the rules come back with it.
452    ///
453    /// The descriptions stayed. A description says what an x86-64 instruction is, how long it is
454    /// and how it encodes, and that is true whether or not anything selects it. Taking them out
455    /// would be deleting a correct account of the machine to make a list shorter, and putting them
456    /// back is then a second thing to get right rather than a line of a rule file.
457    const NARROW: &[&str] = &[
458        // Three of the two address forms against an immediate. The `narrow` pass does write the
459        // shape, since `char c = a | 1;` narrows to a byte `or` against a byte constant, and no
460        // rule selects these yet: the constant goes into a register and the register with
461        // register rule takes it. Their `add`, `sub` and `and` siblings do have rules and are
462        // reached by the bitfield lowering, so this is six rules missing rather than a shape
463        // nothing writes.
464        "or_ri_8",
465        "or_ri_16",
466        "xor_ri_8",
467        "xor_ri_16",
468        "imul_ri_8",
469        "imul_ri_16",
470        // The divides, which are four instructions per width because the quotient and the
471        // remainder come out of one division in two different registers. `narrow` refuses these
472        // on purpose: the most negative byte over minus one is a defined hundred and twenty eight
473        // at four bytes and is the overflow that raises at one, so narrowing a division wants a
474        // range that rules the pair out and there is no range analysis yet.
475        "idiv_quo_8",
476        "idiv_quo_16",
477        "idiv_rem_8",
478        "idiv_rem_16",
479        "div_quo_8",
480        "div_quo_16",
481        "div_rem_8",
482        "div_rem_16",
483        // The shifts by a value, whose count is in `cl` whatever the width being shifted is. The
484        // same refusal for the same kind of reason: a count of twenty is a defined shift to zero
485        // at four bytes and is poison at one, so only a count that is a constant below the narrow
486        // width narrows, and that one selects the immediate forms which do have rules.
487        "shl_rcl_8",
488        "shl_rcl_16",
489        "shr_rcl_8",
490        "shr_rcl_16",
491        "sar_rcl_8",
492        "sar_rcl_16",
493    ];
494
495    /// The arithmetic that reads its second source out of memory, which [`crate::combine`] writes.
496    ///
497    /// A function rather than a list, for the reason the compare pass's exemption is taken from the
498    /// flag description rather than typed out: the pass already writes down which instructions it
499    /// can produce, and a second copy of that here would be a second opinion about one pass.
500    ///
501    /// No rule selects one of these because a rule matches a term and one of these is two terms, a
502    /// load and an arithmetic operation, put together. Whether they may be put together depends on
503    /// what is written between them and on whether anything else wants what the load read, and
504    /// neither is a fact about either term. That is the whole reason the pass exists and the module
505    /// documentation there says it at length.
506    fn combine() -> Vec<&'static str> {
507        crate::combine::FOLDS.iter().map(|fold| fold.into).collect()
508    }
509
510    #[test]
511    fn every_instruction_exempt_from_a_rule_is_one_a_frame_really_writes() {
512        // The same claim as the one about the convention, so that this list cannot grow an opcode
513        // that no frame asks for. In the order `x86_64::FRAME` names them, the copies after the
514        // return because there is one set of them per class the allocator may spill.
515        let frame = &x86_64::FRAME;
516        let mut written = vec![frame.push, frame.pop, frame.ret];
517        for class in frame.classes {
518            written.extend([class.mov, class.load, class.store]);
519        }
520        // And the touch a probing prologue puts on a page, which the target names as an option
521        // because a target with no instruction that writes an address without changing it takes
522        // every frame in one subtraction and has nothing to exempt.
523        written.extend(frame.probe.map(|probe| probe.inst));
524        // And the landing pad and the byte that does nothing, which are options for the same
525        // reason.
526        written.extend(frame.landing);
527        written.extend(frame.pad);
528        // What is left after the ones a rule already reaches, which are the loads and the stores of
529        // both register files, since those are the same instructions a program's own reads and
530        // writes of memory are. The vector pair joined them with the rules for a quad float, and a
531        // spill of one is now the same instruction as a program reading a `_Float128` variable.
532        written.retain(|opcode| !heads().contains(&format!("{PREFIX}{opcode}").as_str()));
533        assert_eq!(written, FRAME);
534    }
535
536    #[test]
537    fn every_instruction_exempt_from_a_rule_is_one_the_convention_really_writes() {
538        // An exemption list that nothing checks is a hole, since an opcode dropped into it stops
539        // being covered by either direction of the pinning. These are the ones `crate::abi` can
540        // name, at the four integer widths and the two float formats it has names for an
541        // argument in, and no others.
542        let strip = |head: &'static str| head.strip_prefix(PREFIX).expect("an x86-64 term");
543        let named = |ty| strip(crate::abi::head_of(ty).expect("every width the pseudos cover"));
544        // The second half of a pair at place one, which is the place a rule cannot name. The first
545        // half at place zero is `ret_val_*` and is reached by a rule, so it is not on this list.
546        let second = |ty| strip(crate::abi::ret_of(ty, 1).expect("every width the pseudos cover"));
547        let widths = || {
548            [8, 16, 32, 64].into_iter().map(rucc_ir::Type::int).chain(
549                [rucc_ir::Float::F32, rucc_ir::Float::F64, rucc_ir::Float::F128]
550                    .map(rucc_ir::Type::float),
551            )
552        };
553        let written: Vec<&str> = widths()
554            .map(named)
555            .chain(widths().map(second))
556            .chain([strip(crate::abi::CALL), strip(crate::abi::CALL_REG)])
557            .collect();
558        assert_eq!(written, CONVENTION);
559    }
560
561    /// The same claim about the block layout's list, which is longer than it looks.
562    ///
563    /// A name here that the layout does not write is an opcode exempted from needing a rule and
564    /// reached by nothing, and a name the layout writes that is not here is a failing test in
565    /// `every_described_instruction_is_reachable_from_a_rule` with a misleading message. Both are
566    /// avoided by taking the list from `rucc_target::x86_64::BRANCH` rather than believing it.
567    #[test]
568    fn every_instruction_exempt_from_a_rule_is_one_the_block_layout_really_writes() {
569        let branch = &x86_64::BRANCH;
570        // Eighty entries name sixteen instructions between them, so this is a set rather than a
571        // list and both sides are sorted before they are held against each other. What the order
572        // of the list itself is for is reading it.
573        let mut written: Vec<&str> = vec![branch.test, branch.jump];
574        written.extend(branch.fused.iter().map(|fusion| fusion.cmp));
575        written.extend(branch.fused.iter().flat_map(|fusion| [fusion.if_true, fusion.if_false]));
576        written.sort_unstable();
577        written.dedup();
578        let mut exempt = LAYOUT.to_vec();
579        exempt.sort_unstable();
580        assert_eq!(written, exempt);
581    }
582
583    /// The same claim about the compare pass. What it writes is what the flag description says is
584    /// left of a comparison, so the exemption is taken from that rather than typed out twice, and
585    /// an entry added there without a rule to go with it shows up here rather than in a build that
586    /// fails somewhere else.
587    #[test]
588    fn every_instruction_exempt_from_a_rule_is_one_the_compare_pass_really_writes() {
589        let mut written: Vec<&str> =
590            x86_64::FLAGS.compares.iter().filter_map(|entry| entry.kept).collect();
591        written.sort_unstable();
592        written.dedup();
593        let mut exempt = COMPARE.to_vec();
594        exempt.sort_unstable();
595        assert_eq!(written, exempt);
596    }
597
598    /// And the same claim about the one the lowering writes, held against the name the target gave
599    /// it rather than against the spelling written above.
600    #[test]
601    fn the_instruction_a_computed_goto_is_exempt_for_is_the_one_the_target_names() {
602        assert_eq!(LABELS, [x86_64::BRANCH.indirect]);
603    }
604
605    #[test]
606    fn every_described_instruction_is_reachable_from_a_rule() {
607        let written = heads();
608        let combine = combine();
609        for &(opcode, _) in x86_64::INSTS {
610            if combine.contains(&opcode) {
611                continue;
612            }
613            if CONVENTION.contains(&opcode) || LAYOUT.contains(&opcode) || FRAME.contains(&opcode) {
614                continue;
615            }
616            if NARROW.contains(&opcode) || BARRIER.contains(&opcode) || X87.contains(&opcode) {
617                continue;
618            }
619            if ATOMIC.contains(&opcode) || PAYLOAD.contains(&opcode) || HINT.contains(&opcode) {
620                continue;
621            }
622            if CONDITIONAL.contains(&opcode) {
623                continue;
624            }
625            if COMPARE.contains(&opcode) || TEMPLATE.contains(&opcode) {
626                continue;
627            }
628            if LABELS.contains(&opcode) || STOP.contains(&opcode) {
629                continue;
630            }
631            let head = format!("{PREFIX}{opcode}");
632            assert!(
633                written.contains(&head.as_str()),
634                "{opcode} is described and no rule in {} selects it",
635                TABLE.source
636            );
637        }
638    }
639
640    /// The same claim about the barrier as the ones above make about the convention and the frame:
641    /// the list holds instructions this target really describes, and holds only the ones that have
642    /// no operands, since an instruction with an operand is one a rule could have been written for.
643    #[test]
644    fn every_instruction_exempt_from_a_rule_is_one_the_memory_model_really_writes() {
645        for &opcode in BARRIER {
646            let form = x86_64::form(opcode).expect("an instruction this target describes");
647            assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
648        }
649    }
650
651    /// The same claim about the instruction a program stops on, which is the barrier's shape
652    /// exactly: no operands, because an instruction with one is an instruction a rule could have
653    /// been written for, and no addressing mode either, because it is given nothing at all.
654    #[test]
655    fn the_instruction_exempt_from_a_rule_because_it_stops_the_program_is_bare() {
656        for &opcode in STOP {
657            let form = x86_64::form(opcode).expect("an instruction this target describes");
658            assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
659            assert!(!form.takes_mem(), "{opcode} is given an address and stopping needs none");
660        }
661    }
662
663    /// The same claim about the hints, with the one difference between them written down. A hint is
664    /// given an address and nothing else, so it has no operands for the reason a barrier has none
665    /// and it does carry an addressing mode, which is what a rule would have had to match on.
666    #[test]
667    fn every_instruction_exempt_from_a_rule_because_it_is_a_hint_is_given_only_an_address() {
668        for &opcode in HINT {
669            let form = x86_64::form(opcode).expect("an instruction this target describes");
670            assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
671            assert!(form.takes_mem(), "{opcode} is a hint about an address and is given none");
672        }
673    }
674
675    /// The same claim about the template list. An instruction is exempt for this reason exactly
676    /// when there is nothing about it for a rule to name, and there are two ways to have nothing.
677    /// No operands and no address, which is the hint. Or every operand fixed to one register by the
678    /// description, which is the question put to the processor: a rule names the operands of a term
679    /// and binds them to the values underneath it, and an operand that can be nothing but `rax` is
680    /// not a place a value goes. Either way the whole of the claim holds, which is that there was
681    /// nowhere else for the instruction to come from.
682    #[test]
683    fn every_instruction_exempt_from_a_rule_because_only_a_template_asks_for_it_is_bare() {
684        for &opcode in TEMPLATE {
685            let form = x86_64::form(opcode).expect("an instruction this target describes");
686            let fixed = form
687                .operands()
688                .iter()
689                .all(|desc| matches!(desc.constraint, rucc_target::Constraint::Fixed(_)));
690            assert!(fixed, "{opcode} has an operand a rule could name");
691            assert!(!form.takes_mem(), "{opcode} is given an address, so a rule could name it");
692        }
693    }
694
695    /// The same claim about the conditional moves, read off the flag description the way the compare
696    /// pass's list is taken from it rather than typed out twice. An instruction is exempt for this
697    /// reason exactly when it reads the condition state and leaves it as it found it, which is what
698    /// says the instruction in front of it is where its meaning comes from. One that wrote the state
699    /// as well would be one a pattern could match on its own.
700    #[test]
701    fn every_instruction_exempt_from_a_rule_because_a_comparison_gives_it_its_meaning_reads_one() {
702        for &opcode in CONDITIONAL {
703            x86_64::form(opcode).expect("an instruction this target describes");
704            assert!(
705                x86_64::FLAGS.reads(opcode).is_some(),
706                "{opcode} reads no comparison, so a rule could name it"
707            );
708            assert!(
709                !(x86_64::FLAGS.writes)(opcode),
710                "{opcode} writes the condition state, so a rule could name it"
711            );
712        }
713    }
714
715    /// The same claim about the atomic list, read off the thing that put the entry there: an
716    /// instruction is exempt for this reason exactly when it writes more than one value, and an
717    /// instruction that writes one is one a rule could have been written for.
718    #[test]
719    fn every_instruction_exempt_from_a_rule_is_one_that_writes_more_than_one_value() {
720        let written = heads();
721        for &opcode in ATOMIC {
722            let form = x86_64::form(opcode).expect("an instruction this target describes");
723            let writes = form.operands().iter().filter(|desc| desc.role.is_def()).count();
724            assert!(writes > 1, "{opcode} writes one value, so a rule could name it");
725            assert!(
726                !written.contains(&format!("{PREFIX}{opcode}").as_str()),
727                "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
728                TABLE.source
729            );
730        }
731    }
732
733    /// The same claim about the payload list, read off the thing that puts an entry there.
734    ///
735    /// Two halves. Each of these writes one value, which is what says the reason above is not the
736    /// reason here, so a list that grew to cover an instruction the atomic list should have had
737    /// fails. And there really is more than one operation behind the one IR opcode, which is the
738    /// whole of why a pattern cannot name any of them, and is a fact about the IR that would stop
739    /// being true if the operations were ever given opcodes of their own.
740    #[test]
741    fn every_instruction_exempt_because_its_operation_is_beside_it_writes_one_value() {
742        assert!(
743            rucc_ir::RmwOp::all().count() > 1,
744            "one operation per opcode would be a head a rule could match"
745        );
746        let written = heads();
747        for &opcode in PAYLOAD {
748            let form = x86_64::form(opcode).expect("an instruction this target describes");
749            let writes = form.operands().iter().filter(|desc| desc.role.is_def()).count();
750            assert_eq!(writes, 1, "{opcode} writes more than one value, so it is the other list's");
751            assert!(
752                !written.contains(&format!("{PREFIX}{opcode}").as_str()),
753                "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
754                TABLE.source
755            );
756        }
757    }
758
759    /// The staleness rule every list in this project is kept under, on the one list here whose
760    /// entries are meant to leave. A rule that starts selecting one of these is `tamnd/rucc#375`
761    /// arriving, and the entry goes with it. An entry naming an instruction nothing describes is a
762    /// misspelling, and it would sit here exempting nothing.
763    #[test]
764    fn an_instruction_a_rule_now_selects_is_off_the_list_of_the_ones_left_for_later() {
765        let written = heads();
766        for &opcode in NARROW {
767            let head = format!("{PREFIX}{opcode}");
768            assert!(
769                !written.contains(&head.as_str()),
770                "a rule in {} selects {opcode} now, so it is not waiting on tamnd/rucc#375",
771                TABLE.source
772            );
773            assert!(
774                x86_64::INSTS.iter().any(|&(described, _)| described == opcode),
775                "{opcode} is not an instruction anything describes"
776            );
777        }
778    }
779
780    /// The same staleness rule on the x87 pair, and one thing more that is particular to them.
781    ///
782    /// They are a pair. An instruction that pushes onto the x87 stack and nothing that pops off it
783    /// again would leave the stack one deeper than the function found it, which is not a mistake
784    /// the allocator or the block layout could catch, since neither of them knows the stack is
785    /// there. So the two arrive together and leave together, and that is what this says.
786    #[test]
787    fn the_x87_stack_is_reached_by_a_pair_and_by_nothing_else() {
788        let written = heads();
789        for &opcode in X87 {
790            assert!(
791                x86_64::INSTS.iter().any(|&(described, _)| described == opcode),
792                "{opcode} is not an instruction anything describes"
793            );
794            assert!(
795                !written.contains(&format!("{PREFIX}{opcode}").as_str()),
796                "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
797                TABLE.source
798            );
799        }
800        // One way onto the stack per format a value can be read from, one way off it per format a
801        // value can be written to, the control word pair that is neither, and the arithmetic. The
802        // count is here as well as in the target description because this list is what says none
803        // of them is reachable, and a name that arrived here without its partner would be a format
804        // this target can convert in one direction and not the other.
805        assert_eq!(X87.len(), 30, "twelve that move a value and eighteen that work on one");
806    }
807}