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 1812 - (download) (annotate)
Sat Apr 7 21:33:09 2012 UTC (7 years, 4 months ago) by jhr
File size: 5533 byte(s)
  Change convention for naming inputs and outputs
(* 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 : {
	    tgt : TargetUtil.target_desc,	(* target information *)
	    inputs : (TreeIL.Ty.ty * string * string * bool) 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")

  (* translate TreeIL types to CLang types *)
    val trType = TreeTyToC.trType

    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 {tgt : TargetUtil.target_desc, inputs, outputs} = let
	  val filename = OS.Path.joinDirFile{
		  dir = #outDir tgt,
		  file = OS.Path.joinBaseExt{base = #outBase tgt, ext = SOME "h"}
		}
	  val prefix = #namespace tgt
	(* the world pointer type *)
	  val worldPtrTy = CL.T_Ptr(CL.T_Named(prefix ^ "World_t"))
	(* create decls for an input variable *)
          fun mkInputDecls (ty, name, desc, hasDflt) = let
                val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
              (* create a description declaration for the input variable *)
                val descDcl = if (desc = "")
                        then []
                        else [
                            CL.D_Var(["extern"], CL.T_Ptr(CL.T_Named "const char"),
                              concat[prefix, name, "Desc"], NONE)
                          ]
                val getDcl = if hasDflt
                        then let
                          val name = concat[prefix, name, "Get"]
                        (* convert the input type to a by-reference C type *)
                          val outTy = (case ty
                                 of Ty.BoolTy => CL.T_Ptr(trType ty)
                                  | Ty.StringTy => CL.T_Ptr(trType ty)
                                  | Ty.IntTy => CL.T_Ptr(trType ty)
                                  | Ty.TensorTy[] => CL.T_Ptr(trType ty)
                                  | Ty.TensorTy _=> trType ty
                                  | Ty.SeqTy _ => trType ty
                                  | Ty.DynSeqTy _ => CL.T_Ptr(trType ty)
                                  | Ty.ImageTy _ => CL.T_Ptr CL.charPtr
                                  | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
                                (* end case *))
                          in [
                            CL.D_Proto([], CL.voidTy, name, [wrldParam, CL.PARAM([], outTy, "v")])
                          ] end
                        else []
                val setDcl = (case ty
                       of Ty.ImageTy _ => [
                              CL.D_Proto(
                                [], CL.boolTy, concat[prefix, name, "SetByName"],
                                [wrldParam, CL.PARAM(["const"], CL.charPtr, "s")]),
                              CL.D_Proto(
                                [], CL.boolTy, concat[prefix, name, "Set"],
                                [wrldParam, CL.PARAM([], nrrdPtrTy, "data")])
                            ]
                        | Ty.DynSeqTy _ => raise Fail "dynamic input not supported yet"
                        | _ => [
                              CL.D_Proto(
                                [], CL.boolTy, concat[prefix, name, "Set"],
                                [wrldParam, CL.PARAM([], trType ty, "v")])
                            ]
                      (* end case *))
                in
                  descDcl @ getDcl @ setDcl
                end
	(* create a decl for an output variable *)
	  fun mkGetDecl (Ty.DynSeqTy _, name) = [
		  CL.D_Proto(
		    [], CL.boolTy, concat[prefix, name, "Out"],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
		]
	    | mkGetDecl (_, name) = [
		  CL.D_Proto(
		    [], CL.boolTy, concat[prefix, name, "Out"],
		    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
		]
	  val placeholders = [
		  ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt),
		  ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt),
		  ("DIDEROT_TARGET", TargetUtil.targetDef tgt),
		  ("HDRFILE", filename),
		  ("HDR_DEFINE", mkSymbol(#outBase tgt)),
		  ("PREFIX", prefix),
		  ("SRCFILE", #srcFile tgt)
		]
	  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/**** Functions etc. for input variables ****/\n"]);
	    List.app (fn input => List.app (fn dcl => PrC.output(outS, dcl)) (mkInputDecls 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