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

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

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