Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4358 Serge 1
/* -*- c++ -*- */
2
/*
3
 * Copyright © 2010 Intel Corporation
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice (including the next
13
 * paragraph) shall be included in all copies or substantial portions of the
14
 * Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
25
#pragma once
26
#ifndef IR_H
27
#define IR_H
28
 
29
#include 
30
#include 
31
 
32
#include "ralloc.h"
33
#include "glsl_types.h"
34
#include "list.h"
35
#include "ir_visitor.h"
36
#include "ir_hierarchical_visitor.h"
37
#include "main/mtypes.h"
38
 
39
#ifdef __cplusplus
40
 
41
/**
42
 * \defgroup IR Intermediate representation nodes
43
 *
44
 * @{
45
 */
46
 
47
/**
48
 * Class tags
49
 *
50
 * Each concrete class derived from \c ir_instruction has a value in this
51
 * enumerant.  The value for the type is stored in \c ir_instruction::ir_type
52
 * by the constructor.  While using type tags is not very C++, it is extremely
53
 * convenient.  For example, during debugging you can simply inspect
54
 * \c ir_instruction::ir_type to find out the actual type of the object.
55
 *
56
 * In addition, it is possible to use a switch-statement based on \c
57
 * \c ir_instruction::ir_type to select different behavior for different object
58
 * types.  For functions that have only slight differences for several object
59
 * types, this allows writing very straightforward, readable code.
60
 */
61
enum ir_node_type {
62
   /**
63
    * Zero is unused so that the IR validator can detect cases where
64
    * \c ir_instruction::ir_type has not been initialized.
65
    */
66
   ir_type_unset,
67
   ir_type_variable,
68
   ir_type_assignment,
69
   ir_type_call,
70
   ir_type_constant,
71
   ir_type_dereference_array,
72
   ir_type_dereference_record,
73
   ir_type_dereference_variable,
74
   ir_type_discard,
75
   ir_type_expression,
76
   ir_type_function,
77
   ir_type_function_signature,
78
   ir_type_if,
79
   ir_type_loop,
80
   ir_type_loop_jump,
81
   ir_type_return,
82
   ir_type_swizzle,
83
   ir_type_texture,
84
   ir_type_max /**< maximum ir_type enum number, for validation */
85
};
86
 
87
/**
88
 * Base class of all IR instructions
89
 */
90
class ir_instruction : public exec_node {
91
public:
92
   enum ir_node_type ir_type;
93
 
94
   /**
95
    * GCC 4.7+ and clang warn when deleting an ir_instruction unless
96
    * there's a virtual destructor present.  Because we almost
97
    * universally use ralloc for our memory management of
98
    * ir_instructions, the destructor doesn't need to do any work.
99
    */
100
   virtual ~ir_instruction()
101
   {
102
   }
103
 
104
   /** ir_print_visitor helper for debugging. */
105
   void print(void) const;
106
 
107
   virtual void accept(ir_visitor *) = 0;
108
   virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
109
   virtual ir_instruction *clone(void *mem_ctx,
110
				 struct hash_table *ht) const = 0;
111
 
112
   /**
113
    * \name IR instruction downcast functions
114
    *
115
    * These functions either cast the object to a derived class or return
116
    * \c NULL if the object's type does not match the specified derived class.
117
    * Additional downcast functions will be added as needed.
118
    */
119
   /*@{*/
120
   virtual class ir_variable *          as_variable()         { return NULL; }
121
   virtual class ir_function *          as_function()         { return NULL; }
122
   virtual class ir_dereference *       as_dereference()      { return NULL; }
123
   virtual class ir_dereference_array *	as_dereference_array() { return NULL; }
124
   virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
125
   virtual class ir_dereference_record *as_dereference_record() { return NULL; }
126
   virtual class ir_expression *        as_expression()       { return NULL; }
127
   virtual class ir_rvalue *            as_rvalue()           { return NULL; }
128
   virtual class ir_loop *              as_loop()             { return NULL; }
129
   virtual class ir_assignment *        as_assignment()       { return NULL; }
130
   virtual class ir_call *              as_call()             { return NULL; }
131
   virtual class ir_return *            as_return()           { return NULL; }
132
   virtual class ir_if *                as_if()               { return NULL; }
133
   virtual class ir_swizzle *           as_swizzle()          { return NULL; }
134
   virtual class ir_constant *          as_constant()         { return NULL; }
135
   virtual class ir_discard *           as_discard()          { return NULL; }
136
   virtual class ir_jump *              as_jump()             { return NULL; }
137
   /*@}*/
138
 
139
protected:
140
   ir_instruction()
141
   {
142
      ir_type = ir_type_unset;
143
   }
144
};
145
 
146
 
147
/**
148
 * The base class for all "values"/expression trees.
149
 */
150
class ir_rvalue : public ir_instruction {
151
public:
152
   const struct glsl_type *type;
153
 
154
   virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;
155
 
156
   virtual void accept(ir_visitor *v)
157
   {
158
      v->visit(this);
159
   }
160
 
161
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
162
 
163
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
164
 
165
   virtual ir_rvalue * as_rvalue()
166
   {
167
      return this;
168
   }
169
 
170
   ir_rvalue *as_rvalue_to_saturate();
171
 
172
   virtual bool is_lvalue() const
173
   {
174
      return false;
175
   }
176
 
177
   /**
178
    * Get the variable that is ultimately referenced by an r-value
179
    */
180
   virtual ir_variable *variable_referenced() const
181
   {
182
      return NULL;
183
   }
184
 
185
 
186
   /**
187
    * If an r-value is a reference to a whole variable, get that variable
188
    *
189
    * \return
190
    * Pointer to a variable that is completely dereferenced by the r-value.  If
191
    * the r-value is not a dereference or the dereference does not access the
192
    * entire variable (i.e., it's just one array element, struct field), \c NULL
193
    * is returned.
194
    */
195
   virtual ir_variable *whole_variable_referenced()
196
   {
197
      return NULL;
198
   }
199
 
200
   /**
201
    * Determine if an r-value has the value zero
202
    *
203
    * The base implementation of this function always returns \c false.  The
204
    * \c ir_constant class over-rides this function to return \c true \b only
205
    * for vector and scalar types that have all elements set to the value
206
    * zero (or \c false for booleans).
207
    *
208
    * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one,
209
    *     ir_constant::is_basis
210
    */
211
   virtual bool is_zero() const;
212
 
213
   /**
214
    * Determine if an r-value has the value one
215
    *
216
    * The base implementation of this function always returns \c false.  The
217
    * \c ir_constant class over-rides this function to return \c true \b only
218
    * for vector and scalar types that have all elements set to the value
219
    * one (or \c true for booleans).
220
    *
221
    * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one,
222
    *     ir_constant::is_basis
223
    */
224
   virtual bool is_one() const;
225
 
226
   /**
227
    * Determine if an r-value has the value negative one
228
    *
229
    * The base implementation of this function always returns \c false.  The
230
    * \c ir_constant class over-rides this function to return \c true \b only
231
    * for vector and scalar types that have all elements set to the value
232
    * negative one.  For boolean types, the result is always \c false.
233
    *
234
    * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
235
    *     ir_constant::is_basis
236
    */
237
   virtual bool is_negative_one() const;
238
 
239
   /**
240
    * Determine if an r-value is a basis vector
241
    *
242
    * The base implementation of this function always returns \c false.  The
243
    * \c ir_constant class over-rides this function to return \c true \b only
244
    * for vector and scalar types that have one element set to the value one,
245
    * and the other elements set to the value zero.  For boolean types, the
246
    * result is always \c false.
247
    *
248
    * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one,
249
    *     is_constant::is_negative_one
250
    */
251
   virtual bool is_basis() const;
252
 
253
 
254
   /**
255
    * Return a generic value of error_type.
256
    *
257
    * Allocation will be performed with 'mem_ctx' as ralloc owner.
258
    */
259
   static ir_rvalue *error_value(void *mem_ctx);
260
 
261
protected:
262
   ir_rvalue();
263
};
264
 
