source: src/joint/semantics.ma @ 1430

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

Bug fixed: push/pop must work on the isp (now added).
Note: the sp is used only in RTL and it is spurious in all other languages;

the isp is spurious in RTL.

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