source: C-semantics/Csem.ma @ 127

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

Allow the storage of pointers in suitably large integers.

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