source: Deliverables/D3.1/C-semantics/Csem.ma @ 492

Last change on this file since 492 was 487, checked in by campbell, 9 years ago

Port Clight semantics to the new-new matita syntax.

File size: 78.1 KB
Line 
1(* *********************************************************************)
2(*                                                                     *)
3(*              The Compcert verified compiler                         *)
4(*                                                                     *)
5(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
6(*                                                                     *)
7(*  Copyright Institut National de Recherche en Informatique et en     *)
8(*  Automatique.  All rights reserved.  This file is distributed       *)
9(*  under the terms of the GNU General Public License as published by  *)
10(*  the Free Software Foundation, either version 2 of the License, or  *)
11(*  (at your option) any later version.  This file is also distributed *)
12(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
13(*                                                                     *)
14(* *********************************************************************)
15
16(* * Dynamic semantics for the Clight language *)
17
18(*include "Coqlib.ma".*)
19(*include "Errors.ma".*)
20(*include "Integers.ma".*)
21(*include "Floats.ma".*)
22(*include "Values.ma".*)
23(*include "AST.ma".*)
24(*include "Mem.ma".*)
25include "Globalenvs.ma".
26include "Csyntax.ma".
27include "Maps.ma".
28(*include "Events.ma".*)
29include "Smallstep.ma".
30
31(* * * Semantics of type-dependent operations *)
32
33(* * Interpretation of values as truth values.
34  Non-zero integers, non-zero floats and non-null pointers are
35  considered as true.  The integer zero (which also represents
36  the null pointer) and the float 0.0 are false. *)
37
38inductive is_false: val → type → Prop ≝
39  | is_false_int: ∀sz,sg.
40      is_false (Vint zero) (Tint sz sg)
41  | is_false_pointer: ∀r,r',t.
42      is_false (Vnull r) (Tpointer r' t)
43 | is_false_float: ∀sz.
44      is_false (Vfloat Fzero) (Tfloat sz).
45
46inductive is_true: val → type → Prop ≝
47  | is_true_int_int: ∀n,sz,sg.
48      n ≠ zero →
49      is_true (Vint n) (Tint sz sg)
50  | is_true_pointer_pointer: ∀psp,b,ofs,s,t.
51      is_true (Vptr psp b ofs) (Tpointer s t)
52  | is_true_float: ∀f,sz.
53      f ≠ Fzero →
54      is_true (Vfloat f) (Tfloat sz).
55
56inductive bool_of_val : val → type → val → Prop ≝
57  | bool_of_val_true: ∀v,ty.
58         is_true v ty →
59         bool_of_val v ty Vtrue
60  | bool_of_val_false: ∀v,ty.
61        is_false v ty →
62        bool_of_val v ty Vfalse.
63
64(* * The following [sem_] functions compute the result of an operator
65  application.  Since operators are overloaded, the result depends
66  both on the static types of the arguments and on their run-time values.
67  Unlike in C, automatic conversions between integers and floats
68  are not performed.  For instance, [e1 + e2] is undefined if [e1]
69  is a float and [e2] an integer.  The Clight producer must have explicitly
70  promoted [e2] to a float. *)
71
72let rec sem_neg (v: val) (ty: type) : option val ≝
73  match ty with
74  [ Tint _ _ ⇒
75      match v with
76      [ Vint n ⇒ Some ? (Vint (neg n))
77      | _ => None ?
78      ]
79  | Tfloat _ ⇒
80      match v with
81      [ Vfloat f ⇒ Some ? (Vfloat (Fneg f))
82      | _ ⇒ None ?
83      ]
84  | _ ⇒ None ?
85  ].
86
87let rec sem_notint (v: val) : option val ≝
88  match v with
89  [ Vint n ⇒ Some ? (Vint (xor n mone))
90  | _ ⇒ None ?
91  ].
92
93let rec sem_notbool (v: val) (ty: type) : option val ≝
94  match ty with
95  [ Tint _ _ ⇒
96      match v with
97      [ Vint n ⇒ Some ? (of_bool (eq n zero))
98      | Vptr _ _ _ ⇒ Some ? Vfalse
99      | _ ⇒ None ?
100      ]
101  | Tpointer _ _ ⇒
102      match v with
103      [ Vint n ⇒ Some ? (of_bool (eq n zero))
104      | Vptr _ _ _ ⇒ Some ? Vfalse
105      | _ ⇒ None ?
106      ]
107  | Tfloat _ ⇒
108      match v with
109      [ Vfloat f ⇒ Some ? (of_bool (Fcmp Ceq f Fzero))
110      | _ ⇒ None ?
111      ]
112  | _ ⇒ None ?
113  ].
114
115let rec sem_add (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
116  match classify_add t1 t2 with
117  [ add_case_ii ⇒                       (**r integer addition *)
118      match v1 with
119      [ Vint n1 ⇒ match v2 with
120        [ Vint n2 ⇒ Some ? (Vint (add n1 n2))
121        | _ ⇒ None ? ]
122      | _ ⇒ None ? ]
123  | add_case_ff ⇒                       (**r float addition *)
124      match v1 with
125      [ Vfloat n1 ⇒ match v2 with
126        [ Vfloat n2 ⇒ Some ? (Vfloat (Fadd n1 n2))
127        | _ ⇒ None ? ]
128      | _ ⇒ None ? ]
129  | add_case_pi ty ⇒                    (**r pointer plus integer *)
130      match v1 with
131      [ Vptr pcl1 b1 ofs1 ⇒ match v2 with
132        [ Vint n2 ⇒ Some ? (Vptr pcl1 b1 (add ofs1 (mul (repr (sizeof ty)) n2)))
133        | _ ⇒ None ? ]
134      | Vnull r ⇒ match v2 with
135        [ Vint n2 ⇒ if eq n2 zero then Some ? (Vnull r) else None ?
136        | _ ⇒ None ? ]
137      | _ ⇒ None ? ]
138  | add_case_ip ty ⇒                    (**r integer plus pointer *)
139      match v1 with
140      [ Vint n1 ⇒ match v2 with
141        [ Vptr pcl2 b2 ofs2 ⇒ Some ? (Vptr pcl2 b2 (add ofs2 (mul (repr (sizeof ty)) n1)))
142        | _ ⇒ None ? ]
143      | _ ⇒ None ? ]
144  | add_default ⇒ None ?
145].
146
147let rec sem_sub (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
148  match classify_sub t1 t2 with
149  [ sub_case_ii ⇒                (**r integer subtraction *)
150      match v1 with
151      [ Vint n1 ⇒ match v2 with
152        [ Vint n2 ⇒ Some ? (Vint (sub n1 n2))
153        | _ ⇒ None ? ]
154      | _ ⇒ None ? ]
155  | sub_case_ff ⇒                (**r float subtraction *)
156      match v1 with
157      [ Vfloat f1 ⇒ match v2 with
158        [ Vfloat f2 ⇒ Some ? (Vfloat (Fsub f1 f2))
159        | _ ⇒ None ? ]
160      | _ ⇒ None ? ]
161  | sub_case_pi ty ⇒             (**r pointer minus integer *)
162      match v1 with
163      [ Vptr pcl1 b1 ofs1 ⇒ match v2 with
164        [ Vint n2 ⇒ Some ? (Vptr pcl1 b1 (sub ofs1 (mul (repr (sizeof ty)) n2)))
165        | _ ⇒ None ? ]
166      | _ ⇒ None ? ]
167  | sub_case_pp ty ⇒             (**r pointer minus pointer *)
168      match v1 with
169      [ Vptr pcl1 b1 ofs1 ⇒ match v2 with
170        [ Vptr pcl2 b2 ofs2 ⇒
171          if eqZb b1 b2 then
172            if eq (repr (sizeof ty)) zero then None ?
173            else Some ? (Vint (divu (sub ofs1 ofs2) (repr (sizeof ty))))
174          else None ?
175        | _ ⇒ None ? ]
176      | Vnull r ⇒ match v2 with [ Vnull r' ⇒ Some ? (Vint zero) | _ ⇒ None ? ]
177      | _ ⇒ None ? ]
178  | sub_default ⇒ None ?
179  ].
180
181let rec sem_mul (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
182 match classify_mul t1 t2 with
183  [ mul_case_ii ⇒
184      match v1 with
185      [ Vint n1 ⇒ match v2 with
186        [ Vint n2 ⇒ Some ? (Vint (mul n1 n2))
187        | _ ⇒ None ? ]
188      | _ ⇒ None ? ]
189  | mul_case_ff ⇒
190      match v1 with
191      [ Vfloat f1 ⇒ match v2 with
192        [ Vfloat f2 ⇒ Some ? (Vfloat (Fmul f1 f2))
193        | _ ⇒ None ? ]
194      | _ ⇒ None ? ]
195  | mul_default ⇒
196      None ?
197].
198
199let rec sem_div (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
200  match classify_div t1 t2 with
201  [ div_case_I32unsi ⇒
202      match v1 with
203      [ Vint n1 ⇒ match v2 with
204        [ Vint n2 ⇒ if eq n2 zero then None ? else Some ? (Vint (divu n1 n2))
205        | _ ⇒ None ? ]
206      | _ ⇒ None ? ]
207  | div_case_ii ⇒
208      match v1 with
209       [ Vint n1 ⇒ match v2 with
210         [ Vint n2 ⇒ if eq n2 zero then None ? else Some ? (Vint(divs n1 n2))
211         | _ ⇒ None ? ]
212      | _ ⇒ None ? ]
213  | div_case_ff ⇒
214      match v1 with
215      [ Vfloat f1 ⇒ match v2 with
216        [ Vfloat f2 ⇒ Some ? (Vfloat(Fdiv f1 f2))
217        | _ ⇒ None ? ]
218      | _ ⇒ None ? ]
219  | div_default ⇒
220      None ?
221  ].
222
223let rec sem_mod (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
224  match classify_mod t1 t2 with
225  [ mod_case_I32unsi ⇒
226      match v1 with
227      [ Vint n1 ⇒ match v2 with
228        [ Vint n2 ⇒ if eq n2 zero then None ? else Some ? (Vint (modu n1 n2))
229        | _ ⇒ None ? ]
230      | _ ⇒ None ? ]
231  | mod_case_ii ⇒
232      match v1 with
233      [ Vint n1 ⇒ match v2 with
234        [ Vint n2 ⇒ if eq n2 zero then None ? else Some ? (Vint (mods n1 n2))
235        | _ ⇒ None ? ]
236      | _ ⇒ None ? ]
237  | mod_default ⇒
238      None ?
239  ].
240
241let rec sem_and (v1,v2: val) : option val ≝
242  match v1 with
243  [ Vint n1 ⇒ match v2 with
244    [ Vint n2 ⇒ Some ? (Vint(i_and n1 n2))
245    | _ ⇒ None ? ]
246  | _ ⇒ None ?
247  ].
248
249let rec sem_or (v1,v2: val) : option val ≝
250  match v1 with
251  [ Vint n1 ⇒ match v2 with
252    [ Vint n2 ⇒ Some ? (Vint(or n1 n2))
253    | _ ⇒ None ? ]
254  | _ ⇒ None ?
255  ].
256
257let rec sem_xor (v1,v2: val) : option val ≝
258  match v1 with
259  [ Vint n1 ⇒ match v2 with
260    [ Vint n2 ⇒ Some ? (Vint(xor n1 n2))
261    | _ ⇒ None ? ]
262  | _ ⇒ None ?
263  ].
264
265let rec sem_shl (v1,v2: val): option val ≝
266  match v1 with
267  [ Vint n1 ⇒ match v2 with
268    [ Vint n2 ⇒
269        if ltu n2 iwordsize then Some ? (Vint(shl n1 n2)) else None ?
270    | _ ⇒ None ? ]
271  | _ ⇒ None ? ].
272
273let rec sem_shr (v1: val) (t1: type) (v2: val) (t2: type): option val ≝
274  match classify_shr t1 t2 with
275  [ shr_case_I32unsi ⇒
276      match v1 with
277      [ Vint n1 ⇒ match v2 with
278        [ Vint n2 ⇒
279            if ltu n2 iwordsize then Some ? (Vint (shru n1 n2)) else None ?
280        | _ ⇒ None ? ]
281      | _ ⇒ None ? ]
282   | shr_case_ii =>
283      match v1 with
284      [ Vint n1 ⇒ match v2 with
285        [ Vint n2 ⇒
286            if ltu n2 iwordsize then Some ? (Vint (shr n1 n2)) else None ?
287        | _ ⇒ None ? ]
288      | _ ⇒ None ? ]
289   | shr_default ⇒
290      None ?
291   ].
292
293let rec sem_cmp_mismatch (c: comparison): option val ≝
294  match c with
295  [ Ceq =>  Some ? Vfalse
296  | Cne =>  Some ? Vtrue
297  | _   => None ?
298  ].
299
300let rec sem_cmp_match (c: comparison): option val ≝
301  match c with
302  [ Ceq =>  Some ? Vtrue
303  | Cne =>  Some ? Vfalse
304  | _   => None ?
305  ].
306 
307let rec sem_cmp (c:comparison)
308                  (v1: val) (t1: type) (v2: val) (t2: type)
309                  (m: mem): option val ≝
310  match classify_cmp t1 t2 with
311  [ cmp_case_I32unsi ⇒
312      match v1 with
313      [ Vint n1 ⇒ match v2 with
314        [ Vint n2 ⇒ Some ? (of_bool (cmpu c n1 n2))
315        | _ ⇒ None ? ]
316      | _ ⇒ None ? ]
317  | cmp_case_ipip ⇒
318      match v1 with
319      [ Vint n1 ⇒ match v2 with
320         [ Vint n2 ⇒ Some ? (of_bool (cmp c n1 n2))
321         | _ ⇒ None ?
322         ]
323      | Vptr r1 b1 ofs1 ⇒
324        match v2 with
325        [ Vptr r2 b2 ofs2 ⇒
326          if valid_pointer m r1 b1 (signed ofs1)
327          ∧ valid_pointer m r2 b2 (signed ofs2) then
328            if eqZb b1 b2
329            then Some ? (of_bool (cmp c ofs1 ofs2))
330            else sem_cmp_mismatch c
331          else None ?
332        | Vnull r2 ⇒ sem_cmp_mismatch c
333        | _ ⇒ None ? ]
334      | Vnull r1 ⇒
335        match v2 with
336        [ Vptr r2 b2 ofs2 ⇒ sem_cmp_mismatch c
337        | Vnull r2 ⇒ sem_cmp_match c
338        | _ ⇒ None ?
339        ]
340      | _ ⇒ None ? ]
341  | cmp_case_ff ⇒
342      match v1 with
343      [ Vfloat f1 ⇒
344        match v2 with
345        [ Vfloat f2 ⇒ Some ? (of_bool (Fcmp c f1 f2))
346        | _ ⇒ None ? ]
347      | _ ⇒ None ? ]
348  | cmp_default ⇒ None ?
349  ].
350
351definition sem_unary_operation
352            : unary_operation → val → type → option val ≝
353  λop,v,ty.
354  match op with
355  [ Onotbool => sem_notbool v ty
356  | Onotint => sem_notint v
357  | Oneg => sem_neg v ty
358  ].
359
360let rec sem_binary_operation
361    (op: binary_operation)
362    (v1: val) (t1: type) (v2: val) (t2:type)
363    (m: mem): option val ≝
364  match op with
365  [ Oadd ⇒ sem_add v1 t1 v2 t2
366  | Osub ⇒ sem_sub v1 t1 v2 t2
367  | Omul ⇒ sem_mul v1 t1 v2 t2
368  | Omod ⇒ sem_mod v1 t1 v2 t2
369  | Odiv ⇒ sem_div v1 t1 v2 t2
370  | Oand ⇒ sem_and v1 v2 
371  | Oor  ⇒ sem_or v1 v2
372  | Oxor ⇒ sem_xor v1 v2
373  | Oshl ⇒ sem_shl v1 v2
374  | Oshr ⇒ sem_shr v1 t1 v2 t2
375  | Oeq ⇒ sem_cmp Ceq v1 t1 v2 t2 m
376  | One ⇒ sem_cmp Cne v1 t1 v2 t2 m
377  | Olt ⇒ sem_cmp Clt v1 t1 v2 t2 m
378  | Ogt ⇒ sem_cmp Cgt v1 t1 v2 t2 m
379  | Ole ⇒ sem_cmp Cle v1 t1 v2 t2 m
380  | Oge ⇒ sem_cmp Cge v1 t1 v2 t2 m
381  ].
382
383(* * Semantic of casts.  [cast v1 t1 t2 v2] holds if value [v1],
384  viewed with static type [t1], can be cast to type [t2],
385  resulting in value [v2].  *)
386
387let rec cast_int_int (sz: intsize) (sg: signedness) (i: int) : int ≝
388  match sz with
389  [ I8 ⇒ match sg with [ Signed ⇒ sign_ext 8 i | Unsigned ⇒ zero_ext 8 i ]
390  | I16 ⇒ match sg with [ Signed => sign_ext 16 i | Unsigned ⇒ zero_ext 16 i ]
391  | I32 ⇒ i
392  ].
393
394let rec cast_int_float (si : signedness) (i: int) : float ≝
395  match si with
396  [ Signed ⇒ floatofint i
397  | Unsigned ⇒ floatofintu i
398  ].
399
400let rec cast_float_int (si : signedness) (f: float) : int ≝
401  match si with
402  [ Signed ⇒ intoffloat f
403  | Unsigned ⇒ intuoffloat f
404  ].
405
406let rec cast_float_float (sz: floatsize) (f: float) : float ≝
407  match sz with
408  [ F32 ⇒ singleoffloat f
409  | F64 ⇒ f
410  ].
411
412inductive type_region : type → region → Prop ≝
413| type_rgn_pointer : ∀s,t. type_region (Tpointer s t) s
414| type_rgn_array : ∀s,t,n. type_region (Tarray s t n) s
415(* XXX Is the following necessary? *)
416| type_rgn_code : ∀tys,ty. type_region (Tfunction tys ty) Code.
417
418inductive cast : mem → val → type → type → val → Prop ≝
419  | cast_ii:   ∀m,i,sz2,sz1,si1,si2.            (**r int to int  *)
420      cast m (Vint i) (Tint sz1 si1) (Tint sz2 si2)
421           (Vint (cast_int_int sz2 si2 i))
422  | cast_fi:   ∀m,f,sz1,sz2,si2.                (**r float to int *)
423      cast m (Vfloat f) (Tfloat sz1) (Tint sz2 si2)
424           (Vint (cast_int_int sz2 si2 (cast_float_int si2 f)))
425  | cast_if:   ∀m,i,sz1,sz2,si1.                (**r int to float  *)
426      cast m (Vint i) (Tint sz1 si1) (Tfloat sz2)
427          (Vfloat (cast_float_float sz2 (cast_int_float si1 i)))
428  | cast_ff:   ∀m,f,sz1,sz2.                    (**r float to float *)
429      cast m (Vfloat f) (Tfloat sz1) (Tfloat sz2)
430           (Vfloat (cast_float_float sz2 f))
431  | cast_pp: ∀m,r,r',ty,ty',b,ofs.
432      type_region ty r →
433      type_region ty' r' →
434      pointer_compat (block_space m b) r' →
435      cast m (Vptr r b ofs) ty ty' (Vptr r' b ofs)
436  | cast_ip_z: ∀m,sz,sg,ty',r.
437      type_region ty' r →
438      cast m (Vint zero) (Tint sz sg) ty' (Vnull r)
439  | cast_pp_z: ∀m,ty,ty',r,r'.
440      type_region ty r →
441      type_region ty' r' →
442      cast m (Vnull r) ty ty' (Vnull r').
443
444(* * * Operational semantics *)
445
446(* * The semantics uses two environments.  The global environment
447  maps names of functions and global variables to memory block references,
448  and function pointers to their definitions.  (See module [Globalenvs].) *)
449
450definition genv ≝ (genv_t Genv) fundef.
451
452(* * The local environment maps local variables to block references.
453  The current value of the variable is stored in the associated memory
454  block. *)
455
456definition env ≝ (tree_t ? PTree) block. (* map variable -> location *)
457
458definition empty_env: env ≝ (empty …).
459
460(* * [load_value_of_type ty m b ofs] computes the value of a datum
461  of type [ty] residing in memory [m] at block [b], offset [ofs].
462  If the type [ty] indicates an access by value, the corresponding
463  memory load is performed.  If the type [ty] indicates an access by
464  reference, the pointer [Vptr b ofs] is returned. *)
465
466let rec load_value_of_type (ty: type) (m: mem) (psp:region) (b: block) (ofs: int) : option val ≝
467  match access_mode ty with
468  [ By_value chunk ⇒ loadv chunk m (Vptr psp b ofs)
469  | By_reference ⇒ Some ? (Vptr psp b ofs)
470  | By_nothing ⇒ None ?
471  ].
472
473(* * Symmetrically, [store_value_of_type ty m b ofs v] returns the
474  memory state after storing the value [v] in the datum
475  of type [ty] residing in memory [m] at block [b], offset [ofs].
476  This is allowed only if [ty] indicates an access by value. *)
477
478let rec store_value_of_type (ty_dest: type) (m: mem) (psp:region) (loc: block) (ofs: int) (v: val) : option mem ≝
479  match access_mode ty_dest with
480  [ By_value chunk ⇒ storev chunk m (Vptr psp loc ofs) v
481  | By_reference ⇒ None ?
482  | By_nothing ⇒ None ?
483  ].
484
485(* * Allocation of function-local variables.
486  [alloc_variables e1 m1 vars e2 m2] allocates one memory block
487  for each variable declared in [vars], and associates the variable
488  name with this block.  [e1] and [m1] are the initial local environment
489  and memory state.  [e2] and [m2] are the final local environment
490  and memory state. *)
491
492inductive alloc_variables: env → mem →
493                            list (ident × type) →
494                            env → mem → Prop ≝
495  | alloc_variables_nil:
496      ∀e,m.
497      alloc_variables e m (nil ?) e m
498  | alloc_variables_cons:
499      ∀e,m,id,ty,vars,m1,b1,m2,e2.
500      alloc m 0 (sizeof ty) Any = 〈m1, b1〉 →
501      alloc_variables (set … id b1 e) m1 vars e2 m2 →
502      alloc_variables e m (〈id, ty〉 :: vars) e2 m2.
503
504(* * Initialization of local variables that are parameters to a function.
505  [bind_parameters e m1 params args m2] stores the values [args]
506  in the memory blocks corresponding to the variables [params].
507  [m1] is the initial memory state and [m2] the final memory state. *)
508
509inductive bind_parameters: env →
510                           mem → list (ident × type) → list val →
511                           mem → Prop ≝
512  | bind_parameters_nil:
513      ∀e,m.
514      bind_parameters e m (nil ?) (nil ?) m
515  | bind_parameters_cons:
516      ∀e,m,id,ty,params,v1,vl,b,m1,m2.
517      get ??? id e = Some ? b →
518      store_value_of_type ty m Any b zero v1 = Some ? m1 →
519      bind_parameters e m1 params vl m2 →
520      bind_parameters e m (〈id, ty〉 :: params) (v1 :: vl) m2.
521
522(* XXX: this doesn't look right - we're assigning arbitrary memory spaces to
523   parameters? *)
524
525(* * Return the list of blocks in the codomain of [e]. *)
526
527definition blocks_of_env : env → list block ≝ λe.
528  map ?? (λx. snd ?? x) (elements ??? e).
529
530(* * Selection of the appropriate case of a [switch], given the value [n]
531  of the selector expression. *)
532
533let rec select_switch (n: int) (sl: labeled_statements)
534                       on sl : labeled_statements ≝
535  match sl with
536  [ LSdefault _ ⇒ sl
537  | LScase c s sl' ⇒ if eq c n then sl else select_switch n sl'
538  ].
539
540(* * Turn a labeled statement into a sequence *)
541
542let rec seq_of_labeled_statement (sl: labeled_statements) : statement ≝
543  match sl with
544  [ LSdefault s ⇒ s
545  | LScase c s sl' ⇒ Ssequence s (seq_of_labeled_statement sl')
546  ].
547
548(*
549Section SEMANTICS.
550
551Variable ge: genv.
552
553(** ** Evaluation of expressions *)
554
555Section EXPR.
556
557Variable e: env.
558Variable m: mem.
559*)
560(* * [eval_expr ge e m a v] defines the evaluation of expression [a]
561  in r-value position.  [v] is the value of the expression.
562  [e] is the current environment and [m] is the current memory state. *)
563
564inductive eval_expr (ge:genv) (e:env) (m:mem) : expr → val → trace → Prop ≝
565  | eval_Econst_int:   ∀i,ty.
566      eval_expr ge e m (Expr (Econst_int i) ty) (Vint i) E0
567  | eval_Econst_float:   ∀f,ty.
568      eval_expr ge e m (Expr (Econst_float f) ty) (Vfloat f) E0
569  | eval_Elvalue: ∀a,ty,psp,loc,ofs,v,tr.
570      eval_lvalue ge e m (Expr a ty) psp loc ofs tr →
571      load_value_of_type ty m psp loc ofs = Some ? v →
572      eval_expr ge e m (Expr a ty) v tr
573  | eval_Eaddrof: ∀a,ty,psp,loc,ofs,tr.
574      eval_lvalue ge e m a psp loc ofs tr →
575      eval_expr ge e m (Expr (Eaddrof a) ty) (Vptr psp loc ofs) tr
576  | eval_Esizeof: ∀ty',ty.
577      eval_expr ge e m (Expr (Esizeof ty') ty) (Vint (repr (sizeof ty'))) E0
578  | eval_Eunop:  ∀op,a,ty,v1,v,tr.
579      eval_expr ge e m a v1 tr →
580      sem_unary_operation op v1 (typeof a) = Some ? v →
581      eval_expr ge e m (Expr (Eunop op a) ty) v tr
582  | eval_Ebinop: ∀op,a1,a2,ty,v1,v2,v,tr1,tr2.
583      eval_expr ge e m a1 v1 tr1 →
584      eval_expr ge e m a2 v2 tr2 →
585      sem_binary_operation op v1 (typeof a1) v2 (typeof a2) m = Some ? v →
586      eval_expr ge e m (Expr (Ebinop op a1 a2) ty) v (tr1⧺tr2)
587  | eval_Econdition_true: ∀a1,a2,a3,ty,v1,v2,tr1,tr2.
588      eval_expr ge e m a1 v1 tr1 →
589      is_true v1 (typeof a1) →
590      eval_expr ge e m a2 v2 tr2 →
591      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v2 (tr1⧺tr2)
592  | eval_Econdition_false: ∀a1,a2,a3,ty,v1,v3,tr1,tr2.
593      eval_expr ge e m a1 v1 tr1 →
594      is_false v1 (typeof a1) →
595      eval_expr ge e m a3 v3 tr2 →
596      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v3 (tr1⧺tr2)
597  | eval_Eorbool_1: ∀a1,a2,ty,v1,tr.
598      eval_expr ge e m a1 v1 tr →
599      is_true v1 (typeof a1) →
600      eval_expr ge e m (Expr (Eorbool a1 a2) ty) Vtrue tr
601  | eval_Eorbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
602      eval_expr ge e m a1 v1 tr1 →
603      is_false v1 (typeof a1) →
604      eval_expr ge e m a2 v2 tr2 →
605      bool_of_val v2 (typeof a2) v →
606      eval_expr ge e m (Expr (Eorbool a1 a2) ty) v (tr1⧺tr2)
607  | eval_Eandbool_1: ∀a1,a2,ty,v1,tr.
608      eval_expr ge e m a1 v1 tr →
609      is_false v1 (typeof a1) →
610      eval_expr ge e m (Expr (Eandbool a1 a2) ty) Vfalse tr
611  | eval_Eandbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
612      eval_expr ge e m a1 v1 tr1 →
613      is_true v1 (typeof a1) →
614      eval_expr ge e m a2 v2 tr2 →
615      bool_of_val v2 (typeof a2) v →
616      eval_expr ge e m (Expr (Eandbool a1 a2) ty) v (tr1⧺tr2)
617  | eval_Ecast:   ∀a,ty,ty',v1,v,tr.
618      eval_expr ge e m a v1 tr →
619      cast m v1 (typeof a) ty v →
620      eval_expr ge e m (Expr (Ecast ty a) ty') v tr
621  | eval_Ecost: ∀a,ty,v,l,tr.
622      eval_expr ge e m a v tr →
623      eval_expr ge e m (Expr (Ecost l a) ty) v (tr⧺Echarge l)
624
625(* * [eval_lvalue ge e m a b ofs] defines the evaluation of expression [a]
626  in l-value position.  The result is the memory location [b, ofs]
627  that contains the value of the expression [a]. *)
628
629with eval_lvalue (*(ge:genv) (e:env) (m:mem)*) : expr → region → block → int → trace → Prop ≝
630  | eval_Evar_local:   ∀id,l,ty.
631      (* XXX notation? e!id*) get ??? id e = Some ? l →
632      eval_lvalue ge e m (Expr (Evar id) ty) Any l zero E0
633  | eval_Evar_global: ∀id,sp,l,ty.
634      (* XXX e!id *) get ??? id e = None ? →
635      find_symbol ?? ge id = Some ? 〈sp,l〉 →
636      eval_lvalue ge e m (Expr (Evar id) ty) sp l zero E0
637  | eval_Ederef: ∀a,ty,psp,l,ofs,tr.
638      eval_expr ge e m a (Vptr psp l ofs) tr →
639      eval_lvalue ge e m (Expr (Ederef a) ty) psp l ofs tr
640 | eval_Efield_struct:   ∀a,i,ty,psp,l,ofs,id,fList,delta,tr.
641      eval_lvalue ge e m a psp l ofs tr →
642      typeof a = Tstruct id fList →
643      field_offset i fList = OK ? delta →
644      eval_lvalue ge e m (Expr (Efield a i) ty) psp l (add ofs (repr delta)) tr
645 | eval_Efield_union:   ∀a,i,ty,psp,l,ofs,id,fList,tr.
646      eval_lvalue ge e m a psp l ofs tr →
647      typeof a = Tunion id fList →
648      eval_lvalue ge e m (Expr (Efield a i) ty) psp l ofs tr.
649
650let rec eval_expr_ind (ge:genv) (e:env) (m:mem)
651  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
652  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
653  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
654  (elv:∀a,ty,psp,loc,ofs,v,tr,H1,H2. P ??? (eval_Elvalue ge e m a ty psp loc ofs v tr H1 H2))
655  (ead:∀a,ty,psp,loc,ofs,tr,H. P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
656  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
657  (eun:∀op,a,ty,v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Eunop ge e m op a ty v1 v tr H1 H2))
658  (ebi:∀op,a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H2 → P ??? (eval_Ebinop ge e m op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3))
659  (ect:∀a1,a2,a3,ty,v1,v2,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Econdition_true ge e m a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3))
660  (ecf:∀a1,a2,a3,ty,v1,v3,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a3 v3 tr2 H3 → P ??? (eval_Econdition_false ge e m a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3))
661  (eo1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eorbool_1 ge e m a1 a2 ty v1 tr H1 H2))
662  (eo2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eorbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
663  (ea1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eandbool_1 ge e m a1 a2 ty v1 tr H1 H2))
664  (ea2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eandbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
665  (ecs:∀a,ty,ty',v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Ecast ge e m a ty ty' v1 v tr H1 H2))
666  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
667  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
668  match ev with
669  [ eval_Econst_int i ty ⇒ eci i ty
670  | eval_Econst_float f ty ⇒ ecF f ty
671  | eval_Elvalue a ty psp loc ofs v tr H1 H2 ⇒ elv a ty psp loc ofs v tr H1 H2
672  | eval_Eaddrof a ty psp loc ofs tr H ⇒ ead a ty psp loc ofs tr H
673  | eval_Esizeof ty' ty ⇒ esz ty' ty
674  | eval_Eunop op a ty v1 v tr H1 H2 ⇒ eun op a ty v1 v tr H1 H2 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a v1 tr H1)
675  | eval_Ebinop op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 ⇒ ebi op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr1 H1) (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a2 v2 tr2 H2)
676  | eval_Econdition_true a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3 ⇒ ect a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr1 H1) (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a2 v2 tr2 H3)
677  | eval_Econdition_false a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3 ⇒ ecf a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr1 H1) (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a3 v3 tr2 H3)
678  | eval_Eorbool_1 a1 a2 ty v1 tr H1 H2 ⇒ eo1 a1 a2 ty v1 tr H1 H2 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr H1)
679  | eval_Eorbool_2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 ⇒ eo2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr1 H1) (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a2 v2 tr2 H3)
680  | eval_Eandbool_1 a1 a2 ty v1 tr H1 H2 ⇒ ea1 a1 a2 ty v1 tr H1 H2 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr H1)
681  | eval_Eandbool_2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 ⇒ ea2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a1 v1 tr1 H1) (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a2 v2 tr2 H3)
682  | eval_Ecast a ty ty' v1 v tr H1 H2 ⇒ ecs a ty ty' v1 v tr H1 H2 (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a v1 tr H1)
683  | eval_Ecost a ty v l tr H ⇒ eco a ty v l tr H (eval_expr_ind ge e m P eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco a v tr H)
684  ].
685
686inverter eval_expr_inv_ind for eval_expr : Prop.
687
688let rec eval_lvalue_ind (ge:genv) (e:env) (m:mem)
689  (P:∀a,psp,loc,ofs,tr. eval_lvalue ge e m a psp loc ofs tr → Prop)
690  (lvl:∀id,l,ty,H. P ????? (eval_Evar_local ge e m id l ty H))
691  (lvg:∀id,sp,l,ty,H1,H2. P ????? (eval_Evar_global ge e m id sp l ty H1 H2))
692  (lde:∀a,ty,psp,l,ofs,tr,H. P ????? (eval_Ederef ge e m a ty psp l ofs tr H))
693  (lfs:∀a,i,ty,psp,l,ofs,id,fList,delta,tr,H1,H2,H3. P a psp l ofs tr H1 → P ????? (eval_Efield_struct ge e m a i ty psp l ofs id fList delta tr H1 H2 H3))
694  (lfu:∀a,i,ty,psp,l,ofs,id,fList,tr,H1,H2. P a psp l ofs tr H1 → P ????? (eval_Efield_union ge e m a i ty psp l ofs id fList tr H1 H2))
695  (a:expr) (psp:region) (loc:block) (ofs:int) (tr:trace) (ev:eval_lvalue ge e m a psp loc ofs tr) on ev : P a psp loc ofs tr ev ≝
696  match ev with
697  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
698  | eval_Evar_global id sp l ty H1 H2 ⇒ lvg id sp l ty H1 H2
699  | eval_Ederef a ty psp l ofs tr H ⇒ lde a ty psp l ofs tr H
700  | eval_Efield_struct a i ty psp l ofs id fList delta tr H1 H2 H3 ⇒ lfs a i ty psp l ofs id fList delta tr H1 H2 H3 (eval_lvalue_ind ge e m P lvl lvg lde lfs lfu a psp l ofs tr H1)
701  | eval_Efield_union a i ty psp l ofs id fList tr H1 H2 ⇒ lfu a i ty psp l ofs id fList tr H1 H2 (eval_lvalue_ind ge e m P lvl lvg lde lfs lfu a psp l ofs tr H1)
702  ].
703
704(*
705ninverter eval_lvalue_inv_ind for eval_lvalue : Prop.
706*)
707
708definition eval_lvalue_inv_ind :
709  ∀x1: genv.
710   ∀x2: env.
711    ∀x3: mem.
712     ∀x4: expr.
713      ∀x5: region.
714       ∀x6: block.
715        ∀x7: int.
716         ∀x8: trace.
717          ∀P:
718            ∀_z1430: expr.
719             ∀_z1429: region.
720              ∀_z1428: block. ∀_z1427: int. ∀_z1426: trace. Prop.
721           ∀_H1: ?.
722            ∀_H2: ?.
723             ∀_H3: ?.
724              ∀_H4: ?.
725               ∀_H5: ?.
726                ∀_Hterm: eval_lvalue x1 x2 x3 x4 x5 x6 x7 x8.
727                 P x4 x5 x6 x7 x8
728:=
729  (λx1:genv.
730    (λx2:env.
731      (λx3:mem.
732        (λx4:expr.
733          (λx5:region.
734            (λx6:block.
735              (λx7:int.
736                (λx8:trace.
737                  (λP:∀_z1430: expr.
738                        ∀_z1429: region.
739                         ∀_z1428: block.
740                          ∀_z1427: int. ∀_z1426: trace. Prop.
741                    (λH1:?.
742                      (λH2:?.
743                        (λH3:?.
744                          (λH4:?.
745                            (λH5:?.
746                              (λHterm:eval_lvalue x1 x2 x3 x4 x5 x6 x7 x8.
747                                ((λHcut:∀z1435: eq expr x4 x4.
748                                          ∀z1434: eq region x5 x5.
749                                           ∀z1433: eq block x6 x6.
750                                            ∀z1432: eq int x7 x7.
751                                             ∀z1431: eq trace x8 x8.
752                                              P x4 x5 x6 x7 x8.
753                                   (Hcut (refl expr x4)
754                                     (refl region x5) (refl block x6)
755                                     (refl int x7) (refl trace x8)))
756                                  ?)))))))))))))))).
757[ @(eval_lvalue_ind x1 x2 x3 (λa,psp,loc,ofs,tr,e. ∀e1:eq ? x4 a. ∀e2:eq ? x5 psp. ∀e3:eq ? x6 loc. ∀e4:eq ? x7 ofs. ∀e5:eq ? x8 tr. P a psp loc ofs tr) … Hterm)
758  [ @H1 | @H2 | @H3 | @H4 | @H5 ]
759| *: skip
760] qed.
761
762let rec eval_expr_ind2 (ge:genv) (e:env) (m:mem)
763  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
764  (Q:∀a,psp,loc,ofs,tr. eval_lvalue ge e m a psp loc ofs tr → Prop)
765  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
766  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
767  (elv:∀a,ty,psp,loc,ofs,v,tr,H1,H2. Q (Expr a ty) psp loc ofs tr H1 → P ??? (eval_Elvalue ge e m a ty psp loc ofs v tr H1 H2))
768  (ead:∀a,ty,psp,loc,ofs,tr,H. Q a psp loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
769  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
770  (eun:∀op,a,ty,v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Eunop ge e m op a ty v1 v tr H1 H2))
771  (ebi:∀op,a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H2 → P ??? (eval_Ebinop ge e m op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3))
772  (ect:∀a1,a2,a3,ty,v1,v2,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Econdition_true ge e m a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3))
773  (ecf:∀a1,a2,a3,ty,v1,v3,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a3 v3 tr2 H3 → P ??? (eval_Econdition_false ge e m a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3))
774  (eo1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eorbool_1 ge e m a1 a2 ty v1 tr H1 H2))
775  (eo2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eorbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
776  (ea1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eandbool_1 ge e m a1 a2 ty v1 tr H1 H2))
777  (ea2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eandbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
778  (ecs:∀a,ty,ty',v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Ecast ge e m a ty ty' v1 v tr H1 H2))
779  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
780  (lvl:∀id,l,ty,H. Q ????? (eval_Evar_local ge e m id l ty H))
781  (lvg:∀id,sp,l,ty,H1,H2. Q ????? (eval_Evar_global ge e m id sp l ty H1 H2))
782  (lde:∀a,ty,psp,l,ofs,tr,H. P a (Vptr psp l ofs) tr H → Q ????? (eval_Ederef ge e m a ty psp l ofs tr H))
783  (lfs:∀a,i,ty,psp,l,ofs,id,fList,delta,tr,H1,H2,H3. Q a psp l ofs tr H1 → Q ????? (eval_Efield_struct ge e m a i ty psp l ofs id fList delta tr H1 H2 H3))
784  (lfu:∀a,i,ty,psp,l,ofs,id,fList,tr,H1,H2. Q a psp l ofs tr H1 → Q ????? (eval_Efield_union ge e m a i ty psp l ofs id fList tr H1 H2))
785 
786  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
787  match ev with
788  [ eval_Econst_int i ty ⇒ eci i ty
789  | eval_Econst_float f ty ⇒ ecF f ty
790  | eval_Elvalue a ty psp loc ofs v tr H1 H2 ⇒ elv a ty psp loc ofs v tr H1 H2 (eval_lvalue_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu (Expr a ty) psp loc ofs tr H1)
791  | eval_Eaddrof a ty psp loc ofs tr H ⇒ ead a ty psp loc ofs tr H (eval_lvalue_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a psp loc ofs tr H)
792  | eval_Esizeof ty' ty ⇒ esz ty' ty
793  | eval_Eunop op a ty v1 v tr H1 H2 ⇒ eun op a ty v1 v tr H1 H2 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a v1 tr H1)
794  | eval_Ebinop op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 ⇒ ebi op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr1 H1) (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a2 v2 tr2 H2)
795  | eval_Econdition_true a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3 ⇒ ect a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr1 H1) (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a2 v2 tr2 H3)
796  | eval_Econdition_false a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3 ⇒ ecf a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr1 H1) (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a3 v3 tr2 H3)
797  | eval_Eorbool_1 a1 a2 ty v1 tr H1 H2 ⇒ eo1 a1 a2 ty v1 tr H1 H2 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr H1)
798  | eval_Eorbool_2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 ⇒ eo2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr1 H1) (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a2 v2 tr2 H3)
799  | eval_Eandbool_1 a1 a2 ty v1 tr H1 H2 ⇒ ea1 a1 a2 ty v1 tr H1 H2 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr H1)
800  | eval_Eandbool_2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 ⇒ ea2 a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a1 v1 tr1 H1) (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a2 v2 tr2 H3)
801  | eval_Ecast a ty ty' v1 v tr H1 H2 ⇒ ecs a ty ty' v1 v tr H1 H2 (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a v1 tr H1)
802  | eval_Ecost a ty v l tr H ⇒ eco a ty v l tr H (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a v tr H)
803  ]
804and eval_lvalue_ind2 (ge:genv) (e:env) (m:mem)
805  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
806  (Q:∀a,psp,loc,ofs,tr. eval_lvalue ge e m a psp loc ofs tr → Prop)
807  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
808  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
809  (elv:∀a,ty,psp,loc,ofs,v,tr,H1,H2. Q (Expr a ty) psp loc ofs tr H1 → P ??? (eval_Elvalue ge e m a ty psp loc ofs v tr H1 H2))
810  (ead:∀a,ty,psp,loc,ofs,tr,H. Q a psp loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
811  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
812  (eun:∀op,a,ty,v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Eunop ge e m op a ty v1 v tr H1 H2))
813  (ebi:∀op,a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H2 → P ??? (eval_Ebinop ge e m op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3))
814  (ect:∀a1,a2,a3,ty,v1,v2,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Econdition_true ge e m a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3))
815  (ecf:∀a1,a2,a3,ty,v1,v3,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a3 v3 tr2 H3 → P ??? (eval_Econdition_false ge e m a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3))
816  (eo1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eorbool_1 ge e m a1 a2 ty v1 tr H1 H2))
817  (eo2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eorbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
818  (ea1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eandbool_1 ge e m a1 a2 ty v1 tr H1 H2))
819  (ea2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eandbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
820  (ecs:∀a,ty,ty',v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Ecast ge e m a ty ty' v1 v tr H1 H2))
821  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
822  (lvl:∀id,l,ty,H. Q ????? (eval_Evar_local ge e m id l ty H))
823  (lvg:∀id,sp,l,ty,H1,H2. Q ????? (eval_Evar_global ge e m id sp l ty H1 H2))
824  (lde:∀a,ty,psp,l,ofs,tr,H. P a (Vptr psp l ofs) tr H → Q ????? (eval_Ederef ge e m a ty psp l ofs tr H))
825  (lfs:∀a,i,ty,psp,l,ofs,id,fList,delta,tr,H1,H2,H3. Q a psp l ofs tr H1 → Q ????? (eval_Efield_struct ge e m a i ty psp l ofs id fList delta tr H1 H2 H3))
826  (lfu:∀a,i,ty,psp,l,ofs,id,fList,tr,H1,H2. Q a psp l ofs tr H1 → Q ????? (eval_Efield_union ge e m a i ty psp l ofs id fList tr H1 H2))
827  (a:expr) (psp:region) (loc:block) (ofs:int) (tr:trace) (ev:eval_lvalue ge e m a psp loc ofs tr) on ev : Q a psp loc ofs tr ev ≝
828  match ev with
829  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
830  | eval_Evar_global id sp l ty H1 H2 ⇒ lvg id sp l ty H1 H2
831  | eval_Ederef a ty psp l ofs tr H ⇒ lde a ty psp l ofs tr H (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a (Vptr psp l ofs) tr H)
832  | eval_Efield_struct a i ty psp l ofs id fList delta tr H1 H2 H3 ⇒ lfs a i ty psp l ofs id fList delta tr H1 H2 H3 (eval_lvalue_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a psp l ofs tr H1)
833  | eval_Efield_union a i ty psp l ofs id fList tr H1 H2 ⇒ lfu a i ty psp l ofs id fList tr H1 H2 (eval_lvalue_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu a psp l ofs tr H1)
834  ].
835
836definition combined_expr_lvalue_ind ≝
837λge,e,m,P,Q,eci,ecF,elv,ead,esz,eun,ebi,ect,ecf,eo1,eo2,ea1,ea2,ecs,eco,lvl,lvg,lde,lfs,lfu. 
838conj ??
839  (eval_expr_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu)
840  (eval_lvalue_ind2 ge e m P Q eci ecF elv ead esz eun ebi ect ecf eo1 eo2 ea1 ea2 ecs eco lvl lvg lde lfs lfu).
841
842(* * [eval_lvalue ge e m a b ofs] defines the evaluation of expression [a]
843  in l-value position.  The result is the memory location [b, ofs]
844  that contains the value of the expression [a]. *)
845
846(*
847Scheme eval_expr_ind22 := Minimality for eval_expr Sort Prop
848  with eval_lvalue_ind2 := Minimality for eval_lvalue Sort Prop.
849*)
850
851(* * [eval_exprlist ge e m al vl] evaluates a list of r-value
852  expressions [al] to their values [vl]. *)
853
854inductive eval_exprlist (ge:genv) (e:env) (m:mem) : list expr → list val → trace → Prop ≝
855  | eval_Enil:
856      eval_exprlist ge e m (nil ?) (nil ?) E0
857  | eval_Econs:   ∀a,bl,v,vl,tr1,tr2.
858      eval_expr ge e m a v tr1 →
859      eval_exprlist ge e m bl vl tr2 →
860      eval_exprlist ge e m (a :: bl) (v :: vl) (tr1⧺tr2).
861
862(*End EXPR.*)
863
864(* * ** Transition semantics for statements and functions *)
865
866(* * Continuations *)
867
868inductive cont: Type[0] :=
869  | Kstop: cont
870  | Kseq: statement -> cont -> cont
871       (**r [Kseq s2 k] = after [s1] in [s1;s2] *)
872  | Kwhile: expr -> statement -> cont -> cont
873       (**r [Kwhile e s k] = after [s] in [while (e) s] *)
874  | Kdowhile: expr -> statement -> cont -> cont
875       (**r [Kdowhile e s k] = after [s] in [do s while (e)] *)
876  | Kfor2: expr -> statement -> statement -> cont -> cont
877       (**r [Kfor2 e2 e3 s k] = after [s] in [for(e1;e2;e3) s] *)
878  | Kfor3: expr -> statement -> statement -> cont -> cont
879       (**r [Kfor3 e2 e3 s k] = after [e3] in [for(e1;e2;e3) s] *)
880  | Kswitch: cont -> cont
881       (**r catches [break] statements arising out of [switch] *)
882  | Kcall: option (region × block × int × type) ->   (**r where to store result *)
883           function ->                      (**r calling function *)
884           env ->                           (**r local env of calling function *)
885           cont -> cont.
886
887(* * Pop continuation until a call or stop *)
888
889let rec call_cont (k: cont) : cont :=
890  match k with
891  [ Kseq s k => call_cont k
892  | Kwhile e s k => call_cont k
893  | Kdowhile e s k => call_cont k
894  | Kfor2 e2 e3 s k => call_cont k
895  | Kfor3 e2 e3 s k => call_cont k
896  | Kswitch k => call_cont k
897  | _ => k
898  ].
899
900definition is_call_cont : cont → Prop ≝ λk.
901  match k with
902  [ Kstop => True
903  | Kcall _ _ _ _ => True
904  | _ => False
905  ].
906
907(* * States *)
908
909inductive state: Type[0] :=
910  | State:
911      ∀f: function.
912      ∀s: statement.
913      ∀k: cont.
914      ∀e: env.
915      ∀m: mem.  state
916  | Callstate:
917      ∀fd: fundef.
918      ∀args: list val.
919      ∀k: cont.
920      ∀m: mem. state
921  | Returnstate:
922      ∀res: val.
923      ∀k: cont.
924      ∀m: mem. state.
925                 
926(* * Find the statement and manufacture the continuation
927  corresponding to a label *)
928
929let rec find_label (lbl: label) (s: statement) (k: cont)
930                    on s: option (statement × cont) :=
931  match s with
932  [ Ssequence s1 s2 =>
933      match find_label lbl s1 (Kseq s2 k) with
934      [ Some sk => Some ? sk
935      | None => find_label lbl s2 k
936      ]
937  | Sifthenelse a s1 s2 =>
938      match find_label lbl s1 k with
939      [ Some sk => Some ? sk
940      | None => find_label lbl s2 k
941      ]
942  | Swhile a s1 =>
943      find_label lbl s1 (Kwhile a s1 k)
944  | Sdowhile a s1 =>
945      find_label lbl s1 (Kdowhile a s1 k)
946  | Sfor a1 a2 a3 s1 =>
947      match find_label lbl a1 (Kseq (Sfor Sskip a2 a3 s1) k) with
948      [ Some sk => Some ? sk
949      | None =>
950          match find_label lbl s1 (Kfor2 a2 a3 s1 k) with
951          [ Some sk => Some ? sk
952          | None => find_label lbl a3 (Kfor3 a2 a3 s1 k)
953          ]
954      ]
955  | Sswitch e sl =>
956      find_label_ls lbl sl (Kswitch k)
957  | Slabel lbl' s' =>
958      match ident_eq lbl lbl' with
959      [ inl _ ⇒ Some ? 〈s', k〉
960      | inr _ ⇒ find_label lbl s' k
961      ]
962  | _ => None ?
963  ]
964
965and find_label_ls (lbl: label) (sl: labeled_statements) (k: cont)
966                    on sl: option (statement × cont) :=
967  match sl with
968  [ LSdefault s => find_label lbl s k
969  | LScase _ s sl' =>
970      match find_label lbl s (Kseq (seq_of_labeled_statement sl') k) with
971      [ Some sk => Some ? sk
972      | None => find_label_ls lbl sl' k
973      ]
974  ].
975
976(* * Transition relation *)
977
978(* Strip off outer pointer for use when comparing function types. *)
979definition fun_typeof ≝
980λe. match typeof e with
981[ Tvoid ⇒ Tvoid
982| Tint a b ⇒ Tint a b
983| Tfloat a ⇒ Tfloat a
984| Tpointer _ ty ⇒ ty
985| Tarray a b c ⇒ Tarray a b c
986| Tfunction a b ⇒ Tfunction a b
987| Tstruct a b ⇒ Tstruct a b
988| Tunion a b ⇒ Tunion a b
989| Tcomp_ptr a b ⇒ Tcomp_ptr a b
990].
991
992(* XXX: note that cost labels in exprs expose a particular eval order. *)
993
994inductive step (ge:genv) : state → trace → state → Prop ≝
995
996  | step_assign:   ∀f,a1,a2,k,e,m,psp,loc,ofs,v2,m',tr1,tr2.
997      eval_lvalue ge e m a1 psp loc ofs tr1 →
998      eval_expr ge e m a2 v2 tr2 →
999      store_value_of_type (typeof a1) m psp loc ofs v2 = Some ? m' →
1000      step ge (State f (Sassign a1 a2) k e m)
1001           (tr1⧺tr2) (State f Sskip k e m')
1002
1003  | step_call_none:   ∀f,a,al,k,e,m,vf,vargs,fd,tr1,tr2.
1004      eval_expr ge e m a vf tr1 →
1005      eval_exprlist ge e m al vargs tr2 →
1006      find_funct ?? ge vf = Some ? fd →
1007      type_of_fundef fd = fun_typeof a →
1008      step ge (State f (Scall (None ?) a al) k e m)
1009           (tr1⧺tr2) (Callstate fd vargs (Kcall (None ?) f e k) m)
1010
1011  | step_call_some:   ∀f,lhs,a,al,k,e,m,psp,loc,ofs,vf,vargs,fd,tr1,tr2,tr3.
1012      eval_lvalue ge e m lhs psp loc ofs tr1 →
1013      eval_expr ge e m a vf tr2 →
1014      eval_exprlist ge e m al vargs tr3 →
1015      find_funct ?? ge vf = Some ? fd →
1016      type_of_fundef fd = fun_typeof a →
1017      step ge (State f (Scall (Some ? lhs) a al) k e m)
1018           (tr1⧺tr2⧺tr3) (Callstate fd vargs (Kcall (Some ? 〈〈〈psp, loc〉, ofs〉, typeof lhs〉) f e k) m)
1019
1020  | step_seq:  ∀f,s1,s2,k,e,m.
1021      step ge (State f (Ssequence s1 s2) k e m)
1022           E0 (State f s1 (Kseq s2 k) e m)
1023  | step_skip_seq: ∀f,s,k,e,m.
1024      step ge (State f Sskip (Kseq s k) e m)
1025           E0 (State f s k e m)
1026  | step_continue_seq: ∀f,s,k,e,m.
1027      step ge (State f Scontinue (Kseq s k) e m)
1028           E0 (State f Scontinue k e m)
1029  | step_break_seq: ∀f,s,k,e,m.
1030      step ge (State f Sbreak (Kseq s k) e m)
1031           E0 (State f Sbreak k e m)
1032
1033  | step_ifthenelse_true:  ∀f,a,s1,s2,k,e,m,v1,tr.
1034      eval_expr ge e m a v1 tr →
1035      is_true v1 (typeof a) →
1036      step ge (State f (Sifthenelse a s1 s2) k e m)
1037           tr (State f s1 k e m)
1038  | step_ifthenelse_false: ∀f,a,s1,s2,k,e,m,v1,tr.
1039      eval_expr ge e m a v1 tr →
1040      is_false v1 (typeof a) →
1041      step ge (State f (Sifthenelse a s1 s2) k e m)
1042           tr (State f s2 k e m)
1043
1044  | step_while_false: ∀f,a,s,k,e,m,v,tr.
1045      eval_expr ge e m a v tr →
1046      is_false v (typeof a) →
1047      step ge (State f (Swhile a s) k e m)
1048           tr (State f Sskip k e m)
1049  | step_while_true: ∀f,a,s,k,e,m,v,tr.
1050      eval_expr ge e m a v tr →
1051      is_true v (typeof a) →
1052      step ge (State f (Swhile a s) k e m)
1053           tr (State f s (Kwhile a s k) e m)
1054  | step_skip_or_continue_while: ∀f,x,a,s,k,e,m.
1055      x = Sskip ∨ x = Scontinue →
1056      step ge (State f x (Kwhile a s k) e m)
1057           E0 (State f (Swhile a s) k e m)
1058  | step_break_while: ∀f,a,s,k,e,m.
1059      step ge (State f Sbreak (Kwhile a s k) e m)
1060           E0 (State f Sskip k e m)
1061
1062  | step_dowhile: ∀f,a,s,k,e,m.
1063      step ge (State f (Sdowhile a s) k e m)
1064        E0 (State f s (Kdowhile a s k) e m)
1065  | step_skip_or_continue_dowhile_false: ∀f,x,a,s,k,e,m,v,tr.
1066      x = Sskip ∨ x = Scontinue →
1067      eval_expr ge e m a v tr →
1068      is_false v (typeof a) →
1069      step ge (State f x (Kdowhile a s k) e m)
1070           tr (State f Sskip k e m)
1071  | step_skip_or_continue_dowhile_true: ∀f,x,a,s,k,e,m,v,tr.
1072      x = Sskip ∨ x = Scontinue →
1073      eval_expr ge e m a v tr →
1074      is_true v (typeof a) →
1075      step ge (State f x (Kdowhile a s k) e m)
1076           tr (State f (Sdowhile a s) k e m)
1077  | step_break_dowhile: ∀f,a,s,k,e,m.
1078      step ge (State f Sbreak (Kdowhile a s k) e m)
1079           E0 (State f Sskip k e m)
1080
1081  | step_for_start: ∀f,a1,a2,a3,s,k,e,m.
1082      a1 ≠ Sskip →
1083      step ge (State f (Sfor a1 a2 a3 s) k e m)
1084           E0 (State f a1 (Kseq (Sfor Sskip a2 a3 s) k) e m)
1085  | step_for_false: ∀f,a2,a3,s,k,e,m,v,tr.
1086      eval_expr ge e m a2 v tr →
1087      is_false v (typeof a2) →
1088      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1089           tr (State f Sskip k e m)
1090  | step_for_true: ∀f,a2,a3,s,k,e,m,v,tr.
1091      eval_expr ge e m a2 v tr →
1092      is_true v (typeof a2) →
1093      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1094           tr (State f s (Kfor2 a2 a3 s k) e m)
1095  | step_skip_or_continue_for2: ∀f,x,a2,a3,s,k,e,m.
1096      x = Sskip ∨ x = Scontinue →
1097      step ge (State f x (Kfor2 a2 a3 s k) e m)
1098           E0 (State f a3 (Kfor3 a2 a3 s k) e m)
1099  | step_break_for2: ∀f,a2,a3,s,k,e,m.
1100      step ge (State f Sbreak (Kfor2 a2 a3 s k) e m)
1101           E0 (State f Sskip k e m)
1102  | step_skip_for3: ∀f,a2,a3,s,k,e,m.
1103      step ge (State f Sskip (Kfor3 a2 a3 s k) e m)
1104           E0 (State f (Sfor Sskip a2 a3 s) k e m)
1105
1106  | step_return_0: ∀f,k,e,m.
1107      fn_return f = Tvoid →
1108      step ge (State f (Sreturn (None ?)) k e m)
1109           E0 (Returnstate Vundef (call_cont k) (free_list m (blocks_of_env e)))
1110  | step_return_1: ∀f,a,k,e,m,v,tr.
1111      fn_return f ≠ Tvoid →
1112      eval_expr ge e m a v tr →
1113      step ge (State f (Sreturn (Some ? a)) k e m)
1114           tr (Returnstate v (call_cont k) (free_list m (blocks_of_env e)))
1115  | step_skip_call: ∀f,k,e,m.
1116      is_call_cont k →
1117      fn_return f = Tvoid →
1118      step ge (State f Sskip k e m)
1119           E0 (Returnstate Vundef k (free_list m (blocks_of_env e)))
1120
1121  | step_switch: ∀f,a,sl,k,e,m,n,tr.
1122      eval_expr ge e m a (Vint n) tr →
1123      step ge (State f (Sswitch a sl) k e m)
1124           tr (State f (seq_of_labeled_statement (select_switch n sl)) (Kswitch k) e m)
1125  | step_skip_break_switch: ∀f,x,k,e,m.
1126      x = Sskip ∨ x = Sbreak →
1127      step ge (State f x (Kswitch k) e m)
1128           E0 (State f Sskip k e m)
1129  | step_continue_switch: ∀f,k,e,m.
1130      step ge (State f Scontinue (Kswitch k) e m)
1131           E0 (State f Scontinue k e m)
1132
1133  | step_label: ∀f,lbl,s,k,e,m.
1134      step ge (State f (Slabel lbl s) k e m)
1135           E0 (State f s k e m)
1136
1137  | step_goto: ∀f,lbl,k,e,m,s',k'.
1138      find_label lbl (fn_body f) (call_cont k) = Some ? 〈s', k'〉 →
1139      step ge (State f (Sgoto lbl) k e m)
1140           E0 (State f s' k' e m)
1141
1142  | step_internal_function: ∀f,vargs,k,m,e,m1,m2.
1143      alloc_variables empty_env m ((fn_params f) @ (fn_vars f)) e m1 →
1144      bind_parameters e m1 (fn_params f) vargs m2 →
1145      step ge (Callstate (Internal f) vargs k m)
1146           E0 (State f (fn_body f) k e m2)
1147
1148  | step_external_function: ∀id,targs,tres,vargs,k,m,vres,t.
1149      event_match (external_function id targs tres) vargs t vres →
1150      step ge (Callstate (External id targs tres) vargs k m)
1151            t (Returnstate vres k m)
1152
1153  | step_returnstate_0: ∀v,f,e,k,m.
1154      step ge (Returnstate v (Kcall (None ?) f e k) m)
1155           E0 (State f Sskip k e m)
1156
1157  | step_returnstate_1: ∀v,f,e,k,m,m',psp,loc,ofs,ty.
1158      store_value_of_type ty m psp loc ofs v = Some ? m' →
1159      step ge (Returnstate v (Kcall (Some ? 〈〈〈psp,loc〉, ofs〉, ty〉) f e k) m)
1160           E0 (State f Sskip k e m')
1161           
1162  | step_cost: ∀f,lbl,s,k,e,m.
1163      step ge (State f (Scost lbl s) k e m)
1164           (Echarge lbl) (State f s k e m).
1165(*
1166(** * Alternate big-step semantics *)
1167
1168(** ** Big-step semantics for terminating statements and functions *)
1169
1170(** The execution of a statement produces an ``outcome'', indicating
1171  how the execution terminated: either normally or prematurely
1172  through the execution of a [break], [continue] or [return] statement. *)
1173
1174inductive outcome: Type[0] :=
1175   | Out_break: outcome                 (**r terminated by [break] *)
1176   | Out_continue: outcome              (**r terminated by [continue] *)
1177   | Out_normal: outcome                (**r terminated normally *)
1178   | Out_return: option val -> outcome. (**r terminated by [return] *)
1179
1180inductive out_normal_or_continue : outcome -> Prop :=
1181  | Out_normal_or_continue_N: out_normal_or_continue Out_normal
1182  | Out_normal_or_continue_C: out_normal_or_continue Out_continue.
1183
1184inductive out_break_or_return : outcome -> outcome -> Prop :=
1185  | Out_break_or_return_B: out_break_or_return Out_break Out_normal
1186  | Out_break_or_return_R: ∀ov.
1187      out_break_or_return (Out_return ov) (Out_return ov).
1188
1189Definition outcome_switch (out: outcome) : outcome :=
1190  match out with
1191  | Out_break => Out_normal
1192  | o => o
1193  end.
1194
1195Definition outcome_result_value (out: outcome) (t: type) (v: val) : Prop :=
1196  match out, t with
1197  | Out_normal, Tvoid => v = Vundef
1198  | Out_return None, Tvoid => v = Vundef
1199  | Out_return (Some v'), ty => ty <> Tvoid /\ v'=v
1200  | _, _ => False
1201  end.
1202
1203(** [exec_stmt ge e m1 s t m2 out] describes the execution of
1204  the statement [s].  [out] is the outcome for this execution.
1205  [m1] is the initial memory state, [m2] the final memory state.
1206  [t] is the trace of input/output events performed during this
1207  evaluation. *)
1208
1209inductive exec_stmt: env -> mem -> statement -> trace -> mem -> outcome -> Prop :=
1210  | exec_Sskip:   ∀e,m.
1211      exec_stmt e m Sskip
1212               E0 m Out_normal
1213  | exec_Sassign:   ∀e,m,a1,a2,loc,ofs,v2,m'.
1214      eval_lvalue e m a1 loc ofs ->
1215      eval_expr e m a2 v2 ->
1216      store_value_of_type (typeof a1) m loc ofs v2 = Some m' ->
1217      exec_stmt e m (Sassign a1 a2)
1218               E0 m' Out_normal
1219  | exec_Scall_none:   ∀e,m,a,al,vf,vargs,f,t,m',vres.
1220      eval_expr e m a vf ->
1221      eval_exprlist e m al vargs ->
1222      Genv.find_funct ge vf = Some f ->
1223      type_of_fundef f = typeof a ->
1224      eval_funcall m f vargs t m' vres ->
1225      exec_stmt e m (Scall None a al)
1226                t m' Out_normal
1227  | exec_Scall_some:   ∀e,m,lhs,a,al,loc,ofs,vf,vargs,f,t,m',vres,m''.
1228      eval_lvalue e m lhs loc ofs ->
1229      eval_expr e m a vf ->
1230      eval_exprlist e m al vargs ->
1231      Genv.find_funct ge vf = Some f ->
1232      type_of_fundef f = typeof a ->
1233      eval_funcall m f vargs t m' vres ->
1234      store_value_of_type (typeof lhs) m' loc ofs vres = Some m'' ->
1235      exec_stmt e m (Scall (Some lhs) a al)
1236                t m'' Out_normal
1237  | exec_Sseq_1:   ∀e,m,s1,s2,t1,m1,t2,m2,out.
1238      exec_stmt e m s1 t1 m1 Out_normal ->
1239      exec_stmt e m1 s2 t2 m2 out ->
1240      exec_stmt e m (Ssequence s1 s2)
1241                (t1 ** t2) m2 out
1242  | exec_Sseq_2:   ∀e,m,s1,s2,t1,m1,out.
1243      exec_stmt e m s1 t1 m1 out ->
1244      out <> Out_normal ->
1245      exec_stmt e m (Ssequence s1 s2)
1246                t1 m1 out
1247  | exec_Sifthenelse_true: ∀e,m,a,s1,s2,v1,t,m',out.
1248      eval_expr e m a v1 ->
1249      is_true v1 (typeof a) ->
1250      exec_stmt e m s1 t m' out ->
1251      exec_stmt e m (Sifthenelse a s1 s2)
1252                t m' out
1253  | exec_Sifthenelse_false: ∀e,m,a,s1,s2,v1,t,m',out.
1254      eval_expr e m a v1 ->
1255      is_false v1 (typeof a) ->
1256      exec_stmt e m s2 t m' out ->
1257      exec_stmt e m (Sifthenelse a s1 s2)
1258                t m' out
1259  | exec_Sreturn_none:   ∀e,m.
1260      exec_stmt e m (Sreturn None)
1261               E0 m (Out_return None)
1262  | exec_Sreturn_some: ∀e,m,a,v.
1263      eval_expr e m a v ->
1264      exec_stmt e m (Sreturn (Some a))
1265               E0 m (Out_return (Some v))
1266  | exec_Sbreak:   ∀e,m.
1267      exec_stmt e m Sbreak
1268               E0 m Out_break
1269  | exec_Scontinue:   ∀e,m.
1270      exec_stmt e m Scontinue
1271               E0 m Out_continue
1272  | exec_Swhile_false: ∀e,m,a,s,v.
1273      eval_expr e m a v ->
1274      is_false v (typeof a) ->
1275      exec_stmt e m (Swhile a s)
1276               E0 m Out_normal
1277  | exec_Swhile_stop: ∀e,m,a,v,s,t,m',out',out.
1278      eval_expr e m a v ->
1279      is_true v (typeof a) ->
1280      exec_stmt e m s t m' out' ->
1281      out_break_or_return out' out ->
1282      exec_stmt e m (Swhile a s)
1283                t m' out
1284  | exec_Swhile_loop: ∀e,m,a,s,v,t1,m1,out1,t2,m2,out.
1285      eval_expr e m a v ->
1286      is_true v (typeof a) ->
1287      exec_stmt e m s t1 m1 out1 ->
1288      out_normal_or_continue out1 ->
1289      exec_stmt e m1 (Swhile a s) t2 m2 out ->
1290      exec_stmt e m (Swhile a s)
1291                (t1 ** t2) m2 out
1292  | exec_Sdowhile_false: ∀e,m,s,a,t,m1,out1,v.
1293      exec_stmt e m s t m1 out1 ->
1294      out_normal_or_continue out1 ->
1295      eval_expr e m1 a v ->
1296      is_false v (typeof a) ->
1297      exec_stmt e m (Sdowhile a s)
1298                t m1 Out_normal
1299  | exec_Sdowhile_stop: ∀e,m,s,a,t,m1,out1,out.
1300      exec_stmt e m s t m1 out1 ->
1301      out_break_or_return out1 out ->
1302      exec_stmt e m (Sdowhile a s)
1303                t m1 out
1304  | exec_Sdowhile_loop: ∀e,m,s,a,m1,m2,t1,t2,out,out1,v.
1305      exec_stmt e m s t1 m1 out1 ->
1306      out_normal_or_continue out1 ->
1307      eval_expr e m1 a v ->
1308      is_true v (typeof a) ->
1309      exec_stmt e m1 (Sdowhile a s) t2 m2 out ->
1310      exec_stmt e m (Sdowhile a s)
1311                (t1 ** t2) m2 out
1312  | exec_Sfor_start: ∀e,m,s,a1,a2,a3,out,m1,m2,t1,t2.
1313      a1 <> Sskip ->
1314      exec_stmt e m a1 t1 m1 Out_normal ->
1315      exec_stmt e m1 (Sfor Sskip a2 a3 s) t2 m2 out ->
1316      exec_stmt e m (Sfor a1 a2 a3 s)
1317                (t1 ** t2) m2 out
1318  | exec_Sfor_false: ∀e,m,s,a2,a3,v.
1319      eval_expr e m a2 v ->
1320      is_false v (typeof a2) ->
1321      exec_stmt e m (Sfor Sskip a2 a3 s)
1322               E0 m Out_normal
1323  | exec_Sfor_stop: ∀e,m,s,a2,a3,v,m1,t,out1,out.
1324      eval_expr e m a2 v ->
1325      is_true v (typeof a2) ->
1326      exec_stmt e m s t m1 out1 ->
1327      out_break_or_return out1 out ->
1328      exec_stmt e m (Sfor Sskip a2 a3 s)
1329                t m1 out
1330  | exec_Sfor_loop: ∀e,m,s,a2,a3,v,m1,m2,m3,t1,t2,t3,out1,out.
1331      eval_expr e m a2 v ->
1332      is_true v (typeof a2) ->
1333      exec_stmt e m s t1 m1 out1 ->
1334      out_normal_or_continue out1 ->
1335      exec_stmt e m1 a3 t2 m2 Out_normal ->
1336      exec_stmt e m2 (Sfor Sskip a2 a3 s) t3 m3 out ->
1337      exec_stmt e m (Sfor Sskip a2 a3 s)
1338                (t1 ** t2 ** t3) m3 out
1339  | exec_Sswitch:   ∀e,m,a,t,n,sl,m1,out.
1340      eval_expr e m a (Vint n) ->
1341      exec_stmt e m (seq_of_labeled_statement (select_switch n sl)) t m1 out ->
1342      exec_stmt e m (Sswitch a sl)
1343                t m1 (outcome_switch out)
1344
1345(** [eval_funcall m1 fd args t m2 res] describes the invocation of
1346  function [fd] with arguments [args].  [res] is the value returned
1347  by the call.  *)
1348
1349with eval_funcall: mem -> fundef -> list val -> trace -> mem -> val -> Prop :=
1350  | eval_funcall_internal: ∀m,f,vargs,t,e,m1,m2,m3,out,vres.
1351      alloc_variables empty_env m (f.(fn_params) ++ f.(fn_vars)) e m1 ->
1352      bind_parameters e m1 f.(fn_params) vargs m2 ->
1353      exec_stmt e m2 f.(fn_body) t m3 out ->
1354      outcome_result_value out f.(fn_return) vres ->
1355      eval_funcall m (Internal f) vargs t (Mem.free_list m3 (blocks_of_env e)) vres
1356  | eval_funcall_external: ∀m,id,targs,tres,vargs,t,vres.
1357      event_match (external_function id targs tres) vargs t vres ->
1358      eval_funcall m (External id targs tres) vargs t m vres.
1359
1360Scheme exec_stmt_ind2 := Minimality for exec_stmt Sort Prop
1361  with eval_funcall_ind2 := Minimality for eval_funcall Sort Prop.
1362
1363(** ** Big-step semantics for diverging statements and functions *)
1364
1365(** Coinductive semantics for divergence.
1366  [execinf_stmt ge e m s t] holds if the execution of statement [s]
1367  diverges, i.e. loops infinitely.  [t] is the possibly infinite
1368  trace of observable events performed during the execution. *)
1369
1370Coinductive execinf_stmt: env -> mem -> statement -> traceinf -> Prop :=
1371  | execinf_Scall_none:   ∀e,m,a,al,vf,vargs,f,t.
1372      eval_expr e m a vf ->
1373      eval_exprlist e m al vargs ->
1374      Genv.find_funct ge vf = Some f ->
1375      type_of_fundef f = typeof a ->
1376      evalinf_funcall m f vargs t ->
1377      execinf_stmt e m (Scall None a al) t
1378  | execinf_Scall_some:   ∀e,m,lhs,a,al,loc,ofs,vf,vargs,f,t.
1379      eval_lvalue e m lhs loc ofs ->
1380      eval_expr e m a vf ->
1381      eval_exprlist e m al vargs ->
1382      Genv.find_funct ge vf = Some f ->
1383      type_of_fundef f = typeof a ->
1384      evalinf_funcall m f vargs t ->
1385      execinf_stmt e m (Scall (Some lhs) a al) t
1386  | execinf_Sseq_1:   ∀e,m,s1,s2,t.
1387      execinf_stmt e m s1 t ->
1388      execinf_stmt e m (Ssequence s1 s2) t
1389  | execinf_Sseq_2:   ∀e,m,s1,s2,t1,m1,t2.
1390      exec_stmt e m s1 t1 m1 Out_normal ->
1391      execinf_stmt e m1 s2 t2 ->
1392      execinf_stmt e m (Ssequence s1 s2) (t1 *** t2)
1393  | execinf_Sifthenelse_true: ∀e,m,a,s1,s2,v1,t.
1394      eval_expr e m a v1 ->
1395      is_true v1 (typeof a) ->
1396      execinf_stmt e m s1 t ->
1397      execinf_stmt e m (Sifthenelse a s1 s2) t
1398  | execinf_Sifthenelse_false: ∀e,m,a,s1,s2,v1,t.
1399      eval_expr e m a v1 ->
1400      is_false v1 (typeof a) ->
1401      execinf_stmt e m s2 t ->
1402      execinf_stmt e m (Sifthenelse a s1 s2) t
1403  | execinf_Swhile_body: ∀e,m,a,v,s,t.
1404      eval_expr e m a v ->
1405      is_true v (typeof a) ->
1406      execinf_stmt e m s t ->
1407      execinf_stmt e m (Swhile a s) t
1408  | execinf_Swhile_loop: ∀e,m,a,s,v,t1,m1,out1,t2.
1409      eval_expr e m a v ->
1410      is_true v (typeof a) ->
1411      exec_stmt e m s t1 m1 out1 ->
1412      out_normal_or_continue out1 ->
1413      execinf_stmt e m1 (Swhile a s) t2 ->
1414      execinf_stmt e m (Swhile a s) (t1 *** t2)
1415  | execinf_Sdowhile_body: ∀e,m,s,a,t.
1416      execinf_stmt e m s t ->
1417      execinf_stmt e m (Sdowhile a s) t
1418  | execinf_Sdowhile_loop: ∀e,m,s,a,m1,t1,t2,out1,v.
1419      exec_stmt e m s t1 m1 out1 ->
1420      out_normal_or_continue out1 ->
1421      eval_expr e m1 a v ->
1422      is_true v (typeof a) ->
1423      execinf_stmt e m1 (Sdowhile a s) t2 ->
1424      execinf_stmt e m (Sdowhile a s) (t1 *** t2)
1425  | execinf_Sfor_start_1: ∀e,m,s,a1,a2,a3,t.
1426      execinf_stmt e m a1 t ->
1427      execinf_stmt e m (Sfor a1 a2 a3 s) t
1428  | execinf_Sfor_start_2: ∀e,m,s,a1,a2,a3,m1,t1,t2.
1429      a1 <> Sskip ->
1430      exec_stmt e m a1 t1 m1 Out_normal ->
1431      execinf_stmt e m1 (Sfor Sskip a2 a3 s) t2 ->
1432      execinf_stmt e m (Sfor a1 a2 a3 s) (t1 *** t2)
1433  | execinf_Sfor_body: ∀e,m,s,a2,a3,v,t.
1434      eval_expr e m a2 v ->
1435      is_true v (typeof a2) ->
1436      execinf_stmt e m s t ->
1437      execinf_stmt e m (Sfor Sskip a2 a3 s) t
1438  | execinf_Sfor_next: ∀e,m,s,a2,a3,v,m1,t1,t2,out1.
1439      eval_expr e m a2 v ->
1440      is_true v (typeof a2) ->
1441      exec_stmt e m s t1 m1 out1 ->
1442      out_normal_or_continue out1 ->
1443      execinf_stmt e m1 a3 t2 ->
1444      execinf_stmt e m (Sfor Sskip a2 a3 s) (t1 *** t2)
1445  | execinf_Sfor_loop: ∀e,m,s,a2,a3,v,m1,m2,t1,t2,t3,out1.
1446      eval_expr e m a2 v ->
1447      is_true v (typeof a2) ->
1448      exec_stmt e m s t1 m1 out1 ->
1449      out_normal_or_continue out1 ->
1450      exec_stmt e m1 a3 t2 m2 Out_normal ->
1451      execinf_stmt e m2 (Sfor Sskip a2 a3 s) t3 ->
1452      execinf_stmt e m (Sfor Sskip a2 a3 s) (t1 *** t2 *** t3)
1453  | execinf_Sswitch:   ∀e,m,a,t,n,sl.
1454      eval_expr e m a (Vint n) ->
1455      execinf_stmt e m (seq_of_labeled_statement (select_switch n sl)) t ->
1456      execinf_stmt e m (Sswitch a sl) t
1457
1458(** [evalinf_funcall ge m fd args t] holds if the invocation of function
1459    [fd] on arguments [args] diverges, with observable trace [t]. *)
1460
1461with evalinf_funcall: mem -> fundef -> list val -> traceinf -> Prop :=
1462  | evalinf_funcall_internal: ∀m,f,vargs,t,e,m1,m2.
1463      alloc_variables empty_env m (f.(fn_params) ++ f.(fn_vars)) e m1 ->
1464      bind_parameters e m1 f.(fn_params) vargs m2 ->
1465      execinf_stmt e m2 f.(fn_body) t ->
1466      evalinf_funcall m (Internal f) vargs t.
1467
1468End SEMANTICS.
1469*)
1470(* * * Whole-program semantics *)
1471
1472(* * Execution of whole programs are described as sequences of transitions
1473  from an initial state to a final state.  An initial state is a [Callstate]
1474  corresponding to the invocation of the ``main'' function of the program
1475  without arguments and with an empty continuation. *)
1476
1477inductive initial_state (p: clight_program): state -> Prop :=
1478  | initial_state_intro: ∀b,f,ge,m0.
1479      globalenv Genv ?? p = OK ? ge →
1480      init_mem Genv ?? p = OK ? m0 →
1481      find_symbol ?? ge (prog_main ?? p) = Some ? 〈Code,b〉 →
1482      find_funct_ptr ?? ge b = Some ? f →
1483      initial_state p (Callstate f (nil ?) Kstop m0).
1484
1485(* * A final state is a [Returnstate] with an empty continuation. *)
1486
1487inductive final_state: state -> int -> Prop :=
1488  | final_state_intro: ∀r,m.
1489      final_state (Returnstate (Vint r) Kstop m) r.
1490
1491(* * Execution of a whole program: [exec_program p beh]
1492  holds if the application of [p]'s main function to no arguments
1493  in the initial memory state for [p] has [beh] as observable
1494  behavior. *)
1495
1496definition exec_program : clight_program → program_behavior → Prop ≝ λp,beh.
1497  ∀ge. globalenv ??? p = OK ? ge →
1498  program_behaves (mk_transrel ?? step) (initial_state p) final_state ge beh.
1499(*
1500(** Big-step execution of a whole program.  *)
1501
1502inductive bigstep_program_terminates (p: program): trace -> int -> Prop :=
1503  | bigstep_program_terminates_intro: ∀b,f,m1,t,r.
1504      let ge := Genv.globalenv p in
1505      let m0 := Genv.init_mem p in
1506      Genv.find_symbol ge p.(prog_main) = Some b ->
1507      Genv.find_funct_ptr ge b = Some f ->
1508      eval_funcall ge m0 f nil t m1 (Vint r) ->
1509      bigstep_program_terminates p t r.
1510
1511inductive bigstep_program_diverges (p: program): traceinf -> Prop :=
1512  | bigstep_program_diverges_intro: ∀b,f,t.
1513      let ge := Genv.globalenv p in
1514      let m0 := Genv.init_mem p in
1515      Genv.find_symbol ge p.(prog_main) = Some b ->
1516      Genv.find_funct_ptr ge b = Some f ->
1517      evalinf_funcall ge m0 f nil t ->
1518      bigstep_program_diverges p t.
1519
1520(** * Implication from big-step semantics to transition semantics *)
1521
1522Section BIGSTEP_TO_TRANSITIONS.
1523
1524Variable prog: program.
1525Let ge : genv := Genv.globalenv prog.
1526
1527Definition exec_stmt_eval_funcall_ind
1528  (PS: env -> mem -> statement -> trace -> mem -> outcome -> Prop)
1529  (PF: mem -> fundef -> list val -> trace -> mem -> val -> Prop) :=
1530  fun a b c d e f g h i j k l m n o p q r s t u v w x y =>
1531  conj (exec_stmt_ind2 ge PS PF a b c d e f g h i j k l m n o p q r s t u v w x y)
1532       (eval_funcall_ind2 ge PS PF a b c d e f g h i j k l m n o p q r s t u v w x y).
1533
1534inductive outcome_state_match
1535       (e: env) (m: mem) (f: function) (k: cont): outcome -> state -> Prop :=
1536  | osm_normal:
1537      outcome_state_match e m f k Out_normal (State f Sskip k e m)
1538  | osm_break:
1539      outcome_state_match e m f k Out_break (State f Sbreak k e m)
1540  | osm_continue:
1541      outcome_state_match e m f k Out_continue (State f Scontinue k e m)
1542  | osm_return_none: ∀k'.
1543      call_cont k' = call_cont k ->
1544      outcome_state_match e m f k
1545        (Out_return None) (State f (Sreturn None) k' e m)
1546  | osm_return_some: ∀a,v,k'.
1547      call_cont k' = call_cont k ->
1548      eval_expr ge e m a v ->
1549      outcome_state_match e m f k
1550        (Out_return (Some v)) (State f (Sreturn (Some a)) k' e m).
1551
1552Lemma is_call_cont_call_cont:
1553  ∀k. is_call_cont k -> call_cont k = k.
1554Proof.
1555  destruct k; simpl; intros; contradiction || auto.
1556Qed.
1557
1558Lemma exec_stmt_eval_funcall_steps:
1559  (∀e,m,s,t,m',out.
1560   exec_stmt ge e m s t m' out ->
1561   ∀f,k. exists S,
1562   star step ge (State f s k e m) t S
1563   /\ outcome_state_match e m' f k out S)
1564/\
1565  (∀m,fd,args,t,m',res.
1566   eval_funcall ge m fd args t m' res ->
1567   ∀k.
1568   is_call_cont k ->
1569   star step ge (Callstate fd args k m) t (Returnstate res k m')).
1570Proof.
1571  apply exec_stmt_eval_funcall_ind; intros.
1572
1573(* skip *)
1574  econstructor; split. apply star_refl. constructor.
1575
1576(* assign *)
1577  econstructor; split. apply star_one. econstructor; eauto. constructor.
1578
1579(* call none *)
1580  econstructor; split.
1581  eapply star_left. econstructor; eauto.
1582  eapply star_right. apply H4. simpl; auto. econstructor. reflexivity. traceEq.
1583  constructor.
1584
1585(* call some *)
1586  econstructor; split.
1587  eapply star_left. econstructor; eauto.
1588  eapply star_right. apply H5. simpl; auto. econstructor; eauto. reflexivity. traceEq.
1589  constructor.
1590
1591(* sequence 2 *)
1592  destruct (H0 f (Kseq s2 k)) as [S1 [A1 B1]]. inv B1.
1593  destruct (H2 f k) as [S2 [A2 B2]].
1594  econstructor; split.
1595  eapply star_left. econstructor.
1596  eapply star_trans. eexact A1.
1597  eapply star_left. constructor. eexact A2.
1598  reflexivity. reflexivity. traceEq.
1599  auto.
1600
1601(* sequence 1 *)
1602  destruct (H0 f (Kseq s2 k)) as [S1 [A1 B1]].
1603  set (S2 :=
1604    match out with
1605    | Out_break => State f Sbreak k e m1
1606    | Out_continue => State f Scontinue k e m1
1607    | _ => S1
1608    end).
1609  exists S2; split.
1610  eapply star_left. econstructor.
1611  eapply star_trans. eexact A1.
1612  unfold S2; inv B1.
1613    congruence.
1614    apply star_one. apply step_break_seq.
1615    apply star_one. apply step_continue_seq.
1616    apply star_refl.
1617    apply star_refl.
1618  reflexivity. traceEq.
1619  unfold S2; inv B1; congruence || econstructor; eauto.
1620
1621(* ifthenelse true *)
1622  destruct (H2 f k) as [S1 [A1 B1]].
1623  exists S1; split.
1624  eapply star_left. eapply step_ifthenelse_true; eauto. eexact A1. traceEq.
1625  auto.
1626
1627(* ifthenelse false *)
1628  destruct (H2 f k) as [S1 [A1 B1]].
1629  exists S1; split.
1630  eapply star_left. eapply step_ifthenelse_false; eauto. eexact A1. traceEq.
1631  auto.
1632
1633(* return none *)
1634  econstructor; split. apply star_refl. constructor. auto.
1635
1636(* return some *)
1637  econstructor; split. apply star_refl. econstructor; eauto.
1638
1639(* break *)
1640  econstructor; split. apply star_refl. constructor.
1641
1642(* continue *)
1643  econstructor; split. apply star_refl. constructor.
1644
1645(* while false *)
1646  econstructor; split.
1647  apply star_one. eapply step_while_false; eauto.
1648  constructor.
1649
1650(* while stop *)
1651  destruct (H2 f (Kwhile a s k)) as [S1 [A1 B1]].
1652  set (S2 :=
1653    match out' with
1654    | Out_break => State f Sskip k e m'
1655    | _ => S1
1656    end).
1657  exists S2; split.
1658  eapply star_left. eapply step_while_true; eauto.
1659  eapply star_trans. eexact A1.
1660  unfold S2. inversion H3; subst.
1661  inv B1. apply star_one. constructor.   
1662  apply star_refl.
1663  reflexivity. traceEq.
1664  unfold S2. inversion H3; subst. constructor. inv B1; econstructor; eauto.
1665
1666(* while loop *)
1667  destruct (H2 f (Kwhile a s k)) as [S1 [A1 B1]].
1668  destruct (H5 f k) as [S2 [A2 B2]].
1669  exists S2; split.
1670  eapply star_left. eapply step_while_true; eauto.
1671  eapply star_trans. eexact A1.
1672  eapply star_left.
1673  inv H3; inv B1; apply step_skip_or_continue_while; auto.
1674  eexact A2.
1675  reflexivity. reflexivity. traceEq.
1676  auto.
1677
1678(* dowhile false *)
1679  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1680  exists (State f Sskip k e m1); split.
1681  eapply star_left. constructor.
1682  eapply star_right. eexact A1.
1683  inv H1; inv B1; eapply step_skip_or_continue_dowhile_false; eauto.
1684  reflexivity. traceEq.
1685  constructor.
1686
1687(* dowhile stop *)
1688  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1689  set (S2 :=
1690    match out1 with
1691    | Out_break => State f Sskip k e m1
1692    | _ => S1
1693    end).
1694  exists S2; split.
1695  eapply star_left. apply step_dowhile.
1696  eapply star_trans. eexact A1.
1697  unfold S2. inversion H1; subst.
1698  inv B1. apply star_one. constructor.
1699  apply star_refl.
1700  reflexivity. traceEq.
1701  unfold S2. inversion H1; subst. constructor. inv B1; econstructor; eauto.
1702
1703(* dowhile loop *)
1704  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1705  destruct (H5 f k) as [S2 [A2 B2]].
1706  exists S2; split.
1707  eapply star_left. apply step_dowhile.
1708  eapply star_trans. eexact A1.
1709  eapply star_left.
1710  inv H1; inv B1; eapply step_skip_or_continue_dowhile_true; eauto.
1711  eexact A2.
1712  reflexivity. reflexivity. traceEq.
1713  auto.
1714
1715(* for start *)
1716  destruct (H1 f (Kseq (Sfor Sskip a2 a3 s) k)) as [S1 [A1 B1]]. inv B1.
1717  destruct (H3 f k) as [S2 [A2 B2]].
1718  exists S2; split.
1719  eapply star_left. apply step_for_start; auto.   
1720  eapply star_trans. eexact A1.
1721  eapply star_left. constructor. eexact A2.
1722  reflexivity. reflexivity. traceEq.
1723  auto.
1724
1725(* for false *)
1726  econstructor; split.
1727  eapply star_one. eapply step_for_false; eauto.
1728  constructor.
1729
1730(* for stop *)
1731  destruct (H2 f (Kfor2 a2 a3 s k)) as [S1 [A1 B1]].
1732  set (S2 :=
1733    match out1 with
1734    | Out_break => State f Sskip k e m1
1735    | _ => S1
1736    end).
1737  exists S2; split.
1738  eapply star_left. eapply step_for_true; eauto.
1739  eapply star_trans. eexact A1.
1740  unfold S2. inversion H3; subst.
1741  inv B1. apply star_one. constructor.
1742  apply star_refl.
1743  reflexivity. traceEq.
1744  unfold S2. inversion H3; subst. constructor. inv B1; econstructor; eauto.
1745
1746(* for loop *)
1747  destruct (H2 f (Kfor2 a2 a3 s k)) as [S1 [A1 B1]].
1748  destruct (H5 f (Kfor3 a2 a3 s k)) as [S2 [A2 B2]]. inv B2.
1749  destruct (H7 f k) as [S3 [A3 B3]].
1750  exists S3; split.
1751  eapply star_left. eapply step_for_true; eauto.
1752  eapply star_trans. eexact A1.
1753  eapply star_trans with (s2 := State f a3 (Kfor3 a2 a3 s k) e m1).
1754  inv H3; inv B1.
1755  apply star_one. constructor. auto.
1756  apply star_one. constructor. auto.
1757  eapply star_trans. eexact A2.
1758  eapply star_left. constructor.
1759  eexact A3.
1760  reflexivity. reflexivity. reflexivity. reflexivity. traceEq.
1761  auto.
1762
1763(* switch *)
1764  destruct (H1 f (Kswitch k)) as [S1 [A1 B1]].
1765  set (S2 :=
1766    match out with
1767    | Out_normal => State f Sskip k e m1
1768    | Out_break => State f Sskip k e m1
1769    | Out_continue => State f Scontinue k e m1
1770    | _ => S1
1771    end).
1772  exists S2; split.
1773  eapply star_left. eapply step_switch; eauto.
1774  eapply star_trans. eexact A1.
1775  unfold S2; inv B1.
1776    apply star_one. constructor. auto.
1777    apply star_one. constructor. auto.
1778    apply star_one. constructor.
1779    apply star_refl.
1780    apply star_refl.
1781  reflexivity. traceEq.
1782  unfold S2. inv B1; simpl; econstructor; eauto.
1783
1784(* call internal *)
1785  destruct (H2 f k) as [S1 [A1 B1]].
1786  eapply star_left. eapply step_internal_function; eauto.
1787  eapply star_right. eexact A1.
1788  inv B1; simpl in H3; try contradiction.
1789  (* Out_normal *)
1790  assert (fn_return f = Tvoid /\ vres = Vundef).
1791    destruct (fn_return f); auto || contradiction.
1792  destruct H5. subst vres. apply step_skip_call; auto.
1793  (* Out_return None *)
1794  assert (fn_return f = Tvoid /\ vres = Vundef).
1795    destruct (fn_return f); auto || contradiction.
1796  destruct H6. subst vres.
1797  rewrite <- (is_call_cont_call_cont k H4). rewrite <- H5.
1798  apply step_return_0; auto.
1799  (* Out_return Some *)
1800  destruct H3. subst vres.
1801  rewrite <- (is_call_cont_call_cont k H4). rewrite <- H5.
1802  eapply step_return_1; eauto.
1803  reflexivity. traceEq.
1804
1805(* call external *)
1806  apply star_one. apply step_external_function; auto.
1807Qed.
1808
1809Lemma exec_stmt_steps:
1810   ∀e,m,s,t,m',out.
1811   exec_stmt ge e m s t m' out ->
1812   ∀f,k. exists S,
1813   star step ge (State f s k e m) t S
1814   /\ outcome_state_match e m' f k out S.
1815Proof (proj1 exec_stmt_eval_funcall_steps).
1816
1817Lemma eval_funcall_steps:
1818   ∀m,fd,args,t,m',res.
1819   eval_funcall ge m fd args t m' res ->
1820   ∀k.
1821   is_call_cont k ->
1822   star step ge (Callstate fd args k m) t (Returnstate res k m').
1823Proof (proj2 exec_stmt_eval_funcall_steps).
1824
1825Definition order (x y: unit) := False.
1826
1827Lemma evalinf_funcall_forever:
1828  ∀m,fd,args,T,k.
1829  evalinf_funcall ge m fd args T ->
1830  forever_N step order ge tt (Callstate fd args k m) T.
1831Proof.
1832  cofix CIH_FUN.
1833  assert (∀e,m,s,T,f,k.
1834          execinf_stmt ge e m s T ->
1835          forever_N step order ge tt (State f s k e m) T).
1836  cofix CIH_STMT.
1837  intros. inv H.
1838
1839(* call none *)
1840  eapply forever_N_plus.
1841  apply plus_one. eapply step_call_none; eauto.
1842  apply CIH_FUN. eauto. traceEq.
1843(* call some *)
1844  eapply forever_N_plus.
1845  apply plus_one. eapply step_call_some; eauto.
1846  apply CIH_FUN. eauto. traceEq.
1847
1848(* seq 1 *)
1849  eapply forever_N_plus.
1850  apply plus_one. econstructor.
1851  apply CIH_STMT; eauto. traceEq.
1852(* seq 2 *)
1853  destruct (exec_stmt_steps _ _ _ _ _ _ H0 f (Kseq s2 k)) as [S1 [A1 B1]].
1854  inv B1.
1855  eapply forever_N_plus.
1856  eapply plus_left. constructor. eapply star_trans. eexact A1.
1857  apply star_one. constructor. reflexivity. reflexivity.
1858  apply CIH_STMT; eauto. traceEq.
1859
1860(* ifthenelse true *)
1861  eapply forever_N_plus.
1862  apply plus_one. eapply step_ifthenelse_true; eauto.
1863  apply CIH_STMT; eauto. traceEq.
1864(* ifthenelse false *)
1865  eapply forever_N_plus.
1866  apply plus_one. eapply step_ifthenelse_false; eauto.
1867  apply CIH_STMT; eauto. traceEq.
1868
1869(* while body *)
1870  eapply forever_N_plus.
1871  eapply plus_one. eapply step_while_true; eauto.
1872  apply CIH_STMT; eauto. traceEq.
1873(* while loop *)
1874  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kwhile a s0 k)) as [S1 [A1 B1]].
1875  eapply forever_N_plus with (s2 := State f (Swhile a s0) k e m1).
1876  eapply plus_left. eapply step_while_true; eauto.
1877  eapply star_right. eexact A1.
1878  inv H3; inv B1; apply step_skip_or_continue_while; auto.
1879  reflexivity. reflexivity.
1880  apply CIH_STMT; eauto. traceEq.
1881
1882(* dowhile body *)
1883  eapply forever_N_plus.
1884  eapply plus_one. eapply step_dowhile.
1885  apply CIH_STMT; eauto.
1886  traceEq.
1887
1888(* dowhile loop *)
1889  destruct (exec_stmt_steps _ _ _ _ _ _ H0 f (Kdowhile a s0 k)) as [S1 [A1 B1]].
1890  eapply forever_N_plus with (s2 := State f (Sdowhile a s0) k e m1).
1891  eapply plus_left. eapply step_dowhile.
1892  eapply star_right. eexact A1.
1893  inv H1; inv B1; eapply step_skip_or_continue_dowhile_true; eauto.
1894  reflexivity. reflexivity.
1895  apply CIH_STMT. eauto.
1896  traceEq.
1897
1898(* for start 1 *)
1899  assert (a1 <> Sskip). red; intros; subst. inv H0.
1900  eapply forever_N_plus.
1901  eapply plus_one. apply step_for_start; auto.
1902  apply CIH_STMT; eauto.
1903  traceEq.
1904
1905(* for start 2 *)
1906  destruct (exec_stmt_steps _ _ _ _ _ _ H1 f (Kseq (Sfor Sskip a2 a3 s0) k)) as [S1 [A1 B1]].
1907  inv B1.
1908  eapply forever_N_plus.
1909  eapply plus_left. eapply step_for_start; eauto.
1910  eapply star_right. eexact A1.
1911  apply step_skip_seq.
1912  reflexivity. reflexivity.
1913  apply CIH_STMT; eauto.
1914  traceEq.
1915
1916(* for body *)
1917  eapply forever_N_plus.
1918  apply plus_one. eapply step_for_true; eauto.
1919  apply CIH_STMT; eauto.
1920  traceEq.
1921
1922(* for next *)
1923  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kfor2 a2 a3 s0 k)) as [S1 [A1 B1]].
1924  eapply forever_N_plus.
1925  eapply plus_left. eapply step_for_true; eauto.
1926  eapply star_trans. eexact A1.
1927  apply star_one.
1928  inv H3; inv B1; apply step_skip_or_continue_for2; auto.
1929  reflexivity. reflexivity.
1930  apply CIH_STMT; eauto.
1931  traceEq.
1932
1933(* for body *)
1934  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kfor2 a2 a3 s0 k)) as [S1 [A1 B1]].
1935  destruct (exec_stmt_steps _ _ _ _ _ _ H4 f (Kfor3 a2 a3 s0 k)) as [S2 [A2 B2]].
1936  inv B2.
1937  eapply forever_N_plus.
1938  eapply plus_left. eapply step_for_true; eauto.
1939  eapply star_trans. eexact A1.
1940  eapply star_left. inv H3; inv B1; apply step_skip_or_continue_for2; auto.
1941  eapply star_right. eexact A2.
1942  constructor.
1943  reflexivity. reflexivity. reflexivity. reflexivity. 
1944  apply CIH_STMT; eauto.
1945  traceEq.
1946
1947(* switch *)
1948  eapply forever_N_plus.
1949  eapply plus_one. eapply step_switch; eauto.
1950  apply CIH_STMT; eauto.
1951  traceEq.
1952
1953(* call internal *)
1954  intros. inv H0.
1955  eapply forever_N_plus.
1956  eapply plus_one. econstructor; eauto.
1957  apply H; eauto.
1958  traceEq.
1959Qed.
1960
1961Theorem bigstep_program_terminates_exec:
1962  ∀t,r. bigstep_program_terminates prog t r -> exec_program prog (Terminates t r).
1963Proof.
1964  intros. inv H. unfold ge0, m0 in *.
1965  econstructor.
1966  econstructor. eauto. eauto.
1967  apply eval_funcall_steps. eauto. red; auto.
1968  econstructor.
1969Qed.
1970
1971Theorem bigstep_program_diverges_exec:
1972  ∀T. bigstep_program_diverges prog T ->
1973  exec_program prog (Reacts T) \/
1974  exists t, exec_program prog (Diverges t) /\ traceinf_prefix t T.
1975Proof.
1976  intros. inv H.
1977  set (st := Callstate f nil Kstop m0).
1978  assert (forever step ge0 st T).
1979    eapply forever_N_forever with (order := order).
1980    red; intros. constructor; intros. red in H. elim H.
1981    eapply evalinf_funcall_forever; eauto.
1982  destruct (forever_silent_or_reactive _ _ _ _ _ _ H)
1983  as [A | [t [s' [T' [B [C D]]]]]].
1984  left. econstructor. econstructor. eauto. eauto. auto.
1985  right. exists t. split.
1986  econstructor. econstructor; eauto. eauto. auto.
1987  subst T. rewrite <- (E0_right t) at 1. apply traceinf_prefix_app. constructor.
1988Qed.
1989
1990End BIGSTEP_TO_TRANSITIONS.
1991
1992
1993
1994*)
1995
1996 
Note: See TracBrowser for help on using the repository browser.