source: src/joint/TranslateUtils.ma @ 2886

Last change on this file since 2886 was 2855, checked in by piccolo, 7 years ago

little bug fixed in TranslateUtils?.

File size: 25.0 KB
Line 
1include "joint/Joint.ma".
2include "joint/blocks.ma".
3include "utilities/hide.ma".
4include "utilities/deqsets_extra.ma".
5
6(*include alias "basics/lists/list.ma".
7let rec repeat_fresh pars globals A (fresh : freshT pars globals A) (n : ℕ)
8  on n :
9  freshT pars globals (Σl : list A. |l| = n) ≝
10  match n  return λx.freshT … (Σl.|l| = x) with
11  [ O ⇒ return «[ ], ?»
12  | S n' ⇒
13    ! regs' ← repeat_fresh … fresh n';
14    ! reg ← fresh ;
15    return «reg::regs',?»
16  ]. [% | @hide_prf cases regs' #l #EQ normalize >EQ % ] qed.*)
17
18definition fresh_label:
19 ∀g_pars,globals.state_monad (joint_internal_function g_pars globals) label ≝
20  λg_pars,globals,def.
21    let 〈r,luniverse〉 ≝ fresh … (joint_if_luniverse … def) in
22     〈set_luniverse … def luniverse, r〉.
23
24definition fresh_register:
25 ∀g_pars,globals.state_monad (joint_internal_function g_pars globals) register ≝
26  λg_pars,globals,def.
27    let 〈r,runiverse〉 ≝ fresh … (joint_if_runiverse … def) in
28     〈set_runiverse … def runiverse, r〉.
29
30(* insert into a graph a list of instructions *)
31let rec adds_graph_pre
32  X
33  (g_pars: graph_params)
34  (globals: list ident)
35  (* for ERTLptr: the label parameter is filled by the last label *)
36  (pre_process : label → X → joint_seq g_pars globals)
37  (insts: list X)
38  (src : label) on insts :
39  state_monad (joint_internal_function g_pars globals) label ≝
40  match insts with
41  [ nil ⇒ return src
42  | cons i rest ⇒
43    ! mid ← fresh_label … ;
44    ! dst ← adds_graph_pre … pre_process rest mid ;
45    !_ state_update … (add_graph … src (sequential … (pre_process dst i) mid)) ;
46    return dst
47  ].
48
49let rec adds_graph_post
50  (g_pars: graph_params)
51  (globals: list ident)
52  (insts: list (joint_seq g_pars globals))
53  (dst : label) on insts :
54  state_monad (joint_internal_function g_pars globals) label ≝
55  match insts with
56  [ nil ⇒ return dst
57  | cons i rest ⇒
58    ! src ← fresh_label … ;
59    ! mid ← adds_graph_post … rest dst ;
60    !_ state_update … (add_graph … src (sequential … i mid)) ;
61    return src
62  ].
63
64definition adds_graph :
65  ∀g_pars : graph_params.
66  ∀globals: list ident.
67  ∀b : step_block g_pars globals.
68  label → label →
69  joint_internal_function g_pars globals → joint_internal_function g_pars globals ≝
70  λg_pars,globals,insts,src,dst,def.
71  let pref ≝ \fst (\fst insts) in
72  let op ≝ \snd (\fst insts) in
73  let post ≝ \snd insts in
74  let 〈def, mid〉 ≝ adds_graph_pre … (λlbl,inst.inst lbl) pref src def in
75  let 〈def, mid'〉 ≝ adds_graph_post … post dst def in
76  add_graph … mid (sequential … (op mid) mid') def.
77
78definition fin_adds_graph :
79  ∀g_pars : graph_params.
80  ∀globals: list ident.
81  ∀b : fin_block g_pars globals.
82  label →
83  joint_internal_function g_pars globals → joint_internal_function g_pars globals ≝
84  λg_pars,globals,insts,src,def.
85  let pref ≝ \fst insts in
86  let last ≝ \snd insts in
87  let 〈def, mid〉 ≝ adds_graph_pre … (λ_.λi.i) pref src def in
88  add_graph … mid (final … last) def.
89
90(* ignoring register allocation for now *)
91
92(*
93definition luniverse_ok : ∀p : graph_params.∀g.joint_internal_function p g → Prop ≝
94λp,g,def.fresh_map_for_univ … (joint_if_code … def) (joint_if_luniverse … def).
95*)
96(*
97lemma All_fresh_not_memb : ∀tag,u,l,id,u'.
98  All (identifier tag) (λid'.¬fresh_for_univ tag id' u) l →
99  〈id, u'〉 = fresh tag u →
100  ¬id ∈ l.
101#tag #u #l elim l [2: #hd #tl #IH] #id #u' *
102[ #hd_fresh #tl_fresh #EQfresh
103  whd in ⊢ (?(?%));
104  change with (eq_identifier ???) in match (?==?);
105  >eq_identifier_sym
106  >(eq_identifier_false … (fresh_distinct … hd_fresh EQfresh))
107  normalize nodelta @(IH … tl_fresh EQfresh)
108| #_ %
109]
110qed.
111
112
113lemma fresh_was_fresh : ∀tag,id,id',u,u'.
114〈id,u'〉 = fresh tag u →
115fresh_for_univ tag id' u' →
116id' ≠ id →
117fresh_for_univ tag id' u.
118#tag * #id * #id' * #u * #u'
119normalize #EQfresh destruct
120#H #NEQ
121elim (le_to_or_lt_eq … H)
122[ (* not recompiling... /2 by monotonic_pred/ *) /2/ ]
123#H >(succ_injective … H) in NEQ;
124* #G elim (G (refl …))
125qed.
126
127lemma fresh_not_in_univ : ∀tag,id,u,u'.
128〈id, u'〉 = fresh tag u →
129¬fresh_for_univ tag id u.
130#tag * #id * #u * #u' normalize #EQ destruct //
131qed.
132*)
133(*
134lemma adds_graph_list_fresh_preserve :
135  ∀g_pars,globals,b,src,dst,def.
136  let def' ≝ adds_graph_list g_pars globals b src dst def in
137  ∀lbl.fresh_for_univ … lbl (joint_if_luniverse … def) →
138       fresh_for_univ … lbl (joint_if_luniverse … def').
139#g_pars #globals #l elim l -l
140[ #src #dst #def whd #lbl #H @H ]
141#hd1 * [ #_ #src #dst #def whd #lbl #H @H ] #hd2 #tl #IH #src #dst #def whd #lbl #H
142whd in match (adds_graph_list ??????);
143whd in match fresh_label; normalize nodelta
144inversion (fresh ??) #mid #luniv' #EQfresh lapply (sym_eq ??? EQfresh) -EQfresh #EQfresh
145normalize nodelta
146@IH whd in match (joint_if_luniverse ???);
147@(fresh_remains_fresh … EQfresh) @H
148qed.
149
150lemma with_last_not_empty : ∀X,pref,last.not_empty X (pref @ [last]).
151#X * [2: #hd #tl ] #last % qed.
152
153lemma split_on_last_ne_elim : ∀X,l.∀P : ((list X) × X) → Prop.
154(∀pref,last.pi1 ?? l = pref @ [last] → P 〈pref, last〉) →
155P (split_on_last_ne X l).
156#X * @list_elim_left [ * ] #last #pref #_ #prf #P #H
157>split_on_last_ne_def @H % qed.
158
159(* use Russell? *)
160lemma adds_graph_list_ok :
161  ∀g_pars,globals,b,src,dst,def.
162  fresh_for_univ … src (joint_if_luniverse … def) →
163  luniverse_ok ?? def →
164  let def' ≝ adds_graph_list g_pars globals b src dst def in
165  luniverse_ok ?? def' ∧
166  (∀lbl.lbl ≠ src → fresh_for_univ … lbl (joint_if_luniverse … def) →
167                      stmt_at … (joint_if_code … def') lbl =
168                      stmt_at … (joint_if_code … def) lbl) ∧
169  let B ≝ ensure_step_block … b in
170  ∃l.bool_to_Prop (uniqueb … l) ∧
171    All … (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def) ∧
172                 fresh_for_univ … lbl (joint_if_luniverse … def')) l ∧
173    src ~❨B,l❩~> dst in joint_if_code … def'.
174#p #g #l elim l -l [2: #hd1 * [ #_ | #hd2 #tl #IH ]]
175#src #dst #def #Hsrc #Hdef
176[1,3: %
177  [1,3: %
178    [1,3: #lbl @(eq_identifier_elim … lbl src) #H destruct [1,3: #_ @Hsrc ]
179      whd in ⊢ (%→?); whd in match (adds_graph_list ??????);
180      >(lookup_add_miss ?????? H) @Hdef
181    |*: #lbl #H #G @lookup_add_miss @H
182    ]
183  |*: %{[]} % [1,3: %% ] %{src} % [1,3:%] %{dst} % [1,3: @lookup_add_hit ] %
184  ]
185]
186whd in match (adds_graph_list ??????);
187whd in match (fresh_label ???);
188inversion (fresh ??) normalize nodelta
189#mid #luniverse' #EQfresh lapply (sym_eq ??? EQfresh) -EQfresh #EQfresh
190letin def' ≝ (add_graph p g src (sequential … hd1 mid) (set_luniverse … def luniverse'))
191lapply (IH mid dst def' ??)
192[ #lbl @(eq_identifier_elim … lbl src) #H destruct
193  [2: whd in ⊢ (%→?); whd in match (adds_graph_list ??????);
194    >(lookup_add_miss ?????? H) ]
195  #Hpres @(fresh_remains_fresh … EQfresh) [ @Hdef ] assumption
196| whd in match def';
197  @(fresh_is_fresh … EQfresh)
198]
199whd in match (joint_if_luniverse ???);
200whd in match (joint_if_code ???);
201** #Hdef'' #stmt_preserved * #l ** #Hl1 #Hl2
202whd in ⊢ (%→?); @split_on_last_ne_elim #pref #last #EQ * #mid' * #Hl3 #Hl4
203%
204[ %{Hdef''} #lbl #NEQ
205  @(eq_identifier_elim ?? lbl mid)
206  [ #EQ destruct #ABS cases (absurd ? ABS ?) @(fresh_not_in_univ … EQfresh)
207  | #NEQ' #H >(stmt_preserved … NEQ')
208    [ whd in match (joint_if_code ???);
209      whd in match (stmt_at ????); >lookup_add_miss [2: @NEQ ] %
210    | @(fresh_remains_fresh … EQfresh) @H
211    ]
212  ]
213]
214%{(mid::l)}
215% [ % ]
216[ whd in ⊢ (?%);
217  cut (Not (bool_to_Prop (mid∈l)))
218  [ % #H elim (All_memb … Hl2 H)
219    whd in match (joint_if_luniverse ???);
220    #G #_ @(absurd ?? G)
221    @ (fresh_is_fresh … EQfresh)
222  | #H >H assumption
223  ]
224| %
225  [ %{(fresh_not_in_univ … EQfresh)}
226    @adds_graph_list_fresh_preserve @(fresh_is_fresh … EQfresh)
227  | @(All_mp … Hl2) #lbl * * #H1 #H2 %{H2} % #H3 @H1
228    @(fresh_remains_fresh … EQfresh) assumption
229  ]
230| whd in match (ensure_step_block ???) in EQ ⊢ %;
231  whd in match (map ??? (hd2 :: ?)); >EQ whd
232  change with ((?::?)@?) in match (?::?@?); >split_on_last_ne_def
233  %{mid'} % [2: @Hl4 ]
234  %{Hl3} %{mid} >stmt_preserved
235  [ % [2: % ] @lookup_add_hit
236  | @(fresh_remains_fresh … EQfresh) assumption
237  | % #ABS destruct @(absurd ? Hsrc) @(fresh_not_in_univ … EQfresh)
238  ]
239]
240qed.
241*)
242(*
243axiom adds_graph_ok :
244  ∀g_pars,globals,B,src,dst,def.
245  fresh_for_univ … src (joint_if_luniverse … def) →
246  luniverse_ok ?? def →
247  let def' ≝ adds_graph g_pars globals B src dst def in
248  luniverse_ok ?? def' ∧
249  (∀lbl.lbl ≠ src → fresh_for_univ … lbl (joint_if_luniverse … def) →
250                      stmt_at … (joint_if_code … def') lbl =
251                      stmt_at … (joint_if_code … def) lbl) ∧
252  ∃l.bool_to_Prop (uniqueb … l) ∧
253    All … (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def) ∧
254                 fresh_for_univ … lbl (joint_if_luniverse … def')) l ∧
255    src ~❨B,src::l❩~> dst in joint_if_code … def'.
256 
257axiom fin_adds_graph_ok :
258  ∀g_pars,globals,B,src,def.
259  fresh_for_univ … src (joint_if_luniverse … def) →
260  luniverse_ok ?? def →
261  let def' ≝ fin_adds_graph g_pars globals B src def in
262  luniverse_ok ?? def' ∧
263  (∀lbl.lbl ≠ src → fresh_for_univ … lbl (joint_if_luniverse … def) →
264                      stmt_at … (joint_if_code … def') lbl =
265                      stmt_at … (joint_if_code … def) lbl) ∧
266  ∃l.bool_to_Prop (uniqueb … l) ∧
267    All … (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def) ∧
268                 fresh_for_univ … lbl (joint_if_luniverse … def')) l ∧
269    src ~❨B,src::l❩~> it in joint_if_code … def'.
270*)
271
272definition append_seq_list :
273∀p,g.bind_step_block p g → bind_new register (list (joint_seq p g)) →
274  bind_step_block p g ≝
275λp,g,bbl,bl.
276! 〈pref, op, post〉 ← bbl ; ! l ← bl ; return 〈pref, op, post @ l〉.
277
278(*
279definition insert_epilogue ≝
280  λg_pars:graph_params.λglobals.λinsts : list (joint_seq g_pars globals).
281  λdef : joint_internal_function g_pars globals.
282  let exit ≝ joint_if_exit … def in
283  match stmt_at … exit
284  return λx.match x with [None ⇒ false | Some _ ⇒ true] → ?
285  with
286  [ Some s ⇒ λ_.
287    let 〈def', tmp〉 as prf ≝ adds_graph_list ?? insts exit def in
288    let def'' ≝ add_graph … tmp s def' in
289    set_joint_code … def'' (joint_if_code … def'') (joint_if_entry … def'') tmp
290  | None ⇒ Ⓧ
291  ] (pi2 … exit).
292whd in match def''; >graph_code_has_point //
293qed.
294*)
295
296definition b_adds_graph :
297  ∀g_pars: graph_params.
298  ∀globals: list ident.
299  ∀b : bind_step_block g_pars globals.
300  label → label →
301  joint_internal_function g_pars globals→
302  joint_internal_function g_pars globals ≝
303  λg_pars,globals,insts,src,dst,def.
304  let 〈def, stmts〉 ≝ bcompile ??? (fresh_register …) insts def in
305  adds_graph … stmts src dst def.
306
307(*
308axiom b_adds_graph_ok :
309  ∀g_pars,globals,B,src,dst,def.
310  fresh_for_univ … src (joint_if_luniverse … def) →
311  luniverse_ok ?? def →
312  let def' ≝ b_adds_graph g_pars globals B src dst def in
313  luniverse_ok ?? def' ∧
314  (∀lbl.lbl ≠ src → fresh_for_univ … lbl (joint_if_luniverse … def) →
315                      stmt_at … (joint_if_code … def') lbl =
316                      stmt_at … (joint_if_code … def) lbl) ∧
317  ∃l,r.
318    bool_to_Prop (uniqueb … l) ∧
319    bool_to_Prop (uniqueb … r) ∧
320    All … (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def) ∧
321                 fresh_for_univ … lbl (joint_if_luniverse … def')) l ∧
322    All … (λreg.¬fresh_for_univ … reg (joint_if_runiverse … def) ∧
323                 fresh_for_univ … reg (joint_if_runiverse … def')) r ∧
324    src ~❨B,src::l,r❩~> dst in joint_if_code … def'.
325*)
326definition b_fin_adds_graph :
327  ∀g_pars: graph_params.
328  ∀globals: list ident.
329  ∀b : bind_fin_block g_pars globals.
330  label →
331  joint_internal_function g_pars globals→
332  joint_internal_function g_pars globals ≝
333  λg_pars,globals,insts,src,def.
334  let 〈def, stmts〉 ≝ bcompile ??? (fresh_register …) insts def in
335  fin_adds_graph … stmts src def.
336
337(*
338axiom b_fin_adds_graph_ok :
339  ∀g_pars,globals,B,src,def.
340  fresh_for_univ … src (joint_if_luniverse … def) →
341  luniverse_ok ?? def →
342  let def' ≝ b_fin_adds_graph g_pars globals B src def in
343  luniverse_ok ?? def' ∧
344  (∀lbl.lbl ≠ src → fresh_for_univ … lbl (joint_if_luniverse … def) →
345                      stmt_at … (joint_if_code … def') lbl =
346                      stmt_at … (joint_if_code … def) lbl) ∧
347  ∃l,r.
348    bool_to_Prop (uniqueb … l) ∧
349    bool_to_Prop (uniqueb … r) ∧
350    All … (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def) ∧
351                 fresh_for_univ … lbl (joint_if_luniverse … def')) l ∧
352    All … (λreg.¬fresh_for_univ … reg (joint_if_runiverse … def) ∧
353                 fresh_for_univ … reg (joint_if_runiverse … def')) r ∧
354    src ~❨B,src::l,r❩~> it in joint_if_code … def'.
355*)
356
357lemma opt_All_intro : ∀X,P,o.
358(∀x.o = Some ? x → P x) → opt_All X P o. #X #P * [//] #x #H @H % qed.
359 
360(*
361definition points_of : ∀p,g.joint_internal_function p g → Type[0] ≝
362λp,g,def.Σl.bool_to_Prop (code_has_point … (joint_if_code … def) l).
363
364unification hint 0 ≔ p, g, def;
365points ≟ code_point p,
366code ≟ joint_if_code p g def,
367P ≟ λl : points.bool_to_Prop (code_has_point p g code l)
368
369points_of p g def ≡ Sig points P.
370
371definition stmt_at_safe : ∀p,g,def.points_of p g def → joint_statement p g ≝
372  λp,g,def,pt.opt_safe ? (stmt_at ?? (joint_if_code ?? def) (pi1 … pt)) ?.
373@hide_prf cases pt -pt #pt whd in ⊢ (?%→?); #H % #G >G in H; * qed.
374*)
375
376let rec bind_new_P' R X (P : list R → X → Prop) (m : bind_new R X) on m : Prop ≝
377match m with
378[ bret x ⇒ P [ ] x
379| bnew f ⇒
380  ∀r.bind_new_P' R X (λl.P (r::l)) (f r)
381].
382
383definition step_registers :  ∀p : uns_params.∀globals.
384  joint_step p globals → list register ≝
385λp,globals,s.get_used_registers_from_step … (functs … p) s.
386
387definition step_forall_registers : ∀p : uns_params.∀globals.
388  (register → Prop) → joint_step p globals → Prop ≝
389λp,globals,P,s.All … P (step_registers … s).
390
391definition fin_step_registers :  ∀p : uns_params.
392  joint_fin_step p → list register ≝
393λp,s.match s with [ TAILCALL _ _ r ⇒ f_call_args … (functs … p) r | _ ⇒ [ ] ].
394
395definition fin_step_forall_registers : ∀p : uns_params.
396  (register → Prop) → joint_fin_step p → Prop ≝
397λp,P,s.All … P (fin_step_registers … s).
398
399definition fin_step_forall_labels : ∀p : uns_params.
400  (label → Prop) → joint_fin_step p → Prop ≝
401λp,P,s.All … P (fin_step_labels … s).
402
403definition step_labels_and_registers_in : ∀p : uns_params.∀globals.
404  list label → list register → joint_step p globals → Prop ≝
405λp,g,allowed_l,allowed_r,s.
406  step_forall_labels … (In ? allowed_l) s ∧
407  step_forall_registers … (In ? allowed_r) s.
408
409definition fin_step_labels_and_registers_in : ∀p : uns_params.
410  list label → list register → joint_fin_step p → Prop ≝
411λp,allowed_l,allowed_r,s.
412  fin_step_forall_labels … (In ? allowed_l) s ∧
413  fin_step_forall_registers … (In ? allowed_r) s.
414
415record b_graph_translate_data
416  (src, dst : graph_params)
417  (globals : list ident) : Type[0] ≝
418{ init_ret : call_dest dst
419; init_params : paramsT dst
420; init_stack_size : ℕ
421; added_prologue : list (joint_seq dst globals)
422; new_regs : list register (* new registers added globally *)
423; f_step : label → joint_step src globals → bind_step_block dst globals
424; f_fin : label → joint_fin_step src → bind_fin_block dst globals
425; good_f_step :
426  ∀l,s.bind_new_P' ??
427    (λlocal_new_regs,block.let 〈pref, op, post〉 ≝ block in
428       ∀l.
429       let allowed_labels ≝ l :: step_labels … s in
430       let allowed_registers ≝ new_regs @ local_new_regs @ step_registers … s in
431       All (label → joint_seq ??)
432         (λs'.step_labels_and_registers_in … allowed_labels allowed_registers (step_seq dst globals (s' l)))
433         pref ∧
434       step_labels_and_registers_in … allowed_labels allowed_registers (op l) ∧
435       All (joint_seq ??) (step_labels_and_registers_in … allowed_labels allowed_registers) post)
436    (f_step l s)
437; good_f_fin :
438  ∀l,s.bind_new_P' ??
439    (λlocal_new_regs,block.let 〈pref, op〉 ≝ block in
440       let allowed_labels ≝ l :: fin_step_labels … s in
441       let allowed_registers ≝ new_regs @ local_new_regs @ fin_step_registers … s in
442       All (joint_seq ??) (λs.step_labels_and_registers_in … allowed_labels allowed_registers s) pref ∧
443       fin_step_labels_and_registers_in … allowed_labels allowed_registers op)
444    (f_fin l s)
445; f_step_on_cost :
446  ∀l,c.f_step l (COST_LABEL … c) =
447    bret ? (step_block ??) 〈[ ], λ_.COST_LABEL dst globals c, [ ]〉
448; cost_in_f_step :
449  ∀l,s,c.
450  bind_new_P ??
451    (λblock.∀l'.\snd (\fst block) l' = COST_LABEL dst globals c →
452       s = COST_LABEL … c) (f_step l s)
453}.
454
455definition bound_b_graph_translate_data ≝
456λsrc,dst,globals.
457Σd : bind_new register (b_graph_translate_data src dst globals).
458bind_new_P' ?? (λregs,data.new_regs ??? data = regs) d.
459
460unification hint 0 ≔ src,dst,globals ⊢
461bound_b_graph_translate_data src dst globals ≡
462Sig (bind_new register (b_graph_translate_data src dst globals)) (λd.bind_new_P' ?? (λregs,data.new_regs ??? data = regs) d).
463
464definition get_first_costlabel : ∀p,g.
465  joint_closed_internal_function p g → costlabel × (succ p) ≝
466  λp,g,def.
467  match stmt_at … (joint_if_code … def) (joint_if_entry … def)
468  return λx.stmt_at ???? = x → ? with
469  [ Some s ⇒
470    match s return λx.stmt_at ???? = Some ? x → ? with
471    [ sequential s' nxt ⇒
472      match s' return λx.stmt_at ???? = Some ? (sequential … x nxt) → ? with
473      [ COST_LABEL c ⇒ λ_.〈c, nxt〉
474      | _ ⇒ λabs.⊥
475      ]
476    | _ ⇒ λabs.⊥
477    ]
478  | _ ⇒ λabs.⊥
479  ] (refl …).
480@hide_prf
481cases def in abs; -def #def #good_def
482cases (entry_costed … good_def) #c * #nxt' #EQ >EQ #ABS destruct
483qed.
484
485definition partial_partition : ∀X.∀Y : DeqSet.(X → list Y) → Prop ≝
486λX,Y,f.
487(∀x.bool_to_Prop (uniqueb … (f x))) ∧
488(∀x1,x2,y.y ∈ f x1 → y ∈ f x2 → x1 = x2).
489
490record b_graph_translate_props
491  (src_g_pars, dst_g_pars : graph_params)
492  (globals: list ident)
493  (data : b_graph_translate_data src_g_pars dst_g_pars globals)
494  (def_in : joint_closed_internal_function src_g_pars globals)
495  (def_out : joint_closed_internal_function dst_g_pars globals)
496  (f_lbls : label → list label)
497  (f_regs : label → list register) : Prop ≝
498{ res_def_out_eq :
499           joint_if_result … def_out = init_ret … data
500; pars_def_out_eq :
501           joint_if_params … def_out = init_params … data
502; ss_def_out_eq :
503           joint_if_stacksize … def_out = init_stack_size … data
504; entry_eq : joint_if_entry … def_out = joint_if_entry … def_in
505; partition_lbls : partial_partition … f_lbls
506; partition_regs : partial_partition … f_regs
507; freshness_lbls :
508  (∀l.All …
509    (λlbl.¬fresh_for_univ … lbl (joint_if_luniverse … def_in) ∧
510           fresh_for_univ … lbl (joint_if_luniverse … def_out)) (f_lbls l))
511; freshness_regs :
512  (∀l.All …
513    (λreg.¬fresh_for_univ … reg (joint_if_runiverse … def_in) ∧
514           fresh_for_univ … reg (joint_if_runiverse … def_out)) (f_regs l))
515; freshness_data_regs :
516  All … (λreg.¬fresh_for_univ … reg (joint_if_runiverse … def_in) ∧
517               fresh_for_univ … reg (joint_if_runiverse … def_out)) (new_regs … data)
518; data_regs_disjoint : ∀l,r.r ∈ f_regs l → r ∈ new_regs … data → False
519; multi_fetch_ok :
520  ∀l,s.stmt_at … (joint_if_code … def_in) l = Some ? s →
521  let lbls ≝ f_lbls l in let regs ≝ f_regs l in
522  match s with
523  [ sequential s' nxt ⇒
524    let block ≝
525      if eq_identifier … (joint_if_entry … def_in) l then
526        append_seq_list … (f_step … data l s') (added_prologue … data)
527      else
528        f_step … data l s' in
529    l ~❨block, l::lbls, regs❩~> nxt in joint_if_code … def_out
530  | final s' ⇒
531    l ~❨f_fin … data l s', l::lbls, regs❩~> it in joint_if_code … def_out
532  | FCOND abs _ _ _ ⇒ Ⓧabs
533  ]
534}.
535
536lemma if_merge_right : ∀A.∀b.∀x,y : A.x = y → if b then x else y = y.
537#A * #x #y #EQ >EQ % qed.
538
539lemma append_seq_list_nil : ∀p,g,b.append_seq_list p g b [ ] = b.
540#p #g #b elim b -b
541[ ** #a #b #c normalize >append_nil %
542| #f #IH @bnew_proper #r @IH
543]
544qed.
545
546definition pair_swap : ∀A,B.(A × B) → B × A ≝ λA,B,pr.〈\snd pr, \fst pr〉.
547
548(* translation with inline fresh register allocation *)
549definition b_graph_translate :
550  ∀src_g_pars,dst_g_pars : graph_params.
551  ∀globals: list ident.
552  (* initialization info *)
553  ∀data : bound_b_graph_translate_data src_g_pars dst_g_pars globals.
554  (* source function *)
555  ∀def_in : joint_closed_internal_function src_g_pars globals.
556  (* destination function *)
557  Σdef_out : joint_closed_internal_function dst_g_pars globals.
558  ∃data',regs,f_lbls,f_regs.
559    bind_new_instantiates ?? data' data regs ∧ (* so new_regs … data = regs *)
560    b_graph_translate_props … data' def_in def_out f_lbls f_regs
561   ≝
562  λsrc_g_pars,dst_g_pars,globals,data,def.
563  let runiv_data ≝ bcompile … (pair_swap ?? ∘ fresh RegisterTag) data (joint_if_runiverse … def) in
564  let runiv ≝ \fst runiv_data in
565  let data ≝ \snd runiv_data in
566  let entry ≝ joint_if_entry … def in
567  let init ≝
568    mk_joint_internal_function dst_g_pars globals
569    (joint_if_luniverse … def)
570    runiv
571    (init_ret … data) (init_params … data) (init_stack_size … data)
572    (joint_if_local_stacksize … def)
573    (add ?? (empty_map ? (joint_statement ??)) entry (RETURN …))
574    entry in
575  let f : label → joint_statement (src_g_pars : params) globals →
576    joint_internal_function dst_g_pars globals → joint_internal_function dst_g_pars globals ≝
577    λlbl,stmt,def.
578    match stmt with
579    [ sequential inst next ⇒
580      b_adds_graph … (f_step … data lbl inst) lbl next def
581    | final inst ⇒
582      b_fin_adds_graph … (f_fin … data lbl inst) lbl def
583    | FCOND abs _ _ _ ⇒ Ⓧabs
584    ] in
585  let def_out ≝ foldi ??? f (joint_if_code … def) init in
586  let init_c_nxt ≝ get_first_costlabel … def in
587  let def_out_nxt ≝ adds_graph_post … (added_prologue … data) (\snd (init_c_nxt)) def_out in
588  ««add_graph … entry (sequential … (COST_LABEL … (\fst init_c_nxt)) (\snd def_out_nxt)) (\fst def_out_nxt), ?», ?».
589@hide_prf
590[ cases daemon
591| cases daemon (* TODO *)
592] qed.
593
594definition b_graph_transform_program :
595  ∀src_g_pars,dst_g_pars : graph_params.
596  (* initialization *)
597  (∀globals.joint_closed_internal_function src_g_pars globals →
598    bound_b_graph_translate_data src_g_pars dst_g_pars globals) →
599  joint_program src_g_pars →
600  joint_program dst_g_pars ≝
601  λsrc,dst,init,p.
602  transform_program ??? p
603    (λvarnames.transf_fundef ?? (λdef_in.
604      b_graph_translate … (init varnames def_in) def_in)).
605
606definition added_registers :
607  ∀p : graph_params.∀g.
608  joint_internal_function p g → (label → list register) → list register ≝
609  λp,g,def,f_regs.
610  let f ≝ λlbl : label.λ_.λacc.(f_regs lbl)@acc in
611  foldi … f (joint_if_code p g def) [ ].
612
613axiom added_registers_ok :
614  ∀p,g,def,f_regs.
615  ∀l,s.stmt_at … (joint_if_code … def) l = Some ? s →
616  (All … (λlbl.bool_to_Prop (lbl ∈ added_registers p g def f_regs)) (f_regs l)).
617
618(*(* translation without register allocation (more or less an alias) *)
619definition graph_translate :
620  ∀src_g_pars,dst_g_pars : graph_params.
621  ∀globals: list ident.
622  (* initialization info *)
623  call_dest dst_g_pars → (* joint_if_result *)
624  paramsT dst_g_pars → (* joint_if_params *)
625  ℕ → (* joint_if_stacksize *)
626  (* functions dictating the translation *)
627  (label → joint_step src_g_pars globals → step_block dst_g_pars globals) →
628  (label → joint_fin_step src_g_pars → fin_block dst_g_pars globals) →
629  (* source function *)
630  joint_internal_function src_g_pars globals →
631  (* destination function *)
632  joint_internal_function dst_g_pars globals ≝
633  λsrc_g_pars,dst_g_pars,globals,init1,init2,init3,trans_step,trans_fin_step.
634  b_graph_translate … init1 init2 init3
635    (λl,s.return trans_step l s)
636    (λl,s.return trans_fin_step l s).
637*)
638(*
639let rec add_translates
640  (pars1: params1) (globals: list ident)
641  (translate_list: list ?) (start_lbl: label) (dest_lbl: label)
642  (def: joint_internal_function … (graph_params pars1 globals))
643    on translate_list ≝
644  match translate_list with
645  [ nil ⇒ add_graph … start_lbl (GOTO … dest_lbl) def
646  | cons trans translate_list ⇒
647    match translate_list with
648    [ nil ⇒ trans start_lbl dest_lbl def
649    | _ ⇒
650      let 〈tmp_lbl, def〉 ≝ fresh_label … def in
651      let def ≝ trans start_lbl tmp_lbl def in
652        add_translates pars1 globals translate_list tmp_lbl dest_lbl def]].
653
654definition adds_graph ≝
655 λpars1:params1.λglobals. λstmt_list: list (label → joint_statement (graph_params_ pars1) globals).
656  add_translates … (map ?? (λf,start_lbl,dest_lbl. add_graph pars1 ? start_lbl (f dest_lbl)) stmt_list).
657  *)
Note: See TracBrowser for help on using the repository browser.