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 /// The instructions the size directed peephole writes rather than a rule.
194 ///
195 /// [`crate::shorten`] turns a comparison of a register against zero into a test of the register
196 /// against itself, which asks the machine the same thing in one byte less, and an addition of
197 /// one into the instruction that adds one and says so in its opcode, which is another byte less.
198 /// No rule could select either. Whether the first says the same thing depends on the constant
199 /// the comparison carries and a pattern binds a value rather than reads a number out of one, and
200 /// whether the second does depends on what reads the carry behind it, which is not something a
201 /// pattern sees at all. The eight bit test is not here because the layout writes that one as
202 /// well and it is on the list below.
203 const PEEPHOLE: &[&str] = &[
204 "test_rr_16",
205 "test_rr_32",
206 "test_rr_64",
207 "inc_r_8",
208 "inc_r_16",
209 "inc_r_32",
210 "inc_r_64",
211 "dec_r_8",
212 "dec_r_16",
213 "dec_r_32",
214 "dec_r_64",
215 ];
216
217 const LAYOUT: &[&str] = &[
218 "test_rr_8",
219 "cmp_rr_8",
220 "cmp_rr_16",
221 "cmp_rr_32",
222 "cmp_rr_64",
223 "cmp_ri_8",
224 "cmp_ri_16",
225 "cmp_ri_32",
226 "cmp_ri_64",
227 "cmp_rm_8",
228 "cmp_rm_16",
229 "cmp_rm_32",
230 "cmp_rm_64",
231 "cmp_mi_8",
232 "cmp_mi_16",
233 "cmp_mi_32",
234 "cmp_mi_64",
235 "jcc_e",
236 "jcc_ne",
237 "jcc_l",
238 "jcc_le",
239 "jcc_g",
240 "jcc_ge",
241 "jcc_b",
242 "jcc_be",
243 "jcc_a",
244 "jcc_ae",
245 "jmp",
246 ];
247
248 /// The instructions the compare pass writes rather than a rule.
249 ///
250 /// The other half of the argument the comparisons above are here under. A rule selects a
251 /// comparison that keeps its answer in a byte, because that is the shape a value has. What is
252 /// left of one when the machine has already made the comparison is the byte with no comparison
253 /// in front of it, and there is no pattern for that: the term it would compute is the same term
254 /// the full comparison computes, and what makes the short one right is the instruction three
255 /// places back rather than anything about the value. So `crate::compare` writes them by name,
256 /// in place of a comparison it found was already made.
257 const COMPARE: &[&str] = &[
258 "set_e", "set_ne", "set_l", "set_le", "set_g", "set_ge", "set_b", "set_be", "set_a",
259 "set_ae",
260 ];
261
262 /// The instruction a computed `goto` is written as rather than a rule.
263 ///
264 /// The one branch `crate::lower` writes by name, and the one the block layout does not write
265 /// either. What it reads is the address, which a pattern could have bound, so it is not
266 /// exempt for the reason the branches above are. What no pattern can say is the rest of it:
267 /// how many arms the block has, which is every label of the function the program took the
268 /// address of, and a rule says what an instruction reads rather than where a block goes.
269 const LABELS: &[&str] = &["jmp_reg"];
270
271 /// The instruction the memory model writes rather than a rule.
272 ///
273 /// A barrier computes nothing, so there is no equality for the solver to discharge and no
274 /// pattern for a rule to be written as. What makes it the right answer is what the machine
275 /// promises about the order two other instructions become visible in, which is a claim about
276 /// the program around it rather than about any value. `crate::lower` writes it by name, at the
277 /// strongest ordering and nowhere else, and `crate::expand` says why the strongest is the only
278 /// one that costs anything here.
279 const BARRIER: &[&str] = &["mfence"];
280
281 /// The instruction a program stops on, which `crate::lower` writes rather than a rule.
282 ///
283 /// The first half of the barrier's reason and not the second. It computes nothing, so there is
284 /// no equality for the solver and no pattern for a rule. What makes it right is not a claim
285 /// about the order anything becomes visible in either: it is what the operating system does
286 /// with the fault, which is a fact about neither the values nor the program around it.
287 const STOP: &[&str] = &["ud2"];
288
289 /// The instructions that are a hint rather than a computation.
290 ///
291 /// The same shape of exemption the barrier gets and for a reason one step further out. A
292 /// barrier computes nothing and still has to be where it is, so there is at least a claim about
293 /// the program around it. A prefetch does not even have that: a machine that drops the whole
294 /// instruction runs the program correctly, because the only thing it can change is how long the
295 /// program takes.
296 ///
297 /// So there is no equality for the solver and no pattern for a rule, and which of the four a
298 /// program gets is decided by a number in the builtin's own arguments rather than by anything
299 /// about the value being prefetched. `crate::lower` writes them by name, out of the hint the IR
300 /// carries beside the instruction.
301 const HINT: &[&str] = &["prefetch_nta", "prefetch_t0", "prefetch_t1", "prefetch_t2"];
302
303 /// The instructions nothing but an `asm` statement asks for.
304 ///
305 /// One step further out again. A prefetch is a hint and is still something the compiler decides
306 /// to write, out of a builtin the program called. These are instructions the program wrote down
307 /// itself, by name, in a template, and nothing else in the language reaches them: there is no
308 /// builtin for either, no rule could match a term that produces one, and `crate::lower` writes
309 /// them only because [`rucc_target::x86_64::read`] found the name in a template and said which
310 /// opcode that is.
311 ///
312 /// `pause` is the hint a spin lock writes between two tries at the lock. `cpuid` is how a
313 /// program asks the processor what it can do, which there is no other way to ask, so every
314 /// program that takes a faster path on some machines than on others has one of these in it.
315 ///
316 /// The alignment is the third, and it is on this list rather than one of its own because it
317 /// meets the claim below outright: an instruction is exempt for this reason exactly when there
318 /// is nothing about it for a rule to name, and an opcode with no operands and no addressing mode
319 /// has nothing. It is not an instruction at all, which is more than the test asks and is the
320 /// reason no rule could have been written for it however the rule language grew.
321 ///
322 /// A byte out of a template is the fourth and is there for the same reason as the alignment,
323 /// one step further still: it is not an instruction, and what it holds is a byte the program
324 /// wrote out itself because its assembler was older than the instruction it wanted. There is
325 /// nothing for a rule to have said about a number a program handed the processor directly.
326 const TEMPLATE: &[&str] = &["cpuid", "pause", "align", "byte"];
327
328 /// The instructions a template asks for that are right because of the line above them.
329 ///
330 /// These are exempt for the reason the ten bytes in [`COMPARE`] are, one step further out. A
331 /// rule selects a conditional move with its comparison in front of it, because that pair is the
332 /// shape a select has. The move on its own computes the same term and what makes it right is the
333 /// comparison somewhere behind it rather than anything about its own operands, so no pattern
334 /// could say what it means. The compare pass does not write one either, because it replaces a
335 /// comparison it found was already made and there is no earlier move here to replace: what
336 /// writes one is a program that put the comparison on one line of a template and the move on the
337 /// next, which is what zstd does to keep a bounds check from becoming a branch.
338 ///
339 /// So these have operands a rule could have named, unlike everything in [`TEMPLATE`], and they
340 /// are still not instructions a rule could have been written for.
341 /// The add with carry and the subtract with borrow, which read a bit off the instruction in
342 /// front of them.
343 ///
344 /// Exempt one step further out again than [`CONDITIONAL`]. A conditional move reads the
345 /// condition state and leaves it alone, so what is missing from a rule that named one is the
346 /// comparison. These read it and write it both, and what is missing is worse than a comparison:
347 /// the bit they read is the carry out of an addition, and an addition in the IR is an addition
348 /// of a width with no carry out at all, so there is no term a rule could match that the bit is
349 /// a part of. A program gets one by writing both halves itself in a template, which is what
350 /// `add_ssaaaa` and `sub_ddmmss` in libgmp's `longlong.h` are. The form against a constant is
351 /// here for the same reason and is the same instruction with a zero where the second source is,
352 /// which `add_sssaaaa` writes for the top word of a number three words wide.
353 ///
354 /// What keeps the two halves together once they are two instructions in a block is not here. It
355 /// is `rucc_target::FlagInsts`, which the scheduler reads for exactly this, and the test below
356 /// checks the entry is there rather than trusting that somebody remembered.
357 const CARRY: &[&str] = &[
358 "adc_rr_8",
359 "adc_rr_16",
360 "adc_rr_32",
361 "adc_rr_64",
362 "sbb_rr_8",
363 "sbb_rr_16",
364 "sbb_rr_32",
365 "sbb_rr_64",
366 "adc_ri_8",
367 "adc_ri_16",
368 "adc_ri_32",
369 "adc_ri_64",
370 "sbb_ri_8",
371 "sbb_ri_16",
372 "sbb_ri_32",
373 "sbb_ri_64",
374 ];
375
376 const CONDITIONAL: &[&str] = &[
377 "cmov_e_16",
378 "cmov_e_32",
379 "cmov_e_64",
380 "cmov_ne_16",
381 "cmov_ne_32",
382 "cmov_ne_64",
383 "cmov_l_16",
384 "cmov_l_32",
385 "cmov_l_64",
386 "cmov_le_16",
387 "cmov_le_32",
388 "cmov_le_64",
389 "cmov_g_16",
390 "cmov_g_32",
391 "cmov_g_64",
392 "cmov_ge_16",
393 "cmov_ge_32",
394 "cmov_ge_64",
395 "cmov_b_16",
396 "cmov_b_32",
397 "cmov_b_64",
398 "cmov_be_16",
399 "cmov_be_32",
400 "cmov_be_64",
401 "cmov_a_16",
402 "cmov_a_32",
403 "cmov_a_64",
404 "cmov_ae_16",
405 "cmov_ae_32",
406 "cmov_ae_64",
407 ];
408
409 /// The instructions that look for a set bit, which a template asks for and nothing else does.
410 ///
411 /// These have a source and a destination a rule could have named, the way the conditional moves
412 /// above do, and the reason no rule names them is a different one again. It is not that their
413 /// meaning comes from the line in front of them: each of these says on its own exactly what it
414 /// computes. It is that [`crate::expand`] already answers the question they answer, out of
415 /// arithmetic every machine has, and it does that because what these do when the source is zero
416 /// is four different things on four families of processor. A rule that selected one would be a
417 /// rule whose answer depends on which machine ran it.
418 ///
419 /// So the only thing that reaches one is a program that wrote the name in a template, which is
420 /// what the libraries that were counting bits before there was a builtin for it all do.
421 /// `crate::lower` writes them for the reason it writes the three in [`TEMPLATE`], and they are
422 /// not on that list because they are not bare: a rule could have named these operands and the
423 /// claim that list makes would be false of them.
424 const SEARCH: &[&str] = &[
425 "bsf_16", "bsf_32", "bsf_64", "bsr_16", "bsr_32", "bsr_64", "lzcnt_32", "lzcnt_64",
426 "tzcnt_32", "tzcnt_64",
427 ];
428
429 /// The instruction that turns a register round, which a template asks for and nothing else does.
430 ///
431 /// The list above, one step simpler. A search is unselected because what it does with a source
432 /// of zero is not the same on every processor, so a rule that chose one would depend on what ran
433 /// it. A byte reversal has no such case: it means exactly one thing everywhere. What keeps it
434 /// off the rule set is a choice made once, in [`crate::expand`], which builds a reversal out of
435 /// shifts and masks so that the answer is the same on every target this compiler has rather than
436 /// good on the one that happens to have the instruction. tamnd/rucc#310 is where that trade is
437 /// written down, and the day a target grows its own reversal is the day to reopen it.
438 ///
439 /// So the only thing that reaches one is a program that wrote the name in a template, which is
440 /// what libgmp does in `gmp-impl.h` to put a limb the other way round.
441 const SWAP: &[&str] = &["bswap_32", "bswap_64"];
442
443 /// The multiply that keeps both halves of its product and the division that reads both halves
444 /// of its dividend, which a template asks for and nothing else does.
445 ///
446 /// A third reason again, and the plainest of the three. A search is unselected because its
447 /// answer depends on the processor and a reversal because a choice was made to build one out of
448 /// arithmetic. This one is unselected because there is nothing in the IR to select it from: a
449 /// multiply in C takes two values of a type and produces a value of that type, so the term a
450 /// rule would match on is the narrow product, and the wide product is not a term at all. A rule
451 /// that fired on the narrow one and wrote this would be writing an instruction that computes
452 /// twice as much as was asked for and leaves the rest in a register nobody asked about.
453 ///
454 /// So the only thing that reaches one is a program that wrote the name in a template, which is
455 /// what `umul_ppmm` in libgmp's `longlong.h` does, and what every library that is building
456 /// arithmetic out of limbs does somewhere.
457 ///
458 /// The division is the same claim upside down and is on this list because the reason is the same
459 /// one. A division in C divides a number by a number of its own width, so the term a rule would
460 /// match is the narrow one, and this compiler already has two opcodes for that: each of them
461 /// fills the high half of the dividend itself and then throws one of the two answers away. A
462 /// dividend the program filled both halves of is not a term the IR has, and `udiv_qrnnd` beside
463 /// the multiply in the same header is how long division a limb at a time is written.
464 const WIDE: &[&str] = &[
465 "mul_wide_16",
466 "mul_wide_32",
467 "mul_wide_64",
468 "imul_wide_16",
469 "imul_wide_32",
470 "imul_wide_64",
471 "div_wide_16",
472 "div_wide_32",
473 "div_wide_64",
474 "idiv_wide_16",
475 "idiv_wide_32",
476 "idiv_wide_64",
477 ];
478
479 /// The instructions that produce two values, which is one more than a rule can name.
480 ///
481 /// A rule replaces a term with a term, and a term is the value one instruction computes. A
482 /// compare and exchange computes two: what it found at the address, and whether what it found
483 /// was what the program expected. There is no way to write the second one down in the rule
484 /// language, and inventing one would be inventing a language for a single instruction.
485 ///
486 /// So `crate::lower` writes it by name, the way it writes the barrier by name, and for a reason
487 /// that is about the rule language rather than about the machine. What the solver would have
488 /// been asked to prove about it is the easy half in any case: the arithmetic is a comparison
489 /// and a select, and what is hard is that the whole of it happens at once, which is the same
490 /// claim about the program around it that a barrier makes.
491 const ATOMIC: &[&str] = &["cmpxchg_8", "cmpxchg_16", "cmpxchg_32", "cmpxchg_64"];
492
493 /// The instructions whose operation is in the payload rather than in the head.
494 ///
495 /// A different exemption from the one above, on instructions that produce one value each and so
496 /// could be named by a rule if the rule had anything to match on. The head a pattern matches is
497 /// an opcode and a type, and every read modify write in the IR is the one opcode `atomic_rmw`.
498 /// Which of the thirteen operations it performs is carried beside the instruction rather than in
499 /// its name, so a pattern written for the exchange would match the add and the nand as well, and
500 /// the rule language has no way to look at what a rule matched to tell them apart.
501 ///
502 /// Giving each operation its own opcode is the other way out and is a worse trade: it is
503 /// thirteen opcodes at four widths where the IR wants one, and every pass that treats a read
504 /// modify write as one thing would then have a list of fifty two.
505 ///
506 /// So `crate::lower` writes these by name too. Three operations here, out of the thirteen: the
507 /// bitwise ones need a loop around a compare and exchange, which is control flow and so is built
508 /// before selection rather than during it, and they are the rest of `tamnd/rucc#311`.
509 const PAYLOAD: &[&str] =
510 &["xchg_8", "xchg_16", "xchg_32", "xchg_64", "xadd_8", "xadd_16", "xadd_32", "xadd_64"];
511
512 /// The instructions a frame writes rather than a rule.
513 ///
514 /// A prologue, an epilogue, a copy, a spill and a reload are not in the program. They are what
515 /// the allocator's answer costs, so they are written after it, by `crate::finish` reading
516 /// `x86_64::FRAME`. Six of the names that describes are already reachable from a rule, since a
517 /// prologue taking its frame is a subtraction and a spill is a store, and those are not here:
518 /// this is only the ones nothing else can reach.
519 const FRAME: &[&str] = &[
520 "push_64",
521 "pop_64",
522 "ret",
523 "mov_rr_64",
524 "movaps_rr",
525 // The touch a probing prologue puts on each page as it reaches it, the landing pad a
526 // prologue opens with, and the byte that does nothing which one reserves room with. All
527 // three are written by a frame and none on a command line that did not ask for it.
528 "or_mi_8",
529 "endbr64",
530 "nop",
531 ];
532
533 /// The instructions that reach the x87 stack, which are selected but not from here.
534 ///
535 /// A third kind of exemption, and the same reason all the way down the list.
536 ///
537 /// Every one of these is written by `crate::lower`, as part of a group rather than on its own.
538 /// What one of them leaves behind and the next picks up is the top of the x87 stack, which is
539 /// not a register anything allocates from and not a value a pattern could bind, so a rule
540 /// could neither match the middle of a group nor name what its replacement produced. And an
541 /// add here reads two addresses and writes a third, where one machine IR instruction carries
542 /// one addressing mode, so the group cannot be folded into a single opcode the way
543 /// `ucomisd_set_e` folds a comparison and a `setcc` either.
544 ///
545 /// So these are exempt for the reason `FRAME` is exempt rather than for the reason the list
546 /// below is, and they will stay exempt. Two of them are not reached by anything yet all the
547 /// same: `fsub_p` and `fdiv_p` are the other direction of the subtraction and the division,
548 /// which a code generator that pushed its operands the other way round would need and this one
549 /// does not. `fabs` is a third, since C spells that as a call to a library function.
550 const X87: &[&str] = &[
551 "fld_t",
552 "fstp_t",
553 "fld_s",
554 "fld_l",
555 "fild_l",
556 "fild_ll",
557 "fstp_s",
558 "fstp_l",
559 "fistp_l",
560 "fistp_ll",
561 "fnstcw",
562 "fldcw",
563 "fadd_p",
564 "fsub_p",
565 "fsubr_p",
566 "fmul_p",
567 "fdiv_p",
568 "fdivr_p",
569 "fchs",
570 "fabs",
571 "fucomip_set_a",
572 "fucomip_set_ae",
573 "fucomip_set_b",
574 "fucomip_set_be",
575 "fucomip_set_e",
576 "fucomip_set_ne",
577 "fucomip_set_p",
578 "fucomip_set_np",
579 "fucomip_set_e_and_np",
580 "fucomip_set_ne_or_p",
581 ];
582
583 /// The instructions no rule selects yet, because the rules that selected them were taken out.
584 ///
585 /// A different kind of exemption from the three above. Those say an instruction is written
586 /// somewhere a rule cannot reach and always will be. These say nobody reaches one at all right
587 /// now, and name the work that puts the rules back.
588 ///
589 /// The rules went out under `tamnd/rucc#368`. C promotes the operands of an arithmetic
590 /// operator to `int`, so a byte add and a two byte compare are things no C program asks the
591 /// back end for, and the rules at those widths sat proved and never selected over the whole
592 /// torture corpus at every optimization level. The width narrowing pass in `tamnd/rucc#375` is
593 /// what asks for them, and the rules come back with it.
594 ///
595 /// The descriptions stayed. A description says what an x86-64 instruction is, how long it is
596 /// and how it encodes, and that is true whether or not anything selects it. Taking them out
597 /// would be deleting a correct account of the machine to make a list shorter, and putting them
598 /// back is then a second thing to get right rather than a line of a rule file.
599 const NARROW: &[&str] = &[
600 // Three of the two address forms against an immediate. The `narrow` pass does write the
601 // shape, since `char c = a | 1;` narrows to a byte `or` against a byte constant, and no
602 // rule selects these yet: the constant goes into a register and the register with
603 // register rule takes it. Their `add`, `sub` and `and` siblings do have rules and are
604 // reached by the bitfield lowering, so this is six rules missing rather than a shape
605 // nothing writes.
606 "or_ri_8",
607 "or_ri_16",
608 "xor_ri_8",
609 "xor_ri_16",
610 "imul_ri_8",
611 "imul_ri_16",
612 // The divides, which are four instructions per width because the quotient and the
613 // remainder come out of one division in two different registers. `narrow` refuses these
614 // on purpose: the most negative byte over minus one is a defined hundred and twenty eight
615 // at four bytes and is the overflow that raises at one, so narrowing a division wants a
616 // range that rules the pair out and there is no range analysis yet.
617 "idiv_quo_8",
618 "idiv_quo_16",
619 "idiv_rem_8",
620 "idiv_rem_16",
621 "div_quo_8",
622 "div_quo_16",
623 "div_rem_8",
624 "div_rem_16",
625 // The shifts by a value, whose count is in `cl` whatever the width being shifted is. The
626 // same refusal for the same kind of reason: a count of twenty is a defined shift to zero
627 // at four bytes and is poison at one, so only a count that is a constant below the narrow
628 // width narrows, and that one selects the immediate forms which do have rules.
629 "shl_rcl_8",
630 "shl_rcl_16",
631 "shr_rcl_8",
632 "shr_rcl_16",
633 "sar_rcl_8",
634 "sar_rcl_16",
635 ];
636
637 /// The arithmetic that reaches memory, which [`crate::combine`] writes: the forms that read a
638 /// source out of it and the forms that leave the answer in it.
639 ///
640 /// A function rather than a list, for the reason the compare pass's exemption is taken from the
641 /// flag description rather than typed out: the pass already writes down which instructions it
642 /// can produce, and a second copy of that here would be a second opinion about one pass.
643 ///
644 /// No rule selects one of these because a rule matches a term and one of these is two terms, a
645 /// load and an arithmetic operation, put together, or three where the answer goes back to
646 /// memory. Whether they may be put together depends on what is written between them and on
647 /// whether anything else wants what the load read, and neither is a fact about any of the
648 /// terms. That is the whole reason the pass exists and the module documentation there says it
649 /// at length.
650 fn combine() -> Vec<&'static str> {
651 let loads = crate::combine::FOLDS.iter().map(|fold| fold.into);
652 // And the instruction a load on the other side comes to, which for most rows is the one
653 // above and for a comparison is the condition the other way round.
654 let swapped = crate::combine::FOLDS.iter().filter_map(|fold| fold.swapped);
655 let stores = crate::combine::UPDATES.iter().map(|update| update.into);
656 let constants = crate::combine::BUMPS.iter().map(|bump| bump.into);
657 loads.chain(swapped).chain(stores).chain(constants).collect()
658 }
659
660 #[test]
661 fn every_instruction_exempt_from_a_rule_is_one_a_frame_really_writes() {
662 // The same claim as the one about the convention, so that this list cannot grow an opcode
663 // that no frame asks for. In the order `x86_64::FRAME` names them, the copies after the
664 // return because there is one set of them per class the allocator may spill.
665 let frame = &x86_64::FRAME;
666 let mut written = vec![frame.push, frame.pop, frame.ret];
667 for class in frame.classes {
668 written.extend([class.mov, class.load, class.store]);
669 }
670 // And the touch a probing prologue puts on a page, which the target names as an option
671 // because a target with no instruction that writes an address without changing it takes
672 // every frame in one subtraction and has nothing to exempt.
673 written.extend(frame.probe.map(|probe| probe.inst));
674 // And the landing pad and the byte that does nothing, which are options for the same
675 // reason.
676 written.extend(frame.landing);
677 written.extend(frame.pad);
678 // What is left after the ones a rule already reaches, which are the loads and the stores of
679 // both register files, since those are the same instructions a program's own reads and
680 // writes of memory are. The vector pair joined them with the rules for a quad float, and a
681 // spill of one is now the same instruction as a program reading a `_Float128` variable.
682 written.retain(|opcode| !heads().contains(&format!("{PREFIX}{opcode}").as_str()));
683 assert_eq!(written, FRAME);
684 }
685
686 #[test]
687 fn every_instruction_exempt_from_a_rule_is_one_the_convention_really_writes() {
688 // An exemption list that nothing checks is a hole, since an opcode dropped into it stops
689 // being covered by either direction of the pinning. These are the ones `crate::abi` can
690 // name, at the four integer widths and the two float formats it has names for an
691 // argument in, and no others.
692 let strip = |head: &'static str| head.strip_prefix(PREFIX).expect("an x86-64 term");
693 let named = |ty| strip(crate::abi::head_of(ty).expect("every width the pseudos cover"));
694 // The second half of a pair at place one, which is the place a rule cannot name. The first
695 // half at place zero is `ret_val_*` and is reached by a rule, so it is not on this list.
696 let second = |ty| strip(crate::abi::ret_of(ty, 1).expect("every width the pseudos cover"));
697 let widths = || {
698 [8, 16, 32, 64].into_iter().map(rucc_ir::Type::int).chain(
699 [rucc_ir::Float::F32, rucc_ir::Float::F64, rucc_ir::Float::F128]
700 .map(rucc_ir::Type::float),
701 )
702 };
703 let written: Vec<&str> = widths()
704 .map(named)
705 .chain(widths().map(second))
706 .chain([strip(crate::abi::CALL), strip(crate::abi::CALL_REG)])
707 .collect();
708 assert_eq!(written, CONVENTION);
709 }
710
711 /// The same claim about the block layout's list, which is longer than it looks.
712 ///
713 /// A name here that the layout does not write is an opcode exempted from needing a rule and
714 /// reached by nothing, and a name the layout writes that is not here is a failing test in
715 /// `every_described_instruction_is_reachable_from_a_rule` with a misleading message. Both are
716 /// avoided by taking the list from `rucc_target::x86_64::BRANCH` rather than believing it.
717 #[test]
718 fn every_instruction_exempt_from_a_rule_is_one_the_block_layout_really_writes() {
719 let branch = &x86_64::BRANCH;
720 // Eighty entries name sixteen instructions between them, so this is a set rather than a
721 // list and both sides are sorted before they are held against each other. What the order
722 // of the list itself is for is reading it.
723 let mut written: Vec<&str> = vec![branch.test, branch.jump];
724 written.extend(branch.fused.iter().map(|fusion| fusion.cmp));
725 written.extend(branch.fused.iter().flat_map(|fusion| [fusion.if_true, fusion.if_false]));
726 written.sort_unstable();
727 written.dedup();
728 let mut exempt = LAYOUT.to_vec();
729 exempt.sort_unstable();
730 assert_eq!(written, exempt);
731 }
732
733 /// The same claim about the compare pass. What it writes is what the flag description says is
734 /// left of a comparison, so the exemption is taken from that rather than typed out twice, and
735 /// an entry added there without a rule to go with it shows up here rather than in a build that
736 /// fails somewhere else.
737 #[test]
738 fn every_instruction_exempt_from_a_rule_is_one_the_compare_pass_really_writes() {
739 let mut written: Vec<&str> =
740 x86_64::FLAGS.compares.iter().filter_map(|entry| entry.kept).collect();
741 written.sort_unstable();
742 written.dedup();
743 let mut exempt = COMPARE.to_vec();
744 exempt.sort_unstable();
745 assert_eq!(written, exempt);
746 }
747
748 /// And the same claim about the one the lowering writes, held against the name the target gave
749 /// it rather than against the spelling written above.
750 #[test]
751 fn the_instruction_a_computed_goto_is_exempt_for_is_the_one_the_target_names() {
752 assert_eq!(LABELS, [x86_64::BRANCH.indirect]);
753 }
754
755 /// The rows of the constant table that take nothing yet are exactly the narrow ones waiting on
756 /// the width narrowing, so the day `NARROW` shrinks is the day this says so.
757 ///
758 /// `crate::combine::BUMPS` has a row per instruction this machine has, which is the whole five
759 /// operations at the whole four widths. Four of those instructions arrive out of a rule that is
760 /// not written yet, so four of the rows sit there taking nothing. That is a fact worth holding
761 /// rather than a thing to notice again later.
762 #[test]
763 fn the_constant_runs_that_take_nothing_are_the_ones_no_rule_selects_yet() {
764 let written = heads();
765 let mut waiting = Vec::new();
766 for bump in crate::combine::BUMPS {
767 if !written.contains(&format!("{PREFIX}{}", bump.from).as_str()) {
768 waiting.push(bump.from);
769 }
770 }
771 assert_eq!(waiting, ["or_ri_8", "or_ri_16", "xor_ri_8", "xor_ri_16"]);
772 for from in waiting {
773 assert!(NARROW.contains(&from), "{from} is unselected and is not on the list");
774 }
775 }
776
777 #[test]
778 fn every_described_instruction_is_reachable_from_a_rule() {
779 let written = heads();
780 let combine = combine();
781 for &(opcode, _) in x86_64::INSTS {
782 if combine.contains(&opcode) {
783 continue;
784 }
785 if CONVENTION.contains(&opcode) || LAYOUT.contains(&opcode) || FRAME.contains(&opcode) {
786 continue;
787 }
788 if PEEPHOLE.contains(&opcode) {
789 continue;
790 }
791 if NARROW.contains(&opcode) || BARRIER.contains(&opcode) || X87.contains(&opcode) {
792 continue;
793 }
794 if ATOMIC.contains(&opcode) || PAYLOAD.contains(&opcode) || HINT.contains(&opcode) {
795 continue;
796 }
797 if CONDITIONAL.contains(&opcode) {
798 continue;
799 }
800 if CARRY.contains(&opcode) {
801 continue;
802 }
803 if COMPARE.contains(&opcode) || TEMPLATE.contains(&opcode) {
804 continue;
805 }
806 if SEARCH.contains(&opcode) || SWAP.contains(&opcode) || WIDE.contains(&opcode) {
807 continue;
808 }
809 if LABELS.contains(&opcode) || STOP.contains(&opcode) {
810 continue;
811 }
812 let head = format!("{PREFIX}{opcode}");
813 assert!(
814 written.contains(&head.as_str()),
815 "{opcode} is described and no rule in {} selects it",
816 TABLE.source
817 );
818 }
819 }
820
821 /// The same claim about the peephole's list, which is a claim about the target's description
822 /// rather than about this crate: every name on it is one the target really has, and every one
823 /// of them is a shorter spelling the description names, which is what says the peephole is
824 /// where it comes from. A name on the list that the peephole could never write would be an
825 /// instruction nothing writes at all, and this test is what stops that sitting there unnoticed.
826 #[test]
827 fn every_instruction_exempt_from_a_rule_is_one_the_peephole_really_writes() {
828 let tests = x86_64::SHORT.testing.iter().map(|entry| entry.into);
829 let steps = x86_64::SHORT.stepping.iter().map(|entry| entry.into);
830 let shorter: Vec<&str> = tests.chain(steps).collect();
831 for &opcode in PEEPHOLE {
832 assert!(
833 x86_64::form(opcode).is_some(),
834 "{opcode} is not an instruction this describes"
835 );
836 assert!(shorter.contains(&opcode), "{opcode} is not one the peephole writes");
837 }
838 }
839
840 /// The same claim about the barrier as the ones above make about the convention and the frame:
841 /// the list holds instructions this target really describes, and holds only the ones that have
842 /// no operands, since an instruction with an operand is one a rule could have been written for.
843 #[test]
844 fn every_instruction_exempt_from_a_rule_is_one_the_memory_model_really_writes() {
845 for &opcode in BARRIER {
846 let form = x86_64::form(opcode).expect("an instruction this target describes");
847 assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
848 }
849 }
850
851 /// The same claim about the instruction a program stops on, which is the barrier's shape
852 /// exactly: no operands, because an instruction with one is an instruction a rule could have
853 /// been written for, and no addressing mode either, because it is given nothing at all.
854 #[test]
855 fn the_instruction_exempt_from_a_rule_because_it_stops_the_program_is_bare() {
856 for &opcode in STOP {
857 let form = x86_64::form(opcode).expect("an instruction this target describes");
858 assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
859 assert!(!form.takes_mem(), "{opcode} is given an address and stopping needs none");
860 }
861 }
862
863 /// The same claim about the hints, with the one difference between them written down. A hint is
864 /// given an address and nothing else, so it has no operands for the reason a barrier has none
865 /// and it does carry an addressing mode, which is what a rule would have had to match on.
866 #[test]
867 fn every_instruction_exempt_from_a_rule_because_it_is_a_hint_is_given_only_an_address() {
868 for &opcode in HINT {
869 let form = x86_64::form(opcode).expect("an instruction this target describes");
870 assert!(form.operands().is_empty(), "{opcode} has operands, so a rule could name it");
871 assert!(form.takes_mem(), "{opcode} is a hint about an address and is given none");
872 }
873 }
874
875 /// The same claim about the template list. An instruction is exempt for this reason exactly
876 /// when there is nothing about it for a rule to name, and there are two ways to have nothing.
877 /// No operands and no address, which is the hint. Or every operand fixed to one register by the
878 /// description, which is the question put to the processor: a rule names the operands of a term
879 /// and binds them to the values underneath it, and an operand that can be nothing but `rax` is
880 /// not a place a value goes. Either way the whole of the claim holds, which is that there was
881 /// nowhere else for the instruction to come from.
882 #[test]
883 fn every_instruction_exempt_from_a_rule_because_only_a_template_asks_for_it_is_bare() {
884 for &opcode in TEMPLATE {
885 let form = x86_64::form(opcode).expect("an instruction this target describes");
886 let fixed = form
887 .operands()
888 .iter()
889 .all(|desc| matches!(desc.constraint, rucc_target::Constraint::Fixed(_)));
890 assert!(fixed, "{opcode} has an operand a rule could name");
891 assert!(!form.takes_mem(), "{opcode} is given an address, so a rule could name it");
892 }
893 }
894
895 /// The same claim about the bit searches, read off the description that put them there and read
896 /// both ways round. An instruction is exempt for this reason exactly when the machine describes
897 /// it as a search, so the list cannot grow an opcode that is something else, and a search this
898 /// target grows later cannot be left off the list and quietly go unselected with nobody saying
899 /// why. Nothing in the rule set selects one, which is the other half of the reason and is what
900 /// the check above would have caught in any case.
901 #[test]
902 fn every_instruction_exempt_from_a_rule_because_only_a_template_searches_for_a_bit_is_one() {
903 let written = heads();
904 for &opcode in SEARCH {
905 let form = x86_64::form(opcode).expect("an instruction this target describes");
906 assert_eq!(form, x86_64::Form::Search, "{opcode} is not a search");
907 assert!(
908 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
909 "a rule in {} selects {opcode}, which only a template asks for",
910 TABLE.source
911 );
912 }
913 for &(opcode, form) in x86_64::INSTS {
914 if form == x86_64::Form::Search {
915 assert!(SEARCH.contains(&opcode), "{opcode} is a search and is not on the list");
916 }
917 }
918 }
919
920 /// The same claim about the byte reversal, read both ways round the way the searches are, and
921 /// with the one thing that is different about it checked as well: this is the instruction of its
922 /// shape that leaves the condition state alone, which is the whole reason it has a form rather
923 /// than being a unary operation, so a description that stopped saying that would stop being the
924 /// reason this list exists.
925 #[test]
926 fn every_instruction_exempt_from_a_rule_because_only_a_template_turns_a_register_round_is_one()
927 {
928 let written = heads();
929 for &opcode in SWAP {
930 let form = x86_64::form(opcode).expect("an instruction this target describes");
931 assert_eq!(form, x86_64::Form::Swap, "{opcode} is not a byte reversal");
932 assert!(
933 !(x86_64::FLAGS.writes)(opcode),
934 "{opcode} writes the condition state, so it is a unary operation after all"
935 );
936 assert!(
937 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
938 "a rule in {} selects {opcode}, which only a template asks for",
939 TABLE.source
940 );
941 }
942 for &(opcode, form) in x86_64::INSTS {
943 if form == x86_64::Form::Swap {
944 assert!(SWAP.contains(&opcode), "{opcode} is a reversal and is not on the list");
945 }
946 }
947 }
948
949 /// The same claim about the two that work on a pair of registers, read both ways round and with
950 /// the thing that puts them out of reach of a rule checked rather than asserted in prose: each
951 /// writes two registers, and a rule replaces a term with a term, so there is no way to say the
952 /// second answer in the rule language at all. That is the same bar the compare and exchange is
953 /// exempt at, and this list is separate from that one because the reason it is nobody's to select
954 /// is different: an atomic is written by name where it is needed, and nothing in this compiler
955 /// needs one of these.
956 #[test]
957 fn every_instruction_exempt_from_a_rule_because_only_a_template_wants_both_halves_writes_two() {
958 let written = heads();
959 let both = [x86_64::Form::MulWide, x86_64::Form::DivWide];
960 for &opcode in WIDE {
961 let form = x86_64::form(opcode).expect("an instruction this target describes");
962 assert!(both.contains(&form), "{opcode} works on one register rather than on a pair");
963 let defs = form.operands().iter().filter(|desc| desc.role.is_def()).count();
964 assert_eq!(defs, 2, "{opcode} writes {defs} registers and a pair takes two");
965 assert!(
966 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
967 "a rule in {} selects {opcode}, which only a template asks for",
968 TABLE.source
969 );
970 }
971 for &(opcode, form) in x86_64::INSTS {
972 if both.contains(&form) {
973 assert!(WIDE.contains(&opcode), "{opcode} works on a pair and is not on the list");
974 }
975 }
976 }
977
978 /// The same claim about the carry pair, and the one thing that has to be true of them that is
979 /// not true of anything else on any of these lists. An instruction here reads the condition
980 /// state and writes it, which is what makes it half of a pair and not a rewrite of its own, and
981 /// the scheduler will only keep it behind the instruction that set the bit if the target says
982 /// it reads one.
983 #[test]
984 fn every_instruction_exempt_from_a_rule_because_it_reads_a_carry_says_it_reads_the_state() {
985 let written = heads();
986 for &opcode in CARRY {
987 let form = x86_64::form(opcode).expect("an instruction this target describes");
988 let pair = matches!(form, x86_64::Form::AluCarry | x86_64::Form::AluCarryI);
989 assert!(pair, "{opcode} is not one of the pair");
990 assert_eq!(
991 x86_64::FLAGS.reads(opcode),
992 Some(rucc_target::Reads::Carry),
993 "{opcode} does not say it reads the carry, so the scheduler may move it"
994 );
995 assert!(
996 (x86_64::FLAGS.writes)(opcode),
997 "{opcode} is said to leave the condition state alone"
998 );
999 assert!(
1000 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
1001 "a rule in {} selects {opcode}, which only a template asks for",
1002 TABLE.source
1003 );
1004 }
1005 for &(opcode, form) in x86_64::INSTS {
1006 if matches!(form, x86_64::Form::AluCarry | x86_64::Form::AluCarryI) {
1007 assert!(CARRY.contains(&opcode), "{opcode} reads a carry and is not on the list");
1008 }
1009 }
1010 }
1011
1012 /// The same claim about the conditional moves, read off the flag description the way the compare
1013 /// pass's list is taken from it rather than typed out twice. An instruction is exempt for this
1014 /// reason exactly when it reads the condition state and leaves it as it found it, which is what
1015 /// says the instruction in front of it is where its meaning comes from. One that wrote the state
1016 /// as well would be one a pattern could match on its own.
1017 #[test]
1018 fn every_instruction_exempt_from_a_rule_because_a_comparison_gives_it_its_meaning_reads_one() {
1019 for &opcode in CONDITIONAL {
1020 x86_64::form(opcode).expect("an instruction this target describes");
1021 assert!(
1022 x86_64::FLAGS.reads(opcode).is_some(),
1023 "{opcode} reads no comparison, so a rule could name it"
1024 );
1025 assert!(
1026 !(x86_64::FLAGS.writes)(opcode),
1027 "{opcode} writes the condition state, so a rule could name it"
1028 );
1029 }
1030 }
1031
1032 /// The same claim about the atomic list, read off the thing that put the entry there: an
1033 /// instruction is exempt for this reason exactly when it writes more than one value, and an
1034 /// instruction that writes one is one a rule could have been written for.
1035 #[test]
1036 fn every_instruction_exempt_from_a_rule_is_one_that_writes_more_than_one_value() {
1037 let written = heads();
1038 for &opcode in ATOMIC {
1039 let form = x86_64::form(opcode).expect("an instruction this target describes");
1040 let writes = form.operands().iter().filter(|desc| desc.role.is_def()).count();
1041 assert!(writes > 1, "{opcode} writes one value, so a rule could name it");
1042 assert!(
1043 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
1044 "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
1045 TABLE.source
1046 );
1047 }
1048 }
1049
1050 /// The same claim about the payload list, read off the thing that puts an entry there.
1051 ///
1052 /// Two halves. Each of these writes one value, which is what says the reason above is not the
1053 /// reason here, so a list that grew to cover an instruction the atomic list should have had
1054 /// fails. And there really is more than one operation behind the one IR opcode, which is the
1055 /// whole of why a pattern cannot name any of them, and is a fact about the IR that would stop
1056 /// being true if the operations were ever given opcodes of their own.
1057 #[test]
1058 fn every_instruction_exempt_because_its_operation_is_beside_it_writes_one_value() {
1059 assert!(
1060 rucc_ir::RmwOp::all().count() > 1,
1061 "one operation per opcode would be a head a rule could match"
1062 );
1063 let written = heads();
1064 for &opcode in PAYLOAD {
1065 let form = x86_64::form(opcode).expect("an instruction this target describes");
1066 let writes = form.operands().iter().filter(|desc| desc.role.is_def()).count();
1067 assert_eq!(writes, 1, "{opcode} writes more than one value, so it is the other list's");
1068 assert!(
1069 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
1070 "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
1071 TABLE.source
1072 );
1073 }
1074 }
1075
1076 /// The staleness rule every list in this project is kept under, on the one list here whose
1077 /// entries are meant to leave. A rule that starts selecting one of these is `tamnd/rucc#375`
1078 /// arriving, and the entry goes with it. An entry naming an instruction nothing describes is a
1079 /// misspelling, and it would sit here exempting nothing.
1080 #[test]
1081 fn an_instruction_a_rule_now_selects_is_off_the_list_of_the_ones_left_for_later() {
1082 let written = heads();
1083 for &opcode in NARROW {
1084 let head = format!("{PREFIX}{opcode}");
1085 assert!(
1086 !written.contains(&head.as_str()),
1087 "a rule in {} selects {opcode} now, so it is not waiting on tamnd/rucc#375",
1088 TABLE.source
1089 );
1090 assert!(
1091 x86_64::INSTS.iter().any(|&(described, _)| described == opcode),
1092 "{opcode} is not an instruction anything describes"
1093 );
1094 }
1095 }
1096
1097 /// The same staleness rule on the x87 pair, and one thing more that is particular to them.
1098 ///
1099 /// They are a pair. An instruction that pushes onto the x87 stack and nothing that pops off it
1100 /// again would leave the stack one deeper than the function found it, which is not a mistake
1101 /// the allocator or the block layout could catch, since neither of them knows the stack is
1102 /// there. So the two arrive together and leave together, and that is what this says.
1103 #[test]
1104 fn the_x87_stack_is_reached_by_a_pair_and_by_nothing_else() {
1105 let written = heads();
1106 for &opcode in X87 {
1107 assert!(
1108 x86_64::INSTS.iter().any(|&(described, _)| described == opcode),
1109 "{opcode} is not an instruction anything describes"
1110 );
1111 assert!(
1112 !written.contains(&format!("{PREFIX}{opcode}").as_str()),
1113 "a rule in {} selects {opcode}, which `crate::lower` also writes by hand",
1114 TABLE.source
1115 );
1116 }
1117 // One way onto the stack per format a value can be read from, one way off it per format a
1118 // value can be written to, the control word pair that is neither, and the arithmetic. The
1119 // count is here as well as in the target description because this list is what says none
1120 // of them is reachable, and a name that arrived here without its partner would be a format
1121 // this target can convert in one direction and not the other.
1122 assert_eq!(X87.len(), 30, "twelve that move a value and eighteen that work on one");
1123 }
1124}