(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  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.     *)
(*                                                                     *)
(* *********************************************************************)
(** Correctness proof for Asm generation: machine-independent framework *)
Require Intv.
(** * Processor registers and register states *)
Hint Extern 2 (_ <> _) => congruence: asmgen.
Hint Resolve preg_of_data: asmgen.
Hint Resolve data_diff: asmgen.
Hint Resolve preg_of_not_SP preg_of_not_PC: asmgen.
Fixpoint preg_notin (r: preg) (rl: list mreg) : Prop :=
match rl with
| nil => True
| r1 :: nil => r <> preg_of r1
| r1 :: rl => r <> preg_of r1 /\ preg_notin r rl
end.
Remark preg_notin_charact:
forall r rl,
preg_notin r rl <-> (forall mr, In mr rl -> r <> preg_of mr).
(** * Agreement between Mach registers and processor registers *)
Record agree (ms: Mach.regset) (sp: val) (rs: Asm.regset) : Prop := mkagree {
agree_sp: rs#SP = sp;
agree_sp_def: sp <> Vundef;
agree_mregs: forall r: mreg, Val.lessdef (ms r) (rs#(preg_of r))
}.
(** Preservation of register agreement under various assignments. *)
(** Connection between Mach and Asm calling conventions for external functions. *)
(** Translation of arguments and results to builtins. *)
Remark builtin_arg_match:
forall ge (rs: regset) sp m a v,
eval_builtin_arg ge (fun r => rs (preg_of r)) sp m a v ->
eval_builtin_arg ge rs sp m (map_builtin_arg preg_of a) v.
(** * Correspondence between Mach code and Asm code *)
(** The ``code tail'' of an instruction list [c] is the list of instructions starting at PC [pos]. *)
Inductive code_tail: Z -> code -> code -> Prop :=
| code_tail_0: forall c,
code_tail 0 c c
| code_tail_S: forall pos i c1 c2,
code_tail pos c1 c2 ->
code_tail (pos + 1) (i :: c1) c2.
Remark code_tail_bounds_1:
forall fn ofs c,
code_tail ofs fn c -> 0 <= ofs <= list_length_z fn.
Remark code_tail_bounds_2:
forall fn ofs i c,
code_tail ofs fn (i :: c) -> 0 <= ofs < list_length_z fn.
(** [transl_code_at_pc pc fb f c ep tf tc] holds if the code pointer [pc] points within the Asm code generated by translating Mach function [f], and [tc] is the tail of the generated code at the position corresponding to the code pointer [pc]. *)
Inductive transl_code_at_pc (ge: Mach.genv):
val -> block -> Mach.function -> Mach.code -> bool -> Asm.function -> Asm.code -> Prop :=
transl_code_at_pc_intro:
forall b ofs f c ep tf tc,
Genv.find_funct_ptr ge b = Some(Internal f) ->
transf_function f = Errors.OK tf ->
transl_code f c ep = OK tc ->
code_tail (Ptrofs.unsigned ofs) (fn_code tf) tc ->
transl_code_at_pc ge (Vptr b ofs) b f c ep tf tc.
(** Equivalence between [transl_code] and [transl_code']. *)
Local Open Scope error_monad_scope.
(** Predictor for return addresses in generated Asm code.  The [return_address_offset] predicate defined here is used in the semantics for Mach to determine the return addresses that are stored in activation records. *)
(** Consider a Mach function [f] and a sequence [c] of Mach instructions representing the Mach code that remains to be executed after a function call returns.  The predicate [return_address_offset f c ofs] holds if [ofs] is the integer offset of the PPC instruction following the call in the Asm code obtained by translating the code of [f]. Graphically: << Mach function f    |--------- Mcall ---------| Mach code c    |                |--------| |                 \        \ |                  \        \ |                   \        \ Asm code           |                    |--------| Asm function       |------------- Pcall ---------|  <-------- ofs -------> >> *)
Definition return_address_offset (f: Mach.function) (c: Mach.code) (ofs: ptrofs) : Prop :=
forall tf tc,
transf_function f = OK tf ->
transl_code f c false = OK tc ->
code_tail (Ptrofs.unsigned ofs) (fn_code tf) tc.
(** We now show that such an offset always exists if the Mach code [c] is a suffix of [f.(fn_code)].  This holds because the translation from Mach to PPC is compositional: each Mach instruction becomes zero, one or several PPC instructions, but the order of instructions is preserved. *)

Lemma is_tail_code_tail:
forall c1 c2, is_tail c1 c2 -> exists ofs, code_tail ofs c2 c1.