ir.h revision 3c36b2df7c854d23b2be9580e416fb04079a1bef
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
32struct ir_program {
33   void *bong_hits;
34};
35
36/**
37 * Base class of all IR instructions
38 */
39class ir_instruction : public exec_node {
40public:
41   const struct glsl_type *type;
42
43   virtual void accept(ir_visitor *) = 0;
44
45   /**
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; }
55   virtual class ir_rvalue *            as_rvalue()           { return NULL; }
56   /*@}*/
57
58protected:
59   ir_instruction()
60   {
61      /* empty */
62   }
63};
64
65
66class 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
83enum 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
97
98class ir_variable : public ir_instruction {
99public:
100   ir_variable(const struct glsl_type *, const char *);
101
102   virtual ir_variable *as_variable()
103   {
104      return this;
105   }
106
107   virtual void accept(ir_visitor *v)
108   {
109      v->visit(this);
110   }
111
112   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
127   virtual void accept(ir_visitor *v)
128   {
129      v->visit(this);
130   }
131
132   const char *label;
133};
134
135
136/*@{*/
137class ir_function_signature : public ir_instruction {
138public:
139   ir_function_signature(const glsl_type *return_type);
140
141   virtual void accept(ir_visitor *v)
142   {
143      v->visit(this);
144   }
145
146   /**
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    */
156   struct exec_list parameters;
157
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:
170   ir_function(const char *name);
171
172   virtual void accept(ir_visitor *v)
173   {
174      v->visit(this);
175   }
176
177   /**
178    * Find a signature that matches a set of actual parameters.
179    */
180   const ir_function_signature *matching_signature(exec_list *actual_param);
181
182   /**
183    * Name of the function.
184    */
185   const char *name;
186
187   /**
188    * Set of overloaded functions with this name.
189    */
190   struct exec_list signatures;
191};
192/*@}*/
193
194
195class ir_assignment : public ir_rvalue {
196public:
197   ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
198
199   virtual void accept(ir_visitor *v)
200   {
201      v->visit(this);
202   }
203
204   /**
205    * Left-hand side of the assignment.
206    */
207   ir_rvalue *lhs;
208
209   /**
210    * Value being assigned
211    */
212   ir_rvalue *rhs;
213
214   /**
215    * Optional condition for the assignment.
216    */
217   ir_rvalue *condition;
218};
219
220/* Update ir_print_visitor.cpp when updating this list. */
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   ir_unop_u2f,      /**< Unsigned-to-float conversion. */
233
234   /**
235    * \name Unary floating-point rounding operations.
236    */
237   /*@{*/
238   ir_unop_trunc,
239   ir_unop_ceil,
240   ir_unop_floor,
241   /*@}*/
242
243   ir_binop_add,
244   ir_binop_sub,
245   ir_binop_mul,
246   ir_binop_div,
247   ir_binop_mod,
248
249   /**
250    * \name Binary comparison operators
251    */
252   /*@{*/
253   ir_binop_less,
254   ir_binop_greater,
255   ir_binop_lequal,
256   ir_binop_gequal,
257   ir_binop_equal,
258   ir_binop_nequal,
259   /*@}*/
260
261   /**
262    * \name Bit-wise binary operations.
263    */
264   /*@{*/
265   ir_binop_lshift,
266   ir_binop_rshift,
267   ir_binop_bit_and,
268   ir_binop_bit_xor,
269   ir_binop_bit_or,
270   /*@}*/
271
272   ir_binop_logic_and,
273   ir_binop_logic_xor,
274   ir_binop_logic_or,
275   ir_binop_logic_not,
276
277   ir_binop_dot,
278   ir_binop_min,
279   ir_binop_max,
280
281   ir_binop_pow
282};
283
284class ir_expression : public ir_rvalue {
285public:
286   ir_expression(int op, const struct glsl_type *type,
287		 ir_rvalue *, ir_rvalue *);
288
289   virtual void accept(ir_visitor *v)
290   {
291      v->visit(this);
292   }
293
294   ir_expression_operation operation;
295   ir_rvalue *operands[2];
296};
297
298
299/**
300 * IR instruction representing a function call
301 */
302class ir_call : public ir_rvalue {
303public:
304   ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
305      : ir_rvalue(), callee(callee)
306   {
307      assert(callee->return_type != NULL);
308      type = callee->return_type;
309      actual_parameters->move_nodes_to(& this->actual_parameters);
310   }
311
312   virtual void accept(ir_visitor *v)
313   {
314      v->visit(this);
315   }
316
317   /**
318    * Get a generic ir_call object when an error occurs
319    */
320   static ir_call *get_error_instruction();
321
322   /**
323    * Get an iterator for the set of acutal parameters
324    */
325   exec_list_iterator iterator()
326   {
327      return actual_parameters.iterator();
328   }
329
330   /**
331    * Get the name of the function being called.
332    */
333   const char *callee_name() const
334   {
335      /* FINISHME: This only works for functions that have definitions. */
336      return callee->definition->label;
337   }
338
339private:
340   ir_call()
341      : ir_rvalue(), callee(NULL)
342   {
343      /* empty */
344   }
345
346   const ir_function_signature *callee;
347   exec_list actual_parameters;
348};
349
350
351/**
352 * \name Jump-like IR instructions.
353 *
354 * These include \c break, \c continue, \c return, and \c discard.
355 */
356/*@{*/
357class ir_jump : public ir_instruction {
358protected:
359   ir_jump()
360      : ir_instruction()
361   {
362      /* empty */
363   }
364};
365
366class ir_return : public ir_jump {
367public:
368   ir_return()
369      : value(NULL)
370   {
371      /* empty */
372   }
373
374   ir_return(ir_rvalue *value)
375      : value(value)
376   {
377      /* empty */
378   }
379
380   ir_rvalue *get_value() const
381   {
382      return value;
383   }
384
385   virtual void accept(ir_visitor *v)
386   {
387      v->visit(this);
388   }
389
390private:
391   ir_rvalue *value;
392};
393/*@}*/
394
395
396struct ir_swizzle_mask {
397   unsigned x:2;
398   unsigned y:2;
399   unsigned z:2;
400   unsigned w:2;
401
402   /**
403    * Number of components in the swizzle.
404    */
405   unsigned num_components:3;
406
407   /**
408    * Does the swizzle contain duplicate components?
409    *
410    * L-value swizzles cannot contain duplicate components.
411    */
412   unsigned has_duplicates:1;
413};
414
415
416class ir_swizzle : public ir_rvalue {
417public:
418   ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
419              unsigned count);
420   /**
421    * Construct an ir_swizzle from the textual representation.  Can fail.
422    */
423   static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
424
425   virtual void accept(ir_visitor *v)
426   {
427      v->visit(this);
428   }
429
430   bool is_lvalue()
431   {
432      return val->is_lvalue();
433   }
434
435   ir_rvalue *val;
436   ir_swizzle_mask mask;
437};
438
439
440class ir_dereference : public ir_rvalue {
441public:
442   ir_dereference(struct ir_instruction *);
443
444   ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
445
446   virtual ir_dereference *as_dereference()
447   {
448      return this;
449   }
450
451   virtual void accept(ir_visitor *v)
452   {
453      v->visit(this);
454   }
455
456   bool is_lvalue()
457   {
458      return var != NULL;
459   }
460
461   enum {
462      ir_reference_variable,
463      ir_reference_array,
464      ir_reference_record
465   } mode;
466
467   /**
468    * Object being dereferenced.
469    *
470    * Must be either an \c ir_variable or an \c ir_rvalue.
471    */
472   ir_instruction *var;
473
474   union {
475      ir_rvalue *array_index;
476      const char *field;
477   } selector;
478};
479
480
481class ir_constant : public ir_rvalue {
482public:
483   ir_constant(const struct glsl_type *type, const void *data);
484   ir_constant(bool b);
485   ir_constant(unsigned int u);
486   ir_constant(int i);
487   ir_constant(float f);
488
489   virtual void accept(ir_visitor *v)
490   {
491      v->visit(this);
492   }
493
494   /**
495    * Value of the constant.
496    *
497    * The field used to back the values supplied by the constant is determined
498    * by the type associated with the \c ir_instruction.  Constants may be
499    * scalars, vectors, or matrices.
500    */
501   union {
502      unsigned u[16];
503      int i[16];
504      float f[16];
505      bool b[16];
506   } value;
507};
508
509
510extern void
511_mesa_glsl_initialize_variables(exec_list *instructions,
512				struct _mesa_glsl_parse_state *state);
513
514#endif /* IR_H */
515