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
]);
514 /* gen this instruction and src registers */
515 inst
= new_instruction(emitInfo
, info
->InstOpcode
);
516 for (i
= 0; i
< info
->NumParams
; i
++)
517 storage_to_src_reg(&inst
->SrcReg
[i
], n
->Children
[i
]->Store
);
520 for (i
= 0; i
< info
->NumParams
; i
++)
521 srcAnnot
[i
] = storage_annotation(n
->Children
[i
], emitInfo
->prog
);
524 for (i
= 0; i
< info
->NumParams
; i
++)
525 free_temp_storage(emitInfo
->vt
, n
->Children
[i
]);
530 /* XXX this size isn't correct, it depends on the operands */
531 if (!alloc_temp_storage(emitInfo
, n
, info
->ResultSize
))
534 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
536 dstAnnot
= storage_annotation(n
, emitInfo
->prog
);
538 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
, srcAnnot
[0],
539 srcAnnot
[1], srcAnnot
[2]);
541 /*_mesa_print_instruction(inst);*/
547 * Emit code for == and != operators. These could normally be handled
548 * by emit_arith() except we need to be able to handle structure comparisons.
550 static struct prog_instruction
*
551 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
553 struct prog_instruction
*inst
;
556 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
558 /* gen code for children */
559 emit(emitInfo
, n
->Children
[0]);
560 emit(emitInfo
, n
->Children
[1]);
562 assert(n
->Children
[0]->Store
->Size
== n
->Children
[1]->Store
->Size
);
563 size
= n
->Children
[0]->Store
->Size
;
566 gl_inst_opcode opcode
;
569 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* 1 bool */
573 opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
574 inst
= new_instruction(emitInfo
, opcode
);
575 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
576 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
577 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
579 else if (size
<= 4) {
581 gl_inst_opcode dotOp
;
585 if (!alloc_temp_storage(emitInfo
, n
, size
)) /* 'size' bools */
591 swizzle
= SWIZZLE_XYZW
;
593 else if (size
== 3) {
595 swizzle
= SWIZZLE_XYZW
;
600 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
603 /* Compute equality, inequality (tmp1 = (A ?= B)) */
604 inst
= new_instruction(emitInfo
, OPCODE_SNE
);
605 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
606 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
607 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
608 inst
->Comment
= _mesa_strdup("Compare values");
610 /* Compute tmp2 = DOT(tmp1, tmp1) (reduction) */
611 inst
= new_instruction(emitInfo
, dotOp
);
612 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
613 storage_to_src_reg(&inst
->SrcReg
[1], n
->Store
);
614 inst
->SrcReg
[0].Swizzle
= inst
->SrcReg
[1].Swizzle
= swizzle
; /*override*/
615 free_temp_storage(emitInfo
->vt
, n
); /* free tmp1 */
616 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* alloc tmp2 */
618 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
619 inst
->Comment
= _mesa_strdup("Reduce vec to bool");
621 if (n
->Opcode
== IR_EQUAL
) {
622 /* compute tmp2.x = !tmp2.x via tmp2.x = (tmp2.x == 0) */
623 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
624 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
625 constant_to_src_reg(&inst
->SrcReg
[1], 0.0, emitInfo
);
626 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
627 inst
->Comment
= _mesa_strdup("Invert true/false");
631 /* size > 4, struct compare */
633 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
634 /*printf("BEGIN COMPARE size %d\n", num);*/
635 for (i
= 0; i
< num
; i
++) {
636 inst
= new_instruction(emitInfo
, opcode
);
637 inst
->SrcReg
[0].File
= n
->Children
[0]->Store
->File
;
638 inst
->SrcReg
[0].Index
= n
->Children
[0]->Store
->Index
+ i
;
639 inst
->SrcReg
[1].File
= n
->Children
[1]->Store
->File
;
640 inst
->SrcReg
[1].Index
= n
->Children
[1]->Store
->Index
+ i
;
641 inst
->DstReg
.File
= n
->Store
->File
;
642 inst
->DstReg
.Index
= n
->Store
->Index
;
644 inst
->CondUpdate
= 1; /* update cond code */
646 inst
->DstReg
.CondMask
= COND_NE
; /* update if !=0 */
648 /*_mesa_print_instruction(inst);*/
650 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
652 _mesa_problem(NULL
, "struct comparison not implemented yet");
657 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
658 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
666 * Generate code for an IR_CLAMP instruction.
668 static struct prog_instruction
*
669 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
671 struct prog_instruction
*inst
;
673 assert(n
->Opcode
== IR_CLAMP
);
679 inst
= emit(emitInfo
, n
->Children
[0]);
681 /* If lower limit == 0.0 and upper limit == 1.0,
682 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
684 * emit OPCODE_MIN, OPCODE_MAX sequence.
687 /* XXX this isn't quite finished yet */
688 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
689 n
->Children
[1]->Value
[0] == 0.0 &&
690 n
->Children
[1]->Value
[1] == 0.0 &&
691 n
->Children
[1]->Value
[2] == 0.0 &&
692 n
->Children
[1]->Value
[3] == 0.0 &&
693 n
->Children
[2]->Opcode
== IR_FLOAT
&&
694 n
->Children
[2]->Value
[0] == 1.0 &&
695 n
->Children
[2]->Value
[1] == 1.0 &&
696 n
->Children
[2]->Value
[2] == 1.0 &&
697 n
->Children
[2]->Value
[3] == 1.0) {
699 inst
= prev_instruction(prog
);
701 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
702 /* and prev instruction's DstReg matches n->Children[0]->Store */
703 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
704 n
->Store
= n
->Children
[0]->Store
;
711 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
714 emit(emitInfo
, n
->Children
[1]);
715 emit(emitInfo
, n
->Children
[2]);
717 /* tmp = max(ch[0], ch[1]) */
718 inst
= new_instruction(emitInfo
, OPCODE_MAX
);
719 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
720 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
721 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
723 /* tmp = min(tmp, ch[2]) */
724 inst
= new_instruction(emitInfo
, OPCODE_MIN
);
725 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
726 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
727 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[2]->Store
);
733 static struct prog_instruction
*
734 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
736 /* Implement as MOV dst, -src; */
737 /* XXX we could look at the previous instruction and in some circumstances
738 * modify it to accomplish the negation.
740 struct prog_instruction
*inst
;
742 emit(emitInfo
, n
->Children
[0]);
745 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
748 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
749 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
750 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
751 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
756 static struct prog_instruction
*
757 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
761 /* XXX this fails in loop tail code - investigate someday */
762 assert(_slang_label_get_location(n
->Label
) < 0);
763 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
766 if (_slang_label_get_location(n
->Label
) < 0)
767 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
775 * Emit code for an inlined function call (subroutine).
777 static struct prog_instruction
*
778 emit_func(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
780 struct gl_program
*progSave
;
781 struct prog_instruction
*inst
;
784 assert(n
->Opcode
== IR_FUNC
);
787 /* save/push cur program */
788 progSave
= emitInfo
->prog
;
789 emitInfo
->prog
= new_subroutine(emitInfo
, &subroutineId
);
791 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
794 if (emitInfo
->EmitBeginEndSub
) {
795 /* BGNSUB isn't a real instruction.
796 * We require a label (i.e. "foobar:") though, if we're going to
797 * print the program in the NV format. The BNGSUB instruction is
798 * really just a NOP to attach the label to.
800 inst
= new_instruction(emitInfo
, OPCODE_BGNSUB
);
801 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
804 /* body of function: */
805 emit(emitInfo
, n
->Children
[0]);
806 n
->Store
= n
->Children
[0]->Store
;
808 /* add RET instruction now, if needed */
809 inst
= prev_instruction(emitInfo
);
810 if (inst
&& inst
->Opcode
!= OPCODE_RET
) {
811 inst
= new_instruction(emitInfo
, OPCODE_RET
);
814 if (emitInfo
->EmitBeginEndSub
) {
815 inst
= new_instruction(emitInfo
, OPCODE_ENDSUB
);
816 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
819 /* pop/restore cur program */
820 emitInfo
->prog
= progSave
;
822 /* emit the function call */
823 inst
= new_instruction(emitInfo
, OPCODE_CAL
);
824 /* The branch target is just the subroutine number (changed later) */
825 inst
->BranchTarget
= subroutineId
;
826 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
827 assert(inst
->BranchTarget
>= 0);
834 * Emit code for a 'return' statement.
836 static struct prog_instruction
*
837 emit_return(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
839 struct prog_instruction
*inst
;
841 assert(n
->Opcode
== IR_RETURN
);
843 inst
= new_instruction(emitInfo
, OPCODE_RET
);
844 inst
->DstReg
.CondMask
= COND_TR
; /* always return */
849 static struct prog_instruction
*
850 emit_kill(slang_emit_info
*emitInfo
)
852 struct prog_instruction
*inst
;
853 /* NV-KILL - discard fragment depending on condition code.
854 * Note that ARB-KILL depends on sign of vector operand.
856 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
857 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
862 static struct prog_instruction
*
863 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
865 struct prog_instruction
*inst
;
867 (void) emit(emitInfo
, n
->Children
[1]);
869 if (n
->Opcode
== IR_TEX
) {
870 inst
= new_instruction(emitInfo
, OPCODE_TEX
);
872 else if (n
->Opcode
== IR_TEXB
) {
873 inst
= new_instruction(emitInfo
, OPCODE_TXB
);
876 assert(n
->Opcode
== IR_TEXP
);
877 inst
= new_instruction(emitInfo
, OPCODE_TXP
);
881 if (!alloc_temp_storage(emitInfo
, n
, 4))
884 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
886 /* Child[1] is the coord */
887 assert(n
->Children
[1]->Store
->File
!= PROGRAM_UNDEFINED
);
888 assert(n
->Children
[1]->Store
->Index
>= 0);
889 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
891 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
892 assert(n
->Children
[0]->Store
);
893 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
895 inst
->Sampler
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
896 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
897 inst
->TexSrcUnit
= 27; /* Dummy value; the TexSrcUnit will be computed at
898 * link time, using the sampler uniform's value.
904 static struct prog_instruction
*
905 emit_move(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
907 struct prog_instruction
*inst
;
910 emit(emitInfo
, n
->Children
[0]);
913 assert(n
->Children
[1]);
914 inst
= emit(emitInfo
, n
->Children
[1]);
916 if (!n
->Children
[1]->Store
) {
917 slang_info_log_error(emitInfo
->log
, "invalid assignment");
920 assert(n
->Children
[1]->Store
->Index
>= 0);
922 n
->Store
= n
->Children
[0]->Store
;
924 #if PEEPHOLE_OPTIMIZATIONS
926 _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
) &&
927 (inst
->DstReg
.File
== n
->Children
[1]->Store
->File
) &&
928 (inst
->DstReg
.Index
== n
->Children
[1]->Store
->Index
)) {
929 /* Peephole optimization:
930 * The Right-Hand-Side has its results in a temporary place.
931 * Modify the RHS (and the prev instruction) to store its results
932 * in the destination specified by n->Children[0].
933 * Then, this MOVE is a no-op.
935 if (n
->Children
[1]->Opcode
!= IR_SWIZZLE
)
936 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
937 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
938 /* fixup the previous instruction (which stored the RHS result) */
939 assert(n
->Children
[0]->Store
->Index
>= 0);
940 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
946 if (n
->Children
[0]->Store
->Size
> 4) {
947 /* move matrix/struct etc (block of registers) */
948 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
949 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
950 GLint size
= srcStore
.Size
;
951 ASSERT(n
->Children
[0]->Writemask
== WRITEMASK_XYZW
);
952 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
956 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
957 inst
->Comment
= _mesa_strdup("IR_MOVE block");
958 storage_to_dst_reg(&inst
->DstReg
, &dstStore
, n
->Writemask
);
959 storage_to_src_reg(&inst
->SrcReg
[0], &srcStore
);
966 /* single register move */
967 char *srcAnnot
, *dstAnnot
;
968 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
969 assert(n
->Children
[0]->Store
->Index
>= 0);
970 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
971 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
972 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
973 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
974 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
975 srcAnnot
, NULL
, NULL
);
977 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
984 * An IR_COND node wraps a boolean expression which is used by an
985 * IF or WHILE test. This is where we'll set condition codes, if needed.
987 static struct prog_instruction
*
988 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
990 struct prog_instruction
*inst
;
992 assert(n
->Opcode
== IR_COND
);
997 /* emit code for the expression */
998 inst
= emit(emitInfo
, n
->Children
[0]);
1000 assert(n
->Children
[0]->Store
);
1001 /*assert(n->Children[0]->Store->Size == 1);*/
1003 if (emitInfo
->EmitCondCodes
) {
1005 n
->Children
[0]->Store
&&
1006 inst
->DstReg
.File
== n
->Children
[0]->Store
->File
&&
1007 inst
->DstReg
.Index
== n
->Children
[0]->Store
->Index
) {
1008 /* The previous instruction wrote to the register who's value
1009 * we're testing. Just fix that instruction so that the
1010 * condition codes are computed.
1012 inst
->CondUpdate
= GL_TRUE
;
1013 n
->Store
= n
->Children
[0]->Store
;
1017 /* This'll happen for things like "if (i) ..." where no code
1018 * is normally generated for the expression "i".
1019 * Generate a move instruction just to set condition codes.
1021 if (!alloc_temp_storage(emitInfo
, n
, 1))
1023 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1024 inst
->CondUpdate
= GL_TRUE
;
1025 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1026 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1027 _slang_free_temp(emitInfo
->vt
, n
->Store
);
1028 inst
->Comment
= _mesa_strdup("COND expr");
1033 /* No-op: the boolean result of the expression is in a regular reg */
1034 n
->Store
= n
->Children
[0]->Store
;
1043 static struct prog_instruction
*
1044 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1046 struct prog_instruction
*inst
;
1049 (void) emit(emitInfo
, n
->Children
[0]);
1050 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
1053 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1056 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
1057 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1058 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1059 constant_to_src_reg(&inst
->SrcReg
[1], 0.0, emitInfo
);
1060 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
1062 inst
->Comment
= _mesa_strdup("NOT");
1067 static struct prog_instruction
*
1068 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1070 struct gl_program
*prog
= emitInfo
->prog
;
1071 struct prog_instruction
*ifInst
, *inst
;
1072 GLuint ifInstLoc
, elseInstLoc
= 0;
1073 GLuint condWritemask
= 0;
1075 inst
= emit(emitInfo
, n
->Children
[0]); /* the condition */
1076 if (emitInfo
->EmitCondCodes
) {
1078 condWritemask
= inst
->DstReg
.WriteMask
;
1082 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
1085 ifInstLoc
= prog
->NumInstructions
;
1086 if (emitInfo
->EmitHighLevelInstructions
) {
1087 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
1088 if (emitInfo
->EmitCondCodes
) {
1089 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
1090 /* only test the cond code (1 of 4) that was updated by the
1091 * previous instruction.
1093 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1097 storage_to_src_reg(&ifInst
->SrcReg
[0], n
->Children
[0]->Store
);
1101 /* conditional jump to else, or endif */
1102 ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1103 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
1104 ifInst
->Comment
= _mesa_strdup("if zero");
1105 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1109 emit(emitInfo
, n
->Children
[1]);
1111 if (n
->Children
[2]) {
1112 /* have else body */
1113 elseInstLoc
= prog
->NumInstructions
;
1114 if (emitInfo
->EmitHighLevelInstructions
) {
1115 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
1118 /* jump to endif instruction */
1119 struct prog_instruction
*inst
;
1120 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1121 inst
->Comment
= _mesa_strdup("else");
1122 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1124 ifInst
= prog
->Instructions
+ ifInstLoc
;
1125 ifInst
->BranchTarget
= prog
->NumInstructions
;
1127 emit(emitInfo
, n
->Children
[2]);
1131 ifInst
= prog
->Instructions
+ ifInstLoc
;
1132 ifInst
->BranchTarget
= prog
->NumInstructions
/*+ 1*/;
1135 if (emitInfo
->EmitHighLevelInstructions
) {
1136 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1139 if (n
->Children
[2]) {
1140 struct prog_instruction
*elseInst
;
1141 elseInst
= prog
->Instructions
+ elseInstLoc
;
1142 elseInst
->BranchTarget
= prog
->NumInstructions
;
1148 static struct prog_instruction
*
1149 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1151 struct gl_program
*prog
= emitInfo
->prog
;
1152 struct prog_instruction
*beginInst
, *endInst
;
1153 GLuint beginInstLoc
, tailInstLoc
, endInstLoc
;
1156 /* emit OPCODE_BGNLOOP */
1157 beginInstLoc
= prog
->NumInstructions
;
1158 if (emitInfo
->EmitHighLevelInstructions
) {
1159 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1163 emit(emitInfo
, n
->Children
[0]);
1166 tailInstLoc
= prog
->NumInstructions
;
1167 if (n
->Children
[1]) {
1168 if (emitInfo
->EmitComments
)
1169 emit_comment(emitInfo
, "Loop tail code:");
1170 emit(emitInfo
, n
->Children
[1]);
1173 endInstLoc
= prog
->NumInstructions
;
1174 if (emitInfo
->EmitHighLevelInstructions
) {
1175 /* emit OPCODE_ENDLOOP */
1176 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1179 /* emit unconditional BRA-nch */
1180 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1181 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1183 /* ENDLOOP's BranchTarget points to the BGNLOOP inst */
1184 endInst
->BranchTarget
= beginInstLoc
;
1186 if (emitInfo
->EmitHighLevelInstructions
) {
1187 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1188 beginInst
= prog
->Instructions
+ beginInstLoc
;
1189 beginInst
->BranchTarget
= prog
->NumInstructions
- 1;
1192 /* Done emitting loop code. Now walk over the loop's linked list of
1193 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1194 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1196 for (ir
= n
->List
; ir
; ir
= ir
->List
) {
1197 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1198 assert(inst
->BranchTarget
< 0);
1199 if (ir
->Opcode
== IR_BREAK
||
1200 ir
->Opcode
== IR_BREAK_IF_FALSE
||
1201 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1202 assert(inst
->Opcode
== OPCODE_BRK
||
1203 inst
->Opcode
== OPCODE_BRK0
||
1204 inst
->Opcode
== OPCODE_BRK1
||
1205 inst
->Opcode
== OPCODE_BRA
);
1206 /* go to instruction after end of loop */
1207 inst
->BranchTarget
= endInstLoc
+ 1;
1210 assert(ir
->Opcode
== IR_CONT
||
1211 ir
->Opcode
== IR_CONT_IF_FALSE
||
1212 ir
->Opcode
== IR_CONT_IF_TRUE
);
1213 assert(inst
->Opcode
== OPCODE_CONT
||
1214 inst
->Opcode
== OPCODE_CONT0
||
1215 inst
->Opcode
== OPCODE_CONT1
||
1216 inst
->Opcode
== OPCODE_BRA
);
1217 /* go to instruction at tail of loop */
1218 inst
->BranchTarget
= endInstLoc
;
1226 * Unconditional "continue" or "break" statement.
1227 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1229 static struct prog_instruction
*
1230 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1232 gl_inst_opcode opcode
;
1233 struct prog_instruction
*inst
;
1235 if (n
->Opcode
== IR_CONT
) {
1236 /* we need to execute the loop's tail code before doing CONT */
1238 assert(n
->Parent
->Opcode
== IR_LOOP
);
1239 if (n
->Parent
->Children
[1]) {
1240 /* emit tail code */
1241 if (emitInfo
->EmitComments
) {
1242 emit_comment(emitInfo
, "continue - tail code:");
1244 emit(emitInfo
, n
->Parent
->Children
[1]);
1248 /* opcode selection */
1249 if (emitInfo
->EmitHighLevelInstructions
) {
1250 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1253 opcode
= OPCODE_BRA
;
1255 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1256 inst
= new_instruction(emitInfo
, opcode
);
1257 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1263 * Conditional "continue" or "break" statement.
1264 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1266 static struct prog_instruction
*
1267 emit_cont_break_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
1268 GLboolean breakTrue
)
1270 gl_inst_opcode opcode
;
1271 struct prog_instruction
*inst
;
1273 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1274 n
->Opcode
== IR_CONT_IF_FALSE
||
1275 n
->Opcode
== IR_BREAK_IF_TRUE
||
1276 n
->Opcode
== IR_BREAK_IF_FALSE
);
1278 /* evaluate condition expr, setting cond codes */
1279 inst
= emit(emitInfo
, n
->Children
[0]);
1280 if (emitInfo
->EmitCondCodes
) {
1282 inst
->CondUpdate
= GL_TRUE
;
1285 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1287 /* opcode selection */
1288 if (emitInfo
->EmitHighLevelInstructions
) {
1289 if (emitInfo
->EmitCondCodes
) {
1290 if (n
->Opcode
== IR_CONT_IF_TRUE
||
1291 n
->Opcode
== IR_CONT_IF_FALSE
)
1292 opcode
= OPCODE_CONT
;
1294 opcode
= OPCODE_BRK
;
1297 if (n
->Opcode
== IR_CONT_IF_TRUE
)
1298 opcode
= OPCODE_CONT1
;
1299 else if (n
->Opcode
== IR_CONT_IF_FALSE
)
1300 opcode
= OPCODE_CONT0
;
1301 else if (n
->Opcode
== IR_BREAK_IF_TRUE
)
1302 opcode
= OPCODE_BRK1
;
1303 else if (n
->Opcode
== IR_BREAK_IF_FALSE
)
1304 opcode
= OPCODE_BRK0
;
1308 opcode
= OPCODE_BRA
;
1311 inst
= new_instruction(emitInfo
, opcode
);
1312 if (emitInfo
->EmitCondCodes
) {
1313 inst
->DstReg
.CondMask
= breakTrue
? COND_NE
: COND_EQ
;
1316 /* BRK0, BRK1, CONT0, CONT1 uses SrcReg[0] as the condition */
1317 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1325 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1326 * Ex: fix_swizzle("zyNN") -> "zyyy"
1329 fix_swizzle(GLuint swizzle
)
1332 for (i
= 0; i
< 4; i
++) {
1333 swz
[i
] = GET_SWZ(swizzle
, i
);
1334 if (swz
[i
] == SWIZZLE_NIL
) {
1335 swz
[i
] = swz
[i
- 1];
1338 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1343 * Return the number of components actually named by the swizzle.
1344 * Recall that swizzles may have undefined/don't-care values.
1347 swizzle_size(GLuint swizzle
)
1350 for (i
= 0; i
< 4; i
++) {
1351 GLuint swz
= GET_SWZ(swizzle
, i
);
1352 size
+= (swz
>= 0 && swz
<= 3);
1358 static struct prog_instruction
*
1359 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1362 struct prog_instruction
*inst
;
1364 inst
= emit(emitInfo
, n
->Children
[0]);
1368 GLuint s
= n
->Children
[0]->Store
->Swizzle
;
1369 assert(GET_SWZ(s
, 0) != SWIZZLE_NIL
);
1370 assert(GET_SWZ(s
, 1) != SWIZZLE_NIL
);
1371 assert(GET_SWZ(s
, 2) != SWIZZLE_NIL
);
1372 assert(GET_SWZ(s
, 3) != SWIZZLE_NIL
);
1375 /* For debug: n->Var = n->Children[0]->Var; */
1377 /* "pull-up" the child's storage info, applying our swizzle info */
1378 n
->Store
->File
= n
->Children
[0]->Store
->File
;
1379 n
->Store
->Index
= n
->Children
[0]->Store
->Index
;
1380 n
->Store
->Size
= swizzle_size(n
->Store
->Swizzle
);
1382 printf("Emit Swizzle %s reg %d chSize %d mySize %d\n",
1383 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0),
1384 n
->Store
->Index
, n
->Children
[0]->Store
->Size
,
1388 /* apply this swizzle to child's swizzle to get composed swizzle */
1389 swizzle
= fix_swizzle(n
->Store
->Swizzle
); /* remove the don't care terms */
1390 n
->Store
->Swizzle
= swizzle_swizzle(n
->Children
[0]->Store
->Swizzle
,
1398 * Dereference array element. Just resolve storage for the array
1399 * element represented by this node.
1401 static struct prog_instruction
*
1402 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1405 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1406 assert(n
->Store
->Size
> 0);
1408 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1409 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1413 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1414 /* Constant index */
1415 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1416 const GLint index
= (GLint
) n
->Children
[1]->Value
[0];
1417 n
->Store
->Index
= arrayAddr
+ index
;
1420 /* Variable index - PROBLEM */
1421 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1422 const GLint index
= 0;
1423 _mesa_problem(NULL
, "variable array indexes not supported yet!");
1424 n
->Store
->Index
= arrayAddr
+ index
;
1426 return NULL
; /* no instruction */
1431 * Resolve storage for accessing a structure field.
1433 static struct prog_instruction
*
1434 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1436 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1437 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1440 GLint offset
= n
->FieldOffset
/ 4;
1441 assert(n
->Children
[0]->Store
->Index
>= 0);
1442 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ offset
;
1443 if (n
->Store
->Size
== 1) {
1444 GLint swz
= n
->FieldOffset
% 4;
1445 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
1448 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
1451 return NULL
; /* no instruction */
1455 static struct prog_instruction
*
1456 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1458 struct prog_instruction
*inst
;
1462 switch (n
->Opcode
) {
1464 /* sequence of two sub-trees */
1465 assert(n
->Children
[0]);
1466 assert(n
->Children
[1]);
1467 emit(emitInfo
, n
->Children
[0]);
1468 inst
= emit(emitInfo
, n
->Children
[1]);
1472 n
->Store
= n
->Children
[1]->Store
;
1476 /* new variable scope */
1477 _slang_push_var_table(emitInfo
->vt
);
1478 inst
= emit(emitInfo
, n
->Children
[0]);
1479 _slang_pop_var_table(emitInfo
->vt
);
1483 /* Variable declaration - allocate a register for it */
1485 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1486 assert(n
->Store
->Size
> 0);
1487 /*assert(n->Store->Index < 0);*/
1488 if (!n
->Var
|| n
->Var
->isTemp
) {
1489 /* a nameless/temporary variable, will be freed after first use */
1491 if (n
->Store
->Index
< 0 && !_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
1492 slang_info_log_error(emitInfo
->log
,
1493 "Ran out of registers, too many temporaries");
1498 /* a regular variable */
1499 _slang_add_variable(emitInfo
->vt
, n
->Var
);
1500 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
1501 slang_info_log_error(emitInfo
->log
,
1502 "Ran out of registers, too many variables");
1506 printf("IR_VAR_DECL %s %d store %p\n",
1507 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1509 assert(n
->Var
->aux
== n
->Store
);
1511 if (emitInfo
->EmitComments
) {
1512 /* emit NOP with comment describing the variable's storage location */
1514 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
1516 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
1517 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
1519 inst
= emit_comment(emitInfo
, s
);
1525 /* Reference to a variable
1526 * Storage should have already been resolved/allocated.
1529 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1531 if (n
->Store
->File
== PROGRAM_STATE_VAR
&&
1532 n
->Store
->Index
< 0) {
1533 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1536 if (n
->Store
->Index
< 0) {
1537 printf("#### VAR %s not allocated!\n", (char*)n
->Var
->a_name
);
1539 assert(n
->Store
->Index
>= 0);
1540 assert(n
->Store
->Size
> 0);
1544 return emit_array_element(emitInfo
, n
);
1546 return emit_struct_field(emitInfo
, n
);
1548 return emit_swizzle(emitInfo
, n
);
1552 emit(emitInfo
, n
->Children
[0]);
1553 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1555 if (!alloc_temp_storage(emitInfo
, n
, 1))
1558 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1559 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1560 if (emitInfo
->EmitComments
)
1561 inst
->Comment
= _mesa_strdup("int to float");
1564 /* Simple arithmetic */
1598 /* trinary operators */
1600 return emit_arith(emitInfo
, n
);
1604 return emit_compare(emitInfo
, n
);
1607 return emit_clamp(emitInfo
, n
);
1611 return emit_tex(emitInfo
, n
);
1613 return emit_negation(emitInfo
, n
);
1615 /* find storage location for this float constant */
1616 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
1619 &n
->Store
->Swizzle
);
1620 if (n
->Store
->Index
< 0) {
1621 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
1627 return emit_move(emitInfo
, n
);
1630 return emit_cond(emitInfo
, n
);
1633 return emit_not(emitInfo
, n
);
1636 return emit_label(emitInfo
, n
);
1639 return emit_kill(emitInfo
);
1642 /* new variable scope for subroutines/function calls*/
1643 _slang_push_var_table(emitInfo
->vt
);
1644 inst
= emit_func(emitInfo
, n
);
1645 _slang_pop_var_table(emitInfo
->vt
);
1649 return emit_if(emitInfo
, n
);
1652 return emit_loop(emitInfo
, n
);
1653 case IR_BREAK_IF_FALSE
:
1654 case IR_CONT_IF_FALSE
:
1655 return emit_cont_break_if(emitInfo
, n
, GL_FALSE
);
1656 case IR_BREAK_IF_TRUE
:
1657 case IR_CONT_IF_TRUE
:
1658 return emit_cont_break_if(emitInfo
, n
, GL_TRUE
);
1662 return emit_cont_break(emitInfo
, n
);
1665 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
1667 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
1669 return emit_return(emitInfo
, n
);
1675 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
1682 * After code generation, any subroutines will be in separate program
1683 * objects. This function appends all the subroutines onto the main
1684 * program and resolves the linking of all the branch/call instructions.
1685 * XXX this logic should really be part of the linking process...
1688 _slang_resolve_subroutines(slang_emit_info
*emitInfo
)
1690 GET_CURRENT_CONTEXT(ctx
);
1691 struct gl_program
*mainP
= emitInfo
->prog
;
1692 GLuint
*subroutineLoc
, i
, total
;
1695 = (GLuint
*) _mesa_malloc(emitInfo
->NumSubroutines
* sizeof(GLuint
));
1697 /* total number of instructions */
1698 total
= mainP
->NumInstructions
;
1699 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1700 subroutineLoc
[i
] = total
;
1701 total
+= emitInfo
->Subroutines
[i
]->NumInstructions
;
1704 /* adjust BrancTargets within the functions */
1705 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1706 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1708 for (j
= 0; j
< sub
->NumInstructions
; j
++) {
1709 struct prog_instruction
*inst
= sub
->Instructions
+ j
;
1710 if (inst
->Opcode
!= OPCODE_CAL
&& inst
->BranchTarget
>= 0) {
1711 inst
->BranchTarget
+= subroutineLoc
[i
];
1716 /* append subroutines' instructions after main's instructions */
1717 mainP
->Instructions
= _mesa_realloc_instructions(mainP
->Instructions
,
1718 mainP
->NumInstructions
,
1720 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1721 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1722 _mesa_copy_instructions(mainP
->Instructions
+ subroutineLoc
[i
],
1724 sub
->NumInstructions
);
1725 /* delete subroutine code */
1726 sub
->Parameters
= NULL
; /* prevent double-free */
1727 _mesa_delete_program(ctx
, sub
);
1729 mainP
->NumInstructions
= total
;
1731 /* Examine CAL instructions.
1732 * At this point, the BranchTarget field of the CAL instructions is
1733 * the number/id of the subroutine to call (an index into the
1734 * emitInfo->Subroutines list).
1735 * Translate that into an actual instruction location now.
1737 for (i
= 0; i
< mainP
->NumInstructions
; i
++) {
1738 struct prog_instruction
*inst
= mainP
->Instructions
+ i
;
1739 if (inst
->Opcode
== OPCODE_CAL
) {
1740 const GLuint f
= inst
->BranchTarget
;
1741 inst
->BranchTarget
= subroutineLoc
[f
];
1745 _mesa_free(subroutineLoc
);
1752 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
1753 struct gl_program
*prog
, GLboolean withEnd
,
1754 slang_info_log
*log
)
1756 GET_CURRENT_CONTEXT(ctx
);
1758 slang_emit_info emitInfo
;
1762 emitInfo
.prog
= prog
;
1763 emitInfo
.Subroutines
= NULL
;
1764 emitInfo
.NumSubroutines
= 0;
1766 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
1767 emitInfo
.EmitCondCodes
= ctx
->Shader
.EmitCondCodes
;
1768 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
1769 emitInfo
.EmitBeginEndSub
= 0; /* XXX for compiler debug only */
1771 if (!emitInfo
.EmitCondCodes
) {
1772 emitInfo
.EmitHighLevelInstructions
= GL_TRUE
;
1775 (void) emit(&emitInfo
, n
);
1777 /* finish up by adding the END opcode to program */
1779 struct prog_instruction
*inst
;
1780 inst
= new_instruction(&emitInfo
, OPCODE_END
);
1783 _slang_resolve_subroutines(&emitInfo
);
1788 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
1789 _mesa_print_program(prog
);
1790 _mesa_print_program_parameters(ctx
,prog
);