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.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
35 #include "prog_instruction.h"
36 #include "prog_parameter.h"
37 #include "prog_statevars.h"
38 #include "slang_typeinfo.h"
39 #include "slang_codegen.h"
40 #include "slang_compile.h"
41 #include "slang_error.h"
42 #include "slang_label.h"
43 #include "slang_simplify.h"
44 #include "slang_emit.h"
45 #include "slang_vartable.h"
47 #include "slang_print.h"
50 static slang_ir_node
*
51 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
55 is_sampler_type(const slang_fully_specified_type
*t
)
57 switch (t
->specifier
.type
) {
58 case SLANG_SPEC_SAMPLER1D
:
59 case SLANG_SPEC_SAMPLER2D
:
60 case SLANG_SPEC_SAMPLER3D
:
61 case SLANG_SPEC_SAMPLERCUBE
:
62 case SLANG_SPEC_SAMPLER1DSHADOW
:
63 case SLANG_SPEC_SAMPLER2DSHADOW
:
72 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
79 case SLANG_SPEC_BVEC2
:
81 case SLANG_SPEC_BVEC3
:
83 case SLANG_SPEC_BVEC4
:
87 case SLANG_SPEC_IVEC2
:
89 case SLANG_SPEC_IVEC3
:
91 case SLANG_SPEC_IVEC4
:
93 case SLANG_SPEC_FLOAT
:
101 case SLANG_SPEC_MAT2
:
103 case SLANG_SPEC_MAT3
:
105 case SLANG_SPEC_MAT4
:
107 case SLANG_SPEC_SAMPLER1D
:
108 case SLANG_SPEC_SAMPLER2D
:
109 case SLANG_SPEC_SAMPLER3D
:
110 case SLANG_SPEC_SAMPLERCUBE
:
111 case SLANG_SPEC_SAMPLER1DSHADOW
:
112 case SLANG_SPEC_SAMPLER2DSHADOW
:
113 return 1; /* special case */
114 case SLANG_SPEC_STRUCT
:
117 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
118 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
119 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
120 /* XXX verify padding */
127 case SLANG_SPEC_ARRAY
:
128 return _slang_sizeof_type_specifier(spec
->_array
);
130 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
138 * Establish the binding between a slang_ir_node and a slang_variable.
139 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
140 * The IR node must be a IR_VAR or IR_VAR_DECL node.
141 * \param n the IR node
142 * \param var the variable to associate with the IR node
145 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
149 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
150 assert(!n
->Var
|| n
->Var
== var
);
155 /* need to setup storage */
156 if (n
->Var
&& n
->Var
->aux
) {
157 /* node storage info = var storage info */
158 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
161 /* alloc new storage info */
162 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
164 n
->Var
->aux
= n
->Store
;
172 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
173 * or -1 if the type is not a sampler.
176 sampler_to_texture_index(const slang_type_specifier_type type
)
179 case SLANG_SPEC_SAMPLER1D
:
180 return TEXTURE_1D_INDEX
;
181 case SLANG_SPEC_SAMPLER2D
:
182 return TEXTURE_2D_INDEX
;
183 case SLANG_SPEC_SAMPLER3D
:
184 return TEXTURE_3D_INDEX
;
185 case SLANG_SPEC_SAMPLERCUBE
:
186 return TEXTURE_CUBE_INDEX
;
187 case SLANG_SPEC_SAMPLER1DSHADOW
:
188 return TEXTURE_1D_INDEX
; /* XXX fix */
189 case SLANG_SPEC_SAMPLER2DSHADOW
:
190 return TEXTURE_2D_INDEX
; /* XXX fix */
198 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
199 * a vertex or fragment program input variable. Return -1 if the input
201 * XXX return size too
204 _slang_input_index(const char *name
, GLenum target
)
210 static const struct input_info vertInputs
[] = {
211 { "gl_Vertex", VERT_ATTRIB_POS
},
212 { "gl_Normal", VERT_ATTRIB_NORMAL
},
213 { "gl_Color", VERT_ATTRIB_COLOR0
},
214 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
},
215 { "gl_FogCoord", VERT_ATTRIB_FOG
},
216 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
},
217 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
},
218 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
},
219 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
},
220 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
},
221 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
},
222 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
},
223 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
},
226 static const struct input_info fragInputs
[] = {
227 { "gl_FragCoord", FRAG_ATTRIB_WPOS
},
228 { "gl_Color", FRAG_ATTRIB_COL0
},
229 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
},
230 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
},
231 { "gl_TexCoord", FRAG_ATTRIB_TEX0
},
235 const struct input_info
*inputs
236 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
238 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
240 for (i
= 0; inputs
[i
].Name
; i
++) {
241 if (strcmp(inputs
[i
].Name
, name
) == 0) {
243 return inputs
[i
].Attrib
;
251 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
252 * a vertex or fragment program output variable. Return -1 for an invalid
256 _slang_output_index(const char *name
, GLenum target
)
262 static const struct output_info vertOutputs
[] = {
263 { "gl_Position", VERT_RESULT_HPOS
},
264 { "gl_FrontColor", VERT_RESULT_COL0
},
265 { "gl_BackColor", VERT_RESULT_BFC0
},
266 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
267 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
268 { "gl_TexCoord", VERT_RESULT_TEX0
}, /* XXX indexed */
269 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
270 { "gl_PointSize", VERT_RESULT_PSIZ
},
273 static const struct output_info fragOutputs
[] = {
274 { "gl_FragColor", FRAG_RESULT_COLR
},
275 { "gl_FragDepth", FRAG_RESULT_DEPR
},
279 const struct output_info
*outputs
280 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
282 for (i
= 0; outputs
[i
].Name
; i
++) {
283 if (strcmp(outputs
[i
].Name
, name
) == 0) {
285 return outputs
[i
].Attrib
;
293 /**********************************************************************/
297 * Map "_asm foo" to IR_FOO, etc.
302 slang_ir_opcode Opcode
;
303 GLuint HaveRetValue
, NumParams
;
307 static slang_asm_info AsmInfo
[] = {
309 { "vec4_add", IR_ADD
, 1, 2 },
310 { "vec4_subtract", IR_SUB
, 1, 2 },
311 { "vec4_multiply", IR_MUL
, 1, 2 },
312 { "vec4_dot", IR_DOT4
, 1, 2 },
313 { "vec3_dot", IR_DOT3
, 1, 2 },
314 { "vec3_cross", IR_CROSS
, 1, 2 },
315 { "vec4_lrp", IR_LRP
, 1, 3 },
316 { "vec4_min", IR_MIN
, 1, 2 },
317 { "vec4_max", IR_MAX
, 1, 2 },
318 { "vec4_clamp", IR_CLAMP
, 1, 3 },
319 { "vec4_seq", IR_SEQ
, 1, 2 },
320 { "vec4_sge", IR_SGE
, 1, 2 },
321 { "vec4_sgt", IR_SGT
, 1, 2 },
323 { "vec4_floor", IR_FLOOR
, 1, 1 },
324 { "vec4_frac", IR_FRAC
, 1, 1 },
325 { "vec4_abs", IR_ABS
, 1, 1 },
326 { "vec4_negate", IR_NEG
, 1, 1 },
327 { "vec4_ddx", IR_DDX
, 1, 1 },
328 { "vec4_ddy", IR_DDY
, 1, 1 },
329 /* float binary op */
330 { "float_add", IR_ADD
, 1, 2 },
331 { "float_multiply", IR_MUL
, 1, 2 },
332 { "float_divide", IR_DIV
, 1, 2 },
333 { "float_power", IR_POW
, 1, 2 },
334 /* texture / sampler */
335 { "vec4_tex1d", IR_TEX
, 1, 2 },
336 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
337 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
338 { "vec4_tex2d", IR_TEX
, 1, 2 },
339 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
340 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
341 { "vec4_tex3d", IR_TEX
, 1, 2 },
342 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
343 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
344 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
347 { "int_to_float", IR_I_TO_F
, 1, 1 },
348 { "float_to_int", IR_F_TO_I
, 1, 1 },
349 { "float_exp", IR_EXP
, 1, 1 },
350 { "float_exp2", IR_EXP2
, 1, 1 },
351 { "float_log2", IR_LOG2
, 1, 1 },
352 { "float_rsq", IR_RSQ
, 1, 1 },
353 { "float_rcp", IR_RCP
, 1, 1 },
354 { "float_sine", IR_SIN
, 1, 1 },
355 { "float_cosine", IR_COS
, 1, 1 },
356 { "float_noise1", IR_NOISE1
, 1, 1},
357 { "float_noise2", IR_NOISE2
, 1, 1},
358 { "float_noise3", IR_NOISE3
, 1, 1},
359 { "float_noise4", IR_NOISE4
, 1, 1},
361 { NULL
, IR_NOP
, 0, 0 }
366 * Recursively free an IR tree.
369 _slang_free_ir_tree(slang_ir_node
*n
)
375 for (i
= 0; i
< 3; i
++)
376 _slang_free_ir_tree(n
->Children
[i
]);
377 /* Do not free n->BranchNode since it's a child elsewhere */
383 static slang_ir_node
*
384 new_node3(slang_ir_opcode op
,
385 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
387 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
393 n
->Writemask
= WRITEMASK_XYZW
;
394 n
->InstLocation
= -1;
399 static slang_ir_node
*
400 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
402 return new_node3(op
, c0
, c1
, NULL
);
405 static slang_ir_node
*
406 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
408 return new_node3(op
, c0
, NULL
, NULL
);
411 static slang_ir_node
*
412 new_node0(slang_ir_opcode op
)
414 return new_node3(op
, NULL
, NULL
, NULL
);
418 static slang_ir_node
*
419 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
425 return new_node2(IR_SEQ
, left
, right
);
428 static slang_ir_node
*
429 new_label(slang_label
*label
)
431 slang_ir_node
*n
= new_node0(IR_LABEL
);
437 static slang_ir_node
*
438 new_float_literal(const float v
[4])
440 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
441 slang_ir_node
*n
= new_node0(IR_FLOAT
);
442 COPY_4V(n
->Value
, v
);
443 /* allocate a storage object, but compute actual location (Index) later */
444 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
450 * \param zeroOrOne indicates if the jump is to be taken on zero, or non-zero
451 * condition code state.
452 * XXX maybe pass an IR node as second param to indicate the jump target???
454 static slang_ir_node
*
455 new_cjump(slang_label
*dest
, GLuint zeroOrOne
)
457 slang_ir_node
*n
= new_node0(zeroOrOne
? IR_CJUMP1
: IR_CJUMP0
);
464 * Unconditional jump.
465 * XXX maybe pass an IR node as second param to indicate the jump target???
467 static slang_ir_node
*
468 new_jump(slang_label
*dest
)
470 slang_ir_node
*n
= new_node0(IR_JUMP
);
477 static slang_ir_node
*
478 new_loop(slang_ir_node
*body
)
480 return new_node1(IR_LOOP
, body
);
484 static slang_ir_node
*
485 new_break(slang_ir_node
*loopNode
)
487 slang_ir_node
*n
= new_node0(IR_BREAK
);
489 assert(loopNode
->Opcode
== IR_LOOP
);
491 /* insert this node at head of linked list */
492 n
->BranchNode
= loopNode
->BranchNode
;
493 loopNode
->BranchNode
= n
;
500 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
502 static slang_ir_node
*
503 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
507 assert(loopNode
->Opcode
== IR_LOOP
);
508 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
510 /* insert this node at head of linked list */
511 n
->BranchNode
= loopNode
->BranchNode
;
512 loopNode
->BranchNode
= n
;
519 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
521 static slang_ir_node
*
522 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
526 assert(loopNode
->Opcode
== IR_LOOP
);
527 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
529 /* insert this node at head of linked list */
530 n
->BranchNode
= loopNode
->BranchNode
;
531 loopNode
->BranchNode
= n
;
537 static slang_ir_node
*
538 new_cont(slang_ir_node
*loopNode
)
540 slang_ir_node
*n
= new_node0(IR_CONT
);
542 assert(loopNode
->Opcode
== IR_LOOP
);
544 /* insert this node at head of linked list */
545 n
->BranchNode
= loopNode
->BranchNode
;
546 loopNode
->BranchNode
= n
;
552 static slang_ir_node
*
553 new_cond(slang_ir_node
*n
)
555 slang_ir_node
*c
= new_node1(IR_COND
, n
);
560 static slang_ir_node
*
561 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
563 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
568 * New IR_VAR node - a reference to a previously declared variable.
570 static slang_ir_node
*
571 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
574 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
578 assert(!oper
->var
|| oper
->var
== var
);
580 n
= new_node0(IR_VAR
);
582 _slang_attach_storage(n
, var
);
589 * Check if the given function is really just a wrapper for a
590 * basic assembly instruction.
593 slang_is_asm_function(const slang_function
*fun
)
595 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
596 fun
->body
->num_children
== 1 &&
597 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
605 _slang_is_noop(const slang_operation
*oper
)
608 oper
->type
== SLANG_OPER_VOID
||
609 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
617 * Produce inline code for a call to an assembly instruction.
619 static slang_operation
*
620 slang_inline_asm_function(slang_assemble_ctx
*A
,
621 slang_function
*fun
, slang_operation
*oper
)
623 const GLuint numArgs
= oper
->num_children
;
624 const slang_operation
*args
= oper
->children
;
626 slang_operation
*inlined
= slang_operation_new(1);
628 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
630 printf("Inline asm %s\n", (char*) fun->header.a_name);
632 inlined
->type
= fun
->body
->children
[0].type
;
633 inlined
->a_id
= fun
->body
->children
[0].a_id
;
634 inlined
->num_children
= numArgs
;
635 inlined
->children
= slang_operation_new(numArgs
);
637 inlined
->locals
= slang_variable_scope_copy(oper
->locals
);
639 assert(inlined
->locals
);
640 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
643 for (i
= 0; i
< numArgs
; i
++) {
644 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
652 slang_resolve_variable(slang_operation
*oper
)
654 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
655 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
661 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
664 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
665 GLuint substCount
, slang_variable
**substOld
,
666 slang_operation
**substNew
, GLboolean isLHS
)
668 switch (oper
->type
) {
669 case SLANG_OPER_VARIABLE_DECL
:
671 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
672 oper
->a_id
, GL_TRUE
);
674 if (v
->initializer
&& oper
->num_children
== 0) {
675 /* set child of oper to copy of initializer */
676 oper
->num_children
= 1;
677 oper
->children
= slang_operation_new(1);
678 slang_operation_copy(&oper
->children
[0], v
->initializer
);
680 if (oper
->num_children
== 1) {
681 /* the initializer */
682 slang_substitute(A
, &oper
->children
[0], substCount
,
683 substOld
, substNew
, GL_FALSE
);
687 case SLANG_OPER_IDENTIFIER
:
688 assert(oper
->num_children
== 0);
689 if (1/**!isLHS XXX FIX */) {
690 slang_atom id
= oper
->a_id
;
693 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
695 printf("var %s not found!\n", (char *) oper
->a_id
);
696 _slang_print_var_scope(oper
->locals
, 6);
702 /* look for a substitution */
703 for (i
= 0; i
< substCount
; i
++) {
704 if (v
== substOld
[i
]) {
705 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
706 #if 0 /* DEBUG only */
707 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
708 assert(substNew
[i
]->var
);
709 assert(substNew
[i
]->var
->a_name
);
710 printf("Substitute %s with %s in id node %p\n",
711 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
715 printf("Substitute %s with %f in id node %p\n",
716 (char*)v
->a_name
, substNew
[i
]->literal
[0],
720 slang_operation_copy(oper
, substNew
[i
]);
727 case SLANG_OPER_RETURN
:
728 /* do return replacement here too */
729 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
730 if (!_slang_is_noop(oper
)) {
736 * then do substitutions on the assignment.
738 slang_operation
*blockOper
, *assignOper
, *returnOper
;
739 blockOper
= slang_operation_new(1);
740 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
741 blockOper
->num_children
= 2;
742 blockOper
->children
= slang_operation_new(2);
743 assignOper
= blockOper
->children
+ 0;
744 returnOper
= blockOper
->children
+ 1;
746 assignOper
->type
= SLANG_OPER_ASSIGN
;
747 assignOper
->num_children
= 2;
748 assignOper
->children
= slang_operation_new(2);
749 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
750 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
751 assignOper
->children
[0].locals
->outer_scope
= oper
->locals
;
752 assignOper
->locals
= oper
->locals
;
753 slang_operation_copy(&assignOper
->children
[1],
756 returnOper
->type
= SLANG_OPER_RETURN
;
757 assert(returnOper
->num_children
== 0);
759 /* do substitutions on the "__retVal = expr" sub-tree */
760 slang_substitute(A
, assignOper
,
761 substCount
, substOld
, substNew
, GL_FALSE
);
763 /* install new code */
764 slang_operation_copy(oper
, blockOper
);
765 slang_operation_destruct(blockOper
);
769 case SLANG_OPER_ASSIGN
:
770 case SLANG_OPER_SUBSCRIPT
:
772 * child[0] can't have substitutions but child[1] can.
774 slang_substitute(A
, &oper
->children
[0],
775 substCount
, substOld
, substNew
, GL_TRUE
);
776 slang_substitute(A
, &oper
->children
[1],
777 substCount
, substOld
, substNew
, GL_FALSE
);
779 case SLANG_OPER_FIELD
:
781 slang_substitute(A
, &oper
->children
[0],
782 substCount
, substOld
, substNew
, GL_TRUE
);
787 for (i
= 0; i
< oper
->num_children
; i
++)
788 slang_substitute(A
, &oper
->children
[i
],
789 substCount
, substOld
, substNew
, GL_FALSE
);
797 * Inline the given function call operation.
798 * Return a new slang_operation that corresponds to the inlined code.
800 static slang_operation
*
801 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
802 slang_operation
*oper
, slang_operation
*returnOper
)
809 ParamMode
*paramMode
;
810 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
811 const GLuint numArgs
= oper
->num_children
;
812 const GLuint totalArgs
= numArgs
+ haveRetValue
;
813 slang_operation
*args
= oper
->children
;
814 slang_operation
*inlined
, *top
;
815 slang_variable
**substOld
;
816 slang_operation
**substNew
;
817 GLuint substCount
, numCopyIn
, i
;
819 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
820 assert(fun
->param_count
== totalArgs
);
822 /* allocate temporary arrays */
823 paramMode
= (ParamMode
*)
824 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
825 substOld
= (slang_variable
**)
826 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
827 substNew
= (slang_operation
**)
828 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
831 printf("Inline call to %s (total vars=%d nparams=%d)\n",
832 (char *) fun
->header
.a_name
,
833 fun
->parameters
->num_variables
, numArgs
);
836 if (haveRetValue
&& !returnOper
) {
837 /* Create 3-child comma sequence for inlined code:
838 * child[0]: declare __resultTmp
839 * child[1]: inlined function body
840 * child[2]: __resultTmp
842 slang_operation
*commaSeq
;
843 slang_operation
*declOper
= NULL
;
844 slang_variable
*resultVar
;
846 commaSeq
= slang_operation_new(1);
847 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
848 assert(commaSeq
->locals
);
849 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
850 commaSeq
->num_children
= 3;
851 commaSeq
->children
= slang_operation_new(3);
852 /* allocate the return var */
853 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
855 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
856 (void*)commaSeq->locals, (char *) fun->header.a_name);
859 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
860 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
861 resultVar
->isTemp
= GL_TRUE
;
863 /* child[0] = __resultTmp declaration */
864 declOper
= &commaSeq
->children
[0];
865 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
866 declOper
->a_id
= resultVar
->a_name
;
867 declOper
->locals
->outer_scope
= commaSeq
->locals
; /*** ??? **/
869 /* child[1] = function body */
870 inlined
= &commaSeq
->children
[1];
871 /* XXXX this may be inappropriate!!!!: */
872 inlined
->locals
->outer_scope
= commaSeq
->locals
;
874 /* child[2] = __resultTmp reference */
875 returnOper
= &commaSeq
->children
[2];
876 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
877 returnOper
->a_id
= resultVar
->a_name
;
878 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
879 declOper
->locals
->outer_scope
= commaSeq
->locals
;
884 top
= inlined
= slang_operation_new(1);
885 /* XXXX this may be inappropriate!!!! */
886 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
890 assert(inlined
->locals
);
892 /* Examine the parameters, look for inout/out params, look for possible
893 * substitutions, etc:
894 * param type behaviour
895 * in copy actual to local
896 * const in substitute param with actual
900 for (i
= 0; i
< totalArgs
; i
++) {
901 slang_variable
*p
= fun
->parameters
->variables
[i
];
903 printf("Param %d: %s %s \n", i,
904 slang_type_qual_string(p->type.qualifier),
907 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
908 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
909 /* an output param */
910 slang_operation
*arg
;
915 paramMode
[i
] = SUBST
;
917 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
918 slang_resolve_variable(arg
);
920 /* replace parameter 'p' with argument 'arg' */
921 substOld
[substCount
] = p
;
922 substNew
[substCount
] = arg
; /* will get copied */
925 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
926 /* a constant input param */
927 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
928 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
929 /* replace all occurances of this parameter variable with the
930 * actual argument variable or a literal.
932 paramMode
[i
] = SUBST
;
933 slang_resolve_variable(&args
[i
]);
934 substOld
[substCount
] = p
;
935 substNew
[substCount
] = &args
[i
]; /* will get copied */
939 paramMode
[i
] = COPY_IN
;
943 paramMode
[i
] = COPY_IN
;
945 assert(paramMode
[i
]);
948 /* actual code inlining: */
949 slang_operation_copy(inlined
, fun
->body
);
951 /*** XXX review this */
952 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
953 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
956 printf("======================= orig body code ======================\n");
957 printf("=== params scope = %p\n", (void*) fun
->parameters
);
958 slang_print_tree(fun
->body
, 8);
959 printf("======================= copied code =========================\n");
960 slang_print_tree(inlined
, 8);
963 /* do parameter substitution in inlined code: */
964 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
967 printf("======================= subst code ==========================\n");
968 slang_print_tree(inlined
, 8);
969 printf("=============================================================\n");
972 /* New prolog statements: (inserted before the inlined code)
973 * Copy the 'in' arguments.
976 for (i
= 0; i
< numArgs
; i
++) {
977 if (paramMode
[i
] == COPY_IN
) {
978 slang_variable
*p
= fun
->parameters
->variables
[i
];
979 /* declare parameter 'p' */
980 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
984 printf("COPY_IN %s from expr\n", (char*)p->a_name);
986 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
987 assert(decl
->locals
);
988 decl
->locals
= fun
->parameters
;
989 decl
->a_id
= p
->a_name
;
990 decl
->num_children
= 1;
991 decl
->children
= slang_operation_new(1);
993 /* child[0] is the var's initializer */
994 slang_operation_copy(&decl
->children
[0], args
+ i
);
1000 /* New epilog statements:
1001 * 1. Create end of function label to jump to from return statements.
1002 * 2. Copy the 'out' parameter vars
1005 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1007 inlined
->num_children
);
1008 lab
->type
= SLANG_OPER_LABEL
;
1009 lab
->label
= A
->CurFunction
->end_label
;
1012 for (i
= 0; i
< totalArgs
; i
++) {
1013 if (paramMode
[i
] == COPY_OUT
) {
1014 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1015 /* actualCallVar = outParam */
1016 /*if (i > 0 || !haveRetValue)*/
1017 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1019 inlined
->num_children
);
1020 ass
->type
= SLANG_OPER_ASSIGN
;
1021 ass
->num_children
= 2;
1022 ass
->locals
= _slang_variable_scope_new(inlined
->locals
);
1023 assert(ass
->locals
);
1024 ass
->children
= slang_operation_new(2);
1025 ass
->children
[0] = args
[i
]; /*XXX copy */
1026 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1027 ass
->children
[1].a_id
= p
->a_name
;
1028 ass
->children
[1].locals
= _slang_variable_scope_new(ass
->locals
);
1032 _mesa_free(paramMode
);
1033 _mesa_free(substOld
);
1034 _mesa_free(substNew
);
1037 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1038 (char *) fun
->header
.a_name
,
1039 fun
->parameters
->num_variables
, numArgs
);
1040 slang_print_tree(top
, 0);
1046 static slang_ir_node
*
1047 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1048 slang_operation
*oper
, slang_operation
*dest
)
1051 slang_operation
*inlined
;
1052 slang_function
*prevFunc
;
1054 prevFunc
= A
->CurFunction
;
1055 A
->CurFunction
= fun
;
1057 if (!A
->CurFunction
->end_label
) {
1059 sprintf(name
, "__endOfFunc_%s_", (char *) A
->CurFunction
->header
.a_name
);
1060 A
->CurFunction
->end_label
= _slang_label_new(name
);
1063 if (slang_is_asm_function(fun
) && !dest
) {
1064 /* assemble assembly function - tree style */
1065 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1068 /* non-assembly function */
1069 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1072 /* Replace the function call with the inlined block */
1074 slang_operation_construct(oper
);
1075 slang_operation_copy(oper
, inlined
);
1082 assert(inlined
->locals
);
1083 printf("*** Inlined code for call to %s:\n",
1084 (char*) fun
->header
.a_name
);
1085 slang_print_tree(oper
, 10);
1089 n
= _slang_gen_operation(A
, oper
);
1091 A
->CurFunction
->end_label
= NULL
; /* XXX delete/free? */
1093 A
->CurFunction
= prevFunc
;
1099 static slang_asm_info
*
1100 slang_find_asm_info(const char *name
)
1103 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1104 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1113 make_writemask(const char *field
)
1119 mask
|= WRITEMASK_X
;
1122 mask
|= WRITEMASK_Y
;
1125 mask
|= WRITEMASK_Z
;
1128 mask
|= WRITEMASK_W
;
1136 return WRITEMASK_XYZW
;
1143 * Generate IR tree for an asm instruction/operation such as:
1144 * __asm vec4_dot __retVal.x, v1, v2;
1146 static slang_ir_node
*
1147 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1148 slang_operation
*dest
)
1150 const slang_asm_info
*info
;
1151 slang_ir_node
*kids
[3], *n
;
1152 GLuint j
, firstOperand
;
1154 assert(oper
->type
== SLANG_OPER_ASM
);
1156 info
= slang_find_asm_info((char *) oper
->a_id
);
1158 _mesa_problem(NULL
, "undefined __asm function %s\n",
1159 (char *) oper
->a_id
);
1162 assert(info
->NumParams
<= 3);
1164 if (info
->NumParams
== oper
->num_children
) {
1165 /* Storage for result is not specified.
1166 * Children[0], [1] are the operands.
1171 /* Storage for result (child[0]) is specified.
1172 * Children[1], [2] are the operands.
1177 /* assemble child(ren) */
1178 kids
[0] = kids
[1] = kids
[2] = NULL
;
1179 for (j
= 0; j
< info
->NumParams
; j
++) {
1180 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1183 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1186 /* Setup n->Store to be a particular location. Otherwise, storage
1187 * for the result (a temporary) will be allocated later.
1189 GLuint writemask
= WRITEMASK_XYZW
;
1190 slang_operation
*dest_oper
;
1193 dest_oper
= &oper
->children
[0];
1194 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1196 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1197 dest_oper
= &dest_oper
->children
[0];
1200 n0
= _slang_gen_operation(A
, dest_oper
);
1204 n
->Store
= n0
->Store
;
1205 n
->Writemask
= writemask
;
1215 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1218 for (i
= 0; i
< scope
->num_functions
; i
++) {
1219 slang_function
*f
= &scope
->functions
[i
];
1220 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1221 printf(" %s (%d args)\n", name
, f
->param_count
);
1224 if (scope
->outer_scope
)
1225 print_funcs(scope
->outer_scope
, name
);
1230 * Return first function in the scope that has the given name.
1231 * This is the function we'll try to call when there is no exact match
1232 * between function parameters and call arguments.
1234 * XXX we should really create a list of candidate functions and try
1237 static slang_function
*
1238 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1241 for (i
= 0; i
< scope
->num_functions
; i
++) {
1242 slang_function
*f
= &scope
->functions
[i
];
1243 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1246 if (scope
->outer_scope
)
1247 return _slang_first_function(scope
->outer_scope
, name
);
1254 * Assemble a function call, given a particular function name.
1255 * \param name the function's name (operators like '*' are possible).
1257 static slang_ir_node
*
1258 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1259 slang_operation
*oper
, slang_operation
*dest
)
1261 slang_operation
*params
= oper
->children
;
1262 const GLuint param_count
= oper
->num_children
;
1264 slang_function
*fun
;
1266 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1267 if (atom
== SLANG_ATOM_NULL
)
1271 * Use 'name' to find the function to call
1273 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1274 &A
->space
, A
->atoms
);
1276 /* A function with exactly the right parameters/types was not found.
1277 * Try adapting the parameters.
1279 fun
= _slang_first_function(A
->space
.funcs
, name
);
1280 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
)) {
1281 RETURN_ERROR2("Undefined function (or no matching parameters)",
1287 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1292 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1294 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1295 oper
->type
== SLANG_OPER_LITERAL_INT
||
1296 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1297 if (oper
->literal
[0])
1303 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1304 oper
->num_children
== 1) {
1305 return _slang_is_constant_cond(&oper
->children
[0], value
);
1313 * Generate loop code using high-level IR_LOOP instruction
1315 static slang_ir_node
*
1316 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1320 * BREAK if !expr (child[0])
1321 * body code (child[1])
1323 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1324 GLboolean isConst
, constTrue
;
1326 /* Check if loop condition is a constant */
1327 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1329 if (isConst
&& !constTrue
) {
1330 /* loop is never executed! */
1331 return new_node0(IR_NOP
);
1334 loop
= new_loop(NULL
);
1336 /* save old, push new loop */
1337 prevLoop
= A
->CurLoop
;
1340 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1341 if (isConst
&& constTrue
) {
1342 /* while(nonzero constant), no conditional break */
1346 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1348 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1349 loop
->Children
[0] = new_seq(breakIf
, body
);
1351 /* Do infinite loop detection */
1352 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1353 /* infinite loop detected */
1354 A
->CurLoop
= prevLoop
; /* clean-up */
1355 RETURN_ERROR("Infinite loop detected!", 0);
1358 /* pop loop, restore prev */
1359 A
->CurLoop
= prevLoop
;
1366 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1368 static slang_ir_node
*
1369 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1373 * body code (child[0])
1374 * BREAK if !expr (child[1])
1376 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1377 GLboolean isConst
, constTrue
;
1379 /* Check if loop condition is a constant */
1380 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1382 loop
= new_loop(NULL
);
1384 /* save old, push new loop */
1385 prevLoop
= A
->CurLoop
;
1388 body
= _slang_gen_operation(A
, &oper
->children
[0]);
1389 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1390 if (isConst
&& constTrue
) {
1391 /* while(nonzero constant), no conditional break */
1395 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1397 loop
->Children
[0] = new_seq(body
, breakIf
);
1399 /* pop loop, restore prev */
1400 A
->CurLoop
= prevLoop
;
1407 * Generate for-loop using high-level IR_LOOP instruction.
1409 static slang_ir_node
*
1410 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1415 * BREAK if !expr (child[1])
1416 * body code (child[3])
1417 * incr code (child[2]) // XXX continue here
1419 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1421 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1422 loop
= new_loop(NULL
);
1424 /* save old, push new loop */
1425 prevLoop
= A
->CurLoop
;
1428 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1429 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1430 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1431 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1432 loop
->Children
[0] = new_seq(breakIf
,
1433 new_seq(body
, incr
));
1435 /* pop loop, restore prev */
1436 A
->CurLoop
= prevLoop
;
1438 return new_seq(init
, loop
);
1444 * Generate IR tree for an if/then/else conditional using BRAnch instructions.
1446 static slang_ir_node
*
1447 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1450 * eval expr (child[0]), updating condcodes
1451 * branch if false to _else or _endif
1453 * if haveElseClause clause:
1456 * "false" code block
1459 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1460 slang_ir_node
*cond
, *bra
, *trueBody
, *endifLab
, *tree
;
1461 slang_atom elseAtom
= slang_atom_pool_gen(A
->atoms
, "__else");
1462 slang_atom endifAtom
= slang_atom_pool_gen(A
->atoms
, "__endif");
1464 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1465 cond
= new_cond(cond
);
1466 /*assert(cond->Store);*/
1467 bra
= new_cjump(haveElseClause
? elseAtom
: endifAtom
, 0);
1468 tree
= new_seq(cond
, bra
);
1470 trueBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1471 tree
= new_seq(tree
, trueBody
);
1473 if (haveElseClause
) {
1475 slang_ir_node
*jump
, *elseLab
, *falseBody
;
1476 jump
= new_jump(endifAtom
);
1477 tree
= new_seq(tree
, jump
);
1479 elseLab
= new_label(elseAtom
);
1480 tree
= new_seq(tree
, elseLab
);
1482 falseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1483 tree
= new_seq(tree
, falseBody
);
1486 endifLab
= new_label(endifAtom
);
1487 tree
= new_seq(tree
, endifLab
);
1495 * Determine if the given operation is of a specific type.
1498 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1500 if (oper
->type
== type
)
1502 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1503 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1504 oper
->num_children
== 1)
1505 return is_operation_type(&oper
->children
[0], type
);
1512 * Generate IR tree for an if/then/else conditional using high-level
1513 * IR_IF instruction.
1515 static slang_ir_node
*
1516 _slang_gen_hl_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1519 * eval expr (child[0]), updating condcodes
1526 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1527 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1529 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1530 cond
= new_cond(cond
);
1532 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1533 /* Special case: generate a conditional break */
1534 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1535 if (haveElseClause
) {
1536 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1537 return new_seq(ifBody
, elseBody
);
1541 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1542 /* Special case: generate a conditional break */
1543 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1544 if (haveElseClause
) {
1545 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1546 return new_seq(ifBody
, elseBody
);
1552 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1554 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1557 ifNode
= new_if(cond
, ifBody
, elseBody
);
1565 * Generate IR node for storage of a temporary of given size.
1567 static slang_ir_node
*
1568 _slang_gen_temporary(GLint size
)
1570 slang_ir_storage
*store
;
1573 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1575 n
= new_node0(IR_VAR_DECL
);
1588 * Generate IR node for allocating/declaring a variable.
1590 static slang_ir_node
*
1591 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1594 assert(!is_sampler_type(&var
->type
));
1595 n
= new_node0(IR_VAR_DECL
);
1597 _slang_attach_storage(n
, var
);
1600 assert(n
->Store
== var
->aux
);
1602 assert(n
->Store
->Index
< 0);
1604 n
->Store
->File
= PROGRAM_TEMPORARY
;
1605 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1606 assert(n
->Store
->Size
> 0);
1613 * Generate code for a selection expression: b ? x : y
1614 * XXX in some cases we could implement a selection expression
1615 * with an LRP instruction (use the boolean as the interpolant).
1617 static slang_ir_node
*
1618 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1620 slang_label
*altLabel
, *endLabel
;
1621 slang_ir_node
*altLab
, *endLab
;
1622 slang_ir_node
*tree
, *tmpDecl
, *tmpVar
, *cond
, *cjump
, *jump
;
1623 slang_ir_node
*bodx
, *body
, *assignx
, *assigny
;
1624 slang_typeinfo type
;
1627 assert(oper
->type
== SLANG_OPER_SELECT
);
1628 assert(oper
->num_children
== 3);
1630 altLabel
= _slang_label_new("selectAlt");
1631 endLabel
= _slang_label_new("selectEnd");
1633 /* size of x or y's type */
1634 slang_typeinfo_construct(&type
);
1635 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1636 size
= _slang_sizeof_type_specifier(&type
.spec
);
1640 tmpDecl
= _slang_gen_temporary(size
);
1642 /* eval condition */
1643 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1644 cond
= new_cond(cond
);
1645 tree
= new_seq(tmpDecl
, cond
);
1647 /* jump if false to "alt" label */
1648 cjump
= new_cjump(altLabel
, 0);
1649 tree
= new_seq(tree
, cjump
);
1651 /* evaluate child 1 (x) and assign to tmp */
1652 tmpVar
= new_node0(IR_VAR
);
1653 tmpVar
->Store
= tmpDecl
->Store
;
1654 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1655 assigny
= new_node2(IR_MOVE
, tmpVar
, body
);
1656 tree
= new_seq(tree
, assigny
);
1658 /* jump to "end" label */
1659 jump
= new_jump(endLabel
);
1660 tree
= new_seq(tree
, jump
);
1663 altLab
= new_label(altLabel
);
1664 tree
= new_seq(tree
, altLab
);
1666 /* evaluate child 2 (y) and assign to tmp */
1667 tmpVar
= new_node0(IR_VAR
);
1668 tmpVar
->Store
= tmpDecl
->Store
;
1669 bodx
= _slang_gen_operation(A
, &oper
->children
[2]);
1670 assignx
= new_node2(IR_MOVE
, tmpVar
, bodx
);
1671 tree
= new_seq(tree
, assignx
);
1674 endLab
= new_label(endLabel
);
1675 tree
= new_seq(tree
, endLab
);
1678 tmpVar
= new_node0(IR_VAR
);
1679 tmpVar
->Store
= tmpDecl
->Store
;
1680 tree
= new_seq(tree
, tmpVar
);
1687 * Generate code for &&.
1689 static slang_ir_node
*
1690 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1692 /* rewrite "a && b" as "a ? b : false" */
1693 slang_operation
*select
;
1696 select
= slang_operation_new(1);
1697 select
->type
= SLANG_OPER_SELECT
;
1698 select
->num_children
= 3;
1699 select
->children
= slang_operation_new(3);
1701 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1702 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1703 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1704 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0);
1705 select
->children
[2].literal_size
= 2;
1707 n
= _slang_gen_select(A
, select
);
1710 free(select
->children
);
1718 * Generate code for ||.
1720 static slang_ir_node
*
1721 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1723 /* rewrite "a || b" as "a ? true : b" */
1724 slang_operation
*select
;
1727 select
= slang_operation_new(1);
1728 select
->type
= SLANG_OPER_SELECT
;
1729 select
->num_children
= 3;
1730 select
->children
= slang_operation_new(3);
1732 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1733 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1734 ASSIGN_4V(select
->children
[2].literal
, 1, 1, 1, 1);
1735 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1736 select
->children
[2].literal_size
= 2;
1738 n
= _slang_gen_select(A
, select
);
1741 free(select
->children
);
1749 * Generate IR tree for a return statement.
1751 static slang_ir_node
*
1752 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1754 if (oper
->num_children
== 0 ||
1755 (oper
->num_children
== 1 &&
1756 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1760 * goto __endOfFunction;
1763 slang_operation gotoOp
;
1764 slang_operation_construct(&gotoOp
);
1765 gotoOp
.type
= SLANG_OPER_GOTO
;
1766 /* XXX don't call function? */
1767 gotoOp
.label
= A
->CurFunction
->end_label
;
1769 /* assemble the new code */
1770 n
= _slang_gen_operation(A
, &gotoOp
);
1771 /* destroy temp code */
1772 slang_operation_destruct(&gotoOp
);
1781 * goto __endOfFunction;
1783 slang_operation
*block
, *assign
, *jump
;
1784 slang_atom a_retVal
;
1787 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1793 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1798 block
= slang_operation_new(1);
1799 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1800 block
->num_children
= 2;
1801 block
->children
= slang_operation_new(2);
1802 assert(block
->locals
);
1803 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1805 /* child[0]: __retVal = expr; */
1806 assign
= &block
->children
[0];
1807 assign
->type
= SLANG_OPER_ASSIGN
;
1808 assign
->locals
->outer_scope
= block
->locals
;
1809 assign
->num_children
= 2;
1810 assign
->children
= slang_operation_new(2);
1811 /* lhs (__retVal) */
1812 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1813 assign
->children
[0].a_id
= a_retVal
;
1814 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1816 /* XXX we might be able to avoid this copy someday */
1817 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1819 /* child[1]: goto __endOfFunction */
1820 jump
= &block
->children
[1];
1821 jump
->type
= SLANG_OPER_GOTO
;
1822 assert(A
->CurFunction
->end_label
);
1823 /* XXX don't call function? */
1824 jump
->label
= A
->CurFunction
->end_label
;
1827 printf("NEW RETURN:\n");
1828 slang_print_tree(block
, 0);
1831 /* assemble the new code */
1832 n
= _slang_gen_operation(A
, block
);
1833 slang_operation_delete(block
);
1840 * Generate IR tree for a variable declaration.
1842 static slang_ir_node
*
1843 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1846 slang_ir_node
*varDecl
;
1848 const char *varName
= (char *) oper
->a_id
;
1850 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1852 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1855 varDecl
= _slang_gen_var_decl(A
, v
);
1857 if (oper
->num_children
> 0) {
1858 /* child is initializer */
1859 slang_ir_node
*var
, *init
, *rhs
;
1860 assert(oper
->num_children
== 1);
1861 var
= new_var(A
, oper
, oper
->a_id
);
1863 RETURN_ERROR2("Undefined variable:", varName
, 0);
1865 /* XXX make copy of this initializer? */
1866 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1868 init
= new_node2(IR_MOVE
, var
, rhs
);
1869 /*assert(rhs->Opcode != IR_SEQ);*/
1870 n
= new_seq(varDecl
, init
);
1872 else if (v
->initializer
) {
1873 slang_ir_node
*var
, *init
, *rhs
;
1874 var
= new_var(A
, oper
, oper
->a_id
);
1876 RETURN_ERROR2("Undefined variable:", varName
, 0);
1879 /* XXX make copy of this initializer? */
1881 slang_operation dup
;
1882 slang_operation_construct(&dup
);
1883 slang_operation_copy(&dup
, v
->initializer
);
1884 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1885 rhs
= _slang_gen_operation(A
, &dup
);
1888 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1889 rhs
= _slang_gen_operation(A
, v
->initializer
);
1892 init
= new_node2(IR_MOVE
, var
, rhs
);
1894 assert(rhs->Opcode != IR_SEQ);
1896 n
= new_seq(varDecl
, init
);
1906 * Generate IR tree for a variable (such as in an expression).
1908 static slang_ir_node
*
1909 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1911 /* If there's a variable associated with this oper (from inlining)
1912 * use it. Otherwise, use the oper's var id.
1914 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1915 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1917 RETURN_ERROR2("Undefined variable:", (char *) aVar
, 0);
1924 * Some write-masked assignments are simple, but others are hard.
1927 * v.xy = vec2(a, b);
1930 * v.yz = vec2(a, b);
1931 * this would have to be transformed/swizzled into:
1932 * v.yz = vec2(a, b).*xy* (* = don't care)
1933 * Instead, we'll effectively do this:
1934 * v.y = vec2(a, b).xxxx;
1935 * v.z = vec2(a, b).yyyy;
1939 _slang_simple_writemask(GLuint writemask
)
1941 switch (writemask
) {
1948 case WRITEMASK_XYZW
:
1957 * Convert the given swizzle into a writemask. In some cases this
1958 * is trivial, in other cases, we'll need to also swizzle the right
1959 * hand side to put components in the right places.
1960 * \param swizzle the incoming swizzle
1961 * \param writemaskOut returns the writemask
1962 * \param swizzleOut swizzle to apply to the right-hand-side
1963 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1966 swizzle_to_writemask(GLuint swizzle
,
1967 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1969 GLuint mask
= 0x0, newSwizzle
[4];
1972 /* make new dst writemask, compute size */
1973 for (i
= 0; i
< 4; i
++) {
1974 const GLuint swz
= GET_SWZ(swizzle
, i
);
1975 if (swz
== SWIZZLE_NIL
) {
1979 assert(swz
>= 0 && swz
<= 3);
1982 assert(mask
<= 0xf);
1983 size
= i
; /* number of components in mask/swizzle */
1985 *writemaskOut
= mask
;
1987 /* make new src swizzle, by inversion */
1988 for (i
= 0; i
< 4; i
++) {
1989 newSwizzle
[i
] = i
; /*identity*/
1991 for (i
= 0; i
< size
; i
++) {
1992 const GLuint swz
= GET_SWZ(swizzle
, i
);
1993 newSwizzle
[swz
] = i
;
1995 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
2000 if (_slang_simple_writemask(mask
)) {
2002 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
2004 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
2006 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
2008 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
2016 static slang_ir_node
*
2017 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2019 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2021 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
2022 n
->Store
->Swizzle
= swizzle
;
2029 * Generate IR tree for an assignment (=).
2031 static slang_ir_node
*
2032 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2034 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2035 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2036 /* Special case of: x = f(a, b)
2037 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2039 * XXX this could be even more effective if we could accomodate
2040 * cases such as "v.x = f();" - would help with typical vertex
2044 n
= _slang_gen_function_call_name(A
,
2045 (const char *) oper
->children
[1].a_id
,
2046 &oper
->children
[1], &oper
->children
[0]);
2050 slang_ir_node
*n
, *lhs
, *rhs
;
2051 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2052 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2054 /* convert lhs swizzle into writemask */
2055 GLuint writemask
, newSwizzle
;
2056 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2057 &writemask
, &newSwizzle
)) {
2058 /* Non-simple writemask, need to swizzle right hand side in
2059 * order to put components into the right place.
2061 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2063 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2064 n
->Writemask
= writemask
;
2075 * Generate IR tree for referencing a field in a struct (or basic vector type)
2077 static slang_ir_node
*
2078 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2082 slang_typeinfo_construct(&ti
);
2083 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2085 if (_slang_type_is_vector(ti
.spec
.type
)) {
2086 /* the field should be a swizzle */
2087 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2091 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2092 RETURN_ERROR("Bad swizzle", 0);
2094 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2099 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2100 /* create new parent node with swizzle */
2101 n
= _slang_gen_swizzle(n
, swizzle
);
2104 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2105 const GLuint rows
= 1;
2109 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2110 RETURN_ERROR("Bad swizzle", 0);
2112 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2116 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2117 /* create new parent node with swizzle */
2118 n
= _slang_gen_swizzle(n
, swizzle
);
2122 /* the field is a structure member (base.field) */
2123 /* oper->children[0] is the base */
2124 /* oper->a_id is the field name */
2125 slang_ir_node
*base
, *n
;
2126 GLint size
= 4; /* XXX fix? */
2128 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2130 n
= new_node1(IR_FIELD
, base
);
2132 n
->Target
= (char *) oper
->a_id
;
2133 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2140 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2148 * Gen code for array indexing.
2150 static slang_ir_node
*
2151 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2153 slang_typeinfo array_ti
;
2155 /* get array's type info */
2156 slang_typeinfo_construct(&array_ti
);
2157 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2159 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2160 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2161 /* translate the index into a swizzle/writemask: "v.x=p" */
2162 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2166 index
= (GLint
) oper
->children
[1].literal
[0];
2167 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2169 RETURN_ERROR("Invalid array index for vector type", 0);
2172 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2174 /* use swizzle to access the element */
2175 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2179 n
= _slang_gen_swizzle(n
, swizzle
);
2180 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2181 n
->Writemask
= WRITEMASK_X
<< index
;
2186 /* conventional array */
2187 slang_typeinfo elem_ti
;
2188 slang_ir_node
*elem
, *array
, *index
;
2191 /* size of array element */
2192 slang_typeinfo_construct(&elem_ti
);
2193 _slang_typeof_operation(A
, oper
, &elem_ti
);
2194 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2195 assert(elemSize
>= 1);
2197 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2198 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2199 if (array
&& index
) {
2200 elem
= new_node2(IR_ELEMENT
, array
, index
);
2201 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2202 array
->Store
->Index
,
2204 /* XXX try to do some array bounds checking here */
2216 * Generate IR tree for a slang_operation (AST node)
2218 static slang_ir_node
*
2219 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2221 switch (oper
->type
) {
2222 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2226 _slang_push_var_table(A
->vartable
);
2228 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2229 n
= _slang_gen_operation(A
, oper
);
2230 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2232 _slang_pop_var_table(A
->vartable
);
2235 n
= new_node1(IR_SCOPE
, n
);
2240 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2241 /* list of operations */
2242 if (oper
->num_children
> 0)
2244 slang_ir_node
*n
, *tree
= NULL
;
2247 for (i
= 0; i
< oper
->num_children
; i
++) {
2248 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2250 _slang_free_ir_tree(tree
);
2251 return NULL
; /* error must have occured */
2253 tree
= tree
? new_seq(tree
, n
) : n
;
2257 if (oper
->locals
->num_variables
> 0) {
2260 printf("\n****** Deallocate vars in scope!\n");
2262 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2263 slang_variable
*v
= oper
->locals
->variables
+ i
;
2265 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2267 printf(" Deallocate var %s\n", (char*) v->a_name);
2269 assert(store
->File
== PROGRAM_TEMPORARY
);
2270 assert(store
->Index
>= 0);
2271 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2279 case SLANG_OPER_EXPRESSION
:
2280 return _slang_gen_operation(A
, &oper
->children
[0]);
2282 case SLANG_OPER_FOR
:
2283 return _slang_gen_for(A
, oper
);
2285 return _slang_gen_do(A
, oper
);
2286 case SLANG_OPER_WHILE
:
2287 return _slang_gen_while(A
, oper
);
2288 case SLANG_OPER_BREAK
:
2290 RETURN_ERROR("'break' not in loop", 0);
2292 return new_break(A
->CurLoop
);
2293 case SLANG_OPER_CONTINUE
:
2295 RETURN_ERROR("'continue' not in loop", 0);
2297 return new_cont(A
->CurLoop
);
2298 case SLANG_OPER_DISCARD
:
2299 return new_node0(IR_KILL
);
2301 case SLANG_OPER_EQUAL
:
2302 return new_node2(IR_SEQUAL
,
2303 _slang_gen_operation(A
, &oper
->children
[0]),
2304 _slang_gen_operation(A
, &oper
->children
[1]));
2305 case SLANG_OPER_NOTEQUAL
:
2306 return new_node2(IR_SNEQUAL
,
2307 _slang_gen_operation(A
, &oper
->children
[0]),
2308 _slang_gen_operation(A
, &oper
->children
[1]));
2309 case SLANG_OPER_GREATER
:
2310 return new_node2(IR_SGT
,
2311 _slang_gen_operation(A
, &oper
->children
[0]),
2312 _slang_gen_operation(A
, &oper
->children
[1]));
2313 case SLANG_OPER_LESS
:
2314 /* child[0] < child[1] ----> child[1] > child[0] */
2315 return new_node2(IR_SGT
,
2316 _slang_gen_operation(A
, &oper
->children
[1]),
2317 _slang_gen_operation(A
, &oper
->children
[0]));
2318 case SLANG_OPER_GREATERequal
:
2319 return new_node2(IR_SGE
,
2320 _slang_gen_operation(A
, &oper
->children
[0]),
2321 _slang_gen_operation(A
, &oper
->children
[1]));
2322 case SLANG_OPER_LESSequal
:
2323 /* child[0] <= child[1] ----> child[1] >= child[0] */
2324 return new_node2(IR_SGE
,
2325 _slang_gen_operation(A
, &oper
->children
[1]),
2326 _slang_gen_operation(A
, &oper
->children
[0]));
2327 case SLANG_OPER_ADD
:
2330 assert(oper
->num_children
== 2);
2331 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2334 case SLANG_OPER_SUBTRACT
:
2337 assert(oper
->num_children
== 2);
2338 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2341 case SLANG_OPER_MULTIPLY
:
2344 assert(oper
->num_children
== 2);
2345 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2348 case SLANG_OPER_DIVIDE
:
2351 assert(oper
->num_children
== 2);
2352 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2355 case SLANG_OPER_MINUS
:
2358 assert(oper
->num_children
== 1);
2359 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2362 case SLANG_OPER_PLUS
:
2363 /* +expr --> do nothing */
2364 return _slang_gen_operation(A
, &oper
->children
[0]);
2365 case SLANG_OPER_VARIABLE_DECL
:
2366 return _slang_gen_declaration(A
, oper
);
2367 case SLANG_OPER_ASSIGN
:
2368 return _slang_gen_assignment(A
, oper
);
2369 case SLANG_OPER_ADDASSIGN
:
2372 assert(oper
->num_children
== 2);
2373 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2376 case SLANG_OPER_SUBASSIGN
:
2379 assert(oper
->num_children
== 2);
2380 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2384 case SLANG_OPER_MULASSIGN
:
2387 assert(oper
->num_children
== 2);
2388 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2391 case SLANG_OPER_DIVASSIGN
:
2394 assert(oper
->num_children
== 2);
2395 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2398 case SLANG_OPER_LOGICALAND
:
2401 assert(oper
->num_children
== 2);
2402 n
= _slang_gen_logical_and(A
, oper
);
2405 case SLANG_OPER_LOGICALOR
:
2408 assert(oper
->num_children
== 2);
2409 n
= _slang_gen_logical_or(A
, oper
);
2412 case SLANG_OPER_LOGICALXOR
:
2415 assert(oper
->num_children
== 2);
2416 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2419 case SLANG_OPER_NOT
:
2422 assert(oper
->num_children
== 1);
2423 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2427 case SLANG_OPER_SELECT
: /* b ? x : y */
2430 assert(oper
->num_children
== 3);
2431 n
= _slang_gen_select(A
, oper
);
2435 case SLANG_OPER_ASM
:
2436 return _slang_gen_asm(A
, oper
, NULL
);
2437 case SLANG_OPER_CALL
:
2438 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2440 case SLANG_OPER_RETURN
:
2441 return _slang_gen_return(A
, oper
);
2442 case SLANG_OPER_GOTO
:
2443 return new_jump(oper
->label
);
2444 case SLANG_OPER_LABEL
:
2445 return new_label(oper
->label
);
2446 case SLANG_OPER_IDENTIFIER
:
2447 return _slang_gen_variable(A
, oper
);
2449 return _slang_gen_hl_if(A
, oper
);
2450 case SLANG_OPER_FIELD
:
2451 return _slang_gen_field(A
, oper
);
2452 case SLANG_OPER_SUBSCRIPT
:
2453 return _slang_gen_subscript(A
, oper
);
2454 case SLANG_OPER_LITERAL_FLOAT
:
2456 case SLANG_OPER_LITERAL_INT
:
2458 case SLANG_OPER_LITERAL_BOOL
:
2459 return new_float_literal(oper
->literal
);
2461 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2464 assert(oper
->num_children
== 1);
2465 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2468 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2471 assert(oper
->num_children
== 1);
2472 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2475 case SLANG_OPER_PREINCREMENT
: /* ++var */
2478 assert(oper
->num_children
== 1);
2479 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2482 case SLANG_OPER_PREDECREMENT
: /* --var */
2485 assert(oper
->num_children
== 1);
2486 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2490 case SLANG_OPER_SEQUENCE
:
2492 slang_ir_node
*tree
= NULL
;
2494 for (i
= 0; i
< oper
->num_children
; i
++) {
2495 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2496 tree
= tree
? new_seq(tree
, n
) : n
;
2501 case SLANG_OPER_NONE
:
2503 case SLANG_OPER_VOID
:
2507 printf("Unhandled node type %d\n", oper
->type
);
2509 return new_node0(IR_NOP
);
2518 * Called by compiler when a global variable has been parsed/compiled.
2519 * Here we examine the variable's type to determine what kind of register
2520 * storage will be used.
2522 * A uniform such as "gl_Position" will become the register specification
2523 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2524 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2526 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2527 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2528 * actual texture unit (as specified by the user calling glUniform1i()).
2531 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2532 slang_unit_type type
)
2534 struct gl_program
*prog
= A
->program
;
2535 const char *varName
= (char *) var
->a_name
;
2536 GLboolean success
= GL_TRUE
;
2538 slang_ir_storage
*store
= NULL
;
2541 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2543 if (texIndex
!= -1) {
2545 * store->File = PROGRAM_SAMPLER
2546 * store->Index = sampler uniform location
2547 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2549 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2550 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2551 if (dbg
) printf("SAMPLER ");
2553 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2554 /* Uniform variable */
2555 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2556 * MAX2(var
->array_len
, 1);
2558 /* user-defined uniform */
2559 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2560 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2563 /* pre-defined uniform, like gl_ModelviewMatrix */
2564 /* We know it's a uniform, but don't allocate storage unless
2567 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2569 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2571 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2572 const GLint size
= 4; /* XXX fix */
2574 /* user-defined varying */
2575 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2576 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2579 /* pre-defined varying, like gl_Color or gl_TexCoord */
2580 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2581 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2583 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2584 assert(index
< FRAG_ATTRIB_MAX
);
2587 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2589 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2590 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2591 assert(index
< VERT_RESULT_MAX
);
2593 if (dbg
) printf("V/F ");
2595 if (dbg
) printf("VARYING ");
2597 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2599 /* user-defined vertex attribute */
2600 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2601 const GLint attr
= -1; /* unknown */
2602 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2605 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2606 VERT_ATTRIB_GENERIC0
+ index
, size
);
2609 /* pre-defined vertex attrib */
2610 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2611 GLint size
= 4; /* XXX? */
2613 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2615 if (dbg
) printf("ATTRIB ");
2617 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2618 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2619 GLint size
= 4; /* XXX? */
2620 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2621 if (dbg
) printf("INPUT ");
2623 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2624 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2625 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2626 GLint size
= 4; /* XXX? */
2627 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2630 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2631 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2632 GLint size
= 4; /* XXX? */
2633 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2635 if (dbg
) printf("OUTPUT ");
2637 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2638 /* pre-defined global constant, like gl_MaxLights */
2639 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2640 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2641 if (dbg
) printf("CONST ");
2644 /* ordinary variable (may be const) */
2647 /* IR node to declare the variable */
2648 n
= _slang_gen_var_decl(A
, var
);
2650 /* IR code for the var's initializer, if present */
2651 if (var
->initializer
) {
2652 slang_ir_node
*lhs
, *rhs
, *init
;
2654 /* Generate IR_MOVE instruction to initialize the variable */
2655 lhs
= new_node0(IR_VAR
);
2657 lhs
->Store
= n
->Store
;
2659 /* constant folding, etc */
2660 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2662 rhs
= _slang_gen_operation(A
, var
->initializer
);
2664 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2665 n
= new_seq(n
, init
);
2668 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
);
2670 _slang_free_ir_tree(n
);
2673 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2674 store
? store
->Index
: -2);
2677 var
->aux
= store
; /* save var's storage info */
2684 * Produce an IR tree from a function AST (fun->body).
2685 * Then call the code emitter to convert the IR tree into gl_program
2689 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2691 slang_ir_node
*n
, *endLabel
;
2692 GLboolean success
= GL_TRUE
;
2694 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2695 /* we only really generate code for main, all other functions get
2698 return GL_TRUE
; /* not an error */
2702 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2705 slang_print_function(fun
, 1);
2708 /* should have been allocated earlier: */
2709 assert(A
->program
->Parameters
);
2710 assert(A
->program
->Varying
);
2711 assert(A
->vartable
);
2713 /* fold constant expressions, etc. */
2714 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2716 A
->CurFunction
= fun
;
2718 /* Create an end-of-function label */
2719 if (!A
->CurFunction
->end_label
)
2720 A
->CurFunction
->end_label
= _slang_label_new("__endOfFunc__main");
2722 /* push new vartable scope */
2723 _slang_push_var_table(A
->vartable
);
2725 /* Generate IR tree for the function body code */
2726 n
= _slang_gen_operation(A
, fun
->body
);
2728 n
= new_node1(IR_SCOPE
, n
);
2730 /* pop vartable, restore previous */
2731 _slang_pop_var_table(A
->vartable
);
2734 /* XXX record error */
2738 /* append an end-of-function-label to IR tree */
2739 endLabel
= new_label(fun
->end_label
);
2740 n
= new_seq(n
, endLabel
);
2742 A
->CurFunction
= NULL
;
2745 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2746 slang_print_function(fun
, 1);
2749 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2750 slang_print_ir(n
, 0);
2753 printf("************* End codegen function ************\n\n");
2756 /* Emit program instructions */
2757 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
);
2758 _slang_free_ir_tree(n
);
2760 /* free codegen context */
2762 _mesa_free(A->codegen);