source: src/joint/Joint_paolo.ma @ 2186

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

updated joint semantics

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