source: src/RTL/RTLtoERTL.ma @ 1131

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

changes to syntax of ertl: removed ertl_st_addr_l and ertl_st_addr_h in favourn of unified instruction ertl_st_addr

File size: 22.2 KB
RevLine 
[756]1include "RTL/RTL.ma".
[1079]2include "RTL/RTLTailcall.ma".
[1075]3include "utilities/RegisterSet.ma".
[1076]4include "common/Identifiers.ma".
[759]5include "ERTL/ERTL.ma".
[756]6
7definition change_exit_label ≝
8  λl: label.
[759]9  λp: ertl_internal_function.
[1077]10  λprf: lookup ? ? (ertl_if_graph p) l ≠ None ?.
[759]11  let ertl_if_luniverse' ≝ ertl_if_luniverse p in
12  let ertl_if_runiverse' ≝ ertl_if_runiverse p in
13  let ertl_if_params' ≝ ertl_if_params p in
14  let ertl_if_locals' ≝ ertl_if_locals p in
[782]15  let ertl_if_stacksize' ≝ ertl_if_stacksize p in
[759]16  let ertl_if_graph' ≝ ertl_if_graph p in
17  let ertl_if_entry' ≝ ertl_if_entry p in
18  let ertl_if_exit' ≝ l in
19    mk_ertl_internal_function ertl_if_luniverse' ertl_if_runiverse'
[782]20                              ertl_if_params' ertl_if_locals' ertl_if_stacksize'
[759]21                              ertl_if_graph' ertl_if_entry' ertl_if_exit'.
[1077]22  @prf
23qed.
[756]24
25definition change_entry_label ≝
26  λl: label.
[759]27  λp: ertl_internal_function.
[1077]28  λprf: lookup ? ? (ertl_if_graph p) l ≠ None ?.
[759]29  let ertl_if_luniverse' ≝ ertl_if_luniverse p in
30  let ertl_if_runiverse' ≝ ertl_if_runiverse p in
31  let ertl_if_params' ≝ ertl_if_params p in
32  let ertl_if_locals' ≝ ertl_if_locals p in
[782]33  let ertl_if_stacksize' ≝ ertl_if_stacksize p in
[759]34  let ertl_if_graph' ≝ ertl_if_graph p in
35  let ertl_if_entry' ≝ l in
36  let ertl_if_exit' ≝ ertl_if_exit p in
37    mk_ertl_internal_function ertl_if_luniverse' ertl_if_runiverse'
[782]38                              ertl_if_params' ertl_if_locals' ertl_if_stacksize'
[759]39                              ertl_if_graph' ertl_if_entry' ertl_if_exit'.
[1077]40  @prf
41qed.
[756]42                             
[759]43definition add_graph ≝
44  λl: label.
45  λstmt.
46  λp.
47  let ertl_if_luniverse' ≝ ertl_if_luniverse p in
48  let ertl_if_runiverse' ≝ ertl_if_runiverse p in
49  let ertl_if_params' ≝ ertl_if_params p in
50  let ertl_if_locals' ≝ ertl_if_locals p in
[782]51  let ertl_if_stacksize' ≝ ertl_if_stacksize p in
[759]52  let ertl_if_graph' ≝ add ? ? (ertl_if_graph p) l stmt in
53  let ertl_if_entry' ≝ ertl_if_entry p in
54  let ertl_if_exit' ≝ ertl_if_exit p in
55    mk_ertl_internal_function ertl_if_luniverse' ertl_if_runiverse'
[782]56                              ertl_if_params' ertl_if_locals' ertl_if_stacksize'
[1077]57                              ertl_if_graph' ? ?.
58  normalize nodelta;
59  [1: generalize in match ertl_if_entry';
60      #HYP
61      cases HYP
62      #LBL #LBL_PRF
63      %
64      [1: @LBL
65      |2: @graph_add_lookup
66          @LBL_PRF
67      ]
68  |2: generalize in match ertl_if_exit';
69      #HYP
70      cases HYP
71      #LBL #LBL_PRF
72      %
73      [1: @LBL
74      |2: @graph_add_lookup
75          @LBL_PRF
76      ]
77  ]
78qed.
[759]79                             
[756]80definition fresh_label ≝
81  λdef.
[1071]82    fresh LabelTag (ertl_if_luniverse def).
[756]83   
84definition change_label ≝
[759]85  λl.
86  λe: ertl_statement.
87  match e with
88  [ ertl_st_skip _ ⇒ ertl_st_skip l
89  | ertl_st_comment s _ ⇒ ertl_st_comment s l
90  | ertl_st_cost c _ ⇒ ertl_st_cost c l
91  | ertl_st_get_hdw r1 r2 _ ⇒ ertl_st_get_hdw r1 r2 l
92  | ertl_st_set_hdw r1 r2 _ ⇒ ertl_st_set_hdw r1 r2 l
93  | ertl_st_hdw_to_hdw r1 r2 _ ⇒ ertl_st_hdw_to_hdw r1 r2 l
94  | ertl_st_new_frame _ ⇒ ertl_st_new_frame l
95  | ertl_st_del_frame _ ⇒ ertl_st_del_frame l
96  | ertl_st_frame_size r _ ⇒ ertl_st_frame_size r l
97  | ertl_st_pop r _ ⇒ ertl_st_pop r l
98  | ertl_st_push r _ ⇒ ertl_st_push r l
[1131]99  | ertl_st_addr r1 r2 x _ ⇒ ertl_st_addr r1 r2 x l
[759]100  | ertl_st_int r i _ ⇒ ertl_st_int r i l
101  | ertl_st_move r1 r2 _ ⇒ ertl_st_move r1 r2 l
[1071]102  | ertl_st_opaccs_a opaccs d s1 s2 _ ⇒ ertl_st_opaccs_a opaccs d s1 s2 l
103  | ertl_st_opaccs_b opaccs d s1 s2 _ ⇒ ertl_st_opaccs_b opaccs d s1 s2 l
[759]104  | ertl_st_op1 op1 d s1 _ ⇒ ertl_st_op1 op1 d s1 l
105  | ertl_st_op2 op2 d s1 s2 _ ⇒ ertl_st_op2 op2 d s1 s2 l
106  | ertl_st_clear_carry _ ⇒ ertl_st_clear_carry l
[1071]107  | ertl_st_set_carry _ ⇒ ertl_st_set_carry l
[759]108  | ertl_st_load d a1 a2 _ ⇒ ertl_st_load d a1 a2 l
109  | ertl_st_store a1 a2 s _ ⇒ ertl_st_store a1 a2 s l
110  | ertl_st_call_id f args _ ⇒ ertl_st_call_id f args l
[1071]111  | ertl_st_cond a i1 i2 ⇒ ertl_st_cond a i1 i2
112  | ertl_st_return ⇒ ertl_st_return
[759]113  ].
114 
[1071]115let rec adds_graph
116  (stmt_list: list ertl_statement) (start_lbl: label)
117  (dest_lbl: label) (def: ertl_internal_function)
118    on stmt_list ≝
[759]119  match stmt_list with
[1071]120  [ nil ⇒ add_graph start_lbl (ertl_st_skip dest_lbl) def
121  | cons stmt stmt_list ⇒
122    match stmt_list with
123    [ nil ⇒ add_graph start_lbl (change_label dest_lbl stmt) def
[759]124    | _ ⇒
[1071]125      let 〈tmp_lbl, nuniv〉 ≝ fresh_label def in
126      let stmt ≝ change_label tmp_lbl stmt in
127      let def ≝ add_graph start_lbl stmt def in
128        adds_graph stmt_list tmp_lbl dest_lbl def
[759]129    ]
130  ].
131
[1071]132let rec add_translates
133  (translate_list: list ?) (start_lbl: label) (dest_lbl: label)
134  (def: ertl_internal_function)
135    on translate_list ≝
[759]136  match translate_list with
[1071]137  [ nil ⇒ add_graph start_lbl (ertl_st_skip dest_lbl) def
138  | cons trans translate_list ⇒
139    match translate_list with
140    [ nil ⇒ trans start_lbl dest_lbl def
[759]141    | _ ⇒
[1071]142      let 〈tmp_lbl, nuniv〉 ≝ fresh_label def in
143      let def ≝ trans start_lbl tmp_lbl def in
144        add_translates translate_list tmp_lbl dest_lbl def
[759]145    ]
146  ].
[763]147
[1071]148axiom register_fresh: universe RegisterTag → register.
149
150definition fresh_reg: ertl_internal_function → ertl_internal_function × register ≝
151  λdef.
152    let r ≝ register_fresh (ertl_if_runiverse def) in
153    let locals ≝ r :: ertl_if_locals def in
154    let ertl_if_luniverse' ≝ ertl_if_luniverse def in
155    let ertl_if_runiverse' ≝ ertl_if_runiverse def in
156    let ertl_if_params' ≝ ertl_if_params def in
157    let ertl_if_locals' ≝ locals in
158    let ertl_if_stacksize' ≝ ertl_if_stacksize def in
159    let ertl_if_graph' ≝ ertl_if_graph def in
160    let ertl_if_entry' ≝ ertl_if_entry def in
161    let ertl_if_exit' ≝ ertl_if_exit def in
162      〈mk_ertl_internal_function
163        ertl_if_luniverse' ertl_if_runiverse' ertl_if_params'
164        ertl_if_locals' ertl_if_stacksize' ertl_if_graph'
165        ertl_if_entry' ertl_if_exit', r〉.
166
167let rec fresh_regs
168  (def: ertl_internal_function) (n: nat)
169    on n ≝
[763]170  match n with
171  [ O ⇒ 〈def, [ ]〉
172  | S n' ⇒
173    let 〈def', regs'〉 ≝ fresh_regs def n' in
174    let 〈def', reg〉 ≝ fresh_reg def' in
175      〈def', reg :: regs'〉
176  ].
177 
[1075]178axiom fresh_regs_length:
179  ∀def: ertl_internal_function.
180  ∀n: nat.
181    |(\snd (fresh_regs def n))| = n.
182
183definition fresh_regs_strong: ? → ∀n: nat. Σregs: ertl_internal_function × (list register). |\snd regs| = n ≝
184  λdef: ertl_internal_function.
185  λn: nat.
186    fresh_regs def n.
187  @fresh_regs_length
188qed.
189 
[777]190definition save_hdws_internal ≝
[1107]191  λdestr_srcr: register × Register.
[777]192  λstart_lbl: label.
193    let 〈destr, srcr〉 ≝ destr_srcr in
194      adds_graph [ ertl_st_get_hdw destr srcr start_lbl ] start_lbl.
195 
[763]196definition save_hdws ≝
[777]197  λl.
198    map ? ? save_hdws_internal l.
199   
200definition restore_hdws_internal ≝
[1107]201  λdestr_srcr: Register × register.
[777]202  λstart_lbl: label.
203    let 〈destr, srcr〉 ≝ destr_srcr in
204    adds_graph [ ertl_st_set_hdw destr srcr start_lbl ] start_lbl.
205   
206definition swap_components ≝
207  λA, B: Type[0].
208  λp: A × B.
209  let 〈l, r〉 ≝ p in
210  〈r, l〉.
211   
212definition restore_hdws ≝
213  λl.
214    map ? ? restore_hdws_internal (map ? ? (swap_components ? ?) l).
215
216definition get_params_hdw_internal ≝
217  λstart_lbl: label.
218    adds_graph [ ertl_st_skip start_lbl ] start_lbl.
219
220definition get_params_hdw ≝
221  λparams: list register.
[1071]222  match params with
223  [ nil ⇒ [get_params_hdw_internal]
[777]224  | _ ⇒
[1071]225    let l ≝ zip_pottier ? ? params parameters in
226      save_hdws l
[777]227  ].
228
229definition get_param_stack ≝
230  λoff: nat.
231  λdestr.
232  λstart_lbl, dest_lbl: label.
233  λdef.
234  let 〈def, addr1〉 ≝ fresh_reg def in
235  let 〈def, addr2〉 ≝ fresh_reg def in
236  let 〈def, tmpr〉 ≝ fresh_reg def in
[1071]237  let 〈carry, int_offset〉 ≝ half_add ? (bitvector_of_nat ? off) int_size in
[777]238  adds_graph [
239    ertl_st_frame_size addr1 start_lbl;
240    ertl_st_int tmpr int_offset start_lbl;
241    ertl_st_op2 Sub addr1 addr1 tmpr start_lbl;
242    ertl_st_get_hdw tmpr RegisterSPL start_lbl;
243    ertl_st_op2 Add addr1 addr1 tmpr start_lbl;
244    ertl_st_int addr2 (bitvector_of_nat 8 0) start_lbl;
245    ertl_st_get_hdw tmpr RegisterSPH start_lbl;
246    ertl_st_op2 Addc addr2 addr2 tmpr start_lbl;
247    ertl_st_load destr addr1 addr2 start_lbl
248  ] start_lbl dest_lbl def.
249 
250definition get_params_stack ≝
251  λparams.
[1071]252  match params with
253  [ nil ⇒ [ λstart_lbl. adds_graph [ertl_st_skip start_lbl] start_lbl ]
254  | _ ⇒
255    let f ≝ λi. λr. get_param_stack i r in
256      mapi ? ? f params
[777]257  ].
258
259definition get_params ≝
260  λparams.
261  let n ≝ min (length ? params) (length ? parameters) in
262  let 〈hdw_params, stack_params〉 ≝ list_split ? n params in
[1071]263  let hdw_params ≝ get_params_hdw hdw_params in
264    hdw_params @ (get_params_stack stack_params).
[777]265 
266definition add_prologue ≝
[1071]267  λparams: list register.
[777]268  λsral.
269  λsrah.
270  λsregs.
271  λdef.
272  let start_lbl ≝ ertl_if_entry def in
[1071]273  let 〈tmp_lbl, nuniv〉 ≝ fresh_label def in
[1075]274  match lookup ? ? (ertl_if_graph def) start_lbl
275    return λx. lookup ? ? (ertl_if_graph def) start_lbl ≠ None ? → ertl_internal_function with
276  [ None ⇒ λnone_absrd. ?
277  | Some last_stmt ⇒ λsome_prf.
[777]278    let def ≝
[1071]279      add_translates
280         ((adds_graph [
281                     ertl_st_new_frame start_lbl
282                   ]) ::
283         (adds_graph [
284                      ertl_st_pop sral start_lbl;
285                      ertl_st_pop srah start_lbl
286                   ]) ::
287         (save_hdws sregs) @
288         (get_params params))
289        start_lbl tmp_lbl def
290    in
291      add_graph tmp_lbl last_stmt def
[1075]292  ] ?.
293  cases not_implemented (* dep. types here *)
[1071]294qed.
295
296definition save_return ≝
297  λret_regs.
298  λstart_lbl: label.
299  λdest_lbl: label.
300  λdef: ertl_internal_function.
301  let 〈def, tmpr〉 ≝ fresh_reg def in
[1075]302  match reduce_strong ? ? RegisterSTS ret_regs with
[1071]303  [ dp crl crl_proof ⇒
304    let commonl ≝ \fst (\fst crl) in
305    let commonr ≝ \fst (\snd crl) in
306    let restl ≝ \snd (\fst crl) in
307    let restr ≝ \snd (\snd crl) in
308    let init_tmpr ≝ ertl_st_int tmpr (zero ?) start_lbl in
309    let f_save ≝ λst. λr. ertl_st_set_hdw st r start_lbl in
[1075]310    let saves ≝ map2 ? ? ? f_save commonl commonr crl_proof in
[1071]311    let f_default ≝ λst. ertl_st_set_hdw st tmpr start_lbl in
312    let defaults ≝ map ? ? f_default restl in
313      adds_graph (init_tmpr :: saves @ defaults) start_lbl dest_lbl def
314  ].
315
[1075]316definition assign_result ≝
317  λstart_lbl: label.
318  match reduce_strong ? ? RegisterRets RegisterSTS with
319  [ dp crl crl_proof ⇒
320    let commonl ≝ \fst (\fst crl) in
321    let commonr ≝ \fst (\snd crl) in
322    let f ≝ λret. λst. ertl_st_hdw_to_hdw ret st start_lbl in
323    let insts ≝ map2 ? ? ? f commonl commonr crl_proof in
324      adds_graph insts start_lbl
325  ].
326
327definition add_epilogue ≝
328  λret_regs.
329  λsral.
330  λsrah.
331  λsregs.
332  λdef.
333  let start_lbl ≝ ertl_if_exit def in
334  let 〈tmp_lbl, nuniv〉 ≝ fresh_label def in
335  match lookup ? ? (ertl_if_graph def) start_lbl
336    return λx. lookup ? ? (ertl_if_graph def) start_lbl ≠ None ? → ? with
337  [ None ⇒ λnone_absd. ?
338  | Some last_stmt ⇒ λsome_prf.
339    let def ≝
340      add_translates (
341        [save_return ret_regs] @
342        restore_hdws sregs @
343        [adds_graph [
344          ertl_st_push srah start_lbl;
345          ertl_st_push sral start_lbl
346        ]] @
347        [adds_graph [
348          ertl_st_del_frame start_lbl
349        ]] @
350        [assign_result]
351      ) start_lbl tmp_lbl def
352    in
353    let def ≝ add_graph tmp_lbl last_stmt def in
[1077]354      change_exit_label tmp_lbl def ?
[1075]355  ] ?.
[1077]356  cases not_implemented (* dep types here, bug in matita too! *)
[1075]357qed.
[777]358 
359definition allocate_regs_internal ≝
[1075]360  λr: Register.
[777]361  λdef_sregs.
362  let 〈def, sregs〉 ≝ def_sregs in
363  let 〈def, r'〉 ≝ fresh_reg def in
364    〈def, 〈r', r〉 :: sregs〉.
365 
366definition allocate_regs ≝
[1075]367  λrs.
[777]368  λsaved: rs_set rs.
369  λdef.
370    rs_fold ? ? allocate_regs_internal saved 〈def, [ ]〉.
[1075]371   
372definition add_pro_and_epilogue ≝
373  λparams.
374  λret_regs.
375  λdef.
376  match fresh_regs_strong def 2 with
377  [ dp def_sra def_sra_proof ⇒
378    let def ≝ \fst def_sra in
379    let sra ≝ \snd def_sra in
380    let sral ≝ nth_safe ? 0 sra ? in
381    let srah ≝ nth_safe ? 1 sra ? in
382    let 〈def, sregs〉 ≝ allocate_regs register_list_set RegisterCalleeSaved def in
383    let def ≝ add_prologue params sral srah sregs def in
384    let def ≝ add_epilogue ret_regs sral srah sregs def in
385      def
386  ].
387  [1: >def_sra_proof //
388  |2: >def_sra_proof //
389  ]
390qed.
391
392definition set_params_hdw ≝
393  λparams.
394  match params with
395  [ nil ⇒ [ λstart_lbl. adds_graph [ertl_st_skip start_lbl] start_lbl]
396  | _ ⇒
397    let l ≝ zip_pottier ? ? params parameters in
398      restore_hdws l
399  ].
400
401definition set_param_stack ≝
402  λoff.
403  λsrcr.
404  λstart_lbl: label.
405  λdest_lbl: label.
406  λdef: ertl_internal_function.
407  let 〈def, addr1〉 ≝ fresh_reg def in
408  let 〈def, addr2〉 ≝ fresh_reg def in
409  let 〈def, tmpr〉 ≝ fresh_reg def in
410  let 〈ignore, int_off〉 ≝ half_add ? off int_size in
411    adds_graph [
412      ertl_st_int addr1 int_off start_lbl;
413      ertl_st_get_hdw tmpr RegisterSPL start_lbl;
414      ertl_st_clear_carry start_lbl;
415      ertl_st_op2 Sub addr1 tmpr addr1 start_lbl;
416      ertl_st_get_hdw tmpr RegisterSPH start_lbl;
417      ertl_st_int addr2 (zero ?) start_lbl;
418      ertl_st_op2 Sub addr2 tmpr addr2 start_lbl;
419      ertl_st_store addr1 addr2 srcr start_lbl
420    ] start_lbl dest_lbl def.   
421
422definition set_params_stack ≝
423  λparams.
424  match params with
425  [ nil ⇒ [ λstart_lbl. adds_graph [ertl_st_skip start_lbl] start_lbl]
426  | _ ⇒
427    let f ≝ λi. λr. set_param_stack (bitvector_of_nat ? i) r in
428      mapi ? ? f params
429  ].
430
431axiom min_fst:
432  ∀m, n: nat.
433    min m n ≤ m.
434
435definition set_params ≝
436  λparams.
437  let n ≝ min (|params|) (|parameters|) in
438  let hdw_stack_params ≝ split ? params n ? in
439  let hdw_params ≝ \fst hdw_stack_params in
440  let stack_params ≝ \snd hdw_stack_params in
441    set_params_hdw hdw_params @ set_params_stack stack_params.
442  @min_fst
443qed.
444
445definition fetch_result ≝
446  λret_regs.
447  λstart_lbl: label.
448  match reduce_strong ? ? RegisterSTS RegisterRets with
449  [ dp crl first_crl_proof ⇒
450    let commonl ≝ \fst (\fst crl) in
451    let commonr ≝ \fst (\snd crl) in
452    let f_save ≝ λst. λret. ertl_st_hdw_to_hdw st ret start_lbl in
453    let saves ≝ map2 ? ? ? f_save commonl commonr ? in
454    match reduce_strong ? ? ret_regs RegisterSTS with
455    [ dp crl second_crl_proof ⇒
456      let commonl ≝ \fst (\fst crl) in
457      let commonr ≝ \fst (\snd crl) in
458      let f_restore ≝ λr. λst. ertl_st_get_hdw r st start_lbl in
459      let restores ≝ map2 ? ? ? f_restore commonl commonr ? in
460        adds_graph (saves @ restores) start_lbl
461    ]
462  ].
463  [ normalize nodelta; @second_crl_proof
464  | @first_crl_proof
465  ]
466qed.
467
468definition translate_call_id ≝
469  λf.
470  λargs.
471  λret_regs.
472  λstart_lbl.
473  λdest_lbl.
474  λdef.
475  let nb_args ≝ |args| in
476    add_translates (
477      set_params args @ [
478      adds_graph [ ertl_st_call_id f (bitvector_of_nat ? nb_args) start_lbl ];
479      fetch_result ret_regs
480      ]
481    ) start_lbl dest_lbl def.
482
483definition translate_stmt ≝
484  λlbl.
485  λstmt.
486  λdef.
487  match stmt with
488  [ rtl_st_skip lbl' ⇒ add_graph lbl (ertl_st_skip lbl') def
489  | rtl_st_cost cost_lbl lbl' ⇒ add_graph lbl (ertl_st_cost cost_lbl lbl') def
[1131]490  | rtl_st_addr r1 r2 x lbl' ⇒ add_graph lbl (ertl_st_addr r1 r2 x lbl') def
[1075]491  | rtl_st_stack_addr r1 r2 lbl' ⇒
492    adds_graph [
493      ertl_st_get_hdw r1 RegisterSPL lbl;
494      ertl_st_get_hdw r2 RegisterSPH lbl
495    ] lbl lbl' def
[1077]496  | rtl_st_int r i lbl' ⇒  add_graph lbl (ertl_st_int r i lbl') def
[1075]497  | rtl_st_move r1 r2 lbl' ⇒ add_graph lbl (ertl_st_move r1 r2 lbl') def
498  | rtl_st_opaccs op destr1 destr2 srcr1 srcr2 lbl' ⇒
499    adds_graph [
500      ertl_st_opaccs_a op destr1 srcr1 srcr2 lbl;
501      ertl_st_opaccs_b op destr2 srcr1 srcr2 lbl
502      ] lbl lbl' def
503  | rtl_st_op1 op1 destr srcr lbl' ⇒
504    add_graph lbl (ertl_st_op1 op1 destr srcr lbl') def
505  | rtl_st_op2 op2 destr srcr1 srcr2 lbl' ⇒
506    add_graph lbl (ertl_st_op2 op2 destr srcr1 srcr2 lbl') def
507  | rtl_st_clear_carry lbl' ⇒
508    add_graph lbl (ertl_st_clear_carry lbl') def
509  | rtl_st_set_carry lbl' ⇒
510    add_graph lbl (ertl_st_set_carry lbl') def
511  | rtl_st_load destr addr1 addr2 lbl' ⇒
512    add_graph lbl (ertl_st_load destr addr1 addr2 lbl') def
513  | rtl_st_store addr1 addr2 srcr lbl' ⇒
514    add_graph lbl (ertl_st_store addr1 addr2 srcr lbl') def
515  | rtl_st_call_id f args ret_regs lbl' ⇒
516    translate_call_id f args ret_regs lbl lbl' def
517  | rtl_st_cond srcr lbl_true lbl_false ⇒
518    add_graph lbl (ertl_st_cond srcr lbl_true lbl_false) def
519  | rtl_st_return ⇒
520    add_graph lbl ertl_st_return def
521  | _ ⇒ ? (* assert false: not implemented or should not happen *)
522  ].
523  cases not_implemented
524qed.   
525
[1079]526(* hack with empty graphs used here *)
[1076]527definition translate_funct_internal ≝
528  λdef.
529  let nb_params ≝ |rtl_if_params def| in
530  let added_stacksize ≝ max 0 (nb_params - |parameters|) in
531  let new_locals ≝ nub_by ? (eq_identifier ?) ((rtl_if_locals def) @ (rtl_if_params def)) in
[1077]532  let entry' ≝ rtl_if_entry def in
533  let exit' ≝ rtl_if_exit def in
[1079]534  let graph' ≝ add ? ? (empty_map ? ?) entry' (ertl_st_skip entry') in
535  let graph' ≝ add ? ? graph' exit' (ertl_st_skip exit') in
[1076]536  let def' ≝
537    mk_ertl_internal_function
538      (rtl_if_luniverse def) (rtl_if_runiverse def)
539      nb_params new_locals ((rtl_if_stacksize def) + added_stacksize)
[1079]540      graph' ? ? in
[1077]541  let def' ≝ foldi ? ? ? translate_stmt (rtl_if_graph def) def' in
[1076]542  let def' ≝ add_pro_and_epilogue (rtl_if_params def) (rtl_if_result def) def' in
543    def'.
[1079]544  [1: %
545      [1: @entry'
546      |2: normalize nodelta
547          @graph_add_lookup
548          @graph_add
549      ]
550  |2: %
551      [1: @exit'
552      |2: normalize nodelta
553          @graph_add
554      ]
555  ]
556qed.
[777]557   
[1076]558definition translate_funct ≝
559  λid_def: ident × ?.
560  let 〈id, def〉 ≝ id_def in
561  let def' ≝
562    match def with
[1077]563    [ Internal def ⇒ Internal ? (translate_funct_internal def)
564    | External def ⇒ External ? def
[1076]565    ] in
566  〈id, def'〉.
[1077]567
568definition generate ≝
569  λstmt.
570  λdef.
571  let 〈entry, nuniv〉 ≝ fresh_label def in
572  let graph ≝ add ? ? (ertl_if_graph def) entry stmt in
573    mk_ertl_internal_function
574      nuniv (ertl_if_runiverse def) (ertl_if_params def)
575      (ertl_if_locals def) (ertl_if_stacksize def) graph
[1079]576      ? ?.
577  [1: %
578    [1: @entry
579    |2: normalize nodelta;
580        @graph_add
581    ]
582  |2: generalize in match (ertl_if_exit def)
583      #HYP
584      cases HYP
585      #LBL #LBL_PRF
586      %
587      [1: @LBL
588      |2: normalize nodelta;
589          @graph_add_lookup
590          @LBL_PRF
591      ]
592  ]
593qed.
[1075]594   
[1079]595let rec find_and_remove_first_cost_label_internal
596  (def: ertl_internal_function) (lbl: label) (num_nodes: nat)
597    on num_nodes ≝
598  match num_nodes with
599  [ O ⇒ 〈None ?, def〉
600  | S num_nodes' ⇒
601    match lookup ? ? (ertl_if_graph def) lbl with
602    [ None ⇒ 〈None ?, def〉
603    | Some stmt ⇒
604      match stmt with
605      [ ertl_st_cost cost_lbl next_lbl ⇒
606          〈Some ? cost_lbl, add_graph lbl (ertl_st_skip next_lbl) def〉
607      | ertl_st_cond _ _ _ ⇒ 〈None ?, def〉
608      | ertl_st_return ⇒ 〈None ?, def〉
609      | ertl_st_skip lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
610      | ertl_st_comment _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
611      | ertl_st_get_hdw _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
612      | ertl_st_set_hdw _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
613      | ertl_st_hdw_to_hdw _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
614      | ertl_st_pop _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
615      | ertl_st_push _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
[1131]616      | ertl_st_addr _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
[1079]617      | ertl_st_int _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
618      | ertl_st_move _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
619      | ertl_st_opaccs_a _ _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
620      | ertl_st_opaccs_b _ _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
621      | ertl_st_op1 _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
622      | ertl_st_op2 _ _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
623      | ertl_st_clear_carry lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
624      | ertl_st_set_carry lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
625      | ertl_st_load _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
626      | ertl_st_store _ _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
627      | ertl_st_call_id _ _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
628      | ertl_st_new_frame lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
629      | ertl_st_del_frame lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
630      | ertl_st_frame_size _ lbl ⇒ find_and_remove_first_cost_label_internal def lbl num_nodes'
631      ]
632    ]
633  ].
634   
635definition find_and_remove_first_cost_label ≝
636  λdef. 
637    find_and_remove_first_cost_label_internal def (ertl_if_entry def) (graph_num_nodes ? (ertl_if_graph def)).
[1077]638
[1079]639definition move_first_cost_label_up_internal ≝
640  λdef.
641  let 〈cost_label, def〉 ≝ find_and_remove_first_cost_label def in
642  match cost_label with
643  [ None ⇒ def
644  | Some cost_label ⇒ generate (ertl_st_cost cost_label (ertl_if_entry def)) def
645  ].
[1077]646
[1079]647definition move_first_cost_label_up ≝
648  λA: Type[0].
649  λid_def: A × ?.
650  let 〈id, def〉 ≝ id_def in
651  let def' ≝
652    match def with
653    [ Internal int_fun ⇒ Internal ? (move_first_cost_label_up_internal int_fun)
654    | External ext ⇒ def
655    ]
656  in
657    〈id, def'〉.
658
659definition translate ≝
660  λp.
661  let p ≝ tailcall_simplify p in (* tailcall simplification here *)
662  let f ≝ λfunct. move_first_cost_label_up ? (translate_funct funct) in
663  let vars ≝ map ? ? f (rtl_pr_functs p) in
[1081]664    mk_ertl_program (rtl_pr_vars p) vars (rtl_pr_main p).
Note: See TracBrowser for help on using the repository browser.