2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "main/imports.h"
27 #include "main/context.h"
29 #include "slang_mem.h"
30 #include "program/prog_instruction.h"
31 #include "program/prog_print.h"
34 static const slang_ir_info IrInfo
[] = {
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 },
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 },
83 { IR_CMP
, "IR_CMP", OPCODE_CMP
, 4, 3 }, /* compare/select */
84 { IR_SEQ
, "IR_SEQ", OPCODE_NOP
, 0, 0 },
85 { IR_SCOPE
, "IR_SCOPE", OPCODE_NOP
, 0, 0 },
86 { IR_LABEL
, "IR_LABEL", OPCODE_NOP
, 0, 0 },
87 { IR_IF
, "IR_IF", OPCODE_NOP
, 0, 0 },
88 { IR_KILL
, "IR_KILL", OPCODE_NOP
, 0, 0 },
89 { IR_COND
, "IR_COND", OPCODE_NOP
, 0, 0 },
90 { IR_CALL
, "IR_CALL", OPCODE_NOP
, 0, 0 },
91 { IR_COPY
, "IR_COPY", OPCODE_NOP
, 0, 1 },
92 { IR_NOT
, "IR_NOT", OPCODE_NOP
, 1, 1 },
93 { IR_VAR
, "IR_VAR", OPCODE_NOP
, 0, 0 },
94 { IR_VAR_DECL
, "IR_VAR_DECL", OPCODE_NOP
, 0, 0 },
95 { IR_TEX
, "IR_TEX", OPCODE_TEX
, 4, 1 },
96 { IR_TEXB
, "IR_TEXB", OPCODE_TXB
, 4, 1 },
97 { IR_TEXP
, "IR_TEXP", OPCODE_TXP
, 4, 1 },
98 { IR_TEX_SH
, "IR_TEX_SH", OPCODE_TEX
, 4, 1 },
99 { IR_TEXB_SH
, "IR_TEXB_SH", OPCODE_TXB
, 4, 1 },
100 { IR_TEXP_SH
, "IR_TEXP_SH", OPCODE_TXP
, 4, 1 },
101 { IR_FLOAT
, "IR_FLOAT", OPCODE_NOP
, 0, 0 }, /* float literal */
102 { IR_FIELD
, "IR_FIELD", OPCODE_NOP
, 0, 0 },
103 { IR_ELEMENT
, "IR_ELEMENT", OPCODE_NOP
, 0, 0 },
104 { IR_SWIZZLE
, "IR_SWIZZLE", OPCODE_NOP
, 0, 0 },
105 { IR_NOP
, "IR_NOP", OPCODE_NOP
, 0, 0 },
110 const slang_ir_info
*
111 _slang_ir_info(slang_ir_opcode opcode
)
114 for (i
= 0; IrInfo
[i
].IrName
; i
++) {
115 if (IrInfo
[i
].IrOpcode
== opcode
) {
124 _slang_init_ir_storage(slang_ir_storage
*st
,
125 gl_register_file file
, GLint index
, GLint size
,
131 st
->Swizzle
= swizzle
;
133 st
->IsIndirect
= GL_FALSE
;
138 * Return a new slang_ir_storage object.
141 _slang_new_ir_storage(gl_register_file file
, GLint index
, GLint size
)
143 slang_ir_storage
*st
;
144 st
= (slang_ir_storage
*) _slang_alloc(sizeof(slang_ir_storage
));
149 st
->Swizzle
= SWIZZLE_NOOP
;
151 st
->IsIndirect
= GL_FALSE
;
158 * Return a new slang_ir_storage object.
161 _slang_new_ir_storage_swz(gl_register_file file
, GLint index
, GLint size
,
164 slang_ir_storage
*st
;
165 st
= (slang_ir_storage
*) _slang_alloc(sizeof(slang_ir_storage
));
170 st
->Swizzle
= swizzle
;
172 st
->IsIndirect
= GL_FALSE
;
179 * Return a new slang_ir_storage object.
182 _slang_new_ir_storage_relative(GLint index
, GLint size
,
183 slang_ir_storage
*parent
)
185 slang_ir_storage
*st
;
186 st
= (slang_ir_storage
*) _slang_alloc(sizeof(slang_ir_storage
));
188 st
->File
= PROGRAM_UNDEFINED
;
191 st
->Swizzle
= SWIZZLE_NOOP
;
193 st
->IsIndirect
= GL_FALSE
;
200 _slang_new_ir_storage_indirect(gl_register_file file
,
203 gl_register_file indirectFile
,
205 GLuint indirectSwizzle
)
207 slang_ir_storage
*st
;
208 st
= (slang_ir_storage
*) _slang_alloc(sizeof(slang_ir_storage
));
213 st
->Swizzle
= SWIZZLE_NOOP
;
214 st
->IsIndirect
= GL_TRUE
;
215 st
->IndirectFile
= indirectFile
;
216 st
->IndirectIndex
= indirectIndex
;
217 st
->IndirectSwizzle
= indirectSwizzle
;
224 * Allocate IR storage for a texture sampler.
225 * \param sampNum the sampler number/index
226 * \param texTarget one of TEXTURE_x_INDEX values
227 * \param size number of samplers (in case of sampler array)
230 _slang_new_ir_storage_sampler(GLint sampNum
, GLuint texTarget
, GLint size
)
232 slang_ir_storage
*st
;
233 assert(texTarget
< NUM_TEXTURE_TARGETS
);
234 st
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, size
);
236 st
->TexTarget
= texTarget
;
243 /* XXX temporary function */
245 _slang_copy_ir_storage(slang_ir_storage
*dst
, const slang_ir_storage
*src
)
254 _slang_ir_name(slang_ir_opcode opcode
)
256 return _slang_ir_info(opcode
)->IrName
;
261 #if 0 /* no longer needed with mempool */
263 * Since many IR nodes might point to the same IR storage info, we need
264 * to be careful when deleting things.
265 * Before deleting an IR tree, traverse it and do refcounting on the
266 * IR storage nodes. Use the refcount info during delete to free things
270 _slang_refcount_storage(slang_ir_node
*n
)
276 n
->Store
->RefCount
++;
277 for (i
= 0; i
< 3; i
++)
278 _slang_refcount_storage(n
->Children
[i
]);
284 _slang_free_ir(slang_ir_node
*n
)
292 n
->Store
->RefCount
--;
293 if (n
->Store
->RefCount
== 0) {
294 _slang_free(n
->Store
);
300 for (i
= 0; i
< 3; i
++)
301 _slang_free_ir(n
->Children
[i
]);
302 /* Do not free n->List since it's a child elsewhere */
308 * Recursively free an IR tree.
311 _slang_free_ir_tree(slang_ir_node
*n
)
314 _slang_refcount_storage(n
);
321 storage_string(const slang_ir_storage
*st
)
323 static const char *files
[] = {
340 assert(Elements(files
) == PROGRAM_FILE_MAX
);
343 _mesa_snprintf(s
, "%s[%d]", files
[st
->File
], st
->Index
);
345 _mesa_snprintf(s
, "%s[%d..%d]", files
[st
->File
], st
->Index
,
346 st
->Index
+ st
->Size
- 1);
348 assert(st
->File
< (GLint
) (sizeof(files
) / sizeof(files
[0])));
349 _mesa_snprintf(s
, sizeof(s
), "%s[%d]", files
[st
->File
], st
->Index
);
364 _slang_print_ir_tree(const slang_ir_node
*n
, int indent
)
371 if (n
->Opcode
!= IR_SEQ
)
373 printf("%3d:", indent
);
380 printf("SEQ at %p\n", (void*) n
);
382 assert(n
->Children
[0]);
383 assert(n
->Children
[1]);
384 _slang_print_ir_tree(n
->Children
[0], indent
+ IND
);
385 _slang_print_ir_tree(n
->Children
[1], indent
+ IND
);
388 printf("NEW SCOPE\n");
389 assert(!n
->Children
[1]);
390 _slang_print_ir_tree(n
->Children
[0], indent
+ 3);
394 _slang_print_ir_tree(n
->Children
[0], indent
+3);
395 _slang_print_ir_tree(n
->Children
[1], indent
+3);
398 printf("LABEL: %s\n", n
->Label
->Name
);
402 _slang_print_ir_tree(n
->Children
[0], indent
+ 3);
407 _slang_print_ir_tree(n
->Children
[0], indent
+3);
410 _slang_print_ir_tree(n
->Children
[1], indent
+3);
411 if (n
->Children
[2]) {
414 _slang_print_ir_tree(n
->Children
[2], indent
+3);
421 printf("BEGIN_SUB\n");
430 printf("CALL %s\n", n
->Label
->Name
);
435 _slang_print_ir_tree(n
->Children
[0], indent
+3);
436 if (n
->Children
[1]) {
439 _slang_print_ir_tree(n
->Children
[1], indent
+3);
450 case IR_BREAK_IF_TRUE
:
451 printf("BREAK_IF_TRUE\n");
452 _slang_print_ir_tree(n
->Children
[0], indent
+3);
454 case IR_CONT_IF_TRUE
:
455 printf("CONT_IF_TRUE\n");
456 _slang_print_ir_tree(n
->Children
[0], indent
+3);
460 printf("VAR %s%s at %s store %p\n",
461 (n
->Var
? (char *) n
->Var
->a_name
: "TEMP"),
462 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0),
463 storage_string(n
->Store
), (void*) n
->Store
);
466 printf("VAR_DECL %s (%p) at %s store %p\n",
467 (n
->Var
? (char *) n
->Var
->a_name
: "TEMP"),
468 (void*) n
->Var
, storage_string(n
->Store
),
472 printf("FIELD %s of\n", n
->Field
);
473 _slang_print_ir_tree(n
->Children
[0], indent
+3);
476 printf("FLOAT %g %g %g %g\n",
477 n
->Value
[0], n
->Value
[1], n
->Value
[2], n
->Value
[3]);
480 printf("INT_TO_FLOAT\n");
481 _slang_print_ir_tree(n
->Children
[0], indent
+3);
484 printf("FLOAT_TO_INT\n");
485 _slang_print_ir_tree(n
->Children
[0], indent
+3);
488 printf("SWIZZLE %s of (store %p) \n",
489 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0), (void*) n
->Store
);
490 _slang_print_ir_tree(n
->Children
[0], indent
+ 3);
493 printf("%s (%p, %p) (store %p)\n", _slang_ir_name(n
->Opcode
),
494 (void*) n
->Children
[0], (void*) n
->Children
[1], (void*) n
->Store
);
495 _slang_print_ir_tree(n
->Children
[0], indent
+3);
496 _slang_print_ir_tree(n
->Children
[1], indent
+3);