2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul 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.
27 * Emit program instructions (PI code) from IR trees.
34 *** To emit GPU instructions, we basically just do an in-order traversal
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "slang_builtin.h"
47 #include "slang_emit.h"
50 #define PEEPHOLE_OPTIMIZATIONS 1
54 /* XXX temporarily here */
61 struct gl_program
*prog
;
62 /* code-gen options */
63 GLboolean EmitHighLevelInstructions
;
64 GLboolean EmitCondCodes
;
65 GLboolean EmitComments
;
70 * Assembly and IR info
74 slang_ir_opcode IrOpcode
;
76 gl_inst_opcode InstOpcode
;
77 GLuint ResultSize
, NumParams
;
82 static const slang_ir_info IrInfo
[] = {
84 { IR_ADD
, "IR_ADD", OPCODE_ADD
, 4, 2 },
85 { IR_SUB
, "IR_SUB", OPCODE_SUB
, 4, 2 },
86 { IR_MUL
, "IR_MUL", OPCODE_MUL
, 4, 2 },
87 { IR_DIV
, "IR_DIV", OPCODE_NOP
, 0, 2 }, /* XXX broke */
88 { IR_DOT4
, "IR_DOT_4", OPCODE_DP4
, 1, 2 },
89 { IR_DOT3
, "IR_DOT_3", OPCODE_DP3
, 1, 2 },
90 { IR_CROSS
, "IR_CROSS", OPCODE_XPD
, 3, 2 },
91 { IR_LRP
, "IR_LRP", OPCODE_LRP
, 4, 3 },
92 { IR_MIN
, "IR_MIN", OPCODE_MIN
, 4, 2 },
93 { IR_MAX
, "IR_MAX", OPCODE_MAX
, 4, 2 },
94 { IR_CLAMP
, "IR_CLAMP", OPCODE_NOP
, 4, 3 }, /* special case: emit_clamp() */
95 { IR_SEQUAL
, "IR_SEQUAL", OPCODE_SEQ
, 4, 2 },
96 { IR_SNEQUAL
, "IR_SNEQUAL", OPCODE_SNE
, 4, 2 },
97 { IR_SGE
, "IR_SGE", OPCODE_SGE
, 4, 2 },
98 { IR_SGT
, "IR_SGT", OPCODE_SGT
, 4, 2 },
99 { IR_SLE
, "IR_SLE", OPCODE_SLE
, 4, 2 },
100 { IR_SLT
, "IR_SLT", OPCODE_SLT
, 4, 2 },
101 { IR_POW
, "IR_POW", OPCODE_POW
, 1, 2 },
103 { IR_I_TO_F
, "IR_I_TO_F", OPCODE_NOP
, 1, 1 },
104 { IR_F_TO_I
, "IR_F_TO_I", OPCODE_INT
, 4, 1 }, /* 4 floats to 4 ints */
105 { IR_EXP
, "IR_EXP", OPCODE_EXP
, 1, 1 },
106 { IR_EXP2
, "IR_EXP2", OPCODE_EX2
, 1, 1 },
107 { IR_LOG2
, "IR_LOG2", OPCODE_LG2
, 1, 1 },
108 { IR_RSQ
, "IR_RSQ", OPCODE_RSQ
, 1, 1 },
109 { IR_RCP
, "IR_RCP", OPCODE_RCP
, 1, 1 },
110 { IR_FLOOR
, "IR_FLOOR", OPCODE_FLR
, 4, 1 },
111 { IR_FRAC
, "IR_FRAC", OPCODE_FRC
, 4, 1 },
112 { IR_ABS
, "IR_ABS", OPCODE_ABS
, 4, 1 },
113 { IR_NEG
, "IR_NEG", OPCODE_NOP
, 4, 1 }, /* special case: emit_negation() */
114 { IR_DDX
, "IR_DDX", OPCODE_DDX
, 4, 1 },
115 { IR_DDX
, "IR_DDY", OPCODE_DDX
, 4, 1 },
116 { IR_SIN
, "IR_SIN", OPCODE_SIN
, 1, 1 },
117 { IR_COS
, "IR_COS", OPCODE_COS
, 1, 1 },
118 { IR_NOISE1
, "IR_NOISE1", OPCODE_NOISE1
, 1, 1 },
119 { IR_NOISE2
, "IR_NOISE2", OPCODE_NOISE2
, 1, 1 },
120 { IR_NOISE3
, "IR_NOISE3", OPCODE_NOISE3
, 1, 1 },
121 { IR_NOISE4
, "IR_NOISE4", OPCODE_NOISE4
, 1, 1 },
124 { IR_SEQ
, "IR_SEQ", OPCODE_NOP
, 0, 0 },
125 { IR_SCOPE
, "IR_SCOPE", OPCODE_NOP
, 0, 0 },
126 { IR_LABEL
, "IR_LABEL", OPCODE_NOP
, 0, 0 },
127 { IR_JUMP
, "IR_JUMP", OPCODE_NOP
, 0, 0 },
128 { IR_IF
, "IR_IF", OPCODE_NOP
, 0, 0 },
129 { IR_KILL
, "IR_KILL", OPCODE_NOP
, 0, 0 },
130 { IR_COND
, "IR_COND", OPCODE_NOP
, 0, 0 },
131 { IR_CALL
, "IR_CALL", OPCODE_NOP
, 0, 0 },
132 { IR_MOVE
, "IR_MOVE", OPCODE_NOP
, 0, 1 },
133 { IR_NOT
, "IR_NOT", OPCODE_NOP
, 1, 1 },
134 { IR_VAR
, "IR_VAR", OPCODE_NOP
, 0, 0 },
135 { IR_VAR_DECL
, "IR_VAR_DECL", OPCODE_NOP
, 0, 0 },
136 { IR_TEX
, "IR_TEX", OPCODE_TEX
, 4, 1 },
137 { IR_TEXB
, "IR_TEXB", OPCODE_TXB
, 4, 1 },
138 { IR_TEXP
, "IR_TEXP", OPCODE_TXP
, 4, 1 },
139 { IR_FLOAT
, "IR_FLOAT", OPCODE_NOP
, 0, 0 }, /* float literal */
140 { IR_FIELD
, "IR_FIELD", OPCODE_NOP
, 0, 0 },
141 { IR_ELEMENT
, "IR_ELEMENT", OPCODE_NOP
, 0, 0 },
142 { IR_SWIZZLE
, "IR_SWIZZLE", OPCODE_NOP
, 0, 0 },
143 { IR_NOP
, NULL
, OPCODE_NOP
, 0, 0 }
147 static const slang_ir_info
*
148 slang_find_ir_info(slang_ir_opcode opcode
)
151 for (i
= 0; IrInfo
[i
].IrName
; i
++) {
152 if (IrInfo
[i
].IrOpcode
== opcode
) {
160 slang_ir_name(slang_ir_opcode opcode
)
162 return slang_find_ir_info(opcode
)->IrName
;
167 * Swizzle a swizzle. That is, return swz2(swz1)
170 swizzle_swizzle(GLuint swz1
, GLuint swz2
)
173 for (i
= 0; i
< 4; i
++) {
174 GLuint c
= GET_SWZ(swz2
, i
);
175 s
[i
] = GET_SWZ(swz1
, c
);
177 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
183 _slang_new_ir_storage(enum register_file file
, GLint index
, GLint size
)
185 slang_ir_storage
*st
;
186 st
= (slang_ir_storage
*) _mesa_calloc(sizeof(slang_ir_storage
));
191 st
->Swizzle
= SWIZZLE_NOOP
;
198 swizzle_string(GLuint swizzle
)
203 for (i
= 1; i
< 5; i
++) {
204 s
[i
] = "xyzw"[GET_SWZ(swizzle
, i
-1)];
211 writemask_string(GLuint writemask
)
216 for (i
= 0; i
< 4; i
++) {
217 if (writemask
& (1 << i
))
225 storage_string(const slang_ir_storage
*st
)
227 static const char *files
[] = {
245 sprintf(s
, "%s[%d]", files
[st
->File
], st
->Index
);
247 sprintf(s
, "%s[%d..%d]", files
[st
->File
], st
->Index
,
248 st
->Index
+ st
->Size
- 1);
250 assert(st
->File
< (GLint
) (sizeof(files
) / sizeof(files
[0])));
251 sprintf(s
, "%s[%d]", files
[st
->File
], st
->Index
);
266 slang_print_ir(const slang_ir_node
*n
, int indent
)
271 if (n
->Opcode
!= IR_SEQ
)
273 printf("%3d:", indent
);
280 printf("SEQ at %p\n", (void*) n
);
282 assert(n
->Children
[0]);
283 assert(n
->Children
[1]);
284 slang_print_ir(n
->Children
[0], indent
+ IND
);
285 slang_print_ir(n
->Children
[1], indent
+ IND
);
288 printf("NEW SCOPE\n");
289 assert(!n
->Children
[1]);
290 slang_print_ir(n
->Children
[0], indent
+ 3);
293 printf("MOVE (writemask = %s)\n", writemask_string(n
->Writemask
));
294 slang_print_ir(n
->Children
[0], indent
+3);
295 slang_print_ir(n
->Children
[1], indent
+3);
298 printf("LABEL: %s\n", n
->Label
->Name
);
302 slang_print_ir(n
->Children
[0], indent
+ 3);
305 printf("JUMP %s\n", n
->Label
->Name
);
310 slang_print_ir(n
->Children
[0], indent
+3);
313 slang_print_ir(n
->Children
[1], indent
+3);
314 if (n
->Children
[2]) {
317 slang_print_ir(n
->Children
[2], indent
+3);
323 printf("BEGIN_SUB\n");
337 slang_print_ir(n
->Children
[0], indent
+3);
347 case IR_BREAK_IF_FALSE
:
348 printf("BREAK_IF_FALSE\n");
349 slang_print_ir(n
->Children
[0], indent
+3);
351 case IR_BREAK_IF_TRUE
:
352 printf("BREAK_IF_TRUE\n");
353 slang_print_ir(n
->Children
[0], indent
+3);
355 case IR_CONT_IF_FALSE
:
356 printf("CONT_IF_FALSE\n");
357 slang_print_ir(n
->Children
[0], indent
+3);
359 case IR_CONT_IF_TRUE
:
360 printf("CONT_IF_TRUE\n");
361 slang_print_ir(n
->Children
[0], indent
+3);
365 printf("VAR %s%s at %s store %p\n",
366 (n
->Var
? (char *) n
->Var
->a_name
: "TEMP"),
367 swizzle_string(n
->Store
->Swizzle
),
368 storage_string(n
->Store
), (void*) n
->Store
);
371 printf("VAR_DECL %s (%p) at %s store %p\n",
372 (n
->Var
? (char *) n
->Var
->a_name
: "TEMP"),
373 (void*) n
->Var
, storage_string(n
->Store
),
377 printf("FIELD %s of\n", n
->Field
);
378 slang_print_ir(n
->Children
[0], indent
+3);
381 printf("FLOAT %g %g %g %g\n",
382 n
->Value
[0], n
->Value
[1], n
->Value
[2], n
->Value
[3]);
385 printf("INT_TO_FLOAT\n");
386 slang_print_ir(n
->Children
[0], indent
+3);
389 printf("FLOAT_TO_INT\n");
390 slang_print_ir(n
->Children
[0], indent
+3);
393 printf("SWIZZLE %s of (store %p) \n",
394 swizzle_string(n
->Store
->Swizzle
), (void*) n
->Store
);
395 slang_print_ir(n
->Children
[0], indent
+ 3);
398 printf("%s (%p, %p) (store %p)\n", slang_ir_name(n
->Opcode
),
399 (void*) n
->Children
[0], (void*) n
->Children
[1], (void*) n
->Store
);
400 slang_print_ir(n
->Children
[0], indent
+3);
401 slang_print_ir(n
->Children
[1], indent
+3);
407 * Allocate temporary storage for an intermediate result (such as for
408 * a multiply or add, etc.
411 alloc_temp_storage(slang_emit_info
*emitInfo
, slang_ir_node
*n
, GLint size
)
416 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
417 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
418 slang_info_log_error(emitInfo
->log
,
419 "Ran out of registers, too many temporaries");
427 * Free temporary storage, if n->Store is, in fact, temp storage.
431 free_temp_storage(slang_var_table
*vt
, slang_ir_node
*n
)
433 if (n
->Store
->File
== PROGRAM_TEMPORARY
&& n
->Store
->Index
>= 0) {
434 if (_slang_is_temp(vt
, n
->Store
)) {
435 _slang_free_temp(vt
, n
->Store
);
436 n
->Store
->Index
= -1;
444 * Convert IR storage to an instruction dst register.
447 storage_to_dst_reg(struct prog_dst_register
*dst
, const slang_ir_storage
*st
,
450 static const GLuint defaultWritemask
[4] = {
452 WRITEMASK_X
| WRITEMASK_Y
,
453 WRITEMASK_X
| WRITEMASK_Y
| WRITEMASK_Z
,
454 WRITEMASK_X
| WRITEMASK_Y
| WRITEMASK_Z
| WRITEMASK_W
456 assert(st
->Index
>= 0);
457 dst
->File
= st
->File
;
458 dst
->Index
= st
->Index
;
459 assert(st
->File
!= PROGRAM_UNDEFINED
);
460 assert(st
->Size
>= 1);
461 assert(st
->Size
<= 4);
463 GLuint comp
= GET_SWZ(st
->Swizzle
, 0);
465 assert(writemask
& WRITEMASK_X
);
467 assert((writemask == WRITEMASK_X) ||
468 (writemask == WRITEMASK_Y) ||
469 (writemask == WRITEMASK_Z) ||
470 (writemask == WRITEMASK_W));
472 dst
->WriteMask
= WRITEMASK_X
<< comp
;
475 dst
->WriteMask
= defaultWritemask
[st
->Size
- 1] & writemask
;
481 * Convert IR storage to an instruction src register.
484 storage_to_src_reg(struct prog_src_register
*src
, const slang_ir_storage
*st
)
486 static const GLuint defaultSwizzle
[4] = {
487 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
488 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
489 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
490 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
)
492 assert(st
->File
>= 0);
493 assert(st
->File
< PROGRAM_UNDEFINED
);
494 assert(st
->Size
>= 1);
495 assert(st
->Size
<= 4);
496 src
->File
= st
->File
;
497 src
->Index
= st
->Index
;
498 if (st
->Swizzle
!= SWIZZLE_NOOP
)
499 src
->Swizzle
= st
->Swizzle
;
501 src
->Swizzle
= defaultSwizzle
[st
->Size
- 1]; /*XXX really need this?*/
503 assert(GET_SWZ(src
->Swizzle
, 0) != SWIZZLE_NIL
);
504 assert(GET_SWZ(src
->Swizzle
, 1) != SWIZZLE_NIL
);
505 assert(GET_SWZ(src
->Swizzle
, 2) != SWIZZLE_NIL
);
506 assert(GET_SWZ(src
->Swizzle
, 3) != SWIZZLE_NIL
);
512 * Add new instruction at end of given program.
513 * \param prog the program to append instruction onto
514 * \param opcode opcode for the new instruction
515 * \return pointer to the new instruction
517 static struct prog_instruction
*
518 new_instruction(slang_emit_info
*emitInfo
, gl_inst_opcode opcode
)
520 struct gl_program
*prog
= emitInfo
->prog
;
521 struct prog_instruction
*inst
;
522 prog
->Instructions
= _mesa_realloc_instructions(prog
->Instructions
,
523 prog
->NumInstructions
,
524 prog
->NumInstructions
+ 1);
525 inst
= prog
->Instructions
+ prog
->NumInstructions
;
526 prog
->NumInstructions
++;
527 _mesa_init_instructions(inst
, 1);
528 inst
->Opcode
= opcode
;
529 inst
->BranchTarget
= -1; /* invalid */
531 printf("New inst %d: %p %s\n", prog->NumInstructions-1,(void*)inst,
532 _mesa_opcode_string(inst->Opcode));
540 * Return pointer to last instruction in program.
542 static struct prog_instruction
*
543 prev_instruction(struct gl_program
*prog
)
545 if (prog
->NumInstructions
== 0)
548 return prog
->Instructions
+ prog
->NumInstructions
- 1;
553 static struct prog_instruction
*
554 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
);
558 * Return an annotation string for given node's storage.
561 storage_annotation(const slang_ir_node
*n
, const struct gl_program
*prog
)
564 const slang_ir_storage
*st
= n
->Store
;
565 static char s
[100] = "";
568 return _mesa_strdup("");
571 case PROGRAM_CONSTANT
:
572 if (st
->Index
>= 0) {
573 const GLfloat
*val
= prog
->Parameters
->ParameterValues
[st
->Index
];
574 if (st
->Swizzle
== SWIZZLE_NOOP
)
575 sprintf(s
, "{%g, %g, %g, %g}", val
[0], val
[1], val
[2], val
[3]);
577 sprintf(s
, "%g", val
[GET_SWZ(st
->Swizzle
, 0)]);
581 case PROGRAM_TEMPORARY
:
583 sprintf(s
, "%s", (char *) n
->Var
->a_name
);
585 sprintf(s
, "t[%d]", st
->Index
);
587 case PROGRAM_STATE_VAR
:
588 case PROGRAM_UNIFORM
:
589 sprintf(s
, "%s", prog
->Parameters
->Parameters
[st
->Index
].Name
);
591 case PROGRAM_VARYING
:
592 sprintf(s
, "%s", prog
->Varying
->Parameters
[st
->Index
].Name
);
595 sprintf(s
, "input[%d]", st
->Index
);
598 sprintf(s
, "output[%d]", st
->Index
);
603 return _mesa_strdup(s
);
611 * Return an annotation string for an instruction.
614 instruction_annotation(gl_inst_opcode opcode
, char *dstAnnot
,
615 char *srcAnnot0
, char *srcAnnot1
, char *srcAnnot2
)
618 const char *operator;
623 len
+= strlen(dstAnnot
);
625 dstAnnot
= _mesa_strdup("");
628 len
+= strlen(srcAnnot0
);
630 srcAnnot0
= _mesa_strdup("");
633 len
+= strlen(srcAnnot1
);
635 srcAnnot1
= _mesa_strdup("");
638 len
+= strlen(srcAnnot2
);
640 srcAnnot2
= _mesa_strdup("");
671 s
= (char *) malloc(len
);
672 sprintf(s
, "%s = %s %s %s %s", dstAnnot
,
673 srcAnnot0
, operator, srcAnnot1
, srcAnnot2
);
674 assert(_mesa_strlen(s
) < len
);
690 * Generate code for a simple arithmetic instruction.
691 * Either 1, 2 or 3 operands.
693 static struct prog_instruction
*
694 emit_arith(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
696 struct prog_instruction
*inst
;
697 const slang_ir_info
*info
= slang_find_ir_info(n
->Opcode
);
698 char *srcAnnot
[3], *dstAnnot
;
702 assert(info
->InstOpcode
!= OPCODE_NOP
);
704 srcAnnot
[0] = srcAnnot
[1] = srcAnnot
[2] = dstAnnot
= NULL
;
706 #if PEEPHOLE_OPTIMIZATIONS
707 /* Look for MAD opportunity */
708 if (info
->NumParams
== 2 &&
709 n
->Opcode
== IR_ADD
&& n
->Children
[0]->Opcode
== IR_MUL
) {
710 /* found pattern IR_ADD(IR_MUL(A, B), C) */
711 emit(emitInfo
, n
->Children
[0]->Children
[0]); /* A */
712 emit(emitInfo
, n
->Children
[0]->Children
[1]); /* B */
713 emit(emitInfo
, n
->Children
[1]); /* C */
714 /* generate MAD instruction */
715 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
716 /* operands: A, B, C: */
717 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Children
[0]->Store
);
718 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[0]->Children
[1]->Store
);
719 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[1]->Store
);
720 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[0]);
721 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[1]);
722 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
724 else if (info
->NumParams
== 2 &&
725 n
->Opcode
== IR_ADD
&& n
->Children
[1]->Opcode
== IR_MUL
) {
726 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
727 emit(emitInfo
, n
->Children
[0]); /* A */
728 emit(emitInfo
, n
->Children
[1]->Children
[0]); /* B */
729 emit(emitInfo
, n
->Children
[1]->Children
[1]); /* C */
730 /* generate MAD instruction */
731 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
732 /* operands: B, C, A */
733 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Children
[0]->Store
);
734 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Children
[1]->Store
);
735 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[0]->Store
);
736 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[0]);
737 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[1]);
738 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
745 /* gen code for children */
746 for (i
= 0; i
< info
->NumParams
; i
++)
747 emit(emitInfo
, n
->Children
[i
]);
749 /* gen this instruction and src registers */
750 inst
= new_instruction(emitInfo
, info
->InstOpcode
);
751 for (i
= 0; i
< info
->NumParams
; i
++)
752 storage_to_src_reg(&inst
->SrcReg
[i
], n
->Children
[i
]->Store
);
755 for (i
= 0; i
< info
->NumParams
; i
++)
756 srcAnnot
[i
] = storage_annotation(n
->Children
[i
], emitInfo
->prog
);
759 for (i
= 0; i
< info
->NumParams
; i
++)
760 free_temp_storage(emitInfo
->vt
, n
->Children
[i
]);
765 if (!alloc_temp_storage(emitInfo
, n
, info
->ResultSize
))
768 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
770 dstAnnot
= storage_annotation(n
, emitInfo
->prog
);
772 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
, srcAnnot
[0],
773 srcAnnot
[1], srcAnnot
[2]);
775 /*_mesa_print_instruction(inst);*/
781 * Emit code for == and != operators. These could normally be handled
782 * by emit_arith() except we need to be able to handle structure comparisons.
784 static struct prog_instruction
*
785 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
787 struct prog_instruction
*inst
;
790 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
792 /* gen code for children */
793 emit(emitInfo
, n
->Children
[0]);
794 emit(emitInfo
, n
->Children
[1]);
797 assert(n
->Children
[0]->Store
->Size
== n
->Children
[1]->Store
->Size
);
798 size
= n
->Children
[0]->Store
->Size
;
800 /* XXX kind of a hack for now... */
801 size
= MIN2(n
->Children
[0]->Store
->Size
, n
->Children
[1]->Store
->Size
);
804 gl_inst_opcode opcode
;
807 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* 1 bool */
811 opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
812 inst
= new_instruction(emitInfo
, opcode
);
813 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
814 ASSERT(inst
->SrcReg
[0].Swizzle
!= SWIZZLE_XYZW
);
815 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
816 ASSERT(inst
->SrcReg
[1].Swizzle
!= SWIZZLE_XYZW
);
817 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
819 else if (size
<= 4) {
820 static const GLfloat zero
[4] = { 0, 0, 0, 0 };
821 GLuint zeroSwizzle
, swizzle
;
822 GLint zeroReg
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
823 zero
, 4, &zeroSwizzle
);
824 gl_inst_opcode dotOp
;
826 assert(zeroReg
>= 0);
829 if (!alloc_temp_storage(emitInfo
, n
, 4)) /* 4 bools */
835 swizzle
= SWIZZLE_XYZW
;
837 else if (size
== 3) {
839 swizzle
= SWIZZLE_XYZW
;
844 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
847 /* Compute equality, inequality */
848 inst
= new_instruction(emitInfo
, OPCODE_SNE
);
849 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
850 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
851 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
852 inst
->Comment
= _mesa_strdup("Compare values");
853 /* compute D = DP4(D, D) (reduction) */
854 inst
= new_instruction(emitInfo
, dotOp
);
855 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
856 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
857 inst
->SrcReg
[0].Swizzle
= swizzle
;
858 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
859 inst
->SrcReg
[1].Index
= n
->Store
->Index
;
860 inst
->SrcReg
[1].Swizzle
= swizzle
;
861 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
862 inst
->DstReg
.Index
= n
->Store
->Index
;
863 inst
->Comment
= _mesa_strdup("Reduce vec to bool");
864 if (n
->Opcode
== IR_EQUAL
) {
865 /* compute D.x = !D.x via D.x = (D.x == 0) */
866 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
867 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
868 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
869 inst
->SrcReg
[1].File
= PROGRAM_CONSTANT
;
870 inst
->SrcReg
[1].Index
= zeroReg
;
871 inst
->SrcReg
[1].Swizzle
= zeroSwizzle
;
872 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
873 inst
->DstReg
.Index
= n
->Store
->Index
;
874 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
875 inst
->Comment
= _mesa_strdup("Invert true/false");
879 /* size > 4, struct compare */
881 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
882 /*printf("BEGIN COMPARE size %d\n", num);*/
883 for (i
= 0; i
< num
; i
++) {
884 inst
= new_instruction(emitInfo
, opcode
);
885 inst
->SrcReg
[0].File
= n
->Children
[0]->Store
->File
;
886 inst
->SrcReg
[0].Index
= n
->Children
[0]->Store
->Index
+ i
;
887 inst
->SrcReg
[1].File
= n
->Children
[1]->Store
->File
;
888 inst
->SrcReg
[1].Index
= n
->Children
[1]->Store
->Index
+ i
;
889 inst
->DstReg
.File
= n
->Store
->File
;
890 inst
->DstReg
.Index
= n
->Store
->Index
;
892 inst
->CondUpdate
= 1; /* update cond code */
894 inst
->DstReg
.CondMask
= COND_NE
; /* update if !=0 */
896 /*_mesa_print_instruction(inst);*/
898 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
900 _mesa_problem(NULL
, "struct comparison not implemented yet");
905 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
906 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
914 * Generate code for an IR_CLAMP instruction.
916 static struct prog_instruction
*
917 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
919 struct prog_instruction
*inst
;
921 assert(n
->Opcode
== IR_CLAMP
);
927 inst
= emit(emitInfo
, n
->Children
[0]);
929 /* If lower limit == 0.0 and upper limit == 1.0,
930 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
932 * emit OPCODE_MIN, OPCODE_MAX sequence.
935 /* XXX this isn't quite finished yet */
936 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
937 n
->Children
[1]->Value
[0] == 0.0 &&
938 n
->Children
[1]->Value
[1] == 0.0 &&
939 n
->Children
[1]->Value
[2] == 0.0 &&
940 n
->Children
[1]->Value
[3] == 0.0 &&
941 n
->Children
[2]->Opcode
== IR_FLOAT
&&
942 n
->Children
[2]->Value
[0] == 1.0 &&
943 n
->Children
[2]->Value
[1] == 1.0 &&
944 n
->Children
[2]->Value
[2] == 1.0 &&
945 n
->Children
[2]->Value
[3] == 1.0) {
947 inst
= prev_instruction(prog
);
949 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
950 /* and prev instruction's DstReg matches n->Children[0]->Store */
951 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
952 n
->Store
= n
->Children
[0]->Store
;
959 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
962 emit(emitInfo
, n
->Children
[1]);
963 emit(emitInfo
, n
->Children
[2]);
965 /* tmp = max(ch[0], ch[1]) */
966 inst
= new_instruction(emitInfo
, OPCODE_MAX
);
967 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
968 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
969 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
971 /* tmp = min(tmp, ch[2]) */
972 inst
= new_instruction(emitInfo
, OPCODE_MIN
);
973 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
974 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
975 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[2]->Store
);
981 static struct prog_instruction
*
982 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
984 /* Implement as MOV dst, -src; */
985 /* XXX we could look at the previous instruction and in some circumstances
986 * modify it to accomplish the negation.
988 struct prog_instruction
*inst
;
990 emit(emitInfo
, n
->Children
[0]);
993 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
996 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
997 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
998 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
999 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
1004 static struct prog_instruction
*
1005 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
1008 assert(_slang_label_get_location(n
->Label
) < 0);
1009 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
1015 static struct prog_instruction
*
1016 emit_jump(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1018 struct prog_instruction
*inst
;
1021 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1022 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1023 inst
->BranchTarget
= _slang_label_get_location(n
->Label
);
1024 if (inst
->BranchTarget
< 0) {
1025 _slang_label_add_reference(n
->Label
, emitInfo
->prog
->NumInstructions
- 1);
1031 static struct prog_instruction
*
1032 emit_kill(slang_emit_info
*emitInfo
)
1034 struct prog_instruction
*inst
;
1035 /* NV-KILL - discard fragment depending on condition code.
1036 * Note that ARB-KILL depends on sign of vector operand.
1038 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
1039 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1044 static struct prog_instruction
*
1045 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1047 struct prog_instruction
*inst
;
1049 (void) emit(emitInfo
, n
->Children
[1]);
1051 if (n
->Opcode
== IR_TEX
) {
1052 inst
= new_instruction(emitInfo
, OPCODE_TEX
);
1054 else if (n
->Opcode
== IR_TEXB
) {
1055 inst
= new_instruction(emitInfo
, OPCODE_TXB
);
1058 assert(n
->Opcode
== IR_TEXP
);
1059 inst
= new_instruction(emitInfo
, OPCODE_TXP
);
1063 if (!alloc_temp_storage(emitInfo
, n
, 4))
1066 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1068 /* Child[1] is the coord */
1069 assert(n
->Children
[1]->Store
->File
!= PROGRAM_UNDEFINED
);
1070 assert(n
->Children
[1]->Store
->Index
>= 0);
1071 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
1073 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
1074 assert(n
->Children
[0]->Store
);
1075 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
1077 inst
->Sampler
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
1078 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
1079 inst
->TexSrcUnit
= 27; /* Dummy value; the TexSrcUnit will be computed at
1080 * link time, using the sampler uniform's value.
1086 static struct prog_instruction
*
1087 emit_move(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1089 struct prog_instruction
*inst
;
1092 emit(emitInfo
, n
->Children
[0]);
1095 assert(n
->Children
[1]);
1096 inst
= emit(emitInfo
, n
->Children
[1]);
1098 assert(n
->Children
[1]->Store
->Index
>= 0);
1101 n
->Store
= n
->Children
[0]->Store
;
1103 #if PEEPHOLE_OPTIMIZATIONS
1104 if (inst
&& _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
)) {
1105 /* Peephole optimization:
1106 * Just modify the RHS to put its result into the dest of this
1107 * MOVE operation. Then, this MOVE is a no-op.
1109 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
1110 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
1111 /* fixup the prev (RHS) instruction */
1112 assert(n
->Children
[0]->Store
->Index
>= 0);
1113 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
1119 if (n
->Children
[0]->Store
->Size
> 4) {
1120 /* move matrix/struct etc (block of registers) */
1121 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
1122 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
1123 GLint size
= srcStore
.Size
;
1124 ASSERT(n
->Children
[0]->Writemask
== WRITEMASK_XYZW
);
1125 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
1129 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1130 inst
->Comment
= _mesa_strdup("IR_MOVE block");
1131 storage_to_dst_reg(&inst
->DstReg
, &dstStore
, n
->Writemask
);
1132 storage_to_src_reg(&inst
->SrcReg
[0], &srcStore
);
1139 /* single register move */
1140 char *srcAnnot
, *dstAnnot
;
1141 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1142 assert(n
->Children
[0]->Store
->Index
>= 0);
1143 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
1144 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
1145 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
1146 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
1147 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
1148 srcAnnot
, NULL
, NULL
);
1150 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
1156 static struct prog_instruction
*
1157 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1159 struct prog_instruction
*inst
;
1161 if (!n
->Children
[0])
1164 inst
= emit(emitInfo
, n
->Children
[0]);
1166 if (emitInfo
->EmitCondCodes
) {
1167 /* Conditional expression (in if/while/for stmts).
1168 * Need to update condition code register.
1169 * Next instruction is typically an IR_IF.
1172 /* set inst's CondUpdate flag */
1173 inst
->CondUpdate
= GL_TRUE
;
1174 n
->Store
= n
->Children
[0]->Store
;
1175 return inst
; /* XXX or null? */
1178 /* This'll happen for things like "if (i) ..." where no code
1179 * is normally generated for the expression "i".
1180 * Generate a move instruction just to set condition codes.
1181 * Note: must use full 4-component vector since all four
1182 * condition codes must be set identically.
1184 if (!alloc_temp_storage(emitInfo
, n
, 4))
1186 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1187 inst
->CondUpdate
= GL_TRUE
;
1188 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1189 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1190 _slang_free_temp(emitInfo
->vt
, n
->Store
);
1191 inst
->Comment
= _mesa_strdup("COND expr");
1192 return inst
; /* XXX or null? */
1197 n
->Store
= n
->Children
[0]->Store
;
1206 static struct prog_instruction
*
1207 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1210 slang_ir_storage st
;
1211 struct prog_instruction
*inst
;
1213 /* need zero constant */
1214 st
.File
= PROGRAM_CONSTANT
;
1216 st
.Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
, &zero
,
1220 (void) emit(emitInfo
, n
->Children
[0]);
1221 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
1224 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1227 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
1228 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1229 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1230 storage_to_src_reg(&inst
->SrcReg
[1], &st
);
1232 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
1234 inst
->Comment
= _mesa_strdup("NOT");
1239 static struct prog_instruction
*
1240 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1242 struct gl_program
*prog
= emitInfo
->prog
;
1243 struct prog_instruction
*ifInst
;
1244 GLuint ifInstLoc
, elseInstLoc
= 0;
1246 emit(emitInfo
, n
->Children
[0]); /* the condition */
1249 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
1252 ifInstLoc
= prog
->NumInstructions
;
1253 if (emitInfo
->EmitHighLevelInstructions
) {
1254 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
1255 if (emitInfo
->EmitCondCodes
) {
1256 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
1260 storage_to_src_reg(&ifInst
->SrcReg
[0], n
->Children
[0]->Store
);
1264 /* conditional jump to else, or endif */
1265 ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1266 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
1267 ifInst
->Comment
= _mesa_strdup("if zero");
1269 if (emitInfo
->EmitCondCodes
) {
1270 /* which condition code to use: */
1271 ifInst
->DstReg
.CondSwizzle
= n
->Children
[0]->Store
->Swizzle
;
1275 emit(emitInfo
, n
->Children
[1]);
1277 if (n
->Children
[2]) {
1278 /* have else body */
1279 elseInstLoc
= prog
->NumInstructions
;
1280 if (emitInfo
->EmitHighLevelInstructions
) {
1281 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
1284 /* jump to endif instruction */
1285 struct prog_instruction
*inst
;
1286 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1287 inst
->Comment
= _mesa_strdup("else");
1288 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1290 ifInst
= prog
->Instructions
+ ifInstLoc
;
1291 ifInst
->BranchTarget
= prog
->NumInstructions
;
1293 emit(emitInfo
, n
->Children
[2]);
1297 ifInst
= prog
->Instructions
+ ifInstLoc
;
1298 ifInst
->BranchTarget
= prog
->NumInstructions
+ 1;
1301 if (emitInfo
->EmitHighLevelInstructions
) {
1302 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1305 if (n
->Children
[2]) {
1306 struct prog_instruction
*elseInst
;
1307 elseInst
= prog
->Instructions
+ elseInstLoc
;
1308 elseInst
->BranchTarget
= prog
->NumInstructions
;
1314 static struct prog_instruction
*
1315 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1317 struct gl_program
*prog
= emitInfo
->prog
;
1318 struct prog_instruction
*beginInst
, *endInst
;
1319 GLuint beginInstLoc
, endInstLoc
;
1322 /* emit OPCODE_BGNLOOP */
1323 beginInstLoc
= prog
->NumInstructions
;
1324 if (emitInfo
->EmitHighLevelInstructions
) {
1325 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1329 emit(emitInfo
, n
->Children
[0]);
1331 endInstLoc
= prog
->NumInstructions
;
1332 if (emitInfo
->EmitHighLevelInstructions
) {
1333 /* emit OPCODE_ENDLOOP */
1334 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1337 /* emit unconditional BRA-nch */
1338 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1339 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1341 /* end instruction's BranchTarget points to top of loop */
1342 endInst
->BranchTarget
= beginInstLoc
;
1344 if (emitInfo
->EmitHighLevelInstructions
) {
1345 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1346 beginInst
= prog
->Instructions
+ beginInstLoc
;
1347 beginInst
->BranchTarget
= prog
->NumInstructions
- 1;
1350 /* Done emitting loop code. Now walk over the loop's linked list of
1351 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1352 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1354 for (ir
= n
->BranchNode
; ir
; ir
= ir
->BranchNode
) {
1355 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1356 assert(inst
->BranchTarget
< 0);
1357 if (ir
->Opcode
== IR_BREAK
||
1358 ir
->Opcode
== IR_BREAK_IF_FALSE
||
1359 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1360 assert(inst
->Opcode
== OPCODE_BRK
||
1361 inst
->Opcode
== OPCODE_BRK0
||
1362 inst
->Opcode
== OPCODE_BRK1
||
1363 inst
->Opcode
== OPCODE_BRA
);
1364 /* go to instruction after end of loop */
1365 inst
->BranchTarget
= endInstLoc
+ 1;
1368 assert(ir
->Opcode
== IR_CONT
||
1369 ir
->Opcode
== IR_CONT_IF_FALSE
||
1370 ir
->Opcode
== IR_CONT_IF_TRUE
);
1371 assert(inst
->Opcode
== OPCODE_CONT
||
1372 inst
->Opcode
== OPCODE_CONT0
||
1373 inst
->Opcode
== OPCODE_CONT1
||
1374 inst
->Opcode
== OPCODE_BRA
);
1375 /* to go instruction at top of loop */
1376 inst
->BranchTarget
= beginInstLoc
;
1384 * Unconditional "continue" or "break" statement.
1385 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1387 static struct prog_instruction
*
1388 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1390 gl_inst_opcode opcode
;
1391 struct prog_instruction
*inst
;
1392 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1393 if (emitInfo
->EmitHighLevelInstructions
) {
1394 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1397 opcode
= OPCODE_BRA
;
1399 inst
= new_instruction(emitInfo
, opcode
);
1400 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1406 * Conditional "continue" or "break" statement.
1407 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1409 static struct prog_instruction
*
1410 emit_cont_break_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
1411 GLboolean breakTrue
)
1413 gl_inst_opcode opcode
;
1414 struct prog_instruction
*inst
;
1416 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1417 n
->Opcode
== IR_CONT_IF_FALSE
||
1418 n
->Opcode
== IR_BREAK_IF_TRUE
||
1419 n
->Opcode
== IR_BREAK_IF_FALSE
);
1421 /* evaluate condition expr, setting cond codes */
1422 inst
= emit(emitInfo
, n
->Children
[0]);
1423 if (emitInfo
->EmitCondCodes
) {
1425 inst
->CondUpdate
= GL_TRUE
;
1428 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1430 /* opcode selection */
1431 if (emitInfo
->EmitHighLevelInstructions
) {
1432 if (emitInfo
->EmitCondCodes
) {
1433 if (n
->Opcode
== IR_CONT_IF_TRUE
||
1434 n
->Opcode
== IR_CONT_IF_FALSE
)
1435 opcode
= OPCODE_CONT
;
1437 opcode
= OPCODE_BRK
;
1440 if (n
->Opcode
== IR_CONT_IF_TRUE
)
1441 opcode
= OPCODE_CONT1
;
1442 else if (n
->Opcode
== IR_CONT_IF_FALSE
)
1443 opcode
= OPCODE_CONT0
;
1444 else if (n
->Opcode
== IR_BREAK_IF_TRUE
)
1445 opcode
= OPCODE_BRK1
;
1446 else if (n
->Opcode
== IR_BREAK_IF_FALSE
)
1447 opcode
= OPCODE_BRK0
;
1451 opcode
= OPCODE_BRA
;
1454 inst
= new_instruction(emitInfo
, opcode
);
1455 if (emitInfo
->EmitCondCodes
) {
1456 inst
->DstReg
.CondMask
= breakTrue
? COND_NE
: COND_EQ
;
1459 /* BRK0, BRK1, CONT0, CONT1 */
1460 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1468 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1469 * Ex: fix_swizzle("zyNN") -> "zyyy"
1472 fix_swizzle(GLuint swizzle
)
1475 for (i
= 0; i
< 4; i
++) {
1476 swz
[i
] = GET_SWZ(swizzle
, i
);
1477 if (swz
[i
] == SWIZZLE_NIL
) {
1478 swz
[i
] = swz
[i
- 1];
1481 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1487 swizzle_size(GLuint swizzle
)
1490 for (i
= 0; i
< 4; i
++) {
1491 GLuint swz
= GET_SWZ(swizzle
, i
);
1492 size
+= (swz
>= 0 && swz
<= 3);
1499 static struct prog_instruction
*
1500 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1504 /* swizzled storage access */
1505 (void) emit(emitInfo
, n
->Children
[0]);
1507 /* "pull-up" the child's storage info, applying our swizzle info */
1508 n
->Store
->File
= n
->Children
[0]->Store
->File
;
1509 n
->Store
->Index
= n
->Children
[0]->Store
->Index
;
1510 n
->Store
->Size
= n
->Children
[0]->Store
->Size
;
1511 /*n->Var = n->Children[0]->Var; XXX for debug */
1512 assert(n
->Store
->Index
>= 0);
1514 swizzle
= fix_swizzle(n
->Store
->Swizzle
);
1517 GLuint s
= n
->Children
[0]->Store
->Swizzle
;
1518 assert(GET_SWZ(s
, 0) != SWIZZLE_NIL
);
1519 assert(GET_SWZ(s
, 1) != SWIZZLE_NIL
);
1520 assert(GET_SWZ(s
, 2) != SWIZZLE_NIL
);
1521 assert(GET_SWZ(s
, 3) != SWIZZLE_NIL
);
1526 n
->Store
->Size
= swizzle_size(n
->Store
->Swizzle
);
1527 printf("Emit Swizzle reg %d chSize %d size %d\n",
1528 n
->Store
->Index
, n
->Children
[0]->Store
->Size
,
1531 /* apply this swizzle to child's swizzle to get composed swizzle */
1532 n
->Store
->Swizzle
= swizzle_swizzle(n
->Children
[0]->Store
->Swizzle
,
1539 * Dereference array element. Just resolve storage for the array
1540 * element represented by this node.
1542 static struct prog_instruction
*
1543 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1546 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1547 assert(n
->Store
->Size
> 0);
1549 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1550 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1554 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1555 /* Constant index */
1556 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1557 const GLint index
= (GLint
) n
->Children
[1]->Value
[0];
1558 n
->Store
->Index
= arrayAddr
+ index
;
1561 /* Variable index - PROBLEM */
1562 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1563 const GLint index
= 0;
1564 _mesa_problem(NULL
, "variable array indexes not supported yet!");
1565 n
->Store
->Index
= arrayAddr
+ index
;
1567 return NULL
; /* no instruction */
1572 * Resolve storage for accessing a structure field.
1574 static struct prog_instruction
*
1575 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1577 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1578 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1581 GLint offset
= n
->FieldOffset
/ 4;
1582 assert(n
->Children
[0]->Store
->Index
>= 0);
1583 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ offset
;
1584 if (n
->Store
->Size
== 1) {
1585 GLint swz
= n
->FieldOffset
% 4;
1586 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
1589 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
1592 return NULL
; /* no instruction */
1596 static struct prog_instruction
*
1597 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1599 struct prog_instruction
*inst
;
1603 switch (n
->Opcode
) {
1605 /* sequence of two sub-trees */
1606 assert(n
->Children
[0]);
1607 assert(n
->Children
[1]);
1608 emit(emitInfo
, n
->Children
[0]);
1609 inst
= emit(emitInfo
, n
->Children
[1]);
1611 n
->Store
= n
->Children
[1]->Store
;
1615 /* new variable scope */
1616 _slang_push_var_table(emitInfo
->vt
);
1617 inst
= emit(emitInfo
, n
->Children
[0]);
1618 _slang_pop_var_table(emitInfo
->vt
);
1622 /* Variable declaration - allocate a register for it */
1624 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1625 assert(n
->Store
->Size
> 0);
1626 assert(n
->Store
->Index
< 0);
1627 if (!n
->Var
|| n
->Var
->isTemp
) {
1628 /* a nameless/temporary variable, will be freed after first use */
1629 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
1630 slang_info_log_error(emitInfo
->log
,
1631 "Ran out of registers, too many temporaries");
1636 /* a regular variable */
1637 _slang_add_variable(emitInfo
->vt
, n
->Var
);
1638 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
1639 slang_info_log_error(emitInfo
->log
,
1640 "Ran out of registers, too many variables");
1644 printf("IR_VAR_DECL %s %d store %p\n",
1645 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1647 assert(n
->Var
->aux
== n
->Store
);
1649 if (emitInfo
->EmitComments
) {
1650 /* emit NOP with comment describing the variable's storage location */
1652 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
1654 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
1655 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
1657 inst
= new_instruction(emitInfo
, OPCODE_NOP
);
1658 inst
->Comment
= _mesa_strdup(s
);
1664 /* Reference to a variable
1665 * Storage should have already been resolved/allocated.
1668 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1670 if (n
->Store
->File
== PROGRAM_STATE_VAR
&&
1671 n
->Store
->Index
< 0) {
1672 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1675 if (n
->Store
->Index
< 0) {
1676 printf("#### VAR %s not allocated!\n", (char*)n
->Var
->a_name
);
1678 assert(n
->Store
->Index
>= 0);
1679 assert(n
->Store
->Size
> 0);
1683 return emit_array_element(emitInfo
, n
);
1685 return emit_struct_field(emitInfo
, n
);
1687 return emit_swizzle(emitInfo
, n
);
1691 emit(emitInfo
, n
->Children
[0]);
1692 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1694 if (!alloc_temp_storage(emitInfo
, n
, 1))
1697 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1698 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1699 if (emitInfo
->EmitComments
)
1700 inst
->Comment
= _mesa_strdup("int to float");
1703 /* Simple arithmetic */
1737 /* trinary operators */
1739 return emit_arith(emitInfo
, n
);
1743 return emit_compare(emitInfo
, n
);
1746 return emit_clamp(emitInfo
, n
);
1750 return emit_tex(emitInfo
, n
);
1752 return emit_negation(emitInfo
, n
);
1754 /* find storage location for this float constant */
1755 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
, n
->Value
,
1757 &n
->Store
->Swizzle
);
1758 if (n
->Store
->Index
< 0) {
1759 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
1765 return emit_move(emitInfo
, n
);
1768 return emit_cond(emitInfo
, n
);
1771 return emit_not(emitInfo
, n
);
1774 return emit_label(emitInfo
, n
);
1778 return emit_jump(emitInfo
, n
);
1780 return emit_kill(emitInfo
);
1783 return emit_if(emitInfo
, n
);
1786 return emit_loop(emitInfo
, n
);
1787 case IR_BREAK_IF_FALSE
:
1788 case IR_CONT_IF_FALSE
:
1789 return emit_cont_break_if(emitInfo
, n
, GL_FALSE
);
1790 case IR_BREAK_IF_TRUE
:
1791 case IR_CONT_IF_TRUE
:
1792 return emit_cont_break_if(emitInfo
, n
, GL_TRUE
);
1796 return emit_cont_break(emitInfo
, n
);
1799 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
1801 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
1803 return new_instruction(emitInfo
, OPCODE_RET
);
1809 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
1817 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
1818 struct gl_program
*prog
, GLboolean withEnd
,
1819 slang_info_log
*log
)
1821 GET_CURRENT_CONTEXT(ctx
);
1823 slang_emit_info emitInfo
;
1827 emitInfo
.prog
= prog
;
1829 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
1830 emitInfo
.EmitCondCodes
= 0; /* XXX temporary! */
1831 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
1833 (void) emit(&emitInfo
, n
);
1835 /* finish up by adding the END opcode to program */
1837 struct prog_instruction
*inst
;
1838 inst
= new_instruction(&emitInfo
, OPCODE_END
);
1843 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
1844 _mesa_print_program(prog
);
1845 _mesa_print_program_parameters(ctx
,prog
);