source: src/joint/Joint.ma @ 2655

Last change on this file since 2655 was 2655, checked in by tranquil, 7 years ago

new step in code semantic lemma

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