(* *********************************************************************)
(*                                                                     *)
(*              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 GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)
(** Observable events, execution traces, and semantics of external calls. *)
Require Intv.
(** * Events and traces *)
(** The observable behaviour of programs is stated in terms of input/output events, which represent the actions of the program that the external world can observe.  CompCert leaves much flexibility as to the exact content of events: the only requirement is that they do not expose memory states nor pointer values (other than pointers to global variables), because these are not preserved literally during compilation.  For concreteness, we use the following type for events.  Each event represents either:  - A system call (e.g. an input/output operation), recording the name of the system call, its parameters, and its result.  - A volatile load from a global memory location, recording the chunk and address being read and the value just read.  - A volatile store to a global memory location, recording the chunk and address being written and the value stored there.  - An annotation, recording the text of the annotation and the values of the arguments.  The values attached to these events are of the following form. As mentioned above, we do not expose pointer values directly. Pointers relative to a global variable are shown with the name of the variable instead of the block identifier. *)
Inductive eventval: Type :=
| EVint: int -> eventval
| EVlong: int64 -> eventval
| EVfloat: float -> eventval
| EVsingle: float32 -> eventval
| EVptr_global: ident -> ptrofs -> eventval.
Notation access_map := (Maps.PMap.t (Z -> option permission)).
Notation delta_map := (Maps.PTree.t (Z -> option (option permission))).
Notation delta_content := (Maps.PTree.t (Z -> option memval)).
Inductive event: Type :=
| Event_syscall: string -> list eventval -> eventval -> event
| Event_vload: memory_chunk -> ident -> ptrofs -> eventval -> event
| Event_vstore: memory_chunk -> ident -> ptrofs -> eventval -> event
| Event_annot: string -> list eventval -> event
| Event_acq: val -> delta_map -> delta_content -> event
| Event_rel: val -> delta_map -> delta_content -> event
| Event_spawn: val -> delta_map -> delta_content -> delta_map -> delta_content -> event.
(** The dynamic semantics for programs collect traces of events. Traces are of two kinds: finite (type [trace]) or infinite (type [traceinf]). *)
Definition trace := list event.
Definition E0 : trace := nil.
Definition Eapp (t1 t2: trace) : trace := t1 ++ t2.
CoInductive traceinf : Type :=
| Econsinf: event -> traceinf -> traceinf.
Fixpoint Eappinf (t: trace) (T: traceinf) {struct t} : traceinf :=
match t with
| nil => T
| ev :: t' => Econsinf ev (Eappinf t' T)
end.
(** Concatenation of traces is written [**] in the finite case or [***] in the infinite case. *)
Infix "**" := Eapp (at level 60, right associativity).
Infix "***" := Eappinf (at level 60, right associativity).

Lemma E0_left: forall t, E0 ** t = t.
Hint Rewrite E0_left E0_right Eapp_assoc
E0_left_inf Eappinf_assoc: trace_rewrite.