Initial commit. lol
[mesa.git] / ir.h
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 struct ir_program {
25 void *bong_hits;
26 };
27
28
29 enum ir_opcodes {
30 ir_op_var_decl,
31 ir_op_assign,
32 ir_op_expression,
33 ir_op_dereference,
34 ir_op_jump,
35 ir_op_label,
36 ir_op_constant,
37 ir_op_func_sig,
38 ir_op_func
39 };
40
41 /**
42 * Base class of all IR instructions
43 */
44 class ir_instruction : public simple_node {
45 public:
46 unsigned mode;
47 const struct glsl_type *type;
48
49 protected:
50 ir_instruction(int mode);
51
52 private:
53 /**
54 * Dummy constructor to catch bad constructors in derived classes.
55 *
56 * Every derived must use the constructor that sets the instructions
57 * mode. Having the \c void constructor private prevents derived classes
58 * from accidentally doing the wrong thing.
59 */
60 ir_instruction(void);
61 };
62
63
64 enum ir_variable_mode {
65 ir_var_auto = 0,
66 ir_var_uniform,
67 ir_var_in,
68 ir_var_out,
69 ir_var_inout
70 };
71
72 enum ir_varaible_interpolation {
73 ir_var_smooth = 0,
74 ir_var_flat,
75 ir_var_noperspective
76 };
77
78 class ir_variable : public ir_instruction {
79 public:
80 ir_variable(const struct glsl_type *, const char *);
81
82 const char *name;
83
84 unsigned read_only:1;
85 unsigned centroid:1;
86 unsigned invariant:1;
87
88 unsigned mode:3;
89 unsigned interpolation:2;
90 };
91
92
93 class ir_label : public ir_instruction {
94 public:
95 ir_label(const char *label);
96
97 const char *label;
98 };
99
100
101 /*@{*/
102 class ir_function_signature : public ir_instruction {
103 public:
104 ir_function_signature(void);
105
106 /**
107 * Function return type.
108 *
109 * \note This discards the optional precision qualifier.
110 */
111 const struct glsl_type *return_type;
112
113 /**
114 * List of function parameters stored as ir_variable objects.
115 */
116 struct simple_node parameters;
117
118 /**
119 * Pointer to the label that begins the function definition.
120 */
121 ir_label *definition;
122 };
123
124
125 /**
126 * Header for tracking functions in the symbol table
127 */
128 class ir_function : public ir_instruction {
129 public:
130 ir_function(void);
131
132 /**
133 * Name of the function.
134 */
135 const char *name;
136
137 struct simple_node signatures;
138 };
139 /*@}*/
140
141 class ir_expression;
142 class ir_dereference;
143
144 class ir_assignment : public ir_instruction {
145 public:
146 ir_assignment(ir_instruction *lhs, ir_instruction *rhs,
147 ir_expression *condition);
148
149 /**
150 * Left-hand side of the assignment.
151 */
152 ir_dereference *lhs;
153
154 /**
155 * Value being assigned
156 *
157 * This should be either \c ir_op_expression or \c ir_op_deference.
158 */
159 ir_instruction *rhs;
160
161 /**
162 * Optional condition for the assignment.
163 */
164 ir_expression *condition;
165 };
166
167
168 enum ir_expression_operation {
169 ir_unop_bit_not,
170 ir_unop_logic_not,
171 ir_unop_neg,
172 ir_unop_abs,
173 ir_unop_rcp,
174 ir_unop_rsq,
175 ir_unop_exp,
176 ir_unop_log,
177 ir_unop_f2i, /**< Float-to-integer conversion. */
178 ir_unop_i2f, /**< Integer-to-float conversion. */
179
180 /**
181 * \name Unary floating-point rounding operations.
182 */
183 /*@{*/
184 ir_unop_trunc,
185 ir_unop_ceil,
186 ir_unop_floor,
187 /*@}*/
188
189 ir_binop_add,
190 ir_binop_sub,
191 ir_binop_mul,
192 ir_binop_div,
193 ir_binop_mod,
194
195 /**
196 * \name Binary comparison operators
197 */
198 /*@{*/
199 ir_binop_less,
200 ir_binop_greater,
201 ir_binop_lequal,
202 ir_binop_gequal,
203 ir_binop_equal,
204 ir_binop_nequal,
205 /*@}*/
206
207 /**
208 * \name Bit-wise binary operations.
209 */
210 /*@{*/
211 ir_binop_lshift,
212 ir_binop_rshift,
213 ir_binop_bit_and,
214 ir_binop_bit_xor,
215 ir_binop_bit_or,
216 /*@}*/
217
218 ir_binop_logic_and,
219 ir_binop_logic_xor,
220 ir_binop_logic_or,
221 ir_binop_logic_not,
222
223 ir_binop_dot,
224 ir_binop_min,
225 ir_binop_max,
226
227 ir_binop_pow
228 };
229
230 class ir_expression : public ir_instruction {
231 public:
232 ir_expression(int op, const struct glsl_type *type,
233 ir_instruction *, ir_instruction *);
234
235 ir_expression_operation operation;
236 ir_instruction *operands[2];
237 };
238
239
240 struct ir_swizzle_mask {
241 unsigned x:2;
242 unsigned y:2;
243 unsigned z:2;
244 unsigned w:2;
245
246 /**
247 * Number of components in the swizzle.
248 */
249 unsigned num_components:2;
250
251 /**
252 * Does the swizzle contain duplicate components?
253 *
254 * L-value swizzles cannot contain duplicate components.
255 */
256 unsigned has_duplicates:1;
257 };
258
259 class ir_dereference : public ir_instruction {
260 public:
261 ir_dereference(struct ir_instruction *);
262
263 enum {
264 ir_reference_variable,
265 ir_reference_array,
266 ir_reference_record
267 } mode;
268
269 /**
270 * Object being dereferenced.
271 *
272 * Must be either an \c ir_variable or an \c ir_deference.
273 */
274 ir_instruction *var;
275
276 union {
277 ir_expression *array_index;
278 const char *field;
279 struct ir_swizzle_mask swizzle;
280 } selector;
281 };
282
283
284 class ir_constant : public ir_instruction {
285 public:
286 ir_constant(const struct glsl_type *type, const void *data);
287
288 /**
289 * Value of the constant.
290 *
291 * The field used to back the values supplied by the constant is determined
292 * by the type associated with the \c ir_instruction. Constants may be
293 * scalars, vectors, or matrices.
294 */
295 union {
296 unsigned u[16];
297 int i[16];
298 float f[16];
299 bool b[16];
300 } value;
301 };
302