source: src/Cminor/toRTLabs.ma @ 887

Last change on this file since 887 was 887, checked in by campbell, 9 years ago

Start bringing RTLabs into line with the prototype compiler:

  • a coarse-grained type is associated with every register
  • the special addressing modes have been removed
File size: 12.3 KB
Line 
1include "utilities/lists.ma".
2include "common/Globalenvs.ma".
3include "Cminor/syntax.ma".
4include "RTLabs/syntax.ma".
5
6definition env ≝ identifier_map SymbolTag register.
7definition label_env ≝ identifier_map SymbolTag label.
8definition populate_env : env → universe RegisterTag → list (ident × typ) → res (list (register×typ) × env × (universe RegisterTag)) ≝
9λen,gen. foldr ??
10 (λidt,rsengen.
11   let 〈id,ty〉 ≝ idt in
12   do 〈rs,en,gen〉 ← rsengen;
13   do 〈r,gen'〉 ← fresh … gen;
14   OK ? 〈〈r,ty〉::rs, add ?? en id r, gen'〉) (OK ? 〈[ ], en, gen〉).
15
16definition populate_label_env : label_env → universe LabelTag → list ident → res (label_env × (universe LabelTag)) ≝
17λen,gen. foldr ??
18 (λid,engen.
19   do 〈en,gen〉 ← engen;
20   do 〈r,gen'〉 ← fresh … gen;
21   OK ? 〈add ?? en id r, gen'〉) (OK ? 〈en, gen〉).
22
23(* Add a statement to the graph, *without* updating the entry label. *)
24definition fill_in_statement : label → statement → internal_function → internal_function ≝
25λl,s,f.
26  mk_internal_function (f_labgen f) (f_reggen f)
27                       (f_result f) (f_params f) (f_locals f)
28                       (f_stacksize f) (add ?? (f_graph f) l s) (f_entry f) (f_exit f).
29
30(* Add a statement to the graph, making it the entry label. *)
31definition add_to_graph : label → statement → internal_function → internal_function ≝
32λl,s,f.
33  mk_internal_function (f_labgen f) (f_reggen f)
34                       (f_result f) (f_params f) (f_locals f)
35                       (f_stacksize f) (add ?? (f_graph f) l s) l (f_exit f).
36
37(* Add a statement with a fresh label to the start of the function.  The
38   statement is parametrised by the *next* instruction's label. *)
39definition add_fresh_to_graph : (label → statement) → internal_function → res internal_function ≝
40λs,f.
41  do 〈l,g〉 ← fresh … (f_labgen f);
42  let s' ≝ s (f_entry f) in
43  OK ? (mk_internal_function g (f_reggen f)
44                       (f_result f) (f_params f) (f_locals f)
45                       (f_stacksize f) (add ?? (f_graph f) l s') l (f_exit f)).
46
47(* Generate a fresh label and use it as a dangling entry point, to be filled in
48   later with the loop head. *)
49definition add_loop_label_to_graph : internal_function → res internal_function ≝
50λf.
51  do 〈l,g〉 ← fresh … (f_labgen f);
52  OK ? (mk_internal_function g (f_reggen f)
53                       (f_result f) (f_params f) (f_locals f)
54                       (f_stacksize f) (f_graph f) l (f_exit f)).
55
56definition fresh_reg : internal_function → typ → res (register × internal_function) ≝
57λf,ty.
58  do 〈r,g〉 ← fresh … (f_reggen f);
59  OK ? 〈r, mk_internal_function (f_labgen f) g
60                       (f_result f) (f_params f) (〈r,ty〉::(f_locals f))
61                       (f_stacksize f) (f_graph f) (f_entry f) (f_exit f)〉.
62
63axiom UnknownVariable : String.
64
65definition choose_reg : env → ∀ty.expr ty → internal_function → res (register × internal_function) ≝
66λenv,ty,e,f.
67  match e with
68  [ Id _ i ⇒
69      do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup … env i);
70      OK ? 〈r, f〉
71  | _ ⇒
72      fresh_reg f ty
73  ].
74
75definition choose_regs : env → list (Σt:typ.expr t) → internal_function → res (list register × internal_function) ≝
76λenv,es,f.
77  foldr (Σt:typ.expr t) ?
78    (λe,acc. do 〈rs,f〉 ← acc;
79             do 〈r,f'〉 ← match e with [ dp t e ⇒ choose_reg env t e f ];
80             OK ? 〈r::rs,f'〉) (OK ? 〈[ ], f〉) es.
81
82axiom BadCminorProgram : String.
83
84let rec add_expr (env:env) (ty:typ) (e:expr ty) (dst:register) (f:internal_function) on e: res internal_function ≝
85match e with
86[ Id _ i ⇒
87    do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup ?? env i);
88    match register_eq r dst with
89    [ inl _ ⇒ OK ? f
90    | inr _ ⇒ add_fresh_to_graph (St_op1 Oid dst r) f
91    ]
92| Cst _ c ⇒ add_fresh_to_graph (St_const dst c) f
93| Op1 _ _ op e' ⇒
94    do 〈r,f〉 ← choose_reg env ? e' f;
95    do f ← add_fresh_to_graph (St_op1 op dst r) f;
96    add_expr env ? e' r f
97| Op2 _ _ _ op e1 e2 ⇒
98    do 〈r1,f〉 ← choose_reg env ? e1 f;
99    do 〈r2,f〉 ← choose_reg env ? e2 f;
100    do f ← add_fresh_to_graph (St_op2 op dst r1 r2) f;
101    do f ← add_expr env ? e2 r2 f;
102    add_expr env ? e1 r1 f
103| Mem _ _ q e' ⇒
104    do 〈r,f〉 ← choose_reg env ? e' f;
105    do f ← add_fresh_to_graph (St_load q r dst) f;
106    add_expr env ? e' r f
107| Cond _ _ _ e' e1 e2 ⇒
108    let resume_at ≝ f_entry f in
109    do f ← add_expr env ? e2 dst f;
110    let lfalse ≝ f_entry f in
111    do f ← add_fresh_to_graph (λ_.St_skip resume_at) f;
112    do f ← add_expr env ? e1 dst f;
113    add_branch_internal env ? e' (f_entry f) lfalse f (add_expr env ? e')
114| Ecost _ l e' ⇒
115    do f ← add_expr env ? e' dst f;
116    add_fresh_to_graph (St_cost l) f
117   
118(* Ugh, the termination checker isn't smart enough to notice that calling
119   add_expr with e is OK, so we take it partially applied and define a proper
120   add_<whatever> afterwards. *)
121]
122and add_branch_internal (env:env) (ty:typ) (e:expr ty) (ltrue:label) (lfalse:label) (f:internal_function)
123        (termination_hack_add_expr : register → internal_function → res internal_function) on e : res internal_function ≝
124match e with
125[ Id _ i ⇒
126    do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup ?? env i);
127    add_fresh_to_graph (λ_. St_cond1 Oid r ltrue lfalse) f
128| Cst _ c ⇒
129    add_fresh_to_graph (λ_. St_condcst c ltrue lfalse) f
130| Op1 _ _ op e' ⇒
131    do 〈r,f〉 ← choose_reg env ? e' f;
132    do f ← add_fresh_to_graph (λ_. St_cond1 op r ltrue lfalse) f;
133    add_expr env ? e' r f
134| Op2 _ _ _ op e1 e2 ⇒
135    do 〈r1,f〉 ← choose_reg env ? e1 f;
136    do 〈r2,f〉 ← choose_reg env ? e2 f;
137    do f ← add_fresh_to_graph (λ_. St_cond2 op r1 r2 ltrue lfalse) f;
138    do f ← add_expr env ? e2 r2 f;
139    add_expr env ? e1 r1 f
140| _ ⇒
141    do 〈r,f〉 ← choose_reg env ? e f;
142    do f ← add_fresh_to_graph (λ_. St_cond1 Oid r ltrue lfalse) f;
143    termination_hack_add_expr r f
144].
145
146(* See comment above. *)
147definition add_branch ≝
148λenv,ty,e,ltrue,lfalse,f. add_branch_internal env ty e ltrue lfalse f (add_expr env ty e).
149
150(* This shouldn't occur; maybe use vectors? *)
151axiom WrongNumberOfArguments : String.
152
153let rec add_exprs (env:env) (es:list (Σt:typ.expr t)) (dsts:list register) (f:internal_function) on es: res internal_function ≝
154match es with
155[ nil ⇒ match dsts with [ nil ⇒ OK ? f | cons _ _ ⇒ Error ? (msg WrongNumberOfArguments) ]
156| cons e et ⇒
157    match dsts with
158    [ nil ⇒ Error ? (msg WrongNumberOfArguments)
159    | cons r rt ⇒
160        do f ← add_exprs env et rt f;
161        match e with [ dp ty e ⇒ add_expr env ty e r f ]
162    ]
163].
164
165axiom UnknownLabel : String.
166axiom ReturnMismatch : String.
167
168let rec add_stmt (env:env) (label_env:label_env) (s:stmt) (exits:list label) (f:internal_function) on s : res internal_function ≝
169match s with
170[ St_skip ⇒ OK ? f
171| St_assign _ x e ⇒
172    do dst ← opt_to_res … [MSG UnknownVariable; CTX ? x] (lookup ?? env x);
173    add_expr env ? e dst f
174| St_store _ _ q e1 e2 ⇒
175    do 〈val_reg, f〉 ← choose_reg env ? e2 f;
176    do 〈addr_reg, f〉 ← choose_reg env ? e1 f;
177    do f ← add_fresh_to_graph (St_store q addr_reg val_reg) f;
178    do f ← add_expr env ? e1 addr_reg f;
179    add_expr env ? e2 val_reg f
180| St_call return_opt_id e args ⇒
181    do return_opt_reg ←
182      match return_opt_id with
183      [ None ⇒ OK ? (None ?)
184      | Some id ⇒ do r ← opt_to_res … [MSG UnknownVariable; CTX ? id] (lookup ?? env id); OK ? (Some ? r)
185      ];
186    do 〈args_regs, f〉 ← choose_regs env args f;
187    do f ←
188      match e with
189      [ Id _ id ⇒ add_fresh_to_graph (St_call_id id args_regs return_opt_reg) f
190      | _ ⇒
191        do 〈fnr, f〉 ← choose_reg env ? e f;
192        do f ← add_fresh_to_graph (St_call_ptr fnr args_regs return_opt_reg) f;
193        add_expr env ? e fnr f
194      ];
195    add_exprs env args args_regs f
196| St_tailcall e args ⇒
197    do 〈args_regs, f〉 ← choose_regs env args f;
198    do f ←
199      match e with
200      [ Id _ id ⇒ add_fresh_to_graph (λ_. St_tailcall_id id args_regs) f
201      | _ ⇒
202        do 〈fnr, f〉 ← choose_reg env ? e f;
203        do f ← add_fresh_to_graph (λ_. St_tailcall_ptr fnr args_regs) f;
204        add_expr env ? e fnr f
205      ];
206    add_exprs env args args_regs f
207| St_seq s1 s2 ⇒
208    do f ← add_stmt env label_env s2 exits f;
209    add_stmt env label_env s1 exits f
210| St_ifthenelse _ _ e s1 s2 ⇒
211    let l_next ≝ f_entry f in
212    do f ← add_stmt env label_env s2 exits f;
213    let l2 ≝ f_entry f in
214    do f ← add_fresh_to_graph ? (* XXX: fails, but works if applied afterwards: λ_. St_skip l_next*) f;
215    do f ← add_stmt env label_env s1 exits f;
216    add_branch env ? e (f_entry f) l2 f
217| St_loop s ⇒
218    do f ← add_loop_label_to_graph f;
219    let l_loop ≝ f_entry f in
220    do f ← add_stmt env label_env s exits f;
221    OK ? (fill_in_statement l_loop (* XXX another odd failure: St_skip (f_entry f)*)? f)
222| St_block s ⇒
223    add_stmt env label_env s ((f_entry f)::exits) f
224| St_exit n ⇒
225    do l ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
226    add_fresh_to_graph (* XXX another: λ_. St_skip l*)? f
227| St_switch sz sg e tab n ⇒
228    do 〈r,f〉 ← choose_reg env ? e f;
229    do l_default ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
230    do f ← add_fresh_to_graph (* XXX grrrr: λ_. St_skip l_default*)? f;
231    do f ← foldr ?? (λcs,f.
232      do f ← f;
233      let 〈i,n〉 ≝ cs in
234      do 〈cr,f〉 ← fresh_reg … f (ASTint sz sg);
235      do l_case ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
236      do f ← add_fresh_to_graph (St_cond2 (Ocmpu Ceq) (* signed? *) r cr l_case) f;
237      add_fresh_to_graph (St_const cr (Ointconst i)) f) (OK ? f) tab;
238    add_expr env ? e r f
239| St_return opt_e ⇒
240    do f ← add_fresh_to_graph (λ_. St_return) f;
241    match opt_e with
242    [ None ⇒ OK ? f
243    | Some e ⇒
244        match f_result f with
245        [ None ⇒ Error ? (msg ReturnMismatch)
246        | Some r ⇒ match e with [ dp ty e ⇒ add_expr env ? e (\fst r) f ]
247        ]
248    ]
249| St_label l s' ⇒
250    do f ← add_stmt env label_env s' exits f;
251    do l' ← opt_to_res … [MSG UnknownLabel; CTX ? l] (lookup ?? label_env l);
252    OK ? (add_to_graph l' (* XXX again: St_skip (f_entry f)*)? f)
253| St_goto l ⇒
254    do l' ← opt_to_res … [MSG UnknownLabel; CTX ? l] (lookup ?? label_env l);
255    add_fresh_to_graph (* XXX again: λ_.St_skip l'*)? f
256| St_cost l s' ⇒
257    do f ← add_stmt env label_env s' exits f;
258    add_fresh_to_graph (St_cost l) f
259].
260[ @(λ_. St_skip l_next)
261| @(St_skip (f_entry f))
262| @(λ_. St_skip l)
263| @(λ_. St_skip l_default)
264| @(St_skip (f_entry f))
265| @(λ_.St_skip l')
266] qed.
267
268(* Get labels from a Cminor statement. *)
269let rec labels_of (s:stmt) : list ident ≝
270match s with
271[ St_seq s1 s2 ⇒ (labels_of s1) @ (labels_of s2)
272| St_ifthenelse _ _ _ s1 s2 ⇒ (labels_of s1) @ (labels_of s2)
273| St_loop s ⇒ labels_of s
274| St_block s ⇒ labels_of s
275| St_label l s ⇒ l::(labels_of s)
276| St_cost _ s ⇒ labels_of s
277| _ ⇒ [ ]
278].
279
280definition c2ra_function (*: internal_function → internal_function*) ≝
281λf.
282let labgen0 ≝ new_universe LabelTag in
283let reggen0 ≝ new_universe RegisterTag in
284let cminor_labels ≝ labels_of (f_body f) in
285do 〈params, env1, reggen1〉 ← populate_env (empty_map …) reggen0 (f_params f);
286do 〈locals0, env, reggen2〉 ← populate_env env1 reggen1 (f_vars f);
287do 〈result, locals, reggen〉 ←
288  match f_return f with
289  [ None ⇒ OK ? 〈None ?, locals0, reggen2〉
290  | Some ty ⇒
291      do 〈r,gen〉 ← fresh … reggen2;
292      OK ? 〈Some ? 〈r,ty〉, 〈r,ty〉::locals0, gen〉 ];
293do 〈label_env, labgen1〉 ← populate_label_env (empty_map …) labgen0 cminor_labels;
294do 〈l,labgen〉 ← fresh … labgen1;
295let emptyfn ≝
296  mk_internal_function
297    labgen
298    reggen
299    result
300    params
301    locals
302    (f_stacksize f)
303    (add ?? (empty_map …) l St_return)
304    l
305    l in
306  add_stmt env label_env (f_body f) [ ] emptyfn
307.
308
309definition cminor_to_rtlabs : Cminor_program → res RTLabs_program ≝
310transform_partial_program ???
311  (transf_partial_fundef ?? c2ra_function).
312
313include "Cminor/initialisation.ma".
314
315definition cminor_init_to_rtlabs : Cminor_program → res RTLabs_program ≝
316λp. let p' ≝ replace_init p in cminor_to_rtlabs p.
Note: See TracBrowser for help on using the repository browser.