265
 
266
/**
267
 * Variable storage classes
268
 */
269
enum ir_variable_mode {
270
   ir_var_auto = 0,     /**< Function local variables and globals. */
271
   ir_var_uniform,      /**< Variable declared as a uniform. */
272
   ir_var_shader_in,
273
   ir_var_shader_out,
274
   ir_var_function_in,
275
   ir_var_function_out,
276
   ir_var_function_inout,
277
   ir_var_const_in,	/**< "in" param that must be a constant expression */
278
   ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
279
   ir_var_temporary,	/**< Temporary variable generated during compilation. */
280
   ir_var_mode_count	/**< Number of variable modes */
281
};
282
 
283
/**
284
 * \brief Layout qualifiers for gl_FragDepth.
285
 *
286
 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
287
 * with a layout qualifier.
288
 */
289
enum ir_depth_layout {
290
    ir_depth_layout_none, /**< No depth layout is specified. */
291
    ir_depth_layout_any,
292
    ir_depth_layout_greater,
293
    ir_depth_layout_less,
294
    ir_depth_layout_unchanged
295
};
296
 
297
/**
298
 * \brief Convert depth layout qualifier to string.
299
 */
300
const char*
301
depth_layout_string(ir_depth_layout layout);
302
 
303
/**
304
 * Description of built-in state associated with a uniform
305
 *
306
 * \sa ir_variable::state_slots
307
 */
308
struct ir_state_slot {
309
   int tokens[5];
310
   int swizzle;
311
};
312
 
313
class ir_variable : public ir_instruction {
314
public:
315
   ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
316
 
317
   virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
318
 
319
   virtual ir_variable *as_variable()
320
   {
321
      return this;
322
   }
323
 
324
   virtual void accept(ir_visitor *v)
325
   {
326
      v->visit(this);
327
   }
328
 
329
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
330
 
331
 
332
   /**
333
    * Get the string value for the interpolation qualifier
334
    *
335
    * \return The string that would be used in a shader to specify \c
336
    * mode will be returned.
337
    *
338
    * This function is used to generate error messages of the form "shader
339
    * uses %s interpolation qualifier", so in the case where there is no
340
    * interpolation qualifier, it returns "no".
341
    *
342
    * This function should only be used on a shader input or output variable.
343
    */
344
   const char *interpolation_string() const;
345
 
346
   /**
347
    * Determine how this variable should be interpolated based on its
348
    * interpolation qualifier (if present), whether it is gl_Color or
349
    * gl_SecondaryColor, and whether flatshading is enabled in the current GL
350
    * state.
351
    *
352
    * The return value will always be either INTERP_QUALIFIER_SMOOTH,
353
    * INTERP_QUALIFIER_NOPERSPECTIVE, or INTERP_QUALIFIER_FLAT.
354
    */
355
   glsl_interp_qualifier determine_interpolation_mode(bool flat_shade);
356
 
357
   /**
358
    * Determine whether or not a variable is part of a uniform block.
359
    */
360
   inline bool is_in_uniform_block() const
361
   {
362
      return this->mode == ir_var_uniform && this->interface_type != NULL;
363
   }
364
 
365
   /**
366
    * Determine whether or not a variable is the declaration of an interface
367
    * block
368
    *
369
    * For the first declaration below, there will be an \c ir_variable named
370
    * "instance" whose type and whose instance_type will be the same
371
    *  \cglsl_type.  For the second declaration, there will be an \c ir_variable
372
    * named "f" whose type is float and whose instance_type is B2.
373
    *
374
    * "instance" is an interface instance variable, but "f" is not.
375
    *
376
    * uniform B1 {
377
    *     float f;
378
    * } instance;
379
    *
380
    * uniform B2 {
381
    *     float f;
382
    * };
383
    */
384
   inline bool is_interface_instance() const
385
   {
386
      const glsl_type *const t = this->type;
387
 
388
      return (t == this->interface_type)
389
         || (t->is_array() && t->fields.array == this->interface_type);
390
    }
391
 
392
   /**
393
    * Declared type of the variable
394
    */
395
   const struct glsl_type *type;
396
 
397
   /**
398
    * Declared name of the variable
399
    */
400
   const char *name;
401
 
402
   /**
403
    * Highest element accessed with a constant expression array index
404
    *
405
    * Not used for non-array variables.
406
    */
407
   unsigned max_array_access;
408
 
409
   /**
410
    * Is the variable read-only?
411
    *
412
    * This is set for variables declared as \c const, shader inputs,
413
    * and uniforms.
414
    */
415
   unsigned read_only:1;
416
   unsigned centroid:1;
417
   unsigned invariant:1;
418
 
419
   /**
420
    * Has this variable been used for reading or writing?
421
    *
422
    * Several GLSL semantic checks require knowledge of whether or not a
423
    * variable has been used.  For example, it is an error to redeclare a
424
    * variable as invariant after it has been used.
425
    *
426
    * This is only maintained in the ast_to_hir.cpp path, not in
427
    * Mesa's fixed function or ARB program paths.
428
    */
429
   unsigned used:1;
430
 
431
   /**
432
    * Has this variable been statically assigned?
433
    *
434
    * This answers whether the variable was assigned in any path of
435
    * the shader during ast_to_hir.  This doesn't answer whether it is
436
    * still written after dead code removal, nor is it maintained in
437
    * non-ast_to_hir.cpp (GLSL parsing) paths.
438
    */
439
   unsigned assigned:1;
440
 
441
   /**
442
    * Storage class of the variable.
443
    *
444
    * \sa ir_variable_mode
445
    */
446
   unsigned mode:4;
447
 
448
   /**
449
    * Interpolation mode for shader inputs / outputs
450
    *
451
    * \sa ir_variable_interpolation
452
    */
453
   unsigned interpolation:2;
454
 
455
   /**
456
    * \name ARB_fragment_coord_conventions
457
    * @{
458
    */
459
   unsigned origin_upper_left:1;
460
   unsigned pixel_center_integer:1;
461
   /*@}*/
462
 
463
   /**
464
    * Was the location explicitly set in the shader?
465
    *
466
    * If the location is explicitly set in the shader, it \b cannot be changed
467
    * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
468
    * no effect).
469
    */
470
   unsigned explicit_location:1;
471
   unsigned explicit_index:1;
472
 
473
   /**
474
    * Was an initial binding explicitly set in the shader?
475
    *
476
    * If so, constant_value contains an integer ir_constant representing the
477
    * initial binding point.
478
    */
479
   unsigned explicit_binding:1;
480
 
481
   /**
482
    * Does this variable have an initializer?
483
    *
484
    * This is used by the linker to cross-validiate initializers of global
485
    * variables.
486
    */
487
   unsigned has_initializer:1;
488
 
489
   /**
490
    * Is this variable a generic output or input that has not yet been matched
491
    * up to a variable in another stage of the pipeline?
492
    *
493
    * This is used by the linker as scratch storage while assigning locations
494
    * to generic inputs and outputs.
495
    */
496
   unsigned is_unmatched_generic_inout:1;
497
 
498
   /**
499
    * If non-zero, then this variable may be packed along with other variables
500
    * into a single varying slot, so this offset should be applied when
501
    * accessing components.  For example, an offset of 1 means that the x
502
    * component of this variable is actually stored in component y of the
503
    * location specified by \c location.
504
    */
505
   unsigned location_frac:2;
506
 
507
   /**
508
    * \brief Layout qualifier for gl_FragDepth.
509
    *
510
    * This is not equal to \c ir_depth_layout_none if and only if this
511
    * variable is \c gl_FragDepth and a layout qualifier is specified.
512
    */
513
   ir_depth_layout depth_layout;
514
 
515
   /**
516
    * Storage location of the base of this variable
517
    *
518
    * The precise meaning of this field depends on the nature of the variable.
519
    *
520
    *   - Vertex shader input: one of the values from \c gl_vert_attrib.
521
    *   - Vertex shader output: one of the values from \c gl_varying_slot.
522
    *   - Fragment shader input: one of the values from \c gl_varying_slot.
523
    *   - Fragment shader output: one of the values from \c gl_frag_result.
524
    *   - Uniforms: Per-stage uniform slot number for default uniform block.
525
    *   - Uniforms: Index within the uniform block definition for UBO members.
526
    *   - Other: This field is not currently used.
527
    *
528
    * If the variable is a uniform, shader input, or shader output, and the
529
    * slot has not been assigned, the value will be -1.
530
    */
531
   int location;
532
 
533
   /**
534
    * output index for dual source blending.
535
    */
536
   int index;
537
 
538
   /**
539
    * Initial binding point for a sampler or UBO.
540
    *
541
    * For array types, this represents the binding point for the first element.
542
    */
543
   int binding;
544
 
545
   /**
546
    * Built-in state that backs this uniform
547
    *
548
    * Once set at variable creation, \c state_slots must remain invariant.
549
    * This is because, ideally, this array would be shared by all clones of
550
    * this variable in the IR tree.  In other words, we'd really like for it
551
    * to be a fly-weight.
552
    *
553
    * If the variable is not a uniform, \c num_state_slots will be zero and
554
    * \c state_slots will be \c NULL.
555
    */
556
   /*@{*/
557
   unsigned num_state_slots;    /**< Number of state slots used */
558
   ir_state_slot *state_slots;  /**< State descriptors. */
559
   /*@}*/
560
 
561
   /**
562
    * Emit a warning if this variable is accessed.
563
    */
564
   const char *warn_extension;
565
 
566
   /**
567
    * Value assigned in the initializer of a variable declared "const"
568
    */
569
   ir_constant *constant_value;
570
 
571
   /**
572
    * Constant expression assigned in the initializer of the variable
573
    *
574
    * \warning
575
    * This field and \c ::constant_value are distinct.  Even if the two fields
576
    * refer to constants with the same value, they must point to separate
577
    * objects.
578
    */
579
   ir_constant *constant_initializer;
580
 
581
   /**
582
    * For variables that are in an interface block or are an instance of an
583
    * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
584
    *
585
    * \sa ir_variable::location
586
    */
587
   const glsl_type *interface_type;
588
};
589
 
