include "joint/semanticsUtils.ma". include "common/StructuredTraces.ma". record evaluation_params (p : sem_params) : Type[0] ≝ { globals : list ident ; ev_genv :> genv p globals }. record prog_params : Type[1] ≝ { prog_spars :> sem_params ; prog : joint_program prog_spars ; stack_sizes : ident → option ℕ (* ; prog_io : state prog_spars → ∀o.res (io_in o) *) }. definition joint_make_global : ∀p : prog_params.evaluation_params (prog_spars p) ≝ λp.mk_evaluation_params ? (prog_names … (prog p)) (joint_globalenv p (prog p) (stack_sizes p)). coercion joint_make_global : ∀p : prog_params.evaluation_params (prog_spars p) ≝ joint_make_global on p : prog_params to evaluation_params ?. definition make_initial_state : ∀pars: prog_params.res (state_pc pars) ≝ λpars.let p ≝ prog pars in let ge ≝ ev_genv pars in (* this is going to change shortly: globals will not reside in globalenv anymore *) ! m0 ← init_mem … (λx.x) p ; let 〈m,spb〉 as H ≝ alloc … m0 0 external_ram_size in let globals_size ≝ globals_stacksize … p in (* stack pointer should start at 2^16 - |globals|, right? first call to main shuold bring it to 2^16 - |globals| - |main stack| Also, on words 2^16 - |globals| = - |globals| *) (* mcu8051ide disallows byte FFFFh of XDATA... bug or feature? *) let spp : xpointer ≝ «mk_pointer spb (mk_offset (bitvector_of_Z 16 (-S (globals_size)))), pi2 … spb» in (* let ispp : xpointer ≝ mk_pointer ispb (mk_offset (bitvector_of_nat ? 47)) in *) let main ≝ prog_main … p in let st ≝ mk_state pars (* frames ≝ *)(Some ? (empty_framesT …)) (* internal_stack ≝ *) empty_is (* carry ≝ *)(BBbit false) (* regs ≝ *)(empty_regsT … spp) (* mem ≝ *)m (* stack_usage ≝ *)0 in return mk_state_pc ? (* state_no_pc ≝ *)(set_sp … spp st) (* pc ≝ *)init_pc (* last_pop ≝ *)(null_pc one). @hide_prf cases m0 in H; #m1 #m2 #m3 #H whd in H:(???%); destruct whd in ⊢(??%?); @Zleb_elim_Type0 // #abs @⊥ @(absurd … (irreflexive_Zlt …)) % #I cases (I OZ) /3 by Zlt_to_Zle_to_Zlt/ qed. definition joint_classify_step : ∀p,globals.joint_step p globals → status_class ≝ λp,g,stmt. match stmt with [ CALL f args dest ⇒ cl_call | COND _ _ ⇒ cl_jump | _ ⇒ cl_other ]. definition joint_classify_final : ∀p.joint_fin_step p → status_class ≝ λp,stmt. match stmt with [ GOTO _ ⇒ cl_other | RETURN ⇒ cl_return | TAILCALL _ f args ⇒ cl_tailcall ]. definition joint_classify : ∀p.∀pars : evaluation_params p.state_pc p → status_class ≝ λp,pars,st. match fetch_statement … (ev_genv … pars) (pc … st) with [ OK i_fn_s ⇒ match \snd i_fn_s with [ sequential s _ ⇒ joint_classify_step … s | final s ⇒ joint_classify_final … s | FCOND _ _ _ _ ⇒ cl_jump ] | _ ⇒ cl_other ]. lemma joint_classify_call : ∀p,pars,st. joint_classify p pars st = cl_call → ∃i_f,f',args,dest,next. fetch_statement … (ev_genv … pars) (pc … st) = OK ? 〈i_f, sequential … (CALL … f' args dest) next〉. #p #pars #st whd in match joint_classify; normalize nodelta inversion (fetch_statement ????) [2: #e #_ whd in ⊢ (??%?→?); #ABS destruct(ABS) ] * #i_f * [2,3: [ * [ #lbl | | #fl #f #args ] | #fl #a #ltrue #lfalse ] #_ normalize nodelta normalize in ⊢ (%→?); #ABS destruct ] * [ #c | #f' #args #dest | #a #lbl | #s ] #nxt #fetch normalize nodelta normalize in ⊢ (%→?); #EQ destruct %[|%[|%[|%[|%[| %]]]]] qed. definition joint_after_ret : ∀p : sem_params.∀pars. (Σs : state_pc p.joint_classify p pars s = cl_call) → state_pc p → Prop ≝ λp,pars,s1,s2. match fetch_statement … (ev_genv … pars) (pc … s1) with [ OK x ⇒ match \snd x with [ sequential s next ⇒ last_pop … s2 = pc … s1 ∧ pc … s2 = succ_pc p (pc … s1) next | _ ⇒ False (* never happens *) ] | _ ⇒ False (* never happens *) ]. definition joint_call_ident : ∀p : sem_params.∀pars. state_pc p → ident ≝ (* this is a definition without a dummy ident : λp,st. match ? return λx. !〈f, s〉 ← fetch_statement ? p … (ev_genv p) (pc … st) ; match s with [ sequential s next ⇒ match s with [ CALL f' args dest ⇒ function_of_call … (ev_genv p) st f' | _ ⇒ Error … [ ] ] | _ ⇒ Error … [ ] ] = x → ? with [ OK v ⇒ λ_.v | Error e ⇒ λABS.⊥ ] (refl …). @hide_prf elim (joint_classify_call … (pi2 … st)) #f *#f' *#args *#dest *#next *#fn *#fd ** #EQ1 #EQ2 #EQ3 lapply ABS -ABS >EQ1 >m_return_bind normalize nodelta >EQ2 #ABS destruct(ABS) qed. *) (* with a dummy ident (which is never used as seen above in the commented script) I think handling of the function is easier *) λp,pars,st. let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *) match fetch_statement … (ev_genv … pars) (pc … st) with [ OK x ⇒ match \snd x with [ sequential s next ⇒ match s with [ CALL f' args dest ⇒ match (! bl ← block_of_call … (ev_genv … pars) f' st; fetch_internal_function … (ev_genv … pars) bl) with [ OK i_f ⇒ \fst i_f | _ ⇒ dummy ] | _ ⇒ dummy ] | _ ⇒ dummy ] | _ ⇒ dummy ]. definition joint_tailcall_ident : ∀p:sem_params.∀pars. state_pc p → ident ≝ λp,pars,st. let dummy : ident ≝ an_identifier ? one in (* used where it cannot ever happen *) match fetch_statement … (ev_genv … pars) (pc … st) with [ OK x ⇒ match \snd x with [ final s ⇒ match s with [ TAILCALL _ f' args ⇒ match (! bl ← block_of_call … (ev_genv … pars) f' st; fetch_internal_function … (ev_genv … pars) bl) with [ OK i_f ⇒ \fst i_f | _ ⇒ dummy ] | _ ⇒ dummy ] | _ ⇒ dummy ] | _ ⇒ dummy ]. definition pcDeq ≝ mk_DeqSet program_counter eq_program_counter ?. *#p1 #EQ1 * #p2 #EQ2 @eq_program_counter_elim [ #EQ destruct % #H % | * #NEQ % #ABS destruct elim (NEQ ?) % ] qed. (* let rec io_evaluate O I X (env : ∀o.res (I o)) (c : IO O I X) on c : res X ≝ match c with [ Value x ⇒ OK … x | Interact o f ⇒ ! i ← env o ; io_evaluate O I X env (f i) | Wrong e ⇒ Error … e ]. *) definition cost_label_of_stmt : ∀p,globals.joint_statement p globals → option costlabel ≝ λp,g,s.match s with [ sequential s _ ⇒ match s with [ COST_LABEL lbl ⇒ Some ? lbl | _ ⇒ None ? ] | _ ⇒ None ? ]. definition joint_label_of_pc ≝ λp,pars. (λpc. match fetch_statement … (ev_genv p pars) pc with [ OK fn_stmt ⇒ cost_label_of_stmt … (\snd fn_stmt) | _ ⇒ None ? ]). (* the prime is to render obsolete old definition of exit_pc, remove when all is corrected *) definition exit_pc' : program_counter ≝ mk_program_counter «mk_block (-1), refl …» (p1 one). definition joint_final: ∀p: sem_params.∀pars. state_pc p → option int ≝ λp,pars,st. let ge ≝ ev_genv p pars in if eq_program_counter (pc … st) exit_pc' then let ret ≝ call_dest_for_main ?? p in match (! vals ← read_result … ge ret st ; Word_of_list_beval vals) with [ OK v ⇒ Some ? v | Error _ ⇒ Some … (maximum ?) ] else None ?. definition joint_abstract_status : ∀p : prog_params. abstract_status ≝ λp. mk_abstract_status (* as_status ≝ *) (state_pc p) (* as_execute ≝ *) (λs1,s2.eval_state … (ev_genv … p) s1 = return s2) (* (* as_init ≝ *) (make_initial_state p) *) (* as_pc ≝ *) pcDeq (* as_pc_of ≝ *) (pc …) (* as_classify ≝ *) (joint_classify … p) (* as_label_of_pc ≝ *) (joint_label_of_pc … p) (* as_after_return ≝ *) (joint_after_ret … p) (* as_result ≝ *) (joint_final … p) (* as_call_ident ≝ *) (λst.joint_call_ident … p st) (* as_tailcall_ident ≝ *) (λst.joint_tailcall_ident … p st). (* alternative definition with integrated prog_params constructor *) definition joint_status : ∀p : sem_params. joint_program p → (ident → option ℕ) → abstract_status ≝ λp,prog,stacksizes.joint_abstract_status (mk_prog_params p prog stacksizes).