source: src/joint/Traces.ma @ 2477

Last change on this file since 2477 was 2477, checked in by tranquil, 8 years ago

status_simulation reformulated
definition of joint_classify split up in subdefinitions

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