590
 
591
/*@{*/
592
/**
593
 * The representation of a function instance; may be the full definition or
594
 * simply a prototype.
595
 */
596
class ir_function_signature : public ir_instruction {
597
   /* An ir_function_signature will be part of the list of signatures in
598
    * an ir_function.
599
    */
600
public:
601
   ir_function_signature(const glsl_type *return_type);
602
 
603
   virtual ir_function_signature *clone(void *mem_ctx,
604
					struct hash_table *ht) const;
605
   ir_function_signature *clone_prototype(void *mem_ctx,
606
					  struct hash_table *ht) const;
607
 
608
   virtual void accept(ir_visitor *v)
609
   {
610
      v->visit(this);
611
   }
612
 
613
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
614
 
615
   /**
616
    * Attempt to evaluate this function as a constant expression,
617
    * given a list of the actual parameters and the variable context.
618
    * Returns NULL for non-built-ins.
619
    */
620
   ir_constant *constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context);
621
 
622
   /**
623
    * Get the name of the function for which this is a signature
624
    */
625
   const char *function_name() const;
626
 
627
   /**
628
    * Get a handle to the function for which this is a signature
629
    *
630
    * There is no setter function, this function returns a \c const pointer,
631
    * and \c ir_function_signature::_function is private for a reason.  The
632
    * only way to make a connection between a function and function signature
633
    * is via \c ir_function::add_signature.  This helps ensure that certain
634
    * invariants (i.e., a function signature is in the list of signatures for
635
    * its \c _function) are met.
636
    *
637
    * \sa ir_function::add_signature
638
    */
639
   inline const class ir_function *function() const
640
   {
641
      return this->_function;
642
   }
643
 
644
   /**
645
    * Check whether the qualifiers match between this signature's parameters
646
    * and the supplied parameter list.  If not, returns the name of the first
647
    * parameter with mismatched qualifiers (for use in error messages).
648
    */
649
   const char *qualifiers_match(exec_list *params);
650
 
651
   /**
652
    * Replace the current parameter list with the given one.  This is useful
653
    * if the current information came from a prototype, and either has invalid
654
    * or missing parameter names.
655
    */
656
   void replace_parameters(exec_list *new_params);
657
 
658
   /**
659
    * Function return type.
660
    *
661
    * \note This discards the optional precision qualifier.
662
    */
663
   const struct glsl_type *return_type;
664
 
665
   /**
666
    * List of ir_variable of function parameters.
667
    *
668
    * This represents the storage.  The paramaters passed in a particular
669
    * call will be in ir_call::actual_paramaters.
670
    */
671
   struct exec_list parameters;
672
 
673
   /** Whether or not this function has a body (which may be empty). */
674
   unsigned is_defined:1;
675
 
676
   /** Whether or not this function signature is a built-in. */
677
   unsigned is_builtin:1;
678
 
679
   /** Body of instructions in the function. */
680
   struct exec_list body;
681
 
682
private:
683
   /** Function of which this signature is one overload. */
684
   class ir_function *_function;
685
 
686
   /** Function signature of which this one is a prototype clone */
687
   const ir_function_signature *origin;
688
 
689
   friend class ir_function;
690
 
691
   /**
692
    * Helper function to run a list of instructions for constant
693
    * expression evaluation.
694
    *
695
    * The hash table represents the values of the visible variables.
696
    * There are no scoping issues because the table is indexed on
697
    * ir_variable pointers, not variable names.
698
    *
699
    * Returns false if the expression is not constant, true otherwise,
700
    * and the value in *result if result is non-NULL.
701
    */
702
   bool constant_expression_evaluate_expression_list(const struct exec_list &body,
703
						     struct hash_table *variable_context,
704
						     ir_constant **result);
705
};
706
 
707
 
708
/**
709
 * Header for tracking multiple overloaded functions with the same name.
710
 * Contains a list of ir_function_signatures representing each of the
711
 * actual functions.
712
 */
713
class ir_function : public ir_instruction {
714
public:
715
   ir_function(const char *name);
716
 
717
   virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
718
 
719
   virtual ir_function *as_function()
720
   {
721
      return this;
722
   }
723
 
724
   virtual void accept(ir_visitor *v)
725
   {
726
      v->visit(this);
727
   }
728
 
729
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
730
 
731
   void add_signature(ir_function_signature *sig)
732
   {
733
      sig->_function = this;
734
      this->signatures.push_tail(sig);
735
   }
736
 
737
   /**
738
    * Get an iterator for the set of function signatures
739
    */
740
   exec_list_iterator iterator()
741
   {
742
      return signatures.iterator();
743
   }
744
 
745
   /**
746
    * Find a signature that matches a set of actual parameters, taking implicit
747
    * conversions into account.  Also flags whether the match was exact.
748
    */
749
   ir_function_signature *matching_signature(const exec_list *actual_param,
750
					     bool *match_is_exact);
751
 
752
   /**
753
    * Find a signature that matches a set of actual parameters, taking implicit
754
    * conversions into account.
755
    */
756
   ir_function_signature *matching_signature(const exec_list *actual_param);
757
 
758
   /**
759
    * Find a signature that exactly matches a set of actual parameters without
760
    * any implicit type conversions.
761
    */
762
   ir_function_signature *exact_matching_signature(const exec_list *actual_ps);
763
 
764
   /**
765
    * Name of the function.
766
    */
767
   const char *name;
768
 
769
   /** Whether or not this function has a signature that isn't a built-in. */
770
   bool has_user_signature();
771
 
772
   /**
773
    * List of ir_function_signature for each overloaded function with this name.
774
    */
775
   struct exec_list signatures;
776
};
777
 
