source: src/RTL/semantics.ma @ 1113

Last change on this file since 1113 was 1113, checked in by sacerdot, 9 years ago

Semantics (interpreter) of RTL.
The file does not compile yet. I am progressively modifying the one for RTLabs.

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