(** ** Location of function arguments *)
(** The RISC-V ABI states the following convention for passing arguments to a function:  - Arguments are passed in registers when possible.  - Up to eight integer registers (ai: int_param_regs) and up to eight floating-point registers (fai: float_param_regs) are used for this purpose.  - If the arguments to a function are conceptualized as fields of a C struct, each with pointer alignment, the argument registers are a shadow of the first eight pointer-words of that struct. If argument i < 8 is a floating-point type, it is passed in floating-point register fa_i; otherwise, it is passed in integer register a_i.  - When primitive arguments twice the size of a pointer-word are passed on the stack, they are naturally aligned. When they are passed in the integer registers, they reside in an aligned even-odd register pair, with the even register holding the least-significant bits.  - Floating-point arguments to variadic functions (except those that are explicitly named in the parameter list) are passed in integer registers.  - The portion of the conceptual struct that is not passed in argument registers is passed on the stack. The stack pointer sp points to the first argument not passed in a register.  The bit about variadic functions doesn't quite fit CompCert's model. We do our best by passing the FP arguments in registers, as usual, and reserving the corresponding integer registers, so that fixup code can be introduced in the Asmexpand pass. *)
Definition int_param_regs :=
R10 :: R11 :: R12 :: R13 :: R14 :: R15 :: R16 :: R17 :: nil.
Definition float_param_regs :=
F10 :: F11 :: F12 :: F13 :: F14 :: F15 :: F16 :: F17 :: nil.
Definition one_arg (regs: list mreg) (rn: Z) (ofs: Z) (ty: typ)
(rec: Z -> Z -> list (rpair loc)) :=
match list_nth_z regs rn with
| Some r =>
One(R r) :: rec (rn + 1) ofs
| None   =>
let ofs := align ofs (typealign ty) in
One(S Outgoing ofs ty) :: rec rn (ofs + (if Archi.ptr64 then 2 else typesize ty))
end.
Definition two_args (regs: list mreg) (rn: Z) (ofs: Z)
(rec: Z -> Z -> list (rpair loc)) :=
let rn := align rn 2 in
match list_nth_z regs rn, list_nth_z regs (rn + 1) with
| Some r1, Some r2 =>
Twolong (R r2) (R r1) :: rec (rn + 2) ofs
| _, _ =>
let ofs := align ofs 2 in
Twolong (S Outgoing (ofs + 1) Tint) (S Outgoing ofs Tint) ::
rec rn (ofs + 2)
end.
Definition hybrid_arg (regs: list mreg) (rn: Z) (ofs: Z) (ty: typ)
(rec: Z -> Z -> list (rpair loc)) :=
let rn := align rn 2 in
match list_nth_z regs rn with
| Some r =>
One (R r) :: rec (rn + 2) ofs
| None =>
let ofs := align ofs 2 in
One (S Outgoing ofs ty) :: rec rn (ofs + 2)
end.
Fixpoint loc_arguments_rec (va: bool)
(tyl: list typ) (r ofs: Z) {struct tyl} : list (rpair loc) :=
match tyl with
| nil => nil
| (Tint | Tany32) as ty :: tys =>
one_arg int_param_regs r ofs ty (loc_arguments_rec va tys)
| Tsingle as ty :: tys =>
one_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
| Tlong as ty :: tys =>
if Archi.ptr64
then one_arg int_param_regs r ofs ty (loc_arguments_rec va tys)
else two_args int_param_regs r ofs  (loc_arguments_rec va tys)
| (Tfloat | Tany64) as ty :: tys =>
if va && negb Archi.ptr64
then hybrid_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
else one_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
end.
(** [loc_arguments s] returns the list of locations where to store arguments when calling a function with signature [s].  *)
Definition loc_arguments (s: signature) : list (rpair loc) :=
loc_arguments_rec s.(sig_cc).(cc_vararg) s.(sig_args) 0 0.
(** [size_arguments s] returns the number of [Outgoing] slots used to call a function with signature [s]. *)
Definition max_outgoing_1 (accu: Z) (l: loc) : Z :=
match l with
| S Outgoing ofs ty => Z.max accu (ofs + typesize ty)
| _ => accu
end.
Definition max_outgoing_2 (accu: Z) (rl: rpair loc) : Z :=
match rl with
| One l => max_outgoing_1 accu l
| Twolong l1 l2 => max_outgoing_1 (max_outgoing_1 accu l1) l2
end.
Definition size_arguments (s: signature) : Z :=
List.fold_left max_outgoing_2 (loc_arguments s) 0.
(** Argument locations are either non-temporary registers or [Outgoing] stack slots at nonnegative offsets. *)
Definition loc_argument_acceptable (l: loc) : Prop :=
match l with
| R r => is_callee_save r = false
| S Outgoing ofs ty => ofs >= 0 /\ (typealign ty | ofs)
| _ => False
end.

Lemma loc_arguments_acceptable:
forall (s: signature) (p: rpair loc),
In p (loc_arguments s) -> forall_rpair loc_argument_acceptable p.