gallium: fix refcount bug introduced in eb20e2984
[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
243 static const char *
244 writemask_string(GLuint writemask)
245 {
246 static char s[6];
247 GLuint i, j = 0;
248 s[j++] = '.';
249 for (i = 0; i < 4; i++) {
250 if (writemask & (1 << i))
251 s[j++] = "xyzw"[i];
252 }
253 s[j] = 0;
254 return s;
255 }
256
257
258 static const char *
259 storage_string(const slang_ir_storage *st)
260 {
261 static const char *files[] = {
262 "TEMP",
263 "LOCAL_PARAM",
264 "ENV_PARAM",
265 "STATE",
266 "INPUT",
267 "OUTPUT",
268 "NAMED_PARAM",
269 "CONSTANT",
270 "UNIFORM",
271 "VARYING",
272 "WRITE_ONLY",
273 "ADDRESS",
274 "SAMPLER",
275 "UNDEFINED"
276 };
277 static char s[100];
278 assert(Elements(files) == PROGRAM_FILE_MAX);
279 #if 0
280 if (st->Size == 1)
281 sprintf(s, "%s[%d]", files[st->File], st->Index);
282 else
283 sprintf(s, "%s[%d..%d]", files[st->File], st->Index,
284 st->Index + st->Size - 1);
285 #endif
286 assert(st->File < (GLint) (sizeof(files) / sizeof(files[0])));
287 sprintf(s, "%s[%d]", files[st->File], st->Index);
288 return s;
289 }
290
291
292 static void
293 spaces(int n)
294 {
295 while (n-- > 0) {
296 printf(" ");
297 }
298 }
299
300
301 void
302 _slang_print_ir_tree(const slang_ir_node *n, int indent)
303 {
304 #define IND 0
305
306 if (!n)
307 return;
308 #if !IND
309 if (n->Opcode != IR_SEQ)
310 #else
311 printf("%3d:", indent);
312 #endif
313 spaces(indent);
314
315 switch (n->Opcode) {
316 case IR_SEQ:
317 #if IND
318 printf("SEQ at %p\n", (void*) n);
319 #endif
320 assert(n->Children[0]);
321 assert(n->Children[1]);
322 _slang_print_ir_tree(n->Children[0], indent + IND);
323 _slang_print_ir_tree(n->Children[1], indent + IND);
324 break;
325 case IR_SCOPE:
326 printf("NEW SCOPE\n");
327 assert(!n->Children[1]);
328 _slang_print_ir_tree(n->Children[0], indent + 3);
329 break;
330 case IR_COPY:
331 printf("COPY (writemask = %s)\n", writemask_string(n->Writemask));
332 _slang_print_ir_tree(n->Children[0], indent+3);
333 _slang_print_ir_tree(n->Children[1], indent+3);
334 break;
335 case IR_LABEL:
336 printf("LABEL: %s\n", n->Label->Name);
337 break;
338 case IR_COND:
339 printf("COND\n");
340 _slang_print_ir_tree(n->Children[0], indent + 3);
341 break;
342
343 case IR_IF:
344 printf("IF \n");
345 _slang_print_ir_tree(n->Children[0], indent+3);
346 spaces(indent);
347 printf("THEN\n");
348 _slang_print_ir_tree(n->Children[1], indent+3);
349 if (n->Children[2]) {
350 spaces(indent);
351 printf("ELSE\n");
352 _slang_print_ir_tree(n->Children[2], indent+3);
353 }
354 spaces(indent);
355 printf("ENDIF\n");
356 break;
357
358 case IR_BEGIN_SUB:
359 printf("BEGIN_SUB\n");
360 break;
361 case IR_END_SUB:
362 printf("END_SUB\n");
363 break;
364 case IR_RETURN:
365 printf("RETURN\n");
366 break;
367 case IR_CALL:
368 printf("CALL %s\n", n->Label->Name);
369 break;
370
371 case IR_LOOP:
372 printf("LOOP\n");
373 _slang_print_ir_tree(n->Children[0], indent+3);
374 if (n->Children[1]) {
375 spaces(indent);
376 printf("TAIL:\n");
377 _slang_print_ir_tree(n->Children[1], indent+3);
378 }
379 spaces(indent);
380 printf("ENDLOOP\n");
381 break;
382 case IR_CONT:
383 printf("CONT\n");
384 break;
385 case IR_BREAK:
386 printf("BREAK\n");
387 break;
388 case IR_BREAK_IF_TRUE:
389 printf("BREAK_IF_TRUE\n");
390 _slang_print_ir_tree(n->Children[0], indent+3);
391 break;
392 case IR_CONT_IF_TRUE:
393 printf("CONT_IF_TRUE\n");
394 _slang_print_ir_tree(n->Children[0], indent+3);
395 break;
396
397 case IR_VAR:
398 printf("VAR %s%s at %s store %p\n",
399 (n->Var ? (char *) n->Var->a_name : "TEMP"),
400 _mesa_swizzle_string(n->Store->Swizzle, 0, 0),
401 storage_string(n->Store), (void*) n->Store);
402 break;
403 case IR_VAR_DECL:
404 printf("VAR_DECL %s (%p) at %s store %p\n",
405 (n->Var ? (char *) n->Var->a_name : "TEMP"),
406 (void*) n->Var, storage_string(n->Store),
407 (void*) n->Store);
408 break;
409 case IR_FIELD:
410 printf("FIELD %s of\n", n->Field);
411 _slang_print_ir_tree(n->Children[0], indent+3);
412 break;
413 case IR_FLOAT:
414 printf("FLOAT %g %g %g %g\n",
415 n->Value[0], n->Value[1], n->Value[2], n->Value[3]);
416 break;
417 case IR_I_TO_F:
418 printf("INT_TO_FLOAT\n");
419 _slang_print_ir_tree(n->Children[0], indent+3);
420 break;
421 case IR_F_TO_I:
422 printf("FLOAT_TO_INT\n");
423 _slang_print_ir_tree(n->Children[0], indent+3);
424 break;
425 case IR_SWIZZLE:
426 printf("SWIZZLE %s of (store %p) \n",
427 _mesa_swizzle_string(n->Store->Swizzle, 0, 0), (void*) n->Store);
428 _slang_print_ir_tree(n->Children[0], indent + 3);
429 break;
430 default:
431 printf("%s (%p, %p) (store %p)\n", _slang_ir_name(n->Opcode),
432 (void*) n->Children[0], (void*) n->Children[1], (void*) n->Store);
433 _slang_print_ir_tree(n->Children[0], indent+3);
434 _slang_print_ir_tree(n->Children[1], indent+3);
435 }
436 }