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

SCM Repository

[diderot] View of /branches/vis12/src/compiler/c-util/c-ty-translate.sml
ViewVC logotype

View of /branches/vis12/src/compiler/c-util/c-ty-translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1818 - (download) (annotate)
Tue Apr 10 22:18:20 2012 UTC (9 years ago) by jhr
Original Path: branches/vis12/src/compiler/c-target/c-ty-translate.sml
File size: 3919 byte(s)
  Working on factoring out the generation of code to translate between the internal
  Diderot representation of types (e.g., Diderot_vec3_t) and the external C API
  (e.g., float[3]).
(* c-ty-translate.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure CTyTranslate : TY_TRANSLATE =
  struct

    structure Ty = TreeIL.Ty
    structure CL = CLang
    structure N = CNames

  (* translate TreeIL types to CLang types *)
    fun trType ty = (case ty
           of Ty.BoolTy => CL.T_Named "bool"
            | Ty.StringTy => CL.charPtr
            | Ty.IntTy => !N.gIntTy
            | Ty.TensorTy[] => !N.gRealTy
            | Ty.TensorTy[n] => CL.T_Named(N.vecTy n)
            | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m))
            | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l))
            | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n)
            | Ty.SeqTy(ty, n) => CL.T_Array(trType ty, SOME n)
(* do we make the following types externally visible? *)
            | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
            | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
            | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
            | _ => raise Fail(concat["CTyTranslate.trType(", Ty.toString ty, ")"])
          (* end case *))

    fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i))

  (* generate code to copy values from their internal Diderot representation to the external
   * C representation.  If the representations are identical, then the empty list is returned.
   *)
    fun copyToC {ty, dst, src} = (case ty
           of Ty.BoolTy => []
            | Ty.StringTy => []
            | Ty.IntTy => []
            | Ty.TensorTy[] => []
            | Ty.TensorTy[n] => List.tabulate (n,
		fn i => CL.mkAssign(
		  subscript (dst, i),
		  subscript (CL.mkSelect(src, "r"), i)))
            | Ty.TensorTy[n, m] => List.tabulate(n,
		fn i => List.tabulate (m,
		  fn j => CL.mkAssign(
		    subscript (dst, i*m + j),
		    subscript(CL.mkSelect(subscript (src, i), "r"), j))))
            | Ty.TensorTy[n, m, l] => List.tabulate(n,
		fn i => List.tabulate (m,
		  fn j => List.tabulate (l,
		    fn k => CL.mkAssign(
		      subscript(dst, i*m*l + j*l + k),
		      subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j)))))
            | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n,
		fn i => CL.mkAssign(
		  subscript (dst, i),
		  subscript (CL.mkSelect(src, "i"), i)))
            | Ty.SeqTy(ty, n) =>
            | Ty.DynSeqTy _ =>
            | Ty.AddrTy info =>
            | Ty.ImageTy info =>
            | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
	  (* end case *))

  (* generate code to copy values from their external C representation to the internal
   * Diderot representation.  If the representations are identical, then the empty list
   * is returned.
   *)
    fun copyFromC {ty, dst, src} = (case ty
           of Ty.BoolTy => []
            | Ty.StringTy => []
            | Ty.IntTy => []
            | Ty.TensorTy[n] => CL.mkAssign(
		CL.mkSelect(dst, "v"),
		CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))
            | Ty.TensorTy[n, m] => List.tabulate(n,
		fn i => CL.mkAssign(
		  CL.mkSelect(subscript(dst, i), "v"),
		  CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j)))))
            | Ty.TensorTy[n, m, l] => List.tabulate(n,
		fn i => List.tabulate (m,
		  fn j => (
		    CL.mkSelect(subscript(subscript(dst, i), j), "v"),
		    CL.mkApply(N.mkVec n,
		      List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k))))))
            | Ty.SeqTy(Ty.IntTy, n) => CL.mkAssign(
		CL.mkSelect(dst, "v"),
		CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i))))
            | Ty.SeqTy(ty, n) =>
            | Ty.DynSeqTy _ =>
            | Ty.AddrTy info =>
            | Ty.ImageTy info =>
            | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
	  (* end case *))

  end

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