• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core.h" /* for MAX2 */
25 #include "ir.h"
26 #include "compiler/glsl_types.h"
27 #include "glsl_parser_extras.h"
28 
29 
ir_rvalue(enum ir_node_type t)30 ir_rvalue::ir_rvalue(enum ir_node_type t)
31    : ir_instruction(t)
32 {
33    this->type = glsl_type::error_type;
34 }
35 
is_zero() const36 bool ir_rvalue::is_zero() const
37 {
38    return false;
39 }
40 
is_one() const41 bool ir_rvalue::is_one() const
42 {
43    return false;
44 }
45 
is_negative_one() const46 bool ir_rvalue::is_negative_one() const
47 {
48    return false;
49 }
50 
51 /**
52  * Modify the swizzle make to move one component to another
53  *
54  * \param m    IR swizzle to be modified
55  * \param from Component in the RHS that is to be swizzled
56  * \param to   Desired swizzle location of \c from
57  */
58 static void
update_rhs_swizzle(ir_swizzle_mask & m,unsigned from,unsigned to)59 update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned to)
60 {
61    switch (to) {
62    case 0: m.x = from; break;
63    case 1: m.y = from; break;
64    case 2: m.z = from; break;
65    case 3: m.w = from; break;
66    default: assert(!"Should not get here.");
67    }
68 }
69 
70 void
set_lhs(ir_rvalue * lhs)71 ir_assignment::set_lhs(ir_rvalue *lhs)
72 {
73    void *mem_ctx = this;
74    bool swizzled = false;
75 
76    while (lhs != NULL) {
77       ir_swizzle *swiz = lhs->as_swizzle();
78 
79       if (swiz == NULL)
80 	 break;
81 
82       unsigned write_mask = 0;
83       ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
84 
85       for (unsigned i = 0; i < swiz->mask.num_components; i++) {
86 	 unsigned c = 0;
87 
88 	 switch (i) {
89 	 case 0: c = swiz->mask.x; break;
90 	 case 1: c = swiz->mask.y; break;
91 	 case 2: c = swiz->mask.z; break;
92 	 case 3: c = swiz->mask.w; break;
93 	 default: assert(!"Should not get here.");
94 	 }
95 
96 	 write_mask |= (((this->write_mask >> i) & 1) << c);
97 	 update_rhs_swizzle(rhs_swiz, i, c);
98          rhs_swiz.num_components = swiz->val->type->vector_elements;
99       }
100 
101       this->write_mask = write_mask;
102       lhs = swiz->val;
103 
104       this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
105       swizzled = true;
106    }
107 
108    if (swizzled) {
109       /* Now, RHS channels line up with the LHS writemask.  Collapse it
110        * to just the channels that will be written.
111        */
112       ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
113       int rhs_chan = 0;
114       for (int i = 0; i < 4; i++) {
115 	 if (write_mask & (1 << i))
116 	    update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
117       }
118       rhs_swiz.num_components = rhs_chan;
119       this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
120    }
121 
122    assert((lhs == NULL) || lhs->as_dereference());
123 
124    this->lhs = (ir_dereference *) lhs;
125 }
126 
127 ir_variable *
whole_variable_written()128 ir_assignment::whole_variable_written()
129 {
130    ir_variable *v = this->lhs->whole_variable_referenced();
131 
132    if (v == NULL)
133       return NULL;
134 
135    if (v->type->is_scalar())
136       return v;
137 
138    if (v->type->is_vector()) {
139       const unsigned mask = (1U << v->type->vector_elements) - 1;
140 
141       if (mask != this->write_mask)
142 	 return NULL;
143    }
144 
145    /* Either all the vector components are assigned or the variable is some
146     * composite type (and the whole thing is assigned.
147     */
148    return v;
149 }
150 
ir_assignment(ir_dereference * lhs,ir_rvalue * rhs,ir_rvalue * condition,unsigned write_mask)151 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
152 			     ir_rvalue *condition, unsigned write_mask)
153    : ir_instruction(ir_type_assignment)
154 {
155    this->condition = condition;
156    this->rhs = rhs;
157    this->lhs = lhs;
158    this->write_mask = write_mask;
159 
160    if (lhs->type->is_scalar() || lhs->type->is_vector()) {
161       int lhs_components = 0;
162       for (int i = 0; i < 4; i++) {
163 	 if (write_mask & (1 << i))
164 	    lhs_components++;
165       }
166 
167       assert(lhs_components == this->rhs->type->vector_elements);
168    }
169 }
170 
ir_assignment(ir_rvalue * lhs,ir_rvalue * rhs,ir_rvalue * condition)171 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
172 			     ir_rvalue *condition)
173    : ir_instruction(ir_type_assignment)
174 {
175    this->condition = condition;
176    this->rhs = rhs;
177 
178    /* If the RHS is a vector type, assume that all components of the vector
179     * type are being written to the LHS.  The write mask comes from the RHS
180     * because we can have a case where the LHS is a vec4 and the RHS is a
181     * vec3.  In that case, the assignment is:
182     *
183     *     (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
184     */
185    if (rhs->type->is_vector())
186       this->write_mask = (1U << rhs->type->vector_elements) - 1;
187    else if (rhs->type->is_scalar())
188       this->write_mask = 1;
189    else
190       this->write_mask = 0;
191 
192    this->set_lhs(lhs);
193 }
194 
ir_expression(int op,const struct glsl_type * type,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2,ir_rvalue * op3)195 ir_expression::ir_expression(int op, const struct glsl_type *type,
196 			     ir_rvalue *op0, ir_rvalue *op1,
197 			     ir_rvalue *op2, ir_rvalue *op3)
198    : ir_rvalue(ir_type_expression)
199 {
200    this->type = type;
201    this->operation = ir_expression_operation(op);
202    this->operands[0] = op0;
203    this->operands[1] = op1;
204    this->operands[2] = op2;
205    this->operands[3] = op3;
206    init_num_operands();
207 
208 #ifndef NDEBUG
209    for (unsigned i = num_operands; i < 4; i++) {
210       assert(this->operands[i] == NULL);
211    }
212 
213    for (unsigned i = 0; i < num_operands; i++) {
214       assert(this->operands[i] != NULL);
215    }
216 #endif
217 }
218 
ir_expression(int op,ir_rvalue * op0)219 ir_expression::ir_expression(int op, ir_rvalue *op0)
220    : ir_rvalue(ir_type_expression)
221 {
222    this->operation = ir_expression_operation(op);
223    this->operands[0] = op0;
224    this->operands[1] = NULL;
225    this->operands[2] = NULL;
226    this->operands[3] = NULL;
227 
228    assert(op <= ir_last_unop);
229    init_num_operands();
230    assert(num_operands == 1);
231    assert(this->operands[0]);
232 
233    switch (this->operation) {
234    case ir_unop_bit_not:
235    case ir_unop_logic_not:
236    case ir_unop_neg:
237    case ir_unop_abs:
238    case ir_unop_sign:
239    case ir_unop_rcp:
240    case ir_unop_rsq:
241    case ir_unop_sqrt:
242    case ir_unop_exp:
243    case ir_unop_log:
244    case ir_unop_exp2:
245    case ir_unop_log2:
246    case ir_unop_trunc:
247    case ir_unop_ceil:
248    case ir_unop_floor:
249    case ir_unop_fract:
250    case ir_unop_round_even:
251    case ir_unop_sin:
252    case ir_unop_cos:
253    case ir_unop_dFdx:
254    case ir_unop_dFdx_coarse:
255    case ir_unop_dFdx_fine:
256    case ir_unop_dFdy:
257    case ir_unop_dFdy_coarse:
258    case ir_unop_dFdy_fine:
259    case ir_unop_bitfield_reverse:
260    case ir_unop_interpolate_at_centroid:
261    case ir_unop_saturate:
262       this->type = op0->type;
263       break;
264 
265    case ir_unop_f2i:
266    case ir_unop_b2i:
267    case ir_unop_u2i:
268    case ir_unop_d2i:
269    case ir_unop_bitcast_f2i:
270    case ir_unop_bit_count:
271    case ir_unop_find_msb:
272    case ir_unop_find_lsb:
273    case ir_unop_subroutine_to_int:
274    case ir_unop_i642i:
275    case ir_unop_u642i:
276       this->type = glsl_type::get_instance(GLSL_TYPE_INT,
277 					   op0->type->vector_elements, 1);
278       break;
279 
280    case ir_unop_b2f:
281    case ir_unop_i2f:
282    case ir_unop_u2f:
283    case ir_unop_d2f:
284    case ir_unop_bitcast_i2f:
285    case ir_unop_bitcast_u2f:
286    case ir_unop_i642f:
287    case ir_unop_u642f:
288       this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
289 					   op0->type->vector_elements, 1);
290       break;
291 
292    case ir_unop_f2b:
293    case ir_unop_i2b:
294    case ir_unop_d2b:
295    case ir_unop_i642b:
296       this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
297 					   op0->type->vector_elements, 1);
298       break;
299 
300    case ir_unop_f2d:
301    case ir_unop_i2d:
302    case ir_unop_u2d:
303    case ir_unop_i642d:
304    case ir_unop_u642d:
305       this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
306 					   op0->type->vector_elements, 1);
307       break;
308 
309    case ir_unop_i2u:
310    case ir_unop_f2u:
311    case ir_unop_d2u:
312    case ir_unop_bitcast_f2u:
313    case ir_unop_i642u:
314    case ir_unop_u642u:
315       this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
316 					   op0->type->vector_elements, 1);
317       break;
318 
319    case ir_unop_i2i64:
320    case ir_unop_u2i64:
321    case ir_unop_b2i64:
322    case ir_unop_f2i64:
323    case ir_unop_d2i64:
324    case ir_unop_u642i64:
325       this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
326 					   op0->type->vector_elements, 1);
327       break;
328 
329    case ir_unop_i2u64:
330    case ir_unop_u2u64:
331    case ir_unop_f2u64:
332    case ir_unop_d2u64:
333    case ir_unop_i642u64:
334       this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
335 					   op0->type->vector_elements, 1);
336       break;
337    case ir_unop_noise:
338       this->type = glsl_type::float_type;
339       break;
340 
341    case ir_unop_unpack_double_2x32:
342    case ir_unop_unpack_uint_2x32:
343       this->type = glsl_type::uvec2_type;
344       break;
345 
346    case ir_unop_unpack_int_2x32:
347       this->type = glsl_type::ivec2_type;
348       break;
349 
350    case ir_unop_pack_snorm_2x16:
351    case ir_unop_pack_snorm_4x8:
352    case ir_unop_pack_unorm_2x16:
353    case ir_unop_pack_unorm_4x8:
354    case ir_unop_pack_half_2x16:
355       this->type = glsl_type::uint_type;
356       break;
357 
358    case ir_unop_pack_double_2x32:
359       this->type = glsl_type::double_type;
360       break;
361 
362    case ir_unop_pack_int_2x32:
363       this->type = glsl_type::int64_t_type;
364       break;
365 
366    case ir_unop_pack_uint_2x32:
367       this->type = glsl_type::uint64_t_type;
368       break;
369 
370    case ir_unop_unpack_snorm_2x16:
371    case ir_unop_unpack_unorm_2x16:
372    case ir_unop_unpack_half_2x16:
373       this->type = glsl_type::vec2_type;
374       break;
375 
376    case ir_unop_unpack_snorm_4x8:
377    case ir_unop_unpack_unorm_4x8:
378       this->type = glsl_type::vec4_type;
379       break;
380 
381    case ir_unop_unpack_sampler_2x32:
382    case ir_unop_unpack_image_2x32:
383       this->type = glsl_type::uvec2_type;
384       break;
385 
386    case ir_unop_pack_sampler_2x32:
387    case ir_unop_pack_image_2x32:
388       this->type = op0->type;
389       break;
390 
391    case ir_unop_frexp_sig:
392       this->type = op0->type;
393       break;
394    case ir_unop_frexp_exp:
395       this->type = glsl_type::get_instance(GLSL_TYPE_INT,
396 					   op0->type->vector_elements, 1);
397       break;
398 
399    case ir_unop_get_buffer_size:
400    case ir_unop_ssbo_unsized_array_length:
401       this->type = glsl_type::int_type;
402       break;
403 
404    case ir_unop_bitcast_i642d:
405    case ir_unop_bitcast_u642d:
406       this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
407                                            op0->type->vector_elements, 1);
408       break;
409 
410    case ir_unop_bitcast_d2i64:
411       this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
412                                            op0->type->vector_elements, 1);
413       break;
414    case ir_unop_bitcast_d2u64:
415       this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
416                                            op0->type->vector_elements, 1);
417       break;
418 
419    default:
420       assert(!"not reached: missing automatic type setup for ir_expression");
421       this->type = op0->type;
422       break;
423    }
424 }
425 
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1)426 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
427    : ir_rvalue(ir_type_expression)
428 {
429    this->operation = ir_expression_operation(op);
430    this->operands[0] = op0;
431    this->operands[1] = op1;
432    this->operands[2] = NULL;
433    this->operands[3] = NULL;
434 
435    assert(op > ir_last_unop);
436    init_num_operands();
437    assert(num_operands == 2);
438    for (unsigned i = 0; i < num_operands; i++) {
439       assert(this->operands[i] != NULL);
440    }
441 
442    switch (this->operation) {
443    case ir_binop_all_equal:
444    case ir_binop_any_nequal:
445       this->type = glsl_type::bool_type;
446       break;
447 
448    case ir_binop_add:
449    case ir_binop_sub:
450    case ir_binop_min:
451    case ir_binop_max:
452    case ir_binop_pow:
453    case ir_binop_mul:
454    case ir_binop_div:
455    case ir_binop_mod:
456       if (op0->type->is_scalar()) {
457 	 this->type = op1->type;
458       } else if (op1->type->is_scalar()) {
459 	 this->type = op0->type;
460       } else {
461          if (this->operation == ir_binop_mul) {
462             this->type = glsl_type::get_mul_type(op0->type, op1->type);
463          } else {
464             assert(op0->type == op1->type);
465             this->type = op0->type;
466          }
467       }
468       break;
469 
470    case ir_binop_logic_and:
471    case ir_binop_logic_xor:
472    case ir_binop_logic_or:
473    case ir_binop_bit_and:
474    case ir_binop_bit_xor:
475    case ir_binop_bit_or:
476        assert(!op0->type->is_matrix());
477        assert(!op1->type->is_matrix());
478       if (op0->type->is_scalar()) {
479          this->type = op1->type;
480       } else if (op1->type->is_scalar()) {
481          this->type = op0->type;
482       } else {
483           assert(op0->type->vector_elements == op1->type->vector_elements);
484           this->type = op0->type;
485       }
486       break;
487 
488    case ir_binop_equal:
489    case ir_binop_nequal:
490    case ir_binop_gequal:
491    case ir_binop_less:
492       assert(op0->type == op1->type);
493       this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
494 					   op0->type->vector_elements, 1);
495       break;
496 
497    case ir_binop_dot:
498       this->type = op0->type->get_base_type();
499       break;
500 
501    case ir_binop_imul_high:
502    case ir_binop_carry:
503    case ir_binop_borrow:
504    case ir_binop_lshift:
505    case ir_binop_rshift:
506    case ir_binop_ldexp:
507    case ir_binop_interpolate_at_offset:
508    case ir_binop_interpolate_at_sample:
509       this->type = op0->type;
510       break;
511 
512    case ir_binop_vector_extract:
513       this->type = op0->type->get_scalar_type();
514       break;
515 
516    default:
517       assert(!"not reached: missing automatic type setup for ir_expression");
518       this->type = glsl_type::float_type;
519    }
520 }
521 
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2)522 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
523                              ir_rvalue *op2)
524    : ir_rvalue(ir_type_expression)
525 {
526    this->operation = ir_expression_operation(op);
527    this->operands[0] = op0;
528    this->operands[1] = op1;
529    this->operands[2] = op2;
530    this->operands[3] = NULL;
531 
532    assert(op > ir_last_binop && op <= ir_last_triop);
533    init_num_operands();
534    assert(num_operands == 3);
535    for (unsigned i = 0; i < num_operands; i++) {
536       assert(this->operands[i] != NULL);
537    }
538 
539    switch (this->operation) {
540    case ir_triop_fma:
541    case ir_triop_lrp:
542    case ir_triop_bitfield_extract:
543    case ir_triop_vector_insert:
544       this->type = op0->type;
545       break;
546 
547    case ir_triop_csel:
548       this->type = op1->type;
549       break;
550 
551    default:
552       assert(!"not reached: missing automatic type setup for ir_expression");
553       this->type = glsl_type::float_type;
554    }
555 }
556 
557 /**
558  * This is only here for ir_reader to used for testing purposes. Please use
559  * the precomputed num_operands field if you need the number of operands.
560  */
561 unsigned
get_num_operands(ir_expression_operation op)562 ir_expression::get_num_operands(ir_expression_operation op)
563 {
564    assert(op <= ir_last_opcode);
565 
566    if (op <= ir_last_unop)
567       return 1;
568 
569    if (op <= ir_last_binop)
570       return 2;
571 
572    if (op <= ir_last_triop)
573       return 3;
574 
575    if (op <= ir_last_quadop)
576       return 4;
577 
578    unreachable("Could not calculate number of operands");
579 }
580 
581 #include "ir_expression_operation_strings.h"
582 
583 const char*
depth_layout_string(ir_depth_layout layout)584 depth_layout_string(ir_depth_layout layout)
585 {
586    switch(layout) {
587    case ir_depth_layout_none:      return "";
588    case ir_depth_layout_any:       return "depth_any";
589    case ir_depth_layout_greater:   return "depth_greater";
590    case ir_depth_layout_less:      return "depth_less";
591    case ir_depth_layout_unchanged: return "depth_unchanged";
592 
593    default:
594       assert(0);
595       return "";
596    }
597 }
598 
599 ir_expression_operation
get_operator(const char * str)600 ir_expression::get_operator(const char *str)
601 {
602    for (int op = 0; op <= int(ir_last_opcode); op++) {
603       if (strcmp(str, ir_expression_operation_strings[op]) == 0)
604 	 return (ir_expression_operation) op;
605    }
606    return (ir_expression_operation) -1;
607 }
608 
609 ir_variable *
variable_referenced() const610 ir_expression::variable_referenced() const
611 {
612    switch (operation) {
613       case ir_binop_vector_extract:
614       case ir_triop_vector_insert:
615          /* We get these for things like a[0] where a is a vector type. In these
616           * cases we want variable_referenced() to return the actual vector
617           * variable this is wrapping.
618           */
619          return operands[0]->variable_referenced();
620       default:
621          return ir_rvalue::variable_referenced();
622    }
623 }
624 
ir_constant()625 ir_constant::ir_constant()
626    : ir_rvalue(ir_type_constant)
627 {
628    this->const_elements = NULL;
629 }
630 
ir_constant(const struct glsl_type * type,const ir_constant_data * data)631 ir_constant::ir_constant(const struct glsl_type *type,
632 			 const ir_constant_data *data)
633    : ir_rvalue(ir_type_constant)
634 {
635    this->const_elements = NULL;
636 
637    assert((type->base_type >= GLSL_TYPE_UINT)
638 	  && (type->base_type <= GLSL_TYPE_IMAGE));
639 
640    this->type = type;
641    memcpy(& this->value, data, sizeof(this->value));
642 }
643 
ir_constant(float f,unsigned vector_elements)644 ir_constant::ir_constant(float f, unsigned vector_elements)
645    : ir_rvalue(ir_type_constant)
646 {
647    assert(vector_elements <= 4);
648    this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT, vector_elements, 1);
649    for (unsigned i = 0; i < vector_elements; i++) {
650       this->value.f[i] = f;
651    }
652    for (unsigned i = vector_elements; i < 16; i++)  {
653       this->value.f[i] = 0;
654    }
655 }
656 
ir_constant(double d,unsigned vector_elements)657 ir_constant::ir_constant(double d, unsigned vector_elements)
658    : ir_rvalue(ir_type_constant)
659 {
660    assert(vector_elements <= 4);
661    this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE, vector_elements, 1);
662    for (unsigned i = 0; i < vector_elements; i++) {
663       this->value.d[i] = d;
664    }
665    for (unsigned i = vector_elements; i < 16; i++)  {
666       this->value.d[i] = 0.0;
667    }
668 }
669 
ir_constant(unsigned int u,unsigned vector_elements)670 ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
671    : ir_rvalue(ir_type_constant)
672 {
673    assert(vector_elements <= 4);
674    this->type = glsl_type::get_instance(GLSL_TYPE_UINT, vector_elements, 1);
675    for (unsigned i = 0; i < vector_elements; i++) {
676       this->value.u[i] = u;
677    }
678    for (unsigned i = vector_elements; i < 16; i++) {
679       this->value.u[i] = 0;
680    }
681 }
682 
ir_constant(int integer,unsigned vector_elements)683 ir_constant::ir_constant(int integer, unsigned vector_elements)
684    : ir_rvalue(ir_type_constant)
685 {
686    assert(vector_elements <= 4);
687    this->type = glsl_type::get_instance(GLSL_TYPE_INT, vector_elements, 1);
688    for (unsigned i = 0; i < vector_elements; i++) {
689       this->value.i[i] = integer;
690    }
691    for (unsigned i = vector_elements; i < 16; i++) {
692       this->value.i[i] = 0;
693    }
694 }
695 
ir_constant(uint64_t u64,unsigned vector_elements)696 ir_constant::ir_constant(uint64_t u64, unsigned vector_elements)
697    : ir_rvalue(ir_type_constant)
698 {
699    assert(vector_elements <= 4);
700    this->type = glsl_type::get_instance(GLSL_TYPE_UINT64, vector_elements, 1);
701    for (unsigned i = 0; i < vector_elements; i++) {
702       this->value.u64[i] = u64;
703    }
704    for (unsigned i = vector_elements; i < 16; i++) {
705       this->value.u64[i] = 0;
706    }
707 }
708 
ir_constant(int64_t int64,unsigned vector_elements)709 ir_constant::ir_constant(int64_t int64, unsigned vector_elements)
710    : ir_rvalue(ir_type_constant)
711 {
712    assert(vector_elements <= 4);
713    this->type = glsl_type::get_instance(GLSL_TYPE_INT64, vector_elements, 1);
714    for (unsigned i = 0; i < vector_elements; i++) {
715       this->value.i64[i] = int64;
716    }
717    for (unsigned i = vector_elements; i < 16; i++) {
718       this->value.i64[i] = 0;
719    }
720 }
721 
ir_constant(bool b,unsigned vector_elements)722 ir_constant::ir_constant(bool b, unsigned vector_elements)
723    : ir_rvalue(ir_type_constant)
724 {
725    assert(vector_elements <= 4);
726    this->type = glsl_type::get_instance(GLSL_TYPE_BOOL, vector_elements, 1);
727    for (unsigned i = 0; i < vector_elements; i++) {
728       this->value.b[i] = b;
729    }
730    for (unsigned i = vector_elements; i < 16; i++) {
731       this->value.b[i] = false;
732    }
733 }
734 
ir_constant(const ir_constant * c,unsigned i)735 ir_constant::ir_constant(const ir_constant *c, unsigned i)
736    : ir_rvalue(ir_type_constant)
737 {
738    this->const_elements = NULL;
739    this->type = c->type->get_base_type();
740 
741    switch (this->type->base_type) {
742    case GLSL_TYPE_UINT:  this->value.u[0] = c->value.u[i]; break;
743    case GLSL_TYPE_INT:   this->value.i[0] = c->value.i[i]; break;
744    case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
745    case GLSL_TYPE_BOOL:  this->value.b[0] = c->value.b[i]; break;
746    case GLSL_TYPE_DOUBLE: this->value.d[0] = c->value.d[i]; break;
747    default:              assert(!"Should not get here."); break;
748    }
749 }
750 
ir_constant(const struct glsl_type * type,exec_list * value_list)751 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
752    : ir_rvalue(ir_type_constant)
753 {
754    this->const_elements = NULL;
755    this->type = type;
756 
757    assert(type->is_scalar() || type->is_vector() || type->is_matrix()
758 	  || type->is_record() || type->is_array());
759 
760    /* If the constant is a record, the types of each of the entries in
761     * value_list must be a 1-for-1 match with the structure components.  Each
762     * entry must also be a constant.  Just move the nodes from the value_list
763     * to the list in the ir_constant.
764     */
765    if (type->is_array() || type->is_record()) {
766       this->const_elements = ralloc_array(this, ir_constant *, type->length);
767       unsigned i = 0;
768       foreach_in_list(ir_constant, value, value_list) {
769 	 assert(value->as_constant() != NULL);
770 
771 	 this->const_elements[i++] = value;
772       }
773       return;
774    }
775 
776    for (unsigned i = 0; i < 16; i++) {
777       this->value.u[i] = 0;
778    }
779 
780    ir_constant *value = (ir_constant *) (value_list->get_head_raw());
781 
782    /* Constructors with exactly one scalar argument are special for vectors
783     * and matrices.  For vectors, the scalar value is replicated to fill all
784     * the components.  For matrices, the scalar fills the components of the
785     * diagonal while the rest is filled with 0.
786     */
787    if (value->type->is_scalar() && value->next->is_tail_sentinel()) {
788       if (type->is_matrix()) {
789 	 /* Matrix - fill diagonal (rest is already set to 0) */
790          assert(type->is_float() || type->is_double());
791          for (unsigned i = 0; i < type->matrix_columns; i++) {
792             if (type->is_float())
793                this->value.f[i * type->vector_elements + i] =
794                   value->value.f[0];
795             else
796                this->value.d[i * type->vector_elements + i] =
797                   value->value.d[0];
798          }
799       } else {
800 	 /* Vector or scalar - fill all components */
801 	 switch (type->base_type) {
802 	 case GLSL_TYPE_UINT:
803 	 case GLSL_TYPE_INT:
804 	    for (unsigned i = 0; i < type->components(); i++)
805 	       this->value.u[i] = value->value.u[0];
806 	    break;
807 	 case GLSL_TYPE_FLOAT:
808 	    for (unsigned i = 0; i < type->components(); i++)
809 	       this->value.f[i] = value->value.f[0];
810 	    break;
811 	 case GLSL_TYPE_DOUBLE:
812 	    for (unsigned i = 0; i < type->components(); i++)
813 	       this->value.d[i] = value->value.d[0];
814 	    break;
815 	 case GLSL_TYPE_UINT64:
816 	 case GLSL_TYPE_INT64:
817 	    for (unsigned i = 0; i < type->components(); i++)
818 	       this->value.u64[i] = value->value.u64[0];
819 	    break;
820 	 case GLSL_TYPE_BOOL:
821 	    for (unsigned i = 0; i < type->components(); i++)
822 	       this->value.b[i] = value->value.b[0];
823 	    break;
824 	 default:
825 	    assert(!"Should not get here.");
826 	    break;
827 	 }
828       }
829       return;
830    }
831 
832    if (type->is_matrix() && value->type->is_matrix()) {
833       assert(value->next->is_tail_sentinel());
834 
835       /* From section 5.4.2 of the GLSL 1.20 spec:
836        * "If a matrix is constructed from a matrix, then each component
837        *  (column i, row j) in the result that has a corresponding component
838        *  (column i, row j) in the argument will be initialized from there."
839        */
840       unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
841       unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
842       for (unsigned i = 0; i < cols; i++) {
843 	 for (unsigned j = 0; j < rows; j++) {
844 	    const unsigned src = i * value->type->vector_elements + j;
845 	    const unsigned dst = i * type->vector_elements + j;
846 	    this->value.f[dst] = value->value.f[src];
847 	 }
848       }
849 
850       /* "All other components will be initialized to the identity matrix." */
851       for (unsigned i = cols; i < type->matrix_columns; i++)
852 	 this->value.f[i * type->vector_elements + i] = 1.0;
853 
854       return;
855    }
856 
857    /* Use each component from each entry in the value_list to initialize one
858     * component of the constant being constructed.
859     */
860    unsigned i = 0;
861    for (;;) {
862       assert(value->as_constant() != NULL);
863       assert(!value->is_tail_sentinel());
864 
865       for (unsigned j = 0; j < value->type->components(); j++) {
866 	 switch (type->base_type) {
867 	 case GLSL_TYPE_UINT:
868 	    this->value.u[i] = value->get_uint_component(j);
869 	    break;
870 	 case GLSL_TYPE_INT:
871 	    this->value.i[i] = value->get_int_component(j);
872 	    break;
873 	 case GLSL_TYPE_FLOAT:
874 	    this->value.f[i] = value->get_float_component(j);
875 	    break;
876 	 case GLSL_TYPE_BOOL:
877 	    this->value.b[i] = value->get_bool_component(j);
878 	    break;
879 	 case GLSL_TYPE_DOUBLE:
880 	    this->value.d[i] = value->get_double_component(j);
881 	    break;
882          case GLSL_TYPE_UINT64:
883 	    this->value.u64[i] = value->get_uint64_component(j);
884 	    break;
885 	 case GLSL_TYPE_INT64:
886 	    this->value.i64[i] = value->get_int64_component(j);
887 	    break;
888 	 default:
889 	    /* FINISHME: What to do?  Exceptions are not the answer.
890 	     */
891 	    break;
892 	 }
893 
894 	 i++;
895 	 if (i >= type->components())
896 	    break;
897       }
898 
899       if (i >= type->components())
900 	 break; /* avoid downcasting a list sentinel */
901       value = (ir_constant *) value->next;
902    }
903 }
904 
905 ir_constant *
zero(void * mem_ctx,const glsl_type * type)906 ir_constant::zero(void *mem_ctx, const glsl_type *type)
907 {
908    assert(type->is_scalar() || type->is_vector() || type->is_matrix()
909 	  || type->is_record() || type->is_array());
910 
911    ir_constant *c = new(mem_ctx) ir_constant;
912    c->type = type;
913    memset(&c->value, 0, sizeof(c->value));
914 
915    if (type->is_array()) {
916       c->const_elements = ralloc_array(c, ir_constant *, type->length);
917 
918       for (unsigned i = 0; i < type->length; i++)
919 	 c->const_elements[i] = ir_constant::zero(c, type->fields.array);
920    }
921 
922    if (type->is_record()) {
923       c->const_elements = ralloc_array(c, ir_constant *, type->length);
924 
925       for (unsigned i = 0; i < type->length; i++) {
926          c->const_elements[i] =
927             ir_constant::zero(mem_ctx, type->fields.structure[i].type);
928       }
929    }
930 
931    return c;
932 }
933 
934 bool
get_bool_component(unsigned i) const935 ir_constant::get_bool_component(unsigned i) const
936 {
937    switch (this->type->base_type) {
938    case GLSL_TYPE_UINT:  return this->value.u[i] != 0;
939    case GLSL_TYPE_INT:   return this->value.i[i] != 0;
940    case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
941    case GLSL_TYPE_BOOL:  return this->value.b[i];
942    case GLSL_TYPE_DOUBLE: return this->value.d[i] != 0.0;
943    case GLSL_TYPE_UINT64: return this->value.u64[i] != 0;
944    case GLSL_TYPE_INT64:  return this->value.i64[i] != 0;
945    default:              assert(!"Should not get here."); break;
946    }
947 
948    /* Must return something to make the compiler happy.  This is clearly an
949     * error case.
950     */
951    return false;
952 }
953 
954 float
get_float_component(unsigned i) const955 ir_constant::get_float_component(unsigned i) const
956 {
957    switch (this->type->base_type) {
958    case GLSL_TYPE_UINT:  return (float) this->value.u[i];
959    case GLSL_TYPE_INT:   return (float) this->value.i[i];
960    case GLSL_TYPE_FLOAT: return this->value.f[i];
961    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1.0f : 0.0f;
962    case GLSL_TYPE_DOUBLE: return (float) this->value.d[i];
963    case GLSL_TYPE_UINT64: return (float) this->value.u64[i];
964    case GLSL_TYPE_INT64:  return (float) this->value.i64[i];
965    default:              assert(!"Should not get here."); break;
966    }
967 
968    /* Must return something to make the compiler happy.  This is clearly an
969     * error case.
970     */
971    return 0.0;
972 }
973 
974 double
get_double_component(unsigned i) const975 ir_constant::get_double_component(unsigned i) const
976 {
977    switch (this->type->base_type) {
978    case GLSL_TYPE_UINT:  return (double) this->value.u[i];
979    case GLSL_TYPE_INT:   return (double) this->value.i[i];
980    case GLSL_TYPE_FLOAT: return (double) this->value.f[i];
981    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1.0 : 0.0;
982    case GLSL_TYPE_DOUBLE: return this->value.d[i];
983    case GLSL_TYPE_UINT64: return (double) this->value.u64[i];
984    case GLSL_TYPE_INT64:  return (double) this->value.i64[i];
985    default:              assert(!"Should not get here."); break;
986    }
987 
988    /* Must return something to make the compiler happy.  This is clearly an
989     * error case.
990     */
991    return 0.0;
992 }
993 
994 int
get_int_component(unsigned i) const995 ir_constant::get_int_component(unsigned i) const
996 {
997    switch (this->type->base_type) {
998    case GLSL_TYPE_UINT:  return this->value.u[i];
999    case GLSL_TYPE_INT:   return this->value.i[i];
1000    case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
1001    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
1002    case GLSL_TYPE_DOUBLE: return (int) this->value.d[i];
1003    case GLSL_TYPE_UINT64: return (int) this->value.u64[i];
1004    case GLSL_TYPE_INT64:  return (int) this->value.i64[i];
1005    default:              assert(!"Should not get here."); break;
1006    }
1007 
1008    /* Must return something to make the compiler happy.  This is clearly an
1009     * error case.
1010     */
1011    return 0;
1012 }
1013 
1014 unsigned
get_uint_component(unsigned i) const1015 ir_constant::get_uint_component(unsigned i) const
1016 {
1017    switch (this->type->base_type) {
1018    case GLSL_TYPE_UINT:  return this->value.u[i];
1019    case GLSL_TYPE_INT:   return this->value.i[i];
1020    case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
1021    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
1022    case GLSL_TYPE_DOUBLE: return (unsigned) this->value.d[i];
1023    case GLSL_TYPE_UINT64: return (unsigned) this->value.u64[i];
1024    case GLSL_TYPE_INT64:  return (unsigned) this->value.i64[i];
1025    default:              assert(!"Should not get here."); break;
1026    }
1027 
1028    /* Must return something to make the compiler happy.  This is clearly an
1029     * error case.
1030     */
1031    return 0;
1032 }
1033 
1034 int64_t
get_int64_component(unsigned i) const1035 ir_constant::get_int64_component(unsigned i) const
1036 {
1037    switch (this->type->base_type) {
1038    case GLSL_TYPE_UINT:  return this->value.u[i];
1039    case GLSL_TYPE_INT:   return this->value.i[i];
1040    case GLSL_TYPE_FLOAT: return (int64_t) this->value.f[i];
1041    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
1042    case GLSL_TYPE_DOUBLE: return (int64_t) this->value.d[i];
1043    case GLSL_TYPE_UINT64: return (int64_t) this->value.u64[i];
1044    case GLSL_TYPE_INT64:  return this->value.i64[i];
1045    default:              assert(!"Should not get here."); break;
1046    }
1047 
1048    /* Must return something to make the compiler happy.  This is clearly an
1049     * error case.
1050     */
1051    return 0;
1052 }
1053 
1054 uint64_t
get_uint64_component(unsigned i) const1055 ir_constant::get_uint64_component(unsigned i) const
1056 {
1057    switch (this->type->base_type) {
1058    case GLSL_TYPE_UINT:  return this->value.u[i];
1059    case GLSL_TYPE_INT:   return this->value.i[i];
1060    case GLSL_TYPE_FLOAT: return (uint64_t) this->value.f[i];
1061    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
1062    case GLSL_TYPE_DOUBLE: return (uint64_t) this->value.d[i];
1063    case GLSL_TYPE_UINT64: return this->value.u64[i];
1064    case GLSL_TYPE_INT64:  return (uint64_t) this->value.i64[i];
1065    default:              assert(!"Should not get here."); break;
1066    }
1067 
1068    /* Must return something to make the compiler happy.  This is clearly an
1069     * error case.
1070     */
1071    return 0;
1072 }
1073 
1074 ir_constant *
get_array_element(unsigned i) const1075 ir_constant::get_array_element(unsigned i) const
1076 {
1077    assert(this->type->is_array());
1078 
1079    /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
1080     *
1081     *     "Behavior is undefined if a shader subscripts an array with an index
1082     *     less than 0 or greater than or equal to the size the array was
1083     *     declared with."
1084     *
1085     * Most out-of-bounds accesses are removed before things could get this far.
1086     * There are cases where non-constant array index values can get constant
1087     * folded.
1088     */
1089    if (int(i) < 0)
1090       i = 0;
1091    else if (i >= this->type->length)
1092       i = this->type->length - 1;
1093 
1094    return const_elements[i];
1095 }
1096 
1097 ir_constant *
get_record_field(int idx)1098 ir_constant::get_record_field(int idx)
1099 {
1100    assert(this->type->is_record());
1101    assert(idx >= 0 && (unsigned) idx < this->type->length);
1102 
1103    return const_elements[idx];
1104 }
1105 
1106 void
copy_offset(ir_constant * src,int offset)1107 ir_constant::copy_offset(ir_constant *src, int offset)
1108 {
1109    switch (this->type->base_type) {
1110    case GLSL_TYPE_UINT:
1111    case GLSL_TYPE_INT:
1112    case GLSL_TYPE_FLOAT:
1113    case GLSL_TYPE_DOUBLE:
1114    case GLSL_TYPE_UINT64:
1115    case GLSL_TYPE_INT64:
1116    case GLSL_TYPE_BOOL: {
1117       unsigned int size = src->type->components();
1118       assert (size <= this->type->components() - offset);
1119       for (unsigned int i=0; i<size; i++) {
1120 	 switch (this->type->base_type) {
1121 	 case GLSL_TYPE_UINT:
1122 	    value.u[i+offset] = src->get_uint_component(i);
1123 	    break;
1124 	 case GLSL_TYPE_INT:
1125 	    value.i[i+offset] = src->get_int_component(i);
1126 	    break;
1127 	 case GLSL_TYPE_FLOAT:
1128 	    value.f[i+offset] = src->get_float_component(i);
1129 	    break;
1130 	 case GLSL_TYPE_BOOL:
1131 	    value.b[i+offset] = src->get_bool_component(i);
1132 	    break;
1133 	 case GLSL_TYPE_DOUBLE:
1134 	    value.d[i+offset] = src->get_double_component(i);
1135 	    break;
1136          case GLSL_TYPE_UINT64:
1137 	    value.u64[i+offset] = src->get_uint64_component(i);
1138 	    break;
1139 	 case GLSL_TYPE_INT64:
1140 	    value.i64[i+offset] = src->get_int64_component(i);
1141 	    break;
1142 	 default: // Shut up the compiler
1143 	    break;
1144 	 }
1145       }
1146       break;
1147    }
1148 
1149    case GLSL_TYPE_STRUCT:
1150    case GLSL_TYPE_ARRAY: {
1151       assert (src->type == this->type);
1152       for (unsigned i = 0; i < this->type->length; i++) {
1153 	 this->const_elements[i] = src->const_elements[i]->clone(this, NULL);
1154       }
1155       break;
1156    }
1157 
1158    default:
1159       assert(!"Should not get here.");
1160       break;
1161    }
1162 }
1163 
1164 void
copy_masked_offset(ir_constant * src,int offset,unsigned int mask)1165 ir_constant::copy_masked_offset(ir_constant *src, int offset, unsigned int mask)
1166 {
1167    assert (!type->is_array() && !type->is_record());
1168 
1169    if (!type->is_vector() && !type->is_matrix()) {
1170       offset = 0;
1171       mask = 1;
1172    }
1173 
1174    int id = 0;
1175    for (int i=0; i<4; i++) {
1176       if (mask & (1 << i)) {
1177 	 switch (this->type->base_type) {
1178 	 case GLSL_TYPE_UINT:
1179 	    value.u[i+offset] = src->get_uint_component(id++);
1180 	    break;
1181 	 case GLSL_TYPE_INT:
1182 	    value.i[i+offset] = src->get_int_component(id++);
1183 	    break;
1184 	 case GLSL_TYPE_FLOAT:
1185 	    value.f[i+offset] = src->get_float_component(id++);
1186 	    break;
1187 	 case GLSL_TYPE_BOOL:
1188 	    value.b[i+offset] = src->get_bool_component(id++);
1189 	    break;
1190 	 case GLSL_TYPE_DOUBLE:
1191 	    value.d[i+offset] = src->get_double_component(id++);
1192 	    break;
1193          case GLSL_TYPE_UINT64:
1194 	    value.u64[i+offset] = src->get_uint64_component(id++);
1195 	    break;
1196 	 case GLSL_TYPE_INT64:
1197 	    value.i64[i+offset] = src->get_int64_component(id++);
1198 	    break;
1199 	 default:
1200 	    assert(!"Should not get here.");
1201 	    return;
1202 	 }
1203       }
1204    }
1205 }
1206 
1207 bool
has_value(const ir_constant * c) const1208 ir_constant::has_value(const ir_constant *c) const
1209 {
1210    if (this->type != c->type)
1211       return false;
1212 
1213    if (this->type->is_array() || this->type->is_record()) {
1214       for (unsigned i = 0; i < this->type->length; i++) {
1215 	 if (!this->const_elements[i]->has_value(c->const_elements[i]))
1216 	    return false;
1217       }
1218       return true;
1219    }
1220 
1221    for (unsigned i = 0; i < this->type->components(); i++) {
1222       switch (this->type->base_type) {
1223       case GLSL_TYPE_UINT:
1224 	 if (this->value.u[i] != c->value.u[i])
1225 	    return false;
1226 	 break;
1227       case GLSL_TYPE_INT:
1228 	 if (this->value.i[i] != c->value.i[i])
1229 	    return false;
1230 	 break;
1231       case GLSL_TYPE_FLOAT:
1232 	 if (this->value.f[i] != c->value.f[i])
1233 	    return false;
1234 	 break;
1235       case GLSL_TYPE_BOOL:
1236 	 if (this->value.b[i] != c->value.b[i])
1237 	    return false;
1238 	 break;
1239       case GLSL_TYPE_DOUBLE:
1240 	 if (this->value.d[i] != c->value.d[i])
1241 	    return false;
1242 	 break;
1243       case GLSL_TYPE_UINT64:
1244 	 if (this->value.u64[i] != c->value.u64[i])
1245 	    return false;
1246 	 break;
1247       case GLSL_TYPE_INT64:
1248 	 if (this->value.i64[i] != c->value.i64[i])
1249 	    return false;
1250 	 break;
1251       default:
1252 	 assert(!"Should not get here.");
1253 	 return false;
1254       }
1255    }
1256 
1257    return true;
1258 }
1259 
1260 bool
is_value(float f,int i) const1261 ir_constant::is_value(float f, int i) const
1262 {
1263    if (!this->type->is_scalar() && !this->type->is_vector())
1264       return false;
1265 
1266    /* Only accept boolean values for 0/1. */
1267    if (int(bool(i)) != i && this->type->is_boolean())
1268       return false;
1269 
1270    for (unsigned c = 0; c < this->type->vector_elements; c++) {
1271       switch (this->type->base_type) {
1272       case GLSL_TYPE_FLOAT:
1273 	 if (this->value.f[c] != f)
1274 	    return false;
1275 	 break;
1276       case GLSL_TYPE_INT:
1277 	 if (this->value.i[c] != i)
1278 	    return false;
1279 	 break;
1280       case GLSL_TYPE_UINT:
1281 	 if (this->value.u[c] != unsigned(i))
1282 	    return false;
1283 	 break;
1284       case GLSL_TYPE_BOOL:
1285 	 if (this->value.b[c] != bool(i))
1286 	    return false;
1287 	 break;
1288       case GLSL_TYPE_DOUBLE:
1289 	 if (this->value.d[c] != double(f))
1290 	    return false;
1291 	 break;
1292       case GLSL_TYPE_UINT64:
1293 	 if (this->value.u64[c] != uint64_t(i))
1294 	    return false;
1295 	 break;
1296       case GLSL_TYPE_INT64:
1297 	 if (this->value.i64[c] != i)
1298 	    return false;
1299 	 break;
1300       default:
1301 	 /* The only other base types are structures, arrays, and samplers.
1302 	  * Samplers cannot be constants, and the others should have been
1303 	  * filtered out above.
1304 	  */
1305 	 assert(!"Should not get here.");
1306 	 return false;
1307       }
1308    }
1309 
1310    return true;
1311 }
1312 
1313 bool
is_zero() const1314 ir_constant::is_zero() const
1315 {
1316    return is_value(0.0, 0);
1317 }
1318 
1319 bool
is_one() const1320 ir_constant::is_one() const
1321 {
1322    return is_value(1.0, 1);
1323 }
1324 
1325 bool
is_negative_one() const1326 ir_constant::is_negative_one() const
1327 {
1328    return is_value(-1.0, -1);
1329 }
1330 
1331 bool
is_uint16_constant() const1332 ir_constant::is_uint16_constant() const
1333 {
1334    if (!type->is_integer())
1335       return false;
1336 
1337    return value.u[0] < (1 << 16);
1338 }
1339 
ir_loop()1340 ir_loop::ir_loop()
1341    : ir_instruction(ir_type_loop)
1342 {
1343 }
1344 
1345 
ir_dereference_variable(ir_variable * var)1346 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1347    : ir_dereference(ir_type_dereference_variable)
1348 {
1349    assert(var != NULL);
1350 
1351    this->var = var;
1352    this->type = var->type;
1353 }
1354 
1355 
ir_dereference_array(ir_rvalue * value,ir_rvalue * array_index)1356 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1357 					   ir_rvalue *array_index)
1358    : ir_dereference(ir_type_dereference_array)
1359 {
1360    this->array_index = array_index;
1361    this->set_array(value);
1362 }
1363 
1364 
ir_dereference_array(ir_variable * var,ir_rvalue * array_index)1365 ir_dereference_array::ir_dereference_array(ir_variable *var,
1366 					   ir_rvalue *array_index)
1367    : ir_dereference(ir_type_dereference_array)
1368 {
1369    void *ctx = ralloc_parent(var);
1370 
1371    this->array_index = array_index;
1372    this->set_array(new(ctx) ir_dereference_variable(var));
1373 }
1374 
1375 
1376 void
set_array(ir_rvalue * value)1377 ir_dereference_array::set_array(ir_rvalue *value)
1378 {
1379    assert(value != NULL);
1380 
1381    this->array = value;
1382 
1383    const glsl_type *const vt = this->array->type;
1384 
1385    if (vt->is_array()) {
1386       type = vt->fields.array;
1387    } else if (vt->is_matrix()) {
1388       type = vt->column_type();
1389    } else if (vt->is_vector()) {
1390       type = vt->get_base_type();
1391    }
1392 }
1393 
1394 
ir_dereference_record(ir_rvalue * value,const char * field)1395 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1396 					     const char *field)
1397    : ir_dereference(ir_type_dereference_record)
1398 {
1399    assert(value != NULL);
1400 
1401    this->record = value;
1402    this->type = this->record->type->field_type(field);
1403    this->field_idx = this->record->type->field_index(field);
1404 }
1405 
1406 
ir_dereference_record(ir_variable * var,const char * field)1407 ir_dereference_record::ir_dereference_record(ir_variable *var,
1408 					     const char *field)
1409    : ir_dereference(ir_type_dereference_record)
1410 {
1411    void *ctx = ralloc_parent(var);
1412 
1413    this->record = new(ctx) ir_dereference_variable(var);
1414    this->type = this->record->type->field_type(field);
1415    this->field_idx = this->record->type->field_index(field);
1416 }
1417 
1418 bool
is_lvalue(const struct _mesa_glsl_parse_state * state) const1419 ir_dereference::is_lvalue(const struct _mesa_glsl_parse_state *state) const
1420 {
1421    ir_variable *var = this->variable_referenced();
1422 
1423    /* Every l-value derference chain eventually ends in a variable.
1424     */
1425    if ((var == NULL) || var->data.read_only)
1426       return false;
1427 
1428    /* From section 4.1.7 of the ARB_bindless_texture spec:
1429     *
1430     * "Samplers can be used as l-values, so can be assigned into and used as
1431     *  "out" and "inout" function parameters."
1432     *
1433     * From section 4.1.X of the ARB_bindless_texture spec:
1434     *
1435     * "Images can be used as l-values, so can be assigned into and used as
1436     *  "out" and "inout" function parameters."
1437     */
1438    if ((!state || state->has_bindless()) &&
1439        (this->type->contains_sampler() || this->type->contains_image()))
1440       return true;
1441 
1442    /* From section 4.1.7 of the GLSL 4.40 spec:
1443     *
1444     *   "Opaque variables cannot be treated as l-values; hence cannot
1445     *    be used as out or inout function parameters, nor can they be
1446     *    assigned into."
1447     */
1448    if (this->type->contains_opaque())
1449       return false;
1450 
1451    return true;
1452 }
1453 
1454 
1455 static const char * const tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txf_ms", "txs", "lod", "tg4", "query_levels", "texture_samples", "samples_identical" };
1456 
opcode_string()1457 const char *ir_texture::opcode_string()
1458 {
1459    assert((unsigned int) op < ARRAY_SIZE(tex_opcode_strs));
1460    return tex_opcode_strs[op];
1461 }
1462 
1463 ir_texture_opcode
get_opcode(const char * str)1464 ir_texture::get_opcode(const char *str)
1465 {
1466    const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1467    for (int op = 0; op < count; op++) {
1468       if (strcmp(str, tex_opcode_strs[op]) == 0)
1469 	 return (ir_texture_opcode) op;
1470    }
1471    return (ir_texture_opcode) -1;
1472 }
1473 
1474 
1475 void
set_sampler(ir_dereference * sampler,const glsl_type * type)1476 ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1477 {
1478    assert(sampler != NULL);
1479    assert(type != NULL);
1480    this->sampler = sampler;
1481    this->type = type;
1482 
1483    if (this->op == ir_txs || this->op == ir_query_levels ||
1484        this->op == ir_texture_samples) {
1485       assert(type->base_type == GLSL_TYPE_INT);
1486    } else if (this->op == ir_lod) {
1487       assert(type->vector_elements == 2);
1488       assert(type->is_float());
1489    } else if (this->op == ir_samples_identical) {
1490       assert(type == glsl_type::bool_type);
1491       assert(sampler->type->is_sampler());
1492       assert(sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS);
1493    } else {
1494       assert(sampler->type->sampled_type == (int) type->base_type);
1495       if (sampler->type->sampler_shadow)
1496 	 assert(type->vector_elements == 4 || type->vector_elements == 1);
1497       else
1498 	 assert(type->vector_elements == 4);
1499    }
1500 }
1501 
1502 
1503 void
init_mask(const unsigned * comp,unsigned count)1504 ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1505 {
1506    assert((count >= 1) && (count <= 4));
1507 
1508    memset(&this->mask, 0, sizeof(this->mask));
1509    this->mask.num_components = count;
1510 
1511    unsigned dup_mask = 0;
1512    switch (count) {
1513    case 4:
1514       assert(comp[3] <= 3);
1515       dup_mask |= (1U << comp[3])
1516 	 & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1517       this->mask.w = comp[3];
1518 
1519    case 3:
1520       assert(comp[2] <= 3);
1521       dup_mask |= (1U << comp[2])
1522 	 & ((1U << comp[0]) | (1U << comp[1]));
1523       this->mask.z = comp[2];
1524 
1525    case 2:
1526       assert(comp[1] <= 3);
1527       dup_mask |= (1U << comp[1])
1528 	 & ((1U << comp[0]));
1529       this->mask.y = comp[1];
1530 
1531    case 1:
1532       assert(comp[0] <= 3);
1533       this->mask.x = comp[0];
1534    }
1535 
1536    this->mask.has_duplicates = dup_mask != 0;
1537 
1538    /* Based on the number of elements in the swizzle and the base type
1539     * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1540     * generate the type of the resulting value.
1541     */
1542    type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
1543 }
1544 
ir_swizzle(ir_rvalue * val,unsigned x,unsigned y,unsigned z,unsigned w,unsigned count)1545 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1546 		       unsigned w, unsigned count)
1547    : ir_rvalue(ir_type_swizzle), val(val)
1548 {
1549    const unsigned components[4] = { x, y, z, w };
1550    this->init_mask(components, count);
1551 }
1552 
ir_swizzle(ir_rvalue * val,const unsigned * comp,unsigned count)1553 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1554 		       unsigned count)
1555    : ir_rvalue(ir_type_swizzle), val(val)
1556 {
1557    this->init_mask(comp, count);
1558 }
1559 
ir_swizzle(ir_rvalue * val,ir_swizzle_mask mask)1560 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1561    : ir_rvalue(ir_type_swizzle), val(val), mask(mask)
1562 {
1563    this->type = glsl_type::get_instance(val->type->base_type,
1564 					mask.num_components, 1);
1565 }
1566 
1567 #define X 1
1568 #define R 5
1569 #define S 9
1570 #define I 13
1571 
1572 ir_swizzle *
create(ir_rvalue * val,const char * str,unsigned vector_length)1573 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1574 {
1575    void *ctx = ralloc_parent(val);
1576 
1577    /* For each possible swizzle character, this table encodes the value in
1578     * \c idx_map that represents the 0th element of the vector.  For invalid
1579     * swizzle characters (e.g., 'k'), a special value is used that will allow
1580     * detection of errors.
1581     */
1582    static const unsigned char base_idx[26] = {
1583    /* a  b  c  d  e  f  g  h  i  j  k  l  m */
1584       R, R, I, I, I, I, R, I, I, I, I, I, I,
1585    /* n  o  p  q  r  s  t  u  v  w  x  y  z */
1586       I, I, S, S, R, S, S, I, I, X, X, X, X
1587    };
1588 
1589    /* Each valid swizzle character has an entry in the previous table.  This
1590     * table encodes the base index encoded in the previous table plus the actual
1591     * index of the swizzle character.  When processing swizzles, the first
1592     * character in the string is indexed in the previous table.  Each character
1593     * in the string is indexed in this table, and the value found there has the
1594     * value form the first table subtracted.  The result must be on the range
1595     * [0,3].
1596     *
1597     * For example, the string "wzyx" will get X from the first table.  Each of
1598     * the charcaters will get X+3, X+2, X+1, and X+0 from this table.  After
1599     * subtraction, the swizzle values are { 3, 2, 1, 0 }.
1600     *
1601     * The string "wzrg" will get X from the first table.  Each of the characters
1602     * will get X+3, X+2, R+0, and R+1 from this table.  After subtraction, the
1603     * swizzle values are { 3, 2, 4, 5 }.  Since 4 and 5 are outside the range
1604     * [0,3], the error is detected.
1605     */
1606    static const unsigned char idx_map[26] = {
1607    /* a    b    c    d    e    f    g    h    i    j    k    l    m */
1608       R+3, R+2, 0,   0,   0,   0,   R+1, 0,   0,   0,   0,   0,   0,
1609    /* n    o    p    q    r    s    t    u    v    w    x    y    z */
1610       0,   0,   S+2, S+3, R+0, S+0, S+1, 0,   0,   X+3, X+0, X+1, X+2
1611    };
1612 
1613    int swiz_idx[4] = { 0, 0, 0, 0 };
1614    unsigned i;
1615 
1616 
1617    /* Validate the first character in the swizzle string and look up the base
1618     * index value as described above.
1619     */
1620    if ((str[0] < 'a') || (str[0] > 'z'))
1621       return NULL;
1622 
1623    const unsigned base = base_idx[str[0] - 'a'];
1624 
1625 
1626    for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1627       /* Validate the next character, and, as described above, convert it to a
1628        * swizzle index.
1629        */
1630       if ((str[i] < 'a') || (str[i] > 'z'))
1631 	 return NULL;
1632 
1633       swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1634       if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1635 	 return NULL;
1636    }
1637 
1638    if (str[i] != '\0')
1639 	 return NULL;
1640 
1641    return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1642 			      swiz_idx[3], i);
1643 }
1644 
1645 #undef X
1646 #undef R
1647 #undef S
1648 #undef I
1649 
1650 ir_variable *
variable_referenced() const1651 ir_swizzle::variable_referenced() const
1652 {
1653    return this->val->variable_referenced();
1654 }
1655 
1656 
1657 bool ir_variable::temporaries_allocate_names = false;
1658 
1659 const char ir_variable::tmp_name[] = "compiler_temp";
1660 
ir_variable(const struct glsl_type * type,const char * name,ir_variable_mode mode)1661 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1662 			 ir_variable_mode mode)
1663    : ir_instruction(ir_type_variable)
1664 {
1665    this->type = type;
1666 
1667    if (mode == ir_var_temporary && !ir_variable::temporaries_allocate_names)
1668       name = NULL;
1669 
1670    /* The ir_variable clone method may call this constructor with name set to
1671     * tmp_name.
1672     */
1673    assert(name != NULL
1674           || mode == ir_var_temporary
1675           || mode == ir_var_function_in
1676           || mode == ir_var_function_out
1677           || mode == ir_var_function_inout);
1678    assert(name != ir_variable::tmp_name
1679           || mode == ir_var_temporary);
1680    if (mode == ir_var_temporary
1681        && (name == NULL || name == ir_variable::tmp_name)) {
1682       this->name = ir_variable::tmp_name;
1683    } else if (name == NULL ||
1684               strlen(name) < ARRAY_SIZE(this->name_storage)) {
1685       strcpy(this->name_storage, name ? name : "");
1686       this->name = this->name_storage;
1687    } else {
1688       this->name = ralloc_strdup(this, name);
1689    }
1690 
1691    this->u.max_ifc_array_access = NULL;
1692 
1693    this->data.explicit_location = false;
1694    this->data.has_initializer = false;
1695    this->data.location = -1;
1696    this->data.location_frac = 0;
1697    this->data.binding = 0;
1698    this->data.warn_extension_index = 0;
1699    this->constant_value = NULL;
1700    this->constant_initializer = NULL;
1701    this->data.origin_upper_left = false;
1702    this->data.pixel_center_integer = false;
1703    this->data.depth_layout = ir_depth_layout_none;
1704    this->data.used = false;
1705    this->data.always_active_io = false;
1706    this->data.read_only = false;
1707    this->data.centroid = false;
1708    this->data.sample = false;
1709    this->data.patch = false;
1710    this->data.invariant = false;
1711    this->data.how_declared = ir_var_declared_normally;
1712    this->data.mode = mode;
1713    this->data.interpolation = INTERP_MODE_NONE;
1714    this->data.max_array_access = -1;
1715    this->data.offset = 0;
1716    this->data.precision = GLSL_PRECISION_NONE;
1717    this->data.memory_read_only = false;
1718    this->data.memory_write_only = false;
1719    this->data.memory_coherent = false;
1720    this->data.memory_volatile = false;
1721    this->data.memory_restrict = false;
1722    this->data.from_ssbo_unsized_array = false;
1723    this->data.fb_fetch_output = false;
1724    this->data.bindless = false;
1725    this->data.bound = false;
1726 
1727    if (type != NULL) {
1728       if (type->is_interface())
1729          this->init_interface_type(type);
1730       else if (type->without_array()->is_interface())
1731          this->init_interface_type(type->without_array());
1732    }
1733 }
1734 
1735 
1736 const char *
interpolation_string(unsigned interpolation)1737 interpolation_string(unsigned interpolation)
1738 {
1739    switch (interpolation) {
1740    case INTERP_MODE_NONE:          return "no";
1741    case INTERP_MODE_SMOOTH:        return "smooth";
1742    case INTERP_MODE_FLAT:          return "flat";
1743    case INTERP_MODE_NOPERSPECTIVE: return "noperspective";
1744    }
1745 
1746    assert(!"Should not get here.");
1747    return "";
1748 }
1749 
1750 const char *const ir_variable::warn_extension_table[] = {
1751    "",
1752    "GL_ARB_shader_stencil_export",
1753    "GL_AMD_shader_stencil_export",
1754 };
1755 
1756 void
enable_extension_warning(const char * extension)1757 ir_variable::enable_extension_warning(const char *extension)
1758 {
1759    for (unsigned i = 0; i < ARRAY_SIZE(warn_extension_table); i++) {
1760       if (strcmp(warn_extension_table[i], extension) == 0) {
1761          this->data.warn_extension_index = i;
1762          return;
1763       }
1764    }
1765 
1766    assert(!"Should not get here.");
1767    this->data.warn_extension_index = 0;
1768 }
1769 
1770 const char *
get_extension_warning() const1771 ir_variable::get_extension_warning() const
1772 {
1773    return this->data.warn_extension_index == 0
1774       ? NULL : warn_extension_table[this->data.warn_extension_index];
1775 }
1776 
ir_function_signature(const glsl_type * return_type,builtin_available_predicate b)1777 ir_function_signature::ir_function_signature(const glsl_type *return_type,
1778                                              builtin_available_predicate b)
1779    : ir_instruction(ir_type_function_signature),
1780      return_type(return_type), is_defined(false),
1781      intrinsic_id(ir_intrinsic_invalid), builtin_avail(b), _function(NULL)
1782 {
1783    this->origin = NULL;
1784 }
1785 
1786 
1787 bool
is_builtin() const1788 ir_function_signature::is_builtin() const
1789 {
1790    return builtin_avail != NULL;
1791 }
1792 
1793 
1794 bool
is_builtin_available(const _mesa_glsl_parse_state * state) const1795 ir_function_signature::is_builtin_available(const _mesa_glsl_parse_state *state) const
1796 {
1797    /* We can't call the predicate without a state pointer, so just say that
1798     * the signature is available.  At compile time, we need the filtering,
1799     * but also receive a valid state pointer.  At link time, we're resolving
1800     * imported built-in prototypes to their definitions, which will always
1801     * be an exact match.  So we can skip the filtering.
1802     */
1803    if (state == NULL)
1804       return true;
1805 
1806    assert(builtin_avail != NULL);
1807    return builtin_avail(state);
1808 }
1809 
1810 
1811 static bool
modes_match(unsigned a,unsigned b)1812 modes_match(unsigned a, unsigned b)
1813 {
1814    if (a == b)
1815       return true;
1816 
1817    /* Accept "in" vs. "const in" */
1818    if ((a == ir_var_const_in && b == ir_var_function_in) ||
1819        (b == ir_var_const_in && a == ir_var_function_in))
1820       return true;
1821 
1822    return false;
1823 }
1824 
1825 
1826 const char *
qualifiers_match(exec_list * params)1827 ir_function_signature::qualifiers_match(exec_list *params)
1828 {
1829    /* check that the qualifiers match. */
1830    foreach_two_lists(a_node, &this->parameters, b_node, params) {
1831       ir_variable *a = (ir_variable *) a_node;
1832       ir_variable *b = (ir_variable *) b_node;
1833 
1834       if (a->data.read_only != b->data.read_only ||
1835 	  !modes_match(a->data.mode, b->data.mode) ||
1836 	  a->data.interpolation != b->data.interpolation ||
1837 	  a->data.centroid != b->data.centroid ||
1838           a->data.sample != b->data.sample ||
1839           a->data.patch != b->data.patch ||
1840           a->data.memory_read_only != b->data.memory_read_only ||
1841           a->data.memory_write_only != b->data.memory_write_only ||
1842           a->data.memory_coherent != b->data.memory_coherent ||
1843           a->data.memory_volatile != b->data.memory_volatile ||
1844           a->data.memory_restrict != b->data.memory_restrict) {
1845 
1846 	 /* parameter a's qualifiers don't match */
1847 	 return a->name;
1848       }
1849    }
1850    return NULL;
1851 }
1852 
1853 
1854 void
replace_parameters(exec_list * new_params)1855 ir_function_signature::replace_parameters(exec_list *new_params)
1856 {
1857    /* Destroy all of the previous parameter information.  If the previous
1858     * parameter information comes from the function prototype, it may either
1859     * specify incorrect parameter names or not have names at all.
1860     */
1861    new_params->move_nodes_to(&parameters);
1862 }
1863 
1864 
ir_function(const char * name)1865 ir_function::ir_function(const char *name)
1866    : ir_instruction(ir_type_function)
1867 {
1868    this->subroutine_index = -1;
1869    this->name = ralloc_strdup(this, name);
1870 }
1871 
1872 
1873 bool
has_user_signature()1874 ir_function::has_user_signature()
1875 {
1876    foreach_in_list(ir_function_signature, sig, &this->signatures) {
1877       if (!sig->is_builtin())
1878 	 return true;
1879    }
1880    return false;
1881 }
1882 
1883 
1884 ir_rvalue *
error_value(void * mem_ctx)1885 ir_rvalue::error_value(void *mem_ctx)
1886 {
1887    ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_unset);
1888 
1889    v->type = glsl_type::error_type;
1890    return v;
1891 }
1892 
1893 
1894 void
visit_exec_list(exec_list * list,ir_visitor * visitor)1895 visit_exec_list(exec_list *list, ir_visitor *visitor)
1896 {
1897    foreach_in_list_safe(ir_instruction, node, list) {
1898       node->accept(visitor);
1899    }
1900 }
1901 
1902 
1903 static void
steal_memory(ir_instruction * ir,void * new_ctx)1904 steal_memory(ir_instruction *ir, void *new_ctx)
1905 {
1906    ir_variable *var = ir->as_variable();
1907    ir_function *fn = ir->as_function();
1908    ir_constant *constant = ir->as_constant();
1909    if (var != NULL && var->constant_value != NULL)
1910       steal_memory(var->constant_value, ir);
1911 
1912    if (var != NULL && var->constant_initializer != NULL)
1913       steal_memory(var->constant_initializer, ir);
1914 
1915    if (fn != NULL && fn->subroutine_types)
1916       ralloc_steal(new_ctx, fn->subroutine_types);
1917 
1918    /* The components of aggregate constants are not visited by the normal
1919     * visitor, so steal their values by hand.
1920     */
1921    if (constant != NULL &&
1922        (constant->type->is_array() || constant->type->is_record())) {
1923       for (unsigned int i = 0; i < constant->type->length; i++) {
1924          steal_memory(constant->const_elements[i], ir);
1925       }
1926    }
1927 
1928    ralloc_steal(new_ctx, ir);
1929 }
1930 
1931 
1932 void
reparent_ir(exec_list * list,void * mem_ctx)1933 reparent_ir(exec_list *list, void *mem_ctx)
1934 {
1935    foreach_in_list(ir_instruction, node, list) {
1936       visit_tree(node, steal_memory, mem_ctx);
1937    }
1938 }
1939 
1940 
1941 static ir_rvalue *
try_min_one(ir_rvalue * ir)1942 try_min_one(ir_rvalue *ir)
1943 {
1944    ir_expression *expr = ir->as_expression();
1945 
1946    if (!expr || expr->operation != ir_binop_min)
1947       return NULL;
1948 
1949    if (expr->operands[0]->is_one())
1950       return expr->operands[1];
1951 
1952    if (expr->operands[1]->is_one())
1953       return expr->operands[0];
1954 
1955    return NULL;
1956 }
1957 
1958 static ir_rvalue *
try_max_zero(ir_rvalue * ir)1959 try_max_zero(ir_rvalue *ir)
1960 {
1961    ir_expression *expr = ir->as_expression();
1962 
1963    if (!expr || expr->operation != ir_binop_max)
1964       return NULL;
1965 
1966    if (expr->operands[0]->is_zero())
1967       return expr->operands[1];
1968 
1969    if (expr->operands[1]->is_zero())
1970       return expr->operands[0];
1971 
1972    return NULL;
1973 }
1974 
1975 ir_rvalue *
as_rvalue_to_saturate()1976 ir_rvalue::as_rvalue_to_saturate()
1977 {
1978    ir_expression *expr = this->as_expression();
1979 
1980    if (!expr)
1981       return NULL;
1982 
1983    ir_rvalue *max_zero = try_max_zero(expr);
1984    if (max_zero) {
1985       return try_min_one(max_zero);
1986    } else {
1987       ir_rvalue *min_one = try_min_one(expr);
1988       if (min_one) {
1989 	 return try_max_zero(min_one);
1990       }
1991    }
1992 
1993    return NULL;
1994 }
1995 
1996 
1997 unsigned
vertices_per_prim(GLenum prim)1998 vertices_per_prim(GLenum prim)
1999 {
2000    switch (prim) {
2001    case GL_POINTS:
2002       return 1;
2003    case GL_LINES:
2004       return 2;
2005    case GL_TRIANGLES:
2006       return 3;
2007    case GL_LINES_ADJACENCY:
2008       return 4;
2009    case GL_TRIANGLES_ADJACENCY:
2010       return 6;
2011    default:
2012       assert(!"Bad primitive");
2013       return 3;
2014    }
2015 }
2016 
2017 /**
2018  * Generate a string describing the mode of a variable
2019  */
2020 const char *
mode_string(const ir_variable * var)2021 mode_string(const ir_variable *var)
2022 {
2023    switch (var->data.mode) {
2024    case ir_var_auto:
2025       return (var->data.read_only) ? "global constant" : "global variable";
2026 
2027    case ir_var_uniform:
2028       return "uniform";
2029 
2030    case ir_var_shader_storage:
2031       return "buffer";
2032 
2033    case ir_var_shader_in:
2034       return "shader input";
2035 
2036    case ir_var_shader_out:
2037       return "shader output";
2038 
2039    case ir_var_function_in:
2040    case ir_var_const_in:
2041       return "function input";
2042 
2043    case ir_var_function_out:
2044       return "function output";
2045 
2046    case ir_var_function_inout:
2047       return "function inout";
2048 
2049    case ir_var_system_value:
2050       return "shader input";
2051 
2052    case ir_var_temporary:
2053       return "compiler temporary";
2054 
2055    case ir_var_mode_count:
2056       break;
2057    }
2058 
2059    assert(!"Should not get here.");
2060    return "invalid variable";
2061 }
2062