1 | include "joint/joint_semantics.ma". |
---|
2 | include "common/StructuredTraces.ma". |
---|
3 | |
---|
4 | record evaluation_params : Type[1] ≝ |
---|
5 | { globals: list ident |
---|
6 | ; sparams:> sem_params |
---|
7 | ; ev_genv: genv sparams globals |
---|
8 | (* ; io_env : state sparams → ∀o:io_out.res (io_in o) *) |
---|
9 | }. |
---|
10 | |
---|
11 | record prog_params : Type[1] ≝ |
---|
12 | { prog_spars : sem_params |
---|
13 | ; prog : joint_program prog_spars |
---|
14 | ; stack_sizes : ident → option ℕ |
---|
15 | (* ; prog_io : state prog_spars → ∀o.res (io_in o) *) |
---|
16 | }. |
---|
17 | |
---|
18 | lemma map_Exists : ∀A,B,f,P,l.Exists B P (map A B f l) → Exists ? (λx.P (f x)) l. |
---|
19 | #A #B #f #P #l elim l -l [*] |
---|
20 | #hd #tl #IH * |
---|
21 | [ #Phd %1{Phd} |
---|
22 | | #Ptl %2{(IH Ptl)} |
---|
23 | ] |
---|
24 | qed. |
---|
25 | |
---|
26 | lemma Exists_In : ∀A,P,l.Exists A P l → ∃x.In A l x ∧ P x. |
---|
27 | #A #P #l elim l -l [*] #hd #tl #IH * |
---|
28 | [ #Phd %{hd} %{Phd} %1 % |
---|
29 | | #Ptl elim (IH Ptl) #x * #H1 #H2 %{x} %{H2} %2{H1} |
---|
30 | ] |
---|
31 | qed. |
---|
32 | |
---|
33 | lemma In_Exists : ∀A,P,l,x.In A l x → P x → Exists A P l. |
---|
34 | #A #P #l elim l -l [ #x *] #hd #tl #IH #x * |
---|
35 | [ #EQ >EQ #H %1{H} |
---|
36 | | #Intl #Px %2{(IH … Intl Px)} |
---|
37 | ] |
---|
38 | qed. |
---|
39 | |
---|
40 | lemma Exists_mp : ∀A,P,Q,l.(∀x.P x → Q x) → Exists A P l → Exists A Q l. |
---|
41 | #A #P #Q #l #H elim l -l [*] #hd #tl #IH * #G [ %1 | %2 ] /2/ qed. |
---|
42 | |
---|
43 | definition make_global : prog_params → evaluation_params |
---|
44 | ≝ |
---|
45 | λpars. |
---|
46 | let p ≝ prog pars in |
---|
47 | let spars ≝ prog_spars pars in |
---|
48 | let genv ≝ globalenv_noinit ? p in |
---|
49 | let get_pc_lbl ≝ λid,lbl. |
---|
50 | ! bl ← block_of_funct_id … spars genv id ; |
---|
51 | pc_of_label … genv bl lbl in |
---|
52 | mk_evaluation_params |
---|
53 | (prog_var_names … p) |
---|
54 | spars |
---|
55 | (mk_genv_gen ?? genv ? (stack_sizes pars) get_pc_lbl) |
---|
56 | (* (prog_io pars) *). |
---|
57 | #s #H |
---|
58 | elim (find_symbol_exists … p s ?) |
---|
59 | [ #bl #EQ >EQ % #ABS destruct(ABS)|*:] |
---|
60 | @Exists_append_r |
---|
61 | @(Exists_mp … H) // |
---|
62 | qed. |
---|
63 | |
---|
64 | coercion prog_params_to_ev_params : ∀p : prog_params.evaluation_params |
---|
65 | ≝ make_global on _p : prog_params to evaluation_params. |
---|
66 | |
---|
67 | definition make_initial_state : |
---|
68 | ∀pars: prog_params.res (state_pc pars) ≝ |
---|
69 | λpars.let p ≝ prog pars in |
---|
70 | let sem_globals : evaluation_params ≝ pars in |
---|
71 | let ge ≝ ev_genv sem_globals in |
---|
72 | let m0 ≝ alloc_mem … p in |
---|
73 | let 〈m,spb〉 as H ≝ alloc … m0 0 external_ram_size in |
---|
74 | let spp : xpointer ≝ |
---|
75 | «mk_pointer spb (mk_offset (bitvector_of_Z ? external_ram_size)), |
---|
76 | pi2 … spb» in |
---|
77 | (* let ispp : xpointer ≝ mk_pointer ispb (mk_offset (bitvector_of_nat ? 47)) in *) |
---|
78 | let main ≝ prog_main … p in |
---|
79 | let st0 ≝ mk_state pars (Some ? (empty_framesT …)) empty_is (BBbit false) (empty_regsT … spp) m in |
---|
80 | (* use exit_pc as ra and call_dest_for_main as dest *) |
---|
81 | let st0' ≝ mk_state_pc … (set_sp … spp st0) exit_pc exit_pc in |
---|
82 | ! st0_no_pc ← save_frame ?? sem_globals ID (call_dest_for_main … pars) st0' ; |
---|
83 | let st0'' ≝ set_no_pc … st0_no_pc st0' in |
---|
84 | ! bl ← block_of_call … ge (inl … main) st0''; |
---|
85 | ! 〈i, fn〉 ← fetch_function … ge bl ; |
---|
86 | match fn with |
---|
87 | [ Internal ifn ⇒ |
---|
88 | ! st' ← eval_internal_call … ge i ifn (call_args_for_main … pars) st0'' ; |
---|
89 | let pc' ≝ pc_of_point … bl (joint_if_entry … ifn) in |
---|
90 | return mk_state_pc … st' pc' exit_pc |
---|
91 | | External _ ⇒ Error … [MSG BadMain; CTX ? main] (* External main not supported: why? *) |
---|
92 | ]. |
---|
93 | cases m0 in H; #m1 #m2 #m3 #H |
---|
94 | whd in H:(???%); destruct whd in ⊢(??%?); @Zleb_elim_Type0 // #abs @⊥ |
---|
95 | @(absurd … (irreflexive_Zlt …)) % #I cases (I OZ) /3 by Zlt_to_Zle_to_Zlt/ |
---|
96 | qed. |
---|
97 | |
---|
98 | definition joint_classify_step : |
---|
99 | ∀p : evaluation_params.joint_step p (globals … p) → status_class ≝ |
---|
100 | λp,stmt. |
---|
101 | match stmt with |
---|
102 | [ CALL f args dest ⇒ cl_call |
---|
103 | | COND _ _ ⇒ cl_jump |
---|
104 | | _ ⇒ cl_other |
---|
105 | ]. |
---|
106 | |
---|
107 | definition joint_classify_final : |
---|
108 | ∀p : evaluation_params.joint_fin_step p → status_class ≝ |
---|
109 | λp,stmt. |
---|
110 | match stmt with |
---|
111 | [ GOTO _ ⇒ cl_other |
---|
112 | | RETURN ⇒ cl_return |
---|
113 | | TAILCALL _ f args ⇒ cl_tailcall |
---|
114 | ]. |
---|
115 | |
---|
116 | definition joint_classify : |
---|
117 | ∀p : evaluation_params.state_pc p → status_class ≝ |
---|
118 | λp,st. |
---|
119 | match fetch_statement … (ev_genv p) (pc … st) with |
---|
120 | [ OK i_fn_s ⇒ |
---|
121 | match \snd i_fn_s with |
---|
122 | [ sequential s _ ⇒ joint_classify_step p s |
---|
123 | | final s ⇒ joint_classify_final p s |
---|
124 | | FCOND _ _ _ _ ⇒ cl_jump |
---|
125 | ] |
---|
126 | | _ ⇒ cl_other |
---|
127 | ]. |
---|
128 | |
---|
129 | lemma joint_classify_call : ∀p : evaluation_params.∀st. |
---|
130 | joint_classify p st = cl_call → |
---|
131 | ∃i_f,f',args,dest,next. |
---|
132 | fetch_statement … (ev_genv p) (pc … st) = |
---|
133 | OK ? 〈i_f, sequential … (CALL … f' args dest) next〉. |
---|
134 | #p #st |
---|
135 | whd in match joint_classify; normalize nodelta |
---|
136 | inversion (fetch_statement … (ev_genv p) (pc … st)) |
---|
137 | [2: #e #_ whd in ⊢ (??%?→?); #ABS destruct(ABS) ] |
---|
138 | * #i_f * |
---|
139 | [2,3: [ * [ #lbl | | #fl #f #args ] | #fl #a #ltrue #lfalse ] #_ |
---|
140 | normalize nodelta normalize in ⊢ (%→?); #ABS destruct |
---|
141 | ] |
---|
142 | * [ #c | #f' #args #dest | #a #lbl | #s ] #nxt #fetch normalize nodelta |
---|
143 | normalize in ⊢ (%→?); #EQ destruct |
---|
144 | %[|%[|%[|%[|%[| %]]]]] |
---|
145 | qed. |
---|
146 | |
---|
147 | definition joint_after_ret : ∀p:evaluation_params. |
---|
148 | (Σs : state_pc p.joint_classify p s = cl_call) → state_pc p → Prop ≝ |
---|
149 | λp,s1,s2. |
---|
150 | match fetch_statement … (ev_genv p) (pc … s1) with |
---|
151 | [ OK x ⇒ |
---|
152 | match \snd x with |
---|
153 | [ sequential s next ⇒ |
---|
154 | last_pop … s2 = pc … s1 ∧ |
---|
155 | pc … s2 = succ_pc p (pc … s1) next |
---|
156 | | _ ⇒ False (* never happens *) |
---|
157 | ] |
---|
158 | | _ ⇒ False (* never happens *) |
---|
159 | ]. |
---|
160 | |
---|
161 | definition joint_call_ident : ∀p:evaluation_params. |
---|
162 | state_pc p → ident ≝ |
---|
163 | (* this is a definition without a dummy ident : |
---|
164 | λp,st. |
---|
165 | match ? |
---|
166 | return λx. |
---|
167 | !〈f, s〉 ← fetch_statement ? p … (ev_genv p) (pc … st) ; |
---|
168 | match s with |
---|
169 | [ sequential s next ⇒ |
---|
170 | match s with |
---|
171 | [ step_seq s ⇒ |
---|
172 | match s with |
---|
173 | [ CALL f' args dest ⇒ |
---|
174 | function_of_call … (ev_genv p) st f' |
---|
175 | | _ ⇒ Error … [ ] |
---|
176 | ] |
---|
177 | | _ ⇒ Error … [ ] |
---|
178 | ] |
---|
179 | | _ ⇒ Error … [ ] |
---|
180 | ] = x → ? with |
---|
181 | [ OK v ⇒ λ_.v |
---|
182 | | Error e ⇒ λABS.⊥ |
---|
183 | ] (refl …). |
---|
184 | @hide_prf |
---|
185 | elim (joint_classify_call … (pi2 … st)) |
---|
186 | #f *#f' *#args *#dest *#next *#fn *#fd ** #EQ1 #EQ2 #EQ3 |
---|
187 | lapply ABS -ABS |
---|
188 | >EQ1 >m_return_bind normalize nodelta >EQ2 #ABS destruct(ABS) |
---|
189 | qed. *) |
---|
190 | (* with a dummy ident (which is never used as seen above in the commented script) |
---|
191 | I think handling of the function is easier *) |
---|
192 | λp,st. |
---|
193 | let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *) |
---|
194 | match fetch_statement … (ev_genv p) (pc … st) with |
---|
195 | [ OK x ⇒ |
---|
196 | match \snd x with |
---|
197 | [ sequential s next ⇒ |
---|
198 | match s with |
---|
199 | [ step_seq s ⇒ |
---|
200 | match s with |
---|
201 | [ CALL f' args dest ⇒ |
---|
202 | match |
---|
203 | (! bl ← block_of_call … (ev_genv p) f' st; |
---|
204 | fetch_internal_function … (ev_genv p) bl) with |
---|
205 | [ OK i_f ⇒ \fst i_f |
---|
206 | | _ ⇒ dummy |
---|
207 | ] |
---|
208 | | _ ⇒ dummy |
---|
209 | ] |
---|
210 | | _ ⇒ dummy |
---|
211 | ] |
---|
212 | | _ ⇒ dummy |
---|
213 | ] |
---|
214 | | _ ⇒ dummy |
---|
215 | ]. |
---|
216 | |
---|
217 | definition joint_tailcall_ident : ∀p:evaluation_params. |
---|
218 | state_pc p → ident ≝ |
---|
219 | λp,st. |
---|
220 | let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *) |
---|
221 | match fetch_statement … (ev_genv p) (pc … st) with |
---|
222 | [ OK x ⇒ |
---|
223 | match \snd x with |
---|
224 | [ final s ⇒ |
---|
225 | match s with |
---|
226 | [ TAILCALL _ f' args ⇒ |
---|
227 | match |
---|
228 | (! bl ← block_of_call … (ev_genv p) f' st; |
---|
229 | fetch_internal_function … (ev_genv p) bl) with |
---|
230 | [ OK i_f ⇒ \fst i_f |
---|
231 | | _ ⇒ dummy |
---|
232 | ] |
---|
233 | | _ ⇒ dummy |
---|
234 | ] |
---|
235 | | _ ⇒ dummy |
---|
236 | ] |
---|
237 | | _ ⇒ dummy |
---|
238 | ]. |
---|
239 | |
---|
240 | definition pcDeq ≝ mk_DeqSet program_counter eq_program_counter ?. |
---|
241 | *#p1 #EQ1 * #p2 #EQ2 @eq_program_counter_elim |
---|
242 | [ #EQ destruct % #H % |
---|
243 | | * #NEQ % #ABS destruct elim (NEQ ?) % |
---|
244 | ] |
---|
245 | qed. |
---|
246 | |
---|
247 | (* |
---|
248 | let rec io_evaluate O I X (env : ∀o.res (I o)) (c : IO O I X) on c : res X ≝ |
---|
249 | match c with |
---|
250 | [ Value x ⇒ OK … x |
---|
251 | | Interact o f ⇒ |
---|
252 | ! i ← env o ; |
---|
253 | io_evaluate O I X env (f i) |
---|
254 | | Wrong e ⇒ Error … e |
---|
255 | ]. |
---|
256 | *) |
---|
257 | |
---|
258 | definition cost_label_of_stmt : |
---|
259 | ∀p : evaluation_params.joint_statement p (globals p) → option costlabel ≝ |
---|
260 | λp,s.match s with |
---|
261 | [ sequential s _ ⇒ |
---|
262 | match s with |
---|
263 | [ COST_LABEL lbl ⇒ Some ? lbl |
---|
264 | | _ ⇒ None ? |
---|
265 | ] |
---|
266 | | _ ⇒ None ? |
---|
267 | ]. |
---|
268 | |
---|
269 | definition joint_label_of_pc ≝ |
---|
270 | λp : evaluation_params. |
---|
271 | (λpc. |
---|
272 | match fetch_statement … (ev_genv p) pc with |
---|
273 | [ OK fn_stmt ⇒ cost_label_of_stmt … (\snd fn_stmt) |
---|
274 | | _ ⇒ None ? |
---|
275 | ]). |
---|
276 | |
---|
277 | definition joint_abstract_status : |
---|
278 | ∀p : prog_params. |
---|
279 | abstract_status ≝ |
---|
280 | λp. |
---|
281 | mk_abstract_status |
---|
282 | (* as_status ≝ *) (state_pc p) |
---|
283 | (* as_execute ≝ *) |
---|
284 | (λs1,s2.eval_state … (ev_genv p) s1 = return s2) |
---|
285 | (* (* as_init ≝ *) (make_initial_state p) *) |
---|
286 | (* as_pc ≝ *) pcDeq |
---|
287 | (* as_pc_of ≝ *) (pc …) |
---|
288 | (* as_classify ≝ *) (joint_classify p) |
---|
289 | (* as_label_of_pc ≝ *) (joint_label_of_pc p) |
---|
290 | (* as_after_return ≝ *) (joint_after_ret p) |
---|
291 | (* as_result ≝ *) (is_final p (globals ?) (ev_genv p) exit_pc) |
---|
292 | (* as_call_ident ≝ *) (λst.joint_call_ident p st) |
---|
293 | (* as_tailcall_ident ≝ *) (λst.joint_tailcall_ident p st). |
---|
294 | |
---|
295 | (* alternative definition with integrated prog_params constructor *) |
---|
296 | definition joint_status : |
---|
297 | ∀p : sem_params. |
---|
298 | joint_program p → (ident → option ℕ) → abstract_status ≝ |
---|
299 | λp,prog,stacksizes.joint_abstract_status (mk_prog_params p prog stacksizes). |
---|