source: src/Cminor/initialisation.ma @ 1680

Last change on this file since 1680 was 1631, checked in by campbell, 8 years ago

Use fact that type environments in Cminor have distinct variables to
remove last freshness related axiom in front-end.

File size: 4.5 KB
Line 
1
2(* Replace initialisation of global variables with equivalent Cminor code. *)
3
4include "Cminor/syntax.ma".
5include "common/Globalenvs.ma".
6
7(* XXX having to specify the return type in the match is annoying. *)
8definition init_expr : init_data → option (𝚺c:memory_chunk. expr (typ_of_memory_chunk c)) ≝
9λinit.
10match init return λ_.option (𝚺c:memory_chunk. expr (typ_of_memory_chunk c)) with
11[ Init_int8 i          ⇒ Some ? (mk_DPair ?? Mint8unsigned (Cst ? (Ointconst I8 i)))
12| Init_int16 i         ⇒ Some ? (mk_DPair ?? Mint16unsigned (Cst ? (Ointconst I16 i)))
13| Init_int32 i         ⇒ Some ? (mk_DPair ?? Mint32 (Cst ? (Ointconst I32 i)))
14| Init_float32 f       ⇒ Some ? (mk_DPair ?? Mfloat32 (Cst ? (Ofloatconst f)))
15| Init_float64 f       ⇒ Some ? (mk_DPair ?? Mfloat64 (Cst ? (Ofloatconst f)))
16| Init_space n         ⇒ None ?
17| Init_null r          ⇒ Some ? (mk_DPair ?? (Mpointer r) (Op1 (ASTint I8 Unsigned) ? (Optrofint ?? r) (Cst ? (Ointconst I8 (zero ?)))))
18| Init_addrof r id off ⇒ Some ? (mk_DPair ?? (Mpointer r) (Cst ? (Oaddrsymbol id off)))
19].
20
21(* None of the additional code introduces locals or labels. *)
22definition stmt_inv : stmt → Prop ≝
23  stmt_P (λs. stmt_vars (λ_.λ_.False) s ∧ stmt_labels (λ_.False) s).
24 
25discriminator option.
26discriminator DPair.
27
28(* Produces a few extra skips - hopefully the compiler will optimise these
29   away. *)
30
31definition init_datum : ident → region → init_data → nat (*offset*) → Σs:stmt. stmt_inv s ≝
32λid,r,init,off.
33match init_expr init with
34[ None ⇒ St_skip
35| Some x ⇒
36    match x with [ mk_DPair chunk e ⇒
37      St_store ? r chunk (Cst ? (Oaddrsymbol id off)) e
38    ]
39].
40% //
41cases init in p; [ 8: #a #b ] #c #p normalize in p;
42destruct;/2/
43qed.
44
45definition init_var : ident → region → list init_data → Σs:stmt. stmt_inv s ≝
46λid,r,init.
47\snd (foldr ??
48  (λdatum,os.
49   let 〈off,s〉 ≝ os in
50     〈off + (size_init_data datum), St_seq (init_datum id r datum off) s〉)
51  〈0, mk_Sig ? (λx.stmt_inv x) St_skip ?〉 init).
52[ % //
53| elim init
54  [ % //
55  | #h #t #IH whd in ⊢ (?(???%)); @breakup_pair whd in ⊢ (?%); % [ % [ % // | @(pi2 … (init_datum …)) ] | @IH ]
56] qed.
57
58definition init_vars : list (ident × region × (list init_data)) → Σs:stmt. stmt_inv s ≝
59λvars. foldr ? (Σs:stmt. stmt_inv s)
60  (λvar,s. mk_Sig ? (λx.stmt_inv x ) (St_seq s (init_var (\fst (\fst var)) (\snd (\fst var)) (\snd var))) ?) (mk_Sig ? (λx.stmt_inv x ) St_skip (conj ?? I I)) vars.
61% [ % [ % // | @(pi2 … s) ] | @(pi2 … (init_var …)) ]
62qed.
63
64lemma no_labels : ∀s. stmt_inv s → labels_of s = [ ].
65#s elim s //
66[ #s1 #s2 #IH1 #IH2 * * * #_ #_ #H1 #H2 whd in ⊢ (??%?);
67  >(IH1 H1) >(IH2 H2) @refl
68| #sz #sg #e #s1 #s2 #IH1 #IH2 * * * #_ #_ #H1 #H2 whd in ⊢ (??%?);
69  >(IH1 H1) >(IH2 H2) @refl
70| #s #IH * * #_ #_ #H @(IH H)
71| #s #IH * * #_ #_ #H @(IH H)
72| #l #s #IH * * #_ *
73| #l #s #IH * * #_ #_ #H @(IH H)
74] qed.
75
76definition add_statement : ident → (Σs:stmt. stmt_inv s) →
77                              list (ident × (fundef internal_function)) →
78                              list (ident × (fundef internal_function)) ≝
79λid,s. match s with [ mk_Sig s H ⇒
80  map ??
81    (λidf.
82      let 〈id',f'〉 ≝ idf in
83      match ident_eq id id' with
84      [ inl _ ⇒
85          match f' with
86          [ Internal f ⇒ 〈id, Internal ? (mk_internal_function
87                                           (f_return f)
88                                           (f_params f)
89                                           (f_vars f)
90                                           (f_distinct f)
91                                           (f_stacksize f)
92                                           (St_seq s (f_body f))
93                                           ?)〉
94          | External f ⇒ 〈id, External ? f〉 (* Error ? *)
95          ]
96      | inr _ ⇒ 〈id',f'〉 ]) ].
97%
98[ % [ % // |
99  @(stmt_P_mp … H) #s * #V #L %
100  [ @(stmt_vars_mp … V) #i #t *
101  | @(stmt_labels_mp … L) #l *
102  ]
103  ]
104| whd in ⊢ (?(λ_.??(?(λ_.???%)?))?); >(no_labels … H) @(f_inv f)
105] qed.
106
107definition empty_vars : list (ident × region × (list init_data)) →
108                        list (ident × region × nat) ≝
109map ?? (λv. 〈〈\fst (\fst v), \snd (\fst v)〉,
110              size_init_data_list (\snd v)〉).
111
112definition replace_init : Cminor_program → Cminor_noinit_program ≝
113λp.
114  mk_program ??
115    (empty_vars (prog_vars ?? p))
116    (add_statement (prog_main ?? p) (init_vars (prog_vars ?? p)) (prog_funct ?? p))
117    (prog_main ?? p).
Note: See TracBrowser for help on using the repository browser.