source: src/joint/Traces.ma @ 2561

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