source: src/joint/semantics.ma @ 1411

Last change on this file since 1411 was 1411, checked in by sacerdot, 8 years ago
  1. sem_params2 splitted into sem_params1 + sem_params2 to take out the semantics of the extended statements
  2. pointer call semantics given in RTL/semantics
File size: 17.3 KB
Line 
1include "utilities/lists.ma".
2include "joint/BEGlobalenvs.ma".
3include "common/IO.ma".
4include "common/SmallstepExec.ma".
5include "joint/Joint.ma".
6
7(* CSC: external functions never fail (??) and always return something of the
8   size of one register, both in the frontend and in the backend.
9   Is this reasonable??? In OCaml it always return a list, but in the frontend
10   only the head is kept (or Undef if the list is empty) ??? *)
11
12record more_sem_params (p:params_): Type[1] ≝
13 { framesT: Type[0]
14 ; empty_framesT: framesT
15 ; regsT: Type[0]
16 ; empty_regsT: regsT
17 ; call_args_for_main: call_args p
18 ; call_dest_for_main: call_dest p
19
20 (*CSC: XXXX succ_pc and pointer_of_label deal with fetching; same for
21   fetch_statement below.
22   should we take 'em out in a different record to simplify the code? *)
23 ; succ_pc: succ p → address → res address
24 ; greg_store_: generic_reg p → beval → regsT → res regsT
25 ; greg_retrieve_: regsT → generic_reg p → res beval
26 ; acca_store_: acc_a_reg p → beval → regsT → res regsT
27 ; acca_retrieve_: regsT → acc_a_reg p → res beval
28 ; accb_store_: acc_b_reg p → beval → regsT → res regsT
29 ; accb_retrieve_: regsT → acc_b_reg p → res beval
30 ; dpl_store_: dpl_reg p → beval → regsT → res regsT
31 ; dpl_retrieve_: regsT → dpl_reg p → res beval
32 ; dph_store_: dph_reg p → beval → regsT → res regsT
33 ; dph_retrieve_: regsT → dph_reg p → res beval
34 ; pair_reg_move_: regsT → pair_reg p → res regsT
35 
36 ; pointer_of_label: label → Σp:pointer. ptype p = Code
37 }.
38
39record sem_params: Type[1] ≝
40 { spp :> params_
41 ; more_sem_pars :> more_sem_params spp
42 }.
43
44record state (p: sem_params): Type[0] ≝
45 { st_frms: framesT ? p
46 ; pc: address
47 ; sp: pointer
48 ; carry: beval
49 ; regs: regsT ? p
50 ; m: bemem
51 }.
52
53(*
54definition empty_state: ∀p. more_sem_params p → state p ≝
55 mk_state … (empty_framesT …)
56*)
57
58definition genv ≝ λglobals.λp:params globals. (genv_t Genv) (joint_function globals p).
59
60record more_sem_params1 (globals: list ident) (p: params globals) : Type[1] ≝
61 { more_sparams :> more_sem_params p
62
63 ; fetch_statement: genv … p → state (mk_sem_params … more_sparams) → res (joint_statement (mk_sem_params … more_sparams) globals)
64
65 ; fetch_ra: state (mk_sem_params … more_sparams) → res ((state (mk_sem_params … more_sparams)) × address)
66 ; result_regs: genv globals p → state (mk_sem_params … more_sparams) → res (list (generic_reg p))
67
68 ; init_locals : localsT p → regsT … more_sparams → regsT … more_sparams
69   (*CSC: save_frame returns a res only because we can call a function with the wrong number and
70     type of arguments. To be fixed using a dependent type *)
71 ; save_frame: address → nat → paramsT … p → call_args p → call_dest p → state (mk_sem_params … more_sparams) → res (state (mk_sem_params … more_sparams))
72 ; pop_frame: genv globals p → state (mk_sem_params … more_sparams) → res ((state (mk_sem_params … more_sparams)))
73
74 ; fetch_external_args: external_function → state (mk_sem_params … more_sparams) → res (list val)
75 ; set_result: list val → state (mk_sem_params … more_sparams) → res (state (mk_sem_params … more_sparams))
76 }.
77
78record sem_params1 (globals: list ident): Type[1] ≝
79 { p1:> params globals
80 ; more_sparams1:> more_sem_params1 globals p1
81 }.
82
83record more_sem_params2 (globals: list ident) (p: params globals) : Type[1] ≝
84 { more_sparams_1 :> more_sem_params1 … p
85 ; exec_extended: genv globals p → extend_statements (mk_sem_params … more_sparams_1) → succ p → state (mk_sem_params … more_sparams_1) → IO io_out io_in (trace × (state (mk_sem_params … more_sparams_1)))
86 }.
87
88record sem_params2 (globals: list ident): Type[1] ≝
89 { p2:> params globals
90 ; more_sparams2:> more_sem_params2 globals p2
91 }.
92
93definition sem_params_of_sem_params1 ≝ λglobals. λsp1: sem_params1 globals. mk_sem_params sp1 sp1.
94coercion sem_params_of_sem_params1 : ∀globals. ∀_x:sem_params1 globals. sem_params
95 ≝ sem_params_of_sem_params1 on _x: sem_params1 ? to sem_params.
96
97definition sem_params1_of_sem_params2 ≝ λglobals. λsp2: sem_params2 globals. mk_sem_params1 … sp2 sp2.
98coercion sem_params1_of_sem_params2 : ∀globals. ∀_x:sem_params2 globals. sem_params1 globals
99 ≝ sem_params1_of_sem_params2 on _x: sem_params2 ? to sem_params1 ?.
100
101definition address_of_label: sem_params → label → address ≝
102 λp,l.address_of_code_pointer (pointer_of_label … p l).
103
104definition set_m: ∀p. bemem → state p → state p ≝
105 λp,m,st. mk_state p (st_frms … st) (pc … st) (sp … st) (carry … st) (regs … st) m.
106
107definition set_regs: ∀p:sem_params. regsT ? p → state p → state p ≝
108 λp,regs,st. mk_state p (st_frms … st) (pc … st) (sp … st) (carry … st) regs (m … st).
109
110definition set_sp: ∀p. pointer → state p → state p ≝
111 λp,sp,st. mk_state … (st_frms … st) (pc … st) sp (carry … st) (regs … st) (m … st).
112
113definition set_carry: ∀p. beval → state p → state p ≝
114 λp,carry,st. mk_state … (st_frms … st) (pc … st) (sp … st) carry (regs … st) (m … st).
115
116definition set_pc: ∀p. address → state p → state p ≝
117 λp,pc,st. mk_state … (st_frms … st) pc (sp … st) (carry … st) (regs … st) (m … st).
118
119definition set_frms: ∀p:sem_params. framesT ? p → state p → state p ≝
120 λp,frms,st. mk_state … frms (pc … st) (sp … st) (carry … st) (regs … st) (m … st).
121
122definition goto: ∀p:sem_params. label → state p → state p ≝
123 λp,l,st. set_pc … (address_of_label p l) st.
124
125definition next: ∀p:sem_params. succ … p → state p → res (state p) ≝
126 λp,l,st.
127  do l' ← succ_pc … (more_sem_pars p) l (pc … st) ;
128  OK … (set_pc … l' st).
129
130definition greg_store: ∀p:sem_params. generic_reg p → beval → state p → res (state p) ≝
131 λp,dst,v,st.
132  do regs  ← greg_store_ … dst v (regs … st);
133  OK ? (set_regs … regs st).
134
135definition greg_retrieve: ∀p:sem_params. state p → generic_reg p → res beval ≝
136 λp,st,dst.greg_retrieve_ … (regs … st) dst.
137
138definition acca_store: ∀p:sem_params. acc_a_reg p → beval → state p → res (state p) ≝
139 λp,dst,v,st.
140  do regs ← acca_store_ … dst v (regs … st);
141  OK ? (set_regs … regs st).
142
143definition acca_retrieve: ∀p:sem_params. state p → acc_a_reg p → res beval ≝
144 λp,st,dst.acca_retrieve_ … (regs … st) dst.
145
146definition accb_store: ∀p:sem_params. acc_b_reg p → beval → state p → res (state p) ≝
147 λp,dst,v,st.
148  do regs ← accb_store_ … dst v (regs … st);
149  OK ? (set_regs … regs st).
150
151definition accb_retrieve: ∀p:sem_params. state p → acc_b_reg p → res beval ≝
152 λp,st,dst.accb_retrieve_ … (regs … st) dst.
153
154definition dpl_store: ∀p:sem_params. dpl_reg p → beval → state p → res (state p) ≝
155 λp,dst,v,st.
156  do regs ← dpl_store_ … dst v (regs … st);
157  OK ? (set_regs … regs st).
158
159definition dpl_retrieve: ∀p:sem_params. state p → dpl_reg p → res beval ≝
160 λp,st,dst.dpl_retrieve_ … (regs … st) dst.
161
162definition dph_store: ∀p:sem_params. dph_reg p → beval → state p → res (state p) ≝
163 λp,dst,v,st.
164  do regs ← dph_store_ … dst v (regs … st);
165  OK ? (set_regs … regs st).
166
167definition dph_retrieve: ∀p:sem_params. state p → dph_reg p → res beval ≝
168 λp,st,dst.dph_retrieve_ … (regs … st) dst.
169
170definition pair_reg_move: ∀p:sem_params. state p → pair_reg p → res (state p) ≝
171 λp,st,rs.
172  do regs ← pair_reg_move_ … (regs … st) rs;
173  OK … (set_regs … regs st).
174
175axiom FailedStore: String.
176
177definition push: ∀p. state p → beval → res (state p) ≝
178 λp,st,v.
179  let sp ≝ sp … st in
180  do m ← opt_to_res … (msg FailedStore) (bestorev (m … st) sp v);
181  let sp ≝ shift_pointer … sp [[false;false;false;false;false;false;false;true]] in
182  OK … (set_m … m (set_sp … sp st)).
183
184definition pop: ∀p. state p → res (state p × beval) ≝
185 λp,st.
186  let sp ≝ sp ? st in
187  let sp ≝ neg_shift_pointer ? sp [[false;false;false;false;false;false;false;true]] in
188  let st ≝ set_sp … sp st in
189  do v ← opt_to_res ? (msg FailedStore) (beloadv (m ? st) sp);
190  OK ? 〈st,v〉.
191
192definition save_ra : ∀p. state p → address → res (state p) ≝
193 λp,st,l.
194  let 〈addrl,addrh〉 ≝ l in
195  do st ← push … st addrl;
196  push … st addrh.
197
198definition load_ra : ∀p.state p → res (state p × address) ≝
199 λp,st.
200  do 〈st,addrh〉 ← pop … st;
201  do 〈st,addrl〉 ← pop … st;
202  OK ? 〈st, 〈addrl,addrh〉〉.
203
204axiom MissingSymbol : String.
205axiom FailedLoad : String.
206axiom BadFunction : String.
207axiom BadPointer : String.
208
209definition eval_call_block:
210 ∀globals.∀p:sem_params1 globals. genv globals p → state p →
211  block → call_args p → call_dest p → address → IO io_out io_in (trace×(state p)) ≝
212 λglobals,p,ge,st,b,args,dest,ra.
213  ! fd ← opt_to_res … [MSG BadPointer] (find_funct_ptr ?? ge b);
214    match fd with
215    [ Internal fn ⇒
216      ! st ← save_frame … ra (joint_if_stacksize … fn) (joint_if_params … fn) args dest st ;
217      let regs ≝ init_locals … (joint_if_locals … fn) (regs … st) in
218      let l' ≝ joint_if_entry … fn in
219       ret ? 〈 E0, goto … l' (set_regs p regs st)〉
220    | External fn ⇒
221      ! params ← fetch_external_args … fn st;
222      ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
223      ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
224      (* CSC: XXX bug here; I think I should split it into Byte-long
225         components; instead I am making a singleton out of it. To be
226         fixed once we fully implement external functions. *)
227        let vs ≝ [mk_val ? evres] in
228      ! st ← set_result … vs st;
229      let st ≝ set_pc … ra st in
230        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), st〉
231     ].
232
233definition eval_call_id:
234 ∀globals.∀p:sem_params1 globals. genv globals p → state p →
235  ident → call_args p → call_dest p → address → IO io_out io_in (trace×(state p)) ≝
236 λglobals,p,ge,st,id,args,dest,ra.
237  ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
238  eval_call_block … ge st b args dest ra.
239 
240definition eval_statement : ∀globals: list ident.∀p:sem_params2 globals. genv globals p → state p → IO io_out io_in (trace × (state p)) ≝
241 λglobals,p,ge,st.
242  ! s ← fetch_statement … ge st;
243  match s with
244    [ GOTO l ⇒ ret ? 〈E0, goto … l st〉
245    | sequential seq l ⇒
246      match seq with
247      [ extension c ⇒ exec_extended … p ge c l st
248      | COST_LABEL cl ⇒
249         ! st ← next … l st ;
250         ret ? 〈Echarge cl, st〉
251      | COMMENT c ⇒
252         ! st ← next … l st ;
253         ret ? 〈E0, st〉
254      | INT dest v ⇒
255         ! st ← greg_store p dest (BVByte v) st;
256         ! st ← next … l st ;
257          ret ? 〈E0, st〉
258      | LOAD dst addrl addrh ⇒
259        ! vaddrh ← dph_retrieve … st addrh;
260        ! vaddrl ← dpl_retrieve … st addrl;
261        ! vaddr ← pointer_of_address 〈vaddrl,vaddrh〉;
262        ! v ← opt_to_res … (msg FailedLoad) (beloadv (m … st) vaddr);
263        ! st ← acca_store p … dst v st;
264        ! st ← next … l st ;
265          ret ? 〈E0, st〉
266      | STORE addrl addrh src ⇒
267        ! vaddrh ← dph_retrieve … st addrh;
268        ! vaddrl ← dpl_retrieve … st addrl;
269        ! vaddr ← pointer_of_address 〈vaddrl,vaddrh〉;
270        ! v ← acca_retrieve … st src;
271        ! m' ← opt_to_res … (msg FailedStore) (bestorev (m … st) vaddr v);
272        let st ≝ set_m … m' st in
273        ! st ← next … l st ;
274          ret ? 〈E0, st〉
275      | COND src ltrue ⇒
276        ! v ← acca_retrieve … st src;
277        ! b ← bool_of_beval v;
278        if b then
279         ret ? 〈E0, goto … ltrue st〉
280        else
281         ! st ← next … l st ;
282         ret ? 〈E0, st〉
283      | ADDRESS ident prf ldest hdest ⇒
284        ! addr ← opt_to_res ? [MSG MissingSymbol; CTX … ident] (find_symbol ?? ge ident);
285        ! 〈laddr,haddr〉 ← address_of_pointer (mk_pointer (block_region addr) addr ? zero_offset);
286        ! st ← dpl_store p ldest laddr st;
287        ! st ← dph_store p hdest haddr st;
288        ! st ← next … l st ;
289          ret ? 〈E0, st〉
290      | OP1 op dacc_a sacc_a ⇒
291        ! v ← acca_retrieve … st sacc_a;
292        ! v ← Byte_of_val v;
293          let v' ≝ BVByte (op1 eval op v) in
294        ! st ← acca_store p dacc_a v' st;
295        ! st ← next … l st ;
296          ret ? 〈E0, st〉
297      | OP2 op dacc_a sacc_a src ⇒
298        ! v1 ← acca_retrieve … st sacc_a;
299        ! v1 ← Byte_of_val v1;
300        ! v2 ← greg_retrieve … st src;
301        ! v2 ← Byte_of_val v2;
302        ! carry ← bool_of_beval (carry … st);
303          let 〈v',carry〉 ≝ op2 eval carry op v1 v2 in
304          let v' ≝ BVByte v' in
305          let carry ≝ beval_of_bool carry in
306        ! st ← acca_store p dacc_a v' st;
307          let st ≝ set_carry … carry st in
308        ! st ← next … l st ;
309          ret ? 〈E0, st〉
310      | CLEAR_CARRY ⇒
311        ! st ← next … l (set_carry … BVfalse st) ;
312         ret ? 〈E0, st〉
313      | SET_CARRY ⇒
314        ! st ← next … l (set_carry … BVtrue st) ;
315         ret ? 〈E0, st〉
316      | OPACCS op dacc_a_reg dacc_b_reg sacc_a_reg sacc_b_reg ⇒
317        ! v1 ← acca_retrieve … st sacc_a_reg;
318        ! v1 ← Byte_of_val v1;
319        ! v2 ← accb_retrieve … st sacc_b_reg;
320        ! v2 ← Byte_of_val v2;
321          let 〈v1',v2'〉 ≝ opaccs eval op v1 v2 in
322          let v1' ≝ BVByte v1' in
323          let v2' ≝ BVByte v2' in
324        ! st ← acca_store p dacc_a_reg v1' st;
325        ! st ← accb_store p dacc_b_reg v2' st;
326        ! st ← next … l st ;
327          ret ? 〈E0, st〉
328      | POP dst ⇒
329        ! 〈st,v〉 ← pop … st;
330        ! st ← acca_store p dst v st;
331        ! st ← next … l st ;
332          ret ? 〈E0, st〉
333      | PUSH src ⇒
334        ! v ← acca_retrieve … st src;
335        ! st ← push … st v;
336        ! st ← next … l st ;
337          ret ? 〈E0, st〉
338      | MOVE dst_src ⇒
339        ! st ← pair_reg_move … st dst_src ;
340        ! st ← next … l st ;
341          ret ? 〈E0, st〉
342      | CALL_ID id args dest ⇒
343        ! ra ← succ_pc … (more_sem_pars p) l (pc … st) ;         
344          eval_call_id … p ge st id args dest ra ]
345    | RETURN ⇒
346      ! 〈st,ra〉 ← fetch_ra … st;
347      ! st ← pop_frame … ge st;
348        ret ? 〈E0,set_pc … ra st〉].
349cases addr //
350qed.
351
352definition is_final: ∀globals:list ident. ∀p: sem_params2 globals. genv … p → address → state p → option int ≝
353 λglobals,p,ge,exit,st.res_to_opt ? (
354  do s ← fetch_statement … ge st;
355  match s with
356   [ RETURN ⇒
357      do 〈st,ra〉 ← fetch_ra … st;
358      if eq_address ra exit then
359       do regs ← result_regs … ge st ;
360       do vals ← mmap … (λreg.greg_retrieve … st reg) regs ;
361       Word_of_list_beval vals
362      else
363       Error ? []
364   | _ ⇒ Error ? []]).
365
366record evaluation_parameters : Type[1] ≝
367 { globals: list ident
368 ; sparams2: sem_params2 globals
369 ; exit: address
370 ; genv2: genv globals sparams2
371 }.
372
373definition joint_exec: trans_system io_out io_in ≝
374  mk_trans_system … evaluation_parameters (λG. state (sparams2 G))
375   (λG.is_final (globals G) (sparams2 G) (genv2 G) (exit G))
376   (λG.eval_statement (globals G) (sparams2 G) (genv2 G)).
377
378definition make_global :
379 ∀pars:∀globals.params globals. ∀sparams:∀p: joint_program pars. more_sem_params2 … (pars (prog_var_names … p)).
380  joint_program pars → evaluation_parameters
381
382 λpars,sparams.
383 (* Invariant: a -1 block is unused in common/Globalenvs *)
384 let b ≝ mk_block Code (-1) in
385 let ptr ≝ mk_pointer Code b ? (mk_offset 0) in
386 let addr ≝ address_of_code_pointer ptr in
387  (λp. mk_evaluation_parameters (prog_var_names … p) (mk_sem_params2 … (sparams p)) addr (globalenv Genv … p)).
388% qed.
389
390axiom ExternalMain: String.
391definition make_initial_state :
392 ∀pars:∀globals.params globals. ∀sparams:∀p: joint_program pars. more_sem_params2 … (pars (prog_var_names … p)).
393 ∀p: joint_program … pars. res (state (mk_sem_params (pars (prog_var_names … p)) (sparams p))) ≝
394λpars,sparams,p.
395  let sem_globals ≝ make_global pars sparams p in
396  let ge ≝ genv2 sem_globals in
397  let m ≝ init_mem Genv … p in
398  let dummy_pc ≝ 〈BVundef,BVundef〉 in
399  let sp ≝ ? in
400  let main ≝ prog_main … p in
401  let st0 ≝ mk_state … (empty_framesT …) dummy_pc sp BVfalse (empty_regsT …) m in
402  let trace_state ≝
403   eval_call_id … (mk_sem_params1 … (sparams …)) ge st0 main (call_args_for_main … (sparams …))
404    (call_dest_for_main … (sparams …)) (exit sem_globals) in
405  match trace_state with
406  [ Value tr_st ⇒ OK … (\snd tr_st) (* E0 trace thrown away *)
407  | Wrong msg ⇒ Error … msg
408  | Interact _ _ ⇒ Error … [MSG ExternalMain] (* External main not supported: why? *)
409  ].
410cases daemon qed.
411(*
412RTL:
413 init_prog == init_mem
414 init_sp: inizializzare lo stack pointer
415 init_isp: inizializzare l'altro stack pointer
416 init_renv: inizializzare i registri fisici
417 init_main_call: chiamata di funzione
418   --- init_fun_call: cambia fra ERTL e LTL
419 change_carry: a 0
420*)
421
422definition joint_fullexec :
423 ∀pars:∀globals.params globals.
424 ∀sparams:∀p: joint_program pars. more_sem_params2 … (pars (prog_var_names … p)).
425 fullexec io_out io_in ≝
426 λpars,sparams.
427  mk_fullexec ??? joint_exec (make_global pars sparams) (make_initial_state pars sparams).
Note: See TracBrowser for help on using the repository browser.