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 1818 - (view) (download)
Original Path: branches/vis12/src/compiler/c-target/c-ty-translate.sml

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 :     *)
6 :    
7 :     structure CTyTranslate : TY_TRANSLATE =
8 :     struct
9 :    
10 :     structure Ty = TreeIL.Ty
11 :     structure CL = CLang
12 :     structure N = CNames
13 :    
14 :     (* translate TreeIL types to CLang types *)
15 :     fun trType ty = (case ty
16 :     of Ty.BoolTy => CL.T_Named "bool"
17 :     | Ty.StringTy => CL.charPtr
18 :     | Ty.IntTy => !N.gIntTy
19 :     | Ty.TensorTy[] => !N.gRealTy
20 :     | Ty.TensorTy[n] => CL.T_Named(N.vecTy n)
21 :     | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m))
22 :     | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l))
23 :     | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n)
24 :     | Ty.SeqTy(ty, n) => CL.T_Array(trType ty, SOME n)
25 :     (* do we make the following types externally visible? *)
26 :     | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
27 :     | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
28 :     | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info)))
29 :     | _ => raise Fail(concat["CTyTranslate.trType(", Ty.toString ty, ")"])
30 :     (* end case *))
31 :    
32 :     fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i))
33 :    
34 :     (* generate code to copy values from their internal Diderot representation to the external
35 :     * C representation. If the representations are identical, then the empty list is returned.
36 :     *)
37 :     fun copyToC {ty, dst, src} = (case ty
38 :     of Ty.BoolTy => []
39 :     | Ty.StringTy => []
40 :     | Ty.IntTy => []
41 :     | Ty.TensorTy[] => []
42 :     | Ty.TensorTy[n] => List.tabulate (n,
43 :     fn i => CL.mkAssign(
44 :     subscript (dst, i),
45 :     subscript (CL.mkSelect(src, "r"), i)))
46 :     | Ty.TensorTy[n, m] => List.tabulate(n,
47 :     fn i => List.tabulate (m,
48 :     fn j => CL.mkAssign(
49 :     subscript (dst, i*m + j),
50 :     subscript(CL.mkSelect(subscript (src, i), "r"), j))))
51 :     | Ty.TensorTy[n, m, l] => List.tabulate(n,
52 :     fn i => List.tabulate (m,
53 :     fn j => List.tabulate (l,
54 :     fn k => CL.mkAssign(
55 :     subscript(dst, i*m*l + j*l + k),
56 :     subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j)))))
57 :     | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n,
58 :     fn i => CL.mkAssign(
59 :     subscript (dst, i),
60 :     subscript (CL.mkSelect(src, "i"), i)))
61 :     | Ty.SeqTy(ty, n) =>
62 :     | Ty.DynSeqTy _ =>
63 :     | Ty.AddrTy info =>
64 :     | Ty.ImageTy info =>
65 :     | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
66 :     (* end case *))
67 :    
68 :     (* generate code to copy values from their external C representation to the internal
69 :     * Diderot representation. If the representations are identical, then the empty list
70 :     * is returned.
71 :     *)
72 :     fun copyFromC {ty, dst, src} = (case ty
73 :     of Ty.BoolTy => []
74 :     | Ty.StringTy => []
75 :     | Ty.IntTy => []
76 :     | Ty.TensorTy[n] => CL.mkAssign(
77 :     CL.mkSelect(dst, "v"),
78 :     CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))
79 :     | Ty.TensorTy[n, m] => List.tabulate(n,
80 :     fn i => CL.mkAssign(
81 :     CL.mkSelect(subscript(dst, i), "v"),
82 :     CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j)))))
83 :     | Ty.TensorTy[n, m, l] => List.tabulate(n,
84 :     fn i => List.tabulate (m,
85 :     fn j => (
86 :     CL.mkSelect(subscript(subscript(dst, i), j), "v"),
87 :     CL.mkApply(N.mkVec n,
88 :     List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k))))))
89 :     | Ty.SeqTy(Ty.IntTy, n) => CL.mkAssign(
90 :     CL.mkSelect(dst, "v"),
91 :     CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i))))
92 :     | Ty.SeqTy(ty, n) =>
93 :     | Ty.DynSeqTy _ =>
94 :     | Ty.AddrTy info =>
95 :     | Ty.ImageTy info =>
96 :     | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"])
97 :     (* end case *))
98 :    
99 :     end

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