source: src/RTL/semantics.ma @ 1322

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

address => stack_address

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