2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. 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
39 #include "main/imports.h"
40 #include "main/context.h"
41 #include "main/macros.h"
42 #include "shader/program.h"
43 #include "shader/prog_instruction.h"
44 #include "shader/prog_parameter.h"
45 #include "shader/prog_print.h"
46 #include "slang_builtin.h"
47 #include "slang_emit.h"
48 #include "slang_mem.h"
51 #define PEEPHOLE_OPTIMIZATIONS 1
59 struct gl_program
*prog
;
60 struct gl_program
**Subroutines
;
61 GLuint NumSubroutines
;
63 GLuint MaxInstructions
; /**< size of prog->Instructions[] buffer */
65 /* code-gen options */
66 GLboolean EmitHighLevelInstructions
;
67 GLboolean EmitCondCodes
;
68 GLboolean EmitComments
;
69 GLboolean EmitBeginEndSub
; /* XXX TEMPORARY */
74 static struct gl_program
*
75 new_subroutine(slang_emit_info
*emitInfo
, GLuint
*id
)
77 GET_CURRENT_CONTEXT(ctx
);
78 const GLuint n
= emitInfo
->NumSubroutines
;
80 emitInfo
->Subroutines
= (struct gl_program
**)
81 _mesa_realloc(emitInfo
->Subroutines
,
82 n
* sizeof(struct gl_program
),
83 (n
+ 1) * sizeof(struct gl_program
));
84 emitInfo
->Subroutines
[n
] = ctx
->Driver
.NewProgram(ctx
, emitInfo
->prog
->Target
, 0);
85 emitInfo
->Subroutines
[n
]->Parameters
= emitInfo
->prog
->Parameters
;
86 emitInfo
->NumSubroutines
++;
88 return emitInfo
->Subroutines
[n
];
93 * Convert a writemask to a swizzle. Used for testing cond codes because
94 * we only want to test the cond code component(s) that was set by the
95 * previous instruction.
98 writemask_to_swizzle(GLuint writemask
)
100 if (writemask
== WRITEMASK_X
)
102 if (writemask
== WRITEMASK_Y
)
104 if (writemask
== WRITEMASK_Z
)
106 if (writemask
== WRITEMASK_W
)
108 return SWIZZLE_XYZW
; /* shouldn't be hit */
113 * Convert a swizzle mask to a writemask.
114 * Note that the slang_ir_storage->Swizzle field can represent either a
115 * swizzle mask or a writemask, depending on how it's used. For example,
116 * when we parse "direction.yz" alone, we don't know whether .yz is a
117 * writemask or a swizzle. In this case, we encode ".yz" in store->Swizzle
118 * as a swizzle mask (.yz?? actually). Later, if direction.yz is used as
119 * an R-value, we use store->Swizzle as-is. Otherwise, if direction.yz is
120 * used as an L-value, we convert it to a writemask.
123 swizzle_to_writemask(GLuint swizzle
)
125 GLuint i
, writemask
= 0x0;
126 for (i
= 0; i
< 4; i
++) {
127 GLuint swz
= GET_SWZ(swizzle
, i
);
128 if (swz
<= SWIZZLE_W
) {
129 writemask
|= (1 << swz
);
137 * Swizzle a swizzle (function composition).
138 * That is, return swz2(swz1), or said another way: swz1.szw2
139 * Example: swizzle_swizzle(".zwxx", ".xxyw") yields ".zzwx"
142 _slang_swizzle_swizzle(GLuint swz1
, GLuint swz2
)
145 for (i
= 0; i
< 4; i
++) {
146 GLuint c
= GET_SWZ(swz2
, i
);
148 s
[i
] = GET_SWZ(swz1
, c
);
152 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
158 * Return the default swizzle mask for accessing a variable of the
159 * given size (in floats). If size = 1, comp is used to identify
160 * which component [0..3] of the register holds the variable.
163 _slang_var_swizzle(GLint size
, GLint comp
)
167 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
169 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
171 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
180 * Allocate storage for the given node (if it hasn't already been allocated).
182 * Typically this is temporary storage for an intermediate result (such as
183 * for a multiply or add, etc).
185 * If n->Store does not exist it will be created and will be of the size
186 * specified by defaultSize.
189 alloc_node_storage(slang_emit_info
*emitInfo
, slang_ir_node
*n
,
194 assert(defaultSize
> 0);
195 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, defaultSize
);
198 /* now allocate actual register(s). I.e. set n->Store->Index >= 0 */
199 if (n
->Store
->Index
< 0) {
200 if (!_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
201 slang_info_log_error(emitInfo
->log
,
202 "Ran out of registers, too many temporaries");
203 _slang_free(n
->Store
);
213 * Free temporary storage, if n->Store is, in fact, temp storage.
217 free_node_storage(slang_var_table
*vt
, slang_ir_node
*n
)
219 if (n
->Store
->File
== PROGRAM_TEMPORARY
&&
220 n
->Store
->Index
>= 0 &&
221 n
->Opcode
!= IR_SWIZZLE
) {
222 if (_slang_is_temp(vt
, n
->Store
)) {
223 _slang_free_temp(vt
, n
->Store
);
224 n
->Store
->Index
= -1;
225 n
->Store
= NULL
; /* XXX this may not be needed */
232 * Helper function to allocate a short-term temporary.
233 * Free it with _slang_free_temp().
236 alloc_local_temp(slang_emit_info
*emitInfo
, slang_ir_storage
*temp
, GLint size
)
240 _mesa_bzero(temp
, sizeof(*temp
));
242 temp
->File
= PROGRAM_TEMPORARY
;
244 return _slang_alloc_temp(emitInfo
->vt
, temp
);
249 * Remove any SWIZZLE_NIL terms from given swizzle mask.
250 * For a swizzle like .z??? generate .zzzz (replicate single component).
251 * Else, for .wx?? generate .wxzw (insert default component for the position).
254 fix_swizzle(GLuint swizzle
)
256 GLuint c0
= GET_SWZ(swizzle
, 0),
257 c1
= GET_SWZ(swizzle
, 1),
258 c2
= GET_SWZ(swizzle
, 2),
259 c3
= GET_SWZ(swizzle
, 3);
260 if (c1
== SWIZZLE_NIL
&& c2
== SWIZZLE_NIL
&& c3
== SWIZZLE_NIL
) {
261 /* smear first component across all positions */
265 /* insert default swizzle components */
266 if (c0
== SWIZZLE_NIL
)
268 if (c1
== SWIZZLE_NIL
)
270 if (c2
== SWIZZLE_NIL
)
272 if (c3
== SWIZZLE_NIL
)
275 return MAKE_SWIZZLE4(c0
, c1
, c2
, c3
);
281 * Convert IR storage to an instruction dst register.
284 storage_to_dst_reg(struct prog_dst_register
*dst
, const slang_ir_storage
*st
)
286 const GLboolean relAddr
= st
->RelAddr
;
287 const GLint size
= st
->Size
;
288 GLint index
= st
->Index
;
289 GLuint swizzle
= st
->Swizzle
;
292 /* if this is storage relative to some parent storage, walk up the tree */
295 assert(st
->Index
>= 0);
297 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
300 assert(st
->File
!= PROGRAM_UNDEFINED
);
301 dst
->File
= st
->File
;
309 if (swizzle
!= SWIZZLE_XYZW
) {
310 dst
->WriteMask
= swizzle_to_writemask(swizzle
);
316 writemask
= WRITEMASK_X
<< GET_SWZ(st
->Swizzle
, 0);
319 writemask
= WRITEMASK_XY
;
322 writemask
= WRITEMASK_XYZ
;
325 writemask
= WRITEMASK_XYZW
;
328 ; /* error would have been caught above */
330 dst
->WriteMask
= writemask
;
333 dst
->RelAddr
= relAddr
;
338 * Convert IR storage to an instruction src register.
341 storage_to_src_reg(struct prog_src_register
*src
, const slang_ir_storage
*st
)
343 const GLboolean relAddr
= st
->RelAddr
;
344 GLint index
= st
->Index
;
345 GLuint swizzle
= st
->Swizzle
;
347 /* if this is storage relative to some parent storage, walk up the tree */
352 /* an error should have been reported already */
355 assert(st
->Index
>= 0);
357 swizzle
= _slang_swizzle_swizzle(fix_swizzle(st
->Swizzle
), swizzle
);
360 assert(st
->File
>= 0);
361 #if 1 /* XXX temporary */
362 if (st
->File
== PROGRAM_UNDEFINED
) {
363 slang_ir_storage
*st0
= (slang_ir_storage
*) st
;
364 st0
->File
= PROGRAM_TEMPORARY
;
367 assert(st
->File
< PROGRAM_UNDEFINED
);
368 src
->File
= st
->File
;
373 swizzle
= fix_swizzle(swizzle
);
374 assert(GET_SWZ(swizzle
, 0) <= SWIZZLE_W
);
375 assert(GET_SWZ(swizzle
, 1) <= SWIZZLE_W
);
376 assert(GET_SWZ(swizzle
, 2) <= SWIZZLE_W
);
377 assert(GET_SWZ(swizzle
, 3) <= SWIZZLE_W
);
378 src
->Swizzle
= swizzle
;
380 src
->RelAddr
= relAddr
;
385 * Setup storage pointing to a scalar constant/literal.
388 constant_to_storage(slang_emit_info
*emitInfo
,
390 slang_ir_storage
*store
)
397 reg
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
400 memset(store
, 0, sizeof(*store
));
401 store
->File
= PROGRAM_CONSTANT
;
403 store
->Swizzle
= swizzle
;
408 * Add new instruction at end of given program.
409 * \param prog the program to append instruction onto
410 * \param opcode opcode for the new instruction
411 * \return pointer to the new instruction
413 static struct prog_instruction
*
414 new_instruction(slang_emit_info
*emitInfo
, gl_inst_opcode opcode
)
416 struct gl_program
*prog
= emitInfo
->prog
;
417 struct prog_instruction
*inst
;
420 /* print prev inst */
421 if (prog
->NumInstructions
> 0) {
422 _mesa_print_instruction(prog
->Instructions
+ prog
->NumInstructions
- 1);
425 assert(prog
->NumInstructions
<= emitInfo
->MaxInstructions
);
427 if (prog
->NumInstructions
== emitInfo
->MaxInstructions
) {
428 /* grow the instruction buffer */
429 emitInfo
->MaxInstructions
+= 20;
431 _mesa_realloc_instructions(prog
->Instructions
,
432 prog
->NumInstructions
,
433 emitInfo
->MaxInstructions
);
436 inst
= prog
->Instructions
+ prog
->NumInstructions
;
437 prog
->NumInstructions
++;
438 _mesa_init_instructions(inst
, 1);
439 inst
->Opcode
= opcode
;
440 inst
->BranchTarget
= -1; /* invalid */
442 printf("New inst %d: %p %s\n", prog->NumInstructions-1,(void*)inst,
443 _mesa_opcode_string(inst->Opcode));
449 static struct prog_instruction
*
450 emit_arl_load(slang_emit_info
*emitInfo
,
451 enum register_file file
, GLint index
, GLuint swizzle
)
453 struct prog_instruction
*inst
= new_instruction(emitInfo
, OPCODE_ARL
);
454 inst
->SrcReg
[0].File
= file
;
455 inst
->SrcReg
[0].Index
= index
;
456 inst
->SrcReg
[0].Swizzle
= swizzle
;
457 inst
->DstReg
.File
= PROGRAM_ADDRESS
;
458 inst
->DstReg
.Index
= 0;
459 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
465 * Emit a new instruction with given opcode, operands.
466 * At this point the instruction may have multiple indirect register
467 * loads/stores. We convert those into ARL loads and address-relative
468 * operands. See comments inside.
469 * At some point in the future we could directly emit indirectly addressed
470 * registers in Mesa GPU instructions.
472 static struct prog_instruction
*
473 emit_instruction(slang_emit_info
*emitInfo
,
474 gl_inst_opcode opcode
,
475 const slang_ir_storage
*dst
,
476 const slang_ir_storage
*src0
,
477 const slang_ir_storage
*src1
,
478 const slang_ir_storage
*src2
)
480 struct prog_instruction
*inst
;
481 GLuint numIndirect
= 0;
482 const slang_ir_storage
*src
[3];
483 slang_ir_storage newSrc
[3], newDst
;
487 isTemp
[0] = isTemp
[1] = isTemp
[2] = GL_FALSE
;
493 /* count up how many operands are indirect loads */
494 for (i
= 0; i
< 3; i
++) {
495 if (src
[i
] && src
[i
]->IsIndirect
)
498 if (dst
&& dst
->IsIndirect
)
501 /* Take special steps for indirect register loads.
502 * If we had multiple address registers this would be simpler.
503 * For example, this GLSL code:
504 * x[i] = y[j] + z[k];
505 * would translate into something like:
509 * ADD TEMP[ADDR.x+5], TEMP[ADDR.y+9], TEMP[ADDR.z+4];
510 * But since we currently only have one address register we have to do this:
512 * MOV t1, TEMP[ADDR.x+9];
514 * MOV t2, TEMP[ADDR.x+4];
516 * ADD TEMP[ADDR.x+5], t1, t2;
517 * The code here figures this out...
519 if (numIndirect
> 0) {
520 for (i
= 0; i
< 3; i
++) {
521 if (src
[i
] && src
[i
]->IsIndirect
) {
522 /* load the ARL register with the indirect register */
523 emit_arl_load(emitInfo
,
524 src
[i
]->IndirectFile
,
525 src
[i
]->IndirectIndex
,
526 src
[i
]->IndirectSwizzle
);
528 if (numIndirect
> 1) {
529 /* Need to load src[i] into a temporary register */
530 slang_ir_storage srcRelAddr
;
531 alloc_local_temp(emitInfo
, &newSrc
[i
], src
[i
]->Size
);
534 /* set RelAddr flag on src register */
535 srcRelAddr
= *src
[i
];
536 srcRelAddr
.RelAddr
= GL_TRUE
;
537 srcRelAddr
.IsIndirect
= GL_FALSE
; /* not really needed */
539 /* MOV newSrc, srcRelAddr; */
540 inst
= emit_instruction(emitInfo
,
550 /* just rewrite the src[i] storage to be ARL-relative */
552 newSrc
[i
].RelAddr
= GL_TRUE
;
553 newSrc
[i
].IsIndirect
= GL_FALSE
; /* not really needed */
560 /* Take special steps for indirect dest register write */
561 if (dst
&& dst
->IsIndirect
) {
562 /* load the ARL register with the indirect register */
563 emit_arl_load(emitInfo
,
566 dst
->IndirectSwizzle
);
568 newDst
.RelAddr
= GL_TRUE
;
569 newDst
.IsIndirect
= GL_FALSE
;
573 /* OK, emit the instruction and its dst, src regs */
574 inst
= new_instruction(emitInfo
, opcode
);
579 storage_to_dst_reg(&inst
->DstReg
, dst
);
581 for (i
= 0; i
< 3; i
++) {
583 storage_to_src_reg(&inst
->SrcReg
[i
], src
[i
]);
586 /* Free any temp registers that we allocated above */
587 for (i
= 0; i
< 3; i
++) {
589 _slang_free_temp(emitInfo
->vt
, &newSrc
[i
]);
598 * Put a comment on the given instruction.
601 inst_comment(struct prog_instruction
*inst
, const char *comment
)
604 inst
->Comment
= _mesa_strdup(comment
);
610 * Return pointer to last instruction in program.
612 static struct prog_instruction
*
613 prev_instruction(slang_emit_info
*emitInfo
)
615 struct gl_program
*prog
= emitInfo
->prog
;
616 if (prog
->NumInstructions
== 0)
619 return prog
->Instructions
+ prog
->NumInstructions
- 1;
623 static struct prog_instruction
*
624 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
);
628 * Return an annotation string for given node's storage.
631 storage_annotation(const slang_ir_node
*n
, const struct gl_program
*prog
)
634 const slang_ir_storage
*st
= n
->Store
;
635 static char s
[100] = "";
638 return _mesa_strdup("");
641 case PROGRAM_CONSTANT
:
642 if (st
->Index
>= 0) {
643 const GLfloat
*val
= prog
->Parameters
->ParameterValues
[st
->Index
];
644 if (st
->Swizzle
== SWIZZLE_NOOP
)
645 sprintf(s
, "{%g, %g, %g, %g}", val
[0], val
[1], val
[2], val
[3]);
647 sprintf(s
, "%g", val
[GET_SWZ(st
->Swizzle
, 0)]);
651 case PROGRAM_TEMPORARY
:
653 sprintf(s
, "%s", (char *) n
->Var
->a_name
);
655 sprintf(s
, "t[%d]", st
->Index
);
657 case PROGRAM_STATE_VAR
:
658 case PROGRAM_UNIFORM
:
659 sprintf(s
, "%s", prog
->Parameters
->Parameters
[st
->Index
].Name
);
661 case PROGRAM_VARYING
:
662 sprintf(s
, "%s", prog
->Varying
->Parameters
[st
->Index
].Name
);
665 sprintf(s
, "input[%d]", st
->Index
);
668 sprintf(s
, "output[%d]", st
->Index
);
673 return _mesa_strdup(s
);
681 * Return an annotation string for an instruction.
684 instruction_annotation(gl_inst_opcode opcode
, char *dstAnnot
,
685 char *srcAnnot0
, char *srcAnnot1
, char *srcAnnot2
)
688 const char *operator;
693 len
+= strlen(dstAnnot
);
695 dstAnnot
= _mesa_strdup("");
698 len
+= strlen(srcAnnot0
);
700 srcAnnot0
= _mesa_strdup("");
703 len
+= strlen(srcAnnot1
);
705 srcAnnot1
= _mesa_strdup("");
708 len
+= strlen(srcAnnot2
);
710 srcAnnot2
= _mesa_strdup("");
741 s
= (char *) malloc(len
);
742 sprintf(s
, "%s = %s %s %s %s", dstAnnot
,
743 srcAnnot0
, operator, srcAnnot1
, srcAnnot2
);
744 assert(_mesa_strlen(s
) < len
);
759 * Emit an instruction that's just a comment.
761 static struct prog_instruction
*
762 emit_comment(slang_emit_info
*emitInfo
, const char *comment
)
764 struct prog_instruction
*inst
= new_instruction(emitInfo
, OPCODE_NOP
);
765 inst_comment(inst
, comment
);
771 * Generate code for a simple arithmetic instruction.
772 * Either 1, 2 or 3 operands.
774 static struct prog_instruction
*
775 emit_arith(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
777 const slang_ir_info
*info
= _slang_ir_info(n
->Opcode
);
778 struct prog_instruction
*inst
;
782 assert(info
->InstOpcode
!= OPCODE_NOP
);
784 #if PEEPHOLE_OPTIMIZATIONS
785 /* Look for MAD opportunity */
786 if (info
->NumParams
== 2 &&
787 n
->Opcode
== IR_ADD
&& n
->Children
[0]->Opcode
== IR_MUL
) {
788 /* found pattern IR_ADD(IR_MUL(A, B), C) */
789 emit(emitInfo
, n
->Children
[0]->Children
[0]); /* A */
790 emit(emitInfo
, n
->Children
[0]->Children
[1]); /* B */
791 emit(emitInfo
, n
->Children
[1]); /* C */
792 alloc_node_storage(emitInfo
, n
, -1); /* dest */
794 inst
= emit_instruction(emitInfo
,
797 n
->Children
[0]->Children
[0]->Store
,
798 n
->Children
[0]->Children
[1]->Store
,
799 n
->Children
[1]->Store
);
801 free_node_storage(emitInfo
->vt
, n
->Children
[0]->Children
[0]);
802 free_node_storage(emitInfo
->vt
, n
->Children
[0]->Children
[1]);
803 free_node_storage(emitInfo
->vt
, n
->Children
[1]);
807 if (info
->NumParams
== 2 &&
808 n
->Opcode
== IR_ADD
&& n
->Children
[1]->Opcode
== IR_MUL
) {
809 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
810 emit(emitInfo
, n
->Children
[0]); /* A */
811 emit(emitInfo
, n
->Children
[1]->Children
[0]); /* B */
812 emit(emitInfo
, n
->Children
[1]->Children
[1]); /* C */
813 alloc_node_storage(emitInfo
, n
, -1); /* dest */
815 inst
= emit_instruction(emitInfo
,
818 n
->Children
[1]->Children
[0]->Store
,
819 n
->Children
[1]->Children
[1]->Store
,
820 n
->Children
[0]->Store
);
822 free_node_storage(emitInfo
->vt
, n
->Children
[1]->Children
[0]);
823 free_node_storage(emitInfo
->vt
, n
->Children
[1]->Children
[1]);
824 free_node_storage(emitInfo
->vt
, n
->Children
[0]);
829 /* gen code for children, may involve temp allocation */
830 for (i
= 0; i
< info
->NumParams
; i
++) {
831 emit(emitInfo
, n
->Children
[i
]);
832 if (!n
->Children
[i
] || !n
->Children
[i
]->Store
) {
839 alloc_node_storage(emitInfo
, n
, -1);
841 inst
= emit_instruction(emitInfo
,
844 (info
->NumParams
> 0 ? n
->Children
[0]->Store
: NULL
),
845 (info
->NumParams
> 1 ? n
->Children
[1]->Store
: NULL
),
846 (info
->NumParams
> 2 ? n
->Children
[2]->Store
: NULL
)
850 for (i
= 0; i
< info
->NumParams
; i
++)
851 free_node_storage(emitInfo
->vt
, n
->Children
[i
]);
858 * Emit code for == and != operators. These could normally be handled
859 * by emit_arith() except we need to be able to handle structure comparisons.
861 static struct prog_instruction
*
862 emit_compare(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
864 struct prog_instruction
*inst
= NULL
;
867 assert(n
->Opcode
== IR_EQUAL
|| n
->Opcode
== IR_NOTEQUAL
);
869 /* gen code for children */
870 emit(emitInfo
, n
->Children
[0]);
871 emit(emitInfo
, n
->Children
[1]);
873 if (n
->Children
[0]->Store
->Size
!= n
->Children
[1]->Store
->Size
) {
874 slang_info_log_error(emitInfo
->log
, "invalid operands to == or !=");
878 /* final result is 1 bool */
879 if (!alloc_node_storage(emitInfo
, n
, 1))
882 size
= n
->Children
[0]->Store
->Size
;
885 gl_inst_opcode opcode
= n
->Opcode
== IR_EQUAL
? OPCODE_SEQ
: OPCODE_SNE
;
886 inst
= emit_instruction(emitInfo
,
889 n
->Children
[0]->Store
,
890 n
->Children
[1]->Store
,
893 else if (size
<= 4) {
894 /* compare two vectors.
895 * Unfortunately, there's no instruction to compare vectors and
896 * return a scalar result. Do it with some compare and dot product
900 gl_inst_opcode dotOp
;
901 slang_ir_storage tempStore
;
903 if (!alloc_local_temp(emitInfo
, &tempStore
, 4)) {
910 swizzle
= SWIZZLE_XYZW
;
912 else if (size
== 3) {
914 swizzle
= SWIZZLE_XYZW
;
919 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
922 /* Compute inequality (temp = (A != B)) */
923 inst
= emit_instruction(emitInfo
,
926 n
->Children
[0]->Store
,
927 n
->Children
[1]->Store
,
929 inst_comment(inst
, "Compare values");
931 /* Compute val = DOT(temp, temp) (reduction) */
932 inst
= emit_instruction(emitInfo
,
938 inst
->SrcReg
[0].Swizzle
= inst
->SrcReg
[1].Swizzle
= swizzle
; /*override*/
939 inst_comment(inst
, "Reduce vec to bool");
941 _slang_free_temp(emitInfo
->vt
, &tempStore
); /* free temp */
943 if (n
->Opcode
== IR_EQUAL
) {
944 /* compute val = !val.x with SEQ val, val, 0; */
945 slang_ir_storage zero
;
946 constant_to_storage(emitInfo
, 0.0, &zero
);
947 inst
= emit_instruction(emitInfo
,
953 inst_comment(inst
, "Invert true/false");
957 /* size > 4, struct or array compare.
958 * XXX this won't work reliably for structs with padding!!
960 GLint i
, num
= (n
->Children
[0]->Store
->Size
+ 3) / 4;
961 slang_ir_storage accTemp
, sneTemp
;
963 if (!alloc_local_temp(emitInfo
, &accTemp
, 4))
966 if (!alloc_local_temp(emitInfo
, &sneTemp
, 4))
969 for (i
= 0; i
< num
; i
++) {
970 slang_ir_storage srcStore0
= *n
->Children
[0]->Store
;
971 slang_ir_storage srcStore1
= *n
->Children
[1]->Store
;
972 srcStore0
.Index
+= i
;
973 srcStore1
.Index
+= i
;
976 /* SNE accTemp, left[i], right[i] */
977 inst
= emit_instruction(emitInfo
, OPCODE_SNE
,
982 inst_comment(inst
, "Begin struct/array comparison");
985 /* SNE sneTemp, left[i], right[i] */
986 inst
= emit_instruction(emitInfo
, OPCODE_SNE
,
991 /* ADD accTemp, accTemp, sneTemp; # like logical-OR */
992 inst
= emit_instruction(emitInfo
, OPCODE_ADD
,
1000 /* compute accTemp.x || accTemp.y || accTemp.z || accTemp.w with DOT4 */
1001 inst
= emit_instruction(emitInfo
, OPCODE_DP4
,
1006 inst_comment(inst
, "End struct/array comparison");
1008 if (n
->Opcode
== IR_EQUAL
) {
1009 /* compute tmp.x = !tmp.x via tmp.x = (tmp.x == 0) */
1010 slang_ir_storage zero
;
1011 constant_to_storage(emitInfo
, 0.0, &zero
);
1012 inst
= emit_instruction(emitInfo
, OPCODE_SEQ
,
1013 n
->Store
, /* dest */
1017 inst_comment(inst
, "Invert true/false");
1020 _slang_free_temp(emitInfo
->vt
, &accTemp
);
1021 _slang_free_temp(emitInfo
->vt
, &sneTemp
);
1025 free_node_storage(emitInfo
->vt
, n
->Children
[0]);
1026 free_node_storage(emitInfo
->vt
, n
->Children
[1]);
1034 * Generate code for an IR_CLAMP instruction.
1036 static struct prog_instruction
*
1037 emit_clamp(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1039 struct prog_instruction
*inst
;
1040 slang_ir_node tmpNode
;
1042 assert(n
->Opcode
== IR_CLAMP
);
1048 inst
= emit(emitInfo
, n
->Children
[0]);
1050 /* If lower limit == 0.0 and upper limit == 1.0,
1051 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
1053 * emit OPCODE_MIN, OPCODE_MAX sequence.
1056 /* XXX this isn't quite finished yet */
1057 if (n
->Children
[1]->Opcode
== IR_FLOAT
&&
1058 n
->Children
[1]->Value
[0] == 0.0 &&
1059 n
->Children
[1]->Value
[1] == 0.0 &&
1060 n
->Children
[1]->Value
[2] == 0.0 &&
1061 n
->Children
[1]->Value
[3] == 0.0 &&
1062 n
->Children
[2]->Opcode
== IR_FLOAT
&&
1063 n
->Children
[2]->Value
[0] == 1.0 &&
1064 n
->Children
[2]->Value
[1] == 1.0 &&
1065 n
->Children
[2]->Value
[2] == 1.0 &&
1066 n
->Children
[2]->Value
[3] == 1.0) {
1068 inst
= prev_instruction(prog
);
1070 if (inst
&& inst
->Opcode
!= OPCODE_NOP
) {
1071 /* and prev instruction's DstReg matches n->Children[0]->Store */
1072 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
1073 n
->Store
= n
->Children
[0]->Store
;
1079 if (!alloc_node_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1082 emit(emitInfo
, n
->Children
[1]);
1083 emit(emitInfo
, n
->Children
[2]);
1085 /* Some GPUs don't allow reading from output registers. So if the
1086 * dest for this clamp() is an output reg, we can't use that reg for
1087 * the intermediate result. Use a temp register instead.
1089 _mesa_bzero(&tmpNode
, sizeof(tmpNode
));
1090 alloc_node_storage(emitInfo
, &tmpNode
, n
->Store
->Size
);
1092 /* tmp = max(ch[0], ch[1]) */
1093 inst
= emit_instruction(emitInfo
, OPCODE_MAX
,
1094 tmpNode
.Store
, /* dest */
1095 n
->Children
[0]->Store
,
1096 n
->Children
[1]->Store
,
1099 /* n->dest = min(tmp, ch[2]) */
1100 inst
= emit_instruction(emitInfo
, OPCODE_MIN
,
1101 n
->Store
, /* dest */
1103 n
->Children
[2]->Store
,
1106 free_node_storage(emitInfo
->vt
, &tmpNode
);
1112 static struct prog_instruction
*
1113 emit_negation(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1115 /* Implement as MOV dst, -src; */
1116 /* XXX we could look at the previous instruction and in some circumstances
1117 * modify it to accomplish the negation.
1119 struct prog_instruction
*inst
;
1121 emit(emitInfo
, n
->Children
[0]);
1123 if (!alloc_node_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1126 inst
= emit_instruction(emitInfo
,
1128 n
->Store
, /* dest */
1129 n
->Children
[0]->Store
,
1132 inst
->SrcReg
[0].NegateBase
= NEGATE_XYZW
;
1137 static struct prog_instruction
*
1138 emit_label(slang_emit_info
*emitInfo
, const slang_ir_node
*n
)
1142 /* XXX this fails in loop tail code - investigate someday */
1143 assert(_slang_label_get_location(n
->Label
) < 0);
1144 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
1147 if (_slang_label_get_location(n
->Label
) < 0)
1148 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
1156 * Emit code for a function call.
1157 * Note that for each time a function is called, we emit the function's
1158 * body code again because the set of available registers may be different.
1160 static struct prog_instruction
*
1161 emit_fcall(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1163 struct gl_program
*progSave
;
1164 struct prog_instruction
*inst
;
1165 GLuint subroutineId
;
1168 assert(n
->Opcode
== IR_CALL
);
1171 /* save/push cur program */
1172 maxInstSave
= emitInfo
->MaxInstructions
;
1173 progSave
= emitInfo
->prog
;
1175 emitInfo
->prog
= new_subroutine(emitInfo
, &subroutineId
);
1176 emitInfo
->MaxInstructions
= emitInfo
->prog
->NumInstructions
;
1178 _slang_label_set_location(n
->Label
, emitInfo
->prog
->NumInstructions
,
1181 if (emitInfo
->EmitBeginEndSub
) {
1182 /* BGNSUB isn't a real instruction.
1183 * We require a label (i.e. "foobar:") though, if we're going to
1184 * print the program in the NV format. The BNGSUB instruction is
1185 * really just a NOP to attach the label to.
1187 inst
= new_instruction(emitInfo
, OPCODE_BGNSUB
);
1188 inst_comment(inst
, n
->Label
->Name
);
1191 /* body of function: */
1192 emit(emitInfo
, n
->Children
[0]);
1193 n
->Store
= n
->Children
[0]->Store
;
1195 /* add RET instruction now, if needed */
1196 inst
= prev_instruction(emitInfo
);
1197 if (inst
&& inst
->Opcode
!= OPCODE_RET
) {
1198 inst
= new_instruction(emitInfo
, OPCODE_RET
);
1201 if (emitInfo
->EmitBeginEndSub
) {
1202 inst
= new_instruction(emitInfo
, OPCODE_ENDSUB
);
1203 inst_comment(inst
, n
->Label
->Name
);
1206 /* pop/restore cur program */
1207 emitInfo
->prog
= progSave
;
1208 emitInfo
->MaxInstructions
= maxInstSave
;
1210 /* emit the function call */
1211 inst
= new_instruction(emitInfo
, OPCODE_CAL
);
1212 /* The branch target is just the subroutine number (changed later) */
1213 inst
->BranchTarget
= subroutineId
;
1214 inst_comment(inst
, n
->Label
->Name
);
1215 assert(inst
->BranchTarget
>= 0);
1222 * Emit code for a 'return' statement.
1224 static struct prog_instruction
*
1225 emit_return(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1227 struct prog_instruction
*inst
;
1229 assert(n
->Opcode
== IR_RETURN
);
1231 inst
= new_instruction(emitInfo
, OPCODE_RET
);
1232 inst
->DstReg
.CondMask
= COND_TR
; /* always return */
1237 static struct prog_instruction
*
1238 emit_kill(slang_emit_info
*emitInfo
)
1240 struct gl_fragment_program
*fp
;
1241 struct prog_instruction
*inst
;
1242 /* NV-KILL - discard fragment depending on condition code.
1243 * Note that ARB-KILL depends on sign of vector operand.
1245 inst
= new_instruction(emitInfo
, OPCODE_KIL_NV
);
1246 inst
->DstReg
.CondMask
= COND_TR
; /* always kill */
1248 assert(emitInfo
->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
1249 fp
= (struct gl_fragment_program
*) emitInfo
->prog
;
1250 fp
->UsesKill
= GL_TRUE
;
1256 static struct prog_instruction
*
1257 emit_tex(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1259 struct prog_instruction
*inst
;
1260 gl_inst_opcode opcode
;
1262 if (n
->Opcode
== IR_TEX
) {
1263 opcode
= OPCODE_TEX
;
1265 else if (n
->Opcode
== IR_TEXB
) {
1266 opcode
= OPCODE_TXB
;
1269 assert(n
->Opcode
== IR_TEXP
);
1270 opcode
= OPCODE_TXP
;
1273 /* emit code for the texcoord operand */
1274 (void) emit(emitInfo
, n
->Children
[1]);
1276 /* alloc storage for result of texture fetch */
1277 if (!alloc_node_storage(emitInfo
, n
, 4))
1280 /* emit TEX instruction; Child[1] is the texcoord */
1281 inst
= emit_instruction(emitInfo
,
1284 n
->Children
[1]->Store
,
1288 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
1289 assert(n
->Children
[0]->Store
);
1290 assert(n
->Children
[0]->Store
->File
== PROGRAM_SAMPLER
);
1291 /* Store->Index is the sampler index */
1292 assert(n
->Children
[0]->Store
->Index
>= 0);
1293 /* Store->Size is the texture target */
1294 assert(n
->Children
[0]->Store
->Size
>= TEXTURE_1D_INDEX
);
1295 assert(n
->Children
[0]->Store
->Size
<= TEXTURE_RECT_INDEX
);
1297 inst
->TexSrcTarget
= n
->Children
[0]->Store
->Size
;
1298 inst
->TexSrcUnit
= n
->Children
[0]->Store
->Index
; /* i.e. uniform's index */
1300 /* mark the sampler as being used */
1301 _mesa_use_uniform(emitInfo
->prog
->Parameters
,
1302 (char *) n
->Children
[0]->Var
->a_name
);
1311 static struct prog_instruction
*
1312 emit_copy(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1314 struct prog_instruction
*inst
;
1316 assert(n
->Opcode
== IR_COPY
);
1319 emit(emitInfo
, n
->Children
[0]);
1320 if (!n
->Children
[0]->Store
|| n
->Children
[0]->Store
->Index
< 0) {
1321 /* an error should have been already recorded */
1326 assert(n
->Children
[1]);
1327 inst
= emit(emitInfo
, n
->Children
[1]);
1329 if (!n
->Children
[1]->Store
|| n
->Children
[1]->Store
->Index
< 0) {
1330 if (!emitInfo
->log
->text
) {
1331 slang_info_log_error(emitInfo
->log
, "invalid assignment");
1336 assert(n
->Children
[1]->Store
->Index
>= 0);
1338 /*assert(n->Children[0]->Store->Size == n->Children[1]->Store->Size);*/
1340 n
->Store
= n
->Children
[0]->Store
;
1342 if (n
->Store
->File
== PROGRAM_SAMPLER
) {
1343 /* no code generated for sampler assignments,
1344 * just copy the sampler index at compile time.
1346 n
->Store
->Index
= n
->Children
[1]->Store
->Index
;
1350 #if PEEPHOLE_OPTIMIZATIONS
1352 _slang_is_temp(emitInfo
->vt
, n
->Children
[1]->Store
) &&
1353 (inst
->DstReg
.File
== n
->Children
[1]->Store
->File
) &&
1354 (inst
->DstReg
.Index
== n
->Children
[1]->Store
->Index
) &&
1355 !n
->Children
[0]->Store
->IsIndirect
&&
1356 n
->Children
[0]->Store
->Size
<= 4) {
1357 /* Peephole optimization:
1358 * The Right-Hand-Side has its results in a temporary place.
1359 * Modify the RHS (and the prev instruction) to store its results
1360 * in the destination specified by n->Children[0].
1361 * Then, this MOVE is a no-op.
1368 if (n
->Children
[1]->Opcode
!= IR_SWIZZLE
)
1369 _slang_free_temp(emitInfo
->vt
, n
->Children
[1]->Store
);
1370 *n
->Children
[1]->Store
= *n
->Children
[0]->Store
;
1372 /* fixup the previous instruction (which stored the RHS result) */
1373 assert(n
->Children
[0]->Store
->Index
>= 0);
1375 storage_to_dst_reg(&inst
->DstReg
, n
->Children
[0]->Store
);
1381 if (n
->Children
[0]->Store
->Size
> 4) {
1382 /* move matrix/struct etc (block of registers) */
1383 slang_ir_storage dstStore
= *n
->Children
[0]->Store
;
1384 slang_ir_storage srcStore
= *n
->Children
[1]->Store
;
1385 GLint size
= srcStore
.Size
;
1386 ASSERT(n
->Children
[1]->Store
->Swizzle
== SWIZZLE_NOOP
);
1390 inst
= emit_instruction(emitInfo
, OPCODE_MOV
,
1395 inst_comment(inst
, "IR_COPY block");
1402 /* single register move */
1403 char *srcAnnot
, *dstAnnot
;
1404 assert(n
->Children
[0]->Store
->Index
>= 0);
1405 inst
= emit_instruction(emitInfo
, OPCODE_MOV
,
1406 n
->Children
[0]->Store
, /* dest */
1407 n
->Children
[1]->Store
,
1410 dstAnnot
= storage_annotation(n
->Children
[0], emitInfo
->prog
);
1411 srcAnnot
= storage_annotation(n
->Children
[1], emitInfo
->prog
);
1412 inst
->Comment
= instruction_annotation(inst
->Opcode
, dstAnnot
,
1413 srcAnnot
, NULL
, NULL
);
1415 free_node_storage(emitInfo
->vt
, n
->Children
[1]);
1422 * An IR_COND node wraps a boolean expression which is used by an
1423 * IF or WHILE test. This is where we'll set condition codes, if needed.
1425 static struct prog_instruction
*
1426 emit_cond(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1428 struct prog_instruction
*inst
;
1430 assert(n
->Opcode
== IR_COND
);
1432 if (!n
->Children
[0])
1435 /* emit code for the expression */
1436 inst
= emit(emitInfo
, n
->Children
[0]);
1438 if (!n
->Children
[0]->Store
) {
1439 /* error recovery */
1443 assert(n
->Children
[0]->Store
);
1444 /*assert(n->Children[0]->Store->Size == 1);*/
1446 if (emitInfo
->EmitCondCodes
) {
1448 n
->Children
[0]->Store
&&
1449 inst
->DstReg
.File
== n
->Children
[0]->Store
->File
&&
1450 inst
->DstReg
.Index
== n
->Children
[0]->Store
->Index
) {
1451 /* The previous instruction wrote to the register who's value
1452 * we're testing. Just fix that instruction so that the
1453 * condition codes are computed.
1455 inst
->CondUpdate
= GL_TRUE
;
1456 n
->Store
= n
->Children
[0]->Store
;
1460 /* This'll happen for things like "if (i) ..." where no code
1461 * is normally generated for the expression "i".
1462 * Generate a move instruction just to set condition codes.
1464 if (!alloc_node_storage(emitInfo
, n
, 1))
1466 inst
= emit_instruction(emitInfo
, OPCODE_MOV
,
1467 n
->Store
, /* dest */
1468 n
->Children
[0]->Store
,
1471 inst
->CondUpdate
= GL_TRUE
;
1472 inst_comment(inst
, "COND expr");
1473 _slang_free_temp(emitInfo
->vt
, n
->Store
);
1478 /* No-op: the boolean result of the expression is in a regular reg */
1479 n
->Store
= n
->Children
[0]->Store
;
1488 static struct prog_instruction
*
1489 emit_not(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1491 static const struct {
1492 gl_inst_opcode op
, opNot
;
1494 { OPCODE_SLT
, OPCODE_SGE
},
1495 { OPCODE_SLE
, OPCODE_SGT
},
1496 { OPCODE_SGT
, OPCODE_SLE
},
1497 { OPCODE_SGE
, OPCODE_SLT
},
1498 { OPCODE_SEQ
, OPCODE_SNE
},
1499 { OPCODE_SNE
, OPCODE_SEQ
},
1502 struct prog_instruction
*inst
;
1503 slang_ir_storage zero
;
1507 inst
= emit(emitInfo
, n
->Children
[0]);
1509 #if PEEPHOLE_OPTIMIZATIONS
1511 /* if the prev instruction was a comparison instruction, invert it */
1512 for (i
= 0; operators
[i
].op
; i
++) {
1513 if (inst
->Opcode
== operators
[i
].op
) {
1514 inst
->Opcode
= operators
[i
].opNot
;
1515 n
->Store
= n
->Children
[0]->Store
;
1522 /* else, invert using SEQ (v = v == 0) */
1523 if (!alloc_node_storage(emitInfo
, n
, n
->Children
[0]->Store
->Size
))
1526 constant_to_storage(emitInfo
, 0.0, &zero
);
1527 inst
= emit_instruction(emitInfo
,
1530 n
->Children
[0]->Store
,
1533 inst_comment(inst
, "NOT");
1535 free_node_storage(emitInfo
->vt
, n
->Children
[0]);
1541 static struct prog_instruction
*
1542 emit_if(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1544 struct gl_program
*prog
= emitInfo
->prog
;
1545 GLuint ifInstLoc
, elseInstLoc
= 0;
1546 GLuint condWritemask
= 0;
1548 /* emit condition expression code */
1550 struct prog_instruction
*inst
;
1551 inst
= emit(emitInfo
, n
->Children
[0]);
1552 if (emitInfo
->EmitCondCodes
) {
1554 /* error recovery */
1557 condWritemask
= inst
->DstReg
.WriteMask
;
1561 if (!n
->Children
[0]->Store
)
1565 assert(n
->Children
[0]->Store
->Size
== 1); /* a bool! */
1568 ifInstLoc
= prog
->NumInstructions
;
1569 if (emitInfo
->EmitHighLevelInstructions
) {
1570 if (emitInfo
->EmitCondCodes
) {
1571 /* IF condcode THEN ... */
1572 struct prog_instruction
*ifInst
;
1573 ifInst
= new_instruction(emitInfo
, OPCODE_IF
);
1574 ifInst
->DstReg
.CondMask
= COND_NE
; /* if cond is non-zero */
1575 /* only test the cond code (1 of 4) that was updated by the
1576 * previous instruction.
1578 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1581 /* IF src[0] THEN ... */
1582 emit_instruction(emitInfo
, OPCODE_IF
,
1584 n
->Children
[0]->Store
, /* op0 */
1590 /* conditional jump to else, or endif */
1591 struct prog_instruction
*ifInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1592 ifInst
->DstReg
.CondMask
= COND_EQ
; /* BRA if cond is zero */
1593 inst_comment(ifInst
, "if zero");
1594 ifInst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1598 emit(emitInfo
, n
->Children
[1]);
1600 if (n
->Children
[2]) {
1601 /* have else body */
1602 elseInstLoc
= prog
->NumInstructions
;
1603 if (emitInfo
->EmitHighLevelInstructions
) {
1604 (void) new_instruction(emitInfo
, OPCODE_ELSE
);
1607 /* jump to endif instruction */
1608 struct prog_instruction
*inst
;
1609 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1610 inst_comment(inst
, "else");
1611 inst
->DstReg
.CondMask
= COND_TR
; /* always branch */
1613 prog
->Instructions
[ifInstLoc
].BranchTarget
= prog
->NumInstructions
;
1614 emit(emitInfo
, n
->Children
[2]);
1618 prog
->Instructions
[ifInstLoc
].BranchTarget
= prog
->NumInstructions
;
1621 if (emitInfo
->EmitHighLevelInstructions
) {
1622 (void) new_instruction(emitInfo
, OPCODE_ENDIF
);
1625 if (n
->Children
[2]) {
1626 prog
->Instructions
[elseInstLoc
].BranchTarget
= prog
->NumInstructions
;
1632 static struct prog_instruction
*
1633 emit_loop(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1635 struct gl_program
*prog
= emitInfo
->prog
;
1636 struct prog_instruction
*endInst
;
1637 GLuint beginInstLoc
, tailInstLoc
, endInstLoc
;
1640 /* emit OPCODE_BGNLOOP */
1641 beginInstLoc
= prog
->NumInstructions
;
1642 if (emitInfo
->EmitHighLevelInstructions
) {
1643 (void) new_instruction(emitInfo
, OPCODE_BGNLOOP
);
1647 emit(emitInfo
, n
->Children
[0]);
1650 tailInstLoc
= prog
->NumInstructions
;
1651 if (n
->Children
[1]) {
1652 if (emitInfo
->EmitComments
)
1653 emit_comment(emitInfo
, "Loop tail code:");
1654 emit(emitInfo
, n
->Children
[1]);
1657 endInstLoc
= prog
->NumInstructions
;
1658 if (emitInfo
->EmitHighLevelInstructions
) {
1659 /* emit OPCODE_ENDLOOP */
1660 endInst
= new_instruction(emitInfo
, OPCODE_ENDLOOP
);
1663 /* emit unconditional BRA-nch */
1664 endInst
= new_instruction(emitInfo
, OPCODE_BRA
);
1665 endInst
->DstReg
.CondMask
= COND_TR
; /* always true */
1667 /* ENDLOOP's BranchTarget points to the BGNLOOP inst */
1668 endInst
->BranchTarget
= beginInstLoc
;
1670 if (emitInfo
->EmitHighLevelInstructions
) {
1671 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1672 prog
->Instructions
[beginInstLoc
].BranchTarget
= prog
->NumInstructions
-1;
1675 /* Done emitting loop code. Now walk over the loop's linked list of
1676 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1677 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1679 for (ir
= n
->List
; ir
; ir
= ir
->List
) {
1680 struct prog_instruction
*inst
= prog
->Instructions
+ ir
->InstLocation
;
1681 assert(inst
->BranchTarget
< 0);
1682 if (ir
->Opcode
== IR_BREAK
||
1683 ir
->Opcode
== IR_BREAK_IF_TRUE
) {
1684 assert(inst
->Opcode
== OPCODE_BRK
||
1685 inst
->Opcode
== OPCODE_BRA
);
1686 /* go to instruction after end of loop */
1687 inst
->BranchTarget
= endInstLoc
+ 1;
1690 assert(ir
->Opcode
== IR_CONT
||
1691 ir
->Opcode
== IR_CONT_IF_TRUE
);
1692 assert(inst
->Opcode
== OPCODE_CONT
||
1693 inst
->Opcode
== OPCODE_BRA
);
1694 /* go to instruction at tail of loop */
1695 inst
->BranchTarget
= endInstLoc
;
1703 * Unconditional "continue" or "break" statement.
1704 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1706 static struct prog_instruction
*
1707 emit_cont_break(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1709 gl_inst_opcode opcode
;
1710 struct prog_instruction
*inst
;
1712 if (n
->Opcode
== IR_CONT
) {
1713 /* we need to execute the loop's tail code before doing CONT */
1715 assert(n
->Parent
->Opcode
== IR_LOOP
);
1716 if (n
->Parent
->Children
[1]) {
1717 /* emit tail code */
1718 if (emitInfo
->EmitComments
) {
1719 emit_comment(emitInfo
, "continue - tail code:");
1721 emit(emitInfo
, n
->Parent
->Children
[1]);
1725 /* opcode selection */
1726 if (emitInfo
->EmitHighLevelInstructions
) {
1727 opcode
= (n
->Opcode
== IR_CONT
) ? OPCODE_CONT
: OPCODE_BRK
;
1730 opcode
= OPCODE_BRA
;
1732 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1733 inst
= new_instruction(emitInfo
, opcode
);
1734 inst
->DstReg
.CondMask
= COND_TR
; /* always true */
1740 * Conditional "continue" or "break" statement.
1741 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1743 static struct prog_instruction
*
1744 emit_cont_break_if_true(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1746 struct prog_instruction
*inst
;
1748 assert(n
->Opcode
== IR_CONT_IF_TRUE
||
1749 n
->Opcode
== IR_BREAK_IF_TRUE
);
1751 /* evaluate condition expr, setting cond codes */
1752 inst
= emit(emitInfo
, n
->Children
[0]);
1753 if (emitInfo
->EmitCondCodes
) {
1755 inst
->CondUpdate
= GL_TRUE
;
1758 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1760 /* opcode selection */
1761 if (emitInfo
->EmitHighLevelInstructions
) {
1762 const gl_inst_opcode opcode
1763 = (n
->Opcode
== IR_CONT_IF_TRUE
) ? OPCODE_CONT
: OPCODE_BRK
;
1764 if (emitInfo
->EmitCondCodes
) {
1765 /* Get the writemask from the previous instruction which set
1766 * the condcodes. Use that writemask as the CondSwizzle.
1768 const GLuint condWritemask
= inst
->DstReg
.WriteMask
;
1769 inst
= new_instruction(emitInfo
, opcode
);
1770 inst
->DstReg
.CondMask
= COND_NE
;
1771 inst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1780 ifInstLoc
= emitInfo
->prog
->NumInstructions
;
1781 inst
= emit_instruction(emitInfo
, OPCODE_IF
,
1783 n
->Children
[0]->Store
,
1786 n
->InstLocation
= emitInfo
->prog
->NumInstructions
;
1788 inst
= new_instruction(emitInfo
, opcode
);
1789 inst
= new_instruction(emitInfo
, OPCODE_ENDIF
);
1791 emitInfo
->prog
->Instructions
[ifInstLoc
].BranchTarget
1792 = emitInfo
->prog
->NumInstructions
;
1797 const GLuint condWritemask
= inst
->DstReg
.WriteMask
;
1798 assert(emitInfo
->EmitCondCodes
);
1799 inst
= new_instruction(emitInfo
, OPCODE_BRA
);
1800 inst
->DstReg
.CondMask
= COND_NE
;
1801 inst
->DstReg
.CondSwizzle
= writemask_to_swizzle(condWritemask
);
1807 static struct prog_instruction
*
1808 emit_swizzle(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1810 struct prog_instruction
*inst
;
1812 inst
= emit(emitInfo
, n
->Children
[0]);
1815 assert(n
->Store
->Parent
);
1816 /* Apply this node's swizzle to parent's storage */
1817 GLuint swizzle
= n
->Store
->Swizzle
;
1818 _slang_copy_ir_storage(n
->Store
, n
->Store
->Parent
);
1819 n
->Store
->Swizzle
= _slang_swizzle_swizzle(n
->Store
->Swizzle
, swizzle
);
1820 assert(!n
->Store
->Parent
);
1827 * Dereference array element: element == array[index]
1828 * This basically involves emitting code for computing the array index
1829 * and updating the node/element's storage info.
1831 static struct prog_instruction
*
1832 emit_array_element(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1834 slang_ir_storage
*arrayStore
, *indexStore
;
1835 const int elemSize
= n
->Store
->Size
; /* number of floats */
1836 const GLint elemSizeVec
= (elemSize
+ 3) / 4; /* number of vec4 */
1837 struct prog_instruction
*inst
;
1839 assert(n
->Opcode
== IR_ELEMENT
);
1840 assert(elemSize
> 0);
1842 /* special case for built-in state variables, like light state */
1844 slang_ir_storage
*root
= n
->Store
;
1845 assert(!root
->Parent
);
1846 while (root
->Parent
)
1847 root
= root
->Parent
;
1849 if (root
->File
== PROGRAM_STATE_VAR
) {
1852 _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
, &direct
);
1858 n
->Store
->Index
= index
;
1859 return NULL
; /* all done */
1864 /* do codegen for array itself */
1865 emit(emitInfo
, n
->Children
[0]);
1866 arrayStore
= n
->Children
[0]->Store
;
1868 /* The initial array element storage is the array's storage,
1869 * then modified below.
1871 _slang_copy_ir_storage(n
->Store
, arrayStore
);
1874 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
1875 /* Constant array index */
1876 const GLint element
= (GLint
) n
->Children
[1]->Value
[0];
1878 /* this element's storage is the array's storage, plus constant offset */
1879 n
->Store
->Index
+= elemSizeVec
* element
;
1882 /* Variable array index */
1884 /* do codegen for array index expression */
1885 emit(emitInfo
, n
->Children
[1]);
1886 indexStore
= n
->Children
[1]->Store
;
1888 if (indexStore
->IsIndirect
) {
1889 /* need to put the array index into a temporary since we can't
1890 * directly support a[b[i]] constructs.
1894 /*indexStore = tempstore();*/
1899 /* need to multiply array index by array element size */
1900 struct prog_instruction
*inst
;
1901 slang_ir_storage
*indexTemp
;
1902 slang_ir_storage elemSizeStore
;
1904 /* allocate 1 float indexTemp */
1905 indexTemp
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
1906 _slang_alloc_temp(emitInfo
->vt
, indexTemp
);
1908 /* allocate a constant containing the element size */
1909 constant_to_storage(emitInfo
, (float) elemSizeVec
, &elemSizeStore
);
1911 /* multiply array index by element size */
1912 inst
= emit_instruction(emitInfo
,
1914 indexTemp
, /* dest */
1915 indexStore
, /* the index */
1919 indexStore
= indexTemp
;
1922 if (arrayStore
->IsIndirect
) {
1923 /* ex: in a[i][j], a[i] (the arrayStore) is indirect */
1924 /* Need to add indexStore to arrayStore->Indirect store */
1925 slang_ir_storage indirectArray
;
1926 slang_ir_storage
*indexTemp
;
1928 _slang_init_ir_storage(&indirectArray
,
1929 arrayStore
->IndirectFile
,
1930 arrayStore
->IndirectIndex
,
1932 arrayStore
->IndirectSwizzle
);
1934 /* allocate 1 float indexTemp */
1935 indexTemp
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
1936 _slang_alloc_temp(emitInfo
->vt
, indexTemp
);
1938 inst
= emit_instruction(emitInfo
,
1940 indexTemp
, /* dest */
1941 indexStore
, /* the index */
1942 &indirectArray
, /* indirect array base */
1945 indexStore
= indexTemp
;
1948 /* update the array element storage info */
1949 n
->Store
->IsIndirect
= GL_TRUE
;
1950 n
->Store
->IndirectFile
= indexStore
->File
;
1951 n
->Store
->IndirectIndex
= indexStore
->Index
;
1952 n
->Store
->IndirectSwizzle
= indexStore
->Swizzle
;
1955 n
->Store
->Size
= elemSize
;
1956 n
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
1958 return NULL
; /* no instruction */
1963 * Resolve storage for accessing a structure field.
1965 static struct prog_instruction
*
1966 emit_struct_field(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
1968 slang_ir_storage
*root
= n
->Store
;
1969 GLint fieldOffset
, fieldSize
;
1971 assert(n
->Opcode
== IR_FIELD
);
1973 assert(!root
->Parent
);
1974 while (root
->Parent
)
1975 root
= root
->Parent
;
1977 /* If this is the field of a state var, allocate constant/uniform
1978 * storage for it now if we haven't already.
1979 * Note that we allocate storage (uniform/constant slots) for state
1980 * variables here rather than at declaration time so we only allocate
1981 * space for the ones that we actually use!
1983 if (root
->File
== PROGRAM_STATE_VAR
) {
1985 GLint index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
, &direct
);
1987 slang_info_log_error(emitInfo
->log
, "Error parsing state variable");
1991 root
->Index
= index
;
1992 return NULL
; /* all done */
1996 /* do codegen for struct */
1997 emit(emitInfo
, n
->Children
[0]);
1998 assert(n
->Children
[0]->Store
->Index
>= 0);
2001 fieldOffset
= n
->Store
->Index
;
2002 fieldSize
= n
->Store
->Size
;
2004 _slang_copy_ir_storage(n
->Store
, n
->Children
[0]->Store
);
2006 n
->Store
->Index
= n
->Children
[0]->Store
->Index
+ fieldOffset
/ 4;
2007 n
->Store
->Size
= fieldSize
;
2009 switch (fieldSize
) {
2012 GLint swz
= fieldOffset
% 4;
2013 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
2017 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2018 SWIZZLE_NIL
, SWIZZLE_NIL
);
2021 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2022 SWIZZLE_Z
, SWIZZLE_NIL
);
2025 n
->Store
->Swizzle
= SWIZZLE_XYZW
;
2028 assert(n
->Store
->Index
>= 0);
2030 return NULL
; /* no instruction */
2035 * Emit code for a variable declaration.
2036 * This usually doesn't result in any code generation, but just
2037 * memory allocation.
2039 static struct prog_instruction
*
2040 emit_var_decl(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
2043 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
2044 assert(n
->Store
->Size
> 0);
2045 /*assert(n->Store->Index < 0);*/
2047 if (!n
->Var
|| n
->Var
->isTemp
) {
2048 /* a nameless/temporary variable, will be freed after first use */
2050 if (n
->Store
->Index
< 0 && !_slang_alloc_temp(emitInfo
->vt
, n
->Store
)) {
2051 slang_info_log_error(emitInfo
->log
,
2052 "Ran out of registers, too many temporaries");
2057 /* a regular variable */
2058 _slang_add_variable(emitInfo
->vt
, n
->Var
);
2059 if (!_slang_alloc_var(emitInfo
->vt
, n
->Store
)) {
2060 slang_info_log_error(emitInfo
->log
,
2061 "Ran out of registers, too many variables");
2065 printf("IR_VAR_DECL %s %d store %p\n",
2066 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
2068 assert(n
->Var
->store
== n
->Store
);
2070 if (emitInfo
->EmitComments
) {
2071 /* emit NOP with comment describing the variable's storage location */
2073 sprintf(s
, "TEMP[%d]%s = variable %s (size %d)",
2075 _mesa_swizzle_string(n
->Store
->Swizzle
, 0, GL_FALSE
),
2076 (n
->Var
? (char *) n
->Var
->a_name
: "anonymous"),
2078 emit_comment(emitInfo
, s
);
2085 * Emit code for a reference to a variable.
2086 * Actually, no code is generated but we may do some memory allocation.
2087 * In particular, state vars (uniforms) are allocated on an as-needed basis.
2089 static struct prog_instruction
*
2090 emit_var_ref(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
2093 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
);
2095 if (n
->Store
->File
== PROGRAM_STATE_VAR
&& n
->Store
->Index
< 0) {
2097 GLint index
= _slang_alloc_statevar(n
, emitInfo
->prog
->Parameters
, &direct
);
2101 snprintf(s
, sizeof(s
), "Undefined variable '%s'",
2102 (char *) n
->Var
->a_name
);
2103 slang_info_log_error(emitInfo
->log
, s
);
2107 n
->Store
->Index
= index
;
2109 else if (n
->Store
->File
== PROGRAM_UNIFORM
||
2110 n
->Store
->File
== PROGRAM_SAMPLER
) {
2111 /* mark var as used */
2112 _mesa_use_uniform(emitInfo
->prog
->Parameters
, (char *) n
->Var
->a_name
);
2115 if (n
->Store
->Index
< 0) {
2116 /* probably ran out of registers */
2119 assert(n
->Store
->Size
> 0);
2125 static struct prog_instruction
*
2126 emit(slang_emit_info
*emitInfo
, slang_ir_node
*n
)
2128 struct prog_instruction
*inst
;
2132 if (emitInfo
->log
->error_flag
) {
2136 switch (n
->Opcode
) {
2138 /* sequence of two sub-trees */
2139 assert(n
->Children
[0]);
2140 assert(n
->Children
[1]);
2141 emit(emitInfo
, n
->Children
[0]);
2142 if (emitInfo
->log
->error_flag
)
2144 inst
= emit(emitInfo
, n
->Children
[1]);
2148 n
->Store
= n
->Children
[1]->Store
;
2152 /* new variable scope */
2153 _slang_push_var_table(emitInfo
->vt
);
2154 inst
= emit(emitInfo
, n
->Children
[0]);
2155 _slang_pop_var_table(emitInfo
->vt
);
2159 /* Variable declaration - allocate a register for it */
2160 inst
= emit_var_decl(emitInfo
, n
);
2164 /* Reference to a variable
2165 * Storage should have already been resolved/allocated.
2167 return emit_var_ref(emitInfo
, n
);
2170 return emit_array_element(emitInfo
, n
);
2172 return emit_struct_field(emitInfo
, n
);
2174 return emit_swizzle(emitInfo
, n
);
2176 /* Simple arithmetic */
2213 /* trinary operators */
2215 return emit_arith(emitInfo
, n
);
2219 return emit_compare(emitInfo
, n
);
2222 return emit_clamp(emitInfo
, n
);
2226 return emit_tex(emitInfo
, n
);
2228 return emit_negation(emitInfo
, n
);
2230 /* find storage location for this float constant */
2231 n
->Store
->Index
= _mesa_add_unnamed_constant(emitInfo
->prog
->Parameters
,
2234 &n
->Store
->Swizzle
);
2235 if (n
->Store
->Index
< 0) {
2236 slang_info_log_error(emitInfo
->log
, "Ran out of space for constants");
2242 return emit_copy(emitInfo
, n
);
2245 return emit_cond(emitInfo
, n
);
2248 return emit_not(emitInfo
, n
);
2251 return emit_label(emitInfo
, n
);
2254 return emit_kill(emitInfo
);
2257 /* new variable scope for subroutines/function calls */
2258 _slang_push_var_table(emitInfo
->vt
);
2259 inst
= emit_fcall(emitInfo
, n
);
2260 _slang_pop_var_table(emitInfo
->vt
);
2264 return emit_if(emitInfo
, n
);
2267 return emit_loop(emitInfo
, n
);
2268 case IR_BREAK_IF_TRUE
:
2269 case IR_CONT_IF_TRUE
:
2270 return emit_cont_break_if_true(emitInfo
, n
);
2274 return emit_cont_break(emitInfo
, n
);
2277 return new_instruction(emitInfo
, OPCODE_BGNSUB
);
2279 return new_instruction(emitInfo
, OPCODE_ENDSUB
);
2281 return emit_return(emitInfo
, n
);
2287 _mesa_problem(NULL
, "Unexpected IR opcode in emit()\n");
2294 * After code generation, any subroutines will be in separate program
2295 * objects. This function appends all the subroutines onto the main
2296 * program and resolves the linking of all the branch/call instructions.
2297 * XXX this logic should really be part of the linking process...
2300 _slang_resolve_subroutines(slang_emit_info
*emitInfo
)
2302 GET_CURRENT_CONTEXT(ctx
);
2303 struct gl_program
*mainP
= emitInfo
->prog
;
2304 GLuint
*subroutineLoc
, i
, total
;
2307 = (GLuint
*) _mesa_malloc(emitInfo
->NumSubroutines
* sizeof(GLuint
));
2309 /* total number of instructions */
2310 total
= mainP
->NumInstructions
;
2311 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
2312 subroutineLoc
[i
] = total
;
2313 total
+= emitInfo
->Subroutines
[i
]->NumInstructions
;
2316 /* adjust BranchTargets within the functions */
2317 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
2318 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
2320 for (j
= 0; j
< sub
->NumInstructions
; j
++) {
2321 struct prog_instruction
*inst
= sub
->Instructions
+ j
;
2322 if (inst
->Opcode
!= OPCODE_CAL
&& inst
->BranchTarget
>= 0) {
2323 inst
->BranchTarget
+= subroutineLoc
[i
];
2328 /* append subroutines' instructions after main's instructions */
2329 mainP
->Instructions
= _mesa_realloc_instructions(mainP
->Instructions
,
2330 mainP
->NumInstructions
,
2332 mainP
->NumInstructions
= total
;
2333 for (i
= 0; i
< emitInfo
->NumSubroutines
; i
++) {
2334 struct gl_program
*sub
= emitInfo
->Subroutines
[i
];
2335 _mesa_copy_instructions(mainP
->Instructions
+ subroutineLoc
[i
],
2337 sub
->NumInstructions
);
2338 /* delete subroutine code */
2339 sub
->Parameters
= NULL
; /* prevent double-free */
2340 _mesa_reference_program(ctx
, &emitInfo
->Subroutines
[i
], NULL
);
2343 /* free subroutine list */
2344 if (emitInfo
->Subroutines
) {
2345 _mesa_free(emitInfo
->Subroutines
);
2346 emitInfo
->Subroutines
= NULL
;
2348 emitInfo
->NumSubroutines
= 0;
2350 /* Examine CAL instructions.
2351 * At this point, the BranchTarget field of the CAL instruction is
2352 * the number/id of the subroutine to call (an index into the
2353 * emitInfo->Subroutines list).
2354 * Translate that into an actual instruction location now.
2356 for (i
= 0; i
< mainP
->NumInstructions
; i
++) {
2357 struct prog_instruction
*inst
= mainP
->Instructions
+ i
;
2358 if (inst
->Opcode
== OPCODE_CAL
) {
2359 const GLuint f
= inst
->BranchTarget
;
2360 inst
->BranchTarget
= subroutineLoc
[f
];
2364 _mesa_free(subroutineLoc
);
2371 _slang_emit_code(slang_ir_node
*n
, slang_var_table
*vt
,
2372 struct gl_program
*prog
, GLboolean withEnd
,
2373 slang_info_log
*log
)
2375 GET_CURRENT_CONTEXT(ctx
);
2377 slang_emit_info emitInfo
;
2382 emitInfo
.prog
= prog
;
2383 emitInfo
.Subroutines
= NULL
;
2384 emitInfo
.NumSubroutines
= 0;
2385 emitInfo
.MaxInstructions
= prog
->NumInstructions
;
2387 emitInfo
.EmitHighLevelInstructions
= ctx
->Shader
.EmitHighLevelInstructions
;
2388 emitInfo
.EmitCondCodes
= ctx
->Shader
.EmitCondCodes
;
2389 emitInfo
.EmitComments
= ctx
->Shader
.EmitComments
;
2390 emitInfo
.EmitBeginEndSub
= GL_TRUE
;
2392 if (!emitInfo
.EmitCondCodes
) {
2393 emitInfo
.EmitHighLevelInstructions
= GL_TRUE
;
2396 /* Check uniform/constant limits */
2397 if (prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2398 maxUniforms
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
/ 4;
2401 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
2402 maxUniforms
= ctx
->Const
.VertexProgram
.MaxUniformComponents
/ 4;
2404 if (prog
->Parameters
->NumParameters
> maxUniforms
) {
2405 slang_info_log_error(log
, "Constant/uniform register limit exceeded");
2409 (void) emit(&emitInfo
, n
);
2411 /* finish up by adding the END opcode to program */
2413 struct prog_instruction
*inst
;
2414 inst
= new_instruction(&emitInfo
, OPCODE_END
);
2417 _slang_resolve_subroutines(&emitInfo
);
2422 printf("*********** End emit code (%u inst):\n", prog
->NumInstructions
);
2423 _mesa_print_program(prog
);
2424 _mesa_print_program_parameters(ctx
,prog
);