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