source: src/RTLabs/semantics.ma @ 1238

Last change on this file since 1238 was 1238, checked in by campbell, 8 years ago

Update Cminor and RTLabs to fit SmallstepExec? changes.

File size: 8.4 KB
RevLine 
[639]1
2(* XXX NB: I haven't checked all of these semantics against the prototype
3           compilers yet! *)
4
[766]5include "utilities/lists.ma".
6
[702]7include "common/Errors.ma".
8include "common/Globalenvs.ma".
[731]9include "common/IO.ma".
[702]10include "common/SmallstepExec.ma".
[639]11
[762]12include "RTLabs/syntax.ma".
[751]13
[639]14definition genv ≝ (genv_t Genv) (fundef internal_function).
15
16record frame : Type[0] ≝
[693]17{ func   : internal_function
[750]18; locals : register_env val
[639]19; next   : label
20; sp     : block
[750]21; retdst : option register
[639]22}.
23
24definition adv : label → frame → frame ≝
[693]25λl,f. mk_frame (func f) (locals f) l (sp f) (retdst f).
[639]26
27inductive state : Type[0] ≝
[693]28| State :
29   ∀   f : frame.
30   ∀  fs : list frame.
31   ∀   m : mem.
32   state
[639]33| Callstate :
34   ∀  fd : fundef internal_function.
35   ∀args : list val.
[750]36   ∀ dst : option register.
[639]37   ∀ stk : list frame.
38   ∀   m : mem.
39   state
40| Returnstate :
[765]41   ∀ rtv : option val.
[750]42   ∀ dst : option register.
[639]43   ∀ stk : list frame.
44   ∀   m : mem.
45   state.
46
[693]47definition mem_of_state : state → mem ≝
48λs. match s with [ State _ _ m ⇒ m | Callstate _ _ _ _ m ⇒ m | Returnstate _ _ _ m ⇒ m ].
49
[639]50definition build_state ≝ λf.λfs.λm.λn. State (adv n f) fs m.
51
[887]52definition init_locals : list (register × typ) → register_env val ≝
53foldr ?? (λidt,en. let 〈id,ty〉 ≝ idt in add RegisterTag val en id Vundef) (empty_map ??).
[761]54
[639]55definition reg_store ≝
[750]56λreg,v,locals.
[761]57  update RegisterTag val locals reg v.
[639]58
[797]59axiom WrongNumberOfParameters : String.
60
[887]61let rec params_store (rs:list (register × typ)) (vs:list val) (locals : register_env val) : res (register_env val) ≝
[639]62match rs with
[797]63[ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? (msg WrongNumberOfParameters)]
[887]64| cons rt rst ⇒ match vs with [ nil ⇒ Error ? (msg WrongNumberOfParameters) | cons v vst ⇒
65  let 〈r,ty〉 ≝ rt in
[761]66  let locals' ≝ add RegisterTag val locals r v in
[639]67  params_store rst vst locals'
68] ].
69
[797]70axiom BadRegister : String.
71
[750]72definition reg_retrieve : register_env ? → register → res val ≝
[797]73λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
[639]74
[797]75axiom FailedOp : String.
76axiom MissingSymbol : String.
77
78axiom MissingStatement : String.
79axiom FailedConstant : String.
80axiom FailedLoad : String.
81axiom FailedStore : String.
82axiom BadFunction : String.
83axiom BadJumpTable : String.
84axiom BadJumpValue : String.
85axiom FinalState : String.
86axiom ReturnMismatch : String.
[639]87
[693]88definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝
89λge,st.
[639]90match st with
91[ State f fs m ⇒
[797]92  ! s ← opt_to_io … [MSG MissingStatement; CTX ? (next f)] (lookup ?? (f_graph (func f)) (next f));
[639]93  match s with
94  [ St_skip l ⇒ ret ? 〈E0, build_state f fs m l〉
95  | St_cost cl l ⇒ ret ? 〈Echarge cl, build_state f fs m l〉
[750]96  | St_const r cst l ⇒
[797]97      ! v ← opt_to_res … (msg FailedConstant) (eval_constant (find_symbol … ge) (sp f) cst);
[750]98      ! locals ← reg_store r v (locals f);
[693]99      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
[639]100  | St_op1 op dst src l ⇒
101      ! v ← reg_retrieve (locals f) src;
[797]102      ! v' ← opt_to_res … (msg FailedOp) (eval_unop op v);
[639]103      ! locals ← reg_store dst v' (locals f);
[693]104      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
[639]105  | St_op2 op dst src1 src2 l ⇒
106      ! v1 ← reg_retrieve (locals f) src1;
107      ! v2 ← reg_retrieve (locals f) src2;
[797]108      ! v' ← opt_to_res … (msg FailedOp) (eval_binop op v1 v2);
[639]109      ! locals ← reg_store dst v' (locals f);
[693]110      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
[887]111  | St_load chunk addr dst l ⇒
112      ! vaddr ← reg_retrieve (locals f) addr;
[797]113      ! v ← opt_to_res … (msg FailedLoad) (loadv chunk m vaddr);
[639]114      ! locals ← reg_store dst v (locals f);
[693]115      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
[887]116  | St_store chunk addr src l ⇒
117      ! vaddr ← reg_retrieve (locals f) addr;
[639]118      ! v ← reg_retrieve (locals f) src;
[797]119      ! m' ← opt_to_res … (msg FailedStore) (storev chunk m vaddr v);
[639]120      ret ? 〈E0, build_state f fs m' l〉
[816]121  | St_call_id id args dst l ⇒
[797]122      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
123      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
[639]124      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
125      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
[816]126  | St_call_ptr frs args dst l ⇒
[639]127      ! fv ← reg_retrieve (locals f) frs;
[797]128      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
[639]129      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
130      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
[816]131  | St_tailcall_id id args ⇒
[797]132      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
133      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
[639]134      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
135      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
[816]136  | St_tailcall_ptr frs args ⇒
[639]137      ! fv ← reg_retrieve (locals f) frs;
[797]138      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
[639]139      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
140      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
141
[888]142  | St_cond src ltrue lfalse ⇒
143      ! v ← reg_retrieve (locals f) src;
[751]144      ! b ← eval_bool_of_val v;
[639]145      ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉
146
[750]147  | St_jumptable r ls ⇒
148      ! v ← reg_retrieve (locals f) r;
[639]149      match v with
[961]150      [ Vint _ i ⇒
[797]151          ! l ← opt_to_io … (msg BadJumpTable) (nth_opt ? (nat_of_bitvector ? i) ls);
[639]152          ret ? 〈E0, build_state f fs m l〉
[797]153      | _ ⇒ Wrong ??? (msg BadJumpValue)
[639]154      ]
155
[765]156  | St_return ⇒
157      ! v ← match f_result (func f) with
158            [ None ⇒ ret ? (None ?)
[887]159            | Some rt ⇒
160                let 〈r,ty〉 ≝ rt in
161                ! v ← reg_retrieve (locals f) r;
162                ret ? (Some ? v)
[765]163            ];
[639]164      ret ? 〈E0, Returnstate v (retdst f) fs (free m (sp f))〉
165  ]
166| Callstate fd params dst fs m ⇒
167    match fd with
168    [ Internal fn ⇒
[761]169        ! locals ← params_store (f_params fn) params (init_locals (f_locals fn));
[1123]170        (* CSC: XXX alignment call (concrete_stacksize in OCaml) is missing
171           here *)
[639]172        let 〈m', sp〉 ≝ alloc m 0 (f_stacksize fn) Any in
[693]173        ret ? 〈E0, State (mk_frame fn locals (f_entry fn) sp dst) fs m'〉
[639]174    | External fn ⇒
175        ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
[879]176        ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
[765]177        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), Returnstate (Some ? (mk_val ? evres) (*FIXME should allow None *)) dst fs m〉
[639]178    ]
179| Returnstate v dst fs m ⇒
180    match fs with
[797]181    [ nil ⇒ Error ? (msg FinalState) (* Already in final state *)
[639]182    | cons f fs' ⇒
[765]183        ! locals ← match dst with
184                   [ None ⇒ match v with [ None ⇒ OK ? (locals f)
[797]185                                         | _ ⇒ Error ? (msg ReturnMismatch) ]
186                   | Some d ⇒ match v with [ None ⇒ Error ? (msg ReturnMismatch)
[765]187                                           | Some v' ⇒ reg_store d v' (locals f) ] ];
[727]188        ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f)) fs' m〉
[639]189    ]
190].
[693]191
192definition is_final : state → option int ≝
193λs. match s with
194[ State _ _ _ ⇒ None ?
195| Callstate _ _ _ _ _ ⇒ None ?
196| Returnstate v _ fs _ ⇒
[961]197    match fs with [ nil ⇒
198      match v with [ Some v' ⇒
199        match v' with
200        [ Vint sz i ⇒ intsize_eq_elim ? sz I32 ? i (λi. Some ? i) (None ?)
201        | _ ⇒ None ? ]
202      | None ⇒ None ? ]
203    | cons _ _ ⇒ None ? ]
[693]204].
205
[1238]206definition RTLabs_exec : trans_system io_out io_in ≝
207  mk_trans_system … ? (λ_.is_final) eval_statement.
[702]208
[1238]209definition make_global : RTLabs_program → genv ≝
210λp. globalenv Genv ?? (λx.[Init_space x]) p.
211
212definition make_initial_state : RTLabs_program → res state ≝
[702]213λp.
[1238]214  let ge ≝ make_global p in
[1139]215  do m ← init_mem Genv ?? (λx.[Init_space x]) p;
[797]216  let main ≝ prog_main ?? p in
217  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
218  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
[1238]219  OK ? (Callstate f (nil ?) (None ?) (nil ?) m).
[702]220
[731]221definition RTLabs_fullexec : fullexec io_out io_in ≝
[1238]222  mk_fullexec … RTLabs_exec make_global make_initial_state.
[731]223
Note: See TracBrowser for help on using the repository browser.