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, whether
33//! the amount is a constant or a register. A target whose instructions do not fit those shapes
34//! needs more than a table, and it will say so by not 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 /// Takes whatever is in a register off the stack pointer, which is how a function makes room
101 /// for an array whose size it does not know until it runs.
102 ///
103 /// The same shape as [`Self::sub`] and different in where the amount comes from, which is the
104 /// whole of the difference between the bytes a prologue takes and the bytes a variable length
105 /// array takes. A prologue knows its number when it is written and a declaration in the body
106 /// does not know it until the expression in the brackets has been worked out.
107 pub grow: &'static str,
108 /// Clears the low bits of the stack pointer, which is how a prologue forces an alignment
109 /// nothing else can give it.
110 pub align: &'static str,
111 /// Writes a constant into a general purpose register.
112 ///
113 /// The one thing a prologue has to do that is not about the stack pointer, and it is here for
114 /// a platform that hands the size of the frame to a routine rather than reaching the pages
115 /// itself. See [`crate::Chkstk`]. A rule file selects this same opcode for a constant the
116 /// program wrote, for the reason the header of a target's table gives: a prologue writing a
117 /// number into a register is the same instruction as an assignment, and the encoder should
118 /// not have two answers for it.
119 pub imm: &'static str,
120 /// Writes a register with an address rather than with what is at it, which is how an
121 /// epilogue puts the stack pointer back when the frame pointer is the only record of where
122 /// it was.
123 pub lea: &'static str,
124 /// Returns to the caller.
125 pub ret: &'static str,
126 /// Compares two general purpose registers and writes whether they differ into a third.
127 ///
128 /// The stack protector's check is the only thing that asks for this, and it is here rather
129 /// than left to a lowering rule because no rule ever sees the comparison: the two words being
130 /// compared are the canary the prologue wrote and the one the runtime still holds, and neither
131 /// of them is a value the program named.
132 pub differ: &'static str,
133 /// Compares two general purpose registers as unsigned numbers and writes whether the first is
134 /// above the second into a third.
135 ///
136 /// Here for the same reason [`Self::differ`] is, and asked for by the one loop that walks a
137 /// distance nothing knew when it was written, which is the pages a variable length array takes.
138 /// A prologue knows how many pages its own frame is and can stop when the stack pointer reaches
139 /// an address worked out in advance, so equality is enough for it. A declaration in the body
140 /// does not: the bytes arrive in a register, the last step down is a whole page whatever is
141 /// left, and the stack pointer lands at or past where it was going rather than on it.
142 ///
143 /// Unsigned because both registers hold addresses. A stack that has grown past the middle of
144 /// the address space is one where a signed comparison of two stack pointers says the wrong
145 /// thing, and nothing about a guard page cares which half of the space it is in.
146 pub above: &'static str,
147 /// Calls the name it is given and reads no register.
148 ///
149 /// Here for the same reason, and used for the one call an epilogue can make, which is the one
150 /// a changed canary makes.
151 pub call: &'static str,
152 /// How a prologue touches a page of the stack, or `None` on a target where nothing can.
153 ///
154 /// See [`Probe`]. It is an option rather than a name because a target that has no such
155 /// instruction is a target where `-fstack-clash-protection` has to do nothing, and a name
156 /// standing for nothing is worse than an absence a caller has to look at.
157 pub probe: Option<Probe>,
158 /// What says an indirect branch may arrive at an address, or `None` on a target where nothing
159 /// does.
160 ///
161 /// What `-fcf-protection=branch` asks for, and an option for the same reason [`Self::probe`]
162 /// is: a target with no such instruction is one the flag cannot be honoured on, and the answer
163 /// there is to say so rather than to write a name that stands for nothing. A prologue puts one
164 /// at the top of every function, because a function's own address is the one address of it a
165 /// pointer can hold, and one goes at the top of every label a program took the address of,
166 /// because a computed `goto` is an indirect branch and those are the addresses it arrives at.
167 pub landing: Option<&'static str>,
168 /// A byte that does nothing, or `None` on a target where nothing is written for the purpose.
169 ///
170 /// What `-fpatchable-function-entry=` reserves room with, and an option for the same reason
171 /// [`Self::landing`] is. The room is counted in bytes, so what is wanted is the shortest
172 /// instruction the machine has that does nothing rather than the shortest sequence that adds
173 /// up to the length: a patcher writes over the room from its start and wants a whole number of
174 /// places it could have started at.
175 pub pad: Option<&'static str>,
176}
177
178impl FrameInsts {
179 /// How a register of that class is moved, or `None` for a class nothing spills.
180 #[must_use]
181 pub fn moves(&self, class: RegClass) -> Option<ClassMoves> {
182 self.classes.get(usize::from(class.number())).copied()
183 }
184}