making the stack explicit

Daniel Wang danwang@cs.princeton.edu
02 Dec 1999 17:13:52 -0500


"Stephen Weeks" <sweeks@intertrust.com> writes:

> Dan, maybe you already figured this out, but I just understood this

> Anyways, I may be rambling a bit, but my main point is that maybe you
> can get away without creating a new IL at all.

Thanks a bunch..  I'll still need in IL that uses region variables to track
the life time of objects so I know when it's safe to free things, but now it
seems like I can reuse MLton to do a lot more work for me. Which is really
good.  

I'll just need to feed the output of MLton with a few extra passes to my
infrastructure which will region annotate the program add a garbage
collector to the output of MLton, convert the region primtives in to
primitive C calls that acess my runtime library and feed the whole thing
back to MLton to optimize like mad!

BTW, I've included a  worked out a example of what output 
I need from MLton below just to make sure we're on the same wave length...

structure ANF =
  struct
    fun f () = 1
    fun g () = 1.0 
    fun h () = let
      val t = f()
    in t + 2
    end

    val x = f()
    val y = g()
    val z = h()
    val ans = (x,y,z)
  end

structure CPS =
  struct
    exception HALT of (int * real * int)
    fun halt x = raise (HALT x)

    fun f (k,()) = k 1
    fun g (k,()) = k 1.0
    fun h (k,()) = let 
      fun ret_t (t) = k (t + 2)
    in f(ret_t,())
    end

    fun ret_x (x) = let
      fun ret_y (y) = let
	fun ret_z (z) = (halt (x,y,z))
      in h(ret_z,()) end
    in g(ret_y,()) end
   val ans = f(ret_x,()) handle (HALT x) => x

  end

structure FOL_CPS =
  struct
    exception HALT of (int * real * int)
    fun halt x = raise (HALT x)

    datatype int_cont  =
      Ret_X  | Ret_T of int_cont | Ret_Z of int * real
    and real_cont = Ret_Y of int

    fun app_int (Ret_X,x) = g (Ret_Y x,())
      | app_int (Ret_T k,t) = app_int (k,t + 2)
      | app_int (Ret_Z(x,y),z) = halt(x,y,z)
    and app_real (Ret_Y x,y)  = h (Ret_Z(x,y),())
    and f (k,()) = app_int  (k,1)
    and g (k,()) = app_real (k,1.0)
    and h (k,()) = f(Ret_T k,())
    val ans = f(Ret_X,()) handle (HALT x) => x
  end ;

val (anf,cps,fol_cps) = (ANF.ans,CPS.ans,FOL_CPS.ans)