(* XXX NB: I haven't checked all of these semantics against the prototype compilers yet! *) include "RTLabs/RTLabs-syntax.ma". include "common/Errors.ma". include "common/Globalenvs.ma". include "common/IO.ma". include "common/SmallstepExec.ma". definition genv ≝ (genv_t Genv) (fundef internal_function). record frame : Type[0] ≝ { func : internal_function ; locals : register_env split_val ; next : label ; sp : block ; retdst : registers (* XXX: not optional? *) }. definition adv : label → frame → frame ≝ λl,f. mk_frame (func f) (locals f) l (sp f) (retdst f). inductive state : Type[0] ≝ | State : ∀ f : frame. ∀ fs : list frame. ∀ m : mem. state | Callstate : ∀ fd : fundef internal_function. ∀args : list val. ∀ dst : registers. ∀ stk : list frame. ∀ m : mem. state | Returnstate : ∀ rtv : val. ∀ dst : registers. ∀ stk : list frame. ∀ m : mem. state. definition mem_of_state : state → mem ≝ λs. match s with [ State _ _ m ⇒ m | Callstate _ _ _ _ m ⇒ m | Returnstate _ _ _ m ⇒ m ]. definition build_state ≝ λf.λfs.λm.λn. State (adv n f) fs m. definition reg_store ≝ λrs,v,locals. match rs with [ dp n regs ⇒ do vs ← break n v; OK ? (fold_right2_i ??? (λ_.λr,v,lcls. add RegisterTag ? lcls r v) locals n regs vs) ]. let rec params_store (rs:list registers) (vs:list val) (locals : register_env split_val) : res (register_env split_val) ≝ match rs with [ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? ] | cons r rst ⇒ match vs with [ nil ⇒ Error ? | cons v vst ⇒ do locals' ← reg_store r v locals; params_store rst vst locals' ] ]. definition reg_retrieve : register_env ? → registers → res val ≝ λlocals,rs. match rs with [ dp n regs ⇒ do vs ← fold_right_i ??? (λm,r,vs. do vs' ← vs; do v ← opt_to_res … (lookup ?? locals r); OK ? (v:::vs')) (OK ? [[ ]]) regs; merge n vs ]. (* TODO: maybe should make immediate = int or offset; val seems like a cheat here - not a real runtime value *) definition eval_addr : genv → frame → ∀m:addressing. Vector registers (addr_mode_args m) → res val ≝ λge,f,m. match m return λm'.Vector registers (addr_mode_args m') → ? with [ Aindexed i ⇒ λargs. do v ← reg_retrieve (locals f) ((args !!! 0) ?); opt_to_res … (ev_add v (Vint i)) | Aindexed2 ⇒ λargs. do v1 ← reg_retrieve (locals f) ((args !!! 0) ?); do v2 ← reg_retrieve (locals f) ((args !!! 1) ?); opt_to_res … (ev_add v1 v2) | Aglobal id off ⇒ λargs. do loc ← opt_to_res … (find_symbol ?? ge id); OK ? (Vptr Any loc ? (shift_offset zero_offset off)) | Abased id off ⇒ λargs. do loc ← opt_to_res … (find_symbol ?? ge id); do v ← reg_retrieve (locals f) ((args !!! 0) ?); opt_to_res … (ev_add (Vptr Any loc ? zero_offset) v) | Ainstack off ⇒ λargs. OK ? (Vptr Any (sp f) ? (shift_offset zero_offset off)) ] . /2/ [ 1,2: cases loc | cases (sp f) ] // qed. definition is_true_val : val → res bool ≝ λv. match v with [ Vint i ⇒ OK ? (notb (eq i zero)) | Vnull _ ⇒ OK ? false | Vptr _ _ _ _ ⇒ OK ? true | _ ⇒ Error ? ]. (* XXX put somewhere sensible *) let rec nth_opt (A:Type[0]) (n:nat) (l:list A) on l : option A ≝ match l with [ nil ⇒ None ? | cons h t ⇒ match n with [ O ⇒ Some ? h | S m ⇒ nth_opt A m t ] ]. definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝ λge,st. match st with [ State f fs m ⇒ ! s ← lookup ?? (f_graph (func f)) (next f); match s with [ St_skip l ⇒ ret ? 〈E0, build_state f fs m l〉 | St_cost cl l ⇒ ret ? 〈Echarge cl, build_state f fs m l〉 | St_const rs cst l ⇒ ! v ← opt_to_res … (eval_constant (find_symbol … ge) (sp f) cst); ! locals ← reg_store rs v (locals f); ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉 | St_op1 op dst src l ⇒ ! v ← reg_retrieve (locals f) src; ! v' ← opt_to_res … (eval_unop op v); ! locals ← reg_store dst v' (locals f); ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉 | St_op2 op dst src1 src2 l ⇒ ! v1 ← reg_retrieve (locals f) src1; ! v2 ← reg_retrieve (locals f) src2; ! v' ← opt_to_res … (eval_binop op v1 v2); ! locals ← reg_store dst v' (locals f); ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉 | St_load chunk mode args dst l ⇒ ! vaddr ← eval_addr ge f mode args; ! v ← opt_to_res … (loadv chunk m vaddr); ! locals ← reg_store dst v (locals f); ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉 | St_store chunk mode args src l ⇒ ! vaddr ← eval_addr ge f mode args; ! v ← reg_retrieve (locals f) src; ! m' ← opt_to_res … (storev chunk m vaddr v); ret ? 〈E0, build_state f fs m' l〉 | St_call_id id args dst sig l ⇒ (* XXX: haven't used sig *) ! b ← opt_to_res … (find_symbol ?? ge id); ! fd ← opt_to_res … (find_funct_ptr ?? ge b); ! vs ← mmap ?? (reg_retrieve (locals f)) args; ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉 | St_call_ptr frs args dst sig l ⇒ (* XXX: haven't used sig *) ! fv ← reg_retrieve (locals f) frs; ! fd ← opt_to_res … (find_funct ?? ge fv); ! vs ← mmap ?? (reg_retrieve (locals f)) args; ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉 | St_tailcall_id id args sig ⇒ (* XXX: haven't used sig *) ! b ← opt_to_res … (find_symbol ?? ge id); ! fd ← opt_to_res … (find_funct_ptr ?? ge b); ! vs ← mmap ?? (reg_retrieve (locals f)) args; ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉 | St_tailcall_ptr frs args sig ⇒ (* XXX: haven't used sig *) ! fv ← reg_retrieve (locals f) frs; ! fd ← opt_to_res … (find_funct ?? ge fv); ! vs ← mmap ?? (reg_retrieve (locals f)) args; ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉 | St_condcst cst ltrue lfalse ⇒ ! v ← opt_to_res … (eval_constant (find_symbol … ge) (sp f) cst); ! b ← is_true_val v; ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉 | St_cond1 op src ltrue lfalse ⇒ ! v ← reg_retrieve (locals f) src; ! v' ← opt_to_res … (eval_unop op v); ! b ← is_true_val v'; ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉 | St_cond2 op src1 src2 ltrue lfalse ⇒ ! v1 ← reg_retrieve (locals f) src1; ! v2 ← reg_retrieve (locals f) src2; ! v' ← opt_to_res … (eval_binop op v1 v2); ! b ← is_true_val v'; ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉 | St_jumptable rs ls ⇒ ! v ← reg_retrieve (locals f) rs; match v with [ Vint i ⇒ ! l ← nth_opt ? (nat_of_bitvector ? i) ls; ret ? 〈E0, build_state f fs m l〉 | _ ⇒ Wrong ??? ] | St_return src ⇒ ! v ← reg_retrieve (locals f) src; ret ? 〈E0, Returnstate v (retdst f) fs (free m (sp f))〉 ] | Callstate fd params dst fs m ⇒ match fd with [ Internal fn ⇒ ! locals ← params_store (f_params fn) params (empty_map RegisterTag ?); let 〈m', sp〉 ≝ alloc m 0 (f_stacksize fn) Any in ret ? 〈E0, State (mk_frame fn locals (f_entry fn) sp dst) fs m'〉 | External fn ⇒ ! evargs ← check_eventval_list params (sig_args (ef_sig fn)); ! evres ← do_io (ef_id fn) evargs (match (sig_res (ef_sig fn)) with [ None ⇒ ASTint | Some t ⇒ t ]); (* XXX hack, should allow none *) ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), Returnstate (mk_val ? evres) dst fs m〉 ] | Returnstate v dst fs m ⇒ match fs with [ nil ⇒ Error ? (* Already in final state *) | cons f fs' ⇒ ! locals ← reg_store dst v (locals f); ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f)) fs' m〉 ] ]. definition is_final : state → option int ≝ λs. match s with [ State _ _ _ ⇒ None ? | Callstate _ _ _ _ _ ⇒ None ? | Returnstate v _ fs _ ⇒ match fs with [ nil ⇒ match v with [ Vint i ⇒ Some ? i | _ ⇒ None ? ] | cons _ _ ⇒ None ? ] ]. definition RTLabs_exec : execstep io_out io_in ≝ mk_execstep … ? is_final mem_of_state eval_statement. definition make_initial_state : RTLabs_program → res (genv × state) ≝ λp. do ge ← globalenv Genv ?? p; do m ← init_mem Genv ?? p; do b ← opt_to_res ? (find_symbol ? ? ge (prog_main ?? p)); do f ← opt_to_res ? (find_funct_ptr ? ? ge b); OK ? 〈ge,Callstate f (nil ?) (dp ??? [[ ]]) (nil ?) m〉. definition RTLabs_fullexec : fullexec io_out io_in ≝ mk_fullexec … RTLabs_exec ? make_initial_state.