source: src/joint/semantics.ma @ 1386

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

Structure of semantic parameters simplified.

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