source: src/Clight/Csem.ma @ 2433

Last change on this file since 2433 was 2433, checked in by campbell, 7 years ago

Tidy up Clight pointer comparison.

File size: 54.4 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 "common/Globalenvs.ma".
26include "Clight/Csyntax.ma".
27(*include "Events.ma".*)
28include "common/Smallstep.ma".
29include "Clight/ClassifyOp.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 sz (zero ?)) (Tint sz sg)
41  | is_false_pointer: ∀t.
42      is_false Vnull (Tpointer 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: ∀sz,sg,n.
48      n ≠ (zero ?) →
49      is_true (Vint sz n) (Tint sz sg)
50  | is_true_pointer_pointer: ∀ptr,t.
51      is_true (Vptr ptr) (Tpointer 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 sz _ ⇒
75      match v with
76      [ Vint sz' n ⇒ if eq_intsize sz sz'
77                     then Some ? (Vint ? (two_complement_negation ? n))
78                     else None ?
79      | _ ⇒ None ?
80      ]
81  | Tfloat _ ⇒
82      match v with
83      [ Vfloat f ⇒ Some ? (Vfloat (Fneg f))
84      | _ ⇒ None ?
85      ]
86  | _ ⇒ None ?
87  ].
88
89let rec sem_notint (v: val) : option val ≝
90  match v with
91  [ Vint sz n ⇒ Some ? (Vint ? (exclusive_disjunction_bv ? n (mone ?))) (* XXX *)
92  | _ ⇒ None ?
93  ].
94
95let rec sem_notbool (v: val) (ty: type) : option val ≝
96  match ty with
97  [ Tint sz _ ⇒
98      match v with
99      [ Vint sz' n ⇒ if eq_intsize sz sz'
100                     then Some ? (of_bool (eq_bv ? n (zero ?)))
101                     else None ?
102      | _ ⇒ None ?
103      ]
104  | Tpointer _ ⇒
105      match v with
106      [ Vptr _ ⇒ Some ? Vfalse
107      | Vnull ⇒ Some ? Vtrue
108      | _ ⇒ None ?
109      ]
110  | Tfloat _ ⇒
111      match v with
112      [ Vfloat f ⇒ Some ? (of_bool (Fcmp Ceq f Fzero))
113      | _ ⇒ None ?
114      ]
115  | _ ⇒ None ?
116  ].
117
118let rec sem_add (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
119  match classify_add t1 t2 with
120  [ add_case_ii _ _ ⇒                       (**r integer addition *)
121      match v1 with
122      [ Vint sz1 n1 ⇒ match v2 with
123        [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
124                        (λn1. Some ? (Vint ? (addition_n ? n1 n2))) (None ?)
125        | _ ⇒ None ? ]
126      | _ ⇒ None ? ]
127  | add_case_ff _ ⇒                       (**r float addition *)
128      match v1 with
129      [ Vfloat n1 ⇒ match v2 with
130        [ Vfloat n2 ⇒ Some ? (Vfloat (Fadd n1 n2))
131        | _ ⇒ None ? ]
132      | _ ⇒ None ? ]
133  | add_case_pi _ ty _ _ ⇒                    (**r pointer plus integer *)
134      match v1 with
135      [ Vptr ptr1 ⇒ match v2 with
136        [ Vint sz2 n2 ⇒ Some ? (Vptr (shift_pointer_n ? ptr1 (sizeof ty) n2))
137        | _ ⇒ None ? ]
138      | Vnull ⇒ match v2 with
139        [ Vint sz2 n2 ⇒ if eq_bv ? n2 (zero ?) then Some ? Vnull else None ?
140        | _ ⇒ None ? ]
141      | _ ⇒ None ? ]
142  | add_case_ip _ _ _ ty ⇒                    (**r integer plus pointer *)
143      match v1 with
144      [ Vint sz1 n1 ⇒ match v2 with
145        [ Vptr ptr2 ⇒ Some ? (Vptr (shift_pointer_n ? ptr2 (sizeof ty) n1))
146        | Vnull ⇒ if eq_bv ? n1 (zero ?) then Some ? Vnull else None ?
147        | _ ⇒ None ? ]
148      | _ ⇒ None ? ]
149  | add_default _ _ ⇒ None ?
150].
151
152let rec sem_sub (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
153  match classify_sub t1 t2 with
154  [ sub_case_ii _ _ ⇒                (**r integer subtraction *)
155      match v1 with
156      [ Vint sz1 n1 ⇒ match v2 with
157        [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
158                        (λn1.Some ? (Vint sz2 (subtraction ? n1 n2))) (None ?)
159        | _ ⇒ None ? ]
160      | _ ⇒ None ? ]
161  | sub_case_ff _ ⇒                (**r float subtraction *)
162      match v1 with
163      [ Vfloat f1 ⇒ match v2 with
164        [ Vfloat f2 ⇒ Some ? (Vfloat (Fsub f1 f2))
165        | _ ⇒ None ? ]
166      | _ ⇒ None ? ]
167  | sub_case_pi _ ty _ _ ⇒             (**r pointer minus integer *)
168      match v1 with
169      [ Vptr ptr1 ⇒ match v2 with
170        [ Vint sz2 n2 ⇒ Some ? (Vptr (neg_shift_pointer_n ? ptr1 (sizeof ty) n2))
171        | _ ⇒ None ? ]
172      | Vnull ⇒ match v2 with
173        [ Vint sz2 n2 ⇒ if eq_bv ? n2 (zero ?) then Some ? Vnull else None ?
174        | _ ⇒ None ? ]
175      | _ ⇒ None ? ]
176  | sub_case_pp _ _ ty _ ⇒             (**r pointer minus pointer *)
177      match v1 with
178      [ Vptr ptr1 ⇒ match v2 with
179        [ Vptr ptr2 ⇒
180          if eq_block (pblock ptr1) (pblock ptr2) then
181            if eqb (sizeof ty) 0 then None ?
182            else match division_u ? (sub_offset ? (poff ptr1) (poff ptr2)) (repr (sizeof ty)) with
183                 [ None ⇒ None ?
184                 | Some v ⇒ Some ? (Vint I32 v) (* XXX choose size from result type? *)
185                 ]
186          else None ?
187        | _ ⇒ None ? ]
188      | Vnull ⇒ match v2 with [ Vnull ⇒ Some ? (Vint I32 (*XXX*) (zero ?)) | _ ⇒ None ? ]
189      | _ ⇒ None ? ]
190  | sub_default _ _ ⇒ None ?
191  ].
192
193let rec sem_mul (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
194 match classify_aop t1 t2 with
195  [ aop_case_ii _ _ ⇒
196      match v1 with
197      [ Vint sz1 n1 ⇒ match v2 with
198          [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
199                          (λn1. Some ? (Vint sz2 (short_multiplication ? n1 n2))) (None ?)
200        | _ ⇒ None ? ]
201      | _ ⇒ None ? ]
202  | aop_case_ff _ ⇒
203      match v1 with
204      [ Vfloat f1 ⇒ match v2 with
205        [ Vfloat f2 ⇒ Some ? (Vfloat (Fmul f1 f2))
206        | _ ⇒ None ? ]
207      | _ ⇒ None ? ]
208  | aop_default _ _ ⇒
209      None ?
210].
211
212let rec sem_div (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
213  match classify_aop t1 t2 with
214  [ aop_case_ii _ sg ⇒
215      match v1 with
216       [ Vint sz1 n1 ⇒ match v2 with
217         [ Vint sz2 n2 ⇒
218           match sg with
219           [ Signed ⇒  intsize_eq_elim ? sz1 sz2 ? n1
220                         (λn1. option_map … (Vint ?) (division_s ? n1 n2)) (None ?)
221           | Unsigned ⇒  intsize_eq_elim ? sz1 sz2 ? n1
222                         (λn1. option_map … (Vint ?) (division_u ? n1 n2)) (None ?)
223           ]
224         | _ ⇒ None ? ]
225      | _ ⇒ None ? ]
226  | aop_case_ff _ ⇒
227      match v1 with
228      [ Vfloat f1 ⇒ match v2 with
229        [ Vfloat f2 ⇒ Some ? (Vfloat(Fdiv f1 f2))
230        | _ ⇒ None ? ]
231      | _ ⇒ None ? ]
232  | aop_default _ _ ⇒
233      None ?
234  ].
235
236let rec sem_mod (v1:val) (t1:type) (v2: val) (t2:type) : option val ≝
237  match classify_aop t1 t2 with
238  [ aop_case_ii sz sg ⇒
239      match v1 with
240      [ Vint sz1 n1 ⇒ match v2 with
241        [ Vint sz2 n2 ⇒
242          match sg with
243          [ Unsigned ⇒ intsize_eq_elim ? sz1 sz2 ? n1
244                        (λn1. option_map … (Vint ?) (modulus_u ? n1 n2)) (None ?)
245          | Signed ⇒ intsize_eq_elim ? sz1 sz2 ? n1
246                      (λn1. option_map … (Vint ?) (modulus_s ? n1 n2)) (None ?)
247          ]
248        | _ ⇒ None ? ]
249      | _ ⇒ None ? ]
250  | _ ⇒
251      None ?
252  ].
253
254let rec sem_and (v1,v2: val) : option val ≝
255  match v1 with
256  [ Vint sz1 n1 ⇒ match v2 with
257    [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
258                    (λn1. Some ? (Vint ? (conjunction_bv ? n1 n2))) (None ?)
259    | _ ⇒ None ? ]
260  | _ ⇒ None ?
261  ].
262
263let rec sem_or (v1,v2: val) : option val ≝
264  match v1 with
265  [ Vint sz1 n1 ⇒ match v2 with
266    [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
267                    (λn1. Some ? (Vint ? (inclusive_disjunction_bv ? n1 n2))) (None ?)
268    | _ ⇒ None ? ]
269  | _ ⇒ None ?
270  ].
271
272let rec sem_xor (v1,v2: val) : option val ≝
273  match v1 with
274  [ Vint sz1 n1 ⇒ match v2 with
275    [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1
276                    (λn1. Some ? (Vint ? (exclusive_disjunction_bv ? n1 n2))) (None ?)
277    | _ ⇒ None ? ]
278  | _ ⇒ None ?
279  ].
280
281let rec sem_shl (v1,v2: val): option val ≝
282  match v1 with
283  [ Vint sz1 n1 ⇒ match v2 with
284    [ Vint sz2 n2 ⇒
285        if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1))
286        then Some ? (Vint sz1 (shift_left ?? (nat_of_bitvector … n2) n1 false))
287        else None ?
288    | _ ⇒ None ? ]
289  | _ ⇒ None ? ].
290
291let rec sem_shr (v1: val) (t1: type) (v2: val) (t2: type): option val ≝
292  match classify_aop t1 t2 with
293  [ aop_case_ii _ sg ⇒
294      match v1 with
295      [ Vint sz1 n1 ⇒ match v2 with
296        [ Vint sz2 n2 ⇒
297          match sg with
298          [ Unsigned ⇒
299            if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1))
300            then Some ? (Vint ? (shift_right ?? (nat_of_bitvector … n2) n1 false))
301            else None ?
302          | Signed ⇒
303            if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1))
304            then Some ? (Vint ? (shift_right ?? (nat_of_bitvector … n2) n1 (head' … n1)))
305            else None ?
306          ]
307        | _ ⇒ None ? ]
308      | _ ⇒ None ? ]
309   | _ ⇒
310      None ?
311   ].
312
313let rec sem_cmp_mismatch (c: comparison): option val ≝
314  match c with
315  [ Ceq ⇒  Some ? Vfalse
316  | Cne ⇒  Some ? Vtrue
317  | _   ⇒ None ?
318  ].
319
320let rec sem_cmp_match (c: comparison): option val ≝
321  match c with
322  [ Ceq ⇒  Some ? Vtrue
323  | Cne ⇒  Some ? Vfalse
324  | _   ⇒ None ?
325  ].
326 
327let rec sem_cmp (c:comparison)
328                  (v1: val) (t1: type) (v2: val) (t2: type)
329                  (m: mem) on m: option val ≝
330  match classify_cmp t1 t2 with
331  [ cmp_case_ii _ sg ⇒
332      match v1 with
333      [ Vint sz1 n1 ⇒ match v2 with
334         [ Vint sz2 n2 ⇒
335           match sg with
336           [ Unsigned ⇒ intsize_eq_elim ? sz1 sz2 ? n1
337                        (λn1. Some ? (of_bool (cmpu_int ? c n1 n2))) (None ?)
338           | Signed ⇒ intsize_eq_elim ? sz1 sz2 ? n1
339                       (λn1. Some ? (of_bool (cmp_int ? c n1 n2))) (None ?)
340           ]
341         | _ ⇒ None ?
342         ]
343      | _ ⇒ None ?     
344      ]
345  | cmp_case_pp _ _ ⇒
346      match v1 with
347      [ Vptr ptr1 ⇒
348        match v2 with
349        [ Vptr ptr2 ⇒
350          if (valid_pointer m ptr1 ∧ valid_pointer m ptr2)
351          then
352            if eq_block (pblock ptr1) (pblock ptr2)
353            then Some ? (of_bool (cmp_offset c (poff ptr1) (poff ptr2)))
354            else sem_cmp_mismatch c
355          else None ?
356        | Vnull ⇒ sem_cmp_mismatch c
357        | _ ⇒ None ? ]
358      | Vnull ⇒
359        match v2 with
360        [ Vptr ptr2 ⇒ sem_cmp_mismatch c
361        | Vnull ⇒ sem_cmp_match c
362        | _ ⇒ None ?
363        ]
364      | _ ⇒ None ? ]
365  | cmp_case_ff _ ⇒
366      match v1 with
367      [ Vfloat f1 ⇒
368        match v2 with
369        [ Vfloat f2 ⇒ Some ? (of_bool (Fcmp c f1 f2))
370        | _ ⇒ None ? ]
371      | _ ⇒ None ? ]
372  | cmp_default _ _ ⇒ None ?
373  ].
374
375definition sem_unary_operation
376            : unary_operation → val → type → option val ≝
377  λop,v,ty.
378  match op with
379  [ Onotbool => sem_notbool v ty
380  | Onotint => sem_notint v
381  | Oneg => sem_neg v ty
382  ].
383
384let rec sem_binary_operation
385    (op: binary_operation)
386    (v1: val) (t1: type) (v2: val) (t2:type)
387    (m: mem): option val ≝
388  match op with
389  [ Oadd ⇒ sem_add v1 t1 v2 t2
390  | Osub ⇒ sem_sub v1 t1 v2 t2
391  | Omul ⇒ sem_mul v1 t1 v2 t2
392  | Omod ⇒ sem_mod v1 t1 v2 t2
393  | Odiv ⇒ sem_div v1 t1 v2 t2
394  | Oand ⇒ sem_and v1 v2 
395  | Oor  ⇒ sem_or v1 v2
396  | Oxor ⇒ sem_xor v1 v2
397  | Oshl ⇒ sem_shl v1 v2
398  | Oshr ⇒ sem_shr v1 t1 v2 t2
399  | Oeq ⇒ sem_cmp Ceq v1 t1 v2 t2 m
400  | One ⇒ sem_cmp Cne v1 t1 v2 t2 m
401  | Olt ⇒ sem_cmp Clt v1 t1 v2 t2 m
402  | Ogt ⇒ sem_cmp Cgt v1 t1 v2 t2 m
403  | Ole ⇒ sem_cmp Cle v1 t1 v2 t2 m
404  | Oge ⇒ sem_cmp Cge v1 t1 v2 t2 m
405  ].
406
407(* * Semantic of casts.  [cast v1 t1 t2 v2] holds if value [v1],
408  viewed with static type [t1], can be cast to type [t2],
409  resulting in value [v2].  *)
410
411let rec cast_int_int (sz: intsize) (sg: signedness) (dstsz: intsize)  (i: BitVector (bitsize_of_intsize sz)) : BitVector (bitsize_of_intsize dstsz) ≝
412  match sg with [ Signed ⇒ sign_ext ?? i | Unsigned ⇒ zero_ext ?? i ].
413
414let rec cast_int_float (si : signedness) (n:nat) (i: BitVector n) : float ≝
415  match si with
416  [ Signed ⇒ floatofint ? i
417  | Unsigned ⇒ floatofintu ? i
418  ].
419
420let rec cast_float_int (sz : intsize) (si : signedness) (f: float) : BitVector (bitsize_of_intsize sz) ≝
421  match si with
422  [ Signed ⇒ intoffloat ? f
423  | Unsigned ⇒ intuoffloat ? f
424  ].
425
426let rec cast_float_float (sz: floatsize) (f: float) : float ≝
427  match sz with
428  [ F32 ⇒ singleoffloat f
429  | F64 ⇒ f
430  ].
431
432(* Only for full 8051 memory spaces
433inductive type_region : type → region → Prop ≝
434| type_rgn_pointer : ∀s,t. type_region (Tpointer s t) s
435| type_rgn_array : ∀s,t,n. type_region (Tarray s t n) s
436(* Is the following necessary? *)
437| type_rgn_code : ∀tys,ty. type_region (Tfunction tys ty) Code.
438*)
439
440inductive type_ptr : type → Prop ≝
441| type_pointer : ∀t. type_ptr (Tpointer t)
442| type_array : ∀t,n. type_ptr (Tarray t n)
443| type_fun : ∀tys,ty. type_ptr (Tfunction tys ty).
444
445inductive cast : mem → val → type → type → val → Prop ≝
446  | cast_ii:   ∀m,sz2,sz1,si1,si2,i.            (**r int to int  *)
447      cast m (Vint sz1 i) (Tint sz1 si1) (Tint sz2 si2)
448           (Vint sz2 (cast_int_int sz1 si1 sz2 i))
449  | cast_fi:   ∀m,f,sz1,sz2,si2.                (**r float to int *)
450      cast m (Vfloat f) (Tfloat sz1) (Tint sz2 si2)
451           (Vint sz2 (cast_float_int sz2 si2 f))
452  | cast_if:   ∀m,sz1,sz2,si1,i.                (**r int to float  *)
453      cast m (Vint sz1 i) (Tint sz1 si1) (Tfloat sz2)
454          (Vfloat (cast_float_float sz2 (cast_int_float si1 ? i)))
455  | cast_ff:   ∀m,f,sz1,sz2.                    (**r float to float *)
456      cast m (Vfloat f) (Tfloat sz1) (Tfloat sz2)
457           (Vfloat (cast_float_float sz2 f))
458  | cast_pp: ∀m,ty,ty',ptr.
459(*      type_region ty (ptype ptr) →
460      type_region ty' r' →
461      ∀pc':pointer_compat (pblock ptr) r'.
462      cast m (Vptr ptr) ty ty' (Vptr (mk_pointer r' (pblock ptr) pc' (poff ptr)))*)
463      type_ptr ty →
464      type_ptr ty' →
465      cast m (Vptr ptr) ty ty' (Vptr ptr)
466  | cast_ip_z: ∀m,sz,sg,ty'.
467(*     type_region ty' r →*)
468      type_ptr ty' →
469      cast m (Vint sz (zero ?)) (Tint sz sg) ty' Vnull
470  | cast_pp_z: ∀m,ty,ty'.
471(*      type_region ty r →
472      type_region ty' r' →*)
473      type_ptr ty →
474      type_ptr ty' →
475      cast m Vnull ty ty' Vnull.
476
477(* * * Operational semantics *)
478
479(* * The semantics uses two environments.  The global environment
480  maps names of functions and global variables to memory block references,
481  and function pointers to their definitions.  (See module [Globalenvs].) *)
482
483definition genv ≝ genv_t clight_fundef.
484
485(* * The local environment maps local variables to block references.
486  The current value of the variable is stored in the associated memory
487  block. *)
488
489definition env ≝ identifier_map SymbolTag block. (* map variable -> location *)
490
491definition empty_env: env ≝ (empty_map …).
492
493(* * [load_value_of_type ty m b ofs] computes the value of a datum
494  of type [ty] residing in memory [m] at block [b], offset [ofs].
495  If the type [ty] indicates an access by value, the corresponding
496  memory load is performed.  If the type [ty] indicates an access by
497  reference, the pointer [Vptr b ofs] is returned. *)
498
499let rec load_value_of_type (ty: type) (m: mem) (b: block) (ofs: offset) : option val ≝
500  match access_mode ty with
501  [ By_value chunk ⇒ loadv chunk m (Vptr (mk_pointer b ofs))
502  | By_reference  ⇒ Some ? (Vptr (mk_pointer b ofs))
503(*    match pointer_compat_dec b r with
504    [ inl p ⇒ Some ? (Vptr (mk_pointer r b p ofs))
505    | inr _ ⇒ None ?
506    ]*)
507  | By_nothing _ ⇒ None ?
508  ].
509(*cases b //
510qed.*)
511
512(* * Symmetrically, [store_value_of_type ty m b ofs v] returns the
513  memory state after storing the value [v] in the datum
514  of type [ty] residing in memory [m] at block [b], offset [ofs].
515  This is allowed only if [ty] indicates an access by value. *)
516
517let rec store_value_of_type (ty_dest: type) (m: mem) (loc: block) (ofs: offset) (v: val) : option mem ≝
518  match access_mode ty_dest with
519  [ By_value chunk ⇒ storev chunk m (Vptr (mk_pointer loc ofs)) v
520  | By_reference  ⇒ None ?
521  | By_nothing _ ⇒ None ?
522  ].
523(*cases loc //
524qed.*)
525
526(* * Allocation of function-local variables.
527  [alloc_variables e1 m1 vars e2 m2] allocates one memory block
528  for each variable declared in [vars], and associates the variable
529  name with this block.  [e1] and [m1] are the initial local environment
530  and memory state.  [e2] and [m2] are the final local environment
531  and memory state. *)
532
533inductive alloc_variables: env → mem →
534                            list (ident × type) →
535                            env → mem → Prop ≝
536  | alloc_variables_nil:
537      ∀e,m.
538      alloc_variables e m (nil ?) e m
539  | alloc_variables_cons:
540      ∀e,m,id,ty,vars,m1,b1,m2,e2.
541      alloc m 0 (sizeof ty) XData = 〈m1, b1〉 →
542      alloc_variables (add … e id (pi1 … b1)) m1 vars e2 m2 →
543      alloc_variables e m (〈id, ty〉 :: vars) e2 m2.
544
545(* * Initialization of local variables that are parameters to a function.
546  [bind_parameters e m1 params args m2] stores the values [args]
547  in the memory blocks corresponding to the variables [params].
548  [m1] is the initial memory state and [m2] the final memory state. *)
549
550inductive bind_parameters: env →
551                           mem → list (ident × type) → list val →
552                           mem → Prop ≝
553  | bind_parameters_nil:
554      ∀e,m.
555      bind_parameters e m (nil ?) (nil ?) m
556  | bind_parameters_cons:
557      ∀e,m,id,ty,params,v1,vl,b,m1,m2.
558      lookup ?? e id = Some ? b →
559      store_value_of_type ty m b zero_offset v1 = Some ? m1 →
560      bind_parameters e m1 params vl m2 →
561      bind_parameters e m (〈id, ty〉 :: params) (v1 :: vl) m2.
562
563(* * Return the list of blocks in the codomain of [e]. *)
564
565definition blocks_of_env : env → list block ≝ λe.
566  map ?? (λx. snd ?? x) (elements ?? e).
567
568(* * Selection of the appropriate case of a [switch], given the value [n]
569  of the selector expression.  We fail if any of the cases has an integer of
570  the wrong size.  (NB: ideally, we'd change the syntax so that there is only
571  one size, but we're trying to keep the impact of changes on existing code
572  down.) *)
573
574let rec select_switch (sz:intsize) (n: BitVector (bitsize_of_intsize sz)) (sl: labeled_statements)
575                       on sl : option labeled_statements ≝
576  match sl with
577  [ LSdefault _ ⇒ Some ? sl
578  | LScase sz' c s sl' ⇒ intsize_eq_elim ? sz sz' ? n
579                         (λn. if eq_bv ? c n then Some ? sl else select_switch sz' n sl') (None ?)
580  ].
581
582(* * Turn a labeled statement into a sequence *)
583
584let rec seq_of_labeled_statement (sl: labeled_statements) : statement ≝
585  match sl with
586  [ LSdefault s ⇒ s
587  | LScase _ c s sl' ⇒ Ssequence s (seq_of_labeled_statement sl')
588  ].
589
590(*
591Section SEMANTICS.
592
593Variable ge: genv.
594
595(** ** Evaluation of expressions *)
596
597Section EXPR.
598
599Variable e: env.
600Variable m: mem.
601*)
602(* * [eval_expr ge e m a v] defines the evaluation of expression [a]
603  in r-value position.  [v] is the value of the expression.
604  [e] is the current environment and [m] is the current memory state. *)
605
606inductive eval_expr (ge:genv) (e:env) (m:mem) : expr → val → trace → Prop ≝
607  | eval_Econst_int:   ∀sz,sg,i.
608      eval_expr ge e m (Expr (Econst_int sz i) (Tint sz sg)) (Vint sz i) E0
609  | eval_Econst_float:   ∀f,ty.
610      eval_expr ge e m (Expr (Econst_float f) ty) (Vfloat f) E0
611  | eval_Elvalue: ∀a,ty,loc,ofs,v,tr.
612      eval_lvalue ge e m (Expr a ty) loc ofs tr →
613      load_value_of_type ty m loc ofs = Some ? v →
614      eval_expr ge e m (Expr a ty) v tr
615  | eval_Eaddrof: ∀a,ty,loc,ofs,tr.
616      eval_lvalue ge e m a loc ofs tr →
617(*      ∀pc:pointer_compat loc r.*)
618      eval_expr ge e m (Expr (Eaddrof a) (Tpointer ty)) (Vptr (mk_pointer loc ofs)) tr
619  | eval_Esizeof: ∀ty',sz,sg.
620      eval_expr ge e m (Expr (Esizeof ty') (Tint sz sg)) (Vint sz (repr ? (sizeof ty'))) E0
621  | eval_Eunop:  ∀op,a,ty,v1,v,tr.
622      eval_expr ge e m a v1 tr →
623      sem_unary_operation op v1 (typeof a) = Some ? v →
624      eval_expr ge e m (Expr (Eunop op a) ty) v tr
625  | eval_Ebinop: ∀op,a1,a2,ty,v1,v2,v,tr1,tr2.
626      eval_expr ge e m a1 v1 tr1 →
627      eval_expr ge e m a2 v2 tr2 →
628      sem_binary_operation op v1 (typeof a1) v2 (typeof a2) m = Some ? v →
629      eval_expr ge e m (Expr (Ebinop op a1 a2) ty) v (tr1⧺tr2)
630  | eval_Econdition_true: ∀a1,a2,a3,ty,v1,v2,tr1,tr2.
631      eval_expr ge e m a1 v1 tr1 →
632      is_true v1 (typeof a1) →
633      eval_expr ge e m a2 v2 tr2 →
634      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v2 (tr1⧺tr2)
635  | eval_Econdition_false: ∀a1,a2,a3,ty,v1,v3,tr1,tr2.
636      eval_expr ge e m a1 v1 tr1 →
637      is_false v1 (typeof a1) →
638      eval_expr ge e m a3 v3 tr2 →
639      eval_expr ge e m (Expr (Econdition a1 a2 a3) ty) v3 (tr1⧺tr2)
640  | eval_Eorbool_1: ∀a1,a2,ty,v1,tr.
641      eval_expr ge e m a1 v1 tr →
642      is_true v1 (typeof a1) →
643      eval_expr ge e m (Expr (Eorbool a1 a2) ty) Vtrue tr
644  | eval_Eorbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
645      eval_expr ge e m a1 v1 tr1 →
646      is_false v1 (typeof a1) →
647      eval_expr ge e m a2 v2 tr2 →
648      bool_of_val v2 (typeof a2) v →
649      eval_expr ge e m (Expr (Eorbool a1 a2) ty) v (tr1⧺tr2)
650  | eval_Eandbool_1: ∀a1,a2,ty,v1,tr.
651      eval_expr ge e m a1 v1 tr →
652      is_false v1 (typeof a1) →
653      eval_expr ge e m (Expr (Eandbool a1 a2) ty) Vfalse tr
654  | eval_Eandbool_2: ∀a1,a2,ty,v1,v2,v,tr1,tr2.
655      eval_expr ge e m a1 v1 tr1 →
656      is_true v1 (typeof a1) →
657      eval_expr ge e m a2 v2 tr2 →
658      bool_of_val v2 (typeof a2) v →
659      eval_expr ge e m (Expr (Eandbool a1 a2) ty) v (tr1⧺tr2)
660  | eval_Ecast:   ∀a,ty,ty',v1,v,tr.
661      eval_expr ge e m a v1 tr →
662      cast m v1 (typeof a) ty v →
663      eval_expr ge e m (Expr (Ecast ty a) ty') v tr
664  | eval_Ecost: ∀a,ty,v,l,tr.
665      eval_expr ge e m a v tr →
666      eval_expr ge e m (Expr (Ecost l a) ty) v (tr⧺Echarge l)
667
668(* * [eval_lvalue ge e m a r b ofs] defines the evaluation of expression [a]
669  in l-value position.  The result is the memory location [b, ofs]
670  that contains the value of the expression [a].  The memory location should
671  be representable in a pointer of region r. *)
672
673with eval_lvalue (*(ge:genv) (e:env) (m:mem)*) : expr → block → offset → trace → Prop ≝
674  | eval_Evar_local:   ∀id,l,ty.
675      (* XXX notation? e!id*) lookup ?? e id = Some ? l →
676      eval_lvalue ge e m (Expr (Evar id) ty) l zero_offset E0
677  | eval_Evar_global: ∀id,l,ty.
678      (* XXX e!id *) lookup ?? e id = None ? →
679      find_symbol … ge id = Some ? l →
680      eval_lvalue ge e m (Expr (Evar id) ty) l zero_offset E0
681  | eval_Ederef: ∀a,ty,l,ofs,tr.
682      eval_expr ge e m a (Vptr (mk_pointer  l  ofs)) tr →
683      eval_lvalue ge e m (Expr (Ederef a) ty) l ofs tr
684    (* Aside: note that each block of memory is entirely contained within one
685       memory region; hence adding a field offset will not produce a location
686       outside of the original location's region. *)
687 | eval_Efield_struct:   ∀a,i,ty,l,ofs,id,fList,delta,tr.
688      eval_lvalue ge e m a l ofs tr →
689      typeof a = Tstruct id fList →
690      field_offset i fList = OK ? delta →
691      eval_lvalue ge e m (Expr (Efield a i) ty) l (shift_offset ? ofs (repr I32 delta)) tr
692 | eval_Efield_union:   ∀a,i,ty,l,ofs,id,fList,tr.
693      eval_lvalue ge e m a l ofs tr →
694      typeof a = Tunion id fList →
695      eval_lvalue ge e m (Expr (Efield a i) ty) l ofs tr.
696
697let rec eval_expr_ind (ge:genv) (e:env) (m:mem)
698  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
699  (eci:∀sz,sg,i. P ??? (eval_Econst_int ge e m sz sg i))
700  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
701  (elv:∀a,ty,loc,ofs,v,tr,H1,H2. P ??? (eval_Elvalue ge e m a ty loc ofs v tr H1 H2))
702  (ead:∀a,ty,loc,ofs,tr,H. P ??? (eval_Eaddrof ge e m a ty loc ofs tr H))
703  (esz:∀ty',sz,sg. P ??? (eval_Esizeof ge e m ty' sz sg))
704  (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))
705  (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))
706  (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))
707  (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))
708  (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))
709  (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))
710  (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))
711  (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))
712  (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))
713  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
714  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
715  match ev with
716  [ eval_Econst_int sz sg i ⇒ eci sz sg i
717  | eval_Econst_float f ty ⇒ ecF f ty
718  | eval_Elvalue a ty loc ofs v tr H1 H2 ⇒ elv a ty loc ofs v tr H1 H2
719  | eval_Eaddrof a ty loc ofs tr H ⇒ ead a ty loc ofs tr H
720  | eval_Esizeof ty' sz sg ⇒ esz ty' sz sg
721  | 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)
722  | 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)
723  | 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)
724  | 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)
725  | 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)
726  | 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)
727  | 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)
728  | 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)
729  | 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)
730  | 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)
731  ].
732(*
733inverter eval_expr_inv_ind for eval_expr : Prop.
734*)
735let rec eval_lvalue_ind (ge:genv) (e:env) (m:mem)
736  (P:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
737  (lvl:∀id,l,ty,H. P ???? (eval_Evar_local ge e m id l ty H))
738  (lvg:∀id,l,ty,H1,H2. P ???? (eval_Evar_global ge e m id l ty H1 H2))
739  (lde:∀a,ty,l,ofs,tr,H. P ???? (eval_Ederef ge e m a ty l ofs tr H))
740  (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))
741  (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))
742  (a:expr) (loc:block) (ofs:offset) (tr:trace) (ev:eval_lvalue ge e m a loc ofs tr) on ev : P a loc ofs tr ev ≝
743  match ev with
744  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
745  | eval_Evar_global id l ty H1 H2 ⇒ lvg id l ty H1 H2
746  | eval_Ederef a ty l ofs tr H ⇒ lde a ty l ofs tr H
747  | 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)
748  | 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)
749  ].
750
751(*
752ninverter eval_lvalue_inv_ind for eval_lvalue : Prop.
753*)
754(*
755definition eval_lvalue_inv_ind :
756  ∀x1: genv.
757   ∀x2: env.
758    ∀x3: mem.
759     ∀x4: expr.
760       ∀x6: block.
761        ∀x7: offset.
762         ∀x8: trace.
763          ∀P:
764            ∀_z1430: expr.
765              ∀_z1428: block. ∀_z1427: offset. ∀_z1426: trace. Prop.
766           ∀_H1: ?.
767            ∀_H2: ?.
768             ∀_H3: ?.
769              ∀_H4: ?.
770               ∀_H5: ?.
771                ∀_Hterm: eval_lvalue x1 x2 x3 x4 x6 x7 x8.
772                 P x4 x6 x7 x8
773:=
774  (λx1:genv.
775    (λx2:env.
776      (λx3:mem.
777        (λx4:expr.
778            (λx6:block.
779              (λx7:offset.
780                (λx8:trace.
781                  (λP:∀_z1430: expr.
782                         ∀_z1428: block.
783                          ∀_z1427: offset. ∀_z1426: trace. Prop.
784                    (λH1:?.
785                      (λH2:?.
786                        (λH3:?.
787                          (λH4:?.
788                            (λH5:?.
789                              (λHterm:eval_lvalue x1 x2 x3 x4 x6 x7 x8.
790                                ((λHcut:∀z1435: eq expr x4 x4.
791                                           ∀z1433: eq block x6 x6.
792                                            ∀z1432: eq offset x7 x7.
793                                             ∀z1431: eq trace x8 x8.
794                                              P x4 x6 x7 x8.
795                                   (Hcut (refl expr x4)
796                                     (refl block x6)
797                                     (refl offset x7) (refl trace x8)))
798                                  ?))))))))))))))).
799[ @(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)
800  [ @H1 | @H2 | @H3 | @H4 | @H5 ]
801| *: skip
802] qed.
803*)
804let rec eval_expr_ind2 (ge:genv) (e:env) (m:mem)
805  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
806  (Q:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
807  (eci:∀sz,sg,i. P ??? (eval_Econst_int ge e m sz sg i))
808  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
809  (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))
810  (ead:∀a,ty,loc,ofs,tr,H. Q a loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty loc ofs tr H))
811  (esz:∀ty',sz,sg. P ??? (eval_Esizeof ge e m ty' sz sg))
812  (eun:∀op,a,ty,v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Eunop ge e m op a ty v1 v tr H1 H2))
813  (ebi:∀op,a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H2 → P ??? (eval_Ebinop ge e m op a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3))
814  (ect:∀a1,a2,a3,ty,v1,v2,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Econdition_true ge e m a1 a2 a3 ty v1 v2 tr1 tr2 H1 H2 H3))
815  (ecf:∀a1,a2,a3,ty,v1,v3,tr1,tr2,H1,H2,H3. P a1 v1 tr1 H1 → P a3 v3 tr2 H3 → P ??? (eval_Econdition_false ge e m a1 a2 a3 ty v1 v3 tr1 tr2 H1 H2 H3))
816  (eo1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eorbool_1 ge e m a1 a2 ty v1 tr H1 H2))
817  (eo2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eorbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
818  (ea1:∀a1,a2,ty,v1,tr,H1,H2. P a1 v1 tr H1 → P ??? (eval_Eandbool_1 ge e m a1 a2 ty v1 tr H1 H2))
819  (ea2:∀a1,a2,ty,v1,v2,v,tr1,tr2,H1,H2,H3,H4. P a1 v1 tr1 H1 → P a2 v2 tr2 H3 → P ??? (eval_Eandbool_2 ge e m a1 a2 ty v1 v2 v tr1 tr2 H1 H2 H3 H4))
820  (ecs:∀a,ty,ty',v1,v,tr,H1,H2. P a v1 tr H1 → P ??? (eval_Ecast ge e m a ty ty' v1 v tr H1 H2))
821  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
822  (lvl:∀id,l,ty,H. Q ???? (eval_Evar_local ge e m id l ty H))
823  (lvg:∀id,l,ty,H1,H2. Q ???? (eval_Evar_global ge e m id l ty H1 H2))
824  (lde:∀a,ty,l,ofs,tr,H. P a (Vptr (mk_pointer l ofs)) tr H → Q ???? (eval_Ederef ge e m a ty l ofs tr H))
825  (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))
826  (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))
827 
828  (a:expr) (v:val) (tr:trace) (ev:eval_expr ge e m a v tr) on ev : P a v tr ev ≝
829  match ev with
830  [ eval_Econst_int sz sg i ⇒ eci sz sg i
831  | eval_Econst_float f ty ⇒ ecF f ty
832  | 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)
833  | eval_Eaddrof a ty loc ofs tr H ⇒ ead a ty 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)
834  | eval_Esizeof ty' sz sg ⇒ esz ty' sz sg
835  | 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)
836  | 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)
837  | 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)
838  | 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)
839  | 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)
840  | 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)
841  | 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)
842  | 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)
843  | 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)
844  | 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)
845  ]
846and eval_lvalue_ind2 (ge:genv) (e:env) (m:mem)
847  (P:∀a,v,tr. eval_expr ge e m a v tr → Prop)
848  (Q:∀a,loc,ofs,tr. eval_lvalue ge e m a loc ofs tr → Prop)
849  (eci:∀sz,sg,i. P ??? (eval_Econst_int ge e m sz sg i))
850  (ecF:∀f,ty. P ??? (eval_Econst_float ge e m f ty))
851  (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))
852  (ead:∀a,ty,loc,ofs,tr,H. Q a loc ofs tr H → P ??? (eval_Eaddrof ge e m a ty loc ofs tr H))
853  (esz:∀ty',sz,sg. P ??? (eval_Esizeof ge e m ty' sz sg))
854  (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))
855  (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))
856  (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))
857  (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))
858  (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))
859  (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))
860  (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))
861  (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))
862  (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))
863  (eco:∀a,ty,v,l,tr,H. P a v tr H → P ??? (eval_Ecost ge e m a ty v l tr H))
864  (lvl:∀id,l,ty,H. Q ???? (eval_Evar_local ge e m id l ty H))
865  (lvg:∀id,l,ty,H1,H2. Q ???? (eval_Evar_global ge e m id l ty H1 H2))
866  (lde:∀a,ty,l,ofs,tr,H. P a (Vptr (mk_pointer l ofs)) tr H → Q ???? (eval_Ederef ge e m a ty l ofs tr H))
867  (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))
868  (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))
869  (a:expr) (loc:block) (ofs:offset) (tr:trace) (ev:eval_lvalue ge e m a loc ofs tr) on ev : Q a loc ofs tr ev ≝
870  match ev with
871  [ eval_Evar_local id l ty H ⇒ lvl id l ty H
872  | eval_Evar_global id l ty H1 H2 ⇒ lvg id l ty H1 H2
873  | eval_Ederef a ty l ofs tr H ⇒ lde a ty 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 (mk_pointer l ofs)) tr H)
874  | 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)
875  | 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)
876  ].
877
878definition combined_expr_lvalue_ind ≝
879λ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. 
880conj ??
881  (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)
882  (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).
883
884(* * [eval_lvalue ge e m a b ofs] defines the evaluation of expression [a]
885  in l-value position.  The result is the memory location [b, ofs]
886  that contains the value of the expression [a]. *)
887
888(*
889Scheme eval_expr_ind22 := Minimality for eval_expr Sort Prop
890  with eval_lvalue_ind2 := Minimality for eval_lvalue Sort Prop.
891*)
892
893(* * [eval_exprlist ge e m al vl] evaluates a list of r-value
894  expressions [al] to their values [vl]. *)
895
896inductive eval_exprlist (ge:genv) (e:env) (m:mem) : list expr → list val → trace → Prop ≝
897  | eval_Enil:
898      eval_exprlist ge e m (nil ?) (nil ?) E0
899  | eval_Econs:   ∀a,bl,v,vl,tr1,tr2.
900      eval_expr ge e m a v tr1 →
901      eval_exprlist ge e m bl vl tr2 →
902      eval_exprlist ge e m (a :: bl) (v :: vl) (tr1⧺tr2).
903
904(*End EXPR.*)
905
906(* * ** Transition semantics for statements and functions *)
907
908(* * Continuations *)
909
910inductive cont: Type[0] :=
911  | Kstop: cont
912  | Kseq: statement -> cont -> cont
913       (**r [Kseq s2 k] = after [s1] in [s1;s2] *)
914  | Kwhile: expr -> statement -> cont -> cont
915       (**r [Kwhile e s k] = after [s] in [while (e) s] *)
916  | Kdowhile: expr -> statement -> cont -> cont
917       (**r [Kdowhile e s k] = after [s] in [do s while (e)] *)
918  | Kfor2: expr -> statement -> statement -> cont -> cont
919       (**r [Kfor2 e2 e3 s k] = after [s] in [for(e1;e2;e3) s] *)
920  | Kfor3: expr -> statement -> statement -> cont -> cont
921       (**r [Kfor3 e2 e3 s k] = after [e3] in [for(e1;e2;e3) s] *)
922  | Kswitch: cont -> cont
923       (**r catches [break] statements arising out of [switch] *)
924  | Kcall: option (block × offset × type) -> (**r where to store result *)
925           function ->                       (**r calling function *)
926           env ->                            (**r local env of calling function *)
927           cont -> cont.
928
929(* * Pop continuation until a call or stop *)
930
931let rec call_cont (k: cont) : cont :=
932  match k with
933  [ Kseq s k => call_cont k
934  | Kwhile e s k => call_cont k
935  | Kdowhile e s k => call_cont k
936  | Kfor2 e2 e3 s k => call_cont k
937  | Kfor3 e2 e3 s k => call_cont k
938  | Kswitch k => call_cont k
939  | _ => k
940  ].
941
942definition is_call_cont : cont → Prop ≝ λk.
943  match k with
944  [ Kstop => True
945  | Kcall _ _ _ _ => True
946  | _ => False
947  ].
948
949(* * States *)
950
951inductive state: Type[0] :=
952  | State:
953      ∀f: function.
954      ∀s: statement.
955      ∀k: cont.
956      ∀e: env.
957      ∀m: mem.  state
958  | Callstate:
959      ∀fd: clight_fundef.
960      ∀args: list val.
961      ∀k: cont.
962      ∀m: mem. state
963  | Returnstate:
964      ∀res: val.
965      ∀k: cont.
966      ∀m: mem. state
967  | Finalstate:
968      ∀r: int.
969      state.
970                 
971(* * Find the statement and manufacture the continuation
972  corresponding to a label *)
973
974let rec find_label (lbl: label) (s: statement) (k: cont)
975                    on s: option (statement × cont) :=
976  match s with
977  [ Ssequence s1 s2 =>
978      match find_label lbl s1 (Kseq s2 k) with
979      [ Some sk => Some ? sk
980      | None => find_label lbl s2 k
981      ]
982  | Sifthenelse a s1 s2 =>
983      match find_label lbl s1 k with
984      [ Some sk => Some ? sk
985      | None => find_label lbl s2 k
986      ]
987  | Swhile a s1 =>
988      find_label lbl s1 (Kwhile a s1 k)
989  | Sdowhile a s1 =>
990      find_label lbl s1 (Kdowhile a s1 k)
991  | Sfor a1 a2 a3 s1 =>
992      match find_label lbl a1 (Kseq (Sfor Sskip a2 a3 s1) k) with
993      [ Some sk => Some ? sk
994      | None =>
995          match find_label lbl s1 (Kfor2 a2 a3 s1 k) with
996          [ Some sk => Some ? sk
997          | None => find_label lbl a3 (Kfor3 a2 a3 s1 k)
998          ]
999      ]
1000  | Sswitch e sl =>
1001      find_label_ls lbl sl (Kswitch k)
1002  | Slabel lbl' s' =>
1003      match ident_eq lbl lbl' with
1004      [ inl _ ⇒ Some ? 〈s', k〉
1005      | inr _ ⇒ find_label lbl s' k
1006      ]
1007  | Scost c s' ⇒
1008      find_label lbl s' k
1009  | _ => None ?
1010  ]
1011
1012and find_label_ls (lbl: label) (sl: labeled_statements) (k: cont)
1013                    on sl: option (statement × cont) :=
1014  match sl with
1015  [ LSdefault s => find_label lbl s k
1016  | LScase _ _ s sl' =>
1017      match find_label lbl s (Kseq (seq_of_labeled_statement sl') k) with
1018      [ Some sk => Some ? sk
1019      | None => find_label_ls lbl sl' k
1020      ]
1021  ].
1022
1023(* * Transition relation *)
1024
1025(* Strip off outer pointer for use when comparing function types. *)
1026definition fun_typeof ≝
1027λe. match typeof e with
1028[ Tvoid ⇒ Tvoid
1029| Tint a b ⇒ Tint a b
1030| Tfloat a ⇒ Tfloat a
1031| Tpointer ty ⇒ ty
1032| Tarray a b ⇒ Tarray a b
1033| Tfunction a b ⇒ Tfunction a b
1034| Tstruct a b ⇒ Tstruct a b
1035| Tunion a b ⇒ Tunion a b
1036| Tcomp_ptr a ⇒ Tcomp_ptr a
1037].
1038
1039(* XXX: note that cost labels in exprs expose a particular eval order. *)
1040
1041inductive step (ge:genv) : state → trace → state → Prop ≝
1042
1043  | step_assign:   ∀f,a1,a2,k,e,m,loc,ofs,v2,m',tr1,tr2.
1044      eval_lvalue ge e m a1 loc ofs tr1 →
1045      eval_expr ge e m a2 v2 tr2 →
1046      store_value_of_type (typeof a1) m loc ofs v2 = Some ? m' →
1047      step ge (State f (Sassign a1 a2) k e m)
1048           (tr1⧺tr2) (State f Sskip k e m')
1049
1050  | step_call_none:   ∀f,a,al,k,e,m,vf,vargs,fd,tr1,tr2.
1051      eval_expr ge e m a vf tr1 →
1052      eval_exprlist ge e m al vargs tr2 →
1053      find_funct … ge vf = Some ? fd →
1054      type_of_fundef fd = fun_typeof a →
1055      step ge (State f (Scall (None ?) a al) k e m)
1056           (tr1⧺tr2) (Callstate fd vargs (Kcall (None ?) f e k) m)
1057
1058  | step_call_some:   ∀f,lhs,a,al,k,e,m,loc,ofs,vf,vargs,fd,tr1,tr2,tr3.
1059      eval_lvalue ge e m lhs loc ofs tr1 →
1060      eval_expr ge e m a vf tr2 →
1061      eval_exprlist ge e m al vargs tr3 →
1062      find_funct … ge vf = Some ? fd →
1063      type_of_fundef fd = fun_typeof a →
1064      step ge (State f (Scall (Some ? lhs) a al) k e m)
1065           (tr1⧺tr2⧺tr3) (Callstate fd vargs (Kcall (Some ? 〈〈loc, ofs〉, typeof lhs〉) f e k) m)
1066
1067  | step_seq:  ∀f,s1,s2,k,e,m.
1068      step ge (State f (Ssequence s1 s2) k e m)
1069           E0 (State f s1 (Kseq s2 k) e m)
1070  | step_skip_seq: ∀f,s,k,e,m.
1071      step ge (State f Sskip (Kseq s k) e m)
1072           E0 (State f s k e m)
1073  | step_continue_seq: ∀f,s,k,e,m.
1074      step ge (State f Scontinue (Kseq s k) e m)
1075           E0 (State f Scontinue k e m)
1076  | step_break_seq: ∀f,s,k,e,m.
1077      step ge (State f Sbreak (Kseq s k) e m)
1078           E0 (State f Sbreak k e m)
1079
1080  | step_ifthenelse_true:  ∀f,a,s1,s2,k,e,m,v1,tr.
1081      eval_expr ge e m a v1 tr →
1082      is_true v1 (typeof a) →
1083      step ge (State f (Sifthenelse a s1 s2) k e m)
1084           tr (State f s1 k e m)
1085  | step_ifthenelse_false: ∀f,a,s1,s2,k,e,m,v1,tr.
1086      eval_expr ge e m a v1 tr →
1087      is_false v1 (typeof a) →
1088      step ge (State f (Sifthenelse a s1 s2) k e m)
1089           tr (State f s2 k e m)
1090
1091  | step_while_false: ∀f,a,s,k,e,m,v,tr.
1092      eval_expr ge e m a v tr →
1093      is_false v (typeof a) →
1094      step ge (State f (Swhile a s) k e m)
1095           tr (State f Sskip k e m)
1096  | step_while_true: ∀f,a,s,k,e,m,v,tr.
1097      eval_expr ge e m a v tr →
1098      is_true v (typeof a) →
1099      step ge (State f (Swhile a s) k e m)
1100           tr (State f s (Kwhile a s k) e m)
1101  | step_skip_or_continue_while: ∀f,x,a,s,k,e,m.
1102      x = Sskip ∨ x = Scontinue →
1103      step ge (State f x (Kwhile a s k) e m)
1104           E0 (State f (Swhile a s) k e m)
1105  | step_break_while: ∀f,a,s,k,e,m.
1106      step ge (State f Sbreak (Kwhile a s k) e m)
1107           E0 (State f Sskip k e m)
1108
1109  | step_dowhile: ∀f,a,s,k,e,m.
1110      step ge (State f (Sdowhile a s) k e m)
1111        E0 (State f s (Kdowhile a s k) e m)
1112  | step_skip_or_continue_dowhile_false: ∀f,x,a,s,k,e,m,v,tr.
1113      x = Sskip ∨ x = Scontinue →
1114      eval_expr ge e m a v tr →
1115      is_false v (typeof a) →
1116      step ge (State f x (Kdowhile a s k) e m)
1117           tr (State f Sskip k e m)
1118  | step_skip_or_continue_dowhile_true: ∀f,x,a,s,k,e,m,v,tr.
1119      x = Sskip ∨ x = Scontinue →
1120      eval_expr ge e m a v tr →
1121      is_true v (typeof a) →
1122      step ge (State f x (Kdowhile a s k) e m)
1123           tr (State f (Sdowhile a s) k e m)
1124  | step_break_dowhile: ∀f,a,s,k,e,m.
1125      step ge (State f Sbreak (Kdowhile a s k) e m)
1126           E0 (State f Sskip k e m)
1127
1128  | step_for_start: ∀f,a1,a2,a3,s,k,e,m.
1129      a1 ≠ Sskip →
1130      step ge (State f (Sfor a1 a2 a3 s) k e m)
1131           E0 (State f a1 (Kseq (Sfor Sskip a2 a3 s) k) e m)
1132  | step_for_false: ∀f,a2,a3,s,k,e,m,v,tr.
1133      eval_expr ge e m a2 v tr →
1134      is_false v (typeof a2) →
1135      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1136           tr (State f Sskip k e m)
1137  | step_for_true: ∀f,a2,a3,s,k,e,m,v,tr.
1138      eval_expr ge e m a2 v tr →
1139      is_true v (typeof a2) →
1140      step ge (State f (Sfor Sskip a2 a3 s) k e m)
1141           tr (State f s (Kfor2 a2 a3 s k) e m)
1142  | step_skip_or_continue_for2: ∀f,x,a2,a3,s,k,e,m.
1143      x = Sskip ∨ x = Scontinue →
1144      step ge (State f x (Kfor2 a2 a3 s k) e m)
1145           E0 (State f a3 (Kfor3 a2 a3 s k) e m)
1146  | step_break_for2: ∀f,a2,a3,s,k,e,m.
1147      step ge (State f Sbreak (Kfor2 a2 a3 s k) e m)
1148           E0 (State f Sskip k e m)
1149  | step_skip_for3: ∀f,a2,a3,s,k,e,m.
1150      step ge (State f Sskip (Kfor3 a2 a3 s k) e m)
1151           E0 (State f (Sfor Sskip a2 a3 s) k e m)
1152
1153  | step_return_0: ∀f,k,e,m.
1154      fn_return f = Tvoid →
1155      step ge (State f (Sreturn (None ?)) k e m)
1156           E0 (Returnstate Vundef (call_cont k) (free_list m (blocks_of_env e)))
1157  | step_return_1: ∀f,a,k,e,m,v,tr.
1158      fn_return f ≠ Tvoid →
1159      eval_expr ge e m a v tr →
1160      step ge (State f (Sreturn (Some ? a)) k e m)
1161           tr (Returnstate v (call_cont k) (free_list m (blocks_of_env e)))
1162  | step_skip_call: ∀f,k,e,m.
1163      is_call_cont k →
1164      fn_return f = Tvoid →
1165      step ge (State f Sskip k e m)
1166           E0 (Returnstate Vundef k (free_list m (blocks_of_env e)))
1167
1168  | step_switch: ∀f,a,sl,sl',k,e,m,sz,sg,n,tr.
1169      eval_expr ge e m a (Vint sz n) tr →
1170      typeof a = Tint sz sg →
1171      select_switch sz n sl = Some ? sl' →
1172      step ge (State f (Sswitch a sl) k e m)
1173           tr (State f (seq_of_labeled_statement sl') (Kswitch k) e m)
1174  | step_skip_break_switch: ∀f,x,k,e,m.
1175      x = Sskip ∨ x = Sbreak →
1176      step ge (State f x (Kswitch k) e m)
1177           E0 (State f Sskip k e m)
1178  | step_continue_switch: ∀f,k,e,m.
1179      step ge (State f Scontinue (Kswitch k) e m)
1180           E0 (State f Scontinue k e m)
1181
1182  | step_label: ∀f,lbl,s,k,e,m.
1183      step ge (State f (Slabel lbl s) k e m)
1184           E0 (State f s k e m)
1185
1186  | step_goto: ∀f,lbl,k,e,m,s',k'.
1187      find_label lbl (fn_body f) (call_cont k) = Some ? 〈s', k'〉 →
1188      step ge (State f (Sgoto lbl) k e m)
1189           E0 (State f s' k' e m)
1190
1191  | step_internal_function: ∀f,vargs,k,m,e,m1,m2.
1192      alloc_variables empty_env m ((fn_params f) @ (fn_vars f)) e m1 →
1193      bind_parameters e m1 (fn_params f) vargs m2 →
1194      step ge (Callstate (CL_Internal f) vargs k m)
1195           E0 (State f (fn_body f) k e m2)
1196
1197  | step_external_function: ∀id,targs,tres,vargs,k,m,vres,t.
1198      event_match (external_function id targs tres) vargs t vres →
1199      step ge (Callstate (CL_External id targs tres) vargs k m)
1200            t (Returnstate vres k m)
1201
1202  | step_returnstate_0: ∀v,f,e,k,m.
1203      step ge (Returnstate v (Kcall (None ?) f e k) m)
1204           E0 (State f Sskip k e m)
1205
1206  | step_returnstate_1: ∀v,f,e,k,m,m',loc,ofs,ty.
1207      store_value_of_type ty m loc ofs v = Some ? m' →
1208      step ge (Returnstate v (Kcall (Some ? 〈〈loc, ofs〉, ty〉) f e k) m)
1209           E0 (State f Sskip k e m')
1210           
1211  | step_cost: ∀f,lbl,s,k,e,m.
1212      step ge (State f (Scost lbl s) k e m)
1213           (Echarge lbl) (State f s k e m)
1214 
1215  | step_final: ∀r,m.
1216      step ge (Returnstate (Vint I32 r) Kstop m)
1217           E0 (Finalstate r).
1218
1219(*
1220End SEMANTICS.
1221*)
1222
1223(* * * Whole-program semantics *)
1224
1225(* * Execution of whole programs are described as sequences of transitions
1226  from an initial state to a final state.  An initial state is a [Callstate]
1227  corresponding to the invocation of the ``main'' function of the program
1228  without arguments and with an empty continuation. *)
1229
1230inductive initial_state (p: clight_program): state -> Prop :=
1231  | initial_state_intro: ∀b,f,ge,m0.
1232      globalenv … (fst ??) p = ge →
1233      init_mem … (fst ??) p = OK ? m0 →
1234      find_symbol … ge (prog_main ?? p) = Some ? b →
1235      find_funct_ptr … ge b = Some ? f →
1236      initial_state p (Callstate f (nil ?) Kstop m0).
1237
1238(* * A final state is a [Returnstate] with an empty continuation. *)
1239
1240inductive final_state: state -> int -> Prop :=
1241  | final_state_intro: ∀r.
1242      final_state (Finalstate r) r.
1243
1244(* * Execution of a whole program: [exec_program p beh]
1245  holds if the application of [p]'s main function to no arguments
1246  in the initial memory state for [p] has [beh] as observable
1247  behavior. *)
1248
1249definition exec_program : clight_program → program_behavior → Prop ≝ λp,beh.
1250  ∀ge. globalenv … (fst ??) p = ge →
1251  program_behaves (mk_transrel ?? step) (initial_state p) final_state ge beh.
1252 
Note: See TracBrowser for help on using the repository browser.