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

SCM Repository

[diderot] Diff of /branches/vis12/src/compiler/c-util/gen-inputs.sml
ViewVC logotype

Diff of /branches/vis12/src/compiler/c-util/gen-inputs.sml

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

revision 2047, Sun Oct 21 14:53:47 2012 UTC revision 2048, Sun Oct 21 17:43:32 2012 UTC
# Line 7  Line 7 
7  structure GenInputs : sig  structure GenInputs : sig
8    
9    (* input variable descriptor: type, name, description, has default *)    (* input variable descriptor: type, name, description, has default *)
10      type input_desc = (TreeIL.Ty.ty * string * string option * bool)      type input_desc = (TreeIL.Ty.ty * string * string option * TreeIL.exp option)
11    
12      val gatherInputs : TreeIL.block -> input_desc list      val gatherInputs : TreeIL.block -> input_desc list
13    
14      (*** Support for standalone executables ***)
15    
16    (* generate the input initialization structure that we use to initialize input    (* generate the input initialization structure that we use to initialize input
17     * globals from command-line arguments in stand-alone executables.     * globals from command-line arguments in stand-alone executables.
18     *)     *)
19      val genInputsStruct : TargetUtil.target_desc * input_desc list -> CLang.decl      val genInputsStruct : TargetUtil.target_desc * input_desc list -> CLang.decl
20    
21    (* generate a function to register command-line options for setting the input variables *)    (* generate the functions that handle inputs for standalone executables.  These are:
22      val genRegisterInputs : TargetUtil.target_desc * input_desc list -> CLang.decl     *    InitDefaults    -- called to initialize the default input values
23       *    RegisterInputs  -- called to register the command-line options for the input globals
24       *    InitInptus      -- called to initialize the input globals from the values specified
25       *                       on the command line.
26       *)
27        val genExecInputFuns : TargetUtil.target_desc * input_desc list -> CLang.decl list
28    
29      (*** Support for libraries ***)
30    
31    (* generate a function to initialize the global input variables from the command-line inputs *)    (* generate the typedef for the defined-input flag struct. *)
32      val genInitInputs : TargetUtil.target_desc * input_desc list -> CLang.decl      val genDefinedInpStruct : TargetUtil.target_desc * input_desc list -> CLang.decl
33    
34    (* generated the functions to initialize inputs for the library API *)    (* generated the functions to initialize inputs for the library API.  This function also
35       * generates the function to initialize the defined input flags struct.
36       *)
37      val genInputFuns : TargetUtil.target_desc * input_desc list -> CLang.decl list      val genInputFuns : TargetUtil.target_desc * input_desc list -> CLang.decl list
38    
39    end = struct    end = struct
# Line 33  Line 44 
44      structure N = CNames      structure N = CNames
45      structure TrTy = CTyTranslate      structure TrTy = CTyTranslate
46    
47      type input_desc = (Ty.ty * string * string option * bool)      type input_desc = (Ty.ty * string * string option * IL.exp option)
48    
49      val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")      val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")
50      val wrldPrefixTy = CL.T_Ptr(CL.T_Named "WorldPrefix_t")      val wrldPrefixTy = CL.T_Ptr(CL.T_Named "WorldPrefix_t")
# Line 43  Line 54 
54    (* translate a TreeIL type to the C types used to represent it in the external API *)    (* translate a TreeIL type to the C types used to represent it in the external API *)
55      val trType = CTyTranslate.toCType      val trType = CTyTranslate.toCType
56    
57      (* an l-value expression for accessing a defined-input flag *)
58        fun defined name = CL.mkSelect(CL.mkIndirect(CL.mkVar "wrld", "definedInp"), name)
59    
60    (* an l-value expression for accessing a global variable *)    (* an l-value expression for accessing a global variable *)
61      fun global name = CL.mkIndirect(CL.mkIndirect(CL.mkVar "wrld", "globals"), name)      fun global name = CL.mkIndirect(CL.mkIndirect(CL.mkVar "wrld", "globals"), name)
62    
63    (* gather the input globals from the input initialization block *)    (* gather the input globals from the input initialization block *)
64      fun gatherInputs (IL.Block{body, ...}) = let      fun gatherInputs (IL.Block{body, ...}) = let
65            fun inputInfo (x, name, desc, NONE) = SOME(IL.Var.ty x, name, desc, false)            fun inputInfo cvt (x, name, desc, NONE) =
66              | inputInfo (x, name, desc, SOME _) = SOME(IL.Var.ty x, name, desc, true)                  SOME(IL.Var.ty x, name, desc, NONE)
67            fun gather (IL.S_Input inp) = inputInfo inp              | inputInfo cvt (x, name, desc, SOME v) =
68              | gather (IL.S_InputNrrd inp) = inputInfo inp                  SOME(IL.Var.ty x, name, desc, SOME(cvt v))
69              fun gather (IL.S_Input inp) = inputInfo (fn e => e) inp
70                | gather (IL.S_InputNrrd inp) = inputInfo (fn s => TreeIL.E_Lit(Literal.String s)) inp
71              | gather _ = NONE              | gather _ = NONE
72            in            in
73              List.mapPartial gather body              List.mapPartial gather body
# Line 68  Line 84 
84              CL.D_StructDef(NONE, List.map mkField inputs, SOME(N.inputsTy tgt))              CL.D_StructDef(NONE, List.map mkField inputs, SOME(N.inputsTy tgt))
85            end            end
86    
87      (* generate code to initialize the default input values *)
88        fun genInitDefaults (tgt, inputs) = let
89            (* the inputs pointer type *)
90              val inputPtrTy = CL.T_Ptr(CL.T_Named(N.inputsTy tgt))
91            (* some common variables *)
92              val inpV = CL.mkVar "inp"
93            (* initialize a given input *)
94              fun initInput (ty, name, _, SOME dflt) = let
95                    val lhs = CL.mkIndirect(inpV, name)
96                    val rhs = TreeToC.trExp (TreeIL.Var.Map.empty, dflt)
97                    in
98                      SOME(CL.mkAssign(lhs, rhs))
99                    end
100                | initInput _ = NONE
101              in
102                CL.D_Func(
103                  ["static"], CL.voidTy, N.initDefaults,
104                  [CL.PARAM([], inputPtrTy, "inp")],
105                  CL.mkBlock(List.mapPartial initInput inputs))
106              end
107    
108    (* generate code to register command-line options for setting the input variables *)    (* generate code to register command-line options for setting the input variables *)
109      fun genRegisterInputs (tgt, inputs) = let      fun genRegisterInputs (tgt, inputs) = let
110          (* the inputs pointer type *)          (* the inputs pointer type *)
# Line 76  Line 113 
113            val inpV = CL.mkVar "inp"            val inpV = CL.mkVar "inp"
114            val optsV = CL.mkVar "opts"            val optsV = CL.mkVar "opts"
115          (* register a given input *)          (* register a given input *)
116            fun registerInput (ty, name, desc, hasDflt) = CL.mkCall(N.input ty, [            fun registerInput (ty, name, desc, optDflt) = CL.mkCall(N.input ty, [
117                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),
118                    CL.mkUnOp(CL.%&, CL.mkIndirect(inpV, name)),                    CL.mkUnOp(CL.%&, CL.mkIndirect(inpV, name)),
119                    CL.mkBool hasDflt                    CL.mkBool(Option.isSome optDflt)
120                  ])                  ])
121            in            in
122              CL.D_Func(              CL.D_Func(
# Line 98  Line 135 
135            val inputPtrTy = CL.T_Ptr(CL.T_Named(N.inputsTy tgt))            val inputPtrTy = CL.T_Ptr(CL.T_Named(N.inputsTy tgt))
136          (* some common variables *)          (* some common variables *)
137            val inpV = CL.mkVar "inp"            val inpV = CL.mkVar "inp"
           val globV = CL.mkVar "glob"  
138            val optsV = CL.mkVar "opts"            val optsV = CL.mkVar "opts"
139          (* initialize a given input global; for sequences and images, this requires          (* initialize a given input global; for sequences and images, this requires
140           * loading the value from the specified nrrd file, while for other types           * loading the value from the specified nrrd file, while for other types
141           * we just copy the values.           * we just copy the values.
142           *)           *)
143            fun initInput (Ty.DynSeqTy elemTy, name, desc, hasDflt) = CL.mkCall("dummy", [            fun initInput ((Ty.DynSeqTy elemTy, name, desc, optDflt), stms) = CL.mkCall("dummy", [
144                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),
145                    CL.mkUnOp(CL.%&, CL.mkIndirect(globV, name)),                    CL.mkUnOp(CL.%&, global name),
146                    CL.mkBool hasDflt                    CL.mkBool(Option.isSome optDflt)
147                  ])                  ]) :: stms
148              | initInput (Ty.ImageTy info, name, desc, hasDflt) = CL.mkCall("dummy", [              | initInput ((Ty.ImageTy info, name, desc, optDflt), stms) = CL.mkCall("dummy", [
149                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),                    optsV, CL.mkStr name, CL.mkStr(Option.getOpt(desc, "")),
150                    CL.mkUnOp(CL.%&, CL.mkIndirect(globV, name)),                    CL.mkUnOp(CL.%&, global name),
151                    CL.mkBool hasDflt                    CL.mkBool(Option.isSome optDflt)
152                  ])                  ]) :: stms
153              | initInput (ty, name, _, _) =              | initInput ((ty, name, _, _), stms) =
154                  CL.mkAssign(CL.mkIndirect(globV, name), CL.mkIndirect(inpV, name))                  TrTy.copyFromC{ty=ty, dst=global name, src=CL.mkIndirect(inpV, name)} @ stms
155            in            in
156              CL.D_Func(              CL.D_Func(
157                ["static"], CL.voidTy, N.initInputs,                ["static"], CL.voidTy, N.initInputs,
158                [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], inputPtrTy, "inp")],                [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], inputPtrTy, "inp")],
159                CL.mkBlock(                CL.mkBlock(
160                  CL.mkDeclInit(globPtrTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::                  CL.mkDeclInit(globPtrTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
161                  List.map initInput inputs))                  List.foldr initInput [] inputs))
162              end
163    
164      (* generate the functions that handle inputs for standalone executables.  These are:
165       *    InitDefaults    -- called to initialize the default input values
166       *    RegisterInputs  -- called to register the command-line options for the input globals
167       *    InitInptus      -- called to initialize the input globals from the values specified
168       *                       on the command line.
169       *)
170        fun genExecInputFuns arg = [
171                genInitDefaults arg,
172                genRegisterInputs arg,
173                genInitInputs arg
174              ]
175    
176      (* generate the typedef for the defined-input flag struct. *)
177        fun genDefinedInpStruct (tgt : target_desc, inputs) = let
178              fun mkField (_, name, _, _) = (CL.boolTy, name)
179              in
180                CL.D_StructDef(NONE, List.map mkField inputs, SOME(N.definedInpTy tgt))
181              end
182    
183        fun genDefineInp (tgt, inputs) = let
184            (* the world pointer type *)
185              val worldPtrTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
186              val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
187            (* the inputs pointer type *)
188              val defPtrTy = CL.T_Ptr(CL.T_Named(N.definedInpTy tgt))
189              fun initFlag (_, name, _, optDflt) =
190                    CL.mkAssign(defined name, CL.mkBool(Option.isSome optDflt))
191              in
192                CL.D_Func(
193                  ["static"], CL.voidTy, N.initDefined tgt,
194                  [wrldParam],
195                  CL.mkBlock(List.map initFlag inputs))
196            end            end
197    
198    (* for each input variable we generate two or three top-level declarations in the    (* for each input variable we generate two or three top-level declarations in the
# Line 133  Line 203 
203            val worldPtrTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))            val worldPtrTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
204            val wrldParam = CL.PARAM([], worldPtrTy, "wrld")            val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
205          (* create decls for an input variable *)          (* create decls for an input variable *)
206            fun mkInputDecls (ty, name, desc, hasDflt) = let            fun mkInputDecls (ty, name, desc, optDflt) = let
207                (* create a description declaration for the input variable *)                (* create a description declaration for the input variable *)
208                  val descDcl = (case desc                  val descDcl = (case desc
209                         of SOME desc => [                         of SOME desc => [
# Line 143  Line 213 
213                              ]                              ]
214                          | NONE => []                          | NONE => []
215                        (* end case *))                        (* end case *))
216                  val getDcl = if hasDflt                  val getDcl = if (Option.isSome optDflt)
217                          then let                          then let
218                            val getName = N.inputGet(tgt, name)                            val getName = N.inputGet(tgt, name)
219                          (* convert the input type to a by-reference C type *)                          (* convert the input type to a by-reference C type *)
# Line 232  Line 302 
302                                  [], CL.boolTy, N.inputSet(tgt, name),                                  [], CL.boolTy, N.inputSet(tgt, name),
303                                  [wrldParam, CL.PARAM([], trType ty, "v")],                                  [wrldParam, CL.PARAM([], trType ty, "v")],
304                                  CL.mkBlock(                                  CL.mkBlock(
305                                      CL.mkAssign(defined name, CL.mkBool true) ::
306                                    TrTy.copyFromC{ty=ty, dst=global name, src=CL.mkVar "v"} @                                    TrTy.copyFromC{ty=ty, dst=global name, src=CL.mkVar "v"} @
307                                      [CL.mkReturn(SOME(CL.mkVar "false"))]))                                      [CL.mkReturn(SOME(CL.mkVar "false"))]))
308                              ]                              ]
309                        (* end case *))                        (* end case *))
310                  in                  in
311                    descDcl @ getDcl @ setDcl                    genDefineInp (tgt, inputs) :: (descDcl @ getDcl @ setDcl)
312                  end                  end
313            in            in
314              List.foldr (fn (input, dcls) => mkInputDecls input @ dcls) [] inputs              List.foldr (fn (input, dcls) => mkInputDecls input @ dcls) [] inputs

Legend:
Removed from v.2047  
changed lines
  Added in v.2048

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