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

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

Make block type a little more abstract; remove knowledge about the old
representation for a pointer from the evaluation of lvalues.

File size: 77.6 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 eq_block 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 eq_block 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_region 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) (b: block) (ofs: int) : option val ≝
467  match access_mode ty with
468  [ By_value chunk ⇒ loadv chunk m (Vptr Any b ofs)
469  | By_reference r ⇒ Some ? (Vptr r 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) (loc: block) (ofs: int) (v: val) : option mem ≝
479  match access_mode ty_dest with
480  [ By_value chunk ⇒ storev chunk m (Vptr Any 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 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(* * Return the list of blocks in the codomain of [e]. *)
523
524definition blocks_of_env : env → list block ≝ λe.
525  map ?? (λx. snd ?? x) (elements ??? e).
526
527(* * Selection of the appropriate case of a [switch], given the value [n]
528  of the selector expression. *)
529
530let rec select_switch (n: int) (sl: labeled_statements)
531                       on sl : labeled_statements ≝
532  match sl with
533  [ LSdefault _ ⇒ sl
534  | LScase c s sl' ⇒ if eq c n then sl else select_switch n sl'
535  ].
536
537(* * Turn a labeled statement into a sequence *)
538
539let rec seq_of_labeled_statement (sl: labeled_statements) : statement ≝
540  match sl with
541  [ LSdefault s ⇒ s
542  | LScase c s sl' ⇒ Ssequence s (seq_of_labeled_statement sl')
543  ].
544
545(*
546Section SEMANTICS.
547
548Variable ge: genv.
549
550(** ** Evaluation of expressions *)
551
552Section EXPR.
553
554Variable e: env.
555Variable m: mem.
556*)
557(* * [eval_expr ge e m a v] defines the evaluation of expression [a]
558  in r-value position.  [v] is the value of the expression.
559  [e] is the current environment and [m] is the current memory state. *)
560
561inductive eval_expr (ge:genv) (e:env) (m:mem) : expr → val → trace → Prop ≝
562  | eval_Econst_int:   ∀i,ty.
563      eval_expr ge e m (Expr (Econst_int i) ty) (Vint i) E0
564  | eval_Econst_float:   ∀f,ty.
565      eval_expr ge e m (Expr (Econst_float f) ty) (Vfloat f) E0
566  | eval_Elvalue: ∀a,ty,loc,ofs,v,tr.
567      eval_lvalue ge e m (Expr a ty) loc ofs tr →
568      load_value_of_type ty m loc ofs = Some ? v →
569      eval_expr ge e m (Expr a ty) v tr
570  | eval_Eaddrof: ∀a,ty,r,loc,ofs,tr.
571      eval_lvalue ge e m a loc ofs tr →
572      eval_expr ge e m (Expr (Eaddrof a) (Tpointer r ty)) (Vptr r loc ofs) tr
573  | eval_Esizeof: ∀ty',ty.
574      eval_expr ge e m (Expr (Esizeof ty') ty) (Vint (repr (sizeof ty'))) E0
575  | eval_Eunop:  ∀op,a,ty,v1,v,tr.
576      eval_expr ge e m a v1 tr →
577      sem_unary_operation op v1 (typeof a) = Some ? v →
578      eval_expr ge e m (Expr (Eunop op a) ty) v tr
579  | eval_Ebinop: ∀op,a1,a2,ty,v1,v2,v,tr1,tr2.
580      eval_expr ge e m a1 v1 tr1 →
581      eval_expr ge e m a2 v2 tr2 →
582      sem_binary_operation op v1 (typeof a1) v2 (typeof a2) m = Some ? v →
583      eval_expr ge e m (Expr (Ebinop op a1 a2) ty) v (tr1⧺tr2)
584  | eval_Econdition_true: ∀a1,a2,a3,ty,v1,v2,tr1,tr2.
585      eval_expr ge e m a1 v1 tr1 →
586      is_true v1 (typeof a1) →
587      eval_expr ge e m a2 v2 tr2 →
588      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v2 (tr1⧺tr2)
589  | eval_Econdition_false: ∀a1,a2,a3,ty,v1,v3,tr1,tr2.
590      eval_expr ge e m a1 v1 tr1 →
591      is_false v1 (typeof a1) →
592      eval_expr ge e m a3 v3 tr2 →
593      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v3 (tr1⧺tr2)
594  | eval_Eorbool_1: ∀a1,a2,ty,v1,tr.
595      eval_expr ge e m a1 v1 tr →
596      is_true v1 (typeof a1) →
597      eval_expr ge e m (Expr (Eorbool a1 a2) ty) Vtrue tr
598  | eval_Eorbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
599      eval_expr ge e m a1 v1 tr1 →
600      is_false v1 (typeof a1) →
601      eval_expr ge e m a2 v2 tr2 →
602      bool_of_val v2 (typeof a2) v →
603      eval_expr ge e m (Expr (Eorbool a1 a2) ty) v (tr1⧺tr2)
604  | eval_Eandbool_1: ∀a1,a2,ty,v1,tr.
605      eval_expr ge e m a1 v1 tr →
606      is_false v1 (typeof a1) →
607      eval_expr ge e m (Expr (Eandbool a1 a2) ty) Vfalse tr
608  | eval_Eandbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
609      eval_expr ge e m a1 v1 tr1 →
610      is_true v1 (typeof a1) →
611      eval_expr ge e m a2 v2 tr2 →
612      bool_of_val v2 (typeof a2) v →
613      eval_expr ge e m (Expr (Eandbool a1 a2) ty) v (tr1⧺tr2)
614  | eval_Ecast:   ∀a,ty,ty',v1,v,tr.
615      eval_expr ge e m a v1 tr →
616      cast m v1 (typeof a) ty v →
617      eval_expr ge e m (Expr (Ecast ty a) ty') v tr
618  | eval_Ecost: ∀a,ty,v,l,tr.
619      eval_expr ge e m a v tr →
620      eval_expr ge e m (Expr (Ecost l a) ty) v (tr⧺Echarge l)
621
622(* * [eval_lvalue ge e m a r b ofs] defines the evaluation of expression [a]
623  in l-value position.  The result is the memory location [b, ofs]
624  that contains the value of the expression [a].  The memory location should
625  be representable in a pointer of region r. *)
626
627with eval_lvalue (*(ge:genv) (e:env) (m:mem)*) : expr → block → int → trace → Prop ≝
628  | eval_Evar_local:   ∀id,l,ty.
629      (* XXX notation? e!id*) get ??? id e = Some ? l →
630      eval_lvalue ge e m (Expr (Evar id) ty) l zero E0
631  | eval_Evar_global: ∀id,l,ty.
632      (* XXX e!id *) get ??? id e = None ? →
633      find_symbol ?? ge id = Some ? l →
634      eval_lvalue ge e m (Expr (Evar id) ty) l zero E0
635  | eval_Ederef: ∀a,ty,r,l,ofs,tr.
636      eval_expr ge e m a (Vptr r l ofs) tr →
637      eval_lvalue ge e m (Expr (Ederef a) ty) l ofs tr
638    (* Aside: note that each block of memory is entirely contained within one
639       memory region; hence adding a field offset will not produce a location
640       outside of the original location's region. *)
641 | eval_Efield_struct:   ∀a,i,ty,l,ofs,id,fList,delta,tr.
642      eval_lvalue ge e m a l ofs tr →
643      typeof a = Tstruct id fList →
644      field_offset i fList = OK ? delta →
645      eval_lvalue ge e m (Expr (Efield a i) ty) l (add ofs (repr delta)) tr
646 | eval_Efield_union:   ∀a,i,ty,l,ofs,id,fList,tr.
647      eval_lvalue ge e m a l ofs tr →
648      typeof a = Tunion id fList →
649      eval_lvalue ge e m (Expr (Efield a i) ty) l ofs tr.
650
651let rec eval_expr_ind (ge:genv) (e:env) (m:mem)
652  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
653  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
654  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
655  (elv:∀a,ty,loc,ofs,v,tr,H1,H2. P ??? (eval_Elvalue ge e m a ty loc ofs v tr H1 H2))
656  (ead:∀a,ty,psp,loc,ofs,tr,H. P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
657  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
658  (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))
659  (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))
660  (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))
661  (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))
662  (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))
663  (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))
664  (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))
665  (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))
666  (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))
667  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
668  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
669  match ev with
670  [ eval_Econst_int i ty ⇒ eci i ty
671  | eval_Econst_float f ty ⇒ ecF f ty
672  | eval_Elvalue a ty loc ofs v tr H1 H2 ⇒ elv a ty loc ofs v tr H1 H2
673  | eval_Eaddrof a ty psp loc ofs tr H ⇒ ead a ty psp loc ofs tr H
674  | eval_Esizeof ty' ty ⇒ esz ty' ty
675  | 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)
676  | 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)
677  | 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)
678  | 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)
679  | 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)
680  | 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)
681  | 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)
682  | 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)
683  | 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)
684  | 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)
685  ].
686
687inverter eval_expr_inv_ind for eval_expr : Prop.
688
689let rec eval_lvalue_ind (ge:genv) (e:env) (m:mem)
690  (P:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
691  (lvl:∀id,l,ty,H. P ???? (eval_Evar_local ge e m id l ty H))
692  (lvg:∀id,l,ty,H1,H2. P ???? (eval_Evar_global ge e m id l ty H1 H2))
693  (lde:∀a,ty,psp,l,ofs,tr,H. P ???? (eval_Ederef ge e m a ty psp l ofs tr H))
694  (lfs:∀a,i,ty,l,ofs,id,fList,delta,tr,H1,H2,H3. P a l ofs tr H1 → P ???? (eval_Efield_struct ge e m a i ty l ofs id fList delta tr H1 H2 H3))
695  (lfu:∀a,i,ty,l,ofs,id,fList,tr,H1,H2. P a l ofs tr H1 → P ???? (eval_Efield_union ge e m a i ty l ofs id fList tr H1 H2))
696  (a:expr) (loc:block) (ofs:int) (tr:trace) (ev:eval_lvalue ge e m a loc ofs tr) on ev : P a loc ofs tr ev ≝
697  match ev with
698  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
699  | eval_Evar_global id l ty H1 H2 ⇒ lvg id l ty H1 H2
700  | eval_Ederef a ty psp l ofs tr H ⇒ lde a ty psp l ofs tr H
701  | eval_Efield_struct a i ty l ofs id fList delta tr H1 H2 H3 ⇒ lfs a i ty l ofs id fList delta tr H1 H2 H3 (eval_lvalue_ind ge e m P lvl lvg lde lfs lfu a l ofs tr H1)
702  | eval_Efield_union a i ty l ofs id fList tr H1 H2 ⇒ lfu a i ty l ofs id fList tr H1 H2 (eval_lvalue_ind ge e m P lvl lvg lde lfs lfu a l ofs tr H1)
703  ].
704
705(*
706ninverter eval_lvalue_inv_ind for eval_lvalue : Prop.
707*)
708
709definition eval_lvalue_inv_ind :
710  ∀x1: genv.
711   ∀x2: env.
712    ∀x3: mem.
713     ∀x4: expr.
714       ∀x6: block.
715        ∀x7: int.
716         ∀x8: trace.
717          ∀P:
718            ∀_z1430: expr.
719              ∀_z1428: block. ∀_z1427: int. ∀_z1426: trace. Prop.
720           ∀_H1: ?.
721            ∀_H2: ?.
722             ∀_H3: ?.
723              ∀_H4: ?.
724               ∀_H5: ?.
725                ∀_Hterm: eval_lvalue x1 x2 x3 x4 x6 x7 x8.
726                 P x4 x6 x7 x8
727:=
728  (λx1:genv.
729    (λx2:env.
730      (λx3:mem.
731        (λx4:expr.
732            (λx6:block.
733              (λx7:int.
734                (λx8:trace.
735                  (λP:∀_z1430: expr.
736                         ∀_z1428: block.
737                          ∀_z1427: int. ∀_z1426: trace. Prop.
738                    (λH1:?.
739                      (λH2:?.
740                        (λH3:?.
741                          (λH4:?.
742                            (λH5:?.
743                              (λHterm:eval_lvalue x1 x2 x3 x4 x6 x7 x8.
744                                ((λHcut:∀z1435: eq expr x4 x4.
745                                           ∀z1433: eq block x6 x6.
746                                            ∀z1432: eq int x7 x7.
747                                             ∀z1431: eq trace x8 x8.
748                                              P x4 x6 x7 x8.
749                                   (Hcut (refl expr x4)
750                                     (refl block x6)
751                                     (refl int x7) (refl trace x8)))
752                                  ?))))))))))))))).
753[ @(eval_lvalue_ind x1 x2 x3 (λa,loc,ofs,tr,e. ∀e1:eq ? x4 a. ∀e3:eq ? x6 loc. ∀e4:eq ? x7 ofs. ∀e5:eq ? x8 tr. P a loc ofs tr) … Hterm)
754  [ @H1 | @H2 | @H3 | @H4 | @H5 ]
755| *: skip
756] qed.
757
758let rec eval_expr_ind2 (ge:genv) (e:env) (m:mem)
759  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
760  (Q:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
761  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
762  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
763  (elv:∀a,ty,loc,ofs,v,tr,H1,H2. Q (Expr a ty) loc ofs tr H1 → P ??? (eval_Elvalue ge e m a ty loc ofs v tr H1 H2))
764  (ead:∀a,ty,psp,loc,ofs,tr,H. Q a loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
765  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
766  (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))
767  (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))
768  (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))
769  (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))
770  (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))
771  (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))
772  (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))
773  (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))
774  (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))
775  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
776  (lvl:∀id,l,ty,H. Q ???? (eval_Evar_local ge e m id l ty H))
777  (lvg:∀id,l,ty,H1,H2. Q ???? (eval_Evar_global ge e m id l ty H1 H2))
778  (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))
779  (lfs:∀a,i,ty,l,ofs,id,fList,delta,tr,H1,H2,H3. Q a l ofs tr H1 → Q ???? (eval_Efield_struct ge e m a i ty l ofs id fList delta tr H1 H2 H3))
780  (lfu:∀a,i,ty,l,ofs,id,fList,tr,H1,H2. Q a l ofs tr H1 → Q ???? (eval_Efield_union ge e m a i ty l ofs id fList tr H1 H2))
781 
782  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
783  match ev with
784  [ eval_Econst_int i ty ⇒ eci i ty
785  | eval_Econst_float f ty ⇒ ecF f ty
786  | eval_Elvalue a ty loc ofs v tr H1 H2 ⇒ elv a ty 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) loc ofs tr H1)
787  | 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 loc ofs tr H)
788  | eval_Esizeof ty' ty ⇒ esz ty' ty
789  | 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)
790  | 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)
791  | 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)
792  | 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)
793  | 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)
794  | 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)
795  | 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)
796  | 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)
797  | 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)
798  | 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)
799  ]
800and eval_lvalue_ind2 (ge:genv) (e:env) (m:mem)
801  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
802  (Q:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
803  (eci:∀i,ty. P ??? (eval_Econst_int ge e m i ty))
804  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
805  (elv:∀a,ty,loc,ofs,v,tr,H1,H2. Q (Expr a ty) loc ofs tr H1 → P ??? (eval_Elvalue ge e m a ty loc ofs v tr H1 H2))
806  (ead:∀a,ty,psp,loc,ofs,tr,H. Q a loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty psp loc ofs tr H))
807  (esz:∀ty',ty. P ??? (eval_Esizeof ge e m ty' ty))
808  (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))
809  (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))
810  (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))
811  (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))
812  (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))
813  (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))
814  (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))
815  (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))
816  (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))
817  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
818  (lvl:∀id,l,ty,H. Q ???? (eval_Evar_local ge e m id l ty H))
819  (lvg:∀id,l,ty,H1,H2. Q ???? (eval_Evar_global ge e m id l ty H1 H2))
820  (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))
821  (lfs:∀a,i,ty,l,ofs,id,fList,delta,tr,H1,H2,H3. Q a l ofs tr H1 → Q ???? (eval_Efield_struct ge e m a i ty l ofs id fList delta tr H1 H2 H3))
822  (lfu:∀a,i,ty,l,ofs,id,fList,tr,H1,H2. Q a l ofs tr H1 → Q ???? (eval_Efield_union ge e m a i ty l ofs id fList tr H1 H2))
823  (a:expr) (loc:block) (ofs:int) (tr:trace) (ev:eval_lvalue ge e m a loc ofs tr) on ev : Q a loc ofs tr ev ≝
824  match ev with
825  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
826  | eval_Evar_global id l ty H1 H2 ⇒ lvg id l ty H1 H2
827  | 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)
828  | eval_Efield_struct a i ty l ofs id fList delta tr H1 H2 H3 ⇒ lfs a i ty 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 l ofs tr H1)
829  | eval_Efield_union a i ty l ofs id fList tr H1 H2 ⇒ lfu a i ty 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 l ofs tr H1)
830  ].
831
832definition combined_expr_lvalue_ind ≝
833λ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. 
834conj ??
835  (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)
836  (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).
837
838(* * [eval_lvalue ge e m a b ofs] defines the evaluation of expression [a]
839  in l-value position.  The result is the memory location [b, ofs]
840  that contains the value of the expression [a]. *)
841
842(*
843Scheme eval_expr_ind22 := Minimality for eval_expr Sort Prop
844  with eval_lvalue_ind2 := Minimality for eval_lvalue Sort Prop.
845*)
846
847(* * [eval_exprlist ge e m al vl] evaluates a list of r-value
848  expressions [al] to their values [vl]. *)
849
850inductive eval_exprlist (ge:genv) (e:env) (m:mem) : list expr → list val → trace → Prop ≝
851  | eval_Enil:
852      eval_exprlist ge e m (nil ?) (nil ?) E0
853  | eval_Econs:   ∀a,bl,v,vl,tr1,tr2.
854      eval_expr ge e m a v tr1 →
855      eval_exprlist ge e m bl vl tr2 →
856      eval_exprlist ge e m (a :: bl) (v :: vl) (tr1⧺tr2).
857
858(*End EXPR.*)
859
860(* * ** Transition semantics for statements and functions *)
861
862(* * Continuations *)
863
864inductive cont: Type[0] :=
865  | Kstop: cont
866  | Kseq: statement -> cont -> cont
867       (**r [Kseq s2 k] = after [s1] in [s1;s2] *)
868  | Kwhile: expr -> statement -> cont -> cont
869       (**r [Kwhile e s k] = after [s] in [while (e) s] *)
870  | Kdowhile: expr -> statement -> cont -> cont
871       (**r [Kdowhile e s k] = after [s] in [do s while (e)] *)
872  | Kfor2: expr -> statement -> statement -> cont -> cont
873       (**r [Kfor2 e2 e3 s k] = after [s] in [for(e1;e2;e3) s] *)
874  | Kfor3: expr -> statement -> statement -> cont -> cont
875       (**r [Kfor3 e2 e3 s k] = after [e3] in [for(e1;e2;e3) s] *)
876  | Kswitch: cont -> cont
877       (**r catches [break] statements arising out of [switch] *)
878  | Kcall: option (block × int × type) ->   (**r where to store result *)
879           function ->                      (**r calling function *)
880           env ->                           (**r local env of calling function *)
881           cont -> cont.
882
883(* * Pop continuation until a call or stop *)
884
885let rec call_cont (k: cont) : cont :=
886  match k with
887  [ Kseq s k => call_cont k
888  | Kwhile e s k => call_cont k
889  | Kdowhile e s k => call_cont k
890  | Kfor2 e2 e3 s k => call_cont k
891  | Kfor3 e2 e3 s k => call_cont k
892  | Kswitch k => call_cont k
893  | _ => k
894  ].
895
896definition is_call_cont : cont → Prop ≝ λk.
897  match k with
898  [ Kstop => True
899  | Kcall _ _ _ _ => True
900  | _ => False
901  ].
902
903(* * States *)
904
905inductive state: Type[0] :=
906  | State:
907      ∀f: function.
908      ∀s: statement.
909      ∀k: cont.
910      ∀e: env.
911      ∀m: mem.  state
912  | Callstate:
913      ∀fd: fundef.
914      ∀args: list val.
915      ∀k: cont.
916      ∀m: mem. state
917  | Returnstate:
918      ∀res: val.
919      ∀k: cont.
920      ∀m: mem. state.
921                 
922(* * Find the statement and manufacture the continuation
923  corresponding to a label *)
924
925let rec find_label (lbl: label) (s: statement) (k: cont)
926                    on s: option (statement × cont) :=
927  match s with
928  [ Ssequence s1 s2 =>
929      match find_label lbl s1 (Kseq s2 k) with
930      [ Some sk => Some ? sk
931      | None => find_label lbl s2 k
932      ]
933  | Sifthenelse a s1 s2 =>
934      match find_label lbl s1 k with
935      [ Some sk => Some ? sk
936      | None => find_label lbl s2 k
937      ]
938  | Swhile a s1 =>
939      find_label lbl s1 (Kwhile a s1 k)
940  | Sdowhile a s1 =>
941      find_label lbl s1 (Kdowhile a s1 k)
942  | Sfor a1 a2 a3 s1 =>
943      match find_label lbl a1 (Kseq (Sfor Sskip a2 a3 s1) k) with
944      [ Some sk => Some ? sk
945      | None =>
946          match find_label lbl s1 (Kfor2 a2 a3 s1 k) with
947          [ Some sk => Some ? sk
948          | None => find_label lbl a3 (Kfor3 a2 a3 s1 k)
949          ]
950      ]
951  | Sswitch e sl =>
952      find_label_ls lbl sl (Kswitch k)
953  | Slabel lbl' s' =>
954      match ident_eq lbl lbl' with
955      [ inl _ ⇒ Some ? 〈s', k〉
956      | inr _ ⇒ find_label lbl s' k
957      ]
958  | _ => None ?
959  ]
960
961and find_label_ls (lbl: label) (sl: labeled_statements) (k: cont)
962                    on sl: option (statement × cont) :=
963  match sl with
964  [ LSdefault s => find_label lbl s k
965  | LScase _ s sl' =>
966      match find_label lbl s (Kseq (seq_of_labeled_statement sl') k) with
967      [ Some sk => Some ? sk
968      | None => find_label_ls lbl sl' k
969      ]
970  ].
971
972(* * Transition relation *)
973
974(* Strip off outer pointer for use when comparing function types. *)
975definition fun_typeof ≝
976λe. match typeof e with
977[ Tvoid ⇒ Tvoid
978| Tint a b ⇒ Tint a b
979| Tfloat a ⇒ Tfloat a
980| Tpointer _ ty ⇒ ty
981| Tarray a b c ⇒ Tarray a b c
982| Tfunction a b ⇒ Tfunction a b
983| Tstruct a b ⇒ Tstruct a b
984| Tunion a b ⇒ Tunion a b
985| Tcomp_ptr a b ⇒ Tcomp_ptr a b
986].
987
988(* XXX: note that cost labels in exprs expose a particular eval order. *)
989
990inductive step (ge:genv) : state → trace → state → Prop ≝
991
992  | step_assign:   ∀f,a1,a2,k,e,m,loc,ofs,v2,m',tr1,tr2.
993      eval_lvalue ge e m a1 loc ofs tr1 →
994      eval_expr ge e m a2 v2 tr2 →
995      store_value_of_type (typeof a1) m loc ofs v2 = Some ? m' →
996      step ge (State f (Sassign a1 a2) k e m)
997           (tr1⧺tr2) (State f Sskip k e m')
998
999  | step_call_none:   ∀f,a,al,k,e,m,vf,vargs,fd,tr1,tr2.
1000      eval_expr ge e m a vf tr1 →
1001      eval_exprlist ge e m al vargs tr2 →
1002      find_funct ?? ge vf = Some ? fd →
1003      type_of_fundef fd = fun_typeof a →
1004      step ge (State f (Scall (None ?) a al) k e m)
1005           (tr1⧺tr2) (Callstate fd vargs (Kcall (None ?) f e k) m)
1006
1007  | step_call_some:   ∀f,lhs,a,al,k,e,m,loc,ofs,vf,vargs,fd,tr1,tr2,tr3.
1008      eval_lvalue ge e m lhs loc ofs tr1 →
1009      eval_expr ge e m a vf tr2 →
1010      eval_exprlist ge e m al vargs tr3 →
1011      find_funct ?? ge vf = Some ? fd →
1012      type_of_fundef fd = fun_typeof a →
1013      step ge (State f (Scall (Some ? lhs) a al) k e m)
1014           (tr1⧺tr2⧺tr3) (Callstate fd vargs (Kcall (Some ? 〈〈loc, ofs〉, typeof lhs〉) f e k) m)
1015
1016  | step_seq:  ∀f,s1,s2,k,e,m.
1017      step ge (State f (Ssequence s1 s2) k e m)
1018           E0 (State f s1 (Kseq s2 k) e m)
1019  | step_skip_seq: ∀f,s,k,e,m.
1020      step ge (State f Sskip (Kseq s k) e m)
1021           E0 (State f s k e m)
1022  | step_continue_seq: ∀f,s,k,e,m.
1023      step ge (State f Scontinue (Kseq s k) e m)
1024           E0 (State f Scontinue k e m)
1025  | step_break_seq: ∀f,s,k,e,m.
1026      step ge (State f Sbreak (Kseq s k) e m)
1027           E0 (State f Sbreak k e m)
1028
1029  | step_ifthenelse_true:  ∀f,a,s1,s2,k,e,m,v1,tr.
1030      eval_expr ge e m a v1 tr →
1031      is_true v1 (typeof a) →
1032      step ge (State f (Sifthenelse a s1 s2) k e m)
1033           tr (State f s1 k e m)
1034  | step_ifthenelse_false: ∀f,a,s1,s2,k,e,m,v1,tr.
1035      eval_expr ge e m a v1 tr →
1036      is_false v1 (typeof a) →
1037      step ge (State f (Sifthenelse a s1 s2) k e m)
1038           tr (State f s2 k e m)
1039
1040  | step_while_false: ∀f,a,s,k,e,m,v,tr.
1041      eval_expr ge e m a v tr →
1042      is_false v (typeof a) →
1043      step ge (State f (Swhile a s) k e m)
1044           tr (State f Sskip k e m)
1045  | step_while_true: ∀f,a,s,k,e,m,v,tr.
1046      eval_expr ge e m a v tr →
1047      is_true v (typeof a) →
1048      step ge (State f (Swhile a s) k e m)
1049           tr (State f s (Kwhile a s k) e m)
1050  | step_skip_or_continue_while: ∀f,x,a,s,k,e,m.
1051      x = Sskip ∨ x = Scontinue →
1052      step ge (State f x (Kwhile a s k) e m)
1053           E0 (State f (Swhile a s) k e m)
1054  | step_break_while: ∀f,a,s,k,e,m.
1055      step ge (State f Sbreak (Kwhile a s k) e m)
1056           E0 (State f Sskip k e m)
1057
1058  | step_dowhile: ∀f,a,s,k,e,m.
1059      step ge (State f (Sdowhile a s) k e m)
1060        E0 (State f s (Kdowhile a s k) e m)
1061  | step_skip_or_continue_dowhile_false: ∀f,x,a,s,k,e,m,v,tr.
1062      x = Sskip ∨ x = Scontinue →
1063      eval_expr ge e m a v tr →
1064      is_false v (typeof a) →
1065      step ge (State f x (Kdowhile a s k) e m)
1066           tr (State f Sskip k e m)
1067  | step_skip_or_continue_dowhile_true: ∀f,x,a,s,k,e,m,v,tr.
1068      x = Sskip ∨ x = Scontinue →
1069      eval_expr ge e m a v tr →
1070      is_true v (typeof a) →
1071      step ge (State f x (Kdowhile a s k) e m)
1072           tr (State f (Sdowhile a s) k e m)
1073  | step_break_dowhile: ∀f,a,s,k,e,m.
1074      step ge (State f Sbreak (Kdowhile a s k) e m)
1075           E0 (State f Sskip k e m)
1076
1077  | step_for_start: ∀f,a1,a2,a3,s,k,e,m.
1078      a1 ≠ Sskip →
1079      step ge (State f (Sfor a1 a2 a3 s) k e m)
1080           E0 (State f a1 (Kseq (Sfor Sskip a2 a3 s) k) e m)
1081  | step_for_false: ∀f,a2,a3,s,k,e,m,v,tr.
1082      eval_expr ge e m a2 v tr →
1083      is_false v (typeof a2) →
1084      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1085           tr (State f Sskip k e m)
1086  | step_for_true: ∀f,a2,a3,s,k,e,m,v,tr.
1087      eval_expr ge e m a2 v tr →
1088      is_true v (typeof a2) →
1089      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1090           tr (State f s (Kfor2 a2 a3 s k) e m)
1091  | step_skip_or_continue_for2: ∀f,x,a2,a3,s,k,e,m.
1092      x = Sskip ∨ x = Scontinue →
1093      step ge (State f x (Kfor2 a2 a3 s k) e m)
1094           E0 (State f a3 (Kfor3 a2 a3 s k) e m)
1095  | step_break_for2: ∀f,a2,a3,s,k,e,m.
1096      step ge (State f Sbreak (Kfor2 a2 a3 s k) e m)
1097           E0 (State f Sskip k e m)
1098  | step_skip_for3: ∀f,a2,a3,s,k,e,m.
1099      step ge (State f Sskip (Kfor3 a2 a3 s k) e m)
1100           E0 (State f (Sfor Sskip a2 a3 s) k e m)
1101
1102  | step_return_0: ∀f,k,e,m.
1103      fn_return f = Tvoid →
1104      step ge (State f (Sreturn (None ?)) k e m)
1105           E0 (Returnstate Vundef (call_cont k) (free_list m (blocks_of_env e)))
1106  | step_return_1: ∀f,a,k,e,m,v,tr.
1107      fn_return f ≠ Tvoid →
1108      eval_expr ge e m a v tr →
1109      step ge (State f (Sreturn (Some ? a)) k e m)
1110           tr (Returnstate v (call_cont k) (free_list m (blocks_of_env e)))
1111  | step_skip_call: ∀f,k,e,m.
1112      is_call_cont k →
1113      fn_return f = Tvoid →
1114      step ge (State f Sskip k e m)
1115           E0 (Returnstate Vundef k (free_list m (blocks_of_env e)))
1116
1117  | step_switch: ∀f,a,sl,k,e,m,n,tr.
1118      eval_expr ge e m a (Vint n) tr →
1119      step ge (State f (Sswitch a sl) k e m)
1120           tr (State f (seq_of_labeled_statement (select_switch n sl)) (Kswitch k) e m)
1121  | step_skip_break_switch: ∀f,x,k,e,m.
1122      x = Sskip ∨ x = Sbreak →
1123      step ge (State f x (Kswitch k) e m)
1124           E0 (State f Sskip k e m)
1125  | step_continue_switch: ∀f,k,e,m.
1126      step ge (State f Scontinue (Kswitch k) e m)
1127           E0 (State f Scontinue k e m)
1128
1129  | step_label: ∀f,lbl,s,k,e,m.
1130      step ge (State f (Slabel lbl s) k e m)
1131           E0 (State f s k e m)
1132
1133  | step_goto: ∀f,lbl,k,e,m,s',k'.
1134      find_label lbl (fn_body f) (call_cont k) = Some ? 〈s', k'〉 →
1135      step ge (State f (Sgoto lbl) k e m)
1136           E0 (State f s' k' e m)
1137
1138  | step_internal_function: ∀f,vargs,k,m,e,m1,m2.
1139      alloc_variables empty_env m ((fn_params f) @ (fn_vars f)) e m1 →
1140      bind_parameters e m1 (fn_params f) vargs m2 →
1141      step ge (Callstate (Internal f) vargs k m)
1142           E0 (State f (fn_body f) k e m2)
1143
1144  | step_external_function: ∀id,targs,tres,vargs,k,m,vres,t.
1145      event_match (external_function id targs tres) vargs t vres →
1146      step ge (Callstate (External id targs tres) vargs k m)
1147            t (Returnstate vres k m)
1148
1149  | step_returnstate_0: ∀v,f,e,k,m.
1150      step ge (Returnstate v (Kcall (None ?) f e k) m)
1151           E0 (State f Sskip k e m)
1152
1153  | step_returnstate_1: ∀v,f,e,k,m,m',loc,ofs,ty.
1154      store_value_of_type ty m loc ofs v = Some ? m' →
1155      step ge (Returnstate v (Kcall (Some ? 〈〈loc, ofs〉, ty〉) f e k) m)
1156           E0 (State f Sskip k e m')
1157           
1158  | step_cost: ∀f,lbl,s,k,e,m.
1159      step ge (State f (Scost lbl s) k e m)
1160           (Echarge lbl) (State f s k e m).
1161(*
1162(** * Alternate big-step semantics *)
1163
1164(** ** Big-step semantics for terminating statements and functions *)
1165
1166(** The execution of a statement produces an ``outcome'', indicating
1167  how the execution terminated: either normally or prematurely
1168  through the execution of a [break], [continue] or [return] statement. *)
1169
1170inductive outcome: Type[0] :=
1171   | Out_break: outcome                 (**r terminated by [break] *)
1172   | Out_continue: outcome              (**r terminated by [continue] *)
1173   | Out_normal: outcome                (**r terminated normally *)
1174   | Out_return: option val -> outcome. (**r terminated by [return] *)
1175
1176inductive out_normal_or_continue : outcome -> Prop :=
1177  | Out_normal_or_continue_N: out_normal_or_continue Out_normal
1178  | Out_normal_or_continue_C: out_normal_or_continue Out_continue.
1179
1180inductive out_break_or_return : outcome -> outcome -> Prop :=
1181  | Out_break_or_return_B: out_break_or_return Out_break Out_normal
1182  | Out_break_or_return_R: ∀ov.
1183      out_break_or_return (Out_return ov) (Out_return ov).
1184
1185Definition outcome_switch (out: outcome) : outcome :=
1186  match out with
1187  | Out_break => Out_normal
1188  | o => o
1189  end.
1190
1191Definition outcome_result_value (out: outcome) (t: type) (v: val) : Prop :=
1192  match out, t with
1193  | Out_normal, Tvoid => v = Vundef
1194  | Out_return None, Tvoid => v = Vundef
1195  | Out_return (Some v'), ty => ty <> Tvoid /\ v'=v
1196  | _, _ => False
1197  end.
1198
1199(** [exec_stmt ge e m1 s t m2 out] describes the execution of
1200  the statement [s].  [out] is the outcome for this execution.
1201  [m1] is the initial memory state, [m2] the final memory state.
1202  [t] is the trace of input/output events performed during this
1203  evaluation. *)
1204
1205inductive exec_stmt: env -> mem -> statement -> trace -> mem -> outcome -> Prop :=
1206  | exec_Sskip:   ∀e,m.
1207      exec_stmt e m Sskip
1208               E0 m Out_normal
1209  | exec_Sassign:   ∀e,m,a1,a2,loc,ofs,v2,m'.
1210      eval_lvalue e m a1 loc ofs ->
1211      eval_expr e m a2 v2 ->
1212      store_value_of_type (typeof a1) m loc ofs v2 = Some m' ->
1213      exec_stmt e m (Sassign a1 a2)
1214               E0 m' Out_normal
1215  | exec_Scall_none:   ∀e,m,a,al,vf,vargs,f,t,m',vres.
1216      eval_expr e m a vf ->
1217      eval_exprlist e m al vargs ->
1218      Genv.find_funct ge vf = Some f ->
1219      type_of_fundef f = typeof a ->
1220      eval_funcall m f vargs t m' vres ->
1221      exec_stmt e m (Scall None a al)
1222                t m' Out_normal
1223  | exec_Scall_some:   ∀e,m,lhs,a,al,loc,ofs,vf,vargs,f,t,m',vres,m''.
1224      eval_lvalue e m lhs loc ofs ->
1225      eval_expr e m a vf ->
1226      eval_exprlist e m al vargs ->
1227      Genv.find_funct ge vf = Some f ->
1228      type_of_fundef f = typeof a ->
1229      eval_funcall m f vargs t m' vres ->
1230      store_value_of_type (typeof lhs) m' loc ofs vres = Some m'' ->
1231      exec_stmt e m (Scall (Some lhs) a al)
1232                t m'' Out_normal
1233  | exec_Sseq_1:   ∀e,m,s1,s2,t1,m1,t2,m2,out.
1234      exec_stmt e m s1 t1 m1 Out_normal ->
1235      exec_stmt e m1 s2 t2 m2 out ->
1236      exec_stmt e m (Ssequence s1 s2)
1237                (t1 ** t2) m2 out
1238  | exec_Sseq_2:   ∀e,m,s1,s2,t1,m1,out.
1239      exec_stmt e m s1 t1 m1 out ->
1240      out <> Out_normal ->
1241      exec_stmt e m (Ssequence s1 s2)
1242                t1 m1 out
1243  | exec_Sifthenelse_true: ∀e,m,a,s1,s2,v1,t,m',out.
1244      eval_expr e m a v1 ->
1245      is_true v1 (typeof a) ->
1246      exec_stmt e m s1 t m' out ->
1247      exec_stmt e m (Sifthenelse a s1 s2)
1248                t m' out
1249  | exec_Sifthenelse_false: ∀e,m,a,s1,s2,v1,t,m',out.
1250      eval_expr e m a v1 ->
1251      is_false v1 (typeof a) ->
1252      exec_stmt e m s2 t m' out ->
1253      exec_stmt e m (Sifthenelse a s1 s2)
1254                t m' out
1255  | exec_Sreturn_none:   ∀e,m.
1256      exec_stmt e m (Sreturn None)
1257               E0 m (Out_return None)
1258  | exec_Sreturn_some: ∀e,m,a,v.
1259      eval_expr e m a v ->
1260      exec_stmt e m (Sreturn (Some a))
1261               E0 m (Out_return (Some v))
1262  | exec_Sbreak:   ∀e,m.
1263      exec_stmt e m Sbreak
1264               E0 m Out_break
1265  | exec_Scontinue:   ∀e,m.
1266      exec_stmt e m Scontinue
1267               E0 m Out_continue
1268  | exec_Swhile_false: ∀e,m,a,s,v.
1269      eval_expr e m a v ->
1270      is_false v (typeof a) ->
1271      exec_stmt e m (Swhile a s)
1272               E0 m Out_normal
1273  | exec_Swhile_stop: ∀e,m,a,v,s,t,m',out',out.
1274      eval_expr e m a v ->
1275      is_true v (typeof a) ->
1276      exec_stmt e m s t m' out' ->
1277      out_break_or_return out' out ->
1278      exec_stmt e m (Swhile a s)
1279                t m' out
1280  | exec_Swhile_loop: ∀e,m,a,s,v,t1,m1,out1,t2,m2,out.
1281      eval_expr e m a v ->
1282      is_true v (typeof a) ->
1283      exec_stmt e m s t1 m1 out1 ->
1284      out_normal_or_continue out1 ->
1285      exec_stmt e m1 (Swhile a s) t2 m2 out ->
1286      exec_stmt e m (Swhile a s)
1287                (t1 ** t2) m2 out
1288  | exec_Sdowhile_false: ∀e,m,s,a,t,m1,out1,v.
1289      exec_stmt e m s t m1 out1 ->
1290      out_normal_or_continue out1 ->
1291      eval_expr e m1 a v ->
1292      is_false v (typeof a) ->
1293      exec_stmt e m (Sdowhile a s)
1294                t m1 Out_normal
1295  | exec_Sdowhile_stop: ∀e,m,s,a,t,m1,out1,out.
1296      exec_stmt e m s t m1 out1 ->
1297      out_break_or_return out1 out ->
1298      exec_stmt e m (Sdowhile a s)
1299                t m1 out
1300  | exec_Sdowhile_loop: ∀e,m,s,a,m1,m2,t1,t2,out,out1,v.
1301      exec_stmt e m s t1 m1 out1 ->
1302      out_normal_or_continue out1 ->
1303      eval_expr e m1 a v ->
1304      is_true v (typeof a) ->
1305      exec_stmt e m1 (Sdowhile a s) t2 m2 out ->
1306      exec_stmt e m (Sdowhile a s)
1307                (t1 ** t2) m2 out
1308  | exec_Sfor_start: ∀e,m,s,a1,a2,a3,out,m1,m2,t1,t2.
1309      a1 <> Sskip ->
1310      exec_stmt e m a1 t1 m1 Out_normal ->
1311      exec_stmt e m1 (Sfor Sskip a2 a3 s) t2 m2 out ->
1312      exec_stmt e m (Sfor a1 a2 a3 s)
1313                (t1 ** t2) m2 out
1314  | exec_Sfor_false: ∀e,m,s,a2,a3,v.
1315      eval_expr e m a2 v ->
1316      is_false v (typeof a2) ->
1317      exec_stmt e m (Sfor Sskip a2 a3 s)
1318               E0 m Out_normal
1319  | exec_Sfor_stop: ∀e,m,s,a2,a3,v,m1,t,out1,out.
1320      eval_expr e m a2 v ->
1321      is_true v (typeof a2) ->
1322      exec_stmt e m s t m1 out1 ->
1323      out_break_or_return out1 out ->
1324      exec_stmt e m (Sfor Sskip a2 a3 s)
1325                t m1 out
1326  | exec_Sfor_loop: ∀e,m,s,a2,a3,v,m1,m2,m3,t1,t2,t3,out1,out.
1327      eval_expr e m a2 v ->
1328      is_true v (typeof a2) ->
1329      exec_stmt e m s t1 m1 out1 ->
1330      out_normal_or_continue out1 ->
1331      exec_stmt e m1 a3 t2 m2 Out_normal ->
1332      exec_stmt e m2 (Sfor Sskip a2 a3 s) t3 m3 out ->
1333      exec_stmt e m (Sfor Sskip a2 a3 s)
1334                (t1 ** t2 ** t3) m3 out
1335  | exec_Sswitch:   ∀e,m,a,t,n,sl,m1,out.
1336      eval_expr e m a (Vint n) ->
1337      exec_stmt e m (seq_of_labeled_statement (select_switch n sl)) t m1 out ->
1338      exec_stmt e m (Sswitch a sl)
1339                t m1 (outcome_switch out)
1340
1341(** [eval_funcall m1 fd args t m2 res] describes the invocation of
1342  function [fd] with arguments [args].  [res] is the value returned
1343  by the call.  *)
1344
1345with eval_funcall: mem -> fundef -> list val -> trace -> mem -> val -> Prop :=
1346  | eval_funcall_internal: ∀m,f,vargs,t,e,m1,m2,m3,out,vres.
1347      alloc_variables empty_env m (f.(fn_params) ++ f.(fn_vars)) e m1 ->
1348      bind_parameters e m1 f.(fn_params) vargs m2 ->
1349      exec_stmt e m2 f.(fn_body) t m3 out ->
1350      outcome_result_value out f.(fn_return) vres ->
1351      eval_funcall m (Internal f) vargs t (Mem.free_list m3 (blocks_of_env e)) vres
1352  | eval_funcall_external: ∀m,id,targs,tres,vargs,t,vres.
1353      event_match (external_function id targs tres) vargs t vres ->
1354      eval_funcall m (External id targs tres) vargs t m vres.
1355
1356Scheme exec_stmt_ind2 := Minimality for exec_stmt Sort Prop
1357  with eval_funcall_ind2 := Minimality for eval_funcall Sort Prop.
1358
1359(** ** Big-step semantics for diverging statements and functions *)
1360
1361(** Coinductive semantics for divergence.
1362  [execinf_stmt ge e m s t] holds if the execution of statement [s]
1363  diverges, i.e. loops infinitely.  [t] is the possibly infinite
1364  trace of observable events performed during the execution. *)
1365
1366Coinductive execinf_stmt: env -> mem -> statement -> traceinf -> Prop :=
1367  | execinf_Scall_none:   ∀e,m,a,al,vf,vargs,f,t.
1368      eval_expr e m a vf ->
1369      eval_exprlist e m al vargs ->
1370      Genv.find_funct ge vf = Some f ->
1371      type_of_fundef f = typeof a ->
1372      evalinf_funcall m f vargs t ->
1373      execinf_stmt e m (Scall None a al) t
1374  | execinf_Scall_some:   ∀e,m,lhs,a,al,loc,ofs,vf,vargs,f,t.
1375      eval_lvalue e m lhs loc ofs ->
1376      eval_expr e m a vf ->
1377      eval_exprlist e m al vargs ->
1378      Genv.find_funct ge vf = Some f ->
1379      type_of_fundef f = typeof a ->
1380      evalinf_funcall m f vargs t ->
1381      execinf_stmt e m (Scall (Some lhs) a al) t
1382  | execinf_Sseq_1:   ∀e,m,s1,s2,t.
1383      execinf_stmt e m s1 t ->
1384      execinf_stmt e m (Ssequence s1 s2) t
1385  | execinf_Sseq_2:   ∀e,m,s1,s2,t1,m1,t2.
1386      exec_stmt e m s1 t1 m1 Out_normal ->
1387      execinf_stmt e m1 s2 t2 ->
1388      execinf_stmt e m (Ssequence s1 s2) (t1 *** t2)
1389  | execinf_Sifthenelse_true: ∀e,m,a,s1,s2,v1,t.
1390      eval_expr e m a v1 ->
1391      is_true v1 (typeof a) ->
1392      execinf_stmt e m s1 t ->
1393      execinf_stmt e m (Sifthenelse a s1 s2) t
1394  | execinf_Sifthenelse_false: ∀e,m,a,s1,s2,v1,t.
1395      eval_expr e m a v1 ->
1396      is_false v1 (typeof a) ->
1397      execinf_stmt e m s2 t ->
1398      execinf_stmt e m (Sifthenelse a s1 s2) t
1399  | execinf_Swhile_body: ∀e,m,a,v,s,t.
1400      eval_expr e m a v ->
1401      is_true v (typeof a) ->
1402      execinf_stmt e m s t ->
1403      execinf_stmt e m (Swhile a s) t
1404  | execinf_Swhile_loop: ∀e,m,a,s,v,t1,m1,out1,t2.
1405      eval_expr e m a v ->
1406      is_true v (typeof a) ->
1407      exec_stmt e m s t1 m1 out1 ->
1408      out_normal_or_continue out1 ->
1409      execinf_stmt e m1 (Swhile a s) t2 ->
1410      execinf_stmt e m (Swhile a s) (t1 *** t2)
1411  | execinf_Sdowhile_body: ∀e,m,s,a,t.
1412      execinf_stmt e m s t ->
1413      execinf_stmt e m (Sdowhile a s) t
1414  | execinf_Sdowhile_loop: ∀e,m,s,a,m1,t1,t2,out1,v.
1415      exec_stmt e m s t1 m1 out1 ->
1416      out_normal_or_continue out1 ->
1417      eval_expr e m1 a v ->
1418      is_true v (typeof a) ->
1419      execinf_stmt e m1 (Sdowhile a s) t2 ->
1420      execinf_stmt e m (Sdowhile a s) (t1 *** t2)
1421  | execinf_Sfor_start_1: ∀e,m,s,a1,a2,a3,t.
1422      execinf_stmt e m a1 t ->
1423      execinf_stmt e m (Sfor a1 a2 a3 s) t
1424  | execinf_Sfor_start_2: ∀e,m,s,a1,a2,a3,m1,t1,t2.
1425      a1 <> Sskip ->
1426      exec_stmt e m a1 t1 m1 Out_normal ->
1427      execinf_stmt e m1 (Sfor Sskip a2 a3 s) t2 ->
1428      execinf_stmt e m (Sfor a1 a2 a3 s) (t1 *** t2)
1429  | execinf_Sfor_body: ∀e,m,s,a2,a3,v,t.
1430      eval_expr e m a2 v ->
1431      is_true v (typeof a2) ->
1432      execinf_stmt e m s t ->
1433      execinf_stmt e m (Sfor Sskip a2 a3 s) t
1434  | execinf_Sfor_next: ∀e,m,s,a2,a3,v,m1,t1,t2,out1.
1435      eval_expr e m a2 v ->
1436      is_true v (typeof a2) ->
1437      exec_stmt e m s t1 m1 out1 ->
1438      out_normal_or_continue out1 ->
1439      execinf_stmt e m1 a3 t2 ->
1440      execinf_stmt e m (Sfor Sskip a2 a3 s) (t1 *** t2)
1441  | execinf_Sfor_loop: ∀e,m,s,a2,a3,v,m1,m2,t1,t2,t3,out1.
1442      eval_expr e m a2 v ->
1443      is_true v (typeof a2) ->
1444      exec_stmt e m s t1 m1 out1 ->
1445      out_normal_or_continue out1 ->
1446      exec_stmt e m1 a3 t2 m2 Out_normal ->
1447      execinf_stmt e m2 (Sfor Sskip a2 a3 s) t3 ->
1448      execinf_stmt e m (Sfor Sskip a2 a3 s) (t1 *** t2 *** t3)
1449  | execinf_Sswitch:   ∀e,m,a,t,n,sl.
1450      eval_expr e m a (Vint n) ->
1451      execinf_stmt e m (seq_of_labeled_statement (select_switch n sl)) t ->
1452      execinf_stmt e m (Sswitch a sl) t
1453
1454(** [evalinf_funcall ge m fd args t] holds if the invocation of function
1455    [fd] on arguments [args] diverges, with observable trace [t]. *)
1456
1457with evalinf_funcall: mem -> fundef -> list val -> traceinf -> Prop :=
1458  | evalinf_funcall_internal: ∀m,f,vargs,t,e,m1,m2.
1459      alloc_variables empty_env m (f.(fn_params) ++ f.(fn_vars)) e m1 ->
1460      bind_parameters e m1 f.(fn_params) vargs m2 ->
1461      execinf_stmt e m2 f.(fn_body) t ->
1462      evalinf_funcall m (Internal f) vargs t.
1463
1464End SEMANTICS.
1465*)
1466(* * * Whole-program semantics *)
1467
1468(* * Execution of whole programs are described as sequences of transitions
1469  from an initial state to a final state.  An initial state is a [Callstate]
1470  corresponding to the invocation of the ``main'' function of the program
1471  without arguments and with an empty continuation. *)
1472
1473inductive initial_state (p: clight_program): state -> Prop :=
1474  | initial_state_intro: ∀b,f,ge,m0.
1475      globalenv Genv ?? p = OK ? ge →
1476      init_mem Genv ?? p = OK ? m0 →
1477      find_symbol ?? ge (prog_main ?? p) = Some ? b →
1478      find_funct_ptr ?? ge b = Some ? f →
1479      initial_state p (Callstate f (nil ?) Kstop m0).
1480
1481(* * A final state is a [Returnstate] with an empty continuation. *)
1482
1483inductive final_state: state -> int -> Prop :=
1484  | final_state_intro: ∀r,m.
1485      final_state (Returnstate (Vint r) Kstop m) r.
1486
1487(* * Execution of a whole program: [exec_program p beh]
1488  holds if the application of [p]'s main function to no arguments
1489  in the initial memory state for [p] has [beh] as observable
1490  behavior. *)
1491
1492definition exec_program : clight_program → program_behavior → Prop ≝ λp,beh.
1493  ∀ge. globalenv ??? p = OK ? ge →
1494  program_behaves (mk_transrel ?? step) (initial_state p) final_state ge beh.
1495(*
1496(** Big-step execution of a whole program.  *)
1497
1498inductive bigstep_program_terminates (p: program): trace -> int -> Prop :=
1499  | bigstep_program_terminates_intro: ∀b,f,m1,t,r.
1500      let ge := Genv.globalenv p in
1501      let m0 := Genv.init_mem p in
1502      Genv.find_symbol ge p.(prog_main) = Some b ->
1503      Genv.find_funct_ptr ge b = Some f ->
1504      eval_funcall ge m0 f nil t m1 (Vint r) ->
1505      bigstep_program_terminates p t r.
1506
1507inductive bigstep_program_diverges (p: program): traceinf -> Prop :=
1508  | bigstep_program_diverges_intro: ∀b,f,t.
1509      let ge := Genv.globalenv p in
1510      let m0 := Genv.init_mem p in
1511      Genv.find_symbol ge p.(prog_main) = Some b ->
1512      Genv.find_funct_ptr ge b = Some f ->
1513      evalinf_funcall ge m0 f nil t ->
1514      bigstep_program_diverges p t.
1515
1516(** * Implication from big-step semantics to transition semantics *)
1517
1518Section BIGSTEP_TO_TRANSITIONS.
1519
1520Variable prog: program.
1521Let ge : genv := Genv.globalenv prog.
1522
1523Definition exec_stmt_eval_funcall_ind
1524  (PS: env -> mem -> statement -> trace -> mem -> outcome -> Prop)
1525  (PF: mem -> fundef -> list val -> trace -> mem -> val -> Prop) :=
1526  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 =>
1527  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)
1528       (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).
1529
1530inductive outcome_state_match
1531       (e: env) (m: mem) (f: function) (k: cont): outcome -> state -> Prop :=
1532  | osm_normal:
1533      outcome_state_match e m f k Out_normal (State f Sskip k e m)
1534  | osm_break:
1535      outcome_state_match e m f k Out_break (State f Sbreak k e m)
1536  | osm_continue:
1537      outcome_state_match e m f k Out_continue (State f Scontinue k e m)
1538  | osm_return_none: ∀k'.
1539      call_cont k' = call_cont k ->
1540      outcome_state_match e m f k
1541        (Out_return None) (State f (Sreturn None) k' e m)
1542  | osm_return_some: ∀a,v,k'.
1543      call_cont k' = call_cont k ->
1544      eval_expr ge e m a v ->
1545      outcome_state_match e m f k
1546        (Out_return (Some v)) (State f (Sreturn (Some a)) k' e m).
1547
1548Lemma is_call_cont_call_cont:
1549  ∀k. is_call_cont k -> call_cont k = k.
1550Proof.
1551  destruct k; simpl; intros; contradiction || auto.
1552Qed.
1553
1554Lemma exec_stmt_eval_funcall_steps:
1555  (∀e,m,s,t,m',out.
1556   exec_stmt ge e m s t m' out ->
1557   ∀f,k. exists S,
1558   star step ge (State f s k e m) t S
1559   /\ outcome_state_match e m' f k out S)
1560/\
1561  (∀m,fd,args,t,m',res.
1562   eval_funcall ge m fd args t m' res ->
1563   ∀k.
1564   is_call_cont k ->
1565   star step ge (Callstate fd args k m) t (Returnstate res k m')).
1566Proof.
1567  apply exec_stmt_eval_funcall_ind; intros.
1568
1569(* skip *)
1570  econstructor; split. apply star_refl. constructor.
1571
1572(* assign *)
1573  econstructor; split. apply star_one. econstructor; eauto. constructor.
1574
1575(* call none *)
1576  econstructor; split.
1577  eapply star_left. econstructor; eauto.
1578  eapply star_right. apply H4. simpl; auto. econstructor. reflexivity. traceEq.
1579  constructor.
1580
1581(* call some *)
1582  econstructor; split.
1583  eapply star_left. econstructor; eauto.
1584  eapply star_right. apply H5. simpl; auto. econstructor; eauto. reflexivity. traceEq.
1585  constructor.
1586
1587(* sequence 2 *)
1588  destruct (H0 f (Kseq s2 k)) as [S1 [A1 B1]]. inv B1.
1589  destruct (H2 f k) as [S2 [A2 B2]].
1590  econstructor; split.
1591  eapply star_left. econstructor.
1592  eapply star_trans. eexact A1.
1593  eapply star_left. constructor. eexact A2.
1594  reflexivity. reflexivity. traceEq.
1595  auto.
1596
1597(* sequence 1 *)
1598  destruct (H0 f (Kseq s2 k)) as [S1 [A1 B1]].
1599  set (S2 :=
1600    match out with
1601    | Out_break => State f Sbreak k e m1
1602    | Out_continue => State f Scontinue k e m1
1603    | _ => S1
1604    end).
1605  exists S2; split.
1606  eapply star_left. econstructor.
1607  eapply star_trans. eexact A1.
1608  unfold S2; inv B1.
1609    congruence.
1610    apply star_one. apply step_break_seq.
1611    apply star_one. apply step_continue_seq.
1612    apply star_refl.
1613    apply star_refl.
1614  reflexivity. traceEq.
1615  unfold S2; inv B1; congruence || econstructor; eauto.
1616
1617(* ifthenelse true *)
1618  destruct (H2 f k) as [S1 [A1 B1]].
1619  exists S1; split.
1620  eapply star_left. eapply step_ifthenelse_true; eauto. eexact A1. traceEq.
1621  auto.
1622
1623(* ifthenelse false *)
1624  destruct (H2 f k) as [S1 [A1 B1]].
1625  exists S1; split.
1626  eapply star_left. eapply step_ifthenelse_false; eauto. eexact A1. traceEq.
1627  auto.
1628
1629(* return none *)
1630  econstructor; split. apply star_refl. constructor. auto.
1631
1632(* return some *)
1633  econstructor; split. apply star_refl. econstructor; eauto.
1634
1635(* break *)
1636  econstructor; split. apply star_refl. constructor.
1637
1638(* continue *)
1639  econstructor; split. apply star_refl. constructor.
1640
1641(* while false *)
1642  econstructor; split.
1643  apply star_one. eapply step_while_false; eauto.
1644  constructor.
1645
1646(* while stop *)
1647  destruct (H2 f (Kwhile a s k)) as [S1 [A1 B1]].
1648  set (S2 :=
1649    match out' with
1650    | Out_break => State f Sskip k e m'
1651    | _ => S1
1652    end).
1653  exists S2; split.
1654  eapply star_left. eapply step_while_true; eauto.
1655  eapply star_trans. eexact A1.
1656  unfold S2. inversion H3; subst.
1657  inv B1. apply star_one. constructor.   
1658  apply star_refl.
1659  reflexivity. traceEq.
1660  unfold S2. inversion H3; subst. constructor. inv B1; econstructor; eauto.
1661
1662(* while loop *)
1663  destruct (H2 f (Kwhile a s k)) as [S1 [A1 B1]].
1664  destruct (H5 f k) as [S2 [A2 B2]].
1665  exists S2; split.
1666  eapply star_left. eapply step_while_true; eauto.
1667  eapply star_trans. eexact A1.
1668  eapply star_left.
1669  inv H3; inv B1; apply step_skip_or_continue_while; auto.
1670  eexact A2.
1671  reflexivity. reflexivity. traceEq.
1672  auto.
1673
1674(* dowhile false *)
1675  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1676  exists (State f Sskip k e m1); split.
1677  eapply star_left. constructor.
1678  eapply star_right. eexact A1.
1679  inv H1; inv B1; eapply step_skip_or_continue_dowhile_false; eauto.
1680  reflexivity. traceEq.
1681  constructor.
1682
1683(* dowhile stop *)
1684  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1685  set (S2 :=
1686    match out1 with
1687    | Out_break => State f Sskip k e m1
1688    | _ => S1
1689    end).
1690  exists S2; split.
1691  eapply star_left. apply step_dowhile.
1692  eapply star_trans. eexact A1.
1693  unfold S2. inversion H1; subst.
1694  inv B1. apply star_one. constructor.
1695  apply star_refl.
1696  reflexivity. traceEq.
1697  unfold S2. inversion H1; subst. constructor. inv B1; econstructor; eauto.
1698
1699(* dowhile loop *)
1700  destruct (H0 f (Kdowhile a s k)) as [S1 [A1 B1]].
1701  destruct (H5 f k) as [S2 [A2 B2]].
1702  exists S2; split.
1703  eapply star_left. apply step_dowhile.
1704  eapply star_trans. eexact A1.
1705  eapply star_left.
1706  inv H1; inv B1; eapply step_skip_or_continue_dowhile_true; eauto.
1707  eexact A2.
1708  reflexivity. reflexivity. traceEq.
1709  auto.
1710
1711(* for start *)
1712  destruct (H1 f (Kseq (Sfor Sskip a2 a3 s) k)) as [S1 [A1 B1]]. inv B1.
1713  destruct (H3 f k) as [S2 [A2 B2]].
1714  exists S2; split.
1715  eapply star_left. apply step_for_start; auto.   
1716  eapply star_trans. eexact A1.
1717  eapply star_left. constructor. eexact A2.
1718  reflexivity. reflexivity. traceEq.
1719  auto.
1720
1721(* for false *)
1722  econstructor; split.
1723  eapply star_one. eapply step_for_false; eauto.
1724  constructor.
1725
1726(* for stop *)
1727  destruct (H2 f (Kfor2 a2 a3 s k)) as [S1 [A1 B1]].
1728  set (S2 :=
1729    match out1 with
1730    | Out_break => State f Sskip k e m1
1731    | _ => S1
1732    end).
1733  exists S2; split.
1734  eapply star_left. eapply step_for_true; eauto.
1735  eapply star_trans. eexact A1.
1736  unfold S2. inversion H3; subst.
1737  inv B1. apply star_one. constructor.
1738  apply star_refl.
1739  reflexivity. traceEq.
1740  unfold S2. inversion H3; subst. constructor. inv B1; econstructor; eauto.
1741
1742(* for loop *)
1743  destruct (H2 f (Kfor2 a2 a3 s k)) as [S1 [A1 B1]].
1744  destruct (H5 f (Kfor3 a2 a3 s k)) as [S2 [A2 B2]]. inv B2.
1745  destruct (H7 f k) as [S3 [A3 B3]].
1746  exists S3; split.
1747  eapply star_left. eapply step_for_true; eauto.
1748  eapply star_trans. eexact A1.
1749  eapply star_trans with (s2 := State f a3 (Kfor3 a2 a3 s k) e m1).
1750  inv H3; inv B1.
1751  apply star_one. constructor. auto.
1752  apply star_one. constructor. auto.
1753  eapply star_trans. eexact A2.
1754  eapply star_left. constructor.
1755  eexact A3.
1756  reflexivity. reflexivity. reflexivity. reflexivity. traceEq.
1757  auto.
1758
1759(* switch *)
1760  destruct (H1 f (Kswitch k)) as [S1 [A1 B1]].
1761  set (S2 :=
1762    match out with
1763    | Out_normal => State f Sskip k e m1
1764    | Out_break => State f Sskip k e m1
1765    | Out_continue => State f Scontinue k e m1
1766    | _ => S1
1767    end).
1768  exists S2; split.
1769  eapply star_left. eapply step_switch; eauto.
1770  eapply star_trans. eexact A1.
1771  unfold S2; inv B1.
1772    apply star_one. constructor. auto.
1773    apply star_one. constructor. auto.
1774    apply star_one. constructor.
1775    apply star_refl.
1776    apply star_refl.
1777  reflexivity. traceEq.
1778  unfold S2. inv B1; simpl; econstructor; eauto.
1779
1780(* call internal *)
1781  destruct (H2 f k) as [S1 [A1 B1]].
1782  eapply star_left. eapply step_internal_function; eauto.
1783  eapply star_right. eexact A1.
1784  inv B1; simpl in H3; try contradiction.
1785  (* Out_normal *)
1786  assert (fn_return f = Tvoid /\ vres = Vundef).
1787    destruct (fn_return f); auto || contradiction.
1788  destruct H5. subst vres. apply step_skip_call; auto.
1789  (* Out_return None *)
1790  assert (fn_return f = Tvoid /\ vres = Vundef).
1791    destruct (fn_return f); auto || contradiction.
1792  destruct H6. subst vres.
1793  rewrite <- (is_call_cont_call_cont k H4). rewrite <- H5.
1794  apply step_return_0; auto.
1795  (* Out_return Some *)
1796  destruct H3. subst vres.
1797  rewrite <- (is_call_cont_call_cont k H4). rewrite <- H5.
1798  eapply step_return_1; eauto.
1799  reflexivity. traceEq.
1800
1801(* call external *)
1802  apply star_one. apply step_external_function; auto.
1803Qed.
1804
1805Lemma exec_stmt_steps:
1806   ∀e,m,s,t,m',out.
1807   exec_stmt ge e m s t m' out ->
1808   ∀f,k. exists S,
1809   star step ge (State f s k e m) t S
1810   /\ outcome_state_match e m' f k out S.
1811Proof (proj1 exec_stmt_eval_funcall_steps).
1812
1813Lemma eval_funcall_steps:
1814   ∀m,fd,args,t,m',res.
1815   eval_funcall ge m fd args t m' res ->
1816   ∀k.
1817   is_call_cont k ->
1818   star step ge (Callstate fd args k m) t (Returnstate res k m').
1819Proof (proj2 exec_stmt_eval_funcall_steps).
1820
1821Definition order (x y: unit) := False.
1822
1823Lemma evalinf_funcall_forever:
1824  ∀m,fd,args,T,k.
1825  evalinf_funcall ge m fd args T ->
1826  forever_N step order ge tt (Callstate fd args k m) T.
1827Proof.
1828  cofix CIH_FUN.
1829  assert (∀e,m,s,T,f,k.
1830          execinf_stmt ge e m s T ->
1831          forever_N step order ge tt (State f s k e m) T).
1832  cofix CIH_STMT.
1833  intros. inv H.
1834
1835(* call none *)
1836  eapply forever_N_plus.
1837  apply plus_one. eapply step_call_none; eauto.
1838  apply CIH_FUN. eauto. traceEq.
1839(* call some *)
1840  eapply forever_N_plus.
1841  apply plus_one. eapply step_call_some; eauto.
1842  apply CIH_FUN. eauto. traceEq.
1843
1844(* seq 1 *)
1845  eapply forever_N_plus.
1846  apply plus_one. econstructor.
1847  apply CIH_STMT; eauto. traceEq.
1848(* seq 2 *)
1849  destruct (exec_stmt_steps _ _ _ _ _ _ H0 f (Kseq s2 k)) as [S1 [A1 B1]].
1850  inv B1.
1851  eapply forever_N_plus.
1852  eapply plus_left. constructor. eapply star_trans. eexact A1.
1853  apply star_one. constructor. reflexivity. reflexivity.
1854  apply CIH_STMT; eauto. traceEq.
1855
1856(* ifthenelse true *)
1857  eapply forever_N_plus.
1858  apply plus_one. eapply step_ifthenelse_true; eauto.
1859  apply CIH_STMT; eauto. traceEq.
1860(* ifthenelse false *)
1861  eapply forever_N_plus.
1862  apply plus_one. eapply step_ifthenelse_false; eauto.
1863  apply CIH_STMT; eauto. traceEq.
1864
1865(* while body *)
1866  eapply forever_N_plus.
1867  eapply plus_one. eapply step_while_true; eauto.
1868  apply CIH_STMT; eauto. traceEq.
1869(* while loop *)
1870  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kwhile a s0 k)) as [S1 [A1 B1]].
1871  eapply forever_N_plus with (s2 := State f (Swhile a s0) k e m1).
1872  eapply plus_left. eapply step_while_true; eauto.
1873  eapply star_right. eexact A1.
1874  inv H3; inv B1; apply step_skip_or_continue_while; auto.
1875  reflexivity. reflexivity.
1876  apply CIH_STMT; eauto. traceEq.
1877
1878(* dowhile body *)
1879  eapply forever_N_plus.
1880  eapply plus_one. eapply step_dowhile.
1881  apply CIH_STMT; eauto.
1882  traceEq.
1883
1884(* dowhile loop *)
1885  destruct (exec_stmt_steps _ _ _ _ _ _ H0 f (Kdowhile a s0 k)) as [S1 [A1 B1]].
1886  eapply forever_N_plus with (s2 := State f (Sdowhile a s0) k e m1).
1887  eapply plus_left. eapply step_dowhile.
1888  eapply star_right. eexact A1.
1889  inv H1; inv B1; eapply step_skip_or_continue_dowhile_true; eauto.
1890  reflexivity. reflexivity.
1891  apply CIH_STMT. eauto.
1892  traceEq.
1893
1894(* for start 1 *)
1895  assert (a1 <> Sskip). red; intros; subst. inv H0.
1896  eapply forever_N_plus.
1897  eapply plus_one. apply step_for_start; auto.
1898  apply CIH_STMT; eauto.
1899  traceEq.
1900
1901(* for start 2 *)
1902  destruct (exec_stmt_steps _ _ _ _ _ _ H1 f (Kseq (Sfor Sskip a2 a3 s0) k)) as [S1 [A1 B1]].
1903  inv B1.
1904  eapply forever_N_plus.
1905  eapply plus_left. eapply step_for_start; eauto.
1906  eapply star_right. eexact A1.
1907  apply step_skip_seq.
1908  reflexivity. reflexivity.
1909  apply CIH_STMT; eauto.
1910  traceEq.
1911
1912(* for body *)
1913  eapply forever_N_plus.
1914  apply plus_one. eapply step_for_true; eauto.
1915  apply CIH_STMT; eauto.
1916  traceEq.
1917
1918(* for next *)
1919  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kfor2 a2 a3 s0 k)) as [S1 [A1 B1]].
1920  eapply forever_N_plus.
1921  eapply plus_left. eapply step_for_true; eauto.
1922  eapply star_trans. eexact A1.
1923  apply star_one.
1924  inv H3; inv B1; apply step_skip_or_continue_for2; auto.
1925  reflexivity. reflexivity.
1926  apply CIH_STMT; eauto.
1927  traceEq.
1928
1929(* for body *)
1930  destruct (exec_stmt_steps _ _ _ _ _ _ H2 f (Kfor2 a2 a3 s0 k)) as [S1 [A1 B1]].
1931  destruct (exec_stmt_steps _ _ _ _ _ _ H4 f (Kfor3 a2 a3 s0 k)) as [S2 [A2 B2]].
1932  inv B2.
1933  eapply forever_N_plus.
1934  eapply plus_left. eapply step_for_true; eauto.
1935  eapply star_trans. eexact A1.
1936  eapply star_left. inv H3; inv B1; apply step_skip_or_continue_for2; auto.
1937  eapply star_right. eexact A2.
1938  constructor.
1939  reflexivity. reflexivity. reflexivity. reflexivity. 
1940  apply CIH_STMT; eauto.
1941  traceEq.
1942
1943(* switch *)
1944  eapply forever_N_plus.
1945  eapply plus_one. eapply step_switch; eauto.
1946  apply CIH_STMT; eauto.
1947  traceEq.
1948
1949(* call internal *)
1950  intros. inv H0.
1951  eapply forever_N_plus.
1952  eapply plus_one. econstructor; eauto.
1953  apply H; eauto.
1954  traceEq.
1955Qed.
1956
1957Theorem bigstep_program_terminates_exec:
1958  ∀t,r. bigstep_program_terminates prog t r -> exec_program prog (Terminates t r).
1959Proof.
1960  intros. inv H. unfold ge0, m0 in *.
1961  econstructor.
1962  econstructor. eauto. eauto.
1963  apply eval_funcall_steps. eauto. red; auto.
1964  econstructor.
1965Qed.
1966
1967Theorem bigstep_program_diverges_exec:
1968  ∀T. bigstep_program_diverges prog T ->
1969  exec_program prog (Reacts T) \/
1970  exists t, exec_program prog (Diverges t) /\ traceinf_prefix t T.
1971Proof.
1972  intros. inv H.
1973  set (st := Callstate f nil Kstop m0).
1974  assert (forever step ge0 st T).
1975    eapply forever_N_forever with (order := order).
1976    red; intros. constructor; intros. red in H. elim H.
1977    eapply evalinf_funcall_forever; eauto.
1978  destruct (forever_silent_or_reactive _ _ _ _ _ _ H)
1979  as [A | [t [s' [T' [B [C D]]]]]].
1980  left. econstructor. econstructor. eauto. eauto. auto.
1981  right. exists t. split.
1982  econstructor. econstructor; eauto. eauto. auto.
1983  subst T. rewrite <- (E0_right t) at 1. apply traceinf_prefix_app. constructor.
1984Qed.
1985
1986End BIGSTEP_TO_TRANSITIONS.
1987
1988
1989
1990*)
1991
1992 
Note: See TracBrowser for help on using the repository browser.