source: src/RTL/semantics.ma @ 1121

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

External function calls implemented (but look at the new comment on the
semantics of the return type).

File size: 11.4 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: external functions never fail (??) and always return something of the
15   size of one register, both in the frontend and in the backend.
16   Is this reasonable??? In OCaml it always return a list, but in the frontend
17   only the head is kept (or Undef if the list is empty) ??? *)
18
19(* CSC: carries are values and booleans are not values; so we use integers.
20   But why using values at all? To have undef? *)
21(* CSC: ???????????? *)
22axiom smerge: val → val → res val.
23(* CSC: I have a byte, I need a value, but it is complex to do so *)
24axiom val_of_Byte: Byte → val.
25axiom Byte_of_val: val → res Byte.
26(* Map from the front-end memory model to the back-end memory model *)
27axiom represent_block: block → val × val.
28(* CSC: fixed size chunk *)
29axiom chunk: memory_chunk.
30
31definition genv ≝ (genv_t Genv) (fundef rtl_internal_function).
32
33(* CSC: added carry *)
34record frame : Type[0] ≝
35{ func   : rtl_internal_function
36; locals : register_env val
37; next   : label
38; sp     : block
39; retdst : list register
40; carry  : val
41}.
42
43(* CSC: added carry *)
44definition adv : label → frame → frame ≝
45λl,f. mk_frame (func f) (locals f) l (sp f) (retdst f) (carry f).
46
47inductive state : Type[0] ≝
48| State :
49   ∀   f : frame.
50   ∀  fs : list frame.
51   ∀   m : mem.
52   state
53| Callstate :
54   ∀  fd : fundef rtl_internal_function.
55   ∀args : list val.
56   ∀ dst : list register. (* CSC: changed from option to list *)
57   ∀ stk : list frame.
58   ∀   m : mem.
59   state
60| Returnstate :
61   ∀ rtv : list val. (* CSC: changed from option to list *)
62   ∀ dst : list register. (* CSC: changed from option to list *)
63   ∀ stk : list frame.
64   ∀   m : mem.
65   state.
66
67definition mem_of_state : state → mem ≝
68λs. match s with [ State _ _ m ⇒ m | Callstate _ _ _ _ m ⇒ m | Returnstate _ _ _ m ⇒ m ].
69
70definition build_state ≝ λf.λfs.λm.λn. State (adv n f) fs m.
71
72definition init_locals : list (register × typ) → register_env val ≝
73foldr ?? (λidt,en. let 〈id,ty〉 ≝ idt in add RegisterTag val en id Vundef) (empty_map ??).
74
75definition reg_store ≝
76λreg,v,locals.
77  update RegisterTag val locals reg v.
78
79axiom WrongNumberOfParameters : String.
80
81let rec params_store (rs:list (register × typ)) (vs:list val) (locals : register_env val) : res (register_env val) ≝
82match rs with
83[ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? (msg WrongNumberOfParameters)]
84| cons rt rst ⇒ match vs with [ nil ⇒ Error ? (msg WrongNumberOfParameters) | cons v vst ⇒
85  let 〈r,ty〉 ≝ rt in
86  let locals' ≝ add RegisterTag val locals r v in
87  params_store rst vst locals'
88] ].
89
90axiom BadRegister : String.
91
92definition reg_retrieve : register_env ? → register → res val ≝
93λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
94
95axiom FailedOp : String.
96axiom MissingSymbol : String.
97
98axiom MissingStatement : String.
99axiom FailedConstant : String.
100axiom FailedLoad : String.
101axiom FailedStore : String.
102axiom BadFunction : String.
103axiom BadJumpTable : String.
104axiom BadJumpValue : String.
105axiom FinalState : String.
106axiom ReturnMismatch : String.
107
108definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝
109λge,st.
110match st with
111[ State f fs m ⇒
112  ! s ← opt_to_io … [MSG MissingStatement; CTX ? (next f)] (lookup ?? (rtl_if_graph (func f)) (next f));
113  match s with
114  [ rtl_st_skip l ⇒ ret ? 〈E0, build_state f fs m l〉
115  | rtl_st_cost cl l ⇒ ret ? 〈Echarge cl, build_state f fs m l〉
116  | rtl_st_load addrl addrh dst l ⇒  (* CSC: pairs of regs vs reg *)
117      ! vaddrh ← reg_retrieve (locals f) addrh;
118      ! vaddrl ← reg_retrieve (locals f) addrl;
119      ! vaddr ← smerge vaddrl vaddrh;
120      ! v ← opt_to_res … (msg FailedLoad) (loadv chunk m vaddr);
121      ! locals ← reg_store dst v (locals f);
122      ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
123  | rtl_st_store addrl addrh src l ⇒  (* CSC: pairs of regs vs reg *)
124      ! vaddrh ← reg_retrieve (locals f) addrh;
125      ! vaddrl ← reg_retrieve (locals f) addrl;
126      ! vaddr ← smerge vaddrl vaddrh;
127      ! v ← reg_retrieve (locals f) src;
128      ! m' ← opt_to_res … (msg FailedStore) (storev chunk m vaddr v);
129      ret ? 〈E0, build_state f fs m' l〉
130  | rtl_st_call_id id args dst l ⇒
131      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
132      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
133      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
134      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
135  | rtl_st_call_ptr lfrs hfrs args dst l ⇒ (* CSC: pairs of regs vs reg *)
136      ! hfv ← reg_retrieve (locals f) hfrs;
137      ! lfv ← reg_retrieve (locals f) lfrs;
138      ! fv  ← smerge lfv hfv;
139      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
140      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
141      ret ? 〈E0, Callstate fd vs dst (adv l f::fs) m〉
142  | rtl_st_tailcall_id id args ⇒
143      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
144      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
145      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
146      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
147  | rtl_st_tailcall_ptr lfrs hfrs args ⇒ (* CSC: pairs of regs vs reg *)
148      ! hfv ← reg_retrieve (locals f) hfrs;
149      ! lfv ← reg_retrieve (locals f) lfrs;
150      ! fv  ← smerge lfv hfv;
151      ! fd ← opt_to_res … (msg BadFunction) (find_funct ?? ge fv);
152      ! vs ← mmap ?? (reg_retrieve (locals f)) args;
153      ret ? 〈E0, Callstate fd vs (retdst f) fs (free m (sp f))〉
154  | rtl_st_return ⇒
155      (* CSC: rtl_if_result/f_result; list vs option *)
156      let dest ≝ rtl_if_result (func f) in
157      ! v ←  mmap … (reg_retrieve (locals f)) dest;
158      ret ? 〈E0, Returnstate v (retdst f) fs (free m (sp f))〉
159  | rtl_st_cond src ltrue lfalse ⇒
160      ! v ← reg_retrieve (locals f) src;
161      ! b ← eval_bool_of_val v;
162      ret ? 〈E0, build_state f fs m (if b then ltrue else lfalse)〉
163  (* CSC: mismatch between the memory models and failures for the next two opx *)
164  | rtl_st_op1 op dst src l ⇒
165     ! v ← reg_retrieve (locals f) src;
166     ! v ← Byte_of_val v;
167     let v' ≝ val_of_Byte (op1 eval op v) in
168     ! locals ← reg_store dst v' (locals f);
169     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
170  | rtl_st_op2 op dst src1 src2 l ⇒
171     ! v1 ← reg_retrieve (locals f) src1;
172     ! v1 ← Byte_of_val v1;
173     ! v2 ← reg_retrieve (locals f) src2;
174     ! v2 ← Byte_of_val v2;
175     ! carry ← eval_bool_of_val (carry f);
176     let 〈v',carry〉 ≝ op2 eval carry op v1 v2 in
177     let v' ≝ val_of_Byte v' in
178     let carry ≝ of_bool carry in
179     ! locals ← reg_store dst v' (locals f);
180     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) carry) fs m〉
181
182  (* CSC: Removed: St_jumptable *)
183  (* CSC: Added: *)
184 
185  (* CSC: St_const splitted into rtl_st_int, rtl_st_addr and rt_st_stack_addr; *)
186  | rtl_st_addr ldest hdest id l ⇒
187     ! addr ← opt_to_res … [MSG MissingSymbol; CTX … id] (find_symbol ?? ge id);
188     let 〈laddr,haddr〉 ≝ represent_block addr in
189     ! locals ← reg_store ldest laddr (locals f);
190     ! locals ← reg_store hdest haddr locals;
191     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
192  | rtl_st_stack_addr ldest hdest l ⇒
193     let 〈laddr,haddr〉 ≝ represent_block (sp f) in
194     ! locals ← reg_store ldest laddr (locals f);
195     ! locals ← reg_store hdest haddr locals;
196     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
197  | rtl_st_int dest v l ⇒
198     ! locals ← reg_store dest (val_of_Byte v) (locals f);
199     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
200
201  (* CSC: St_op1 splitted into rtl_st_op1 and rtl_st_move *)
202  | rtl_st_move dst src l ⇒
203     ! v ← reg_retrieve (locals f) src;
204     ! locals ← reg_store dst v (locals f);
205     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
206  (* CSC: St_op2 splitted into rtl_st_op2 and rtl_st_opaccs *) 
207  | rtl_st_opaccs op dacc dbacc sacc sbacc l ⇒
208     ! v1 ← reg_retrieve (locals f) sacc;
209     ! v1 ← Byte_of_val v1;
210     ! v2 ← reg_retrieve (locals f) sbacc;
211     ! v2 ← Byte_of_val v2;
212     let 〈v1',v2'〉 ≝ opaccs eval op v1 v2 in
213     let v1' ≝ val_of_Byte v1' in
214     let v2' ≝ val_of_Byte v2' in
215     ! locals ← reg_store dacc v1' (locals f);
216     ! locals ← reg_store dbacc v2' locals;
217     ret ? 〈E0, State (mk_frame (func f) locals l (sp f) (retdst f) (carry f)) fs m〉
218 
219  | rtl_st_clear_carry l ⇒
220    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vfalse) fs m〉
221  | rtl_st_set_carry l ⇒
222    ret ? 〈E0, State (mk_frame (func f) (locals f) l (sp f) (retdst f) Vtrue) fs m〉
223  ]
224| Callstate fd params dst fs m ⇒
225    match fd with
226    [ Internal fn ⇒ ? (*
227        ! locals ← params_store (f_params fn) params (init_locals (f_locals fn));
228        let 〈m', sp〉 ≝ alloc m 0 (f_stacksize fn) Any in
229        ret ? 〈E0, State (mk_frame fn locals (f_entry fn) sp dst Vundef) fs m'〉 *)
230    | External fn ⇒
231        ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
232        ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
233        (* CSC: return type changed from option to list *)
234        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), Returnstate [mk_val ? evres] dst fs m〉
235    ]
236| Returnstate v dst fs m ⇒ ? (* CSC: XXXXXXXXXXXXXXXXXX
237    match fs with
238    [ nil ⇒ Error ? (msg FinalState) (* Already in final state *)
239    | cons f fs' ⇒
240        ! locals ← match dst with
241                   [ None ⇒ match v with [ None ⇒ OK ? (locals f)
242                                         | _ ⇒ Error ? (msg ReturnMismatch) ]
243                   | Some d ⇒ match v with [ None ⇒ Error ? (msg ReturnMismatch)
244                                           | Some v' ⇒ reg_store d v' (locals f) ] ];
245        ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f)) fs' m〉
246    ] *)
247].
248
249definition is_final : state → option int ≝
250λs. match s with
251[ State _ _ _ ⇒ None ?
252| Callstate _ _ _ _ _ ⇒ None ?
253| Returnstate v _ fs _ ⇒
254    match fs with [ nil ⇒
255      match v with [ Some v' ⇒
256        match v' with
257        [ Vint sz i ⇒ intsize_eq_elim ? sz I32 ? i (λi. Some ? i) (None ?)
258        | _ ⇒ None ? ]
259      | None ⇒ None ? ]
260    | cons _ _ ⇒ None ? ]
261].
262
263definition RTLabs_exec : execstep io_out io_in ≝
264  mk_execstep … ? is_final mem_of_state eval_statement.
265
266definition make_initial_state : RTLabs_program → res (genv × state) ≝
267λp.
268  do ge ← globalenv Genv ?? p;
269  do m ← init_mem Genv ?? p;
270  let main ≝ prog_main ?? p in
271  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
272  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
273  OK ? 〈ge,Callstate f (nil ?) (None ?) (nil ?) m〉.
274
275definition RTLabs_fullexec : fullexec io_out io_in ≝
276  mk_fullexec … RTLabs_exec ? make_initial_state.
277
Note: See TracBrowser for help on using the repository browser.