Move swizzles out of ir_dereference and into their own class.
[mesa.git] / ir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <string.h>
24 #include "main/imports.h"
25 #include "main/simple_list.h"
26 #include "ir.h"
27 #include "glsl_types.h"
28
29 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
30 ir_rvalue *condition)
31 : ir_rvalue()
32 {
33 this->lhs = lhs;
34 this->rhs = rhs;
35 this->condition = condition;
36 }
37
38
39 ir_expression::ir_expression(int op, const struct glsl_type *type,
40 ir_rvalue *op0, ir_rvalue *op1)
41 : ir_rvalue()
42 {
43 this->type = type;
44 this->operation = ir_expression_operation(op);
45 this->operands[0] = op0;
46 this->operands[1] = op1;
47 }
48
49
50 ir_label::ir_label(const char *label)
51 : ir_instruction(), label(label)
52 {
53 /* empty */
54 }
55
56
57 ir_constant::ir_constant(const struct glsl_type *type, const void *data)
58 : ir_rvalue()
59 {
60 const unsigned elements =
61 ((type->vector_elements == 0) ? 1 : type->vector_elements)
62 * ((type->matrix_columns == 0) ? 1 : type->matrix_columns);
63 unsigned size = 0;
64
65 this->type = type;
66 switch (type->base_type) {
67 case GLSL_TYPE_UINT: size = sizeof(this->value.u[0]); break;
68 case GLSL_TYPE_INT: size = sizeof(this->value.i[0]); break;
69 case GLSL_TYPE_FLOAT: size = sizeof(this->value.f[0]); break;
70 case GLSL_TYPE_BOOL: size = sizeof(this->value.b[0]); break;
71 default:
72 /* FINISHME: What to do? Exceptions are not the answer.
73 */
74 break;
75 }
76
77 memcpy(& this->value, data, size * elements);
78 }
79
80
81 ir_dereference::ir_dereference(ir_instruction *var)
82 : ir_rvalue()
83 {
84 this->mode = ir_reference_variable;
85 this->var = var;
86 this->type = (var != NULL) ? var->type : glsl_error_type;
87 }
88
89
90 ir_dereference::ir_dereference(ir_instruction *var,
91 ir_rvalue *array_index)
92 : ir_rvalue(), mode(ir_reference_array),
93 var(var)
94 {
95 this->type = (var != NULL) ? var->type : glsl_error_type;
96 this->selector.array_index = array_index;
97 }
98
99
100 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
101 unsigned w, unsigned count)
102 : val(val)
103 {
104 assert((count >= 1) && (count <= 4));
105
106 const unsigned dup_mask = 0
107 | ((count > 1) ? ((1U << y) & ((1U << x) )) : 0)
108 | ((count > 2) ? ((1U << z) & ((1U << x) | (1U << y) )) : 0)
109 | ((count > 3) ? ((1U << w) & ((1U << x) | (1U << y) | (1U << z))) : 0);
110
111 assert(x <= 3);
112 assert(y <= 3);
113 assert(z <= 3);
114 assert(w <= 3);
115
116 mask.x = x;
117 mask.y = y;
118 mask.z = z;
119 mask.w = w;
120 mask.num_components = count;
121 mask.has_duplicates = dup_mask != 0;
122
123 /* Based on the number of elements in the swizzle and the base type
124 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
125 * generate the type of the resulting value.
126 */
127 type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
128 }
129
130 #define X 1
131 #define R 5
132 #define S 9
133 #define I 13
134
135 ir_swizzle *
136 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
137 {
138 /* For each possible swizzle character, this table encodes the value in
139 * \c idx_map that represents the 0th element of the vector. For invalid
140 * swizzle characters (e.g., 'k'), a special value is used that will allow
141 * detection of errors.
142 */
143 static const unsigned char base_idx[26] = {
144 /* a b c d e f g h i j k l m */
145 R, R, I, I, I, I, R, I, I, I, I, I, I,
146 /* n o p q r s t u v w x y z */
147 I, I, S, S, R, S, S, I, I, X, X, X, X
148 };
149
150 /* Each valid swizzle character has an entry in the previous table. This
151 * table encodes the base index encoded in the previous table plus the actual
152 * index of the swizzle character. When processing swizzles, the first
153 * character in the string is indexed in the previous table. Each character
154 * in the string is indexed in this table, and the value found there has the
155 * value form the first table subtracted. The result must be on the range
156 * [0,3].
157 *
158 * For example, the string "wzyx" will get X from the first table. Each of
159 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
160 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
161 *
162 * The string "wzrg" will get X from the first table. Each of the characters
163 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
164 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
165 * [0,3], the error is detected.
166 */
167 static const unsigned char idx_map[26] = {
168 /* a b c d e f g h i j k l m */
169 R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
170 /* n o p q r s t u v w x y z */
171 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
172 };
173
174 int swiz_idx[4] = { 0, 0, 0, 0 };
175 unsigned i;
176
177
178 /* Validate the first character in the swizzle string and look up the base
179 * index value as described above.
180 */
181 if ((str[0] < 'a') || (str[0] > 'z'))
182 return NULL;
183
184 const unsigned base = base_idx[str[0] - 'a'];
185
186
187 for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
188 /* Validate the next character, and, as described above, convert it to a
189 * swizzle index.
190 */
191 if ((str[i] < 'a') || (str[i] > 'z'))
192 return NULL;
193
194 swiz_idx[i] = idx_map[str[i] - 'a'] - base;
195 if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
196 return NULL;
197 }
198
199 if (str[i] != '\0')
200 return NULL;
201
202 return new ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
203 swiz_idx[3], i);
204 }
205
206 #undef X
207 #undef R
208 #undef S
209 #undef I
210
211
212 ir_variable::ir_variable(const struct glsl_type *type, const char *name)
213 : ir_instruction(), read_only(false), centroid(false), invariant(false),
214 mode(ir_var_auto), interpolation(ir_var_smooth)
215 {
216 this->type = type;
217 this->name = name;
218 }
219
220
221 ir_function_signature::ir_function_signature(const glsl_type *return_type)
222 : ir_instruction(), return_type(return_type), definition(NULL)
223 {
224 /* empty */
225 }
226
227
228 ir_function::ir_function(const char *name)
229 : ir_instruction(), name(name)
230 {
231 /* empty */
232 }
233
234
235 ir_call *
236 ir_call::get_error_instruction()
237 {
238 ir_call *call = new ir_call;
239
240 call->type = glsl_error_type;
241 return call;
242 }