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

SCM Repository

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

View of /branches/vis15/src/compiler/c-util/tree-to-c.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3810 - (download) (annotate)
Tue May 3 23:26:44 2016 UTC (2 years, 10 months ago) by jhr
File size: 4655 byte(s)
  Working on merge
(* tree-to-c.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.
 *
 * Translate TreeIR to the C version of CLang.
 *)

structure PseudoVars =
  struct
(* TreeIR "variables" that are used to get the names needed to access the
 * global and strand state variables.  These are just used as keys to lookup
 * the C names in the environment, so their kind and type are irrelevant.
 *)
    local
      fun new name = TreeIR.Var.new (name, TreeIR.Ty.IntTy)
    in
    val selfIn = new "$selfIn"
    val selfOut = new "$selfOut"
    val global = new "$global"
    end (* local *)
  end

structure TreeToC : sig

    type env = CLang.typed_var TreeIR.Var.Map.map

    val empty : env

    val trType : TreeTypes.t -> CLang.ty

    val trBlock : env * TreeIR.block -> CLang.stm

    val trFragment : env * TreeIR.block -> env * CLang.stm list

    val trExp : env * TreeIR.exp -> CLang.exp

  (* translate an expression to a variable form; return the variable (as an expresison)
   * and the (optional) declaration.
   *)
    val expToVar : env * CLang.ty * string * TreeIR.exp -> CLang.exp * CLang.stm list

    val trAssign : env * CLang.exp * TreeIR.exp -> CLang.stm list

  end = struct

    structure CL = CLang
    structure IR = TreeIR
    structure Op = TreeOps
    structure Ty = TreeTypes
    structure V = IR.Var

    datatype var = datatype CL.typed_var
    type env = CL.typed_var TreeIR.Var.Map.map

    val empty = V.Map.empty

    fun lookup (env, x) = (case V.Map.find (env, x)
           of SOME(V(_, x')) => x'
            | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
          (* end case *))


  (* Translate a TreeIR operator application to a CLang expression *)
    fun trOp (rator, args) = (case (rator, args)
	    | (Op.IAdd, [a, b]) => CL.mkBinOp(a, CL.#+, b)
	    | (Op.ISub, [a, b]) => CL.mkBinOp(a, CL.#-, b)
	    | (Op.IMul, [a, b]) => CL.mkBinOp(a, CL.#*, b)
	    | (Op.IDiv, [a, b]) => CL.mkBinOp(a, CL.#/, b)
	    | (Op.IMod, [a, b]) => CL.mkBinOp(a, CL.#%, b)
	    | (Op.INeg, [a]) => CL.mkUnOp(CL.%-, a)
	    | (Op.RAdd, [a, b]) => CL.mkBinOp(a, CL.#+, b)
	    | (Op.RSub, [a, b]) => CL.mkBinOp(a, CL.#-, b)
	    | (Op.RMul, [a, b]) => CL.mkBinOp(a, CL.#*, b)
	    | (Op.RDiv, [a, b]) => CL.mkBinOp(a, CL.#/, b)
	    | (Op.RNeg, [a]) => CL.mkUnOp(CL.%-, a)
            | (Op.LT ty, [a, b]) => CL.mkBinOp(a, CL.#<, b)
            | (Op.LTE ty, [a, b]) => CL.mkBinOp(a, CL.#<=, b)
            | (Op.EQ ty, [a, b]) => CL.mkBinOp(a, CL.#==, b)
            | (Op.NEQ ty, [a, b]) => CL.mkBinOp(a, CL.#!=, b)
            | (Op.GTE ty, [a, b]) => CL.mkBinOp(a, CL.#>=, b)
            | (Op.GT ty, [a, b]) => CL.mkBinOp(a, CL.#>, b)
            | (Op.Not, [a]) => CL.mkUnOp(CL.%!, a)
	    | (Op.Abs ty, []) =>
	    | (Op.Max ty, []) =>
	    | (Op.Min ty, []) =>
	    | (Op.Clamp ty, []) =>
	    | (Op.Lerp ty, []) =>
	    | (Op.VAdd d, [a, b]) =>
	    | (Op.VSub d, [a, b]) =>
	    | (Op.VScale d, [a, b]) =>
	    | (Op.VMul d, [a, b]) =>
	    | (Op.VNeg d, [a]) =>
	    | (Op.VSum d, [a]) =>
	    | (Op.TensorIndex(ty * shape), []) =>
	    | (Op.EigenVecs2x2, []) =>
	    | (Op.EigenVecs3x3, []) =>
	    | (Op.EigenVals2x2, []) =>
	    | (Op.EigenVals3x3, []) =>
	    | (Op.Zero ty, []) =>
	    | (Op.Select(ty * int), []) =>
	    | (Op.Subscript ty, []) =>
	    | (Op.MkDynamic(ty * int), []) =>
	    | (Op.Append ty, []) =>
	    | (Op.Prepend ty, []) =>
	    | (Op.Concat ty, []) =>
	    | (Op.Range, []) =>
	    | (Op.Length ty, []) =>
	    | (Op.SphereQuery(ty * ty), []) =>
	    | (Op.Sqrt, []) =>
	    | (Op.Cos, []) =>
	    | (Op.ArcCos, []) =>
	    | (Op.Sine, []) =>
	    | (Op.ArcSin, []) =>
	    | (Op.Tan, []) =>
	    | (Op.ArcTan, []) =>
	    | (Op.Exp, []) =>
	    | (Op.Ceiling d, []) =>
	    | (Op.Floor d, []) =>
	    | (Op.Round d, []) =>
	    | (Op.Trunc d, []) =>
	    | (Op.IntToReal, []) =>
	    | (Op.RealToInt d, []) =>
(*
	    | R_All of ty
	    | R_Exists of ty
	    | R_Max of ty
	    | R_Min of ty
	    | R_Sum of ty
	    | R_Product of ty
	    | R_Mean of ty
	    | R_Variance of ty
*)
	    | (Op.Transform(ImageInfo.info * int), []) =>
	    | (Op.Translate(ImageInfo.info), []) =>
	    | (Op.BaseAddress(ImageInfo.info), []) =>
	    | (Op.ControlIndex(ImageInfo.info * idxctl * int), []) =>
	    | (Op.Inside(ImageInfo.info * int), []) =>
	    | (Op.ImageDim(ImageInfo.info * int), []) =>
	    | (Op.LoadSeq(ty * string), []) =>
	    | (Op.LoadImage(ty * string), []) =>
	    | (Op.Print(tys), []) =>
	    | (Op.MathFn f, args) => CL.mkApply(??, args)
	  (* end case *))

  end

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