Skip to content
This repository
tag: v64
Fetching contributors…

Cannot retrieve contributors at this time

file 620 lines (520 sloc) 22.305 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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
(*
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/>.
*)
(**
Intermediate language for CPS transformations.

This language is based on "Compiling with Continuations, Continued",
by Andrew Kennedy, in Proceedings of ICFP 2007.

Note: In a near-future, the algebraic structure will be
in part replaced by a graph structure, which permits
numerous phases of optimization in O(n) rather than O(n^2)

This file has no mli because it would just duplicate the IL ast.

The file define a module IL because for convenience we write :

IL.Continuation, IL.Bypass, etc... because of clash of constructor names
between this module and the module QmlAst.

We do not want to write the name of the module (QmlCpsIL),
and neither do an 'open' leading to having constructors like Bypass in the
scope.

@author David Rajchenbach-Teller
@author Mathieu Barbin
@author Rudy Sicard
*)

(**
TODO : if needed, the cps rewriter should return consistents gamma & annotmap
The IL ast should be updated to support annotation
*)

module IL = struct
  type ident = Ident.t

  (** The name of a continuation.
This may be replaced in the future by a (cident, ExprIdent.t) UnionFind.t
where cident will be a black type. *)
  type cident = Continuation of ident

  (** The name of a value.
Same remark, (vident, ExprIdent.t) UnionFind.t *)
  type vident = Value of ident

  (**The name of a field*)
  type fident = Field of string

  (** skey, restriction option.
The restriction arg is used to distinguish between :
- standard bypass : {[QmlAst.Bypass key]}
represented by : {[IL.Bypass (key, None)]}
- restricted bypass : {[QmlAst.Directive (`restricted_bypass pass_id, [{ e = Bypass key }], _)|}
the pass_id the string contained in the string option in this case :
represented by : {[IL.Bypass (key, Some pass_id)]} *)
  type bypass = Bypass of (BslKey.t * string option)

  (* used to build stack traces *)
  type stack_trace_info =
      { caller_cont : cident option; (** continuation of the closest enclosing function if any *)
        callee_name : string option; (** local name of the function we're calling if any *)
        position : string option; (** position of the Apply if any *) }

  (* TODO : see if this is needed for later optimizations *)
  (*The type of a function definition*)
  (*
type func =
{return: cident;
(**[None] if the function takes place outside of a transaction.*)
transact: cident option;
(* (**[None] if the function is called from outside a request.*)
request: cident option; *)
arg: vident;
term: term}
and application =
{app_called: vident;
app_return: cident;
app_transact: cident option;
app_term
}
*)
  (* not used yet, either
type application =
{app_called: vident
;app_arg: vident
;app_return: cident
;app_transaction: cident option
}
*)

  type value =
    | Constant of QmlAst.const_expr
    | Record of (fident * vident * Annot.t) list
    | ExtendRecord of (fident * vident) list * vident
    | LazyRecord of (fident * vident * Annot.t) list * vident option
    | BasicBypass of bypass (* for value bypass only (not functions) *)
    | ValueSkip of QmlAst.expr

  and cps_function = CpsVident of vident | CpsBypass of bypass

  and term =

    (**[LetVal("x", v, t)] represents [let x = v in t].
At this stage, this is the only way of using a value in a term. *)
    | LetVal of vident * value * term

    (**[LetProj("x", ("y", "foo"), t)] represents [let x = y.foo in t].
This is the only way of accessing a field. *)
    | LetProj of vident * (vident * fident) * term

    (**[LetCont(("k", "x", t, u), parent)] represents
[let(*continuation*) k (x -> t) in u].
This introduces a continuation [k] with one unique argument [x],
to be used in [u].
the parent is an option of the parent continuation id.

<!> Assert: x MUST be fresh and appear ONLY in t
or it would definitly break the substitution mechanism.
*)
    | LetCont of ((cident * vident * term * term) * cident option)

    (**Introduce a set of mutually recursive definitions. *)
    | LetRecFun of ( vident (** function name *)
                    * vident list (** arguments *)
                    * cident (** the continuation *)
                    * term (** body *)
                   ) list * term

    (**Introduce a set of non-recursive definitions. *)
    | LetFun of (vident * vident list * cident * term) list * term

    (**Apply a continuation to a value, e.g. to jump
or return from a function. *)
    | ApplyCont of cident * vident

    (**[ApplyExpr(a,b,k)] computes [a] (which must be a rewrited-function)
with argument [b] and continuation [k]
The function [a] used to take arg [b] and return type c
[k] is a 'c continuation, and function [a] has been rewritten to
'b -> 'c continuation -> unit
*)
    | ApplyExpr of cps_function * vident * cident

    (**
Nary mode for application.
TODO: remove ApplyExpr from the ast, keep only ApplyNary
*)
    | ApplyNary of cps_function * vident list * cident * stack_trace_info option

    (**
Special case of application of a non cps bypass, with all its arguments.
Bypasses cannot be currified.
*)
    | ApplyBypass of bypass * (vident list) * cident

    | Match of vident * (QmlAst.pat * term) list

    (**End of program, with result*)
    | Done of vident * string

    | Directive of
        (QmlAst.qml_directive * term list * QmlAst.ty list)

    (**In some cases, no operation, keep the qml 'as it is'*)
    (**Currently this node is unused*)
    | Skip of QmlAst.expr


  (** tools for code generation *)
  let fresh_c () = Continuation (Ident.next "cont")
  let fresh_v () = Value (Ident.next "val")

  let fresh_fun () = Value (Ident.next "fun")

end

(** {6 Traversal} *)
(** for optimisation and rewriting rules in IL *)
(** *)
module Subs : TraverseInterface.S2 with type 'a t = IL.term constraint 'a = _ * _ * _ =
struct
  open IL
  open Base
  type 'a t = term constraint 'a = _ * _ * _

  let foldmap tra acc t =
    match t with
    | LetVal (vident, value, term) ->
        let acc, fterm = tra acc term in
        acc,
        if term == fterm then t else
          LetVal (vident, value, fterm)

    | LetProj (vident, (vident', fident), term) ->
        let acc, fterm = tra acc term in
        acc,
        if term == fterm then t else
          LetProj (vident, (vident', fident), fterm)

    | LetCont ((cident, vident, term, term'), cident') ->
        let acc, fterm = tra acc term in
        let acc, fterm' = tra acc term' in
        acc,
        if term == fterm && term' == fterm' then t else
          LetCont ((cident, vident, fterm, fterm'), cident')

    | LetRecFun (list, term) ->
        let foldmap acc ((vident, vident_list, cident, term) as t) =
          let acc, fterm = tra acc term in
          acc,
          if term == fterm then t else
            (vident, vident_list, cident, fterm) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        let acc, fterm = tra acc term in
        acc,
        if term == fterm && list == flist then t else
          LetRecFun (flist, fterm)

    | LetFun (list, term) ->
        let foldmap acc ((vident, vident_list, cident, term) as t) =
          let acc, fterm = tra acc term in
          acc,
          if term == fterm then t else
            (vident, vident_list, cident, fterm) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        let acc, fterm = tra acc term in
        acc,
        if list == flist && term == fterm then t else
          LetFun (flist, fterm)

    | ApplyCont (_cident, _vident) -> acc, t

    | ApplyExpr (_cps_function, _vident', _cident) -> acc, t

    | ApplyNary (_cps_function, _vident_list, _cident, _stack_infos) -> acc, t

    | ApplyBypass (_bypass, _vident_list, _cident) -> acc, t

    | Match (vident, pat_term_list) ->
        let foldmap acc ((pat, term) as t) =
          let acc, fterm = tra acc term in
          acc,
          if term == fterm then t else
            (pat, fterm) in
        let acc, fpat_term_list = List.fold_left_map_stable foldmap acc pat_term_list in
        acc,
        if pat_term_list == fpat_term_list then t else
          Match (vident, fpat_term_list)

    | Done (_vident, _string) -> acc, t

    | Directive (qml_directive, terms, tys) ->
        let acc, fterms = List.fold_left_map_stable tra acc terms in
        acc,
        if terms == fterms then t else
          Directive (qml_directive, fterms, tys)

    | Skip _expr -> acc, t

  let iter x = Traverse.Unoptimized.iter foldmap x
  let map x = Traverse.Unoptimized.map foldmap x
  let fold x = Traverse.Unoptimized.fold foldmap x
end

module Walk : TraverseInterface.TRAVERSE
  with type 'a t = IL.term constraint 'a = _ * _ * _
  and type 'a container = IL.term constraint 'a = _ * _ * _
      = Traverse.Make2 ( Subs )

(* TODO: replace this with the unionFind algorithm *)
module IdentWalk :
sig
  open IL
  val foldmap : ( 'acc -> ident -> 'acc * ident ) -> 'acc -> term -> 'acc * term
  val foldmap_up : ( 'acc -> ident -> 'acc * ident ) -> 'acc -> term -> 'acc * term
  val foldmap_down : ( 'acc -> ident -> 'acc * ident ) -> 'acc -> term -> 'acc * term
  val exists : ident -> value -> bool
end =
struct
  open IL
  open Base

  let foldmap_vident tra acc ((Value id) as v) =
    let acc, fid = tra acc id in
    acc,
    if id == fid then v else
      Value fid

  let foldmap_cident tra acc ((Continuation id) as v) =
    let acc, fid = tra acc id in
    acc,
    if id == fid then v else
      Continuation fid

  let foldmap_skip tra acc e =
    QmlAstWalk.Expr.foldmap
      (fun acc e ->
         match e with
         | QmlAst.Ident (_, id) ->
             let acc, fid = tra acc id in
             acc,
             if id == fid then e else
               QmlAstCons.UntypedExpr.ident fid
         | _ -> acc, e
      ) acc e

  let foldmap_cps_function tra acc cps =
    match cps with
    | CpsVident (Value id) ->
        let acc, fid = tra acc id in
        acc,
        if id == fid then cps else
          CpsVident (Value fid)
    | _ -> acc, cps

  let foldmap_stack_info tra acc stack_info =
    match stack_info.caller_cont with
    | None -> acc, stack_info
    | Some cident ->
        let acc, fcident = foldmap_cident tra acc cident in
        acc,
        if cident == fcident then stack_info else {stack_info with caller_cont = Some fcident}

  let foldmap_ident_value tra acc t =
    match t with
    | Constant _ -> acc, t
    | Record list ->
        let foldmap acc ((fident, vident, annot) as t) =
          let acc, fvident = foldmap_vident tra acc vident in
          acc,
          if vident == fvident then t else
            (fident, fvident, annot) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        acc,
        if list == flist then t else
          Record flist
    | ExtendRecord (list, vident) ->
        let foldmap acc ((fident, vident (*, annot*)) as t) =
          let acc, fvident = foldmap_vident tra acc vident in
          acc,
          if vident == fvident then t else
            (fident, fvident(*, annot*)) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        let acc, fvident = foldmap_vident tra acc vident in
        acc,
        if list == flist && vident == fvident then t else
          ExtendRecord (flist, fvident)
    | LazyRecord (list, vident_option) ->
        let foldmap acc ((fident, vident, annot) as t) =
          let acc, fvident = foldmap_vident tra acc vident in
          acc,
          if vident == fvident then t else
            (fident, fvident, annot) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        let acc, fvident_option = Option.foldmap_stable (foldmap_vident tra) acc vident_option in
        acc,
        if list == flist && vident_option == fvident_option then t else
          LazyRecord (flist, fvident_option)
    | BasicBypass _ -> acc, t
    | ValueSkip e ->
        let acc, fe = foldmap_skip tra acc e in
        acc,
        if e == fe then t else
          ValueSkip fe

  let foldmap_ident tra acc t =
    match t with
    | LetVal (vident, value, term) ->
        let acc, fvident = foldmap_vident tra acc vident in
        let acc, fvalue = foldmap_ident_value tra acc value in
        acc,
        if vident == fvident && value == fvalue then t else
          LetVal (fvident, fvalue, term)

    | LetProj (vident, (vident', fident), term) ->
        let acc, fvident = foldmap_vident tra acc vident in
        let acc, fvident' = foldmap_vident tra acc vident' in
        acc,
        if vident == fvident && vident' == fvident' then t else
          LetProj (fvident, (fvident', fident), term)

    | LetCont ((cident, vident, term, term'), cident') ->
        let acc, fcident = foldmap_cident tra acc cident in
        let acc, fvident = foldmap_vident tra acc vident in
        let acc, fcident' = Option.foldmap_stable (foldmap_cident tra) acc cident' in
        acc,
        if cident == fcident && vident == fvident && cident' == fcident' then t else
          LetCont ((fcident, fvident, term, term'), fcident')

    | LetRecFun (list, term) ->
        let foldmap acc ((vident, vident_list, cident, term) as t)=
          let acc, fvident = foldmap_vident tra acc vident in
          let acc, fvident_list = List.fold_left_map_stable (foldmap_vident tra) acc vident_list in
          let acc, fcident = foldmap_cident tra acc cident in
          acc,
          if vident == fvident && vident_list == fvident_list && cident = fcident then t else
            (fvident, fvident_list, fcident, term) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        acc,
        if list == flist then t else
          LetRecFun (flist, term)

    | LetFun (list, term) ->
        let foldmap acc ((vident, vident_list, cident, term) as t) =
          let acc, fvident = foldmap_vident tra acc vident in
          let acc, fvident_list = List.fold_left_map_stable (foldmap_vident tra) acc vident_list in
          let acc, fcident = foldmap_cident tra acc cident in
          acc,
          if vident == fvident && vident_list == fvident_list && cident == fcident then t else
            (fvident, fvident_list, fcident, term) in
        let acc, flist = List.fold_left_map_stable foldmap acc list in
        acc,
        if list == flist then t else
          LetFun (flist, term)

    | ApplyCont (cident, vident) ->
        let acc, fcident = foldmap_cident tra acc cident in
        let acc, fvident = foldmap_vident tra acc vident in
        acc,
        if cident == fcident && vident == fvident then t else
          ApplyCont (fcident, fvident)

    | ApplyExpr (cps_function, vident, cident) ->
        let acc, fcps_function = foldmap_cps_function tra acc cps_function in
        let acc, fvident = foldmap_vident tra acc vident in
        let acc, fcident = foldmap_cident tra acc cident in
        acc,
        if cps_function == fcps_function && vident == fvident && cident == fcident then t else
          ApplyExpr (fcps_function, fvident, fcident)

    | ApplyNary (cps_function, vident_list, cident, stack_info_opt) ->
        let acc, fcps_function = foldmap_cps_function tra acc cps_function in
        let acc, fvident_list = List.fold_left_map_stable (foldmap_vident tra) acc vident_list in
        let acc, fcident = foldmap_cident tra acc cident in
        let acc, fstack_info_opt = Option.foldmap (foldmap_stack_info tra) acc stack_info_opt in
        acc,
        if cps_function == fcps_function && vident_list == fvident_list && cident == fcident && stack_info_opt == fstack_info_opt then t else
          ApplyNary (fcps_function, fvident_list, fcident, fstack_info_opt)

    | ApplyBypass (bypass, vident_list, cident) ->
        let acc, fvident_list = List.fold_left_map_stable (foldmap_vident tra) acc vident_list in
        let acc, fcident = foldmap_cident tra acc cident in
        acc,
        if vident_list == fvident_list && cident == fcident then t else
          ApplyBypass (bypass, fvident_list, fcident)

    | Match (vident, pat_term_list) ->
        let acc, fvident = foldmap_vident tra acc vident in
        acc,
        if vident == fvident then t else
          Match (fvident, pat_term_list)

    | Done (vident, string) ->
        let acc, fvident = foldmap_vident tra acc vident in
        acc,
        if vident == fvident then t else
          Done (fvident, string)

    | Directive (_qml_directive, _terms, _tys) -> acc, t

    | Skip e ->
        let acc, fe = foldmap_skip tra acc e in
        acc,
        if e == fe then t else
          Skip fe

  let foldmap tra = Walk.foldmap (foldmap_ident tra)
  let foldmap_up tra = Walk.foldmap_up (foldmap_ident tra)
  let foldmap_down tra = Walk.foldmap_down (foldmap_ident tra)

  let exists ident value =
    try
      ignore
        (foldmap_ident_value
           (fun () v ->
              if Ident.equal v ident then raise Exit;
              (), v) () value);
      false
    with Exit -> true

end


(** {6 Utils on IL} *)

#<Debugvar:CPS_VERBOSE>

let debug fmt =
  Printf.fprintf stderr ("[Cps] "^^fmt^^"\n%!")

module Substitution :
sig
  open IL
  val ident : ident -> ident -> term -> term
end =
struct
  let ident ident ident' term =
    let subst v = if Ident.equal v ident then ident' else v in
    let foldmap () ident = (), subst ident in
    let _, term = IdentWalk.foldmap foldmap () term in
    term
end

module Factorize :
sig
  open IL

  (** letcont : factorize all occurence of the form
[LetCont (k, (x -> term), [ ApplyCont (k, y) ])]
rewriting it so :
[ <term> {x <- y} ]

<!> The Optimized implementation uses a side-effect with the
union find structure.
assert : x is fresh and appears only in term.
*)
  val letcont : term -> term

  (** debug : give the number of letcont simplified until now *)
  val count : unit -> int

  (** profile : give the global time taken doing substitution *)
  val chrono_subst : unit -> float
end =
struct
  open IL
  open Base

  let counter = ref 0
  let count () = !counter

  let _chrono_subst = Chrono.make ()
  let chrono_subst () = Chrono.read _chrono_subst

  let letcont term =
    let rec map t =
      match t with
(* This case is unused, but is should be done somewhere. Keep this code until we find the correct way to do it

| LetCont ((Continuation cident, Value vident, ApplyCont (Continuation cident', Value vident'), term), cident_option)
when Ident.compare vident vident' = 0 ->

(* we could eventually loose some thread_context, be sure that not before simplifying *)
let authorized_simplification =
match cident_option with
| None -> true
| Some (Continuation cident'') -> Ident.compare cident' cident'' = 0
in
if authorized_simplification
then
let _ =
incr(counter);
#<If:CPS_VERBOSE $minlevel 2>
debug "#%d LetCont %s (%s -> ApplyCont %s %s) in <term>" (!counter)
(Ident.stident cident) (Ident.stident vident)
(Ident.stident cident') (Ident.stident vident')
#<End> in

let _ = #<If:CPS_VERBOSE $minlevel 1> Chrono.start _chrono_subst #<End> in
let term = Substitution.ident cident cident' term in
let _ = #<If:CPS_VERBOSE $minlevel 1> Chrono.stop _chrono_subst #<End> in
map term
else
term
*)

      | LetCont ((Continuation cident, Value vident, term, applycont), _) ->
          begin
            (* traversing local LetVal *)
            let rec find_applycont acc = function
              | ApplyCont (Continuation cident', (Value vident')) ->
                  if Ident.compare cident cident' <> 0
                  then None
                  else Some (acc, vident')
              | LetVal (vident, value, term) when not (IdentWalk.exists cident value) -> find_applycont ((vident, value)::acc) term
              | _ -> None
            in
            match find_applycont [] applycont with
            | None -> t
            | Some (letvals, vident') ->
                let _ =
                  incr(counter);
                  #<If:CPS_VERBOSE $minlevel 2>
                    debug "#%d LetCont %s (%s -> <term>) in ApplyCont %s %s" (!counter)
                    (Ident.stident cident) (Ident.stident vident)
                    (Ident.stident cident) (Ident.stident vident')
                  #<End> in

                (* this can be factorized <term> { vident <- vident' } *)
                (* Optimisation: once Vident are (vident, ExprIdent.t) UnionFind.t,
we can use : UnionFind.replace vident vident', but we need to be
sure that [term] is the only one containing occurences of [vident] *)

                let _ = #<If:CPS_VERBOSE $minlevel 1> Chrono.start _chrono_subst #<End> in
                let term = Substitution.ident vident vident' term in
                let _ = #<If:CPS_VERBOSE $minlevel 1> Chrono.stop _chrono_subst #<End> in

                (* putting back local LetVal in the same order *)
                let term = List.fold_left (fun term (vident, const) -> LetVal (vident, const, term)) term letvals in
                map term
          end
      | _ -> t in
    Walk.map_down map term

end
Something went wrong with that request. Please try again.