source: src/ERTL/semantics.ma @ 1142

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

More progress.

File size: 12.8 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 "ERTL/ERTL.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(*
24(* CSC: ???????????? In OCaml: IntValue.Int32.merge
25   Note also that the translation can fail; so it should be a res int! *)
26axiom smerge2: list val → int.
27*)
28(* CSC: I have a byte, I need a value, but it is complex to do so *)
29axiom val_of_Byte: Byte → val.
30axiom Byte_of_val: val → res Byte.
31(* Map from the front-end memory model to the back-end memory model *)
32axiom represent_block: block → val × val.
33(* CSC: fixed size chunk *)
34axiom chunk: memory_chunk.
35axiom val_of_memory_chunk: memory_chunk → val. (* CSC: only to shift the sp *)
36
37axiom address: Type[0].
38axiom val_of_address: address → val. (* CSC: only to shift the sp *)
39axiom address_of_val: val → address. (* CSC: only to shift the sp *)
40(* CSC: ??? *)
41axiom address_of_label: mem → label → address.
42axiom address_chunks_of_label: mem → label → Byte × Byte.
43axiom label_of_address_chunks: Byte → Byte → label.
44axiom address_of_address_chunks: Byte → Byte → address.
45axiom mRegisterMap: Type[0]. (* CSC: use the one in I8051? *)
46axiom hwreg_retrieve : mRegisterMap → Register → res val.
47axiom hwreg_store : Register → val → mRegisterMap → res mRegisterMap.
48
49definition genv ≝ (genv_t Genv) (fundef ertl_internal_function).
50
51(* CSC: frame reduced to this *)
52definition frame: Type[0] ≝ register_env val.
53
54record state : Type[0] ≝
55 { st_frms: list frame
56 ; pc: address
57 ; sp: address
58(* ; exit: address *)
59 ; locals: register_env val
60 ; carry: val
61 ; regs: mRegisterMap
62 ; m: mem
63 }.
64
65definition set_m: mem → state → state ≝
66 λm,st. mk_state (st_frms st) (pc st) (sp st) (locals st) (carry st) (regs st) m.
67
68definition set_locals: register_env val → state → state ≝
69 λlocals,st. mk_state (st_frms st) (pc st) (sp st) locals (carry st) (regs st) (m st).
70
71definition set_regs: mRegisterMap → state → state ≝
72 λregs,st. mk_state (st_frms st) (pc st) (sp st) (locals st) (carry st) regs (m st).
73
74definition set_sp: address → state → state ≝
75 λsp,st. mk_state (st_frms st) (pc st) sp (locals st) (carry st) (regs st) (m st).
76
77definition set_carry: val → state → state ≝
78 λcarry,st. mk_state (st_frms st) (pc st) (sp st) (locals st) carry (regs st) (m st).
79
80definition set_pc: address → state → state ≝
81 λpc,st. mk_state (st_frms st) pc (sp st) (locals st) (carry st) (regs st) (m st).
82
83(* CSC: was build_state in RTL *)
84definition goto: label → state → state ≝
85 λl,st. set_pc (address_of_label (m st) l) st.
86
87(* pushes the locals (i.e. the current frame) on top of the frames list *)
88definition save_frame: state → state ≝
89 λst. mk_state (locals st::st_frms st) (pc st) (sp st) (locals st) (carry st) (regs st) (m st).
90
91axiom FailedPopFrame: String.
92
93(* removes the top frame from the frames list *)
94definition pop_frame: state → res state ≝
95 λst.
96  match st_frms st with
97   [ nil ⇒ Error ? (msg FailedPopFrame)
98   | cons _ frms ⇒
99      OK ? (mk_state frms (pc st) (sp st) (locals st) (carry st) (regs st) (m st)) ].
100
101axiom FailedStore: String.
102
103definition push: state → Byte → res state ≝
104 λst,v.
105  let sp ≝ val_of_address (sp st) in
106  (*CSC: no monadic notation here *)
107  bind ?? (opt_to_res ? (msg FailedStore) (storev chunk (m st) sp (val_of_Byte v)))
108   (λm.
109    let sp ≝ address_of_val (add sp (val_of_memory_chunk chunk)) in
110    OK ? (set_m m (set_sp sp st))).
111
112
113definition pop: state → res (state × Byte) ≝
114 λst:state.
115  let sp ≝ val_of_address (sp st) in
116  let sp ≝ sub sp (val_of_memory_chunk chunk) in
117  let st ≝ set_sp (address_of_val sp) st in
118  (*CSC: no monadic notation here *)
119  bind ?? (opt_to_res ? (msg FailedStore) (loadv chunk (m st) sp))
120   (λv. bind ?? (Byte_of_val v) (λv. OK ? 〈st,v〉)).
121
122definition save_ra : state → label → res state ≝
123 λst,l.
124  let 〈addrl,addrh〉 ≝ address_chunks_of_label (m st) l in
125  (* No monadic notation here *)
126  bind ?? (push st addrl) (λst.push st addrh).
127
128definition fetch_ra : state → res (state × label) ≝
129 λst.
130  (* No monadic notation here *)
131  bind ?? (pop st) (λres. let 〈st,addrh〉 ≝ res in
132  bind ?? (pop st) (λres. let 〈st,addrl〉 ≝ res in
133   OK ? 〈st, label_of_address_chunks addrl addrh〉)).
134
135(*CSC: To be implemented *)
136axiom fetch_statement: state → res ertl_statement.
137
138definition init_locals : list register → register_env val ≝
139foldr ?? (λid,en. add RegisterTag val en id Vundef) (empty_map ??).
140
141definition reg_store ≝
142λreg,v,locals. update RegisterTag val locals reg v.
143
144(*
145axiom WrongNumberOfParameters : String.
146
147(* CSC: modified to take in input a list of registers (untyped) *)
148let rec params_store (rs:list register) (vs:list val) (locals : register_env val) : res (register_env val) ≝
149match rs with
150[ nil ⇒ match vs with [ nil ⇒ OK ? locals | _ ⇒ Error ? (msg WrongNumberOfParameters)]
151| cons r rst ⇒ match vs with [ nil ⇒ Error ? (msg WrongNumberOfParameters) | cons v vst ⇒
152  let locals' ≝ add RegisterTag val locals r v in
153  params_store rst vst locals'
154] ].
155*)
156
157axiom BadRegister : String.
158
159definition reg_retrieve : register_env val → register → res val ≝
160λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
161
162(*
163axiom FailedOp : String.
164*)
165axiom MissingSymbol : String.
166(*
167axiom MissingStatement : String.
168axiom FailedConstant : String. *)
169axiom FailedLoad : String.
170axiom BadFunction : String.
171(*axiom BadJumpTable : String.
172axiom BadJumpValue : String.
173axiom FinalState : String.
174axiom ReturnMismatch : String.
175*)
176
177(*CSC: to be implemented *)
178axiom fetch_external_args: external_function → state → list val.
179axiom set_result: list val → state → state.
180
181definition eval_statement : genv → state → IO io_out io_in (trace × state) ≝
182λge,st.
183  ! s ← fetch_statement st;
184  match s with
185  [ ertl_st_skip l ⇒ ret ? 〈E0, goto l st〉
186  | ertl_st_cost cl l ⇒ ret ? 〈Echarge cl, goto l st〉
187  | ertl_st_int dest v l ⇒
188     ! locals ← reg_store dest (val_of_Byte v) (locals st);
189     ret ? 〈E0, goto l (set_locals locals st)〉
190  | ertl_st_load addrl addrh dst l ⇒
191      ! vaddrh ← reg_retrieve (locals st) addrh;
192      ! vaddrl ← reg_retrieve (locals st) addrl;
193      ! vaddr ← smerge vaddrl vaddrh;
194      ! v ← opt_to_res … (msg FailedLoad) (loadv chunk (m st) vaddr);
195      ! locals ← reg_store dst v (locals st);
196      ret ? 〈E0, goto l (set_locals locals st)〉
197  | ertl_st_store addrl addrh src l ⇒
198      ! vaddrh ← reg_retrieve (locals st) addrh;
199      ! vaddrl ← reg_retrieve (locals st) addrl;
200      ! vaddr ← smerge vaddrl vaddrh;
201      ! v ← reg_retrieve (locals st) src;
202      ! m' ← opt_to_res … (msg FailedStore) (storev chunk (m st) vaddr v);
203      ret ? 〈E0, goto l (set_m m' st)〉
204  | ertl_st_cond src ltrue lfalse ⇒
205      ! v ← reg_retrieve (locals st) src;
206      ! b ← eval_bool_of_val v;
207      ret ? 〈E0, goto (if b then ltrue else lfalse) st〉
208  | ertl_st_addr ldest hdest id l ⇒
209     ! addr ← opt_to_res … [MSG MissingSymbol; CTX … id] (find_symbol ?? ge id);
210     let 〈laddr,haddr〉 ≝ represent_block addr in
211     ! locals ← reg_store ldest laddr (locals st);
212     ! locals ← reg_store hdest haddr locals;
213     ret ? 〈E0, goto l (set_locals locals st)〉
214  | ertl_st_op1 op dst src l ⇒
215     ! v ← reg_retrieve (locals st) src;
216     ! v ← Byte_of_val v;
217     let v' ≝ val_of_Byte (op1 eval op v) in
218     ! locals ← reg_store dst v' (locals st);
219     ret ? 〈E0, goto l (set_locals locals st)〉
220  | ertl_st_op2 op dst src1 src2 l ⇒
221     ! v1 ← reg_retrieve (locals st) src1;
222     ! v1 ← Byte_of_val v1;
223     ! v2 ← reg_retrieve (locals st) src2;
224     ! v2 ← Byte_of_val v2;
225     ! carry ← eval_bool_of_val (carry st);
226     let 〈v',carry〉 ≝ op2 eval carry op v1 v2 in
227     let v' ≝ val_of_Byte v' in
228     let carry ≝ of_bool carry in
229     ! locals ← reg_store dst v' (locals st);
230     ret ? 〈E0, goto l (set_locals locals (set_carry carry st))〉
231  | ertl_st_opaccs op dacc dbacc sacc sbacc l ⇒
232     ! v1 ← reg_retrieve (locals st) sacc;
233     ! v1 ← Byte_of_val v1;
234     ! v2 ← reg_retrieve (locals st) sbacc;
235     ! v2 ← Byte_of_val v2;
236     let 〈v1',v2'〉 ≝ opaccs eval op v1 v2 in
237     let v1' ≝ val_of_Byte v1' in
238     let v2' ≝ val_of_Byte v2' in
239     ! locals ← reg_store dacc v1' (locals st);
240     ! locals ← reg_store dbacc v2' locals;
241     ret ? 〈E0, goto l (set_locals locals st)〉
242  | ertl_st_clear_carry l ⇒
243    ret ? 〈E0, goto l (set_carry Vfalse st)〉
244  | ertl_st_set_carry l ⇒
245    ret ? 〈E0, goto l (set_carry Vtrue st)〉
246     
247  (*CSC: rtl_st_move is splitted into ertl_st_move, ertl_st_get_hdw,
248         ertl_st_set_hdw, ertl_stl_hdw_to_hdw. Maybe one datatype would
249         be more than enough... *)
250  | ertl_st_move dst src l ⇒
251     ! v ← reg_retrieve (locals st) src;
252     ! locals ← reg_store dst v (locals st);
253     ret ? 〈E0, goto l (set_locals locals st)〉
254  | ertl_st_hdw_to_hdw dst src l ⇒
255     ! v ← hwreg_retrieve (regs st) src;
256     ! regs ← hwreg_store dst v (regs st);
257     ret ? 〈E0, goto l (set_regs regs st)〉
258  | ertl_st_get_hdw dst src l ⇒
259     ! v ← hwreg_retrieve (regs st) src;
260     ! locals ← reg_store dst v (locals st);
261     ret ? 〈E0, goto l (set_locals locals st)〉
262  | ertl_st_set_hdw dst src l ⇒
263     ! v ← reg_retrieve (locals st) src;
264     ! regs ← hwreg_store dst v (regs st);
265     ret ? 〈E0, goto l (set_regs regs st)〉
266
267  (* CSC: Dropped:
268      - rtl_st_stackaddr (becomes two get_hdw)
269      - rtl_st_tailcall_id/rtl_st_tailcall_ptr (unimplemented ATM)
270      - rtl_st_call_ptr (unimplemented ATM) *)
271     
272  (* CSC: changed, where the meat is *)
273  | ertl_st_call_id id argsno l ⇒ (* CSC: only the number of args kept, no return reg *)
274      ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
275      ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
276      match fd with
277      [ Internal fn ⇒
278        ! st ← save_ra st l;
279        let st ≝ save_frame st in
280        let locals ≝ init_locals (ertl_if_locals fn) in
281        let l' ≝ ertl_if_entry fn in
282         ret ? 〈 E0, goto l' (set_locals locals st)〉
283      | External fn ⇒
284        let params ≝ fetch_external_args fn st in
285        ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
286        ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
287        (* CSC: XXX bug here; I think I should split it into Byte-long
288           components; instead I am making a singleton out of it *)
289        let vs ≝ [mk_val ? evres] in
290        ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres),
291         goto l (set_result vs st)〉
292      ]
293  | ertl_st_return ⇒
294      ! st ← pop_frame st;
295      ! 〈st,pch〉 ← pop st;
296      ! 〈st,pcl〉 ← pop st;
297      let pc ≝ address_of_address_chunks pcl pch in
298      ret ? 〈E0,set_pc pc st〉
299  | _ ⇒ ? ]
300(*
301  ]
302| Returnstate v dst fs m ⇒
303    match fs with
304    [ nil ⇒ Error ? (msg FinalState) (* Already in final state *)
305    | cons f fs' ⇒
306       ! locals ←
307         mfold_left2 ???
308          (λlocals,dst,v. reg_store dst v locals) (OK ? (locals f)) dst v;
309        ret ? 〈E0, State (mk_frame (func f) locals (next f) (sp f) (retdst f) (carry f)) fs' m〉
310    ]
311]*).
312
313axiom WrongReturnValueType: String.
314
315definition is_final : state → option ((*CSC: why not res*) int) ≝
316λs. match s with
317[ State _ _ _ ⇒ None ?
318| Callstate _ _ _ _ _ ⇒ None ?
319| Returnstate v _ fs _ ⇒
320    match fs with
321     [ nil ⇒ Some ? (smerge2 v)
322     | _ ⇒ None ? ]].
323
324definition RTL_exec : execstep io_out io_in ≝
325  mk_execstep … ? is_final mem_of_state eval_statement.
326
327(* CSC: XXX the program type does not fit with the globalenv and init_mem
328definition make_initial_state : rtl_program → res (genv × state) ≝
329λp.
330  do ge ← globalenv Genv ?? p;
331  do m ← init_mem Genv ?? p;
332  let main ≝ rtl_pr_main ?? p in
333  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
334  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
335  OK ? 〈ge,Callstate f (nil ?) [ ] (nil ?) m〉.
336
337definition RTL_fullexec : fullexec io_out io_in ≝
338  mk_fullexec … RTL_exec ? make_initial_state.
339*)
Note: See TracBrowser for help on using the repository browser.