blob: 63ae0bc3b696ee777bea59ab51673d2b036702d2 [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 Romanick78b51b02010-03-09 16:23:37 -080043 virtual void accept(ir_visitor *) = 0;
44
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070045 /**
46 * \name IR instruction downcast functions
47 *
48 * These functions either cast the object to a derived class or return
49 * \c NULL if the object's type does not match the specified derived class.
50 * Additional downcast functions will be added as needed.
51 */
52 /*@{*/
53 virtual class ir_variable * as_variable() { return NULL; }
54 virtual class ir_dereference * as_dereference() { return NULL; }
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070055 virtual class ir_rvalue * as_rvalue() { return NULL; }
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070056 /*@}*/
57
Ian Romanicka87ac252010-02-22 13:19:34 -080058protected:
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -070059 ir_instruction()
Ian Romanickd27ec242010-03-11 14:23:41 -080060 {
61 /* empty */
62 }
Ian Romanicka87ac252010-02-22 13:19:34 -080063};
64
65
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070066class ir_rvalue : public ir_instruction {
67public:
68 virtual ir_rvalue * as_rvalue()
69 {
70 return this;
71 }
72
73 virtual bool is_lvalue()
74 {
75 return false;
76 }
77
78protected:
79 ir_rvalue() : ir_instruction() { }
80};
81
82
Ian Romanicka87ac252010-02-22 13:19:34 -080083enum ir_variable_mode {
84 ir_var_auto = 0,
85 ir_var_uniform,
86 ir_var_in,
87 ir_var_out,
88 ir_var_inout
89};
90
91enum ir_varaible_interpolation {
92 ir_var_smooth = 0,
93 ir_var_flat,
94 ir_var_noperspective
95};
96
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070097
Ian Romanicka87ac252010-02-22 13:19:34 -080098class ir_variable : public ir_instruction {
99public:
100 ir_variable(const struct glsl_type *, const char *);
101
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700102 virtual ir_variable *as_variable()
103 {
104 return this;
105 }
106
Ian Romanick78b51b02010-03-09 16:23:37 -0800107 virtual void accept(ir_visitor *v)
108 {
109 v->visit(this);
110 }
111
Ian Romanicka87ac252010-02-22 13:19:34 -0800112 const char *name;
113
114 unsigned read_only:1;
115 unsigned centroid:1;
116 unsigned invariant:1;
117
118 unsigned mode:3;
119 unsigned interpolation:2;
120};
121
122
123class ir_label : public ir_instruction {
124public:
125 ir_label(const char *label);
126
Ian Romanick78b51b02010-03-09 16:23:37 -0800127 virtual void accept(ir_visitor *v)
128 {
129 v->visit(this);
130 }
131
Ian Romanicka87ac252010-02-22 13:19:34 -0800132 const char *label;
133};
134
135
136/*@{*/
137class ir_function_signature : public ir_instruction {
138public:
Ian Romanicke39cc692010-03-23 12:19:13 -0700139 ir_function_signature(const glsl_type *return_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800140
Ian Romanick78b51b02010-03-09 16:23:37 -0800141 virtual void accept(ir_visitor *v)
142 {
143 v->visit(this);
144 }
145
Ian Romanicka87ac252010-02-22 13:19:34 -0800146 /**
147 * Function return type.
148 *
149 * \note This discards the optional precision qualifier.
150 */
151 const struct glsl_type *return_type;
152
153 /**
154 * List of function parameters stored as ir_variable objects.
155 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800156 struct exec_list parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -0800157
158 /**
159 * Pointer to the label that begins the function definition.
160 */
161 ir_label *definition;
162};
163
164
165/**
166 * Header for tracking functions in the symbol table
167 */
168class ir_function : public ir_instruction {
169public:
Ian Romanick882dad72010-03-23 17:42:04 -0700170 ir_function(const char *name);
Ian Romanicka87ac252010-02-22 13:19:34 -0800171
Ian Romanick78b51b02010-03-09 16:23:37 -0800172 virtual void accept(ir_visitor *v)
173 {
174 v->visit(this);
175 }
176
Ian Romanicka87ac252010-02-22 13:19:34 -0800177 /**
Ian Romanick471471f2010-03-11 14:50:30 -0800178 * Find a signature that matches a set of actual parameters.
179 */
180 const ir_function_signature *matching_signature(exec_list *actual_param);
181
182 /**
Ian Romanicka87ac252010-02-22 13:19:34 -0800183 * Name of the function.
184 */
185 const char *name;
186
Ian Romanick471471f2010-03-11 14:50:30 -0800187 /**
188 * Set of overloaded functions with this name.
189 */
Ian Romanick0044e7e2010-03-08 23:44:00 -0800190 struct exec_list signatures;
Ian Romanicka87ac252010-02-22 13:19:34 -0800191};
192/*@}*/
193
Ian Romanicka87ac252010-02-22 13:19:34 -0800194
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700195class ir_assignment : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800196public:
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700197 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
Ian Romanicka87ac252010-02-22 13:19:34 -0800198
Ian Romanick78b51b02010-03-09 16:23:37 -0800199 virtual void accept(ir_visitor *v)
200 {
201 v->visit(this);
202 }
203
Ian Romanicka87ac252010-02-22 13:19:34 -0800204 /**
205 * Left-hand side of the assignment.
206 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700207 ir_rvalue *lhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800208
209 /**
210 * Value being assigned
Ian Romanicka87ac252010-02-22 13:19:34 -0800211 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700212 ir_rvalue *rhs;
Ian Romanicka87ac252010-02-22 13:19:34 -0800213
214 /**
215 * Optional condition for the assignment.
216 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700217 ir_rvalue *condition;
Ian Romanicka87ac252010-02-22 13:19:34 -0800218};
219
220
221enum ir_expression_operation {
222 ir_unop_bit_not,
223 ir_unop_logic_not,
224 ir_unop_neg,
225 ir_unop_abs,
226 ir_unop_rcp,
227 ir_unop_rsq,
228 ir_unop_exp,
229 ir_unop_log,
230 ir_unop_f2i, /**< Float-to-integer conversion. */
231 ir_unop_i2f, /**< Integer-to-float conversion. */
232
233 /**
234 * \name Unary floating-point rounding operations.
235 */
236 /*@{*/
237 ir_unop_trunc,
238 ir_unop_ceil,
239 ir_unop_floor,
240 /*@}*/
241
242 ir_binop_add,
243 ir_binop_sub,
244 ir_binop_mul,
245 ir_binop_div,
246 ir_binop_mod,
247
248 /**
249 * \name Binary comparison operators
250 */
251 /*@{*/
252 ir_binop_less,
253 ir_binop_greater,
254 ir_binop_lequal,
255 ir_binop_gequal,
256 ir_binop_equal,
257 ir_binop_nequal,
258 /*@}*/
259
260 /**
261 * \name Bit-wise binary operations.
262 */
263 /*@{*/
264 ir_binop_lshift,
265 ir_binop_rshift,
266 ir_binop_bit_and,
267 ir_binop_bit_xor,
268 ir_binop_bit_or,
269 /*@}*/
270
271 ir_binop_logic_and,
272 ir_binop_logic_xor,
273 ir_binop_logic_or,
274 ir_binop_logic_not,
275
276 ir_binop_dot,
277 ir_binop_min,
278 ir_binop_max,
279
280 ir_binop_pow
281};
282
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700283class ir_expression : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800284public:
285 ir_expression(int op, const struct glsl_type *type,
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700286 ir_rvalue *, ir_rvalue *);
Ian Romanicka87ac252010-02-22 13:19:34 -0800287
Ian Romanick78b51b02010-03-09 16:23:37 -0800288 virtual void accept(ir_visitor *v)
289 {
290 v->visit(this);
291 }
292
Ian Romanicka87ac252010-02-22 13:19:34 -0800293 ir_expression_operation operation;
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700294 ir_rvalue *operands[2];
Ian Romanicka87ac252010-02-22 13:19:34 -0800295};
296
297
Ian Romanicked45ec62010-03-11 14:34:27 -0800298/**
299 * IR instruction representing a function call
300 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700301class ir_call : public ir_rvalue {
Ian Romanicked45ec62010-03-11 14:34:27 -0800302public:
Ian Romanick471471f2010-03-11 14:50:30 -0800303 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700304 : ir_rvalue(), callee(callee)
Ian Romanicked45ec62010-03-11 14:34:27 -0800305 {
Ian Romanick9e7c34b2010-03-23 12:21:18 -0700306 assert(callee->return_type != NULL);
307 type = callee->return_type;
Ian Romanick471471f2010-03-11 14:50:30 -0800308 actual_parameters->move_nodes_to(& this->actual_parameters);
Ian Romanicked45ec62010-03-11 14:34:27 -0800309 }
310
311 virtual void accept(ir_visitor *v)
312 {
313 v->visit(this);
314 }
315
316 /**
317 * Get a generic ir_call object when an error occurs
318 */
319 static ir_call *get_error_instruction();
320
321private:
Ian Romanick471471f2010-03-11 14:50:30 -0800322 ir_call()
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700323 : ir_rvalue(), callee(NULL)
Ian Romanick471471f2010-03-11 14:50:30 -0800324 {
325 /* empty */
326 }
327
328 const ir_function_signature *callee;
Ian Romanicked45ec62010-03-11 14:34:27 -0800329 exec_list actual_parameters;
330};
331
332
Ian Romanick9578c872010-03-19 16:44:52 -0700333/**
334 * \name Jump-like IR instructions.
335 *
336 * These include \c break, \c continue, \c return, and \c discard.
337 */
338/*@{*/
339class ir_jump : public ir_instruction {
340protected:
341 ir_jump()
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700342 : ir_instruction()
Ian Romanick9578c872010-03-19 16:44:52 -0700343 {
344 /* empty */
345 }
346};
347
348class ir_return : public ir_jump {
349public:
350 ir_return()
351 : value(NULL)
352 {
353 /* empty */
354 }
355
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700356 ir_return(ir_rvalue *value)
Ian Romanick9578c872010-03-19 16:44:52 -0700357 : value(value)
358 {
359 /* empty */
360 }
361
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700362 ir_rvalue *get_value() const
Ian Romanick9578c872010-03-19 16:44:52 -0700363 {
364 return value;
365 }
366
367 virtual void accept(ir_visitor *v)
368 {
369 v->visit(this);
370 }
371
372private:
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700373 ir_rvalue *value;
Ian Romanick9578c872010-03-19 16:44:52 -0700374};
375/*@}*/
376
377
Ian Romanicka87ac252010-02-22 13:19:34 -0800378struct ir_swizzle_mask {
379 unsigned x:2;
380 unsigned y:2;
381 unsigned z:2;
382 unsigned w:2;
383
384 /**
385 * Number of components in the swizzle.
386 */
Kenneth Graunkef25a5ad2010-03-25 11:22:42 -0700387 unsigned num_components:3;
Ian Romanicka87ac252010-02-22 13:19:34 -0800388
389 /**
390 * Does the swizzle contain duplicate components?
391 *
392 * L-value swizzles cannot contain duplicate components.
393 */
394 unsigned has_duplicates:1;
395};
396
Kenneth Graunkeaffc1412010-03-26 01:20:08 -0700397
398class ir_swizzle : public ir_rvalue {
399public:
400 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
401 unsigned count);
402 /**
403 * Construct an ir_swizzle from the textual representation. Can fail.
404 */
405 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
406
407 virtual void accept(ir_visitor *v)
408 {
409 v->visit(this);
410 }
411
412 bool is_lvalue()
413 {
414 return val->is_lvalue();
415 }
416
417 ir_rvalue *val;
418 ir_swizzle_mask mask;
419};
420
421
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700422class ir_dereference : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800423public:
424 ir_dereference(struct ir_instruction *);
425
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700426 ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
Ian Romanick95469972010-03-25 17:01:15 -0700427
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -0700428 virtual ir_dereference *as_dereference()
429 {
430 return this;
431 }
432
Ian Romanick78b51b02010-03-09 16:23:37 -0800433 virtual void accept(ir_visitor *v)
434 {
435 v->visit(this);
436 }
437
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700438 bool is_lvalue()
439 {
440 return var != NULL;
441 }
442
Ian Romanicka87ac252010-02-22 13:19:34 -0800443 enum {
444 ir_reference_variable,
445 ir_reference_array,
446 ir_reference_record
447 } mode;
448
449 /**
450 * Object being dereferenced.
451 *
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700452 * Must be either an \c ir_variable or an \c ir_rvalue.
Ian Romanicka87ac252010-02-22 13:19:34 -0800453 */
454 ir_instruction *var;
455
456 union {
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700457 ir_rvalue *array_index;
Ian Romanicka87ac252010-02-22 13:19:34 -0800458 const char *field;
Ian Romanicka87ac252010-02-22 13:19:34 -0800459 } selector;
460};
461
462
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700463class ir_constant : public ir_rvalue {
Ian Romanicka87ac252010-02-22 13:19:34 -0800464public:
465 ir_constant(const struct glsl_type *type, const void *data);
466
Ian Romanick78b51b02010-03-09 16:23:37 -0800467 virtual void accept(ir_visitor *v)
468 {
469 v->visit(this);
470 }
471
Ian Romanicka87ac252010-02-22 13:19:34 -0800472 /**
473 * Value of the constant.
474 *
475 * The field used to back the values supplied by the constant is determined
476 * by the type associated with the \c ir_instruction. Constants may be
477 * scalars, vectors, or matrices.
478 */
479 union {
480 unsigned u[16];
481 int i[16];
482 float f[16];
483 bool b[16];
484 } value;
485};
486
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800487
488extern void
489_mesa_glsl_initialize_variables(exec_list *instructions,
490 struct _mesa_glsl_parse_state *state);
Ian Romanicke309a602010-03-15 15:20:15 -0700491
492#endif /* IR_H */