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