Fix matrix dimensioning
[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 enum ir_opcodes {
38 ir_op_var_decl,
39 ir_op_assign,
40 ir_op_expression,
41 ir_op_dereference,
42 ir_op_jump,
43 ir_op_label,
44 ir_op_constant,
45 ir_op_func_sig,
46 ir_op_func,
47 ir_op_call,
48 };
49
50 /**
51 * Base class of all IR instructions
52 */
53 class ir_instruction : public exec_node {
54 public:
55 unsigned mode;
56 const struct glsl_type *type;
57
58 virtual void accept(ir_visitor *) = 0;
59
60 protected:
61 ir_instruction(int mode)
62 : mode(mode)
63 {
64 /* empty */
65 }
66
67 private:
68 /**
69 * Dummy constructor to catch bad constructors in derived classes.
70 *
71 * Every derived must use the constructor that sets the instructions
72 * mode. Having the \c void constructor private prevents derived classes
73 * from accidentally doing the wrong thing.
74 */
75 ir_instruction(void);
76 };
77
78
79 enum ir_variable_mode {
80 ir_var_auto = 0,
81 ir_var_uniform,
82 ir_var_in,
83 ir_var_out,
84 ir_var_inout
85 };
86
87 enum ir_varaible_interpolation {
88 ir_var_smooth = 0,
89 ir_var_flat,
90 ir_var_noperspective
91 };
92
93 class ir_variable : public ir_instruction {
94 public:
95 ir_variable(const struct glsl_type *, const char *);
96
97 virtual void accept(ir_visitor *v)
98 {
99 v->visit(this);
100 }
101
102 const char *name;
103
104 unsigned read_only:1;
105 unsigned centroid:1;
106 unsigned invariant:1;
107
108 unsigned mode:3;
109 unsigned interpolation:2;
110 };
111
112
113 class ir_label : public ir_instruction {
114 public:
115 ir_label(const char *label);
116
117 virtual void accept(ir_visitor *v)
118 {
119 v->visit(this);
120 }
121
122 const char *label;
123 };
124
125
126 /*@{*/
127 class ir_function_signature : public ir_instruction {
128 public:
129 ir_function_signature(const glsl_type *return_type);
130
131 virtual void accept(ir_visitor *v)
132 {
133 v->visit(this);
134 }
135
136 /**
137 * Function return type.
138 *
139 * \note This discards the optional precision qualifier.
140 */
141 const struct glsl_type *return_type;
142
143 /**
144 * List of function parameters stored as ir_variable objects.
145 */
146 struct exec_list parameters;
147
148 /**
149 * Pointer to the label that begins the function definition.
150 */
151 ir_label *definition;
152 };
153
154
155 /**
156 * Header for tracking functions in the symbol table
157 */
158 class ir_function : public ir_instruction {
159 public:
160 ir_function(const char *name);
161
162 virtual void accept(ir_visitor *v)
163 {
164 v->visit(this);
165 }
166
167 /**
168 * Find a signature that matches a set of actual parameters.
169 */
170 const ir_function_signature *matching_signature(exec_list *actual_param);
171
172 /**
173 * Name of the function.
174 */
175 const char *name;
176
177 /**
178 * Set of overloaded functions with this name.
179 */
180 struct exec_list signatures;
181 };
182 /*@}*/
183
184 class ir_expression;
185 class ir_dereference;
186
187 class ir_assignment : public ir_instruction {
188 public:
189 ir_assignment(ir_instruction *lhs, ir_instruction *rhs,
190 ir_expression *condition);
191
192 virtual void accept(ir_visitor *v)
193 {
194 v->visit(this);
195 }
196
197 /**
198 * Left-hand side of the assignment.
199 */
200 ir_dereference *lhs;
201
202 /**
203 * Value being assigned
204 *
205 * This should be either \c ir_op_expression or \c ir_op_deference.
206 */
207 ir_instruction *rhs;
208
209 /**
210 * Optional condition for the assignment.
211 */
212 ir_expression *condition;
213 };
214
215
216 enum ir_expression_operation {
217 ir_unop_bit_not,
218 ir_unop_logic_not,
219 ir_unop_neg,
220 ir_unop_abs,
221 ir_unop_rcp,
222 ir_unop_rsq,
223 ir_unop_exp,
224 ir_unop_log,
225 ir_unop_f2i, /**< Float-to-integer conversion. */
226 ir_unop_i2f, /**< Integer-to-float conversion. */
227
228 /**
229 * \name Unary floating-point rounding operations.
230 */
231 /*@{*/
232 ir_unop_trunc,
233 ir_unop_ceil,
234 ir_unop_floor,
235 /*@}*/
236
237 ir_binop_add,
238 ir_binop_sub,
239 ir_binop_mul,
240 ir_binop_div,
241 ir_binop_mod,
242
243 /**
244 * \name Binary comparison operators
245 */
246 /*@{*/
247 ir_binop_less,
248 ir_binop_greater,
249 ir_binop_lequal,
250 ir_binop_gequal,
251 ir_binop_equal,
252 ir_binop_nequal,
253 /*@}*/
254
255 /**
256 * \name Bit-wise binary operations.
257 */
258 /*@{*/
259 ir_binop_lshift,
260 ir_binop_rshift,
261 ir_binop_bit_and,
262 ir_binop_bit_xor,
263 ir_binop_bit_or,
264 /*@}*/
265
266 ir_binop_logic_and,
267 ir_binop_logic_xor,
268 ir_binop_logic_or,
269 ir_binop_logic_not,
270
271 ir_binop_dot,
272 ir_binop_min,
273 ir_binop_max,
274
275 ir_binop_pow
276 };
277
278 class ir_expression : public ir_instruction {
279 public:
280 ir_expression(int op, const struct glsl_type *type,
281 ir_instruction *, ir_instruction *);
282
283 virtual void accept(ir_visitor *v)
284 {
285 v->visit(this);
286 }
287
288 ir_expression_operation operation;
289 ir_instruction *operands[2];
290 };
291
292
293 /**
294 * IR instruction representing a function call
295 */
296 class ir_call : public ir_instruction {
297 public:
298 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
299 : ir_instruction(ir_op_call), callee(callee)
300 {
301 assert(callee->return_type != NULL);
302 type = callee->return_type;
303 actual_parameters->move_nodes_to(& this->actual_parameters);
304 }
305
306 virtual void accept(ir_visitor *v)
307 {
308 v->visit(this);
309 }
310
311 /**
312 * Get a generic ir_call object when an error occurs
313 */
314 static ir_call *get_error_instruction();
315
316 private:
317 ir_call()
318 : ir_instruction(ir_op_call), callee(NULL)
319 {
320 /* empty */
321 }
322
323 const ir_function_signature *callee;
324 exec_list actual_parameters;
325 };
326
327
328 /**
329 * \name Jump-like IR instructions.
330 *
331 * These include \c break, \c continue, \c return, and \c discard.
332 */
333 /*@{*/
334 class ir_jump : public ir_instruction {
335 protected:
336 ir_jump()
337 : ir_instruction(ir_op_jump)
338 {
339 /* empty */
340 }
341 };
342
343 class ir_return : public ir_jump {
344 public:
345 ir_return()
346 : value(NULL)
347 {
348 /* empty */
349 }
350
351 ir_return(ir_expression *value)
352 : value(value)
353 {
354 /* empty */
355 }
356
357 ir_expression *get_value() const
358 {
359 return value;
360 }
361
362 virtual void accept(ir_visitor *v)
363 {
364 v->visit(this);
365 }
366
367 private:
368 ir_expression *value;
369 };
370 /*@}*/
371
372
373 struct ir_swizzle_mask {
374 unsigned x:2;
375 unsigned y:2;
376 unsigned z:2;
377 unsigned w:2;
378
379 /**
380 * Number of components in the swizzle.
381 */
382 unsigned num_components:2;
383
384 /**
385 * Does the swizzle contain duplicate components?
386 *
387 * L-value swizzles cannot contain duplicate components.
388 */
389 unsigned has_duplicates:1;
390 };
391
392 class ir_dereference : public ir_instruction {
393 public:
394 ir_dereference(struct ir_instruction *);
395
396 virtual void accept(ir_visitor *v)
397 {
398 v->visit(this);
399 }
400
401 /**
402 * Setting the swizzle of a derefernce
403 */
404 void set_swizzle(unsigned x, unsigned y, unsigned z, unsigned w,
405 unsigned count);
406
407
408 enum {
409 ir_reference_variable,
410 ir_reference_array,
411 ir_reference_record
412 } mode;
413
414 /**
415 * Object being dereferenced.
416 *
417 * Must be either an \c ir_variable or an \c ir_deference.
418 */
419 ir_instruction *var;
420
421 union {
422 ir_expression *array_index;
423 const char *field;
424 struct ir_swizzle_mask swizzle;
425 } selector;
426 };
427
428
429 class ir_constant : public ir_instruction {
430 public:
431 ir_constant(const struct glsl_type *type, const void *data);
432
433 virtual void accept(ir_visitor *v)
434 {
435 v->visit(this);
436 }
437
438 /**
439 * Value of the constant.
440 *
441 * The field used to back the values supplied by the constant is determined
442 * by the type associated with the \c ir_instruction. Constants may be
443 * scalars, vectors, or matrices.
444 */
445 union {
446 unsigned u[16];
447 int i[16];
448 float f[16];
449 bool b[16];
450 } value;
451 };
452
453
454 extern void
455 _mesa_glsl_initialize_variables(exec_list *instructions,
456 struct _mesa_glsl_parse_state *state);
457
458 #endif /* IR_H */