source: src/Cminor/toRTLabs.ma @ 816

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

Clight to Cminor compilation, modulo switch statements, temporary
generation, 32 to 8 bit translation and miscellaneous bugs.

Also, remove (unused) signatures from function call statements in Cminor
and RTLabs; and separate comparison of integers and pointers in Clight
semantics.

File size: 15.2 KB
RevLine 
[766]1include "utilities/lists.ma".
[764]2include "common/Globalenvs.ma".
3include "Cminor/syntax.ma".
4include "RTLabs/syntax.ma".
5
6definition env ≝ identifier_map SymbolTag register.
[766]7definition label_env ≝ identifier_map SymbolTag label.
[764]8definition populate_env : env → universe RegisterTag → list ident → res (list register × env × (universe RegisterTag)) ≝
9λen,gen. foldr ??
10 (λid,rsengen.
11   do 〈rs,en,gen〉 ← rsengen;
12   do 〈r,gen'〉 ← fresh … gen;
13   OK ? 〈r::rs, add ?? en id r, gen'〉) (OK ? 〈[ ], en, gen〉).
14
[766]15definition populate_label_env : label_env → universe LabelTag → list ident → res (label_env × (universe LabelTag)) ≝
16λen,gen. foldr ??
17 (λid,engen.
18   do 〈en,gen〉 ← engen;
19   do 〈r,gen'〉 ← fresh … gen;
20   OK ? 〈add ?? en id r, gen'〉) (OK ? 〈en, gen〉).
[764]21
[766]22(* Add a statement to the graph, *without* updating the entry label. *)
23definition fill_in_statement : label → statement → internal_function → internal_function ≝
24λl,s,f.
25  mk_internal_function (f_labgen f) (f_reggen f) (f_sig f)
26                       (f_result f) (f_params f) (f_locals f) (f_ptrs f)
27                       (f_stacksize f) (add ?? (f_graph f) l s) (f_entry f) (f_exit f).
[764]28
[766]29(* Add a statement to the graph, making it the entry label. *)
30definition add_to_graph : label → statement → internal_function → internal_function ≝
31λl,s,f.
32  mk_internal_function (f_labgen f) (f_reggen f) (f_sig f)
33                       (f_result f) (f_params f) (f_locals f) (f_ptrs f)
34                       (f_stacksize f) (add ?? (f_graph f) l s) l (f_exit f).
[764]35
[766]36(* Add a statement with a fresh label to the start of the function.  The
37   statement is parametrised by the *next* instruction's label. *)
38definition add_fresh_to_graph : (label → statement) → internal_function → res internal_function ≝
39λs,f.
40  do 〈l,g〉 ← fresh … (f_labgen f);
41  let s' ≝ s (f_entry f) in
42  OK ? (mk_internal_function g (f_reggen f) (f_sig f)
43                       (f_result f) (f_params f) (f_locals f) (f_ptrs f)
44                       (f_stacksize f) (add ?? (f_graph f) l s') l (f_exit f)).
45
46(* Generate a fresh label and use it as a dangling entry point, to be filled in
47   later with the loop head. *)
48definition add_loop_label_to_graph : internal_function → res internal_function ≝
49λf.
50  do 〈l,g〉 ← fresh … (f_labgen f);
51  OK ? (mk_internal_function g (f_reggen f) (f_sig f)
52                       (f_result f) (f_params f) (f_locals f) (f_ptrs f)
53                       (f_stacksize f) (f_graph f) l (f_exit f)).
54
55definition fresh_reg : internal_function → res (register × internal_function) ≝
56λf.
57  do 〈r,g〉 ← fresh … (f_reggen f);
58  OK ? 〈r, mk_internal_function (f_labgen f) g (f_sig f)
59                       (f_result f) (f_params f) (r::(f_locals f)) (f_ptrs f)
60                       (f_stacksize f) (f_graph f) (f_entry f) (f_exit f)〉.
61
[780]62definition fresh_ptr_reg : internal_function → res (register × internal_function) ≝
63λf.
64  do 〈r,g〉 ← fresh … (f_reggen f);
65  OK ? 〈r, mk_internal_function (f_labgen f) g (f_sig f)
66                       (f_result f) (f_params f) (r::(f_locals f)) (r::(f_ptrs f))
67                       (f_stacksize f) (f_graph f) (f_entry f) (f_exit f)〉.
68
69let rec expr_yields_pointer (e:expr) (ptrs:list ident) : bool ≝
70match e with
71[ Id i ⇒ exists ? (eq_identifier ? i) ptrs
72| Cst c ⇒ match c with [ Oaddrsymbol _ _ ⇒ true | Oaddrstack _ ⇒ true | _ ⇒ false ]
73| Op1 op e' ⇒
74    match op with
75    [ Oid ⇒ expr_yields_pointer e' ptrs
76    | Optrofint _ ⇒ true
77    | _ ⇒ false
78    ]
79| Op2 op e1 e2 ⇒
80    match op with
81    [ Oaddp ⇒ true
82    | Osubpi ⇒ true
83    | _ ⇒ false
84    ]
85| Mem q e' ⇒
86    match q with
87    [ Mpointer _ ⇒ true
88    | _ ⇒ false
89    ]
90(* Both branches ought to be the same? *)
91| Cond e' e1 e2 ⇒ (expr_yields_pointer e1 ptrs) ∨ (expr_yields_pointer e2 ptrs)
92| Ecost _ e' ⇒ expr_yields_pointer e' ptrs
93].
94
[797]95axiom UnknownVariable : String.
96
[780]97definition choose_reg : env → expr → list ident → internal_function → res (register × internal_function) ≝
98λenv,e,ptrs,f.
[766]99  match e with
100  [ Id i ⇒
[797]101      do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup … env i);
[766]102      OK ? 〈r, f〉
[780]103  | _ ⇒
104      if expr_yields_pointer e ptrs then fresh_ptr_reg f else fresh_reg f
[766]105  ].
106
[780]107definition choose_regs : env → list expr → list ident → internal_function → res (list register × internal_function) ≝
108λenv,es,ptrs,f.
[766]109  foldr ?? (λe,acc. do 〈rs,f〉 ← acc;
[780]110                    do 〈r,f'〉 ← choose_reg env e ptrs f;
[766]111                    OK ? 〈r::rs,f'〉) (OK ? 〈[ ], f〉) es.
112
[797]113axiom BadCminorProgram : String.
114
[780]115let rec add_expr (env:env) (e:expr) (dst:register) (ptrs:list ident) (f:internal_function) on e: res internal_function ≝
[766]116match e with
117[ Id i ⇒
[797]118    do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup ?? env i);
[766]119    match register_eq r dst with
120    [ inl _ ⇒ OK ? f
121    | inr _ ⇒ add_fresh_to_graph (St_op1 Oid dst r) f
122    ]
123| Cst c ⇒ add_fresh_to_graph (St_const dst c) f
124| Op1 op e' ⇒
[780]125    do 〈r,f〉 ← choose_reg env e' ptrs f;
[767]126    do f ← add_fresh_to_graph (St_op1 op dst r) f;
[780]127    add_expr env e' r ptrs f
[766]128| Op2 op e1 e2 ⇒
[780]129    do 〈r1,f〉 ← choose_reg env e1 ptrs f;
130    do 〈r2,f〉 ← choose_reg env e2 ptrs f;
[767]131    do f ← add_fresh_to_graph (St_op2 op dst r1 r2) f;
[780]132    do f ← add_expr env e2 r2 ptrs f;
133    add_expr env e1 r1 ptrs f
[766]134| Mem q e' ⇒
[780]135    add_with_addressing_internal env e' (λm,rs. St_load q m rs dst) ptrs f (add_expr env e')
[766]136| Cond e' e1 e2 ⇒
137    let resume_at ≝ f_entry f in
[780]138    do f ← add_expr env e2 dst ptrs f;
[767]139    let lfalse ≝ f_entry f in
140    do f ← add_fresh_to_graph (λ_.St_skip resume_at) f;
[780]141    do f ← add_expr env e1 dst ptrs f;
142    add_branch_internal env e' (f_entry f) lfalse ptrs f (add_expr env e')
[766]143| Ecost l e' ⇒
[780]144    do f ← add_expr env e' dst ptrs f;
[767]145    add_fresh_to_graph (St_cost l) f
[766]146   
147(* Ugh, the termination checker isn't smart enough to notice that calling
148   add_expr with e is OK, so we take it partially applied and define a proper
[772]149   add_<whatever> afterwards. *)
150]
151and add_with_addressing_internal (env:env) (e:expr)
152                          (s:∀m:addressing. addr m → label → statement)
[780]153                          (ptrs:list ident)
[772]154                          (f:internal_function)
[780]155                          (termination_hack:register → list ident → internal_function → res internal_function)
[772]156                          on e : res internal_function ≝
157let add_default : unit → res internal_function ≝ λ_.
[780]158    do 〈r, f〉 ← choose_reg env e ptrs f;
[772]159    do f ← add_fresh_to_graph (s (Aindexed zero) [[ r ]]) f;
[780]160    termination_hack r ptrs f
[772]161in
162match e with
163[ Cst c ⇒
164    match c with
165    [ Oaddrsymbol id i ⇒ add_fresh_to_graph (s (Aglobal id i) [[ ]]) f
166    | Oaddrstack i ⇒ add_fresh_to_graph (s (Ainstack i) [[ ]]) f
[797]167    | _ ⇒ Error ? (msg BadCminorProgram) (* int and float constants are nonsense here *)
[772]168    ]
169| Op2 op e1 e2 ⇒
170    match op with
171    [ Oaddp ⇒
172        let add_generic_addp : unit → res internal_function ≝ λ_.
[780]173          do 〈r1, f〉 ← choose_reg env e1 ptrs f;
174          do 〈r2, f〉 ← choose_reg env e2 ptrs f;
[772]175          do f ← add_fresh_to_graph (s Aindexed2 [[ r1 ; r2 ]]) f;
[780]176          do f ← add_expr env e2 r2 ptrs f;
177          add_expr env e1 r1 ptrs f
[772]178        in
179        match e1 with
180        [ Cst c ⇒
181            match c with
182            [ Oaddrsymbol id i ⇒
[780]183                do 〈r, f〉 ← choose_reg env e ptrs f;
[772]184                do f ← add_fresh_to_graph (s (Abased id i) [[ r ]]) f;
[780]185                add_expr env e2 r ptrs f
[772]186            | _ ⇒ add_generic_addp it
187            ]
188        | _ ⇒ add_generic_addp it
189        ]
190    | _ ⇒ add_default it
191    ]
192| _ ⇒ add_default it
193]
194(* and again *)
[780]195and add_branch_internal (env:env) (e:expr) (ltrue:label) (lfalse:label) (ptrs:list ident) (f:internal_function)
196        (termination_hack_add_expr : register → list ident → internal_function → res internal_function) on e : res internal_function ≝
[766]197match e with
198[ Id i ⇒
[797]199    do r ← opt_to_res … [MSG UnknownVariable; CTX ? i] (lookup ?? env i);
[766]200    add_fresh_to_graph (λ_. St_cond1 Oid r ltrue lfalse) f
201| Cst c ⇒
202    add_fresh_to_graph (λ_. St_condcst c ltrue lfalse) f
203| Op1 op e' ⇒
[780]204    do 〈r,f〉 ← choose_reg env e' ptrs f;
[767]205    do f ← add_fresh_to_graph (λ_. St_cond1 op r ltrue lfalse) f;
[780]206    add_expr env e' r ptrs f
[766]207| Op2 op e1 e2 ⇒
[780]208    do 〈r1,f〉 ← choose_reg env e1 ptrs f;
209    do 〈r2,f〉 ← choose_reg env e2 ptrs f;
[767]210    do f ← add_fresh_to_graph (λ_. St_cond2 op r1 r2 ltrue lfalse) f;
[780]211    do f ← add_expr env e2 r2 ptrs f;
212    add_expr env e1 r1 ptrs f
[766]213| _ ⇒
[780]214    do 〈r,f〉 ← choose_reg env e ptrs f;
[767]215    do f ← add_fresh_to_graph (λ_. St_cond1 Oid r ltrue lfalse) f;
[780]216    termination_hack_add_expr r ptrs f
[766]217].
218
[772]219(* See comment above. *)
220definition add_with_addressing ≝
[780]221λenv,e,s,ptrs,f. add_with_addressing_internal env e s ptrs f (add_expr env e).
[766]222definition add_branch ≝
[780]223λenv,e,ltrue,lfalse,ptrs,f. add_branch_internal env e ltrue lfalse ptrs f (add_expr env e).
[766]224
[797]225(* This shouldn't occur; maybe use vectors? *)
226axiom WrongNumberOfArguments : String.
227
[780]228let rec add_exprs (env:env) (es:list expr) (dsts:list register) (ptrs:list ident) (f:internal_function) on es: res internal_function ≝
[766]229match es with
[797]230[ nil ⇒ match dsts with [ nil ⇒ OK ? f | cons _ _ ⇒ Error ? (msg WrongNumberOfArguments) ]
[766]231| cons e et ⇒
232    match dsts with
[797]233    [ nil ⇒ Error ? (msg WrongNumberOfArguments)
[766]234    | cons r rt ⇒
[780]235        do f ← add_exprs env et rt ptrs f;
236        add_expr env e r ptrs f
[766]237    ]
238].
239
[797]240axiom UnknownLabel : String.
241axiom ReturnMismatch : String.
242
[780]243let rec add_stmt (env:env) (label_env:label_env) (s:stmt) (exits:list label) (ptrs:list ident) (f:internal_function) on s : res internal_function ≝
[766]244match s with
245[ St_skip ⇒ OK ? f
246| St_assign x e ⇒
[797]247    do dst ← opt_to_res … [MSG UnknownVariable; CTX ? x] (lookup ?? env x);
[780]248    add_expr env e dst ptrs f
[766]249| St_store q e1 e2 ⇒
[780]250    do 〈val_reg, f〉 ← choose_reg env e2 ptrs f;
251    do f ← add_with_addressing env e1 (λm,rs. St_store q m rs val_reg) ptrs f;
252    add_expr env e2 val_reg ptrs f
[816]253| St_call return_opt_id e args ⇒
[766]254    do return_opt_reg ←
255      match return_opt_id with
256      [ None ⇒ OK ? (None ?)
[797]257      | Some id ⇒ do r ← opt_to_res … [MSG UnknownVariable; CTX ? id] (lookup ?? env id); OK ? (Some ? r)
[766]258      ];
[780]259    do 〈args_regs, f〉 ← choose_regs env args ptrs f;
[767]260    do f ←
[766]261      match e with
[816]262      [ Id id ⇒ add_fresh_to_graph (St_call_id id args_regs return_opt_reg) f
[766]263      | _ ⇒
[780]264        do 〈fnr, f〉 ← choose_reg env e ptrs f;
[816]265        do f ← add_fresh_to_graph (St_call_ptr fnr args_regs return_opt_reg) f;
[780]266        add_expr env e fnr ptrs f
[766]267      ];
[780]268    add_exprs env args args_regs ptrs f
[816]269| St_tailcall e args ⇒
[780]270    do 〈args_regs, f〉 ← choose_regs env args ptrs f;
[767]271    do f ←
[766]272      match e with
[816]273      [ Id id ⇒ add_fresh_to_graph (λ_. St_tailcall_id id args_regs) f
[766]274      | _ ⇒
[780]275        do 〈fnr, f〉 ← choose_reg env e ptrs f;
[816]276        do f ← add_fresh_to_graph (λ_. St_tailcall_ptr fnr args_regs) f;
[780]277        add_expr env e fnr ptrs f
[766]278      ];
[780]279    add_exprs env args args_regs ptrs f
[766]280| St_seq s1 s2 ⇒
[780]281    do f ← add_stmt env label_env s2 exits ptrs f;
282    add_stmt env label_env s1 exits ptrs f
[766]283| St_ifthenelse e s1 s2 ⇒
[767]284    let l_next ≝ f_entry f in
[780]285    do f ← add_stmt env label_env s2 exits ptrs f;
[766]286    let l2 ≝ f_entry f in
[767]287    do f ← add_fresh_to_graph ? (* XXX: fails, but works if applied afterwards: λ_. St_skip l_next*) f;
[780]288    do f ← add_stmt env label_env s1 exits ptrs f;
289    add_branch env e (f_entry f) l2 ptrs f
[766]290| St_loop s ⇒
[767]291    do f ← add_loop_label_to_graph f;
292    let l_loop ≝ f_entry f in
[780]293    do f ← add_stmt env label_env s exits ptrs f;
[767]294    OK ? (fill_in_statement l_loop (* XXX another odd failure: St_skip (f_entry f)*)? f)
[766]295| St_block s ⇒
[780]296    add_stmt env label_env s ((f_entry f)::exits) ptrs f
[766]297| St_exit n ⇒
[797]298    do l ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
[766]299    add_fresh_to_graph (* XXX another: λ_. St_skip l*)? f
[771]300| St_switch e tab n ⇒
[780]301    do 〈r,f〉 ← choose_reg env e ptrs f;
[797]302    do l_default ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
[771]303    do f ← add_fresh_to_graph (* XXX grrrr: λ_. St_skip l_default*)? f;
304    do f ← foldr ?? (λcs,f.
305      do f ← f;
306      let 〈i,n〉 ≝ cs in
307      do 〈cr,f〉 ← fresh_reg … f;
[797]308      do l_case ← opt_to_res … (msg BadCminorProgram) (nth_opt ? n exits);
[771]309      do f ← add_fresh_to_graph (St_cond2 (Ocmpu Ceq) (* signed? *) r cr l_case) f;
310      add_fresh_to_graph (St_const cr (Ointconst i)) f) (OK ? f) tab;
[780]311    add_expr env e r ptrs f
[766]312| St_return opt_e ⇒
[767]313    do f ← add_fresh_to_graph (λ_. St_return) f;
[766]314    match opt_e with
[767]315    [ None ⇒ OK ? f
[766]316    | Some e ⇒
317        match f_result f with
[797]318        [ None ⇒ Error ? (msg ReturnMismatch)
[780]319        | Some r ⇒ add_expr env e r ptrs f
[766]320        ]
321    ]
322| St_label l s' ⇒
[780]323    do f ← add_stmt env label_env s' exits ptrs f;
[797]324    do l' ← opt_to_res … [MSG UnknownLabel; CTX ? l] (lookup ?? label_env l);
[767]325    OK ? (add_to_graph l' (* XXX again: St_skip (f_entry f)*)? f)
[766]326| St_goto l ⇒
[797]327    do l' ← opt_to_res … [MSG UnknownLabel; CTX ? l] (lookup ?? label_env l);
[766]328    add_fresh_to_graph (* XXX again: λ_.St_skip l'*)? f
329| St_cost l s' ⇒
[780]330    do f ← add_stmt env label_env s' exits ptrs f;
[767]331    add_fresh_to_graph (St_cost l) f
[766]332].
[767]333[ @(λ_. St_skip l_next)
[766]334| @(St_skip (f_entry f))
335| @(λ_. St_skip l)
[771]336| @(λ_. St_skip l_default)
[767]337| @(St_skip (f_entry f))
[766]338| @(λ_.St_skip l')
339] qed.
340
341(* Get labels from a Cminor statement. *)
342let rec labels_of (s:stmt) : list ident ≝
343match s with
344[ St_seq s1 s2 ⇒ (labels_of s1) @ (labels_of s2)
345| St_ifthenelse _ s1 s2 ⇒ (labels_of s1) @ (labels_of s2)
346| St_loop s ⇒ labels_of s
347| St_block s ⇒ labels_of s
348| St_label l s ⇒ l::(labels_of s)
349| St_cost _ s ⇒ labels_of s
350| _ ⇒ [ ]
351].
352
[764]353definition c2ra_function (*: internal_function → internal_function*) ≝
354λf.
[766]355let labgen0 ≝ new_universe LabelTag in
[764]356let reggen0 ≝ new_universe RegisterTag in
[766]357let cminor_labels ≝ labels_of (f_body f) in
[764]358do 〈params, env1, reggen1〉 ← populate_env (empty_map …) reggen0 (f_params f);
[766]359do 〈locals0, env, reggen2〉 ← populate_env env1 reggen1 (f_vars f);
360do 〈result, locals, reggen〉 ←
361  match sig_res (f_sig f) with
362  [ None ⇒ OK ? 〈None ?, locals0, reggen2〉
363  | Some _ ⇒
364      do 〈r,gen〉 ← fresh … reggen2;
365      OK ? 〈Some ? r, r::locals0, gen〉 ];
[797]366do ptrs ← mmap ?? (λid. opt_to_res … [MSG UnknownVariable; CTX ? id] (lookup ?? env id)) (f_ptrs f);
[766]367do 〈label_env, labgen1〉 ← populate_label_env (empty_map …) labgen0 cminor_labels;
368do 〈l,labgen〉 ← fresh … labgen1;
369let emptyfn ≝
370  mk_internal_function
[764]371    labgen
[766]372    reggen
[764]373    (f_sig f)
[766]374    result
[764]375    params
376    locals
377    ptrs
378    (f_stacksize f)
[766]379    (add ?? (empty_map …) l St_return)
380    l
381    l in
[780]382  add_stmt env label_env (f_body f) [ ] (f_ptrs f) emptyfn
[766]383.
[764]384
385definition cminor_to_rtlabs : Cminor_program → res RTLabs_program ≝
386transform_partial_program ???
387  (transf_partial_fundef ?? c2ra_function).
[766]388
389include "Cminor/initialisation.ma".
390
391definition cminor_init_to_rtlabs : Cminor_program → res RTLabs_program ≝
392λp. let p' ≝ replace_init p in cminor_to_rtlabs p.
Note: See TracBrowser for help on using the repository browser.