1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 (* ********************************************************************** *)
18 (* Cosimo Oliboni, oliboni@cs.unibo.it *)
20 (* ********************************************************************** *)
22 include "compiler/environment.ma".
24 (* ********************** *)
25 (* GESTIONE AMBIENTE FLAT *)
26 (* ********************** *)
28 (* STRUTTURA AMBIENTE FLAT *)
30 (* elemento: name + desc *)
31 inductive flatVar_elem : Type ≝
32 VAR_FLAT_ELEM: aux_strId_type → desc_elem → flatVar_elem.
35 definition aux_flatEnv_type ≝ list flatVar_elem.
38 definition get_nameId_flatVar ≝ λv:flatVar_elem.match v with [ VAR_FLAT_ELEM n _ ⇒ n ].
39 definition get_desc_flatVar ≝ λv:flatVar_elem.match v with [ VAR_FLAT_ELEM _ d ⇒ d ].
42 definition empty_flatEnv ≝ nil flatVar_elem.
44 (* STRUTTURA MAPPA TRASFORMAZIONE *)
46 (* elemento: nome + max + cur *)
47 inductive maxCur_elem : Type ≝
48 MAX_CUR_ELEM: aux_str_type → nat → nat → maxCur_elem.
51 definition aux_trasfMap_type ≝ list maxCur_elem.
54 definition get_name_maxCur ≝ λmc:maxCur_elem.match mc with [ MAX_CUR_ELEM n _ _ ⇒ n ].
55 definition get_max_maxCur ≝ λmc:maxCur_elem.match mc with [ MAX_CUR_ELEM _ m _ ⇒ m ].
56 definition get_cur_maxCur ≝ λmc:maxCur_elem.match mc with [ MAX_CUR_ELEM _ _ c ⇒ c ].
58 (* mappa di trasformazione vuota *)
59 definition empty_trasfMap ≝ nil maxCur_elem.
61 (* INTERAZIONE AMBIENTE FLAT / MAPPA TRASFORMAZIONE *)
63 (* recupera descrittore da mappa trasformazione : dummy se non esiste (impossibile) *)
64 let rec get_maxcur_from_map_aux (map:aux_trasfMap_type) (str:aux_str_type) (dummy:option maxCur_elem) on map ≝
67 | cons h tl ⇒ match strCmp_str str (get_name_maxCur h) with
68 [ true ⇒ Some ? h | false ⇒ get_maxcur_from_map_aux tl str dummy ]].
70 definition get_maxcur_from_map ≝
71 λmap:aux_trasfMap_type.λstr:aux_str_type.
72 match get_maxcur_from_map_aux map str (None ?) with
73 [ None ⇒ MAX_CUR_ELEM str 0 0 | Some x ⇒ x ].
75 (* aggiungi/aggiorna descrittore mappa trasformazione *)
76 let rec add_maxcur_map_aux (map:aux_trasfMap_type) (str:aux_str_type) (flag:bool) on map ≝
78 [ nil ⇒ match flag with
80 | false ⇒ [MAX_CUR_ELEM str 0 0]
82 | cons h tl ⇒ match strCmp_str str (get_name_maxCur h) with
83 [ true ⇒ [MAX_CUR_ELEM str (S (get_max_maxCur h)) (S (get_max_maxCur h))]@(add_maxcur_map_aux tl str true)
84 | false ⇒ [h]@(add_maxcur_map_aux tl str flag)
88 definition add_maxcur_map ≝
89 λmap:aux_trasfMap_type.λstr:aux_str_type.add_maxcur_map_aux map str false.
91 (* nome -> nome + id *)
92 definition name_to_nameId ≝
93 λmap:aux_trasfMap_type.λstr:aux_str_type.
94 STR_ID str (get_cur_maxCur (get_maxcur_from_map map str)).
96 (* recupera descrittore da ambiente : dummy se non esiste (impossibile) *)
97 let rec get_desc_from_flatEnv_aux (e:aux_flatEnv_type) (str:aux_strId_type) (dummy:option desc_elem) on e ≝
100 | cons h tl ⇒ match strCmp_strId str (get_nameId_flatVar h) with
101 [ true ⇒ Some ? (get_desc_flatVar h)
102 | false ⇒ get_desc_from_flatEnv_aux tl str dummy ]].
104 definition get_desc_from_flatEnv ≝
105 λe:aux_flatEnv_type.λstr:aux_strId_type.
106 match get_desc_from_flatEnv_aux e str (None ?) with
107 [ None ⇒ DESC_ELEM true (AST_TYPE_BASE AST_BASE_TYPE_BYTE8) | Some x ⇒ x ].
109 (* aggiungi descrittore ad ambiente *)
110 definition add_desc_flatEnv ≝
111 λe:aux_flatEnv_type.λstr:aux_strId_type.λb:bool.λt:ast_type.
112 e@[ VAR_FLAT_ELEM str (DESC_ELEM b t) ].
114 (* snapshot della mappa trasformazione *)
115 let rec build_snapshot (map:aux_trasfMap_type) on map ≝
118 | cons h tl ⇒ [ STR_ID (get_name_maxCur h) (get_cur_maxCur h) ]@(build_snapshot tl)
121 (* uscita da un blocco, rollback di cur in mappa di trasformazione *)
122 let rec find_in_snapshot (snap:list aux_strId_type) (str:aux_str_type) on snap ≝
125 | cons h tl ⇒ match strCmp_str str (get_name_strId h) with
126 [ true ⇒ Some ? (get_id_strId h)
127 | false ⇒ find_in_snapshot tl str
131 let rec rollback_map (map:aux_trasfMap_type) (snap:list aux_strId_type) on map ≝
133 [ nil ⇒ empty_trasfMap
134 | cons h tl ⇒ match find_in_snapshot snap (get_name_maxCur h) with
136 | Some newCur ⇒ [ MAX_CUR_ELEM (get_name_maxCur h) (get_max_maxCur h) newCur ]
137 ]@(rollback_map tl snap)
140 (* sequenza di utilizzo:
144 |DICHIARAZIONE "pippo":
145 | -) MAP1 <- add_maxcur_map MAP0 "pippo"
146 | -) ENV1 <- add_desc_flatEnv ENV0 (name_to_nameId MAP1 "pippo") DESC
149 | -) name_to_nameId MAP1 "pippo"
151 |PREPARAZIONE ENTRATA BLOCCO INTERNO:
152 | -) SNAP <- build_snapshot MAP1
156 |DICHIARAZIONE "pippo":
157 | -) MAP2 <- add_maxcur_map MAP1 "pippo"
158 | -) ENV2 <- add_desc_flatEnv ENV (name_to_nameId MAP2 "pippo") DESC
161 | -) name_to_nameId MAP2 "pippo"
163 |PREPARAZIONE USCITA BLOCCO INTERNO:
164 | -) MAP3 <- rollback_map MAP2 SNAP
170 definition pippo ≝ [ ch_P ].
171 definition pluto ≝ [ ch_Q ].
172 definition pippodesc1 ≝ DESC_ELEM true (AST_TYPE_BASE AST_BASE_TYPE_BYTE8).
173 definition pippodesc2 ≝ DESC_ELEM false (AST_TYPE_BASE AST_BASE_TYPE_BYTE8).
174 definition pippodesc3 ≝ DESC_ELEM false (AST_TYPE_ARRAY (AST_TYPE_BASE AST_BASE_TYPE_BYTE8) 2).
175 definition plutodesc1 ≝ DESC_ELEM true (AST_TYPE_BASE AST_BASE_TYPE_WORD16).
176 definition plutodesc2 ≝ DESC_ELEM false (AST_TYPE_BASE AST_BASE_TYPE_WORD16).
177 definition plutodesc3 ≝ DESC_ELEM false (AST_TYPE_ARRAY (AST_TYPE_BASE AST_BASE_TYPE_WORD16) 2).
179 definition map1 ≝ add_maxcur_map empty_trasfMap pippo.
180 definition env1 ≝ add_desc_flatEnv empty_flatEnv (name_to_nameId map1 pippo) pippodesc1.
181 definition map2 ≝ add_maxcur_map map1 pluto.
182 definition env2 ≝ add_desc_flatEnv env1 (name_to_nameId map2 pluto) plutodesc1.
184 definition snap1 ≝ build_snapshot map2.
186 definition map3 ≝ add_maxcur_map map2 pippo.
187 definition env3 ≝ add_desc_flatEnv env2 (name_to_nameId map3 pippo) pippodesc2.
188 definition map4 ≝ add_maxcur_map map3 pluto.
189 definition env4 ≝ add_desc_flatEnv env3 (name_to_nameId map4 pluto) plutodesc2.
191 definition snap2 ≝ build_snapshot map4.
193 definition map5 ≝ add_maxcur_map map4 pippo.
194 definition env5 ≝ add_desc_flatEnv env4 (name_to_nameId map5 pippo) pippodesc3.
195 definition map6 ≝ add_maxcur_map map5 pluto.
196 definition env6 ≝ add_desc_flatEnv env5 (name_to_nameId map6 pluto) plutodesc3.
198 definition map7 ≝ rollback_map map6 snap2.
200 definition map8 ≝ rollback_map map7 snap1.
202 lemma checkenv : None ? = Some ? env6.
203 normalize; elim daemon.
206 lemma checkmap : None ? = Some ? map8.
207 normalize; elim daemon.
210 lemma checksnap : None ? = Some ? snap2.
211 normalize; elim daemon.