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 2024 - (download) (annotate)
Wed Oct 10 13:36:38 2012 UTC (7 years ago) by jhr
File size: 7192 byte(s)
  Working on dynamic sequences
(* c-ty-translate.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Functions to support the translation between the external C API and the internal (host-side)
 * Diderot representation.
 *)

signature TY_TRANSLATE =
  sig

  (* translate a type to its internal Diderot represenation *)
    val toType : TreeIL.Ty.ty -> CLang.ty

  (* translate a TreeIL type to the C types used to represent it in the external API *)
    val toCType : TreeIL.Ty.ty -> CLang.ty

  (* generate code to copy values from their internal Diderot representation to the external
   * C representation.
   *)
    val copyToC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list

  (* generate code to copy values from their external C representation to the internal
   * Diderot representation.
   *)
    val copyFromC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list

  end

structure CTyTranslate : TY_TRANSLATE =
  struct

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

  (* translate a type to its internal Diderot representation *)
    fun toType ty = (case ty
           of Ty.BoolTy => CL.T_Named "bool"
            | Ty.StringTy => CL.T_Named "Diderot_string_t"
            | Ty.IntTy => CL.T_Named "Diderot_int_t"
            | Ty.TensorTy[] => CL.T_Named "Diderot_real_t"
            | 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(toType 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.toType(", Ty.toString ty, ")"])
          (* end case *))

  (* translate a TreeIL type to the C types used to represent it in the external API *)
    fun toCType 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_Array(!N.gRealTy, SOME n)
            | Ty.TensorTy[n, m] => CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m)
            | Ty.TensorTy[n, m, l] => CL.T_Array(CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m), SOME l)
            | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!N.gIntTy, SOME n)
            | Ty.SeqTy(ty, n) => CL.T_Array(toCType ty, SOME n)
            | 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.toCType(", Ty.toString ty, ")"])
          (* end case *))

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

(* we might want to use memcpy in some cases
    fun copy (ty, dst, src) = let
          fun assign () = CL.mkAssign(dst, src)
          fun addrOf (CL.E_UnOp(CL.%*, x)) = x
            | addrOf x = CL.mkUnOp(CL.%&, x)
          fun memcpy () = CL.mkCall("memcpy", [addrOf dst, addrOf src, CL.mkSizeof(trType ty)])
          in
            case ty
             of Ty.BoolTy => assign()
              | Ty.StringTy => CL.mkCall("strcpy", [addrOf dst, addrOf src])
              | Ty.IntTy => assign()
              | Ty.TensorTy[] => assign()
              | Ty.TensorTy _ => memcpy()
              | Ty.SeqTy _ => memcpy()
              | Ty.DynSeqTy _ => raise Fail "dynamic sequence"
              | Ty.ImageTy _ => raise Fail "unexpected image copy"
              | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
            (* end case *)
          end
*)

  (* generate code to copy values from their internal Diderot representation to the external
   * C representation.
   *)
    fun copyToC {ty, dst, src} = (case ty
           of Ty.BoolTy => [CL.mkAssign(dst, src)]
            | Ty.StringTy => [CL.mkAssign(dst, src)]
            | Ty.IntTy => [CL.mkAssign(dst, src)]
            | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
            | Ty.TensorTy[n] => let
                val src' = CL.mkCast(CL.T_Named(N.unionTy n), src)
                in
                  List.tabulate (n,
                    fn i => CL.mkAssign(
                      subscript (CL.mkUnOp(CL.%&, dst), i),
                      subscript (CL.mkSelect(src', "r"), i)))
                end
            | Ty.TensorTy[n, m] => List.concat(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.concat(List.tabulate(n,
		fn i => List.concat(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)))
(* do we make the following types externally visible?
            | 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.
   *)
    fun copyFromC {ty, dst, src} = (case ty
           of Ty.BoolTy => [CL.mkAssign(dst, src)]
            | Ty.StringTy => [CL.mkAssign(dst, src)]
            | Ty.IntTy => [CL.mkAssign(dst, src)]
            | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
            | Ty.TensorTy[n] => [CL.mkAssign(
		dst,
		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.concat(List.tabulate(n,
		fn i => List.tabulate (m,
		  fn j => CL.mkAssign(
		    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))))
                ]
(* do we make the following types externally visible?
            | 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