source: src/joint/semantics.ma @ 1384

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