Construct an ir_constant from a list of ir_constant values
[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 "ir.h"
26 #include "ir_visitor.h"
27 #include "glsl_types.h"
28
29 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
30 ir_rvalue *condition)
31 {
32 this->lhs = lhs;
33 this->rhs = rhs;
34 this->condition = condition;
35 }
36
37
38 ir_expression::ir_expression(int op, const struct glsl_type *type,
39 ir_rvalue *op0, ir_rvalue *op1)
40 {
41 this->type = type;
42 this->operation = ir_expression_operation(op);
43 this->operands[0] = op0;
44 this->operands[1] = op1;
45 }
46
47 unsigned int
48 ir_expression::get_num_operands(ir_expression_operation op)
49 {
50 /* Update ir_print_visitor.cpp when updating this list. */
51 const int num_operands[] = {
52 1, /* ir_unop_bit_not */
53 1, /* ir_unop_logic_not */
54 1, /* ir_unop_neg */
55 1, /* ir_unop_abs */
56 1, /* ir_unop_sign */
57 1, /* ir_unop_rcp */
58 1, /* ir_unop_rsq */
59 1, /* ir_unop_sqrt */
60 1, /* ir_unop_exp */
61 1, /* ir_unop_log */
62 1, /* ir_unop_exp2 */
63 1, /* ir_unop_log2 */
64 1, /* ir_unop_f2i */
65 1, /* ir_unop_i2f */
66 1, /* ir_unop_f2b */
67 1, /* ir_unop_b2f */
68 1, /* ir_unop_i2b */
69 1, /* ir_unop_b2i */
70 1, /* ir_unop_u2f */
71
72 1, /* ir_unop_trunc */
73 1, /* ir_unop_ceil */
74 1, /* ir_unop_floor */
75
76 1, /* ir_unop_sin */
77 1, /* ir_unop_cos */
78
79 1, /* ir_unop_dFdx */
80 1, /* ir_unop_dFdy */
81
82 2, /* ir_binop_add */
83 2, /* ir_binop_sub */
84 2, /* ir_binop_mul */
85 2, /* ir_binop_div */
86 2, /* ir_binop_mod */
87
88 2, /* ir_binop_less */
89 2, /* ir_binop_greater */
90 2, /* ir_binop_lequal */
91 2, /* ir_binop_gequal */
92 2, /* ir_binop_equal */
93 2, /* ir_binop_nequal */
94
95 2, /* ir_binop_lshift */
96 2, /* ir_binop_rshift */
97 2, /* ir_binop_bit_and */
98 2, /* ir_binop_bit_xor */
99 2, /* ir_binop_bit_or */
100
101 2, /* ir_binop_logic_and */
102 2, /* ir_binop_logic_xor */
103 2, /* ir_binop_logic_or */
104
105 2, /* ir_binop_dot */
106 2, /* ir_binop_min */
107 2, /* ir_binop_max */
108
109 2, /* ir_binop_pow */
110 };
111
112 assert(sizeof(num_operands) / sizeof(num_operands[0]) == ir_binop_pow + 1);
113
114 return num_operands[op];
115 }
116
117 static const char *const operator_strs[] = {
118 "~",
119 "!",
120 "neg",
121 "abs",
122 "sign",
123 "rcp",
124 "rsq",
125 "sqrt",
126 "exp",
127 "log",
128 "exp2",
129 "log2",
130 "f2i",
131 "i2f",
132 "f2b",
133 "b2f",
134 "i2b",
135 "b2i",
136 "u2f",
137 "trunc",
138 "ceil",
139 "floor",
140 "sin",
141 "cos",
142 "dFdx",
143 "dFdy",
144 "+",
145 "-",
146 "*",
147 "/",
148 "%",
149 "<",
150 ">",
151 "<=",
152 ">=",
153 "==",
154 "!=",
155 "<<",
156 ">>",
157 "&",
158 "^",
159 "|",
160 "&&",
161 "^^",
162 "||",
163 "dot",
164 "min",
165 "max",
166 "pow",
167 };
168
169 const char *ir_expression::operator_string()
170 {
171 assert((unsigned int) operation <=
172 sizeof(operator_strs) / sizeof(operator_strs[0]));
173 return operator_strs[operation];
174 }
175
176 ir_expression_operation
177 ir_expression::get_operator(const char *str)
178 {
179 const int operator_count = sizeof(operator_strs) / sizeof(operator_strs[0]);
180 for (int op = 0; op < operator_count; op++) {
181 if (strcmp(str, operator_strs[op]) == 0)
182 return (ir_expression_operation) op;
183 }
184 return (ir_expression_operation) -1;
185 }
186
187 ir_constant::ir_constant(const struct glsl_type *type, const void *data)
188 {
189 unsigned size = 0;
190
191 this->type = type;
192 switch (type->base_type) {
193 case GLSL_TYPE_UINT: size = sizeof(this->value.u[0]); break;
194 case GLSL_TYPE_INT: size = sizeof(this->value.i[0]); break;
195 case GLSL_TYPE_FLOAT: size = sizeof(this->value.f[0]); break;
196 case GLSL_TYPE_BOOL: size = sizeof(this->value.b[0]); break;
197 default:
198 /* FINISHME: What to do? Exceptions are not the answer.
199 */
200 break;
201 }
202
203 memcpy(& this->value, data, size * type->components());
204 }
205
206 ir_constant::ir_constant(float f)
207 {
208 this->type = glsl_type::float_type;
209 this->value.f[0] = f;
210 }
211
212 ir_constant::ir_constant(unsigned int u)
213 {
214 this->type = glsl_type::uint_type;
215 this->value.u[0] = u;
216 }
217
218 ir_constant::ir_constant(int i)
219 {
220 this->type = glsl_type::int_type;
221 this->value.i[0] = i;
222 }
223
224 ir_constant::ir_constant(bool b)
225 {
226 this->type = glsl_type::bool_type;
227 this->value.b[0] = b;
228 }
229
230 ir_constant::ir_constant(const ir_constant *c, unsigned i)
231 {
232 this->type = c->type->get_base_type();
233
234 switch (this->type->base_type) {
235 case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
236 case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;
237 case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
238 case GLSL_TYPE_BOOL: this->value.b[0] = c->value.b[i]; break;
239 default: assert(!"Should not get here."); break;
240 }
241 }
242
243 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
244 {
245 this->type = type;
246
247 /* FINISHME: Support structure and array types. */
248 assert(type->is_scalar() || type->is_vector() || type->is_matrix());
249
250 ir_constant *value = (ir_constant *) (value_list->head);
251
252 /* Use each component from each entry in the value_list to initialize one
253 * component of the constant being constructed.
254 */
255 for (unsigned i = 0; i < type->components(); /* empty */) {
256 assert(value->as_constant() != NULL);
257 assert(!value->is_tail_sentinal());
258
259 for (unsigned j = 0; j < value->type->components(); j++) {
260 switch (type->base_type) {
261 case GLSL_TYPE_UINT:
262 this->value.u[i] = value->get_uint_component(j);
263 break;
264 case GLSL_TYPE_INT:
265 this->value.i[i] = value->get_int_component(j);
266 break;
267 case GLSL_TYPE_FLOAT:
268 this->value.f[i] = value->get_float_component(j);
269 break;
270 case GLSL_TYPE_BOOL:
271 this->value.b[i] = value->get_bool_component(j);
272 break;
273 default:
274 /* FINISHME: What to do? Exceptions are not the answer.
275 */
276 break;
277 }
278
279 i++;
280 if (i >= type->components())
281 break;
282 }
283
284 value = (ir_constant *) value->next;
285 }
286 }
287
288 bool
289 ir_constant::get_bool_component(unsigned i) const
290 {
291 switch (this->type->base_type) {
292 case GLSL_TYPE_UINT: return this->value.u[i] != 0;
293 case GLSL_TYPE_INT: return this->value.i[i] != 0;
294 case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
295 case GLSL_TYPE_BOOL: return this->value.b[i];
296 default: assert(!"Should not get here."); break;
297 }
298
299 /* Must return something to make the compiler happy. This is clearly an
300 * error case.
301 */
302 return false;
303 }
304
305 float
306 ir_constant::get_float_component(unsigned i) const
307 {
308 switch (this->type->base_type) {
309 case GLSL_TYPE_UINT: return (float) this->value.u[i];
310 case GLSL_TYPE_INT: return (float) this->value.i[i];
311 case GLSL_TYPE_FLOAT: return this->value.f[i];
312 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0 : 0.0;
313 default: assert(!"Should not get here."); break;
314 }
315
316 /* Must return something to make the compiler happy. This is clearly an
317 * error case.
318 */
319 return 0.0;
320 }
321
322 int
323 ir_constant::get_int_component(unsigned i) const
324 {
325 switch (this->type->base_type) {
326 case GLSL_TYPE_UINT: return this->value.u[i];
327 case GLSL_TYPE_INT: return this->value.i[i];
328 case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
329 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
330 default: assert(!"Should not get here."); break;
331 }
332
333 /* Must return something to make the compiler happy. This is clearly an
334 * error case.
335 */
336 return 0;
337 }
338
339 unsigned
340 ir_constant::get_uint_component(unsigned i) const
341 {
342 switch (this->type->base_type) {
343 case GLSL_TYPE_UINT: return this->value.u[i];
344 case GLSL_TYPE_INT: return this->value.i[i];
345 case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
346 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
347 default: assert(!"Should not get here."); break;
348 }
349
350 /* Must return something to make the compiler happy. This is clearly an
351 * error case.
352 */
353 return 0;
354 }
355
356
357 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
358 {
359 this->var = var;
360 this->type = (var != NULL) ? var->type : glsl_type::error_type;
361 }
362
363
364 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
365 ir_rvalue *array_index)
366 {
367 this->array_index = array_index;
368 this->set_array(value);
369 }
370
371
372 ir_dereference_array::ir_dereference_array(ir_variable *var,
373 ir_rvalue *array_index)
374 {
375 this->array_index = array_index;
376 this->set_array(new ir_dereference_variable(var));
377 }
378
379
380 void
381 ir_dereference_array::set_array(ir_rvalue *value)
382 {
383 this->array = value;
384 this->type = glsl_type::error_type;
385
386 if (this->array != NULL) {
387 const glsl_type *const vt = this->array->type;
388
389 if (vt->is_array()) {
390 type = vt->element_type();
391 } else if (vt->is_matrix()) {
392 type = vt->column_type();
393 } else if (vt->is_vector()) {
394 type = vt->get_base_type();
395 }
396 }
397 }
398
399
400 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
401 const char *field)
402 {
403 this->record = value;
404 this->field = field;
405 this->type = (this->record != NULL)
406 ? this->record->type->field_type(field) : glsl_type::error_type;
407 }
408
409
410 ir_dereference_record::ir_dereference_record(ir_variable *var,
411 const char *field)
412 {
413 this->record = new ir_dereference_variable(var);
414 this->field = field;
415 this->type = (this->record != NULL)
416 ? this->record->type->field_type(field) : glsl_type::error_type;
417 }
418
419
420 bool
421 ir_dereference::is_lvalue()
422 {
423 ir_variable *var = this->variable_referenced();
424
425 /* Every l-value derference chain eventually ends in a variable.
426 */
427 if ((var == NULL) || var->read_only)
428 return false;
429
430 if (this->type->is_array() && !var->array_lvalue)
431 return false;
432
433 return true;
434 }
435
436
437 const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf" };
438
439 const char *ir_texture::opcode_string()
440 {
441 assert((unsigned int) op <=
442 sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]));
443 return tex_opcode_strs[op];
444 }
445
446 ir_texture_opcode
447 ir_texture::get_opcode(const char *str)
448 {
449 const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
450 for (int op = 0; op < count; op++) {
451 if (strcmp(str, tex_opcode_strs[op]) == 0)
452 return (ir_texture_opcode) op;
453 }
454 return (ir_texture_opcode) -1;
455 }
456
457
458 void
459 ir_texture::set_sampler(ir_dereference *sampler)
460 {
461 assert(sampler != NULL);
462 this->sampler = sampler;
463
464 switch (sampler->type->sampler_type) {
465 case GLSL_TYPE_FLOAT:
466 this->type = glsl_type::vec4_type;
467 break;
468 case GLSL_TYPE_INT:
469 this->type = glsl_type::ivec4_type;
470 break;
471 case GLSL_TYPE_UINT:
472 this->type = glsl_type::uvec4_type;
473 break;
474 }
475 }
476
477
478 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
479 unsigned w, unsigned count)
480 : val(val)
481 {
482 assert((count >= 1) && (count <= 4));
483
484 const unsigned dup_mask = 0
485 | ((count > 1) ? ((1U << y) & ((1U << x) )) : 0)
486 | ((count > 2) ? ((1U << z) & ((1U << x) | (1U << y) )) : 0)
487 | ((count > 3) ? ((1U << w) & ((1U << x) | (1U << y) | (1U << z))) : 0);
488
489 assert(x <= 3);
490 assert(y <= 3);
491 assert(z <= 3);
492 assert(w <= 3);
493
494 mask.x = x;
495 mask.y = y;
496 mask.z = z;
497 mask.w = w;
498 mask.num_components = count;
499 mask.has_duplicates = dup_mask != 0;
500
501 /* Based on the number of elements in the swizzle and the base type
502 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
503 * generate the type of the resulting value.
504 */
505 type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
506 }
507
508 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
509 {
510 this->val = val;
511 this->mask = mask;
512 this->type = glsl_type::get_instance(val->type->base_type,
513 mask.num_components, 1);
514 }
515
516 #define X 1
517 #define R 5
518 #define S 9
519 #define I 13
520
521 ir_swizzle *
522 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
523 {
524 /* For each possible swizzle character, this table encodes the value in
525 * \c idx_map that represents the 0th element of the vector. For invalid
526 * swizzle characters (e.g., 'k'), a special value is used that will allow
527 * detection of errors.
528 */
529 static const unsigned char base_idx[26] = {
530 /* a b c d e f g h i j k l m */
531 R, R, I, I, I, I, R, I, I, I, I, I, I,
532 /* n o p q r s t u v w x y z */
533 I, I, S, S, R, S, S, I, I, X, X, X, X
534 };
535
536 /* Each valid swizzle character has an entry in the previous table. This
537 * table encodes the base index encoded in the previous table plus the actual
538 * index of the swizzle character. When processing swizzles, the first
539 * character in the string is indexed in the previous table. Each character
540 * in the string is indexed in this table, and the value found there has the
541 * value form the first table subtracted. The result must be on the range
542 * [0,3].
543 *
544 * For example, the string "wzyx" will get X from the first table. Each of
545 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
546 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
547 *
548 * The string "wzrg" will get X from the first table. Each of the characters
549 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
550 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
551 * [0,3], the error is detected.
552 */
553 static const unsigned char idx_map[26] = {
554 /* a b c d e f g h i j k l m */
555 R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
556 /* n o p q r s t u v w x y z */
557 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
558 };
559
560 int swiz_idx[4] = { 0, 0, 0, 0 };
561 unsigned i;
562
563
564 /* Validate the first character in the swizzle string and look up the base
565 * index value as described above.
566 */
567 if ((str[0] < 'a') || (str[0] > 'z'))
568 return NULL;
569
570 const unsigned base = base_idx[str[0] - 'a'];
571
572
573 for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
574 /* Validate the next character, and, as described above, convert it to a
575 * swizzle index.
576 */
577 if ((str[i] < 'a') || (str[i] > 'z'))
578 return NULL;
579
580 swiz_idx[i] = idx_map[str[i] - 'a'] - base;
581 if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
582 return NULL;
583 }
584
585 if (str[i] != '\0')
586 return NULL;
587
588 return new ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
589 swiz_idx[3], i);
590 }
591
592 #undef X
593 #undef R
594 #undef S
595 #undef I
596
597 ir_variable *
598 ir_swizzle::variable_referenced()
599 {
600 return this->val->variable_referenced();
601 }
602
603 ir_variable::ir_variable(const struct glsl_type *type, const char *name)
604 : max_array_access(0), read_only(false), centroid(false), invariant(false),
605 mode(ir_var_auto), interpolation(ir_var_smooth)
606 {
607 this->type = type;
608 this->name = name;
609 this->constant_value = NULL;
610
611 if (type && type->base_type == GLSL_TYPE_SAMPLER)
612 this->read_only = true;
613 }
614
615
616 ir_function_signature::ir_function_signature(const glsl_type *return_type)
617 : return_type(return_type), is_defined(false)
618 {
619 /* empty */
620 }
621
622
623 const char *
624 ir_function_signature::qualifiers_match(exec_list *params)
625 {
626 exec_list_iterator iter_a = parameters.iterator();
627 exec_list_iterator iter_b = params->iterator();
628
629 /* check that the qualifiers match. */
630 while (iter_a.has_next()) {
631 ir_variable *a = (ir_variable *)iter_a.get();
632 ir_variable *b = (ir_variable *)iter_b.get();
633
634 if (a->read_only != b->read_only ||
635 a->mode != b->mode ||
636 a->interpolation != b->interpolation ||
637 a->centroid != b->centroid) {
638
639 /* parameter a's qualifiers don't match */
640 return a->name;
641 }
642
643 iter_a.next();
644 iter_b.next();
645 }
646 return NULL;
647 }
648
649
650 void
651 ir_function_signature::replace_parameters(exec_list *new_params)
652 {
653 /* Destroy all of the previous parameter information. If the previous
654 * parameter information comes from the function prototype, it may either
655 * specify incorrect parameter names or not have names at all.
656 */
657 foreach_iter(exec_list_iterator, iter, parameters) {
658 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
659
660 iter.remove();
661 delete (ir_instruction*) iter.get();
662 }
663
664 new_params->move_nodes_to(&parameters);
665 }
666
667
668 ir_function::ir_function(const char *name)
669 : name(name)
670 {
671 /* empty */
672 }
673
674
675 ir_call *
676 ir_call::get_error_instruction()
677 {
678 ir_call *call = new ir_call;
679
680 call->type = glsl_type::error_type;
681 return call;
682 }
683
684 void
685 visit_exec_list(exec_list *list, ir_visitor *visitor)
686 {
687 foreach_iter(exec_list_iterator, iter, *list) {
688 ((ir_instruction *)iter.get())->accept(visitor);
689 }
690 }
691