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 2818 - (view) (download)

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

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