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 3917 - (download) (annotate)
Sat May 28 16:41:39 2016 UTC (4 years ago) by jhr
File size: 3066 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 -> CLang.decl list

  end = struct

    structure IR = TreeIR
    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, TreeStateVar.ty sv), TreeStateVar.name 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, body) = let
	  val fName = strandName ^ "_init"
	  val selfParam = if TargetSpec.dualState(Env.target env)
		then RN.selfOutVar
		else RN.selfVar
	  val params =
		CL.PARAM([], strandTy, selfParam) :: params
	  val params = if #hasGlobals(Env.target env)
		then RN.globalsParam :: 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 genMethod (env, strandName, strandTy, methTy, methName, body) = let
	  val fName = concat[strandName, "_", methName]
	  val stateParams = 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 #hasGlobals(Env.target env)
		then RN.globalsParam :: stateParams
		else stateParams
	  val body = TreeToCxx.trBlock (env, body)
	  in
	    CL.D_Func(["static"], methTy, fName, params, body)
	  end

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

    fun genUpdateMethod (spec, strandName, strandTy, body) =
	  genMethod (spec, strandName, strandTy, strandStatusTy, "update", body)

    fun genStabilizeMethod (_, _, _, NONE) = []
      | genStabilizeMethod (spec, strandName, strandTy, SOME body) =
	  [genMethod (spec, strandName, strandTy, CL.voidTy, "stabilize", body)]

    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"))
	  val params = List.map (TreeToCxx.trParam env) params
	  in
	    genStrandStruct (env, name, state) ::
	    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