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 1793 - (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 1701 inputs : (TreeIL.Ty.ty * string) list,
22 :     outputs : (TreeIL.Ty.ty * string) list
23 :     } -> unit
24 :    
25 :     end = struct
26 :    
27 :     structure Ty = TreeIL.Ty
28 :     structure CL = CLang
29 :     structure N = CNames
30 :     structure PrC = PrintAsC
31 :    
32 :     val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")
33 :    
34 :     (* FIXME: we will need this function elsewhere *)
35 :     (* translate TreeIL types to CLang types *)
36 :     fun trType ty = (case ty
37 :     of Ty.BoolTy => CL.T_Named "bool"
38 :     | Ty.StringTy => CL.charPtr
39 :     | Ty.IntTy => !N.gIntTy
40 :     | Ty.TensorTy[] => !N.gRealTy
41 :     | Ty.TensorTy[n] => CL.T_Array(!N.gRealTy, SOME n)
42 :     | Ty.TensorTy[n, m] => CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m)
43 :     | Ty.TensorTy[n, m, l] => CL.T_Array(CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m), SOME l)
44 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!N.gIntTy, SOME n)
45 :     | Ty.SeqTy(ty, n) => CL.T_Array(trType ty, SOME n)
46 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
47 : jhr 1793 | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
48 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
49 : jhr 1713 | _ => raise Fail(concat["GenLibraryInterface.trType(", Ty.toString ty, ")"])
50 : jhr 1701 (* end case *))
51 :    
52 :     fun mkSymbol base = let
53 :     fun tr c = if Char.isAlpha c then Char.toUpper c
54 :     else if Char.isDigit c then c
55 :     else #"_"
56 :     in
57 :     String.concat["_", CharVector.map tr base, "_H_"]
58 :     end
59 :    
60 : jhr 1706 fun gen {tgt : TargetUtil.target_desc, inputs, outputs} = let
61 : jhr 1701 val filename = OS.Path.joinDirFile{
62 : jhr 1706 dir = #outDir tgt,
63 :     file = OS.Path.joinBaseExt{base = #outBase tgt, ext = SOME "h"}
64 : jhr 1701 }
65 : jhr 1713 val prefix = #namespace tgt
66 : jhr 1701 (* the world pointer type *)
67 :     val worldPtrTy = CL.T_Ptr(CL.T_Named(prefix ^ "World_t"))
68 :     (* create a decl for an input variable *)
69 :     fun mkSetDecl (Ty.ImageTy _, name) = [
70 :     CL.D_Proto(
71 :     [], CL.voidTy, concat[prefix, "Set_", name, "_ByName"],
72 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM(["const"], CL.charPtr, "s")]),
73 :     CL.D_Proto(
74 :     [], CL.voidTy, concat[prefix, "Set_", name],
75 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
76 :     ]
77 :     | mkSetDecl (ty as Ty.DynSeqTy _, name) = raise Fail "dynamic input not supported yet"
78 :     | mkSetDecl (ty, name) = [
79 :     CL.D_Proto(
80 :     [], CL.voidTy, concat[prefix, "Set_", name],
81 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], trType ty, "v")])
82 :     ]
83 :     (* create a decl for an output variable *)
84 :     fun mkGetDecl (Ty.DynSeqTy _, name) = [
85 :     CL.D_Proto(
86 :     [], CL.boolTy, concat[prefix, "Get_", name],
87 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
88 :     ]
89 :     | mkGetDecl (_, name) = [
90 :     CL.D_Proto(
91 :     [], CL.boolTy, concat[prefix, "Get_", name],
92 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
93 :     ]
94 :     val placeholders = [
95 : jhr 1706 ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt),
96 :     ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt),
97 :     ("DIDEROT_TARGET", TargetUtil.targetDef tgt),
98 : jhr 1701 ("HDRFILE", filename),
99 : jhr 1706 ("HDR_DEFINE", mkSymbol(#outBase tgt)),
100 : jhr 1701 ("PREFIX", prefix),
101 : jhr 1706 ("SRCFILE", #srcFile tgt)
102 : jhr 1701 ]
103 :     val outStrm = TextIO.openOut filename
104 :     val outS = PrC.new outStrm
105 :     in
106 :     PrC.output (outS, CL.verbatim [LibInterfaceHeadFrag.text] placeholders);
107 : jhr 1706 PrC.output (outS, CL.D_Verbatim ["\n/**** Setters for input values ****/\n"]);
108 : jhr 1701 List.app (fn input => List.app (fn dcl => PrC.output(outS, dcl)) (mkSetDecl input)) inputs;
109 :     PrC.output (outS, CL.verbatim [LibInterfaceBodyFrag.text] placeholders);
110 : jhr 1706 PrC.output (outS, CL.D_Verbatim ["\n/**** Getters for output values ****/\n"]);
111 : jhr 1701 List.app (fn output => List.app (fn dcl => PrC.output(outS, dcl)) (mkGetDecl output)) outputs;
112 :     PrC.output (outS, CL.verbatim [LibInterfaceFootFrag.text] placeholders);
113 :     PrC.close outS;
114 :     TextIO.closeOut outStrm
115 :     end
116 :    
117 :     end

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