source: src/joint/Joint.ma @ 2422

Last change on this file since 2422 was 2422, checked in by tranquil, 8 years ago

adapted joint to cl_call f

File size: 19.4 KB
Line 
1include "common/BackEndOps.ma".
2include "common/CostLabel.ma".
3include "common/Registers.ma".
4include "ASM/I8051.ma".
5include "common/Graphs.ma".
6include "utilities/lists.ma".
7include "common/LabelledObjects.ma".
8include "ASM/Util.ma".
9
10(* Here is the structure of parameter records (downward edges are coercions,
11   the ↓ edges are the only explicitly defined coercions). lin_params and
12   graph_params are simple wrappers of unserialized_params, and the coercions
13   from them to params instantiate the missing bits with values for linarized
14   programs and graph programs respectively.
15
16        lin_params      graph_params
17              |   \_____ /____   |
18              |         /     \  |
19              |        /      ↓  ↓
20              |       |      params
21              |       |        |
22              |       |   stmt_params
23              |       |    /   
24          unserialized_params             
25
26unserialized_params : things unrelated to being in graph or linear form
27stmt_params : adds successor type needed to define statements
28params : adds type of code and related properties *)
29
30inductive possible_flows : Type[0] ≝
31| Labels : list label → possible_flows
32| Call : possible_flows.
33
34inductive argument (T : Type[0]) : Type[0] ≝
35| Reg : T → argument T
36| Imm : beval → argument T.
37
38definition psd_argument ≝ argument register.
39 
40definition psd_argument_from_reg : register → psd_argument ≝ Reg register.
41coercion reg_to_psd_argument : ∀r : register.psd_argument ≝ psd_argument_from_reg
42  on _r : register to psd_argument.
43
44definition psd_argument_from_beval : beval → psd_argument ≝ Imm register.
45coercion beval_to_psd_argument : ∀b : beval.psd_argument ≝ psd_argument_from_beval
46  on _b : beval to psd_argument.
47
48definition psd_argument_from_byte : Byte → psd_argument ≝ λb.Imm ? (BVByte b).
49coercion byte_to_psd_argument : ∀b : Byte.psd_argument ≝ psd_argument_from_byte
50  on _b : Byte to psd_argument.
51
52definition hdw_argument ≝ argument Register.
53 
54definition hdw_argument_from_reg : Register → hdw_argument ≝ Reg Register.
55coercion reg_to_hdw_argument : ∀r : Register.hdw_argument ≝ hdw_argument_from_reg
56  on _r : Register to hdw_argument.
57
58definition hdw_argument_from_beval : beval → hdw_argument ≝ Imm Register.
59coercion beval_to_hdw_argument : ∀b : beval.hdw_argument ≝ hdw_argument_from_beval
60  on _b : beval to hdw_argument.
61
62definition hdw_argument_from_byte : Byte → hdw_argument ≝ λb.Imm ? (BVByte b).
63coercion byte_to_hdw_argument : ∀b : Byte.psd_argument ≝ psd_argument_from_byte
64  on _b : Byte to hdw_argument.
65
66definition byte_of_nat : nat → Byte ≝ bitvector_of_nat 8.
67definition zero_byte : Byte ≝ bv_zero 8.
68
69record unserialized_params : Type[1] ≝
70 { acc_a_reg: Type[0] (* registers that will eventually need to be A *)
71 ; acc_b_reg: Type[0] (* registers that will eventually need to be B *)
72 ; acc_a_arg: Type[0] (* arguments that will eventually need to be A *)
73 ; acc_b_arg: Type[0] (* arguments that will eventually need to be B *)
74 ; dpl_reg: Type[0]   (* low address registers *)
75 ; dph_reg: Type[0]   (* high address registers *)
76 ; dpl_arg: Type[0]   (* low address registers *)
77 ; dph_arg: Type[0]   (* high address registers *)
78 ; snd_arg : Type[0]  (* second argument of binary op *)
79 ; pair_move: Type[0] (* argument of move instructions *)
80 ; call_args: Type[0] (* arguments of function calls *)
81 ; call_dest: Type[0] (* possible destination of function computation *)
82 (* other instructions not fitting in the general framework *)
83 ; ext_seq : Type[0]
84(* ; ext_branch : Type[0]
85 ; ext_branch_labels : ext_branch → list label*)
86 ; ext_call : Type[0]
87 ; ext_tailcall : Type[0]
88 (* if needed: ; ext_fin_branch : Type[0] ; ext_fin_branch_labels : ext_fin_branch → list label *)
89 ; paramsT : Type[0]
90 ; localsT: Type[0]
91 }.
92
93inductive joint_seq (p:unserialized_params) (globals: list ident): Type[0] ≝
94  | COMMENT: String → joint_seq p globals
95  | COST_LABEL: costlabel → joint_seq p globals
96  | MOVE: pair_move p → joint_seq p globals
97  | POP: acc_a_reg p → joint_seq p globals
98  | PUSH: acc_a_arg p → joint_seq p globals
99  | ADDRESS: ∀i: ident. (member i (eq_identifier ?) globals) → dpl_reg p → dph_reg p → joint_seq p globals
100  | OPACCS: OpAccs → acc_a_reg p → acc_b_reg p → acc_a_arg p → acc_b_arg p → joint_seq p globals
101  | OP1: Op1 → acc_a_reg p → acc_a_reg p → joint_seq p globals
102  | OP2: Op2 → acc_a_reg p → acc_a_arg p → snd_arg p → joint_seq p globals
103  (* int done with generic move *)
104(*| INT: generic_reg p → Byte → joint_seq p globals *)
105  | CLEAR_CARRY: joint_seq p globals
106  | SET_CARRY: joint_seq p globals
107  | LOAD: acc_a_reg p → dpl_arg p → dph_arg p → joint_seq p globals
108  | STORE: dpl_arg p → dph_arg p → acc_a_arg p → joint_seq p globals
109  | CALL_ID: ident → call_args p → call_dest p → joint_seq p globals
110  | extension_seq : ext_seq p → joint_seq p globals
111  | extension_call : ext_call p → joint_seq p globals.
112
113axiom EmptyString : String.
114definition NOOP ≝ λp,globals.COMMENT p globals EmptyString.
115
116notation "r ← a1 .op. a2" with precedence 60 for
117  @{'op2 $op $r $a1 $a2}.
118notation "r ← . op . a" with precedence 60 for
119  @{'op1 $op $r $a}.
120notation "r ← a" with precedence 60 for
121  @{'mov $r $a}. (* to be set in individual languages *)
122notation "❮r, s❯ ← a1 . op . a2" with precedence 55 for
123  @{'opaccs $op $r $s $a1 $a2}.
124
125interpretation "op2" 'op2 op r a1 a2 = (OP2 ? ? op r a1 a2).
126interpretation "op1" 'op1 op r a = (OP1 ? ? op r a).
127interpretation "opaccs" 'opaccs op r s a1 a2 = (OPACCS ? ? op r s a1 a2).
128
129coercion extension_seq_to_seq : ∀p,globals.∀s : ext_seq p.joint_seq p globals ≝
130  extension_seq on _s : ext_seq ? to joint_seq ??.
131coercion extension_call_to_seq : ∀p,globals.∀s : ext_call p.joint_seq p globals ≝
132  extension_call on _s : ext_call ? to joint_seq ??.
133 
134(* inductive joint_branch (p : step_params) : Type[0] ≝
135  | COND: acc_a_reg p → label → joint_branch p
136  | extension_branch : ext_branch p → joint_branch p.*)
137
138(*coercion extension_to_branch : ∀p.∀s : ext_branch p.joint_branch p ≝
139  extension_branch on _s : ext_branch ? to joint_branch ?.*)
140
141inductive joint_step (p : unserialized_params) (globals : list ident) : Type[0] ≝
142  | step_seq : joint_seq p globals → joint_step p globals
143  | COND: acc_a_reg p → label → joint_step p globals.
144
145coercion seq_to_step : ∀p,globals.∀s : joint_seq p globals.joint_step p globals ≝
146  step_seq on _s : joint_seq ?? to joint_step ??.
147
148definition step_flows ≝ λp,globals.λs : joint_step p globals.
149  match s with
150  [ step_seq s ⇒
151    match s with
152    [ CALL_ID _ _ _ ⇒ Call
153    | extension_call _ ⇒ Call
154    | _ ⇒ Labels … [ ]
155    ]
156  | COND _ l ⇒ Labels … [l]
157  ].
158
159definition step_labels ≝
160  λp, globals.λs : joint_step p globals.
161    match step_flows … s with
162    [ Labels lbls ⇒ lbls
163    | Call ⇒ [ ]
164    ].
165
166definition step_forall_labels : ∀p.∀globals.
167    (label → Prop) → joint_step p globals → Prop ≝
168λp,g,P,inst. All … P (step_labels … inst).
169
170record stmt_params : Type[1] ≝
171  { uns_pars :> unserialized_params
172  ; succ : Type[0]
173  ; succ_label : succ → option label
174  }.
175
176inductive joint_fin_step (p: unserialized_params): Type[0] ≝
177  | GOTO: label → joint_fin_step p
178  | RETURN: joint_fin_step p
179  | tailcall : ext_tailcall p → joint_fin_step p.
180
181definition fin_step_flows ≝ λp.λs : joint_fin_step p.
182  match s with
183  [ GOTO l ⇒ Labels … [l]
184  | tailcall _ ⇒ Call (* tailcalls will need to be integrated in structured traces *)
185  | _ ⇒ Labels … [ ]
186  ].
187
188definition fin_step_labels ≝
189  λp.λs : joint_fin_step p.
190    match fin_step_flows … s with
191    [ Labels lbls ⇒ lbls
192    | Call ⇒ [ ]
193    ].
194
195inductive joint_statement (p: stmt_params) (globals: list ident): Type[0] ≝
196  | sequential: joint_step p globals → succ p → joint_statement p globals
197  | final: joint_fin_step p → joint_statement p globals.
198
199coercion extension_fin_to_fin_step : ∀p : stmt_params.
200  ∀s : ext_tailcall p.joint_fin_step p ≝
201  tailcall on _s : ext_tailcall ? to joint_fin_step ?.
202
203coercion fin_step_to_stmt : ∀p : stmt_params.∀globals.
204  ∀s : joint_fin_step p.joint_statement p globals ≝
205  final on _s : joint_fin_step ? to joint_statement ??.
206
207record params : Type[1] ≝
208 { stmt_pars :> stmt_params
209 ; codeT: list ident → Type[0]
210 ; code_point : Type[0]
211 ; stmt_at : ∀globals.codeT globals → code_point → option (joint_statement stmt_pars globals)
212 ; point_of_label : ∀globals.codeT globals → label → option code_point
213 ; point_of_succ : code_point → succ stmt_pars → code_point
214 }.
215
216definition code_has_point ≝
217  λp,globals,c,pt.match stmt_at p globals c pt with [Some _ ⇒ true | None ⇒ false].
218
219(* interpretation "code membership" 'mem p c = (code_has_point ?? c p). *)
220
221definition point_in_code ≝ λp,globals,code.Σpt.bool_to_Prop (code_has_point p globals code pt).
222unification hint 0 ≔ p, globals, code ⊢ point_in_code p globals code ≡ Sig (code_point p) (λpt.bool_to_Prop (code_has_point p globals code pt)).
223
224definition stmt_at_safe ≝ λp,globals,code.λpt : point_in_code p globals code.
225  match pt with
226  [ mk_Sig pt' pt_prf ⇒
227    match stmt_at … code pt' return λx.stmt_at … code pt' = x → ? with
228    [ Some x ⇒ λ_.x
229    | None ⇒ λabs.⊥
230    ] (refl …)
231  ]. normalize in pt_prf;
232    >abs in pt_prf; // qed.
233
234definition forall_statements : ∀p : params.∀globals.pred_transformer (joint_statement p globals) (codeT p globals)  ≝
235  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P s.
236
237definition forall_statements_i :
238  ∀p : params.∀globals.(code_point p → joint_statement p globals → Prop) →
239    codeT p globals → Prop  ≝
240  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P pt s.
241
242lemma forall_statements_mp : ∀p,globals.modus_ponens ?? (forall_statements p globals).
243#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
244
245lemma forall_statements_i_mp : ∀p,globals.∀P,Q.(∀pt,s.P pt s → Q pt s) →
246  ∀c.forall_statements_i p globals P c → forall_statements_i p globals Q c.
247#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
248
249definition code_has_label ≝ λp,globals,c,l.
250  match point_of_label p globals c l with
251  [ Some pt ⇒ code_has_point … c pt
252  | None ⇒ false
253  ].
254
255definition stmt_explicit_labels :
256  ∀p,globals.
257  joint_statement p globals → list label ≝
258  λp,globals,stmt. match stmt with
259  [ sequential c _ ⇒ step_labels … c
260  | final c ⇒ fin_step_labels … c
261  ].
262
263definition stmt_implicit_label : ∀p,globals.joint_statement p globals →
264  option label ≝
265 λp,globals,s.match s with [ sequential _ s ⇒ succ_label … s | _ ⇒ None ?].
266 
267definition stmt_labels : ∀p : stmt_params.∀globals.
268    joint_statement p globals → list label ≝
269  λp,g,stmt.
270  (match stmt_implicit_label … stmt with
271     [ Some l ⇒ [l]
272     | None ⇒ [ ]
273     ]) @ stmt_explicit_labels … stmt.
274
275definition stmt_forall_labels ≝
276  λp, globals.λ P : label → Prop.λs : joint_statement p globals.
277  All … P (stmt_labels … s).
278
279lemma stmt_forall_labels_explicit : ∀p,globals,P.∀s : joint_statement p globals.
280  stmt_forall_labels … P s → All … P (stmt_explicit_labels … s).
281#p#globals#P #s
282whd in ⊢ (% → ?);
283whd in ⊢ (???% → ?);
284elim (stmt_implicit_label ???) [2: #next * #_] //
285qed.
286
287lemma stmt_forall_labels_implicit : ∀p,globals,P.∀s : joint_statement p globals.
288  stmt_forall_labels … P s →
289    opt_All … P (stmt_implicit_label … s).
290#p#globals#P#s
291whd in ⊢ (% → ?);
292whd in ⊢ (???% → ?);
293elim (stmt_implicit_label ???)
294[ //
295| #next * #Pnext #_ @Pnext
296]
297qed.
298
299definition code_forall_labels ≝
300  λp,globals,P,c.forall_statements p globals (stmt_forall_labels … P) c.
301
302lemma code_forall_labels_mp : ∀p,globals,P,Q.(∀l.P l → Q l) →
303  ∀c.code_forall_labels p globals P c → code_forall_labels … Q c ≝
304  λp,globals,P,Q,H.forall_statements_mp … (λs. All_mp … H ?).
305
306record lin_params : Type[1] ≝
307  { l_u_pars : unserialized_params }.
308 
309lemma index_of_label_length : ∀tag,A,lbl,l.occurs_exactly_once ?? lbl l → lt (index_of_label tag A lbl l) (|l|).
310#tag #A #lbl #l elim l [*]
311** [2: #id] #a #tl #IH
312[ change with (if (eq_identifier ???) then ? else ?) in match (occurs_exactly_once ????);
313  change with (if (eq_identifier ???) then ? else ?) in match (index_of_label ????);
314  @eq_identifier_elim #Heq normalize nodelta
315  [ #_ normalize / by /]
316| whd in ⊢ (?%→?%?);
317]
318#H >(index_of_label_from_internal … H)
319@le_S_S @(IH H)
320qed.
321
322(* mv *)
323lemma nth_opt_hit_length : ∀A,l,n,x.nth_opt A n l = Some ? x → n < |l|.
324#A #l elim l normalize [ #n #x #ABS destruct(ABS)]
325#hd #tl #IH * [2:#n] #x normalize [#H @le_S_S @(IH … H)] /2 by /
326qed.
327
328lemma nth_opt_miss_length : ∀A,l,n.nth_opt A n l = None ? → n ≥ |l|.
329#A #l elim l [//] #hd #tl #IH * normalize [#ABS destruct(ABS)]
330#n' #H @le_S_S @(IH … H)
331qed.
332
333lemma nth_opt_safe : ∀A,l,n,prf.nth_opt A n l = Some ? (nth_safe A n l prf).
334#A #l elim l
335[ #n #ABS @⊥ /2 by absurd/
336| #hd #tl #IH * normalize //
337]
338qed.
339
340definition lin_params_to_params ≝
341  λlp : lin_params.
342     mk_params
343      (mk_stmt_params (l_u_pars lp) unit (λ_.None ?))
344    (* codeT ≝ *)(λglobals.list ((option label) × (joint_statement ? globals)))
345    (* code_point ≝ *)ℕ
346    (* stmt_at ≝ *)(λglobals,code,point.! ls ← nth_opt ? point code ; return \snd ls)
347    (* point_of_label ≝ *)(λglobals,c,lbl.
348      If occurs_exactly_once ?? lbl c then with prf do
349        return index_of_label ?? lbl c
350      else
351        None ?)
352    (* point_of_succ ≝ *)(λcurrent.λ_.S (current)).
353
354coercion lp_to_p : ∀lp : lin_params.params ≝ lin_params_to_params
355  on _lp : lin_params to params.
356 
357lemma lin_code_has_point : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
358  ∀pt.code_has_point … code pt = leb (S pt) (|code|).
359#lp #globals #code elim code
360[ #pt %
361| #hd #tl #IH * [%]
362  #n @IH
363]qed.
364
365lemma lin_code_has_label : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
366  ∀lbl.code_has_label … code lbl = occurs_exactly_once ?? lbl code.
367#lp #globals #code #lbl
368whd in match (code_has_label ????);
369whd in match (point_of_label ????);
370elim (true_or_false_Prop (occurs_exactly_once ?? lbl code))
371#Heq >Heq normalize nodelta
372[ >lin_code_has_point @(leb_elim (S ?)) [#_ |
373  #ABS elim(absurd ?? ABS) -ABS
374  @index_of_label_length assumption ]] %
375qed.
376
377record graph_params : Type[1] ≝
378  { g_u_pars : unserialized_params }.
379
380(* One common instantiation of params via Graphs of joint_statements
381   (all languages but LIN) *)
382definition graph_params_to_params ≝
383  λgp : graph_params.
384     mk_params
385      (mk_stmt_params (g_u_pars gp) label (Some ?))
386    (* codeT ≝ *)(λglobals.graph (joint_statement ? globals))
387    (* code_point ≝ *)label
388    (* stmt_at ≝ *)(λglobals,code.lookup LabelTag ? code)
389    (* point_of_label ≝ *)(λ_.λ_.λlbl.return lbl)
390    (* point_of_succ ≝ *)(λ_.λlbl.lbl).
391
392coercion gp_to_p : ∀gp:graph_params.params ≝ graph_params_to_params
393on _gp : graph_params to params.
394
395lemma graph_code_has_point : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
396  ∀pt.code_has_point … code pt = (pt ∈ code). // qed.
397
398lemma graph_code_has_label : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
399  ∀lbl.code_has_label … code lbl = (lbl ∈ code). // qed.
400
401definition stmt_forall_succ ≝ λp,globals.λP : succ p → Prop.
402  λs : joint_statement p globals.
403  match s with
404  [ sequential _ n ⇒ P n
405  | _ ⇒ True
406  ].
407
408definition statement_closed : ∀globals.∀p : params.
409  codeT p globals → code_point p → (joint_statement p globals) → Prop ≝
410λglobals,p,code,pt,s.
411  All ? (λl.bool_to_Prop (code_has_label ?? code l)) (stmt_explicit_labels … s) ∧
412  stmt_forall_succ … (λn.bool_to_Prop (code_has_point … code (point_of_succ ? pt n))) s.
413
414definition code_closed : ∀p : params.∀globals.
415  codeT p globals → Prop ≝ λp,globals,code.
416    forall_statements_i … (statement_closed … code) code.
417
418record joint_internal_function (p:params) (globals: list ident) : Type[0] ≝
419{ joint_if_luniverse: universe LabelTag;    (*CSC: used only for compilation*)
420  joint_if_runiverse: universe RegisterTag; (*CSC: used only for compilation*)
421  (* Paolo: if we want this machinery to work for RTLabs too, we will need the
422     following, right? *)
423(*  joint_if_sig: signature;  -- dropped in front end *)
424  joint_if_result   : call_dest p;
425  joint_if_params   : paramsT p;
426  joint_if_locals   : list (localsT p); (* use void where no locals are present *)
427(*CSC: XXXXX stacksize unused for LTL-...*)
428  joint_if_stacksize: nat;
429  joint_if_code     : codeT p globals ;
430  joint_if_entry : point_in_code … joint_if_code ;
431  joint_if_exit : point_in_code … joint_if_code
432}.
433
434definition joint_closed_internal_function ≝
435  λp,globals.
436    Σdef : joint_internal_function p globals. code_closed … (joint_if_code … def).
437
438definition set_joint_code ≝
439  λglobals: list ident.
440  λpars: params.
441  λint_fun: joint_internal_function pars globals.
442  λgraph: codeT pars globals.
443  λentry.
444  λexit.
445    mk_joint_internal_function pars globals
446      (joint_if_luniverse … int_fun) (joint_if_runiverse … int_fun) (joint_if_result … int_fun)
447      (joint_if_params … int_fun) (joint_if_locals … int_fun) (joint_if_stacksize … int_fun)
448      graph entry exit.
449
450definition set_joint_if_graph ≝
451  λglobals.λpars : graph_params.
452  λgraph.
453  λp:joint_internal_function pars globals.
454  λentry_prf.
455  λexit_prf.
456    set_joint_code globals pars p
457      graph
458      (mk_Sig ?? (joint_if_entry ?? p) entry_prf)
459      (mk_Sig … (joint_if_exit ?? p) exit_prf).
460
461definition set_luniverse ≝
462  λglobals,pars.
463  λp : joint_internal_function globals pars.
464  λluniverse: universe LabelTag.
465   mk_joint_internal_function globals pars
466    luniverse (joint_if_runiverse … p) (joint_if_result … p)
467    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
468    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
469
470definition set_runiverse ≝
471  λglobals,pars.
472  λp : joint_internal_function globals pars.
473  λruniverse: universe RegisterTag.
474   mk_joint_internal_function globals pars
475    (joint_if_luniverse … p) runiverse (joint_if_result … p)
476    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
477    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
478   
479(* Specialized for graph_params *)
480definition add_graph ≝
481  λg_pars : graph_params.λglobals.λl:label.λstmt.
482    λp:joint_internal_function g_pars globals.
483   let code ≝ add … (joint_if_code … p) l stmt in
484    mk_joint_internal_function …
485     (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
486     (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
487     code
488     (pi1 … (joint_if_entry … p))
489     (pi1 … (joint_if_exit … p)).
490>graph_code_has_point whd in match code; >mem_set_add
491@orb_Prop_r [elim (joint_if_entry ???) | elim (joint_if_exit ???) ]
492#x #H <graph_code_has_point @H
493qed.
494
495definition set_locals ≝
496  λpars,globals.
497  λp : joint_internal_function pars globals.
498  λlocals.
499   mk_joint_internal_function pars globals
500    (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
501    (joint_if_params … p) locals (joint_if_stacksize … p)
502    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
503
504definition joint_function ≝ λp,globals. fundef (joint_closed_internal_function p globals).
505
506definition joint_program ≝
507 λp:params. program (joint_function p) nat.
Note: See TracBrowser for help on using the repository browser.