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
;
71 * Swizzle a swizzle. That is, return swz2(swz1)
74 swizzle_swizzle(GLuint swz1
, GLuint swz2
)
77 for (i
= 0; i
< 4; i
++) {
78 GLuint c
= GET_SWZ(swz2
, i
);
79 s
[i
] = GET_SWZ(swz1
, c
);
81 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
87 _slang_new_ir_storage(enum register_file file
, GLint index
, GLint size
)
90 st
= (slang_ir_storage
*) _mesa_calloc(sizeof(slang_ir_storage
));
95 st
->Swizzle
= SWIZZLE_NOOP
;
102 * Allocate temporary storage for an intermediate result (such as for
103 * a multiply or add, etc.
106 alloc_temp_storage(slang_emit_info
*emitInfo
, slang_ir_node
*n
, GLint size
)
111 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
112 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
113 slang_info_log_error(emitInfo
->log
,
114 "Ran out of registers, too many temporaries");
122 * Free temporary storage, if n->Store is, in fact, temp storage.
126 free_temp_storage(slang_var_table
*vt
, slang_ir_node
*n
)
128 if (n
->Store
->File
== PROGRAM_TEMPORARY
&&
129 n
->Store
->Index
>= 0 &&
130 n
->Opcode
!= IR_SWIZZLE
) {
131 if (_slang_is_temp(vt
, n
->Store
)) {
132 _slang_free_temp(vt
, n
->Store
);
133 n
->Store
->Index
= -1;
141 * Convert IR storage to an instruction dst register.
144 storage_to_dst_reg(struct prog_dst_register
*dst
, const slang_ir_storage
*st
,
147 assert(st
->Index
>= 0);
148 dst
->File
= st
->File
;
149 dst
->Index
= st
->Index
;
150 assert(st
->File
!= PROGRAM_UNDEFINED
);
151 assert(st
->Size
>= 1);
152 assert(st
->Size
<= 4);
154 GLuint comp
= GET_SWZ(st
->Swizzle
, 0);
156 dst
->WriteMask
= WRITEMASK_X
<< comp
;
159 dst
->WriteMask
= writemask
;
165 * Convert IR storage to an instruction src register.
168 storage_to_src_reg(struct prog_src_register
*src
, const slang_ir_storage
*st
)
170 static const GLuint defaultSwizzle
[4] = {
171 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
172 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
173 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
174 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
)
176 assert(st
->File
>= 0);
177 assert(st
->File
< PROGRAM_UNDEFINED
);
178 assert(st
->Size
>= 1);
179 assert(st
->Size
<= 4);
180 src
->File
= st
->File
;
181 src
->Index
= st
->Index
;
182 if (st
->Swizzle
!= SWIZZLE_NOOP
)
183 src
->Swizzle
= st
->Swizzle
;
185 src
->Swizzle
= defaultSwizzle
[st
->Size
- 1]; /*XXX really need this?*/
187 assert(GET_SWZ(src
->Swizzle
, 0) <= 3);
188 assert(GET_SWZ(src
->Swizzle
, 1) <= 3);
189 assert(GET_SWZ(src
->Swizzle
, 2) <= 3);
190 assert(GET_SWZ(src
->Swizzle
, 3) <= 3);
196 * Add new instruction at end of given program.
197 * \param prog the program to append instruction onto
198 * \param opcode opcode for the new instruction
199 * \return pointer to the new instruction
201 static struct prog_instruction
*
202 new_instruction(slang_emit_info
*emitInfo
, gl_inst_opcode opcode
)
204 struct gl_program
*prog
= emitInfo
->prog
;
205 struct prog_instruction
*inst
;
206 prog
->Instructions
= _mesa_realloc_instructions(prog
->Instructions
,
207 prog
->NumInstructions
,
208 prog
->NumInstructions
+ 1);
209 inst
= prog
->Instructions
+ prog
->NumInstructions
;
210 prog
->NumInstructions
++;
211 _mesa_init_instructions(inst
, 1);
212 inst
->Opcode
= opcode
;
213 inst
->BranchTarget
= -1; /* invalid */
215 printf("New inst %d: %p %s\n", prog->NumInstructions-1,(void*)inst,
216 _mesa_opcode_string(inst->Opcode));
224 * Return pointer to last instruction in program.
226 static struct prog_instruction
*
227 prev_instruction(struct gl_program
*prog
)
229 if (prog
->NumInstructions
== 0)
232 return prog
->Instructions
+ prog
->NumInstructions
- 1;
237 static struct prog_instruction
*
238 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
);
242 * Return an annotation string for given node's storage.
245 storage_annotation(const slang_ir_node
*n
, const struct gl_program
*prog
)
248 const slang_ir_storage
*st
= n
->Store
;
249 static char s
[100] = "";
252 return _mesa_strdup("");
255 case PROGRAM_CONSTANT
:
256 if (st
->Index
>= 0) {
257 const GLfloat
*val
= prog
->Parameters
->ParameterValues
[st
->Index
];
258 if (st
->Swizzle
== SWIZZLE_NOOP
)
259 sprintf(s
, "{%g, %g, %g, %g}", val
[0], val
[1], val
[2], val
[3]);
261 sprintf(s
, "%g", val
[GET_SWZ(st
->Swizzle
, 0)]);
265 case PROGRAM_TEMPORARY
:
267 sprintf(s
, "%s", (char *) n
->Var
->a_name
);
269 sprintf(s
, "t[%d]", st
->Index
);
271 case PROGRAM_STATE_VAR
:
272 case PROGRAM_UNIFORM
:
273 sprintf(s
, "%s", prog
->Parameters
->Parameters
[st
->Index
].Name
);
275 case PROGRAM_VARYING
:
276 sprintf(s
, "%s", prog
->Varying
->Parameters
[st
->Index
].Name
);
279 sprintf(s
, "input[%d]", st
->Index
);
282 sprintf(s
, "output[%d]", st
->Index
);
287 return _mesa_strdup(s
);
295 * Return an annotation string for an instruction.
298 instruction_annotation(gl_inst_opcode opcode
, char *dstAnnot
,
299 char *srcAnnot0
, char *srcAnnot1
, char *srcAnnot2
)
302 const char *operator;
307 len
+= strlen(dstAnnot
);
309 dstAnnot
= _mesa_strdup("");
312 len
+= strlen(srcAnnot0
);
314 srcAnnot0
= _mesa_strdup("");
317 len
+= strlen(srcAnnot1
);
319 srcAnnot1
= _mesa_strdup("");
322 len
+= strlen(srcAnnot2
);
324 srcAnnot2
= _mesa_strdup("");
355 s
= (char *) malloc(len
);
356 sprintf(s
, "%s = %s %s %s %s", dstAnnot
,
357 srcAnnot0
, operator, srcAnnot1
, srcAnnot2
);
358 assert(_mesa_strlen(s
) < len
);
373 * Emit an instruction that's just a comment.
375 static struct prog_instruction
*
376 emit_comment(slang_emit_info
*emitInfo
, const char *s
)
378 struct prog_instruction
*inst
= new_instruction(emitInfo
, OPCODE_NOP
);
380 inst
->Comment
= _mesa_strdup(s
);
387 * Generate code for a simple arithmetic instruction.
388 * Either 1, 2 or 3 operands.
390 static struct prog_instruction
*
391 emit_arith(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
393 struct prog_instruction
*inst
;
394 const slang_ir_info
*info
= _slang_ir_info(n
->Opcode
);
395 char *srcAnnot
[3], *dstAnnot
;
399 assert(info
->InstOpcode
!= OPCODE_NOP
);
401 srcAnnot
[0] = srcAnnot
[1] = srcAnnot
[2] = dstAnnot
= NULL
;
403 #if PEEPHOLE_OPTIMIZATIONS
404 /* Look for MAD opportunity */
405 if (info
->NumParams
== 2 &&
406 n
->Opcode
== IR_ADD
&& n
->Children
[0]->Opcode
== IR_MUL
) {
407 /* found pattern IR_ADD(IR_MUL(A, B), C) */
408 emit(emitInfo
, n
->Children
[0]->Children
[0]); /* A */
409 emit(emitInfo
, n
->Children
[0]->Children
[1]); /* B */
410 emit(emitInfo
, n
->Children
[1]); /* C */
411 /* generate MAD instruction */
412 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
413 /* operands: A, B, C: */
414 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Children
[0]->Store
);
415 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[0]->Children
[1]->Store
);
416 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[1]->Store
);
417 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[0]);
418 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[1]);
419 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
421 else if (info
->NumParams
== 2 &&
422 n
->Opcode
== IR_ADD
&& n
->Children
[1]->Opcode
== IR_MUL
) {
423 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
424 emit(emitInfo
, n
->Children
[0]); /* A */
425 emit(emitInfo
, n
->Children
[1]->Children
[0]); /* B */
426 emit(emitInfo
, n
->Children
[1]->Children
[1]); /* C */
427 /* generate MAD instruction */
428 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
429 /* operands: B, C, A */
430 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Children
[0]->Store
);
431 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Children
[1]->Store
);
432 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[0]->Store
);
433 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[0]);
434 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[1]);
435 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
442 /* gen code for children */
443 for (i
= 0; i
< info
->NumParams
; i
++)
444 emit(emitInfo
, n
->Children
[i
]);
446 /* gen this instruction and src registers */
447 inst
= new_instruction(emitInfo
, info
->InstOpcode
);
448 for (i
= 0; i
< info
->NumParams
; i
++)
449 storage_to_src_reg(&inst
->SrcReg
[i
], n
->Children
[i
]->Store
);
452 for (i
= 0; i
< info
->NumParams
; i
++)
453 srcAnnot
[i
] = storage_annotation(n
->Children
[i
], emitInfo
->prog
);
456 for (i
= 0; i
< info
->NumParams
; i
++)
457 free_temp_storage(emitInfo
->vt
, n
->Children
[i
]);
462 if (!alloc_temp_storage(emitInfo
, n
, info
->ResultSize
))
465 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
467 dstAnnot
= storage_annotation(n
, emitInfo
->prog
);
469 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
, srcAnnot
[0],
470 srcAnnot
[1], srcAnnot
[2]);
472 /*_mesa_print_instruction(inst);*/
478 * Emit code for == and != operators. These could normally be handled
479 * by emit_arith() except we need to be able to handle structure comparisons.
481 static struct prog_instruction
*
482 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
484 struct prog_instruction
*inst
;
487 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
489 /* gen code for children */
490 emit(emitInfo
, n
->Children
[0]);
491 emit(emitInfo
, n
->Children
[1]);
493 assert(n
->Children
[0]->Store
->Size
== n
->Children
[1]->Store
->Size
);
494 size
= n
->Children
[0]->Store
->Size
;
497 gl_inst_opcode opcode
;
500 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* 1 bool */
504 opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
505 inst
= new_instruction(emitInfo
, opcode
);
506 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
507 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
508 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
510 else if (size
<= 4) {
511 static const GLfloat zero
[4] = { 0, 0, 0, 0 };
512 GLuint zeroSwizzle
, swizzle
;
513 GLint zeroReg
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
514 zero
, 4, &zeroSwizzle
);
515 gl_inst_opcode dotOp
;
517 assert(zeroReg
>= 0);
520 if (!alloc_temp_storage(emitInfo
, n
, 4)) /* 4 bools */
526 swizzle
= SWIZZLE_XYZW
;
528 else if (size
== 3) {
530 swizzle
= SWIZZLE_XYZW
;
535 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
538 /* Compute equality, inequality */
539 inst
= new_instruction(emitInfo
, OPCODE_SNE
);
540 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
541 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
542 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
543 inst
->Comment
= _mesa_strdup("Compare values");
544 /* compute D = DP4(D, D) (reduction) */
545 inst
= new_instruction(emitInfo
, dotOp
);
546 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
547 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
548 inst
->SrcReg
[0].Swizzle
= swizzle
;
549 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
550 inst
->SrcReg
[1].Index
= n
->Store
->Index
;
551 inst
->SrcReg
[1].Swizzle
= swizzle
;
552 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
553 inst
->DstReg
.Index
= n
->Store
->Index
;
554 inst
->Comment
= _mesa_strdup("Reduce vec to bool");
555 if (n
->Opcode
== IR_EQUAL
) {
556 /* compute D.x = !D.x via D.x = (D.x == 0) */
557 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
558 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
559 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
560 inst
->SrcReg
[1].File
= PROGRAM_CONSTANT
;
561 inst
->SrcReg
[1].Index
= zeroReg
;
562 inst
->SrcReg
[1].Swizzle
= zeroSwizzle
;
563 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
564 inst
->DstReg
.Index
= n
->Store
->Index
;
565 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
566 inst
->Comment
= _mesa_strdup("Invert true/false");
570 /* size > 4, struct compare */
572 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
573 /*printf("BEGIN COMPARE size %d\n", num);*/
574 for (i
= 0; i
< num
; i
++) {
575 inst
= new_instruction(emitInfo
, opcode
);
576 inst
->SrcReg
[0].File
= n
->Children
[0]->Store
->File
;
577 inst
->SrcReg
[0].Index
= n
->Children
[0]->Store
->Index
+ i
;
578 inst
->SrcReg
[1].File
= n
->Children
[1]->Store
->File
;
579 inst
->SrcReg
[1].Index
= n
->Children
[1]->Store
->Index
+ i
;
580 inst
->DstReg
.File
= n
->Store
->File
;
581 inst
->DstReg
.Index
= n
->Store
->Index
;
583 inst
->CondUpdate
= 1; /* update cond code */
585 inst
->DstReg
.CondMask
= COND_NE
; /* update if !=0 */
587 /*_mesa_print_instruction(inst);*/
589 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
591 _mesa_problem(NULL
, "struct comparison not implemented yet");
596 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
597 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
605 * Generate code for an IR_CLAMP instruction.
607 static struct prog_instruction
*
608 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
610 struct prog_instruction
*inst
;
612 assert(n
->Opcode
== IR_CLAMP
);
618 inst
= emit(emitInfo
, n
->Children
[0]);
620 /* If lower limit == 0.0 and upper limit == 1.0,
621 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
623 * emit OPCODE_MIN, OPCODE_MAX sequence.
626 /* XXX this isn't quite finished yet */
627 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
628 n
->Children
[1]->Value
[0] == 0.0 &&
629 n
->Children
[1]->Value
[1] == 0.0 &&
630 n
->Children
[1]->Value
[2] == 0.0 &&
631 n
->Children
[1]->Value
[3] == 0.0 &&
632 n
->Children
[2]->Opcode
== IR_FLOAT
&&
633 n
->Children
[2]->Value
[0] == 1.0 &&
634 n
->Children
[2]->Value
[1] == 1.0 &&
635 n
->Children
[2]->Value
[2] == 1.0 &&
636 n
->Children
[2]->Value
[3] == 1.0) {
638 inst
= prev_instruction(prog
);
640 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
641 /* and prev instruction's DstReg matches n->Children[0]->Store */
642 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
643 n
->Store
= n
->Children
[0]->Store
;
650 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
653 emit(emitInfo
, n
->Children
[1]);
654 emit(emitInfo
, n
->Children
[2]);
656 /* tmp = max(ch[0], ch[1]) */
657 inst
= new_instruction(emitInfo
, OPCODE_MAX
);
658 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
659 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
660 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
662 /* tmp = min(tmp, ch[2]) */
663 inst
= new_instruction(emitInfo
, OPCODE_MIN
);
664 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
665 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
666 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[2]->Store
);
672 static struct prog_instruction
*
673 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
675 /* Implement as MOV dst, -src; */
676 /* XXX we could look at the previous instruction and in some circumstances
677 * modify it to accomplish the negation.
679 struct prog_instruction
*inst
;
681 emit(emitInfo
, n
->Children
[0]);
684 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
687 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
688 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
689 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
690 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
695 static struct prog_instruction
*
696 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
700 /* XXX this fails in loop tail code - investigate someday */
701 assert(_slang_label_get_location(n
->Label
) < 0);
702 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
705 if (_slang_label_get_location(n
->Label
) < 0)
706 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
713 static struct prog_instruction
*
714 emit_jump(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
716 struct prog_instruction
*inst
;
719 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
720 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
721 inst
->BranchTarget
= _slang_label_get_location(n
->Label
);
722 if (inst
->BranchTarget
< 0) {
723 _slang_label_add_reference(n
->Label
, emitInfo
->prog
->NumInstructions
- 1);
729 static struct prog_instruction
*
730 emit_kill(slang_emit_info
*emitInfo
)
732 struct prog_instruction
*inst
;
733 /* NV-KILL - discard fragment depending on condition code.
734 * Note that ARB-KILL depends on sign of vector operand.
736 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
737 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
742 static struct prog_instruction
*
743 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
745 struct prog_instruction
*inst
;
747 (void) emit(emitInfo
, n
->Children
[1]);
749 if (n
->Opcode
== IR_TEX
) {
750 inst
= new_instruction(emitInfo
, OPCODE_TEX
);
752 else if (n
->Opcode
== IR_TEXB
) {
753 inst
= new_instruction(emitInfo
, OPCODE_TXB
);
756 assert(n
->Opcode
== IR_TEXP
);
757 inst
= new_instruction(emitInfo
, OPCODE_TXP
);
761 if (!alloc_temp_storage(emitInfo
, n
, 4))
764 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
766 /* Child[1] is the coord */
767 assert(n
->Children
[1]->Store
->File
!= PROGRAM_UNDEFINED
);
768 assert(n
->Children
[1]->Store
->Index
>= 0);
769 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
771 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
772 assert(n
->Children
[0]->Store
);
773 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
775 inst
->Sampler
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
776 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
777 inst
->TexSrcUnit
= 27; /* Dummy value; the TexSrcUnit will be computed at
778 * link time, using the sampler uniform's value.
784 static struct prog_instruction
*
785 emit_move(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
787 struct prog_instruction
*inst
;
790 emit(emitInfo
, n
->Children
[0]);
793 assert(n
->Children
[1]);
794 inst
= emit(emitInfo
, n
->Children
[1]);
796 assert(n
->Children
[1]->Store
->Index
>= 0);
801 n
->Store
= n
->Children
[0]->Store
;
803 #if PEEPHOLE_OPTIMIZATIONS
804 if (inst
&& _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
)) {
805 /* Peephole optimization:
806 * Just modify the RHS to put its result into the dest of this
807 * MOVE operation. Then, this MOVE is a no-op.
809 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
810 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
811 /* fixup the prev (RHS) instruction */
812 assert(n
->Children
[0]->Store
->Index
>= 0);
813 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
819 if (n
->Children
[0]->Store
->Size
> 4) {
820 /* move matrix/struct etc (block of registers) */
821 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
822 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
823 GLint size
= srcStore
.Size
;
824 ASSERT(n
->Children
[0]->Writemask
== WRITEMASK_XYZW
);
825 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
829 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
830 inst
->Comment
= _mesa_strdup("IR_MOVE block");
831 storage_to_dst_reg(&inst
->DstReg
, &dstStore
, n
->Writemask
);
832 storage_to_src_reg(&inst
->SrcReg
[0], &srcStore
);
839 /* single register move */
840 char *srcAnnot
, *dstAnnot
;
841 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
842 assert(n
->Children
[0]->Store
->Index
>= 0);
843 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
844 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
845 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
846 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
847 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
848 srcAnnot
, NULL
, NULL
);
850 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
856 static struct prog_instruction
*
857 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
859 struct prog_instruction
*inst
;
864 inst
= emit(emitInfo
, n
->Children
[0]);
866 if (emitInfo
->EmitCondCodes
) {
867 /* Conditional expression (in if/while/for stmts).
868 * Need to update condition code register.
869 * Next instruction is typically an IR_IF.
872 /* set inst's CondUpdate flag */
873 inst
->CondUpdate
= GL_TRUE
;
874 n
->Store
= n
->Children
[0]->Store
;
875 return inst
; /* XXX or null? */
878 /* This'll happen for things like "if (i) ..." where no code
879 * is normally generated for the expression "i".
880 * Generate a move instruction just to set condition codes.
881 * Note: must use full 4-component vector since all four
882 * condition codes must be set identically.
884 if (!alloc_temp_storage(emitInfo
, n
, 4))
886 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
887 inst
->CondUpdate
= GL_TRUE
;
888 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
889 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
890 _slang_free_temp(emitInfo
->vt
, n
->Store
);
891 inst
->Comment
= _mesa_strdup("COND expr");
892 return inst
; /* XXX or null? */
897 n
->Store
= n
->Children
[0]->Store
;
906 static struct prog_instruction
*
907 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
911 struct prog_instruction
*inst
;
913 /* need zero constant */
914 st
.File
= PROGRAM_CONSTANT
;
916 st
.Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
, &zero
,
920 (void) emit(emitInfo
, n
->Children
[0]);
921 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
924 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
927 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
928 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
929 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
930 storage_to_src_reg(&inst
->SrcReg
[1], &st
);
932 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
934 inst
->Comment
= _mesa_strdup("NOT");
939 static struct prog_instruction
*
940 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
942 struct gl_program
*prog
= emitInfo
->prog
;
943 struct prog_instruction
*ifInst
;
944 GLuint ifInstLoc
, elseInstLoc
= 0;
946 emit(emitInfo
, n
->Children
[0]); /* the condition */
949 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
952 ifInstLoc
= prog
->NumInstructions
;
953 if (emitInfo
->EmitHighLevelInstructions
) {
954 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
955 if (emitInfo
->EmitCondCodes
) {
956 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
960 storage_to_src_reg(&ifInst
->SrcReg
[0], n
->Children
[0]->Store
);
964 /* conditional jump to else, or endif */
965 ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
966 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
967 ifInst
->Comment
= _mesa_strdup("if zero");
969 if (emitInfo
->EmitCondCodes
) {
970 /* which condition code to use: */
971 ifInst
->DstReg
.CondSwizzle
= n
->Children
[0]->Store
->Swizzle
;
975 emit(emitInfo
, n
->Children
[1]);
977 if (n
->Children
[2]) {
979 elseInstLoc
= prog
->NumInstructions
;
980 if (emitInfo
->EmitHighLevelInstructions
) {
981 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
984 /* jump to endif instruction */
985 struct prog_instruction
*inst
;
986 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
987 inst
->Comment
= _mesa_strdup("else");
988 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
990 ifInst
= prog
->Instructions
+ ifInstLoc
;
991 ifInst
->BranchTarget
= prog
->NumInstructions
;
993 emit(emitInfo
, n
->Children
[2]);
997 ifInst
= prog
->Instructions
+ ifInstLoc
;
998 ifInst
->BranchTarget
= prog
->NumInstructions
+ 1;
1001 if (emitInfo
->EmitHighLevelInstructions
) {
1002 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1005 if (n
->Children
[2]) {
1006 struct prog_instruction
*elseInst
;
1007 elseInst
= prog
->Instructions
+ elseInstLoc
;
1008 elseInst
->BranchTarget
= prog
->NumInstructions
;
1014 static struct prog_instruction
*
1015 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1017 struct gl_program
*prog
= emitInfo
->prog
;
1018 struct prog_instruction
*beginInst
, *endInst
;
1019 GLuint beginInstLoc
, tailInstLoc
, endInstLoc
;
1022 /* emit OPCODE_BGNLOOP */
1023 beginInstLoc
= prog
->NumInstructions
;
1024 if (emitInfo
->EmitHighLevelInstructions
) {
1025 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1029 emit(emitInfo
, n
->Children
[0]);
1032 tailInstLoc
= prog
->NumInstructions
;
1033 if (n
->Children
[1]) {
1034 if (emitInfo
->EmitComments
)
1035 emit_comment(emitInfo
, "Loop tail code:");
1036 emit(emitInfo
, n
->Children
[1]);
1039 endInstLoc
= prog
->NumInstructions
;
1040 if (emitInfo
->EmitHighLevelInstructions
) {
1041 /* emit OPCODE_ENDLOOP */
1042 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1045 /* emit unconditional BRA-nch */
1046 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1047 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1049 /* ENDLOOP's BranchTarget points to the BGNLOOP inst */
1050 endInst
->BranchTarget
= beginInstLoc
;
1052 if (emitInfo
->EmitHighLevelInstructions
) {
1053 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1054 beginInst
= prog
->Instructions
+ beginInstLoc
;
1055 beginInst
->BranchTarget
= prog
->NumInstructions
- 1;
1058 /* Done emitting loop code. Now walk over the loop's linked list of
1059 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1060 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1062 for (ir
= n
->List
; ir
; ir
= ir
->List
) {
1063 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1064 assert(inst
->BranchTarget
< 0);
1065 if (ir
->Opcode
== IR_BREAK
||
1066 ir
->Opcode
== IR_BREAK_IF_FALSE
||
1067 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1068 assert(inst
->Opcode
== OPCODE_BRK
||
1069 inst
->Opcode
== OPCODE_BRK0
||
1070 inst
->Opcode
== OPCODE_BRK1
||
1071 inst
->Opcode
== OPCODE_BRA
);
1072 /* go to instruction after end of loop */
1073 inst
->BranchTarget
= endInstLoc
+ 1;
1076 assert(ir
->Opcode
== IR_CONT
||
1077 ir
->Opcode
== IR_CONT_IF_FALSE
||
1078 ir
->Opcode
== IR_CONT_IF_TRUE
);
1079 assert(inst
->Opcode
== OPCODE_CONT
||
1080 inst
->Opcode
== OPCODE_CONT0
||
1081 inst
->Opcode
== OPCODE_CONT1
||
1082 inst
->Opcode
== OPCODE_BRA
);
1083 /* go to instruction at tail of loop */
1084 inst
->BranchTarget
= endInstLoc
;
1092 * Unconditional "continue" or "break" statement.
1093 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1095 static struct prog_instruction
*
1096 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1098 gl_inst_opcode opcode
;
1099 struct prog_instruction
*inst
;
1101 if (n
->Opcode
== IR_CONT
) {
1102 /* we need to execute the loop's tail code before doing CONT */
1104 assert(n
->Parent
->Opcode
== IR_LOOP
);
1105 if (n
->Parent
->Children
[1]) {
1106 /* emit tail code */
1107 if (emitInfo
->EmitComments
) {
1108 emit_comment(emitInfo
, "continue - tail code:");
1110 emit(emitInfo
, n
->Parent
->Children
[1]);
1114 /* opcode selection */
1115 if (emitInfo
->EmitHighLevelInstructions
) {
1116 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1119 opcode
= OPCODE_BRA
;
1121 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1122 inst
= new_instruction(emitInfo
, opcode
);
1123 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1129 * Conditional "continue" or "break" statement.
1130 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1132 static struct prog_instruction
*
1133 emit_cont_break_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
1134 GLboolean breakTrue
)
1136 gl_inst_opcode opcode
;
1137 struct prog_instruction
*inst
;
1139 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1140 n
->Opcode
== IR_CONT_IF_FALSE
||
1141 n
->Opcode
== IR_BREAK_IF_TRUE
||
1142 n
->Opcode
== IR_BREAK_IF_FALSE
);
1144 /* evaluate condition expr, setting cond codes */
1145 inst
= emit(emitInfo
, n
->Children
[0]);
1146 if (emitInfo
->EmitCondCodes
) {
1148 inst
->CondUpdate
= GL_TRUE
;
1151 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1153 /* opcode selection */
1154 if (emitInfo
->EmitHighLevelInstructions
) {
1155 if (emitInfo
->EmitCondCodes
) {
1156 if (n
->Opcode
== IR_CONT_IF_TRUE
||
1157 n
->Opcode
== IR_CONT_IF_FALSE
)
1158 opcode
= OPCODE_CONT
;
1160 opcode
= OPCODE_BRK
;
1163 if (n
->Opcode
== IR_CONT_IF_TRUE
)
1164 opcode
= OPCODE_CONT1
;
1165 else if (n
->Opcode
== IR_CONT_IF_FALSE
)
1166 opcode
= OPCODE_CONT0
;
1167 else if (n
->Opcode
== IR_BREAK_IF_TRUE
)
1168 opcode
= OPCODE_BRK1
;
1169 else if (n
->Opcode
== IR_BREAK_IF_FALSE
)
1170 opcode
= OPCODE_BRK0
;
1174 opcode
= OPCODE_BRA
;
1177 inst
= new_instruction(emitInfo
, opcode
);
1178 if (emitInfo
->EmitCondCodes
) {
1179 inst
->DstReg
.CondMask
= breakTrue
? COND_NE
: COND_EQ
;
1182 /* BRK0, BRK1, CONT0, CONT1 uses SrcReg[0] as the condition */
1183 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1191 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1192 * Ex: fix_swizzle("zyNN") -> "zyyy"
1195 fix_swizzle(GLuint swizzle
)
1198 for (i
= 0; i
< 4; i
++) {
1199 swz
[i
] = GET_SWZ(swizzle
, i
);
1200 if (swz
[i
] == SWIZZLE_NIL
) {
1201 swz
[i
] = swz
[i
- 1];
1204 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1209 * Return the number of components actually named by the swizzle.
1210 * Recall that swizzles may have undefined/don't-care values.
1213 swizzle_size(GLuint swizzle
)
1216 for (i
= 0; i
< 4; i
++) {
1217 GLuint swz
= GET_SWZ(swizzle
, i
);
1218 size
+= (swz
>= 0 && swz
<= 3);
1224 static struct prog_instruction
*
1225 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1229 (void) emit(emitInfo
, n
->Children
[0]);
1233 GLuint s
= n
->Children
[0]->Store
->Swizzle
;
1234 assert(GET_SWZ(s
, 0) != SWIZZLE_NIL
);
1235 assert(GET_SWZ(s
, 1) != SWIZZLE_NIL
);
1236 assert(GET_SWZ(s
, 2) != SWIZZLE_NIL
);
1237 assert(GET_SWZ(s
, 3) != SWIZZLE_NIL
);
1240 /* For debug: n->Var = n->Children[0]->Var; */
1242 /* "pull-up" the child's storage info, applying our swizzle info */
1243 n
->Store
->File
= n
->Children
[0]->Store
->File
;
1244 n
->Store
->Index
= n
->Children
[0]->Store
->Index
;
1245 n
->Store
->Size
= swizzle_size(n
->Store
->Swizzle
);
1247 printf("Emit Swizzle reg %d chSize %d size %d swz %s\n",
1248 n
->Store
->Index
, n
->Children
[0]->Store
->Size
,
1250 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0));
1253 /* apply this swizzle to child's swizzle to get composed swizzle */
1254 swizzle
= fix_swizzle(n
->Store
->Swizzle
); /* remove the don't care terms */
1255 n
->Store
->Swizzle
= swizzle_swizzle(n
->Children
[0]->Store
->Swizzle
,
1263 * Dereference array element. Just resolve storage for the array
1264 * element represented by this node.
1266 static struct prog_instruction
*
1267 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1270 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1271 assert(n
->Store
->Size
> 0);
1273 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1274 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1278 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1279 /* Constant index */
1280 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1281 const GLint index
= (GLint
) n
->Children
[1]->Value
[0];
1282 n
->Store
->Index
= arrayAddr
+ index
;
1285 /* Variable index - PROBLEM */
1286 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1287 const GLint index
= 0;
1288 _mesa_problem(NULL
, "variable array indexes not supported yet!");
1289 n
->Store
->Index
= arrayAddr
+ index
;
1291 return NULL
; /* no instruction */
1296 * Resolve storage for accessing a structure field.
1298 static struct prog_instruction
*
1299 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1301 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1302 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1305 GLint offset
= n
->FieldOffset
/ 4;
1306 assert(n
->Children
[0]->Store
->Index
>= 0);
1307 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ offset
;
1308 if (n
->Store
->Size
== 1) {
1309 GLint swz
= n
->FieldOffset
% 4;
1310 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
1313 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
1316 return NULL
; /* no instruction */
1320 static struct prog_instruction
*
1321 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1323 struct prog_instruction
*inst
;
1327 switch (n
->Opcode
) {
1329 /* sequence of two sub-trees */
1330 assert(n
->Children
[0]);
1331 assert(n
->Children
[1]);
1332 emit(emitInfo
, n
->Children
[0]);
1333 inst
= emit(emitInfo
, n
->Children
[1]);
1337 n
->Store
= n
->Children
[1]->Store
;
1341 /* new variable scope */
1342 _slang_push_var_table(emitInfo
->vt
);
1343 inst
= emit(emitInfo
, n
->Children
[0]);
1344 _slang_pop_var_table(emitInfo
->vt
);
1348 /* Variable declaration - allocate a register for it */
1350 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1351 assert(n
->Store
->Size
> 0);
1352 /*assert(n->Store->Index < 0);*/
1353 if (!n
->Var
|| n
->Var
->isTemp
) {
1354 /* a nameless/temporary variable, will be freed after first use */
1356 if (n
->Store
->Index
< 0 && !_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
1357 slang_info_log_error(emitInfo
->log
,
1358 "Ran out of registers, too many temporaries");
1363 /* a regular variable */
1364 _slang_add_variable(emitInfo
->vt
, n
->Var
);
1365 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
1366 slang_info_log_error(emitInfo
->log
,
1367 "Ran out of registers, too many variables");
1371 printf("IR_VAR_DECL %s %d store %p\n",
1372 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1374 assert(n
->Var
->aux
== n
->Store
);
1376 if (emitInfo
->EmitComments
) {
1377 /* emit NOP with comment describing the variable's storage location */
1379 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
1381 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
1382 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
1384 inst
= emit_comment(emitInfo
, s
);
1390 /* Reference to a variable
1391 * Storage should have already been resolved/allocated.
1394 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1396 if (n
->Store
->File
== PROGRAM_STATE_VAR
&&
1397 n
->Store
->Index
< 0) {
1398 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1401 if (n
->Store
->Index
< 0) {
1402 printf("#### VAR %s not allocated!\n", (char*)n
->Var
->a_name
);
1404 assert(n
->Store
->Index
>= 0);
1405 assert(n
->Store
->Size
> 0);
1409 return emit_array_element(emitInfo
, n
);
1411 return emit_struct_field(emitInfo
, n
);
1413 return emit_swizzle(emitInfo
, n
);
1417 emit(emitInfo
, n
->Children
[0]);
1418 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1420 if (!alloc_temp_storage(emitInfo
, n
, 1))
1423 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1424 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1425 if (emitInfo
->EmitComments
)
1426 inst
->Comment
= _mesa_strdup("int to float");
1429 /* Simple arithmetic */
1463 /* trinary operators */
1465 return emit_arith(emitInfo
, n
);
1469 return emit_compare(emitInfo
, n
);
1472 return emit_clamp(emitInfo
, n
);
1476 return emit_tex(emitInfo
, n
);
1478 return emit_negation(emitInfo
, n
);
1480 /* find storage location for this float constant */
1481 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
1484 &n
->Store
->Swizzle
);
1485 if (n
->Store
->Index
< 0) {
1486 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
1492 return emit_move(emitInfo
, n
);
1495 return emit_cond(emitInfo
, n
);
1498 return emit_not(emitInfo
, n
);
1501 return emit_label(emitInfo
, n
);
1505 return emit_jump(emitInfo
, n
);
1507 return emit_kill(emitInfo
);
1510 return emit_if(emitInfo
, n
);
1513 return emit_loop(emitInfo
, n
);
1514 case IR_BREAK_IF_FALSE
:
1515 case IR_CONT_IF_FALSE
:
1516 return emit_cont_break_if(emitInfo
, n
, GL_FALSE
);
1517 case IR_BREAK_IF_TRUE
:
1518 case IR_CONT_IF_TRUE
:
1519 return emit_cont_break_if(emitInfo
, n
, GL_TRUE
);
1523 return emit_cont_break(emitInfo
, n
);
1526 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
1528 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
1530 return new_instruction(emitInfo
, OPCODE_RET
);
1536 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
1544 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
1545 struct gl_program
*prog
, GLboolean withEnd
,
1546 slang_info_log
*log
)
1548 GET_CURRENT_CONTEXT(ctx
);
1550 slang_emit_info emitInfo
;
1554 emitInfo
.prog
= prog
;
1556 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
1557 emitInfo
.EmitCondCodes
= ctx
->Shader
.EmitCondCodes
;
1558 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
1560 (void) emit(&emitInfo
, n
);
1562 /* finish up by adding the END opcode to program */
1564 struct prog_instruction
*inst
;
1565 inst
= new_instruction(&emitInfo
, OPCODE_END
);
1570 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
1571 _mesa_print_program(prog
);
1572 _mesa_print_program_parameters(ctx
,prog
);