Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/target-cpu/gen-strand.sml
ViewVC logotype

View of /branches/vis15/src/compiler/target-cpu/gen-strand.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3955 - (download) (annotate)
Sun Jun 12 15:55:01 2016 UTC (3 years, 1 month ago) by jhr
File size: 3948 byte(s)
  Working on merge: code generation
(* gen-strand.sml
 *
 * Code generation for strands
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

structure GenStrand : sig

    val gen : CodeGenEnv.t * TreeIR.strand -> {
	    structDef : CLang.decl,
	    methods : CLang.decl list
	  }

  end = struct

    structure IR = TreeIR
    structure SV = TreeStateVar
    structure CL = CLang
    structure RN = CxxNames
    structure Env = CodeGenEnv

    val strandStatusTy = CL.T_Named "diderot::strand_status"

  (* generate the strand-state struct declaration *)
    fun genStrandStruct (env, strandName, state) = let
	  fun mkField sv = (TreeToCxx.trType(env, SV.ty sv), SV.qname sv)
	  in
	    CL.D_StructDef(SOME(strandName ^ "_strand"), List.map mkField state, NONE)
	  end

  (* generate the strand-state initialization code.  The variables are the strand
   * parameters.
   *)
    fun genStateInit (env, strandName, strandTy, params, def) = let
	  val IR.Method{usesGlobals, needsWorld, body} = def
	  val fName = strandName ^ "_init"
	  val (env, params) = List.foldr
		(fn (x, (env, ps)) => let
		    val (env, p) = TreeToCxx.trParam(env, x)
		    in
		      (env, p::ps)
		    end)
		  (env, []) params
	  val selfParam = if TargetSpec.dualState(Env.target env)
		then RN.selfOutVar
		else RN.selfVar
	  val params =
		CL.PARAM([], strandTy, selfParam) :: params
	  val params = if usesGlobals
		then RN.globalsParam :: params
		else params
	  val params = if needsWorld
		then RN.worldParam :: params
		else params
	  val body = TreeToCxx.trBlock (env, body)
	  in
	    CL.D_Func(["static"], CL.voidTy, fName, params, body)
	  end

  (* generate a function definition for a strand method *)
    fun genMethodDef (env, strandName, strandTy, methTy, methName, usesGlobals, needsWorld, body) = let
	  val fName = concat[strandName, "_", methName]
	  val params = if TargetSpec.dualState(Env.target env)
		then [
		    CL.PARAM(["const"], strandTy, RN.selfVar),
		    CL.PARAM([], strandTy, RN.selfOutVar)
		  ]
		else [CL.PARAM([], strandTy, RN.selfVar)]
	  val params = if usesGlobals
		then RN.globalsParam :: params
		else params
	  val params = if needsWorld
		then RN.worldParam :: params
		else params
	  in
	    CL.D_Func(["static"], methTy, fName, params, body)
	  end

  (* generate a function definition for a strand method *)
    fun genMethod (env, strandName, strandTy, methTy, methName, IR.Method{usesGlobals, needsWorld, body}) =
	  genMethodDef (
	    env, strandName, strandTy, methTy, methName,
	    usesGlobals, needsWorld, TreeToCxx.trBlock (env, body))

    fun genInitiallyMethod (_, _, _, NONE) = []
      | genInitiallyMethod (env, strandName, strandTy, SOME meth) =
	  [genMethod (env, strandName, strandTy, strandStatusTy, "initially", meth)]

    fun genUpdateMethod (env, strandName, strandTy, meth) =
	  genMethod (env, strandName, strandTy, strandStatusTy, "update", meth)

    fun genStabilizeMethod (env, strandName, strandTy, NONE) =
	  if TargetSpec.dualState(Env.target env)
	    then raise Fail "FIXME: default stabilize for dual state"
	    else [
		genMethodDef (
		  env, strandName, strandTy, CL.voidTy, "stabilize", false, false, CL.mkBlock[])
	      ]
      | genStabilizeMethod (env, strandName, strandTy, SOME meth) =
	  [genMethod (env, strandName, strandTy, CL.voidTy, "stabilize", meth)]

    fun gen (env, strand) = let
	  val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
	  val name = Atom.toString name
	  val strandTy = CL.T_Ptr(CL.T_Named(name ^ "_strand"))
	  in {
	    structDef = genStrandStruct (env, name, state),
	    methods = genStateInit (env, name, strandTy, params, stateInit) ::
	      genInitiallyMethod (env, name, strandTy, initM) @
	      genUpdateMethod (env, name, strandTy, updateM) ::
	      genStabilizeMethod (env, name, strandTy, stabilizeM)
	  } end

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0