blob: d8568a8dae7eb9309bab5e8ce7a191a0ab0a956d [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 Romanick0044e7e2010-03-08 23:44:00 -080031
Ian Romanicka87ac252010-02-22 13:19:34 -080032struct ir_program {
33 void *bong_hits;
34};
35
Ian Romanicka87ac252010-02-22 13:19:34 -080036/**
37 * Base class of all IR instructions
38 */
Ian Romanick0044e7e2010-03-08 23:44:00 -080039class ir_instruction : public exec_node {
Ian Romanicka87ac252010-02-22 13:19:34 -080040public:
Ian Romanicka87ac252010-02-22 13:19:34 -080041 const struct glsl_type *type;
42
Ian Romanick1cf43a42010-03-30 16:56:50 -070043 class ir_constant *constant_expression_value();
Ian Romanick78b51b02010-03-09 16:23:37 -080044 virtual void accept(ir_visitor *) = 0;
45
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070046 /**
47 * \name IR instruction downcast functions
48 *
49 * These functions either cast the object to a derived class or return
50 * \c NULL if the object's type does not match the specified derived class.
51 * Additional downcast functions will be added as needed.
52 */
53 /*@{*/
54 virtual class ir_variable * as_variable() { return NULL; }
Kenneth Graunke6202cbf2010-04-21 16:02:15 -070055 virtual class ir_function * as_function() { return NULL; }
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070056 virtual class ir_dereference * as_dereference() { return NULL; }
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070057 virtual class ir_rvalue * as_rvalue() { return NULL; }
Eric Anholt7d211042010-04-16 16:43:47 -070058 virtual class ir_label * as_label() { return NULL; }
Ian Romanick01f8de42010-04-05 17:13:14 -070059 virtual class ir_loop * as_loop() { return NULL; }
Eric Anholtcad97662010-04-07 11:46:26 -070060 virtual class ir_assignment * as_assignment() { return NULL; }
61 virtual class ir_call * as_call() { return NULL; }
62 virtual class ir_return * as_return() { return NULL; }
Eric Anholt5ba94202010-04-14 17:03:03 -070063 virtual class ir_if * as_if() { return NULL; }
Eric Anholt7d211042010-04-16 16:43:47 -070064 virtual class ir_swizzle * as_swizzle() { return NULL; }
Eric Anholt5c89f0e2010-05-04 13:04:40 -070065 virtual class ir_constant * as_constant() { return NULL; }
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070066 /*@}*/
67
Ian Romanicka87ac252010-02-22 13:19:34 -080068protected:
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070069 ir_instruction()
Ian Romanickd27ec242010-03-11 14:23:41 -080070 {
71 /* empty */
72 }
Ian Romanicka87ac252010-02-22 13:19:34 -080073};
74
75
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070076class ir_rvalue : public ir_instruction {
77public:
78 virtual ir_rvalue * as_rvalue()
79 {
80 return this;
81 }
82
83 virtual bool is_lvalue()
84 {
85 return false;
86 }
87
88protected:
Ian Romanickb427c912010-04-07 18:03:50 -070089 ir_rvalue()
90 {
91 /* empty */
92 }
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070093};
94
95
Ian Romanicka87ac252010-02-22 13:19:34 -080096enum ir_variable_mode {
97 ir_var_auto = 0,
98 ir_var_uniform,
99 ir_var_in,
100 ir_var_out,
101 ir_var_inout
102};
103
104enum ir_varaible_interpolation {
105 ir_var_smooth = 0,
106 ir_var_flat,
107 ir_var_noperspective
108};
109
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700110
Ian Romanicka87ac252010-02-22 13:19:34 -0800111class ir_variable : public ir_instruction {
112public:
113 ir_variable(const struct glsl_type *, const char *);
114
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700115 virtual ir_variable *as_variable()
116 {
117 return this;
118 }
119
Ian Romanick78b51b02010-03-09 16:23:37 -0800120 virtual void accept(ir_visitor *v)
121 {
122 v->visit(this);
123 }
124
Ian Romanick2d394d42010-03-31 17:52:44 -0700125 /**
126 * Duplicate an IR variable
127 *
128 * \note
129 * This will probably be made \c virtual and moved to the base class
130 * eventually.
131 */
132 ir_variable *clone() const
133 {
134 ir_variable *var = new ir_variable(type, name);
135
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700136 var->max_array_access = this->max_array_access;
Ian Romanick2d394d42010-03-31 17:52:44 -0700137 var->read_only = this->read_only;
138 var->centroid = this->centroid;
139 var->invariant = this->invariant;
140 var->mode = this->mode;
141 var->interpolation = this->interpolation;
142
143 return var;
144 }
145
Ian Romanicka87ac252010-02-22 13:19:34 -0800146 const char *name;
147
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700148 /**
149 * Highest element accessed with a constant expression array index
150 *
151 * Not used for non-array variables.
152 */
153 unsigned max_array_access;
154
Ian Romanicka87ac252010-02-22 13:19:34 -0800155 unsigned read_only:1;
156 unsigned centroid:1;
157 unsigned invariant:1;
Eric Anholt71df19f2010-04-19 11:10:37 -0700158 /** If the variable is initialized outside of the scope of the shader */
159 unsigned shader_in:1;
160 /**
161 * If the variable value is later used outside of the scope of the shader.
162 */
163 unsigned shader_out:1;
Ian Romanicka87ac252010-02-22 13:19:34 -0800164
165 unsigned mode:3;
166 unsigned interpolation:2;
Ian Romanick9d975372010-04-02 17:17:47 -0700167
168 /**
169 * Flag that the whole array is assignable
170 *
171 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
172 * equality). This flag enables this behavior.
173 */
174 unsigned array_lvalue:1;
Eric Anholt326c6762010-04-06 10:30:54 -0700175
176 /**
Ian Romanickc178c742010-04-07 16:53:54 -0700177 * Emit a warning if this variable is accessed.
178 */
179 const char *warn_extension;
180
181 /**
Eric Anholt326c6762010-04-06 10:30:54 -0700182 * Value assigned in the initializer of a variable declared "const"
183 */
184 ir_constant *constant_value;
Ian Romanicka87ac252010-02-22 13:19:34 -0800185};
186
187
Ian Romanicka87ac252010-02-22 13:19:34 -0800188/*@{*/
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700189/**
190 * The representation of a function instance; may be the full definition or
191 * simply a prototype.
192 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800193class ir_function_signature : public ir_instruction {
Eric Anholt894ea972010-04-07 13:19:11 -0700194 /* An ir_function_signature will be part of the list of signatures in
195 * an ir_function.
196 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800197public:
Ian Romanicke39cc692010-03-23 12:19:13 -0700198 ir_function_signature(const glsl_type *return_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800199
Ian Romanick78b51b02010-03-09 16:23:37 -0800200 virtual void accept(ir_visitor *v)
201 {
202 v->visit(this);
203 }
204
Ian Romanicka87ac252010-02-22 13:19:34 -0800205 /**
Ian Romanick0f0ea582010-03-31 16:44:12 -0700206 * Get the name of the function for which this is a signature
207 */
208 const char *function_name() const;
209
210 /**
Kenneth Graunkeabd40b12010-04-28 11:49:12 -0700211 * Check whether the qualifiers match between this signature's parameters
212 * and the supplied parameter list. If not, returns the name of the first
213 * parameter with mismatched qualifiers (for use in error messages).
214 */
215 const char *qualifiers_match(exec_list *params);
216
217 /**
Kenneth Graunkebff60132010-04-28 12:44:24 -0700218 * Replace the current parameter list with the given one. This is useful
219 * if the current information came from a prototype, and either has invalid
220 * or missing parameter names.
221 */
222 void replace_parameters(exec_list *new_params);
223
224 /**
Ian Romanicka87ac252010-02-22 13:19:34 -0800225 * Function return type.
226 *
227 * \note This discards the optional precision qualifier.
228 */
229 const struct glsl_type *return_type;
230
231 /**
Eric Anholtf1ddca92010-04-07 12:35:34 -0700232 * List of ir_variable of function parameters.
233 *
234 * This represents the storage. The paramaters passed in a particular
235 * call will be in ir_call::actual_paramaters.
Ian Romanicka87ac252010-02-22 13:19:34 -0800236 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800237 struct exec_list parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -0800238
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700239 /** Whether or not this function has a body (which may be empty). */
240 unsigned is_defined:1;
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700241
Eric Anholt894ea972010-04-07 13:19:11 -0700242 /** Body of instructions in the function. */
243 struct exec_list body;
244
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700245private:
246 /** Function of which this signature is one overload. */
247 class ir_function *function;
248
249 friend class ir_function;
Ian Romanicka87ac252010-02-22 13:19:34 -0800250};
251
252
253/**
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700254 * Header for tracking multiple overloaded functions with the same name.
255 * Contains a list of ir_function_signatures representing each of the
256 * actual functions.
Ian Romanicka87ac252010-02-22 13:19:34 -0800257 */
258class ir_function : public ir_instruction {
259public:
Ian Romanick882dad72010-03-23 17:42:04 -0700260 ir_function(const char *name);
Ian Romanicka87ac252010-02-22 13:19:34 -0800261
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700262 virtual ir_function *as_function()
263 {
264 return this;
265 }
266
Ian Romanick78b51b02010-03-09 16:23:37 -0800267 virtual void accept(ir_visitor *v)
268 {
269 v->visit(this);
270 }
271
Ian Romanick6a15d5b2010-03-31 16:37:10 -0700272 void add_signature(ir_function_signature *sig)
273 {
274 sig->function = this;
275 signatures.push_tail(sig);
276 }
277
Ian Romanicka87ac252010-02-22 13:19:34 -0800278 /**
Ian Romanick95cd6cc2010-03-31 16:40:26 -0700279 * Get an iterator for the set of function signatures
280 */
281 exec_list_iterator iterator()
282 {
283 return signatures.iterator();
284 }
285
286 /**
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700287 * Find a signature that matches a set of actual parameters, taking implicit
288 * conversions into account.
Ian Romanick471471f2010-03-11 14:50:30 -0800289 */
290 const ir_function_signature *matching_signature(exec_list *actual_param);
291
292 /**
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700293 * Find a signature that exactly matches a set of actual parameters without
294 * any implicit type conversions.
295 */
296 ir_function_signature *exact_matching_signature(exec_list *actual_ps);
297
298 /**
Ian Romanicka87ac252010-02-22 13:19:34 -0800299 * Name of the function.
300 */
301 const char *name;
302
Ian Romanicka4775822010-03-31 16:40:58 -0700303private:
Ian Romanick471471f2010-03-11 14:50:30 -0800304 /**
Eric Anholtf1ddca92010-04-07 12:35:34 -0700305 * List of ir_function_signature for each overloaded function with this name.
Ian Romanick471471f2010-03-11 14:50:30 -0800306 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800307 struct exec_list signatures;
Ian Romanicka87ac252010-02-22 13:19:34 -0800308};
Ian Romanick0f0ea582010-03-31 16:44:12 -0700309
310inline const char *ir_function_signature::function_name() const
311{
312 return function->name;
313}
Ian Romanicka87ac252010-02-22 13:19:34 -0800314/*@}*/
315
Ian Romanicka87ac252010-02-22 13:19:34 -0800316
Ian Romanick3c6fea32010-03-29 14:11:25 -0700317/**
318 * IR instruction representing high-level if-statements
319 */
320class ir_if : public ir_instruction {
321public:
322 ir_if(ir_rvalue *condition)
323 : condition(condition)
324 {
325 /* empty */
326 }
327
Eric Anholt5ba94202010-04-14 17:03:03 -0700328 virtual ir_if *as_if()
329 {
330 return this;
331 }
332
Ian Romanick3c6fea32010-03-29 14:11:25 -0700333 virtual void accept(ir_visitor *v)
334 {
335 v->visit(this);
336 }
337
338 ir_rvalue *condition;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700339 /** List of ir_instruction for the body of the then branch */
Ian Romanick3c6fea32010-03-29 14:11:25 -0700340 exec_list then_instructions;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700341 /** List of ir_instruction for the body of the else branch */
Ian Romanick3c6fea32010-03-29 14:11:25 -0700342 exec_list else_instructions;
343};
344
345
Ian Romanickfad607a2010-04-05 16:16:07 -0700346/**
347 * IR instruction representing a high-level loop structure.
348 */
349class ir_loop : public ir_instruction {
350public:
351 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
352 {
353 /* empty */
354 }
355
356 virtual void accept(ir_visitor *v)
357 {
358 v->visit(this);
359 }
360
Ian Romanick01f8de42010-04-05 17:13:14 -0700361 virtual ir_loop *as_loop()
362 {
363 return this;
364 }
365
Ian Romanickfad607a2010-04-05 16:16:07 -0700366 /**
367 * Get an iterator for the instructions of the loop body
368 */
369 exec_list_iterator iterator()
370 {
371 return body_instructions.iterator();
372 }
373
Eric Anholtf1ddca92010-04-07 12:35:34 -0700374 /** List of ir_instruction that make up the body of the loop. */
Ian Romanickfad607a2010-04-05 16:16:07 -0700375 exec_list body_instructions;
376
377 /**
378 * \name Loop counter and controls
379 */
380 /*@{*/
381 ir_rvalue *from;
382 ir_rvalue *to;
383 ir_rvalue *increment;
384 ir_variable *counter;
385 /*@}*/
386};
387
388
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700389class ir_assignment : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800390public:
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700391 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
Ian Romanicka87ac252010-02-22 13:19:34 -0800392
Ian Romanick78b51b02010-03-09 16:23:37 -0800393 virtual void accept(ir_visitor *v)
394 {
395 v->visit(this);
396 }
397
Eric Anholtcad97662010-04-07 11:46:26 -0700398 virtual ir_assignment * as_assignment()
399 {
400 return this;
401 }
402
Ian Romanicka87ac252010-02-22 13:19:34 -0800403 /**
404 * Left-hand side of the assignment.
405 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700406 ir_rvalue *lhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800407
408 /**
409 * Value being assigned
Ian Romanicka87ac252010-02-22 13:19:34 -0800410 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700411 ir_rvalue *rhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800412
413 /**
414 * Optional condition for the assignment.
415 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700416 ir_rvalue *condition;
Ian Romanicka87ac252010-02-22 13:19:34 -0800417};
418
Kenneth Graunke3b969962010-04-07 17:18:29 -0700419/* Update ir_expression::num_operands() and operator_strs when
Eric Anholt160d0922010-04-01 18:07:08 -1000420 * updating this list.
Kenneth Graunke3b969962010-04-07 17:18:29 -0700421 */
Ian Romanicka87ac252010-02-22 13:19:34 -0800422enum ir_expression_operation {
423 ir_unop_bit_not,
424 ir_unop_logic_not,
425 ir_unop_neg,
426 ir_unop_abs,
427 ir_unop_rcp,
428 ir_unop_rsq,
Eric Anholt44d68fd2010-03-27 13:01:51 -0700429 ir_unop_sqrt,
Ian Romanicka87ac252010-02-22 13:19:34 -0800430 ir_unop_exp,
431 ir_unop_log,
Eric Anholt01665262010-03-27 13:56:35 -0700432 ir_unop_exp2,
433 ir_unop_log2,
Ian Romanicka87ac252010-02-22 13:19:34 -0800434 ir_unop_f2i, /**< Float-to-integer conversion. */
435 ir_unop_i2f, /**< Integer-to-float conversion. */
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000436 ir_unop_f2b, /**< Float-to-boolean conversion */
437 ir_unop_b2f, /**< Boolean-to-float conversion */
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000438 ir_unop_i2b, /**< int-to-boolean conversion */
439 ir_unop_b2i, /**< Boolean-to-int conversion */
Ian Romanick6c86ea82010-03-26 16:11:48 -0700440 ir_unop_u2f, /**< Unsigned-to-float conversion. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800441
442 /**
443 * \name Unary floating-point rounding operations.
444 */
445 /*@{*/
446 ir_unop_trunc,
447 ir_unop_ceil,
448 ir_unop_floor,
449 /*@}*/
450
451 ir_binop_add,
452 ir_binop_sub,
453 ir_binop_mul,
454 ir_binop_div,
455 ir_binop_mod,
456
457 /**
458 * \name Binary comparison operators
459 */
460 /*@{*/
461 ir_binop_less,
462 ir_binop_greater,
463 ir_binop_lequal,
464 ir_binop_gequal,
465 ir_binop_equal,
466 ir_binop_nequal,
467 /*@}*/
468
469 /**
470 * \name Bit-wise binary operations.
471 */
472 /*@{*/
473 ir_binop_lshift,
474 ir_binop_rshift,
475 ir_binop_bit_and,
476 ir_binop_bit_xor,
477 ir_binop_bit_or,
478 /*@}*/
479
480 ir_binop_logic_and,
481 ir_binop_logic_xor,
482 ir_binop_logic_or,
Ian Romanicka87ac252010-02-22 13:19:34 -0800483
484 ir_binop_dot,
485 ir_binop_min,
486 ir_binop_max,
487
488 ir_binop_pow
489};
490
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700491class ir_expression : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800492public:
493 ir_expression(int op, const struct glsl_type *type,
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700494 ir_rvalue *, ir_rvalue *);
Ian Romanicka87ac252010-02-22 13:19:34 -0800495
Kenneth Graunke7dd6adb2010-04-07 16:56:57 -0700496 static unsigned int get_num_operands(ir_expression_operation);
497 unsigned int get_num_operands()
498 {
499 return get_num_operands(operation);
500 }
Eric Anholt160d0922010-04-01 18:07:08 -1000501
Kenneth Graunke3b969962010-04-07 17:18:29 -0700502 /**
503 * Return a string representing this expression's operator.
504 */
505 const char *operator_string();
506
507 /**
508 * Do a reverse-lookup to translate the given string into an operator.
509 */
510 static ir_expression_operation get_operator(const char *);
511
Ian Romanick78b51b02010-03-09 16:23:37 -0800512 virtual void accept(ir_visitor *v)
513 {
514 v->visit(this);
515 }
516
Eric Anholtcad97662010-04-07 11:46:26 -0700517 ir_expression *clone();
518
Ian Romanicka87ac252010-02-22 13:19:34 -0800519 ir_expression_operation operation;
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700520 ir_rvalue *operands[2];
Ian Romanicka87ac252010-02-22 13:19:34 -0800521};
522
523
Ian Romanicked45ec62010-03-11 14:34:27 -0800524/**
525 * IR instruction representing a function call
526 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700527class ir_call : public ir_rvalue {
Ian Romanicked45ec62010-03-11 14:34:27 -0800528public:
Ian Romanick471471f2010-03-11 14:50:30 -0800529 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
Ian Romanickb427c912010-04-07 18:03:50 -0700530 : callee(callee)
Ian Romanicked45ec62010-03-11 14:34:27 -0800531 {
Ian Romanick9e7c34b2010-03-23 12:21:18 -0700532 assert(callee->return_type != NULL);
533 type = callee->return_type;
Ian Romanick471471f2010-03-11 14:50:30 -0800534 actual_parameters->move_nodes_to(& this->actual_parameters);
Ian Romanicked45ec62010-03-11 14:34:27 -0800535 }
536
Eric Anholtcad97662010-04-07 11:46:26 -0700537 virtual ir_call *as_call()
538 {
539 return this;
540 }
541
Ian Romanicked45ec62010-03-11 14:34:27 -0800542 virtual void accept(ir_visitor *v)
543 {
544 v->visit(this);
545 }
546
547 /**
548 * Get a generic ir_call object when an error occurs
549 */
550 static ir_call *get_error_instruction();
551
Ian Romanick9878c652010-03-26 17:19:47 -0700552 /**
553 * Get an iterator for the set of acutal parameters
554 */
555 exec_list_iterator iterator()
556 {
557 return actual_parameters.iterator();
558 }
559
Ian Romanick93614bc2010-03-26 17:29:29 -0700560 /**
561 * Get the name of the function being called.
562 */
563 const char *callee_name() const
564 {
Ian Romanick0f0ea582010-03-31 16:44:12 -0700565 return callee->function_name();
Ian Romanick93614bc2010-03-26 17:29:29 -0700566 }
567
Eric Anholtcad97662010-04-07 11:46:26 -0700568 const ir_function_signature *get_callee()
569 {
570 return callee;
571 }
572
573 /**
574 * Generates an inline version of the function before @ir,
575 * returning the return value of the function.
576 */
577 ir_rvalue *generate_inline(ir_instruction *ir);
578
Ian Romanicked45ec62010-03-11 14:34:27 -0800579private:
Ian Romanick471471f2010-03-11 14:50:30 -0800580 ir_call()
Ian Romanickb427c912010-04-07 18:03:50 -0700581 : callee(NULL)
Ian Romanick471471f2010-03-11 14:50:30 -0800582 {
583 /* empty */
584 }
585
586 const ir_function_signature *callee;
Eric Anholtf1ddca92010-04-07 12:35:34 -0700587
588 /* List of ir_rvalue of paramaters passed in this call. */
Ian Romanicked45ec62010-03-11 14:34:27 -0800589 exec_list actual_parameters;
590};
591
592
Ian Romanick9578c872010-03-19 16:44:52 -0700593/**
594 * \name Jump-like IR instructions.
595 *
596 * These include \c break, \c continue, \c return, and \c discard.
597 */
598/*@{*/
599class ir_jump : public ir_instruction {
600protected:
601 ir_jump()
Ian Romanick9578c872010-03-19 16:44:52 -0700602 {
603 /* empty */
604 }
605};
606
607class ir_return : public ir_jump {
608public:
609 ir_return()
610 : value(NULL)
611 {
612 /* empty */
613 }
614
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700615 ir_return(ir_rvalue *value)
Ian Romanick9578c872010-03-19 16:44:52 -0700616 : value(value)
617 {
618 /* empty */
619 }
620
Eric Anholtcad97662010-04-07 11:46:26 -0700621 virtual ir_return *as_return()
622 {
623 return this;
624 }
625
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700626 ir_rvalue *get_value() const
Ian Romanick9578c872010-03-19 16:44:52 -0700627 {
628 return value;
629 }
630
631 virtual void accept(ir_visitor *v)
632 {
633 v->visit(this);
634 }
635
636private:
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700637 ir_rvalue *value;
Ian Romanick9578c872010-03-19 16:44:52 -0700638};
Ian Romanickf8e31e02010-04-05 16:28:15 -0700639
640
641/**
642 * Jump instructions used inside loops
643 *
644 * These include \c break and \c continue. The \c break within a loop is
645 * different from the \c break within a switch-statement.
646 *
647 * \sa ir_switch_jump
648 */
649class ir_loop_jump : public ir_jump {
650public:
651 enum jump_mode {
652 jump_break,
653 jump_continue
654 };
655
656 ir_loop_jump(ir_loop *loop, jump_mode mode)
657 : loop(loop), mode(mode)
658 {
659 /* empty */
660 }
661
662 virtual void accept(ir_visitor *v)
663 {
664 v->visit(this);
665 }
666
667 bool is_break() const
668 {
669 return mode == jump_break;
670 }
671
672 bool is_continue() const
673 {
674 return mode == jump_continue;
675 }
676
677private:
678 /** Loop containing this break instruction. */
679 ir_loop *loop;
680
681 /** Mode selector for the jump instruction. */
682 enum jump_mode mode;
683};
Ian Romanick9578c872010-03-19 16:44:52 -0700684/*@}*/
685
686
Ian Romanicka87ac252010-02-22 13:19:34 -0800687struct ir_swizzle_mask {
688 unsigned x:2;
689 unsigned y:2;
690 unsigned z:2;
691 unsigned w:2;
692
693 /**
694 * Number of components in the swizzle.
695 */
Kenneth Graunkef25a5ad2010-03-25 11:22:42 -0700696 unsigned num_components:3;
Ian Romanicka87ac252010-02-22 13:19:34 -0800697
698 /**
699 * Does the swizzle contain duplicate components?
700 *
701 * L-value swizzles cannot contain duplicate components.
702 */
703 unsigned has_duplicates:1;
704};
705
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700706
707class ir_swizzle : public ir_rvalue {
708public:
709 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
710 unsigned count);
Eric Anholt05a4e592010-05-03 17:08:01 -0700711 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
Eric Anholtcad97662010-04-07 11:46:26 -0700712
Eric Anholt7d211042010-04-16 16:43:47 -0700713 virtual ir_swizzle *as_swizzle()
714 {
715 return this;
716 }
717
Eric Anholtcad97662010-04-07 11:46:26 -0700718 ir_swizzle *clone()
719 {
720 return new ir_swizzle(this->val, this->mask);
721 }
722
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700723 /**
724 * Construct an ir_swizzle from the textual representation. Can fail.
725 */
726 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
727
728 virtual void accept(ir_visitor *v)
729 {
730 v->visit(this);
731 }
732
733 bool is_lvalue()
734 {
Eric Anholta9fafc62010-03-28 01:29:18 -0700735 return val->is_lvalue() && !mask.has_duplicates;
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700736 }
737
738 ir_rvalue *val;
739 ir_swizzle_mask mask;
740};
741
742
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700743class ir_dereference : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800744public:
745 ir_dereference(struct ir_instruction *);
746
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700747 ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
Ian Romanick95469972010-03-25 17:01:15 -0700748
Ian Romanick7ee79fb2010-04-19 15:40:49 -0700749 ir_dereference(ir_instruction *variable, const char *field);
750
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700751 virtual ir_dereference *as_dereference()
752 {
753 return this;
754 }
755
Ian Romanick78b51b02010-03-09 16:23:37 -0800756 virtual void accept(ir_visitor *v)
757 {
758 v->visit(this);
759 }
760
Eric Anholtc7da28b2010-04-01 20:27:35 -1000761 bool is_lvalue();
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700762
Ian Romanicka87ac252010-02-22 13:19:34 -0800763 enum {
764 ir_reference_variable,
765 ir_reference_array,
766 ir_reference_record
767 } mode;
768
769 /**
770 * Object being dereferenced.
771 *
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700772 * Must be either an \c ir_variable or an \c ir_rvalue.
Ian Romanicka87ac252010-02-22 13:19:34 -0800773 */
774 ir_instruction *var;
775
776 union {
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700777 ir_rvalue *array_index;
Ian Romanicka87ac252010-02-22 13:19:34 -0800778 const char *field;
Ian Romanicka87ac252010-02-22 13:19:34 -0800779 } selector;
780};
781
782
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700783class ir_constant : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800784public:
785 ir_constant(const struct glsl_type *type, const void *data);
Eric Anholt3c36b2d2010-03-26 12:07:44 -0700786 ir_constant(bool b);
787 ir_constant(unsigned int u);
788 ir_constant(int i);
789 ir_constant(float f);
Ian Romanicka87ac252010-02-22 13:19:34 -0800790
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700791 virtual ir_constant *as_constant()
792 {
793 return this;
794 }
795
Ian Romanick78b51b02010-03-09 16:23:37 -0800796 virtual void accept(ir_visitor *v)
797 {
798 v->visit(this);
799 }
800
Eric Anholtcad97662010-04-07 11:46:26 -0700801 ir_constant *clone()
802 {
803 return new ir_constant(this->type, &this->value);
804 }
805
Ian Romanicka87ac252010-02-22 13:19:34 -0800806 /**
807 * Value of the constant.
808 *
809 * The field used to back the values supplied by the constant is determined
810 * by the type associated with the \c ir_instruction. Constants may be
811 * scalars, vectors, or matrices.
812 */
813 union {
814 unsigned u[16];
815 int i[16];
816 float f[16];
817 bool b[16];
818 } value;
819};
820
Eric Anholt70b74922010-04-06 11:52:09 -0700821void
822visit_exec_list(exec_list *list, ir_visitor *visitor);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800823
824extern void
825_mesa_glsl_initialize_variables(exec_list *instructions,
826 struct _mesa_glsl_parse_state *state);
Ian Romanicke309a602010-03-15 15:20:15 -0700827
Eric Anholtc22c4002010-03-26 18:20:30 -0700828extern void
829_mesa_glsl_initialize_functions(exec_list *instructions,
830 struct _mesa_glsl_parse_state *state);
831
Ian Romanicke309a602010-03-15 15:20:15 -0700832#endif /* IR_H */