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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/codegen/codegen-env.sml
ViewVC logotype

View of /branches/vis15/src/compiler/codegen/codegen-env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3896 - (download) (annotate)
Sun May 22 18:24:49 2016 UTC (3 years, 6 months ago) by jhr
File size: 2483 byte(s)
working on merge: code generation
(* codegen-env.sml
 *
 * 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 CodeGenEnv : sig

    type t

    val new : {
	    spec : TargetSpec.t,	(* target spec *)
	    global : CLang.var,		(* variable for accessing globals *)
	    selfIn : CLang.var,		(* variable for accessing input strand state *)
	    selfOut : CLang.var		(* variable for accessing output strand state *)
	  } -> t

    val empty : TargetSpec.t -> t

  (* get the target spec record from the environment *)
    val target : t -> TargetSpec.t

    val lookup : t * TreeVar.t -> CLang.var

    val insert : t * TreeVar.t * CLang.var -> t

  (* lookup the bindings for accessing the globals and strand state *)
    val global : t -> CLang.var
    val selfIn : t -> CLang.var
    val selfOut : t -> CLang.var

  (* get CLang versions of standard Diderot types *)
    val realTy  : t -> CLang.ty
    val intTy   : t -> CLang.ty
    val boolTy  : t -> CLang.ty

  (* CLang versions of templated runtime types *)
    val optionsTy : t -> CLang.ty

  end = struct

    structure V = TreeVar
    structure VMap = TreeVar.Map
    structure CL = CLang

    datatype t = Env of {
	vmap : CL.var VMap.map,
	spec : TargetSpec.t
      }

    fun new {spec, global, selfIn, selfOut} = Env{
	    vmap = List.foldl VMap.insert' VMap.empty [
		(PseudoVars.global, global),
		(PseudoVars.selfIn, selfIn),
		(PseudoVars.selfOut, selfOut)
	      ],
	    spec = spec
	  }

    fun empty spec = Env{
	    vmap = VMap.empty,
	    spec = spec
	  }

    fun target (Env{spec, ...}) = spec

    fun lookup (Env{vmap, ...}, x) = (case VMap.find (vmap, x)
           of SOME x' => x'
            | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
          (* end case *))

    fun insert (Env{vmap, spec}, x, x') = Env{vmap = VMap.insert(vmap, x, x'), spec = spec}

    fun global env = lookup(env, PseudoVars.global)
    fun selfIn env = lookup(env, PseudoVars.selfIn)
    fun selfOut env = lookup(env, PseudoVars.selfOut)

    fun realTy (Env{spec, ...}) = if (#double spec) then CL.double else CL.float
    fun intTy (Env{spec, ...}) = if (#longint spec) then CL.int64 else CL.int32
(* FIXME: to support versions of C without <stdbool>, we will need to map bool to int *)
    fun boolTy (Env{spec, ...}) = CL.boolTy

    fun optionsTy env = CL.T_Template("diderot::options", [realTy env, intTy env])

  end

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