Skip to content

Commit fc1b2bf

Browse files
committed
ARM: tweak stack layout so that back link and return address are lower
This reduces the chances that back link and return address cannot be saved by a single str instruction. We generate correct code for the overflow case, but the code isn't very efficient, so let's make it uncommon.
1 parent 6c34468 commit fc1b2bf

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

arm/Stacklayout.v

+34-37
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ Require Import Bounds.
1919
(** The general shape of activation records is as follows,
2020
from bottom (lowest offsets) to top:
2121
- Space for outgoing arguments to function calls.
22-
- Local stack slots.
23-
- Saved values of integer callee-save registers used by the function.
24-
- Saved values of float callee-save registers used by the function.
25-
- Saved return address into caller.
2622
- Pointer to activation record of the caller.
23+
- Saved return address into caller.
24+
- Local stack slots.
25+
- Saved values of callee-save registers used by the function.
2726
- Space for the stack-allocated data declared in Cminor.
2827
2928
The [frame_env] compilation environment records the positions of
@@ -36,11 +35,11 @@ Definition fe_ofs_arg := 0.
3635
function. *)
3736

3837
Definition make_env (b: bounds) :=
39-
let ol := align (4 * b.(bound_outgoing)) 8 in (* locals *)
38+
let olink := 4 * b.(bound_outgoing) in (* back link*)
39+
let ora := olink + 4 in (* return address *)
40+
let ol := align (ora + 4) 8 in (* locals *)
4041
let ocs := ol + 4 * b.(bound_local) in (* callee-saves *)
41-
let ora := align (size_callee_save_area b ocs) 4 in (* retaddr *)
42-
let olink := ora + 4 in (* back link *)
43-
let ostkdata := align (olink + 4) 8 in (* stack data *)
42+
let ostkdata := align (size_callee_save_area b ocs) 8 in (* retaddr *)
4443
let sz := align (ostkdata + b.(bound_stack_data)) 8 in
4544
{| fe_size := sz;
4645
fe_ofs_link := olink;
@@ -67,33 +66,32 @@ Lemma frame_env_separated:
6766
Proof.
6867
Local Opaque Z.add Z.mul sepconj range.
6968
intros; simpl.
70-
set (ol := align (4 * b.(bound_outgoing)) 8);
69+
set (olink := 4 * b.(bound_outgoing));
70+
set (ora := olink + 4);
71+
set (ol := align (ora + 4) 8);
7172
set (ocs := ol + 4 * b.(bound_local));
72-
set (ora := align (size_callee_save_area b ocs) 4);
73-
set (olink := ora + 4);
74-
set (ostkdata := align (olink + 4) 8).
73+
set (ostkdata := align (size_callee_save_area b ocs) 8).
7574
generalize b.(bound_local_pos) b.(bound_outgoing_pos) b.(bound_stack_data_pos); intros.
76-
assert (4 * b.(bound_outgoing) <= ol) by (apply align_le; omega).
75+
assert (0 <= olink) by (unfold olink; omega).
76+
assert (olink <= ora) by (unfold ora; omega).
77+
assert (ora + 4 <= ol) by (apply align_le; omega).
7778
assert (ol + 4 * b.(bound_local) <= ocs) by (unfold ocs; omega).
7879
assert (ocs <= size_callee_save_area b ocs) by apply size_callee_save_area_incr.
79-
assert (size_callee_save_area b ocs <= ora) by (apply align_le; omega).
80-
assert (ora <= olink) by (unfold olink; omega).
81-
assert (olink + 4 <= ostkdata) by (apply align_le; omega).
80+
assert (size_callee_save_area b ocs <= ostkdata) by (apply align_le; omega).
8281
(* Reorder as:
8382
outgoing
84-
local
85-
callee-save
83+
back link
8684
retaddr
87-
back link *)
85+
local
86+
callee-save *)
8887
rewrite sep_swap12.
89-
rewrite sep_swap45.
88+
rewrite sep_swap23.
9089
rewrite sep_swap34.
91-
rewrite sep_swap45.
9290
(* Apply range_split and range_split2 repeatedly *)
9391
unfold fe_ofs_arg.
94-
apply range_split_2. fold ol; omega. omega.
9592
apply range_split. omega.
96-
apply range_split_2. fold ora; omega. omega.
93+
apply range_split. omega.
94+
apply range_split_2. fold ol; omega. omega.
9795
apply range_split. omega.
9896
apply range_drop_right with ostkdata. omega.
9997
eapply sep_drop2. eexact H.
@@ -105,18 +103,18 @@ Lemma frame_env_range:
105103
0 <= fe_stack_data fe /\ fe_stack_data fe + bound_stack_data b <= fe_size fe.
106104
Proof.
107105
intros; simpl.
108-
set (ol := align (4 * b.(bound_outgoing)) 8);
106+
set (olink := 4 * b.(bound_outgoing));
107+
set (ora := olink + 4);
108+
set (ol := align (ora + 4) 8);
109109
set (ocs := ol + 4 * b.(bound_local));
110-
set (ora := align (size_callee_save_area b ocs) 4);
111-
set (olink := ora + 4);
112-
set (ostkdata := align (olink + 4) 8).
110+
set (ostkdata := align (size_callee_save_area b ocs) 8).
113111
generalize b.(bound_local_pos) b.(bound_outgoing_pos) b.(bound_stack_data_pos); intros.
114-
assert (4 * b.(bound_outgoing) <= ol) by (apply align_le; omega).
112+
assert (0 <= olink) by (unfold olink; omega).
113+
assert (olink <= ora) by (unfold ora; omega).
114+
assert (ora + 4 <= ol) by (apply align_le; omega).
115115
assert (ol + 4 * b.(bound_local) <= ocs) by (unfold ocs; omega).
116116
assert (ocs <= size_callee_save_area b ocs) by apply size_callee_save_area_incr.
117-
assert (size_callee_save_area b ocs <= ora) by (apply align_le; omega).
118-
assert (ora <= olink) by (unfold olink; omega).
119-
assert (olink + 4 <= ostkdata) by (apply align_le; omega).
117+
assert (size_callee_save_area b ocs <= ostkdata) by (apply align_le; omega).
120118
split. omega. apply align_le; omega.
121119
Qed.
122120

@@ -130,14 +128,13 @@ Lemma frame_env_aligned:
130128
/\ (4 | fe_ofs_retaddr fe).
131129
Proof.
132130
intros; simpl.
133-
set (ol := align (4 * b.(bound_outgoing)) 8);
131+
set (olink := 4 * b.(bound_outgoing));
132+
set (ora := olink + 4);
133+
set (ol := align (ora + 4) 8);
134134
set (ocs := ol + 4 * b.(bound_local));
135-
set (ora := align (size_callee_save_area b ocs) 4);
136-
set (olink := ora + 4);
137-
set (ostkdata := align (olink + 4) 8).
135+
set (ostkdata := align (size_callee_save_area b ocs) 8).
138136
split. apply Zdivide_0.
139137
split. apply align_divides; omega.
140138
split. apply align_divides; omega.
141-
split. apply Z.divide_add_r. apply align_divides; omega. apply Z.divide_refl.
142-
apply align_divides; omega.
139+
unfold ora, olink; auto using Z.divide_mul_l, Z.divide_add_r, Z.divide_refl.
143140
Qed.

0 commit comments

Comments
 (0)