778
inline const char *ir_function_signature::function_name() const
779
{
780
   return this->_function->name;
781
}
782
/*@}*/
783
 
784
 
785
/**
786
 * IR instruction representing high-level if-statements
787
 */
788
class ir_if : public ir_instruction {
789
public:
790
   ir_if(ir_rvalue *condition)
791
      : condition(condition)
792
   {
793
      ir_type = ir_type_if;
794
   }
795
 
796
   virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
797
 
798
   virtual ir_if *as_if()
799
   {
800
      return this;
801
   }
802
 
803
   virtual void accept(ir_visitor *v)
804
   {
805
      v->visit(this);
806
   }
807
 
808
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
809
 
810
   ir_rvalue *condition;
811
   /** List of ir_instruction for the body of the then branch */
812
   exec_list  then_instructions;
813
   /** List of ir_instruction for the body of the else branch */
814
   exec_list  else_instructions;
815
};
816
 
817
 
818
/**
819
 * IR instruction representing a high-level loop structure.
820
 */
821
class ir_loop : public ir_instruction {
822
public:
823
   ir_loop();
824
 
825
   virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
826
 
827
   virtual void accept(ir_visitor *v)
828
   {
829
      v->visit(this);
830
   }
831
 
832
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
833
 
834
   virtual ir_loop *as_loop()
835
   {
836
      return this;
837
   }
838
 
839
   /**
840
    * Get an iterator for the instructions of the loop body
841
    */
842
   exec_list_iterator iterator()
843
   {
844
      return body_instructions.iterator();
845
   }
846
 
847
   /** List of ir_instruction that make up the body of the loop. */
848
   exec_list body_instructions;
849
 
850
   /**
851
    * \name Loop counter and controls
852
    *
853
    * Represents a loop like a FORTRAN \c do-loop.
854
    *
855
    * \note
856
    * If \c from and \c to are the same value, the loop will execute once.
857
    */
858
   /*@{*/
859
   ir_rvalue *from;             /** Value of the loop counter on the first
860
				 * iteration of the loop.
861
				 */
862
   ir_rvalue *to;               /** Value of the loop counter on the last
863
				 * iteration of the loop.
864
				 */
865
   ir_rvalue *increment;
866
   ir_variable *counter;
867
 
868
   /**
869
    * Comparison operation in the loop terminator.
870
    *
871
    * If any of the loop control fields are non-\c NULL, this field must be
872
    * one of \c ir_binop_less, \c ir_binop_greater, \c ir_binop_lequal,
873
    * \c ir_binop_gequal, \c ir_binop_equal, or \c ir_binop_nequal.
874
    */
875
   int cmp;
876
   /*@}*/
877
};
878
 
879
 
880
class ir_assignment : public ir_instruction {
881
public:
882
   ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition = NULL);
883
 
884
   /**
885
    * Construct an assignment with an explicit write mask
886
    *
887
    * \note
888
    * Since a write mask is supplied, the LHS must already be a bare
889
    * \c ir_dereference.  The cannot be any swizzles in the LHS.
890
    */
891
   ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
892
		 unsigned write_mask);
893
 
894
   virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
895
 
896
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
897
 
898
   virtual void accept(ir_visitor *v)
899
   {
900
      v->visit(this);
901
   }
902
 
903
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
904
 
905
   virtual ir_assignment * as_assignment()
906
   {
907
      return this;
908
   }
909
 
910
   /**
911
    * Get a whole variable written by an assignment
912
    *
913
    * If the LHS of the assignment writes a whole variable, the variable is
914
    * returned.  Otherwise \c NULL is returned.  Examples of whole-variable
915
    * assignment are:
916
    *
917
    *  - Assigning to a scalar
918
    *  - Assigning to all components of a vector
919
    *  - Whole array (or matrix) assignment
920
    *  - Whole structure assignment
921
    */
922
   ir_variable *whole_variable_written();
923
 
924
   /**
925
    * Set the LHS of an assignment
926
    */
927
   void set_lhs(ir_rvalue *lhs);
928
 
929
   /**
930
    * Left-hand side of the assignment.
931
    *
932
    * This should be treated as read only.  If you need to set the LHS of an
933
    * assignment, use \c ir_assignment::set_lhs.
934
    */
935
   ir_dereference *lhs;
936
 
937
   /**
938
    * Value being assigned
939
    */
940
   ir_rvalue *rhs;
941
 
942
   /**
943
    * Optional condition for the assignment.
944
    */
945
   ir_rvalue *condition;
946
 
947
 
948
   /**
949
    * Component mask written
950
    *
951
    * For non-vector types in the LHS, this field will be zero.  For vector
952
    * types, a bit will be set for each component that is written.  Note that
953
    * for \c vec2 and \c vec3 types only the lower bits will ever be set.
954
    *
955
    * A partially-set write mask means that each enabled channel gets
956
    * the value from a consecutive channel of the rhs.  For example,
957
    * to write just .xyw of gl_FrontColor with color:
958
    *
959
    * (assign (constant bool (1)) (xyw)
960
    *     (var_ref gl_FragColor)
961
    *     (swiz xyw (var_ref color)))
962
    */
963
   unsigned write_mask:4;
964
};
965
 
966
/* Update ir_expression::get_num_operands() and operator_strs when
967
 * updating this list.
968
 */
