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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/cxx-util/output-util.sml
ViewVC logotype

View of /branches/vis15/src/compiler/cxx-util/output-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3937 - (download) (annotate)
Mon Jun 6 13:16:10 2016 UTC (2 years, 9 months ago) by jhr
File size: 10984 byte(s)
  Working on merge: FIXMEs
(* output-util.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.
 *
 * Common code for output.
 *)

structure OutputUtil : sig

    type output_info = {
	name : string,		(* the variable's name *)
	ty : APITypes.t,	(* the variables API type *)
	isGlobal : bool		(* is the output a global variable? (always false for now) *)
      }

  (* return information about the output type.  This is a tuple
   *
   *    (c-type, nrrd-type, nrrd-axis-kind, # elements)
   *)
    val infoOf : CodeGenEnv.t * APITypes.t -> (CLang.ty * NrrdEnums.ty * NrrdEnums.axis_kind * int)

  (* code fragment to allocate nrrd data and check for errors *)
    val maybeAlloc : CodeGenEnv.t * CLang.exp * CLang.var * int -> CLang.stm

  (* gather the outputs of a program *)
    val gatherOutputs : TreeIR.program -> output_info list

  (* generate code to register command-line options for redirecting the output in standalone
   * executables.  This function returns a list consisting of the global C variables that hold
   * the option values and the registration function.
   *)
    val genRegisterOutputOpts : CodeGenEnv.t * output_info list -> CLang.decl list

  (* generate the nrrd-file output and print functions used by standalone executables *)
    val genOutput : CodeGenEnv.t * output_info list -> CLang.decl list

  end = struct

    structure IR = TreeIR
    structure Ty = APITypes
    structure CL = CLang
    structure Nrrd = NrrdEnums
    structure Env = CodeGenEnv
    structure RN = CxxNames

    type output_info = {
	name : string,		(* the variable's name *)
	ty : Ty.t,		(* the variables API type *)
	isGlobal : bool		(* is the output a global variable? (always false for now) *)
      }

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

    val outputGet = GenLibraryInterface.outputGet

  (* variables in the generated code *)
    val wrldV = CL.mkVar "wrld"
    val sizesV = CL.mkVar "sizes"
    val nDataV = CL.mkVar "nData"
    val nLengthsV = CL.mkVar "nLengths"
    val NRRD = CL.mkVar "NRRD"
    val msgV = CL.mkVar "msg"

    fun infoOf (env, ty) = (case ty
           of Ty.IntTy => if #longint(Env.target env)
                then (CL.int64, Nrrd.TypeLLong, Nrrd.KindScalar, 1)
                else (CL.int32, Nrrd.TypeInt, Nrrd.KindScalar, 1)
            | Ty.TensorTy [] => if #double(Env.target env)
                then (CL.double, Nrrd.TypeDouble, Nrrd.KindScalar, 1)
                else (CL.float, Nrrd.TypeFloat, Nrrd.KindScalar, 1)
            | Ty.TensorTy dims => let
                val (axisKind, nElems) = (case dims
                       of [2] => (Nrrd.Kind2Vector, 2)
                        | [3] => (Nrrd.Kind3Vector, 3)
                        | [4] => (Nrrd.Kind4Vector, 4)
                        | [2,2] => (Nrrd.Kind2DMatrix, 4)
                        | [3,3] => (Nrrd.Kind3DMatrix, 9)
                        | _ => (Nrrd.KindList, List.foldl Int.* 1 dims)
                      (* end case *))
                in
                  if #double(Env.target env)
                    then (CL.double, Nrrd.TypeDouble, axisKind, nElems)
                    else (CL.float, Nrrd.TypeFloat, axisKind, nElems)
                end
            | Ty.SeqTy(ty, SOME n) => raise Fail "FIXME" (*let
                val (elemTy, nrrdTy, dims) = infoOf (tgt, ty)
                in
                  (elemTy, nrrdTy, n::dims)
                end*)
            | _ => raise Fail(concat["GetOutput.infoOf(", Ty.toString ty, ")"])
          (* end case *))

  (* code fragment to allocate nrrd data and check for errors
        if (nrrdMaybeAlloc_nva(<nrrdVar>, <nrrdType>, <nDims>, sizes) != 0) {
            char *msg = biffGetDone(NRRD);
            biffMsgAdd (wrld->_errors, msg);
            std::free (msg);
            return true;
        }
   *)
    fun maybeAlloc (env, nrrdVar, nrrdType, nDims) =
          CL.mkIfThen(
            CL.mkBinOp(
              CL.mkApply("nrrdMaybeAlloc_nva", [
                  nrrdVar, CL.mkVar nrrdType, CL.mkInt(IntInf.fromInt nDims), sizesV
                ]),
              CL.#!=,
              CL.mkInt 0),
          (* then *)
            CL.mkBlock[
                CL.mkDeclInit(CL.charPtr, "msg", CL.mkApply("biffGetDone", [NRRD])),
                TreeToCxx.errorMsgAdd (env, msgV),
                CL.mkCall("std::free", [msgV]),
                CL.mkReturn(SOME(CL.mkVar "true"))
              ]
          (* endif*))

    fun gatherOutputs (IR.Program{strand=IR.Strand{state, ...}, ...}) = let
	  fun getOutput  x = if TreeStateVar.isOutput x
		then SOME{name = TreeStateVar.name x, ty = TreeStateVar.apiTy x, isGlobal = false}
		else NONE
	  in
	    List.mapPartial getOutput state
	  end

  (* global variable names for output file/stems *)
    val outfile = "Outfile"
    fun outstem name = "OutStem_" ^ name

  (* generate code to register command-line options for redirecting the output in standalone
   * executables.
   *)
    fun genRegisterOutputOpts (env, outputs : output_info list) = let
        (* some common variables *)
          val optsV = CL.mkVar "opts"
	(* make a global variable declaration *)
	  fun mkDecl (name, value) =
		CL.D_Var(["static"], CL.T_Named "std::string", name, SOME(CL.I_Exp(CL.mkStr value)))
        (* register a flag for a given output *)
          fun registerOutput {name, ty, isGlobal} = let
		val optName = "redirect-" ^ name
		in
		  CL.mkExpStm(CL.mkIndirectDispatch(optsV, "add", [
		      CL.mkStr(concat["o-", name, ",", "output-", name]),
		      CL.mkStr("specify output-file stem for " ^ name),
		      CL.mkUnOp(CL.%&, CL.mkVar(outstem name)),
		      CL.mkBool true
		    ]))
		end
	  fun multi () = let
		val stms = List.map registerOutput outputs
		val dcls = List.map (fn {name, ...} => mkDecl (outstem name, name)) outputs
		in
		  (stms, dcls)
		end
	  val (stms, dcls) = (case outputs
		 of [{ty = Ty.SeqTy(_, NONE), ...}] => multi () (* two ouput files *)
		  | [{name, ...}] => let (* one output file, so use "-o" redirect option *)
		      val stm = CL.mkExpStm(CL.mkIndirectDispatch(optsV, "add", [
			  CL.mkStr "o,output", CL.mkStr("specify output-file file"),
			  CL.mkUnOp(CL.%&, CL.mkVar outfile),
			  CL.mkBool true
			]))
		      val dcl = mkDecl (outfile, name ^ ".nrrd")
		      in
			([stm], [dcl])
		      end
		  | _ => multi ()
		(* end case *))
	  val registerFn = CL.D_Func(
		["static"], CL.voidTy, "register_outputs",
		[CL.PARAM([], RN.optionsPtrTy env, "opts")],
		CL.mkBlock stms)
          in
	    dcls @ [registerFn]
          end

  (* generate the nrrd-file output and print functions used by standalone executables *)
    fun genOutput (env, outputs : output_info list) = let
	  val spec = Env.target env
          fun isDyn ty = (case ty of Ty.SeqTy(_, NONE) => true | _ => false)
(* FIXME: use biffMsgAddF and return error status *)
          fun error msg = CL.mkBlock[
		  CL.mkExpStm(List.foldl
		    (fn (e, lhs) => CL.mkBinOp(lhs, CL.#<<, e))
		      (CL.mkVar "std::cerr")
			(msg @ [CL.mkVar "std::endl"])),
		  CL.mkCall("exit", [CL.mkInt 1])
		]
          val outDecls = if List.exists (isDyn o #ty) outputs
                then [CL.mkDecl(nrrdPtrTy, "nLengths", NONE), CL.mkDecl(nrrdPtrTy, "nData", NONE)]
                else [CL.mkDecl(nrrdPtrTy, "nData", NONE)]
          val prDecls = outDecls @ [CL.mkDecl(filePtrTy, "outS", NONE)]
          fun nrrdNew v = CL.mkAssign(v, CL.mkApply("nrrdNew", []))
          fun nrrdNuke v = CL.mkCall("nrrdNuke", [v])
	  val isMultiOutput = (case outputs
		 of [{ty, ...}] => isDyn ty
		  | _ => true
		(* end case *))
          fun writeNrrd {name, ty, isGlobal} = if isDyn ty
                then [
                    nrrdNew (nLengthsV),
                    nrrdNew (nDataV),
                    CL.mkIfThenElse(
                      CL.mkApply(outputGet(spec, name), [wrldV, nLengthsV, nDataV]),
                    (* then *)
                        error [
			    CL.mkStr "Error getting nrrd data:\n",
                            CL.mkApply("biffMsgStrGet", [CL.mkIndirect(wrldV, "_errors")])
			  ],
                    (* else *)
                      CL.mkIfThen(
                        CL.mkBinOp(
                          CL.mkApply("NrrdSaveHelper", [
			      CL.mkVar(outstem name), CL.mkStr "-len", CL.mkStr "nrrd", nLengthsV
                            ]),
                          CL.#||,
                          CL.mkApply("NrrdSaveHelper", [
			      CL.mkVar(outstem name), CL.mkStr "-data", CL.mkStr "nrrd", nDataV
                            ])),
                      (* then *)
                        error [
			    CL.mkStr "Error saving nrrd:\n",
			    CL.mkApply("biffGetDone", [NRRD])
			  ]
                      (* endif *))
                    (* endif *)),
                    nrrdNuke nLengthsV,
                    nrrdNuke nDataV
                  ]
	      else if isMultiOutput
		then [
                    nrrdNew (nDataV),
                    CL.mkIfThenElse(
                      CL.mkApply(outputGet(spec, name), [wrldV, nDataV]),
                    (* then *)
                        error [
			    CL.mkStr "Error getting nrrd data:\n",
                            CL.mkApply("biffMsgStrGet", [CL.mkIndirect(wrldV, "_errors")])
			  ],
                    (* else *)
                      CL.mkIfThen(
                        CL.mkApply("NrrdSaveHelper", [
			    CL.mkVar(outstem name), CL.mkStr "", CL.mkStr "nrrd", nDataV
                          ]),
                      (* then *)
                        error [
			    CL.mkStr "Error saving nrrd:\n",
			    CL.mkApply("biffGetDone", [NRRD])
			  ]
                      (* endif *))
                    (* endif *)),
                    nrrdNuke nDataV
                  ]
		else [
                    nrrdNew (nDataV),
                    CL.mkIfThenElse(
                      CL.mkApply(outputGet(spec, name), [wrldV, nDataV]),
                    (* then *)
                        error [
			    CL.mkStr "Error getting nrrd data:\n",
                            CL.mkApply("biffMsgStrGet", [CL.mkIndirect(wrldV, "_errors")])
			  ],
                    (* else *)
                      CL.mkIfThen(
                        CL.mkApply("nrrdSave", [
			    CL.mkDispatch(CL.mkVar outfile, "c_str", []), nDataV, CL.mkVar "NULL"
                          ]),
                      (* then *)
                        error [
			    CL.mkStr "Error saving nrrd:\n%s\n",
			    CL.mkApply("biffGetDone", [NRRD])
			  ]
                      (* endif *))
                    (* endif *)),
                    nrrdNuke nDataV
		  ]
          in [
            CL.D_Func(
	      ["static"], CL.voidTy, "write_output",
	      [CL.PARAM([], RN.worldPtrTy, "wrld")],
              CL.mkBlock(outDecls @ List.foldr (fn (output, l) => writeNrrd output @ l) [] outputs))
          ] end

  end

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