source: src/RTL/semantics.ma @ 1384

Last change on this file since 1384 was 1384, checked in by sacerdot, 8 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: 4.7 KB
Line 
1include "joint/SemanticUtils.ma".
2include "RTL/RTL.ma". (* CSC: syntax.ma in RTLabs *)
3include alias "common/Identifiers.ma".
4include alias "ASM/Util.ma".
5
6record frame: Type[0] ≝
7 { fr_ret_regs: list register
8 ; fr_pc: address
9 ; fr_sp: pointer
10 ; fr_carry: beval
11 ; fr_regs: register_env beval
12 }.
13
14definition rtl_more_sem_params: more_sem_params rtl_params_ :=
15 mk_more_sem_params rtl_params_
16  (list frame) (register_env beval) graph_succ_p
17   reg_store reg_retrieve reg_store reg_retrieve reg_store reg_retrieve
18    reg_store reg_retrieve reg_store reg_retrieve
19     (λlocals,dest_src.
20       do v ← reg_retrieve locals (\snd dest_src) ;
21       reg_store (\fst dest_src) v locals)
22     pointer_of_label.
23definition rtl_sem_params: sem_params ≝ mk_sem_params … rtl_more_sem_params.
24
25definition rtl_init_locals :
26 list register → register_env beval → register_env beval ≝
27 λlocals,env.
28  foldl ?? (λlenv,reg. add … lenv reg BVundef) env locals.
29
30(*CSC: could we use here a dependent type to avoid the Error case? *)
31axiom EmptyStack: String.
32axiom OutOfBounds: String.
33
34definition rtl_fetch_ra:
35 state … rtl_sem_params → res ((state … rtl_sem_params) × address) ≝
36 λst.
37  match st_frms ? st with
38  [ nil ⇒ Error ? [MSG EmptyStack]
39  | cons hd tl ⇒ OK … 〈st, fr_pc hd〉 ].
40
41
42(*CSC: XXXXX, for is_final only *)
43axiom rtl_fetch_result: state rtl_sem_params → res (list beval).
44(*  let ret_vals ≝ map … (λreg.greg_retrieve rtl_sem_params st reg) ret_val_regs in
45*)
46
47(*CSC: we could use a dependent type here: the list of return values should have
48  the same length of the list of return registers that store the values. This
49  saves the OutOfBounds exception *)
50definition rtl_pop_frame:
51 state … rtl_sem_params → res (state … rtl_sem_params) ≝
52 λst.
53  do ret_vals ← rtl_fetch_result … st ;
54  let frms ≝ st_frms ? st in
55  match frms with
56  [ nil ⇒ Error ? [MSG EmptyStack]
57  | cons hd tl ⇒
58     do st ←
59      mfold_left_i ??
60       (λi.λst.λreg.
61         do v ← opt_to_res ? [MSG OutOfBounds] (nth_opt … i ret_vals) ;
62         greg_store rtl_sem_params reg v st)
63       (OK … st) (fr_ret_regs hd) ;
64     OK …
65      (set_frms rtl_sem_params tl
66        (set_regs rtl_sem_params (fr_regs hd)
67         (set_sp … (fr_sp hd)
68          (set_carry … (fr_carry hd)
69           (set_m … (free … (m … st) (pblock (sp … st))) st)))))].
70
71definition rtl_save_frame:
72 address → nat → list register → list register → list register → state … rtl_sem_params → res (state … rtl_sem_params) ≝
73 λl,stacksize,formal_arg_regs,actual_arg_regs,retregs,st.
74  let 〈mem,b〉 ≝ alloc … (m … st) 0 stacksize XData in
75  do new_regs ←
76   mfold_left2 …
77    (λlenv,dest,src.
78      do v ← greg_retrieve … st src ;
79      OK … (add … lenv dest v))
80    (OK … (empty_map …)) formal_arg_regs actual_arg_regs ;
81  OK …
82   (set_frms rtl_sem_params
83    (mk_frame retregs l (sp … st) (carry … st) (regs … st) :: (st_frms … st))
84     (set_regs rtl_sem_params new_regs
85      (set_m … mem
86       (set_sp … (mk_pointer XData b ? (mk_offset 0)) st)))).
87cases daemon (*CSC: XXXX easy lemma on alloc to be turned into a dependent type*)
88qed.
89
90definition rtl_more_sem_params1 : more_sem_params1 … rtl_params1 ≝
91 mk_more_sem_params1 ? rtl_params1 rtl_more_sem_params
92  rtl_init_locals rtl_pop_frame rtl_save_frame.
93
94(*CSC: XXXX, for external functions only*)
95axiom rtl_fetch_external_args: external_function → state rtl_sem_params → res (list val).
96axiom rtl_set_result: list val → state rtl_sem_params → res (state rtl_sem_params).
97
98definition rtl_exec_extended:
99 ∀globals. genv globals (rtl_params globals) →
100  rtl_statement_extension → label → state rtl_sem_params →
101   IO io_out io_in (trace × (state rtl_sem_params)) ≝
102 λglobals,ge,stm,l,st.
103  match stm with
104   [ rtl_st_ext_stack_address dreg1 dreg2  ⇒
105      let sp ≝ sp ? st in
106      ! 〈dpl,dph〉 ← beval_pair_of_pointer sp ;
107      ! st ← greg_store rtl_sem_params dreg1 dpl st ;
108      ! st ← greg_store rtl_sem_params dreg2 dph st ;
109       ret ? 〈E0, goto … l st〉
110   | rtl_st_ext_call_ptr _ _ _ _ ⇒ ?
111   | rtl_st_ext_tailcall_id id regs ⇒ ?
112   | rtl_st_ext_tailcall_ptr _ _ _ ⇒ ?
113   ].
114[1,2,3: cases not_implemented ] (*CSC: tailcalls and pointer calls not implemented ATM *)
115qed.
116
117definition rtl_more_sem_params2: ∀globals. more_sem_params2 … (rtl_params globals) ≝
118 λglobals.
119  mk_more_sem_params2 … rtl_more_sem_params1
120   (graph_fetch_statement …) rtl_fetch_ra rtl_fetch_result rtl_fetch_external_args
121    rtl_set_result (rtl_exec_extended …).
122
123definition rtl_fullexec ≝
124 joint_fullexec … (λp. rtl_more_sem_params2 (prog_var_names … p)).
Note: See TracBrowser for help on using the repository browser.