969
enum ir_expression_operation {
970
   ir_unop_bit_not,
971
   ir_unop_logic_not,
972
   ir_unop_neg,
973
   ir_unop_abs,
974
   ir_unop_sign,
975
   ir_unop_rcp,
976
   ir_unop_rsq,
977
   ir_unop_sqrt,
978
   ir_unop_exp,         /**< Log base e on gentype */
979
   ir_unop_log,	        /**< Natural log on gentype */
980
   ir_unop_exp2,
981
   ir_unop_log2,
982
   ir_unop_f2i,         /**< Float-to-integer conversion. */
983
   ir_unop_f2u,         /**< Float-to-unsigned conversion. */
984
   ir_unop_i2f,         /**< Integer-to-float conversion. */
985
   ir_unop_f2b,         /**< Float-to-boolean conversion */
986
   ir_unop_b2f,         /**< Boolean-to-float conversion */
987
   ir_unop_i2b,         /**< int-to-boolean conversion */
988
   ir_unop_b2i,         /**< Boolean-to-int conversion */
989
   ir_unop_u2f,         /**< Unsigned-to-float conversion. */
990
   ir_unop_i2u,         /**< Integer-to-unsigned conversion. */
991
   ir_unop_u2i,         /**< Unsigned-to-integer conversion. */
992
   ir_unop_bitcast_i2f, /**< Bit-identical int-to-float "conversion" */
993
   ir_unop_bitcast_f2i, /**< Bit-identical float-to-int "conversion" */
994
   ir_unop_bitcast_u2f, /**< Bit-identical uint-to-float "conversion" */
995
   ir_unop_bitcast_f2u, /**< Bit-identical float-to-uint "conversion" */
996
   ir_unop_any,
997
 
998
   /**
999
    * \name Unary floating-point rounding operations.
1000
    */
1001
   /*@{*/
1002
   ir_unop_trunc,
1003
   ir_unop_ceil,
1004
   ir_unop_floor,
1005
   ir_unop_fract,
1006
   ir_unop_round_even,
1007
   /*@}*/
1008
 
1009
   /**
1010
    * \name Trigonometric operations.
1011
    */
1012
   /*@{*/
1013
   ir_unop_sin,
1014
   ir_unop_cos,
1015
   ir_unop_sin_reduced,    /**< Reduced range sin. [-pi, pi] */
1016
   ir_unop_cos_reduced,    /**< Reduced range cos. [-pi, pi] */
1017
   /*@}*/
1018
 
1019
   /**
1020
    * \name Partial derivatives.
1021
    */
1022
   /*@{*/
1023
   ir_unop_dFdx,
1024
   ir_unop_dFdy,
1025
   /*@}*/
1026
 
1027
   /**
1028
    * \name Floating point pack and unpack operations.
1029
    */
1030
   /*@{*/
1031
   ir_unop_pack_snorm_2x16,
1032
   ir_unop_pack_snorm_4x8,
1033
   ir_unop_pack_unorm_2x16,
1034
   ir_unop_pack_unorm_4x8,
1035
   ir_unop_pack_half_2x16,
1036
   ir_unop_unpack_snorm_2x16,
1037
   ir_unop_unpack_snorm_4x8,
1038
   ir_unop_unpack_unorm_2x16,
1039
   ir_unop_unpack_unorm_4x8,
1040
   ir_unop_unpack_half_2x16,
1041
   /*@}*/
1042
 
1043
   /**
1044
    * \name Lowered floating point unpacking operations.
1045
    *
1046
    * \see lower_packing_builtins_visitor::split_unpack_half_2x16
1047
    */
1048
   /*@{*/
1049
   ir_unop_unpack_half_2x16_split_x,
1050
   ir_unop_unpack_half_2x16_split_y,
1051
   /*@}*/
1052
 
1053
   /**
1054
    * \name Bit operations, part of ARB_gpu_shader5.
1055
    */
1056
   /*@{*/
1057
   ir_unop_bitfield_reverse,
1058
   ir_unop_bit_count,
1059
   ir_unop_find_msb,
1060
   ir_unop_find_lsb,
1061
   /*@}*/
1062
 
1063
   ir_unop_noise,
1064
 
1065
   /**
1066
    * A sentinel marking the last of the unary operations.
1067
    */
1068
   ir_last_unop = ir_unop_noise,
1069
 
1070
   ir_binop_add,
1071
   ir_binop_sub,
1072
   ir_binop_mul,
1073
   ir_binop_div,
1074
 
1075
   /**
1076
    * Takes one of two combinations of arguments:
1077
    *
1078
    * - mod(vecN, vecN)
1079
    * - mod(vecN, float)
1080
    *
1081
    * Does not take integer types.
1082
    */
1083
   ir_binop_mod,
1084
 
1085
   /**
1086
    * \name Binary comparison operators which return a boolean vector.
1087
    * The type of both operands must be equal.
1088
    */
1089
   /*@{*/
1090
   ir_binop_less,
1091
   ir_binop_greater,
1092
   ir_binop_lequal,
1093
   ir_binop_gequal,
1094
   ir_binop_equal,
1095
   ir_binop_nequal,
1096
   /**
1097
    * Returns single boolean for whether all components of operands[0]
1098
    * equal the components of operands[1].
1099
    */
1100
   ir_binop_all_equal,
1101
   /**
1102
    * Returns single boolean for whether any component of operands[0]
1103
    * is not equal to the corresponding component of operands[1].
1104
    */
1105
   ir_binop_any_nequal,
1106
   /*@}*/
1107
 
1108
   /**
1109
    * \name Bit-wise binary operations.
1110
    */
1111
   /*@{*/
1112
   ir_binop_lshift,
1113
   ir_binop_rshift,
1114
   ir_binop_bit_and,
1115
   ir_binop_bit_xor,
1116
   ir_binop_bit_or,
1117
   /*@}*/
1118
 
1119
   ir_binop_logic_and,
1120
   ir_binop_logic_xor,
1121
   ir_binop_logic_or,
1122
 
1123
   ir_binop_dot,
1124
   ir_binop_min,
1125
   ir_binop_max,
1126
 
1127
   ir_binop_pow,
1128
 
1129
   /**
1130
    * \name Lowered floating point packing operations.
1131
    *
1132
    * \see lower_packing_builtins_visitor::split_pack_half_2x16
1133
    */
1134
   /*@{*/
1135
   ir_binop_pack_half_2x16_split,
1136
   /*@}*/
1137
 
1138
   /**
1139
    * \name First half of a lowered bitfieldInsert() operation.
1140
    *
1141
    * \see lower_instructions::bitfield_insert_to_bfm_bfi
1142
    */
1143
   /*@{*/
1144
   ir_binop_bfm,
1145
   /*@}*/
1146
 
1147
   /**
1148
    * Load a value the size of a given GLSL type from a uniform block.
1149
    *
1150
    * operand0 is the ir_constant uniform block index in the linked shader.
1151
    * operand1 is a byte offset within the uniform block.
1152
    */
1153
   ir_binop_ubo_load,
1154
 
1155
   /**
1156
    * Extract a scalar from a vector
1157
    *
1158
    * operand0 is the vector
1159
    * operand1 is the index of the field to read from operand0
1160
    */
1161
   ir_binop_vector_extract,
1162
 
1163
   /**
1164
    * A sentinel marking the last of the binary operations.
1165
    */
1166
   ir_last_binop = ir_binop_vector_extract,
1167
 
1168
   ir_triop_lrp,
1169
 
1170
   /**
1171
    * \name Second half of a lowered bitfieldInsert() operation.
1172
    *
1173
    * \see lower_instructions::bitfield_insert_to_bfm_bfi
1174
    */
1175
   /*@{*/
1176
   ir_triop_bfi,
1177
   /*@}*/
1178
 
1179
   ir_triop_bitfield_extract,
1180
 
1181
   /**
1182
    * Generate a value with one field of a vector changed
1183
    *
1184
    * operand0 is the vector
1185
    * operand1 is the value to write into the vector result
1186
    * operand2 is the index in operand0 to be modified
1187
    */
1188
   ir_triop_vector_insert,
1189
 
1190
   /**
1191
    * A sentinel marking the last of the ternary operations.
1192
    */
1193
   ir_last_triop = ir_triop_vector_insert,
1194
 
1195
   ir_quadop_bitfield_insert,
1196
 
1197
   ir_quadop_vector,
1198
 
1199
   /**
1200
    * A sentinel marking the last of the ternary operations.
1201
    */
1202
   ir_last_quadop = ir_quadop_vector,
1203
 
1204
   /**
1205
    * A sentinel marking the last of all operations.
1206
    */
1207
   ir_last_opcode = ir_quadop_vector
1208
};
1209
 
1210
class ir_expression : public ir_rvalue {
1211
public:
1212
   ir_expression(int op, const struct glsl_type *type,
1213
                 ir_rvalue *op0, ir_rvalue *op1 = NULL,
1214
                 ir_rvalue *op2 = NULL, ir_rvalue *op3 = NULL);
1215
 
1216
   /**
1217
    * Constructor for unary operation expressions
1218
    */
1219
   ir_expression(int op, ir_rvalue *);
1220
 
1221
   /**
1222
    * Constructor for binary operation expressions
1223
    */
1224
   ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
1225
 
1226
   virtual ir_expression *as_expression()
1227
   {
1228
      return this;
1229
   }
1230
 
1231
   virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
1232
 
1233
   /**
1234
    * Attempt to constant-fold the expression
1235
    *
1236
    * The "variable_context" hash table links ir_variable * to ir_constant *
1237
    * that represent the variables' values.  \c NULL represents an empty
1238
    * context.
1239
    *
1240
    * If the expression cannot be constant folded, this method will return
1241
    * \c NULL.
1242
    */
1243
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1244
 
1245
   /**
1246
    * Determine the number of operands used by an expression
1247
    */
1248
   static unsigned int get_num_operands(ir_expression_operation);
1249
 
1250
   /**
1251
    * Determine the number of operands used by an expression
1252
    */
1253
   unsigned int get_num_operands() const
1254
   {
1255
      return (this->operation == ir_quadop_vector)
1256
	 ? this->type->vector_elements : get_num_operands(operation);
1257
   }
1258
 
1259
   /**
1260
    * Return a string representing this expression's operator.
1261
    */
1262
   const char *operator_string();
1263
 
1264
   /**
1265
    * Return a string representing this expression's operator.
1266
    */
1267
   static const char *operator_string(ir_expression_operation);
1268
 
1269
 
1270
   /**
1271
    * Do a reverse-lookup to translate the given string into an operator.
1272
    */
1273
   static ir_expression_operation get_operator(const char *);
1274
 
1275
   virtual void accept(ir_visitor *v)
1276
   {
1277
      v->visit(this);
1278
   }
1279
 
1280
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1281
 
1282
   ir_expression_operation operation;
1283
   ir_rvalue *operands[4];
1284
};
1285
 
