source: src/joint/Traces.ma @ 2681

Last change on this file since 2681 was 2681, checked in by tranquil, 8 years ago
  • improvements to the graph translation function
  • fixed passes up to LTL
File size: 9.0 KB
Line 
1include "joint/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
49let spars ≝ prog_spars pars in
50let genv ≝ globalenv_noinit ? p in
51let get_pc_lbl ≝ λid,lbl.
52  ! bl ← block_of_funct_id … spars genv id ;
53  pc_of_label … genv bl lbl in
54mk_evaluation_params
55  (prog_var_names … p)
56  spars
57  (mk_genv_gen ?? genv ? (stack_sizes pars) get_pc_lbl)
58 (* (prog_io pars) *).
59#s #H
60elim (find_symbol_exists … p s ?)
61[ #bl #EQ >EQ % #ABS destruct(ABS)|*:]
62@Exists_append_r
63@(Exists_mp … H) //
64qed.
65
66coercion prog_params_to_ev_params : ∀p : prog_params.evaluation_params
67≝ make_global on _p : prog_params to evaluation_params.
68
69definition make_initial_state :
70 ∀pars: prog_params.res (state_pc pars) ≝
71λpars.let p ≝ prog pars in
72  let sem_globals : evaluation_params ≝ pars in
73  let ge ≝ ev_genv sem_globals in
74  let m0 ≝ alloc_mem … p in
75  let 〈m,spb〉 as H ≝ alloc … m0 0 external_ram_size in
76  let spp : xpointer ≝
77    «mk_pointer spb (mk_offset (bitvector_of_Z ? external_ram_size)),
78     pi2 … spb» in
79(*  let ispp : xpointer ≝ mk_pointer ispb (mk_offset (bitvector_of_nat ? 47)) in *)
80  let main ≝ prog_main … p in
81  let st0 ≝ mk_state pars (empty_framesT …) empty_is (BBbit false) (empty_regsT … spp) m in
82  (* use exit_pc as ra and call_dest_for_main as dest *)
83  let st0' ≝ mk_state_pc … (set_sp … spp st0) exit_pc exit_pc in
84  ! st0_no_pc ← save_frame ?? sem_globals ID (call_dest_for_main … pars) (prog_main … p) st0' ;
85  let st0'' ≝ set_no_pc … st0_no_pc st0' in
86  ! bl ← block_of_call … ge (inl … main) st0'';
87  ! 〈i, fn〉 ← fetch_function … ge bl ;
88  match fn with
89  [ Internal ifn ⇒
90    ! st' ← eval_internal_call … ge i ifn (call_args_for_main … pars) st0'' ;
91    let pc' ≝ pc_of_point … bl (joint_if_entry … ifn) in
92    return mk_state_pc … st' pc' exit_pc
93  | External _ ⇒ Error … [MSG BadMain; CTX ? main] (* External main not supported: why? *)
94  ].
95cases m0 in H; #m1 #m2 #m3 #H
96whd in H:(???%); destruct whd in ⊢(??%?); @Zleb_elim_Type0 // #abs @⊥
97@(absurd … (irreflexive_Zlt …)) % #I cases (I OZ) /3 by Zlt_to_Zle_to_Zlt/
98qed.
99
100definition joint_classify_step :
101  ∀p : evaluation_params.joint_step p (globals … p) → status_class ≝
102  λp,stmt.
103  match stmt with
104  [ step_seq _ ⇒ cl_other
105  | CALL f args dest ⇒ cl_call
106  | COND _ _ ⇒ cl_jump
107  ].
108
109definition joint_classify_final :
110  ∀p : evaluation_params.joint_fin_step p → status_class ≝
111  λp,stmt.
112  match stmt with
113  [ GOTO _ ⇒ cl_other
114  | RETURN ⇒ cl_return
115  | TAILCALL _ f args ⇒ cl_tailcall
116  ].
117
118definition joint_classify :
119  ∀p : evaluation_params.state_pc p → option status_class ≝
120  λp,st.
121  ! 〈i,f,s〉 ← res_to_opt … (fetch_statement … (ev_genv p) (pc … st)) ;
122  match s with
123  [ sequential s _ ⇒ Some ? (joint_classify_step p s)
124  | final s ⇒ Some ? (joint_classify_final p s)
125  | FCOND _ _ _ _ ⇒ Some ? cl_jump
126  ].
127
128definition no_cost_label : ∀p,g.joint_seq p g → Prop ≝
129  λp,g,s.match s with
130  [ COST_LABEL _ ⇒ False
131  | _ ⇒ True
132  ].
133
134lemma joint_classify_call : ∀p : evaluation_params.∀st.
135  joint_classify p st = Some ? cl_call →
136  ∃i_f,f',args,dest,next.
137  fetch_statement … (ev_genv p) (pc … st) =
138    OK ? 〈i_f, sequential … (CALL … f' args dest) next〉.
139#p #st
140whd in match joint_classify; normalize nodelta
141inversion (fetch_statement … (ev_genv p) (pc … st))
142[2: #e #_ whd in ⊢ (??%?→?); #ABS destruct(ABS) ]
143* #i_f *
144[2,3: [ * [ #lbl | | #fl #f #args ] | #fl #a #ltrue #lfalse ] #_
145  >m_return_bind normalize nodelta normalize in ⊢ (%→?); #ABS destruct
146]
147* [ #f' #args #dest | #a #lbl | #s ] #nxt #fetch >m_return_bind
148normalize in ⊢ (%→?); #EQ destruct
149%[|%[|%[|%[|%[| %]]]]]
150qed.
151
152definition joint_after_ret : ∀p:evaluation_params.
153  (Σs : state_pc p.joint_classify p s = Some ? cl_call) → state_pc p → Prop ≝
154λp,s1,s2.
155match fetch_statement … (ev_genv p) (pc … s1) with
156[ OK x ⇒
157  match \snd x with
158  [ sequential s next ⇒
159    last_pop … s2 = pc … s1 ∧
160    pc … s2 = succ_pc p (pc … s1) next
161  | _ ⇒ False (* never happens *)
162  ]
163| _ ⇒ False (* never happens *)
164].
165
166definition joint_call_ident : ∀p:evaluation_params.
167  (Σs : state_pc p.joint_classify p s = Some ? cl_call) → ident ≝
168(* this is a definition without a dummy ident :
169λp,st.
170match ?
171return λx.
172  !〈f, s〉 ← fetch_statement ? p … (ev_genv p) (pc … st) ;
173  match s with
174  [ sequential s next ⇒
175    match s with
176    [ step_seq s ⇒
177      match s with
178      [ CALL f' args dest ⇒
179        function_of_call … (ev_genv p) st f'
180      | _ ⇒ Error … [ ]
181      ]
182    | _ ⇒ Error … [ ]
183    ]
184  | _ ⇒ Error … [ ]
185  ] = x → ? with
186[ OK v ⇒ λ_.v
187| Error e ⇒ λABS.⊥
188] (refl …).
189@hide_prf
190elim (joint_classify_call … (pi2 … st))
191#f *#f' *#args *#dest *#next *#fn *#fd ** #EQ1 #EQ2 #EQ3
192lapply ABS -ABS
193>EQ1 >m_return_bind normalize nodelta >EQ2 #ABS destruct(ABS)
194qed. *)
195(* with a dummy ident (which is never used as seen above in the commented script)
196   I think handling of the function is easier *)
197λp,st.
198let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *)
199match fetch_statement … (ev_genv p) (pc … st) with
200[ OK x ⇒
201  match \snd x with
202  [ sequential s next ⇒
203    match s with
204    [ step_seq s ⇒
205      match s with
206      [ CALL f' args dest ⇒
207        match
208          (! bl ← block_of_call … (ev_genv p) f' st;
209           fetch_internal_function … (ev_genv p) bl) with
210        [ OK i_f ⇒ \fst i_f
211        | _ ⇒ dummy
212        ]
213      | _ ⇒ dummy
214      ]
215    | _ ⇒ dummy
216    ]
217  | _ ⇒ dummy
218  ]
219| _ ⇒ dummy
220].
221
222definition joint_tailcall_ident : ∀p:evaluation_params.
223  (Σs : state_pc p.joint_classify p s = Some ? cl_tailcall) → ident ≝
224λp,st.
225let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *)
226match fetch_statement … (ev_genv p) (pc … st) with
227[ OK x ⇒
228  match \snd x with
229  [ final s ⇒
230    match s with
231    [ TAILCALL _ f' args ⇒
232      match
233        (! bl ← block_of_call … (ev_genv p) f' st;
234         fetch_internal_function … (ev_genv p) bl) with
235      [ OK i_f ⇒ \fst i_f
236      | _ ⇒ dummy
237      ]
238    | _ ⇒ dummy
239    ]
240  | _ ⇒ dummy
241  ]
242| _ ⇒ dummy
243].
244
245definition pcDeq ≝ mk_DeqSet program_counter eq_program_counter ?.
246*#p1 #EQ1 * #p2 #EQ2 @eq_program_counter_elim
247[ #EQ destruct % #H %
248| * #NEQ % #ABS destruct elim (NEQ ?) %
249]
250qed.
251
252(*
253let rec io_evaluate O I X (env : ∀o.res (I o)) (c : IO O I X) on c : res X ≝
254  match c with
255  [ Value x ⇒ OK … x
256  | Interact o f ⇒
257    ! i ← env o ;
258    io_evaluate O I X env (f i)
259  | Wrong e ⇒ Error … e
260  ].
261*)
262
263definition cost_label_of_stmt :
264  ∀p : evaluation_params.joint_statement p (globals p) → option costlabel ≝
265  λp,s.match s with
266  [ sequential s _ ⇒
267    match s with
268    [ step_seq s ⇒
269      match s with
270      [ COST_LABEL lbl ⇒ Some ? lbl
271      | _ ⇒ None ?
272      ]
273    | _ ⇒ None ?
274    ]
275  | _ ⇒ None ?
276  ].
277
278
279lemma no_label_uncosted : ∀p : evaluation_params.∀s,nxt.
280no_cost_label p (globals p) s →
281cost_label_of_stmt … (sequential … s nxt) = None ?.
282#p * // #lbl #next *
283qed.
284
285definition joint_abstract_status :
286 ∀p : evaluation_params.
287 abstract_status ≝
288 λp.
289 mk_abstract_status
290   (* as_status ≝ *) (state_pc p)
291   (* as_execute ≝ *)
292    (λs1,s2.(* io_evaluate … (io_env p s1) *) (eval_state … (ev_genv p) s1) = return s2)
293   (* as_pc ≝ *) pcDeq
294   (* as_pc_of ≝ *) (pc …)
295   (* as_classify ≝ *) (joint_classify p)
296   (* as_label_of_pc ≝ *)
297    (λpc.
298      match fetch_statement … (ev_genv p) pc with
299      [ OK fn_stmt ⇒ cost_label_of_stmt … (\snd fn_stmt)
300      | _ ⇒ None ?
301      ])
302   (* as_after_return ≝ *) (joint_after_ret p)
303   (* as_final ≝ *) (λs.is_final p  (globals ?) (ev_genv p) exit_pc s ≠ None ?)
304   (* as_call_ident ≝ *) (joint_call_ident p)
305   (* as_tailcall_ident ≝ *) (joint_tailcall_ident p).
Note: See TracBrowser for help on using the repository browser.