source: src/joint/semantics.ma @ 1387

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

Further simplification *params1 no longer used.

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