source: src/Clight/toCminor.ma @ 1884

Last change on this file since 1884 was 1884, checked in by campbell, 6 years ago

Syntax changes to fit Paolo's commit.

File size: 74.5 KB
Line 
1include "Clight/ClassifyOp.ma".
2include "basics/lists/list.ma".
3include "Clight/fresh.ma".
4
5(* Identify local variables that must be allocated memory. *)
6(* These are the variables whose addresses are taken. *)
7let rec gather_mem_vars_expr (e:expr) : identifier_set SymbolTag ≝
8match e with
9[ Expr ed ty ⇒
10    match ed with
11    [ Ederef e1 ⇒ gather_mem_vars_expr e1
12    | Eaddrof e1 ⇒ gather_mem_vars_addr e1
13    | Eunop _ e1 ⇒ gather_mem_vars_expr e1
14    | Ebinop _ e1 e2 ⇒ gather_mem_vars_expr e1 ∪
15                       gather_mem_vars_expr e2
16    | Ecast _ e1 ⇒ gather_mem_vars_expr e1
17    | Econdition e1 e2 e3 ⇒ gather_mem_vars_expr e1 ∪
18                            gather_mem_vars_expr e2 ∪
19                            gather_mem_vars_expr e3
20    | Eandbool e1 e2 ⇒ gather_mem_vars_expr e1 ∪
21                       gather_mem_vars_expr e2
22    | Eorbool e1 e2 ⇒ gather_mem_vars_expr e1 ∪
23                      gather_mem_vars_expr e2
24    | Efield e1 _ ⇒ gather_mem_vars_expr e1
25    | Ecost _ e1 ⇒ gather_mem_vars_expr e1
26    | _ ⇒ ∅
27    ]
28]
29and gather_mem_vars_addr (e:expr) : identifier_set SymbolTag ≝
30match e with
31[ Expr ed ty ⇒
32    match ed with
33    [ Evar x ⇒ { (x) }
34    | Ederef e1 ⇒ gather_mem_vars_expr e1
35    | Efield e1 _ ⇒ gather_mem_vars_addr e1
36    | _ ⇒ ∅ (* not an lvalue *)
37    ]
38].
39
40let rec gather_mem_vars_stmt (s:statement) : identifier_set SymbolTag ≝
41match s with
42[ Sskip ⇒ ∅
43| Sassign e1 e2 ⇒ gather_mem_vars_expr e1 ∪
44                  gather_mem_vars_expr e2
45| Scall oe1 e2 es ⇒ match oe1 with [ None ⇒ ∅ | Some e1 ⇒ gather_mem_vars_expr e1 ] ∪
46                    gather_mem_vars_expr e2 ∪
47                    (foldl ?? (λs,e. s ∪ gather_mem_vars_expr e) ∅ es)
48| Ssequence s1 s2 ⇒ gather_mem_vars_stmt s1 ∪
49                    gather_mem_vars_stmt s2
50| Sifthenelse e1 s1 s2 ⇒ gather_mem_vars_expr e1 ∪
51                         gather_mem_vars_stmt s1 ∪
52                         gather_mem_vars_stmt s2
53| Swhile e1 s1 ⇒ gather_mem_vars_expr e1 ∪
54                 gather_mem_vars_stmt s1
55| Sdowhile e1 s1 ⇒ gather_mem_vars_expr e1 ∪
56                   gather_mem_vars_stmt s1
57| Sfor s1 e1 s2 s3 ⇒ gather_mem_vars_stmt s1 ∪
58                     gather_mem_vars_expr e1 ∪
59                     gather_mem_vars_stmt s2 ∪
60                     gather_mem_vars_stmt s3
61| Sbreak ⇒ ∅
62| Scontinue ⇒ ∅
63| Sreturn oe1 ⇒ match oe1 with [ None ⇒ ∅ | Some e1 ⇒ gather_mem_vars_expr e1 ]
64| Sswitch e1 ls ⇒ gather_mem_vars_expr e1 ∪
65                  gather_mem_vars_ls ls
66| Slabel _ s1 ⇒ gather_mem_vars_stmt s1
67| Sgoto _ ⇒ ∅
68| Scost _ s1 ⇒ gather_mem_vars_stmt s1
69]
70and gather_mem_vars_ls (ls:labeled_statements) on ls : identifier_set SymbolTag ≝
71match ls with
72[ LSdefault s1 ⇒ gather_mem_vars_stmt s1
73| LScase _ _ s1 ls1 ⇒ gather_mem_vars_stmt s1 ∪
74                      gather_mem_vars_ls ls1
75].
76
77(* Defines where a variable should be allocated. *)
78inductive var_type : Type[0] ≝
79| Global : region → var_type  (* A global, allocated statically in a given region (which one ???)  *)
80| Stack  : nat → var_type     (* On the stack, at a given height *)
81| Local  : var_type           (* Locally (hopefully, in a register) *)
82.
83
84(* A map associating each variable identifier to its allocation mode and its type. *)
85definition var_types ≝ identifier_map SymbolTag (var_type × type).
86
87axiom UndeclaredIdentifier : String.
88
89definition lookup' ≝
90λvars:var_types.λid. opt_to_res … [MSG UndeclaredIdentifier; CTX ? id] (lookup ?? vars id).
91
92(* Assert that an identifier is a local variable with the given typ. *)
93definition local_id : var_types → ident → typ → Prop ≝
94λvars,id,t. match lookup' vars id with [ OK vt ⇒ match (\fst vt) with [ Global _ ⇒ False | _ ⇒ t = typ_of_type (\snd vt) ] | _ ⇒ False ].
95
96(* Note that the semantics allows locals to shadow globals.
97   Parameters start out as locals, but get stack allocated if their address
98   is taken.  We will add code to store them if that's the case.
99 *)
100
101(* Some kind of data is never allocated in registers, even if it fits, typically structured data. *)
102definition always_alloc : type → bool ≝
103λt. match t with
104[ Tarray _ _ _ ⇒ true
105| Tstruct _ _ ⇒ true
106| Tunion _ _ ⇒ true
107| _ ⇒ false
108].
109
110(* This builds a [var_types] map characterizing the allocation mode, of variables,
111 * and it returns a stack usage for the function (in bytes, according to [sizeof]) *)
112definition characterise_vars : list (ident×region×type) → function → var_types × nat ≝
113λglobals, f.
114  (* globals are added into a map, with var_type Global, region π_2(idrt) and type π_3(idrt) *)
115  let m ≝ foldr ?? (λidrt,m. add ?? m (\fst (\fst idrt)) 〈Global (\snd (\fst idrt)), \snd idrt〉) (empty_map ??) globals in
116  (* variables in the body of the function are gathered in [mem_vars] *)
117  let mem_vars ≝ gather_mem_vars_stmt (fn_body f) in
118  (* iterate on the parameters and local variables of the function, with a tuple (map, stack_high) as an accumulator *)
119  let 〈m,stacksize〉 ≝ foldr ?? (λv,ms.
120    let 〈m,stack_high〉 ≝ ms in
121    let 〈id,ty〉 ≝ v in         
122    let 〈c,stack_high〉 ≝
123      (* if the (local, parameter) variable is of a compound type OR if its adress is taken, we allocate it on the stack. *)
124      if always_alloc ty ∨ id ∈ mem_vars then
125        〈Stack stack_high,stack_high + sizeof ty〉
126      else
127        〈Local, stack_high〉
128    in
129      〈add ?? m id 〈c, ty〉, stack_high〉) 〈m,0〉 (fn_params f @ fn_vars f) in
130  〈m,stacksize〉.
131
132(* A local variable id' status is not modified by the removal of a global variable id : id' is still local *)
133lemma local_id_add_global : ∀vars,id,r,t,id',t'.
134  local_id (add ?? vars id 〈Global r, t〉) id' t' → local_id vars id' t'.
135#var #id #r #t #id' #t'
136whd in ⊢ (% → ?); whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?] → ?);
137cases (identifier_eq ? id id')
138[ #E >E >lookup_add_hit whd in ⊢ (% → ?); *
139| #NE >lookup_add_miss /2/
140] qed.
141
142(* If I add a variable id ≠ id', then id' is still local *)
143lemma local_id_add_miss : ∀vars,id,vt,id',t'.
144  id ≠ id' → local_id (add ?? vars id vt) id' t' → local_id vars id' t'.
145#vars #id #vt #id' #t' #NE
146whd in ⊢ (% → %);
147whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ] → match % with [ _ ⇒ ? | _ ⇒ ? ]);
148>lookup_add_miss
149[ #H @H | /2/ ]
150qed.
151
152(* After characterise_vars, a variable in the resulting map is either a global or a "local"(register or stack allocated) *)
153lemma characterise_vars_src : ∀gl,f,vars,n.
154  characterise_vars gl f = 〈vars,n〉 →
155  ∀id. present ?? vars id →
156   (∃r,ty. lookup' vars id = OK ? 〈Global r,ty〉 ∧ Exists ? (λx.x = 〈〈id,r〉,ty〉) gl) ∨
157   ∃t.local_id vars id t.
158#globals #f
159whd in ⊢ (∀_.∀_.??%? → ?);
160elim (fn_params f @ fn_vars f)
161[ #vars #n whd in ⊢ (??%? → ?); #E destruct #i #H %1
162  elim globals in H ⊢ %;
163  [ normalize * #H cases (H (refl ??))
164  | * * #id #rg #ty #tl #IH #H
165    cases (identifier_eq ? i id)
166    [ #E <E %{rg} %{ty} % [ whd in ⊢ (??%?); >lookup_add_hit // | %1 // ]
167    | #NE cases (IH ?)
168      [ #rg' * #ty' * #H1 #H2 %{rg'} %{ty'} %
169        [ whd in ⊢ (??%?); >lookup_add_miss  [ @H1 | @NE ]
170        | %2 @H2
171        ]
172      | whd in H ⊢ %; >lookup_add_miss in H; //
173      ]
174    ]
175  ]
176| * #id #ty #tl #IH #vars #n whd in ⊢ (??(match % with [ _ ⇒ ? ])? → ?); #E #i
177  #H >(contract_pair var_types nat ?) in E;
178  whd in ⊢ (??(match ? with [ _ ⇒ (λ_.λ_.%) ])? → ?);
179  cases (always_alloc ty ∨ id ∈ ?) whd in ⊢ (??(match ? with [ _ ⇒ (λ_.λ_.%) ])? → ?);
180  #H' lapply (extract_pair ???????? H') -H' * #m0 * #n0 * #EQ #EQ2
181  cases (identifier_eq ? i id)
182  [ 1,3: #E' <E' in EQ2:%; #EQ2 %2 %{(typ_of_type ty)}
183         destruct (EQ2) whd whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]);
184         >lookup_add_hit @refl
185  | *: #NE cases (IH m0 n0 ? i ?)
186    [ 1,5: * #rg' * #ty' * #H1 #H2 %1 %{rg'} %{ty'} % //
187           destruct (EQ2) whd in ⊢ (??%?); >lookup_add_miss try @NE @H1
188    | 2,6: * #t #H1 %2 %{t} destruct (EQ2) whd whd in ⊢ (match % with [_ ⇒ ?|_ ⇒ ?]);
189           >lookup_add_miss //
190    | 3,7: <EQ @refl
191    | *: destruct (EQ2) whd in H; >lookup_add_miss in H; //
192    ]
193  ]
194] qed.
195
196(* A local variable in a function is either a parameter or a "local" (:=register or stack alloc'd)
197 * variable, with the right type *)
198lemma characterise_vars_all : ∀l,f,vars,n.
199  characterise_vars l f = 〈vars,n〉 →
200  ∀i,t. local_id vars i t →
201        Exists ? (λx.\fst x = i ∧ typ_of_type (\snd x) = t) (fn_params f @ fn_vars f).
202#globals #f
203whd in ⊢ (∀_.∀_.??%? → ?);
204elim (fn_params f @ fn_vars f)
205[ #vars #n whd in ⊢ (??%? → ?); #E destruct #i #t #H @False_ind
206  elim globals in H;
207  [ normalize //
208  | * * #id #rg #t #tl #IH whd in ⊢ (?%?? → ?); #H @IH @(local_id_add_global … H)
209  ]
210| * #id #ty #tl #IH #vars #n whd in ⊢ (??(match % with [ _ ⇒ ? ])? → ?); #E #i #t
211
212  #H >(contract_pair var_types nat ?) in E;
213  whd in ⊢ (??(match ? with [ _ ⇒ (λ_.λ_.%) ])? → ?);
214  cases (always_alloc ty ∨ id ∈ ?) whd in ⊢ (??(match ? with [ _ ⇒ (λ_.λ_.%) ])? → ?);
215  #H' lapply (extract_pair ???????? H') -H' * #m0 * #n0 * #EQ #EQ2
216
217  cases (identifier_eq ? id i)
218  [ 1,3: #E' >E' in EQ2:%; #EQ2 % %
219    [ 1,3: @refl
220    | *: destruct (EQ2) change with (add ?????) in H:(?%??);
221      whd in H; whd in H:(match % with [_ ⇒ ?|_ ⇒ ?]); >lookup_add_hit in H;
222      whd in ⊢ (% → ?); #E'' >E'' @refl
223    ]
224  | *: #NE %2 @(IH m0 n0)
225    [ 1,3: @sym_eq whd in ⊢ (???(match ?????% with [ _ ⇒ ? ])); >contract_pair @EQ
226    | 2,4: destruct (EQ2) @(local_id_add_miss … H) @NE
227    ]
228  ]
229] qed.
230
231(* The map generated by characterise_vars is "correct" wrt the fresh ident generator of tag [u],
232   i.e. by generating fresh idents with u, we risk no collision with the idents in the map domain. *)
233lemma characterise_vars_fresh : ∀gl,f,vars,n,u.
234  characterise_vars gl f = 〈vars,n〉 →              (* If we generate a map ... *)
235  globals_fresh_for_univ ? gl u →                  (* and the globals are out of the idents generated by u *)
236  fn_fresh_for_univ f u →                          (* and the variables of the function f are cool with u too ... *)
237  fresh_map_for_univ … vars u.                     (* then there won't be collisions between the map and idents made from u *)
238#gl #f #vars #n #u #CH #GL #FN
239#id #H
240cases (characterise_vars_src … CH … H)
241[ * #rg * #ty * #H1 #H2
242  cases (Exists_All … H2 GL) * * #id' #rg' #ty' * #E #H destruct //
243| * #t #H lapply (characterise_vars_all … CH id t H) #EX
244  cases (Exists_All … EX FN) * #id' #ty' * * #E1 #E2 #H' -H destruct //
245] qed.
246
247include "Cminor/syntax.ma".
248include "common/Errors.ma".
249
250alias id "CMexpr" = "cic:/matita/cerco/Cminor/syntax/expr.ind(1,0,0)".
251
252axiom BadlyTypedAccess : String.
253axiom BadLvalue : String.
254axiom MissingField : String.
255
256(* type_should_eq enforces that two types are equal and eliminates this equality by
257   transporting P ty1 to P ty2. If ty1 != ty2, then Error *)
258definition type_should_eq : ∀ty1,ty2. ∀P:type → Type[0]. P ty1 → res (P ty2) ≝
259λty1,ty2,P,p.
260  do E ← assert_type_eq ty1 ty2;
261  OK ? (match E return λx.λ_. P ty1 → P x with [ refl ⇒ λp.p ] p). 
262
263(* same gig for regions *)
264definition region_should_eq : ∀r1,r2. ∀P:region → Type[0]. P r1 → res (P r2).
265* * #P #p try @(OK ? p) @(Error ? (msg TypeMismatch))
266qed.
267
268(* same gig for AST typs *)
269definition typ_should_eq : ∀ty1,ty2. ∀P:typ → Type[0]. P ty1 → res (P ty2).
270* [ #sz1 #sg1 | #r1 | #sz1 ]
271* [ 1,5,9: | *: #a #b #c try #d @(Error ? (msg TypeMismatch)) ]
272[ *; cases sz1 [ 1,5,9: | *: #a #b #c @(Error ? (msg TypeMismatch)) ]
273  *; cases sg1 #P #p try @(OK ? p) @(Error ? (msg TypeMismatch))
274| *; #P #p @(region_should_eq r1 ?? p)
275| *; cases sz1 #P #p try @(OK ? p) @(Error ? (msg TypeMismatch))
276] qed.
277
278alias id "CLunop" = "cic:/matita/cerco/Clight/Csyntax/unary_operation.ind(1,0,0)".
279alias id "CMunop" = "cic:/matita/cerco/common/FrontEndOps/unary_operation.ind(1,0,0)".
280
281(* XXX: For some reason matita refuses to pick the right one unless forced. *)
282alias id "CMnotbool" = "cic:/matita/cerco/common/FrontEndOps/unary_operation.con(0,3,0)".
283
284(* Translates a Clight unary operation into a Cminor one, while checking
285 * that the domain and codomain types are consistent. *)
286definition translate_unop : ∀t,t':typ. CLunop → res (CMunop t t') ≝
287λt,t'.λop:CLunop.
288  match op with
289  [ Onotbool ⇒
290      match t return λt. res (CMunop t t') with
291      [ ASTint sz sg ⇒
292          match t' return λt'. res (CMunop ? t') with
293          [ ASTint sz' sg' ⇒ OK ? (CMnotbool ????)
294          | _ ⇒ Error ? (msg TypeMismatch)
295          ]
296      | ASTptr r ⇒
297          match t' return λt'. res (CMunop ? t') with
298          [ ASTint sz' sg' ⇒ OK ? (CMnotbool ????)
299          | _ ⇒ Error ? (msg TypeMismatch)
300          ]
301      | _ ⇒ Error ? (msg TypeMismatch)
302      ]
303  | Onotint ⇒
304      match t' return λt'. res (CMunop t t') with
305      [ ASTint sz sg ⇒ typ_should_eq ?? (λt. CMunop t (ASTint ??)) (Onotint sz sg)
306      | _ ⇒ Error ? (msg TypeMismatch)
307      ]
308  | Oneg ⇒
309      match t' return λt'. res (CMunop t t') with
310      [ ASTint sz sg ⇒ typ_should_eq ?? (λt.CMunop t (ASTint ??)) (Onegint sz sg)
311      | ASTfloat sz ⇒ typ_should_eq ?? (λt.CMunop t (ASTfloat sz)) (Onegf sz)
312      | _ ⇒ Error ? (msg TypeMismatch)
313      ]
314  ]. @I qed.
315
316(* Translates a Clight addition into a Cminor one. Four cases to consider :
317  - integer/integer add
318  - fp/fp add
319  - pointer/integer
320  - integer/pointer.
321  Consistency of the type is enforced by explicit checks.
322*)
323
324(* First, how to get rid of a abstract-away pointer or array type *)
325definition fix_ptr_type : ∀r,ty,n. expr (typ_of_type (ptr_type r ty n)) → expr (ASTptr r) ≝
326λr,ty,n,e. e⌈expr (typ_of_type (ptr_type r ty n)) ↦ expr (ASTptr r)⌉.
327cases n //
328qed.
329
330definition translate_add ≝
331λty1,ty2,ty'.
332let ty1' ≝ typ_of_type ty1 in
333let ty2' ≝ typ_of_type ty2 in
334match classify_add ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
335[ add_case_ii sz sg ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Oadd ??) e1 e2)
336| add_case_ff sz ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Oaddf sz) e1 e2)
337(* XXX we cast up to I16 Signed to prevent overflow, but often we could use I8 *)
338| add_case_pi n r ty sz sg ⇒
339    λe1,e2. typ_should_eq ??? (Op2 ??? (Oaddp I16 r) (fix_ptr_type … e1) (Op2 ??? (Omul I16 Signed) (Op1 ?? (Ocastint sz sg I16 Signed) e2) (Cst ? (Ointconst I16 Signed (repr ? (sizeof ty))))))
340| add_case_ip n sz sg r ty ⇒
341    λe1,e2. typ_should_eq ??? (Op2 ??? (Oaddp I16 r) (fix_ptr_type … e2) (Op2 ??? (Omul I16 Signed) (Op1 ?? (Ocastint sz sg I16 Signed) e1) (Cst ? (Ointconst I16 Signed (repr ? (sizeof ty))))))
342| add_default _ _ ⇒ λe1,e2. Error ? (msg TypeMismatch)
343].
344
345
346definition translate_sub ≝
347λty1,ty2,ty'.
348let ty1' ≝ typ_of_type ty1 in
349let ty2' ≝ typ_of_type ty2 in
350match classify_sub ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
351[ sub_case_ii sz sg ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Osub ??) e1 e2)
352| sub_case_ff sz ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Osubf sz) e1 e2)
353(* XXX could optimise cast as above *)
354| sub_case_pi n r ty sz sg ⇒
355    λe1,e2. typ_should_eq ??? (Op2 ??? (Osubpi I16 r) (fix_ptr_type … e1) (Op2 ??? (Omul I16 Signed) (Op1 ?? (Ocastint sz sg I16 Signed) e2) (Cst ? (Ointconst I16 Signed (repr ? (sizeof ty))))))
356(* XXX check in detail? *)
357| sub_case_pp n1 n2 r1 ty1 r2 ty2 ⇒
358    λe1,e2. match ty' return λty'. res (CMexpr (typ_of_type ty')) with
359    [ Tint sz sg ⇒ OK ? (Op1 ?? (Ocastint I16 Signed sz sg) (Op2 ??? (Odiv I16) (Op2 ??? (Osubpp I16 r1 r2) (fix_ptr_type … e1) (fix_ptr_type ??? e2)) (Cst ? (Ointconst I16 Signed (repr ? (sizeof ty2))))))
360    | _ ⇒ Error ? (msg TypeMismatch)
361    ]
362| sub_default _ _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
363].
364
365definition translate_mul ≝
366λty1,ty2,ty'.
367let ty1' ≝ typ_of_type ty1 in
368let ty2' ≝ typ_of_type ty2 in
369match classify_aop ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
370[ aop_case_ii sz sg ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omul …) e1 e2)
371| aop_case_ff sz ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omulf …) e1 e2)
372| aop_default _ _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
373].
374
375definition translate_div ≝
376λty1,ty2,ty'.
377let ty1' ≝ typ_of_type ty1 in
378let ty2' ≝ typ_of_type ty2 in
379match classify_aop ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
380[ aop_case_ii sz sg ⇒
381    match sg return λsg. CMexpr (ASTint sz sg) → CMexpr (ASTint sz sg) → res (CMexpr (typ_of_type ty')) with
382    [ Unsigned ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Odivu …) e1 e2)
383    | Signed ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Odiv …) e1 e2)
384    ]
385| aop_case_ff sz ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Odivf …) e1 e2)
386| aop_default _ _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
387].
388
389definition translate_mod ≝
390λty1,ty2,ty'.
391let ty1' ≝ typ_of_type ty1 in
392let ty2' ≝ typ_of_type ty2 in
393match classify_aop ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
394[ aop_case_ii sz sg ⇒
395    match sg return λsg. CMexpr (ASTint sz sg) → CMexpr (ASTint sz sg) → res (CMexpr (typ_of_type ty')) with
396    [ Unsigned ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omodu …) e1 e2)
397    | Signed ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omod …) e1 e2)
398    ]
399(* no float case *)
400| _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
401].
402
403definition translate_shr ≝
404λty1,ty2,ty'.
405let ty1' ≝ typ_of_type ty1 in
406let ty2' ≝ typ_of_type ty2 in
407match classify_aop ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
408[ aop_case_ii sz sg ⇒
409    match sg return λsg. CMexpr (ASTint sz sg) → CMexpr (ASTint sz sg) → res (CMexpr (typ_of_type ty')) with
410    [ Unsigned ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omodu …) e1 e2)
411    | Signed ⇒ λe1,e2. typ_should_eq ??? (Op2 ??? (Omod …) e1 e2)
412    ]
413(* no float case *)
414| _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
415].
416
417definition complete_cmp : ∀ty'. CMexpr (ASTint I8 Unsigned) → res (CMexpr (typ_of_type ty')) ≝
418λty',e.
419match ty' return λty'. res (CMexpr (typ_of_type ty')) with
420[ Tint sz sg ⇒ OK ? (Op1 ?? (Ocastint I8 Unsigned sz sg) e)
421| _ ⇒ Error ? (msg TypeMismatch)
422].
423
424definition translate_cmp ≝
425λc,ty1,ty2,ty'.
426let ty1' ≝ typ_of_type ty1 in
427let ty2' ≝ typ_of_type ty2 in
428match classify_cmp ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
429[ cmp_case_ii sz sg ⇒
430    match sg return λsg. CMexpr (ASTint sz sg) → CMexpr (ASTint sz sg) → res (CMexpr (typ_of_type ty')) with
431    [ Unsigned ⇒ λe1,e2. complete_cmp ty' (Op2 ??? (Ocmpu … c) e1 e2)
432    | Signed ⇒ λe1,e2. complete_cmp ty' (Op2 ??? (Ocmp … c) e1 e2)
433    ]
434| cmp_case_pp n r ty ⇒
435    λe1,e2. complete_cmp ty' (Op2 ??? (Ocmpp … c) (fix_ptr_type … e1) (fix_ptr_type … e2))
436| cmp_case_ff sz ⇒ λe1,e2. complete_cmp ty' (Op2 ??? (Ocmpf … c) e1 e2)
437| cmp_default _ _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
438].
439
440definition translate_misc_aop ≝
441λty1,ty2,ty',op.
442let ty1' ≝ typ_of_type ty1 in
443let ty2' ≝ typ_of_type ty2 in
444match classify_aop ty1 ty2 return λty1,ty2.λ_. CMexpr (typ_of_type ty1) → CMexpr (typ_of_type ty2) → res (CMexpr (typ_of_type ty')) with
445[ aop_case_ii sz sg ⇒ λe1,e2. typ_should_eq ??? (Op2 ?? (ASTint sz sg) (op sz sg) e1 e2)
446| _ ⇒ λ_.λ_. Error ? (msg TypeMismatch)
447].
448
449definition translate_binop : binary_operation → type → CMexpr ? → type → CMexpr ? → type → res (CMexpr ?) ≝
450λop,ty1,e1,ty2,e2,ty.
451let ty' ≝ typ_of_type ty in
452match op with
453[ Oadd ⇒ translate_add ty1 ty2 ty e1 e2
454| Osub ⇒ translate_sub ty1 ty2 ty e1 e2
455| Omul ⇒ translate_mul ty1 ty2 ty e1 e2
456| Omod ⇒ translate_mod ty1 ty2 ty e1 e2
457| Odiv ⇒ translate_div ty1 ty2 ty e1 e2
458| Oand ⇒ translate_misc_aop ty1 ty2 ty Oand e1 e2
459| Oor  ⇒ translate_misc_aop ty1 ty2 ty Oor e1 e2
460| Oxor ⇒ translate_misc_aop ty1 ty2 ty Oxor e1 e2
461| Oshl ⇒ translate_misc_aop ty1 ty2 ty Oshl e1 e2
462| Oshr ⇒ translate_shr ty1 ty2 ty e1 e2
463| Oeq ⇒ translate_cmp Ceq ty1 ty2 ty e1 e2
464| One ⇒ translate_cmp Cne ty1 ty2 ty e1 e2
465| Olt ⇒ translate_cmp Clt ty1 ty2 ty e1 e2
466| Ogt ⇒ translate_cmp Cgt ty1 ty2 ty e1 e2
467| Ole ⇒ translate_cmp Cle ty1 ty2 ty e1 e2
468| Oge ⇒ translate_cmp Cge ty1 ty2 ty e1 e2
469].
470
471lemma typ_equals : ∀t1,t2. ∀P:∀t. expr t → Prop. ∀v1,v2.
472  typ_should_eq t1 t2 expr v1 = OK ? v2 →
473  P t1 v1 →
474  P t2 v2.
475* [ * * | * | * ]
476* try * try *
477#P #v1 #v2 #E whd in E:(??%?); destruct
478#H @H
479qed.
480
481lemma unfix_ptr_type : ∀r,ty,n,e.∀P:∀t. expr t → Prop.
482  P (typ_of_type (ptr_type r ty n)) e →
483  P (ASTptr r) (fix_ptr_type r ty n e).
484#r #ty * [ 2: #n ] #e #P #H @H
485qed.
486
487(* Recall that [expr_vars], defined in Cminor/Syntax.ma, asserts a predicate on
488  all the variables of a program. [translate_binop_vars], given
489  a predicate verified for all variables of subexprs e1 and e2, produces
490  a proof that all variables of [translate_binop op _ e1 _ e2 _] satisfy this
491  predicate. *)
492
493lemma translate_binop_vars : ∀P,op,ty1,e1,ty2,e2,ty,e.
494  expr_vars ? e1 P →
495  expr_vars ? e2 P →
496  translate_binop op ty1 e1 ty2 e2 ty = OK ? e →
497  expr_vars ? e P.
498#P * #ty1 #e1 #ty2 #e2 #ty #e #H1 #H2
499whd in ⊢ (??%? → ?);
500[ inversion (classify_add ty1 ty2) in ⊢ ?;
501  [ #sz #sg #E1 #E2 #E3 destruct >E3 #E4 -E3 change with (typ_should_eq ???? = OK ??) in E4;
502    @(typ_equals … E4) % //
503  | #sz #E1 #E2 #E3 destruct >E3 #E4
504    @(typ_equals … E4) % //
505  | #n #r #ty0 #sz #sg #E1 #E2 #E3 destruct >E3 #E4
506    @(typ_equals … E4) -E4 -E3 % [ @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H1)| % // ]
507  | #n #sz #sg #r #ty0 #E1 #E2 #E3 destruct >E3 #E4
508    @(typ_equals … E4) % [ @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H2)| % // ]
509  | #ty1' #ty2' #E1 #E2 #E3 destruct >E3 #E4 whd in E4:(??%?); destruct
510  ]
511| inversion (classify_sub ty1 ty2) in ⊢ ?;
512  [ #sz #sg #E1 #E2 #E3 destruct >E3 #E4
513    @(typ_equals … E4) % //
514  | #sz #E1 #E2 #E3 destruct >E3 #E4
515    @(typ_equals … E4) % //
516  | #n #r #ty0 #sz #sg #E1 #E2 #E3 destruct >E3 #E4
517    @(typ_equals … E4) % [ @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H1)| % // ]
518  | #n1 #n2 #r1 #ty1' #r2 #ty2' #E1 #E2 #E3 destruct >E3
519    whd in ⊢ (??%? → ?); cases ty in e ⊢ %;
520    [ 2: #sz #sg #e #E4 | 4: #r #ty #e #E4 | 5: #r #ty' #n' #e #E4
521    | *: normalize #X1 #X2 try #X3 try #X4 destruct
522    ] whd in E4:(??%?); destruct % // %
523    [ @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H1) | @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H2) ]
524  | #ty1' #ty2' #E1 #E2 #E3 destruct >E3 #E4 whd in E4:(??%?); destruct
525  ]
526| 3,4,5,6,7,8,9,10: inversion (classify_aop ty1 ty2) in ⊢ ?;
527  (* Note that some cases require a split on signedness of integer type. *)
528  [ 1,4,7,10,13,16,19,22: #sz * #E1 #E2 #E3 destruct >E3 #E4
529    @(typ_equals … E4) % //
530  | 2,5: #sz #E1 #E2 #E3 destruct >E3 #E4
531    @(typ_equals … E4) % //
532  | 8,11,14,17,20,23: #sz #E1 #E2 #E3 destruct >E3 #E4 whd in E4:(??%?); destruct
533  | 3,6,9,12,15,18,21,24: #ty1' #ty2' #E1 #E2 #E3 destruct >E3 #E4 whd in E4:(??%?); destruct
534  ]
535| 11,12,13,14,15,16: inversion (classify_cmp ty1 ty2) in ⊢ ?;
536  [ 1,5,9,13,17,21: #sz * #E1 #E2 #E3 destruct >E3
537  | 2,6,10,14,18,22: #n #r #ty' #E1 #E2 #E3 destruct >E3
538  | 3,7,11,15,19,23: #sz #E1 #E2 #E3 destruct >E3
539  | *: #ty1' #ty2' #E1 #E2 #E3 destruct >E3 #E4 whd in E4:(??%?); @⊥ destruct
540  ] whd in ⊢ (??%? → ?); cases ty in e ⊢ %;
541  try (normalize #X1 #X2 try #X3 try #X4 try #X5 destruct #FAIL)
542  #sz #sg #e #E4
543  whd in E4:(??%?); destruct %
544  [ 25,27,29,31,33,35: @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H1)
545  | 26,28,30,32,34,36: @(unfix_ptr_type ???? (λt,e. expr_vars t e P) H2)
546  | *: //
547  ]
548] qed.
549
550
551(* We'll need to implement proper translation of pointers if we really do memory
552   spaces. *)
553(* This function performs leibniz-style subst if r1 = r2, and fails otherwise. *)
554definition check_region : ∀r1:region. ∀r2:region. ∀P:region → Type[0]. P r1 → res (P r2) ≝
555λr1,r2,P.
556  match r1 return λx.P x → res (P r2) with
557  [ Any ⇒   match r2 return λx.P Any → res (P x) with [ Any ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
558  | Data ⇒  match r2 return λx.P Data → res (P x) with [ Data ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
559  | IData ⇒ match r2 return λx.P IData → res (P x) with [ IData ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
560  | PData ⇒ match r2 return λx.P PData → res (P x) with [ PData ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
561  | XData ⇒ match r2 return λx.P XData → res (P x) with [ XData ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
562  | Code ⇒  match r2 return λx.P Code → res (P x) with [ Code ⇒ OK ? | _ ⇒ λ_.Error ? (msg TypeMismatch) ]
563  ].
564
565(* Simple application of [check_region] to translate between terms. *)
566definition translate_ptr : ∀P,r1,r2. (Σe:CMexpr (ASTptr r1). expr_vars ? e P) → res (Σe':CMexpr (ASTptr r2).expr_vars ? e' P) ≝
567λP,r1,r2,e. check_region r1 r2 (λr.Σe:CMexpr (ASTptr r).expr_vars ? e P) e.
568
569axiom FIXME : String.
570
571(* Given a source and target type, translate an expession of type source to type target *)
572definition translate_cast : ∀P. ∀ty1:type.∀ty2:type. (Σe:CMexpr (typ_of_type ty1). expr_vars ? e P) → res (Σe':CMexpr (typ_of_type ty2). expr_vars ? e' P) ≝
573λP,ty1,ty2.
574match ty1 return λx.(Σe:CMexpr (typ_of_type x). expr_vars ? e P) → ? with
575[ Tint sz1 sg1 ⇒ λe.
576    match ty2 return λx.res (Σe':CMexpr (typ_of_type x).expr_vars ? e' P) with
577    [ Tint sz2 sg2 ⇒ OK ? (Op1 ?? (Ocastint ? sg1 sz2 ?) e)
578    | Tfloat sz2 ⇒ OK ? (Op1 ?? (match sg1 with [ Unsigned ⇒ Ofloatofintu ?? | _ ⇒ Ofloatofint ??]) e)
579    | Tpointer r _ ⇒ OK ? (Op1 ?? (Optrofint ?? r) e)
580    | Tarray r _ _ ⇒ OK ? (Op1 ?? (Optrofint ?? r) e)
581    | _ ⇒ Error ? (msg TypeMismatch)
582    ]
583| Tfloat sz1 ⇒ λe.
584    match ty2 return λx.res (Σe':CMexpr (typ_of_type x).expr_vars ? e' P) with
585    [ Tint sz2 sg2 ⇒ OK ? «Op1 ?? (match sg2 with [ Unsigned ⇒ Ointuoffloat ? sz2 | _ ⇒ Ointoffloat ? sz2 ]) e, ?»
586    | Tfloat sz2 ⇒ Error ? (msg FIXME) (* OK ? «Op1 ?? (Oid ?) e, ?» (* FIXME *) *)
587    | _ ⇒ Error ? (msg TypeMismatch)
588    ]
589| Tpointer r1 _ ⇒ λe. (* will need changed for memory regions *)
590    match ty2 return λx.res (Σe':CMexpr (typ_of_type x). expr_vars ? e' P) with
591    [ Tint sz2 sg2 ⇒ OK ? «Op1 ?? (Ointofptr sz2 ??) e, ?»
592    | Tarray r2 _ _ ⇒ translate_ptr ? r1 r2 e
593    | Tpointer r2 _ ⇒ translate_ptr ? r1 r2 e
594    | _ ⇒ Error ? (msg TypeMismatch)
595    ]
596| Tarray r1 _ _ ⇒ λe. (* will need changed for memory regions *)
597    match ty2 return λx.res (Σe':CMexpr (typ_of_type x).expr_vars ? e' P) with
598    [ Tint sz2 sg2 ⇒ OK ? «Op1 (ASTptr r1) (ASTint sz2 sg2) (Ointofptr sz2 ??) e, ?»
599    | Tarray r2 _ _ ⇒ translate_ptr ? r1 r2 e
600    | Tpointer r2 _ ⇒ translate_ptr ? r1 r2 e
601    | _ ⇒ Error ? (msg TypeMismatch)
602    ]
603| _ ⇒ λ_. Error ? (msg TypeMismatch)
604]. whd normalize nodelta @pi2
605qed.
606
607(* Translate Clight exprs into Cminor ones.
608  Arguments :
609  - vars:var_types, an environment mapping each variable to a couple (allocation mode, type)
610  - e:expr, the expression to be converted
611  Result : res (Σe':CMexpr (typ_of_type (typeof e)). expr_vars ? e' (local_id vars))
612  that is, either
613  . an error
614  . an expression e', matching the type of e, such that e' respect the property that all variables
615    in it are not global. In effect, [translate_expr] will replace global variables by constant symbols.
616*)
617let rec translate_expr (vars:var_types) (e:expr) on e : res (Σe':CMexpr (typ_of_type (typeof e)). expr_vars ? e' (local_id vars)) ≝
618match e return λe. res (Σe':CMexpr (typ_of_type (typeof e)). expr_vars ? e' (local_id vars)) with
619[ Expr ed ty ⇒
620  match ed with
621  [ Econst_int sz i ⇒
622      match ty return λty. res (Σe':CMexpr (typ_of_type ty).  expr_vars ? e' (local_id vars)) with
623      [ Tint sz' sg ⇒ intsize_eq_elim' sz sz' (λsz,sz'. res (Σe':CMexpr (typ_of_type (Tint sz' sg)). expr_vars ? e' (local_id vars)))
624                        (OK ? «Cst ? (Ointconst sz sg i), ?»)
625                        (Error ? (msg TypeMismatch))
626      | _ ⇒ Error ? (msg TypeMismatch)
627      ]
628  | Econst_float f ⇒
629      match ty return λty. res (Σe':CMexpr (typ_of_type ty). ?) with
630      [ Tfloat sz ⇒ OK ? «Cst ? (Ofloatconst sz f), ?»
631      | _ ⇒ Error ? (msg TypeMismatch)
632      ]
633  | Evar id ⇒
634      do 〈c,t〉 as E ← lookup' vars id; (* E is an equality proof of the shape "lookup' vars id = Ok <c,t>" *)
635      match c return λx.? = ? → res (Σe':CMexpr ?. ?) with
636      [ Global r ⇒ λ_.
637          (* We are accessing a global variable in an expression. Its Cminor counterpart also depends on
638             its access mode:
639             - By_value q, where q is a memory chunk specification (whitch should match the type of the global)
640             - By_reference, and we only take the adress of the variable
641             - By_nothing : error
642           *)
643          match access_mode ty return λt.λ_. res (Σe':CMexpr t. expr_vars ? e' (local_id vars)) with
644          [ By_value t ⇒ OK ? «Mem t r (Cst ? (Oaddrsymbol r id 0)), ?» (* Mem is "load" in compcert *)
645          | By_reference r ⇒ OK ? «Cst ? (Oaddrsymbol r id 0), ?»
646          | By_nothing _ ⇒ Error ? [MSG BadlyTypedAccess; CTX ? id]
647          ]
648      | Stack n ⇒ λE.
649          (* We have decided that the variable should be allocated on the stack,
650           * because its adress was taken somewhere or becauste it's a structured data. *)
651          match access_mode ty return λt.λ_. res (Σe':CMexpr t. expr_vars ? e' (local_id vars)) with
652          [ By_value t ⇒ OK ? «Mem t Any (Cst ? (Oaddrstack n)), ?»
653          | By_reference r ⇒ match r return λr. res (Σe':CMexpr (ASTptr r). ?) with
654                             [ Any ⇒ OK ? «Cst ? (Oaddrstack n), ?»
655                             | _ ⇒ Error  ? [MSG BadlyTypedAccess; CTX ? id]
656                             ]
657          | By_nothing _ ⇒ Error ? [MSG BadlyTypedAccess; CTX ? id]
658          ]
659          (* This is a local variable. Keep it as an identifier in the Cminor code, ensuring that the type of the original expr and of ty match. *)
660      | Local ⇒ λE. type_should_eq t ty (λt.Σe':CMexpr (typ_of_type t).expr_vars (typ_of_type t) e' (local_id vars))  («Id (typ_of_type t) id, ?»)
661      ] E
662  | Ederef e1 ⇒
663      do e1' ← translate_expr vars e1;
664      (* According to the way the data pointed to by e1 is accessed, the generated Cminor code will vary.
665        - if e1 is a kind of int* ptr, then we load ("Mem") the ptr returned by e1
666        - if e1 is a struct* or a function ptr, then we acess by reference, in which case we :
667           1) check the consistency of the regions in the type of e1 and in the access mode of its type
668           2) return directly the converted CMinor expression "as is" (TODO : what is the strange notation with the ceil function and the mapsto ?)
669      *)
670      match typ_of_type (typeof e1) return λx.(Σz:CMexpr x.expr_vars ? z (local_id vars)) → ? with
671      [ ASTptr r ⇒ λe1'.
672          match access_mode ty return λt.λ_. res (Σe':CMexpr t. expr_vars ? e' (local_id vars)) with
673          [ By_value t ⇒ OK ? «Mem t ? (pi1 … e1'), ?»
674          | By_reference r' ⇒ region_should_eq r r' ? e1'
675          | By_nothing _ ⇒ Error ? (msg BadlyTypedAccess)
676          ]
677      | _ ⇒ λ_. Error ? (msg TypeMismatch)
678      ] e1'
679  | Eaddrof e1 ⇒
680      do e1' ← translate_addr vars e1;
681      match typ_of_type ty return λx.res (Σz:CMexpr x.?) with
682      [ ASTptr r ⇒
683          match e1' with
684          [ mk_DPair r1 e1' ⇒ region_should_eq r1 r ? e1'
685          ]
686      | _ ⇒ Error ? (msg TypeMismatch)
687      ]
688  | Eunop op e1 ⇒
689      do op' ← translate_unop (typ_of_type (typeof e1)) (typ_of_type ty) op;
690      do e1' ← translate_expr vars e1;
691      OK ? «Op1 ?? op' e1', ?»
692  | Ebinop op e1 e2 ⇒
693      do e1' ← translate_expr vars e1;
694      do e2' ← translate_expr vars e2;
695      do e' as E ← translate_binop op (typeof e1) e1' (typeof e2) e2' ty;
696      OK ? «e', ?»
697  | Ecast ty1 e1 ⇒
698      do e1' ← translate_expr vars e1;
699      do e' ← translate_cast ? (typeof e1) ty1 e1';
700      do e' ← typ_should_eq (typ_of_type ty1) (typ_of_type ty) ? e';
701      OK ? e'
702  | Econdition e1 e2 e3 ⇒
703      do e1' ← translate_expr vars e1;
704      do e2' ← translate_expr vars e2;
705      do e2' ← type_should_eq ? ty (λx.Σe:CMexpr (typ_of_type x).?) e2';
706      do e3' ← translate_expr vars e3;
707      do e3' ← type_should_eq ? ty (λx.Σe:CMexpr (typ_of_type x).?) e3';
708      match typ_of_type (typeof e1) return λx.(Σe1':CMexpr x. expr_vars ? e1' (local_id vars)) → ? with
709      [ ASTint _ _ ⇒ λe1'. OK ? «Cond ??? e1' e2' e3', ?»
710      | _ ⇒ λ_.Error ? (msg TypeMismatch)
711      ] e1'
712  | Eandbool e1 e2 ⇒
713      do e1' ← translate_expr vars e1;
714      do e2' ← translate_expr vars e2;
715      match ty return λty. res (Σe':CMexpr (typ_of_type ty). ?) with
716      [ Tint sz sg ⇒
717        do e2' ← type_should_eq ? (Tint sz sg) (λx.Σe:CMexpr (typ_of_type x).?) e2';
718        match typ_of_type (typeof e1) return λx.(Σe:CMexpr x. expr_vars ? e (local_id vars)) → res ? with
719        [ ASTint _ _ ⇒ λe1'. OK ? «Cond ??? e1' e2' (Cst ? (Ointconst sz sg (zero ?))), ?»
720        | _ ⇒ λ_.Error ? (msg TypeMismatch)
721        ] e1'
722      | _ ⇒ Error ? (msg TypeMismatch)
723      ]
724  | Eorbool e1 e2 ⇒
725      do e1' ← translate_expr vars e1;
726      do e2' ← translate_expr vars e2;
727      match ty return λty. res (Σe':CMexpr (typ_of_type ty). ?) with
728      [ Tint sz sg ⇒
729        do e2' ← type_should_eq ? (Tint sz sg) (λx.Σe:CMexpr (typ_of_type x).?) e2';
730        match typ_of_type (typeof e1) return λx.(Σe:CMexpr x. expr_vars ? e (local_id vars)) → ? with
731        [ ASTint _ _ ⇒ λe1'. OK ? «Cond ??? e1' (Cst ? (Ointconst sz sg (repr ? 1))) e2', ?»
732        | _ ⇒ λ_.Error ? (msg TypeMismatch)
733        ] e1'
734      | _ ⇒ Error ? (msg TypeMismatch)
735      ]
736  | Esizeof ty1 ⇒
737      match ty return λty. res (Σe':CMexpr (typ_of_type ty). ?) with
738      [ Tint sz sg ⇒ OK ? «Cst ? (Ointconst sz sg (repr ? (sizeof ty1))), ?»
739      | _ ⇒ Error ? (msg TypeMismatch)
740      ]
741  | Efield e1 id ⇒
742      match typeof e1 with
743      [ Tstruct _ fl ⇒
744          do e1' ← translate_addr vars e1;
745          match e1' with
746          [ mk_DPair r e1' ⇒
747            do off ← field_offset id fl;
748            match access_mode ty return λt.λ_. res (Σe':CMexpr t. expr_vars ? e' (local_id vars)) with
749            [ By_value t ⇒
750                OK ? «Mem t r (Op2 ? (ASTint I16 Signed (* XXX efficiency? *)) ?
751                                   (Oaddp …) e1' (Cst ? (Ointconst I16 Signed (repr ? off)))),?»
752            | By_reference r' ⇒
753                do e1' ← region_should_eq r r' ? e1';
754                OK ? «Op2 (ASTptr r') (ASTint I16 Signed (* XXX efficiency? *)) (ASTptr r')
755                        (Oaddp …) e1' (Cst ? (Ointconst I16 Signed (repr ? off))),?»
756            | By_nothing _ ⇒ Error ? (msg BadlyTypedAccess)
757            ]
758          ]
759      | Tunion _ _ ⇒
760          do e1' ← translate_addr vars e1;
761          match e1' with
762          [ mk_DPair r e1' ⇒
763            match access_mode ty return λt.λ_. res (Σz:CMexpr t.?) with
764            [ By_value t ⇒ OK ? «Mem t ? e1', ?»
765            | By_reference r' ⇒ region_should_eq r r' ? e1'
766            | By_nothing _ ⇒ Error ? (msg BadlyTypedAccess)
767            ]
768          ]
769      | _ ⇒ Error ? (msg BadlyTypedAccess)
770      ]
771  | Ecost l e1 ⇒
772      do e1' ← translate_expr vars e1;
773      do e' ← OK ? «Ecost ? l e1',?»;
774      typ_should_eq (typ_of_type (typeof e1)) (typ_of_type ty) (λx.Σe:CMexpr x.?) e'
775  ]
776]
777
778(* Translate addr takes an expression e1, and returns a Cminor code computing the address of the result of [e1].   *)
779and translate_addr (vars:var_types) (e:expr) on e : res (𝚺r. Σe':CMexpr (ASTptr r). expr_vars ? e' (local_id vars)) ≝
780match e with
781[ Expr ed _ ⇒
782  match ed with
783  [ Evar id ⇒
784      do 〈c,t〉 ← lookup' vars id;
785      match c return λ_. res (𝚺r.Σz:CMexpr (ASTptr r).?) with
786      [ Global r ⇒ OK ? ❬r, «Cst ? (Oaddrsymbol r id 0), ?»❭
787      | Stack n ⇒ OK ? ❬Any, «Cst ? (Oaddrstack n), ?»❭
788      | Local ⇒ Error ? [MSG BadlyTypedAccess; CTX ? id] (* TODO: could rule out? *)
789      ]
790  | Ederef e1 ⇒
791      do e1' ← translate_expr vars e1;
792      match typ_of_type (typeof e1) return λx. (Σz:CMexpr x.expr_vars ? z (local_id vars)) → res (𝚺r. Σz:CMexpr (ASTptr r). ?) with
793      [ ASTptr r ⇒ λe1'.OK ? ❬r, e1'❭
794      | _ ⇒ λ_.Error ? (msg BadlyTypedAccess)
795      ] e1'
796  | Efield e1 id ⇒
797      match typeof e1 with
798      [ Tstruct _ fl ⇒
799          do e1' ← translate_addr vars e1;
800          do off ← field_offset id fl;
801          match e1' with
802          [ mk_DPair r e1'' ⇒ OK (𝚺r:region.Σe:CMexpr (ASTptr r).?)
803             (❬r, «Op2 (ASTptr r) (ASTint I16 Signed (* FIXME inefficient?*)) (ASTptr r)
804                     (Oaddp I16 r) e1'' (Cst ? (Ointconst I16 Signed (repr ? off))), ?» ❭)
805          ]
806      | Tunion _ _ ⇒ translate_addr vars e1
807      | _ ⇒ Error ? (msg BadlyTypedAccess)
808      ]
809  | _ ⇒ Error ? (msg BadLvalue)
810  ]
811].
812whd try @I
813[ >E whd @refl
814| 2,3: @pi2
815| @(translate_binop_vars … E) @pi2
816| % [ % ] @pi2
817| % [ % @pi2 ] whd @I
818| % [ % [ @pi2 | @I ] | @pi2 ]
819| % [ @pi2 | @I ]
820| % [ @pi2 | @I ]
821| @pi2
822| @pi2
823| % [ @pi2 | @I ]
824] qed.
825
826(* We provide a function to work out how to do an assignment to an lvalue
827   expression.  It is used for both Clight assignments and Clight function call
828   destinations, but doesn't take the value to be assigned so that we can use
829   it to form a single St_store when possible (and avoid introducing an
830   unnecessary temporary variable and assignment).
831   *)
832inductive destination (vars:var_types) : Type[0] ≝
833| IdDest : ∀id,ty. local_id vars id (typ_of_type ty) → destination vars
834| MemDest : ∀r:region. (Σe:CMexpr (ASTptr r).expr_vars ? e (local_id vars)) → destination vars.
835
836(* Let a source Clight expression be assign(e1, e2). First of all, observe that [e1] is a
837  /Clight/ expression, not converted by translate_expr. We thus have to do part of the work
838  of [translate_expr] in this function. [translate_dest] will convert e1
839   into a proper destination for an assignement operation. We proceed by case analysis on e1.
840   - if e1 is a variable [id], then we proceed by case analysis on its allocation mode:
841      - if [id] is allocated locally (in a register), then id becomes directly
842        the target for the assignement, as (IdDest vars id t H), where t is the type
843        of id, and H asserts that id is indeed a local variable.
844      - if [id] is a global variable stored in region [r], then we perform [translate_expr]'s
845        job and return an adress, given as a constant symbol corresponding to [id], with
846        region r and memory chunk specified by the access mode of the rhs type ty2 of [e2].
847      - same thing for stack-allocated variables, except that we don't specify any region.
848   - if e1 is not a variable, we use [translate_addr] to generate a Cminor expression computing
849    the adres of e1
850*)
851definition translate_dest ≝
852λvars,e1.
853  match e1 with
854  [ Expr ed1 ty1 ⇒
855      match ed1 with
856      [ Evar id ⇒
857          do 〈c,t〉 as E ← lookup' vars id;
858          match c return λx.? → ? with
859          [ Local ⇒ λE. OK ? (IdDest vars id t ?)
860          | Global r ⇒ λE. OK ? (MemDest ? r (Cst ? (Oaddrsymbol r id 0)))
861          | Stack n ⇒ λE. OK ? (MemDest ? Any (Cst ? (Oaddrstack n)))
862          ] E
863      | _ ⇒
864          do e1' ← translate_addr vars e1;
865          match e1' with [ mk_DPair r e1' ⇒ OK ? (MemDest ? r e1') ]
866      ]
867  ].
868whd // >E @refl
869qed.
870
871(* [lenv] is the type of maps from Clight labels to Cminor labels. *)
872definition lenv ≝ identifier_map SymbolTag (identifier Label).
873
874axiom MissingLabel : String.
875
876(* Find the Cminor label corresponding to [l] or fail. *)
877definition lookup_label ≝
878λlbls:lenv.λl. opt_to_res … [MSG MissingLabel; CTX ? l] (lookup ?? lbls l).
879
880(* True iff the Cminor label [l] is in the codomain of [lbls] *)
881definition lpresent ≝ λlbls:lenv. λl. ∃l'. lookup_label lbls l' = OK ? l.
882
883(* True iff The Clight label [l] is in the domain of [lbls] *)
884definition label_in_domain ≝ λlbls:lenv. λl. present ?? lbls l.
885
886let rec fresh_list_for_univ (l:list (identifier Label)) (u:universe Label) ≝
887match l with
888[ nil ⇒ True
889| cons elt tl ⇒ fresh_for_univ ? elt u ∧ fresh_list_for_univ tl u].
890
891record labgen : Type[0] ≝ {
892  labuniverse   : universe Label;
893  label_genlist    : list (identifier Label);
894  genlist_is_fresh : fresh_list_for_univ label_genlist labuniverse
895}.
896
897lemma fresh_list_stays_fresh : ∀l,tmp,u,u'. fresh_list_for_univ l u → 〈tmp,u'〉=fresh Label u → fresh_list_for_univ l u'.
898#l elim l
899[ 1: normalize //
900| 2: #hd #tl #Hind #tmp #u #u' #HA #HB
901  whd
902  @conj
903  [ 1: whd in HA ⊢ ?;
904    elim HA #HAleft #HAright
905    @(fresh_remains_fresh ? hd tmp u u') assumption
906  | 2: whd in HA ⊢ ?;
907    elim HA #HAleft #HAright   
908    @Hind //
909  ]
910]
911qed.
912
913definition In ≝ λelttype.λelt.λl.Exists elttype (λx.x=elt) l.   
914
915definition generate_fresh_label :
916 ∀ul. Σlul:(identifier Label × labgen).
917               (And (∀lab. In ? lab (label_genlist ul) → In ? lab (label_genlist (snd … lul)))
918                   (In ? (fst … lul) (label_genlist (snd … lul)))) ≝
919λul.
920let 〈tmp,u〉 as E ≝ fresh ? (labuniverse ul) in
921 «〈tmp, mk_labgen u (tmp::(label_genlist ul)) ?〉, ?».
922[ 1: normalize @conj
923  [ 1: @(fresh_is_fresh ? tmp u (labuniverse ul) ?) assumption
924  | 2: @fresh_list_stays_fresh // ]
925| @conj /2/
926]
927qed.
928
929let rec labels_defined (s:statement) : list ident ≝
930match s with
931[ Ssequence s1 s2 ⇒ labels_defined s1 @ labels_defined s2
932| Sifthenelse _ s1 s2 ⇒ labels_defined s1 @ labels_defined s2
933| Swhile _ s ⇒ labels_defined s
934| Sdowhile _ s ⇒ labels_defined s
935| Sfor s1 _ s2 s3 ⇒ labels_defined s1 @ labels_defined s2 @ labels_defined s3
936| Sswitch _ ls ⇒ labels_defined_switch ls
937| Slabel l s ⇒ l::(labels_defined s)
938| Scost _ s ⇒ labels_defined s
939| _ ⇒ [ ]
940]
941and labels_defined_switch (ls:labeled_statements) : list ident ≝
942match ls with
943[ LSdefault s ⇒ labels_defined s
944| LScase _ _ s ls ⇒ labels_defined s @ labels_defined_switch ls
945].
946
947definition ldefined ≝ λs.λl.Exists ? (λl'.l' = l) (labels_of s).
948
949(* For each label l in s, there exists a matching label l' = lenv(l) defined in s' *)
950definition labels_translated : lenv → statement → stmt → Prop ≝
951λlbls,s,s'.  ∀l.
952  (Exists ? (λl'.l' = l) (labels_defined s)) →
953  ∃l'. lookup_label lbls l = (OK ? l') ∧ ldefined s' l'.
954
955
956(* Invariant on statements, holds during conversion to Cminor *)
957definition stmt_inv ≝  λvars. stmt_P (stmt_vars (local_id vars)).
958
959(* I (Ilias) decided to inline the following definition, to make explicit the data constructed.
960 * This was needed to prove some stuff in translate_statement at some point, but it might be
961 * useless now. If needed, I can revert this change.  *)
962definition translate_assign : ∀vars:var_types. expr → expr → res (Σs:stmt. stmt_inv vars s) ≝
963λvars,e1,e2.
964do e2' ← translate_expr vars e2;
965do dest ← translate_dest vars e1;
966match dest with
967[ IdDest id ty p ⇒
968    do e2' ← type_should_eq (typeof e2) ty ? e2';
969    OK ? «St_assign ? id e2', ?»
970| MemDest r e1' ⇒ OK ? «St_store ? r e1' e2', ?»
971].
972% try (//)  try (elim e2' //) elim e1' //
973qed.
974
975definition m_option_map : ∀A,B:Type[0]. (A → res B) → option A → res (option B) ≝
976λA,B,f,oa.
977match oa with
978[ None ⇒ OK ? (None ?)
979| Some a ⇒ do b ← f a; OK ? (Some ? b)
980].
981
982definition translate_expr_sigma : ∀vars:var_types. expr → res (Σe:(𝚺t:typ.CMexpr t). match e with [ mk_DPair t e ⇒ expr_vars t e (local_id vars) ]) ≝
983λv,e.
984  do e' ← translate_expr v e;
985  OK (Σe:(𝚺t:typ.CMexpr t).?) «❬?, e'❭, ?».
986whd @pi2
987qed.
988
989(* Add the list of typed variables tmpenv to the environment [var_types] with
990   the allocation mode Local. *)
991definition add_tmps : var_types → list (ident × type) → var_types ≝
992λvs,tmpenv.
993  foldr ?? (λidty,vs. add ?? vs (\fst idty) 〈Local, \snd idty〉) vs tmpenv.
994
995record tmpgen (vars:var_types) : Type[0] ≝ {
996  tmp_universe : universe SymbolTag;
997  tmp_env : list (ident × type);
998  tmp_ok : fresh_map_for_univ … (add_tmps vars tmp_env) tmp_universe;
999  tmp_preserved :
1000    ∀id,ty. local_id vars id ty → local_id (add_tmps vars tmp_env) id ty
1001}.
1002
1003definition alloc_tmp : ∀vars. type → tmpgen vars → ident × (tmpgen vars) ≝
1004λvars,ty,g.
1005  let 〈tmp,u〉 as E ≝ fresh ? (tmp_universe ? g) in
1006  〈tmp, mk_tmpgen ? u (〈tmp, ty〉::(tmp_env ? g)) ??〉.
1007[ #id #ty'
1008  whd in ⊢ (? → ?%??);
1009  whd in ⊢ (% → %);
1010  whd in ⊢ (? → match % with [_ ⇒ ? | _ ⇒ ?]); #H
1011  >lookup_add_miss
1012  [ @(tmp_preserved … g) @H
1013  | @(fresh_distinct … E) @(tmp_ok … g)
1014    lapply (tmp_preserved … g id ty' H)
1015    whd in ⊢ (% → %);
1016    whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?);
1017    cases (lookup ??? id)
1018    [ * | #x #_ % #E destruct ]
1019  ]
1020| @fresh_map_add
1021  [ @(fresh_map_preserved … E) @(tmp_ok … g)
1022  | @(fresh_is_fresh … E)
1023  ]
1024] qed.
1025
1026
1027lemma lookup_label_hit : ∀lbls,l,l'.
1028  lookup_label lbls l = OK ? l' →
1029  lpresent lbls l'.
1030#lbls #l #l' #E whd %{l} @E
1031qed.
1032
1033(* TODO: is this really needed now? *)
1034
1035definition tmps_preserved : ∀vars:var_types. tmpgen vars → tmpgen vars → Prop ≝
1036λvars,u1,u2.
1037  ∀id,ty. local_id (add_tmps vars (tmp_env … u1)) id ty → local_id (add_tmps vars (tmp_env … u2)) id ty.
1038
1039lemma alloc_tmp_preserves : ∀vars,tmp,u,u',q.
1040  〈tmp,u'〉 = alloc_tmp ? q u → tmps_preserved vars u u'.
1041#vars #tmp * #u1 #e1 #F1 #P1 * #u2 #e2 #F2 #P2 #q
1042whd in ⊢ (???% → ?); generalize in ⊢ (???(?%) → ?);
1043cases (fresh SymbolTag u1) in ⊢ (??%? → ???(match % with [ _ ⇒ ? ]?) → ?);
1044#tmp' #u' #E1 #E2 whd in E2:(???%); destruct
1045#id #ty #H whd in ⊢ (?%??); whd in H ⊢ %;
1046whd in ⊢ match % with [ _ ⇒ ? | _ ⇒ ? ];
1047>lookup_add_miss // @(fresh_distinct … E1) @F1
1048whd in H:(match % with [_ ⇒ ?|_ ⇒ ?]) ⊢ %;
1049cases (lookup ??? id) in H ⊢ %;
1050[ * | #x #_ % #E destruct ]
1051qed.
1052
1053lemma add_tmps_oblivious : ∀vars,s,u.
1054  stmt_inv vars s → stmt_inv (add_tmps vars (tmp_env vars u)) s.
1055#vars #s #u #H
1056@(stmt_P_mp … H)
1057#s' #H1 @(stmt_vars_mp … H1) #id #t #H @(tmp_preserved ? u ?? H)
1058qed.
1059
1060lemma local_id_fresh_tmp : ∀vars,tmp,u,ty,u0.
1061  〈tmp,u〉 = alloc_tmp vars ty u0 → local_id (add_tmps vars (tmp_env … u)) tmp (typ_of_type ty).
1062#vars #tmp #u #ty #u0
1063whd in ⊢ (???% → ?); generalize in ⊢ (???(?%) → ?);
1064cases (fresh SymbolTag (tmp_universe vars u0)) in ⊢ (??%? → ???(match % with [_⇒?]?) → ?);
1065* #tmp' #u' #e #E whd in E:(???%);
1066destruct
1067whd in ⊢ (?%??); whd whd in ⊢ match % with [ _ ⇒ ? | _ ⇒ ? ]; >lookup_add_hit
1068@refl
1069qed.
1070
1071
1072let rec mklabels (ul:labgen) : (identifier Label) × (identifier Label) × labgen ≝
1073  match generate_fresh_label ul with
1074  [ mk_Sig res1 H1 ⇒
1075     let 〈entry_label, ul1〉 as E1 ≝ res1 in
1076     match generate_fresh_label ul1 with
1077     [ mk_Sig res2 H2 ⇒
1078        let 〈exit_label, ul2〉 as E2 ≝ res2 in
1079        〈entry_label, exit_label, ul2〉
1080     ]
1081  ].
1082
1083(* When converting loops into gotos, and in order to eliminate blocks, we have
1084 * to convert continues and breaks into goto's, too. We add some "flags" in
1085 * in argument to [translate_statement], meaning that the next encountered break
1086 * or continue has to be converted into a goto to some contained label.
1087 * ConvertTo l1 l2 means "convert continue to goto l1 and convert break to goto l2".
1088 *)
1089inductive convert_flag : Type[0] ≝
1090| DoNotConvert : convert_flag
1091| ConvertTo    : identifier Label → identifier Label → convert_flag. (* continue, break *)
1092
1093let rec labels_of_flag (flag : convert_flag) : list (identifier Label) ≝
1094match flag with
1095[ DoNotConvert ⇒ [ ]
1096| ConvertTo continue break ⇒ continue :: break :: [ ]
1097].
1098
1099(* For a top-level expression, [label-wf] collapses to "all labels are properly declared" *)
1100definition label_wf ≝
1101λ (s : statement) .λ (s' : stmt) .λ (lbls : lenv). λ (flag : convert_flag).
1102    stmt_P (λs1. stmt_labels (λl.ldefined s' l ∨ lpresent lbls l ∨ In ? l (labels_of_flag flag)) s1) s'.
1103
1104(* trans_inv is the invariant which is enforced during the translation from Clight to Cminor.
1105  The involved arguments are the following:
1106  . vars:var_types, an environment mapping variables to their types and allocation modes
1107  . lbls:lenv, a mapping from old (Clight) to fresh and new (Cminor) labels,
1108  . s:statement, a Clight statement,
1109  . uv, a fresh variable generator (containing itself some invariants)
1110  . flag, wich maps "break" and "continue" to "gotos"
1111  . su', a couple of a Cminor statement and fresh variable generator.
1112*)
1113definition trans_inv : ∀vars:var_types . ∀lbls:lenv . statement → tmpgen vars → convert_flag → ((tmpgen vars) × labgen × stmt) → Prop ≝
1114λvars,lbls,s,uv,flag,su'.
1115  let 〈uv', ul', s'〉 ≝ su' in
1116  stmt_inv (add_tmps vars (tmp_env … uv')) s' ∧   (* remaining variables in s' are local*)
1117  labels_translated lbls s s' ∧                   (* all the labels in s are transformed in label of s' using [lbls] as a map *)
1118  tmps_preserved vars uv uv' ∧                    (* the variables generated are local and grows in a monotonic fashion *)
1119  label_wf s s' lbls flag.                        (* labels are "properly" declared, as defined in [ŀabel_wf].*)
1120                                 
1121let rec translate_statement (vars:var_types) (uv:tmpgen vars) (ul:labgen) (lbls:lenv) (flag:convert_flag) (s:statement) on s
1122  : res (Σsu:(tmpgen vars)×labgen×stmt.trans_inv vars lbls s uv flag su) ≝
1123match s return λs.res (Σsu:(tmpgen vars)×labgen×stmt.trans_inv vars lbls s uv flag su) with
1124[ Sskip ⇒ OK ? «〈uv, ul, St_skip〉, ?»
1125| Sassign e1 e2 ⇒
1126    do e2' ← translate_expr vars e2;  (* rhs *)
1127    do dest ← translate_dest vars e1; (* e1 *)
1128    match dest with
1129    [ IdDest id ty p ⇒
1130       do e2' ← type_should_eq (typeof e2) ty ? e2';
1131       OK ? «〈uv, ul, St_assign ? id e2'〉, ?»
1132    | MemDest r e1' ⇒
1133       OK ? «〈uv, ul, St_store ? r e1' e2'〉, ?»
1134    ]
1135| Scall ret ef args ⇒
1136    do ef' ← translate_expr vars ef;
1137    do ef' ← typ_should_eq (typ_of_type (typeof ef)) (ASTptr Code) ? ef';
1138    do args' ← mmap_sigma ??? (translate_expr_sigma vars) args;
1139    match ret with
1140    [ None ⇒ OK ? «〈uv, ul, St_call (None ?) ef' args'〉, ?»
1141    | Some e1 ⇒
1142        do dest ← translate_dest vars e1;
1143        match dest with
1144        [ IdDest id ty p ⇒ OK ? «〈uv, ul, St_call (Some ? 〈id,typ_of_type ty〉) ef' args'〉, ?»
1145        | MemDest r e1' ⇒
1146            let 〈tmp, uv1〉 as Etmp ≝ alloc_tmp ? (typeof e1) uv in
1147            OK ? «〈uv1, ul, St_seq (St_call (Some ? 〈tmp,typ_of_type (typeof e1)〉) ef' args') (St_store (typ_of_type (typeof e1)) r e1' (Id ? tmp))〉, ?»
1148        ]
1149    ]
1150| Ssequence s1 s2 ⇒
1151    do «fgens1, s1', H1» ← translate_statement vars uv ul «lbls,?» flag s1;
1152    do «fgens2, s2', H2» ← translate_statement vars (fst … fgens1) (snd … fgens1) «lbls,?» flag s2;
1153    OK ? «〈fgens2, St_seq s1' s2'〉, ?»
1154| Sifthenelse e1 s1 s2 ⇒
1155    do e1' ← translate_expr vars e1;
1156    match typ_of_type (typeof e1) return λx.(Σe:CMexpr x.expr_vars ? e ?) → ? with
1157    [ ASTint _ _ ⇒ λe1'.
1158         do «fgens1, s1', H1» ← translate_statement vars uv ul «lbls,?» flag s1;
1159         do «fgens2, s2', H2» ← translate_statement vars (fst … fgens1) (snd … fgens1) «lbls,?» flag s2;
1160        OK ? «〈fgens2, St_ifthenelse ?? e1' s1' s2'〉, ?»
1161    | _ ⇒ λ_.Error ? (msg TypeMismatch)
1162    ] e1'
1163| Swhile e1 s1 ⇒
1164    do e1' ← translate_expr vars e1;
1165    match typ_of_type (typeof e1) return λx.(Σe:CMexpr x.expr_vars ? e ?) → ? with
1166    [ ASTint _ _ ⇒ λe1'.         
1167         (* TODO: this is a little different from the prototype and CompCert, is it OK? *)
1168         match mklabels ul with
1169         [ mk_Sig result Hmklabels =>
1170              let 〈labels, ul1〉 as E1 ≝ result in
1171              let 〈entry, exit〉 as E2 ≝ labels in
1172              do «fgens2, s1',H1» ← translate_statement vars uv ul1 «lbls,?» (ConvertTo entry exit) s1;
1173              let converted_loop ≝
1174               St_label entry
1175               (St_seq
1176                 (St_ifthenelse ?? e1' (St_seq s1' (St_goto entry)) St_skip)
1177                 (St_label exit St_skip))
1178              in         
1179              OK ? «〈fgens2, converted_loop〉, ?»
1180         ]
1181    | _ ⇒ λ_.Error ? (msg TypeMismatch)
1182    ] e1'
1183| Sdowhile e1 s1 ⇒
1184    do e1' ← translate_expr vars e1;
1185    match typ_of_type (typeof e1) return λx.(Σe:CMexpr x. expr_vars ? e ?) → ? with
1186    [ ASTint _ _ ⇒ λe1'.
1187         match mklabels ul with
1188         [ mk_Sig result Hmklabels ⇒
1189              let 〈labels, ul1〉 as E1 ≝ result in
1190              let 〈entry, exit〉 as E2 ≝ labels in             
1191              do «fgens2, s1', H1» ← translate_statement vars uv ul1 «lbls,?» (ConvertTo entry exit) s1;
1192              let converted_loop ≝
1193               St_label entry
1194                 (St_seq
1195                   (St_seq
1196                     s1'
1197                     (St_ifthenelse ?? e1' (St_goto entry) St_skip)
1198                   )
1199                   (St_label exit St_skip))
1200              in
1201              (* TODO: this is a little different from the prototype and CompCert, is it OK? *)
1202              OK ? «〈fgens2, converted_loop〉, ?»
1203         ]
1204    | _ ⇒ λ_.Error ? (msg TypeMismatch)
1205    ] e1'
1206| Sfor s1 e1 s2 s3 ⇒
1207    do e1' ← translate_expr vars e1;
1208    match typ_of_type (typeof e1) return λx.(Σe:CMexpr x. expr_vars ? e ?) → ? with
1209    [ ASTint _ _ ⇒ λe1'.
1210         match mklabels ul with
1211         [ mk_Sig result Hmklabels ⇒
1212              let 〈labels, ul1〉 as E ≝ result in
1213              let 〈entry, exit〉 as E2 ≝ labels in                           
1214              do «fgens2, s1', H1» ← translate_statement vars uv ul1 «lbls,?» flag s1;
1215              do «fgens3, s2', H2» ← translate_statement vars (fst … fgens2) (snd … fgens2) «lbls, ?» (ConvertTo entry exit) s2;
1216              do «fgens4, s3', H3» ← translate_statement vars (fst … fgens3) (snd … fgens3) «lbls, ?» (ConvertTo entry exit) s3;
1217              (* TODO: this is a little different from the prototype and CompCert, is it OK? *)
1218              let converted_loop ≝
1219                St_seq
1220                 s1'
1221                 (St_label entry
1222                   (St_seq
1223                     (St_ifthenelse ?? e1' (St_seq s3' (St_seq s2' (St_goto entry))) St_skip)
1224                     (St_label exit St_skip)
1225                    ))
1226              in
1227             OK ? «〈fgens4, converted_loop〉, ?»
1228        ]
1229    | _ ⇒ λ_.Error ? (msg TypeMismatch)
1230    ] e1'
1231| Sbreak ⇒
1232   match flag return λf.flag = f → ? with
1233   [ DoNotConvert ⇒ λEflag.
1234     Error ? (msg FIXME)
1235   | ConvertTo continue_label break_label ⇒ λEflag.
1236     OK ? «〈uv, ul, St_goto break_label〉, ?»
1237   ] (refl ? flag)
1238| Scontinue ⇒
1239  match flag return λf.flag = f → ? with
1240  [ DoNotConvert ⇒ λEflag.
1241    Error ? (msg FIXME)
1242  | ConvertTo continue_label break_label ⇒ λEflag.
1243    OK ? «〈uv, ul, St_goto continue_label〉, ?»
1244  ] (refl ? flag)
1245| Sreturn ret ⇒
1246    match ret with
1247    [ None ⇒ OK ? «〈uv, ul, St_return (None ?)〉, ?»
1248    | Some e1 ⇒
1249        do e1' ← translate_expr vars e1;
1250        OK ? «〈uv, ul, St_return (Some ? (mk_DPair … e1'))〉, ?»
1251    ]
1252| Sswitch e1 ls ⇒ Error ? (msg FIXME)
1253| Slabel l s1 ⇒
1254    do l' as E ← lookup_label lbls l;
1255    do «fgens1, s1', H1» ← translate_statement vars uv ul lbls flag s1;
1256    OK ? «〈fgens1, St_label l' s1'〉, ?»
1257| Sgoto l ⇒
1258    do l' as E ← lookup_label lbls l;
1259    OK ? «〈uv, ul, St_goto l'〉, ?»
1260| Scost l s1 ⇒
1261    do «fgens1, s1', H1» ← translate_statement vars uv ul lbls flag s1;
1262    OK ? «〈fgens1, St_cost l s1'〉, ?»
1263].
1264try @conj try @conj try @conj try @conj try @conj try @conj try @conj
1265try (@I)
1266try (#l #H elim H)
1267try (#size #sign #H assumption)
1268try (#region #H assumption)
1269[ 1,5: @(tmp_preserved … p) ]
1270[ 1,3: elim e2' | 2,9: elim e1' | 4,7,14: elim ef' ]
1271[ 1,2,3,4,5,6,7 : #e #Hvars @(expr_vars_mp … Hvars) #i #t #Hlocal @(tmp_preserved … Hlocal) ]
1272[ 1: @All_mp [ 1: @(λe.match e with [ mk_DPair t e0 ⇒ expr_vars t e0 (local_id vars) ])
1273             | 2: * #t #e #Hev whd in Hev ⊢ %; @(expr_vars_mp … Hev) #i #t #Hp @(tmp_preserved … Hp)
1274             | 3: elim args' // ]
1275| 8: (* we should be able to merge this case with the previous ... *)
1276     @All_mp [ 1: @(λe.match e with [ mk_DPair t e0 ⇒ expr_vars t e0 (local_id vars) ])
1277             | 2: * #t #e #Hev whd in Hev ⊢ %; @(expr_vars_mp … Hev) #i #t #Hp @(tmp_preserved … Hp)
1278             | 3: elim args' // ]
1279| 2: @(local_id_fresh_tmp vars tmp uv1 (typeof e1) uv Etmp)
1280| 3:  @(All_mp (𝚺 t:typ.expr t) (λe. match e with [ mk_DPair t e0 ⇒ expr_vars t e0 (local_id vars)]))
1281       [ 1: #a #Ha elim a in Ha ⊢ ?; #ta #a #Ha whd @(expr_vars_mp ?? (local_id vars))
1282       [ 1: #i0 #t0 #H0 @(tmp_preserved vars uv1 i0 t0 H0)
1283       | 2: assumption ]
1284       | 2: elim args' // ]
1285| 4: @(local_id_fresh_tmp vars tmp uv1 (typeof e1) uv Etmp) ]
1286[ 1: #size #sign | 2: #reg | 3: #fsize ]
1287[ 1,2,3: #H @(alloc_tmp_preserves vars tmp uv uv1 … Etmp) @H ]
1288try @(match fgens1 return λx.x=fgens1 → ? with
1289     [ mk_Prod uv1 ul1 ⇒ λHfgens1.? ] (refl ? fgens1))
1290try @(match fgens2 return λx.x=fgens2 → ? with
1291     [ mk_Prod uv2 ul2 ⇒ λHfgens2.? ] (refl ? fgens2))
1292try @(match fgens3 return λx.x=fgens3 → ? with
1293     [ mk_Prod uv3 ul3 ⇒ λHfgens3.? ] (refl ? fgens3))
1294try @(match fgens4 return λx.x=fgens4 → ? with
1295     [ mk_Prod uv4 ul4 ⇒ λHfgens4.? ] (refl ? fgens4))
1296whd in H1 H2 H3 ⊢ ?; destruct whd nodelta in H1 H2 H3;
1297try (elim H1 -H1 * * #Hstmt_inv1 #Hlabels_tr1 #Htmps_pres1)
1298try (elim H2 -H2 * * #Hstmt_inv2 #Hlabels_tr2 #Htmps_pres2)
1299try (elim H3 -H3 * * #Hstmt_inv3 #Hlabels_tr3 #Htmps_pres3)
1300[ 1,2: #Hind1 #Hind2 | 3,4,9,11: #Hind | 5: #Hind1 #Hind2 #Hind3 ]
1301try @conj try @conj try @conj try @conj try @conj try (whd @I) try assumption
1302[ 1,7: @(stmt_P_mp … Hstmt_inv1) #e #Hvars @(stmt_vars_mp … Hvars) #i #t #Hlocal @(Htmps_pres2 … Hlocal)
1303| 2: #l #H cases (Exists_append ???? H) #Hcase
1304         [ 1: elim (Hlabels_tr1 l Hcase) #label #Hlabel @(ex_intro … label) @conj
1305           [ 1: @(proj1 ?? Hlabel)
1306           | 2: normalize @Exists_append_l @(proj2 … Hlabel) ]
1307         | 2: elim (Hlabels_tr2 l Hcase) #label #Hlabel @(ex_intro … label) @conj
1308           [ 1: @(proj1 ?? Hlabel)
1309           | 2: normalize @Exists_append_r @(proj2 … Hlabel) ]
1310         ]
1311| 3,9: #id #ty #H @(Htmps_pres2 … (Htmps_pres1 id ty H)) ]
1312[ 1: @(stmt_P_mp … Hind2) | 2: @(stmt_P_mp … Hind1) ]
1313[ 1,2: #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1314     #l * try * [ 1,4: #H %1 %1 normalize in H ⊢ %; try (@Exists_append_l @H); try (@Exists_append_r @H)
1315                | 2,5: #H %1 %2 assumption
1316                | 3,6: #H %2 assumption ]
1317(* if/then/else *)
1318| 3: whd elim e1' #e #Hvars @(expr_vars_mp … Hvars) #i #t #Hlocal @(tmp_preserved … Hlocal)
1319| 4: whd #l #H
1320       cases (Exists_append ???? H) #Hcase
1321         [ 1: elim (Hlabels_tr1 l Hcase) #label #Hlabel @(ex_intro … label) @conj
1322           [ 1: @(proj1 ?? Hlabel)
1323           | 2: normalize @Exists_append_l @(proj2 … Hlabel) ]
1324         | 2: elim (Hlabels_tr2 l Hcase) #label #Hlabel @(ex_intro … label) @conj
1325           [ 1: @(proj1 ?? Hlabel)
1326           | 2: normalize @Exists_append_r @(proj2 … Hlabel) ]
1327         ]
1328]                 
1329[ 1: 1: @(stmt_P_mp … Hind2) | 2: @(stmt_P_mp … Hind1) ]
1330[ 1,2: #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1331     #l * try * [ 1,4: #H %1 %1 normalize in H ⊢ %; try (@Exists_append_l @H); try (@Exists_append_r @H)
1332                | 2,5: #H %1 %2 assumption
1333                | 3,6: #H %2 assumption ] ]
1334try @conj try @conj try @conj try @conj try @conj try @conj try @conj try @conj try @I
1335[ 1,9,19,32: whd elim e1' #e #Hvars @(expr_vars_mp … Hvars) #i #t #Hlocal @(tmp_preserved … Hlocal)
1336| 2,8: @(stmt_P_mp … Hstmt_inv1) #s0 #Hstmt_vars @(stmt_vars_mp … Hstmt_vars) //
1337| 3,10: whd #l #H normalize in H;
1338         elim (Hlabels_tr1 … H) #label #Hlabel @(ex_intro … label)
1339         @conj
1340         [ 1,3: @(proj1 … Hlabel)
1341         | 2,4: whd @or_intror normalize @Exists_append_l @Exists_append_l try @Exists_append_l
1342              @(proj2 … Hlabel) ]
1343| 30: whd %2 %2 whd /2/
1344| 31: whd %2 whd /2/
1345| 4,16: whd %1 %1 normalize /2/
1346| 5,12: @(stmt_P_mp … Hind) #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1347   #l * try * [ 1,5: #H %1 %1 normalize %2 @Exists_append_l @Exists_append_l try @Exists_append_l @H
1348              | 2,6: #H %1 %2 assumption
1349              | 3,7: #H <H %1 %1 normalize /2/
1350              | 4,8: #H normalize in H; elim H [ 1,3: #E <E %1 %1 normalize %2
1351                                                 @Exists_append_r normalize /2/
1352                                               | 2,4: * ]
1353              ]
1354| 6: normalize %1 %1 %1 //                                                                                   
1355| 7,14: normalize %1 %1 %2 @Exists_append_r normalize /2/
1356| 11,13: whd %1 %1 normalize /2/
1357| 15: whd #label * [ 1: #Eq @(ex_intro … l') @conj [ 1: destruct // | whd /2/ ]
1358                   | 2: #H elim (Hlabels_tr1 label H)
1359                         #lab * #Hlookup #Hdef @(ex_intro … lab) @conj
1360                         [ 1: // | 2: whd %2 assumption ]
1361                   ]
1362| 17: @(stmt_P_mp … Hind) #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1363   #l * try * [ 1: #H %1 %1 normalize %2 @H
1364              | 2: #H %1 %2 assumption
1365              | 3: #H %2 assumption ]
1366| 18: @(stmt_P_mp … Hstmt_inv1) #s0 #Hstmt_vars @(stmt_vars_mp … Hstmt_vars) #i #t
1367   #H @(Htmps_pres3 … (Htmps_pres2 … H))
1368| 20: @(stmt_P_mp … Hstmt_inv3) //
1369| 21: @(stmt_P_mp … Hstmt_inv2) #s0 #Hstmt_vars @(stmt_vars_mp … Hstmt_vars) #i #t
1370   #H @(Htmps_pres3 … H)
1371| 22: whd #l #H normalize in H;
1372      cases (Exists_append … H) #Hcase
1373      [ 1: elim (Hlabels_tr1 l Hcase) #label #Hlabel @(ex_intro … label) @conj
1374        [ 1: @(proj1 … Hlabel)
1375        | 2: normalize @Exists_append_l @(proj2 … Hlabel)
1376        ]
1377      | 2: cases (Exists_append … Hcase) #Hcase2
1378        [ 1: elim (Hlabels_tr2 l Hcase2) #label #Hlabel @(ex_intro … label) @conj
1379          [ 1: @(proj1 … Hlabel)
1380          | 2: normalize >append_nil >append_nil >append_cons
1381               @Exists_append_r @Exists_append_l @Exists_append_r
1382               @(proj2 … Hlabel)
1383          ]
1384        | 2: elim (Hlabels_tr3 l Hcase2) #label #Hlabel @(ex_intro … label) @conj
1385          [ 1: @(proj1 … Hlabel)
1386          | 2: normalize >append_nil >append_nil >append_cons
1387             @Exists_append_r @Exists_append_l @Exists_append_l
1388             @(proj2 … Hlabel)
1389          ]
1390        ]
1391      ]
1392| 23: #id #ty #H @(Htmps_pres3 … (Htmps_pres2 … (Htmps_pres1 … H)))
1393| 24: @(stmt_P_mp … Hind3) #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1394   #l * try * [ 1: #H %1 %1 normalize @Exists_append_l @H
1395              | 2: #H %1 %2 assumption
1396              | 3: #H %2 assumption ]
1397| 25: whd %1 %1 normalize /2/
1398| 26: @(stmt_P_mp … Hind1) #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1399   #l * try * [ 1: #H %1 %1 normalize @Exists_append_r @(Exists_add ?? (nil ?))
1400                   @Exists_append_r @Exists_append_l @Exists_append_l
1401                   @Exists_append_l assumption
1402              | 2: #H %1 %2 assumption
1403              | 3: #H <H %1 %1 normalize
1404                   @Exists_append_r whd %1 //
1405              | 4: * [ 1: #H <H %1 %1 normalize
1406                       @Exists_append_r @(Exists_add ?? (nil ?))
1407                       @Exists_append_r @Exists_append_r
1408                       whd %1 //
1409                     | 2: * ]
1410              ]
1411| 27: @(stmt_P_mp … Hind2) #s0 #Hstmt_labels @(stmt_labels_mp … Hstmt_labels)
1412   #l * try * [ 1: #H %1 %1 normalize @Exists_append_r @(Exists_add ?? (nil ?))
1413                   @Exists_append_r @Exists_append_l @Exists_append_l                   
1414                   @Exists_append_r @Exists_append_l assumption
1415              | 2: #H %1 %2 assumption
1416              | 3: #H <H %1 %1 normalize
1417                   @Exists_append_r whd %1 //
1418              | 4: * [ 1: #Eq <Eq %1 %1 whd normalize
1419                       @Exists_append_r @(Exists_add ?? (nil ?)) @Exists_append_r
1420                       @Exists_append_r whd %1 //
1421                     | 2: * ]
1422              ]
1423| 28: whd %1 %1 normalize /2/
1424| 29: whd %1 %1 normalize
1425      @Exists_append_r @(Exists_add ?? (nil ?)) @Exists_append_r @Exists_append_r
1426      whd %1 //
1427| 33: whd %1 %2 whd @(ex_intro … l) @E
1428] qed.
1429
1430axiom ParamGlobalMixup : String.
1431
1432(* params and statement aren't real parameters, they're just there for giving the invariant. *)
1433definition alloc_params :
1434 ∀vars:var_types.∀lbls,statement,uv,flag. list (ident×type) → (Σsu:(tmpgen vars)×labgen×stmt. trans_inv vars lbls statement uv flag su)
1435   → res (Σsu:(tmpgen vars)×labgen×stmt.trans_inv vars lbls statement uv flag su) ≝   
1436λvars,lbls,statement,uv,ul,params,s. foldl ?? (λsu,it.
1437  let 〈id,ty〉 ≝ it in
1438  do «result,Is» ← su;
1439  let 〈fgens1, s〉 as Eresult ≝ result in
1440  do 〈t,ty'〉 as E ← lookup' vars id;
1441  match t return λx.? → res (Σsu:(tmpgen vars)×labgen×stmt.trans_inv vars lbls statement uv ul su) with
1442  [ Local ⇒ λE. OK (Σs:(tmpgen vars)×labgen×stmt.?) «result,Is»
1443  | Stack n ⇒ λE.
1444      OK ? «〈fgens1, St_seq (St_store ? Any (Cst ? (Oaddrstack n)) (Id (typ_of_type ty') id)) s〉, ?»
1445  | Global _ ⇒ λE. Error ? [MSG ParamGlobalMixup; CTX ? id]
1446  ] E) (OK ? s) params.
1447whd
1448@(match fgens1 return λx.x=fgens1 → ? with
1449  [ mk_Prod uv1 ul1 ⇒ λHfgens1.? ] (refl ? fgens1))
1450whd in Is ⊢ %; destruct whd in Is;
1451try @conj try @conj try @conj try @conj try @conj try @conj try @conj try @I
1452elim Is * * #Hincl #Hstmt_inv #Hlab_tr #Htmp_pr try assumption
1453@(expr_vars_mp … (tmp_preserved … uv1)) whd >E @refl
1454qed.
1455
1456axiom DuplicateLabel : String.
1457
1458definition lenv_list_inv : lenv → lenv → list ident → Prop ≝
1459λlbls0,lbls,ls.
1460 ∀l,l'. lookup_label lbls l = OK ? l' →
1461 Exists ? (λl'. l' = l) ls ∨ lookup_label lbls0 l = OK ? l'.
1462
1463lemma lookup_label_add : ∀lbls,l,l'.
1464  lookup_label (add … lbls l l') l = OK ? l'.
1465#lbls #l #l' whd in ⊢ (??%?); >lookup_add_hit @refl
1466qed.
1467
1468lemma lookup_label_miss : ∀lbls,l,l',l0.
1469  l0 ≠ l →
1470  lookup_label (add … lbls l l') l0 = lookup_label lbls l0.
1471#lbls #l #l' #l0 #NE
1472whd in ⊢ (??%%);
1473>lookup_add_miss
1474[ @refl | @NE ]
1475qed.
1476
1477let rec populate_lenv (ls:list ident) (ul:labgen) (lbls:lenv): res ((Σlbls':lenv. lenv_list_inv lbls lbls' ls) × labgen) ≝
1478match ls return λls.res ((Σlbls':lenv. lenv_list_inv lbls lbls' ls) × labgen) with
1479[ nil ⇒ OK ? 〈«lbls, ?», ul〉
1480| cons l t ⇒
1481  match lookup_label lbls l return λlook. lookup_label lbls l = look → ? with
1482  [ OK _    ⇒ λ_.Error ? (msg DuplicateLabel)
1483  | Error _ ⇒ λLOOK.
1484    match generate_fresh_label … ul with
1485    [ mk_Sig ret H ⇒
1486       do 〈packed_lbls, ul1〉 ← populate_lenv t (snd ?? ret) (add … lbls l (fst ?? ret));
1487       match packed_lbls with
1488       [ mk_Sig lbls' Hinv ⇒ OK ? 〈«lbls', ?», ul1〉 ]
1489    ]
1490  ] (refl ? (lookup_label lbls l))
1491].
1492[ 1: whd #l #l' #Hlookup %2 assumption
1493| 2: whd in Hinv; whd #cl_lab #cm_lab #Hlookup
1494     @(eq_identifier_elim … l cl_lab)
1495     [ 1: #Heq %1 >Heq whd %1 //
1496     | 2: #Hneq cases (Hinv cl_lab cm_lab Hlookup)
1497           [ 1: #H %1 %2 @H
1498           | 2: #LOOK' %2 >lookup_label_miss in LOOK'; /2/ #H @H ]
1499     ]
1500]
1501qed.
1502
1503definition build_label_env :
1504   ∀body:statement. res ((Σlbls:lenv. ∀l,l'.lookup_label lbls l = OK ? l' → Exists ? (λl'.l' = l) (labels_defined body)) × labgen) ≝
1505λbody.
1506  let initial_labgen ≝ mk_labgen (new_universe ?) (nil ?) ?  in
1507  do 〈label_map, u〉 ← populate_lenv (labels_defined body) initial_labgen (empty_map ??);
1508  let lbls ≝ pi1 ?? label_map in
1509  let H    ≝ pi2 ?? label_map in
1510  OK ? 〈«lbls, ?», u〉.
1511[ 1: #l #l' #E cases (H l l' E) //
1512     whd in ⊢ (??%? → ?); #H destruct
1513| 2: whd @I ]
1514qed.
1515
1516lemma local_id_split : ∀vars,tmpgen,i,t.
1517  local_id (add_tmps vars (tmp_env vars tmpgen)) i t →
1518  local_id vars i t ∨ Exists ? (λx. \fst x = i ∧ typ_of_type (\snd x) = t) (tmp_env … tmpgen).
1519#vars #tmpgen #i #t
1520whd in ⊢ (?%?? → ?);
1521elim (tmp_env vars tmpgen)
1522[ #H %1 @H
1523| * #id #ty #tl #IH
1524  cases (identifier_eq ? i id)
1525  [ #E >E #H %2 whd %1 % [ @refl | whd in H; whd in H:(match % with [_⇒?|_⇒?]); >lookup_add_hit in H; #E1 >E1 @refl ]
1526  | #NE #H cases (IH ?)
1527    [ #H' %1 @H'
1528    | #H' %2 %2 @H'
1529    | whd in H; whd in H:(match % with [ _ ⇒ ? | _ ⇒ ? ]);
1530      >lookup_add_miss in H; [ #H @H | @NE ]
1531    ]
1532  ]
1533] qed.
1534
1535lemma Exists_squeeze : ∀A,P,l1,l2,l3.
1536  Exists A P (l1@l3) → Exists A P (l1@l2@l3).
1537#A #P #l1 #l2 #l3 #EX
1538cases (Exists_append … EX)
1539[ #EX1 @Exists_append_l @EX1
1540| #EX3 @Exists_append_r @Exists_append_r @EX3
1541] qed.
1542
1543(* This lemma allows to merge two stmt_P in one. Used in the following parts to merge an invariant on variables
1544   and an invariant on labels. *)
1545lemma stmt_P_conj : ∀ (P1:stmt → Prop). ∀ (P2:stmt → Prop). ∀ s. stmt_P P1 s → stmt_P P2 s → stmt_P (λs.P1 s ∧ P2 s) s.
1546#P1 #P2 #s elim s
1547normalize try @conj try @conj try /3/
1548[ #z0 #s #Hind1 #Hind2 * * #HA #HB #HC * * #HD #HE #HF try @conj try @conj try @conj try /2/
1549| #H5 #H6 #H7 #H8 #H9 #H10 #H11 * * #H15 #H16 #H17 * * #H20 #H21 #H22
1550  try @conj try @conj try @conj try /2/
1551| 3,4: #H24 #H25 * #H29 #H30 * #H33 #H34 try @conj try @conj try @conj try /2/
1552| 5,6: #H36 #H37 #H38 * #H42 #H43 * #H46 #H47 try @conj try @conj try @conj try /2/ ]
1553qed.
1554
1555definition translate_function :
1556  ∀tmpuniverse:universe SymbolTag.
1557  ∀globals:list (ident×region×type).
1558  ∀f:function.
1559    globals_fresh_for_univ ? globals tmpuniverse →
1560    fn_fresh_for_univ f tmpuniverse →
1561  res internal_function ≝
1562λtmpuniverse, globals, f, Fglobals, Ffn.
1563  do 〈env_pack, ul〉 ← build_label_env (fn_body f);
1564    match env_pack with
1565    [ mk_Sig lbls Ilbls ⇒
1566      let 〈vartypes, stacksize〉 as E ≝ characterise_vars globals f in
1567      let uv ≝ mk_tmpgen vartypes tmpuniverse [ ] ?? in
1568      do s0 ← translate_statement vartypes uv ul lbls DoNotConvert (fn_body f);
1569      do «fgens, s1, Is» ← alloc_params vartypes lbls ? uv DoNotConvert (fn_params f) s0;
1570      let params ≝ map ?? (λv.〈\fst v, typ_of_type (\snd v)〉) (fn_params f) in
1571      let vars ≝ map ?? (λv.〈\fst v, typ_of_type (\snd v)〉) (tmp_env ? (fst ?? fgens) @ fn_vars f) in
1572      do D ← check_distinct_env ?? (params @ vars);
1573      OK ? (mk_internal_function
1574        (opttyp_of_type (fn_return f))
1575        params
1576        vars
1577        D
1578        stacksize
1579        s1 ?)
1580  ].
1581[ 1: #i #t #Hloc whd @Hloc
1582| 2: whd #id #Hpresent normalize in Hpresent:(???%?); whd in Hpresent;
1583      @(characterise_vars_fresh … (sym_eq … E)) //
1584| 3: @(match fgens return λx.x=fgens → ? with
1585     [ mk_Prod uv' ul' ⇒ λHfgens.? ] (refl ? fgens))
1586     whd in Is; <Hfgens in Is; #Is whd in Is ⊢ %;
1587     elim Is * * #Hstmt_inv #Hlab_trans #Htmps_pres #Hlabel_wf
1588     (* merge Hlabel_wf with Hstmt_inv and eliminate right away *)
1589     @(stmt_P_mp … (stmt_P_conj … Hstmt_inv Hlabel_wf))
1590     #s * #Hstmt_vars #Hstmt_labels @conj
1591     [ 1: (* prove that variables are either parameters or locals *)
1592        @(stmt_vars_mp … Hstmt_vars) #i #t #H
1593        (* Case analysis: (i, t) is either in vartypes, or in (tmp_env vartypes uv) *)
1594        cases (local_id_split … H)
1595        [ 1: #H' >map_append
1596          @Exists_map [ 1: #x @(And (\fst x = i) (typ_of_type (\snd x) = t))  (* * #id #ty @(〈id, typ_of_type ty〉=〈i, t〉)*)
1597                      | 2: whd @Exists_squeeze @(characterise_vars_all globals f ?? (sym_eq ??? E) i t H')
1598                      | 3: * #id #ty * #E1 #E2 <E1 <E2 @refl
1599                      ]
1600        | 2: #EX @Exists_append_r whd in ⊢ (???%); <map_append @Exists_append_l
1601          @Exists_map [ 1: #x @(And (\fst x = i) (typ_of_type (\snd x) = t))
1602                      | 2: <Hfgens @EX
1603                      | 3: * #id #ty * #E1 #E2 <E1 <E2 % @refl
1604                      ]
1605        ]
1606     | 2: (* prove that labels are properly declared. *)
1607          @(stmt_labels_mp … Hstmt_labels) #l * *
1608          [ 1: #H assumption
1609          | 2: * #cl_label #Hlookup lapply (Ilbls cl_label l Hlookup) #Hdefined
1610                cases (Hlab_trans … Hdefined) #lx * #LOOKUPx >LOOKUPx in Hlookup; #Ex destruct (Ex)
1611                #H @H
1612          ]
1613    ]
1614] qed.   
1615
1616definition translate_fundef :
1617  ∀tmpuniverse:universe SymbolTag.
1618  ∀globals:list (ident×region×type).
1619    globals_fresh_for_univ ? globals tmpuniverse →
1620  ∀f:clight_fundef.
1621    fd_fresh_for_univ f tmpuniverse →
1622  res (fundef internal_function) ≝
1623λtmpuniverse,globals,Fglobals,f.
1624match f return λf. fd_fresh_for_univ f ? → ? with
1625[ CL_Internal fn ⇒ λFf. do fn' ← translate_function tmpuniverse globals fn Fglobals Ff; OK ? (Internal ? fn')
1626| CL_External fn argtys retty ⇒ λ_. OK ? (External ? (mk_external_function fn (signature_of_type argtys retty)))
1627].
1628
1629(* TODO: move universe generation to higher level once we do runtime function
1630   generation.  Cheating a bit - we only need the new identifiers to be fresh
1631   for individual functions. *)
1632
1633let rec map_partial_All (A,B:Type[0]) (P:A → Prop) (f:∀a:A. P a → res B)
1634  (l:list A) (H:All A P l) on l : res (list B) ≝
1635match l return λl. All A P l → ? with
1636[ nil ⇒ λ_. OK (list B) (nil B)
1637| cons hd tl ⇒ λH.
1638    do b_hd ← f hd (proj1 … H);
1639    do b_tl ← map_partial_All A B P f tl (proj2 … H);
1640      OK (list B) (cons B b_hd b_tl)
1641] H.
1642
1643definition clight_to_cminor : clight_program → res Cminor_program ≝
1644λp.
1645  let tmpuniverse ≝ universe_for_program p in
1646  let fun_globals ≝ map ?? (λidf. 〈\fst idf,Code,type_of_fundef (\snd idf)〉) (prog_funct ?? p) in
1647  let var_globals ≝ map ?? (λv. 〈\fst (\fst v), \snd (\fst v), \snd (\snd v)〉) (prog_vars ?? p) in
1648  let globals ≝ fun_globals @ var_globals in
1649  do fns ← map_partial_All ??? (λx,H. do f ← translate_fundef tmpuniverse globals ? (\snd x) H; OK ? 〈\fst x, f〉) (prog_funct ?? p) ?;
1650    OK ? (mk_program ??
1651      (map ?? (λv. 〈\fst v, \fst (\snd v)〉) (prog_vars ?? p))
1652      fns
1653      (prog_main ?? p)).
1654cases (prog_fresh p) * #H1 #H2 #H3
1655[ @(All_mp … H1) #x * //
1656| @All_append
1657  [ elim (prog_funct ?? p) in H1 ⊢ %; // * #id #fd #tl #IH * * #Hhd1 #Hhd2 #Htl % // @IH @Htl
1658  | whd in H3; elim (prog_vars ?? p) in H3 ⊢ %; // #hd #tl #IH * #Hhd #Htl % /2/
1659  ]
1660] qed.
1661
1662(* It'd be nice to go back to some generic thing like
1663
1664 transform_partial_program2 … p (translate_fundef tmpuniverse globals) (λi. OK ? (\fst i)).
1665
1666   rather than the messier definition above.
1667*)
Note: See TracBrowser for help on using the repository browser.