source: src/RTL/semantics.ma @ 1117

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

More operations implemented.

File size: 9.7 KB
Line 
1
2(* XXX NB: I haven't checked all of these semantics against the prototype
3           compilers yet! *)
4
5include "utilities/lists.ma".
6
7include "common/Errors.ma".
8include "common/Globalenvs.ma".
9include "common/IO.ma".
10include "common/SmallstepExec.ma".
11
12include "RTL/RTL.ma". (* CSC: syntax.ma in RTLabs *)
13
14(* CSC: carries are values and booleans are not values; so we use integers.
15   But why using values at all? To have undef? *)
16(* CSC: ???????????? *)
17axiom smerge: val → val → res val.
18(* CSC: I have a byte, I need a value, but it is complex to do so *)
19axiom val_of_Byte: Byte → val.
20(* CSC: fixed size chunk *)
21axiom chunk: memory_chunk.
22
23definition genv ≝ (genv_t Genv) (fundef rtl_internal_function).
24
25(* CSC: added carry *)
26record frame : Type[0] ≝
27{ func   : rtl_internal_function
28; locals : register_env val
29; next   : label
30; sp     : block
31; retdst : list register
32; carry  : val
33}.
34
35(* CSC: added carry *)
36definition adv : label → frame → frame ≝
37λl,f. mk_frame (func f) (locals f) l (sp f) (retdst f) (carry f).
38
39inductive state : Type[0] ≝
40| State :
41   ∀   f : frame.
42   ∀  fs : list frame.
43   ∀   m : mem.
44   state
45| Callstate :
46   ∀  fd : fundef rtl_internal_function.
47   ∀args : list val.
48   ∀ dst : list register. (* CSC: changed from option to list *)
49   ∀ stk : list frame.
50   ∀   m : mem.
51   state
52| Returnstate :
53   ∀ rtv : list val. (* CSC: changed from option to list *)
54   ∀ dst : list register. (* CSC: changed from option to list *)
55   ∀ stk : list frame.
56   ∀   m : mem.
57   state.
58
59definition mem_of_state : state → mem ≝
60λs. match s with [ State _ _ m ⇒ m | Callstate _ _ _ _ m ⇒ m | Returnstate _ _ _ m ⇒ m ].
61
62definition build_state ≝ λf.λfs.λm.λn. State (adv n f) fs m.
63
64definition init_locals : list (register × typ) → register_env val ≝
65foldr ?? (λidt,en. let 〈id,ty〉 ≝ idt in add RegisterTag val en id Vundef) (empty_map ??).
66
67definition reg_store ≝
68λreg,v,locals.
69  update RegisterTag val locals reg v.
70
71axiom WrongNumberOfParameters : String.
72
73let rec params_store (rs:list (register × typ)) (vs:list val) (locals : register_env val) : res (register_env val) ≝
74match rs with
75[ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? (msg WrongNumberOfParameters)]
76| cons rt rst ⇒ match vs with [ nil ⇒ Error ? (msg WrongNumberOfParameters) | cons v vst ⇒
77  let 〈r,ty〉 ≝ rt in
78  let locals' ≝ add RegisterTag val locals r v in
79  params_store rst vst locals'
80] ].
81
82axiom BadRegister : String.
83
84definition reg_retrieve : register_env ? → register → res val ≝
85λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
86
87axiom FailedOp : String.
88axiom MissingSymbol : String.
89
90axiom MissingStatement : String.
91axiom FailedConstant : String.
92axiom FailedLoad : String.
93axiom FailedStore : String.
94axiom BadFunction : String.
95axiom BadJumpTable : String.
96axiom BadJumpValue : String.
97axiom FinalState : String.
98axiom ReturnMismatch : String.
99
100definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝
101λge,st.
102match st with
103[ State f fs m ⇒
104  ! s ← opt_to_io … [MSG MissingStatement; CTX ? (next f)] (lookup ?? (rtl_if_graph (func f)) (next f));
105  match s with
106  [ rtl_st_skip l ⇒ ret ? 〈E0, build_state f fs m l〉
107  | rtl_st_cost cl l ⇒ ret ? 〈Echarge cl, build_state f fs m l〉
108  | rtl_st_load addrl addrh dst l ⇒  (* CSC: pairs of regs vs reg *)
109      ! vaddrh ← reg_retrieve (locals f) addrh;
110      ! vaddrl ← reg_retrieve (locals f) addrl;
111      ! vaddr ← smerge vaddrl vaddrh;
112      ! v ← opt_to_res … (msg FailedLoad) (loadv chunk m vaddr);
113      ! locals ← reg_store dst v (locals f);
114      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
115  | rtl_st_store addrl addrh src l ⇒  (* CSC: pairs of regs vs reg *)
116      ! vaddrh ← reg_retrieve (locals f) addrh;
117      ! vaddrl ← reg_retrieve (locals f) addrl;
118      ! vaddr ← smerge vaddrl vaddrh;
119      ! v ← reg_retrieve (locals f) src;
120      ! m' ← opt_to_res … (msg FailedStore) (storev chunk m vaddr v);
121      ret ? 〈E0, build_state f fs m' l〉
122  | rtl_st_call_id id args dst l ⇒
123      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
124      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
125      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
126      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
127  | rtl_st_call_ptr lfrs hfrs args dst l ⇒ (* CSC: pairs of regs vs reg *)
128      ! hfv ← reg_retrieve (locals f) hfrs;
129      ! lfv ← reg_retrieve (locals f) lfrs;
130      ! fv  ← smerge lfv hfv;
131      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
132      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
133      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
134  | rtl_st_tailcall_id id args ⇒
135      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
136      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
137      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
138      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
139  | rtl_st_tailcall_ptr lfrs hfrs args ⇒ (* CSC: pairs of regs vs reg *)
140      ! hfv ← reg_retrieve (locals f) hfrs;
141      ! lfv ← reg_retrieve (locals f) lfrs;
142      ! fv  ← smerge lfv hfv;
143      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
144      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
145      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
146  | rtl_st_return ⇒
147      (* CSC: rtl_if_result/f_result; list vs option *)
148      let dest ≝ rtl_if_result (func f) in
149      ! v ←  mmap … (reg_retrieve (locals f)) dest;
150      ret ? 〈E0, Returnstate v (retdst f) fs (free m (sp f))〉
151  | rtl_st_cond src ltrue lfalse ⇒
152      ! v ← reg_retrieve (locals f) src;
153      ! b ← eval_bool_of_val v;
154      ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉
155
156  (*
157  | St_const r cst l ⇒
158      ! v ← opt_to_res … (msg FailedConstant) (eval_constant (find_symbol … ge) (sp f) cst);
159      ! locals ← reg_store r v (locals f);
160      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
161  | St_op1 op dst src l ⇒
162      ! v ← reg_retrieve (locals f) src;
163      ! v' ← opt_to_res … (msg FailedOp) (eval_unop op v);
164      ! locals ← reg_store dst v' (locals f);
165      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉
166  | St_op2 op dst src1 src2 l ⇒
167      ! v1 ← reg_retrieve (locals f) src1;
168      ! v2 ← reg_retrieve (locals f) src2;
169      ! v' ← opt_to_res … (msg FailedOp) (eval_binop op v1 v2);
170      ! locals ← reg_store dst v' (locals f);
171      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f)) fs m〉 *)
172  (* CSC: Removed: St_jumptable *)
173  (* CSC: Added: *)
174  | rtl_st_addr ldest hdest id l ⇒ ?
175  | rtl_st_stack_addr ldest hdest l ⇒ ?
176  (* CSC: St_const splitted into rtl_st_int *)
177  | rtl_st_int dest v l ⇒
178     ! locals ← reg_store dest (val_of_Byte v) (locals f);
179     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
180  | rtl_st_move dreg sreg l ⇒ ?
181  | rtl_st_opaccs op dacc dbacc sacc sbacc l ⇒ ?
182  | rtl_st_op1 op dreg sreg l ⇒ ?
183  | rtl_st_op2 op dreg sreg1 sreg2 l ⇒ ?
184 
185  | rtl_st_clear_carry l ⇒
186    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vfalse) fs m〉
187  | rtl_st_set_carry l ⇒
188    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vtrue) fs m〉
189  ]
190| Callstate fd params dst fs m ⇒ ? (* CSC: XXXXXXXXXXXX
191    match fd with
192    [ Internal fn ⇒
193        ! locals ← params_store (f_params fn) params (init_locals (f_locals fn));
194        let 〈m', sp〉 ≝ alloc m 0 (f_stacksize fn) Any in
195        ret ? 〈E0, State (mk_frame fn locals (f_entry fn) sp dst Vundef) fs m'〉
196    | External fn ⇒
197        ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
198        ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
199        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), Returnstate (Some ? (mk_val ? evres) (*FIXME should allow None *)) dst fs m〉
200    ] *)
201| Returnstate v dst fs m ⇒ ? (* CSC: XXXXXXXXXXXXXXXXXX
202    match fs with
203    [ nil ⇒ Error ? (msg FinalState) (* Already in final state *)
204    | cons f fs' ⇒
205        ! locals ← match dst with
206                   [ None ⇒ match v with [ None ⇒ OK ? (locals f)
207                                         | _ ⇒ Error ? (msg ReturnMismatch) ]
208                   | Some d ⇒ match v with [ None ⇒ Error ? (msg ReturnMismatch)
209                                           | Some v' ⇒ reg_store d v' (locals f) ] ];
210        ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f)) fs' m〉
211    ] *)
212].
213
214definition is_final : state → option int ≝
215λs. match s with
216[ State _ _ _ ⇒ None ?
217| Callstate _ _ _ _ _ ⇒ None ?
218| Returnstate v _ fs _ ⇒
219    match fs with [ nil ⇒
220      match v with [ Some v' ⇒
221        match v' with
222        [ Vint sz i ⇒ intsize_eq_elim ? sz I32 ? i (λi. Some ? i) (None ?)
223        | _ ⇒ None ? ]
224      | None ⇒ None ? ]
225    | cons _ _ ⇒ None ? ]
226].
227
228definition RTLabs_exec : execstep io_out io_in ≝
229  mk_execstep … ? is_final mem_of_state eval_statement.
230
231definition make_initial_state : RTLabs_program → res (genv × state) ≝
232λp.
233  do ge ← globalenv Genv ?? p;
234  do m ← init_mem Genv ?? p;
235  let main ≝ prog_main ?? p in
236  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
237  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
238  OK ? 〈ge,Callstate f (nil ?) (None ?) (nil ?) m〉.
239
240definition RTLabs_fullexec : fullexec io_out io_in ≝
241  mk_fullexec … RTLabs_exec ? make_initial_state.
242
Note: See TracBrowser for help on using the repository browser.