source: src/joint/Joint_paolo.ma @ 1643

Last change on this file since 1643 was 1643, checked in by tranquil, 9 years ago
  • some changes in everything
  • separated extensions in sequential and final, to accomodate tailcalls
  • finished RTL's semantics
File size: 12.2 KB
Line 
1include "ASM/I8051.ma".
2include "common/CostLabel.ma".
3include "common/AST.ma".
4include "common/Registers.ma".
5include "common/Graphs.ma".
6
7(* Here is the structure of parameter records (downward edges are coercions,
8   the ↓ edges are the only explicitly defined coercions). lin_params and
9   graph_params are simple wrappers of unserialized_params, and the coercions
10   from them to params instantiate the missing bits with values for linarized
11   programs and graph programs respectively.
12
13        lin_params      graph_params
14          |       \_____ /____   |
15          |             /     \  |
16          \_______     /      ↓  ↓
17                  \   _\____ params
18                   \_/  \      |
19                    / \  \     ↓
20              _____/   unserialized_params
21             /      _______/   |
22            /      /           |
23     stmt_params  /   local_params
24        |      __/             |
25    inst_params       funct_params
26
27inst_params : types needed to define instructions
28stmt_params : adds successor type needed to define statements
29funct_params : types of result register and parameters of function
30local_params : adds types of local registers
31params : adds type of code and lookup function
32graph_params : is made just of inst_params and local_params, and the coercion
33  to params adds structure common to graph languages *)
34
35record inst_params : Type[1] ≝
36 { acc_a_reg: Type[0] (* registers that will eventually need to be A *)
37 ; acc_b_reg: Type[0] (* registers that will eventually need to be B *)
38 ; acc_a_arg: Type[0] (* arguments that will eventually need to be A *)
39 ; acc_b_arg: Type[0] (* arguments that will eventually need to be B *)
40 ; dpl_reg: Type[0]   (* low address registers *)
41 ; dph_reg: Type[0]   (* high address registers *)
42 ; dpl_arg: Type[0]   (* low address registers *)
43 ; dph_arg: Type[0]   (* high address registers *)
44 ; snd_arg : Type[0]  (* second argument of binary op *)
45 ; pair_move: Type[0] (* argument of move instructions *)
46 ; call_args: Type[0] (* arguments of function calls *)
47 ; call_dest: Type[0] (* possible destination of function computation *)
48 ; ext_instruction: Type[0] (* other instructions not fitting in the general framework *)
49 ; ext_forall_labels : (label → Prop) → ext_instruction → Prop
50 ; ext_fin_instruction: Type[0] (* final instructions (e.g. tailcalls) not fitting in the general framework *)
51 ; ext_fin_forall_labels : (label → Prop) → ext_fin_instruction → Prop
52 }.
53 
54inductive joint_instruction (p:inst_params) (globals: list ident): Type[0] ≝
55  | COMMENT: String → joint_instruction p globals
56  | COST_LABEL: costlabel → joint_instruction p globals
57  | MOVE: pair_move p → joint_instruction p globals
58  | POP: acc_a_reg p → joint_instruction p globals
59  | PUSH: acc_a_arg p → joint_instruction p globals
60  | ADDRESS: ∀i: ident. (member i (eq_identifier ?) globals) → dpl_reg p → dph_reg p → joint_instruction p globals
61  | OPACCS: OpAccs → acc_a_reg p → acc_b_reg p → acc_a_arg p → acc_b_arg p → joint_instruction p globals
62  | OP1: Op1 → acc_a_reg p → acc_a_reg p → joint_instruction p globals
63  | OP2: Op2 → acc_a_reg p → acc_a_arg p → snd_arg p → joint_instruction p globals
64  (* int done with generic move *)
65(*| INT: generic_reg p → Byte → joint_instruction p globals *)
66  | CLEAR_CARRY: joint_instruction p globals
67  | SET_CARRY: joint_instruction p globals
68  | LOAD: acc_a_reg p → dpl_arg p → dph_arg p → joint_instruction p globals
69  | STORE: dpl_arg p → dph_arg p → acc_a_arg p → joint_instruction p globals
70  | CALL_ID: ident → call_args p → call_dest p → joint_instruction p globals
71  | COND: acc_a_reg p → label → joint_instruction p globals
72  | extension: ext_instruction p → joint_instruction p globals.
73
74(* Paolo: to be moved elsewhere *)
75
76notation "r ← a1 .op. a2" with precedence 50 for
77  @{'op2 $op $r $a1 $a2}.
78notation "r ← . op . a" with precedence 50 for
79  @{'op1 $op $r $a}.
80notation "r ← a" with precedence 50 for
81  @{'mov $r $a}. (* to be set in individual languages *)
82notation "❮r, s❯ ← a1 . op . a2" with precedence 50 for
83  @{'opaccs $op $r $s $a1 $a2}.
84
85interpretation "op2" 'op2 op r a1 a2 = (OP2 ? ? op r a1 a2).
86interpretation "op1" 'op1 op r a = (OP1 ? ? op r a).
87interpretation "opaccs" 'opaccs op r s a1 a2 = (OPACCS ? ? op r s a1 a2).
88
89
90definition inst_forall_labels : ∀p : inst_params.∀globals.
91    (label → Prop) → joint_instruction p globals → Prop ≝
92λp,g,P,inst. match inst with
93  [ COND _ l ⇒ P l
94  | extension ext_s ⇒ ext_forall_labels p P ext_s
95  | _ ⇒ True
96 ].
97
98record funct_params : Type[1] ≝
99  { resultT : Type[0]
100  ; paramsT : Type[0]
101  }.
102 
103record local_params : Type[1] ≝
104 { funct_pars :> funct_params
105 ; localsT: Type[0]
106 }.
107
108record unserialized_params : Type[1] ≝
109 { u_inst_pars :> inst_params
110 ; u_local_pars :> local_params
111 }.
112
113record stmt_params : Type[1] ≝
114  { unserialized_pars :> unserialized_params
115  ; succ : Type[0]
116  ; succ_forall_labels : (label → Prop) → succ → Prop
117  }.
118
119inductive joint_statement (p: stmt_params) (globals: list ident): Type[0] ≝
120  | sequential: joint_instruction p globals → succ p → joint_statement p globals
121  | GOTO: label → joint_statement p globals
122  | RETURN: joint_statement p globals
123  | extension_fin : ext_fin_instruction p → joint_statement p globals.
124
125record params : Type[1] ≝
126 { stmt_pars :> stmt_params
127 ; codeT: list ident → Type[0]
128 ; code_has_label: ∀globals.codeT globals → label → Prop
129 ; forall_statements : ∀globals.(joint_statement stmt_pars globals → Prop) →
130    codeT globals → Prop
131 }.
132
133
134definition stmt_forall_labels : ∀p : stmt_params.∀globals.
135    (label → Prop) → joint_statement p globals → Prop ≝
136  λp,g,P,stmt. match stmt with
137  [ sequential inst next ⇒ inst_forall_labels p g P inst ∧ succ_forall_labels p P next
138  | GOTO l ⇒ P l
139  | RETURN ⇒ True
140  | extension_fin c ⇒ ext_fin_forall_labels … P c
141  ].
142
143record lin_params : Type[1] ≝
144  { l_u_pars :> unserialized_params }.
145
146include "utilities/option.ma".
147
148definition lin_params_to_params ≝
149  λlp : lin_params.let stmt_pars ≝ (mk_stmt_params lp unit (λ_.λ_.True)) in
150     mk_params
151      stmt_pars
152    (* codeT ≝ *)(λglobals.list ((option label) × (joint_statement stmt_pars globals)))
153    (* code_has_label ≝ *)(λglobals,code,lbl.Exists ?
154        (λl_stmt. \fst l_stmt = Some ? lbl) code)
155    (* forall_statements ≝ *)(λglobals,P,code.All ?
156        (λl_stmt. P (\snd l_stmt)) code).
157
158coercion lp_to_p : ∀lp : lin_params.params ≝ lin_params_to_params
159  on _lp : lin_params to params.
160
161record graph_params : Type[1] ≝
162  { g_u_pars :> unserialized_params }.
163
164(* One common instantiation of params via Graphs of joint_statements
165   (all languages but LIN) *)
166definition graph_params_to_params ≝
167  λgp : graph_params.let stmt_pars ≝ mk_stmt_params gp label (λP.P) in
168     mk_params
169      stmt_pars
170    (* codeT ≝ *)(λglobals.graph (joint_statement stmt_pars globals))
171    (* code_has_label ≝ *)(λglobals,code,lbl.lookup … code lbl ≠ None ?)
172    (* forall_statements ≝ *)(λglobals,P,code.
173      ∀l,s.lookup … code l = Some ? s → P s).
174   
175coercion gp_to_p : ∀gp:graph_params.params ≝ graph_params_to_params
176on _gp : graph_params to params.
177   
178
179definition labels_present : ∀globals.∀p : params.
180  codeT p globals → (joint_statement p globals) → Prop ≝
181λglobals,p,code,s. stmt_forall_labels p globals
182  (λl.code_has_label ?? code l) s.
183
184definition code_closed : ∀p : params.∀globals.
185  codeT p globals → Prop ≝ λp,globals,code.
186    forall_statements … (labels_present … code) code.
187
188(* CSC: special case where localsT is a list of registers (RTL and ERTL) *)
189definition rtl_ertl_params : ?→?→params ≝ λinst_pars,funct_pars.
190  (mk_graph_params (mk_unserialized_params inst_pars (mk_local_params funct_pars (list register)))).
191
192record joint_internal_function (globals: list ident) (p:params) : Type[0] ≝
193{ joint_if_luniverse: universe LabelTag;    (*CSC: used only for compilation*)
194  joint_if_runiverse: universe RegisterTag; (*CSC: used only for compilation*)
195  (* Paolo: if we want this machinery to work for RTLabs too, we will need the
196     following, right? *)
197(*  joint_if_sig: signature;  -- dropped in front end *)
198  joint_if_result   : resultT p;
199  joint_if_params   : paramsT p;
200  joint_if_locals   : localsT p;
201(*CSC: XXXXX stacksize unused for LTL-...*)
202  joint_if_stacksize: nat;
203  joint_if_code     : codeT p globals ;
204(*CSC: XXXXX entry unused for LIN, but invariant in that case... *)
205  joint_if_entry    : Σl: label. code_has_label … joint_if_code l;
206(*CSC: XXXXX exit only used up to RTL (and only for tailcall removal) *)
207  joint_if_exit     : Σl: label. code_has_label …  … joint_if_code l
208}.
209
210definition joint_closed_internal_function ≝
211  λglobals,p.
212    Σdef : joint_internal_function globals p. code_closed … (joint_if_code … def).
213
214(* Currified form *)
215definition set_joint_if_exit ≝
216  λglobals,pars.
217  λexit: label.
218  λp:joint_internal_function globals pars.
219  λprf: code_has_label … (joint_if_code … p) exit.
220   mk_joint_internal_function globals pars
221    (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
222    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
223    (joint_if_code … p) (joint_if_entry … p) (mk_Sig … exit prf).
224
225definition set_joint_code ≝
226  λglobals: list ident.
227  λpars: params.
228  λint_fun: joint_internal_function globals pars.
229  λgraph: codeT pars globals.
230  λentry.
231  λexit.
232    mk_joint_internal_function globals pars
233      (joint_if_luniverse … int_fun) (joint_if_runiverse … int_fun) (joint_if_result … int_fun)
234      (joint_if_params … int_fun) (joint_if_locals … int_fun) (joint_if_stacksize … int_fun)
235      graph entry exit.
236
237definition set_joint_if_graph ≝
238  λglobals,pars.
239  λgraph.
240  λp:joint_internal_function globals pars.
241  λentry_prf: code_has_label … graph (joint_if_entry … p).
242  λexit_prf: code_has_label … graph (joint_if_exit … p).
243    set_joint_code globals pars p
244      graph
245      (mk_Sig … (joint_if_entry … p) entry_prf)
246      (mk_Sig … (joint_if_exit … p) exit_prf).
247
248definition set_luniverse ≝
249  λglobals,pars.
250  λp : joint_internal_function globals pars.
251  λluniverse: universe LabelTag.
252   mk_joint_internal_function globals pars
253    luniverse (joint_if_runiverse … p) (joint_if_result … p)
254    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
255    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
256
257definition set_runiverse ≝
258  λglobals,pars.
259  λp : joint_internal_function globals pars.
260  λruniverse: universe RegisterTag.
261   mk_joint_internal_function globals pars
262    (joint_if_luniverse … p) runiverse (joint_if_result … p)
263    (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
264    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
265   
266(* Paolo: probably should move these elsewhere *)
267definition graph_dom ≝ λX.λg : graph X.Σl : label.lookup … g l ≠ None ?.
268definition graph_dom_add_incl : ∀X.∀g : graph X.∀l : label.∀x : X.
269  graph_dom ? g → graph_dom ? (add … g l x) ≝ λX,g,l,x,sig_l.
270  mk_Sig … (pi1 ?? sig_l) ?.
271@graph_add_lookup
272@(pi2 … sig_l)
273qed.
274
275(* Specialized for graph_params *)
276definition add_graph ≝
277  λg_pars : graph_params.λglobals.λl:label.λstmt.
278    λp:joint_internal_function globals g_pars.
279   let code ≝ add … (joint_if_code … p) l stmt in
280    mk_joint_internal_function ? g_pars
281     (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
282     (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p)
283     code
284     (graph_dom_add_incl … (joint_if_entry … p))
285     (graph_dom_add_incl … (joint_if_exit … p)).
286
287definition set_locals ≝
288  λglobals,pars.
289  λp : joint_internal_function globals pars.
290  λlocals.
291   mk_joint_internal_function globals pars
292    (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
293    (joint_if_params … p) locals (joint_if_stacksize … p)
294    (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p).
295
296definition joint_function ≝ λp,globals. fundef (joint_internal_function p globals).
297
298definition joint_program ≝
299 λp:params. program (λglobals. joint_function globals p) nat.
Note: See TracBrowser for help on using the repository browser.