Add ir_variable::clone
[mesa.git] / ir.h
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 "list.h"
30 #include "ir_visitor.h"
31
32 struct ir_program {
33 void *bong_hits;
34 };
35
36 /**
37 * Base class of all IR instructions
38 */
39 class ir_instruction : public exec_node {
40 public:
41 const struct glsl_type *type;
42
43 class ir_constant *constant_expression_value();
44 virtual void accept(ir_visitor *) = 0;
45
46 /**
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; }
55 virtual class ir_dereference * as_dereference() { return NULL; }
56 virtual class ir_rvalue * as_rvalue() { return NULL; }
57 /*@}*/
58
59 protected:
60 ir_instruction()
61 {
62 /* empty */
63 }
64 };
65
66
67 class ir_rvalue : public ir_instruction {
68 public:
69 virtual ir_rvalue * as_rvalue()
70 {
71 return this;
72 }
73
74 virtual bool is_lvalue()
75 {
76 return false;
77 }
78
79 protected:
80 ir_rvalue() : ir_instruction() { }
81 };
82
83
84 enum ir_variable_mode {
85 ir_var_auto = 0,
86 ir_var_uniform,
87 ir_var_in,
88 ir_var_out,
89 ir_var_inout
90 };
91
92 enum ir_varaible_interpolation {
93 ir_var_smooth = 0,
94 ir_var_flat,
95 ir_var_noperspective
96 };
97
98
99 class ir_variable : public ir_instruction {
100 public:
101 ir_variable(const struct glsl_type *, const char *);
102
103 virtual ir_variable *as_variable()
104 {
105 return this;
106 }
107
108 virtual void accept(ir_visitor *v)
109 {
110 v->visit(this);
111 }
112
113 /**
114 * Duplicate an IR variable
115 *
116 * \note
117 * This will probably be made \c virtual and moved to the base class
118 * eventually.
119 */
120 ir_variable *clone() const
121 {
122 ir_variable *var = new ir_variable(type, name);
123
124 var->read_only = this->read_only;
125 var->centroid = this->centroid;
126 var->invariant = this->invariant;
127 var->mode = this->mode;
128 var->interpolation = this->interpolation;
129
130 return var;
131 }
132
133 const char *name;
134
135 unsigned read_only:1;
136 unsigned centroid:1;
137 unsigned invariant:1;
138
139 unsigned mode:3;
140 unsigned interpolation:2;
141 };
142
143
144 class ir_label : public ir_instruction {
145 public:
146 ir_label(const char *label);
147
148 virtual void accept(ir_visitor *v)
149 {
150 v->visit(this);
151 }
152
153 const char *label;
154 };
155
156
157 /*@{*/
158 class ir_function_signature : public ir_instruction {
159 public:
160 ir_function_signature(const glsl_type *return_type);
161
162 virtual void accept(ir_visitor *v)
163 {
164 v->visit(this);
165 }
166
167 /**
168 * Get the name of the function for which this is a signature
169 */
170 const char *function_name() const;
171
172 /**
173 * Function return type.
174 *
175 * \note This discards the optional precision qualifier.
176 */
177 const struct glsl_type *return_type;
178
179 /**
180 * List of function parameters stored as ir_variable objects.
181 */
182 struct exec_list parameters;
183
184 /**
185 * Pointer to the label that begins the function definition.
186 */
187 ir_label *definition;
188
189 private:
190 /** Function of which this signature is one overload. */
191 class ir_function *function;
192
193 friend class ir_function;
194 };
195
196
197 /**
198 * Header for tracking functions in the symbol table
199 */
200 class ir_function : public ir_instruction {
201 public:
202 ir_function(const char *name);
203
204 virtual void accept(ir_visitor *v)
205 {
206 v->visit(this);
207 }
208
209 void add_signature(ir_function_signature *sig)
210 {
211 sig->function = this;
212 signatures.push_tail(sig);
213 }
214
215 /**
216 * Get an iterator for the set of function signatures
217 */
218 exec_list_iterator iterator()
219 {
220 return signatures.iterator();
221 }
222
223 /**
224 * Find a signature that matches a set of actual parameters.
225 */
226 const ir_function_signature *matching_signature(exec_list *actual_param);
227
228 /**
229 * Name of the function.
230 */
231 const char *name;
232
233 private:
234 /**
235 * Set of overloaded functions with this name.
236 */
237 struct exec_list signatures;
238 };
239
240 inline const char *ir_function_signature::function_name() const
241 {
242 return function->name;
243 }
244 /*@}*/
245
246
247 /**
248 * IR instruction representing high-level if-statements
249 */
250 class ir_if : public ir_instruction {
251 public:
252 ir_if(ir_rvalue *condition)
253 : condition(condition)
254 {
255 /* empty */
256 }
257
258 virtual void accept(ir_visitor *v)
259 {
260 v->visit(this);
261 }
262
263 ir_rvalue *condition;
264 exec_list then_instructions;
265 exec_list else_instructions;
266 };
267
268
269 class ir_assignment : public ir_rvalue {
270 public:
271 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
272
273 virtual void accept(ir_visitor *v)
274 {
275 v->visit(this);
276 }
277
278 /**
279 * Left-hand side of the assignment.
280 */
281 ir_rvalue *lhs;
282
283 /**
284 * Value being assigned
285 */
286 ir_rvalue *rhs;
287
288 /**
289 * Optional condition for the assignment.
290 */
291 ir_rvalue *condition;
292 };
293
294 /* Update ir_print_visitor.cpp when updating this list. */
295 enum ir_expression_operation {
296 ir_unop_bit_not,
297 ir_unop_logic_not,
298 ir_unop_neg,
299 ir_unop_abs,
300 ir_unop_rcp,
301 ir_unop_rsq,
302 ir_unop_sqrt,
303 ir_unop_exp,
304 ir_unop_log,
305 ir_unop_exp2,
306 ir_unop_log2,
307 ir_unop_f2i, /**< Float-to-integer conversion. */
308 ir_unop_i2f, /**< Integer-to-float conversion. */
309 ir_unop_u2f, /**< Unsigned-to-float conversion. */
310
311 /**
312 * \name Unary floating-point rounding operations.
313 */
314 /*@{*/
315 ir_unop_trunc,
316 ir_unop_ceil,
317 ir_unop_floor,
318 /*@}*/
319
320 ir_binop_add,
321 ir_binop_sub,
322 ir_binop_mul,
323 ir_binop_div,
324 ir_binop_mod,
325
326 /**
327 * \name Binary comparison operators
328 */
329 /*@{*/
330 ir_binop_less,
331 ir_binop_greater,
332 ir_binop_lequal,
333 ir_binop_gequal,
334 ir_binop_equal,
335 ir_binop_nequal,
336 /*@}*/
337
338 /**
339 * \name Bit-wise binary operations.
340 */
341 /*@{*/
342 ir_binop_lshift,
343 ir_binop_rshift,
344 ir_binop_bit_and,
345 ir_binop_bit_xor,
346 ir_binop_bit_or,
347 /*@}*/
348
349 ir_binop_logic_and,
350 ir_binop_logic_xor,
351 ir_binop_logic_or,
352 ir_binop_logic_not,
353
354 ir_binop_dot,
355 ir_binop_min,
356 ir_binop_max,
357
358 ir_binop_pow
359 };
360
361 class ir_expression : public ir_rvalue {
362 public:
363 ir_expression(int op, const struct glsl_type *type,
364 ir_rvalue *, ir_rvalue *);
365
366 virtual void accept(ir_visitor *v)
367 {
368 v->visit(this);
369 }
370
371 ir_expression_operation operation;
372 ir_rvalue *operands[2];
373 };
374
375
376 /**
377 * IR instruction representing a function call
378 */
379 class ir_call : public ir_rvalue {
380 public:
381 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
382 : ir_rvalue(), callee(callee)
383 {
384 assert(callee->return_type != NULL);
385 type = callee->return_type;
386 actual_parameters->move_nodes_to(& this->actual_parameters);
387 }
388
389 virtual void accept(ir_visitor *v)
390 {
391 v->visit(this);
392 }
393
394 /**
395 * Get a generic ir_call object when an error occurs
396 */
397 static ir_call *get_error_instruction();
398
399 /**
400 * Get an iterator for the set of acutal parameters
401 */
402 exec_list_iterator iterator()
403 {
404 return actual_parameters.iterator();
405 }
406
407 /**
408 * Get the name of the function being called.
409 */
410 const char *callee_name() const
411 {
412 return callee->function_name();
413 }
414
415 private:
416 ir_call()
417 : ir_rvalue(), callee(NULL)
418 {
419 /* empty */
420 }
421
422 const ir_function_signature *callee;
423 exec_list actual_parameters;
424 };
425
426
427 /**
428 * \name Jump-like IR instructions.
429 *
430 * These include \c break, \c continue, \c return, and \c discard.
431 */
432 /*@{*/
433 class ir_jump : public ir_instruction {
434 protected:
435 ir_jump()
436 : ir_instruction()
437 {
438 /* empty */
439 }
440 };
441
442 class ir_return : public ir_jump {
443 public:
444 ir_return()
445 : value(NULL)
446 {
447 /* empty */
448 }
449
450 ir_return(ir_rvalue *value)
451 : value(value)
452 {
453 /* empty */
454 }
455
456 ir_rvalue *get_value() const
457 {
458 return value;
459 }
460
461 virtual void accept(ir_visitor *v)
462 {
463 v->visit(this);
464 }
465
466 private:
467 ir_rvalue *value;
468 };
469 /*@}*/
470
471
472 struct ir_swizzle_mask {
473 unsigned x:2;
474 unsigned y:2;
475 unsigned z:2;
476 unsigned w:2;
477
478 /**
479 * Number of components in the swizzle.
480 */
481 unsigned num_components:3;
482
483 /**
484 * Does the swizzle contain duplicate components?
485 *
486 * L-value swizzles cannot contain duplicate components.
487 */
488 unsigned has_duplicates:1;
489 };
490
491
492 class ir_swizzle : public ir_rvalue {
493 public:
494 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
495 unsigned count);
496 /**
497 * Construct an ir_swizzle from the textual representation. Can fail.
498 */
499 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
500
501 virtual void accept(ir_visitor *v)
502 {
503 v->visit(this);
504 }
505
506 bool is_lvalue()
507 {
508 return val->is_lvalue() && !mask.has_duplicates;
509 }
510
511 ir_rvalue *val;
512 ir_swizzle_mask mask;
513 };
514
515
516 class ir_dereference : public ir_rvalue {
517 public:
518 ir_dereference(struct ir_instruction *);
519
520 ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
521
522 virtual ir_dereference *as_dereference()
523 {
524 return this;
525 }
526
527 virtual void accept(ir_visitor *v)
528 {
529 v->visit(this);
530 }
531
532 bool is_lvalue()
533 {
534 if (var == NULL)
535 return false;
536
537 ir_variable *const as_var = var->as_variable();
538 if (as_var == NULL)
539 return false;
540
541 return !as_var->read_only;
542 }
543
544 enum {
545 ir_reference_variable,
546 ir_reference_array,
547 ir_reference_record
548 } mode;
549
550 /**
551 * Object being dereferenced.
552 *
553 * Must be either an \c ir_variable or an \c ir_rvalue.
554 */
555 ir_instruction *var;
556
557 union {
558 ir_rvalue *array_index;
559 const char *field;
560 } selector;
561 };
562
563
564 class ir_constant : public ir_rvalue {
565 public:
566 ir_constant(const struct glsl_type *type, const void *data);
567 ir_constant(bool b);
568 ir_constant(unsigned int u);
569 ir_constant(int i);
570 ir_constant(float f);
571
572 virtual void accept(ir_visitor *v)
573 {
574 v->visit(this);
575 }
576
577 /**
578 * Value of the constant.
579 *
580 * The field used to back the values supplied by the constant is determined
581 * by the type associated with the \c ir_instruction. Constants may be
582 * scalars, vectors, or matrices.
583 */
584 union {
585 unsigned u[16];
586 int i[16];
587 float f[16];
588 bool b[16];
589 } value;
590 };
591
592
593 extern void
594 _mesa_glsl_initialize_variables(exec_list *instructions,
595 struct _mesa_glsl_parse_state *state);
596
597 extern void
598 _mesa_glsl_initialize_functions(exec_list *instructions,
599 struct _mesa_glsl_parse_state *state);
600
601 #endif /* IR_H */