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 *)
146 datatype foo = FOO | BAR of baz
147 and baz = BAZ | QUUX of foo
156 and baz = BAZ (* fixindent *)
160 datatype foo = datatype M.foo
163 signature S = S' where type foo = int
168 , let val x = f 42 in g (x,x,44) end
173 let val x = f 42 in g (x,x,44) end
179 let val x = f 42 in g (x,x,44) end
183 , let val x = f 42 in g (x,x,44) end
184 , foldl (fn ((p,q),s) => g (p,q,Vector.length q) ^ ":" ^ s)
185 "" (Beeblebrox.masterCountList mlist2)
186 , if null mlist2 then ";" else ""
189 fun foo (true::rest) = 1 + 2 * foo rest
191 = let val _ = 1 in 2 end
204 then 2 (* Could also be indented by a basic offset. *)
213 F.APP(F.VAR fl, OU.filter filt vs)
290 structure Foo = struct
294 structure Foo = struct val x = 1
299 type flint = FLINT.prog
300 val split: flint -> flint * flint option
303 structure FSplit :> FSPLIT =
308 structure S = IntRedBlackSet
309 structure M = FLINTIntMap
311 structure OU = OptUtils
312 structure FU = FlintUtil
313 structure LT = LtyExtern
314 structure PO = PrimOp
315 structure PP = PPFlint
316 structure CTRL = FLINT_Control
319 val say = Control_Print.say
320 fun bug msg = ErrorMsg.impossible ("FSplit: "^msg)
321 fun buglexp (msg,le) = (say "\n"; PP.printLexp le; say " "; bug msg)
322 fun bugval (msg,v) = (say "\n"; PP.printSval v; say " "; bug msg)
323 fun assert p = if p then () else bug ("assertion failed")
326 val mklv = LambdaVar.mkLvar
327 val cplv = LambdaVar.dupLvar
329 fun S_rmv(x, s) = S.delete(s, x) handle NotFound => s
331 fun addv (s,F.VAR lv) = S.add(s, lv)
333 fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
334 fun rmvs (s,lvs) = foldl (fn (l,s) => S_rmv(l, s)) s lvs
338 fun split (fdec as (fk,f,args,body)) = let
339 val {getLty,addLty,...} = Recover.recover (fdec, false)
341 val m = Intmap.new(64, Unknown)
342 fun addpurefun f = Intmap.add m (f, false)
343 fun funeffect f = (Intmap.map m f) handle Uknown => true
345 (* sexp: env -> lexp -> (leE, leI, fvI, leRet)
346 * - env: IntSetF.set current environment
347 * - lexp: lexp expression to split
348 * - leRet: lexp the core return expression of lexp
349 * - leE: lexp -> lexp recursively split lexp: leE leRet == lexp
350 * - leI: lexp option inlinable part of lexp (if any)
351 * - fvI: IntSetF.set free variables of leI: FU.freevars leI == fvI
353 * sexp splits the lexp into an expansive part and an inlinable part.
354 * The inlinable part is guaranteed to be side-effect free.
355 * The expansive part doesn't bother to eliminate unused copies of
356 * elements copied to the inlinable part.
357 * If the inlinable part cannot be constructed, leI is set to F.RET[].
358 * This implies that fvI == S.empty, which in turn prevents us from
359 * mistakenly adding anything to leI.
361 fun sexp env lexp = (* fixindent *)
363 (* non-side effecting binds are copied to leI if exported *)
364 fun let1 (le,lewrap,lv,vs,effect) =
365 let val (leE,leI,fvI,leRet) = sexp (S.add(env, lv)) le
366 val leE = lewrap o leE
367 in if effect orelse not (S.member(fvI, lv))
368 then (leE, leI, fvI, leRet)
369 else (leE, lewrap leI, addvs(S_rmv(lv, fvI), vs), leRet)
373 (* we can completely move both RET and TAPP to the I part *)
374 of F.RECORD (rk,vs,lv,le as F.RET [F.VAR lv']) =>
376 then (fn e => e, lexp, addvs(S.empty, vs), lexp)
377 else (fn e => e, le, S.singleton lv', le)
379 (fn e => e, lexp, addvs(S.empty, vs), lexp)
380 | F.TAPP (F.VAR tf,tycs) =>
381 (fn e => e, lexp, S.singleton tf, lexp)
383 (* recursive splittable lexps *)
384 | F.FIX (fdecs,le) => sfix env (fdecs, le)
385 | F.TFN (tfdec,le) => stfn env (tfdec, le)
388 | F.CON (dc,tycs,v,lv,le) =>
389 let1(le, fn e => F.CON(dc, tycs, v, lv, e), lv, [v], false)
390 | F.RECORD (rk,vs,lv,le) =>
391 let1(le, fn e => F.RECORD(rk, vs, lv, e), lv, vs, false)
392 | F.SELECT (v,i,lv,le) =>
393 let1(le, fn e => F.SELECT(v, i, lv, e), lv, [v], false)
394 | F.PRIMOP (po,vs,lv,le) =>
395 let1(le, fn e => F.PRIMOP(po, vs, lv, e), lv, vs, PO.effect(#2 po))
397 (* IMPROVEME: lvs should not be restricted to [lv] *)
398 | F.LET(lvs as [lv],body as F.TAPP (v,tycs),le) =>
399 let1(le, fn e => F.LET(lvs, body, e), lv, [v], false)
400 | F.LET (lvs as [lv],body as F.APP (v as F.VAR f,vs),le) =>
401 let1(le, fn e => F.LET(lvs, body, e), lv, v::vs, funeffect f)
403 | F.SWITCH (v,ac,[(dc as F.DATAcon(_,_,lv),le)],NONE) =>
404 let1(le, fn e => F.SWITCH(v, ac, [(dc, e)], NONE), lv, [v], false)
406 | F.LET (lvs,body,le) =>
407 let val (leE,leI,fvI,leRet) = sexp (S.union(S.addList(S.empty, lvs), env)) le
408 in (fn e => F.LET(lvs, body, leE e), leI, fvI, leRet)
411 (* useless sophistication *)
412 | F.APP (F.VAR f,args) =>
414 then (fn e => e, F.RET[], S.empty, lexp)
415 else (fn e => e, lexp, addvs(S.singleton f, args), lexp)
417 (* other non-binding lexps result in unsplittable functions *)
418 | (F.APP _ | F.TAPP _) => bug "strange (T)APP"
419 | (F.SWITCH _ | F.RAISE _ | F.BRANCH _ | F.HANDLE _) =>
420 (fn e => e, F.RET[], S.empty, lexp)
423 (* Functions definitions fall into the following categories:
424 * - inlinable: if exported, copy to leI
425 * - (mutually) recursive: don't bother
426 * - non-inlinable non-recursive: split recursively *)
427 and sfix env (fdecs,le) =
428 let val nenv = S.union(S.addList(S.empty, map #2 fdecs), env)
429 val (leE,leI,fvI,leRet) = sexp nenv le
430 val nleE = fn e => F.FIX(fdecs, leE e)
432 of [({inline=inl as (F.IH_ALWAYS | F.IH_MAYBE _),...},f,args,body)] =>
433 let val min = case inl of F.IH_MAYBE(n,_) => n | _ => 0
434 in if not(S.member(fvI, f)) orelse min > !CTRL.splitThreshold
435 then (nleE, leI, fvI, leRet)
436 else (nleE, F.FIX(fdecs, leI),
437 rmvs(S.union(fvI, FU.freevars body),
441 | [fdec as (fk as {cconv=F.CC_FCT,...},_,_,_)] =>
442 sfdec env (leE,leI,fvI,leRet) fdec
444 | _ => (nleE, leI, fvI, leRet)
447 and sfdec env (leE,leI,fvI,leRet) (fk,f,args,body) =
448 let val benv = S.union(S.addList(S.empty, map #1 args), env)
449 val (bodyE,bodyI,fvbI,bodyRet) = sexp benv body
452 (fn e => F.FIX([(fk, f, args, bodyE bodyRet)], e),
455 let val fvbIs = S.listItems(S.difference(fvbI, benv))
456 val (nfk,fkE) = OU.fk_wrap(fk, NONE)
460 val fErets = (map F.VAR fvbIs)
461 val bodyE = bodyE(F.RET fErets)
463 val bodyE = bodyE(F.RECORD(F.RK_STRUCT, map F.VAR fvbIs,
464 tmp, F.RET[F.VAR tmp])) *)
465 val fdecE = (fkE, fE, args, bodyE)
466 val fElty = LT.ltc_fct(map #2 args, map getLty fErets)
467 val _ = addLty(fE, fElty)
470 val fkI = {inline=F.IH_ALWAYS, cconv=F.CC_FCT,
471 known=true, isrec=NONE}
473 (map (fn lv => (lv, getLty(F.VAR lv))) fvbIs) @ args
474 val fdecI as (_,fI,_,_) = FU.copyfdec(fkI,f,argsI,bodyI)
475 val _ = addpurefun fI
478 val nargs = map (fn (v,t) => (cplv v, t)) args
479 val argsv = map (fn (v,t) => F.VAR v) nargs
481 let val lvs = map cplv fvbIs
482 in F.LET(lvs, F.APP(F.VAR fE, argsv),
483 F.APP(F.VAR fI, (map F.VAR lvs)@argsv))
485 (* let val lv = mklv()
486 in F.LET([lv], F.APP(F.VAR fE, argsv),
487 F.APP(F.VAR fI, (F.VAR lv)::argsv))
489 val nfdec = (nfk, f, nargs, nbody)
491 (* and now, for the whole F.FIX *)
493 F.FIX([fdecE], F.FIX([fdecI], F.FIX([nfdec], leE e)))
495 in if not(S.member(fvI, f)) then (nleE, leI, fvI, leRet)
497 F.FIX([fdecI], F.FIX([nfdec], leI)),
498 S.add(S.union(S_rmv(f, fvI), S.intersection(env, fvbI)), fE),
503 (* TFNs are kinda like FIX except there's no recursion *)
504 and stfn env (tfdec as (tfk,tf,args,body),le) =
505 let val (bodyE,bodyI,fvbI,bodyRet) =
506 if #inline tfk = F.IH_ALWAYS
507 then (fn e => body, body, FU.freevars body, body)
509 val nenv = S.add(env, tf)
510 val (leE,leI,fvI,leRet) = sexp nenv le
511 in case (bodyI, S.listItems(S.difference(fvbI, env)))
512 of ((F.RET _ | F.RECORD(_,_,_,F.RET _)),_) =>
514 (fn e => F.TFN((tfk, tf, args, bodyE bodyRet), leE e),
517 (* everything was split out *)
518 let val ntfdec = ({inline=F.IH_ALWAYS}, tf, args, bodyE bodyRet)
519 val nlE = fn e => F.TFN(ntfdec, leE e)
520 in if not(S.member(fvI, tf)) then (nlE, leI, fvI, leRet)
521 else (nlE, F.TFN(ntfdec, leI),
522 S_rmv(tf, S.union(fvI, fvbI)), leRet)
527 val tfEvs = map F.VAR fvbIs
528 val bodyE = bodyE(F.RET tfEvs)
529 val tfElty = LT.lt_nvpoly(args, map getLty tfEvs)
530 val _ = addLty(tfE, tfElty)
533 val tfkI = {inline=F.IH_ALWAYS}
534 val argsI = map (fn (v,k) => (cplv v, k)) args
535 (* val tmap = ListPair.map (fn (a1,a2) =>
536 * (#1 a1, LT.tcc_nvar(#1 a2)))
538 val bodyI = FU.copy tmap M.empty
539 (F.LET(fvbIs, F.TAPP(F.VAR tfE, map #2 tmap),
543 F.TFN((tfk, tfE, args, bodyE),
544 F.TFN((tfkI, tf, argsI, bodyI), leE e))
546 in if not(S.member(fvI, tf)) then (nleE, leI, fvI, leRet)
548 F.TFN((tfkI, tf, argsI, bodyI), leI),
549 S.add(S.union(S_rmv(tf, fvI), S.intersection(env, fvbI)), tfE),
554 (* here, we use B-decomposition, so the args should not be
555 * considered as being in scope *)
556 val (bodyE,bodyI,fvbI,bodyRet) = sexp S.empty body
557 in case (bodyI, bodyRet)
558 of (F.RET _,_) => ((fk, f, args, bodyE bodyRet), NONE)
559 | (_,F.RECORD (rk,vs,lv,F.RET[lv'])) =>
560 let val fvbIs = S.listItems fvbI
563 val bodyE = bodyE(F.RECORD(rk, vs@(map F.VAR fvbIs), lv, F.RET[lv']))
564 val fdecE as (_,fE,_,_) = (fk, cplv f, args, bodyE)
568 val argLtys = (map getLty vs) @ (map (getLty o F.VAR) fvbIs)
569 val argsI = [(argI, LT.ltc_str argLtys)]
570 val (_,bodyI) = foldl (fn (lv,(n,le)) =>
571 (n+1, F.SELECT(F.VAR argI, n, lv, le)))
572 (length vs, bodyI) fvbIs
573 val fdecI as (_,fI,_,_) = FU.copyfdec (fk, f, argsI, bodyI)
575 val nargs = map (fn (v,t) => (cplv v, t)) args
582 F.APP(F.VAR fE, map (F.VAR o #1) nargs),
583 F.APP(F.VAR fI, [F.VAR argI]))))),
587 | _ => (fdec, NONE) (* sorry, can't do that *)
588 (* (PPFlint.printLexp bodyRet; bug "couldn't find the returned record") *)