mesa: no longer need Writemask field in GLSL IR nodes
[mesa.git] / src / mesa / shader / slang / slang_ir.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/imports.h"
27 #include "main/context.h"
28 #include "slang_ir.h"
29 #include "slang_mem.h"
30 #include "shader/prog_print.h"
31
32
33 static const slang_ir_info IrInfo[] = {
34 /* binary ops */
35 { IR_ADD, "IR_ADD", OPCODE_ADD, 4, 2 },
36 { IR_SUB, "IR_SUB", OPCODE_SUB, 4, 2 },
37 { IR_MUL, "IR_MUL", OPCODE_MUL, 4, 2 },
38 { IR_DIV, "IR_DIV", OPCODE_NOP, 0, 2 }, /* XXX broke */
39 { IR_DOT4, "IR_DOT_4", OPCODE_DP4, 1, 2 },
40 { IR_DOT3, "IR_DOT_3", OPCODE_DP3, 1, 2 },
41 { IR_CROSS, "IR_CROSS", OPCODE_XPD, 3, 2 },
42 { IR_LRP, "IR_LRP", OPCODE_LRP, 4, 3 },
43 { IR_MIN, "IR_MIN", OPCODE_MIN, 4, 2 },
44 { IR_MAX, "IR_MAX", OPCODE_MAX, 4, 2 },
45 { IR_CLAMP, "IR_CLAMP", OPCODE_NOP, 4, 3 }, /* special case: emit_clamp() */
46 { IR_SEQUAL, "IR_SEQUAL", OPCODE_SEQ, 4, 2 },
47 { IR_SNEQUAL, "IR_SNEQUAL", OPCODE_SNE, 4, 2 },
48 { IR_SGE, "IR_SGE", OPCODE_SGE, 4, 2 },
49 { IR_SGT, "IR_SGT", OPCODE_SGT, 4, 2 },
50 { IR_SLE, "IR_SLE", OPCODE_SLE, 4, 2 },
51 { IR_SLT, "IR_SLT", OPCODE_SLT, 4, 2 },
52 { IR_POW, "IR_POW", OPCODE_POW, 1, 2 },
53 { IR_EQUAL, "IR_EQUAL", OPCODE_NOP, 1, 2 },
54 { IR_NOTEQUAL, "IR_NOTEQUAL", OPCODE_NOP, 1, 2 },
55
56 /* unary ops */
57 { IR_MOVE, "IR_MOVE", OPCODE_MOV, 4, 1 },
58 { IR_I_TO_F, "IR_I_TO_F", OPCODE_MOV, 4, 1 }, /* int[4] to float[4] */
59 { IR_F_TO_I, "IR_F_TO_I", OPCODE_INT, 4, 1 }, /* 4 floats to 4 ints */
60 { IR_EXP, "IR_EXP", OPCODE_EXP, 1, 1 },
61 { IR_EXP2, "IR_EXP2", OPCODE_EX2, 1, 1 },
62 { IR_LOG2, "IR_LOG2", OPCODE_LG2, 1, 1 },
63 { IR_RSQ, "IR_RSQ", OPCODE_RSQ, 1, 1 },
64 { IR_RCP, "IR_RCP", OPCODE_RCP, 1, 1 },
65 { IR_FLOOR, "IR_FLOOR", OPCODE_FLR, 4, 1 },
66 { IR_FRAC, "IR_FRAC", OPCODE_FRC, 4, 1 },
67 { IR_ABS, "IR_ABS", OPCODE_ABS, 4, 1 },
68 { IR_NEG, "IR_NEG", OPCODE_NOP, 4, 1 }, /* special case: emit_negation() */
69 { IR_DDX, "IR_DDX", OPCODE_DDX, 4, 1 },
70 { IR_DDY, "IR_DDY", OPCODE_DDY, 4, 1 },
71 { IR_SIN, "IR_SIN", OPCODE_SIN, 1, 1 },
72 { IR_COS, "IR_COS", OPCODE_COS, 1, 1 },
73 { IR_NOISE1, "IR_NOISE1", OPCODE_NOISE1, 1, 1 },
74 { IR_NOISE2, "IR_NOISE2", OPCODE_NOISE2, 1, 1 },
75 { IR_NOISE3, "IR_NOISE3", OPCODE_NOISE3, 1, 1 },
76 { IR_NOISE4, "IR_NOISE4", OPCODE_NOISE4, 1, 1 },
77
78 /* other */
79 { IR_SEQ, "IR_SEQ", OPCODE_NOP, 0, 0 },
80 { IR_SCOPE, "IR_SCOPE", OPCODE_NOP, 0, 0 },
81 { IR_LABEL, "IR_LABEL", OPCODE_NOP, 0, 0 },
82 { IR_IF, "IR_IF", OPCODE_NOP, 0, 0 },
83 { IR_KILL, "IR_KILL", OPCODE_NOP, 0, 0 },
84 { IR_COND, "IR_COND", OPCODE_NOP, 0, 0 },
85 { IR_CALL, "IR_CALL", OPCODE_NOP, 0, 0 },
86 { IR_COPY, "IR_COPY", OPCODE_NOP, 0, 1 },
87 { IR_NOT, "IR_NOT", OPCODE_NOP, 1, 1 },
88 { IR_VAR, "IR_VAR", OPCODE_NOP, 0, 0 },
89 { IR_VAR_DECL, "IR_VAR_DECL", OPCODE_NOP, 0, 0 },
90 { IR_TEX, "IR_TEX", OPCODE_TEX, 4, 1 },
91 { IR_TEXB, "IR_TEXB", OPCODE_TXB, 4, 1 },
92 { IR_TEXP, "IR_TEXP", OPCODE_TXP, 4, 1 },
93 { IR_FLOAT, "IR_FLOAT", OPCODE_NOP, 0, 0 }, /* float literal */
94 { IR_FIELD, "IR_FIELD", OPCODE_NOP, 0, 0 },
95 { IR_ELEMENT, "IR_ELEMENT", OPCODE_NOP, 0, 0 },
96 { IR_SWIZZLE, "IR_SWIZZLE", OPCODE_NOP, 0, 0 },
97 { IR_NOP, "IR_NOP", OPCODE_NOP, 0, 0 },
98 { 0, NULL, 0, 0, 0 }
99 };
100
101
102 const slang_ir_info *
103 _slang_ir_info(slang_ir_opcode opcode)
104 {
105 GLuint i;
106 for (i = 0; IrInfo[i].IrName; i++) {
107 if (IrInfo[i].IrOpcode == opcode) {
108 return IrInfo + i;
109 }
110 }
111 return NULL;
112 }
113
114
115 /**
116 * Return a new slang_ir_storage object.
117 */
118 slang_ir_storage *
119 _slang_new_ir_storage(enum register_file file, GLint index, GLint size)
120 {
121 slang_ir_storage *st;
122 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
123 if (st) {
124 st->File = file;
125 st->Index = index;
126 st->Size = size;
127 st->Swizzle = SWIZZLE_NOOP;
128 st->Parent = NULL;
129 }
130 return st;
131 }
132
133
134 /**
135 * Return a new slang_ir_storage object.
136 */
137 slang_ir_storage *
138 _slang_new_ir_storage_swz(enum register_file file, GLint index, GLint size,
139 GLuint swizzle)
140 {
141 slang_ir_storage *st;
142 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
143 if (st) {
144 st->File = file;
145 st->Index = index;
146 st->Size = size;
147 st->Swizzle = swizzle;
148 st->Parent = NULL;
149 }
150 return st;
151 }
152
153
154 /**
155 * Return a new slang_ir_storage object.
156 */
157 slang_ir_storage *
158 _slang_new_ir_storage_relative(GLint index, GLint size,
159 slang_ir_storage *parent)
160 {
161 slang_ir_storage *st;
162 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
163 if (st) {
164 st->File = PROGRAM_UNDEFINED;
165 st->Index = index;
166 st->Size = size;
167 st->Swizzle = SWIZZLE_NOOP;
168 st->Parent = parent;
169 }
170 return st;
171 }
172
173
174
175 static const char *
176 _slang_ir_name(slang_ir_opcode opcode)
177 {
178 return _slang_ir_info(opcode)->IrName;
179 }
180
181
182
183 #if 0 /* no longer needed with mempool */
184 /**
185 * Since many IR nodes might point to the same IR storage info, we need
186 * to be careful when deleting things.
187 * Before deleting an IR tree, traverse it and do refcounting on the
188 * IR storage nodes. Use the refcount info during delete to free things
189 * properly.
190 */
191 static void
192 _slang_refcount_storage(slang_ir_node *n)
193 {
194 GLuint i;
195 if (!n)
196 return;
197 if (n->Store)
198 n->Store->RefCount++;
199 for (i = 0; i < 3; i++)
200 _slang_refcount_storage(n->Children[i]);
201 }
202 #endif
203
204
205 static void
206 _slang_free_ir(slang_ir_node *n)
207 {
208 GLuint i;
209 if (!n)
210 return;
211
212 #if 0
213 if (n->Store) {
214 n->Store->RefCount--;
215 if (n->Store->RefCount == 0) {
216 _slang_free(n->Store);
217 n->Store = NULL;
218 }
219 }
220 #endif
221
222 for (i = 0; i < 3; i++)
223 _slang_free_ir(n->Children[i]);
224 /* Do not free n->List since it's a child elsewhere */
225 _slang_free(n);
226 }
227
228
229 /**
230 * Recursively free an IR tree.
231 */
232 void
233 _slang_free_ir_tree(slang_ir_node *n)
234 {
235 #if 0
236 _slang_refcount_storage(n);
237 #endif
238 _slang_free_ir(n);
239 }
240
241
242 static const char *
243 storage_string(const slang_ir_storage *st)
244 {
245 static const char *files[] = {
246 "TEMP",
247 "LOCAL_PARAM",
248 "ENV_PARAM",
249 "STATE",
250 "INPUT",
251 "OUTPUT",
252 "NAMED_PARAM",
253 "CONSTANT",
254 "UNIFORM",
255 "VARYING",
256 "WRITE_ONLY",
257 "ADDRESS",
258 "SAMPLER",
259 "UNDEFINED"
260 };
261 static char s[100];
262 assert(Elements(files) == PROGRAM_FILE_MAX);
263 #if 0
264 if (st->Size == 1)
265 sprintf(s, "%s[%d]", files[st->File], st->Index);
266 else
267 sprintf(s, "%s[%d..%d]", files[st->File], st->Index,
268 st->Index + st->Size - 1);
269 #endif
270 assert(st->File < (GLint) (sizeof(files) / sizeof(files[0])));
271 sprintf(s, "%s[%d]", files[st->File], st->Index);
272 return s;
273 }
274
275
276 static void
277 spaces(int n)
278 {
279 while (n-- > 0) {
280 printf(" ");
281 }
282 }
283
284
285 void
286 _slang_print_ir_tree(const slang_ir_node *n, int indent)
287 {
288 #define IND 0
289
290 if (!n)
291 return;
292 #if !IND
293 if (n->Opcode != IR_SEQ)
294 #else
295 printf("%3d:", indent);
296 #endif
297 spaces(indent);
298
299 switch (n->Opcode) {
300 case IR_SEQ:
301 #if IND
302 printf("SEQ at %p\n", (void*) n);
303 #endif
304 assert(n->Children[0]);
305 assert(n->Children[1]);
306 _slang_print_ir_tree(n->Children[0], indent + IND);
307 _slang_print_ir_tree(n->Children[1], indent + IND);
308 break;
309 case IR_SCOPE:
310 printf("NEW SCOPE\n");
311 assert(!n->Children[1]);
312 _slang_print_ir_tree(n->Children[0], indent + 3);
313 break;
314 case IR_COPY:
315 printf("COPY\n");
316 _slang_print_ir_tree(n->Children[0], indent+3);
317 _slang_print_ir_tree(n->Children[1], indent+3);
318 break;
319 case IR_LABEL:
320 printf("LABEL: %s\n", n->Label->Name);
321 break;
322 case IR_COND:
323 printf("COND\n");
324 _slang_print_ir_tree(n->Children[0], indent + 3);
325 break;
326
327 case IR_IF:
328 printf("IF \n");
329 _slang_print_ir_tree(n->Children[0], indent+3);
330 spaces(indent);
331 printf("THEN\n");
332 _slang_print_ir_tree(n->Children[1], indent+3);
333 if (n->Children[2]) {
334 spaces(indent);
335 printf("ELSE\n");
336 _slang_print_ir_tree(n->Children[2], indent+3);
337 }
338 spaces(indent);
339 printf("ENDIF\n");
340 break;
341
342 case IR_BEGIN_SUB:
343 printf("BEGIN_SUB\n");
344 break;
345 case IR_END_SUB:
346 printf("END_SUB\n");
347 break;
348 case IR_RETURN:
349 printf("RETURN\n");
350 break;
351 case IR_CALL:
352 printf("CALL %s\n", n->Label->Name);
353 break;
354
355 case IR_LOOP:
356 printf("LOOP\n");
357 _slang_print_ir_tree(n->Children[0], indent+3);
358 if (n->Children[1]) {
359 spaces(indent);
360 printf("TAIL:\n");
361 _slang_print_ir_tree(n->Children[1], indent+3);
362 }
363 spaces(indent);
364 printf("ENDLOOP\n");
365 break;
366 case IR_CONT:
367 printf("CONT\n");
368 break;
369 case IR_BREAK:
370 printf("BREAK\n");
371 break;
372 case IR_BREAK_IF_TRUE:
373 printf("BREAK_IF_TRUE\n");
374 _slang_print_ir_tree(n->Children[0], indent+3);
375 break;
376 case IR_CONT_IF_TRUE:
377 printf("CONT_IF_TRUE\n");
378 _slang_print_ir_tree(n->Children[0], indent+3);
379 break;
380
381 case IR_VAR:
382 printf("VAR %s%s at %s store %p\n",
383 (n->Var ? (char *) n->Var->a_name : "TEMP"),
384 _mesa_swizzle_string(n->Store->Swizzle, 0, 0),
385 storage_string(n->Store), (void*) n->Store);
386 break;
387 case IR_VAR_DECL:
388 printf("VAR_DECL %s (%p) at %s store %p\n",
389 (n->Var ? (char *) n->Var->a_name : "TEMP"),
390 (void*) n->Var, storage_string(n->Store),
391 (void*) n->Store);
392 break;
393 case IR_FIELD:
394 printf("FIELD %s of\n", n->Field);
395 _slang_print_ir_tree(n->Children[0], indent+3);
396 break;
397 case IR_FLOAT:
398 printf("FLOAT %g %g %g %g\n",
399 n->Value[0], n->Value[1], n->Value[2], n->Value[3]);
400 break;
401 case IR_I_TO_F:
402 printf("INT_TO_FLOAT\n");
403 _slang_print_ir_tree(n->Children[0], indent+3);
404 break;
405 case IR_F_TO_I:
406 printf("FLOAT_TO_INT\n");
407 _slang_print_ir_tree(n->Children[0], indent+3);
408 break;
409 case IR_SWIZZLE:
410 printf("SWIZZLE %s of (store %p) \n",
411 _mesa_swizzle_string(n->Store->Swizzle, 0, 0), (void*) n->Store);
412 _slang_print_ir_tree(n->Children[0], indent + 3);
413 break;
414 default:
415 printf("%s (%p, %p) (store %p)\n", _slang_ir_name(n->Opcode),
416 (void*) n->Children[0], (void*) n->Children[1], (void*) n->Store);
417 _slang_print_ir_tree(n->Children[0], indent+3);
418 _slang_print_ir_tree(n->Children[1], indent+3);
419 }
420 }