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/c-ty-translate.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/c-util/c-ty-translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2813 - (view) (download)

1 : jhr 1818 (* c-ty-translate.sml
2 :     *
3 :     * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 : jhr 1820 *
6 :     * Functions to support the translation between the external C API and the internal (host-side)
7 :     * Diderot representation.
8 : jhr 1818 *)
9 :    
10 : jhr 1820 signature TY_TRANSLATE =
11 :     sig
12 :    
13 : jhr 2588 (* translate a type to its internal Diderot representation *)
14 : jhr 1820 val toType : TreeIL.Ty.ty -> CLang.ty
15 :    
16 :     (* translate a TreeIL type to the C types used to represent it in the external API *)
17 :     val toCType : TreeIL.Ty.ty -> CLang.ty
18 :    
19 : jhr 2118 (* return true if the external API typed used to represent a TreeIL type is a C array type *)
20 :     val isCArrayTy : TreeIL.Ty.ty -> bool
21 :    
22 : jhr 1820 (* generate code to copy values from their internal Diderot representation to the external
23 :     * C representation.
24 :     *)
25 :     val copyToC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list
26 :    
27 :     (* generate code to copy values from their external C representation to the internal
28 :     * Diderot representation.
29 :     *)
30 :     val copyFromC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list
31 :    
32 : jhr 2588 (* return a CLang expression that represents the size of the given Diderot type *)
33 :     val sizeOfType : TreeIL.Ty.ty -> CLang.exp
34 :    
35 : jhr 1820 end
36 :    
37 : jhr 1818 structure CTyTranslate : TY_TRANSLATE =
38 :     struct
39 :    
40 :     structure Ty = TreeIL.Ty
41 :     structure CL = CLang
42 :     structure N = CNames
43 :    
44 : jhr 2024 (* translate a type to its internal Diderot representation *)
45 : jhr 1820 fun toType ty = (case ty
46 : jhr 1818 of Ty.BoolTy => CL.T_Named "bool"
47 : jhr 1820 | Ty.StringTy => CL.T_Named "Diderot_string_t"
48 :     | Ty.IntTy => CL.T_Named "Diderot_int_t"
49 :     | Ty.TensorTy[] => CL.T_Named "Diderot_real_t"
50 : jhr 1818 | Ty.TensorTy[n] => CL.T_Named(N.vecTy n)
51 :     | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m))
52 :     | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l))
53 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n)
54 : jhr 1820 | Ty.SeqTy(ty, n) => CL.T_Array(toType ty, SOME n)
55 : jhr 1818 (* do we make the following types externally visible? *)
56 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
57 :     | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
58 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
59 : jhr 1820 | _ => raise Fail(concat["CTyTranslate.toType(", Ty.toString ty, ")"])
60 : jhr 1818 (* end case *))
61 :    
62 : jhr 1820 (* translate a TreeIL type to the C types used to represent it in the external API *)
63 :     fun toCType ty = (case ty
64 :     of Ty.BoolTy => CL.T_Named "bool"
65 :     | Ty.StringTy => CL.charPtr
66 :     | Ty.IntTy => !N.gIntTy
67 :     | Ty.TensorTy[] => !N.gRealTy
68 :     | Ty.TensorTy[n] => CL.T_Array(!N.gRealTy, SOME n)
69 :     | Ty.TensorTy[n, m] => CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m)
70 :     | Ty.TensorTy[n, m, l] => CL.T_Array(CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m), SOME l)
71 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!N.gIntTy, SOME n)
72 :     | Ty.SeqTy(ty, n) => CL.T_Array(toCType ty, SOME n)
73 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
74 :     | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
75 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
76 :     | _ => raise Fail(concat["CTyTranslate.toCType(", Ty.toString ty, ")"])
77 :     (* end case *))
78 :    
79 : jhr 2118 (* return true if the external API typed used to represent a TreeIL type is a C array type *)
80 :     fun isCArrayTy ty = (case ty
81 :     of Ty.TensorTy[n] => true
82 :     | Ty.TensorTy[n, m] => true
83 :     | Ty.TensorTy[n, m, l] => true
84 :     | Ty.SeqTy(Ty.IntTy, n) => true
85 :     | Ty.SeqTy(ty, n) => true
86 :     | _ => false
87 :     (* end case *))
88 :    
89 : jhr 1818 fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i))
90 :    
91 : jhr 1820 (* we might want to use memcpy in some cases
92 :     fun copy (ty, dst, src) = let
93 :     fun assign () = CL.mkAssign(dst, src)
94 :     fun addrOf (CL.E_UnOp(CL.%*, x)) = x
95 :     | addrOf x = CL.mkUnOp(CL.%&, x)
96 :     fun memcpy () = CL.mkCall("memcpy", [addrOf dst, addrOf src, CL.mkSizeof(trType ty)])
97 :     in
98 :     case ty
99 :     of Ty.BoolTy => assign()
100 :     | Ty.StringTy => CL.mkCall("strcpy", [addrOf dst, addrOf src])
101 :     | Ty.IntTy => assign()
102 :     | Ty.TensorTy[] => assign()
103 :     | Ty.TensorTy _ => memcpy()
104 :     | Ty.SeqTy _ => memcpy()
105 :     | Ty.DynSeqTy _ => raise Fail "dynamic sequence"
106 :     | Ty.ImageTy _ => raise Fail "unexpected image copy"
107 :     | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
108 :     (* end case *)
109 :     end
110 :     *)
111 :    
112 : jhr 1818 (* generate code to copy values from their internal Diderot representation to the external
113 : jhr 1820 * C representation.
114 : jhr 1818 *)
115 :     fun copyToC {ty, dst, src} = (case ty
116 : jhr 1820 of Ty.BoolTy => [CL.mkAssign(dst, src)]
117 :     | Ty.StringTy => [CL.mkAssign(dst, src)]
118 :     | Ty.IntTy => [CL.mkAssign(dst, src)]
119 :     | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
120 : jhr 1858 | Ty.TensorTy[n] => let
121 :     val src' = CL.mkCast(CL.T_Named(N.unionTy n), src)
122 :     in
123 :     List.tabulate (n,
124 :     fn i => CL.mkAssign(
125 : jhr 2813 subscript (dst, i),
126 : jhr 1858 subscript (CL.mkSelect(src', "r"), i)))
127 :     end
128 : jhr 1820 | Ty.TensorTy[n, m] => List.concat(List.tabulate(n,
129 : jhr 1818 fn i => List.tabulate (m,
130 :     fn j => CL.mkAssign(
131 :     subscript (dst, i*m + j),
132 : jhr 1820 subscript(CL.mkSelect(subscript (src, i), "r"), j)))))
133 :     | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n,
134 :     fn i => List.concat(List.tabulate (m,
135 : jhr 1818 fn j => List.tabulate (l,
136 :     fn k => CL.mkAssign(
137 :     subscript(dst, i*m*l + j*l + k),
138 : jhr 1820 subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j)))))))
139 : jhr 1818 | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n,
140 :     fn i => CL.mkAssign(
141 :     subscript (dst, i),
142 :     subscript (CL.mkSelect(src, "i"), i)))
143 : jhr 1820 (* do we make the following types externally visible?
144 : jhr 1818 | Ty.SeqTy(ty, n) =>
145 :     | Ty.DynSeqTy _ =>
146 :     | Ty.AddrTy info =>
147 :     | Ty.ImageTy info =>
148 : jhr 1820 *)
149 : jhr 1818 | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
150 :     (* end case *))
151 :    
152 :     (* generate code to copy values from their external C representation to the internal
153 : jhr 1820 * Diderot representation.
154 : jhr 1818 *)
155 :     fun copyFromC {ty, dst, src} = (case ty
156 : jhr 1820 of Ty.BoolTy => [CL.mkAssign(dst, src)]
157 :     | Ty.StringTy => [CL.mkAssign(dst, src)]
158 :     | Ty.IntTy => [CL.mkAssign(dst, src)]
159 : jhr 1857 | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
160 : jhr 1820 | Ty.TensorTy[n] => [CL.mkAssign(
161 : jhr 1858 dst,
162 : jhr 1820 CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))]
163 : jhr 1818 | Ty.TensorTy[n, m] => List.tabulate(n,
164 :     fn i => CL.mkAssign(
165 :     CL.mkSelect(subscript(dst, i), "v"),
166 :     CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j)))))
167 : jhr 1820 | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n,
168 : jhr 1818 fn i => List.tabulate (m,
169 : jhr 1820 fn j => CL.mkAssign(
170 : jhr 1818 CL.mkSelect(subscript(subscript(dst, i), j), "v"),
171 :     CL.mkApply(N.mkVec n,
172 : jhr 1820 List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k)))))))
173 :     | Ty.SeqTy(Ty.IntTy, n) => [
174 :     CL.mkAssign(
175 :     CL.mkSelect(dst, "v"),
176 :     CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i))))
177 :     ]
178 :     (* do we make the following types externally visible?
179 : jhr 1818 | Ty.SeqTy(ty, n) =>
180 :     | Ty.DynSeqTy _ =>
181 :     | Ty.AddrTy info =>
182 :     | Ty.ImageTy info =>
183 : jhr 1820 *)
184 : jhr 1818 | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
185 :     (* end case *))
186 :    
187 : jhr 2588 (* return a CLang expression that represents the size of the given Diderot type *)
188 :     fun sizeOfType ty = let
189 :     fun tensorSz n = CL.mkBinOp(CL.mkInt(IntInf.fromInt n), CL.#*, CL.E_Var "SIZEOF_DIDEROT_REAL")
190 :     in
191 :     case ty
192 :     of Ty.BoolTy => CL.mkSizeof CL.boolTy
193 :     | Ty.StringTy => CL.mkSizeof(CL.T_Named "Diderot_string_t")
194 :     | Ty.IntTy => CL.E_Var "SIZEOF_DIDEROT_INT"
195 :     | Ty.TensorTy[] => CL.E_Var "SIZEOF_DIDEROT_REAL"
196 :     | Ty.TensorTy[n] => tensorSz n
197 :     | Ty.TensorTy[n, m] => tensorSz (n*m)
198 :     | Ty.TensorTy[n, m, l] => tensorSz (n*m*l)
199 :     | Ty.SeqTy(Ty.IntTy, n) => CL.mkSizeof(CL.T_Named(N.ivecTy n))
200 :     | Ty.SeqTy(ty, n) => CL.mkSizeof(CL.T_Array(toType ty, SOME n))
201 :     (* do we make the following types externally visible? *)
202 :     | Ty.DynSeqTy _ => CL.mkSizeof CL.voidTy
203 :     | Ty.AddrTy info => CL.mkSizeof CL.voidTy
204 :     | Ty.ImageTy info => CL.mkSizeof CL.voidTy
205 :     | _ => raise Fail(concat["CTyTranslate.sizeOfType(", Ty.toString ty, ")"])
206 :     (* end case *)
207 :     end
208 :    
209 : jhr 1818 end

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