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 2118 - (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 :     (* translate a type to its internal Diderot represenation *)
14 :     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 :     end
33 :    
34 : jhr 1818 structure CTyTranslate : TY_TRANSLATE =
35 :     struct
36 :    
37 :     structure Ty = TreeIL.Ty
38 :     structure CL = CLang
39 :     structure N = CNames
40 :    
41 : jhr 2024 (* translate a type to its internal Diderot representation *)
42 : jhr 1820 fun toType ty = (case ty
43 : jhr 1818 of Ty.BoolTy => CL.T_Named "bool"
44 : jhr 1820 | Ty.StringTy => CL.T_Named "Diderot_string_t"
45 :     | Ty.IntTy => CL.T_Named "Diderot_int_t"
46 :     | Ty.TensorTy[] => CL.T_Named "Diderot_real_t"
47 : jhr 1818 | Ty.TensorTy[n] => CL.T_Named(N.vecTy n)
48 :     | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m))
49 :     | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l))
50 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n)
51 : jhr 1820 | Ty.SeqTy(ty, n) => CL.T_Array(toType ty, SOME n)
52 : jhr 1818 (* do we make the following types externally visible? *)
53 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
54 :     | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
55 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
56 : jhr 1820 | _ => raise Fail(concat["CTyTranslate.toType(", Ty.toString ty, ")"])
57 : jhr 1818 (* end case *))
58 :    
59 : jhr 1820 (* translate a TreeIL type to the C types used to represent it in the external API *)
60 :     fun toCType ty = (case ty
61 :     of Ty.BoolTy => CL.T_Named "bool"
62 :     | Ty.StringTy => CL.charPtr
63 :     | Ty.IntTy => !N.gIntTy
64 :     | Ty.TensorTy[] => !N.gRealTy
65 :     | Ty.TensorTy[n] => CL.T_Array(!N.gRealTy, SOME n)
66 :     | Ty.TensorTy[n, m] => CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m)
67 :     | Ty.TensorTy[n, m, l] => CL.T_Array(CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m), SOME l)
68 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!N.gIntTy, SOME n)
69 :     | Ty.SeqTy(ty, n) => CL.T_Array(toCType ty, SOME n)
70 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
71 :     | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
72 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
73 :     | _ => raise Fail(concat["CTyTranslate.toCType(", Ty.toString ty, ")"])
74 :     (* end case *))
75 :    
76 : jhr 2118 (* return true if the external API typed used to represent a TreeIL type is a C array type *)
77 :     fun isCArrayTy ty = (case ty
78 :     of Ty.TensorTy[n] => true
79 :     | Ty.TensorTy[n, m] => true
80 :     | Ty.TensorTy[n, m, l] => true
81 :     | Ty.SeqTy(Ty.IntTy, n) => true
82 :     | Ty.SeqTy(ty, n) => true
83 :     | _ => false
84 :     (* end case *))
85 :    
86 : jhr 1818 fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i))
87 :    
88 : jhr 1820 (* we might want to use memcpy in some cases
89 :     fun copy (ty, dst, src) = let
90 :     fun assign () = CL.mkAssign(dst, src)
91 :     fun addrOf (CL.E_UnOp(CL.%*, x)) = x
92 :     | addrOf x = CL.mkUnOp(CL.%&, x)
93 :     fun memcpy () = CL.mkCall("memcpy", [addrOf dst, addrOf src, CL.mkSizeof(trType ty)])
94 :     in
95 :     case ty
96 :     of Ty.BoolTy => assign()
97 :     | Ty.StringTy => CL.mkCall("strcpy", [addrOf dst, addrOf src])
98 :     | Ty.IntTy => assign()
99 :     | Ty.TensorTy[] => assign()
100 :     | Ty.TensorTy _ => memcpy()
101 :     | Ty.SeqTy _ => memcpy()
102 :     | Ty.DynSeqTy _ => raise Fail "dynamic sequence"
103 :     | Ty.ImageTy _ => raise Fail "unexpected image copy"
104 :     | _ => raise Fail(concat["bogus input type ", Ty.toString ty])
105 :     (* end case *)
106 :     end
107 :     *)
108 :    
109 : jhr 1818 (* generate code to copy values from their internal Diderot representation to the external
110 : jhr 1820 * C representation.
111 : jhr 1818 *)
112 :     fun copyToC {ty, dst, src} = (case ty
113 : jhr 1820 of Ty.BoolTy => [CL.mkAssign(dst, src)]
114 :     | Ty.StringTy => [CL.mkAssign(dst, src)]
115 :     | Ty.IntTy => [CL.mkAssign(dst, src)]
116 :     | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
117 : jhr 1858 | Ty.TensorTy[n] => let
118 :     val src' = CL.mkCast(CL.T_Named(N.unionTy n), src)
119 :     in
120 :     List.tabulate (n,
121 :     fn i => CL.mkAssign(
122 :     subscript (CL.mkUnOp(CL.%&, dst), i),
123 :     subscript (CL.mkSelect(src', "r"), i)))
124 :     end
125 : jhr 1820 | Ty.TensorTy[n, m] => List.concat(List.tabulate(n,
126 : jhr 1818 fn i => List.tabulate (m,
127 :     fn j => CL.mkAssign(
128 :     subscript (dst, i*m + j),
129 : jhr 1820 subscript(CL.mkSelect(subscript (src, i), "r"), j)))))
130 :     | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n,
131 :     fn i => List.concat(List.tabulate (m,
132 : jhr 1818 fn j => List.tabulate (l,
133 :     fn k => CL.mkAssign(
134 :     subscript(dst, i*m*l + j*l + k),
135 : jhr 1820 subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j)))))))
136 : jhr 1818 | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n,
137 :     fn i => CL.mkAssign(
138 :     subscript (dst, i),
139 :     subscript (CL.mkSelect(src, "i"), i)))
140 : jhr 1820 (* do we make the following types externally visible?
141 : jhr 1818 | Ty.SeqTy(ty, n) =>
142 :     | Ty.DynSeqTy _ =>
143 :     | Ty.AddrTy info =>
144 :     | Ty.ImageTy info =>
145 : jhr 1820 *)
146 : jhr 1818 | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
147 :     (* end case *))
148 :    
149 :     (* generate code to copy values from their external C representation to the internal
150 : jhr 1820 * Diderot representation.
151 : jhr 1818 *)
152 :     fun copyFromC {ty, dst, src} = (case ty
153 : jhr 1820 of Ty.BoolTy => [CL.mkAssign(dst, src)]
154 :     | Ty.StringTy => [CL.mkAssign(dst, src)]
155 :     | Ty.IntTy => [CL.mkAssign(dst, src)]
156 : jhr 1857 | Ty.TensorTy[] => [CL.mkAssign(dst, src)]
157 : jhr 1820 | Ty.TensorTy[n] => [CL.mkAssign(
158 : jhr 1858 dst,
159 : jhr 1820 CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))]
160 : jhr 1818 | Ty.TensorTy[n, m] => List.tabulate(n,
161 :     fn i => CL.mkAssign(
162 :     CL.mkSelect(subscript(dst, i), "v"),
163 :     CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j)))))
164 : jhr 1820 | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n,
165 : jhr 1818 fn i => List.tabulate (m,
166 : jhr 1820 fn j => CL.mkAssign(
167 : jhr 1818 CL.mkSelect(subscript(subscript(dst, i), j), "v"),
168 :     CL.mkApply(N.mkVec n,
169 : jhr 1820 List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k)))))))
170 :     | Ty.SeqTy(Ty.IntTy, n) => [
171 :     CL.mkAssign(
172 :     CL.mkSelect(dst, "v"),
173 :     CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i))))
174 :     ]
175 :     (* do we make the following types externally visible?
176 : jhr 1818 | Ty.SeqTy(ty, n) =>
177 :     | Ty.DynSeqTy _ =>
178 :     | Ty.AddrTy info =>
179 :     | Ty.ImageTy info =>
180 : jhr 1820 *)
181 : jhr 1818 | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
182 :     (* end case *))
183 :    
184 :     end

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