source: src/ERTL/semantics.ma @ 1161

Last change on this file since 1161 was 1161, checked in by mulligan, 9 years ago

changes from today: merged ertl, ltl and lin into one datatype to simplify semantics

File size: 15.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(* CSC: ???????????? In OCaml: IntValue.Int32.merge
24   Note also that the translation can fail; so it should be a res int! *)
25axiom smerge2: list val → int.
26(* CSC: I have a byte, I need a value, but it is complex to do so *)
27axiom val_of_Byte: Byte → val.
28axiom Byte_of_val: val → res Byte.
29axiom val_of_nat: nat → val.
30(* Map from the front-end memory model to the back-end memory model *)
31axiom represent_block: block → val × val.
32(* CSC: fixed size chunk *)
33axiom chunk: memory_chunk.
34axiom val_of_memory_chunk: memory_chunk → val. (* CSC: only to shift the sp *)
35
36axiom address: Type[0].
37axiom val_of_address: address → val. (* CSC: only to shift the sp *)
38axiom address_of_val: val → address. (* CSC: only to shift the sp *)
39axiom addr_sub: address → nat → address.
40axiom addr_add: address → nat → address.
41(* CSC: ??? *)
42axiom address_of_label: mem → label → address.
43axiom address_chunks_of_label: mem → label → Byte × Byte.
44axiom label_of_address_chunks: Byte → Byte → label.
45axiom address_of_address_chunks: Byte → Byte → address.
46axiom address_chunks_of_address: address → Byte × Byte. (* CSC: only to shift the hwsp *)
47axiom mRegisterMap: Type[0]. (* CSC: use the one in I8051? *)
48axiom hwreg_retrieve : mRegisterMap → Register → res val.
49axiom hwreg_store : Register → val → mRegisterMap → res mRegisterMap.
50
51definition genv ≝ λglobals. (genv_t Genv) (fundef (ertl_internal_function globals)).
52
53(* CSC: frame reduced to this *)
54definition frame: Type[0] ≝ register_env val.
55
56(* CSC: exit not put in the state, like in OCaml. It is constant througout
57   execution *)
58record state : Type[0] ≝
59 { st_frms: list frame
60 ; pc: address
61 ; sp: address
62 ; locals: register_env val
63 ; carry: val
64 ; regs: mRegisterMap
65 ; m: mem
66 }.
67
68definition set_m: mem → state → state ≝
69 λm,st. mk_state (st_frms st) (pc st) (sp st) (locals st) (carry st) (regs st) m.
70
71definition set_locals: register_env val → state → state ≝
72 λlocals,st. mk_state (st_frms st) (pc st) (sp st) locals (carry st) (regs st) (m st).
73
74definition set_regs: mRegisterMap → state → state ≝
75 λregs,st. mk_state (st_frms st) (pc st) (sp st) (locals st) (carry st) regs (m st).
76
77definition set_sp: address → state → state ≝
78 λsp,st. mk_state (st_frms st) (pc st) sp (locals st) (carry st) (regs st) (m st).
79
80definition set_carry: val → state → state ≝
81 λcarry,st. mk_state (st_frms st) (pc st) (sp st) (locals st) carry (regs st) (m st).
82
83definition set_pc: address → state → state ≝
84 λpc,st. mk_state (st_frms st) pc (sp st) (locals st) (carry st) (regs st) (m st).
85
86(* CSC: was build_state in RTL *)
87definition goto: label → state → state ≝
88 λl,st. set_pc (address_of_label (m st) l) st.
89
90(* pushes the locals (i.e. the current frame) on top of the frames list *)
91definition save_frame: state → state ≝
92 λst. mk_state (locals st::st_frms st) (pc st) (sp st) (locals st) (carry st) (regs st) (m st).
93
94axiom FailedPopFrame: String.
95
96(* removes the top frame from the frames list *)
97definition pop_frame: state → res state ≝
98 λst.
99  match st_frms st with
100   [ nil ⇒ Error ? (msg FailedPopFrame)
101   | cons _ frms ⇒
102      OK ? (mk_state frms (pc st) (sp st) (locals st) (carry st) (regs st) (m st)) ].
103
104axiom FailedStore: String.
105
106definition push: state → Byte → res state ≝
107 λst,v.
108  let sp ≝ val_of_address (sp st) in
109  (*CSC: no monadic notation here *)
110  bind ?? (opt_to_res ? (msg FailedStore) (storev chunk (m st) sp (val_of_Byte v)))
111   (λm.
112    let sp ≝ address_of_val (add sp (val_of_memory_chunk chunk)) in
113    OK ? (set_m m (set_sp sp st))).
114
115
116definition pop: state → res (state × Byte) ≝
117 λst:state.
118  let sp ≝ val_of_address (sp st) in
119  let sp ≝ sub sp (val_of_memory_chunk chunk) in
120  let st ≝ set_sp (address_of_val sp) st in
121  (*CSC: no monadic notation here *)
122  bind ?? (opt_to_res ? (msg FailedStore) (loadv chunk (m st) sp))
123   (λv. bind ?? (Byte_of_val v) (λv. OK ? 〈st,v〉)).
124
125definition save_ra : state → label → res state ≝
126 λst,l.
127  let 〈addrl,addrh〉 ≝ address_chunks_of_label (m st) l in
128  (* No monadic notation here *)
129  bind ?? (push st addrl) (λst.push st addrh).
130
131definition fetch_ra : state → res (state × label) ≝
132 λst.
133  (* No monadic notation here *)
134  bind ?? (pop st) (λres. let 〈st,addrh〉 ≝ res in
135  bind ?? (pop st) (λres. let 〈st,addrl〉 ≝ res in
136   OK ? 〈st, label_of_address_chunks addrl addrh〉)).
137
138(*CSC: To be implemented *)
139
140axiom fetch_statement: ∀globals: list ident. state → res (ertl_statement globals).
141axiom fetch_function: state → res ertl_internal_function.
142
143definition init_locals : list register → register_env val ≝
144foldr ?? (λid,en. add RegisterTag val en id Vundef) (empty_map ??).
145
146definition reg_store ≝
147λreg,v,locals. update RegisterTag val locals reg v.
148
149axiom BadRegister : String.
150
151definition reg_retrieve : register_env val → register → res val ≝
152λlocals,reg. opt_to_res … [MSG BadRegister; CTX ? reg] (lookup ?? locals reg).
153
154axiom MissingSymbol : String.
155axiom FailedLoad : String.
156axiom BadFunction : String.
157
158(*CSC: to be implemented *)
159axiom fetch_external_args: external_function → state → res (list val).(* ≝
160 λfn,st.
161  let registerno ≝ ? fn in
162  ! vs ←
163   mmap …
164    (λr. hwreg_retrieve (regs st) r) (prefix … registersno RegisterParameters);
165  (* CSC: HERE WE NEED TO RETRIEVE THE REMAINING REGISTERS FROM THE STACK! *)
166  ?.*)
167
168(*CSC: to be implemented; fails if the first list is longer *)
169axiom fold_left2_first_not_longer:
170 ∀A,B,C:Type[0]. ∀f:(C → A → B → res C).
171  ∀acc:C. ∀l1:list A. ∀l2: list B.
172   res C.
173
174(* CSC: the list should be a vector or have an upper bounded length *)
175definition set_result: list val → state → res state ≝
176 λvs,st.
177  (* CSC: monadic notation not used *)
178  bind ?? (
179   fold_left2_first_not_longer …
180    (λregs,v,reg. hwreg_store reg v regs) (regs st) vs RegisterRets)
181  (λregs.OK ? (set_regs regs st)).
182
183definition fetch_result: state → nat → res (list val) ≝
184 λst,registersno.
185  let retregs ≝ prefix … registersno RegisterRets in
186  mmap … (λr. hwreg_retrieve (regs st) r) retregs.
187
188definition framesize: state → res nat ≝
189 λst.
190  (* CSC: monadic notation missing here *)
191  bind ?? (fetch_function st) (λf.
192  OK ? (ertl_if_stacksize f)).
193
194definition get_hwsp : state → res address ≝
195 λst.
196  (* CSC: monadic notation missing here *)
197  bind ?? (hwreg_retrieve (regs st) RegisterSPL) (λspl.
198  (* CSC: monadic notation missing here *)
199  bind ?? (Byte_of_val spl) (λspl.
200  (* CSC: monadic notation missing here *)
201  bind ?? (hwreg_retrieve (regs st) RegisterSPL) (λsph.
202  (* CSC: monadic notation missing here *)
203  bind ?? (Byte_of_val sph) (λsph.
204  OK ? (address_of_address_chunks spl sph))))).
205
206definition set_hwsp : address → state → res state ≝
207 λsp,st.
208  let 〈spl,sph〉 ≝ address_chunks_of_address sp in
209  (* CSC: monadic notation missing here *) 
210  bind ?? (hwreg_store RegisterSPL (val_of_Byte spl) (regs st)) (λregs.
211  (* CSC: monadic notation missing here *) 
212  bind ?? (hwreg_store RegisterSPL (val_of_Byte sph) regs) (λregs.
213  OK ? (set_regs regs st))).
214
215definition eval_statement : ∀globals: list ident. (genv globals) → state → IO io_out io_in (trace × state) ≝
216  λglobals. λge,st.
217  ! s ← fetch_statement globals st;
218  match s with
219  [ ertl_st_lift_pre pre ⇒
220    match pre with
221    [ joint_st_goto l ⇒ ret ? 〈E0, goto l st〉
222    | joint_st_sequential seq l ⇒
223      match seq with
224      [ joint_instr_cost_label cl ⇒ ret ? 〈Echarge cl, goto l st〉
225      | joint_instr_comment c ⇒ ret ? 〈E0, goto l st〉
226      | joint_instr_int dest v ⇒
227        ! locals ← reg_store dest (val_of_Byte v) (locals st);
228          ret ? 〈E0, goto l (set_locals locals st)〉
229      | joint_instr_load addrl addrh dst ⇒
230        ! vaddrh ← reg_retrieve (locals st) addrh;
231        ! vaddrl ← reg_retrieve (locals st) addrl;
232        ! vaddr ← smerge vaddrl vaddrh;
233        ! v ← opt_to_res … (msg FailedLoad) (loadv chunk (m st) vaddr);
234        ! locals ← reg_store dst v (locals st);
235          ret ? 〈E0, goto l (set_locals locals st)〉
236      | joint_instr_store addrl addrh src ⇒
237        ! vaddrh ← reg_retrieve (locals st) addrh;
238        ! vaddrl ← reg_retrieve (locals st) addrl;
239        ! vaddr ← smerge vaddrl vaddrh;
240        ! v ← reg_retrieve (locals st) src;
241        ! m' ← opt_to_res … (msg FailedStore) (storev chunk (m st) vaddr v);
242          ret ? 〈E0, goto l (set_m m' st)〉
243      | joint_instr_cond src ltrue ⇒
244        ! v ← reg_retrieve (locals st) src;
245        ! b ← eval_bool_of_val v;
246          ret ? 〈E0, goto (if b then ltrue else l) st〉
247      | joint_instr_address ident prf ldest hdest ⇒
248        ! addr ← opt_to_res ? [MSG MissingSymbol; CTX … ident] (find_symbol ?? ge ident);
249          let 〈laddr,haddr〉 ≝ represent_block addr in
250        ! locals ← reg_store ldest laddr (locals st);
251        ! locals ← reg_store hdest haddr locals;
252          ret ? 〈E0, goto l (set_locals locals st)〉
253      | joint_instr_op1 op acc_a ⇒
254        ! v ← reg_retrieve (locals st) acc_a;
255        ! v ← Byte_of_val v;
256          let v' ≝ val_of_Byte (op1 eval op v) in
257        ! locals ← reg_store acc_a v' (locals st);
258          ret ? 〈E0, goto l (set_locals locals st)〉
259      | joint_instr_op2 op acc_a_reg dest ⇒
260        ! v1 ← reg_retrieve (locals st) acc_a_reg;
261        ! v1 ← Byte_of_val v1;
262        ! v2 ← reg_retrieve (locals st) acc_a_reg;
263        ! v2 ← Byte_of_val v2;
264        ! carry ← eval_bool_of_val (carry st);
265          let 〈v',carry〉 ≝ op2 eval carry op v1 v2 in
266          let v' ≝ val_of_Byte v' in
267          let carry ≝ of_bool carry in
268        ! locals ← reg_store dest v' (locals st);
269          ret ? 〈E0, goto l (set_locals locals (set_carry carry st))〉
270      | joint_instr_clear_carry ⇒ ret ? 〈E0, goto l (set_carry Vfalse st)〉
271      | joint_instr_set_carry ⇒ ret ? 〈E0, goto l (set_carry Vtrue st)〉
272      | joint_instr_hdw_to_hdw dst_src ⇒
273          let 〈dst, src〉 ≝ dst_src in
274        ! v ← hwreg_retrieve (regs st) src;
275        ! regs ← hwreg_store dst v (regs st);
276          ret ? 〈E0, goto l (set_regs regs st)〉
277(* XXX: examine: where is the result being stored?
278      | joint_instr_opaccs op acc_a_reg acc_b_reg ⇒
279        ! v1 ← reg_retrieve (locals st) acc_a_reg;
280        ! v1 ← Byte_of_val v1;
281        ! v2 ← reg_retrieve (locals st) acc_b_reg;
282        ! v2 ← Byte_of_val v2;
283          let 〈v1',v2'〉 ≝ opaccs eval op v1 v2 in
284          let v1' ≝ val_of_Byte v1' in
285          let v2' ≝ val_of_Byte v2' in
286        ! locals ← reg_store dacc v1' (locals st);
287        ! locals ← reg_store dbacc v2' locals;
288          ret ? 〈E0, goto l (set_locals locals st)〉
289*)
290      | joint_instr_pop dst ⇒
291        ! 〈st,v〉 ← pop st;
292        ! locals ← reg_store dst (val_of_Byte v) (locals st);
293          ret ? 〈E0, goto l (set_locals locals st)〉
294      | joint_instr_push src ⇒
295        ! v ← reg_retrieve (locals st) src;
296        ! v ← Byte_of_val v;
297        ! st ← push st v;
298          ret ? 〈E0, goto l st〉
299      | joint_instr_call_id id argsno ⇒ (* CSC: only the number of args kept, no return reg *)
300        ! b ← opt_to_res … [MSG MissingSymbol; CTX ? id] (find_symbol ?? ge id);
301        ! fd ← opt_to_res … [MSG BadFunction; CTX ? id] (find_funct_ptr ?? ge b);
302          match fd with
303          [ Internal fn ⇒
304            ! st ← save_ra st l;
305              let st ≝ save_frame st in
306              let locals ≝ init_locals (ertl_if_locals fn) in
307              let l' ≝ ertl_if_entry fn in
308              ret ? 〈 E0, goto l' (set_locals locals st)〉
309          | External fn ⇒
310            ! params ← fetch_external_args fn st;
311            ! evargs ← check_eventval_list params (sig_args (ef_sig fn));
312            ! evres ← do_io (ef_id fn) evargs (proj_sig_res (ef_sig fn));
313            (* CSC: XXX bug here; I think I should split it into Byte-long
314               components; instead I am making a singleton out of it *)
315              let vs ≝ [mk_val ? evres] in
316            ! st ← set_result vs st;
317              ret ? 〈Eextcall (ef_id fn) evargs (mk_eventval ? evres), goto l st〉
318      ]
319      | _ ⇒ ?
320      ]
321    | joint_st_return ⇒
322      ! st ← pop_frame st;
323      ! 〈st,pch〉 ← pop st;
324      ! 〈st,pcl〉 ← pop st;
325        let pc ≝ address_of_address_chunks pcl pch in
326        ret ? 〈E0,set_pc pc st〉
327    | _ ⇒ ?
328    ]
329  | ertl_st_get_hdw dst src l ⇒
330     ! v ← hwreg_retrieve (regs st) src;
331     ! locals ← reg_store dst v (locals st);
332     ret ? 〈E0, goto l (set_locals locals st)〉
333  | ertl_st_set_hdw dst src l ⇒
334     ! v ← reg_retrieve (locals st) src;
335     ! regs ← hwreg_store dst v (regs st);
336     ret ? 〈E0, goto l (set_regs regs st)〉
337  | ertl_st_new_frame _ ⇒ ?
338  | ertl_st_del_frame _ ⇒ ?
339  | ertl_st_frame_size _ _ ⇒ ?
340  (*CSC: rtl_st_move is splitted into ertl_st_move, ertl_st_get_hdw,
341         ertl_st_set_hdw, ertl_stl_hdw_to_hdw. Maybe one datatype would
342         be more than enough... *)
343  | ertl_st_move dst src l ⇒
344     ! v ← reg_retrieve (locals st) src;
345     ! locals ← reg_store dst v (locals st);
346     ret ? 〈E0, goto l (set_locals locals st)〉
347  ].
348 
349 
350  match s with
351
352  (* CSC: Dropped:
353      - rtl_st_stackaddr (becomes two get_hdw)
354      - rtl_st_tailcall_id/rtl_st_tailcall_ptr (unimplemented ATM)
355      - rtl_st_call_ptr (unimplemented ATM) *)
356     
357  (* CSC: changed, where the meat is *)
358 
359  (* CSC: new stuff *)
360  | ertl_st_comment _ l ⇒ ret ? 〈E0, goto l st〉
361<<<<<<< .mine  ].
362
363axiom WrongReturnValueType: String.
364
365definition is_final : label → nat → state → option ((*CSC: why not res*) int) ≝
366λexit,registersno,st.
367 (* CSC: monadic notation missing here *)
368 match fetch_statement st with
369  [ Error _ ⇒ None ?
370  | OK s ⇒
371     match s with
372      [ ertl_st_return ⇒
373       (* CSC: monadic notation missing here *)
374        match fetch_ra st with
375         [ Error _ ⇒ None ?
376         | OK st_l ⇒ let 〈st,l〉 ≝ st_l in
377            match label_eq l exit with
378             [ inl _ ⇒
379               (* CSC: monadic notation missing here *)
380                match fetch_result st registersno with
381                 [ Error _ ⇒ None ?
382                 | OK vals ⇒ Some ? (smerge2 vals) ]
383             | inr _ ⇒ None ? ]]
384      | _ ⇒ None … ]].
385
386definition ERTL_exec : label → nat → execstep io_out io_in ≝
387 λexit,registersno.
388  mk_execstep … ? (is_final exit registersno) m eval_statement.
389
390(* CSC: XXX the program type does not fit with the globalenv and init_mem
391definition make_initial_state : rtl_program → res (genv × state) ≝
392λp.
393  do ge ← globalenv Genv ?? p;
394  do m ← init_mem Genv ?? p;
395  let main ≝ rtl_pr_main ?? p in
396  do b ← opt_to_res ? [MSG MissingSymbol; CTX ? main] (find_symbol ? ? ge main);
397  do f ← opt_to_res ? [MSG BadFunction; CTX ? main] (find_funct_ptr ? ? ge b);
398  OK ? 〈ge,Callstate f (nil ?) [ ] (nil ?) m〉.
399
400definition RTL_fullexec : fullexec io_out io_in ≝
401  mk_fullexec … RTL_exec ? make_initial_state.
402*)
Note: See TracBrowser for help on using the repository browser.