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 1706 - (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 :     | Ty.AddrTy(ImageInfo.ImgInfo{ty=(_, rTy), ...}) => CL.T_Ptr(CL.T_Num rTy)
48 :     | Ty.ImageTy(ImageInfo.ImgInfo{dim, ...}) => CL.T_Ptr(CL.T_Named(N.imageTy dim))
49 :     | _ => raise Fail(concat["TreeToC.trType(", Ty.toString ty, ")"])
50 :     (* 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 1706 val prefix = (case #namespace tgt
66 : jhr 1701 of NONE => "Diderot_"
67 :     | SOME s => s ^ "_"
68 :     (* end case *))
69 :     (* the world pointer type *)
70 :     val worldPtrTy = CL.T_Ptr(CL.T_Named(prefix ^ "World_t"))
71 :     (* create a decl for an input variable *)
72 :     fun mkSetDecl (Ty.ImageTy _, name) = [
73 :     CL.D_Proto(
74 :     [], CL.voidTy, concat[prefix, "Set_", name, "_ByName"],
75 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM(["const"], CL.charPtr, "s")]),
76 :     CL.D_Proto(
77 :     [], CL.voidTy, concat[prefix, "Set_", name],
78 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
79 :     ]
80 :     | mkSetDecl (ty as Ty.DynSeqTy _, name) = raise Fail "dynamic input not supported yet"
81 :     | mkSetDecl (ty, name) = [
82 :     CL.D_Proto(
83 :     [], CL.voidTy, concat[prefix, "Set_", name],
84 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], trType ty, "v")])
85 :     ]
86 :     (* create a decl for an output variable *)
87 :     fun mkGetDecl (Ty.DynSeqTy _, name) = [
88 :     CL.D_Proto(
89 :     [], CL.boolTy, concat[prefix, "Get_", name],
90 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "lengths"), CL.PARAM([], nrrdPtrTy, "data")])
91 :     ]
92 :     | mkGetDecl (_, name) = [
93 :     CL.D_Proto(
94 :     [], CL.boolTy, concat[prefix, "Get_", name],
95 :     [CL.PARAM([], worldPtrTy, "wrld"), CL.PARAM([], nrrdPtrTy, "data")])
96 :     ]
97 :     val placeholders = [
98 : jhr 1706 ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt),
99 :     ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt),
100 :     ("DIDEROT_TARGET", TargetUtil.targetDef tgt),
101 : jhr 1701 ("HDRFILE", filename),
102 : jhr 1706 ("HDR_DEFINE", mkSymbol(#outBase tgt)),
103 : jhr 1701 ("PREFIX", prefix),
104 : jhr 1706 ("SRCFILE", #srcFile tgt)
105 : jhr 1701 ]
106 :     val outStrm = TextIO.openOut filename
107 :     val outS = PrC.new outStrm
108 :     in
109 :     PrC.output (outS, CL.verbatim [LibInterfaceHeadFrag.text] placeholders);
110 : jhr 1706 PrC.output (outS, CL.D_Verbatim ["\n/**** Setters for input values ****/\n"]);
111 : jhr 1701 List.app (fn input => List.app (fn dcl => PrC.output(outS, dcl)) (mkSetDecl input)) inputs;
112 :     PrC.output (outS, CL.verbatim [LibInterfaceBodyFrag.text] placeholders);
113 : jhr 1706 PrC.output (outS, CL.D_Verbatim ["\n/**** Getters for output values ****/\n"]);
114 : jhr 1701 List.app (fn output => List.app (fn dcl => PrC.output(outS, dcl)) (mkGetDecl output)) outputs;
115 :     PrC.output (outS, CL.verbatim [LibInterfaceFootFrag.text] placeholders);
116 :     PrC.close outS;
117 :     TextIO.closeOut outStrm
118 :     end
119 :    
120 :     end

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