[781] | 1 | include "Clight/Csyntax.ma". |
---|
| 2 | |
---|
[2399] | 3 | (* Extract cost labels from a program. *) |
---|
[2353] | 4 | |
---|
[2399] | 5 | let rec labels_of_expr (e:expr) : list costlabel ≝ |
---|
[2353] | 6 | match e with |
---|
| 7 | [ Expr e' _ ⇒ |
---|
| 8 | match e' with |
---|
[2399] | 9 | [ Ederef e1 ⇒ labels_of_expr e1 |
---|
| 10 | | Eaddrof e1 ⇒ labels_of_expr e1 |
---|
| 11 | | Eunop _ e1 ⇒ labels_of_expr e1 |
---|
| 12 | | Ebinop _ e1 e2 ⇒ labels_of_expr e1 @ labels_of_expr e2 |
---|
| 13 | | Ecast _ e1 ⇒ labels_of_expr e1 |
---|
| 14 | | Econdition e1 e2 e3 ⇒ labels_of_expr e1 @ labels_of_expr e2 @ labels_of_expr e3 |
---|
| 15 | | Eandbool e1 e2 ⇒ labels_of_expr e1 @ labels_of_expr e2 |
---|
| 16 | | Eorbool e1 e2 ⇒ labels_of_expr e1 @ labels_of_expr e2 |
---|
| 17 | | Efield e1 _ ⇒ labels_of_expr e1 |
---|
| 18 | | Ecost cl e1 ⇒ cl::(labels_of_expr e1) |
---|
| 19 | | _ ⇒ [ ] |
---|
[2353] | 20 | ] |
---|
| 21 | ]. |
---|
| 22 | |
---|
[2399] | 23 | let rec labels_of_statement (s:statement) : list costlabel ≝ |
---|
[2353] | 24 | match s with |
---|
[2399] | 25 | [ Sassign e1 e2 ⇒ labels_of_expr e1 @ labels_of_expr e2 |
---|
| 26 | | Scall oe e1 es ⇒ option_map_def … labels_of_expr [ ] oe @ labels_of_expr e1 @ foldl … (λls,e. labels_of_expr e @ ls) [ ] es |
---|
| 27 | | Ssequence s1 s2 ⇒ labels_of_statement s1 @ labels_of_statement s2 |
---|
| 28 | | Sifthenelse e1 s1 s2 ⇒ labels_of_expr e1 @ labels_of_statement s1 @ labels_of_statement s2 |
---|
| 29 | | Swhile e1 s1 ⇒ labels_of_expr e1 @ labels_of_statement s1 |
---|
| 30 | | Sdowhile e1 s1 ⇒ labels_of_expr e1 @ labels_of_statement s1 |
---|
| 31 | | Sfor s1 e1 s2 s3 ⇒ labels_of_statement s1 @ labels_of_expr e1 @ labels_of_statement s2 @ labels_of_statement s3 |
---|
| 32 | | Sreturn oe ⇒ option_map_def … labels_of_expr [ ] oe |
---|
| 33 | | Sswitch e1 ls ⇒ labels_of_expr e1 @ labels_of_labeled_statements ls |
---|
| 34 | | Slabel _ s1 ⇒ labels_of_statement s1 |
---|
| 35 | | Scost cl s1 ⇒ cl::(labels_of_statement s1) |
---|
| 36 | | _ ⇒ [ ] |
---|
| 37 | ] and labels_of_labeled_statements (ls:labeled_statements) : list costlabel ≝ |
---|
[2353] | 38 | match ls with |
---|
[2399] | 39 | [ LSdefault s1 ⇒ labels_of_statement s1 |
---|
| 40 | | LScase _ _ s1 ls1 ⇒ labels_of_statement s1 @ labels_of_labeled_statements ls1 |
---|
[2353] | 41 | ]. |
---|
| 42 | |
---|
[2399] | 43 | definition labels_of_clight_fundef : ident × clight_fundef → list costlabel ≝ |
---|
[2353] | 44 | λifd. match \snd ifd with |
---|
[2399] | 45 | [ CL_Internal f ⇒ labels_of_statement (fn_body f) |
---|
| 46 | | _ ⇒ [ ] |
---|
[2353] | 47 | ]. |
---|
| 48 | |
---|
[2399] | 49 | definition labels_of_clight : clight_program → list costlabel ≝ |
---|
| 50 | λp. foldl … (λls,f. labels_of_clight_fundef f @ ls) [ ] (prog_funct ?? p). |
---|
| 51 | |
---|
| 52 | definition in_clight_program : clight_program → costlabel → Prop ≝ |
---|
| 53 | λp,l. Exists … (λx.x=l) (labels_of_clight p). |
---|
| 54 | |
---|
[2505] | 55 | definition in_clight_label ≝ |
---|
| 56 | λp. Σl. in_clight_program p l. |
---|
| 57 | |
---|
| 58 | definition clight_cost_map ≝ |
---|
| 59 | λp. (in_clight_label p) → ℕ. |
---|
| 60 | |
---|
[2353] | 61 | definition clight_label_free : clight_program → bool ≝ |
---|
[2399] | 62 | λp. match labels_of_clight p with [ nil ⇒ true | _ ⇒ false ]. |
---|
[2353] | 63 | |
---|
| 64 | (* Adding labels *) |
---|
| 65 | |
---|
[1056] | 66 | definition add_cost_before : statement → universe CostTag → statement × (universe CostTag) ≝ |
---|
[781] | 67 | λs,gen. |
---|
[1056] | 68 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 69 | 〈Scost l s, gen〉. |
---|
[781] | 70 | |
---|
[1056] | 71 | definition add_cost_after : statement → universe CostTag → statement × (universe CostTag) ≝ |
---|
[781] | 72 | λs,gen. |
---|
[1056] | 73 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 74 | 〈Ssequence s (Scost l Sskip), gen〉. |
---|
[781] | 75 | |
---|
[1056] | 76 | definition add_cost_expr : expr → universe CostTag → expr × (universe CostTag) ≝ |
---|
[781] | 77 | λe,gen. |
---|
[1056] | 78 | let 〈l,gen〉 ≝ fresh … gen in |
---|
| 79 | 〈Expr (Ecost l e) (typeof e), gen〉. |
---|
[781] | 80 | |
---|
[2588] | 81 | definition const_int : intsize → nat → expr ≝ |
---|
| 82 | λsz,n. Expr (Econst_int ? (repr sz n)) (Tint sz Signed). |
---|
[1888] | 83 | |
---|
[781] | 84 | let rec label_expr (e:expr) (costgen:universe CostTag) |
---|
[1056] | 85 | on e : expr × (universe CostTag) ≝ |
---|
[781] | 86 | match e with |
---|
[1888] | 87 | [ Expr ed ty ⇒ let 〈ed,costgen〉 ≝ label_expr_descr ed costgen ty in |
---|
[1056] | 88 | 〈Expr ed ty, costgen〉 |
---|
[781] | 89 | ] |
---|
[1888] | 90 | and label_expr_descr (e:expr_descr) (costgen:universe CostTag) (ty:type) |
---|
[1056] | 91 | on e : expr_descr × (universe CostTag) ≝ |
---|
[781] | 92 | match e with |
---|
| 93 | [ Ederef e' ⇒ |
---|
[1056] | 94 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 95 | 〈Ederef e', costgen〉 |
---|
[781] | 96 | | Eaddrof e' ⇒ |
---|
[1056] | 97 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 98 | 〈Eaddrof e', costgen〉 |
---|
[781] | 99 | | Eunop op e' ⇒ |
---|
[1056] | 100 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 101 | 〈Eunop op e', costgen〉 |
---|
[781] | 102 | | Ebinop op e1 e2 ⇒ |
---|
[1056] | 103 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 104 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 105 | 〈Ebinop op e1 e2, costgen〉 |
---|
[781] | 106 | | Ecast ty e' ⇒ |
---|
[1056] | 107 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 108 | 〈Ecast ty e', costgen〉 |
---|
[781] | 109 | | Econdition e' e1 e2 ⇒ |
---|
[1056] | 110 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 111 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 112 | let 〈e1,costgen〉 ≝ add_cost_expr e1 costgen in |
---|
| 113 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 114 | let 〈e2,costgen〉 ≝ add_cost_expr e2 costgen in |
---|
| 115 | 〈Econdition e' e1 e2, costgen〉 |
---|
[781] | 116 | (* andbool and orbool are changed to conditionals to capture their |
---|
[2392] | 117 | short-circuiting cost difference; note that we have to return 0 or 1, and |
---|
| 118 | we get rather more cost labels than I'd like *) |
---|
[781] | 119 | | Eandbool e1 e2 ⇒ |
---|
[1056] | 120 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
[2588] | 121 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 122 | match ty with |
---|
| 123 | [ Tint sz sg ⇒ |
---|
| 124 | let 〈et,costgen〉 ≝ add_cost_expr (const_int sz 1) costgen in |
---|
| 125 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int sz 0) costgen in |
---|
| 126 | let 〈e2,costgen〉 ≝ add_cost_expr (Expr (Econdition e2 et ef) ty) costgen in |
---|
| 127 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int sz 0) costgen in |
---|
| 128 | 〈Econdition e1 e2 ef, costgen〉 |
---|
| 129 | | _ ⇒ (* default on 32 bit consts if inconsistent type. *) |
---|
| 130 | let 〈et,costgen〉 ≝ add_cost_expr (const_int I32 1) costgen in |
---|
| 131 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int I32 0) costgen in |
---|
| 132 | let 〈e2,costgen〉 ≝ add_cost_expr (Expr (Econdition e2 et ef) ty) costgen in |
---|
| 133 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int I32 0) costgen in |
---|
| 134 | 〈Econdition e1 e2 ef, costgen〉 |
---|
| 135 | ] |
---|
[781] | 136 | | Eorbool e1 e2 ⇒ |
---|
[1056] | 137 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
[2392] | 138 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
[2588] | 139 | match ty with |
---|
| 140 | [ Tint sz sg ⇒ |
---|
| 141 | let 〈et,costgen〉 ≝ add_cost_expr (const_int sz 1) costgen in |
---|
| 142 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int sz 0) costgen in |
---|
| 143 | let 〈e2,costgen〉 ≝ add_cost_expr (Expr (Econdition e2 et ef) ty) costgen in |
---|
| 144 | let 〈et,costgen〉 ≝ add_cost_expr (const_int sz 1) costgen in |
---|
| 145 | 〈Econdition e1 et e2, costgen〉 |
---|
| 146 | | _ ⇒ |
---|
| 147 | let 〈et,costgen〉 ≝ add_cost_expr (const_int I32 1) costgen in |
---|
| 148 | let 〈ef,costgen〉 ≝ add_cost_expr (const_int I32 0) costgen in |
---|
| 149 | let 〈e2,costgen〉 ≝ add_cost_expr (Expr (Econdition e2 et ef) ty) costgen in |
---|
| 150 | let 〈et,costgen〉 ≝ add_cost_expr (const_int I32 1) costgen in |
---|
| 151 | 〈Econdition e1 et e2, costgen〉 |
---|
| 152 | ] |
---|
[781] | 153 | | Efield e' id ⇒ |
---|
[1056] | 154 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 155 | 〈Efield e' id, costgen〉 |
---|
[781] | 156 | (* The prototype asserts on preexisting cost labels, but I'd quite like to |
---|
| 157 | keep them. *) |
---|
| 158 | | Ecost l e' ⇒ |
---|
[1056] | 159 | let 〈e',costgen〉 ≝ label_expr e' costgen in |
---|
| 160 | 〈Ecost l e', costgen〉 |
---|
[781] | 161 | |
---|
| 162 | (* The remaining expressions don't have subexpressions. *) |
---|
[1056] | 163 | | _ ⇒ 〈e,costgen〉 |
---|
[781] | 164 | ]. |
---|
| 165 | |
---|
| 166 | let rec label_exprs (l:list expr) (costgen:universe CostTag) |
---|
[1056] | 167 | on l : list expr × (universe CostTag) ≝ |
---|
[781] | 168 | match l with |
---|
[1056] | 169 | [ nil ⇒ 〈nil ?,costgen〉 |
---|
[781] | 170 | | cons e es ⇒ |
---|
[1056] | 171 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 172 | let 〈es,costgen〉 ≝ label_exprs es costgen in |
---|
| 173 | 〈e::es,costgen〉 |
---|
[781] | 174 | ]. |
---|
| 175 | |
---|
| 176 | definition label_opt_expr ≝ |
---|
| 177 | λoe,costgen. match oe with |
---|
[1056] | 178 | [ None ⇒ 〈None ?, costgen〉 |
---|
| 179 | | Some e ⇒ let 〈e,costgen〉 ≝ label_expr e costgen in 〈Some ? e,costgen〉 |
---|
[781] | 180 | ]. |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | let rec label_statement (s:statement) (costgen:universe CostTag) |
---|
[1056] | 184 | on s : statement × (universe CostTag) ≝ |
---|
[781] | 185 | match s with |
---|
[1056] | 186 | [ Sskip ⇒ 〈Sskip,costgen〉 |
---|
[781] | 187 | | Sassign e1 e2 ⇒ |
---|
[1056] | 188 | let 〈e1,costgen〉 ≝ label_expr e1 costgen in |
---|
| 189 | let 〈e2,costgen〉 ≝ label_expr e2 costgen in |
---|
| 190 | 〈Sassign e1 e2, costgen〉 |
---|
[781] | 191 | | Scall e_ret e_fn e_args ⇒ |
---|
[1056] | 192 | let 〈e_ret,costgen〉 ≝ label_opt_expr e_ret costgen in |
---|
| 193 | let 〈e_fn,costgen〉 ≝ label_expr e_fn costgen in |
---|
| 194 | let 〈e_args,costgen〉 ≝ label_exprs e_args costgen in |
---|
| 195 | 〈Scall e_ret e_fn e_args, costgen〉 |
---|
[781] | 196 | | Ssequence s1 s2 ⇒ |
---|
[1056] | 197 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 198 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 199 | 〈Ssequence s1 s2, costgen〉 |
---|
[781] | 200 | | Sifthenelse e s1 s2 ⇒ |
---|
[1056] | 201 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 202 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 203 | let 〈s1,costgen〉 ≝ add_cost_before s1 costgen in |
---|
| 204 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 205 | let 〈s2,costgen〉 ≝ add_cost_before s2 costgen in |
---|
| 206 | 〈Sifthenelse e s1 s2, costgen〉 |
---|
[2391] | 207 | | Swhile e s' ⇒ |
---|
[1056] | 208 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 209 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 210 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
[2391] | 211 | let 〈s,costgen〉 ≝ add_cost_after (Swhile e s') costgen in |
---|
| 212 | 〈s,costgen〉 |
---|
[781] | 213 | | Sdowhile e s' ⇒ |
---|
[1056] | 214 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 215 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 216 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
| 217 | let 〈s,costgen〉 ≝ add_cost_after (Sdowhile e s') costgen in |
---|
| 218 | 〈s,costgen〉 |
---|
[781] | 219 | | Sfor s1 e s2 s3 ⇒ |
---|
[1056] | 220 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 221 | let 〈s1,costgen〉 ≝ label_statement s1 costgen in |
---|
| 222 | let 〈s2,costgen〉 ≝ label_statement s2 costgen in |
---|
| 223 | let 〈s3,costgen〉 ≝ label_statement s3 costgen in |
---|
| 224 | let 〈s3,costgen〉 ≝ add_cost_before s3 costgen in |
---|
| 225 | let 〈s,costgen〉 ≝ add_cost_after (Sfor s1 e s2 s3) costgen in |
---|
| 226 | 〈s,costgen〉 |
---|
| 227 | | Sbreak ⇒ 〈Sbreak,costgen〉 |
---|
| 228 | | Scontinue ⇒ 〈Scontinue,costgen〉 |
---|
[781] | 229 | | Sreturn opt_e ⇒ |
---|
[1056] | 230 | let 〈opt_e,costgen〉 ≝ label_opt_expr opt_e costgen in |
---|
| 231 | 〈Sreturn opt_e,costgen〉 |
---|
[781] | 232 | | Sswitch e ls ⇒ |
---|
[1056] | 233 | let 〈e,costgen〉 ≝ label_expr e costgen in |
---|
| 234 | let 〈ls,costgen〉 ≝ label_lstatements ls costgen in |
---|
| 235 | 〈Sswitch e ls, costgen〉 |
---|
[781] | 236 | | Slabel l s' ⇒ |
---|
[1056] | 237 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 238 | let 〈s',costgen〉 ≝ add_cost_before s' costgen in |
---|
| 239 | 〈Slabel l s', costgen〉 |
---|
| 240 | | Sgoto l ⇒ 〈Sgoto l, costgen〉 |
---|
[781] | 241 | |
---|
| 242 | (* The prototype asserts on preexisting cost labels, but I'd quite like to |
---|
| 243 | keep them. *) |
---|
| 244 | | Scost l s' ⇒ |
---|
[1056] | 245 | let 〈s',costgen〉 ≝ label_statement s' costgen in |
---|
| 246 | 〈Scost l s', costgen〉 |
---|
[781] | 247 | ] |
---|
| 248 | and label_lstatements (ls:labeled_statements) (costgen:universe CostTag) |
---|
[1056] | 249 | on ls : labeled_statements × (universe CostTag) ≝ |
---|
[781] | 250 | match ls with |
---|
| 251 | [ LSdefault s ⇒ |
---|
[1056] | 252 | let 〈s,costgen〉 ≝ label_statement s costgen in |
---|
| 253 | let 〈s,costgen〉 ≝ add_cost_before s costgen in |
---|
| 254 | 〈LSdefault s, costgen〉 |
---|
[961] | 255 | | LScase sz i s ls' ⇒ |
---|
[1056] | 256 | let 〈s,costgen〉 ≝ label_statement s costgen in |
---|
| 257 | let 〈s,costgen〉 ≝ add_cost_before s costgen in |
---|
| 258 | let 〈ls',costgen〉 ≝ label_lstatements ls' costgen in |
---|
| 259 | 〈LScase sz i s ls', costgen〉 |
---|
[781] | 260 | ]. |
---|
| 261 | |
---|
[2319] | 262 | definition label_function : universe CostTag → function → function × (universe CostTag) ≝ |
---|
| 263 | λcostgen,f. |
---|
[1056] | 264 | let 〈body,costgen〉 ≝ label_statement (fn_body f) costgen in |
---|
| 265 | let 〈body,costgen〉 ≝ add_cost_before body costgen in |
---|
[2319] | 266 | 〈mk_function (fn_return f) (fn_params f) (fn_vars f) body, costgen〉. |
---|
[781] | 267 | |
---|
[2319] | 268 | definition label_fundef : universe CostTag → clight_fundef → clight_fundef × (universe CostTag) ≝ |
---|
| 269 | λgen,f. match f with |
---|
| 270 | [ CL_Internal f ⇒ |
---|
| 271 | let 〈f',gen'〉 ≝ label_function gen f in |
---|
| 272 | 〈CL_Internal f', gen'〉 |
---|
| 273 | | CL_External id args ty ⇒ 〈CL_External id args ty, gen〉 |
---|
[781] | 274 | ]. |
---|
| 275 | |
---|
[2319] | 276 | |
---|
| 277 | |
---|
| 278 | definition clight_label : clight_program → clight_program × costlabel ≝ |
---|
| 279 | λp. |
---|
| 280 | let costgen ≝ new_universe CostTag in |
---|
| 281 | let 〈init_cost, costgen〉 ≝ fresh … costgen in |
---|
| 282 | 〈\fst (transform_program_gen … costgen p (λ_.label_fundef)), init_cost〉. |
---|