Skip to content

Commit 08efc2a

Browse files
authored
Platform-independent implementation of Conventions.size_arguments (#222)
The "size_arguments" function and its properties can be systematically derived from the "loc_arguments" function and its properties. Before, the RISC-V port used this derivation, and all other ports used hand-written "size_arguments" functions and proofs. This commit moves the definition of "size_arguments" to the platform-independent file backend/Conventions.v, using the systematic derivation, and removes the platform-specific definitions. This reduces code and proof size, and makes it easier to change the calling conventions.
1 parent 3bdb983 commit 08efc2a

File tree

7 files changed

+68
-649
lines changed

7 files changed

+68
-649
lines changed

aarch64/Conventions1.v

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,6 @@ Fixpoint loc_arguments_rec
190190
Definition loc_arguments (s: signature) : list (rpair loc) :=
191191
loc_arguments_rec s.(sig_args) 0 0 0.
192192

193-
(** [size_arguments s] returns the number of [Outgoing] slots used
194-
to call a function with signature [s]. *)
195-
196-
Fixpoint size_arguments_rec (tyl: list typ) (ir fr ofs: Z) {struct tyl} : Z :=
197-
match tyl with
198-
| nil => ofs
199-
| (Tint | Tlong | Tany32 | Tany64) :: tys =>
200-
match list_nth_z int_param_regs ir with
201-
| None => size_arguments_rec tys ir fr (ofs + 2)
202-
| Some ireg => size_arguments_rec tys (ir + 1) fr ofs
203-
end
204-
| (Tfloat | Tsingle) :: tys =>
205-
match list_nth_z float_param_regs fr with
206-
| None => size_arguments_rec tys ir fr (ofs + 2)
207-
| Some freg => size_arguments_rec tys ir (fr + 1) ofs
208-
end
209-
end.
210-
211-
Definition size_arguments (s: signature) : Z :=
212-
size_arguments_rec s.(sig_args) 0 0 0.
213-
214193
(** Argument locations are either caller-save registers or [Outgoing]
215194
stack slots at nonnegative offsets. *)
216195

@@ -285,92 +264,6 @@ Qed.
285264

286265
Hint Resolve loc_arguments_acceptable: locs.
287266

288-
(** The offsets of [Outgoing] arguments are below [size_arguments s]. *)
289-
290-
Remark size_arguments_rec_above:
291-
forall tyl ir fr ofs0,
292-
ofs0 <= size_arguments_rec tyl ir fr ofs0.
293-
Proof.
294-
induction tyl; simpl; intros.
295-
omega.
296-
assert (A: ofs0 <=
297-
match list_nth_z int_param_regs ir with
298-
| Some _ => size_arguments_rec tyl (ir + 1) fr ofs0
299-
| None => size_arguments_rec tyl ir fr (ofs0 + 2)
300-
end).
301-
{ destruct (list_nth_z int_param_regs ir); eauto.
302-
apply Z.le_trans with (ofs0 + 2); auto. omega. }
303-
assert (B: ofs0 <=
304-
match list_nth_z float_param_regs fr with
305-
| Some _ => size_arguments_rec tyl ir (fr + 1) ofs0
306-
| None => size_arguments_rec tyl ir fr (ofs0 + 2)
307-
end).
308-
{ destruct (list_nth_z float_param_regs fr); eauto.
309-
apply Z.le_trans with (ofs0 + 2); auto. omega. }
310-
destruct a; auto.
311-
Qed.
312-
313-
Lemma size_arguments_above:
314-
forall s, size_arguments s >= 0.
315-
Proof.
316-
intros; unfold size_arguments. apply Z.le_ge. apply size_arguments_rec_above.
317-
Qed.
318-
319-
Lemma loc_arguments_rec_bounded:
320-
forall ofs ty tyl ir fr ofs0,
321-
In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments_rec tyl ir fr ofs0)) ->
322-
ofs + typesize ty <= size_arguments_rec tyl ir fr ofs0.
323-
Proof.
324-
induction tyl; simpl; intros.
325-
- contradiction.
326-
- assert (T: forall ty0, typesize ty0 <= 2).
327-
{ destruct ty0; simpl; omega. }
328-
assert (A: forall ty0,
329-
In (S Outgoing ofs ty) (regs_of_rpairs
330-
match list_nth_z int_param_regs ir with
331-
| Some ireg =>
332-
One (R ireg) :: loc_arguments_rec tyl (ir + 1) fr ofs0
333-
| None => One (S Outgoing ofs0 ty0) :: loc_arguments_rec tyl ir fr (ofs0 + 2)
334-
end) ->
335-
ofs + typesize ty <=
336-
match list_nth_z int_param_regs ir with
337-
| Some _ => size_arguments_rec tyl (ir + 1) fr ofs0
338-
| None => size_arguments_rec tyl ir fr (ofs0 + 2)
339-
end).
340-
{ intros. destruct (list_nth_z int_param_regs ir); simpl in H0; destruct H0.
341-
- discriminate.
342-
- eapply IHtyl; eauto.
343-
- inv H0. apply Z.le_trans with (ofs + 2). specialize (T ty). omega. apply size_arguments_rec_above.
344-
- eapply IHtyl; eauto. }
345-
assert (B: forall ty0,
346-
In (S Outgoing ofs ty) (regs_of_rpairs
347-
match list_nth_z float_param_regs fr with
348-
| Some ireg =>
349-
One (R ireg) :: loc_arguments_rec tyl ir (fr + 1) ofs0
350-
| None => One (S Outgoing ofs0 ty0) :: loc_arguments_rec tyl ir fr (ofs0 + 2)
351-
end) ->
352-
ofs + typesize ty <=
353-
match list_nth_z float_param_regs fr with
354-
| Some _ => size_arguments_rec tyl ir (fr + 1) ofs0
355-
| None => size_arguments_rec tyl ir fr (ofs0 + 2)
356-
end).
357-
{ intros. destruct (list_nth_z float_param_regs fr); simpl in H0; destruct H0.
358-
- discriminate.
359-
- eapply IHtyl; eauto.
360-
- inv H0. apply Z.le_trans with (ofs + 2). specialize (T ty). omega. apply size_arguments_rec_above.
361-
- eapply IHtyl; eauto. }
362-
destruct a; eauto.
363-
Qed.
364-
365-
Lemma loc_arguments_bounded:
366-
forall (s: signature) (ofs: Z) (ty: typ),
367-
In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments s)) ->
368-
ofs + typesize ty <= size_arguments s.
369-
Proof.
370-
unfold loc_arguments, size_arguments; intros.
371-
eauto using loc_arguments_rec_bounded.
372-
Qed.
373-
374267
Lemma loc_arguments_main:
375268
loc_arguments signature_main = nil.
376269
Proof.

