blob: 33b6069ea5c1512c07fbda02728cc189c3f5cef6 [file] [log] [blame]
Ian Romanick986b8f72010-03-10 13:58:12 -08001/* -*- c++ -*- */
Ian Romanicka87ac252010-02-22 13:19:34 -08002/*
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
Ian Romanicke309a602010-03-15 15:20:15 -070025#pragma once
26#ifndef IR_H
27#define IR_H
28
Ian Romanick0044e7e2010-03-08 23:44:00 -080029#include "list.h"
Ian Romanick78b51b02010-03-09 16:23:37 -080030#include "ir_visitor.h"
Ian Romanick8895bae2010-05-14 12:39:23 -070031#include "ir_hierarchical_visitor.h"
Ian Romanick0044e7e2010-03-08 23:44:00 -080032
Ian Romanicka87ac252010-02-22 13:19:34 -080033struct ir_program {
34 void *bong_hits;
35};
36
Ian Romanicka87ac252010-02-22 13:19:34 -080037/**
38 * Base class of all IR instructions
39 */
Ian Romanick0044e7e2010-03-08 23:44:00 -080040class ir_instruction : public exec_node {
Ian Romanicka87ac252010-02-22 13:19:34 -080041public:
Ian Romanicka87ac252010-02-22 13:19:34 -080042 const struct glsl_type *type;
43
Ian Romanick1cf43a42010-03-30 16:56:50 -070044 class ir_constant *constant_expression_value();
Ian Romanick78b51b02010-03-09 16:23:37 -080045 virtual void accept(ir_visitor *) = 0;
Ian Romanick8895bae2010-05-14 12:39:23 -070046 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
Ian Romanick78b51b02010-03-09 16:23:37 -080047
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070048 /**
49 * \name IR instruction downcast functions
50 *
51 * These functions either cast the object to a derived class or return
52 * \c NULL if the object's type does not match the specified derived class.
53 * Additional downcast functions will be added as needed.
54 */
55 /*@{*/
56 virtual class ir_variable * as_variable() { return NULL; }
Kenneth Graunke6202cbf2010-04-21 16:02:15 -070057 virtual class ir_function * as_function() { return NULL; }
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070058 virtual class ir_dereference * as_dereference() { return NULL; }
Eric Anholtb145e902010-05-11 11:31:09 -070059 virtual class ir_dereference_array * as_dereference_array() { return NULL; }
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070060 virtual class ir_rvalue * as_rvalue() { return NULL; }
Ian Romanick01f8de42010-04-05 17:13:14 -070061 virtual class ir_loop * as_loop() { return NULL; }
Eric Anholtcad97662010-04-07 11:46:26 -070062 virtual class ir_assignment * as_assignment() { return NULL; }
63 virtual class ir_call * as_call() { return NULL; }
64 virtual class ir_return * as_return() { return NULL; }
Eric Anholt5ba94202010-04-14 17:03:03 -070065 virtual class ir_if * as_if() { return NULL; }
Eric Anholt7d211042010-04-16 16:43:47 -070066 virtual class ir_swizzle * as_swizzle() { return NULL; }
Eric Anholt5c89f0e2010-05-04 13:04:40 -070067 virtual class ir_constant * as_constant() { return NULL; }
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070068 /*@}*/
69
Ian Romanicka87ac252010-02-22 13:19:34 -080070protected:
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070071 ir_instruction()
Ian Romanickd27ec242010-03-11 14:23:41 -080072 {
73 /* empty */
74 }
Ian Romanicka87ac252010-02-22 13:19:34 -080075};
76
77
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070078class ir_rvalue : public ir_instruction {
79public:
80 virtual ir_rvalue * as_rvalue()
81 {
82 return this;
83 }
84
85 virtual bool is_lvalue()
86 {
87 return false;
88 }
89
Ian Romanick2b3c4762010-05-14 17:35:42 -070090 /**
91 * Get the variable that is ultimately referenced by an r-value
92 */
93 virtual ir_variable *variable_referenced()
94 {
95 return NULL;
96 }
97
Ian Romanickb067db22010-05-26 11:32:52 -070098
99 /**
100 * If an r-value is a reference to a whole variable, get that variable
101 *
102 * \return
103 * Pointer to a variable that is completely dereferenced by the r-value. If
104 * the r-value is not a dereference or the dereference does not access the
105 * entire variable (i.e., it's just one array element, struct field), \c NULL
106 * is returned.
107 */
108 virtual ir_variable *whole_variable_referenced()
109 {
110 return NULL;
111 }
112
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700113protected:
Ian Romanickb427c912010-04-07 18:03:50 -0700114 ir_rvalue()
115 {
116 /* empty */
117 }
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700118};
119
120
Ian Romanicka87ac252010-02-22 13:19:34 -0800121enum ir_variable_mode {
122 ir_var_auto = 0,
123 ir_var_uniform,
124 ir_var_in,
125 ir_var_out,
126 ir_var_inout
127};
128
129enum ir_varaible_interpolation {
130 ir_var_smooth = 0,
131 ir_var_flat,
132 ir_var_noperspective
133};
134
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700135
Ian Romanicka87ac252010-02-22 13:19:34 -0800136class ir_variable : public ir_instruction {
137public:
138 ir_variable(const struct glsl_type *, const char *);
139
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700140 virtual ir_variable *as_variable()
141 {
142 return this;
143 }
144
Ian Romanick78b51b02010-03-09 16:23:37 -0800145 virtual void accept(ir_visitor *v)
146 {
147 v->visit(this);
148 }
149
Ian Romanick8895bae2010-05-14 12:39:23 -0700150 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
151
Ian Romanick2d394d42010-03-31 17:52:44 -0700152 /**
153 * Duplicate an IR variable
154 *
155 * \note
156 * This will probably be made \c virtual and moved to the base class
157 * eventually.
158 */
159 ir_variable *clone() const
160 {
161 ir_variable *var = new ir_variable(type, name);
162
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700163 var->max_array_access = this->max_array_access;
Ian Romanick2d394d42010-03-31 17:52:44 -0700164 var->read_only = this->read_only;
165 var->centroid = this->centroid;
166 var->invariant = this->invariant;
167 var->mode = this->mode;
168 var->interpolation = this->interpolation;
169
170 return var;
171 }
172
Ian Romanicka87ac252010-02-22 13:19:34 -0800173 const char *name;
174
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700175 /**
176 * Highest element accessed with a constant expression array index
177 *
178 * Not used for non-array variables.
179 */
180 unsigned max_array_access;
181
Ian Romanicka87ac252010-02-22 13:19:34 -0800182 unsigned read_only:1;
183 unsigned centroid:1;
184 unsigned invariant:1;
Eric Anholt71df19f2010-04-19 11:10:37 -0700185 /** If the variable is initialized outside of the scope of the shader */
186 unsigned shader_in:1;
187 /**
188 * If the variable value is later used outside of the scope of the shader.
189 */
190 unsigned shader_out:1;
Ian Romanicka87ac252010-02-22 13:19:34 -0800191
192 unsigned mode:3;
193 unsigned interpolation:2;
Ian Romanick9d975372010-04-02 17:17:47 -0700194
195 /**
196 * Flag that the whole array is assignable
197 *
198 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
199 * equality). This flag enables this behavior.
200 */
201 unsigned array_lvalue:1;
Eric Anholt326c6762010-04-06 10:30:54 -0700202
203 /**
Ian Romanickc178c742010-04-07 16:53:54 -0700204 * Emit a warning if this variable is accessed.
205 */
206 const char *warn_extension;
207
208 /**
Eric Anholt326c6762010-04-06 10:30:54 -0700209 * Value assigned in the initializer of a variable declared "const"
210 */
211 ir_constant *constant_value;
Ian Romanicka87ac252010-02-22 13:19:34 -0800212};
213
214
Ian Romanicka87ac252010-02-22 13:19:34 -0800215/*@{*/
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700216/**
217 * The representation of a function instance; may be the full definition or
218 * simply a prototype.
219 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800220class ir_function_signature : public ir_instruction {
Eric Anholt894ea972010-04-07 13:19:11 -0700221 /* An ir_function_signature will be part of the list of signatures in
222 * an ir_function.
223 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800224public:
Ian Romanicke39cc692010-03-23 12:19:13 -0700225 ir_function_signature(const glsl_type *return_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800226
Ian Romanick78b51b02010-03-09 16:23:37 -0800227 virtual void accept(ir_visitor *v)
228 {
229 v->visit(this);
230 }
231
Ian Romanick8895bae2010-05-14 12:39:23 -0700232 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
233
Ian Romanicka87ac252010-02-22 13:19:34 -0800234 /**
Ian Romanick0f0ea582010-03-31 16:44:12 -0700235 * Get the name of the function for which this is a signature
236 */
237 const char *function_name() const;
238
239 /**
Kenneth Graunkeabd40b12010-04-28 11:49:12 -0700240 * Check whether the qualifiers match between this signature's parameters
241 * and the supplied parameter list. If not, returns the name of the first
242 * parameter with mismatched qualifiers (for use in error messages).
243 */
244 const char *qualifiers_match(exec_list *params);
245
246 /**
Kenneth Graunkebff60132010-04-28 12:44:24 -0700247 * Replace the current parameter list with the given one. This is useful
248 * if the current information came from a prototype, and either has invalid
249 * or missing parameter names.
250 */
251 void replace_parameters(exec_list *new_params);
252
253 /**
Ian Romanicka87ac252010-02-22 13:19:34 -0800254 * Function return type.
255 *
256 * \note This discards the optional precision qualifier.
257 */
258 const struct glsl_type *return_type;
259
260 /**
Eric Anholtf1ddca92010-04-07 12:35:34 -0700261 * List of ir_variable of function parameters.
262 *
263 * This represents the storage. The paramaters passed in a particular
264 * call will be in ir_call::actual_paramaters.
Ian Romanicka87ac252010-02-22 13:19:34 -0800265 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800266 struct exec_list parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -0800267
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700268 /** Whether or not this function has a body (which may be empty). */
269 unsigned is_defined:1;
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700270
Eric Anholt894ea972010-04-07 13:19:11 -0700271 /** Body of instructions in the function. */
272 struct exec_list body;
273
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700274private:
275 /** Function of which this signature is one overload. */
276 class ir_function *function;
277
278 friend class ir_function;
Ian Romanicka87ac252010-02-22 13:19:34 -0800279};
280
281
282/**
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700283 * Header for tracking multiple overloaded functions with the same name.
284 * Contains a list of ir_function_signatures representing each of the
285 * actual functions.
Ian Romanicka87ac252010-02-22 13:19:34 -0800286 */
287class ir_function : public ir_instruction {
288public:
Ian Romanick882dad72010-03-23 17:42:04 -0700289 ir_function(const char *name);
Ian Romanicka87ac252010-02-22 13:19:34 -0800290
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700291 virtual ir_function *as_function()
292 {
293 return this;
294 }
295
Ian Romanick78b51b02010-03-09 16:23:37 -0800296 virtual void accept(ir_visitor *v)
297 {
298 v->visit(this);
299 }
300
Ian Romanick8895bae2010-05-14 12:39:23 -0700301 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
302
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700303 void add_signature(ir_function_signature *sig)
304 {
305 sig->function = this;
306 signatures.push_tail(sig);
307 }
308
Ian Romanicka87ac252010-02-22 13:19:34 -0800309 /**
Ian Romanick95cd6cc2010-03-31 16:40:26 -0700310 * Get an iterator for the set of function signatures
311 */
312 exec_list_iterator iterator()
313 {
314 return signatures.iterator();
315 }
316
317 /**
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700318 * Find a signature that matches a set of actual parameters, taking implicit
319 * conversions into account.
Ian Romanick471471f2010-03-11 14:50:30 -0800320 */
321 const ir_function_signature *matching_signature(exec_list *actual_param);
322
323 /**
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700324 * Find a signature that exactly matches a set of actual parameters without
325 * any implicit type conversions.
326 */
327 ir_function_signature *exact_matching_signature(exec_list *actual_ps);
328
329 /**
Ian Romanicka87ac252010-02-22 13:19:34 -0800330 * Name of the function.
331 */
332 const char *name;
333
Ian Romanicka4775822010-03-31 16:40:58 -0700334private:
Ian Romanick471471f2010-03-11 14:50:30 -0800335 /**
Eric Anholtf1ddca92010-04-07 12:35:34 -0700336 * List of ir_function_signature for each overloaded function with this name.
Ian Romanick471471f2010-03-11 14:50:30 -0800337 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800338 struct exec_list signatures;
Ian Romanicka87ac252010-02-22 13:19:34 -0800339};
Ian Romanick0f0ea582010-03-31 16:44:12 -0700340
341inline const char *ir_function_signature::function_name() const
342{
343 return function->name;
344}
Ian Romanicka87ac252010-02-22 13:19:34 -0800345/*@}*/
346
Ian Romanicka87ac252010-02-22 13:19:34 -0800347
Ian Romanick3c6fea32010-03-29 14:11:25 -0700348/**
349 * IR instruction representing high-level if-statements
350 */
351class ir_if : public ir_instruction {
352public:
353 ir_if(ir_rvalue *condition)
354 : condition(condition)
355 {
356 /* empty */
357 }
358
Eric Anholt5ba94202010-04-14 17:03:03 -0700359 virtual ir_if *as_if()
360 {
361 return this;
362 }
363
Ian Romanick3c6fea32010-03-29 14:11:25 -0700364 virtual void accept(ir_visitor *v)
365 {
366 v->visit(this);
367 }
368
Ian Romanick8895bae2010-05-14 12:39:23 -0700369 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
370
Ian Romanick3c6fea32010-03-29 14:11:25 -0700371 ir_rvalue *condition;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700372 /** List of ir_instruction for the body of the then branch */
Ian Romanick3c6fea32010-03-29 14:11:25 -0700373 exec_list then_instructions;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700374 /** List of ir_instruction for the body of the else branch */
Ian Romanick3c6fea32010-03-29 14:11:25 -0700375 exec_list else_instructions;
376};
377
378
Ian Romanickfad607a2010-04-05 16:16:07 -0700379/**
380 * IR instruction representing a high-level loop structure.
381 */
382class ir_loop : public ir_instruction {
383public:
384 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
385 {
386 /* empty */
387 }
388
389 virtual void accept(ir_visitor *v)
390 {
391 v->visit(this);
392 }
393
Ian Romanick8895bae2010-05-14 12:39:23 -0700394 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
395
Ian Romanick01f8de42010-04-05 17:13:14 -0700396 virtual ir_loop *as_loop()
397 {
398 return this;
399 }
400
Ian Romanickfad607a2010-04-05 16:16:07 -0700401 /**
402 * Get an iterator for the instructions of the loop body
403 */
404 exec_list_iterator iterator()
405 {
406 return body_instructions.iterator();
407 }
408
Eric Anholtf1ddca92010-04-07 12:35:34 -0700409 /** List of ir_instruction that make up the body of the loop. */
Ian Romanickfad607a2010-04-05 16:16:07 -0700410 exec_list body_instructions;
411
412 /**
413 * \name Loop counter and controls
414 */
415 /*@{*/
416 ir_rvalue *from;
417 ir_rvalue *to;
418 ir_rvalue *increment;
419 ir_variable *counter;
420 /*@}*/
421};
422
423
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700424class ir_assignment : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800425public:
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700426 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
Ian Romanicka87ac252010-02-22 13:19:34 -0800427
Ian Romanick78b51b02010-03-09 16:23:37 -0800428 virtual void accept(ir_visitor *v)
429 {
430 v->visit(this);
431 }
432
Ian Romanick8895bae2010-05-14 12:39:23 -0700433 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
434
Eric Anholtcad97662010-04-07 11:46:26 -0700435 virtual ir_assignment * as_assignment()
436 {
437 return this;
438 }
439
Ian Romanicka87ac252010-02-22 13:19:34 -0800440 /**
441 * Left-hand side of the assignment.
442 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700443 ir_rvalue *lhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800444
445 /**
446 * Value being assigned
Ian Romanicka87ac252010-02-22 13:19:34 -0800447 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700448 ir_rvalue *rhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800449
450 /**
451 * Optional condition for the assignment.
452 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700453 ir_rvalue *condition;
Ian Romanicka87ac252010-02-22 13:19:34 -0800454};
455
Kenneth Graunke3b969962010-04-07 17:18:29 -0700456/* Update ir_expression::num_operands() and operator_strs when
Eric Anholt160d0922010-04-01 18:07:08 -1000457 * updating this list.
Kenneth Graunke3b969962010-04-07 17:18:29 -0700458 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800459enum ir_expression_operation {
460 ir_unop_bit_not,
461 ir_unop_logic_not,
462 ir_unop_neg,
463 ir_unop_abs,
Kenneth Graunkea4b7b5a2010-05-03 20:05:57 -0700464 ir_unop_sign,
Ian Romanicka87ac252010-02-22 13:19:34 -0800465 ir_unop_rcp,
466 ir_unop_rsq,
Eric Anholt44d68fd2010-03-27 13:01:51 -0700467 ir_unop_sqrt,
Ian Romanicka87ac252010-02-22 13:19:34 -0800468 ir_unop_exp,
469 ir_unop_log,
Eric Anholt01665262010-03-27 13:56:35 -0700470 ir_unop_exp2,
471 ir_unop_log2,
Ian Romanicka87ac252010-02-22 13:19:34 -0800472 ir_unop_f2i, /**< Float-to-integer conversion. */
473 ir_unop_i2f, /**< Integer-to-float conversion. */
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000474 ir_unop_f2b, /**< Float-to-boolean conversion */
475 ir_unop_b2f, /**< Boolean-to-float conversion */
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000476 ir_unop_i2b, /**< int-to-boolean conversion */
477 ir_unop_b2i, /**< Boolean-to-int conversion */
Ian Romanick6c86ea82010-03-26 16:11:48 -0700478 ir_unop_u2f, /**< Unsigned-to-float conversion. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800479
480 /**
481 * \name Unary floating-point rounding operations.
482 */
483 /*@{*/
484 ir_unop_trunc,
485 ir_unop_ceil,
486 ir_unop_floor,
487 /*@}*/
488
Kenneth Graunke57e7da12010-05-03 22:11:17 -0700489 /**
490 * \name Trigonometric operations.
491 */
492 /*@{*/
493 ir_unop_sin,
494 ir_unop_cos,
495 /*@}*/
496
Kenneth Graunkeb843c7a2010-06-09 14:42:41 -0700497 /**
498 * \name Partial derivatives.
499 */
500 /*@{*/
501 ir_unop_dFdx,
502 ir_unop_dFdy,
503 /*@}*/
504
Ian Romanicka87ac252010-02-22 13:19:34 -0800505 ir_binop_add,
506 ir_binop_sub,
507 ir_binop_mul,
508 ir_binop_div,
509 ir_binop_mod,
510
511 /**
512 * \name Binary comparison operators
513 */
514 /*@{*/
515 ir_binop_less,
516 ir_binop_greater,
517 ir_binop_lequal,
518 ir_binop_gequal,
519 ir_binop_equal,
520 ir_binop_nequal,
521 /*@}*/
522
523 /**
524 * \name Bit-wise binary operations.
525 */
526 /*@{*/
527 ir_binop_lshift,
528 ir_binop_rshift,
529 ir_binop_bit_and,
530 ir_binop_bit_xor,
531 ir_binop_bit_or,
532 /*@}*/
533
534 ir_binop_logic_and,
535 ir_binop_logic_xor,
536 ir_binop_logic_or,
Ian Romanicka87ac252010-02-22 13:19:34 -0800537
538 ir_binop_dot,
539 ir_binop_min,
540 ir_binop_max,
541
542 ir_binop_pow
543};
544
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700545class ir_expression : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800546public:
547 ir_expression(int op, const struct glsl_type *type,
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700548 ir_rvalue *, ir_rvalue *);
Ian Romanicka87ac252010-02-22 13:19:34 -0800549
Kenneth Graunke7dd6adb2010-04-07 16:56:57 -0700550 static unsigned int get_num_operands(ir_expression_operation);
551 unsigned int get_num_operands()
552 {
553 return get_num_operands(operation);
554 }
Eric Anholt160d0922010-04-01 18:07:08 -1000555
Kenneth Graunke3b969962010-04-07 17:18:29 -0700556 /**
557 * Return a string representing this expression's operator.
558 */
559 const char *operator_string();
560
561 /**
562 * Do a reverse-lookup to translate the given string into an operator.
563 */
564 static ir_expression_operation get_operator(const char *);
565
Ian Romanick78b51b02010-03-09 16:23:37 -0800566 virtual void accept(ir_visitor *v)
567 {
568 v->visit(this);
569 }
570
Ian Romanick8895bae2010-05-14 12:39:23 -0700571 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
572
Eric Anholtcad97662010-04-07 11:46:26 -0700573 ir_expression *clone();
574
Ian Romanicka87ac252010-02-22 13:19:34 -0800575 ir_expression_operation operation;
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700576 ir_rvalue *operands[2];
Ian Romanicka87ac252010-02-22 13:19:34 -0800577};
578
579
Ian Romanicked45ec62010-03-11 14:34:27 -0800580/**
581 * IR instruction representing a function call
582 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700583class ir_call : public ir_rvalue {
Ian Romanicked45ec62010-03-11 14:34:27 -0800584public:
Ian Romanick471471f2010-03-11 14:50:30 -0800585 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
Ian Romanickb427c912010-04-07 18:03:50 -0700586 : callee(callee)
Ian Romanicked45ec62010-03-11 14:34:27 -0800587 {
Ian Romanick9e7c34b2010-03-23 12:21:18 -0700588 assert(callee->return_type != NULL);
589 type = callee->return_type;
Ian Romanick471471f2010-03-11 14:50:30 -0800590 actual_parameters->move_nodes_to(& this->actual_parameters);
Ian Romanicked45ec62010-03-11 14:34:27 -0800591 }
592
Eric Anholtcad97662010-04-07 11:46:26 -0700593 virtual ir_call *as_call()
594 {
595 return this;
596 }
597
Ian Romanicked45ec62010-03-11 14:34:27 -0800598 virtual void accept(ir_visitor *v)
599 {
600 v->visit(this);
601 }
602
Ian Romanick8895bae2010-05-14 12:39:23 -0700603 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
604
Ian Romanicked45ec62010-03-11 14:34:27 -0800605 /**
606 * Get a generic ir_call object when an error occurs
607 */
608 static ir_call *get_error_instruction();
609
Ian Romanick9878c652010-03-26 17:19:47 -0700610 /**
611 * Get an iterator for the set of acutal parameters
612 */
613 exec_list_iterator iterator()
614 {
615 return actual_parameters.iterator();
616 }
617
Ian Romanick93614bc2010-03-26 17:29:29 -0700618 /**
619 * Get the name of the function being called.
620 */
621 const char *callee_name() const
622 {
Ian Romanick0f0ea582010-03-31 16:44:12 -0700623 return callee->function_name();
Ian Romanick93614bc2010-03-26 17:29:29 -0700624 }
625
Eric Anholtcad97662010-04-07 11:46:26 -0700626 const ir_function_signature *get_callee()
627 {
628 return callee;
629 }
630
631 /**
632 * Generates an inline version of the function before @ir,
633 * returning the return value of the function.
634 */
635 ir_rvalue *generate_inline(ir_instruction *ir);
636
Ian Romanicked45ec62010-03-11 14:34:27 -0800637private:
Ian Romanick471471f2010-03-11 14:50:30 -0800638 ir_call()
Ian Romanickb427c912010-04-07 18:03:50 -0700639 : callee(NULL)
Ian Romanick471471f2010-03-11 14:50:30 -0800640 {
641 /* empty */
642 }
643
644 const ir_function_signature *callee;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700645
646 /* List of ir_rvalue of paramaters passed in this call. */
Ian Romanicked45ec62010-03-11 14:34:27 -0800647 exec_list actual_parameters;
648};
649
650
Ian Romanick9578c872010-03-19 16:44:52 -0700651/**
652 * \name Jump-like IR instructions.
653 *
654 * These include \c break, \c continue, \c return, and \c discard.
655 */
656/*@{*/
657class ir_jump : public ir_instruction {
658protected:
659 ir_jump()
Ian Romanick9578c872010-03-19 16:44:52 -0700660 {
661 /* empty */
662 }
663};
664
665class ir_return : public ir_jump {
666public:
667 ir_return()
668 : value(NULL)
669 {
670 /* empty */
671 }
672
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700673 ir_return(ir_rvalue *value)
Ian Romanick9578c872010-03-19 16:44:52 -0700674 : value(value)
675 {
676 /* empty */
677 }
678
Eric Anholtcad97662010-04-07 11:46:26 -0700679 virtual ir_return *as_return()
680 {
681 return this;
682 }
683
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700684 ir_rvalue *get_value() const
Ian Romanick9578c872010-03-19 16:44:52 -0700685 {
686 return value;
687 }
688
689 virtual void accept(ir_visitor *v)
690 {
691 v->visit(this);
692 }
693
Ian Romanick8895bae2010-05-14 12:39:23 -0700694 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
695
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700696 ir_rvalue *value;
Ian Romanick9578c872010-03-19 16:44:52 -0700697};
Ian Romanickf8e31e02010-04-05 16:28:15 -0700698
699
700/**
701 * Jump instructions used inside loops
702 *
703 * These include \c break and \c continue. The \c break within a loop is
704 * different from the \c break within a switch-statement.
705 *
706 * \sa ir_switch_jump
707 */
708class ir_loop_jump : public ir_jump {
709public:
710 enum jump_mode {
711 jump_break,
712 jump_continue
713 };
714
715 ir_loop_jump(ir_loop *loop, jump_mode mode)
716 : loop(loop), mode(mode)
717 {
718 /* empty */
719 }
720
721 virtual void accept(ir_visitor *v)
722 {
723 v->visit(this);
724 }
725
Ian Romanick8895bae2010-05-14 12:39:23 -0700726 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
727
Ian Romanickf8e31e02010-04-05 16:28:15 -0700728 bool is_break() const
729 {
730 return mode == jump_break;
731 }
732
733 bool is_continue() const
734 {
735 return mode == jump_continue;
736 }
737
738private:
739 /** Loop containing this break instruction. */
740 ir_loop *loop;
741
742 /** Mode selector for the jump instruction. */
743 enum jump_mode mode;
744};
Ian Romanick9578c872010-03-19 16:44:52 -0700745/*@}*/
746
747
Ian Romanick81377c02010-04-28 18:42:36 -0700748/**
749 * Texture sampling opcodes used in ir_texture
750 */
751enum ir_texture_opcode {
752 ir_tex, /* Regular texture look-up */
753 ir_txb, /* Texture look-up with LOD bias */
754 ir_txl, /* Texture look-up with explicit LOD */
755 ir_txd, /* Texture look-up with partial derivatvies */
756 ir_txf /* Texel fetch with explicit LOD */
757};
758
759
760/**
761 * IR instruction to sample a texture
762 *
763 * The specific form of the IR instruction depends on the \c mode value
764 * selected from \c ir_texture_opcodes. In the printed IR, these will
765 * appear as:
766 *
767 * Texel offset
768 * | Projection divisor
769 * | | Shadow comparitor
770 * | | |
771 * v v v
772 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
773 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
774 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
775 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
776 * (txf (sampler) (coordinate) (0 0 0) (lod))
777 */
778class ir_texture : public ir_rvalue {
779public:
780 ir_texture(enum ir_texture_opcode op)
Kenneth Graunkeb97efa52010-06-09 11:07:53 -0700781 : op(op), projector(NULL), shadow_comparitor(NULL)
Ian Romanick81377c02010-04-28 18:42:36 -0700782 {
783 /* empty */
784 }
785
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700786 virtual void accept(ir_visitor *v)
787 {
788 v->visit(this);
789 }
790
791 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
792
Kenneth Graunkec30f6e52010-05-26 16:41:47 -0700793 /**
794 * Return a string representing the ir_texture_opcode.
795 */
796 const char *opcode_string();
797
Kenneth Graunke56d3f6a2010-06-03 15:07:34 -0700798 /** Set the sampler and infer the type. */
799 void set_sampler(ir_dereference *sampler);
800
Kenneth Graunkec30f6e52010-05-26 16:41:47 -0700801 /**
802 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
803 */
804 static ir_texture_opcode get_opcode(const char *);
805
Ian Romanick81377c02010-04-28 18:42:36 -0700806 enum ir_texture_opcode op;
807
808 /** Sampler to use for the texture access. */
809 ir_dereference *sampler;
810
811 /** Texture coordinate to sample */
812 ir_rvalue *coordinate;
813
814 /**
815 * Value used for projective divide.
816 *
817 * If there is no projective divide (the common case), this will be
818 * \c NULL. Optimization passes should check for this to point to a constant
819 * of 1.0 and replace that with \c NULL.
820 */
821 ir_rvalue *projector;
822
823 /**
824 * Coordinate used for comparison on shadow look-ups.
825 *
826 * If there is no shadow comparison, this will be \c NULL. For the
827 * \c ir_txf opcode, this *must* be \c NULL.
828 */
829 ir_rvalue *shadow_comparitor;
830
831 /** Explicit texel offsets. */
832 signed char offsets[3];
833
834 union {
835 ir_rvalue *lod; /**< Floating point LOD */
836 ir_rvalue *bias; /**< Floating point LOD bias */
837 struct {
838 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
839 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
840 } grad;
841 } lod_info;
842};
843
844
Ian Romanicka87ac252010-02-22 13:19:34 -0800845struct ir_swizzle_mask {
846 unsigned x:2;
847 unsigned y:2;
848 unsigned z:2;
849 unsigned w:2;
850
851 /**
852 * Number of components in the swizzle.
853 */
Kenneth Graunkef25a5ad2010-03-25 11:22:42 -0700854 unsigned num_components:3;
Ian Romanicka87ac252010-02-22 13:19:34 -0800855
856 /**
857 * Does the swizzle contain duplicate components?
858 *
859 * L-value swizzles cannot contain duplicate components.
860 */
861 unsigned has_duplicates:1;
862};
863
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700864
865class ir_swizzle : public ir_rvalue {
866public:
867 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
868 unsigned count);
Eric Anholt05a4e592010-05-03 17:08:01 -0700869 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
Eric Anholtcad97662010-04-07 11:46:26 -0700870
Eric Anholt7d211042010-04-16 16:43:47 -0700871 virtual ir_swizzle *as_swizzle()
872 {
873 return this;
874 }
875
Eric Anholtcad97662010-04-07 11:46:26 -0700876 ir_swizzle *clone()
877 {
878 return new ir_swizzle(this->val, this->mask);
879 }
880
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700881 /**
882 * Construct an ir_swizzle from the textual representation. Can fail.
883 */
884 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
885
886 virtual void accept(ir_visitor *v)
887 {
888 v->visit(this);
889 }
890
Ian Romanick8895bae2010-05-14 12:39:23 -0700891 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
892
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700893 bool is_lvalue()
894 {
Eric Anholta9fafc62010-03-28 01:29:18 -0700895 return val->is_lvalue() && !mask.has_duplicates;
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700896 }
897
Ian Romanick2b3c4762010-05-14 17:35:42 -0700898 /**
899 * Get the variable that is ultimately referenced by an r-value
900 */
901 virtual ir_variable *variable_referenced();
902
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700903 ir_rvalue *val;
904 ir_swizzle_mask mask;
905};
906
907
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700908class ir_dereference : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800909public:
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700910 virtual ir_dereference *as_dereference()
911 {
912 return this;
913 }
914
Eric Anholtc7da28b2010-04-01 20:27:35 -1000915 bool is_lvalue();
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700916
Ian Romanick2b3c4762010-05-14 17:35:42 -0700917 /**
918 * Get the variable that is ultimately referenced by an r-value
919 */
Ian Romanick70fe8b62010-05-19 11:37:35 +0200920 virtual ir_variable *variable_referenced() = 0;
Ian Romanick70fe8b62010-05-19 11:37:35 +0200921};
922
923
924class ir_dereference_variable : public ir_dereference {
925public:
926 ir_dereference_variable(ir_variable *var);
927
928 /**
929 * Get the variable that is ultimately referenced by an r-value
930 */
931 virtual ir_variable *variable_referenced()
932 {
Ian Romanick36ea2862010-05-19 13:52:29 +0200933 return this->var;
Ian Romanick70fe8b62010-05-19 11:37:35 +0200934 }
Ian Romanickf3a002b2010-05-19 12:02:19 +0200935
Ian Romanickb067db22010-05-26 11:32:52 -0700936 virtual ir_variable *whole_variable_referenced()
937 {
938 /* ir_dereference_variable objects always dereference the entire
939 * variable. However, if this dereference is dereferenced by anything
940 * else, the complete deferefernce chain is not a whole-variable
941 * dereference. This method should only be called on the top most
942 * ir_rvalue in a dereference chain.
943 */
944 return this->var;
945 }
946
Ian Romanickc7b10462010-05-19 13:20:12 +0200947 virtual void accept(ir_visitor *v)
948 {
949 v->visit(this);
950 }
951
Ian Romanickf3a002b2010-05-19 12:02:19 +0200952 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
Ian Romanick36ea2862010-05-19 13:52:29 +0200953
954 /**
955 * Object being dereferenced.
956 */
957 ir_variable *var;
Ian Romanick70fe8b62010-05-19 11:37:35 +0200958};
959
960
961class ir_dereference_array : public ir_dereference {
962public:
963 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
964
965 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
966
Eric Anholtb145e902010-05-11 11:31:09 -0700967 virtual ir_dereference_array *as_dereference_array()
968 {
969 return this;
970 }
971
Ian Romanick70fe8b62010-05-19 11:37:35 +0200972 /**
973 * Get the variable that is ultimately referenced by an r-value
974 */
975 virtual ir_variable *variable_referenced()
976 {
Ian Romanick36ea2862010-05-19 13:52:29 +0200977 return this->array->variable_referenced();
Ian Romanick70fe8b62010-05-19 11:37:35 +0200978 }
979
Ian Romanickc7b10462010-05-19 13:20:12 +0200980 virtual void accept(ir_visitor *v)
981 {
982 v->visit(this);
983 }
984
Ian Romanickf3a002b2010-05-19 12:02:19 +0200985 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200986
Ian Romanick36ea2862010-05-19 13:52:29 +0200987 ir_rvalue *array;
988 ir_rvalue *array_index;
989
Ian Romanick70fe8b62010-05-19 11:37:35 +0200990private:
991 void set_array(ir_rvalue *value);
992};
993
994
995class ir_dereference_record : public ir_dereference {
996public:
997 ir_dereference_record(ir_rvalue *value, const char *field);
998
999 ir_dereference_record(ir_variable *var, const char *field);
1000
1001 /**
1002 * Get the variable that is ultimately referenced by an r-value
1003 */
1004 virtual ir_variable *variable_referenced()
1005 {
Ian Romanick36ea2862010-05-19 13:52:29 +02001006 return this->record->variable_referenced();
Ian Romanick70fe8b62010-05-19 11:37:35 +02001007 }
Ian Romanickf3a002b2010-05-19 12:02:19 +02001008
Ian Romanickc7b10462010-05-19 13:20:12 +02001009 virtual void accept(ir_visitor *v)
1010 {
1011 v->visit(this);
1012 }
1013
Ian Romanickf3a002b2010-05-19 12:02:19 +02001014 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
Ian Romanick36ea2862010-05-19 13:52:29 +02001015
1016 ir_rvalue *record;
1017 const char *field;
Ian Romanicka87ac252010-02-22 13:19:34 -08001018};
1019
1020
Ian Romanickbe1d2bf2010-06-11 14:01:44 -07001021/**
1022 * Data stored in an ir_constant
1023 */
1024union ir_constant_data {
1025 unsigned u[16];
1026 int i[16];
1027 float f[16];
1028 bool b[16];
1029};
1030
1031
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001032class ir_constant : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -08001033public:
Ian Romanick824b6592010-06-11 16:57:47 -07001034 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
Eric Anholt3c36b2d2010-03-26 12:07:44 -07001035 ir_constant(bool b);
1036 ir_constant(unsigned int u);
1037 ir_constant(int i);
1038 ir_constant(float f);
Ian Romanicka87ac252010-02-22 13:19:34 -08001039
Ian Romanick989cfc42010-06-04 16:13:35 -07001040 /**
Ian Romanick756a3fa2010-06-04 16:34:38 -07001041 * Construct an ir_constant from a list of ir_constant values
1042 */
1043 ir_constant(const struct glsl_type *type, exec_list *values);
1044
1045 /**
Ian Romanick989cfc42010-06-04 16:13:35 -07001046 * Construct an ir_constant from a scalar component of another ir_constant
1047 *
1048 * The new \c ir_constant inherits the type of the component from the
1049 * source constant.
1050 *
1051 * \note
1052 * In the case of a matrix constant, the new constant is a scalar, \b not
1053 * a vector.
1054 */
1055 ir_constant(const ir_constant *c, unsigned i);
1056
Eric Anholt5c89f0e2010-05-04 13:04:40 -07001057 virtual ir_constant *as_constant()
1058 {
1059 return this;
1060 }
1061
Ian Romanick78b51b02010-03-09 16:23:37 -08001062 virtual void accept(ir_visitor *v)
1063 {
1064 v->visit(this);
1065 }
1066
Ian Romanick8895bae2010-05-14 12:39:23 -07001067 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1068
Ian Romanick710919f2010-06-09 17:18:04 -07001069 ir_constant *clone();
Eric Anholtcad97662010-04-07 11:46:26 -07001070
Ian Romanicka87ac252010-02-22 13:19:34 -08001071 /**
Ian Romanick31881902010-06-04 16:30:07 -07001072 * Get a particular component of a constant as a specific type
1073 *
1074 * This is useful, for example, to get a value from an integer constant
1075 * as a float or bool. This appears frequently when constructors are
1076 * called with all constant parameters.
1077 */
1078 /*@{*/
1079 bool get_bool_component(unsigned i) const;
1080 float get_float_component(unsigned i) const;
1081 int get_int_component(unsigned i) const;
1082 unsigned get_uint_component(unsigned i) const;
1083 /*@}*/
1084
Ian Romanickb94c29a2010-06-09 17:28:54 -07001085 ir_constant *get_record_field(const char *name);
1086
Ian Romanick31881902010-06-04 16:30:07 -07001087 /**
Ian Romanicka87ac252010-02-22 13:19:34 -08001088 * Value of the constant.
1089 *
1090 * The field used to back the values supplied by the constant is determined
1091 * by the type associated with the \c ir_instruction. Constants may be
1092 * scalars, vectors, or matrices.
1093 */
Ian Romanickbe1d2bf2010-06-11 14:01:44 -07001094 union ir_constant_data value;
Ian Romanick7f1ab832010-06-09 17:11:50 -07001095
1096 exec_list components;
Ian Romanick710919f2010-06-09 17:18:04 -07001097
1098private:
1099 /**
1100 * Parameterless constructor only used by the clone method
1101 */
1102 ir_constant(void);
Ian Romanicka87ac252010-02-22 13:19:34 -08001103};
1104
Eric Anholt70b74922010-04-06 11:52:09 -07001105void
1106visit_exec_list(exec_list *list, ir_visitor *visitor);
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001107
1108extern void
1109_mesa_glsl_initialize_variables(exec_list *instructions,
1110 struct _mesa_glsl_parse_state *state);
Ian Romanicke309a602010-03-15 15:20:15 -07001111
Eric Anholtc22c4002010-03-26 18:20:30 -07001112extern void
1113_mesa_glsl_initialize_functions(exec_list *instructions,
1114 struct _mesa_glsl_parse_state *state);
1115
Ian Romanicke309a602010-03-15 15:20:15 -07001116#endif /* IR_H */