1 (* Copyright 1999,2004,2007,2010-2012,2014 Stefan Monnier <monnier@gnu.org> *)
3 (* sml-mode here treats the second `=' as an equal op because it
4 * thinks it's seeing something like "... type t = (s.t = ...)". FIXME! *)
5 functor foo (structure s : S) where type t = s.t =
61 = Attributes of string list
63 sharing type node' = node
67 functor DoWrap1(type node) : S = struct
74 functor DoWrap(type node) : sig
77 sharing type node' = node
89 val tut = fn (x,y) z y e r =>
91 val tut = fn (x,y) => fn z y => fn e r =>
101 val x = 1 in val x = x end
103 local val x = 1 in val x = x end
104 local val x = 1 in val x = x end
105 local val x = 1 in val x = x end (* fixindent *)
106 local val x = 1 in val x = x end
118 (* From "Christopher Dutchyn" <cdutchyn@cs.ubc.ca> *)
120 (* This is actually not valid SML anyway. *)
128 (* Testing obedience to user overrides: *)
129 x := 3; (* fixindent *)
145 datatype foo = FOO | BAR of baz
146 and baz = BAZ | QUUX of foo
155 and baz = BAZ (* fixindent *)
159 datatype foo = datatype M.foo
162 signature S = S' where type foo = int
167 , let val x = f 42 in g (x,x,44) end
172 let val x = f 42 in g (x,x,44) end
178 let val x = f 42 in g (x,x,44) end
182 , let val x = f 42 in g (x,x,44) end
183 , foldl (fn ((p,q),s) => g (p,q,Vector.length q) ^ ":" ^ s)
184 "" (Beeblebrox.masterCountList mlist2)
185 , if null mlist2 then ";" else ""
188 fun foo (true::rest) = 1 + 2 * foo rest
190 = let val _ = 1 in 2 end
203 then 2 (* Could also be indented by a basic offset. *)
212 F.APP(F.VAR fl, OU.filter filt vs)
289 structure Foo = struct
293 structure Foo = struct val x = 1
298 type flint = FLINT.prog
299 val split: flint -> flint * flint option
302 structure FSplit :> FSPLIT =
307 structure S = IntRedBlackSet
308 structure M = FLINTIntMap
310 structure OU = OptUtils
311 structure FU = FlintUtil
312 structure LT = LtyExtern
313 structure PO = PrimOp
314 structure PP = PPFlint
315 structure CTRL = FLINT_Control
318 val say = Control_Print.say
319 fun bug msg = ErrorMsg.impossible ("FSplit: "^msg)
320 fun buglexp (msg,le) = (say "\n"; PP.printLexp le; say " "; bug msg)
321 fun bugval (msg,v) = (say "\n"; PP.printSval v; say " "; bug msg)
322 fun assert p = if p then () else bug ("assertion failed")
325 val mklv = LambdaVar.mkLvar
326 val cplv = LambdaVar.dupLvar
328 fun S_rmv(x, s) = S.delete(s, x) handle NotFound => s
330 fun addv (s,F.VAR lv) = S.add(s, lv)
332 fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
333 fun rmvs (s,lvs) = foldl (fn (l,s) => S_rmv(l, s)) s lvs
337 fun split (fdec as (fk,f,args,body)) = let
338 val {getLty,addLty,...} = Recover.recover (fdec, false)
340 val m = Intmap.new(64, Unknown)
341 fun addpurefun f = Intmap.add m (f, false)
342 fun funeffect f = (Intmap.map m f) handle Uknown => true
344 (* sexp: env -> lexp -> (leE, leI, fvI, leRet)
345 * - env: IntSetF.set current environment
346 * - lexp: lexp expression to split
347 * - leRet: lexp the core return expression of lexp
348 * - leE: lexp -> lexp recursively split lexp: leE leRet == lexp
349 * - leI: lexp option inlinable part of lexp (if any)
350 * - fvI: IntSetF.set free variables of leI: FU.freevars leI == fvI
352 * sexp splits the lexp into an expansive part and an inlinable part.
353 * The inlinable part is guaranteed to be side-effect free.
354 * The expansive part doesn't bother to eliminate unused copies of
355 * elements copied to the inlinable part.
356 * If the inlinable part cannot be constructed, leI is set to F.RET[].
357 * This implies that fvI == S.empty, which in turn prevents us from
358 * mistakenly adding anything to leI.
360 fun sexp env lexp = (* fixindent *)
362 (* non-side effecting binds are copied to leI if exported *)
363 fun let1 (le,lewrap,lv,vs,effect) =
364 let val (leE,leI,fvI,leRet) = sexp (S.add(env, lv)) le
365 val leE = lewrap o leE
366 in if effect orelse not (S.member(fvI, lv))
367 then (leE, leI, fvI, leRet)
368 else (leE, lewrap leI, addvs(S_rmv(lv, fvI), vs), leRet)
372 (* we can completely move both RET and TAPP to the I part *)
373 of F.RECORD (rk,vs,lv,le as F.RET [F.VAR lv']) =>
375 then (fn e => e, lexp, addvs(S.empty, vs), lexp)
376 else (fn e => e, le, S.singleton lv', le)
378 (fn e => e, lexp, addvs(S.empty, vs), lexp)
379 | F.TAPP (F.VAR tf,tycs) =>
380 (fn e => e, lexp, S.singleton tf, lexp)
382 (* recursive splittable lexps *)
383 | F.FIX (fdecs,le) => sfix env (fdecs, le)
384 | F.TFN (tfdec,le) => stfn env (tfdec, le)
387 | F.CON (dc,tycs,v,lv,le) =>
388 let1(le, fn e => F.CON(dc, tycs, v, lv, e), lv, [v], false)
389 | F.RECORD (rk,vs,lv,le) =>
390 let1(le, fn e => F.RECORD(rk, vs, lv, e), lv, vs, false)
391 | F.SELECT (v,i,lv,le) =>
392 let1(le, fn e => F.SELECT(v, i, lv, e), lv, [v], false)
393 | F.PRIMOP (po,vs,lv,le) =>
394 let1(le, fn e => F.PRIMOP(po, vs, lv, e), lv, vs, PO.effect(#2 po))
396 (* IMPROVEME: lvs should not be restricted to [lv] *)
397 | F.LET(lvs as [lv],body as F.TAPP (v,tycs),le) =>
398 let1(le, fn e => F.LET(lvs, body, e), lv, [v], false)
399 | F.LET (lvs as [lv],body as F.APP (v as F.VAR f,vs),le) =>
400 let1(le, fn e => F.LET(lvs, body, e), lv, v::vs, funeffect f)
402 | F.SWITCH (v,ac,[(dc as F.DATAcon(_,_,lv),le)],NONE) =>
403 let1(le, fn e => F.SWITCH(v, ac, [(dc, e)], NONE), lv, [v], false)
405 | F.LET (lvs,body,le) =>
406 let val (leE,leI,fvI,leRet) = sexp (S.union(S.addList(S.empty, lvs), env)) le
407 in (fn e => F.LET(lvs, body, leE e), leI, fvI, leRet)
410 (* useless sophistication *)
411 | F.APP (F.VAR f,args) =>
413 then (fn e => e, F.RET[], S.empty, lexp)
414 else (fn e => e, lexp, addvs(S.singleton f, args), lexp)
416 (* other non-binding lexps result in unsplittable functions *)
417 | (F.APP _ | F.TAPP _) => bug "strange (T)APP"
418 | (F.SWITCH _ | F.RAISE _ | F.BRANCH _ | F.HANDLE _) =>
419 (fn e => e, F.RET[], S.empty, lexp)
422 (* Functions definitions fall into the following categories:
423 * - inlinable: if exported, copy to leI
424 * - (mutually) recursive: don't bother
425 * - non-inlinable non-recursive: split recursively *)
426 and sfix env (fdecs,le) =
427 let val nenv = S.union(S.addList(S.empty, map #2 fdecs), env)
428 val (leE,leI,fvI,leRet) = sexp nenv le
429 val nleE = fn e => F.FIX(fdecs, leE e)
431 of [({inline=inl as (F.IH_ALWAYS | F.IH_MAYBE _),...},f,args,body)] =>
432 let val min = case inl of F.IH_MAYBE(n,_) => n | _ => 0
433 in if not(S.member(fvI, f)) orelse min > !CTRL.splitThreshold
434 then (nleE, leI, fvI, leRet)
435 else (nleE, F.FIX(fdecs, leI),
436 rmvs(S.union(fvI, FU.freevars body),
440 | [fdec as (fk as {cconv=F.CC_FCT,...},_,_,_)] =>
441 sfdec env (leE,leI,fvI,leRet) fdec
443 | _ => (nleE, leI, fvI, leRet)
446 and sfdec env (leE,leI,fvI,leRet) (fk,f,args,body) =
447 let val benv = S.union(S.addList(S.empty, map #1 args), env)
448 val (bodyE,bodyI,fvbI,bodyRet) = sexp benv body
451 (fn e => F.FIX([(fk, f, args, bodyE bodyRet)], e),
454 let val fvbIs = S.listItems(S.difference(fvbI, benv))
455 val (nfk,fkE) = OU.fk_wrap(fk, NONE)
459 val fErets = (map F.VAR fvbIs)
460 val bodyE = bodyE(F.RET fErets)
462 val bodyE = bodyE(F.RECORD(F.RK_STRUCT, map F.VAR fvbIs,
463 tmp, F.RET[F.VAR tmp])) *)
464 val fdecE = (fkE, fE, args, bodyE)
465 val fElty = LT.ltc_fct(map #2 args, map getLty fErets)
466 val _ = addLty(fE, fElty)
469 val fkI = {inline=F.IH_ALWAYS, cconv=F.CC_FCT,
470 known=true, isrec=NONE}
472 (map (fn lv => (lv, getLty(F.VAR lv))) fvbIs) @ args
473 val fdecI as (_,fI,_,_) = FU.copyfdec(fkI,f,argsI,bodyI)
474 val _ = addpurefun fI
477 val nargs = map (fn (v,t) => (cplv v, t)) args
478 val argsv = map (fn (v,t) => F.VAR v) nargs
480 let val lvs = map cplv fvbIs
481 in F.LET(lvs, F.APP(F.VAR fE, argsv),
482 F.APP(F.VAR fI, (map F.VAR lvs)@argsv))
484 (* let val lv = mklv()
485 in F.LET([lv], F.APP(F.VAR fE, argsv),
486 F.APP(F.VAR fI, (F.VAR lv)::argsv))
488 val nfdec = (nfk, f, nargs, nbody)
490 (* and now, for the whole F.FIX *)
492 F.FIX([fdecE], F.FIX([fdecI], F.FIX([nfdec], leE e)))
494 in if not(S.member(fvI, f)) then (nleE, leI, fvI, leRet)
496 F.FIX([fdecI], F.FIX([nfdec], leI)),
497 S.add(S.union(S_rmv(f, fvI), S.intersection(env, fvbI)), fE),
502 (* TFNs are kinda like FIX except there's no recursion *)
503 and stfn env (tfdec as (tfk,tf,args,body),le) =
504 let val (bodyE,bodyI,fvbI,bodyRet) =
505 if #inline tfk = F.IH_ALWAYS
506 then (fn e => body, body, FU.freevars body, body)
508 val nenv = S.add(env, tf)
509 val (leE,leI,fvI,leRet) = sexp nenv le
510 in case (bodyI, S.listItems(S.difference(fvbI, env)))
511 of ((F.RET _ | F.RECORD(_,_,_,F.RET _)),_) =>
513 (fn e => F.TFN((tfk, tf, args, bodyE bodyRet), leE e),
516 (* everything was split out *)
517 let val ntfdec = ({inline=F.IH_ALWAYS}, tf, args, bodyE bodyRet)
518 val nlE = fn e => F.TFN(ntfdec, leE e)
519 in if not(S.member(fvI, tf)) then (nlE, leI, fvI, leRet)
520 else (nlE, F.TFN(ntfdec, leI),
521 S_rmv(tf, S.union(fvI, fvbI)), leRet)
526 val tfEvs = map F.VAR fvbIs
527 val bodyE = bodyE(F.RET tfEvs)
528 val tfElty = LT.lt_nvpoly(args, map getLty tfEvs)
529 val _ = addLty(tfE, tfElty)
532 val tfkI = {inline=F.IH_ALWAYS}
533 val argsI = map (fn (v,k) => (cplv v, k)) args
534 (* val tmap = ListPair.map (fn (a1,a2) =>
535 * (#1 a1, LT.tcc_nvar(#1 a2)))
537 val bodyI = FU.copy tmap M.empty
538 (F.LET(fvbIs, F.TAPP(F.VAR tfE, map #2 tmap),
542 F.TFN((tfk, tfE, args, bodyE),
543 F.TFN((tfkI, tf, argsI, bodyI), leE e))
545 in if not(S.member(fvI, tf)) then (nleE, leI, fvI, leRet)
547 F.TFN((tfkI, tf, argsI, bodyI), leI),
548 S.add(S.union(S_rmv(tf, fvI), S.intersection(env, fvbI)), tfE),
553 (* here, we use B-decomposition, so the args should not be
554 * considered as being in scope *)
555 val (bodyE,bodyI,fvbI,bodyRet) = sexp S.empty body
556 in case (bodyI, bodyRet)
557 of (F.RET _,_) => ((fk, f, args, bodyE bodyRet), NONE)
558 | (_,F.RECORD (rk,vs,lv,F.RET[lv'])) =>
559 let val fvbIs = S.listItems fvbI
562 val bodyE = bodyE(F.RECORD(rk, vs@(map F.VAR fvbIs), lv, F.RET[lv']))
563 val fdecE as (_,fE,_,_) = (fk, cplv f, args, bodyE)
567 val argLtys = (map getLty vs) @ (map (getLty o F.VAR) fvbIs)
568 val argsI = [(argI, LT.ltc_str argLtys)]
569 val (_,bodyI) = foldl (fn (lv,(n,le)) =>
570 (n+1, F.SELECT(F.VAR argI, n, lv, le)))
571 (length vs, bodyI) fvbIs
572 val fdecI as (_,fI,_,_) = FU.copyfdec (fk, f, argsI, bodyI)
574 val nargs = map (fn (v,t) => (cplv v, t)) args
581 F.APP(F.VAR fE, map (F.VAR o #1) nargs),
582 F.APP(F.VAR fI, [F.VAR argI]))))),
586 | _ => (fdec, NONE) (* sorry, can't do that *)
587 (* (PPFlint.printLexp bodyRet; bug "couldn't find the returned record") *)