source: src/joint/Joint.ma @ 2446

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

generalised calls to calls with pointers

File size: 19.2 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: (ident + dpl_arg p×(dph_arg p)) → call_args p → call_dest p → joint_seq p globals
110  | extension_seq : ext_seq p → joint_seq p globals.
111
112axiom EmptyString : String.
113definition NOOP ≝ λp,globals.COMMENT p globals EmptyString.
114
115notation "r ← a1 .op. a2" with precedence 60 for
116  @{'op2 $op $r $a1 $a2}.
117notation "r ← . op . a" with precedence 60 for
118  @{'op1 $op $r $a}.
119notation "r ← a" with precedence 60 for
120  @{'mov $r $a}. (* to be set in individual languages *)
121notation "❮r, s❯ ← a1 . op . a2" with precedence 55 for
122  @{'opaccs $op $r $s $a1 $a2}.
123
124interpretation "op2" 'op2 op r a1 a2 = (OP2 ? ? op r a1 a2).
125interpretation "op1" 'op1 op r a = (OP1 ? ? op r a).
126interpretation "opaccs" 'opaccs op r s a1 a2 = (OPACCS ? ? op r s a1 a2).
127
128coercion extension_seq_to_seq : ∀p,globals.∀s : ext_seq p.joint_seq p globals ≝
129  extension_seq on _s : ext_seq ? to joint_seq ??.
130
131(* inductive joint_branch (p : step_params) : Type[0] ≝
132  | COND: acc_a_reg p → label → joint_branch p
133  | extension_branch : ext_branch p → joint_branch p.*)
134
135(*coercion extension_to_branch : ∀p.∀s : ext_branch p.joint_branch p ≝
136  extension_branch on _s : ext_branch ? to joint_branch ?.*)
137
138inductive joint_step (p : unserialized_params) (globals : list ident) : Type[0] ≝
139  | step_seq : joint_seq p globals → joint_step p globals
140  | COND: acc_a_reg p → label → 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_flows ≝ λp,globals.λs : joint_step p globals.
146  match s with
147  [ step_seq s ⇒
148    match s with
149    [ CALL _ _ _ ⇒ Call
150    | _ ⇒ Labels … [ ]
151    ]
152  | COND _ l ⇒ Labels … [l]
153  ].
154
155definition step_labels ≝
156  λp, globals.λs : joint_step p globals.
157    match step_flows … s with
158    [ Labels lbls ⇒ lbls
159    | Call ⇒ [ ]
160    ].
161
162definition step_forall_labels : ∀p.∀globals.
163    (label → Prop) → joint_step p globals → Prop ≝
164λp,g,P,inst. All … P (step_labels … inst).
165
166record stmt_params : Type[1] ≝
167  { uns_pars :> unserialized_params
168  ; succ : Type[0]
169  ; succ_label : succ → option label
170  }.
171
172inductive joint_fin_step (p: unserialized_params): Type[0] ≝
173  | GOTO: label → joint_fin_step p
174  | RETURN: joint_fin_step p
175  | tailcall : ext_tailcall p → joint_fin_step p.
176
177definition fin_step_flows ≝ λp.λs : joint_fin_step p.
178  match s with
179  [ GOTO l ⇒ Labels … [l]
180  | tailcall _ ⇒ Call (* tailcalls will need to be integrated in structured traces *)
181  | _ ⇒ Labels … [ ]
182  ].
183
184definition fin_step_labels ≝
185  λp.λs : joint_fin_step p.
186    match fin_step_flows … s with
187    [ Labels lbls ⇒ lbls
188    | Call ⇒ [ ]
189    ].
190
191inductive joint_statement (p: stmt_params) (globals: list ident): Type[0] ≝
192  | sequential: joint_step p globals → succ p → joint_statement p globals
193  | final: joint_fin_step p → joint_statement p globals.
194
195coercion extension_fin_to_fin_step : ∀p : stmt_params.
196  ∀s : ext_tailcall p.joint_fin_step p ≝
197  tailcall on _s : ext_tailcall ? to joint_fin_step ?.
198
199coercion fin_step_to_stmt : ∀p : stmt_params.∀globals.
200  ∀s : joint_fin_step p.joint_statement p globals ≝
201  final on _s : joint_fin_step ? to joint_statement ??.
202
203record params : Type[1] ≝
204 { stmt_pars :> stmt_params
205 ; codeT: list ident → Type[0]
206 ; code_point : Type[0]
207 ; stmt_at : ∀globals.codeT globals → code_point → option (joint_statement stmt_pars globals)
208 ; point_of_label : ∀globals.codeT globals → label → option code_point
209 ; point_of_succ : code_point → succ stmt_pars → code_point
210 }.
211
212definition code_has_point ≝
213  λp,globals,c,pt.match stmt_at p globals c pt with [Some _ ⇒ true | None ⇒ false].
214
215(* interpretation "code membership" 'mem p c = (code_has_point ?? c p). *)
216
217definition point_in_code ≝ λp,globals,code.Σpt.bool_to_Prop (code_has_point p globals code pt).
218unification 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)).
219
220definition stmt_at_safe ≝ λp,globals,code.λpt : point_in_code p globals code.
221  match pt with
222  [ mk_Sig pt' pt_prf ⇒
223    match stmt_at … code pt' return λx.stmt_at … code pt' = x → ? with
224    [ Some x ⇒ λ_.x
225    | None ⇒ λabs.⊥
226    ] (refl …)
227  ]. normalize in pt_prf;
228    >abs in pt_prf; // qed.
229
230definition forall_statements : ∀p : params.∀globals.pred_transformer (joint_statement p globals) (codeT p globals)  ≝
231  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P s.
232
233definition forall_statements_i :
234  ∀p : params.∀globals.(code_point p → joint_statement p globals → Prop) →
235    codeT p globals → Prop  ≝
236  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P pt s.
237
238lemma forall_statements_mp : ∀p,globals.modus_ponens ?? (forall_statements p globals).
239#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
240
241lemma forall_statements_i_mp : ∀p,globals.∀P,Q.(∀pt,s.P pt s → Q pt s) →
242  ∀c.forall_statements_i p globals P c → forall_statements_i p globals Q c.
243#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
244
245definition code_has_label ≝ λp,globals,c,l.
246  match point_of_label p globals c l with
247  [ Some pt ⇒ code_has_point … c pt
248  | None ⇒ false
249  ].
250
251definition stmt_explicit_labels :
252  ∀p,globals.
253  joint_statement p globals → list label ≝
254  λp,globals,stmt. match stmt with
255  [ sequential c _ ⇒ step_labels … c
256  | final c ⇒ fin_step_labels … c
257  ].
258
259definition stmt_implicit_label : ∀p,globals.joint_statement p globals →
260  option label ≝
261 λp,globals,s.match s with [ sequential _ s ⇒ succ_label … s | _ ⇒ None ?].
262 
263definition stmt_labels : ∀p : stmt_params.∀globals.
264    joint_statement p globals → list label ≝
265  λp,g,stmt.
266  (match stmt_implicit_label … stmt with
267     [ Some l ⇒ [l]
268     | None ⇒ [ ]
269     ]) @ stmt_explicit_labels … stmt.
270
271definition stmt_forall_labels ≝
272  λp, globals.λ P : label → Prop.λs : joint_statement p globals.
273  All … P (stmt_labels … s).
274
275lemma stmt_forall_labels_explicit : ∀p,globals,P.∀s : joint_statement p globals.
276  stmt_forall_labels … P s → All … P (stmt_explicit_labels … s).
277#p#globals#P #s
278whd in ⊢ (% → ?);
279whd in ⊢ (???% → ?);
280elim (stmt_implicit_label ???) [2: #next * #_] //
281qed.
282
283lemma stmt_forall_labels_implicit : ∀p,globals,P.∀s : joint_statement p globals.
284  stmt_forall_labels … P s →
285    opt_All … P (stmt_implicit_label … s).
286#p#globals#P#s
287whd in ⊢ (% → ?);
288whd in ⊢ (???% → ?);
289elim (stmt_implicit_label ???)
290[ //
291| #next * #Pnext #_ @Pnext
292]
293qed.
294
295definition code_forall_labels ≝
296  λp,globals,P,c.forall_statements p globals (stmt_forall_labels … P) c.
297
298lemma code_forall_labels_mp : ∀p,globals,P,Q.(∀l.P l → Q l) →
299  ∀c.code_forall_labels p globals P c → code_forall_labels … Q c ≝
300  λp,globals,P,Q,H.forall_statements_mp … (λs. All_mp … H ?).
301
302record lin_params : Type[1] ≝
303  { l_u_pars : unserialized_params }.
304 
305lemma index_of_label_length : ∀tag,A,lbl,l.occurs_exactly_once ?? lbl l → lt (index_of_label tag A lbl l) (|l|).
306#tag #A #lbl #l elim l [*]
307** [2: #id] #a #tl #IH
308[ change with (if (eq_identifier ???) then ? else ?) in match (occurs_exactly_once ????);
309  change with (if (eq_identifier ???) then ? else ?) in match (index_of_label ????);
310  @eq_identifier_elim #Heq normalize nodelta
311  [ #_ normalize / by /]
312| whd in ⊢ (?%→?%?);
313]
314#H >(index_of_label_from_internal … H)
315@le_S_S @(IH H)
316qed.
317
318(* mv *)
319lemma nth_opt_hit_length : ∀A,l,n,x.nth_opt A n l = Some ? x → n < |l|.
320#A #l elim l normalize [ #n #x #ABS destruct(ABS)]
321#hd #tl #IH * [2:#n] #x normalize [#H @le_S_S @(IH … H)] /2 by /
322qed.
323
324lemma nth_opt_miss_length : ∀A,l,n.nth_opt A n l = None ? → n ≥ |l|.
325#A #l elim l [//] #hd #tl #IH * normalize [#ABS destruct(ABS)]
326#n' #H @le_S_S @(IH … H)
327qed.
328
329lemma nth_opt_safe : ∀A,l,n,prf.nth_opt A n l = Some ? (nth_safe A n l prf).
330#A #l elim l
331[ #n #ABS @⊥ /2 by absurd/
332| #hd #tl #IH * normalize //
333]
334qed.
335
336definition lin_params_to_params ≝
337  λlp : lin_params.
338     mk_params
339      (mk_stmt_params (l_u_pars lp) unit (λ_.None ?))
340    (* codeT ≝ *)(λglobals.list ((option label) × (joint_statement ? globals)))
341    (* code_point ≝ *)ℕ
342    (* stmt_at ≝ *)(λglobals,code,point.! ls ← nth_opt ? point code ; return \snd ls)
343    (* point_of_label ≝ *)(λglobals,c,lbl.
344      If occurs_exactly_once ?? lbl c then with prf do
345        return index_of_label ?? lbl c
346      else
347        None ?)
348    (* point_of_succ ≝ *)(λcurrent.λ_.S (current)).
349
350coercion lp_to_p : ∀lp : lin_params.params ≝ lin_params_to_params
351  on _lp : lin_params to params.
352 
353lemma lin_code_has_point : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
354  ∀pt.code_has_point … code pt = leb (S pt) (|code|).
355#lp #globals #code elim code
356[ #pt %
357| #hd #tl #IH * [%]
358  #n @IH
359]qed.
360
361lemma lin_code_has_label : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
362  ∀lbl.code_has_label … code lbl = occurs_exactly_once ?? lbl code.
363#lp #globals #code #lbl
364whd in match (code_has_label ????);
365whd in match (point_of_label ????);
366elim (true_or_false_Prop (occurs_exactly_once ?? lbl code))
367#Heq >Heq normalize nodelta
368[ >lin_code_has_point @(leb_elim (S ?)) [#_ |
369  #ABS elim(absurd ?? ABS) -ABS
370  @index_of_label_length assumption ]] %
371qed.
372
373record graph_params : Type[1] ≝
374  { g_u_pars : unserialized_params }.
375
376(* One common instantiation of params via Graphs of joint_statements
377   (all languages but LIN) *)
378definition graph_params_to_params ≝
379  λgp : graph_params.
380     mk_params
381      (mk_stmt_params (g_u_pars gp) label (Some ?))
382    (* codeT ≝ *)(λglobals.graph (joint_statement ? globals))
383    (* code_point ≝ *)label
384    (* stmt_at ≝ *)(λglobals,code.lookup LabelTag ? code)
385    (* point_of_label ≝ *)(λ_.λ_.λlbl.return lbl)
386    (* point_of_succ ≝ *)(λ_.λlbl.lbl).
387
388coercion gp_to_p : ∀gp:graph_params.params ≝ graph_params_to_params
389on _gp : graph_params to params.
390
391lemma graph_code_has_point : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
392  ∀pt.code_has_point … code pt = (pt ∈ code). // qed.
393
394lemma graph_code_has_label : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
395  ∀lbl.code_has_label … code lbl = (lbl ∈ code). // qed.
396
397definition stmt_forall_succ ≝ λp,globals.λP : succ p → Prop.
398  λs : joint_statement p globals.
399  match s with
400  [ sequential _ n ⇒ P n
401  | _ ⇒ True
402  ].
403
404definition statement_closed : ∀globals.∀p : params.
405  codeT p globals → code_point p → (joint_statement p globals) → Prop ≝
406λglobals,p,code,pt,s.
407  All ? (λl.bool_to_Prop (code_has_label ?? code l)) (stmt_explicit_labels … s) ∧
408  stmt_forall_succ … (λn.bool_to_Prop (code_has_point … code (point_of_succ ? pt n))) s.
409
410definition code_closed : ∀p : params.∀globals.
411  codeT p globals → Prop ≝ λp,globals,code.
412    forall_statements_i … (statement_closed … code) code.
413
414record joint_internal_function (p:params) (globals: list ident) : Type[0] ≝
415{ joint_if_luniverse: universe LabelTag;    (*CSC: used only for compilation*)
416  joint_if_runiverse: universe RegisterTag; (*CSC: used only for compilation*)
417  (* Paolo: if we want this machinery to work for RTLabs too, we will need the
418     following, right? *)
419(*  joint_if_sig: signature;  -- dropped in front end *)
420  joint_if_result   : call_dest p;
421  joint_if_params   : paramsT p;
422  joint_if_locals   : list (localsT p); (* use void where no locals are present *)
423(*CSC: XXXXX stacksize unused for LTL-...*)
424  joint_if_stacksize: nat;
425  joint_if_code     : codeT p globals ;
426  joint_if_entry : point_in_code … joint_if_code ;
427  joint_if_exit : point_in_code … joint_if_code
428}.
429
430definition joint_closed_internal_function ≝
431  λp,globals.
432    Σdef : joint_internal_function p globals. code_closed … (joint_if_code … def).
433
434definition set_joint_code ≝
435  λglobals: list ident.
436  λpars: params.
437  λint_fun: joint_internal_function pars globals.
438  λgraph: codeT pars globals.
439  λentry.
440  λexit.
441    mk_joint_internal_function pars globals
442      (joint_if_luniverse … int_fun) (joint_if_runiverse … int_fun) (joint_if_result … int_fun)
443      (joint_if_params … int_fun) (joint_if_locals … int_fun) (joint_if_stacksize … int_fun)
444      graph entry exit.
445
446definition set_joint_if_graph ≝
447  λglobals.λpars : graph_params.
448  λgraph.
449  λp:joint_internal_function pars globals.
450  λentry_prf.
451  λexit_prf.
452    set_joint_code globals pars p
453      graph
454      (mk_Sig ?? (joint_if_entry ?? p) entry_prf)
455      (mk_Sig … (joint_if_exit ?? p) exit_prf).
456
457definition set_luniverse ≝
458  λglobals,pars.
459  λp : joint_internal_function globals pars.
460  λluniverse: universe LabelTag.
461   mk_joint_internal_function globals pars
462    luniverse (joint_if_runiverse … p) (joint_if_result … p)
463    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
464    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
465
466definition set_runiverse ≝
467  λglobals,pars.
468  λp : joint_internal_function globals pars.
469  λruniverse: universe RegisterTag.
470   mk_joint_internal_function globals pars
471    (joint_if_luniverse … p) runiverse (joint_if_result … p)
472    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
473    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
474   
475(* Specialized for graph_params *)
476definition add_graph ≝
477  λg_pars : graph_params.λglobals.λl:label.λstmt.
478    λp:joint_internal_function g_pars globals.
479   let code ≝ add … (joint_if_code … p) l stmt in
480    mk_joint_internal_function …
481     (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
482     (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
483     code
484     (pi1 … (joint_if_entry … p))
485     (pi1 … (joint_if_exit … p)).
486>graph_code_has_point whd in match code; >mem_set_add
487@orb_Prop_r [elim (joint_if_entry ???) | elim (joint_if_exit ???) ]
488#x #H <graph_code_has_point @H
489qed.
490
491definition set_locals ≝
492  λpars,globals.
493  λp : joint_internal_function pars globals.
494  λlocals.
495   mk_joint_internal_function pars globals
496    (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
497    (joint_if_params … p) locals (joint_if_stacksize … p)
498    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
499
500definition joint_function ≝ λp,globals. fundef (joint_closed_internal_function p globals).
501
502definition joint_program ≝
503 λp:params. program (joint_function p) nat.
Note: See TracBrowser for help on using the repository browser.