SCM Repository
Annotation of /trunk/src/compiler/IL/types.sml
Parent Directory
|
Revision Log
Revision 60 -
(view)
(download)
Original Path: trunk/src/IL/types.sml
1 : | jhr | 7 | (* types.sml |
2 : | * | ||
3 : | * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) | ||
4 : | * All rights reserved. | ||
5 : | jhr | 9 | * |
6 : | * This file defines the internal representation of Diderot types. Most values | ||
7 : | * have the tensor type, which includes scalars, vectors, etc. The internal | ||
8 : | * type system supports polymorphism, which is used to give types to higher-order | ||
9 : | * operators (e.g., derivatives). | ||
10 : | jhr | 7 | *) |
11 : | |||
12 : | jhr | 8 | structure Types = |
13 : | struct | ||
14 : | jhr | 7 | |
15 : | (* kinds for type variables *) | ||
16 : | datatype kind | ||
17 : | = TK_DIM (* ranges over dimensions (1, 2, ...) *) | ||
18 : | | TK_INT (* ranges over integer types *) | ||
19 : | | TK_FLT (* ranges over floating-point types *) | ||
20 : | | TK_RAW (* ranges over raw (scalar) types *) | ||
21 : | jhr | 8 | | TK_TYPE (* ranges over types *) |
22 : | (* Question: do we want kinds for tensors of different orders (e.g., TK_TENSOR of word?); | ||
23 : | * Then TK_RAW would be TK_TENSOR of 0w0. | ||
24 : | *) | ||
25 : | jhr | 7 | |
26 : | jhr | 8 | (* raw numeric types as supported by NRRD *) |
27 : | jhr | 7 | datatype raw_ty |
28 : | jhr | 8 | = RT_Int8 | RT_UInt8 |
29 : | | RT_Int16 | RT_UInt16 | ||
30 : | | RT_Int32 | RT_UInt32 | ||
31 : | | RT_Int64 | RT_UInt64 | ||
32 : | | RT_Float | RT_Double | ||
33 : | | RT_Var of var | ||
34 : | jhr | 7 | |
35 : | jhr | 8 | and ty |
36 : | jhr | 7 | = T_Var of var |
37 : | | T_Bool | ||
38 : | (* scalars, vectors, matrices, etc. *) | ||
39 : | jhr | 12 | (* Question: perhaps we want {order : word, dim : dim, ty : raw_ty} instead *) |
40 : | jhr | 7 | | T_Tensor of {order : dim list, ty : raw_ty} |
41 : | jhr | 12 | (* Question: change "data set" to "image"? *) |
42 : | jhr | 60 | (* data sets from NRRD *) |
43 : | jhr | 7 | | T_Data of { |
44 : | dim : dim, (* 2D or 3D data set *) | ||
45 : | jhr | 60 | shape : dim list, (* tensor shape; order is length of list *) |
46 : | jhr | 8 | repTy : raw_ty (* representation type of elements (raw kind) *) |
47 : | jhr | 7 | } |
48 : | (* continuous field reconstructed from a data set *) | ||
49 : | | T_Field of { | ||
50 : | jhr | 60 | diff : dim, (* number of levels of differentiation supported *) |
51 : | jhr | 7 | dim : dim, (* 2D or 3D field *) |
52 : | jhr | 60 | shape : dim list, (* tensor shape; order is length of list *) |
53 : | jhr | 8 | repTy : raw_ty, (* representation type of elements (raw kind) *) |
54 : | ty : raw_ty (* type of samples (float kind) *) | ||
55 : | jhr | 7 | } |
56 : | |||
57 : | and dim = DIM of word | DIMVAR of var | ||
58 : | |||
59 : | jhr | 8 | (* type variables; the kind field restricts their range *) |
60 : | jhr | 7 | and var = TV of { |
61 : | jhr | 8 | kind : kind, |
62 : | stamp : Stamp.stamp | ||
63 : | jhr | 7 | } |
64 : | |||
65 : | (* given a dataset or field type, return the element type, which will be | ||
66 : | * a tensor type. | ||
67 : | *) | ||
68 : | jhr | 8 | fun elemTypeOf (T_Data{order, repTy, ...}) = T_Tensor{order=order, ty=repTy} |
69 : | jhr | 7 | | elemTypeOf (T_Field{order, ty, ...}) = T_Tensor{order=order, ty=ty} |
70 : | | elemTypeOf _ = raise Fail "not a dataset/field" | ||
71 : | |||
72 : | jhr | 8 | (* is a type well-formed? *) |
73 : | fun wellFormed ty = let | ||
74 : | (* is a dimension well-formed? *) | ||
75 : | fun okDim (DIM w) = (w > 0w0) | ||
76 : | | okDim (DIMVAR(TV{kind=TK_DIM, ...})) = true | ||
77 : | | okDim _ = false | ||
78 : | (* is a raw type well formed *) | ||
79 : | fun okRawTy (RT_Var(TV{kind=TK_INT, ...})) = true | ||
80 : | | okRawTy (RT_Var(TV{kind=TK_FLT, ...})) = true | ||
81 : | | okRawTy (RT_Var(TV{kind=TK_RAW, ...})) = true | ||
82 : | | okRawTy (RT_Var _) = false | ||
83 : | | okRawTy _ = true | ||
84 : | (* is a raw type a floating-point type? *) | ||
85 : | fun okFloatTy (RT_Var(TV{kind=TK_FLT, ...})) = true | ||
86 : | | okFloatTy RT_Float = true | ||
87 : | | okFloatTy RT_Double = true | ||
88 : | | okFloatTy _ = false | ||
89 : | fun wellFormed' (T_Var(TV{kind=TK_TYPE, ...})) = true | ||
90 : | | wellFormed' (T_Var _) = false | ||
91 : | | wellFormed' (T_Tensor{order, ty}) = | ||
92 : | List.all okDim order | ||
93 : | andalso okRawTy ty | ||
94 : | | wellFormed' (T_Data{dim, order, repTy}) = | ||
95 : | okDim dim | ||
96 : | andalso List.all okDim order | ||
97 : | andalso okRawTy repTy | ||
98 : | | wellFormed' (T_Field{dim, order, repTy, ty}) = | ||
99 : | okDim dim | ||
100 : | andalso List.all okDim order | ||
101 : | andalso okRawTy repTy | ||
102 : | jhr | 12 | andalso okFloatTy ty |
103 : | jhr | 8 | | wellFormed' _ = false |
104 : | in | ||
105 : | wellFormed' ty | ||
106 : | end | ||
107 : | |||
108 : | jhr | 9 | (* some common types *) |
109 : | local | ||
110 : | fun scalarTy rty = T_Tensor{order=[], ty=rty} | ||
111 : | fun vecTy (d, rty) = T_Tensor{order=[DIM d], ty=rty} | ||
112 : | in | ||
113 : | val intTy = scalarTy RT_Int32 | ||
114 : | val int2Ty = vecTy (0w2, RT_Int32) | ||
115 : | val intT3y = vecTy (0w3, RT_Int32) | ||
116 : | val intT4y = vecTy (0w4, RT_Int32) | ||
117 : | val uintTy = scalarTy RT_UInt32 | ||
118 : | val uint2Ty = vecTy (0w2, RT_UInt32) | ||
119 : | val uintT3y = vecTy (0w3, RT_UInt32) | ||
120 : | val uintT4y = vecTy (0w4, RT_UInt32) | ||
121 : | val floatTy = scalarTy RT_Float | ||
122 : | val float2Ty = vecTy (0w2, RT_Float) | ||
123 : | val floatT3y = vecTy (0w3, RT_Float) | ||
124 : | val floatT4y = vecTy (0w4, RT_Float) | ||
125 : | val doubleTy = scalarTy RT_Double | ||
126 : | val double2Ty = vecTy (0w2, RT_Double) | ||
127 : | val double3Ty = vecTy (0w3, RT_Double) | ||
128 : | val double4Ty = vecTy (0w4, RT_Double) | ||
129 : | end (* local *) | ||
130 : | |||
131 : | (* new type variables *) | ||
132 : | fun newVar k = TV{kind=k, stamp=Stamp.new()} | ||
133 : | |||
134 : | jhr | 7 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |