rucc_codegen/frame.rs
1//! The frame: what a function's stack looks like while it runs.
2//!
3//! Design: `spec/10-backend.md` section 10.7.
4//!
5//! This is worked out after register allocation and not before, because the largest area in most
6//! frames is the spill slots and nothing knows how many of those there are until the allocator has
7//! finished running out of registers. It is worked out from the rewritten function rather than
8//! from the assignment alone, because the rewrite is what decides which scratch registers a reload
9//! uses, and a scratch register a call preserves is one the prologue has to save.
10//!
11//! # What is in one
12//!
13//! Section 10.7 lists the areas and this is the order they are in, from the stack pointer upward,
14//! which is the order of increasing address on every machine here.
15//!
16//! ```text
17//! incoming stack arguments the caller wrote these and they are above everything
18//! return address the call instruction pushed it, on a machine that does
19//! saved frame pointer when the function keeps one
20//! saved general purpose regs pushed, one word each
21//! saved vector registers stored rather than pushed, since no machine here pushes one
22//! stack protector canary when the function has one, above everything a local reaches
23//! locals what an alloca becomes, widest alignment first
24//! spill slots one for every value the allocator ran out of registers for
25//! outgoing argument area at the bottom, because a call reads its stack arguments from
26//! the stack pointer upward
27//! ```
28//!
29//! Every offset reported here is from the stack pointer as it stands in the body of the function,
30//! which is after the prologue and before the epilogue. That is the one base register always
31//! available. A frame pointer is a second way to reach the same bytes and the prologue is what
32//! knows the distance between the two, so nothing here reports an offset from it. There are two
33//! exceptions and [`Frame::incoming`] is one of them, because the bytes it reports are the caller's
34//! rather than this function's, which is the one part of the picture a realigned frame loses sight
35//! of. It says which register it counted from. The other is a frame that grows, which is the next
36//! section and where the stack pointer stops being a base register at all.
37//!
38//! # Where the alignment comes from
39//!
40//! A call has to leave the stack pointer on a multiple of the convention's alignment, so a
41//! function's own frame is what puts it back: the call that reached this function pushed a return
42//! address and left the stack pointer one word off, and the prologue's pushes either fix that or
43//! make it worse depending on how many there are. The size the prologue subtracts is therefore not
44//! the size of the areas. It is whatever brings the stack pointer back to a multiple of the
45//! alignment given the pushes in front of it, which is the arithmetic in [`Frame::of`].
46//!
47//! # The red zone
48//!
49//! A leaf function may use the bytes below the stack pointer without moving it, which is what
50//! `red_zone` on a convention says and what makes a small leaf function's prologue and epilogue
51//! empty. Then the offsets are negative, which is why they are signed, and the areas are in the
52//! same order as ever, below the line rather than above it. Anything that calls, or is too big for
53//! the zone, or wants more alignment than the stack pointer has for free, moves the stack pointer.
54//!
55//! # Realignment
56//!
57//! A local wanting more alignment than a call leaves the stack pointer with cannot be placed by
58//! arithmetic, because nothing in the frame knows what the caller's stack pointer was a multiple
59//! of. The prologue has to force it, and forcing it destroys the only record of where the caller's
60//! stack was, so a realigned frame needs a frame pointer and the distance from the body's stack
61//! pointer to the incoming arguments stops being a constant. [`Frame::realign`] is where that is
62//! reported and it is why [`Frame::incoming`] answers from the frame pointer in such a frame and
63//! from the stack pointer in every other one.
64//!
65//! # Growing
66//!
67//! A variable length array is bytes the function takes off the stack pointer where the declaration
68//! stands, so in a function that has one the stack pointer is in a different place in the middle of
69//! the body than it was at the top of it. Every other offset in the frame was a distance from the
70//! stack pointer, and a distance from a register that moves is not a distance, so in a frame like
71//! this they are all distances from the frame pointer instead. That is what [`Layout::grows`] says
72//! and [`Frame::grows`] reports, and it is why such a frame keeps a frame pointer whatever the
73//! flags asked for, the same way a realigned one does and for a version of the same reason.
74//!
75//! Three other things follow from it. The red zone is gone, because the zone is the bytes below the
76//! stack pointer and the first thing an array like this does is move the stack pointer down over
77//! them. The frame asks for the convention's alignment even when nothing in it wanted that much, so
78//! that the stack pointer is on a multiple of it when the body starts and stays on one as each
79//! array rounds its own size up. And the bytes the array hands out start above the outgoing
80//! argument area rather than at the stack pointer, because that area stays at the bottom of the
81//! frame wherever the bottom has moved to, which is what [`Frame::below`] is for.
82//!
83//! An array asking for more alignment than that is not a realignment of the frame, and nothing
84//! here has to know about it. [`crate::expand::rounds`] asks for the alignment in extra bytes and
85//! hands out an address inside them, so the stack pointer moves by a multiple of the convention's
86//! alignment as it always did and the frame is an ordinary growing one.
87//!
88//! Realigning and growing together is the one combination that is not here. After the prologue has
89//! forced an alignment the distance from the frame pointer to the body's stack pointer is already
90//! not a constant, so there is no register left for the rest of the frame to be counted from, and
91//! what fixes that is a second pointer held for the purpose. The lowering refuses that pair rather
92//! than this guessing at it.
93//!
94//! # Late
95//!
96//! Where in the prologue the frame pointer is established is the platform's answer rather than this
97//! file's, and [`rucc_target::CallRegs::late_frame_pointer`] is where the reason for it is written
98//! down. On Windows it goes up after the frame has been taken rather than before, because the
99//! unwind record there cannot describe the other order, and that moves it: it holds a copy of the
100//! body's stack pointer rather than the address of the caller's copy of itself.
101//!
102//! Which is the easier of the two to lay out rather than the harder. Every offset here is from the
103//! body's stack pointer already, so in a frame like this the frame pointer holds exactly what those
104//! offsets are counted from, and a frame that grows needs no adjustment at all where the other
105//! order needs the whole frame and every push taken off. [`Frame::late`] is what says which it is.
106//!
107//! The realigned frame is the one that cannot have it whatever the platform says. There the
108//! prologue forces the alignment after the pushes, which leaves the pushes at a distance from the
109//! body's stack pointer that is not a constant, so a pointer established after all that gives the
110//! record nothing to count them from. Such a frame keeps the early order and is the one shape on
111//! Windows that still has no record, which is `tamnd/rucc#1422`.
112
113use rucc_mir::Func;
114use rucc_regalloc::Allocation;
115use rucc_regalloc::assign::Place;
116use rucc_target::{CallRegs, PhysReg, RegClass, RegFile};
117
118use crate::slots::{Cell, Slots};
119
120/// One register the prologue puts away in the frame, and where in the frame it goes.
121///
122/// A pushed register does not need one of these, because where it goes is wherever the stack
123/// pointer had reached, and the epilogue pops them back in the opposite order without having to
124/// know. A register that is stored rather than pushed does need one.
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub struct Save {
127 /// The register.
128 pub reg: PhysReg,
129 /// Where it goes, from the stack pointer in the body of the function.
130 pub at: i32,
131}
132
133/// Where the arguments the caller passed on the stack are, and which register reaches them.
134///
135/// Two fields rather than one number because a realigned frame has no constant distance from its
136/// stack pointer to the caller's. Forcing the alignment threw that distance away, and the frame
137/// pointer is what still reaches the caller's stack afterwards, which is why a realigned frame is
138/// made to keep one. So there is always an answer, and which register it is counted from is part of
139/// it rather than something the reader is left to work out.
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub struct Incoming {
142 /// How far above that register the first argument passed on the stack is.
143 pub at: i32,
144 /// Whether the register is the frame pointer rather than the stack pointer.
145 pub through_frame_pointer: bool,
146}
147
148impl Incoming {
149 /// That far above the stack pointer as it stands in the body of the function, which is where
150 /// every other offset in a frame is from.
151 #[must_use]
152 pub fn from_stack(at: i32) -> Self {
153 Self { at, through_frame_pointer: false }
154 }
155
156 /// That far above the frame pointer, which is the only way a realigned frame reaches back.
157 #[must_use]
158 pub fn from_frame(at: i32) -> Self {
159 Self { at, through_frame_pointer: true }
160 }
161}
162
163/// A piece of memory the function needs for its own use, which is what an `alloca` becomes.
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub struct Local {
166 /// How many bytes of it there are.
167 pub size: u32,
168 /// What its address has to be a multiple of.
169 pub align: u32,
170}
171
172/// Everything about a function's frame that does not come out of its allocation.
173#[derive(Debug, Clone, Copy)]
174pub struct Layout<'a> {
175 /// Where the convention this function is compiled for puts things.
176 pub conv: &'a CallRegs,
177 /// The registers the target has, which is what says how wide a spill slot of a class is.
178 pub file: RegFile,
179 /// The memory the function asked for itself, in the order it wants it reported back.
180 pub locals: &'a [Local],
181 /// How many bytes the widest call in the function needs for arguments it passes on the stack.
182 pub outgoing: u32,
183 /// Whether the function calls nothing, which is what the alignment and the red zone turn on.
184 pub leaf: bool,
185 /// Whether the function keeps a frame pointer, which `-fno-omit-frame-pointer` asks for and
186 /// which a realigned or a dynamically grown frame requires whatever the flags say.
187 pub frame_pointer: bool,
188 /// Whether the function moves the stack pointer while it runs, which is what a variable length
189 /// array does and what the rest of the frame then has to be reached around.
190 ///
191 /// See `Growing` in the module documentation. A frame like this keeps a frame pointer, takes
192 /// its bytes rather than living in the red zone, and reports every offset in its body from the
193 /// frame pointer, because the stack pointer stops being somewhere a constant reaches from.
194 pub grows: bool,
195 /// Whether the red zone may be used at all, which `-mno-red-zone` and every kernel turns off.
196 pub red_zone: bool,
197 /// Whether the frame holds a stack protector's canary, which `-fstack-protector` and the
198 /// function's own attribute decide between them.
199 ///
200 /// A protected frame is never a leaf, whatever the function called, because the check at the
201 /// end of it calls when it fails. The caller sets `leaf` accordingly rather than this working
202 /// it out, so that there is one place a frame learns whether it owes an aligned stack pointer.
203 pub protect: bool,
204 /// Whether the function is written without a prologue or an epilogue, which
205 /// `__attribute__((naked))` asks for.
206 ///
207 /// A frame like this is empty and nothing is written around the body. No register is put away,
208 /// because the program said it would do that itself and the first thing one of these usually
209 /// does is read something the saving would have moved. No bytes are taken, because taking them
210 /// is the prologue's job and there is no prologue, which is why a naked function that wants any
211 /// is refused rather than given a frame nothing sets up. See [`Frame::of`].
212 pub naked: bool,
213 /// Which locals and spill slots share their bytes with which, or `None` for a frame where
214 /// every one of them gets a run of its own.
215 ///
216 /// Worked out in [`crate::slots`], because what may share is a question about liveness and this
217 /// file is about arithmetic. `None` is the layout there was before that pass existed and is
218 /// what `-fstack-reuse=none` asks for.
219 pub share: Option<&'a Slots>,
220}
221
222impl<'a> Layout<'a> {
223 /// A layout for a function with nothing in it but what its allocation says: a leaf with no
224 /// locals and no calls, which is what every function is until the pieces that produce those
225 /// exist.
226 #[must_use]
227 pub fn new(conv: &'a CallRegs, file: RegFile) -> Self {
228 Self {
229 conv,
230 file,
231 locals: &[],
232 outgoing: 0,
233 leaf: true,
234 frame_pointer: false,
235 grows: false,
236 red_zone: true,
237 protect: false,
238 naked: false,
239 share: None,
240 }
241 }
242}
243
244/// What a function's stack looks like while it runs.
245#[derive(Debug, Clone, PartialEq, Eq)]
246pub struct Frame {
247 saved_int: Vec<PhysReg>,
248 saved_sse: Vec<Save>,
249 slots: Vec<i32>,
250 locals: Vec<i32>,
251 canary: Option<i32>,
252 outgoing: u32,
253 below: u32,
254 size: u32,
255 realign: Option<u32>,
256 incoming: Incoming,
257 frame_pointer: bool,
258 late: bool,
259 grows: bool,
260 naked: bool,
261 usage: u32,
262}
263
264impl Frame {
265 /// Works out the frame of a function the allocator has finished with.
266 ///
267 /// # Panics
268 ///
269 /// Panics on a frame of two gigabytes or more, which is a stack no machine here gives a
270 /// thread, and on a local whose alignment is not a power of two.
271 #[must_use]
272 pub fn of(func: &Func, allocation: &Allocation, layout: &Layout<'_>) -> Self {
273 let conv = layout.conv;
274 let word = conv.word;
275 // How far one push moves the stack pointer, which is the word on x86-64 and the whole
276 // alignment on AArch64. A machine where it is the whole alignment is one whose stack
277 // pointer is never allowed off it, and so a leaf there owes itself an aligned frame even
278 // though it owes nobody else one.
279 let push = conv.push;
280 let aligned = push % conv.stack_align == 0;
281 let (saved_int, vectors) = saved(func, allocation, layout);
282
283 // The vector registers are saved in the frame rather than pushed, because no machine here
284 // has an instruction that pushes one.
285 let vector = width(layout, conv.sse_class);
286 let mut top = 0;
287 let mut align = word;
288 let mut saved_sse = Vec::with_capacity(vectors.len());
289 for reg in vectors {
290 align = align.max(vector);
291 saved_sse.push(Save { reg, at: offset(top) });
292 top += vector;
293 }
294
295 // A frame that grows hands out the bytes above the outgoing area, and what makes that
296 // address usable for anything is the stack pointer being on a multiple of the convention's
297 // alignment when the body starts. Asking for that much here is what buys it: the area below
298 // is padded to `align` and the frame is rounded to land the stack pointer back on it.
299 if layout.grows {
300 align = align.max(conv.stack_align);
301 }
302
303 // One list rather than two, because a local and a spill slot that are never both wanted can
304 // be the same bytes and neither of them can share with something on the other list if the
305 // two lists are placed one after the other. See [`crate::slots`]. A layout that was handed
306 // no plan gets the one where nothing shares anything, which is the frame there was before
307 // that pass existed.
308 let apart;
309 let plan = match layout.share {
310 Some(plan) => plan,
311 None => {
312 apart = Slots::apart(layout.locals, &widths(layout, allocation));
313 &apart
314 }
315 };
316 let mut cells = Vec::with_capacity(plan.cells().len());
317 let mut order: Vec<usize> = (0..plan.cells().len()).collect();
318 // Widest alignment first, so that placing each one straight after the last never leaves a
319 // hole bigger than the alignment the next one asked for. Within one alignment, the cells
320 // that are a whole number of it go before the ones that are not, because a cell that ends
321 // part way through leaves a hole in front of the next cell that asked for the same
322 // alignment and none at all in front of a narrower one. A cell shared by a wide thing and
323 // a strict one is exactly how a size that is not a multiple of its own alignment arises,
324 // so without this a frame could come out larger for sharing than it was for not.
325 order.sort_by_key(|&cell| {
326 let Cell { size, align } = plan.cells()[cell];
327 (std::cmp::Reverse(align), size % align != 0)
328 });
329 cells.resize(plan.cells().len(), 0);
330 for cell in order {
331 let Cell { size, align: want } = plan.cells()[cell];
332 assert!(
333 want.is_power_of_two(),
334 "a local aligned to something that is not a power of 2"
335 );
336 align = align.max(want);
337 top = top.next_multiple_of(want);
338 cells[cell] = offset(top);
339 top += size;
340 }
341
342 // Read back out to the two lists the rest of the compiler asks its questions in. A cell
343 // several things share gives all of them the same offset, which is the whole point of it.
344 let placed = |cell: Option<usize>| cells[cell.expect("a plan covering every slot")];
345 let mut locals: Vec<i32> =
346 (0..layout.locals.len()).map(|local| placed(plan.local(local))).collect();
347 let mut slots: Vec<i32> = (0..allocation.assignment.slots().len())
348 .map(|slot| placed(plan.slot(u32::try_from(slot).expect("a frame"))))
349 .collect();
350
351 // Above everything the function can reach through a local, which is the whole point of it.
352 // A write that runs off the end of an array in this frame passes the canary before it
353 // reaches the saved registers and the return address, so the check at the end of the
354 // function sees a word that changed rather than a return that has already been taken.
355 let mut canary = None;
356 if layout.protect {
357 top = top.next_multiple_of(word);
358 canary = Some(offset(top));
359 top += word;
360 }
361
362 // A call reads its stack arguments from the stack pointer upward, so the outgoing area is
363 // at the bottom of the frame and its size is what shifts everything else.
364 let outgoing = if layout.leaf { 0 } else { layout.outgoing.max(conv.shadow) };
365 // Everything above it was placed as though it were not there, so moving it up by the size
366 // of the area is what would break its alignment. The area is padded to the widest
367 // alignment anything above it asked for, which costs at most that many bytes once and
368 // costs nothing at all in the usual frame, where the area is a multiple of it already.
369 // What the padding must not do is move the area itself: the callee reads its arguments
370 // from the stack pointer, so the bottom of the area is the stack pointer whatever is
371 // above it.
372 let shifted = outgoing.next_multiple_of(align);
373 let body = (top + shifted).next_multiple_of(word);
374
375 let realign = (align > conv.stack_align).then_some(align);
376 // Refused by [`crate::pipeline`] before anything gets here, because the two of them together
377 // want one register twice. See `Growing` above.
378 assert!(
379 !(layout.grows && realign.is_some()),
380 "a frame that grows and forces its alignment needs a second base register"
381 );
382 // Two frames keep one whatever the flags asked for, and each of them for its own version of
383 // the same reason: the prologue is about to leave the stack pointer somewhere no constant
384 // reaches the rest of the frame from, and the frame pointer is the register that still
385 // does. Forcing an alignment is one of the two and growing while the function runs is the
386 // other.
387 //
388 // A function that calls something on a machine whose call leaves the return address in a
389 // register is a third. The call writes over that register, so the prologue has to put it
390 // away, and it goes with the frame pointer as the one frame record the machine's unwinders
391 // and `__builtin_frame_address` expect to find.
392 let frame_pointer = layout.frame_pointer
393 || realign.is_some()
394 || layout.grows
395 || (!layout.leaf && conv.link.is_some());
396 // Where in the prologue the pointer is established, which is the platform's answer except
397 // in the one frame that has an answer of its own. See `Late` above.
398 let late = conv.late_frame_pointer && realign.is_none();
399
400 // Where the stack pointer sits once the prologue has finished pushing: one return address
401 // short of aligned when the function starts, and one push further off for every push. The
402 // frame pointer is a push like any other here, which is why this is asked after the frames
403 // that keep one without being asked to have said so.
404 let pushed = u32::from(frame_pointer) + u32::try_from(saved_int.len()).expect("a frame");
405 let entry = wrap(conv.stack_align, conv.return_address);
406 let after = (entry + wrap(conv.stack_align, push * pushed)) % conv.stack_align;
407
408 // A frame that grows cannot be one of the free ones. The red zone is the bytes below the
409 // stack pointer, and the first thing a variable length array does is move the stack pointer
410 // down over them, so what was in the zone would be handed out twice.
411 let free = layout.leaf
412 && layout.red_zone
413 && realign.is_none()
414 && !layout.grows
415 && align <= word
416 && body <= conv.red_zone;
417 let size = match realign {
418 _ if free => 0,
419 // Once the prologue has forced the alignment, keeping the frame a multiple of it keeps
420 // everything in the frame aligned too.
421 Some(to) => body.next_multiple_of(to),
422 // A leaf owes nobody an aligned stack pointer, so it takes exactly what it uses.
423 None if layout.leaf && align <= word && !aligned => body,
424 // The smallest frame that lands the stack pointer back on a multiple of the alignment
425 // given where the pushes left it.
426 None => body + (after + conv.stack_align - body % conv.stack_align) % conv.stack_align,
427 };
428
429 // With the stack pointer left where it was, the areas are the same areas in the same order
430 // and they are below it rather than above it.
431 //
432 // A frame that grows is counted from the frame pointer instead, which is the same areas in
433 // the same order with one more constant taken off: the prologue pushed the registers and
434 // then took the frame, so the body's stack pointer is that far below where the frame
435 // pointer was set. That distance is what a variable length array destroys and the frame
436 // pointer is what is left, which is why a growing frame keeps one.
437 //
438 // Unless the pointer is established late, where there is nothing to take off: the prologue
439 // points it at the stack pointer once the frame is whole, so the two hold the same address
440 // when the body starts and every distance from one is a distance from the other.
441 let mut shift = if free { -offset(body) } else { offset(shifted) };
442 if layout.grows && !late {
443 shift -= offset(size) + offset(push) * i32::try_from(saved_int.len()).expect("a frame");
444 }
445 for at in slots
446 .iter_mut()
447 .chain(locals.iter_mut())
448 .chain(canary.iter_mut())
449 .chain(saved_sse.iter_mut().map(|save| &mut save.at))
450 {
451 *at += shift;
452 }
453
454 // What `-fstack-usage` reports, worked out here because this is the one place every term
455 // of it is in hand. See [`Frame::usage`] for what the number is. Every push comes before
456 // the prologue forces an alignment, so the pushes are what gets rounded up.
457 let pushes = conv.return_address + push * pushed;
458 let usage = realign.map_or(pushes, |to| pushes.next_multiple_of(to)) + size;
459
460 Self {
461 saved_int,
462 saved_sse,
463 slots,
464 locals,
465 canary,
466 outgoing,
467 below: shifted,
468 size,
469 realign,
470 incoming: match () {
471 // A pointer established late holds what the body's stack pointer holds, so the
472 // caller's stack is the whole frame and every push above it, which is the same
473 // number a frame with no pointer counts from the stack pointer.
474 () if late && layout.grows => {
475 Incoming::from_frame(offset(size + push * pushed + conv.return_address))
476 }
477 // The prologue saves the frame pointer before it does anything else and points it
478 // at where it saved it, so the caller's stack is one push for that and one return
479 // address above it, whatever the prologue did to the stack pointer afterwards.
480 () if realign.is_some() || layout.grows => {
481 Incoming::from_frame(offset(push + conv.return_address))
482 }
483 () => Incoming::from_stack(offset(size + push * pushed + conv.return_address)),
484 },
485 frame_pointer,
486 late,
487 grows: layout.grows,
488 naked: layout.naked,
489 usage,
490 }
491 }
492
493 /// The general purpose registers the prologue pushes, in the order it pushes them.
494 ///
495 /// The frame pointer is not among them even when the convention calls it a saved register,
496 /// because a function that keeps one saves it as part of setting it up.
497 #[must_use]
498 pub fn saved_int(&self) -> &[PhysReg] {
499 &self.saved_int
500 }
501
502 /// The vector registers the prologue stores into the frame, and where each of them goes.
503 #[must_use]
504 pub fn saved_sse(&self) -> &[Save] {
505 &self.saved_sse
506 }
507
508 /// Where a spill slot is, from the stack pointer in the body of the function.
509 #[must_use]
510 pub fn slot(&self, slot: u32) -> Option<i32> {
511 self.slots.get(usize::try_from(slot).ok()?).copied()
512 }
513
514 /// Where a local is, from the stack pointer in the body of the function.
515 #[must_use]
516 pub fn local(&self, local: usize) -> Option<i32> {
517 self.locals.get(local).copied()
518 }
519
520 /// Where a local is, from the call frame address, which is what a debugger counts from.
521 ///
522 /// The call frame address is the stack pointer the caller held when it made the call, and
523 /// [`Frame::incoming`] is already the distance up to it, since the first argument passed on the
524 /// stack sits there. So the answer is one subtraction, and it is a negative number, because the
525 /// frame is below the address the call was made from.
526 ///
527 /// `None` in a frame whose alignment the prologue had to force, where there is no answer to
528 /// give. Rounding the stack pointer down throws away however far it was from where the caller
529 /// left it, so the distance from the body's stack pointer up to the call frame address is not a
530 /// constant in such a function, and the two numbers subtracted here are counted from different
531 /// registers on top of that. What the locals of such a function want is a location counted from
532 /// the frame pointer, which is a different expression from the one a frame base gives.
533 ///
534 /// `None` as well for a local this frame never placed.
535 #[must_use]
536 pub fn from_frame_base(&self, local: usize) -> Option<i32> {
537 if self.realign.is_some() {
538 return None;
539 }
540 Some(self.local(local)? - self.incoming.at)
541 }
542
543 /// Where a spill slot is, from the call frame address, which is what a debugger counts from.
544 ///
545 /// The same subtraction [`Frame::from_frame_base`] makes and `None` in the same function, for
546 /// the same reasons. It is the other half of the same question: a local the program named is
547 /// either in the part of the frame the front end asked for or in the part the allocator ran
548 /// out of registers into, and a debugger wants both counted from the same place.
549 #[must_use]
550 pub fn slot_from_frame_base(&self, slot: u32) -> Option<i32> {
551 if self.realign.is_some() {
552 return None;
553 }
554 Some(self.slot(slot)? - self.incoming.at)
555 }
556
557 /// Where the stack protector's canary is, from the stack pointer in the body of the function,
558 /// or `None` in a frame that has none.
559 #[must_use]
560 pub fn canary(&self) -> Option<i32> {
561 self.canary
562 }
563
564 /// How many bytes the prologue takes off the stack pointer, which is nothing for a function
565 /// small enough and quiet enough to live in the red zone.
566 #[must_use]
567 pub fn size(&self) -> u32 {
568 self.size
569 }
570
571 /// How many bytes at the bottom of the frame belong to the arguments of calls this function
572 /// makes, which is where the shadow space goes on Windows.
573 #[must_use]
574 pub fn outgoing(&self) -> u32 {
575 self.outgoing
576 }
577
578 /// How many bytes at the bottom of the frame nothing else may be placed in, which is that area
579 /// padded to the alignment everything above it asked for.
580 ///
581 /// What a variable length array has to step over. It takes its bytes off the stack pointer,
582 /// which leaves them at the bottom of the frame where the next call is going to write its
583 /// arguments, so the address it hands out is this far above the stack pointer rather than the
584 /// stack pointer itself.
585 #[must_use]
586 pub fn below(&self) -> u32 {
587 self.below
588 }
589
590 /// Whether the function moves the stack pointer while it runs.
591 ///
592 /// Every offset in the body of such a frame is from the frame pointer rather than from the
593 /// stack pointer, because a variable length array leaves the stack pointer somewhere no
594 /// constant reaches the rest of the frame from. See `Growing` in the module documentation.
595 #[must_use]
596 pub fn grows(&self) -> bool {
597 self.grows
598 }
599
600 /// What the prologue has to force the stack pointer to be a multiple of, when a local wants
601 /// more alignment than a call leaves it with.
602 #[must_use]
603 pub fn realign(&self) -> Option<u32> {
604 self.realign
605 }
606
607 /// Where the first argument the caller passed on the stack is, and which register reaches it.
608 ///
609 /// The only offset here that is not always from the stack pointer. A realigned frame counts
610 /// from the frame pointer instead, because forcing the alignment threw away however far the
611 /// caller's stack pointer was from where the prologue wanted it, and the frame pointer is what
612 /// reaches the caller's stack afterwards.
613 #[must_use]
614 pub fn incoming(&self) -> Incoming {
615 self.incoming
616 }
617
618 /// Whether the function keeps a frame pointer.
619 #[must_use]
620 pub fn frame_pointer(&self) -> bool {
621 self.frame_pointer
622 }
623
624 /// Whether nothing at all is to be written around the body, which `__attribute__((naked))`
625 /// asks for. See [`Layout::naked`].
626 ///
627 /// Such a frame is empty, since [`crate::pipeline`] refuses a naked function that wanted any
628 /// bytes rather than handing it a frame no prologue sets up. What is left for this to say is
629 /// that the prologue and the epilogue are not to be written, and the epilogue is the point:
630 /// the `ret` at the end of a function is written there, and a naked function ends where its
631 /// own text ends.
632 #[must_use]
633 pub fn naked(&self) -> bool {
634 self.naked
635 }
636
637 /// Whether the prologue points the frame pointer at the frame after taking it rather than
638 /// before, which is [`rucc_target::CallRegs::late_frame_pointer`] and the one frame that cannot
639 /// have it whatever the platform says. See `Late` in the module documentation.
640 #[must_use]
641 pub fn late(&self) -> bool {
642 self.late
643 }
644
645 /// How many bytes of stack the function uses, not counting what a variable length array or an
646 /// `alloca` takes while it runs, which is the number `-fstack-usage` reports.
647 ///
648 /// Counted the way gcc counts its own frames, so that the two compilers' reports can be
649 /// compared function by function: the distance from the stack pointer the caller held just
650 /// before its call instruction down to where the prologue leaves the stack pointer. That is
651 /// the return address when the call pushes one, every register the prologue pushes, the frame
652 /// pointer among them, and the size the prologue subtracts, which already holds the saved
653 /// vector registers, the locals, the spill slots, the canary and the outgoing argument area.
654 /// Bytes a leaf keeps in the red zone are not counted, and gcc does not count them either.
655 ///
656 /// A frame whose alignment the prologue forces has the pushes rounded up to that alignment
657 /// before the rest is added, which is gcc's rule for the same frame. It is exact when the
658 /// caller's stack pointer happened to be aligned that far already, and otherwise it is off by
659 /// less than the alignment in one direction or the other, since how much the rounding throws
660 /// away depends on where the caller's stack pointer was.
661 ///
662 /// The two numbers agree on what they count and not always on the bytes: a function whose
663 /// locals rucc keeps in fewer or more slots than gcc does comes out smaller or larger by that
664 /// much. A frame that [`Frame::grows`] is reported as `dynamic` beside this, the same as gcc.
665 #[must_use]
666 pub fn usage(&self) -> u32 {
667 self.usage
668 }
669}
670
671/// The registers a call preserves that this function writes anyway, so the prologue has to put
672/// them back.
673///
674/// The rewritten function is what is read here rather than the assignment, because a spilled value
675/// is reloaded into a scratch register that no assignment mentions, and a scratch register the
676/// convention preserves is one this has to find.
677///
678/// Nothing at all in a naked function, which is the whole of what the attribute asks for. Such a
679/// function names `%rbx` and `%rbp` in its own text and means the registers rather than places to
680/// keep something, and putting them away in front of it would be the compiler answering a question
681/// the program did not ask. See [`Layout::naked`].
682fn saved(
683 func: &Func,
684 allocation: &Allocation,
685 layout: &Layout<'_>,
686) -> (Vec<PhysReg>, Vec<PhysReg>) {
687 if layout.naked {
688 return (Vec::new(), Vec::new());
689 }
690 let mut used: Vec<(RegClass, PhysReg)> = Vec::new();
691 let mut note = |class: RegClass, at: PhysReg| {
692 if !used.contains(&(class, at)) {
693 used.push((class, at));
694 }
695 };
696 for block in func.blocks() {
697 for inst in func.insts(block) {
698 for operand in &func[func[inst].operands] {
699 if let Some(at) = operand.reg.phys() {
700 note(operand.class, at);
701 }
702 }
703 }
704 }
705 for edit in &allocation.edits {
706 for place in [edit.mov.from, edit.mov.to] {
707 if let Place::Reg(at) = place {
708 note(edit.class, at);
709 }
710 }
711 }
712
713 let conv = layout.conv;
714 let wanted = |class: RegClass, at: PhysReg| used.contains(&(class, at));
715 // In the convention's order rather than the order the function happened to reach for them, so
716 // that two functions saving the same registers get the same prologue.
717 let saved_int = conv
718 .int_saved
719 .iter()
720 .copied()
721 .filter(|&at| wanted(conv.int_class, at))
722 .filter(|&at| !(layout.frame_pointer && at == conv.frame_pointer))
723 .collect();
724 let saved_sse =
725 conv.sse_saved.iter().copied().filter(|&at| wanted(conv.sse_class, at)).collect();
726 (saved_int, saved_sse)
727}
728
729/// How many bytes a value of a class takes on the stack.
730///
731/// A power of two at least a word wide, because a slot is addressed and an address that is not a
732/// multiple of the size of the thing at it is a fault on some machines and slow on the rest. An
733/// eighty bit `long double` takes sixteen bytes for that reason, which is what every compiler
734/// does with one.
735fn width(layout: &Layout<'_>, class: RegClass) -> u32 {
736 let bits = layout.file.class(class).map_or(0, |info| info.bits);
737 bits.div_ceil(8).max(layout.conv.word).next_power_of_two()
738}
739
740/// How many bytes each of an allocation's spill slots takes on the stack.
741///
742/// The same question the width of one register class is, asked of a whole allocation at once, and
743/// public because [`crate::slots`] needs it to say how big a cell holding a spilled value has to
744/// be, which it has to know before there is a frame to ask.
745#[must_use]
746pub fn widths(layout: &Layout<'_>, allocation: &Allocation) -> Vec<u32> {
747 allocation.assignment.slots().iter().map(|&class| width(layout, class)).collect()
748}
749
750/// How far past a multiple of an alignment a number is, counted the other way: what has to be
751/// added to it to reach the next one.
752fn wrap(align: u32, value: u32) -> u32 {
753 (align - value % align) % align
754}
755
756/// A distance in a frame, as the signed number every offset out of here is.
757fn offset(bytes: u32) -> i32 {
758 i32::try_from(bytes).expect("a frame under two gigabytes")
759}
760
761#[cfg(test)]
762mod tests {
763 use rucc_base::Interner;
764 use rucc_mir::{Opcode, Operand, Reg};
765 use rucc_regalloc::assign::Env;
766 use rucc_target::x86_64::{GPR, RBP, REGS, SYSV, WIN64, XMM};
767
768 use super::*;
769
770 /// An environment offering that many of the convention's registers, with everything after
771 /// them held back as scratch.
772 fn env(conv: &CallRegs, count: usize) -> Env {
773 Env::new().with(GPR, &conv.int_order[..count], &conv.int_order[count..])
774 }
775
776 /// A function of that many values, every one of them written before any is read, allocated
777 /// with that many registers to hand out.
778 ///
779 /// Every value is live at the first read, so a count below the number of values is what puts
780 /// the function under enough pressure to spill, and each read wants one value so a reload
781 /// never needs more than one scratch register.
782 fn pressure(conv: &CallRegs, values: usize, count: usize) -> (Func, Allocation) {
783 let mut names = Interner::new();
784 let mut func = Func::new(names.intern("f"));
785 let opcode = Opcode::new(names.intern("x64.nop"));
786 let block = func.create_block();
787 let regs: Vec<Reg> = (0..values).map(|_| func.new_vreg(GPR)).collect();
788 for ® in ®s {
789 func.build(block, opcode).def(reg, GPR).finish();
790 }
791 for ® in ®s {
792 func.build(block, opcode).uses(reg, GPR).finish();
793 }
794 let allocation = rucc_regalloc::run(&mut func, &env(conv, count), "test", true);
795 (func, allocation)
796 }
797
798 /// What a list of registers is called, which is what an assertion reads.
799 fn named(regs: &[PhysReg]) -> Vec<&'static str> {
800 regs.iter().map(|®| REGS.name(GPR, reg).expect("a register")).collect()
801 }
802
803 #[test]
804 fn a_function_that_needs_nothing_of_the_stack_has_no_frame_at_all() {
805 let (func, allocation) = pressure(&SYSV, 2, 4);
806 let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
807
808 assert_eq!(frame.size(), 0);
809 assert_eq!(named(frame.saved_int()), Vec::<&str>::new());
810 assert_eq!(frame.slot(0), None);
811 // Nothing between the stack pointer and the return address the call pushed.
812 assert_eq!(frame.incoming(), Incoming::from_stack(8));
813 }
814
815 #[test]
816 fn a_small_leaf_function_puts_its_spills_in_the_red_zone_and_moves_nothing() {
817 let (func, allocation) = pressure(&SYSV, 4, 2);
818 let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
819
820 // Two registers for four values that are all live at once, so two are on the stack, and a
821 // leaf function small enough is entitled to the bytes below the stack pointer.
822 assert_eq!(frame.size(), 0);
823 assert_eq!((frame.slot(0), frame.slot(1)), (Some(-16), Some(-8)));
824 assert_eq!(frame.slot(2), None);
825 assert_eq!(frame.incoming(), Incoming::from_stack(8));
826 }
827
828 #[test]
829 fn a_leaf_function_told_it_has_no_red_zone_takes_the_bytes_instead() {
830 let (func, allocation) = pressure(&SYSV, 4, 2);
831 let base = Layout::new(&SYSV, REGS);
832 let frame = Frame::of(&func, &allocation, &Layout { red_zone: false, ..base });
833
834 assert_eq!(frame.size(), 16);
835 assert_eq!((frame.slot(0), frame.slot(1)), (Some(0), Some(8)));
836 assert_eq!(frame.incoming(), Incoming::from_stack(24));
837 }
838
839 #[test]
840 fn a_frame_too_big_for_the_red_zone_takes_the_bytes_whatever_else_is_true() {
841 let (func, allocation) = pressure(&SYSV, 40, 2);
842 let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
843
844 // Thirty eight values on the stack is three hundred and four bytes, and the red zone is a
845 // hundred and twenty eight.
846 assert_eq!(frame.size(), 304);
847 assert_eq!(frame.slot(0), Some(0));
848 assert_eq!(frame.slot(37), Some(296));
849 }
850
851 #[test]
852 fn a_function_that_calls_something_leaves_the_stack_pointer_where_a_call_wants_it() {
853 let (func, allocation) = pressure(&SYSV, 4, 2);
854 let base = Layout::new(&SYSV, REGS);
855 let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
856
857 // Sixteen bytes of spills, and the call that reached this function left the stack pointer
858 // eight bytes off, so the frame is eight bytes wider than the spills need and every call
859 // this function makes is correctly aligned.
860 assert_eq!(frame.size(), 24);
861 assert_eq!((frame.slot(0), frame.slot(1)), (Some(0), Some(8)));
862 assert_eq!(frame.incoming(), Incoming::from_stack(32));
863 }
864
865 #[test]
866 fn a_push_is_counted_in_the_alignment_the_frame_has_to_produce() {
867 let (func, allocation) = pressure(&SYSV, 12, 12);
868 let base = Layout::new(&SYSV, REGS);
869 let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
870
871 // Twelve values reach into the preserved end of the allocation order, so three registers
872 // are pushed, and three pushes plus the return address is a multiple of sixteen already.
873 // The frame is empty and stays empty rather than being padded for the sake of it.
874 assert_eq!(named(frame.saved_int()), ["rbx", "r12", "r13"]);
875 assert_eq!(frame.size(), 0);
876 assert_eq!(frame.incoming(), Incoming::from_stack(32));
877 }
878
879 #[test]
880 fn the_registers_a_call_leaves_alone_are_saved_in_the_order_the_convention_lists_them() {
881 let (func, allocation) = pressure(&SYSV, 13, 13);
882 let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
883
884 // Four of them now, in the convention's order rather than the order the allocator handed
885 // them out in, so that two functions saving the same registers get the same prologue.
886 assert_eq!(named(frame.saved_int()), ["rbx", "r12", "r13", "r14"]);
887 }
888
889 #[test]
890 fn a_function_that_keeps_a_frame_pointer_does_not_save_it_twice() {
891 let mut names = Interner::new();
892 let mut func = Func::new(names.intern("f"));
893 let opcode = Opcode::new(names.intern("x64.nop"));
894 let block = func.create_block();
895 // An instruction that names the frame pointer register outright, which is what a lowering
896 // rule for something that has to use it produces.
897 func.build(block, opcode).operand(Operand::write(Reg::physical(RBP), GPR)).finish();
898 let allocation = rucc_regalloc::run(&mut func, &env(&SYSV, 4), "test", true);
899 let base = Layout::new(&SYSV, REGS);
900
901 let kept = Frame::of(&func, &allocation, &Layout { frame_pointer: true, ..base });
902 let dropped = Frame::of(&func, &allocation, &base);
903
904 // `rbp` is a register SysV preserves, so a function that leaves it alone saves it in the
905 // ordinary way, and a function that keeps a frame pointer in it saves it as part of
906 // setting the frame pointer up instead.
907 assert_eq!(named(dropped.saved_int()), ["rbp"]);
908 assert_eq!(named(kept.saved_int()), Vec::<&str>::new());
909 assert!(kept.frame_pointer());
910 }
911
912 #[test]
913 fn locals_are_placed_widest_alignment_first_and_reported_in_the_order_they_arrived() {
914 let (func, allocation) = pressure(&SYSV, 2, 4);
915 let locals = [
916 Local { size: 1, align: 1 },
917 Local { size: 16, align: 16 },
918 Local { size: 8, align: 8 },
919 ];
920 let base = Layout::new(&SYSV, REGS);
921 let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
922
923 // The sixteen byte one is placed first, so nothing is padded to reach it, and the one
924 // byte one goes last where the padding after it costs nothing.
925 assert_eq!((frame.local(1), frame.local(2), frame.local(0)), (Some(0), Some(16), Some(24)));
926 assert_eq!(frame.local(3), None);
927 // A local wanting sixteen byte alignment is more than the stack pointer has for free, so
928 // the frame is taken rather than the red zone used, and it is padded to keep the local
929 // where it was put.
930 assert_eq!(frame.size(), 40);
931 assert_eq!(frame.realign(), None);
932 }
933
934 #[test]
935 fn a_local_is_counted_from_the_call_frame_address_wherever_the_frame_was_put() {
936 let (func, allocation) = pressure(&SYSV, 2, 4);
937 let base = Layout::new(&SYSV, REGS);
938
939 let locals = [
940 Local { size: 1, align: 1 },
941 Local { size: 16, align: 16 },
942 Local { size: 8, align: 8 },
943 ];
944 let taken = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
945
946 // The frame is forty bytes and the return address is eight more, so the call frame address
947 // is forty eight above the stack pointer and every local is that much less than wherever
948 // the layout put it. The one byte one is nearest, at the top of the frame.
949 assert_eq!(taken.incoming(), Incoming::from_stack(48));
950 assert_eq!(taken.from_frame_base(1), Some(-48));
951 assert_eq!(taken.from_frame_base(2), Some(-32));
952 assert_eq!(taken.from_frame_base(0), Some(-24));
953 assert_eq!(taken.from_frame_base(3), None);
954
955 // And a leaf small enough to live in the red zone takes no frame at all, so its stack
956 // pointer is still one return address below the call frame address and its local is below
957 // that. The same subtraction answers both, which is the point of doing it this way.
958 let one = [Local { size: 8, align: 8 }];
959 let free = Frame::of(&func, &allocation, &Layout { locals: &one, ..base });
960
961 assert_eq!(free.size(), 0);
962 assert_eq!(free.incoming(), Incoming::from_stack(8));
963 assert_eq!(free.from_frame_base(0), Some(-16));
964 }
965
966 #[test]
967 fn a_realigned_frame_is_no_constant_distance_from_the_call_frame_address() {
968 let (func, allocation) = pressure(&SYSV, 2, 4);
969 let locals = [Local { size: 64, align: 32 }];
970 let base = Layout::new(&SYSV, REGS);
971 let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
972
973 // The prologue rounds the stack pointer down to a multiple of thirty two, which throws
974 // away however far the caller left it from there, so how far the local is below the call
975 // frame address is a different number every time the function is called.
976 assert_eq!(frame.realign(), Some(32));
977 assert_eq!(frame.local(0), Some(0));
978 assert_eq!(frame.from_frame_base(0), None);
979 }
980
981 #[test]
982 fn a_local_wanting_more_alignment_than_a_call_gives_makes_the_prologue_force_it() {
983 let (func, allocation) = pressure(&SYSV, 2, 4);
984 let locals = [Local { size: 64, align: 32 }];
985 let base = Layout::new(&SYSV, REGS);
986 let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
987
988 assert_eq!(frame.realign(), Some(32));
989 assert_eq!(frame.local(0), Some(0));
990 assert_eq!(frame.size(), 64);
991 // Forcing the alignment throws away how far the caller's stack pointer was from where the
992 // prologue wanted it, so a frame pointer is needed and the caller's stack is reached
993 // through it instead: one word for the saved frame pointer and one for the return address.
994 assert!(frame.frame_pointer());
995 assert_eq!(frame.incoming(), Incoming::from_frame(16));
996 }
997
998 #[test]
999 fn the_canary_is_above_every_byte_a_local_or_a_spill_reaches() {
1000 let (func, allocation) = pressure(&SYSV, 4, 2);
1001 let locals = [Local { size: 16, align: 16 }, Local { size: 8, align: 8 }];
1002 let base = Layout::new(&SYSV, REGS);
1003 let there = Layout { leaf: false, locals: &locals, protect: true, ..base };
1004 let frame = Frame::of(&func, &allocation, &there);
1005
1006 // Two spill slots at the bottom, then the two locals, then the canary above all four. That
1007 // order is the whole mechanism: a write that runs off the end of either local passes the
1008 // canary before it reaches the saved registers and the return address.
1009 let canary = frame.canary().expect("a protected frame has a slot");
1010 for below in [frame.slot(0), frame.slot(1), frame.local(0), frame.local(1)] {
1011 assert!(below.expect("a slot that was asked for") < canary);
1012 }
1013 assert_eq!(canary, 40);
1014 // Forty eight bytes of areas, and then the eight that put the stack pointer back where a
1015 // call wants it, because the arm the check fails on makes one.
1016 assert_eq!(frame.size(), 56);
1017 assert_eq!((frame.size() + SYSV.return_address) % SYSV.stack_align, 0);
1018 }
1019
1020 #[test]
1021 fn a_frame_with_no_protector_has_no_slot_for_a_canary() {
1022 let (func, allocation) = pressure(&SYSV, 2, 4);
1023 let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
1024
1025 assert_eq!(frame.canary(), None);
1026 }
1027
1028 #[test]
1029 fn a_call_reads_its_stack_arguments_from_the_bottom_of_the_frame() {
1030 let (func, allocation) = pressure(&SYSV, 4, 2);
1031 let base = Layout::new(&SYSV, REGS);
1032 let frame = Frame::of(&func, &allocation, &Layout { leaf: false, outgoing: 24, ..base });
1033
1034 // The outgoing area is at the stack pointer, because that is where the callee will look
1035 // for it, and the spills sit above it.
1036 assert_eq!(frame.outgoing(), 24);
1037 assert_eq!((frame.slot(0), frame.slot(1)), (Some(24), Some(32)));
1038 assert_eq!(frame.size(), 40);
1039 }
1040
1041 /// Moving everything up by the size of the outgoing area is what would break its alignment,
1042 /// so the area is padded to the widest alignment anything above it wanted. The area itself
1043 /// still starts at the stack pointer, because that is the one thing about it that is not this
1044 /// frame's to choose.
1045 #[test]
1046 fn what_is_above_the_outgoing_area_keeps_the_alignment_it_asked_for() {
1047 let (func, allocation) = pressure(&SYSV, 2, 4);
1048 let locals = [Local { size: 16, align: 16 }];
1049 let base = Layout::new(&SYSV, REGS);
1050 let there = Layout { leaf: false, outgoing: 8, locals: &locals, ..base };
1051 let frame = Frame::of(&func, &allocation, &there);
1052
1053 assert_eq!(frame.outgoing(), 8);
1054 assert_eq!(frame.local(0), Some(16));
1055 assert_eq!(frame.size(), 40);
1056 // A call leaves the stack pointer one return address short of aligned and nothing was
1057 // pushed on top of that, so the frame is what puts it back and the local lands aligned.
1058 assert_eq!((frame.size() + SYSV.return_address) % SYSV.stack_align, 0);
1059 }
1060
1061 #[test]
1062 fn a_windows_call_gets_the_thirty_two_bytes_below_it_even_when_it_passes_nothing() {
1063 let (func, allocation) = pressure(&WIN64, 2, 4);
1064 let base = Layout::new(&WIN64, REGS);
1065 let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
1066
1067 // Windows has no red zone and every caller reserves thirty two bytes below the call for
1068 // the callee to spill its register arguments into.
1069 assert_eq!(frame.outgoing(), 32);
1070 assert_eq!(frame.size(), 40);
1071 assert_eq!(frame.incoming(), Incoming::from_stack(48));
1072 }
1073
1074 #[test]
1075 fn a_windows_frame_pointer_is_established_after_the_frame_rather_than_before_it() {
1076 let (func, allocation) = pressure(&WIN64, 4, 2);
1077 let base = Layout::new(&WIN64, REGS);
1078 let kept = Frame::of(&func, &allocation, &Layout { frame_pointer: true, ..base });
1079 let dropped = Frame::of(&func, &allocation, &base);
1080
1081 // The unwind record that platform reads cannot describe the other order, so the prologue
1082 // pushes, takes the frame and only then points the pointer at it. What that buys is that
1083 // the pointer holds what the stack pointer holds, so a frame with one and a frame without
1084 // one are the same frame with the same numbers in it.
1085 assert!(kept.frame_pointer());
1086 assert!(kept.late());
1087 assert!(!dropped.frame_pointer());
1088 assert_eq!(kept.size(), dropped.size());
1089 assert_eq!((kept.slot(0), kept.slot(1)), (dropped.slot(0), dropped.slot(1)));
1090 assert_eq!(kept.incoming(), Incoming::from_stack(dropped.incoming().at + 8));
1091 }
1092
1093 #[test]
1094 fn a_windows_frame_that_grows_keeps_the_numbers_it_had_and_changes_the_register() {
1095 let (func, allocation) = pressure(&WIN64, 4, 2);
1096 let base = Layout::new(&WIN64, REGS);
1097 let there = Layout { leaf: false, frame_pointer: true, ..base };
1098 let still = Frame::of(&func, &allocation, &there);
1099 let grown = Frame::of(&func, &allocation, &Layout { grows: true, ..there });
1100
1101 // A frame that grows keeps a pointer whatever the flags asked for, and on this platform
1102 // that pointer is established late, which means it is a copy of the stack pointer as the
1103 // body finds it. So every distance the frame had already worked out from the stack pointer
1104 // is the same distance from the pointer, and growing changes which register the offsets are
1105 // counted from and nothing else. That is the whole of why this frame needs no adjustment.
1106 assert!(grown.grows());
1107 assert!(grown.late());
1108 assert_eq!(grown.size(), still.size());
1109 assert_eq!(grown.outgoing(), still.outgoing());
1110 assert_eq!((grown.slot(0), grown.slot(1)), (still.slot(0), still.slot(1)));
1111 assert_eq!(grown.incoming(), Incoming::from_frame(still.incoming().at));
1112 }
1113
1114 #[test]
1115 fn a_realigned_frame_on_windows_keeps_the_early_order_it_has_no_choice_about() {
1116 let (func, allocation) = pressure(&WIN64, 2, 4);
1117 let locals = [Local { size: 64, align: 32 }];
1118 let base = Layout::new(&WIN64, REGS);
1119 let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
1120
1121 // Forcing the alignment leaves the pushes at no constant distance from anything, so the
1122 // pointer has to go up before the mask and the platform's answer does not apply. Such a
1123 // frame is described by nothing and the assembler refuses it by name, which is
1124 // `tamnd/rucc#1422`.
1125 assert_eq!(frame.realign(), Some(32));
1126 assert!(frame.frame_pointer());
1127 assert!(!frame.late());
1128 assert_eq!(frame.incoming(), Incoming::from_frame(16));
1129 }
1130
1131 #[test]
1132 fn a_slot_is_as_wide_as_the_widest_thing_of_its_class() {
1133 let base = Layout::new(&SYSV, REGS);
1134
1135 assert_eq!(width(&base, GPR), 8);
1136 assert_eq!(width(&base, XMM), 16);
1137 // A long double is eighty bits and takes sixteen bytes, because an address has to be a
1138 // multiple of the size of what is at it.
1139 assert_eq!(width(&base, REGS.class_named("x87").expect("a class")), 16);
1140 }
1141}