source: src/RTL/semantics.ma @ 1359

Last change on this file since 1359 was 1359, checked in by sacerdot, 9 years ago
  1. more work on the RTL semantics
  2. changes to joint/semantics to accomodate the RTL one
File size: 15.3 KB
Line 
1include "joint/SemanticUtils.ma".
2(*
3include "utilities/lists.ma".
4include "common/Errors.ma".
5include "common/Globalenvs.ma".
6include "common/IO.ma".
7include "common/SmallstepExec.ma".
8*)
9include "RTL/RTL.ma". (* CSC: syntax.ma in RTLabs *)
10include alias "common/Identifiers.ma".
11
12(* CSC: a streamlined version of the ERTL state *)
13record frame: Type[0] ≝
14 { fr_ret_regs: list register
15 ; fr_pc: address
16 ; fr_sp: pointer
17 ; fr_carry: beval
18 ; fr_regs: register_env beval
19 }.
20
21definition rtl_more_sem_params: more_sem_params rtl_params_ :=
22 mk_more_sem_params rtl_params_
23  (list frame) (register_env beval) graph_succ_p
24   reg_store reg_retrieve reg_store reg_retrieve reg_store reg_retrieve
25    reg_store reg_retrieve reg_store reg_retrieve
26     (λlocals,dest_src.
27       do v ← reg_retrieve locals (\snd dest_src) ;
28       reg_store (\fst dest_src) v locals)
29     pointer_of_label.
30definition rtl_sem_params: sem_params ≝ mk_sem_params … rtl_more_sem_params.
31
32definition rtl_init_locals :
33 list register → register_env beval → register_env beval ≝
34 λlocals.λ_.
35  foldl ?? (λlenv,reg. add … lenv reg BVundef) (empty_map …) locals.
36
37(*CSC: could we use here a dependent type to avoid the Error case? *)
38axiom EmptyStack: String.
39axiom OutOfBounds: String.
40include alias "ASM/Util.ma".
41
42(*CSC: we could use a dependent type here: the list of return values should have
43  the same length of the list of return registers that store the values. This
44  saves the OutOfBounds exception *)
45definition rtl_pop_frame: list beval → state … rtl_sem_params → res (state … rtl_sem_params) ≝
46 λret_vals,st.
47  let frms ≝ st_frms ? st in
48  match frms with
49  [ nil ⇒ Error ? [MSG EmptyStack]
50  | cons hd tl ⇒
51     do st ←
52      mfold_left_i ??
53       (λi.λst.λreg.
54         do v ← opt_to_res ? [MSG OutOfBounds] (nth_opt … i ret_vals) ;
55         greg_store rtl_sem_params reg v st)
56       (OK … st) (fr_ret_regs hd) ;
57     OK …
58      (set_frms rtl_sem_params tl
59       (set_regs rtl_sem_params (fr_regs hd)
60        (set_sp … (fr_sp hd)
61         (set_pc … (fr_pc hd)
62          (set_carry … (fr_carry hd) st))))) ].
63
64definition rtl_save_frame: list register → state … rtl_sem_params → state … rtl_sem_params ≝
65 λretregs.λst.
66  set_frms rtl_sem_params
67   (mk_frame retregs (pc … st) (sp … st) (carry … st) (regs ? st) :: (st_frms … st)) st.
68
69definition rtl_more_sem_params1 : more_sem_params1 … rtl_params1 ≝
70 mk_more_sem_params1 ? rtl_params1 rtl_more_sem_params
71  rtl_init_locals rtl_pop_frame rtl_save_frame.
72
73
74(*CSC: XXXXX, for is_final only *)
75axiom rtl_fetch_result: state rtl_sem_params → res (list beval).
76(*  let ret_vals ≝ map … (λreg.greg_retrieve rtl_sem_params st reg) ret_val_regs in
77*)
78
79(*CSC: XXXX, for external functions only*)
80axiom rtl_fetch_external_args: external_function → state rtl_sem_params → res (list val).
81axiom rtl_set_result: list val → state rtl_sem_params → res (state rtl_sem_params).
82
83definition rtl_exec_extended:
84 ∀globals. genv globals (rtl_params globals) →
85  rtl_statement_extension → label → state rtl_sem_params →
86   IO io_out io_in (trace × (state rtl_sem_params)) ≝
87 λglobals,ge,stm,l,st.
88  match stm with
89   [ rtl_st_ext_stack_address dreg1 dreg2  ⇒
90      let sp ≝ sp ? st in
91      ! 〈dpl,dph〉 ← beval_pair_of_pointer sp ;
92      ! st ← greg_store rtl_sem_params dreg1 dpl st ;
93      ! st ← greg_store rtl_sem_params dreg2 dph st ;
94       ret ? 〈E0, goto … l st〉
95   | rtl_st_ext_call_ptr _ _ _ _ ⇒ ?
96   | rtl_st_ext_tailcall_id id regs ⇒ ?
97   | rtl_st_ext_tailcall_ptr _ _ _ ⇒ ?
98   ].
99[1,2,3: cases not_implemented ] (*CSC: tailcalls and pointer calls not implemented ATM *)
100qed.
101
102definition rtl_more_sem_params2: ∀globals. more_sem_params2 … (rtl_params globals) ≝
103 λglobals.
104  mk_more_sem_params2 … rtl_more_sem_params1
105   (graph_fetch_statement …) rtl_fetch_result rtl_fetch_external_args rtl_set_result
106    (rtl_exec_extended …).
107
108definition rtl_fullexec ≝
109 joint_fullexec … (λp. rtl_more_sem_params2 (prog_var_names … p)).
110
111(*
112(* CSC: external functions never fail (??) and always return something of the
113   size of one register, both in the frontend and in the backend.
114   Is this reasonable??? In OCaml it always return a list, but in the frontend
115   only the head is kept (or Undef if the list is empty) ??? *)
116
117(* CSC: carries are values and booleans are not values; so we use integers.
118   But why using values at all? To have undef? *)
119(* CSC: ???????????? *)
120axiom smerge: val → val → res val.
121(* CSC: ???????????? In OCaml: IntValue.Int32.merge
122   Note also that the translation can fail; so it should be a res int! *)
123axiom smerge2: list val → int.
124(* CSC: I have a byte, I need a value, but it is complex to do so *)
125axiom val_of_Byte: Byte → val.
126axiom Byte_of_val: val → res Byte.
127(* Map from the front-end memory model to the back-end memory model *)
128axiom represent_block: block → val × val.
129(* CSC: fixed size chunk *)
130axiom chunk: memory_chunk.
131
132definition genv ≝ (genv_t Genv) (fundef rtl_internal_function).
133
134(* CSC: added carry *)
135record frame : Type[0] ≝
136{ func   : rtl_internal_function
137; locals : register_env val
138; next   : label
139; sp     : block
140; retdst : list register
141; carry  : val
142}.
143
144(* CSC: added carry *)
145definition adv : label → frame → frame ≝
146λl,f. mk_frame (func f) (locals f) l (sp f) (retdst f) (carry f).
147
148inductive state : Type[0] ≝
149| State :
150   ∀   f : frame.
151   ∀  fs : list frame.
152   ∀   m : mem.
153   state
154| Callstate :
155   ∀  fd : fundef rtl_internal_function.
156   ∀args : list val.
157   ∀ dst : list register. (* CSC: changed from option to list *)
158   ∀ stk : list frame.
159   ∀   m : mem.
160   state
161| Returnstate :
162   (* CSC: the two lists should have the same length, but this is
163      enforced only by the semantics *)
164   ∀ rtv : list val. (* CSC: changed from option to list *)
165   ∀ dst : list register. (* CSC: changed from option to list *)
166   ∀ stk : list frame.
167   ∀   m : mem.
168   state.
169
170definition mem_of_state : state → mem ≝
171λs. match s with [ State _ _ m ⇒ m | Callstate _ _ _ _ m ⇒ m | Returnstate _ _ _ m ⇒ m ].
172
173definition build_state ≝ λf.λfs.λm.λn. State (adv n f) fs m.
174
175(* CSC: modified to take in input a list of registers (untyped) *)
176definition init_locals : list register → register_env val ≝
177foldr ?? (λid,en. add RegisterTag val en id Vundef) (empty_map ??).
178
179definition reg_store ≝
180λreg,v,locals.
181  update RegisterTag val locals reg v.
182
183axiom WrongNumberOfParameters : String.
184
185(* CSC: modified to take in input a list of registers (untyped) *)
186let rec params_store (rs:list register) (vs:list val) (locals : register_env val) : res (register_env val) ≝
187match rs with
188[ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? (msg WrongNumberOfParameters)]
189| cons r rst ⇒ match vs with [ nil ⇒ Error ? (msg WrongNumberOfParameters) | cons v vst ⇒
190  let locals' ≝ add RegisterTag val locals r v in
191  params_store rst vst locals'
192] ].
193
194axiom BadRegister : String.
195
196definition reg_retrieve : register_env ? → register → res val ≝
197λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
198
199axiom FailedOp : String.
200axiom MissingSymbol : String.
201
202axiom MissingStatement : String.
203axiom FailedConstant : String.
204axiom FailedLoad : String.
205axiom FailedStore : String.
206axiom BadFunction : String.
207axiom BadJumpTable : String.
208axiom BadJumpValue : String.
209axiom FinalState : String.
210axiom ReturnMismatch : String.
211
212definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝
213λge,st.
214match st with
215[ State f fs m ⇒
216  ! s ← opt_to_io … [MSG MissingStatement; CTX ? (next f)] (lookup ?? (rtl_if_graph (func f)) (next f));
217  match s with
218  [ rtl_st_skip l ⇒ ret ? 〈E0, build_state f fs m l〉
219  | rtl_st_cost cl l ⇒ ret ? 〈Echarge cl, build_state f fs m l〉
220  | rtl_st_load addrl addrh dst l ⇒  (* CSC: pairs of regs vs reg *)
221      ! vaddrh ← reg_retrieve (locals f) addrh;
222      ! vaddrl ← reg_retrieve (locals f) addrl;
223      ! vaddr ← smerge vaddrl vaddrh;
224      ! v ← opt_to_res … (msg FailedLoad) (loadv chunk m vaddr);
225      ! locals ← reg_store dst v (locals f);
226      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
227  | rtl_st_store addrl addrh src l ⇒  (* CSC: pairs of regs vs reg *)
228      ! vaddrh ← reg_retrieve (locals f) addrh;
229      ! vaddrl ← reg_retrieve (locals f) addrl;
230      ! vaddr ← smerge vaddrl vaddrh;
231      ! v ← reg_retrieve (locals f) src;
232      ! m' ← opt_to_res … (msg FailedStore) (storev chunk m vaddr v);
233      ret ? 〈E0, build_state f fs m' l〉
234  | rtl_st_call_id id args dst l ⇒
235      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
236      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
237      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
238      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
239  | rtl_st_call_ptr lfrs hfrs args dst l ⇒ (* CSC: pairs of regs vs reg *)
240      ! hfv ← reg_retrieve (locals f) hfrs;
241      ! lfv ← reg_retrieve (locals f) lfrs;
242      ! fv  ← smerge lfv hfv;
243      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
244      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
245      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
246  | rtl_st_tailcall_id id args ⇒
247      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
248      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
249      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
250      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
251  | rtl_st_tailcall_ptr lfrs hfrs args ⇒ (* CSC: pairs of regs vs reg *)
252      ! hfv ← reg_retrieve (locals f) hfrs;
253      ! lfv ← reg_retrieve (locals f) lfrs;
254      ! fv  ← smerge lfv hfv;
255      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
256      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
257      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
258  | rtl_st_return ⇒
259      (* CSC: rtl_if_result/f_result; list vs option *)
260      let dest ≝ rtl_if_result (func f) in
261      ! v ←  mmap … (reg_retrieve (locals f)) dest;
262      ret ? 〈E0, Returnstate v (retdst f) fs (free m (sp f))〉
263  | rtl_st_cond src ltrue lfalse ⇒
264      ! v ← reg_retrieve (locals f) src;
265      ! b ← eval_bool_of_val v;
266      ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉
267  (* CSC: mismatch between the memory models and failures for the next two opx *)
268  | rtl_st_op1 op dst src l ⇒
269     ! v ← reg_retrieve (locals f) src;
270     ! v ← Byte_of_val v;
271     let v' ≝ val_of_Byte (op1 eval op v) in
272     ! locals ← reg_store dst v' (locals f);
273     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
274  | rtl_st_op2 op dst src1 src2 l ⇒
275     ! v1 ← reg_retrieve (locals f) src1;
276     ! v1 ← Byte_of_val v1;
277     ! v2 ← reg_retrieve (locals f) src2;
278     ! v2 ← Byte_of_val v2;
279     ! carry ← eval_bool_of_val (carry f);
280     let 〈v',carry〉 ≝ op2 eval carry op v1 v2 in
281     let v' ≝ val_of_Byte v' in
282     let carry ≝ of_bool carry in
283     ! locals ← reg_store dst v' (locals f);
284     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) carry) fs m〉
285
286  (* CSC: Removed: St_jumptable *)
287  (* CSC: Added: *)
288 
289  (* CSC: St_const splitted into rtl_st_int, rtl_st_addr and rt_st_stack_addr; *)
290  | rtl_st_addr ldest hdest id l ⇒
291     ! addr ← opt_to_res … [MSG MissingSymbol; CTX … id] (find_symbol ?? ge id);
292     let 〈laddr,haddr〉 ≝ represent_block addr in
293     ! locals ← reg_store ldest laddr (locals f);
294     ! locals ← reg_store hdest haddr locals;
295     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
296  | rtl_st_stack_addr ldest hdest l ⇒
297     let 〈laddr,haddr〉 ≝ represent_block (sp f) in
298     ! locals ← reg_store ldest laddr (locals f);
299     ! locals ← reg_store hdest haddr locals;
300     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
301  | rtl_st_int dest v l ⇒
302     ! locals ← reg_store dest (val_of_Byte v) (locals f);
303     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
304
305  (* CSC: St_op1 splitted into rtl_st_op1 and rtl_st_move *)
306  | rtl_st_move dst src l ⇒
307     ! v ← reg_retrieve (locals f) src;
308     ! locals ← reg_store dst v (locals f);
309     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
310  (* CSC: St_op2 splitted into rtl_st_op2 and rtl_st_opaccs *) 
311  | rtl_st_opaccs op dacc dbacc sacc sbacc l ⇒
312     ! v1 ← reg_retrieve (locals f) sacc;
313     ! v1 ← Byte_of_val v1;
314     ! v2 ← reg_retrieve (locals f) sbacc;
315     ! v2 ← Byte_of_val v2;
316     let 〈v1',v2'〉 ≝ opaccs eval op v1 v2 in
317     let v1' ≝ val_of_Byte v1' in
318     let v2' ≝ val_of_Byte v2' in
319     ! locals ← reg_store dacc v1' (locals f);
320     ! locals ← reg_store dbacc v2' locals;
321     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
322 
323  | rtl_st_clear_carry l ⇒
324    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vfalse) fs m〉
325  | rtl_st_set_carry l ⇒
326    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vtrue) fs m〉
327  ]
328| Callstate fd params dst fs m ⇒
329    match fd with
330    [ Internal fn ⇒
331        ! locals ← params_store (rtl_if_params fn) params (init_locals (rtl_if_locals fn));
332        let 〈m', sp〉 ≝ alloc m 0 (rtl_if_stacksize fn) Any in
333        (* CSC: added carry *)
334        ret ? 〈E0, State (mk_frame fn locals (rtl_if_entry fn) sp dst Vundef) fs m'〉
335    | External fn ⇒
336        ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
337        ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
338        (* CSC: return type changed from option to list *)
339        (* CSC: XXX bug here; I think I should split it into Byte-long
340           components; instead I am making a singleton out of it *)
341        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), Returnstate [mk_val ? evres] dst fs m〉
342    ]
343| Returnstate v dst fs m ⇒
344    match fs with
345    [ nil ⇒ Error ? (msg FinalState) (* Already in final state *)
346    | cons f fs' ⇒
347       ! locals ←
348         mfold_left2 ???
349          (λlocals,dst,v. reg_store dst v locals) (OK ? (locals f)) dst v;
350        ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f) (carry f)) fs' m〉
351    ]
352].
353
354axiom WrongReturnValueType: String.
355
356definition is_final : state → option ((*CSC: why not res*) int) ≝
357λs. match s with
358[ State _ _ _ ⇒ None ?
359| Callstate _ _ _ _ _ ⇒ None ?
360| Returnstate v _ fs _ ⇒
361    match fs with
362     [ nil ⇒ Some ? (smerge2 v)
363     | _ ⇒ None ? ]].
364(*
365definition RTL_exec : execstep io_out io_in ≝
366  mk_execstep … ? is_final mem_of_state eval_statement.
367*)
368(* CSC: XXX the program type does not fit with the globalenv and init_mem
369definition make_initial_state : rtl_program → res (genv × state) ≝
370λp.
371  do ge ← globalenv Genv ?? p;
372  do m ← init_mem Genv ?? p;
373  let main ≝ rtl_pr_main ?? p in
374  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
375  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
376  OK ? 〈ge,Callstate f (nil ?) [ ] (nil ?) m〉.
377
378definition RTL_fullexec : fullexec io_out io_in ≝
379  mk_fullexec … RTL_exec ? make_initial_state.
380*)
381*)
Note: See TracBrowser for help on using the repository browser.