source: src/Cminor/initialisation.ma @ 1599

Last change on this file since 1599 was 1599, checked in by sacerdot, 8 years ago

Start of merging of stuff into the standard library of Matita.

File size: 4.4 KB
Line 
1
2(* Replace initialisation of global variables with equivalent Cminor code. *)
3
4include "Cminor/syntax.ma".
5include "common/Globalenvs.ma".
6include "utilities/deppair.ma".
7
8(* XXX having to specify the return type in the match is annoying. *)
9definition init_expr : init_data → option (Σc:memory_chunk. expr (typ_of_memory_chunk c)) ≝
10λinit.
11match init return λ_.option (Σc:memory_chunk. expr (typ_of_memory_chunk c)) with
12[ Init_int8 i          ⇒ Some ? (dp ?? Mint8unsigned (Cst ? (Ointconst I8 i)))
13| Init_int16 i         ⇒ Some ? (dp ?? Mint16unsigned (Cst ? (Ointconst I16 i)))
14| Init_int32 i         ⇒ Some ? (dp ?? Mint32 (Cst ? (Ointconst I32 i)))
15| Init_float32 f       ⇒ Some ? (dp ?? Mfloat32 (Cst ? (Ofloatconst f)))
16| Init_float64 f       ⇒ Some ? (dp ?? Mfloat64 (Cst ? (Ofloatconst f)))
17| Init_space n         ⇒ None ?
18| Init_null r          ⇒ Some ? (dp ?? (Mpointer r) (Op1 (ASTint I8 Unsigned) ? (Optrofint ?? r) (Cst ? (Ointconst I8 (zero ?)))))
19| Init_addrof r id off ⇒ Some ? (dp ?? (Mpointer r) (Cst ? (Oaddrsymbol id off)))
20].
21
22(* None of the additional code introduces locals or labels. *)
23definition stmt_inv : stmt → Prop ≝
24  stmt_P (λs. stmt_vars (λ_.False) s ∧ stmt_labels (λ_.False) s).
25 
26discriminator option.
27discriminator Sig.
28
29(* Produces a few extra skips - hopefully the compiler will optimise these
30   away. *)
31
32definition init_datum : ident → region → init_data → nat (*offset*) → Σs:stmt. stmt_inv s ≝
33λid,r,init,off.
34match init_expr init with
35[ None ⇒ St_skip
36| Some x ⇒
37    match x with [ dp chunk e ⇒
38      St_store ? r chunk (Cst ? (Oaddrsymbol id off)) e
39    ]
40].
41% //
42cases init in p; [ 8: #a #b ] #c #p normalize in p;
43destruct;/2/
44qed.
45
46definition init_var : ident → region → list init_data → Σs:stmt. stmt_inv s ≝
47λid,r,init.
48\snd (foldr ??
49  (λdatum,os.
50   let 〈off,s〉 ≝ os in
51     〈off + (size_init_data datum), St_seq (init_datum id r datum off) s〉)
52  〈0, dp ? (λx.stmt_inv x) St_skip ?〉 init).
53[ % //
54| elim init
55  [ % //
56  | #h #t #IH whd in ⊢ (?(???%)); @breakup_pair whd in ⊢ (?%); % [ % [ % // | @(use_sig ? stmt_inv) ] | @IH ]
57] qed.
58
59definition init_vars : list (ident × region × (list init_data)) → Σs:stmt. stmt_inv s ≝
60λvars. foldr ? (Σs:stmt. stmt_inv s)
61  (λvar,s. dp ? (λx.stmt_inv x ) (St_seq s (init_var (\fst (\fst var)) (\snd (\fst var)) (\snd var))) ?) (dp ? (λx.stmt_inv x ) St_skip (conj ?? I I)) vars.
62% [ % [ % // | @(use_sig ? stmt_inv) ] | @(use_sig ? stmt_inv) ]
63qed.
64
65lemma no_labels : ∀s. stmt_inv s → labels_of s = [ ].
66#s elim s //
67[ #s1 #s2 #IH1 #IH2 * * * #_ #_ #H1 #H2 whd in ⊢ (??%?);
68  >(IH1 H1) >(IH2 H2) @refl
69| #sz #sg #e #s1 #s2 #IH1 #IH2 * * * #_ #_ #H1 #H2 whd in ⊢ (??%?);
70  >(IH1 H1) >(IH2 H2) @refl
71| #s #IH * * #_ #_ #H @(IH H)
72| #s #IH * * #_ #_ #H @(IH H)
73| #l #s #IH * * #_ *
74| #l #s #IH * * #_ #_ #H @(IH H)
75] qed.
76
77definition add_statement : ident → (Σs:stmt. stmt_inv s) →
78                              list (ident × (fundef internal_function)) →
79                              list (ident × (fundef internal_function)) ≝
80λid,s. match s with [ dp s H ⇒
81  map ??
82    (λidf.
83      let 〈id',f'〉 ≝ idf in
84      match ident_eq id id' with
85      [ inl _ ⇒
86          match f' with
87          [ Internal f ⇒ 〈id, Internal ? (mk_internal_function
88                                           (f_return f)
89                                           (f_params f)
90                                           (f_vars 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 *
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.