arm/Conventions1.v

Lines changed: 0 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -269,48 +269,6 @@ Definition loc_arguments (s: signature) : list (rpair loc) :=
269269
else loc_arguments_hf s.(sig_args) 0 0 0
270270
end.
271271

272-
(** [size_arguments s] returns the number of [Outgoing] slots used
273-
to call a function with signature [s]. *)
274-
275-
Fixpoint size_arguments_hf (tyl: list typ) (ir fr ofs: Z) {struct tyl} : Z :=
276-
match tyl with
277-
| nil => ofs
278-
| (Tint|Tany32) :: tys =>
279-
if zlt ir 4
280-
then size_arguments_hf tys (ir + 1) fr ofs
281-
else size_arguments_hf tys ir fr (ofs + 1)
282-
| (Tfloat|Tany64) :: tys =>
283-
if zlt fr 8
284-
then size_arguments_hf tys ir (fr + 1) ofs
285-
else size_arguments_hf tys ir fr (align ofs 2 + 2)
286-
| Tsingle :: tys =>
287-
if zlt fr 8
288-
then size_arguments_hf tys ir (fr + 1) ofs
289-
else size_arguments_hf tys ir fr (ofs + 1)
290-
| Tlong :: tys =>
291-
let ir := align ir 2 in
292-
if zlt ir 4
293-
then size_arguments_hf tys (ir + 2) fr ofs
294-
else size_arguments_hf tys ir fr (align ofs 2 + 2)
295-
end.
296-
297-
Fixpoint size_arguments_sf (tyl: list typ) (ofs: Z) {struct tyl} : Z :=
298-
match tyl with
299-
| nil => Z.max 0 ofs
300-
| (Tint | Tsingle | Tany32) :: tys => size_arguments_sf tys (ofs + 1)
301-
| (Tfloat | Tlong | Tany64) :: tys => size_arguments_sf tys (align ofs 2 + 2)
302-
end.
303-
304-
Definition size_arguments (s: signature) : Z :=
305-
match Archi.abi with
306-
| Archi.Softfloat =>
307-
size_arguments_sf s.(sig_args) (-4)
308-
| Archi.Hardfloat =>
309-
if s.(sig_cc).(cc_vararg)
310-
then size_arguments_sf s.(sig_args) (-4)
311-
else size_arguments_hf s.(sig_args) 0 0 0
312-
end.
313-
314272
(** Argument locations are either non-temporary registers or [Outgoing]
315273
stack slots at nonnegative offsets. *)
316274

