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

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