Skip to content
This repository
tree: f008678a2b
Fetching contributors…

Cannot retrieve contributors at this time

file 425 lines (383 sloc) 16.319 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
module Q = QmlAst
module List = Base.List

type ignored_directive = Q.type_directive

(* for each top level function, we record
1-its defining identifier,
2-the name of its closure
3-its arity
4-its typescheme (as found in \Gamma)
*)
type func_info = {
  arity: int;
  code : Ident.t; (* name of the function *)
  closure : Ident.t; (* name of the closure *)
  used : bool ref; (* shared between a real function and its aliases,
* which would not be possible if the field was mutable *)
  tsc : QmlTypes.typescheme option; (* None everywhere in untyped mode
* Some everywhere in typed mode *)
  from_current_package : bool; (* true if the closure is defined in the current package *)
}

let _count_closures = ref 0
let _count_rec_closures = ref 0
let _count_cleaned = ref 0
let _count_rec_cleaned = ref 0

(* the global knowledge *)
type env = { funcs: func_info IdentMap.t }

module S =
struct
  type t = env
  let pass = "qmlUncurry"
  let pp f {funcs} =
    IdentMap.iter
      (fun _ {arity;code;closure} ->
         Format.fprintf f "%s: arity: %d, clos %s@\n" (Ident.to_string code) arity (Ident.to_string closure)
      ) funcs
end

module R =
struct
  include ObjectFiles.MakeClientServer(S)
  let load ~side env =
    let funcs =
      fold_with_name ~side
        (fun package acc env ->
           let refreshed_env =
             IdentMap.map
               (fun r ->
                  assert r.from_current_package;
                  {r with
                     from_current_package = false;
                     tsc = Option.map (QmlRefresh.refresh_typevars_from_tsc package) r.tsc
                  })
               env.funcs in
           IdentMap.safe_merge acc refreshed_env
        ) env.funcs in
    {funcs = funcs}
end

(* utility function *)
let get_ty annotmap ann =
  QmlAnnotMap.find_ty ann annotmap

(* classify between the three kind of objects that we deal with
* - functions
* - aliases on function identifier (as an optimization)
* - everything else (we don't care about it)
*)
let classify e =
  let o =
    QmlAstWalk.Expr.traverse_findmap
      (fun tra e ->
         match e with
         | Q.Lambda _ -> Some `lambda
         | Q.Ident (_, i) -> Some (`alias i)
         | Q.Coerce _
         | Q.Directive(_, #ignored_directive, _, _) -> tra e
         | _ -> None) e in
  Option.default `nothing o

(* a special of the previous function: returns whether e is a function or not *)
let is_lambda e =
  QmlAstWalk.Expr.traverse_exists
    (fun tra e ->
       match e with
       | Q.Lambda _ -> true
       | Q.Coerce _
       | Q.Directive (_, #ignored_directive, _, _) -> tra e
       | _ -> false) e

(* returns the arity of a function *)
let get_arity e =
  let o =
    QmlAstWalk.Expr.traverse_findmap
      (fun tra e ->
         match e with
         | Q.Lambda (_, l, _) -> Some (List.length l)
         | Q.Coerce _
         | Q.Directive (_, #ignored_directive, _, _) -> tra e
         | _ -> None) e in
  Option.default 0 o

(* rewrite code by inserting closure_create and closure_apply, etc. directives *)
(* build the directive `closure_create using stored information in func_info *)
let closure_create cons label info =
  #<If:CLOSURE_STAT>incr _count_closures#<End>;
  let expr = Q.Directive (label, `closure_create(info.code, info.arity, info.tsc),[],[]) in
  cons#make_from_annot expr (Annot.annot label)

(* build the directive `closure_apply *)
let closure_apply cons label f args =
  let expr = Q.Directive (label, `closure_apply, (f :: args), []) in
  cons#make_from_annot expr (Annot.annot label)

(* build the closure without the implementation, for recursive definitions *)
let closure_create_no_function cons label info =
  #<If:CLOSURE_STAT>incr _count_rec_closures#<End>;
  let expr = Q.Directive (label, `closure_create_no_function (info.code,info.arity,info.tsc), [], []) in
  cons#make_from_annot expr (Annot.annot label)