1286
 
1287
/**
1288
 * HIR instruction representing a high-level function call, containing a list
1289
 * of parameters and returning a value in the supplied temporary.
1290
 */
1291
class ir_call : public ir_instruction {
1292
public:
1293
   ir_call(ir_function_signature *callee,
1294
	   ir_dereference_variable *return_deref,
1295
	   exec_list *actual_parameters)
1296
      : return_deref(return_deref), callee(callee)
1297
   {
1298
      ir_type = ir_type_call;
1299
      assert(callee->return_type != NULL);
1300
      actual_parameters->move_nodes_to(& this->actual_parameters);
1301
      this->use_builtin = callee->is_builtin;
1302
   }
1303
 
1304
   virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
1305
 
1306
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1307
 
1308
   virtual ir_call *as_call()
1309
   {
1310
      return this;
1311
   }
1312
 
1313
   virtual void accept(ir_visitor *v)
1314
   {
1315
      v->visit(this);
1316
   }
1317
 
1318
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1319
 
1320
   /**
1321
    * Get an iterator for the set of acutal parameters
1322
    */
1323
   exec_list_iterator iterator()
1324
   {
1325
      return actual_parameters.iterator();
1326
   }
1327
 
1328
   /**
1329
    * Get the name of the function being called.
1330
    */
1331
   const char *callee_name() const
1332
   {
1333
      return callee->function_name();
1334
   }
1335
 
1336
   /**
1337
    * Generates an inline version of the function before @ir,
1338
    * storing the return value in return_deref.
1339
    */
1340
   void generate_inline(ir_instruction *ir);
1341
 
1342
   /**
1343
    * Storage for the function's return value.
1344
    * This must be NULL if the return type is void.
1345
    */
1346
   ir_dereference_variable *return_deref;
1347
 
1348
   /**
1349
    * The specific function signature being called.
1350
    */
1351
   ir_function_signature *callee;
1352
 
1353
   /* List of ir_rvalue of paramaters passed in this call. */
1354
   exec_list actual_parameters;
1355
 
1356
   /** Should this call only bind to a built-in function? */
1357
   bool use_builtin;
1358
};
1359
 
1360
 
1361
/**
1362
 * \name Jump-like IR instructions.
1363
 *
1364
 * These include \c break, \c continue, \c return, and \c discard.
1365
 */
1366
/*@{*/
1367
class ir_jump : public ir_instruction {
1368
protected:
1369
   ir_jump()
1370
   {
1371
      ir_type = ir_type_unset;
1372
   }
1373
 
1374
public:
1375
   virtual ir_jump *as_jump()
1376
   {
1377
      return this;
1378
   }
1379
};
1380
 
1381
class ir_return : public ir_jump {
1382
public:
1383
   ir_return()
1384
      : value(NULL)
1385
   {
1386
      this->ir_type = ir_type_return;
1387
   }
1388
 
1389
   ir_return(ir_rvalue *value)
1390
      : value(value)
1391
   {
1392
      this->ir_type = ir_type_return;
1393
   }
1394
 
1395
   virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
1396
 
1397
   virtual ir_return *as_return()
1398
   {
1399
      return this;
1400
   }
1401
 
1402
   ir_rvalue *get_value() const
1403
   {
1404
      return value;
1405
   }
1406
 
1407
   virtual void accept(ir_visitor *v)
1408
   {
1409
      v->visit(this);
1410
   }
1411
 
1412
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1413
 
1414
   ir_rvalue *value;
1415
};
1416
 
1417
 
1418
/**
1419
 * Jump instructions used inside loops
1420
 *
1421
 * These include \c break and \c continue.  The \c break within a loop is
1422
 * different from the \c break within a switch-statement.
1423
 *
1424
 * \sa ir_switch_jump
1425
 */
1426
class ir_loop_jump : public ir_jump {
1427
public:
1428
   enum jump_mode {
1429
      jump_break,
1430
      jump_continue
1431
   };
1432
 
1433
   ir_loop_jump(jump_mode mode)
1434
   {
1435
      this->ir_type = ir_type_loop_jump;
1436
      this->mode = mode;
1437
   }
1438
 
1439
   virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
1440
 
1441
   virtual void accept(ir_visitor *v)
1442
   {
1443
      v->visit(this);
1444
   }
1445
 
1446
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1447
 
1448
   bool is_break() const
1449
   {
1450
      return mode == jump_break;
1451
   }
1452
 
1453
   bool is_continue() const
1454
   {
1455
      return mode == jump_continue;
1456
   }
1457
 
1458
   /** Mode selector for the jump instruction. */
1459
   enum jump_mode mode;
1460
};
1461
 
1462
/**
1463
 * IR instruction representing discard statements.
1464
 */
1465
class ir_discard : public ir_jump {
1466
public:
1467
   ir_discard()
1468
   {
1469
      this->ir_type = ir_type_discard;
1470
      this->condition = NULL;
1471
   }
1472
 
1473
   ir_discard(ir_rvalue *cond)
1474
   {
1475
      this->ir_type = ir_type_discard;
1476
      this->condition = cond;
1477
   }
1478
 
1479
   virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
1480
 
1481
   virtual void accept(ir_visitor *v)
1482
   {
1483
      v->visit(this);
1484
   }
1485
 
1486
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1487
 
1488
   virtual ir_discard *as_discard()
1489
   {
1490
      return this;
1491
   }
1492
 
1493
   ir_rvalue *condition;
1494
};
1495
/*@}*/
1496
 
1497
 
1498
/**
1499
 * Texture sampling opcodes used in ir_texture
1500
 */
1501
enum ir_texture_opcode {
1502
   ir_tex,		/**< Regular texture look-up */
1503
   ir_txb,		/**< Texture look-up with LOD bias */
1504
   ir_txl,		/**< Texture look-up with explicit LOD */
1505
   ir_txd,		/**< Texture look-up with partial derivatvies */
1506
   ir_txf,		/**< Texel fetch with explicit LOD */
1507
   ir_txf_ms,           /**< Multisample texture fetch */
1508
   ir_txs,		/**< Texture size */
1509
   ir_lod		/**< Texture lod query */
1510
};
1511
 
1512
 
1513
/**
1514
 * IR instruction to sample a texture
1515
 *
1516
 * The specific form of the IR instruction depends on the \c mode value
1517
 * selected from \c ir_texture_opcodes.  In the printed IR, these will
1518
 * appear as:
1519
 *
1520
 *                                    Texel offset (0 or an expression)
1521
 *                                    | Projection divisor
1522
 *                                    | |  Shadow comparitor
1523
 *                                    | |  |
1524
 *                                    v v  v
1525
 * (tex    0 1 ( ))
1526
 * (txb    0 1 ( ) )
1527
 * (txl    0 1 ( ) )
1528
 * (txd    0 1 ( ) (dPdx dPdy))
1529
 * (txf    0       )
1530
 * (txf_ms
1531
 *                 )
1532
 * (txs   )
1533
 * (lod   )
1534
 */
