a414036a362fda600aad02d4a34cd22408216c26
[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 "imports.h"
27 #include "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_I_TO_F, "IR_I_TO_F", OPCODE_NOP, 1, 1 },
58 { IR_F_TO_I, "IR_F_TO_I", OPCODE_INT, 4, 1 }, /* 4 floats to 4 ints */
59 { IR_EXP, "IR_EXP", OPCODE_EXP, 1, 1 },
60 { IR_EXP2, "IR_EXP2", OPCODE_EX2, 1, 1 },
61 { IR_LOG2, "IR_LOG2", OPCODE_LG2, 1, 1 },
62 { IR_RSQ, "IR_RSQ", OPCODE_RSQ, 1, 1 },
63 { IR_RCP, "IR_RCP", OPCODE_RCP, 1, 1 },
64 { IR_FLOOR, "IR_FLOOR", OPCODE_FLR, 4, 1 },
65 { IR_FRAC, "IR_FRAC", OPCODE_FRC, 4, 1 },
66 { IR_ABS, "IR_ABS", OPCODE_ABS, 4, 1 },
67 { IR_NEG, "IR_NEG", OPCODE_NOP, 4, 1 }, /* special case: emit_negation() */
68 { IR_DDX, "IR_DDX", OPCODE_DDX, 4, 1 },
69 { IR_DDY, "IR_DDY", OPCODE_DDY, 4, 1 },
70 { IR_SIN, "IR_SIN", OPCODE_SIN, 1, 1 },
71 { IR_COS, "IR_COS", OPCODE_COS, 1, 1 },
72 { IR_NOISE1, "IR_NOISE1", OPCODE_NOISE1, 1, 1 },
73 { IR_NOISE2, "IR_NOISE2", OPCODE_NOISE2, 1, 1 },
74 { IR_NOISE3, "IR_NOISE3", OPCODE_NOISE3, 1, 1 },
75 { IR_NOISE4, "IR_NOISE4", OPCODE_NOISE4, 1, 1 },
76
77 /* other */
78 { IR_SEQ, "IR_SEQ", OPCODE_NOP, 0, 0 },
79 { IR_SCOPE, "IR_SCOPE", OPCODE_NOP, 0, 0 },
80 { IR_LABEL, "IR_LABEL", OPCODE_NOP, 0, 0 },
81 { IR_IF, "IR_IF", OPCODE_NOP, 0, 0 },
82 { IR_KILL, "IR_KILL", OPCODE_NOP, 0, 0 },
83 { IR_COND, "IR_COND", OPCODE_NOP, 0, 0 },
84 { IR_CALL, "IR_CALL", OPCODE_NOP, 0, 0 },
85 { IR_MOVE, "IR_MOVE", OPCODE_NOP, 0, 1 },
86 { IR_NOT, "IR_NOT", OPCODE_NOP, 1, 1 },
87 { IR_VAR, "IR_VAR", OPCODE_NOP, 0, 0 },
88 { IR_VAR_DECL, "IR_VAR_DECL", OPCODE_NOP, 0, 0 },
89 { IR_TEX, "IR_TEX", OPCODE_TEX, 4, 1 },
90 { IR_TEXB, "IR_TEXB", OPCODE_TXB, 4, 1 },
91 { IR_TEXP, "IR_TEXP", OPCODE_TXP, 4, 1 },
92 { IR_FLOAT, "IR_FLOAT", OPCODE_NOP, 0, 0 }, /* float literal */
93 { IR_FIELD, "IR_FIELD", OPCODE_NOP, 0, 0 },
94 { IR_ELEMENT, "IR_ELEMENT", OPCODE_NOP, 0, 0 },
95 { IR_SWIZZLE, "IR_SWIZZLE", OPCODE_NOP, 0, 0 },
96 { IR_NOP, "IR_NOP", OPCODE_NOP, 0, 0 },
97 { 0, NULL, 0, 0, 0 }
98 };
99
100
101 const slang_ir_info *
102 _slang_ir_info(slang_ir_opcode opcode)
103 {
104 GLuint i;
105 for (i = 0; IrInfo[i].IrName; i++) {
106 if (IrInfo[i].IrOpcode == opcode) {
107 return IrInfo + i;
108 }
109 }
110 return NULL;
111 }
112
113
114 /**
115 * Return a new slang_ir_storage object.
116 */
117 slang_ir_storage *
118 _slang_new_ir_storage(enum register_file file, GLint index, GLint size)
119 {
120 slang_ir_storage *st;
121 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
122 if (st) {
123 st->File = file;
124 st->Index = index;
125 st->Size = size;
126 st->Swizzle = SWIZZLE_NOOP;
127 st->Parent = NULL;
128 }
129 return st;
130 }
131
132
133 /**
134 * Return a new slang_ir_storage object.
135 */
136 slang_ir_storage *
137 _slang_new_ir_storage_swz(enum register_file file, GLint index, GLint size,
138 GLuint swizzle)
139 {
140 slang_ir_storage *st;
141 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
142 if (st) {
143 st->File = file;
144 st->Index = index;
145 st->Size = size;
146 st->Swizzle = swizzle;
147 st->Parent = NULL;
148 }
149 return st;
150 }
151
152
153 /**
154 * Return a new slang_ir_storage object.
155 */
156 slang_ir_storage *
157 _slang_new_ir_storage_relative(GLint index, GLint size,
158 slang_ir_storage *parent)
159 {
160 slang_ir_storage *st;
161 st = (slang_ir_storage *) _slang_alloc(sizeof(slang_ir_storage));
162 if (st) {
163 st->File = PROGRAM_UNDEFINED;
164 st->Index = index;
165 st->Size = size;
166 st->Swizzle = SWIZZLE_NOOP;
167 st->Parent = parent;
168 }
169 return st;
170 }
171
172
173
174 static const char *
175 _slang_ir_name(slang_ir_opcode opcode)
176 {
177 return _slang_ir_info(opcode)->IrName;
178 }
179
180
181
182 #if 0 /* no longer needed with mempool */
183 /**
184 * Since many IR nodes might point to the same IR storage info, we need
185 * to be careful when deleting things.
186 * Before deleting an IR tree, traverse it and do refcounting on the
187 * IR storage nodes. Use the refcount info during delete to free things
188 * properly.
189 */
190 static void
191 _slang_refcount_storage(slang_ir_node *n)
192 {
193 GLuint i;
194 if (!n)
195 return;
196 if (n->Store)
197 n->Store->RefCount++;
198 for (i = 0; i < 3; i++)
199 _slang_refcount_storage(n->Children[i]);
200 }
201 #endif
202
203
204 static void
205 _slang_free_ir(slang_ir_node *n)
206 {
207 GLuint i;
208 if (!n)
209 return;
210
211 #if 0
212 if (n->Store) {
213 n->Store->RefCount--;
214 if (n->Store->RefCount == 0) {
215 _slang_free(n->Store);
216 n->Store = NULL;
217 }
218 }
219 #endif
220
221 for (i = 0; i < 3; i++)
222 _slang_free_ir(n->Children[i]);
223 /* Do not free n->List since it's a child elsewhere */
224 _slang_free(n);
225 }
226
227
228 /**
229 * Recursively free an IR tree.
230 */
231 void
232 _slang_free_ir_tree(slang_ir_node *n)
233 {
234 #if 0
235 _slang_refcount_storage(n);
236 #endif
237 _slang_free_ir(n);
238 }
239
240
241
242 static const char *
243 writemask_string(GLuint writemask)
244 {
245 static char s[6];
246 GLuint i, j = 0;
247 s[j++] = '.';
248 for (i = 0; i < 4; i++) {
249 if (writemask & (1 << i))
250 s[j++] = "xyzw"[i];
251 }
252 s[j] = 0;
253 return s;
254 }
255
256
257 static const char *
258 storage_string(const slang_ir_storage *st)
259 {
260 static const char *files[] = {
261 "TEMP",
262 "LOCAL_PARAM",
263 "ENV_PARAM",
264 "STATE",
265 "INPUT",
266 "OUTPUT",
267 "NAMED_PARAM",
268 "CONSTANT",
269 "UNIFORM",
270 "VARYING",
271 "WRITE_ONLY",
272 "ADDRESS",
273 "SAMPLER",
274 "UNDEFINED"
275 };
276 static char s[100];
277 assert(Elements(files) == PROGRAM_FILE_MAX);
278 #if 0
279 if (st->Size == 1)
280 sprintf(s, "%s[%d]", files[st->File], st->Index);
281 else
282 sprintf(s, "%s[%d..%d]", files[st->File], st->Index,
283 st->Index + st->Size - 1);
284 #endif
285 assert(st->File < (GLint) (sizeof(files) / sizeof(files[0])));
286 sprintf(s, "%s[%d]", files[st->File], st->Index);
287 return s;
288 }
289
290
291 static void
292 spaces(int n)
293 {
294 while (n-- > 0) {
295 printf(" ");
296 }
297 }
298
299
300 void
301 _slang_print_ir_tree(const slang_ir_node *n, int indent)
302 {
303 #define IND 0
304
305 if (!n)
306 return;
307 #if !IND
308 if (n->Opcode != IR_SEQ)
309 #else
310 printf("%3d:", indent);
311 #endif
312 spaces(indent);
313
314 switch (n->Opcode) {
315 case IR_SEQ:
316 #if IND
317 printf("SEQ at %p\n", (void*) n);
318 #endif
319 assert(n->Children[0]);
320 assert(n->Children[1]);
321 _slang_print_ir_tree(n->Children[0], indent + IND);
322 _slang_print_ir_tree(n->Children[1], indent + IND);
323 break;
324 case IR_SCOPE:
325 printf("NEW SCOPE\n");
326 assert(!n->Children[1]);
327 _slang_print_ir_tree(n->Children[0], indent + 3);
328 break;
329 case IR_MOVE:
330 printf("MOVE (writemask = %s)\n", writemask_string(n->Writemask));
331 _slang_print_ir_tree(n->Children[0], indent+3);
332 _slang_print_ir_tree(n->Children[1], indent+3);
333 break;
334 case IR_LABEL:
335 printf("LABEL: %s\n", n->Label->Name);
336 break;
337 case IR_COND:
338 printf("COND\n");
339 _slang_print_ir_tree(n->Children[0], indent + 3);
340 break;
341
342 case IR_IF:
343 printf("IF \n");
344 _slang_print_ir_tree(n->Children[0], indent+3);
345 spaces(indent);
346 printf("THEN\n");
347 _slang_print_ir_tree(n->Children[1], indent+3);
348 if (n->Children[2]) {
349 spaces(indent);
350 printf("ELSE\n");
351 _slang_print_ir_tree(n->Children[2], indent+3);
352 }
353 spaces(indent);
354 printf("ENDIF\n");
355 break;
356
357 case IR_BEGIN_SUB:
358 printf("BEGIN_SUB\n");
359 break;
360 case IR_END_SUB:
361 printf("END_SUB\n");
362 break;
363 case IR_RETURN:
364 printf("RETURN\n");
365 break;
366 case IR_CALL:
367 printf("CALL %s\n", n->Label->Name);
368 break;
369
370 case IR_LOOP:
371 printf("LOOP\n");
372 _slang_print_ir_tree(n->Children[0], indent+3);
373 if (n->Children[1]) {
374 spaces(indent);
375 printf("TAIL:\n");
376 _slang_print_ir_tree(n->Children[1], indent+3);
377 }
378 spaces(indent);
379 printf("ENDLOOP\n");
380 break;
381 case IR_CONT:
382 printf("CONT\n");
383 break;
384 case IR_BREAK:
385 printf("BREAK\n");
386 break;
387 case IR_BREAK_IF_TRUE:
388 printf("BREAK_IF_TRUE\n");
389 _slang_print_ir_tree(n->Children[0], indent+3);
390 break;
391 case IR_CONT_IF_TRUE:
392 printf("CONT_IF_TRUE\n");
393 _slang_print_ir_tree(n->Children[0], indent+3);
394 break;
395
396 case IR_VAR:
397 printf("VAR %s%s at %s store %p\n",
398 (n->Var ? (char *) n->Var->a_name : "TEMP"),
399 _mesa_swizzle_string(n->Store->Swizzle, 0, 0),
400 storage_string(n->Store), (void*) n->Store);
401 break;
402 case IR_VAR_DECL:
403 printf("VAR_DECL %s (%p) at %s store %p\n",
404 (n->Var ? (char *) n->Var->a_name : "TEMP"),
405 (void*) n->Var, storage_string(n->Store),
406 (void*) n->Store);
407 break;
408 case IR_FIELD:
409 printf("FIELD %s of\n", n->Field);
410 _slang_print_ir_tree(n->Children[0], indent+3);
411 break;
412 case IR_FLOAT:
413 printf("FLOAT %g %g %g %g\n",
414 n->Value[0], n->Value[1], n->Value[2], n->Value[3]);
415 break;
416 case IR_I_TO_F:
417 printf("INT_TO_FLOAT\n");
418 _slang_print_ir_tree(n->Children[0], indent+3);
419 break;
420 case IR_F_TO_I:
421 printf("FLOAT_TO_INT\n");
422 _slang_print_ir_tree(n->Children[0], indent+3);
423 break;
424 case IR_SWIZZLE:
425 printf("SWIZZLE %s of (store %p) \n",
426 _mesa_swizzle_string(n->Store->Swizzle, 0, 0), (void*) n->Store);
427 _slang_print_ir_tree(n->Children[0], indent + 3);
428 break;
429 default:
430 printf("%s (%p, %p) (store %p)\n", _slang_ir_name(n->Opcode),
431 (void*) n->Children[0], (void*) n->Children[1], (void*) n->Store);
432 _slang_print_ir_tree(n->Children[0], indent+3);
433 _slang_print_ir_tree(n->Children[1], indent+3);
434 }
435 }