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