Skip to main content

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//! Realigning and growing together is the one combination that is not here. After the prologue has
84//! forced an alignment the distance from the frame pointer to the body's stack pointer is already
85//! not a constant, so there is no register left for the rest of the frame to be counted from, and
86//! what fixes that is a second pointer held for the purpose. The lowering refuses that pair rather
87//! than this guessing at it.
88
89use rucc_mir::Func;
90use rucc_regalloc::Allocation;
91use rucc_regalloc::assign::Place;
92use rucc_target::{CallRegs, PhysReg, RegClass, RegFile};
93
94/// One register the prologue puts away in the frame, and where in the frame it goes.
95///
96/// A pushed register does not need one of these, because where it goes is wherever the stack
97/// pointer had reached, and the epilogue pops them back in the opposite order without having to
98/// know. A register that is stored rather than pushed does need one.
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub struct Save {
101    /// The register.
102    pub reg: PhysReg,
103    /// Where it goes, from the stack pointer in the body of the function.
104    pub at: i32,
105}
106
107/// Where the arguments the caller passed on the stack are, and which register reaches them.
108///
109/// Two fields rather than one number because a realigned frame has no constant distance from its
110/// stack pointer to the caller's. Forcing the alignment threw that distance away, and the frame
111/// pointer is what still reaches the caller's stack afterwards, which is why a realigned frame is
112/// made to keep one. So there is always an answer, and which register it is counted from is part of
113/// it rather than something the reader is left to work out.
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub struct Incoming {
116    /// How far above that register the first argument passed on the stack is.
117    pub at: i32,
118    /// Whether the register is the frame pointer rather than the stack pointer.
119    pub through_frame_pointer: bool,
120}
121
122impl Incoming {
123    /// That far above the stack pointer as it stands in the body of the function, which is where
124    /// every other offset in a frame is from.
125    #[must_use]
126    pub fn from_stack(at: i32) -> Self {
127        Self { at, through_frame_pointer: false }
128    }
129
130    /// That far above the frame pointer, which is the only way a realigned frame reaches back.
131    #[must_use]
132    pub fn from_frame(at: i32) -> Self {
133        Self { at, through_frame_pointer: true }
134    }
135}
136
137/// A piece of memory the function needs for its own use, which is what an `alloca` becomes.
138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub struct Local {
140    /// How many bytes of it there are.
141    pub size: u32,
142    /// What its address has to be a multiple of.
143    pub align: u32,
144}
145
146/// Everything about a function's frame that does not come out of its allocation.
147#[derive(Debug, Clone, Copy)]
148pub struct Layout<'a> {
149    /// Where the convention this function is compiled for puts things.
150    pub conv: &'a CallRegs,
151    /// The registers the target has, which is what says how wide a spill slot of a class is.
152    pub file: RegFile,
153    /// The memory the function asked for itself, in the order it wants it reported back.
154    pub locals: &'a [Local],
155    /// How many bytes the widest call in the function needs for arguments it passes on the stack.
156    pub outgoing: u32,
157    /// Whether the function calls nothing, which is what the alignment and the red zone turn on.
158    pub leaf: bool,
159    /// Whether the function keeps a frame pointer, which `-fno-omit-frame-pointer` asks for and
160    /// which a realigned or a dynamically grown frame requires whatever the flags say.
161    pub frame_pointer: bool,
162    /// Whether the function moves the stack pointer while it runs, which is what a variable length
163    /// array does and what the rest of the frame then has to be reached around.
164    ///
165    /// See `Growing` in the module documentation. A frame like this keeps a frame pointer, takes
166    /// its bytes rather than living in the red zone, and reports every offset in its body from the
167    /// frame pointer, because the stack pointer stops being somewhere a constant reaches from.
168    pub grows: bool,
169    /// Whether the red zone may be used at all, which `-mno-red-zone` and every kernel turns off.
170    pub red_zone: bool,
171    /// Whether the frame holds a stack protector's canary, which `-fstack-protector` and the
172    /// function's own attribute decide between them.
173    ///
174    /// A protected frame is never a leaf, whatever the function called, because the check at the
175    /// end of it calls when it fails. The caller sets `leaf` accordingly rather than this working
176    /// it out, so that there is one place a frame learns whether it owes an aligned stack pointer.
177    pub protect: bool,
178}
179
180impl<'a> Layout<'a> {
181    /// A layout for a function with nothing in it but what its allocation says: a leaf with no
182    /// locals and no calls, which is what every function is until the pieces that produce those
183    /// exist.
184    #[must_use]
185    pub fn new(conv: &'a CallRegs, file: RegFile) -> Self {
186        Self {
187            conv,
188            file,
189            locals: &[],
190            outgoing: 0,
191            leaf: true,
192            frame_pointer: false,
193            grows: false,
194            red_zone: true,
195            protect: false,
196        }
197    }
198}
199
200/// What a function's stack looks like while it runs.
201#[derive(Debug, Clone, PartialEq, Eq)]
202pub struct Frame {
203    saved_int: Vec<PhysReg>,
204    saved_sse: Vec<Save>,
205    slots: Vec<i32>,
206    locals: Vec<i32>,
207    canary: Option<i32>,
208    outgoing: u32,
209    below: u32,
210    size: u32,
211    realign: Option<u32>,
212    incoming: Incoming,
213    frame_pointer: bool,
214    grows: bool,
215}
216
217impl Frame {
218    /// Works out the frame of a function the allocator has finished with.
219    ///
220    /// # Panics
221    ///
222    /// Panics on a frame of two gigabytes or more, which is a stack no machine here gives a
223    /// thread, and on a local whose alignment is not a power of two.
224    #[must_use]
225    pub fn of(func: &Func, allocation: &Allocation, layout: &Layout<'_>) -> Self {
226        let conv = layout.conv;
227        let word = conv.word;
228        let (saved_int, vectors) = saved(func, allocation, layout);
229
230        // The vector registers are saved in the frame rather than pushed, because no machine here
231        // has an instruction that pushes one.
232        let vector = width(layout, conv.sse_class);
233        let mut top = 0;
234        let mut align = word;
235        let mut saved_sse = Vec::with_capacity(vectors.len());
236        for reg in vectors {
237            align = align.max(vector);
238            saved_sse.push(Save { reg, at: offset(top) });
239            top += vector;
240        }
241
242        // A frame that grows hands out the bytes above the outgoing area, and what makes that
243        // address usable for anything is the stack pointer being on a multiple of the convention's
244        // alignment when the body starts. Asking for that much here is what buys it: the area below
245        // is padded to `align` and the frame is rounded to land the stack pointer back on it.
246        if layout.grows {
247            align = align.max(conv.stack_align);
248        }
249
250        let mut locals = vec![0; layout.locals.len()];
251        let mut order: Vec<usize> = (0..layout.locals.len()).collect();
252        // Widest alignment first, so that placing each one straight after the last never leaves a
253        // hole bigger than the alignment the next one asked for.
254        order.sort_by_key(|&local| std::cmp::Reverse(layout.locals[local].align));
255        for local in order {
256            let Local { size, align: want } = layout.locals[local];
257            assert!(
258                want.is_power_of_two(),
259                "a local aligned to something that is not a power of 2"
260            );
261            align = align.max(want);
262            top = top.next_multiple_of(want);
263            locals[local] = offset(top);
264            top += size;
265        }
266
267        let mut slots = Vec::with_capacity(allocation.assignment.slots().len());
268        for &class in allocation.assignment.slots() {
269            let size = width(layout, class);
270            align = align.max(size);
271            top = top.next_multiple_of(size);
272            slots.push(offset(top));
273            top += size;
274        }
275
276        // Above everything the function can reach through a local, which is the whole point of it.
277        // A write that runs off the end of an array in this frame passes the canary before it
278        // reaches the saved registers and the return address, so the check at the end of the
279        // function sees a word that changed rather than a return that has already been taken.
280        let mut canary = None;
281        if layout.protect {
282            top = top.next_multiple_of(word);
283            canary = Some(offset(top));
284            top += word;
285        }
286
287        // A call reads its stack arguments from the stack pointer upward, so the outgoing area is
288        // at the bottom of the frame and its size is what shifts everything else.
289        let outgoing = if layout.leaf { 0 } else { layout.outgoing.max(conv.shadow) };
290        // Everything above it was placed as though it were not there, so moving it up by the size
291        // of the area is what would break its alignment. The area is padded to the widest
292        // alignment anything above it asked for, which costs at most that many bytes once and
293        // costs nothing at all in the usual frame, where the area is a multiple of it already.
294        // What the padding must not do is move the area itself: the callee reads its arguments
295        // from the stack pointer, so the bottom of the area is the stack pointer whatever is
296        // above it.
297        let shifted = outgoing.next_multiple_of(align);
298        let body = (top + shifted).next_multiple_of(word);
299
300        let realign = (align > conv.stack_align).then_some(align);
301        // Refused by [`crate::pipeline`] before anything gets here, because the two of them together
302        // want one register twice. See `Growing` above.
303        assert!(
304            !(layout.grows && realign.is_some()),
305            "a frame that grows and forces its alignment needs a second base register"
306        );
307        // Two frames keep one whatever the flags asked for, and each of them for its own version of
308        // the same reason: the prologue is about to leave the stack pointer somewhere no constant
309        // reaches the rest of the frame from, and the frame pointer is the register that still
310        // does. Forcing an alignment is one of the two and growing while the function runs is the
311        // other.
312        let frame_pointer = layout.frame_pointer || realign.is_some() || layout.grows;
313
314        // Where the stack pointer sits once the prologue has finished pushing: one return address
315        // short of aligned when the function starts, and one word further off for every push. The
316        // frame pointer is a push like any other here, which is why this is asked after the two
317        // frames that keep one without being asked to have said so.
318        let pushed = u32::from(frame_pointer) + u32::try_from(saved_int.len()).expect("a frame");
319        let entry = wrap(conv.stack_align, conv.return_address);
320        let after = (entry + wrap(conv.stack_align, word * pushed)) % conv.stack_align;
321
322        // A frame that grows cannot be one of the free ones. The red zone is the bytes below the
323        // stack pointer, and the first thing a variable length array does is move the stack pointer
324        // down over them, so what was in the zone would be handed out twice.
325        let free = layout.leaf
326            && layout.red_zone
327            && realign.is_none()
328            && !layout.grows
329            && align <= word
330            && body <= conv.red_zone;
331        let size = match realign {
332            _ if free => 0,
333            // Once the prologue has forced the alignment, keeping the frame a multiple of it keeps
334            // everything in the frame aligned too.
335            Some(to) => body.next_multiple_of(to),
336            // A leaf owes nobody an aligned stack pointer, so it takes exactly what it uses.
337            None if layout.leaf && align <= word => body,
338            // The smallest frame that lands the stack pointer back on a multiple of the alignment
339            // given where the pushes left it.
340            None => body + (after + conv.stack_align - body % conv.stack_align) % conv.stack_align,
341        };
342
343        // With the stack pointer left where it was, the areas are the same areas in the same order
344        // and they are below it rather than above it.
345        //
346        // A frame that grows is counted from the frame pointer instead, which is the same areas in
347        // the same order with one more constant taken off: the prologue pushed the registers and
348        // then took the frame, so the body's stack pointer is that far below where the frame
349        // pointer was set. That distance is what a variable length array destroys and the frame
350        // pointer is what is left, which is why a growing frame keeps one.
351        let mut shift = if free { -offset(body) } else { offset(shifted) };
352        if layout.grows {
353            shift -= offset(size) + offset(word) * i32::try_from(saved_int.len()).expect("a frame");
354        }
355        for at in slots
356            .iter_mut()
357            .chain(locals.iter_mut())
358            .chain(canary.iter_mut())
359            .chain(saved_sse.iter_mut().map(|save| &mut save.at))
360        {
361            *at += shift;
362        }
363
364        Self {
365            saved_int,
366            saved_sse,
367            slots,
368            locals,
369            canary,
370            outgoing,
371            below: shifted,
372            size,
373            realign,
374            incoming: if realign.is_some() || layout.grows {
375                // The prologue saves the frame pointer before it does anything else and points it
376                // at where it saved it, so the caller's stack is one word for that and one return
377                // address above it, whatever the prologue did to the stack pointer afterwards.
378                Incoming::from_frame(offset(word + conv.return_address))
379            } else {
380                Incoming::from_stack(offset(size + word * pushed + conv.return_address))
381            },
382            frame_pointer,
383            grows: layout.grows,
384        }
385    }
386
387    /// The general purpose registers the prologue pushes, in the order it pushes them.
388    ///
389    /// The frame pointer is not among them even when the convention calls it a saved register,
390    /// because a function that keeps one saves it as part of setting it up.
391    #[must_use]
392    pub fn saved_int(&self) -> &[PhysReg] {
393        &self.saved_int
394    }
395
396    /// The vector registers the prologue stores into the frame, and where each of them goes.
397    #[must_use]
398    pub fn saved_sse(&self) -> &[Save] {
399        &self.saved_sse
400    }
401
402    /// Where a spill slot is, from the stack pointer in the body of the function.
403    #[must_use]
404    pub fn slot(&self, slot: u32) -> Option<i32> {
405        self.slots.get(usize::try_from(slot).ok()?).copied()
406    }
407
408    /// Where a local is, from the stack pointer in the body of the function.
409    #[must_use]
410    pub fn local(&self, local: usize) -> Option<i32> {
411        self.locals.get(local).copied()
412    }
413
414    /// Where the stack protector's canary is, from the stack pointer in the body of the function,
415    /// or `None` in a frame that has none.
416    #[must_use]
417    pub fn canary(&self) -> Option<i32> {
418        self.canary
419    }
420
421    /// How many bytes the prologue takes off the stack pointer, which is nothing for a function
422    /// small enough and quiet enough to live in the red zone.
423    #[must_use]
424    pub fn size(&self) -> u32 {
425        self.size
426    }
427
428    /// How many bytes at the bottom of the frame belong to the arguments of calls this function
429    /// makes, which is where the shadow space goes on Windows.
430    #[must_use]
431    pub fn outgoing(&self) -> u32 {
432        self.outgoing
433    }
434
435    /// How many bytes at the bottom of the frame nothing else may be placed in, which is that area
436    /// padded to the alignment everything above it asked for.
437    ///
438    /// What a variable length array has to step over. It takes its bytes off the stack pointer,
439    /// which leaves them at the bottom of the frame where the next call is going to write its
440    /// arguments, so the address it hands out is this far above the stack pointer rather than the
441    /// stack pointer itself.
442    #[must_use]
443    pub fn below(&self) -> u32 {
444        self.below
445    }
446
447    /// Whether the function moves the stack pointer while it runs.
448    ///
449    /// Every offset in the body of such a frame is from the frame pointer rather than from the
450    /// stack pointer, because a variable length array leaves the stack pointer somewhere no
451    /// constant reaches the rest of the frame from. See `Growing` in the module documentation.
452    #[must_use]
453    pub fn grows(&self) -> bool {
454        self.grows
455    }
456
457    /// What the prologue has to force the stack pointer to be a multiple of, when a local wants
458    /// more alignment than a call leaves it with.
459    #[must_use]
460    pub fn realign(&self) -> Option<u32> {
461        self.realign
462    }
463
464    /// Where the first argument the caller passed on the stack is, and which register reaches it.
465    ///
466    /// The only offset here that is not always from the stack pointer. A realigned frame counts
467    /// from the frame pointer instead, because forcing the alignment threw away however far the
468    /// caller's stack pointer was from where the prologue wanted it, and the frame pointer is what
469    /// reaches the caller's stack afterwards.
470    #[must_use]
471    pub fn incoming(&self) -> Incoming {
472        self.incoming
473    }
474
475    /// Whether the function keeps a frame pointer.
476    #[must_use]
477    pub fn frame_pointer(&self) -> bool {
478        self.frame_pointer
479    }
480}
481
482/// The registers a call preserves that this function writes anyway, so the prologue has to put
483/// them back.
484///
485/// The rewritten function is what is read here rather than the assignment, because a spilled value
486/// is reloaded into a scratch register that no assignment mentions, and a scratch register the
487/// convention preserves is one this has to find.
488fn saved(
489    func: &Func,
490    allocation: &Allocation,
491    layout: &Layout<'_>,
492) -> (Vec<PhysReg>, Vec<PhysReg>) {
493    let mut used: Vec<(RegClass, PhysReg)> = Vec::new();
494    let mut note = |class: RegClass, at: PhysReg| {
495        if !used.contains(&(class, at)) {
496            used.push((class, at));
497        }
498    };
499    for block in func.blocks() {
500        for inst in func.insts(block) {
501            for operand in &func[func[inst].operands] {
502                if let Some(at) = operand.reg.phys() {
503                    note(operand.class, at);
504                }
505            }
506        }
507    }
508    for edit in &allocation.edits {
509        for place in [edit.mov.from, edit.mov.to] {
510            if let Place::Reg(at) = place {
511                note(edit.class, at);
512            }
513        }
514    }
515
516    let conv = layout.conv;
517    let wanted = |class: RegClass, at: PhysReg| used.contains(&(class, at));
518    // In the convention's order rather than the order the function happened to reach for them, so
519    // that two functions saving the same registers get the same prologue.
520    let saved_int = conv
521        .int_saved
522        .iter()
523        .copied()
524        .filter(|&at| wanted(conv.int_class, at))
525        .filter(|&at| !(layout.frame_pointer && at == conv.frame_pointer))
526        .collect();
527    let saved_sse =
528        conv.sse_saved.iter().copied().filter(|&at| wanted(conv.sse_class, at)).collect();
529    (saved_int, saved_sse)
530}
531
532/// How many bytes a value of a class takes on the stack.
533///
534/// A power of two at least a word wide, because a slot is addressed and an address that is not a
535/// multiple of the size of the thing at it is a fault on some machines and slow on the rest. An
536/// eighty bit `long double` takes sixteen bytes for that reason, which is what every compiler
537/// does with one.
538fn width(layout: &Layout<'_>, class: RegClass) -> u32 {
539    let bits = layout.file.class(class).map_or(0, |info| info.bits);
540    bits.div_ceil(8).max(layout.conv.word).next_power_of_two()
541}
542
543/// How far past a multiple of an alignment a number is, counted the other way: what has to be
544/// added to it to reach the next one.
545fn wrap(align: u32, value: u32) -> u32 {
546    (align - value % align) % align
547}
548
549/// A distance in a frame, as the signed number every offset out of here is.
550fn offset(bytes: u32) -> i32 {
551    i32::try_from(bytes).expect("a frame under two gigabytes")
552}
553
554#[cfg(test)]
555mod tests {
556    use rucc_base::Interner;
557    use rucc_mir::{Opcode, Operand, Reg};
558    use rucc_regalloc::assign::Env;
559    use rucc_target::x86_64::{GPR, RBP, REGS, SYSV, WIN64, XMM};
560
561    use super::*;
562
563    /// An environment offering that many of the convention's registers, with everything after
564    /// them held back as scratch.
565    fn env(conv: &CallRegs, count: usize) -> Env {
566        Env::new().with(GPR, &conv.int_order[..count], &conv.int_order[count..])
567    }
568
569    /// A function of that many values, every one of them written before any is read, allocated
570    /// with that many registers to hand out.
571    ///
572    /// Every value is live at the first read, so a count below the number of values is what puts
573    /// the function under enough pressure to spill, and each read wants one value so a reload
574    /// never needs more than one scratch register.
575    fn pressure(conv: &CallRegs, values: usize, count: usize) -> (Func, Allocation) {
576        let mut names = Interner::new();
577        let mut func = Func::new(names.intern("f"));
578        let opcode = Opcode::new(names.intern("x64.nop"));
579        let block = func.create_block();
580        let regs: Vec<Reg> = (0..values).map(|_| func.new_vreg(GPR)).collect();
581        for &reg in &regs {
582            func.build(block, opcode).def(reg, GPR).finish();
583        }
584        for &reg in &regs {
585            func.build(block, opcode).uses(reg, GPR).finish();
586        }
587        let allocation = rucc_regalloc::run(&mut func, &env(conv, count), "test");
588        (func, allocation)
589    }
590
591    /// What a list of registers is called, which is what an assertion reads.
592    fn named(regs: &[PhysReg]) -> Vec<&'static str> {
593        regs.iter().map(|&reg| REGS.name(GPR, reg).expect("a register")).collect()
594    }
595
596    #[test]
597    fn a_function_that_needs_nothing_of_the_stack_has_no_frame_at_all() {
598        let (func, allocation) = pressure(&SYSV, 2, 4);
599        let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
600
601        assert_eq!(frame.size(), 0);
602        assert_eq!(named(frame.saved_int()), Vec::<&str>::new());
603        assert_eq!(frame.slot(0), None);
604        // Nothing between the stack pointer and the return address the call pushed.
605        assert_eq!(frame.incoming(), Incoming::from_stack(8));
606    }
607
608    #[test]
609    fn a_small_leaf_function_puts_its_spills_in_the_red_zone_and_moves_nothing() {
610        let (func, allocation) = pressure(&SYSV, 4, 2);
611        let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
612
613        // Two registers for four values that are all live at once, so two are on the stack, and a
614        // leaf function small enough is entitled to the bytes below the stack pointer.
615        assert_eq!(frame.size(), 0);
616        assert_eq!((frame.slot(0), frame.slot(1)), (Some(-16), Some(-8)));
617        assert_eq!(frame.slot(2), None);
618        assert_eq!(frame.incoming(), Incoming::from_stack(8));
619    }
620
621    #[test]
622    fn a_leaf_function_told_it_has_no_red_zone_takes_the_bytes_instead() {
623        let (func, allocation) = pressure(&SYSV, 4, 2);
624        let base = Layout::new(&SYSV, REGS);
625        let frame = Frame::of(&func, &allocation, &Layout { red_zone: false, ..base });
626
627        assert_eq!(frame.size(), 16);
628        assert_eq!((frame.slot(0), frame.slot(1)), (Some(0), Some(8)));
629        assert_eq!(frame.incoming(), Incoming::from_stack(24));
630    }
631
632    #[test]
633    fn a_frame_too_big_for_the_red_zone_takes_the_bytes_whatever_else_is_true() {
634        let (func, allocation) = pressure(&SYSV, 40, 2);
635        let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
636
637        // Thirty eight values on the stack is three hundred and four bytes, and the red zone is a
638        // hundred and twenty eight.
639        assert_eq!(frame.size(), 304);
640        assert_eq!(frame.slot(0), Some(0));
641        assert_eq!(frame.slot(37), Some(296));
642    }
643
644    #[test]
645    fn a_function_that_calls_something_leaves_the_stack_pointer_where_a_call_wants_it() {
646        let (func, allocation) = pressure(&SYSV, 4, 2);
647        let base = Layout::new(&SYSV, REGS);
648        let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
649
650        // Sixteen bytes of spills, and the call that reached this function left the stack pointer
651        // eight bytes off, so the frame is eight bytes wider than the spills need and every call
652        // this function makes is correctly aligned.
653        assert_eq!(frame.size(), 24);
654        assert_eq!((frame.slot(0), frame.slot(1)), (Some(0), Some(8)));
655        assert_eq!(frame.incoming(), Incoming::from_stack(32));
656    }
657
658    #[test]
659    fn a_push_is_counted_in_the_alignment_the_frame_has_to_produce() {
660        let (func, allocation) = pressure(&SYSV, 12, 12);
661        let base = Layout::new(&SYSV, REGS);
662        let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
663
664        // Twelve values reach into the preserved end of the allocation order, so three registers
665        // are pushed, and three pushes plus the return address is a multiple of sixteen already.
666        // The frame is empty and stays empty rather than being padded for the sake of it.
667        assert_eq!(named(frame.saved_int()), ["rbx", "r12", "r13"]);
668        assert_eq!(frame.size(), 0);
669        assert_eq!(frame.incoming(), Incoming::from_stack(32));
670    }
671
672    #[test]
673    fn the_registers_a_call_leaves_alone_are_saved_in_the_order_the_convention_lists_them() {
674        let (func, allocation) = pressure(&SYSV, 13, 13);
675        let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
676
677        // Four of them now, in the convention's order rather than the order the allocator handed
678        // them out in, so that two functions saving the same registers get the same prologue.
679        assert_eq!(named(frame.saved_int()), ["rbx", "r12", "r13", "r14"]);
680    }
681
682    #[test]
683    fn a_function_that_keeps_a_frame_pointer_does_not_save_it_twice() {
684        let mut names = Interner::new();
685        let mut func = Func::new(names.intern("f"));
686        let opcode = Opcode::new(names.intern("x64.nop"));
687        let block = func.create_block();
688        // An instruction that names the frame pointer register outright, which is what a lowering
689        // rule for something that has to use it produces.
690        func.build(block, opcode).operand(Operand::write(Reg::physical(RBP), GPR)).finish();
691        let allocation = rucc_regalloc::run(&mut func, &env(&SYSV, 4), "test");
692        let base = Layout::new(&SYSV, REGS);
693
694        let kept = Frame::of(&func, &allocation, &Layout { frame_pointer: true, ..base });
695        let dropped = Frame::of(&func, &allocation, &base);
696
697        // `rbp` is a register SysV preserves, so a function that leaves it alone saves it in the
698        // ordinary way, and a function that keeps a frame pointer in it saves it as part of
699        // setting the frame pointer up instead.
700        assert_eq!(named(dropped.saved_int()), ["rbp"]);
701        assert_eq!(named(kept.saved_int()), Vec::<&str>::new());
702        assert!(kept.frame_pointer());
703    }
704
705    #[test]
706    fn locals_are_placed_widest_alignment_first_and_reported_in_the_order_they_arrived() {
707        let (func, allocation) = pressure(&SYSV, 2, 4);
708        let locals = [
709            Local { size: 1, align: 1 },
710            Local { size: 16, align: 16 },
711            Local { size: 8, align: 8 },
712        ];
713        let base = Layout::new(&SYSV, REGS);
714        let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
715
716        // The sixteen byte one is placed first, so nothing is padded to reach it, and the one
717        // byte one goes last where the padding after it costs nothing.
718        assert_eq!((frame.local(1), frame.local(2), frame.local(0)), (Some(0), Some(16), Some(24)));
719        assert_eq!(frame.local(3), None);
720        // A local wanting sixteen byte alignment is more than the stack pointer has for free, so
721        // the frame is taken rather than the red zone used, and it is padded to keep the local
722        // where it was put.
723        assert_eq!(frame.size(), 40);
724        assert_eq!(frame.realign(), None);
725    }
726
727    #[test]
728    fn a_local_wanting_more_alignment_than_a_call_gives_makes_the_prologue_force_it() {
729        let (func, allocation) = pressure(&SYSV, 2, 4);
730        let locals = [Local { size: 64, align: 32 }];
731        let base = Layout::new(&SYSV, REGS);
732        let frame = Frame::of(&func, &allocation, &Layout { locals: &locals, ..base });
733
734        assert_eq!(frame.realign(), Some(32));
735        assert_eq!(frame.local(0), Some(0));
736        assert_eq!(frame.size(), 64);
737        // Forcing the alignment throws away how far the caller's stack pointer was from where the
738        // prologue wanted it, so a frame pointer is needed and the caller's stack is reached
739        // through it instead: one word for the saved frame pointer and one for the return address.
740        assert!(frame.frame_pointer());
741        assert_eq!(frame.incoming(), Incoming::from_frame(16));
742    }
743
744    #[test]
745    fn the_canary_is_above_every_byte_a_local_or_a_spill_reaches() {
746        let (func, allocation) = pressure(&SYSV, 4, 2);
747        let locals = [Local { size: 16, align: 16 }, Local { size: 8, align: 8 }];
748        let base = Layout::new(&SYSV, REGS);
749        let there = Layout { leaf: false, locals: &locals, protect: true, ..base };
750        let frame = Frame::of(&func, &allocation, &there);
751
752        // Two spill slots at the bottom, then the two locals, then the canary above all four. That
753        // order is the whole mechanism: a write that runs off the end of either local passes the
754        // canary before it reaches the saved registers and the return address.
755        let canary = frame.canary().expect("a protected frame has a slot");
756        for below in [frame.slot(0), frame.slot(1), frame.local(0), frame.local(1)] {
757            assert!(below.expect("a slot that was asked for") < canary);
758        }
759        assert_eq!(canary, 40);
760        // Forty eight bytes of areas, and then the eight that put the stack pointer back where a
761        // call wants it, because the arm the check fails on makes one.
762        assert_eq!(frame.size(), 56);
763        assert_eq!((frame.size() + SYSV.return_address) % SYSV.stack_align, 0);
764    }
765
766    #[test]
767    fn a_frame_with_no_protector_has_no_slot_for_a_canary() {
768        let (func, allocation) = pressure(&SYSV, 2, 4);
769        let frame = Frame::of(&func, &allocation, &Layout::new(&SYSV, REGS));
770
771        assert_eq!(frame.canary(), None);
772    }
773
774    #[test]
775    fn a_call_reads_its_stack_arguments_from_the_bottom_of_the_frame() {
776        let (func, allocation) = pressure(&SYSV, 4, 2);
777        let base = Layout::new(&SYSV, REGS);
778        let frame = Frame::of(&func, &allocation, &Layout { leaf: false, outgoing: 24, ..base });
779
780        // The outgoing area is at the stack pointer, because that is where the callee will look
781        // for it, and the spills sit above it.
782        assert_eq!(frame.outgoing(), 24);
783        assert_eq!((frame.slot(0), frame.slot(1)), (Some(24), Some(32)));
784        assert_eq!(frame.size(), 40);
785    }
786
787    /// Moving everything up by the size of the outgoing area is what would break its alignment,
788    /// so the area is padded to the widest alignment anything above it wanted. The area itself
789    /// still starts at the stack pointer, because that is the one thing about it that is not this
790    /// frame's to choose.
791    #[test]
792    fn what_is_above_the_outgoing_area_keeps_the_alignment_it_asked_for() {
793        let (func, allocation) = pressure(&SYSV, 2, 4);
794        let locals = [Local { size: 16, align: 16 }];
795        let base = Layout::new(&SYSV, REGS);
796        let there = Layout { leaf: false, outgoing: 8, locals: &locals, ..base };
797        let frame = Frame::of(&func, &allocation, &there);
798
799        assert_eq!(frame.outgoing(), 8);
800        assert_eq!(frame.local(0), Some(16));
801        assert_eq!(frame.size(), 40);
802        // A call leaves the stack pointer one return address short of aligned and nothing was
803        // pushed on top of that, so the frame is what puts it back and the local lands aligned.
804        assert_eq!((frame.size() + SYSV.return_address) % SYSV.stack_align, 0);
805    }
806
807    #[test]
808    fn a_windows_call_gets_the_thirty_two_bytes_below_it_even_when_it_passes_nothing() {
809        let (func, allocation) = pressure(&WIN64, 2, 4);
810        let base = Layout::new(&WIN64, REGS);
811        let frame = Frame::of(&func, &allocation, &Layout { leaf: false, ..base });
812
813        // Windows has no red zone and every caller reserves thirty two bytes below the call for
814        // the callee to spill its register arguments into.
815        assert_eq!(frame.outgoing(), 32);
816        assert_eq!(frame.size(), 40);
817        assert_eq!(frame.incoming(), Incoming::from_stack(48));
818    }
819
820    #[test]
821    fn a_slot_is_as_wide_as_the_widest_thing_of_its_class() {
822        let base = Layout::new(&SYSV, REGS);
823
824        assert_eq!(width(&base, GPR), 8);
825        assert_eq!(width(&base, XMM), 16);
826        // A long double is eighty bits and takes sixteen bytes, because an address has to be a
827        // multiple of the size of what is at it.
828        assert_eq!(width(&base, REGS.class_named("x87").expect("a class")), 16);
829    }
830}