SCM Repository
Annotation of /branches/lamont/src/compiler/c-util/c-ty-translate.sml
Parent Directory
|
Revision Log
Revision 2083 - (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 : | lamonts | 2083 | structure RN = RuntimeNames |
38 : | jhr | 1818 | |
39 : | jhr | 2024 | (* translate a type to its internal Diderot representation *) |
40 : | jhr | 1820 | fun toType ty = (case ty |
41 : | jhr | 1818 | of Ty.BoolTy => CL.T_Named "bool" |
42 : | jhr | 1820 | | Ty.StringTy => CL.T_Named "Diderot_string_t" |
43 : | | Ty.IntTy => CL.T_Named "Diderot_int_t" | ||
44 : | | Ty.TensorTy[] => CL.T_Named "Diderot_real_t" | ||
45 : | jhr | 1818 | | Ty.TensorTy[n] => CL.T_Named(N.vecTy n) |
46 : | | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m)) | ||
47 : | | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l)) | ||
48 : | | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n) | ||
49 : | jhr | 1820 | | Ty.SeqTy(ty, n) => CL.T_Array(toType ty, SOME n) |
50 : | lamonts | 2083 | | Ty.StrandSeqTy n => CL.T_Ptr(CL.T_Named (RN.strandTy (Atom.toString n))) |
51 : | jhr | 1818 | (* do we make the following types externally visible? *) |
52 : | | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy) | ||
53 : | | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info)) | ||
54 : | | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info))) | ||
55 : | jhr | 1820 | | _ => raise Fail(concat["CTyTranslate.toType(", Ty.toString ty, ")"]) |
56 : | jhr | 1818 | (* end case *)) |
57 : | |||
58 : | jhr | 1820 | (* translate a TreeIL type to the C types used to represent it in the external API *) |
59 : | fun toCType ty = (case ty | ||
60 : | of Ty.BoolTy => CL.T_Named "bool" | ||
61 : | | Ty.StringTy => CL.charPtr | ||
62 : | | Ty.IntTy => !N.gIntTy | ||
63 : | lamonts | 2083 | | Ty.StrandSeqTy n => CL.T_Ptr(CL.T_Named (RN.strandTy (Atom.toString n))) |
64 : | jhr | 1820 | | 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 | 1818 | fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i)) |
77 : | |||
78 : | jhr | 1820 | (* we might want to use memcpy in some cases |
79 : | fun copy (ty, dst, src) = let | ||
80 : | fun assign () = CL.mkAssign(dst, src) | ||
81 : | fun addrOf (CL.E_UnOp(CL.%*, x)) = x | ||
82 : | | addrOf x = CL.mkUnOp(CL.%&, x) | ||
83 : | fun memcpy () = CL.mkCall("memcpy", [addrOf dst, addrOf src, CL.mkSizeof(trType ty)]) | ||
84 : | in | ||
85 : | case ty | ||
86 : | of Ty.BoolTy => assign() | ||
87 : | | Ty.StringTy => CL.mkCall("strcpy", [addrOf dst, addrOf src]) | ||
88 : | | Ty.IntTy => assign() | ||
89 : | | Ty.TensorTy[] => assign() | ||
90 : | | Ty.TensorTy _ => memcpy() | ||
91 : | | Ty.SeqTy _ => memcpy() | ||
92 : | | Ty.DynSeqTy _ => raise Fail "dynamic sequence" | ||
93 : | | Ty.ImageTy _ => raise Fail "unexpected image copy" | ||
94 : | | _ => raise Fail(concat["bogus input type ", Ty.toString ty]) | ||
95 : | (* end case *) | ||
96 : | end | ||
97 : | *) | ||
98 : | |||
99 : | jhr | 1818 | (* generate code to copy values from their internal Diderot representation to the external |
100 : | jhr | 1820 | * C representation. |
101 : | jhr | 1818 | *) |
102 : | fun copyToC {ty, dst, src} = (case ty | ||
103 : | jhr | 1820 | of Ty.BoolTy => [CL.mkAssign(dst, src)] |
104 : | | Ty.StringTy => [CL.mkAssign(dst, src)] | ||
105 : | | Ty.IntTy => [CL.mkAssign(dst, src)] | ||
106 : | | Ty.TensorTy[] => [CL.mkAssign(dst, src)] | ||
107 : | jhr | 1858 | | Ty.TensorTy[n] => let |
108 : | val src' = CL.mkCast(CL.T_Named(N.unionTy n), src) | ||
109 : | in | ||
110 : | List.tabulate (n, | ||
111 : | fn i => CL.mkAssign( | ||
112 : | subscript (CL.mkUnOp(CL.%&, dst), i), | ||
113 : | subscript (CL.mkSelect(src', "r"), i))) | ||
114 : | end | ||
115 : | jhr | 1820 | | Ty.TensorTy[n, m] => List.concat(List.tabulate(n, |
116 : | jhr | 1818 | fn i => List.tabulate (m, |
117 : | fn j => CL.mkAssign( | ||
118 : | subscript (dst, i*m + j), | ||
119 : | jhr | 1820 | subscript(CL.mkSelect(subscript (src, i), "r"), j))))) |
120 : | | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n, | ||
121 : | fn i => List.concat(List.tabulate (m, | ||
122 : | jhr | 1818 | fn j => List.tabulate (l, |
123 : | fn k => CL.mkAssign( | ||
124 : | subscript(dst, i*m*l + j*l + k), | ||
125 : | jhr | 1820 | subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j))))))) |
126 : | jhr | 1818 | | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n, |
127 : | fn i => CL.mkAssign( | ||
128 : | subscript (dst, i), | ||
129 : | subscript (CL.mkSelect(src, "i"), i))) | ||
130 : | jhr | 1820 | (* do we make the following types externally visible? |
131 : | jhr | 1818 | | Ty.SeqTy(ty, n) => |
132 : | | Ty.DynSeqTy _ => | ||
133 : | | Ty.AddrTy info => | ||
134 : | | Ty.ImageTy info => | ||
135 : | jhr | 1820 | *) |
136 : | jhr | 1818 | | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"]) |
137 : | (* end case *)) | ||
138 : | |||
139 : | (* generate code to copy values from their external C representation to the internal | ||
140 : | jhr | 1820 | * Diderot representation. |
141 : | jhr | 1818 | *) |
142 : | fun copyFromC {ty, dst, src} = (case ty | ||
143 : | jhr | 1820 | of Ty.BoolTy => [CL.mkAssign(dst, src)] |
144 : | | Ty.StringTy => [CL.mkAssign(dst, src)] | ||
145 : | | Ty.IntTy => [CL.mkAssign(dst, src)] | ||
146 : | jhr | 1857 | | Ty.TensorTy[] => [CL.mkAssign(dst, src)] |
147 : | jhr | 1820 | | Ty.TensorTy[n] => [CL.mkAssign( |
148 : | jhr | 1858 | dst, |
149 : | jhr | 1820 | CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))] |
150 : | jhr | 1818 | | Ty.TensorTy[n, m] => List.tabulate(n, |
151 : | fn i => CL.mkAssign( | ||
152 : | CL.mkSelect(subscript(dst, i), "v"), | ||
153 : | CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j))))) | ||
154 : | jhr | 1820 | | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n, |
155 : | jhr | 1818 | fn i => List.tabulate (m, |
156 : | jhr | 1820 | fn j => CL.mkAssign( |
157 : | jhr | 1818 | CL.mkSelect(subscript(subscript(dst, i), j), "v"), |
158 : | CL.mkApply(N.mkVec n, | ||
159 : | jhr | 1820 | List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k))))))) |
160 : | | Ty.SeqTy(Ty.IntTy, n) => [ | ||
161 : | CL.mkAssign( | ||
162 : | CL.mkSelect(dst, "v"), | ||
163 : | CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i)))) | ||
164 : | ] | ||
165 : | (* do we make the following types externally visible? | ||
166 : | jhr | 1818 | | Ty.SeqTy(ty, n) => |
167 : | | Ty.DynSeqTy _ => | ||
168 : | | Ty.AddrTy info => | ||
169 : | | Ty.ImageTy info => | ||
170 : | jhr | 1820 | *) |
171 : | jhr | 1818 | | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"]) |
172 : | (* end case *)) | ||
173 : | |||
174 : | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |