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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/cxx-util/type-to-cxx.sml
ViewVC logotype

View of /branches/vis15/src/compiler/cxx-util/type-to-cxx.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4032 - (download) (annotate)
Thu Jun 23 05:27:06 2016 UTC (3 years, 2 months ago) by jhr
File size: 3722 byte(s)
  Working on merge: getting namespaces right
(* type-to-cxx.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 TypeToCxx : sig

  (* the scope in which a type will appear *)
    datatype namespace = NSDiderot | NSProgram | NSTopLevel

  (* translate a type for use inside the program's namespace scope
   * `trType (env, ty)` is the same as `trQType (env, Program, ty)`
   *)
    val trType : CodeGenEnv.t * TreeTypes.t -> CLang.ty

  (* translate a type for use outside the program's namespace scope *)
    val trQType : CodeGenEnv.t * namespace * TreeTypes.t -> CLang.ty

    val dynseqTy : CodeGenEnv.t * TreeTypes.t -> CLang.ty

    val trAPIType : CodeGenEnv.t * APITypes.t -> CLang.ty

  end = struct

    structure ATy = APITypes
    structure Ty = TreeTypes
    structure CL = CLang
    structure Env = CodeGenEnv
    structure RN = CxxNames

    datatype namespace = NSDiderot | NSProgram | NSTopLevel

    fun trQType (env, ns, ty) = let
	  val progNS = #namespace(Env.target env)
	  fun diderotQ s = (case ns
		 of NSDiderot => CL.T_Named s
		  | _ => CL.T_Named("diderot::" ^ s)
		(* end case *))
	  fun diderotTQ (s, args) = (case ns
		 of NSDiderot => CL.T_Template(s, args)
		  | _ => CL.T_Template("diderot::" ^ s, args)
		(* end case *))
	  fun programQ s = (case ns
		 of NSProgram => CL.T_Named s
		  | _ => CL.T_Named(concat[progNS, "::",  s])
		(* end case *))
	  fun tr ty = (case ty
	       of Ty.BoolTy => CL.boolTy
		| Ty.StringTy => CL.T_Named "std::string"
		| Ty.IntTy => Env.intTy env
		| (Ty.VecTy(1, 1)) => Env.realTy env
		| (Ty.VecTy(d, _)) => CL.T_Named("vec" ^ Int.toString d)
		| (Ty.TensorTy []) => Env.realTy env
		| (Ty.TensorTy dd) => programQ(RN.tensorStruct dd)
		| (Ty.TensorRefTy _) => CL.constPtrTy(Env.realTy env)
		| (Ty.TupleTy tys) => raise Fail "FIXME: TupleTy"
		| (Ty.SeqTy(ty, NONE)) => diderotTQ("dynseq", [tr ty])
		| (Ty.SeqTy(ty, SOME n)) => CL.T_Array(tr ty, SOME n)
		| (Ty.ImageTy info) =>
		    diderotTQ (
		      concat["image", Int.toString(ImageInfo.dim info), "d"],
		      [Env.realTy env])
		| (Ty.StrandTy name) => programQ("strand_" ^ Atom.toString name)
	      (* end case *))
	  in
	    tr ty
	  end

    fun trType (env, ty) = (case ty
	   of Ty.BoolTy => CL.boolTy
	    | Ty.StringTy => CL.T_Named "std::string"
	    | Ty.IntTy => Env.intTy env
	    | (Ty.VecTy(1, 1)) => Env.realTy env
	    | (Ty.VecTy(d, _)) => CL.T_Named("vec" ^ Int.toString d)
	    | (Ty.TensorTy []) => Env.realTy env
	    | (Ty.TensorTy dd) => RN.tensorTy dd
	    | (Ty.TensorRefTy _) => CL.constPtrTy(Env.realTy env)
	    | (Ty.TupleTy tys) => raise Fail "FIXME: TupleTy"
	    | (Ty.SeqTy(ty, NONE)) => dynseqTy (env, ty)
	    | (Ty.SeqTy(ty, SOME n)) => CL.T_Array(trType(env, ty), SOME n)
	    | (Ty.ImageTy info) =>
		CL.T_Template(
		  concat["diderot::image", Int.toString(ImageInfo.dim info), "d"],
		  [Env.realTy env])
	    | (Ty.StrandTy name) => CL.T_Named("strand_" ^ Atom.toString name)
	  (* end case *))

  (* dynamic sequence of the specified element type *)
    and dynseqTy (env, ty) = CL.T_Template("diderot::dynseq", [trType(env, ty)])

    fun trAPIType (env, ty) = (case ty
	   of ATy.IntTy => Env.intTy env
	    | ATy.BoolTy => CL.boolTy
	    | ATy.TensorTy[] => Env.realTy env
	    | ATy.TensorTy dd => RN.tensorTy dd
	    | ATy.StringTy => CL.T_Named "std::string"
	    | ATy.ImageTy(dim, shp) =>
		CL.T_Template(concat["diderot::image", Int.toString dim, "d"], [Env.realTy env])
	    | ATy.SeqTy(ty, NONE) => CL.T_Template("diderot::dynseq", [trAPIType(env, ty)])
	    | ATy.SeqTy(ty, SOME n) => CL.T_Array(trAPIType(env, ty), SOME n)
	  (* end case *))

  end

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