source: src/RTL/semantics.ma @ 1320

Last change on this file since 1320 was 1320, checked in by sacerdot, 8 years ago

Frame operations implemented.

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