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-load-nrrd.sml
ViewVC logotype

View of /branches/vis15/src/compiler/c-util/gen-load-nrrd.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3810 - (download) (annotate)
Tue May 3 23:26:44 2016 UTC (3 years ago) by jhr
File size: 3486 byte(s)
  Working on merge
(* gen-load-nrrd.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.
 *)

structure GenLoadNrrd : sig

  (* loadImage (lhs, info, name)
   *    returns code to load an image from a Nrrd file, where "lhs" is the l-value to hold
   *    the image, "info" specifies information about the image format, and "name" specifies
   *    the file name.  The generated code check the status of the load attempt and will
   *    return "true" (i.e., error) if the load fails.
   *)
    val loadImage : CLang.exp * ImageInfo.info * CLang.exp -> CLang.stm

  (* setImage (lhs, info, nrrd)
   *    returns code to initialize an image from a Nrrd, where "lhs" is the l-value to hold
   *    the image, "info" specifies information about the image format, and "nrrd" specifies
   *    the nrrd.  The generated code check the status of the initialization and will
   *    return "true" (i.e., error) if the load fails.
   *)
    val setImage : CLang.exp * ImageInfo.info * CLang.exp -> CLang.stm

    val loadSeqFromFile : CLang.exp * APITypes.t * CLang.exp -> CLang.stm
    val loadSeq : CLang.exp * APITypes.t * CLang.exp -> CLang.stm

  end = struct

    structure CL = CLang
    structure RN = RuntimeNames
    structure Ty = APITypes

    val wrldPrefixTy = CL.T_Ptr(CL.T_Named "WorldBase")

    fun doImage imageFn (lhs, info, arg) = let
          val dim = ImageInfo.dim info
	  val loadExp = CL.mkApply(imageFn dim, [
                  CL.mkCast(wrldPrefixTy, CL.mkVar "wrld"),
                  arg,
                  CL.mkUnOp(CL.%&, lhs)
                ])
          in
(* FIXME: we should also generate code to check that the loaded image has the right type, etc. *)
            CL.mkIfThen(loadExp, CL.mkReturn(SOME(CL.mkVar "true")))
          end

    val loadImage = doImage RN.loadImage
    val setImage = doImage RN.setImage

    fun doSeq loadFn (lhs, elemTy, arg) = let
          val (nDims, dimInit, dimExp, elemTy) = (case elemTy
                 of Ty.TensorTy(dims as _::_) => let
                      val nDims = List.length dims
                      fun lp (_, [], init) = CL.I_Array(List.rev init)
                        | lp (i, d::dd, init) =
                            lp(i+1, dd, (i, CL.I_Exp(CL.mkInt(IntInf.fromInt d)))::init)
                      val dimInit = CL.mkDecl(
                            CL.T_Array(CL.T_Named "unsigned int", SOME nDims), "_dims",
                            SOME(lp(0, dims, [])))
                      in
                        (nDims, [dimInit], CL.mkVar "_dims", Ty.realTy)
                      end
                  | Ty.SeqTy ty' => raise Fail "loading sequences of type not supported yet"
                  | _ => (0, [], CL.mkInt 0, elemTy)
                (* end case *))
          in CL.mkBlock (
            dimInit @ [
                CL.mkAssign(
                  lhs,
                  CL.mkApply(loadFn elemTy, [
                      CL.mkCast(CL.T_Ptr(CL.T_Named "WorldBase"), CL.mkVar "wrld"),
                      arg,
                      CL.mkInt(IntInf.fromInt nDims),
                      dimExp
                    ])),
                CL.mkIfThen(
                  CL.mkBinOp(lhs, CL.#==, CL.mkInt 0),
                  CL.mkReturn(SOME(CL.mkVar "true")))
              ]
          ) end

    val loadSeqFromFile = doSeq RN.loadDynSeqFromFile
    val loadSeq = doSeq RN.loadDynSeq

  end

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