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

View of /branches/vis15/src/compiler/c-util/gen-library-interface.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: 7230 byte(s)
  Working on merge
(* gen-library-interface.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * 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 : {
            props : Properties.props,           (* target information *)
            rt : string option,                 (* fragment with extra runtime system hooks *)
            inputs : GenInputsUtil.input_desc list,
            outputs : (TreeTypes.t * string) list
          } -> unit

  end = struct

    structure Ty = TreeIR.Ty
    structure CL = CLang
    structure RN = RuntimeNames
    structure PrC = PrintAsC

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

  (* translate a TreeIR type to the C types used to represent it in the external API *)
    val trType = CTyTranslate.toCType

    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 {props, rt, inputs, outputs} = let
          val filename = OS.Path.joinDirFile{
                  dir = #outDir props,
                  file = OS.Path.joinBaseExt{base = #outBase props, ext = SOME "h"}
                }
        (* the world pointer type *)
          val worldPtrTy = RN.worldPtrTy props
        (* create decls for an input variable *)
          fun mkInputDecls (gv, name, desc, optDflt) = let
                val ty = TreeIR.GlobalVar.ty gv
                val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
              (* create a description declaration for the input variable *)
                val descDcl = (case desc
                       of NONE => []
                        | SOME desc => [
                              CL.D_Var(["extern"], CL.T_Ptr(CL.T_Named "const char"),
                                RN.inputDesc(props, name), NONE)
                            ]
                      (* end case *))
                val getDcl = if Option.isSome optDflt
                        then let
                          val name = RN.inputGet(props, name)
                        (* 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
(* FIXME: for images and sequences, it is not clear what the get function should return.
 * For now, we just return 0
 *)
                                  | Ty.DynSeqTy _ => CL.T_Ptr CL.voidPtr
                                  | Ty.ImageTy _ => CL.T_Ptr CL.voidPtr
                                  | _ => 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 = let
                    (* prototypes for setting an image or dynamic sequence from a nrrd *)
                      fun loadPrototypes () = [
                              CL.D_Proto(
                                [], CL.boolTy, RN.inputSetByName(props, name),
                                [wrldParam, CL.PARAM(["const"], CL.charPtr, "s")]),
                              CL.D_Proto(
                                [], CL.boolTy, RN.inputSet(props, name),
                                [wrldParam, CL.PARAM([], nrrdPtrTy, "data")])
                            ]
                      in
                        case ty
                         of Ty.ImageTy _ => loadPrototypes()
                          | Ty.DynSeqTy _ => loadPrototypes()
                          | _ => [
                                CL.D_Proto(
                                  [], CL.boolTy, RN.inputSet(props, name),
                                  [wrldParam, CL.PARAM([], trType ty, "v")])
                              ]
                        (* end case *)
                      end
                in
                  descDcl @ getDcl @ setDcl
                end
        (* create a decl for an output variable *)
          fun mkGetDecl snapshot (Ty.DynSeqTy _, name) = [
                  CL.D_Proto(
                    [], CL.boolTy, (if snapshot then RN.snapshotGet else RN.outputGet)(props, name),
                    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
                ]
            | mkGetDecl snapshot (_, name) = [
                  CL.D_Proto(
                    [], CL.boolTy, (if snapshot then RN.snapshotGet else RN.outputGet)(props, name),
                    [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
                ]
(* NOTE: there is some redundancy between placeholders and the substitutions defined in the target files *)
          val placeholders = [
                  ("DIDEROTC_CMD", #diderotc props),
                  ("DIDEROTC_ARGV", String.concatWith " " (#argv props)),
                  ("DIDEROTC_VERSION", #version props),
                  ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props),
                  ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props),
                  ("DIDEROT_TARGET", Properties.targetDef props),
                  ("HDRFILE", filename),
                  ("HDR_DEFINE", mkSymbol(#outBase props)),
                  ("PREFIX", #namespace props),
                  ("SRCFILE", #srcFile props)
                ]
          val outStrm = TextIO.openOut filename
          val outS = PrC.new outStrm
          fun prFrag frag = PrC.output (outS, CL.verbatimDcl [frag] placeholders)
          val prDecls = List.app (fn dcl => PrC.output(outS, dcl))
          in
            prFrag LibInterfaceHeadFrag.text;
            PrC.output (outS, CL.D_Verbatim ["\n/**** Functions etc. for input variables ****/\n"]);
            List.app (fn input => prDecls (mkInputDecls input)) inputs;
            prFrag LibInterfaceRTFrag.text;
            case rt of SOME rt => prFrag rt | _ => ();
            prFrag LibInterfaceBodyFrag.text;
            PrC.output (outS, CL.D_Verbatim ["\n/**** Getters for output values ****/\n"]);
            if (#snapshot props)
              then List.app (fn output => prDecls (mkGetDecl true output)) outputs
              else ();
            List.app (fn output => prDecls (mkGetDecl false output)) outputs;
            prFrag LibInterfaceFootFrag.text;
            PrC.close outS;
            TextIO.closeOut outStrm
          end

  end

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