(* define the implementation of an existing closure, for use on closure defined with the above line *)
let closure_define_function cons label info =
  let expr = Q.Directive (label, `closure_define_function (info.closure, info.code, info.tsc), [],[]) in
  let void_ty = Q.TypeRecord (Q.TyRow ([], None)) in
  cons#make expr void_ty

(* get the identifier of the closure identified by info *)
let get_closure_ident cons annot info =
  info.used := true;
  cons#ident_from_annot info.closure annot


(* register each function in the environment *)
(* register one function val x = body
we compute the arity from body
and get the typescheme from \Gamma *)
let make_clos ident =
  Ident.refresh ~map:(fun s -> "clos_"^s) ident
let register_function cons env (x,body) =
  let n = get_arity body in
  let tsc =
    if cons#typed then
      Some (QmlTypes.Env.Ident.find x cons#gamma)
    else
      None in
  let info =
    { arity = n
    ; code = x
    ; closure = make_clos x
    ; used = ref false
    ; tsc = tsc
    ; from_current_package = true } in
  let funcs = IdentMap.add x info env.funcs in
  { funcs = funcs }, info

(* Do not spamm the list manipulated by the compiler with dummy nodes *)
let cons_dec code_elt code =
  match code_elt with
  | Q.NewVal (_, [])
  | Q.NewValRec (_, []) -> code
  | _ -> code_elt :: code

(* go through the code and register each function
* and add the closure definitions in the code *)
let register_code_elt cons env elt =
  match elt with
  | Q.NewVal (label, bnds) as c ->
      (* [val f(x) = 2
* and g(y) = 2]
*
* becomes
*
* [val f(x) = 2
* and g(y) = 2
* val clos_f = closure_create f 1 "f"
* and clos_g = closure_create g 1 "g"]
*)
      let env, clos_bnds =
        List.fold_left_filter_map
          (fun env ((i,body) as fun_bnd) ->
             match classify body with
             | `nothing ->
                 env, None
             | `alias ident -> (
                 try
                   (* when we have an alias g = f where f is a lambda
* we also create an alias clos_g = clos_f and we won't rewrite g = f
* so that the implementation is still reachable *)
                   let info = IdentMap.find ident env.funcs in
                   if info.from_current_package then
                     (* avoiding introducing useless aliases and also avoid confusing
* the cleaning below (because we don't clean the aliases introduced
* so we should make sure that they never have to been cleaned in
* the first place) *)
                     let env = {funcs = IdentMap.add i info env.funcs} in
                     env, None
                   else (
                     let clos_ident = make_clos i in
                     let new_info =
                       (* same arity, tsc, used *)
                       {info with code = i; closure = clos_ident; from_current_package = true} in
                     let code_elt = (clos_ident, cons#ident_from_annot info.closure (Q.QAnnot.expr body)) in
                     let env = {funcs = IdentMap.add i new_info env.funcs} in
                     env, Some code_elt
                   )
                 with Not_found ->
                   (* not an alias to a toplevel function *)
                   env, None
               )
             | `lambda ->
                 let env, info = register_function cons env fun_bnd in
                 let expr = closure_create cons (Q.Label.expr body) info in
                 let closure_decl = (info.closure, expr) in
                 env, Some closure_decl)
          env
          bnds in
      env, (cons_dec c (cons_dec (Q.NewVal (label, clos_bnds)) []))
  | Q.NewValRec (label, bnds) as c ->
      (* [val rec f(x) = g(x)
* and g(y) = f(y)]
*
* becomes
*
* [val clos_f = closure_create_no_function 1 "f"
* and clos_g = closure_create_no_function 1 "g"
* val rec f(x) = g(x)
* and g(y) = f(y)
* val _ = closure_define_function clos_f f
* and _ = closure_define_function clos_g g]
*)
      (* no need to look for aliases, there can't be any in letrec *)
      let fun_bnds = List.filter (fun (_,e) -> is_lambda e) bnds in
      let env, closure_decl =
        List.fold_left_map
          (fun env ((_,body) as fun_bnd) ->
             let env, info = register_function cons env fun_bnd in
             let expr = closure_create_no_function cons (Q.Label.expr body) info in
             let pre_closure_decl = (info.closure, expr) in
             let expr = closure_define_function cons (Q.Label.expr body) info in
             let post_closure_decl = (Ident.next "_", expr) in
             env, (pre_closure_decl, post_closure_decl))
          env
          fun_bnds in
      let pre_closure_decls, post_closure_decls = List.split closure_decl in
      env, cons_dec
        (Q.NewVal (label, pre_closure_decls))
        (cons_dec c (cons_dec (Q.NewVal (label, post_closure_decls)) []))
    | c -> env, [c]


(* the rewriting process:
it basically rewrites each Apply node into a `closure_apply directive
there are two exceptions:
1- bypass:
After BypassHoisting pass, it is guaranteed that each bypass application is total.
Some care must be taken if bypass are allowed to take qml functions as argument.

2- non partial calls of toplevel functions.

Hence, each remaining Apply node in the code is guaranteed to be non partial.
*)
let rewrite_expr cons env e =
  QmlAstWalk.Expr.self_traverse_map
    (fun self tra e ->
       match e with
       | Q.Ident (_, x) -> (
           try
             let func_info = IdentMap.find x env.funcs in
             get_closure_ident cons (Q.QAnnot.expr e) func_info
           with Not_found -> e
         )

       | Q.Directive (label2, `partial_apply missing, (Q.Apply (_, Q.Ident (label, x), args) :: more_args), []) ->
           let args = List.map self args in
           let more_args = List.map self more_args in
           let func_info =
             try IdentMap.find x env.funcs
             with Not_found ->
               OManager.i_error "Partial application on %s, which is not in env.funcs" (Ident.to_string x) in
           (*
we can only do partial application on toplevel lambdas (syntactically)
because only the lambda lifting introduces such cases
*)
           let f = get_closure_ident cons (Annot.annot label) func_info in
           Q.Directive (label2, `partial_apply missing, (closure_apply cons label2 f args :: more_args), [])

       | Q.Directive (_, `partial_apply _, _, _) -> assert false

       | Q.Apply (label, (Q.Ident (_, x) as f), args) -> (
           (*
since we are in n-ary, this case can be either a full application of a toplevel function
or the application of a closure to its argument
*)
           let args = List.map self args in
           match IdentMap.find_opt x env.funcs with
           | None ->
               closure_apply cons (Q.Label.expr e) f args

           | Some func_info ->
               (* full application *)
               assert (
                 List.length args = func_info.arity ||
                   (Format.printf "%d arguments given vs arity of %d on %s in %a@."
                      (List.length args) func_info.arity (Ident.to_string x) QmlPrint.pp#expr e; false)
               );
               Q.Apply (label, f, args)
         )

       | Q.Apply (_, (Q.Bypass _ | Q.Directive (_, (`may_cps | `restricted_bypass _), _, _)), _) ->
           (*
bypass application, it must be full
FIXME: assert it
*)
           tra e

       | Q.Apply (label, f, args) ->
           let args = List.map self args in
           let f = self f in
           closure_apply cons label f args

       | _ ->
           tra e

    ) e

(* some 'specialized' cleaning for closures
* since we know which closure we used and which one was useless, there is no
* to go through the whole code to find that information
* it is a good idea to clean useless since we create them for every toplevel function
* when often, they aren't useful because the function is always fully applied *)
let clean_binding ~can_be_cleaned env (_,e) =
  match e with
  | Q.Directive (_, `closure_create (i, _, _), _, _) ->
      let info = IdentMap.find i env.funcs in
      let kept = !(info.used) || not (can_be_cleaned i) in
      #<If:CLOSURE_STAT> if not kept then incr _count_cleaned#<End>;
      kept
  | Q.Directive (_, `closure_create_no_function (i, _, _), _, _)
  | Q.Directive (_, `closure_define_function (_, i, _), _, _) ->
      let info = IdentMap.find i env.funcs in
      let kept = !(info.used) || not (can_be_cleaned i) in
      #<If:CLOSURE_STAT> if not kept then incr _count_rec_cleaned#<End>;
      kept
  | _ ->
      true

let clean_code ~can_be_cleaned env code =
  QmlAstWalk.Code.filter_binding (clean_binding ~can_be_cleaned env) code

(* initial env
for separate compilation: it must be populated initially
*)
let empty_env = { funcs = IdentMap.empty }

(* register each toplevel functions *)
let register_code cons env code =
  List.fold_left_collect (register_code_elt cons) env code

let rewrite_bnd cons env ((i,e) as bnd) =
  match classify e with
  | `alias i when IdentMap.mem i env.funcs -> bnd
  | _ -> (i,rewrite_expr cons env e)

(* rewrite the entire code *)
let rewrite_code cons env code =
  QmlAstWalk.CodeExpr.map_name_expr (fun bnd -> rewrite_bnd cons env bnd) code

(* by default, since we don't have separate compilation
* we allow ourselves to clean every unused closures
* FIXME we should only clean the closures that cannot be accessed
* by other modules (ie from local functions, or from functions hidden by the signatures) *)
let process_code ?(can_be_cleaned=fun _ -> ObjectFiles.compilation_mode () = `linking) ~side ~typed gamma annotmap code =
  #<If:CLOSURE_STAT>
    _count_closures := 0;
    _count_cleaned := 0;
  #<End>;
  let cons, get_state = QmlAstCons.make_cons ~typed gamma annotmap in
  let loaded_env = R.load ~side empty_env in
  let computed_env, code = register_code cons loaded_env code in
  let diff_env = {funcs = IdentMap.diff computed_env.funcs loaded_env.funcs} in
  R.save ~side diff_env;
  let exported_map =
    IdentMap.fold
      (fun _ {closure; code} acc -> IdentMap.add closure code acc)
      diff_env.funcs
      IdentMap.empty in
  let code = rewrite_code cons computed_env code in
  let code = clean_code ~can_be_cleaned computed_env code in
  #<If:CLOSURE_STAT>
    let c = !_count_closures in
    let rc = !_count_rec_closures in
    let cleaned = !_count_cleaned in
    let rcleaned = !_count_rec_cleaned / 2 in (* the counter was increment once when removing
* the @closure_define and @closure_create_no_function *)
    let total = c + rc in
    let total_cleaned = cleaned + rcleaned in
    let percentage c t = if t = 0 then " - " else Printf.sprintf "%2d%%" (c * 100 / t) in
    Printf.printf "Total closures created: %d\n" total;
    Printf.printf " Simple closure: %d\t%s\n" c (percentage c total);
    Printf.printf " Recursive closure: %d\t%s\n" rc (percentage rc total);
    Printf.printf "Total closures removed: %d\t%s\n" total_cleaned (percentage total_cleaned total);
    Printf.printf " Simple closure: %d\t%s\n" cleaned (percentage cleaned c);
    Printf.printf " Recursive closure: %d\t%s\n%!" rcleaned (percentage rcleaned rc)
  #<End>;
  get_state (), exported_map, code
Something went wrong with that request. Please try again.