[781] | 1 | include "Clight/Csyntax.ma". |
---|
| 2 | |
---|
[1056] | 3 | definition add_cost_before : statement → universe CostTag → statement × (universe CostTag) ≝ |
---|
[781] | 4 | λs,gen. |
---|
[1056] | 5 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 6 | 〈Scost l s, gen〉. |
---|
[781] | 7 | |
---|
[1056] | 8 | definition add_cost_after : statement → universe CostTag → statement × (universe CostTag) ≝ |
---|
[781] | 9 | λs,gen. |
---|
[1056] | 10 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 11 | 〈Ssequence s (Scost l Sskip), gen〉. |
---|
[781] | 12 | |
---|
[1056] | 13 | definition add_cost_expr : expr → universe CostTag → expr × (universe CostTag) ≝ |
---|
[781] | 14 | λe,gen. |
---|
[1056] | 15 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 16 | 〈Expr (Ecost l e) (typeof e), gen〉. |
---|
[781] | 17 | |
---|
| 18 | let rec label_expr (e:expr) (costgen:universe CostTag) |
---|
[1056] | 19 | on e : expr × (universe CostTag) ≝ |
---|
[781] | 20 | match e with |
---|
[1056] | 21 | [ Expr ed ty ⇒ let 〈ed,costgen〉 ≝ label_expr_descr ed costgen in |
---|
| 22 | 〈Expr ed ty, costgen〉 |
---|
[781] | 23 | ] |
---|
| 24 | and label_expr_descr (e:expr_descr) (costgen:universe CostTag) |
---|
[1056] | 25 | on e : expr_descr × (universe CostTag) ≝ |
---|
[781] | 26 | match e with |
---|
| 27 | [ Ederef e' ⇒ |
---|
[1056] | 28 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 29 | 〈Ederef e', costgen〉 |
---|
[781] | 30 | | Eaddrof e' ⇒ |
---|
[1056] | 31 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 32 | 〈Eaddrof e', costgen〉 |
---|
[781] | 33 | | Eunop op e' ⇒ |
---|
[1056] | 34 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 35 | 〈Eunop op e', costgen〉 |
---|
[781] | 36 | | Ebinop op e1 e2 ⇒ |
---|
[1056] | 37 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 38 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 39 | 〈Ebinop op e1 e2, costgen〉 |
---|
[781] | 40 | | Ecast ty e' ⇒ |
---|
[1056] | 41 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 42 | 〈Ecast ty e', costgen〉 |
---|
[781] | 43 | | Econdition e' e1 e2 ⇒ |
---|
[1056] | 44 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 45 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 46 | let 〈e1,costgen〉 ≝ add_cost_expr e1 costgen in |
---|
| 47 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 48 | let 〈e2,costgen〉 ≝ add_cost_expr e2 costgen in |
---|
| 49 | 〈Econdition e' e1 e2, costgen〉 |
---|
[781] | 50 | (* andbool and orbool are changed to conditionals to capture their |
---|
| 51 | short-circuiting cost difference *) |
---|
| 52 | | Eandbool e1 e2 ⇒ |
---|
[1056] | 53 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 54 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 55 | let 〈e2,costgen〉 ≝ add_cost_expr e2 costgen in |
---|
| 56 | let 〈ef,costgen〉 ≝ add_cost_expr (Expr (Econst_int I32 (zero ?)) (Tint I32 Signed)) costgen in |
---|
| 57 | 〈Econdition e1 e2 ef, costgen〉 |
---|
[781] | 58 | | Eorbool e1 e2 ⇒ |
---|
[1056] | 59 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 60 | let 〈et,costgen〉 ≝ add_cost_expr (Expr (Econst_int I32 (repr ? 1)) (Tint I32 Signed)) costgen in |
---|
| 61 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 62 | let 〈e2,costgen〉 ≝ add_cost_expr e2 costgen in |
---|
| 63 | 〈Econdition e1 et e2, costgen〉 |
---|
[781] | 64 | |
---|
| 65 | | Efield e' id ⇒ |
---|
[1056] | 66 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 67 | 〈Efield e' id, costgen〉 |
---|
[781] | 68 | (* The prototype asserts on preexisting cost labels, but I'd quite like to |
---|
| 69 | keep them. *) |
---|
| 70 | | Ecost l e' ⇒ |
---|
[1056] | 71 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 72 | 〈Ecost l e', costgen〉 |
---|
[781] | 73 | |
---|
| 74 | (* The remaining expressions don't have subexpressions. *) |
---|
[1056] | 75 | | _ ⇒ 〈e,costgen〉 |
---|
[781] | 76 | ]. |
---|
| 77 | |
---|
| 78 | let rec label_exprs (l:list expr) (costgen:universe CostTag) |
---|
[1056] | 79 | on l : list expr × (universe CostTag) ≝ |
---|
[781] | 80 | match l with |
---|
[1056] | 81 | [ nil ⇒ 〈nil ?,costgen〉 |
---|
[781] | 82 | | cons e es ⇒ |
---|
[1056] | 83 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 84 | let 〈es,costgen〉 ≝ label_exprs es costgen in |
---|
| 85 | 〈e::es,costgen〉 |
---|
[781] | 86 | ]. |
---|
| 87 | |
---|
| 88 | definition label_opt_expr ≝ |
---|
| 89 | λoe,costgen. match oe with |
---|
[1056] | 90 | [ None ⇒ 〈None ?, costgen〉 |
---|
| 91 | | Some e ⇒ let 〈e,costgen〉 ≝ label_expr e costgen in 〈Some ? e,costgen〉 |
---|
[781] | 92 | ]. |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | let rec label_statement (s:statement) (costgen:universe CostTag) |
---|
[1056] | 96 | on s : statement × (universe CostTag) ≝ |
---|
[781] | 97 | match s with |
---|
[1056] | 98 | [ Sskip ⇒ 〈Sskip,costgen〉 |
---|
[781] | 99 | | Sassign e1 e2 ⇒ |
---|
[1056] | 100 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 101 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 102 | 〈Sassign e1 e2, costgen〉 |
---|
[781] | 103 | | Scall e_ret e_fn e_args ⇒ |
---|
[1056] | 104 | let 〈e_ret,costgen〉 ≝ label_opt_expr e_ret costgen in |
---|
| 105 | let 〈e_fn,costgen〉 ≝ label_expr e_fn costgen in |
---|
| 106 | let 〈e_args,costgen〉 ≝ label_exprs e_args costgen in |
---|
| 107 | 〈Scall e_ret e_fn e_args, costgen〉 |
---|
[781] | 108 | | Ssequence s1 s2 ⇒ |
---|
[1056] | 109 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 110 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 111 | 〈Ssequence s1 s2, costgen〉 |
---|
[781] | 112 | | Sifthenelse e s1 s2 ⇒ |
---|
[1056] | 113 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 114 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 115 | let 〈s1,costgen〉 ≝ add_cost_before s1 costgen in |
---|
| 116 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 117 | let 〈s2,costgen〉 ≝ add_cost_before s2 costgen in |
---|
| 118 | 〈Sifthenelse e s1 s2, costgen〉 |
---|
[781] | 119 | | Swhile e s' ⇒ |
---|
[1056] | 120 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 121 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 122 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
| 123 | let 〈s,costgen〉 ≝ add_cost_after (Swhile e s') costgen in |
---|
| 124 | 〈s,costgen〉 |
---|
[781] | 125 | | Sdowhile e s' ⇒ |
---|
[1056] | 126 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 127 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 128 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
| 129 | let 〈s,costgen〉 ≝ add_cost_after (Sdowhile e s') costgen in |
---|
| 130 | 〈s,costgen〉 |
---|
[781] | 131 | | Sfor s1 e s2 s3 ⇒ |
---|
[1056] | 132 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 133 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 134 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 135 | let 〈s3,costgen〉 ≝ label_statement s3 costgen in |
---|
| 136 | let 〈s3,costgen〉 ≝ add_cost_before s3 costgen in |
---|
| 137 | let 〈s,costgen〉 ≝ add_cost_after (Sfor s1 e s2 s3) costgen in |
---|
| 138 | 〈s,costgen〉 |
---|
| 139 | | Sbreak ⇒ 〈Sbreak,costgen〉 |
---|
| 140 | | Scontinue ⇒ 〈Scontinue,costgen〉 |
---|
[781] | 141 | | Sreturn opt_e ⇒ |
---|
[1056] | 142 | let 〈opt_e,costgen〉 ≝ label_opt_expr opt_e costgen in |
---|
| 143 | 〈Sreturn opt_e,costgen〉 |
---|
[781] | 144 | | Sswitch e ls ⇒ |
---|
[1056] | 145 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 146 | let 〈ls,costgen〉 ≝ label_lstatements ls costgen in |
---|
| 147 | 〈Sswitch e ls, costgen〉 |
---|
[781] | 148 | | Slabel l s' ⇒ |
---|
[1056] | 149 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 150 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
| 151 | 〈Slabel l s', costgen〉 |
---|
| 152 | | Sgoto l ⇒ 〈Sgoto l, costgen〉 |
---|
[781] | 153 | |
---|
| 154 | (* The prototype asserts on preexisting cost labels, but I'd quite like to |
---|
| 155 | keep them. *) |
---|
| 156 | | Scost l s' ⇒ |
---|
[1056] | 157 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 158 | 〈Scost l s', costgen〉 |
---|
[781] | 159 | ] |
---|
| 160 | and label_lstatements (ls:labeled_statements) (costgen:universe CostTag) |
---|
[1056] | 161 | on ls : labeled_statements × (universe CostTag) ≝ |
---|
[781] | 162 | match ls with |
---|
| 163 | [ LSdefault s ⇒ |
---|
[1056] | 164 | let 〈s,costgen〉 ≝ label_statement s costgen in |
---|
| 165 | let 〈s,costgen〉 ≝ add_cost_before s costgen in |
---|
| 166 | 〈LSdefault s, costgen〉 |
---|
[961] | 167 | | LScase sz i s ls' ⇒ |
---|
[1056] | 168 | let 〈s,costgen〉 ≝ label_statement s costgen in |
---|
| 169 | let 〈s,costgen〉 ≝ add_cost_before s costgen in |
---|
| 170 | let 〈ls',costgen〉 ≝ label_lstatements ls' costgen in |
---|
| 171 | 〈LScase sz i s ls', costgen〉 |
---|
[781] | 172 | ]. |
---|
| 173 | |
---|
[1515] | 174 | definition label_function : function → function ≝ |
---|
[781] | 175 | λf. |
---|
| 176 | let costgen ≝ new_universe CostTag in |
---|
[1056] | 177 | let 〈body,costgen〉 ≝ label_statement (fn_body f) costgen in |
---|
| 178 | let 〈body,costgen〉 ≝ add_cost_before body costgen in |
---|
[1515] | 179 | mk_function (fn_return f) (fn_params f) (fn_vars f) body. |
---|
[781] | 180 | |
---|
[1515] | 181 | definition label_fundef : clight_fundef → clight_fundef ≝ |
---|
[781] | 182 | λf. match f with |
---|
[1515] | 183 | [ CL_Internal f ⇒ CL_Internal (label_function f) |
---|
| 184 | | CL_External id args ty ⇒ CL_External id args ty |
---|
[781] | 185 | ]. |
---|
| 186 | |
---|
[1515] | 187 | definition clight_label : clight_program → clight_program ≝ |
---|
| 188 | λp. transform_program … p label_fundef. |
---|