1535
class ir_texture : public ir_rvalue {
1536
public:
1537
   ir_texture(enum ir_texture_opcode op)
1538
      : op(op), sampler(NULL), coordinate(NULL), projector(NULL),
1539
        shadow_comparitor(NULL), offset(NULL)
1540
   {
1541
      this->ir_type = ir_type_texture;
1542
   }
1543
 
1544
   virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
1545
 
1546
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1547
 
1548
   virtual void accept(ir_visitor *v)
1549
   {
1550
      v->visit(this);
1551
   }
1552
 
1553
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1554
 
1555
   /**
1556
    * Return a string representing the ir_texture_opcode.
1557
    */
1558
   const char *opcode_string();
1559
 
1560
   /** Set the sampler and type. */
1561
   void set_sampler(ir_dereference *sampler, const glsl_type *type);
1562
 
1563
   /**
1564
    * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1565
    */
1566
   static ir_texture_opcode get_opcode(const char *);
1567
 
1568
   enum ir_texture_opcode op;
1569
 
1570
   /** Sampler to use for the texture access. */
1571
   ir_dereference *sampler;
1572
 
1573
   /** Texture coordinate to sample */
1574
   ir_rvalue *coordinate;
1575
 
1576
   /**
1577
    * Value used for projective divide.
1578
    *
1579
    * If there is no projective divide (the common case), this will be
1580
    * \c NULL.  Optimization passes should check for this to point to a constant
1581
    * of 1.0 and replace that with \c NULL.
1582
    */
1583
   ir_rvalue *projector;
1584
 
1585
   /**
1586
    * Coordinate used for comparison on shadow look-ups.
1587
    *
1588
    * If there is no shadow comparison, this will be \c NULL.  For the
1589
    * \c ir_txf opcode, this *must* be \c NULL.
1590
    */
1591
   ir_rvalue *shadow_comparitor;
1592
 
1593
   /** Texel offset. */
1594
   ir_rvalue *offset;
1595
 
1596
   union {
1597
      ir_rvalue *lod;		/**< Floating point LOD */
1598
      ir_rvalue *bias;		/**< Floating point LOD bias */
1599
      ir_rvalue *sample_index;  /**< MSAA sample index */
1600
      struct {
1601
	 ir_rvalue *dPdx;	/**< Partial derivative of coordinate wrt X */
1602
	 ir_rvalue *dPdy;	/**< Partial derivative of coordinate wrt Y */
1603
      } grad;
1604
   } lod_info;
1605
};
1606
 
1607
 
1608
struct ir_swizzle_mask {
1609
   unsigned x:2;
1610
   unsigned y:2;
1611
   unsigned z:2;
1612
   unsigned w:2;
1613
 
1614
   /**
1615
    * Number of components in the swizzle.
1616
    */
1617
   unsigned num_components:3;
1618
 
1619
   /**
1620
    * Does the swizzle contain duplicate components?
1621
    *
1622
    * L-value swizzles cannot contain duplicate components.
1623
    */
1624
   unsigned has_duplicates:1;
1625
};
1626
 
1627
 
1628
class ir_swizzle : public ir_rvalue {
1629
public:
1630
   ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1631
              unsigned count);
1632
 
1633
   ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1634
 
1635
   ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1636
 
1637
   virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1638
 
1639
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1640
 
1641
   virtual ir_swizzle *as_swizzle()
1642
   {
1643
      return this;
1644
   }
1645
 
1646
   /**
1647
    * Construct an ir_swizzle from the textual representation.  Can fail.
1648
    */
1649
   static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1650
 
1651
   virtual void accept(ir_visitor *v)
1652
   {
1653
      v->visit(this);
1654
   }
1655
 
1656
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1657
 
1658
   bool is_lvalue() const
1659
   {
1660
      return val->is_lvalue() && !mask.has_duplicates;
1661
   }
1662
 
1663
   /**
1664
    * Get the variable that is ultimately referenced by an r-value
1665
    */
1666
   virtual ir_variable *variable_referenced() const;
1667
 
1668
   ir_rvalue *val;
1669
   ir_swizzle_mask mask;
1670
 
1671
private:
1672
   /**
1673
    * Initialize the mask component of a swizzle
1674
    *
1675
    * This is used by the \c ir_swizzle constructors.
1676
    */
1677
   void init_mask(const unsigned *components, unsigned count);
1678
};
1679
 
1680
 
1681
class ir_dereference : public ir_rvalue {
1682
public:
1683
   virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
1684
 
1685
   virtual ir_dereference *as_dereference()
1686
   {
1687
      return this;
1688
   }
1689
 
1690
   bool is_lvalue() const;
1691
 
1692
   /**
1693
    * Get the variable that is ultimately referenced by an r-value
1694
    */
1695
   virtual ir_variable *variable_referenced() const = 0;
1696
 
1697
   /**
1698
    * Get the constant that is ultimately referenced by an r-value,
1699
    * in a constant expression evaluation context.
1700
    *
1701
    * The offset is used when the reference is to a specific column of
1702
    * a matrix.
1703
    */
1704
  virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const = 0;
1705
};
1706
 
1707
 
1708
class ir_dereference_variable : public ir_dereference {
1709
public:
1710
   ir_dereference_variable(ir_variable *var);
1711
 
1712
   virtual ir_dereference_variable *clone(void *mem_ctx,
1713
					  struct hash_table *) const;
1714
 
1715
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1716
 
1717
   virtual ir_dereference_variable *as_dereference_variable()
1718
   {
1719
      return this;
1720
   }
1721
 
1722
   /**
1723
    * Get the variable that is ultimately referenced by an r-value
1724
    */
1725
   virtual ir_variable *variable_referenced() const
1726
   {
1727
      return this->var;
1728
   }
1729
 
1730
   /**
1731
    * Get the constant that is ultimately referenced by an r-value,
1732
    * in a constant expression evaluation context.
1733
    *
1734
    * The offset is used when the reference is to a specific column of
1735
    * a matrix.
1736
    */
1737
   virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const;
1738
 
1739
   virtual ir_variable *whole_variable_referenced()
1740
   {
1741
      /* ir_dereference_variable objects always dereference the entire
1742
       * variable.  However, if this dereference is dereferenced by anything
1743
       * else, the complete deferefernce chain is not a whole-variable
1744
       * dereference.  This method should only be called on the top most
1745
       * ir_rvalue in a dereference chain.
1746
       */
1747
      return this->var;
1748
   }
1749
 
1750
   virtual void accept(ir_visitor *v)
1751
   {
1752
      v->visit(this);
1753
   }
1754
 
1755
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1756
 
1757
   /**
1758
    * Object being dereferenced.
1759
    */
1760
   ir_variable *var;
1761
};
1762
 
1763
 
1764
class ir_dereference_array : public ir_dereference {
1765
public:
1766
   ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1767
 
1768
   ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1769
 
1770
   virtual ir_dereference_array *clone(void *mem_ctx,
1771
				       struct hash_table *) const;
1772
 
1773
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1774
 
1775
   virtual ir_dereference_array *as_dereference_array()
1776
   {
1777
      return this;
1778
   }
1779
 
1780
   /**
1781
    * Get the variable that is ultimately referenced by an r-value
1782
    */
1783
   virtual ir_variable *variable_referenced() const
1784
   {
1785
      return this->array->variable_referenced();
1786
   }
1787
 
1788
   /**
1789
    * Get the constant that is ultimately referenced by an r-value,
1790
    * in a constant expression evaluation context.
1791
    *
1792
    * The offset is used when the reference is to a specific column of
1793
    * a matrix.
1794
    */
1795
   virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const;
1796
 
1797
   virtual void accept(ir_visitor *v)
1798
   {
1799
      v->visit(this);
1800
   }
1801
 
1802
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1803
 
1804
   ir_rvalue *array;
1805
   ir_rvalue *array_index;
1806
 
1807
private:
1808
   void set_array(ir_rvalue *value);
1809
};
1810
 
1811
 
