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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2081 - (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 1706 tgt : TargetUtil.target_desc, (* target information *)
21 : jhr 1844 rt : string option, (* fragment with extra runtime system hooks *)
22 : jhr 2048 inputs : (TreeIL.Ty.ty * string * string option * TreeIL.exp option) 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 1844 fun gen {tgt, rt, inputs, outputs} = let
47 : jhr 1701 val filename = OS.Path.joinDirFile{
48 : jhr 1706 dir = #outDir tgt,
49 :     file = OS.Path.joinBaseExt{base = #outBase tgt, ext = SOME "h"}
50 : jhr 1701 }
51 :     (* the world pointer type *)
52 : jhr 1815 val worldPtrTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
53 : jhr 1803 (* create decls for an input variable *)
54 : jhr 2048 fun mkInputDecls (ty, name, desc, optDflt) = let
55 : jhr 1803 val wrldParam = CL.PARAM([], worldPtrTy, "wrld")
56 :     (* create a description declaration for the input variable *)
57 : jhr 2012 val descDcl = (case desc
58 :     of NONE => []
59 :     | SOME desc => [
60 :     CL.D_Var(["extern"], CL.T_Ptr(CL.T_Named "const char"),
61 :     N.inputDesc(tgt, name), NONE)
62 :     ]
63 :     (* end case *))
64 : jhr 2048 val getDcl = if Option.isSome optDflt
65 : jhr 1803 then let
66 : jhr 1815 val name = N.inputGet(tgt, name)
67 : jhr 1803 (* convert the input type to a by-reference C type *)
68 :     val outTy = (case ty
69 :     of Ty.BoolTy => CL.T_Ptr(trType ty)
70 :     | Ty.StringTy => CL.T_Ptr(trType ty)
71 :     | Ty.IntTy => CL.T_Ptr(trType ty)
72 :     | Ty.TensorTy[] => CL.T_Ptr(trType ty)
73 :     | Ty.TensorTy _=> trType ty
74 :     | Ty.SeqTy _ => trType ty
75 : jhr 2054 (* FIXME: for images and sequences, it is not clear what the get function should return.
76 :     * For now, we just return 0
77 :     *)
78 :     | Ty.DynSeqTy _ => CL.T_Ptr CL.voidPtr
79 :     | Ty.ImageTy _ => CL.T_Ptr CL.voidPtr
80 : jhr 1803 | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
81 :     (* end case *))
82 :     in [
83 :     CL.D_Proto([], CL.voidTy, name, [wrldParam, CL.PARAM([], outTy, "v")])
84 :     ] end
85 :     else []
86 : jhr 2054 val setDcl = let
87 :     (* prototypes for setting an image or dynamic sequence from a nrrd *)
88 :     fun loadPrototypes () = [
89 : jhr 1803 CL.D_Proto(
90 : jhr 1815 [], CL.boolTy, N.inputSetByName(tgt, name),
91 : jhr 1803 [wrldParam, CL.PARAM(["const"], CL.charPtr, "s")]),
92 :     CL.D_Proto(
93 : jhr 1815 [], CL.boolTy, N.inputSet(tgt, name),
94 : jhr 1803 [wrldParam, CL.PARAM([], nrrdPtrTy, "data")])
95 : jhr 2054 ]
96 :     in
97 :     case ty
98 :     of Ty.ImageTy _ => loadPrototypes()
99 :     | Ty.DynSeqTy _ => loadPrototypes()
100 :     | _ => [
101 :     CL.D_Proto(
102 :     [], CL.boolTy, N.inputSet(tgt, name),
103 :     [wrldParam, CL.PARAM([], trType ty, "v")])
104 :     ]
105 :     (* end case *)
106 :     end
107 : jhr 1803 in
108 :     descDcl @ getDcl @ setDcl
109 :     end
110 : jhr 1701 (* create a decl for an output variable *)
111 : jhr 1912 fun mkGetDecl snapshot (Ty.DynSeqTy _, name) = [
112 : jhr 1701 CL.D_Proto(
113 : jhr 1912 [], CL.boolTy, (if snapshot then N.snapshotGet else N.outputGet)(tgt, name),
114 : jhr 1701 [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
115 :     ]
116 : jhr 1912 | mkGetDecl snapshot (_, name) = [
117 : jhr 1701 CL.D_Proto(
118 : jhr 1912 [], CL.boolTy, (if snapshot then N.snapshotGet else N.outputGet)(tgt, name),
119 : jhr 1701 [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
120 :     ]
121 :     val placeholders = [
122 : jhr 1706 ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt),
123 :     ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt),
124 :     ("DIDEROT_TARGET", TargetUtil.targetDef tgt),
125 : jhr 1701 ("HDRFILE", filename),
126 : jhr 1706 ("HDR_DEFINE", mkSymbol(#outBase tgt)),
127 : jhr 1815 ("PREFIX", #namespace tgt),
128 : jhr 1706 ("SRCFILE", #srcFile tgt)
129 : jhr 1701 ]
130 :     val outStrm = TextIO.openOut filename
131 :     val outS = PrC.new outStrm
132 : jhr 1844 fun prFrag frag = PrC.output (outS, CL.verbatim [frag] placeholders)
133 :     val prDecls = List.app (fn dcl => PrC.output(outS, dcl))
134 : jhr 1701 in
135 : jhr 1844 prFrag LibInterfaceHeadFrag.text;
136 : jhr 1812 PrC.output (outS, CL.D_Verbatim ["\n/**** Functions etc. for input variables ****/\n"]);
137 : jhr 1844 List.app (fn input => prDecls (mkInputDecls input)) inputs;
138 :     prFrag LibInterfaceRTFrag.text;
139 :     case rt of SOME rt => prFrag rt | _ => ();
140 :     prFrag LibInterfaceBodyFrag.text;
141 : jhr 1706 PrC.output (outS, CL.D_Verbatim ["\n/**** Getters for output values ****/\n"]);
142 : jhr 1912 if (#snapshot tgt)
143 :     then List.app (fn output => prDecls (mkGetDecl true output)) outputs
144 :     else ();
145 :     List.app (fn output => prDecls (mkGetDecl false output)) outputs;
146 : jhr 1844 prFrag LibInterfaceFootFrag.text;
147 : jhr 1701 PrC.close outS;
148 :     TextIO.closeOut outStrm
149 :     end
150 :    
151 :     end

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