source: src/RTL/RTLtoERTL.ma @ 1254

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

More progress towards porting of RTLtoERTL to joint syntax.

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