(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*                Xavier Leroy, INRIA Paris                            *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)
(** Function calling conventions and other conventions regarding the use of machine registers and stack slots. *)
(** * Classification of machine registers *)
(** Machine registers (type [mreg] in module [Locations]) are divided in the following groups: - Callee-save registers, whose value is preserved across a function call. - Caller-save registers that can be modified during a function call.  We follow the x86-32 and x86-64 application binary interfaces (ABI) in our choice of callee- and caller-save registers. *)
Definition is_callee_save (r: mreg) : bool :=
match r with
| AX | CX | DX => false
| BX | BP => true
| SI | DI => negb Archi.ptr64 (**r callee-save in 32 bits but not in 64 bits *)
| R8 | R9 | R10 | R11 => false
| R12 | R13 | R14 | R15 => true
| X0 | X1 | X2 | X3 | X4 | X5 | X6 | X7 => false
| X8 | X9 | X10 | X11 | X12 | X13 | X14 | X15 => false
| FP0 => false
end.
Definition int_caller_save_regs :=
if Archi.ptr64
then AX :: CX :: DX :: SI :: DI :: R8 :: R9 :: R10 :: R11 :: nil
else AX :: CX :: DX :: nil.
Definition float_caller_save_regs :=
if Archi.ptr64
then X0 :: X1 :: X2 :: X3 :: X4 :: X5 :: X6 :: X7 ::
X8 :: X9 :: X10 :: X11 :: X12 :: X13 :: X14 :: X15 :: nil
else X0 :: X1 :: X2 :: X3 :: X4 :: X5 :: X6 :: X7 :: nil.
Definition int_callee_save_regs :=
if Archi.ptr64
then BX :: BP :: R12 :: R13 :: R14 :: R15 :: nil
else BX :: SI :: DI :: BP :: nil.
Definition float_callee_save_regs : list mreg := nil.
Definition destroyed_at_call :=
List.filter (fun r => negb (is_callee_save r)) all_mregs.
Definition dummy_int_reg := AX.     (**r Used in [Regalloc]. *)
Definition dummy_float_reg := X0.   (**r Used in [Regalloc]. *)
Definition callee_save_type := mreg_type.
Definition is_float_reg (r: mreg) :=
match r with
| AX | BX | CX | DX | SI | DI | BP
| R8 | R9 | R10 | R11 | R12 | R13 | R14 | R15 => false
| X0 | X1 | X2 | X3 | X4 | X5 | X6 | X7
| X8 | X9 | X10 | X11 | X12 | X13 | X14 | X15 | FP0 => true
end.
(** * Function calling conventions *)
(** The functions in this section determine the locations (machine registers and stack slots) used to communicate arguments and results between the caller and the callee during function calls.  These locations are functions of the signature of the function and of the call instruction. Agreement between the caller and the callee on the locations to use is guaranteed by our dynamic semantics for Cminor and RTL, which demand that the signature of the call instruction is identical to that of the called function.  Calling conventions are largely arbitrary: they must respect the properties proved in this section (such as no overlapping between the locations of function arguments), but this leaves much liberty in choosing actual locations.  To ensure binary interoperability of code generated by our compiler with libraries compiled by another compiler, we implement the standard x86-32 and x86-64 conventions. *)
(** ** Location of function result *)
(** In 32 bit mode, the result value of a function is passed back to the caller in registers [AX] or [DX:AX] or [FP0], depending on the type of the returned value.  We treat a function without result as a function with one integer result. *)
Definition loc_result_32 (s: signature) : rpair mreg :=
match proj_sig_res s with
| Tint | Tany32 => One AX
| Tfloat | Tsingle => One FP0
| Tany64 => One X0
| Tlong => Twolong DX AX
end.
(** In 64 bit mode, he result value of a function is passed back to the caller in registers [AX] or [X0]. *)
Definition loc_result_64 (s: signature) : rpair mreg :=
match proj_sig_res s with
| Tint | Tlong | Tany32 | Tany64 => One AX
| Tfloat | Tsingle => One X0
end.
Definition loc_result :=
if Archi.ptr64 then loc_result_64 else loc_result_32.
(** The result registers have types compatible with that given in the signature. *)
(** The result locations are caller-save registers *)
(** If the result is in a pair of registers, those registers are distinct and have type [Tint] at least. *)

Lemma loc_result_pair:
forall sg,
match loc_result sg with
| One _ => True
| Twolong r1 r2 =>
r1 <> r2 /\ proj_sig_res sg = Tlong
/\ subtype Tint (mreg_type r1) = true /\ subtype Tint (mreg_type r2) = true
/\ Archi.ptr64 = false
end.