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

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/cxx-util/gen-load-nrrd.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/cxx-util/gen-load-nrrd.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3894, Sun May 22 14:51:27 2016 UTC revision 3988, Fri Jun 17 17:26:51 2016 UTC
# Line 8  Line 8 
8    
9  structure GenLoadNrrd : sig  structure GenLoadNrrd : sig
10    
11    (* loadImage (lhs, dim, name)    (* loadImage (img, arg)
12     *    returns code to load an image from a Nrrd file, where "lhs" is the l-value to hold     *    returns code to load an image from either a Nrrd file or an in-memory Nrrd, where
13     *    the image, "dim" specifies the dimension of the image format, and "name" specifies     *    "lhs" is the image object and arg specifies either the file or nrrd.
14     *    the file name.  The generated code check the status of the load attempt and will     *    The generated code checks the status of the load attempt and will return "true"
15     *    return "true" (i.e., error) if the load fails.     *    (i.e., error) if the load fails.
16     *)     *)
17      val loadImage : CLang.exp * int * CLang.exp -> CLang.stm      val loadImage : CLang.exp * CLang.exp -> CLang.stm
18    
19    (* setImage (lhs, info, nrrd)      val loadSeqFromFile : CodeGenEnv.t * CLang.exp * APITypes.t * CLang.exp -> CLang.stm
20     *    returns code to initialize an image from a Nrrd, where "lhs" is the l-value to hold      val loadSeq : CodeGenEnv.t * CLang.exp * APITypes.t * CLang.exp -> CLang.stm
    *    the image, "dim" specifies the dimension of 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 * int * CLang.exp -> CLang.stm  
   
     val loadSeqFromFile : CLang.exp * APITypes.t * CLang.exp -> CLang.stm  
     val loadSeq : CLang.exp * APITypes.t * CLang.exp -> CLang.stm  
21    
22    end = struct    end = struct
23    
24      structure CL = CLang      structure CL = CLang
25      structure RN = RuntimeNames      structure RN = CxxNames
26      structure Ty = APITypes      structure Ty = APITypes
27    
28      val wrldPrefixTy = CL.T_Ptr(CL.T_Named "WorldBase")      val wrldV = CL.mkVar "wrld"
29    
30      fun doImage imageFn (lhs, dim, arg) = let      fun loadImage (img, arg) = let
31            val loadExp = CL.mkApply(imageFn dim, [            val loadExp = CL.mkApplyExp(CL.mkSelect(img, "load"), [wrldV, arg])
                   CL.mkCast(wrldPrefixTy, CL.mkVar "wrld"),  
                   arg,  
                   CL.mkUnOp(CL.%&, lhs)  
                 ])  
32            in            in
33  (* FIXME: we should also generate code to check that the loaded image has the right type, etc. *)  (* FIXME: we should also generate code to check that the loaded image has the right type, etc. *)
34              CL.mkIfThen(loadExp, CL.mkReturn(SOME(CL.mkVar "true")))              CL.mkIfThen(loadExp, CL.mkReturn(SOME(CL.mkVar "true")))
35            end            end
36    
37      val loadImage = doImage RN.loadImage      fun loadSeq loadFn (env, lhs, elemTy, file) = let
38      val setImage = doImage RN.setImage            val stsDcl = CL.mkDecl(CL.boolTy, "sts", NONE)
39              val stsV = CL.mkVar "sts"
40      fun doSeq loadFn (lhs, elemTy, arg) = let            val loadExp = CL.mkTemplateApply(loadFn,
41            val (nDims, dimInit, dimExp, elemTy) = (case elemTy                  [TypeToCxx.trAPIType(env, elemTy)],
42                   of Ty.TensorTy(dims as _::_) => let                  [wrldV, file, stsV])
                       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, [])))  
43                        in                        in
44                          (nDims, [dimInit], CL.mkVar "_dims", Ty.realTy)              CL.mkBlock [
45                        end                  stsDcl,
46                    | Ty.SeqTy(ty', _) => raise Fail(concat[                  CL.mkAssign (lhs, loadExp),
47                          "loading sequences of type ", Ty.toString ty', " not supported yet"                  CL.mkIfThen (stsV, CL.mkReturn(SOME(CL.mkVar "true")))
                       ])  
                   | _ => (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")))  
48                ]                ]
49            ) end            end
50    
51      val loadSeqFromFile = doSeq RN.loadDynSeqFromFile      val loadSeqFromFile = loadSeq RN.loadDynSeqFromFile
52      val loadSeq = doSeq RN.loadDynSeq      val loadSeq = loadSeq RN.loadDynSeq
53    
54    end    end

Legend:
Removed from v.3894  
changed lines
  Added in v.3988

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