rucc_target/frame.rs
1//! The instructions a frame is made of.
2//!
3//! Design: `spec/10-backend.md` sections 10.7 and 10.8.
4//!
5//! A prologue pushes registers and moves the stack pointer, an epilogue puts them back, and a
6//! spill is a store and a reload is a load. None of that is chosen by a lowering rule, because
7//! none of it comes from anything the program wrote: it comes from how many registers the
8//! allocator ran out of and which of them the convention says a call leaves alone. So the
9//! opcodes are named here, which is the list of what a frame may produce, rather than only in
10//! [`crate::x86_64::INSTS`], which is the list of what the selector may produce and what the
11//! allocator therefore has to understand. The encoder reads both.
12//!
13//! Some names are in both lists, which is not a duplication of anything. A load is a load
14//! whether a rule selected it or a reload wrote it, and the instruction description in `INSTS`
15//! is what the allocator reads about the one the selector produced. What the two lists are is
16//! two answers to two questions, and an instruction being an answer to both is ordinary. What
17//! would be a mistake is a frame opcode nobody has described anywhere, which is why an entry
18//! here that is not in `INSTS` is still an entry the encoder has to know.
19//!
20//! Everything named here is a name rather than a variant, for the same reason
21//! `rucc_mir::Opcode` is: the crate that writes the prologue is a pipeline crate and
22//! `spec/10-backend.md` section 10.8 says a pipeline crate holds no target-specific code. It
23//! reads the names out of the target it was handed and writes them into the machine IR, and what
24//! any of them means is the encoder's answer against this same description.
25//!
26//! # What each one has to be
27//!
28//! The shapes are fixed, because the code that writes them writes one shape each. A push reads
29//! one register and a pop writes one. A move writes a register and reads another of the same
30//! class. A load writes a register and reads memory, a store reads a register and writes memory,
31//! and both reach the frame through the stack pointer with a constant added. The arithmetic on
32//! the stack pointer is two-address, so it writes the stack pointer and reads it back. A target
33//! whose instructions do not fit those shapes needs more than a table, and it will say so by not
34//! being able to fill this in.
35
36use crate::regs::RegClass;
37
38/// How a register of one class is moved between two registers and between a register and the
39/// frame.
40///
41/// Three names rather than one, because a machine that moves a general purpose register with
42/// `mov` moves a vector register with something else, and because a load and a store are
43/// different instructions on every machine here even when a dump writes them with the same
44/// mnemonic.
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct ClassMoves {
47 /// Writes the first register with what is in the second.
48 pub mov: &'static str,
49 /// Writes the register with what is in the frame.
50 pub load: &'static str,
51 /// Writes the frame with what is in the register.
52 pub store: &'static str,
53}
54
55/// How a prologue touches the stack as it takes a frame, on a target that can.
56///
57/// What `-fstack-clash-protection` asks for. An operating system leaves one page below every
58/// stack unmapped, so that a stack which grows into it faults rather than running into whatever
59/// is under it, and a frame larger than that page can move the stack pointer clean over it
60/// without ever writing to it. A prologue that takes the frame a page at a time and writes
61/// something to each page as it arrives cannot: the first page it reaches that is not mapped is
62/// the one that faults.
63///
64/// Two facts rather than one because neither implies the other. What touches a page is an
65/// instruction of the machine, and how far apart the pages are is what the kernel that runs the
66/// program left, and they are together here because a prologue that has one and not the other
67/// cannot write anything.
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub struct Probe {
70 /// Writes an address without changing what is there, which is what makes it safe to do to a
71 /// page a local has not been put in yet.
72 pub inst: &'static str,
73 /// How far apart the touches are, which is the page the operating system leaves below the
74 /// stack. A prologue never moves the stack pointer further than this without touching where
75 /// it landed.
76 pub interval: u32,
77}
78
79/// Every instruction a prologue, an epilogue, a spill or a reload is made of.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct FrameInsts {
82 /// What a rule file and the machine IR put in front of this target's opcodes, such as
83 /// `x64.`, which says which target a term belongs to and is not part of the opcode.
84 pub prefix: &'static str,
85 /// How a register of each class is moved, one for each class of the register file in the
86 /// order the file numbers them.
87 ///
88 /// Shorter than the file when the classes at the end are ones nothing spills. An x87 stack
89 /// register is one of those: the allocator is never given one to hand out, so nothing ever
90 /// asks how to move it, and a target that answered anyway would be writing down a guess.
91 pub classes: &'static [ClassMoves],
92 /// Puts a register on the stack and moves the stack pointer down by one word.
93 pub push: &'static str,
94 /// Takes a word off the stack into a register and moves the stack pointer back up.
95 pub pop: &'static str,
96 /// Adds a constant to the stack pointer, which is how an epilogue gives the frame back.
97 pub add: &'static str,
98 /// Takes a constant off the stack pointer, which is how a prologue takes the frame.
99 pub sub: &'static str,
100 /// Clears the low bits of the stack pointer, which is how a prologue forces an alignment
101 /// nothing else can give it.
102 pub align: &'static str,
103 /// Writes a register with an address rather than with what is at it, which is how an
104 /// epilogue puts the stack pointer back when the frame pointer is the only record of where
105 /// it was.
106 pub lea: &'static str,
107 /// Returns to the caller.
108 pub ret: &'static str,
109 /// Compares two general purpose registers and writes whether they differ into a third.
110 ///
111 /// The stack protector's check is the only thing that asks for this, and it is here rather
112 /// than left to a lowering rule because no rule ever sees the comparison: the two words being
113 /// compared are the canary the prologue wrote and the one the runtime still holds, and neither
114 /// of them is a value the program named.
115 pub differ: &'static str,
116 /// Calls the name it is given and reads no register.
117 ///
118 /// Here for the same reason, and used for the one call an epilogue can make, which is the one
119 /// a changed canary makes.
120 pub call: &'static str,
121 /// How a prologue touches a page of the stack, or `None` on a target where nothing can.
122 ///
123 /// See [`Probe`]. It is an option rather than a name because a target that has no such
124 /// instruction is a target where `-fstack-clash-protection` has to do nothing, and a name
125 /// standing for nothing is worse than an absence a caller has to look at.
126 pub probe: Option<Probe>,
127 /// What says an indirect branch may arrive at an address, or `None` on a target where nothing
128 /// does.
129 ///
130 /// What `-fcf-protection=branch` asks for, and an option for the same reason [`Self::probe`]
131 /// is: a target with no such instruction is one the flag cannot be honoured on, and the answer
132 /// there is to say so rather than to write a name that stands for nothing. A prologue puts one
133 /// at the top of every function, because a function's own address is the one address of it a
134 /// pointer can hold.
135 pub landing: Option<&'static str>,
136}
137
138impl FrameInsts {
139 /// How a register of that class is moved, or `None` for a class nothing spills.
140 #[must_use]
141 pub fn moves(&self, class: RegClass) -> Option<ClassMoves> {
142 self.classes.get(usize::from(class.number())).copied()
143 }
144}