source: src/Clight/Csem.ma @ 2429

Last change on this file since 2429 was 2429, checked in by garnier, 8 years ago

Restrict semantics of pointer comparison to what CompCert? does - i.e. no more pointer one of the end of a block.

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