@@ -471,170 +429,6 @@ Qed.
471429

472430
Hint Resolve loc_arguments_acceptable: locs.
473431

474-
(** The offsets of [Outgoing] arguments are below [size_arguments s]. *)
475-
476-
Remark size_arguments_hf_above:
477-
forall tyl ir fr ofs0,
478-
ofs0 <= size_arguments_hf tyl ir fr ofs0.
479-
Proof.
480-
induction tyl; simpl; intros.
481-
omega.
482-
destruct a.
483-
destruct (zlt ir 4); eauto. apply Z.le_trans with (ofs0 + 1); auto; omega.
484-
destruct (zlt fr 8); eauto.
485-
apply Z.le_trans with (align ofs0 2). apply align_le; omega.
486-
apply Z.le_trans with (align ofs0 2 + 2); auto; omega.
487-
set (ir' := align ir 2).
488-
destruct (zlt ir' 4); eauto.
489-
apply Z.le_trans with (align ofs0 2). apply align_le; omega.
490-
apply Z.le_trans with (align ofs0 2 + 2); auto; omega.
491-
destruct (zlt fr 8); eauto.
492-
apply Z.le_trans with (ofs0 + 1); eauto. omega.
493-
destruct (zlt ir 4); eauto. apply Z.le_trans with (ofs0 + 1); auto; omega.
494-
destruct (zlt fr 8); eauto.
495-
apply Z.le_trans with (align ofs0 2). apply align_le; omega.
496-
apply Z.le_trans with (align ofs0 2 + 2); auto; omega.
497-
Qed.
498-
499-
Remark size_arguments_sf_above:
500-
forall tyl ofs0,
501-
Z.max 0 ofs0 <= size_arguments_sf tyl ofs0.
502-
Proof.
503-
induction tyl; simpl; intros.
504-
omega.
505-
destruct a; (eapply Z.le_trans; [idtac|eauto]).
506-
xomega.
507-
assert (ofs0 <= align ofs0 2) by (apply align_le; omega). xomega.
508-
assert (ofs0 <= align ofs0 2) by (apply align_le; omega). xomega.
509-
xomega.
510-
xomega.
511-
assert (ofs0 <= align ofs0 2) by (apply align_le; omega). xomega.
512-
Qed.
513-
514-
Lemma size_arguments_above:
515-
forall s, size_arguments s >= 0.
516-
Proof.
517-
intros; unfold size_arguments. apply Z.le_ge.
518-
assert (0 <= size_arguments_sf (sig_args s) (-4)).
519-
{ change 0 with (Z.max 0 (-4)). apply size_arguments_sf_above. }
520-
assert (0 <= size_arguments_hf (sig_args s) 0 0 0).
521-
{ apply size_arguments_hf_above. }
522-
destruct Archi.abi; [ | destruct (cc_vararg (sig_cc s)) ]; auto.
523-
Qed.
524-
525-
Lemma loc_arguments_hf_bounded:
526-
forall ofs ty tyl ir fr ofs0,
527-
In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments_hf tyl ir fr ofs0)) ->
528-
ofs + typesize ty <= size_arguments_hf tyl ir fr ofs0.
529-
Proof.
530-
induction tyl; simpl; intros.
531-
elim H.
532-
destruct a.
533-
- (* int *)
534-
destruct (zlt ir 4); destruct H.
535-
discriminate.
536-
eauto.
537-
inv H. apply size_arguments_hf_above.
538-
eauto.
539-
- (* float *)
540-
destruct (zlt fr 8); destruct H.
541-
discriminate.
542-
eauto.
543-
inv H. apply size_arguments_hf_above.
544-
eauto.
545-
- (* long *)
546-
destruct (zlt (align ir 2) 4).
547-
destruct H. discriminate. destruct H. discriminate. eauto.
548-
destruct Archi.big_endian.
549-
destruct H. inv H.
550-
eapply Z.le_trans. 2: apply size_arguments_hf_above. simpl; omega.
551-
destruct H. inv H.
552-
rewrite <- Z.add_assoc. simpl. apply size_arguments_hf_above.
553-
eauto.
554-
destruct H. inv H.
555-
rewrite <- Z.add_assoc. simpl. apply size_arguments_hf_above.
556-
destruct H. inv H.
557-
eapply Z.le_trans. 2: apply size_arguments_hf_above. simpl; omega.
558-
eauto.
559-
- (* float *)
560-
destruct (zlt fr 8); destruct H.
561-
discriminate.
562-
eauto.
563-
inv H. apply size_arguments_hf_above.
564-
eauto.
565-
- (* any32 *)
566-
destruct (zlt ir 4); destruct H.
567-
discriminate.
568-
eauto.
569-
inv H. apply size_arguments_hf_above.
570-
eauto.
571-
- (* any64 *)
572-
destruct (zlt fr 8); destruct H.
573-
discriminate.
574-
eauto.
575-
inv H. apply size_arguments_hf_above.
576-
eauto.
577-
Qed.
578-
579-
Lemma loc_arguments_sf_bounded:
580-
forall ofs ty tyl ofs0,
581-
In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments_sf tyl ofs0)) ->
582-
Z.max 0 (ofs + typesize ty) <= size_arguments_sf tyl ofs0.
583-
Proof.
584-
induction tyl; simpl; intros.
585-
elim H.
586-
destruct a.
587-
- (* int *)
588-
destruct H.
589-
destruct (zlt ofs0 0); inv H. apply size_arguments_sf_above.
590-
eauto.
591-
- (* float *)
592-
destruct H.
593-
destruct (zlt (align ofs0 2) 0); inv H. apply size_arguments_sf_above.
594-
eauto.
595-
- (* long *)
596-
destruct H.
597-
destruct Archi.big_endian.
598-
destruct (zlt (align ofs0 2) 0); inv H.
599-
eapply Z.le_trans. 2: apply size_arguments_sf_above. simpl; xomega.
600-
destruct (zlt (align ofs0 2) 0); inv H.
601-
rewrite <- Z.add_assoc. simpl. apply size_arguments_sf_above.
602-
destruct H.
603-
destruct Archi.big_endian.
604-
destruct (zlt (align ofs0 2) 0); inv H.
605-
rewrite <- Z.add_assoc. simpl. apply size_arguments_sf_above.
606-
destruct (zlt (align ofs0 2) 0); inv H.
607-
eapply Z.le_trans. 2: apply size_arguments_sf_above. simpl; xomega.
608-
eauto.
609-
- (* float *)
610-
destruct H.
611-
destruct (zlt ofs0 0); inv H. apply size_arguments_sf_above.
612-
eauto.
613-
- (* any32 *)
614-
destruct H.
615-
destruct (zlt ofs0 0); inv H. apply size_arguments_sf_above.
616-
eauto.
617-
- (* any64 *)
618-
destruct H.
619-
destruct (zlt (align ofs0 2) 0); inv H. apply size_arguments_sf_above.
620-
eauto.
621-
Qed.
622-
623-
Lemma loc_arguments_bounded:
624-
forall (s: signature) (ofs: Z) (ty: typ),
625-
In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments s)) ->
626-
ofs + typesize ty <= size_arguments s.
627-
Proof.
628-
unfold loc_arguments, size_arguments; intros.
629-
assert (In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments_sf (sig_args s) (-4))) ->
630-
ofs + typesize ty <= size_arguments_sf (sig_args s) (-4)).
631-
{ intros. eapply Z.le_trans. 2: eapply loc_arguments_sf_bounded; eauto. xomega. }
632-
assert (In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments_hf (sig_args s) 0 0 0)) ->
633-
ofs + typesize ty <= size_arguments_hf (sig_args s) 0 0 0).
634-
{ intros. eapply loc_arguments_hf_bounded; eauto. }
635-
destruct Archi.abi; [ | destruct (cc_vararg (sig_cc s)) ]; eauto.
636-
Qed.
637-
638432
Lemma loc_arguments_main:
639433
loc_arguments signature_main = nil.
640434
Proof.

0 commit comments

Comments
 (0)