1812
class ir_dereference_record : public ir_dereference {
1813
public:
1814
   ir_dereference_record(ir_rvalue *value, const char *field);
1815
 
1816
   ir_dereference_record(ir_variable *var, const char *field);
1817
 
1818
   virtual ir_dereference_record *clone(void *mem_ctx,
1819
					struct hash_table *) const;
1820
 
1821
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1822
 
1823
   virtual ir_dereference_record *as_dereference_record()
1824
   {
1825
      return this;
1826
   }
1827
 
1828
   /**
1829
    * Get the variable that is ultimately referenced by an r-value
1830
    */
1831
   virtual ir_variable *variable_referenced() const
1832
   {
1833
      return this->record->variable_referenced();
1834
   }
1835
 
1836
   /**
1837
    * Get the constant that is ultimately referenced by an r-value,
1838
    * in a constant expression evaluation context.
1839
    *
1840
    * The offset is used when the reference is to a specific column of
1841
    * a matrix.
1842
    */
1843
   virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const;
1844
 
1845
   virtual void accept(ir_visitor *v)
1846
   {
1847
      v->visit(this);
1848
   }
1849
 
1850
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1851
 
1852
   ir_rvalue *record;
1853
   const char *field;
1854
};
1855
 
1856
 
1857
/**
1858
 * Data stored in an ir_constant
1859
 */
1860
union ir_constant_data {
1861
      unsigned u[16];
1862
      int i[16];
1863
      float f[16];
1864
      bool b[16];
1865
};
1866
 
1867
 
1868
class ir_constant : public ir_rvalue {
1869
public:
1870
   ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1871
   ir_constant(bool b);
1872
   ir_constant(unsigned int u);
1873
   ir_constant(int i);
1874
   ir_constant(float f);
1875
 
1876
   /**
1877
    * Construct an ir_constant from a list of ir_constant values
1878
    */
1879
   ir_constant(const struct glsl_type *type, exec_list *values);
1880
 
1881
   /**
1882
    * Construct an ir_constant from a scalar component of another ir_constant
1883
    *
1884
    * The new \c ir_constant inherits the type of the component from the
1885
    * source constant.
1886
    *
1887
    * \note
1888
    * In the case of a matrix constant, the new constant is a scalar, \b not
1889
    * a vector.
1890
    */
1891
   ir_constant(const ir_constant *c, unsigned i);
1892
 
1893
   /**
1894
    * Return a new ir_constant of the specified type containing all zeros.
1895
    */
1896
   static ir_constant *zero(void *mem_ctx, const glsl_type *type);
1897
 
1898
   virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
1899
 
1900
   virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1901
 
1902
   virtual ir_constant *as_constant()
1903
   {
1904
      return this;
1905
   }
1906
 
1907
   virtual void accept(ir_visitor *v)
1908
   {
1909
      v->visit(this);
1910
   }
1911
 
1912
   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1913
 
1914
   /**
1915
    * Get a particular component of a constant as a specific type
1916
    *
1917
    * This is useful, for example, to get a value from an integer constant
1918
    * as a float or bool.  This appears frequently when constructors are
1919
    * called with all constant parameters.
1920
    */
1921
   /*@{*/
1922
   bool get_bool_component(unsigned i) const;
1923
   float get_float_component(unsigned i) const;
1924
   int get_int_component(unsigned i) const;
1925
   unsigned get_uint_component(unsigned i) const;
1926
   /*@}*/
1927
 
1928
   ir_constant *get_array_element(unsigned i) const;
1929
 
1930
   ir_constant *get_record_field(const char *name);
1931
 
1932
   /**
1933
    * Copy the values on another constant at a given offset.
1934
    *
1935
    * The offset is ignored for array or struct copies, it's only for
1936
    * scalars or vectors into vectors or matrices.
1937
    *
1938
    * With identical types on both sides and zero offset it's clone()
1939
    * without creating a new object.
1940
    */
1941
 
1942
   void copy_offset(ir_constant *src, int offset);
1943
 
1944
   /**
1945
    * Copy the values on another constant at a given offset and
1946
    * following an assign-like mask.
1947
    *
1948
    * The mask is ignored for scalars.
1949
    *
1950
    * Note that this function only handles what assign can handle,
1951
    * i.e. at most a vector as source and a column of a matrix as
1952
    * destination.
1953
    */
1954
 
1955
   void copy_masked_offset(ir_constant *src, int offset, unsigned int mask);
1956
 
1957
   /**
1958
    * Determine whether a constant has the same value as another constant
1959
    *
1960
    * \sa ir_constant::is_zero, ir_constant::is_one,
1961
    * ir_constant::is_negative_one, ir_constant::is_basis
1962
    */
1963
   bool has_value(const ir_constant *) const;
1964
 
1965
   virtual bool is_zero() const;
1966
   virtual bool is_one() const;
1967
   virtual bool is_negative_one() const;
1968
   virtual bool is_basis() const;
1969
 
1970
   /**
1971
    * Value of the constant.
1972
    *
1973
    * The field used to back the values supplied by the constant is determined
1974
    * by the type associated with the \c ir_instruction.  Constants may be
1975
    * scalars, vectors, or matrices.
1976
    */
1977
   union ir_constant_data value;
1978
 
1979
   /* Array elements */
1980
   ir_constant **array_elements;
1981
 
1982
   /* Structure fields */
1983
   exec_list components;
1984
 
1985
private:
1986
   /**
1987
    * Parameterless constructor only used by the clone method
1988
    */
1989
   ir_constant(void);
1990
};
1991
 
1992
/*@}*/
1993
 
1994
/**
1995
 * Apply a visitor to each IR node in a list
1996
 */
1997
void
1998
visit_exec_list(exec_list *list, ir_visitor *visitor);
1999
 
2000
/**
2001
 * Validate invariants on each IR node in a list
2002
 */
2003
void validate_ir_tree(exec_list *instructions);
2004
 
2005
struct _mesa_glsl_parse_state;
2006
struct gl_shader_program;
2007
 
2008
/**
2009
 * Detect whether an unlinked shader contains static recursion
2010
 *
2011
 * If the list of instructions is determined to contain static recursion,
2012
 * \c _mesa_glsl_error will be called to emit error messages for each function
2013
 * that is in the recursion cycle.
2014
 */
2015
void
2016
detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
2017
			  exec_list *instructions);
2018
 
2019
/**
2020
 * Detect whether a linked shader contains static recursion
2021
 *
2022
 * If the list of instructions is determined to contain static recursion,
2023
 * \c link_error_printf will be called to emit error messages for each function
2024
 * that is in the recursion cycle.  In addition,
2025
 * \c gl_shader_program::LinkStatus will be set to false.
2026
 */
2027
void
2028
detect_recursion_linked(struct gl_shader_program *prog,
2029
			exec_list *instructions);
2030
 
2031
/**
2032
 * Make a clone of each IR instruction in a list
2033
 *
2034
 * \param in   List of IR instructions that are to be cloned
2035
 * \param out  List to hold the cloned instructions
2036
 */
2037
void
2038
clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
2039
 
2040
extern void
2041
_mesa_glsl_initialize_variables(exec_list *instructions,
2042
				struct _mesa_glsl_parse_state *state);
2043
 
2044
extern void
2045
_mesa_glsl_initialize_functions(_mesa_glsl_parse_state *state);
2046
 
2047
extern void
2048
_mesa_glsl_release_functions(void);
2049
 
2050
extern void
2051
reparent_ir(exec_list *list, void *mem_ctx);
2052
 
2053
struct glsl_symbol_table;
2054
 
2055
extern void
2056
import_prototypes(const exec_list *source, exec_list *dest,
2057
		  struct glsl_symbol_table *symbols, void *mem_ctx);
2058
 
2059
extern bool
2060
ir_has_call(ir_instruction *ir);
2061
 
2062
extern void
2063
do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
2064
                      bool is_fragment_shader);
2065
 
2066
extern char *
2067
prototype_string(const glsl_type *return_type, const char *name,
2068
		 exec_list *parameters);
2069
 
2070
extern "C" {
2071
#endif /* __cplusplus */
2072
 
2073
extern void _mesa_print_ir(struct exec_list *instructions,
2074
                           struct _mesa_glsl_parse_state *state);
2075
 
2076
#ifdef __cplusplus
2077
} /* extern "C" */
2078
#endif
2079
 
2080
#endif /* IR_H */