source: src/joint/Traces.ma @ 2688

Last change on this file since 2688 was 2688, checked in by tranquil, 7 years ago
  • in Arithmeticcs.ma: commented include that breaks script in latest matita
  • moved COST_LABEL out of joint_seq
File size: 8.6 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  [ CALL f args dest ⇒ cl_call
105  | COND _ _ ⇒ cl_jump
106  | _ ⇒ cl_other
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
128lemma joint_classify_call : ∀p : evaluation_params.∀st.
129  joint_classify p st = Some ? cl_call →
130  ∃i_f,f',args,dest,next.
131  fetch_statement … (ev_genv p) (pc … st) =
132    OK ? 〈i_f, sequential … (CALL … f' args dest) next〉.
133#p #st
134whd in match joint_classify; normalize nodelta
135inversion (fetch_statement … (ev_genv p) (pc … st))
136[2: #e #_ whd in ⊢ (??%?→?); #ABS destruct(ABS) ]
137* #i_f *
138[2,3: [ * [ #lbl | | #fl #f #args ] | #fl #a #ltrue #lfalse ] #_
139  >m_return_bind normalize nodelta normalize in ⊢ (%→?); #ABS destruct
140]
141* [ #c | #f' #args #dest | #a #lbl | #s ] #nxt #fetch >m_return_bind
142normalize in ⊢ (%→?); #EQ destruct
143%[|%[|%[|%[|%[| %]]]]]
144qed.
145
146definition joint_after_ret : ∀p:evaluation_params.
147  (Σs : state_pc p.joint_classify p s = Some ? cl_call) → state_pc p → Prop ≝
148λp,s1,s2.
149match fetch_statement … (ev_genv p) (pc … s1) with
150[ OK x ⇒
151  match \snd x with
152  [ sequential s next ⇒
153    last_pop … s2 = pc … s1 ∧
154    pc … s2 = succ_pc p (pc … s1) next
155  | _ ⇒ False (* never happens *)
156  ]
157| _ ⇒ False (* never happens *)
158].
159
160definition joint_call_ident : ∀p:evaluation_params.
161  (Σs : state_pc p.joint_classify p s = Some ? cl_call) → ident ≝
162(* this is a definition without a dummy ident :
163λp,st.
164match ?
165return λx.
166  !〈f, s〉 ← fetch_statement ? p … (ev_genv p) (pc … st) ;
167  match s with
168  [ sequential s next ⇒
169    match s with
170    [ step_seq s ⇒
171      match s with
172      [ CALL f' args dest ⇒
173        function_of_call … (ev_genv p) st f'
174      | _ ⇒ Error … [ ]
175      ]
176    | _ ⇒ Error … [ ]
177    ]
178  | _ ⇒ Error … [ ]
179  ] = x → ? with
180[ OK v ⇒ λ_.v
181| Error e ⇒ λABS.⊥
182] (refl …).
183@hide_prf
184elim (joint_classify_call … (pi2 … st))
185#f *#f' *#args *#dest *#next *#fn *#fd ** #EQ1 #EQ2 #EQ3
186lapply ABS -ABS
187>EQ1 >m_return_bind normalize nodelta >EQ2 #ABS destruct(ABS)
188qed. *)
189(* with a dummy ident (which is never used as seen above in the commented script)
190   I think handling of the function is easier *)
191λp,st.
192let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *)
193match fetch_statement … (ev_genv p) (pc … st) with
194[ OK x ⇒
195  match \snd x with
196  [ sequential s next ⇒
197    match s with
198    [ step_seq s ⇒
199      match s with
200      [ CALL f' args dest ⇒
201        match
202          (! bl ← block_of_call … (ev_genv p) f' st;
203           fetch_internal_function … (ev_genv p) bl) with
204        [ OK i_f ⇒ \fst i_f
205        | _ ⇒ dummy
206        ]
207      | _ ⇒ dummy
208      ]
209    | _ ⇒ dummy
210    ]
211  | _ ⇒ dummy
212  ]
213| _ ⇒ dummy
214].
215
216definition joint_tailcall_ident : ∀p:evaluation_params.
217  (Σs : state_pc p.joint_classify p s = Some ? cl_tailcall) → ident ≝
218λp,st.
219let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *)
220match fetch_statement … (ev_genv p) (pc … st) with
221[ OK x ⇒
222  match \snd x with
223  [ final s ⇒
224    match s with
225    [ TAILCALL _ f' args ⇒
226      match
227        (! bl ← block_of_call … (ev_genv p) f' st;
228         fetch_internal_function … (ev_genv p) bl) with
229      [ OK i_f ⇒ \fst i_f
230      | _ ⇒ dummy
231      ]
232    | _ ⇒ dummy
233    ]
234  | _ ⇒ dummy
235  ]
236| _ ⇒ dummy
237].
238
239definition pcDeq ≝ mk_DeqSet program_counter eq_program_counter ?.
240*#p1 #EQ1 * #p2 #EQ2 @eq_program_counter_elim
241[ #EQ destruct % #H %
242| * #NEQ % #ABS destruct elim (NEQ ?) %
243]
244qed.
245
246(*
247let rec io_evaluate O I X (env : ∀o.res (I o)) (c : IO O I X) on c : res X ≝
248  match c with
249  [ Value x ⇒ OK … x
250  | Interact o f ⇒
251    ! i ← env o ;
252    io_evaluate O I X env (f i)
253  | Wrong e ⇒ Error … e
254  ].
255*)
256
257definition cost_label_of_stmt :
258  ∀p : evaluation_params.joint_statement p (globals p) → option costlabel ≝
259  λp,s.match s with
260  [ sequential s _ ⇒
261    match s with
262    [  COST_LABEL lbl ⇒ Some ? lbl
263    | _ ⇒ None ?
264    ]
265  | _ ⇒ None ?
266  ].
267
268definition joint_abstract_status :
269 ∀p : evaluation_params.
270 abstract_status ≝
271 λp.
272 mk_abstract_status
273   (* as_status ≝ *) (state_pc p)
274   (* as_execute ≝ *)
275    (λs1,s2.(* io_evaluate … (io_env p s1) *) (eval_state … (ev_genv p) s1) = return s2)
276   (* as_pc ≝ *) pcDeq
277   (* as_pc_of ≝ *) (pc …)
278   (* as_classify ≝ *) (joint_classify p)
279   (* as_label_of_pc ≝ *)
280    (λpc.
281      match fetch_statement … (ev_genv p) pc with
282      [ OK fn_stmt ⇒ cost_label_of_stmt … (\snd fn_stmt)
283      | _ ⇒ None ?
284      ])
285   (* as_after_return ≝ *) (joint_after_ret p)
286   (* as_final ≝ *) (λs.is_final p  (globals ?) (ev_genv p) exit_pc s ≠ None ?)
287   (* as_call_ident ≝ *) (joint_call_ident p)
288   (* as_tailcall_ident ≝ *) (joint_tailcall_ident p).
Note: See TracBrowser for help on using the repository browser.