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
58 struct gl_program
*prog
;
59 struct gl_program
**Subroutines
;
60 GLuint NumSubroutines
;
62 /* code-gen options */
63 GLboolean EmitHighLevelInstructions
;
64 GLboolean EmitCondCodes
;
65 GLboolean EmitComments
;
66 GLboolean EmitBeginEndSub
; /* XXX TEMPORARY */
71 static struct gl_program
*
72 new_subroutine(slang_emit_info
*emitInfo
, GLuint
*id
)
74 GET_CURRENT_CONTEXT(ctx
);
75 const GLuint n
= emitInfo
->NumSubroutines
;
77 emitInfo
->Subroutines
= (struct gl_program
**)
78 _mesa_realloc(emitInfo
->Subroutines
,
79 n
* sizeof(struct gl_program
),
80 (n
+ 1) * sizeof(struct gl_program
));
81 emitInfo
->Subroutines
[n
] = _mesa_new_program(ctx
, emitInfo
->prog
->Target
, 0);
82 emitInfo
->Subroutines
[n
]->Parameters
= emitInfo
->prog
->Parameters
;
83 emitInfo
->NumSubroutines
++;
85 return emitInfo
->Subroutines
[n
];
90 * Convert a writemask to a swizzle. Used for testing cond codes because
91 * we only want to test the cond code component(s) that was set by the
92 * previous instruction.
95 writemask_to_swizzle(GLuint writemask
)
97 if (writemask
== WRITEMASK_X
)
99 if (writemask
== WRITEMASK_Y
)
101 if (writemask
== WRITEMASK_Z
)
103 if (writemask
== WRITEMASK_W
)
105 return SWIZZLE_XYZW
; /* shouldn't be hit */
110 * Swizzle a swizzle. That is, return swz2(swz1)
113 swizzle_swizzle(GLuint swz1
, GLuint swz2
)
116 for (i
= 0; i
< 4; i
++) {
117 GLuint c
= GET_SWZ(swz2
, i
);
118 s
[i
] = GET_SWZ(swz1
, c
);
120 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
126 _slang_new_ir_storage(enum register_file file
, GLint index
, GLint size
)
128 slang_ir_storage
*st
;
129 st
= (slang_ir_storage
*) _mesa_calloc(sizeof(slang_ir_storage
));
134 st
->Swizzle
= SWIZZLE_NOOP
;
141 * Allocate temporary storage for an intermediate result (such as for
142 * a multiply or add, etc.
145 alloc_temp_storage(slang_emit_info
*emitInfo
, slang_ir_node
*n
, GLint size
)
150 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
151 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
152 slang_info_log_error(emitInfo
->log
,
153 "Ran out of registers, too many temporaries");
161 * Free temporary storage, if n->Store is, in fact, temp storage.
165 free_temp_storage(slang_var_table
*vt
, slang_ir_node
*n
)
167 if (n
->Store
->File
== PROGRAM_TEMPORARY
&&
168 n
->Store
->Index
>= 0 &&
169 n
->Opcode
!= IR_SWIZZLE
) {
170 if (_slang_is_temp(vt
, n
->Store
)) {
171 _slang_free_temp(vt
, n
->Store
);
172 n
->Store
->Index
= -1;
174 _mesa_free(n
->Store
);
182 * Convert IR storage to an instruction dst register.
185 storage_to_dst_reg(struct prog_dst_register
*dst
, const slang_ir_storage
*st
,
188 assert(st
->Index
>= 0);
189 dst
->File
= st
->File
;
190 dst
->Index
= st
->Index
;
191 assert(st
->File
!= PROGRAM_UNDEFINED
);
192 assert(st
->Size
>= 1);
193 assert(st
->Size
<= 4);
195 GLuint comp
= GET_SWZ(st
->Swizzle
, 0);
197 dst
->WriteMask
= WRITEMASK_X
<< comp
;
200 dst
->WriteMask
= writemask
;
206 * Convert IR storage to an instruction src register.
209 storage_to_src_reg(struct prog_src_register
*src
, const slang_ir_storage
*st
)
211 static const GLuint defaultSwizzle
[4] = {
212 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
213 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
214 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
215 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
)
217 assert(st
->File
>= 0);
218 assert(st
->File
< PROGRAM_UNDEFINED
);
219 assert(st
->Size
>= 1);
220 assert(st
->Size
<= 4);
221 src
->File
= st
->File
;
222 src
->Index
= st
->Index
;
223 if (st
->Swizzle
!= SWIZZLE_NOOP
)
224 src
->Swizzle
= st
->Swizzle
;
226 src
->Swizzle
= defaultSwizzle
[st
->Size
- 1]; /*XXX really need this?*/
228 assert(GET_SWZ(src
->Swizzle
, 0) <= 3);
229 assert(GET_SWZ(src
->Swizzle
, 1) <= 3);
230 assert(GET_SWZ(src
->Swizzle
, 2) <= 3);
231 assert(GET_SWZ(src
->Swizzle
, 3) <= 3);
236 * Setup an instrucion src register to point to a scalar constant.
239 constant_to_src_reg(struct prog_src_register
*src
, GLfloat val
,
240 slang_emit_info
*emitInfo
)
247 zeroReg
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
248 value
, 1, &zeroSwizzle
);
249 assert(zeroReg
>= 0);
251 src
->File
= PROGRAM_CONSTANT
;
252 src
->Index
= zeroReg
;
253 src
->Swizzle
= zeroSwizzle
;
258 * Add new instruction at end of given program.
259 * \param prog the program to append instruction onto
260 * \param opcode opcode for the new instruction
261 * \return pointer to the new instruction
263 static struct prog_instruction
*
264 new_instruction(slang_emit_info
*emitInfo
, gl_inst_opcode opcode
)
266 struct gl_program
*prog
= emitInfo
->prog
;
267 struct prog_instruction
*inst
;
270 /* print prev inst */
271 if (prog
->NumInstructions
> 0) {
272 _mesa_print_instruction(prog
->Instructions
+ prog
->NumInstructions
- 1);
275 prog
->Instructions
= _mesa_realloc_instructions(prog
->Instructions
,
276 prog
->NumInstructions
,
277 prog
->NumInstructions
+ 1);
278 inst
= prog
->Instructions
+ prog
->NumInstructions
;
279 prog
->NumInstructions
++;
280 _mesa_init_instructions(inst
, 1);
281 inst
->Opcode
= opcode
;
282 inst
->BranchTarget
= -1; /* invalid */
284 printf("New inst %d: %p %s\n", prog->NumInstructions-1,(void*)inst,
285 _mesa_opcode_string(inst->Opcode));
292 * Return pointer to last instruction in program.
294 static struct prog_instruction
*
295 prev_instruction(slang_emit_info
*emitInfo
)
297 struct gl_program
*prog
= emitInfo
->prog
;
298 if (prog
->NumInstructions
== 0)
301 return prog
->Instructions
+ prog
->NumInstructions
- 1;
305 static struct prog_instruction
*
306 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
);
310 * Return an annotation string for given node's storage.
313 storage_annotation(const slang_ir_node
*n
, const struct gl_program
*prog
)
316 const slang_ir_storage
*st
= n
->Store
;
317 static char s
[100] = "";
320 return _mesa_strdup("");
323 case PROGRAM_CONSTANT
:
324 if (st
->Index
>= 0) {
325 const GLfloat
*val
= prog
->Parameters
->ParameterValues
[st
->Index
];
326 if (st
->Swizzle
== SWIZZLE_NOOP
)
327 sprintf(s
, "{%g, %g, %g, %g}", val
[0], val
[1], val
[2], val
[3]);
329 sprintf(s
, "%g", val
[GET_SWZ(st
->Swizzle
, 0)]);
333 case PROGRAM_TEMPORARY
:
335 sprintf(s
, "%s", (char *) n
->Var
->a_name
);
337 sprintf(s
, "t[%d]", st
->Index
);
339 case PROGRAM_STATE_VAR
:
340 case PROGRAM_UNIFORM
:
341 sprintf(s
, "%s", prog
->Parameters
->Parameters
[st
->Index
].Name
);
343 case PROGRAM_VARYING
:
344 sprintf(s
, "%s", prog
->Varying
->Parameters
[st
->Index
].Name
);
347 sprintf(s
, "input[%d]", st
->Index
);
350 sprintf(s
, "output[%d]", st
->Index
);
355 return _mesa_strdup(s
);
363 * Return an annotation string for an instruction.
366 instruction_annotation(gl_inst_opcode opcode
, char *dstAnnot
,
367 char *srcAnnot0
, char *srcAnnot1
, char *srcAnnot2
)
370 const char *operator;
375 len
+= strlen(dstAnnot
);
377 dstAnnot
= _mesa_strdup("");
380 len
+= strlen(srcAnnot0
);
382 srcAnnot0
= _mesa_strdup("");
385 len
+= strlen(srcAnnot1
);
387 srcAnnot1
= _mesa_strdup("");
390 len
+= strlen(srcAnnot2
);
392 srcAnnot2
= _mesa_strdup("");
423 s
= (char *) malloc(len
);
424 sprintf(s
, "%s = %s %s %s %s", dstAnnot
,
425 srcAnnot0
, operator, srcAnnot1
, srcAnnot2
);
426 assert(_mesa_strlen(s
) < len
);
441 * Emit an instruction that's just a comment.
443 static struct prog_instruction
*
444 emit_comment(slang_emit_info
*emitInfo
, const char *s
)
446 struct prog_instruction
*inst
= new_instruction(emitInfo
, OPCODE_NOP
);
448 inst
->Comment
= _mesa_strdup(s
);
455 * Generate code for a simple arithmetic instruction.
456 * Either 1, 2 or 3 operands.
458 static struct prog_instruction
*
459 emit_arith(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
461 struct prog_instruction
*inst
;
462 const slang_ir_info
*info
= _slang_ir_info(n
->Opcode
);
463 char *srcAnnot
[3], *dstAnnot
;
467 assert(info
->InstOpcode
!= OPCODE_NOP
);
469 srcAnnot
[0] = srcAnnot
[1] = srcAnnot
[2] = dstAnnot
= NULL
;
471 #if PEEPHOLE_OPTIMIZATIONS
472 /* Look for MAD opportunity */
473 if (info
->NumParams
== 2 &&
474 n
->Opcode
== IR_ADD
&& n
->Children
[0]->Opcode
== IR_MUL
) {
475 /* found pattern IR_ADD(IR_MUL(A, B), C) */
476 emit(emitInfo
, n
->Children
[0]->Children
[0]); /* A */
477 emit(emitInfo
, n
->Children
[0]->Children
[1]); /* B */
478 emit(emitInfo
, n
->Children
[1]); /* C */
479 /* generate MAD instruction */
480 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
481 /* operands: A, B, C: */
482 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Children
[0]->Store
);
483 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[0]->Children
[1]->Store
);
484 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[1]->Store
);
485 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[0]);
486 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[1]);
487 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
489 else if (info
->NumParams
== 2 &&
490 n
->Opcode
== IR_ADD
&& n
->Children
[1]->Opcode
== IR_MUL
) {
491 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
492 emit(emitInfo
, n
->Children
[0]); /* A */
493 emit(emitInfo
, n
->Children
[1]->Children
[0]); /* B */
494 emit(emitInfo
, n
->Children
[1]->Children
[1]); /* C */
495 /* generate MAD instruction */
496 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
497 /* operands: B, C, A */
498 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Children
[0]->Store
);
499 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Children
[1]->Store
);
500 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[0]->Store
);
501 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[0]);
502 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[1]);
503 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
510 /* gen code for children */
511 for (i
= 0; i
< info
->NumParams
; i
++) {
512 emit(emitInfo
, n
->Children
[i
]);
513 if (!n
->Children
[i
] || !n
->Children
[i
]->Store
) {
519 /* gen this instruction and src registers */
520 inst
= new_instruction(emitInfo
, info
->InstOpcode
);
521 for (i
= 0; i
< info
->NumParams
; i
++)
522 storage_to_src_reg(&inst
->SrcReg
[i
], n
->Children
[i
]->Store
);
525 for (i
= 0; i
< info
->NumParams
; i
++)
526 srcAnnot
[i
] = storage_annotation(n
->Children
[i
], emitInfo
->prog
);
529 for (i
= 0; i
< info
->NumParams
; i
++)
530 free_temp_storage(emitInfo
->vt
, n
->Children
[i
]);
535 /* XXX this size isn't correct, it depends on the operands */
536 if (!alloc_temp_storage(emitInfo
, n
, info
->ResultSize
))
539 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
541 dstAnnot
= storage_annotation(n
, emitInfo
->prog
);
543 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
, srcAnnot
[0],
544 srcAnnot
[1], srcAnnot
[2]);
546 /*_mesa_print_instruction(inst);*/
552 * Emit code for == and != operators. These could normally be handled
553 * by emit_arith() except we need to be able to handle structure comparisons.
555 static struct prog_instruction
*
556 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
558 struct prog_instruction
*inst
;
561 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
563 /* gen code for children */
564 emit(emitInfo
, n
->Children
[0]);
565 emit(emitInfo
, n
->Children
[1]);
567 assert(n
->Children
[0]->Store
->Size
== n
->Children
[1]->Store
->Size
);
568 size
= n
->Children
[0]->Store
->Size
;
571 gl_inst_opcode opcode
;
574 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* 1 bool */
578 opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
579 inst
= new_instruction(emitInfo
, opcode
);
580 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
581 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
582 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
584 else if (size
<= 4) {
586 gl_inst_opcode dotOp
;
590 if (!alloc_temp_storage(emitInfo
, n
, size
)) /* 'size' bools */
596 swizzle
= SWIZZLE_XYZW
;
598 else if (size
== 3) {
600 swizzle
= SWIZZLE_XYZW
;
605 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
608 /* Compute equality, inequality (tmp1 = (A ?= B)) */
609 inst
= new_instruction(emitInfo
, OPCODE_SNE
);
610 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
611 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
612 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
613 inst
->Comment
= _mesa_strdup("Compare values");
615 /* Compute tmp2 = DOT(tmp1, tmp1) (reduction) */
616 inst
= new_instruction(emitInfo
, dotOp
);
617 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
618 storage_to_src_reg(&inst
->SrcReg
[1], n
->Store
);
619 inst
->SrcReg
[0].Swizzle
= inst
->SrcReg
[1].Swizzle
= swizzle
; /*override*/
620 free_temp_storage(emitInfo
->vt
, n
); /* free tmp1 */
621 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* alloc tmp2 */
623 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
624 inst
->Comment
= _mesa_strdup("Reduce vec to bool");
626 if (n
->Opcode
== IR_EQUAL
) {
627 /* compute tmp2.x = !tmp2.x via tmp2.x = (tmp2.x == 0) */
628 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
629 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
630 constant_to_src_reg(&inst
->SrcReg
[1], 0.0, emitInfo
);
631 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
632 inst
->Comment
= _mesa_strdup("Invert true/false");
636 /* size > 4, struct compare */
638 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
639 /*printf("BEGIN COMPARE size %d\n", num);*/
640 for (i
= 0; i
< num
; i
++) {
641 inst
= new_instruction(emitInfo
, opcode
);
642 inst
->SrcReg
[0].File
= n
->Children
[0]->Store
->File
;
643 inst
->SrcReg
[0].Index
= n
->Children
[0]->Store
->Index
+ i
;
644 inst
->SrcReg
[1].File
= n
->Children
[1]->Store
->File
;
645 inst
->SrcReg
[1].Index
= n
->Children
[1]->Store
->Index
+ i
;
646 inst
->DstReg
.File
= n
->Store
->File
;
647 inst
->DstReg
.Index
= n
->Store
->Index
;
649 inst
->CondUpdate
= 1; /* update cond code */
651 inst
->DstReg
.CondMask
= COND_NE
; /* update if !=0 */
653 /*_mesa_print_instruction(inst);*/
655 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
657 _mesa_problem(NULL
, "struct comparison not implemented yet");
662 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
663 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
671 * Generate code for an IR_CLAMP instruction.
673 static struct prog_instruction
*
674 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
676 struct prog_instruction
*inst
;
678 assert(n
->Opcode
== IR_CLAMP
);
684 inst
= emit(emitInfo
, n
->Children
[0]);
686 /* If lower limit == 0.0 and upper limit == 1.0,
687 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
689 * emit OPCODE_MIN, OPCODE_MAX sequence.
692 /* XXX this isn't quite finished yet */
693 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
694 n
->Children
[1]->Value
[0] == 0.0 &&
695 n
->Children
[1]->Value
[1] == 0.0 &&
696 n
->Children
[1]->Value
[2] == 0.0 &&
697 n
->Children
[1]->Value
[3] == 0.0 &&
698 n
->Children
[2]->Opcode
== IR_FLOAT
&&
699 n
->Children
[2]->Value
[0] == 1.0 &&
700 n
->Children
[2]->Value
[1] == 1.0 &&
701 n
->Children
[2]->Value
[2] == 1.0 &&
702 n
->Children
[2]->Value
[3] == 1.0) {
704 inst
= prev_instruction(prog
);
706 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
707 /* and prev instruction's DstReg matches n->Children[0]->Store */
708 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
709 n
->Store
= n
->Children
[0]->Store
;
716 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
719 emit(emitInfo
, n
->Children
[1]);
720 emit(emitInfo
, n
->Children
[2]);
722 /* tmp = max(ch[0], ch[1]) */
723 inst
= new_instruction(emitInfo
, OPCODE_MAX
);
724 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
725 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
726 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
728 /* tmp = min(tmp, ch[2]) */
729 inst
= new_instruction(emitInfo
, OPCODE_MIN
);
730 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
731 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
732 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[2]->Store
);
738 static struct prog_instruction
*
739 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
741 /* Implement as MOV dst, -src; */
742 /* XXX we could look at the previous instruction and in some circumstances
743 * modify it to accomplish the negation.
745 struct prog_instruction
*inst
;
747 emit(emitInfo
, n
->Children
[0]);
750 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
753 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
754 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
755 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
756 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
761 static struct prog_instruction
*
762 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
766 /* XXX this fails in loop tail code - investigate someday */
767 assert(_slang_label_get_location(n
->Label
) < 0);
768 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
771 if (_slang_label_get_location(n
->Label
) < 0)
772 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
780 * Emit code for an inlined function call (subroutine).
782 static struct prog_instruction
*
783 emit_func(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
785 struct gl_program
*progSave
;
786 struct prog_instruction
*inst
;
789 assert(n
->Opcode
== IR_FUNC
);
792 /* save/push cur program */
793 progSave
= emitInfo
->prog
;
794 emitInfo
->prog
= new_subroutine(emitInfo
, &subroutineId
);
796 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
799 if (emitInfo
->EmitBeginEndSub
) {
800 /* BGNSUB isn't a real instruction.
801 * We require a label (i.e. "foobar:") though, if we're going to
802 * print the program in the NV format. The BNGSUB instruction is
803 * really just a NOP to attach the label to.
805 inst
= new_instruction(emitInfo
, OPCODE_BGNSUB
);
806 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
809 /* body of function: */
810 emit(emitInfo
, n
->Children
[0]);
811 n
->Store
= n
->Children
[0]->Store
;
813 /* add RET instruction now, if needed */
814 inst
= prev_instruction(emitInfo
);
815 if (inst
&& inst
->Opcode
!= OPCODE_RET
) {
816 inst
= new_instruction(emitInfo
, OPCODE_RET
);
819 if (emitInfo
->EmitBeginEndSub
) {
820 inst
= new_instruction(emitInfo
, OPCODE_ENDSUB
);
821 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
824 /* pop/restore cur program */
825 emitInfo
->prog
= progSave
;
827 /* emit the function call */
828 inst
= new_instruction(emitInfo
, OPCODE_CAL
);
829 /* The branch target is just the subroutine number (changed later) */
830 inst
->BranchTarget
= subroutineId
;
831 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
832 assert(inst
->BranchTarget
>= 0);
839 * Emit code for a 'return' statement.
841 static struct prog_instruction
*
842 emit_return(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
844 struct prog_instruction
*inst
;
846 assert(n
->Opcode
== IR_RETURN
);
848 inst
= new_instruction(emitInfo
, OPCODE_RET
);
849 inst
->DstReg
.CondMask
= COND_TR
; /* always return */
854 static struct prog_instruction
*
855 emit_kill(slang_emit_info
*emitInfo
)
857 struct prog_instruction
*inst
;
858 /* NV-KILL - discard fragment depending on condition code.
859 * Note that ARB-KILL depends on sign of vector operand.
861 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
862 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
867 static struct prog_instruction
*
868 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
870 struct prog_instruction
*inst
;
872 (void) emit(emitInfo
, n
->Children
[1]);
874 if (n
->Opcode
== IR_TEX
) {
875 inst
= new_instruction(emitInfo
, OPCODE_TEX
);
877 else if (n
->Opcode
== IR_TEXB
) {
878 inst
= new_instruction(emitInfo
, OPCODE_TXB
);
881 assert(n
->Opcode
== IR_TEXP
);
882 inst
= new_instruction(emitInfo
, OPCODE_TXP
);
886 if (!alloc_temp_storage(emitInfo
, n
, 4))
889 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
891 /* Child[1] is the coord */
892 assert(n
->Children
[1]->Store
->File
!= PROGRAM_UNDEFINED
);
893 assert(n
->Children
[1]->Store
->Index
>= 0);
894 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
896 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
897 assert(n
->Children
[0]->Store
);
898 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
900 inst
->Sampler
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
901 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
902 inst
->TexSrcUnit
= 27; /* Dummy value; the TexSrcUnit will be computed at
903 * link time, using the sampler uniform's value.
909 static struct prog_instruction
*
910 emit_move(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
912 struct prog_instruction
*inst
;
915 emit(emitInfo
, n
->Children
[0]);
918 assert(n
->Children
[1]);
919 inst
= emit(emitInfo
, n
->Children
[1]);
921 if (!n
->Children
[1]->Store
) {
922 slang_info_log_error(emitInfo
->log
, "invalid assignment");
925 assert(n
->Children
[1]->Store
->Index
>= 0);
927 n
->Store
= n
->Children
[0]->Store
;
929 #if PEEPHOLE_OPTIMIZATIONS
931 _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
) &&
932 (inst
->DstReg
.File
== n
->Children
[1]->Store
->File
) &&
933 (inst
->DstReg
.Index
== n
->Children
[1]->Store
->Index
)) {
934 /* Peephole optimization:
935 * The Right-Hand-Side has its results in a temporary place.
936 * Modify the RHS (and the prev instruction) to store its results
937 * in the destination specified by n->Children[0].
938 * Then, this MOVE is a no-op.
940 if (n
->Children
[1]->Opcode
!= IR_SWIZZLE
)
941 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
942 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
943 /* fixup the previous instruction (which stored the RHS result) */
944 assert(n
->Children
[0]->Store
->Index
>= 0);
945 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
951 if (n
->Children
[0]->Store
->Size
> 4) {
952 /* move matrix/struct etc (block of registers) */
953 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
954 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
955 GLint size
= srcStore
.Size
;
956 ASSERT(n
->Children
[0]->Writemask
== WRITEMASK_XYZW
);
957 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
961 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
962 inst
->Comment
= _mesa_strdup("IR_MOVE block");
963 storage_to_dst_reg(&inst
->DstReg
, &dstStore
, n
->Writemask
);
964 storage_to_src_reg(&inst
->SrcReg
[0], &srcStore
);
971 /* single register move */
972 char *srcAnnot
, *dstAnnot
;
973 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
974 assert(n
->Children
[0]->Store
->Index
>= 0);
975 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
976 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
977 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
978 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
979 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
980 srcAnnot
, NULL
, NULL
);
982 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
989 * An IR_COND node wraps a boolean expression which is used by an
990 * IF or WHILE test. This is where we'll set condition codes, if needed.
992 static struct prog_instruction
*
993 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
995 struct prog_instruction
*inst
;
997 assert(n
->Opcode
== IR_COND
);
1002 /* emit code for the expression */
1003 inst
= emit(emitInfo
, n
->Children
[0]);
1005 if (!n
->Children
[0]->Store
) {
1006 /* error recovery */
1010 assert(n
->Children
[0]->Store
);
1011 /*assert(n->Children[0]->Store->Size == 1);*/
1013 if (emitInfo
->EmitCondCodes
) {
1015 n
->Children
[0]->Store
&&
1016 inst
->DstReg
.File
== n
->Children
[0]->Store
->File
&&
1017 inst
->DstReg
.Index
== n
->Children
[0]->Store
->Index
) {
1018 /* The previous instruction wrote to the register who's value
1019 * we're testing. Just fix that instruction so that the
1020 * condition codes are computed.
1022 inst
->CondUpdate
= GL_TRUE
;
1023 n
->Store
= n
->Children
[0]->Store
;
1027 /* This'll happen for things like "if (i) ..." where no code
1028 * is normally generated for the expression "i".
1029 * Generate a move instruction just to set condition codes.
1031 if (!alloc_temp_storage(emitInfo
, n
, 1))
1033 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1034 inst
->CondUpdate
= GL_TRUE
;
1035 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1036 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1037 _slang_free_temp(emitInfo
->vt
, n
->Store
);
1038 inst
->Comment
= _mesa_strdup("COND expr");
1043 /* No-op: the boolean result of the expression is in a regular reg */
1044 n
->Store
= n
->Children
[0]->Store
;
1053 static struct prog_instruction
*
1054 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1056 struct prog_instruction
*inst
;
1059 (void) emit(emitInfo
, n
->Children
[0]);
1060 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
1063 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1066 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
1067 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1068 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1069 constant_to_src_reg(&inst
->SrcReg
[1], 0.0, emitInfo
);
1070 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
1072 inst
->Comment
= _mesa_strdup("NOT");
1077 static struct prog_instruction
*
1078 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1080 struct gl_program
*prog
= emitInfo
->prog
;
1081 struct prog_instruction
*ifInst
, *inst
;
1082 GLuint ifInstLoc
, elseInstLoc
= 0;
1083 GLuint condWritemask
= 0;
1085 inst
= emit(emitInfo
, n
->Children
[0]); /* the condition */
1086 if (emitInfo
->EmitCondCodes
) {
1088 /* error recovery */
1091 condWritemask
= inst
->DstReg
.WriteMask
;
1095 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
1098 ifInstLoc
= prog
->NumInstructions
;
1099 if (emitInfo
->EmitHighLevelInstructions
) {
1100 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
1101 if (emitInfo
->EmitCondCodes
) {
1102 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
1103 /* only test the cond code (1 of 4) that was updated by the
1104 * previous instruction.
1106 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1110 storage_to_src_reg(&ifInst
->SrcReg
[0], n
->Children
[0]->Store
);
1114 /* conditional jump to else, or endif */
1115 ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1116 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
1117 ifInst
->Comment
= _mesa_strdup("if zero");
1118 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1122 emit(emitInfo
, n
->Children
[1]);
1124 if (n
->Children
[2]) {
1125 /* have else body */
1126 elseInstLoc
= prog
->NumInstructions
;
1127 if (emitInfo
->EmitHighLevelInstructions
) {
1128 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
1131 /* jump to endif instruction */
1132 struct prog_instruction
*inst
;
1133 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1134 inst
->Comment
= _mesa_strdup("else");
1135 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1137 ifInst
= prog
->Instructions
+ ifInstLoc
;
1138 ifInst
->BranchTarget
= prog
->NumInstructions
;
1140 emit(emitInfo
, n
->Children
[2]);
1144 ifInst
= prog
->Instructions
+ ifInstLoc
;
1145 ifInst
->BranchTarget
= prog
->NumInstructions
/*+ 1*/;
1148 if (emitInfo
->EmitHighLevelInstructions
) {
1149 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1152 if (n
->Children
[2]) {
1153 struct prog_instruction
*elseInst
;
1154 elseInst
= prog
->Instructions
+ elseInstLoc
;
1155 elseInst
->BranchTarget
= prog
->NumInstructions
;
1161 static struct prog_instruction
*
1162 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1164 struct gl_program
*prog
= emitInfo
->prog
;
1165 struct prog_instruction
*beginInst
, *endInst
;
1166 GLuint beginInstLoc
, tailInstLoc
, endInstLoc
;
1169 /* emit OPCODE_BGNLOOP */
1170 beginInstLoc
= prog
->NumInstructions
;
1171 if (emitInfo
->EmitHighLevelInstructions
) {
1172 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1176 emit(emitInfo
, n
->Children
[0]);
1179 tailInstLoc
= prog
->NumInstructions
;
1180 if (n
->Children
[1]) {
1181 if (emitInfo
->EmitComments
)
1182 emit_comment(emitInfo
, "Loop tail code:");
1183 emit(emitInfo
, n
->Children
[1]);
1186 endInstLoc
= prog
->NumInstructions
;
1187 if (emitInfo
->EmitHighLevelInstructions
) {
1188 /* emit OPCODE_ENDLOOP */
1189 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1192 /* emit unconditional BRA-nch */
1193 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1194 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1196 /* ENDLOOP's BranchTarget points to the BGNLOOP inst */
1197 endInst
->BranchTarget
= beginInstLoc
;
1199 if (emitInfo
->EmitHighLevelInstructions
) {
1200 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1201 beginInst
= prog
->Instructions
+ beginInstLoc
;
1202 beginInst
->BranchTarget
= prog
->NumInstructions
- 1;
1205 /* Done emitting loop code. Now walk over the loop's linked list of
1206 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1207 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1209 for (ir
= n
->List
; ir
; ir
= ir
->List
) {
1210 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1211 assert(inst
->BranchTarget
< 0);
1212 if (ir
->Opcode
== IR_BREAK
||
1213 ir
->Opcode
== IR_BREAK_IF_FALSE
||
1214 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1215 assert(inst
->Opcode
== OPCODE_BRK
||
1216 inst
->Opcode
== OPCODE_BRK0
||
1217 inst
->Opcode
== OPCODE_BRK1
||
1218 inst
->Opcode
== OPCODE_BRA
);
1219 /* go to instruction after end of loop */
1220 inst
->BranchTarget
= endInstLoc
+ 1;
1223 assert(ir
->Opcode
== IR_CONT
||
1224 ir
->Opcode
== IR_CONT_IF_FALSE
||
1225 ir
->Opcode
== IR_CONT_IF_TRUE
);
1226 assert(inst
->Opcode
== OPCODE_CONT
||
1227 inst
->Opcode
== OPCODE_CONT0
||
1228 inst
->Opcode
== OPCODE_CONT1
||
1229 inst
->Opcode
== OPCODE_BRA
);
1230 /* go to instruction at tail of loop */
1231 inst
->BranchTarget
= endInstLoc
;
1239 * Unconditional "continue" or "break" statement.
1240 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1242 static struct prog_instruction
*
1243 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1245 gl_inst_opcode opcode
;
1246 struct prog_instruction
*inst
;
1248 if (n
->Opcode
== IR_CONT
) {
1249 /* we need to execute the loop's tail code before doing CONT */
1251 assert(n
->Parent
->Opcode
== IR_LOOP
);
1252 if (n
->Parent
->Children
[1]) {
1253 /* emit tail code */
1254 if (emitInfo
->EmitComments
) {
1255 emit_comment(emitInfo
, "continue - tail code:");
1257 emit(emitInfo
, n
->Parent
->Children
[1]);
1261 /* opcode selection */
1262 if (emitInfo
->EmitHighLevelInstructions
) {
1263 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1266 opcode
= OPCODE_BRA
;
1268 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1269 inst
= new_instruction(emitInfo
, opcode
);
1270 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1276 * Conditional "continue" or "break" statement.
1277 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1279 static struct prog_instruction
*
1280 emit_cont_break_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
1281 GLboolean breakTrue
)
1283 gl_inst_opcode opcode
;
1284 struct prog_instruction
*inst
;
1286 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1287 n
->Opcode
== IR_CONT_IF_FALSE
||
1288 n
->Opcode
== IR_BREAK_IF_TRUE
||
1289 n
->Opcode
== IR_BREAK_IF_FALSE
);
1291 /* evaluate condition expr, setting cond codes */
1292 inst
= emit(emitInfo
, n
->Children
[0]);
1293 if (emitInfo
->EmitCondCodes
) {
1295 inst
->CondUpdate
= GL_TRUE
;
1298 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1300 /* opcode selection */
1301 if (emitInfo
->EmitHighLevelInstructions
) {
1302 if (emitInfo
->EmitCondCodes
) {
1303 if (n
->Opcode
== IR_CONT_IF_TRUE
||
1304 n
->Opcode
== IR_CONT_IF_FALSE
)
1305 opcode
= OPCODE_CONT
;
1307 opcode
= OPCODE_BRK
;
1310 if (n
->Opcode
== IR_CONT_IF_TRUE
)
1311 opcode
= OPCODE_CONT1
;
1312 else if (n
->Opcode
== IR_CONT_IF_FALSE
)
1313 opcode
= OPCODE_CONT0
;
1314 else if (n
->Opcode
== IR_BREAK_IF_TRUE
)
1315 opcode
= OPCODE_BRK1
;
1316 else if (n
->Opcode
== IR_BREAK_IF_FALSE
)
1317 opcode
= OPCODE_BRK0
;
1321 opcode
= OPCODE_BRA
;
1324 inst
= new_instruction(emitInfo
, opcode
);
1325 if (emitInfo
->EmitCondCodes
) {
1326 inst
->DstReg
.CondMask
= breakTrue
? COND_NE
: COND_EQ
;
1329 /* BRK0, BRK1, CONT0, CONT1 uses SrcReg[0] as the condition */
1330 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1338 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1339 * Ex: fix_swizzle("zyNN") -> "zyyy"
1342 fix_swizzle(GLuint swizzle
)
1345 for (i
= 0; i
< 4; i
++) {
1346 swz
[i
] = GET_SWZ(swizzle
, i
);
1347 if (swz
[i
] == SWIZZLE_NIL
) {
1348 swz
[i
] = swz
[i
- 1];
1351 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1356 * Return the number of components actually named by the swizzle.
1357 * Recall that swizzles may have undefined/don't-care values.
1360 swizzle_size(GLuint swizzle
)
1363 for (i
= 0; i
< 4; i
++) {
1364 GLuint swz
= GET_SWZ(swizzle
, i
);
1365 size
+= (swz
>= 0 && swz
<= 3);
1371 static struct prog_instruction
*
1372 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1375 struct prog_instruction
*inst
;
1377 inst
= emit(emitInfo
, n
->Children
[0]);
1381 GLuint s
= n
->Children
[0]->Store
->Swizzle
;
1382 assert(GET_SWZ(s
, 0) != SWIZZLE_NIL
);
1383 assert(GET_SWZ(s
, 1) != SWIZZLE_NIL
);
1384 assert(GET_SWZ(s
, 2) != SWIZZLE_NIL
);
1385 assert(GET_SWZ(s
, 3) != SWIZZLE_NIL
);
1388 /* For debug: n->Var = n->Children[0]->Var; */
1390 /* "pull-up" the child's storage info, applying our swizzle info */
1391 n
->Store
->File
= n
->Children
[0]->Store
->File
;
1392 n
->Store
->Index
= n
->Children
[0]->Store
->Index
;
1393 n
->Store
->Size
= swizzle_size(n
->Store
->Swizzle
);
1395 printf("Emit Swizzle %s reg %d chSize %d mySize %d\n",
1396 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0),
1397 n
->Store
->Index
, n
->Children
[0]->Store
->Size
,
1401 /* apply this swizzle to child's swizzle to get composed swizzle */
1402 swizzle
= fix_swizzle(n
->Store
->Swizzle
); /* remove the don't care terms */
1403 n
->Store
->Swizzle
= swizzle_swizzle(n
->Children
[0]->Store
->Swizzle
,
1411 * Dereference array element. Just resolve storage for the array
1412 * element represented by this node.
1414 static struct prog_instruction
*
1415 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1418 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1419 assert(n
->Store
->Size
> 0);
1421 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1422 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1426 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1427 /* Constant index */
1428 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1429 const GLint index
= (GLint
) n
->Children
[1]->Value
[0];
1430 n
->Store
->Index
= arrayAddr
+ index
;
1433 /* Variable index - PROBLEM */
1434 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1435 const GLint index
= 0;
1436 _mesa_problem(NULL
, "variable array indexes not supported yet!");
1437 n
->Store
->Index
= arrayAddr
+ index
;
1439 return NULL
; /* no instruction */
1444 * Resolve storage for accessing a structure field.
1446 static struct prog_instruction
*
1447 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1449 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1450 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1453 GLint offset
= n
->FieldOffset
/ 4;
1454 assert(n
->Children
[0]->Store
->Index
>= 0);
1455 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ offset
;
1456 if (n
->Store
->Size
== 1) {
1457 GLint swz
= n
->FieldOffset
% 4;
1458 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
1461 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
1464 return NULL
; /* no instruction */
1468 static struct prog_instruction
*
1469 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1471 struct prog_instruction
*inst
;
1475 switch (n
->Opcode
) {
1477 /* sequence of two sub-trees */
1478 assert(n
->Children
[0]);
1479 assert(n
->Children
[1]);
1480 emit(emitInfo
, n
->Children
[0]);
1481 inst
= emit(emitInfo
, n
->Children
[1]);
1485 n
->Store
= n
->Children
[1]->Store
;
1489 /* new variable scope */
1490 _slang_push_var_table(emitInfo
->vt
);
1491 inst
= emit(emitInfo
, n
->Children
[0]);
1492 _slang_pop_var_table(emitInfo
->vt
);
1496 /* Variable declaration - allocate a register for it */
1498 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1499 assert(n
->Store
->Size
> 0);
1500 /*assert(n->Store->Index < 0);*/
1501 if (!n
->Var
|| n
->Var
->isTemp
) {
1502 /* a nameless/temporary variable, will be freed after first use */
1504 if (n
->Store
->Index
< 0 && !_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
1505 slang_info_log_error(emitInfo
->log
,
1506 "Ran out of registers, too many temporaries");
1511 /* a regular variable */
1512 _slang_add_variable(emitInfo
->vt
, n
->Var
);
1513 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
1514 slang_info_log_error(emitInfo
->log
,
1515 "Ran out of registers, too many variables");
1519 printf("IR_VAR_DECL %s %d store %p\n",
1520 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1522 assert(n
->Var
->aux
== n
->Store
);
1524 if (emitInfo
->EmitComments
) {
1525 /* emit NOP with comment describing the variable's storage location */
1527 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
1529 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
1530 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
1532 inst
= emit_comment(emitInfo
, s
);
1538 /* Reference to a variable
1539 * Storage should have already been resolved/allocated.
1542 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1544 if (n
->Store
->File
== PROGRAM_STATE_VAR
&&
1545 n
->Store
->Index
< 0) {
1546 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1549 if (n
->Store
->Index
< 0) {
1550 printf("#### VAR %s not allocated!\n", (char*)n
->Var
->a_name
);
1552 assert(n
->Store
->Index
>= 0);
1553 assert(n
->Store
->Size
> 0);
1557 return emit_array_element(emitInfo
, n
);
1559 return emit_struct_field(emitInfo
, n
);
1561 return emit_swizzle(emitInfo
, n
);
1565 emit(emitInfo
, n
->Children
[0]);
1566 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1568 if (!alloc_temp_storage(emitInfo
, n
, 1))
1571 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1572 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1573 if (emitInfo
->EmitComments
)
1574 inst
->Comment
= _mesa_strdup("int to float");
1577 /* Simple arithmetic */
1611 /* trinary operators */
1613 return emit_arith(emitInfo
, n
);
1617 return emit_compare(emitInfo
, n
);
1620 return emit_clamp(emitInfo
, n
);
1624 return emit_tex(emitInfo
, n
);
1626 return emit_negation(emitInfo
, n
);
1628 /* find storage location for this float constant */
1629 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
1632 &n
->Store
->Swizzle
);
1633 if (n
->Store
->Index
< 0) {
1634 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
1640 return emit_move(emitInfo
, n
);
1643 return emit_cond(emitInfo
, n
);
1646 return emit_not(emitInfo
, n
);
1649 return emit_label(emitInfo
, n
);
1652 return emit_kill(emitInfo
);
1655 /* new variable scope for subroutines/function calls*/
1656 _slang_push_var_table(emitInfo
->vt
);
1657 inst
= emit_func(emitInfo
, n
);
1658 _slang_pop_var_table(emitInfo
->vt
);
1662 return emit_if(emitInfo
, n
);
1665 return emit_loop(emitInfo
, n
);
1666 case IR_BREAK_IF_FALSE
:
1667 case IR_CONT_IF_FALSE
:
1668 return emit_cont_break_if(emitInfo
, n
, GL_FALSE
);
1669 case IR_BREAK_IF_TRUE
:
1670 case IR_CONT_IF_TRUE
:
1671 return emit_cont_break_if(emitInfo
, n
, GL_TRUE
);
1675 return emit_cont_break(emitInfo
, n
);
1678 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
1680 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
1682 return emit_return(emitInfo
, n
);
1688 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
1695 * After code generation, any subroutines will be in separate program
1696 * objects. This function appends all the subroutines onto the main
1697 * program and resolves the linking of all the branch/call instructions.
1698 * XXX this logic should really be part of the linking process...
1701 _slang_resolve_subroutines(slang_emit_info
*emitInfo
)
1703 GET_CURRENT_CONTEXT(ctx
);
1704 struct gl_program
*mainP
= emitInfo
->prog
;
1705 GLuint
*subroutineLoc
, i
, total
;
1708 = (GLuint
*) _mesa_malloc(emitInfo
->NumSubroutines
* sizeof(GLuint
));
1710 /* total number of instructions */
1711 total
= mainP
->NumInstructions
;
1712 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1713 subroutineLoc
[i
] = total
;
1714 total
+= emitInfo
->Subroutines
[i
]->NumInstructions
;
1717 /* adjust BrancTargets within the functions */
1718 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1719 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1721 for (j
= 0; j
< sub
->NumInstructions
; j
++) {
1722 struct prog_instruction
*inst
= sub
->Instructions
+ j
;
1723 if (inst
->Opcode
!= OPCODE_CAL
&& inst
->BranchTarget
>= 0) {
1724 inst
->BranchTarget
+= subroutineLoc
[i
];
1729 /* append subroutines' instructions after main's instructions */
1730 mainP
->Instructions
= _mesa_realloc_instructions(mainP
->Instructions
,
1731 mainP
->NumInstructions
,
1733 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1734 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1735 _mesa_copy_instructions(mainP
->Instructions
+ subroutineLoc
[i
],
1737 sub
->NumInstructions
);
1738 /* delete subroutine code */
1739 sub
->Parameters
= NULL
; /* prevent double-free */
1740 _mesa_delete_program(ctx
, sub
);
1742 mainP
->NumInstructions
= total
;
1744 /* Examine CAL instructions.
1745 * At this point, the BranchTarget field of the CAL instructions is
1746 * the number/id of the subroutine to call (an index into the
1747 * emitInfo->Subroutines list).
1748 * Translate that into an actual instruction location now.
1750 for (i
= 0; i
< mainP
->NumInstructions
; i
++) {
1751 struct prog_instruction
*inst
= mainP
->Instructions
+ i
;
1752 if (inst
->Opcode
== OPCODE_CAL
) {
1753 const GLuint f
= inst
->BranchTarget
;
1754 inst
->BranchTarget
= subroutineLoc
[f
];
1758 _mesa_free(subroutineLoc
);
1765 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
1766 struct gl_program
*prog
, GLboolean withEnd
,
1767 slang_info_log
*log
)
1769 GET_CURRENT_CONTEXT(ctx
);
1771 slang_emit_info emitInfo
;
1775 emitInfo
.prog
= prog
;
1776 emitInfo
.Subroutines
= NULL
;
1777 emitInfo
.NumSubroutines
= 0;
1779 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
1780 emitInfo
.EmitCondCodes
= ctx
->Shader
.EmitCondCodes
;
1781 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
1782 emitInfo
.EmitBeginEndSub
= 0; /* XXX for compiler debug only */
1784 if (!emitInfo
.EmitCondCodes
) {
1785 emitInfo
.EmitHighLevelInstructions
= GL_TRUE
;
1788 (void) emit(&emitInfo
, n
);
1790 /* finish up by adding the END opcode to program */
1792 struct prog_instruction
*inst
;
1793 inst
= new_instruction(&emitInfo
, OPCODE_END
);
1796 _slang_resolve_subroutines(&emitInfo
);
1801 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
1802 _mesa_print_program(prog
);
1803 _mesa_print_program_parameters(ctx
,prog
);