(* *********************************************************************)
(*                                                                     *)
(*              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.     *)
(*                                                                     *)
(* *********************************************************************)
(** Corollaries of the main semantic preservation theorem. *)
(** * Preservation of whole-program behaviors *)
(** From the simulation diagrams proved in file [Compiler]. it follows that whole-program observable behaviors are preserved in the following sense. First, every behavior of the generated assembly code is matched by a behavior of the source C code.  The behavior [beh] of the assembly code is either identical to the behavior [beh'] of the source C code or ``improves upon'' [beh']  by replacing a ``going wrong'' behavior with a more defined behavior. *)
(** As a corollary, if the source C code cannot go wrong, i.e. is free of undefined behaviors, the behavior of the generated assembly code is one of the possible behaviors of the source C code. *)
(** If we consider the C evaluation strategy implemented by the compiler, we get stronger preservation results. *)
(** We can also use the alternate big-step semantics for [Cstrategy] to establish behaviors of the generated assembly code. *)

Theorem bigstep_cstrategy_preservation:
forall p tp,
transf_c_program p = OK tp ->
(forall t r,
Cstrategy.bigstep_program_terminates p t r ->
program_behaves (Asm.semantics tp) (Terminates t r))
/\(forall T,
Cstrategy.bigstep_program_diverges p T ->
program_behaves (Asm.semantics tp) (Reacts T)
\/ exists t, program_behaves (Asm.semantics tp) (Diverges t) /\ traceinf_prefix t T).