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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/c-util/gen-library-interface.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/c-util/gen-library-interface.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3291 - (view) (download)

1 : jhr 1701 (* gen-library-interface.sml
2 :     *
3 : jhr 3291 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 1701 * All rights reserved.
7 :     *
8 :     * Generate the C header file for the library produced by the compiler.
9 :     *
10 :     * The format of header file is:
11 :     *
12 : jhr 3223 * HEAD
13 :     * decls for input variables
14 :     * BODY
15 :     * decls for output variables
16 :     * FOOT
17 : jhr 1701 *)
18 :    
19 :     structure GenLibraryInterface : sig
20 :    
21 :     val gen : {
22 : jhr 3223 props : Properties.props, (* target information *)
23 :     rt : string option, (* fragment with extra runtime system hooks *)
24 :     inputs : GenInputsUtil.input_desc list,
25 :     outputs : (TreeIL.Ty.ty * string) list
26 :     } -> unit
27 : jhr 1701
28 :     end = struct
29 :    
30 :     structure Ty = TreeIL.Ty
31 :     structure CL = CLang
32 : jhr 3098 structure RN = RuntimeNames
33 : jhr 1701 structure PrC = PrintAsC
34 :    
35 :     val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")
36 :    
37 : jhr 1820 (* translate a TreeIL type to the C types used to represent it in the external API *)
38 :     val trType = CTyTranslate.toCType
39 : jhr 1701
40 :     fun mkSymbol base = let
41 : jhr 3223 fun tr c = if Char.isAlpha c then Char.toUpper c
42 :     else if Char.isDigit c then c
43 :     else #"_"
44 :     in
45 :     String.concat["_", CharVector.map tr base, "_H_"]
46 :     end
47 : jhr 1701
48 : jhr 2082 fun gen {props, rt, inputs, outputs} = let
49 : jhr 3223 val filename = OS.Path.joinDirFile{
50 :     dir = #outDir props,
51 :     file = OS.Path.joinBaseExt{base = #outBase props, ext = SOME "h"}
52 :     }
53 :     (* the world pointer type *)
54 :     val worldPtrTy = RN.worldPtrTy props
55 :     (* create decls for an input variable *)
56 : jhr 2818 fun mkInputDecls (gv, name, desc, optDflt) = let
57 : jhr 3223 val ty = TreeIL.GlobalVar.ty gv
58 : jhr 1803 val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
59 :     (* create a description declaration for the input variable *)
60 : jhr 2012 val descDcl = (case desc
61 : jhr 3223 of NONE => []
62 :     | SOME desc => [
63 :     CL.D_Var(["extern"], CL.T_Ptr(CL.T_Named "const char"),
64 :     RN.inputDesc(props, name), NONE)
65 :     ]
66 :     (* end case *))
67 : jhr 2048 val getDcl = if Option.isSome optDflt
68 : jhr 1803 then let
69 : jhr 3098 val name = RN.inputGet(props, name)
70 : jhr 1803 (* convert the input type to a by-reference C type *)
71 :     val outTy = (case ty
72 :     of Ty.BoolTy => CL.T_Ptr(trType ty)
73 :     | Ty.StringTy => CL.T_Ptr(trType ty)
74 :     | Ty.IntTy => CL.T_Ptr(trType ty)
75 :     | Ty.TensorTy[] => CL.T_Ptr(trType ty)
76 :     | Ty.TensorTy _=> trType ty
77 :     | Ty.SeqTy _ => trType ty
78 : jhr 2054 (* FIXME: for images and sequences, it is not clear what the get function should return.
79 :     * For now, we just return 0
80 :     *)
81 :     | Ty.DynSeqTy _ => CL.T_Ptr CL.voidPtr
82 :     | Ty.ImageTy _ => CL.T_Ptr CL.voidPtr
83 : jhr 1803 | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
84 :     (* end case *))
85 :     in [
86 :     CL.D_Proto([], CL.voidTy, name, [wrldParam, CL.PARAM([], outTy, "v")])
87 :     ] end
88 :     else []
89 : jhr 2054 val setDcl = let
90 : jhr 3223 (* prototypes for setting an image or dynamic sequence from a nrrd *)
91 :     fun loadPrototypes () = [
92 : jhr 1803 CL.D_Proto(
93 : jhr 3098 [], CL.boolTy, RN.inputSetByName(props, name),
94 : jhr 1803 [wrldParam, CL.PARAM(["const"], CL.charPtr, "s")]),
95 :     CL.D_Proto(
96 : jhr 3098 [], CL.boolTy, RN.inputSet(props, name),
97 : jhr 1803 [wrldParam, CL.PARAM([], nrrdPtrTy, "data")])
98 : jhr 3223 ]
99 :     in
100 :     case ty
101 :     of Ty.ImageTy _ => loadPrototypes()
102 :     | Ty.DynSeqTy _ => loadPrototypes()
103 :     | _ => [
104 :     CL.D_Proto(
105 :     [], CL.boolTy, RN.inputSet(props, name),
106 :     [wrldParam, CL.PARAM([], trType ty, "v")])
107 :     ]
108 :     (* end case *)
109 :     end
110 : jhr 1803 in
111 :     descDcl @ getDcl @ setDcl
112 :     end
113 : jhr 3223 (* create a decl for an output variable *)
114 :     fun mkGetDecl snapshot (Ty.DynSeqTy _, name) = [
115 :     CL.D_Proto(
116 :     [], CL.boolTy, (if snapshot then RN.snapshotGet else RN.outputGet)(props, name),
117 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
118 :     ]
119 :     | mkGetDecl snapshot (_, name) = [
120 :     CL.D_Proto(
121 :     [], CL.boolTy, (if snapshot then RN.snapshotGet else RN.outputGet)(props, name),
122 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
123 :     ]
124 : jhr 3199 (* NOTE: there is some redundancy between placeholders and the substitutions defined in the target files *)
125 : jhr 3223 val placeholders = [
126 :     ("DIDEROTC_CMD", #diderotc props),
127 :     ("DIDEROTC_ARGV", String.concatWith " " (#argv props)),
128 :     ("DIDEROTC_VERSION", #version props),
129 :     ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props),
130 :     ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props),
131 :     ("DIDEROT_TARGET", Properties.targetDef props),
132 :     ("HDRFILE", filename),
133 :     ("HDR_DEFINE", mkSymbol(#outBase props)),
134 :     ("PREFIX", #namespace props),
135 :     ("SRCFILE", #srcFile props)
136 :     ]
137 :     val outStrm = TextIO.openOut filename
138 :     val outS = PrC.new outStrm
139 :     fun prFrag frag = PrC.output (outS, CL.verbatimDcl [frag] placeholders)
140 :     val prDecls = List.app (fn dcl => PrC.output(outS, dcl))
141 :     in
142 :     prFrag LibInterfaceHeadFrag.text;
143 :     PrC.output (outS, CL.D_Verbatim ["\n/**** Functions etc. for input variables ****/\n"]);
144 :     List.app (fn input => prDecls (mkInputDecls input)) inputs;
145 :     prFrag LibInterfaceRTFrag.text;
146 :     case rt of SOME rt => prFrag rt | _ => ();
147 :     prFrag LibInterfaceBodyFrag.text;
148 :     PrC.output (outS, CL.D_Verbatim ["\n/**** Getters for output values ****/\n"]);
149 :     if (#snapshot props)
150 :     then List.app (fn output => prDecls (mkGetDecl true output)) outputs
151 :     else ();
152 :     List.app (fn output => prDecls (mkGetDecl false output)) outputs;
153 :     prFrag LibInterfaceFootFrag.text;
154 :     PrC.close outS;
155 :     TextIO.closeOut outStrm
156 :     end
157 : jhr 1701
158 :     end

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