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 4349 - (download) (annotate)
Tue Aug 2 18:14:48 2016 UTC (3 years, 5 months ago) by jhr
File size: 4679 byte(s)
  Working on merge: spatial query support
(* 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)
            CL.D_StructDef(SOME(strandName ^ "_strand"), List.map mkField state, NONE)

  (* generate the strand-state initialization code.  The variables are the strand
   * parameters.
   * NOTE: the calling convention generated here must match that used in GenWorld.genInitiallyFun!
    fun genStateInit (env, strandName, strandTy, params, def) = let
          val IR.Method{needsW, hasG, body} = def
          val fName = strandName ^ "_init"
          val (env, params) = List.foldr
                (fn (x, (env, ps)) => let
                    val (env, p) = TreeToCxx.trParam(env, x)
                      (env, p::ps)
                  (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 hasG
                then RN.globalsParam :: params
                else params
          val params = if needsW
                then RN.worldParam :: params
                else params
          val body = TreeToCxx.trBlock (env, body)
            CL.D_Func(["static"], CL.voidTy, [], fName, params, body)

  (* generate a function definition for a strand method *)
    fun genMethodDef (env, strandName, strandTy, methTy, methName, needsWorld, usesGlobals, body) =
          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
            CL.D_Func(["static"], methTy, [], fName, params, body)

  (* generate a function definition for a strand method *)
    fun genMethod (env, strandName, strandTy, methTy, methName, IR.Method{needsW, hasG, body}) =
          genMethodDef (
            env, strandName, strandTy, methTy, methName,
            needsW, hasG, 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


ViewVC Help
Powered by ViewVC 1.0.0