source: src/joint/semantics.ma @ 1303

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