1 (* Copyright 1999,2004,2007,2010-2012 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 =
60 val tut = fn (x,y) z y e r =>
62 val tut = fn (x,y) => fn z y => fn e r =>
72 val x = 1 in val x = x end
74 local val x = 1 in val x = x end
75 local val x = 1 in val x = x end
76 local val x = 1 in val x = x end (* fixindent *)
77 local val x = 1 in val x = x end
89 (* From "Christopher Dutchyn" <cdutchyn@cs.ubc.ca> *)
91 (* This is actually not valid SML anyway. *)
99 (* Testing obedience to user overrides: *)
100 x := 3; (* fixindent *)
116 datatype foo = FOO | BAR of baz
117 and baz = BAZ | QUUX of foo
126 and baz = BAZ (* fixindent *)
130 datatype foo = datatype M.foo
133 signature S = S' where type foo = int
138 , let val x = f 42 in g (x,x,44) end
143 let val x = f 42 in g (x,x,44) end
149 let val x = f 42 in g (x,x,44) end
153 , let val x = f 42 in g (x,x,44) end
154 , foldl (fn ((p,q),s) => g (p,q,Vector.length q) ^ ":" ^ s)
155 "" (Beeblebrox.masterCountList mlist2)
156 , if null mlist2 then ";" else ""
159 fun foo (true::rest) = 1 + 2 * foo rest
161 = let val _ = 1 in 2 end
174 then 2 (* Could also be indented by a basic offset. *)
183 F.APP(F.VAR fl, OU.filter filt vs)
260 structure Foo = struct
264 structure Foo = struct val x = 1
269 type flint = FLINT.prog
270 val split: flint -> flint * flint option
273 structure FSplit :> FSPLIT =
278 structure S = IntRedBlackSet
279 structure M = FLINTIntMap
281 structure OU = OptUtils
282 structure FU = FlintUtil
283 structure LT = LtyExtern
284 structure PO = PrimOp
285 structure PP = PPFlint
286 structure CTRL = FLINT_Control
289 val say = Control_Print.say
290 fun bug msg = ErrorMsg.impossible ("FSplit: "^msg)
291 fun buglexp (msg,le) = (say "\n"; PP.printLexp le; say " "; bug msg)
292 fun bugval (msg,v) = (say "\n"; PP.printSval v; say " "; bug msg)
293 fun assert p = if p then () else bug ("assertion failed")
296 val mklv = LambdaVar.mkLvar
297 val cplv = LambdaVar.dupLvar
299 fun S_rmv(x, s) = S.delete(s, x) handle NotFound => s
301 fun addv (s,F.VAR lv) = S.add(s, lv)
303 fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
304 fun rmvs (s,lvs) = foldl (fn (l,s) => S_rmv(l, s)) s lvs
308 fun split (fdec as (fk,f,args,body)) = let
309 val {getLty,addLty,...} = Recover.recover (fdec, false)
311 val m = Intmap.new(64, Unknown)
312 fun addpurefun f = Intmap.add m (f, false)
313 fun funeffect f = (Intmap.map m f) handle Uknown => true
315 (* sexp: env -> lexp -> (leE, leI, fvI, leRet)
316 * - env: IntSetF.set current environment
317 * - lexp: lexp expression to split
318 * - leRet: lexp the core return expression of lexp
319 * - leE: lexp -> lexp recursively split lexp: leE leRet == lexp
320 * - leI: lexp option inlinable part of lexp (if any)
321 * - fvI: IntSetF.set free variables of leI: FU.freevars leI == fvI
323 * sexp splits the lexp into an expansive part and an inlinable part.
324 * The inlinable part is guaranteed to be side-effect free.
325 * The expansive part doesn't bother to eliminate unused copies of
326 * elements copied to the inlinable part.
327 * If the inlinable part cannot be constructed, leI is set to F.RET[].
328 * This implies that fvI == S.empty, which in turn prevents us from
329 * mistakenly adding anything to leI.
331 fun sexp env lexp = (* fixindent *)
333 (* non-side effecting binds are copied to leI if exported *)
334 fun let1 (le,lewrap,lv,vs,effect) =
335 let val (leE,leI,fvI,leRet) = sexp (S.add(env, lv)) le
336 val leE = lewrap o leE
337 in if effect orelse not (S.member(fvI, lv))
338 then (leE, leI, fvI, leRet)
339 else (leE, lewrap leI, addvs(S_rmv(lv, fvI), vs), leRet)
343 (* we can completely move both RET and TAPP to the I part *)
344 of F.RECORD (rk,vs,lv,le as F.RET [F.VAR lv']) =>
346 then (fn e => e, lexp, addvs(S.empty, vs), lexp)
347 else (fn e => e, le, S.singleton lv', le)
349 (fn e => e, lexp, addvs(S.empty, vs), lexp)
350 | F.TAPP (F.VAR tf,tycs) =>
351 (fn e => e, lexp, S.singleton tf, lexp)
353 (* recursive splittable lexps *)
354 | F.FIX (fdecs,le) => sfix env (fdecs, le)
355 | F.TFN (tfdec,le) => stfn env (tfdec, le)
358 | F.CON (dc,tycs,v,lv,le) =>
359 let1(le, fn e => F.CON(dc, tycs, v, lv, e), lv, [v], false)
360 | F.RECORD (rk,vs,lv,le) =>
361 let1(le, fn e => F.RECORD(rk, vs, lv, e), lv, vs, false)
362 | F.SELECT (v,i,lv,le) =>
363 let1(le, fn e => F.SELECT(v, i, lv, e), lv, [v], false)
364 | F.PRIMOP (po,vs,lv,le) =>
365 let1(le, fn e => F.PRIMOP(po, vs, lv, e), lv, vs, PO.effect(#2 po))
367 (* IMPROVEME: lvs should not be restricted to [lv] *)
368 | F.LET(lvs as [lv],body as F.TAPP (v,tycs),le) =>
369 let1(le, fn e => F.LET(lvs, body, e), lv, [v], false)
370 | F.LET (lvs as [lv],body as F.APP (v as F.VAR f,vs),le) =>
371 let1(le, fn e => F.LET(lvs, body, e), lv, v::vs, funeffect f)
373 | F.SWITCH (v,ac,[(dc as F.DATAcon(_,_,lv),le)],NONE) =>
374 let1(le, fn e => F.SWITCH(v, ac, [(dc, e)], NONE), lv, [v], false)
376 | F.LET (lvs,body,le) =>
377 let val (leE,leI,fvI,leRet) = sexp (S.union(S.addList(S.empty, lvs), env)) le
378 in (fn e => F.LET(lvs, body, leE e), leI, fvI, leRet)
381 (* useless sophistication *)
382 | F.APP (F.VAR f,args) =>
384 then (fn e => e, F.RET[], S.empty, lexp)
385 else (fn e => e, lexp, addvs(S.singleton f, args), lexp)
387 (* other non-binding lexps result in unsplittable functions *)
388 | (F.APP _ | F.TAPP _) => bug "strange (T)APP"
389 | (F.SWITCH _ | F.RAISE _ | F.BRANCH _ | F.HANDLE _) =>
390 (fn e => e, F.RET[], S.empty, lexp)
393 (* Functions definitions fall into the following categories:
394 * - inlinable: if exported, copy to leI
395 * - (mutually) recursive: don't bother
396 * - non-inlinable non-recursive: split recursively *)
397 and sfix env (fdecs,le) =
398 let val nenv = S.union(S.addList(S.empty, map #2 fdecs), env)
399 val (leE,leI,fvI,leRet) = sexp nenv le
400 val nleE = fn e => F.FIX(fdecs, leE e)
402 of [({inline=inl as (F.IH_ALWAYS | F.IH_MAYBE _),...},f,args,body)] =>
403 let val min = case inl of F.IH_MAYBE(n,_) => n | _ => 0
404 in if not(S.member(fvI, f)) orelse min > !CTRL.splitThreshold
405 then (nleE, leI, fvI, leRet)
406 else (nleE, F.FIX(fdecs, leI),
407 rmvs(S.union(fvI, FU.freevars body),
411 | [fdec as (fk as {cconv=F.CC_FCT,...},_,_,_)] =>
412 sfdec env (leE,leI,fvI,leRet) fdec
414 | _ => (nleE, leI, fvI, leRet)
417 and sfdec env (leE,leI,fvI,leRet) (fk,f,args,body) =
418 let val benv = S.union(S.addList(S.empty, map #1 args), env)
419 val (bodyE,bodyI,fvbI,bodyRet) = sexp benv body
422 (fn e => F.FIX([(fk, f, args, bodyE bodyRet)], e),
425 let val fvbIs = S.listItems(S.difference(fvbI, benv))
426 val (nfk,fkE) = OU.fk_wrap(fk, NONE)
430 val fErets = (map F.VAR fvbIs)
431 val bodyE = bodyE(F.RET fErets)
433 val bodyE = bodyE(F.RECORD(F.RK_STRUCT, map F.VAR fvbIs,
434 tmp, F.RET[F.VAR tmp])) *)
435 val fdecE = (fkE, fE, args, bodyE)
436 val fElty = LT.ltc_fct(map #2 args, map getLty fErets)
437 val _ = addLty(fE, fElty)
440 val fkI = {inline=F.IH_ALWAYS, cconv=F.CC_FCT,
441 known=true, isrec=NONE}
443 (map (fn lv => (lv, getLty(F.VAR lv))) fvbIs) @ args
444 val fdecI as (_,fI,_,_) = FU.copyfdec(fkI,f,argsI,bodyI)
445 val _ = addpurefun fI
448 val nargs = map (fn (v,t) => (cplv v, t)) args
449 val argsv = map (fn (v,t) => F.VAR v) nargs
451 let val lvs = map cplv fvbIs
452 in F.LET(lvs, F.APP(F.VAR fE, argsv),
453 F.APP(F.VAR fI, (map F.VAR lvs)@argsv))
455 (* let val lv = mklv()
456 in F.LET([lv], F.APP(F.VAR fE, argsv),
457 F.APP(F.VAR fI, (F.VAR lv)::argsv))
459 val nfdec = (nfk, f, nargs, nbody)
461 (* and now, for the whole F.FIX *)
463 F.FIX([fdecE], F.FIX([fdecI], F.FIX([nfdec], leE e)))
465 in if not(S.member(fvI, f)) then (nleE, leI, fvI, leRet)
467 F.FIX([fdecI], F.FIX([nfdec], leI)),
468 S.add(S.union(S_rmv(f, fvI), S.intersection(env, fvbI)), fE),
473 (* TFNs are kinda like FIX except there's no recursion *)
474 and stfn env (tfdec as (tfk,tf,args,body),le) =
475 let val (bodyE,bodyI,fvbI,bodyRet) =
476 if #inline tfk = F.IH_ALWAYS
477 then (fn e => body, body, FU.freevars body, body)
479 val nenv = S.add(env, tf)
480 val (leE,leI,fvI,leRet) = sexp nenv le
481 in case (bodyI, S.listItems(S.difference(fvbI, env)))
482 of ((F.RET _ | F.RECORD(_,_,_,F.RET _)),_) =>
484 (fn e => F.TFN((tfk, tf, args, bodyE bodyRet), leE e),
487 (* everything was split out *)
488 let val ntfdec = ({inline=F.IH_ALWAYS}, tf, args, bodyE bodyRet)
489 val nlE = fn e => F.TFN(ntfdec, leE e)
490 in if not(S.member(fvI, tf)) then (nlE, leI, fvI, leRet)
491 else (nlE, F.TFN(ntfdec, leI),
492 S_rmv(tf, S.union(fvI, fvbI)), leRet)
497 val tfEvs = map F.VAR fvbIs
498 val bodyE = bodyE(F.RET tfEvs)
499 val tfElty = LT.lt_nvpoly(args, map getLty tfEvs)
500 val _ = addLty(tfE, tfElty)
503 val tfkI = {inline=F.IH_ALWAYS}
504 val argsI = map (fn (v,k) => (cplv v, k)) args
505 (* val tmap = ListPair.map (fn (a1,a2) =>
506 * (#1 a1, LT.tcc_nvar(#1 a2)))
508 val bodyI = FU.copy tmap M.empty
509 (F.LET(fvbIs, F.TAPP(F.VAR tfE, map #2 tmap),
513 F.TFN((tfk, tfE, args, bodyE),
514 F.TFN((tfkI, tf, argsI, bodyI), leE e))
516 in if not(S.member(fvI, tf)) then (nleE, leI, fvI, leRet)
518 F.TFN((tfkI, tf, argsI, bodyI), leI),
519 S.add(S.union(S_rmv(tf, fvI), S.intersection(env, fvbI)), tfE),
524 (* here, we use B-decomposition, so the args should not be
525 * considered as being in scope *)
526 val (bodyE,bodyI,fvbI,bodyRet) = sexp S.empty body
527 in case (bodyI, bodyRet)
528 of (F.RET _,_) => ((fk, f, args, bodyE bodyRet), NONE)
529 | (_,F.RECORD (rk,vs,lv,F.RET[lv'])) =>
530 let val fvbIs = S.listItems fvbI
533 val bodyE = bodyE(F.RECORD(rk, vs@(map F.VAR fvbIs), lv, F.RET[lv']))
534 val fdecE as (_,fE,_,_) = (fk, cplv f, args, bodyE)
538 val argLtys = (map getLty vs) @ (map (getLty o F.VAR) fvbIs)
539 val argsI = [(argI, LT.ltc_str argLtys)]
540 val (_,bodyI) = foldl (fn (lv,(n,le)) =>
541 (n+1, F.SELECT(F.VAR argI, n, lv, le)))
542 (length vs, bodyI) fvbIs
543 val fdecI as (_,fI,_,_) = FU.copyfdec (fk, f, argsI, bodyI)
545 val nargs = map (fn (v,t) => (cplv v, t)) args
552 F.APP(F.VAR fE, map (F.VAR o #1) nargs),
553 F.APP(F.VAR fI, [F.VAR argI]))))),
557 | _ => (fdec, NONE) (* sorry, can't do that *)
558 (* (PPFlint.printLexp bodyRet; bug "couldn't find the returned record") *)