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
];
91 * Swizzle a swizzle. That is, return swz2(swz1)
94 swizzle_swizzle(GLuint swz1
, GLuint swz2
)
97 for (i
= 0; i
< 4; i
++) {
98 GLuint c
= GET_SWZ(swz2
, i
);
99 s
[i
] = GET_SWZ(swz1
, c
);
101 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
107 _slang_new_ir_storage(enum register_file file
, GLint index
, GLint size
)
109 slang_ir_storage
*st
;
110 st
= (slang_ir_storage
*) _mesa_calloc(sizeof(slang_ir_storage
));
115 st
->Swizzle
= SWIZZLE_NOOP
;
122 * Allocate temporary storage for an intermediate result (such as for
123 * a multiply or add, etc.
126 alloc_temp_storage(slang_emit_info
*emitInfo
, slang_ir_node
*n
, GLint size
)
131 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
132 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
133 slang_info_log_error(emitInfo
->log
,
134 "Ran out of registers, too many temporaries");
142 * Free temporary storage, if n->Store is, in fact, temp storage.
146 free_temp_storage(slang_var_table
*vt
, slang_ir_node
*n
)
148 if (n
->Store
->File
== PROGRAM_TEMPORARY
&&
149 n
->Store
->Index
>= 0 &&
150 n
->Opcode
!= IR_SWIZZLE
) {
151 if (_slang_is_temp(vt
, n
->Store
)) {
152 _slang_free_temp(vt
, n
->Store
);
153 n
->Store
->Index
= -1;
161 * Convert IR storage to an instruction dst register.
164 storage_to_dst_reg(struct prog_dst_register
*dst
, const slang_ir_storage
*st
,
167 assert(st
->Index
>= 0);
168 dst
->File
= st
->File
;
169 dst
->Index
= st
->Index
;
170 assert(st
->File
!= PROGRAM_UNDEFINED
);
171 assert(st
->Size
>= 1);
172 assert(st
->Size
<= 4);
174 GLuint comp
= GET_SWZ(st
->Swizzle
, 0);
176 dst
->WriteMask
= WRITEMASK_X
<< comp
;
179 dst
->WriteMask
= writemask
;
185 * Convert IR storage to an instruction src register.
188 storage_to_src_reg(struct prog_src_register
*src
, const slang_ir_storage
*st
)
190 static const GLuint defaultSwizzle
[4] = {
191 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
192 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
193 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
194 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
)
196 assert(st
->File
>= 0);
197 assert(st
->File
< PROGRAM_UNDEFINED
);
198 assert(st
->Size
>= 1);
199 assert(st
->Size
<= 4);
200 src
->File
= st
->File
;
201 src
->Index
= st
->Index
;
202 if (st
->Swizzle
!= SWIZZLE_NOOP
)
203 src
->Swizzle
= st
->Swizzle
;
205 src
->Swizzle
= defaultSwizzle
[st
->Size
- 1]; /*XXX really need this?*/
207 assert(GET_SWZ(src
->Swizzle
, 0) <= 3);
208 assert(GET_SWZ(src
->Swizzle
, 1) <= 3);
209 assert(GET_SWZ(src
->Swizzle
, 2) <= 3);
210 assert(GET_SWZ(src
->Swizzle
, 3) <= 3);
216 * Add new instruction at end of given program.
217 * \param prog the program to append instruction onto
218 * \param opcode opcode for the new instruction
219 * \return pointer to the new instruction
221 static struct prog_instruction
*
222 new_instruction(slang_emit_info
*emitInfo
, gl_inst_opcode opcode
)
224 struct gl_program
*prog
= emitInfo
->prog
;
225 struct prog_instruction
*inst
;
228 /* print prev inst */
229 if (prog
->NumInstructions
> 0) {
230 _mesa_print_instruction(prog
->Instructions
+ prog
->NumInstructions
- 1);
233 prog
->Instructions
= _mesa_realloc_instructions(prog
->Instructions
,
234 prog
->NumInstructions
,
235 prog
->NumInstructions
+ 1);
236 inst
= prog
->Instructions
+ prog
->NumInstructions
;
237 prog
->NumInstructions
++;
238 _mesa_init_instructions(inst
, 1);
239 inst
->Opcode
= opcode
;
240 inst
->BranchTarget
= -1; /* invalid */
242 printf("New inst %d: %p %s\n", prog->NumInstructions-1,(void*)inst,
243 _mesa_opcode_string(inst->Opcode));
250 * Return pointer to last instruction in program.
252 static struct prog_instruction
*
253 prev_instruction(slang_emit_info
*emitInfo
)
255 struct gl_program
*prog
= emitInfo
->prog
;
256 if (prog
->NumInstructions
== 0)
259 return prog
->Instructions
+ prog
->NumInstructions
- 1;
263 static struct prog_instruction
*
264 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
);
268 * Return an annotation string for given node's storage.
271 storage_annotation(const slang_ir_node
*n
, const struct gl_program
*prog
)
274 const slang_ir_storage
*st
= n
->Store
;
275 static char s
[100] = "";
278 return _mesa_strdup("");
281 case PROGRAM_CONSTANT
:
282 if (st
->Index
>= 0) {
283 const GLfloat
*val
= prog
->Parameters
->ParameterValues
[st
->Index
];
284 if (st
->Swizzle
== SWIZZLE_NOOP
)
285 sprintf(s
, "{%g, %g, %g, %g}", val
[0], val
[1], val
[2], val
[3]);
287 sprintf(s
, "%g", val
[GET_SWZ(st
->Swizzle
, 0)]);
291 case PROGRAM_TEMPORARY
:
293 sprintf(s
, "%s", (char *) n
->Var
->a_name
);
295 sprintf(s
, "t[%d]", st
->Index
);
297 case PROGRAM_STATE_VAR
:
298 case PROGRAM_UNIFORM
:
299 sprintf(s
, "%s", prog
->Parameters
->Parameters
[st
->Index
].Name
);
301 case PROGRAM_VARYING
:
302 sprintf(s
, "%s", prog
->Varying
->Parameters
[st
->Index
].Name
);
305 sprintf(s
, "input[%d]", st
->Index
);
308 sprintf(s
, "output[%d]", st
->Index
);
313 return _mesa_strdup(s
);
321 * Return an annotation string for an instruction.
324 instruction_annotation(gl_inst_opcode opcode
, char *dstAnnot
,
325 char *srcAnnot0
, char *srcAnnot1
, char *srcAnnot2
)
328 const char *operator;
333 len
+= strlen(dstAnnot
);
335 dstAnnot
= _mesa_strdup("");
338 len
+= strlen(srcAnnot0
);
340 srcAnnot0
= _mesa_strdup("");
343 len
+= strlen(srcAnnot1
);
345 srcAnnot1
= _mesa_strdup("");
348 len
+= strlen(srcAnnot2
);
350 srcAnnot2
= _mesa_strdup("");
381 s
= (char *) malloc(len
);
382 sprintf(s
, "%s = %s %s %s %s", dstAnnot
,
383 srcAnnot0
, operator, srcAnnot1
, srcAnnot2
);
384 assert(_mesa_strlen(s
) < len
);
399 * Emit an instruction that's just a comment.
401 static struct prog_instruction
*
402 emit_comment(slang_emit_info
*emitInfo
, const char *s
)
404 struct prog_instruction
*inst
= new_instruction(emitInfo
, OPCODE_NOP
);
406 inst
->Comment
= _mesa_strdup(s
);
413 * Generate code for a simple arithmetic instruction.
414 * Either 1, 2 or 3 operands.
416 static struct prog_instruction
*
417 emit_arith(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
419 struct prog_instruction
*inst
;
420 const slang_ir_info
*info
= _slang_ir_info(n
->Opcode
);
421 char *srcAnnot
[3], *dstAnnot
;
425 assert(info
->InstOpcode
!= OPCODE_NOP
);
427 srcAnnot
[0] = srcAnnot
[1] = srcAnnot
[2] = dstAnnot
= NULL
;
429 #if PEEPHOLE_OPTIMIZATIONS
430 /* Look for MAD opportunity */
431 if (info
->NumParams
== 2 &&
432 n
->Opcode
== IR_ADD
&& n
->Children
[0]->Opcode
== IR_MUL
) {
433 /* found pattern IR_ADD(IR_MUL(A, B), C) */
434 emit(emitInfo
, n
->Children
[0]->Children
[0]); /* A */
435 emit(emitInfo
, n
->Children
[0]->Children
[1]); /* B */
436 emit(emitInfo
, n
->Children
[1]); /* C */
437 /* generate MAD instruction */
438 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
439 /* operands: A, B, C: */
440 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Children
[0]->Store
);
441 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[0]->Children
[1]->Store
);
442 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[1]->Store
);
443 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[0]);
444 free_temp_storage(emitInfo
->vt
, n
->Children
[0]->Children
[1]);
445 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
447 else if (info
->NumParams
== 2 &&
448 n
->Opcode
== IR_ADD
&& n
->Children
[1]->Opcode
== IR_MUL
) {
449 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
450 emit(emitInfo
, n
->Children
[0]); /* A */
451 emit(emitInfo
, n
->Children
[1]->Children
[0]); /* B */
452 emit(emitInfo
, n
->Children
[1]->Children
[1]); /* C */
453 /* generate MAD instruction */
454 inst
= new_instruction(emitInfo
, OPCODE_MAD
);
455 /* operands: B, C, A */
456 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Children
[0]->Store
);
457 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Children
[1]->Store
);
458 storage_to_src_reg(&inst
->SrcReg
[2], n
->Children
[0]->Store
);
459 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[0]);
460 free_temp_storage(emitInfo
->vt
, n
->Children
[1]->Children
[1]);
461 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
468 /* gen code for children */
469 for (i
= 0; i
< info
->NumParams
; i
++)
470 emit(emitInfo
, n
->Children
[i
]);
472 /* gen this instruction and src registers */
473 inst
= new_instruction(emitInfo
, info
->InstOpcode
);
474 for (i
= 0; i
< info
->NumParams
; i
++)
475 storage_to_src_reg(&inst
->SrcReg
[i
], n
->Children
[i
]->Store
);
478 for (i
= 0; i
< info
->NumParams
; i
++)
479 srcAnnot
[i
] = storage_annotation(n
->Children
[i
], emitInfo
->prog
);
482 for (i
= 0; i
< info
->NumParams
; i
++)
483 free_temp_storage(emitInfo
->vt
, n
->Children
[i
]);
488 if (!alloc_temp_storage(emitInfo
, n
, info
->ResultSize
))
491 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
493 dstAnnot
= storage_annotation(n
, emitInfo
->prog
);
495 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
, srcAnnot
[0],
496 srcAnnot
[1], srcAnnot
[2]);
498 /*_mesa_print_instruction(inst);*/
504 * Emit code for == and != operators. These could normally be handled
505 * by emit_arith() except we need to be able to handle structure comparisons.
507 static struct prog_instruction
*
508 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
510 struct prog_instruction
*inst
;
513 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
515 /* gen code for children */
516 emit(emitInfo
, n
->Children
[0]);
517 emit(emitInfo
, n
->Children
[1]);
519 assert(n
->Children
[0]->Store
->Size
== n
->Children
[1]->Store
->Size
);
520 size
= n
->Children
[0]->Store
->Size
;
523 gl_inst_opcode opcode
;
526 if (!alloc_temp_storage(emitInfo
, n
, 1)) /* 1 bool */
530 opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
531 inst
= new_instruction(emitInfo
, opcode
);
532 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
533 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
534 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
536 else if (size
<= 4) {
537 static const GLfloat zero
[4] = { 0, 0, 0, 0 };
538 GLuint zeroSwizzle
, swizzle
;
539 GLint zeroReg
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
540 zero
, 4, &zeroSwizzle
);
541 gl_inst_opcode dotOp
;
543 assert(zeroReg
>= 0);
546 if (!alloc_temp_storage(emitInfo
, n
, 4)) /* 4 bools */
552 swizzle
= SWIZZLE_XYZW
;
554 else if (size
== 3) {
556 swizzle
= SWIZZLE_XYZW
;
561 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
564 /* Compute equality, inequality */
565 inst
= new_instruction(emitInfo
, OPCODE_SNE
);
566 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
567 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
568 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
569 inst
->Comment
= _mesa_strdup("Compare values");
570 /* compute D = DP4(D, D) (reduction) */
571 inst
= new_instruction(emitInfo
, dotOp
);
572 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
573 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
574 inst
->SrcReg
[0].Swizzle
= swizzle
;
575 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
576 inst
->SrcReg
[1].Index
= n
->Store
->Index
;
577 inst
->SrcReg
[1].Swizzle
= swizzle
;
578 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
579 inst
->DstReg
.Index
= n
->Store
->Index
;
580 inst
->Comment
= _mesa_strdup("Reduce vec to bool");
581 if (n
->Opcode
== IR_EQUAL
) {
582 /* compute D.x = !D.x via D.x = (D.x == 0) */
583 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
584 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
585 inst
->SrcReg
[0].Index
= n
->Store
->Index
;
586 inst
->SrcReg
[1].File
= PROGRAM_CONSTANT
;
587 inst
->SrcReg
[1].Index
= zeroReg
;
588 inst
->SrcReg
[1].Swizzle
= zeroSwizzle
;
589 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
590 inst
->DstReg
.Index
= n
->Store
->Index
;
591 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
592 inst
->Comment
= _mesa_strdup("Invert true/false");
596 /* size > 4, struct compare */
598 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
599 /*printf("BEGIN COMPARE size %d\n", num);*/
600 for (i
= 0; i
< num
; i
++) {
601 inst
= new_instruction(emitInfo
, opcode
);
602 inst
->SrcReg
[0].File
= n
->Children
[0]->Store
->File
;
603 inst
->SrcReg
[0].Index
= n
->Children
[0]->Store
->Index
+ i
;
604 inst
->SrcReg
[1].File
= n
->Children
[1]->Store
->File
;
605 inst
->SrcReg
[1].Index
= n
->Children
[1]->Store
->Index
+ i
;
606 inst
->DstReg
.File
= n
->Store
->File
;
607 inst
->DstReg
.Index
= n
->Store
->Index
;
609 inst
->CondUpdate
= 1; /* update cond code */
611 inst
->DstReg
.CondMask
= COND_NE
; /* update if !=0 */
613 /*_mesa_print_instruction(inst);*/
615 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
617 _mesa_problem(NULL
, "struct comparison not implemented yet");
622 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
623 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
631 * Generate code for an IR_CLAMP instruction.
633 static struct prog_instruction
*
634 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
636 struct prog_instruction
*inst
;
638 assert(n
->Opcode
== IR_CLAMP
);
644 inst
= emit(emitInfo
, n
->Children
[0]);
646 /* If lower limit == 0.0 and upper limit == 1.0,
647 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
649 * emit OPCODE_MIN, OPCODE_MAX sequence.
652 /* XXX this isn't quite finished yet */
653 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
654 n
->Children
[1]->Value
[0] == 0.0 &&
655 n
->Children
[1]->Value
[1] == 0.0 &&
656 n
->Children
[1]->Value
[2] == 0.0 &&
657 n
->Children
[1]->Value
[3] == 0.0 &&
658 n
->Children
[2]->Opcode
== IR_FLOAT
&&
659 n
->Children
[2]->Value
[0] == 1.0 &&
660 n
->Children
[2]->Value
[1] == 1.0 &&
661 n
->Children
[2]->Value
[2] == 1.0 &&
662 n
->Children
[2]->Value
[3] == 1.0) {
664 inst
= prev_instruction(prog
);
666 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
667 /* and prev instruction's DstReg matches n->Children[0]->Store */
668 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
669 n
->Store
= n
->Children
[0]->Store
;
676 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
679 emit(emitInfo
, n
->Children
[1]);
680 emit(emitInfo
, n
->Children
[2]);
682 /* tmp = max(ch[0], ch[1]) */
683 inst
= new_instruction(emitInfo
, OPCODE_MAX
);
684 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
685 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
686 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[1]->Store
);
688 /* tmp = min(tmp, ch[2]) */
689 inst
= new_instruction(emitInfo
, OPCODE_MIN
);
690 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
691 storage_to_src_reg(&inst
->SrcReg
[0], n
->Store
);
692 storage_to_src_reg(&inst
->SrcReg
[1], n
->Children
[2]->Store
);
698 static struct prog_instruction
*
699 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
701 /* Implement as MOV dst, -src; */
702 /* XXX we could look at the previous instruction and in some circumstances
703 * modify it to accomplish the negation.
705 struct prog_instruction
*inst
;
707 emit(emitInfo
, n
->Children
[0]);
710 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
713 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
714 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
715 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
716 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
721 static struct prog_instruction
*
722 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
726 /* XXX this fails in loop tail code - investigate someday */
727 assert(_slang_label_get_location(n
->Label
) < 0);
728 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
731 if (_slang_label_get_location(n
->Label
) < 0)
732 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
740 * Emit code for an inlined function call (subroutine).
742 static struct prog_instruction
*
743 emit_func(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
745 struct gl_program
*progSave
;
746 struct prog_instruction
*inst
;
749 assert(n
->Opcode
== IR_FUNC
);
752 /* save/push cur program */
753 progSave
= emitInfo
->prog
;
754 emitInfo
->prog
= new_subroutine(emitInfo
, &subroutineId
);
756 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
759 if (emitInfo
->EmitBeginEndSub
) {
760 inst
= new_instruction(emitInfo
, OPCODE_BGNSUB
);
761 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
764 /* body of function: */
765 emit(emitInfo
, n
->Children
[0]);
766 n
->Store
= n
->Children
[0]->Store
;
768 /* add RET instruction now, if needed */
769 inst
= prev_instruction(emitInfo
);
770 if (inst
&& inst
->Opcode
!= OPCODE_RET
) {
771 inst
= new_instruction(emitInfo
, OPCODE_RET
);
774 if (emitInfo
->EmitBeginEndSub
) {
775 inst
= new_instruction(emitInfo
, OPCODE_ENDSUB
);
776 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
779 /* pop/restore cur program */
780 emitInfo
->prog
= progSave
;
782 /* emit the function call */
783 inst
= new_instruction(emitInfo
, OPCODE_CAL
);
784 /* The branch target is just the subroutine number (changed later) */
785 inst
->BranchTarget
= subroutineId
;
786 inst
->Comment
= _mesa_strdup(n
->Label
->Name
);
787 assert(inst
->BranchTarget
>= 0);
794 * Emit code for a 'return' statement.
796 static struct prog_instruction
*
797 emit_return(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
799 struct prog_instruction
*inst
;
801 assert(n
->Opcode
== IR_RETURN
);
803 inst
= new_instruction(emitInfo
, OPCODE_RET
/*BRA*/); /*XXX TEMPORARY*/
804 inst
->DstReg
.CondMask
= COND_TR
; /* always return/branch */
806 if (inst
->Opcode
== OPCODE_BRA
) {
807 inst
->BranchTarget
= _slang_label_get_location(n
->Label
);
808 if (inst
->BranchTarget
< 0) {
809 _slang_label_add_reference(n
->Label
,
810 emitInfo
->prog
->NumInstructions
- 1);
818 static struct prog_instruction
*
819 emit_kill(slang_emit_info
*emitInfo
)
821 struct prog_instruction
*inst
;
822 /* NV-KILL - discard fragment depending on condition code.
823 * Note that ARB-KILL depends on sign of vector operand.
825 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
826 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
831 static struct prog_instruction
*
832 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
834 struct prog_instruction
*inst
;
836 (void) emit(emitInfo
, n
->Children
[1]);
838 if (n
->Opcode
== IR_TEX
) {
839 inst
= new_instruction(emitInfo
, OPCODE_TEX
);
841 else if (n
->Opcode
== IR_TEXB
) {
842 inst
= new_instruction(emitInfo
, OPCODE_TXB
);
845 assert(n
->Opcode
== IR_TEXP
);
846 inst
= new_instruction(emitInfo
, OPCODE_TXP
);
850 if (!alloc_temp_storage(emitInfo
, n
, 4))
853 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
855 /* Child[1] is the coord */
856 assert(n
->Children
[1]->Store
->File
!= PROGRAM_UNDEFINED
);
857 assert(n
->Children
[1]->Store
->Index
>= 0);
858 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
860 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
861 assert(n
->Children
[0]->Store
);
862 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
864 inst
->Sampler
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
865 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
866 inst
->TexSrcUnit
= 27; /* Dummy value; the TexSrcUnit will be computed at
867 * link time, using the sampler uniform's value.
873 static struct prog_instruction
*
874 emit_move(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
876 struct prog_instruction
*inst
;
879 emit(emitInfo
, n
->Children
[0]);
882 assert(n
->Children
[1]);
883 inst
= emit(emitInfo
, n
->Children
[1]);
885 if (!n
->Children
[1]->Store
) {
886 slang_info_log_error(emitInfo
->log
, "invalid assignment");
889 assert(n
->Children
[1]->Store
->Index
>= 0);
891 n
->Store
= n
->Children
[0]->Store
;
893 #if PEEPHOLE_OPTIMIZATIONS
895 _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
) &&
896 (inst
->DstReg
.File
== n
->Children
[1]->Store
->File
) &&
897 (inst
->DstReg
.Index
== n
->Children
[1]->Store
->Index
)) {
898 /* Peephole optimization:
899 * The Right-Hand-Side has its results in a temporary place.
900 * Modify the RHS (and the prev instruction) to store its results
901 * in the destination specified by n->Children[0].
902 * Then, this MOVE is a no-op.
904 if (n
->Children
[1]->Opcode
!= IR_SWIZZLE
)
905 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
906 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
907 /* fixup the previous instruction (which stored the RHS result) */
908 assert(n
->Children
[0]->Store
->Index
>= 0);
909 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
915 if (n
->Children
[0]->Store
->Size
> 4) {
916 /* move matrix/struct etc (block of registers) */
917 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
918 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
919 GLint size
= srcStore
.Size
;
920 ASSERT(n
->Children
[0]->Writemask
== WRITEMASK_XYZW
);
921 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
925 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
926 inst
->Comment
= _mesa_strdup("IR_MOVE block");
927 storage_to_dst_reg(&inst
->DstReg
, &dstStore
, n
->Writemask
);
928 storage_to_src_reg(&inst
->SrcReg
[0], &srcStore
);
935 /* single register move */
936 char *srcAnnot
, *dstAnnot
;
937 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
938 assert(n
->Children
[0]->Store
->Index
>= 0);
939 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
, n
->Writemask
);
940 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[1]->Store
);
941 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
942 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
943 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
944 srcAnnot
, NULL
, NULL
);
946 free_temp_storage(emitInfo
->vt
, n
->Children
[1]);
952 static struct prog_instruction
*
953 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
955 struct prog_instruction
*inst
;
960 inst
= emit(emitInfo
, n
->Children
[0]);
962 if (emitInfo
->EmitCondCodes
) {
963 /* Conditional expression (in if/while/for stmts).
964 * Need to update condition code register.
965 * Next instruction is typically an IR_IF.
968 n
->Children
[0]->Store
&&
969 inst
->DstReg
.File
== n
->Children
[0]->Store
->File
&&
970 inst
->DstReg
.Index
== n
->Children
[0]->Store
->Index
) {
971 /* The previous instruction wrote to the register who's value
972 * we're testing. Just update that instruction so that the
973 * condition codes are updated.
975 inst
->CondUpdate
= GL_TRUE
;
976 n
->Store
= n
->Children
[0]->Store
;
980 /* This'll happen for things like "if (i) ..." where no code
981 * is normally generated for the expression "i".
982 * Generate a move instruction just to set condition codes.
983 * Note: must use full 4-component vector since all four
984 * condition codes must be set identically.
986 if (!alloc_temp_storage(emitInfo
, n
, 4))
988 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
989 inst
->CondUpdate
= GL_TRUE
;
990 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
991 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
992 _slang_free_temp(emitInfo
->vt
, n
->Store
);
993 inst
->Comment
= _mesa_strdup("COND expr");
994 return inst
; /* XXX or null? */
999 n
->Store
= n
->Children
[0]->Store
;
1008 static struct prog_instruction
*
1009 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1012 slang_ir_storage st
;
1013 struct prog_instruction
*inst
;
1015 /* need zero constant */
1016 st
.File
= PROGRAM_CONSTANT
;
1018 st
.Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
, &zero
,
1022 (void) emit(emitInfo
, n
->Children
[0]);
1023 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
1026 if (!alloc_temp_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1029 inst
= new_instruction(emitInfo
, OPCODE_SEQ
);
1030 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1031 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1032 storage_to_src_reg(&inst
->SrcReg
[1], &st
);
1034 free_temp_storage(emitInfo
->vt
, n
->Children
[0]);
1036 inst
->Comment
= _mesa_strdup("NOT");
1041 static struct prog_instruction
*
1042 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1044 struct gl_program
*prog
= emitInfo
->prog
;
1045 struct prog_instruction
*ifInst
;
1046 GLuint ifInstLoc
, elseInstLoc
= 0;
1048 emit(emitInfo
, n
->Children
[0]); /* the condition */
1051 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
1054 ifInstLoc
= prog
->NumInstructions
;
1055 if (emitInfo
->EmitHighLevelInstructions
) {
1056 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
1057 if (emitInfo
->EmitCondCodes
) {
1058 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
1062 storage_to_src_reg(&ifInst
->SrcReg
[0], n
->Children
[0]->Store
);
1066 /* conditional jump to else, or endif */
1067 ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1068 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
1069 ifInst
->Comment
= _mesa_strdup("if zero");
1071 if (emitInfo
->EmitCondCodes
) {
1072 /* which condition code to use: */
1073 ifInst
->DstReg
.CondSwizzle
= n
->Children
[0]->Store
->Swizzle
;
1077 emit(emitInfo
, n
->Children
[1]);
1079 if (n
->Children
[2]) {
1080 /* have else body */
1081 elseInstLoc
= prog
->NumInstructions
;
1082 if (emitInfo
->EmitHighLevelInstructions
) {
1083 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
1086 /* jump to endif instruction */
1087 struct prog_instruction
*inst
;
1088 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1089 inst
->Comment
= _mesa_strdup("else");
1090 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1092 ifInst
= prog
->Instructions
+ ifInstLoc
;
1093 ifInst
->BranchTarget
= prog
->NumInstructions
;
1095 emit(emitInfo
, n
->Children
[2]);
1099 ifInst
= prog
->Instructions
+ ifInstLoc
;
1100 ifInst
->BranchTarget
= prog
->NumInstructions
+ 1;
1103 if (emitInfo
->EmitHighLevelInstructions
) {
1104 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1107 if (n
->Children
[2]) {
1108 struct prog_instruction
*elseInst
;
1109 elseInst
= prog
->Instructions
+ elseInstLoc
;
1110 elseInst
->BranchTarget
= prog
->NumInstructions
;
1116 static struct prog_instruction
*
1117 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1119 struct gl_program
*prog
= emitInfo
->prog
;
1120 struct prog_instruction
*beginInst
, *endInst
;
1121 GLuint beginInstLoc
, tailInstLoc
, endInstLoc
;
1124 /* emit OPCODE_BGNLOOP */
1125 beginInstLoc
= prog
->NumInstructions
;
1126 if (emitInfo
->EmitHighLevelInstructions
) {
1127 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1131 emit(emitInfo
, n
->Children
[0]);
1134 tailInstLoc
= prog
->NumInstructions
;
1135 if (n
->Children
[1]) {
1136 if (emitInfo
->EmitComments
)
1137 emit_comment(emitInfo
, "Loop tail code:");
1138 emit(emitInfo
, n
->Children
[1]);
1141 endInstLoc
= prog
->NumInstructions
;
1142 if (emitInfo
->EmitHighLevelInstructions
) {
1143 /* emit OPCODE_ENDLOOP */
1144 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1147 /* emit unconditional BRA-nch */
1148 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1149 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1151 /* ENDLOOP's BranchTarget points to the BGNLOOP inst */
1152 endInst
->BranchTarget
= beginInstLoc
;
1154 if (emitInfo
->EmitHighLevelInstructions
) {
1155 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1156 beginInst
= prog
->Instructions
+ beginInstLoc
;
1157 beginInst
->BranchTarget
= prog
->NumInstructions
- 1;
1160 /* Done emitting loop code. Now walk over the loop's linked list of
1161 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1162 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1164 for (ir
= n
->List
; ir
; ir
= ir
->List
) {
1165 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1166 assert(inst
->BranchTarget
< 0);
1167 if (ir
->Opcode
== IR_BREAK
||
1168 ir
->Opcode
== IR_BREAK_IF_FALSE
||
1169 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1170 assert(inst
->Opcode
== OPCODE_BRK
||
1171 inst
->Opcode
== OPCODE_BRK0
||
1172 inst
->Opcode
== OPCODE_BRK1
||
1173 inst
->Opcode
== OPCODE_BRA
);
1174 /* go to instruction after end of loop */
1175 inst
->BranchTarget
= endInstLoc
+ 1;
1178 assert(ir
->Opcode
== IR_CONT
||
1179 ir
->Opcode
== IR_CONT_IF_FALSE
||
1180 ir
->Opcode
== IR_CONT_IF_TRUE
);
1181 assert(inst
->Opcode
== OPCODE_CONT
||
1182 inst
->Opcode
== OPCODE_CONT0
||
1183 inst
->Opcode
== OPCODE_CONT1
||
1184 inst
->Opcode
== OPCODE_BRA
);
1185 /* go to instruction at tail of loop */
1186 inst
->BranchTarget
= endInstLoc
;
1194 * Unconditional "continue" or "break" statement.
1195 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1197 static struct prog_instruction
*
1198 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1200 gl_inst_opcode opcode
;
1201 struct prog_instruction
*inst
;
1203 if (n
->Opcode
== IR_CONT
) {
1204 /* we need to execute the loop's tail code before doing CONT */
1206 assert(n
->Parent
->Opcode
== IR_LOOP
);
1207 if (n
->Parent
->Children
[1]) {
1208 /* emit tail code */
1209 if (emitInfo
->EmitComments
) {
1210 emit_comment(emitInfo
, "continue - tail code:");
1212 emit(emitInfo
, n
->Parent
->Children
[1]);
1216 /* opcode selection */
1217 if (emitInfo
->EmitHighLevelInstructions
) {
1218 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1221 opcode
= OPCODE_BRA
;
1223 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1224 inst
= new_instruction(emitInfo
, opcode
);
1225 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1231 * Conditional "continue" or "break" statement.
1232 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1234 static struct prog_instruction
*
1235 emit_cont_break_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
1236 GLboolean breakTrue
)
1238 gl_inst_opcode opcode
;
1239 struct prog_instruction
*inst
;
1241 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1242 n
->Opcode
== IR_CONT_IF_FALSE
||
1243 n
->Opcode
== IR_BREAK_IF_TRUE
||
1244 n
->Opcode
== IR_BREAK_IF_FALSE
);
1246 /* evaluate condition expr, setting cond codes */
1247 inst
= emit(emitInfo
, n
->Children
[0]);
1248 if (emitInfo
->EmitCondCodes
) {
1250 inst
->CondUpdate
= GL_TRUE
;
1253 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1255 /* opcode selection */
1256 if (emitInfo
->EmitHighLevelInstructions
) {
1257 if (emitInfo
->EmitCondCodes
) {
1258 if (n
->Opcode
== IR_CONT_IF_TRUE
||
1259 n
->Opcode
== IR_CONT_IF_FALSE
)
1260 opcode
= OPCODE_CONT
;
1262 opcode
= OPCODE_BRK
;
1265 if (n
->Opcode
== IR_CONT_IF_TRUE
)
1266 opcode
= OPCODE_CONT1
;
1267 else if (n
->Opcode
== IR_CONT_IF_FALSE
)
1268 opcode
= OPCODE_CONT0
;
1269 else if (n
->Opcode
== IR_BREAK_IF_TRUE
)
1270 opcode
= OPCODE_BRK1
;
1271 else if (n
->Opcode
== IR_BREAK_IF_FALSE
)
1272 opcode
= OPCODE_BRK0
;
1276 opcode
= OPCODE_BRA
;
1279 inst
= new_instruction(emitInfo
, opcode
);
1280 if (emitInfo
->EmitCondCodes
) {
1281 inst
->DstReg
.CondMask
= breakTrue
? COND_NE
: COND_EQ
;
1284 /* BRK0, BRK1, CONT0, CONT1 uses SrcReg[0] as the condition */
1285 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1293 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1294 * Ex: fix_swizzle("zyNN") -> "zyyy"
1297 fix_swizzle(GLuint swizzle
)
1300 for (i
= 0; i
< 4; i
++) {
1301 swz
[i
] = GET_SWZ(swizzle
, i
);
1302 if (swz
[i
] == SWIZZLE_NIL
) {
1303 swz
[i
] = swz
[i
- 1];
1306 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1311 * Return the number of components actually named by the swizzle.
1312 * Recall that swizzles may have undefined/don't-care values.
1315 swizzle_size(GLuint swizzle
)
1318 for (i
= 0; i
< 4; i
++) {
1319 GLuint swz
= GET_SWZ(swizzle
, i
);
1320 size
+= (swz
>= 0 && swz
<= 3);
1326 static struct prog_instruction
*
1327 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1330 struct prog_instruction
*inst
;
1332 inst
= emit(emitInfo
, n
->Children
[0]);
1336 GLuint s
= n
->Children
[0]->Store
->Swizzle
;
1337 assert(GET_SWZ(s
, 0) != SWIZZLE_NIL
);
1338 assert(GET_SWZ(s
, 1) != SWIZZLE_NIL
);
1339 assert(GET_SWZ(s
, 2) != SWIZZLE_NIL
);
1340 assert(GET_SWZ(s
, 3) != SWIZZLE_NIL
);
1343 /* For debug: n->Var = n->Children[0]->Var; */
1345 /* "pull-up" the child's storage info, applying our swizzle info */
1346 n
->Store
->File
= n
->Children
[0]->Store
->File
;
1347 n
->Store
->Index
= n
->Children
[0]->Store
->Index
;
1348 n
->Store
->Size
= swizzle_size(n
->Store
->Swizzle
);
1350 printf("Emit Swizzle %s reg %d chSize %d mySize %d\n",
1351 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, 0),
1352 n
->Store
->Index
, n
->Children
[0]->Store
->Size
,
1356 /* apply this swizzle to child's swizzle to get composed swizzle */
1357 swizzle
= fix_swizzle(n
->Store
->Swizzle
); /* remove the don't care terms */
1358 n
->Store
->Swizzle
= swizzle_swizzle(n
->Children
[0]->Store
->Swizzle
,
1366 * Dereference array element. Just resolve storage for the array
1367 * element represented by this node.
1369 static struct prog_instruction
*
1370 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1373 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1374 assert(n
->Store
->Size
> 0);
1376 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1377 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1381 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1382 /* Constant index */
1383 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1384 const GLint index
= (GLint
) n
->Children
[1]->Value
[0];
1385 n
->Store
->Index
= arrayAddr
+ index
;
1388 /* Variable index - PROBLEM */
1389 const GLint arrayAddr
= n
->Children
[0]->Store
->Index
;
1390 const GLint index
= 0;
1391 _mesa_problem(NULL
, "variable array indexes not supported yet!");
1392 n
->Store
->Index
= arrayAddr
+ index
;
1394 return NULL
; /* no instruction */
1399 * Resolve storage for accessing a structure field.
1401 static struct prog_instruction
*
1402 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1404 if (n
->Store
->File
== PROGRAM_STATE_VAR
) {
1405 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1408 GLint offset
= n
->FieldOffset
/ 4;
1409 assert(n
->Children
[0]->Store
->Index
>= 0);
1410 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ offset
;
1411 if (n
->Store
->Size
== 1) {
1412 GLint swz
= n
->FieldOffset
% 4;
1413 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
1416 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
1419 return NULL
; /* no instruction */
1423 static struct prog_instruction
*
1424 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1426 struct prog_instruction
*inst
;
1430 switch (n
->Opcode
) {
1432 /* sequence of two sub-trees */
1433 assert(n
->Children
[0]);
1434 assert(n
->Children
[1]);
1435 emit(emitInfo
, n
->Children
[0]);
1436 inst
= emit(emitInfo
, n
->Children
[1]);
1440 n
->Store
= n
->Children
[1]->Store
;
1444 /* new variable scope */
1445 _slang_push_var_table(emitInfo
->vt
);
1446 inst
= emit(emitInfo
, n
->Children
[0]);
1447 _slang_pop_var_table(emitInfo
->vt
);
1451 /* Variable declaration - allocate a register for it */
1453 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1454 assert(n
->Store
->Size
> 0);
1455 /*assert(n->Store->Index < 0);*/
1456 if (!n
->Var
|| n
->Var
->isTemp
) {
1457 /* a nameless/temporary variable, will be freed after first use */
1459 if (n
->Store
->Index
< 0 && !_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
1460 slang_info_log_error(emitInfo
->log
,
1461 "Ran out of registers, too many temporaries");
1466 /* a regular variable */
1467 _slang_add_variable(emitInfo
->vt
, n
->Var
);
1468 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
1469 slang_info_log_error(emitInfo
->log
,
1470 "Ran out of registers, too many variables");
1474 printf("IR_VAR_DECL %s %d store %p\n",
1475 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1477 assert(n
->Var
->aux
== n
->Store
);
1479 if (emitInfo
->EmitComments
) {
1480 /* emit NOP with comment describing the variable's storage location */
1482 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
1484 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
1485 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
1487 inst
= emit_comment(emitInfo
, s
);
1493 /* Reference to a variable
1494 * Storage should have already been resolved/allocated.
1497 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
1499 if (n
->Store
->File
== PROGRAM_STATE_VAR
&&
1500 n
->Store
->Index
< 0) {
1501 n
->Store
->Index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
);
1504 if (n
->Store
->Index
< 0) {
1505 printf("#### VAR %s not allocated!\n", (char*)n
->Var
->a_name
);
1507 assert(n
->Store
->Index
>= 0);
1508 assert(n
->Store
->Size
> 0);
1512 return emit_array_element(emitInfo
, n
);
1514 return emit_struct_field(emitInfo
, n
);
1516 return emit_swizzle(emitInfo
, n
);
1520 emit(emitInfo
, n
->Children
[0]);
1521 inst
= new_instruction(emitInfo
, OPCODE_MOV
);
1523 if (!alloc_temp_storage(emitInfo
, n
, 1))
1526 storage_to_dst_reg(&inst
->DstReg
, n
->Store
, n
->Writemask
);
1527 storage_to_src_reg(&inst
->SrcReg
[0], n
->Children
[0]->Store
);
1528 if (emitInfo
->EmitComments
)
1529 inst
->Comment
= _mesa_strdup("int to float");
1532 /* Simple arithmetic */
1566 /* trinary operators */
1568 return emit_arith(emitInfo
, n
);
1572 return emit_compare(emitInfo
, n
);
1575 return emit_clamp(emitInfo
, n
);
1579 return emit_tex(emitInfo
, n
);
1581 return emit_negation(emitInfo
, n
);
1583 /* find storage location for this float constant */
1584 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
1587 &n
->Store
->Swizzle
);
1588 if (n
->Store
->Index
< 0) {
1589 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
1595 return emit_move(emitInfo
, n
);
1598 return emit_cond(emitInfo
, n
);
1601 return emit_not(emitInfo
, n
);
1604 return emit_label(emitInfo
, n
);
1607 return emit_kill(emitInfo
);
1610 /* new variable scope for subroutines/function calls*/
1611 _slang_push_var_table(emitInfo
->vt
);
1612 inst
= emit_func(emitInfo
, n
);
1613 _slang_pop_var_table(emitInfo
->vt
);
1617 return emit_if(emitInfo
, n
);
1620 return emit_loop(emitInfo
, n
);
1621 case IR_BREAK_IF_FALSE
:
1622 case IR_CONT_IF_FALSE
:
1623 return emit_cont_break_if(emitInfo
, n
, GL_FALSE
);
1624 case IR_BREAK_IF_TRUE
:
1625 case IR_CONT_IF_TRUE
:
1626 return emit_cont_break_if(emitInfo
, n
, GL_TRUE
);
1630 return emit_cont_break(emitInfo
, n
);
1633 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
1635 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
1637 return emit_return(emitInfo
, n
);
1643 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
1650 * After code generation, any subroutines will be in separate program
1651 * objects. This function appends all the subroutines onto the main
1652 * program and resolves the linking of all the branch/call instructions.
1653 * XXX this logic should really be part of the linking process...
1656 _slang_resolve_subroutines(slang_emit_info
*emitInfo
)
1658 GET_CURRENT_CONTEXT(ctx
);
1659 struct gl_program
*mainP
= emitInfo
->prog
;
1660 GLuint
*subroutineLoc
, i
, total
;
1663 = (GLuint
*) _mesa_malloc(emitInfo
->NumSubroutines
* sizeof(GLuint
));
1665 /* total number of instructions */
1666 total
= mainP
->NumInstructions
;
1667 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1668 subroutineLoc
[i
] = total
;
1669 total
+= emitInfo
->Subroutines
[i
]->NumInstructions
;
1672 /* adjust BrancTargets within the functions */
1673 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1674 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1676 for (j
= 0; j
< sub
->NumInstructions
; j
++) {
1677 struct prog_instruction
*inst
= sub
->Instructions
+ j
;
1678 if (inst
->Opcode
!= OPCODE_CAL
&& inst
->BranchTarget
>= 0) {
1679 inst
->BranchTarget
+= subroutineLoc
[i
];
1684 /* append subroutines' instructions after main's instructions */
1685 mainP
->Instructions
= _mesa_realloc_instructions(mainP
->Instructions
,
1686 mainP
->NumInstructions
,
1688 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
1689 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
1690 _mesa_copy_instructions(mainP
->Instructions
+ subroutineLoc
[i
],
1692 sub
->NumInstructions
);
1693 /* delete subroutine code */
1694 sub
->Parameters
= NULL
; /* prevent double-free */
1695 _mesa_delete_program(ctx
, sub
);
1697 mainP
->NumInstructions
= total
;
1699 /* Examine CAL instructions.
1700 * At this point, the BranchTarget field of the CAL instructions is
1701 * the number/id of the subroutine to call (an index into the
1702 * emitInfo->Subroutines list).
1703 * Translate that into an actual instruction location now.
1705 for (i
= 0; i
< mainP
->NumInstructions
; i
++) {
1706 struct prog_instruction
*inst
= mainP
->Instructions
+ i
;
1707 if (inst
->Opcode
== OPCODE_CAL
) {
1708 const GLuint f
= inst
->BranchTarget
;
1709 inst
->BranchTarget
= subroutineLoc
[f
];
1713 _mesa_free(subroutineLoc
);
1720 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
1721 struct gl_program
*prog
, GLboolean withEnd
,
1722 slang_info_log
*log
)
1724 GET_CURRENT_CONTEXT(ctx
);
1726 slang_emit_info emitInfo
;
1730 emitInfo
.prog
= prog
;
1731 emitInfo
.Subroutines
= NULL
;
1732 emitInfo
.NumSubroutines
= 0;
1734 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
1735 emitInfo
.EmitCondCodes
= ctx
->Shader
.EmitCondCodes
;
1736 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
1737 emitInfo
.EmitBeginEndSub
= 0; /* XXX for compiler debug only */
1739 (void) emit(&emitInfo
, n
);
1741 /* finish up by adding the END opcode to program */
1743 struct prog_instruction
*inst
;
1744 inst
= new_instruction(&emitInfo
, OPCODE_END
);
1747 _slang_resolve_subroutines(&emitInfo
);
1752 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
1753 _mesa_print_program(prog
);
1754 _mesa_print_program_parameters(ctx
,prog
);