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/gen-library-interface.sml
ViewVC logotype

View of /branches/vis12/src/compiler/c-util/gen-library-interface.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1701 - (download) (annotate)
Tue Mar 6 12:54:48 2012 UTC (7 years, 4 months ago) by jhr
File size: 4488 byte(s)
  Working on library and output generation
(* gen-library-interface.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Generate the C header file for the library produced by the compiler.
 *
 * The format of header file is:
 *
 *	HEAD
 *	decls for input variables
 *	BODY
 *	decls for output variables
 *	FOOT
 *)

structure GenLibraryInterface : sig

    val gen : {
	    srcFile : string,		(* source filename (including directory) *)
	    base : string,		(* base part of output file name (no suffix) *)
	    prefix : string option,	(* optional namespace prefix *)
	    inputs : (TreeIL.Ty.ty * string) list,
	    outputs : (TreeIL.Ty.ty * string) list
	  } -> unit

  end = struct

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

    val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")

(* FIXME: we will need this function elsewhere *)
  (* 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_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(trType ty, SOME n)
            | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
            | Ty.AddrTy(ImageInfo.ImgInfo{ty=(_, rTy), ...}) => CL.T_Ptr(CL.T_Num rTy)
            | Ty.ImageTy(ImageInfo.ImgInfo{dim, ...}) => CL.T_Ptr(CL.T_Named(N.imageTy dim))
            | _ => raise Fail(concat["TreeToC.trType(", Ty.toString ty, ")"])
          (* end case *))

    fun mkSymbol base = let
	  fun tr c = if Char.isAlpha c then Char.toUpper c
		else if Char.isDigit c then c
		else #"_"
	  in
	    String.concat["_", CharVector.map tr base, "_H_"]
	  end

    fun gen {srcFile, base, prefix, inputs, outputs} = let
	  val {dir, ...} = OS.Path.splitDirFile srcFile
	  val filename = OS.Path.joinDirFile{
		  dir = dir,
		  file = OS.Path.joinBaseExt{base = base, ext = SOME "h"}
		}
	  val prefix = (case prefix
		 of NONE => "Diderot_"
		  | SOME s => s ^ "_"
		(* end case *))
	(* the world pointer type *)
	  val worldPtrTy = CL.T_Ptr(CL.T_Named(prefix ^ "World_t"))
	(* create a decl for an input variable *)
	  fun mkSetDecl (Ty.ImageTy _, name) = [
		  CL.D_Proto(
		    [], CL.voidTy, concat[prefix, "Set_", name, "_ByName"],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM(["const"], CL.charPtr, "s")]),
		  CL.D_Proto(
		    [], CL.voidTy, concat[prefix, "Set_", name],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
		]
	    | mkSetDecl (ty as Ty.DynSeqTy _, name) = raise Fail "dynamic input not supported yet"
	    | mkSetDecl (ty, name) = [
		  CL.D_Proto(
		    [], CL.voidTy, concat[prefix, "Set_", name],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], trType ty, "v")])
		]
	(* create a decl for an output variable *)
	  fun mkGetDecl (Ty.DynSeqTy _, name) = [
		  CL.D_Proto(
		    [], CL.boolTy, concat[prefix, "Get_", name],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
		]
	    | mkGetDecl (_, name) = [
		  CL.D_Proto(
		    [], CL.boolTy, concat[prefix, "Get_", name],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
		]
	  val placeholders = [
		  ("HDRFILE", filename),
		  ("HDR_DEFINE", mkSymbol base),
		  ("PREFIX", prefix),
		  ("SRCFILE", srcFile)
		]
	  val outStrm = TextIO.openOut filename
	  val outS = PrC.new outStrm
	  in
	    PrC.output (outS, CL.verbatim [LibInterfaceHeadFrag.text] placeholders);
	    PrC.output (outS, CL.D_Verbatim ["[\n/**** Setters for input values ****/\n]"]);
	    List.app (fn input => List.app (fn dcl => PrC.output(outS, dcl)) (mkSetDecl input)) inputs;
	    PrC.output (outS, CL.verbatim [LibInterfaceBodyFrag.text] placeholders);
	    PrC.output (outS, CL.D_Verbatim ["[\n/**** Getters for output values ****/\n]"]);
	    List.app (fn output => List.app (fn dcl => PrC.output(outS, dcl)) (mkGetDecl output)) outputs;
	    PrC.output (outS, CL.verbatim [LibInterfaceFootFrag.text] placeholders);
	    PrC.close outS;
	    TextIO.closeOut outStrm
	  end

  end

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