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.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
44 #include "prog_instruction.h"
45 #include "prog_parameter.h"
46 #include "prog_statevars.h"
47 #include "slang_typeinfo.h"
48 #include "slang_codegen.h"
49 #include "slang_compile.h"
50 #include "slang_label.h"
51 #include "slang_simplify.h"
52 #include "slang_emit.h"
53 #include "slang_vartable.h"
55 #include "slang_print.h"
58 static slang_ir_node
*
59 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
63 is_sampler_type(const slang_fully_specified_type
*t
)
65 switch (t
->specifier
.type
) {
66 case SLANG_SPEC_SAMPLER1D
:
67 case SLANG_SPEC_SAMPLER2D
:
68 case SLANG_SPEC_SAMPLER3D
:
69 case SLANG_SPEC_SAMPLERCUBE
:
70 case SLANG_SPEC_SAMPLER1DSHADOW
:
71 case SLANG_SPEC_SAMPLER2DSHADOW
:
80 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
87 case SLANG_SPEC_BVEC2
:
89 case SLANG_SPEC_BVEC3
:
91 case SLANG_SPEC_BVEC4
:
95 case SLANG_SPEC_IVEC2
:
97 case SLANG_SPEC_IVEC3
:
99 case SLANG_SPEC_IVEC4
:
101 case SLANG_SPEC_FLOAT
:
103 case SLANG_SPEC_VEC2
:
105 case SLANG_SPEC_VEC3
:
107 case SLANG_SPEC_VEC4
:
109 case SLANG_SPEC_MAT2
:
111 case SLANG_SPEC_MAT3
:
113 case SLANG_SPEC_MAT4
:
115 case SLANG_SPEC_SAMPLER1D
:
116 case SLANG_SPEC_SAMPLER2D
:
117 case SLANG_SPEC_SAMPLER3D
:
118 case SLANG_SPEC_SAMPLERCUBE
:
119 case SLANG_SPEC_SAMPLER1DSHADOW
:
120 case SLANG_SPEC_SAMPLER2DSHADOW
:
121 return 1; /* special case */
122 case SLANG_SPEC_STRUCT
:
125 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
126 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
127 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
128 /* XXX verify padding */
135 case SLANG_SPEC_ARRAY
:
136 return _slang_sizeof_type_specifier(spec
->_array
);
138 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
146 * Establish the binding between a slang_ir_node and a slang_variable.
147 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
148 * The IR node must be a IR_VAR or IR_VAR_DECL node.
149 * \param n the IR node
150 * \param var the variable to associate with the IR node
153 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
157 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
158 assert(!n
->Var
|| n
->Var
== var
);
163 /* need to setup storage */
164 if (n
->Var
&& n
->Var
->aux
) {
165 /* node storage info = var storage info */
166 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
169 /* alloc new storage info */
170 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
172 n
->Var
->aux
= n
->Store
;
180 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
181 * or -1 if the type is not a sampler.
184 sampler_to_texture_index(const slang_type_specifier_type type
)
187 case SLANG_SPEC_SAMPLER1D
:
188 return TEXTURE_1D_INDEX
;
189 case SLANG_SPEC_SAMPLER2D
:
190 return TEXTURE_2D_INDEX
;
191 case SLANG_SPEC_SAMPLER3D
:
192 return TEXTURE_3D_INDEX
;
193 case SLANG_SPEC_SAMPLERCUBE
:
194 return TEXTURE_CUBE_INDEX
;
195 case SLANG_SPEC_SAMPLER1DSHADOW
:
196 return TEXTURE_1D_INDEX
; /* XXX fix */
197 case SLANG_SPEC_SAMPLER2DSHADOW
:
198 return TEXTURE_2D_INDEX
; /* XXX fix */
206 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
207 * a vertex or fragment program input variable. Return -1 if the input
209 * XXX return size too
212 _slang_input_index(const char *name
, GLenum target
)
218 static const struct input_info vertInputs
[] = {
219 { "gl_Vertex", VERT_ATTRIB_POS
},
220 { "gl_Normal", VERT_ATTRIB_NORMAL
},
221 { "gl_Color", VERT_ATTRIB_COLOR0
},
222 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
},
223 { "gl_FogCoord", VERT_ATTRIB_FOG
},
224 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
},
225 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
},
226 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
},
227 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
},
228 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
},
229 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
},
230 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
},
231 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
},
234 static const struct input_info fragInputs
[] = {
235 { "gl_FragCoord", FRAG_ATTRIB_WPOS
},
236 { "gl_Color", FRAG_ATTRIB_COL0
},
237 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
},
238 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
},
239 { "gl_TexCoord", FRAG_ATTRIB_TEX0
},
243 const struct input_info
*inputs
244 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
246 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
248 for (i
= 0; inputs
[i
].Name
; i
++) {
249 if (strcmp(inputs
[i
].Name
, name
) == 0) {
251 return inputs
[i
].Attrib
;
259 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
260 * a vertex or fragment program output variable. Return -1 for an invalid
264 _slang_output_index(const char *name
, GLenum target
)
270 static const struct output_info vertOutputs
[] = {
271 { "gl_Position", VERT_RESULT_HPOS
},
272 { "gl_FrontColor", VERT_RESULT_COL0
},
273 { "gl_BackColor", VERT_RESULT_BFC0
},
274 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
275 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
276 { "gl_TexCoord", VERT_RESULT_TEX0
}, /* XXX indexed */
277 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
278 { "gl_PointSize", VERT_RESULT_PSIZ
},
281 static const struct output_info fragOutputs
[] = {
282 { "gl_FragColor", FRAG_RESULT_COLR
},
283 { "gl_FragDepth", FRAG_RESULT_DEPR
},
287 const struct output_info
*outputs
288 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
290 for (i
= 0; outputs
[i
].Name
; i
++) {
291 if (strcmp(outputs
[i
].Name
, name
) == 0) {
293 return outputs
[i
].Attrib
;
301 /**********************************************************************/
305 * Map "_asm foo" to IR_FOO, etc.
310 slang_ir_opcode Opcode
;
311 GLuint HaveRetValue
, NumParams
;
315 static slang_asm_info AsmInfo
[] = {
317 { "vec4_add", IR_ADD
, 1, 2 },
318 { "vec4_subtract", IR_SUB
, 1, 2 },
319 { "vec4_multiply", IR_MUL
, 1, 2 },
320 { "vec4_dot", IR_DOT4
, 1, 2 },
321 { "vec3_dot", IR_DOT3
, 1, 2 },
322 { "vec3_cross", IR_CROSS
, 1, 2 },
323 { "vec4_lrp", IR_LRP
, 1, 3 },
324 { "vec4_min", IR_MIN
, 1, 2 },
325 { "vec4_max", IR_MAX
, 1, 2 },
326 { "vec4_clamp", IR_CLAMP
, 1, 3 },
327 { "vec4_seq", IR_SEQ
, 1, 2 },
328 { "vec4_sge", IR_SGE
, 1, 2 },
329 { "vec4_sgt", IR_SGT
, 1, 2 },
331 { "vec4_floor", IR_FLOOR
, 1, 1 },
332 { "vec4_frac", IR_FRAC
, 1, 1 },
333 { "vec4_abs", IR_ABS
, 1, 1 },
334 { "vec4_negate", IR_NEG
, 1, 1 },
335 { "vec4_ddx", IR_DDX
, 1, 1 },
336 { "vec4_ddy", IR_DDY
, 1, 1 },
337 /* float binary op */
338 { "float_add", IR_ADD
, 1, 2 },
339 { "float_multiply", IR_MUL
, 1, 2 },
340 { "float_divide", IR_DIV
, 1, 2 },
341 { "float_power", IR_POW
, 1, 2 },
342 /* texture / sampler */
343 { "vec4_tex1d", IR_TEX
, 1, 2 },
344 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
345 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
346 { "vec4_tex2d", IR_TEX
, 1, 2 },
347 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
348 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
349 { "vec4_tex3d", IR_TEX
, 1, 2 },
350 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
351 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
352 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
355 { "int_to_float", IR_I_TO_F
, 1, 1 },
356 { "float_to_int", IR_F_TO_I
, 1, 1 },
357 { "float_exp", IR_EXP
, 1, 1 },
358 { "float_exp2", IR_EXP2
, 1, 1 },
359 { "float_log2", IR_LOG2
, 1, 1 },
360 { "float_rsq", IR_RSQ
, 1, 1 },
361 { "float_rcp", IR_RCP
, 1, 1 },
362 { "float_sine", IR_SIN
, 1, 1 },
363 { "float_cosine", IR_COS
, 1, 1 },
364 { "float_noise1", IR_NOISE1
, 1, 1},
365 { "float_noise2", IR_NOISE2
, 1, 1},
366 { "float_noise3", IR_NOISE3
, 1, 1},
367 { "float_noise4", IR_NOISE4
, 1, 1},
369 { NULL
, IR_NOP
, 0, 0 }
374 * Recursively free an IR tree.
377 _slang_free_ir_tree(slang_ir_node
*n
)
383 for (i
= 0; i
< 3; i
++)
384 _slang_free_ir_tree(n
->Children
[i
]);
385 /* Do not free n->BranchNode since it's a child elsewhere */
391 static slang_ir_node
*
392 new_node3(slang_ir_opcode op
,
393 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
395 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
401 n
->Writemask
= WRITEMASK_XYZW
;
402 n
->InstLocation
= -1;
407 static slang_ir_node
*
408 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
410 return new_node3(op
, c0
, c1
, NULL
);
413 static slang_ir_node
*
414 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
416 return new_node3(op
, c0
, NULL
, NULL
);
419 static slang_ir_node
*
420 new_node0(slang_ir_opcode op
)
422 return new_node3(op
, NULL
, NULL
, NULL
);
426 static slang_ir_node
*
427 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
433 return new_node2(IR_SEQ
, left
, right
);
436 static slang_ir_node
*
437 new_label(slang_label
*label
)
439 slang_ir_node
*n
= new_node0(IR_LABEL
);
446 static slang_ir_node
*
447 new_float_literal(const float v
[4])
449 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
450 slang_ir_node
*n
= new_node0(IR_FLOAT
);
451 COPY_4V(n
->Value
, v
);
452 /* allocate a storage object, but compute actual location (Index) later */
453 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
459 * \param zeroOrOne indicates if the jump is to be taken on zero, or non-zero
460 * condition code state.
462 static slang_ir_node
*
463 new_cjump(slang_label
*dest
, GLuint zeroOrOne
)
465 slang_ir_node
*n
= new_node0(zeroOrOne
? IR_CJUMP1
: IR_CJUMP0
);
472 * Unconditional jump.
474 static slang_ir_node
*
475 new_jump(slang_label
*dest
)
477 slang_ir_node
*n
= new_node0(IR_JUMP
);
485 static slang_ir_node
*
486 new_loop(slang_ir_node
*body
)
488 return new_node1(IR_LOOP
, body
);
492 static slang_ir_node
*
493 new_break(slang_ir_node
*loopNode
)
495 slang_ir_node
*n
= new_node0(IR_BREAK
);
497 assert(loopNode
->Opcode
== IR_LOOP
);
499 /* insert this node at head of linked list */
500 n
->BranchNode
= loopNode
->BranchNode
;
501 loopNode
->BranchNode
= n
;
508 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
510 static slang_ir_node
*
511 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
515 assert(loopNode
->Opcode
== IR_LOOP
);
516 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
518 /* insert this node at head of linked list */
519 n
->BranchNode
= loopNode
->BranchNode
;
520 loopNode
->BranchNode
= n
;
527 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
529 static slang_ir_node
*
530 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
534 assert(loopNode
->Opcode
== IR_LOOP
);
535 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
537 /* insert this node at head of linked list */
538 n
->BranchNode
= loopNode
->BranchNode
;
539 loopNode
->BranchNode
= n
;
545 static slang_ir_node
*
546 new_cont(slang_ir_node
*loopNode
)
548 slang_ir_node
*n
= new_node0(IR_CONT
);
550 assert(loopNode
->Opcode
== IR_LOOP
);
552 /* insert this node at head of linked list */
553 n
->BranchNode
= loopNode
->BranchNode
;
554 loopNode
->BranchNode
= n
;
560 static slang_ir_node
*
561 new_cond(slang_ir_node
*n
)
563 slang_ir_node
*c
= new_node1(IR_COND
, n
);
568 static slang_ir_node
*
569 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
571 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
576 * New IR_VAR node - a reference to a previously declared variable.
578 static slang_ir_node
*
579 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
582 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
586 assert(!oper
->var
|| oper
->var
== var
);
588 n
= new_node0(IR_VAR
);
590 _slang_attach_storage(n
, var
);
597 * Check if the given function is really just a wrapper for a
598 * basic assembly instruction.
601 slang_is_asm_function(const slang_function
*fun
)
603 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
604 fun
->body
->num_children
== 1 &&
605 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
613 _slang_is_noop(const slang_operation
*oper
)
616 oper
->type
== SLANG_OPER_VOID
||
617 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
625 * Produce inline code for a call to an assembly instruction.
627 static slang_operation
*
628 slang_inline_asm_function(slang_assemble_ctx
*A
,
629 slang_function
*fun
, slang_operation
*oper
)
631 const GLuint numArgs
= oper
->num_children
;
632 const slang_operation
*args
= oper
->children
;
634 slang_operation
*inlined
= slang_operation_new(1);
636 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
638 printf("Inline asm %s\n", (char*) fun->header.a_name);
640 inlined
->type
= fun
->body
->children
[0].type
;
641 inlined
->a_id
= fun
->body
->children
[0].a_id
;
642 inlined
->num_children
= numArgs
;
643 inlined
->children
= slang_operation_new(numArgs
);
644 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
646 for (i
= 0; i
< numArgs
; i
++) {
647 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
655 slang_resolve_variable(slang_operation
*oper
)
657 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
658 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
664 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
667 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
668 GLuint substCount
, slang_variable
**substOld
,
669 slang_operation
**substNew
, GLboolean isLHS
)
671 switch (oper
->type
) {
672 case SLANG_OPER_VARIABLE_DECL
:
674 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
675 oper
->a_id
, GL_TRUE
);
677 if (v
->initializer
&& oper
->num_children
== 0) {
678 /* set child of oper to copy of initializer */
679 oper
->num_children
= 1;
680 oper
->children
= slang_operation_new(1);
681 slang_operation_copy(&oper
->children
[0], v
->initializer
);
683 if (oper
->num_children
== 1) {
684 /* the initializer */
685 slang_substitute(A
, &oper
->children
[0], substCount
,
686 substOld
, substNew
, GL_FALSE
);
690 case SLANG_OPER_IDENTIFIER
:
691 assert(oper
->num_children
== 0);
692 if (1/**!isLHS XXX FIX */) {
693 slang_atom id
= oper
->a_id
;
696 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
698 printf("var %s not found!\n", (char *) oper
->a_id
);
699 _slang_print_var_scope(oper
->locals
, 6);
705 /* look for a substitution */
706 for (i
= 0; i
< substCount
; i
++) {
707 if (v
== substOld
[i
]) {
708 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
709 #if 0 /* DEBUG only */
710 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
711 assert(substNew
[i
]->var
);
712 assert(substNew
[i
]->var
->a_name
);
713 printf("Substitute %s with %s in id node %p\n",
714 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
718 printf("Substitute %s with %f in id node %p\n",
719 (char*)v
->a_name
, substNew
[i
]->literal
[0],
723 slang_operation_copy(oper
, substNew
[i
]);
730 case SLANG_OPER_RETURN
:
731 /* do return replacement here too */
732 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
733 if (!_slang_is_noop(oper
)) {
739 * then do substitutions on the assignment.
741 slang_operation
*blockOper
, *assignOper
, *returnOper
;
742 blockOper
= slang_operation_new(1);
743 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
744 blockOper
->num_children
= 2;
745 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
746 blockOper
->children
= slang_operation_new(2);
747 assignOper
= blockOper
->children
+ 0;
748 returnOper
= blockOper
->children
+ 1;
750 assignOper
->type
= SLANG_OPER_ASSIGN
;
751 assignOper
->num_children
= 2;
752 assignOper
->locals
->outer_scope
= blockOper
->locals
;
753 assignOper
->children
= slang_operation_new(2);
754 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
755 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
756 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
758 slang_operation_copy(&assignOper
->children
[1],
761 returnOper
->type
= SLANG_OPER_RETURN
;
762 assert(returnOper
->num_children
== 0);
764 /* do substitutions on the "__retVal = expr" sub-tree */
765 slang_substitute(A
, assignOper
,
766 substCount
, substOld
, substNew
, GL_FALSE
);
768 /* install new code */
769 slang_operation_copy(oper
, blockOper
);
770 slang_operation_destruct(blockOper
);
774 case SLANG_OPER_ASSIGN
:
775 case SLANG_OPER_SUBSCRIPT
:
777 * child[0] can't have substitutions but child[1] can.
779 slang_substitute(A
, &oper
->children
[0],
780 substCount
, substOld
, substNew
, GL_TRUE
);
781 slang_substitute(A
, &oper
->children
[1],
782 substCount
, substOld
, substNew
, GL_FALSE
);
784 case SLANG_OPER_FIELD
:
786 slang_substitute(A
, &oper
->children
[0],
787 substCount
, substOld
, substNew
, GL_TRUE
);
792 for (i
= 0; i
< oper
->num_children
; i
++)
793 slang_substitute(A
, &oper
->children
[i
],
794 substCount
, substOld
, substNew
, GL_FALSE
);
802 * Inline the given function call operation.
803 * Return a new slang_operation that corresponds to the inlined code.
805 static slang_operation
*
806 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
807 slang_operation
*oper
, slang_operation
*returnOper
)
814 ParamMode
*paramMode
;
815 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
816 const GLuint numArgs
= oper
->num_children
;
817 const GLuint totalArgs
= numArgs
+ haveRetValue
;
818 slang_operation
*args
= oper
->children
;
819 slang_operation
*inlined
, *top
;
820 slang_variable
**substOld
;
821 slang_operation
**substNew
;
822 GLuint substCount
, numCopyIn
, i
;
824 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
825 assert(fun
->param_count
== totalArgs
);
827 /* allocate temporary arrays */
828 paramMode
= (ParamMode
*)
829 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
830 substOld
= (slang_variable
**)
831 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
832 substNew
= (slang_operation
**)
833 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
836 printf("Inline call to %s (total vars=%d nparams=%d)\n",
837 (char *) fun
->header
.a_name
,
838 fun
->parameters
->num_variables
, numArgs
);
841 if (haveRetValue
&& !returnOper
) {
842 /* Create 3-child comma sequence for inlined code:
843 * child[0]: declare __resultTmp
844 * child[1]: inlined function body
845 * child[2]: __resultTmp
847 slang_operation
*commaSeq
;
848 slang_operation
*declOper
= NULL
;
849 slang_variable
*resultVar
;
851 commaSeq
= slang_operation_new(1);
852 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
853 assert(commaSeq
->locals
);
854 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
855 commaSeq
->num_children
= 3;
856 commaSeq
->children
= slang_operation_new(3);
857 /* allocate the return var */
858 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
860 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
861 (void*)commaSeq->locals, (char *) fun->header.a_name);
864 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
865 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
866 resultVar
->isTemp
= GL_TRUE
;
868 /* child[0] = __resultTmp declaration */
869 declOper
= &commaSeq
->children
[0];
870 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
871 declOper
->a_id
= resultVar
->a_name
;
872 declOper
->locals
->outer_scope
= commaSeq
->locals
;
874 /* child[1] = function body */
875 inlined
= &commaSeq
->children
[1];
876 inlined
->locals
->outer_scope
= commaSeq
->locals
;
878 /* child[2] = __resultTmp reference */
879 returnOper
= &commaSeq
->children
[2];
880 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
881 returnOper
->a_id
= resultVar
->a_name
;
882 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
887 top
= inlined
= slang_operation_new(1);
888 /* XXXX this may be inappropriate!!!! */
889 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
893 assert(inlined
->locals
);
895 /* Examine the parameters, look for inout/out params, look for possible
896 * substitutions, etc:
897 * param type behaviour
898 * in copy actual to local
899 * const in substitute param with actual
903 for (i
= 0; i
< totalArgs
; i
++) {
904 slang_variable
*p
= fun
->parameters
->variables
[i
];
906 printf("Param %d: %s %s \n", i,
907 slang_type_qual_string(p->type.qualifier),
910 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
911 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
912 /* an output param */
913 slang_operation
*arg
;
918 paramMode
[i
] = SUBST
;
920 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
921 slang_resolve_variable(arg
);
923 /* replace parameter 'p' with argument 'arg' */
924 substOld
[substCount
] = p
;
925 substNew
[substCount
] = arg
; /* will get copied */
928 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
929 /* a constant input param */
930 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
931 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
932 /* replace all occurances of this parameter variable with the
933 * actual argument variable or a literal.
935 paramMode
[i
] = SUBST
;
936 slang_resolve_variable(&args
[i
]);
937 substOld
[substCount
] = p
;
938 substNew
[substCount
] = &args
[i
]; /* will get copied */
942 paramMode
[i
] = COPY_IN
;
946 paramMode
[i
] = COPY_IN
;
948 assert(paramMode
[i
]);
951 /* actual code inlining: */
952 slang_operation_copy(inlined
, fun
->body
);
954 /*** XXX review this */
955 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
956 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
959 printf("======================= orig body code ======================\n");
960 printf("=== params scope = %p\n", (void*) fun
->parameters
);
961 slang_print_tree(fun
->body
, 8);
962 printf("======================= copied code =========================\n");
963 slang_print_tree(inlined
, 8);
966 /* do parameter substitution in inlined code: */
967 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
970 printf("======================= subst code ==========================\n");
971 slang_print_tree(inlined
, 8);
972 printf("=============================================================\n");
975 /* New prolog statements: (inserted before the inlined code)
976 * Copy the 'in' arguments.
979 for (i
= 0; i
< numArgs
; i
++) {
980 if (paramMode
[i
] == COPY_IN
) {
981 slang_variable
*p
= fun
->parameters
->variables
[i
];
982 /* declare parameter 'p' */
983 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
987 printf("COPY_IN %s from expr\n", (char*)p->a_name);
989 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
990 assert(decl
->locals
);
991 decl
->locals
->outer_scope
= inlined
->locals
;
992 decl
->a_id
= p
->a_name
;
993 decl
->num_children
= 1;
994 decl
->children
= slang_operation_new(1);
996 /* child[0] is the var's initializer */
997 slang_operation_copy(&decl
->children
[0], args
+ i
);
1003 /* New epilog statements:
1004 * 1. Create end of function label to jump to from return statements.
1005 * 2. Copy the 'out' parameter vars
1008 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1010 inlined
->num_children
);
1011 lab
->type
= SLANG_OPER_LABEL
;
1012 lab
->label
= A
->curFuncEndLabel
;
1015 for (i
= 0; i
< totalArgs
; i
++) {
1016 if (paramMode
[i
] == COPY_OUT
) {
1017 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1018 /* actualCallVar = outParam */
1019 /*if (i > 0 || !haveRetValue)*/
1020 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1022 inlined
->num_children
);
1023 ass
->type
= SLANG_OPER_ASSIGN
;
1024 ass
->num_children
= 2;
1025 ass
->locals
->outer_scope
= inlined
->locals
;
1026 ass
->children
= slang_operation_new(2);
1027 ass
->children
[0] = args
[i
]; /*XXX copy */
1028 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1029 ass
->children
[1].a_id
= p
->a_name
;
1030 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1034 _mesa_free(paramMode
);
1035 _mesa_free(substOld
);
1036 _mesa_free(substNew
);
1039 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1040 (char *) fun
->header
.a_name
,
1041 fun
->parameters
->num_variables
, numArgs
);
1042 slang_print_tree(top
, 0);
1048 static slang_ir_node
*
1049 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1050 slang_operation
*oper
, slang_operation
*dest
)
1053 slang_operation
*inlined
;
1054 slang_label
*prevFuncEndLabel
;
1057 prevFuncEndLabel
= A
->curFuncEndLabel
;
1058 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1059 A
->curFuncEndLabel
= _slang_label_new(name
);
1060 assert(A
->curFuncEndLabel
);
1062 if (slang_is_asm_function(fun
) && !dest
) {
1063 /* assemble assembly function - tree style */
1064 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1067 /* non-assembly function */
1068 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1071 /* Replace the function call with the inlined block */
1073 slang_operation_construct(oper
);
1074 slang_operation_copy(oper
, inlined
);
1081 assert(inlined
->locals
);
1082 printf("*** Inlined code for call to %s:\n",
1083 (char*) fun
->header
.a_name
);
1084 slang_print_tree(oper
, 10);
1088 n
= _slang_gen_operation(A
, oper
);
1090 /*_slang_label_delete(A->curFuncEndLabel);*/
1091 A
->curFuncEndLabel
= prevFuncEndLabel
;
1092 assert(A
->curFuncEndLabel
);
1098 static slang_asm_info
*
1099 slang_find_asm_info(const char *name
)
1102 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1103 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1112 make_writemask(const char *field
)
1118 mask
|= WRITEMASK_X
;
1121 mask
|= WRITEMASK_Y
;
1124 mask
|= WRITEMASK_Z
;
1127 mask
|= WRITEMASK_W
;
1135 return WRITEMASK_XYZW
;
1142 * Generate IR tree for an asm instruction/operation such as:
1143 * __asm vec4_dot __retVal.x, v1, v2;
1145 static slang_ir_node
*
1146 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1147 slang_operation
*dest
)
1149 const slang_asm_info
*info
;
1150 slang_ir_node
*kids
[3], *n
;
1151 GLuint j
, firstOperand
;
1153 assert(oper
->type
== SLANG_OPER_ASM
);
1155 info
= slang_find_asm_info((char *) oper
->a_id
);
1157 _mesa_problem(NULL
, "undefined __asm function %s\n",
1158 (char *) oper
->a_id
);
1161 assert(info
->NumParams
<= 3);
1163 if (info
->NumParams
== oper
->num_children
) {
1164 /* Storage for result is not specified.
1165 * Children[0], [1] are the operands.
1170 /* Storage for result (child[0]) is specified.
1171 * Children[1], [2] are the operands.
1176 /* assemble child(ren) */
1177 kids
[0] = kids
[1] = kids
[2] = NULL
;
1178 for (j
= 0; j
< info
->NumParams
; j
++) {
1179 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1182 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1185 /* Setup n->Store to be a particular location. Otherwise, storage
1186 * for the result (a temporary) will be allocated later.
1188 GLuint writemask
= WRITEMASK_XYZW
;
1189 slang_operation
*dest_oper
;
1192 dest_oper
= &oper
->children
[0];
1193 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1195 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1196 dest_oper
= &dest_oper
->children
[0];
1199 n0
= _slang_gen_operation(A
, dest_oper
);
1203 n
->Store
= n0
->Store
;
1204 n
->Writemask
= writemask
;
1214 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1217 for (i
= 0; i
< scope
->num_functions
; i
++) {
1218 slang_function
*f
= &scope
->functions
[i
];
1219 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1220 printf(" %s (%d args)\n", name
, f
->param_count
);
1223 if (scope
->outer_scope
)
1224 print_funcs(scope
->outer_scope
, name
);
1229 * Return first function in the scope that has the given name.
1230 * This is the function we'll try to call when there is no exact match
1231 * between function parameters and call arguments.
1233 * XXX we should really create a list of candidate functions and try
1236 static slang_function
*
1237 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1240 for (i
= 0; i
< scope
->num_functions
; i
++) {
1241 slang_function
*f
= &scope
->functions
[i
];
1242 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1245 if (scope
->outer_scope
)
1246 return _slang_first_function(scope
->outer_scope
, name
);
1253 * Assemble a function call, given a particular function name.
1254 * \param name the function's name (operators like '*' are possible).
1256 static slang_ir_node
*
1257 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1258 slang_operation
*oper
, slang_operation
*dest
)
1260 slang_operation
*params
= oper
->children
;
1261 const GLuint param_count
= oper
->num_children
;
1263 slang_function
*fun
;
1265 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1266 if (atom
== SLANG_ATOM_NULL
)
1270 * Use 'name' to find the function to call
1272 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1273 &A
->space
, A
->atoms
, A
->log
);
1275 /* A function with exactly the right parameters/types was not found.
1276 * Try adapting the parameters.
1278 fun
= _slang_first_function(A
->space
.funcs
, name
);
1279 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1280 slang_info_log_error(A
->log
, "Undefined function '%s'", name
);
1286 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1291 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1293 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1294 oper
->type
== SLANG_OPER_LITERAL_INT
||
1295 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1296 if (oper
->literal
[0])
1302 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1303 oper
->num_children
== 1) {
1304 return _slang_is_constant_cond(&oper
->children
[0], value
);
1312 * Generate loop code using high-level IR_LOOP instruction
1314 static slang_ir_node
*
1315 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1319 * BREAK if !expr (child[0])
1320 * body code (child[1])
1322 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1323 GLboolean isConst
, constTrue
;
1325 /* Check if loop condition is a constant */
1326 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1328 if (isConst
&& !constTrue
) {
1329 /* loop is never executed! */
1330 return new_node0(IR_NOP
);
1333 loop
= new_loop(NULL
);
1335 /* save old, push new loop */
1336 prevLoop
= A
->CurLoop
;
1339 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1340 if (isConst
&& constTrue
) {
1341 /* while(nonzero constant), no conditional break */
1345 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1347 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1348 loop
->Children
[0] = new_seq(breakIf
, body
);
1350 /* Do infinite loop detection */
1351 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1352 /* infinite loop detected */
1353 A
->CurLoop
= prevLoop
; /* clean-up */
1354 slang_info_log_error(A
->log
, "Infinite loop detected!");
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
);
1443 * Determine if the given operation is of a specific type.
1446 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1448 if (oper
->type
== type
)
1450 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1451 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1452 oper
->num_children
== 1)
1453 return is_operation_type(&oper
->children
[0], type
);
1460 * Generate IR tree for an if/then/else conditional using high-level
1461 * IR_IF instruction.
1463 static slang_ir_node
*
1464 _slang_gen_hl_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1467 * eval expr (child[0]), updating condcodes
1474 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1475 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1477 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1478 cond
= new_cond(cond
);
1480 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1481 /* Special case: generate a conditional break */
1482 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1483 if (haveElseClause
) {
1484 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1485 return new_seq(ifBody
, elseBody
);
1489 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1490 /* Special case: generate a conditional break */
1491 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1492 if (haveElseClause
) {
1493 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1494 return new_seq(ifBody
, elseBody
);
1500 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1502 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1505 ifNode
= new_if(cond
, ifBody
, elseBody
);
1513 * Generate IR node for storage of a temporary of given size.
1515 static slang_ir_node
*
1516 _slang_gen_temporary(GLint size
)
1518 slang_ir_storage
*store
;
1521 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1523 n
= new_node0(IR_VAR_DECL
);
1536 * Generate IR node for allocating/declaring a variable.
1538 static slang_ir_node
*
1539 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1542 assert(!is_sampler_type(&var
->type
));
1543 n
= new_node0(IR_VAR_DECL
);
1545 _slang_attach_storage(n
, var
);
1548 assert(n
->Store
== var
->aux
);
1550 assert(n
->Store
->Index
< 0);
1552 n
->Store
->File
= PROGRAM_TEMPORARY
;
1553 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1554 assert(n
->Store
->Size
> 0);
1561 * Generate code for a selection expression: b ? x : y
1562 * XXX in some cases we could implement a selection expression
1563 * with an LRP instruction (use the boolean as the interpolant).
1565 static slang_ir_node
*
1566 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1568 slang_label
*altLabel
, *endLabel
;
1569 slang_ir_node
*altLab
, *endLab
;
1570 slang_ir_node
*tree
, *tmpDecl
, *tmpVar
, *cond
, *cjump
, *jump
;
1571 slang_ir_node
*bodx
, *body
, *assignx
, *assigny
;
1572 slang_typeinfo type
;
1575 assert(oper
->type
== SLANG_OPER_SELECT
);
1576 assert(oper
->num_children
== 3);
1578 altLabel
= _slang_label_new("selectAlt");
1579 endLabel
= _slang_label_new("selectEnd");
1581 /* size of x or y's type */
1582 slang_typeinfo_construct(&type
);
1583 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1584 size
= _slang_sizeof_type_specifier(&type
.spec
);
1588 tmpDecl
= _slang_gen_temporary(size
);
1590 /* eval condition */
1591 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1592 cond
= new_cond(cond
);
1593 tree
= new_seq(tmpDecl
, cond
);
1595 /* jump if false to "alt" label */
1596 cjump
= new_cjump(altLabel
, 0);
1597 tree
= new_seq(tree
, cjump
);
1599 /* evaluate child 1 (x) and assign to tmp */
1600 tmpVar
= new_node0(IR_VAR
);
1601 tmpVar
->Store
= tmpDecl
->Store
;
1602 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1603 assigny
= new_node2(IR_MOVE
, tmpVar
, body
);
1604 tree
= new_seq(tree
, assigny
);
1606 /* jump to "end" label */
1607 jump
= new_jump(endLabel
);
1608 tree
= new_seq(tree
, jump
);
1611 altLab
= new_label(altLabel
);
1612 tree
= new_seq(tree
, altLab
);
1614 /* evaluate child 2 (y) and assign to tmp */
1615 tmpVar
= new_node0(IR_VAR
);
1616 tmpVar
->Store
= tmpDecl
->Store
;
1617 bodx
= _slang_gen_operation(A
, &oper
->children
[2]);
1618 assignx
= new_node2(IR_MOVE
, tmpVar
, bodx
);
1619 tree
= new_seq(tree
, assignx
);
1622 endLab
= new_label(endLabel
);
1623 tree
= new_seq(tree
, endLab
);
1626 tmpVar
= new_node0(IR_VAR
);
1627 tmpVar
->Store
= tmpDecl
->Store
;
1628 tree
= new_seq(tree
, tmpVar
);
1635 * Generate code for &&.
1637 static slang_ir_node
*
1638 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1640 /* rewrite "a && b" as "a ? b : false" */
1641 slang_operation
*select
;
1644 select
= slang_operation_new(1);
1645 select
->type
= SLANG_OPER_SELECT
;
1646 select
->num_children
= 3;
1647 select
->children
= slang_operation_new(3);
1649 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1650 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1651 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1652 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1653 select
->children
[2].literal_size
= 1;
1655 n
= _slang_gen_select(A
, select
);
1658 free(select
->children
);
1666 * Generate code for ||.
1668 static slang_ir_node
*
1669 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1671 /* rewrite "a || b" as "a ? true : b" */
1672 slang_operation
*select
;
1675 select
= slang_operation_new(1);
1676 select
->type
= SLANG_OPER_SELECT
;
1677 select
->num_children
= 3;
1678 select
->children
= slang_operation_new(3);
1680 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1681 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1682 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1683 select
->children
[1].literal_size
= 1;
1684 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1686 n
= _slang_gen_select(A
, select
);
1689 free(select
->children
);
1697 * Generate IR tree for a return statement.
1699 static slang_ir_node
*
1700 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1702 if (oper
->num_children
== 0 ||
1703 (oper
->num_children
== 1 &&
1704 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1708 * goto __endOfFunction;
1711 slang_operation gotoOp
;
1712 slang_operation_construct(&gotoOp
);
1713 gotoOp
.type
= SLANG_OPER_GOTO
;
1714 gotoOp
.label
= A
->curFuncEndLabel
;
1715 assert(gotoOp
.label
);
1717 /* assemble the new code */
1718 n
= _slang_gen_operation(A
, &gotoOp
);
1719 /* destroy temp code */
1720 slang_operation_destruct(&gotoOp
);
1729 * goto __endOfFunction;
1731 slang_operation
*block
, *assign
, *jump
;
1732 slang_atom a_retVal
;
1735 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1741 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1746 block
= slang_operation_new(1);
1747 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1748 assert(block
->locals
);
1749 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1750 block
->num_children
= 2;
1751 block
->children
= slang_operation_new(2);
1753 /* child[0]: __retVal = expr; */
1754 assign
= &block
->children
[0];
1755 assign
->type
= SLANG_OPER_ASSIGN
;
1756 assign
->locals
->outer_scope
= block
->locals
;
1757 assign
->num_children
= 2;
1758 assign
->children
= slang_operation_new(2);
1759 /* lhs (__retVal) */
1760 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1761 assign
->children
[0].a_id
= a_retVal
;
1762 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1764 /* XXX we might be able to avoid this copy someday */
1765 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1767 /* child[1]: goto __endOfFunction */
1768 jump
= &block
->children
[1];
1769 jump
->type
= SLANG_OPER_GOTO
;
1770 assert(A
->curFuncEndLabel
);
1771 /* XXX don't call function? */
1772 jump
->label
= A
->curFuncEndLabel
;
1773 assert(jump
->label
);
1776 printf("NEW RETURN:\n");
1777 slang_print_tree(block
, 0);
1780 /* assemble the new code */
1781 n
= _slang_gen_operation(A
, block
);
1782 slang_operation_delete(block
);
1789 * Generate IR tree for a variable declaration.
1791 static slang_ir_node
*
1792 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1795 slang_ir_node
*varDecl
;
1797 const char *varName
= (char *) oper
->a_id
;
1799 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1801 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1804 varDecl
= _slang_gen_var_decl(A
, v
);
1806 if (oper
->num_children
> 0) {
1807 /* child is initializer */
1808 slang_ir_node
*var
, *init
, *rhs
;
1809 assert(oper
->num_children
== 1);
1810 var
= new_var(A
, oper
, oper
->a_id
);
1812 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1815 /* XXX make copy of this initializer? */
1816 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1818 init
= new_node2(IR_MOVE
, var
, rhs
);
1819 /*assert(rhs->Opcode != IR_SEQ);*/
1820 n
= new_seq(varDecl
, init
);
1822 else if (v
->initializer
) {
1823 slang_ir_node
*var
, *init
, *rhs
;
1824 var
= new_var(A
, oper
, oper
->a_id
);
1826 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1830 /* XXX make copy of this initializer? */
1832 slang_operation dup
;
1833 slang_operation_construct(&dup
);
1834 slang_operation_copy(&dup
, v
->initializer
);
1835 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1836 rhs
= _slang_gen_operation(A
, &dup
);
1839 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1840 rhs
= _slang_gen_operation(A
, v
->initializer
);
1843 init
= new_node2(IR_MOVE
, var
, rhs
);
1845 assert(rhs->Opcode != IR_SEQ);
1847 n
= new_seq(varDecl
, init
);
1857 * Generate IR tree for a variable (such as in an expression).
1859 static slang_ir_node
*
1860 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1862 /* If there's a variable associated with this oper (from inlining)
1863 * use it. Otherwise, use the oper's var id.
1865 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1866 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1868 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
1876 * Some write-masked assignments are simple, but others are hard.
1879 * v.xy = vec2(a, b);
1882 * v.zy = vec2(a, b);
1883 * this gets transformed/swizzled into:
1884 * v.zy = vec2(a, b).*yx* (* = don't care)
1885 * This function helps to determine simple vs. non-simple.
1888 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1890 switch (writemask
) {
1892 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1894 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1896 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1898 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1900 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1901 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1903 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1904 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1905 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1906 case WRITEMASK_XYZW
:
1907 return swizzle
== SWIZZLE_NOOP
;
1915 * Convert the given swizzle into a writemask. In some cases this
1916 * is trivial, in other cases, we'll need to also swizzle the right
1917 * hand side to put components in the right places.
1918 * \param swizzle the incoming swizzle
1919 * \param writemaskOut returns the writemask
1920 * \param swizzleOut swizzle to apply to the right-hand-side
1921 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1924 swizzle_to_writemask(GLuint swizzle
,
1925 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1927 GLuint mask
= 0x0, newSwizzle
[4];
1930 /* make new dst writemask, compute size */
1931 for (i
= 0; i
< 4; i
++) {
1932 const GLuint swz
= GET_SWZ(swizzle
, i
);
1933 if (swz
== SWIZZLE_NIL
) {
1937 assert(swz
>= 0 && swz
<= 3);
1940 assert(mask
<= 0xf);
1941 size
= i
; /* number of components in mask/swizzle */
1943 *writemaskOut
= mask
;
1945 /* make new src swizzle, by inversion */
1946 for (i
= 0; i
< 4; i
++) {
1947 newSwizzle
[i
] = i
; /*identity*/
1949 for (i
= 0; i
< size
; i
++) {
1950 const GLuint swz
= GET_SWZ(swizzle
, i
);
1951 newSwizzle
[swz
] = i
;
1953 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1958 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1960 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1962 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1964 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1966 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1974 static slang_ir_node
*
1975 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
1977 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
1979 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
1980 n
->Store
->Swizzle
= swizzle
;
1987 * Generate IR tree for an assignment (=).
1989 static slang_ir_node
*
1990 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
1992 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
1993 oper
->children
[1].type
== SLANG_OPER_CALL
) {
1994 /* Special case of: x = f(a, b)
1995 * Replace with f(a, b, x) (where x == hidden __retVal out param)
1997 * XXX this could be even more effective if we could accomodate
1998 * cases such as "v.x = f();" - would help with typical vertex
2002 n
= _slang_gen_function_call_name(A
,
2003 (const char *) oper
->children
[1].a_id
,
2004 &oper
->children
[1], &oper
->children
[0]);
2008 slang_ir_node
*n
, *lhs
, *rhs
;
2009 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2010 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2012 /* convert lhs swizzle into writemask */
2013 GLuint writemask
, newSwizzle
;
2014 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2015 &writemask
, &newSwizzle
)) {
2016 /* Non-simple writemask, need to swizzle right hand side in
2017 * order to put components into the right place.
2019 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2021 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2022 n
->Writemask
= writemask
;
2033 * Generate IR tree for referencing a field in a struct (or basic vector type)
2035 static slang_ir_node
*
2036 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2040 slang_typeinfo_construct(&ti
);
2041 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2043 if (_slang_type_is_vector(ti
.spec
.type
)) {
2044 /* the field should be a swizzle */
2045 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2049 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2050 slang_info_log_error(A
->log
, "Bad swizzle");
2052 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2057 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2058 /* create new parent node with swizzle */
2059 n
= _slang_gen_swizzle(n
, swizzle
);
2062 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2063 const GLuint rows
= 1;
2067 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2068 slang_info_log_error(A
->log
, "Bad swizzle");
2070 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2074 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2075 /* create new parent node with swizzle */
2076 n
= _slang_gen_swizzle(n
, swizzle
);
2080 /* the field is a structure member (base.field) */
2081 /* oper->children[0] is the base */
2082 /* oper->a_id is the field name */
2083 slang_ir_node
*base
, *n
;
2084 GLint size
= 4; /* XXX fix? */
2086 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2088 /* error previously found */
2092 n
= new_node1(IR_FIELD
, base
);
2094 n
->Field
= (char *) oper
->a_id
;
2095 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2102 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2110 * Gen code for array indexing.
2112 static slang_ir_node
*
2113 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2115 slang_typeinfo array_ti
;
2117 /* get array's type info */
2118 slang_typeinfo_construct(&array_ti
);
2119 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2121 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2122 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2123 /* translate the index into a swizzle/writemask: "v.x=p" */
2124 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2128 index
= (GLint
) oper
->children
[1].literal
[0];
2129 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2131 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2135 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2137 /* use swizzle to access the element */
2138 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2142 n
= _slang_gen_swizzle(n
, swizzle
);
2143 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2144 n
->Writemask
= WRITEMASK_X
<< index
;
2149 /* conventional array */
2150 slang_typeinfo elem_ti
;
2151 slang_ir_node
*elem
, *array
, *index
;
2154 /* size of array element */
2155 slang_typeinfo_construct(&elem_ti
);
2156 _slang_typeof_operation(A
, oper
, &elem_ti
);
2157 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2158 if (elemSize
<= 0) {
2159 /* unknown var or type */
2160 slang_info_log_error(A
->log
, "Undefined var or type");
2164 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2165 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2166 if (array
&& index
) {
2167 elem
= new_node2(IR_ELEMENT
, array
, index
);
2168 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2169 array
->Store
->Index
,
2171 /* XXX try to do some array bounds checking here */
2182 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2183 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2184 * Since matrices are stored in column-major order, the second form of
2185 * multiplication is much more efficient (just 4 dot products).
2188 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2190 static const struct {
2192 const char *tranpose
;
2194 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2195 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2196 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2197 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2198 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2202 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2203 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2205 for (i
= 0; matrices
[i
].orig
; i
++) {
2206 if (oper
->children
[0].a_id
2207 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2209 _mesa_printf("Replace %s with %s\n",
2210 matrices[i].orig, matrices[i].tranpose);
2212 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2213 oper
->children
[0].a_id
2214 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2215 /* finally, swap the operands */
2216 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2225 * Generate IR tree for a slang_operation (AST node)
2227 static slang_ir_node
*
2228 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2230 switch (oper
->type
) {
2231 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2235 _slang_push_var_table(A
->vartable
);
2237 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2238 n
= _slang_gen_operation(A
, oper
);
2239 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2241 _slang_pop_var_table(A
->vartable
);
2244 n
= new_node1(IR_SCOPE
, n
);
2249 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2250 /* list of operations */
2251 if (oper
->num_children
> 0)
2253 slang_ir_node
*n
, *tree
= NULL
;
2256 for (i
= 0; i
< oper
->num_children
; i
++) {
2257 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2259 _slang_free_ir_tree(tree
);
2260 return NULL
; /* error must have occured */
2262 tree
= tree
? new_seq(tree
, n
) : n
;
2266 if (oper
->locals
->num_variables
> 0) {
2269 printf("\n****** Deallocate vars in scope!\n");
2271 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2272 slang_variable
*v
= oper
->locals
->variables
+ i
;
2274 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2276 printf(" Deallocate var %s\n", (char*) v->a_name);
2278 assert(store
->File
== PROGRAM_TEMPORARY
);
2279 assert(store
->Index
>= 0);
2280 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2288 case SLANG_OPER_EXPRESSION
:
2289 return _slang_gen_operation(A
, &oper
->children
[0]);
2291 case SLANG_OPER_FOR
:
2292 return _slang_gen_for(A
, oper
);
2294 return _slang_gen_do(A
, oper
);
2295 case SLANG_OPER_WHILE
:
2296 return _slang_gen_while(A
, oper
);
2297 case SLANG_OPER_BREAK
:
2299 slang_info_log_error(A
->log
, "'break' not in loop");
2301 return new_break(A
->CurLoop
);
2302 case SLANG_OPER_CONTINUE
:
2304 slang_info_log_error(A
->log
, "'continue' not in loop");
2306 return new_cont(A
->CurLoop
);
2307 case SLANG_OPER_DISCARD
:
2308 return new_node0(IR_KILL
);
2310 case SLANG_OPER_EQUAL
:
2311 return new_node2(IR_SEQUAL
,
2312 _slang_gen_operation(A
, &oper
->children
[0]),
2313 _slang_gen_operation(A
, &oper
->children
[1]));
2314 case SLANG_OPER_NOTEQUAL
:
2315 return new_node2(IR_SNEQUAL
,
2316 _slang_gen_operation(A
, &oper
->children
[0]),
2317 _slang_gen_operation(A
, &oper
->children
[1]));
2318 case SLANG_OPER_GREATER
:
2319 return new_node2(IR_SGT
,
2320 _slang_gen_operation(A
, &oper
->children
[0]),
2321 _slang_gen_operation(A
, &oper
->children
[1]));
2322 case SLANG_OPER_LESS
:
2323 /* child[0] < child[1] ----> child[1] > child[0] */
2324 return new_node2(IR_SGT
,
2325 _slang_gen_operation(A
, &oper
->children
[1]),
2326 _slang_gen_operation(A
, &oper
->children
[0]));
2327 case SLANG_OPER_GREATEREQUAL
:
2328 return new_node2(IR_SGE
,
2329 _slang_gen_operation(A
, &oper
->children
[0]),
2330 _slang_gen_operation(A
, &oper
->children
[1]));
2331 case SLANG_OPER_LESSEQUAL
:
2332 /* child[0] <= child[1] ----> child[1] >= child[0] */
2333 return new_node2(IR_SGE
,
2334 _slang_gen_operation(A
, &oper
->children
[1]),
2335 _slang_gen_operation(A
, &oper
->children
[0]));
2336 case SLANG_OPER_ADD
:
2339 assert(oper
->num_children
== 2);
2340 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2343 case SLANG_OPER_SUBTRACT
:
2346 assert(oper
->num_children
== 2);
2347 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2350 case SLANG_OPER_MULTIPLY
:
2353 assert(oper
->num_children
== 2);
2354 _slang_check_matmul_optimization(A
, oper
);
2355 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2358 case SLANG_OPER_DIVIDE
:
2361 assert(oper
->num_children
== 2);
2362 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2365 case SLANG_OPER_MINUS
:
2368 assert(oper
->num_children
== 1);
2369 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2372 case SLANG_OPER_PLUS
:
2373 /* +expr --> do nothing */
2374 return _slang_gen_operation(A
, &oper
->children
[0]);
2375 case SLANG_OPER_VARIABLE_DECL
:
2376 return _slang_gen_declaration(A
, oper
);
2377 case SLANG_OPER_ASSIGN
:
2378 return _slang_gen_assignment(A
, oper
);
2379 case SLANG_OPER_ADDASSIGN
:
2382 assert(oper
->num_children
== 2);
2383 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2386 case SLANG_OPER_SUBASSIGN
:
2389 assert(oper
->num_children
== 2);
2390 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2394 case SLANG_OPER_MULASSIGN
:
2397 assert(oper
->num_children
== 2);
2398 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2401 case SLANG_OPER_DIVASSIGN
:
2404 assert(oper
->num_children
== 2);
2405 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2408 case SLANG_OPER_LOGICALAND
:
2411 assert(oper
->num_children
== 2);
2412 n
= _slang_gen_logical_and(A
, oper
);
2415 case SLANG_OPER_LOGICALOR
:
2418 assert(oper
->num_children
== 2);
2419 n
= _slang_gen_logical_or(A
, oper
);
2422 case SLANG_OPER_LOGICALXOR
:
2425 assert(oper
->num_children
== 2);
2426 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2429 case SLANG_OPER_NOT
:
2432 assert(oper
->num_children
== 1);
2433 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2437 case SLANG_OPER_SELECT
: /* b ? x : y */
2440 assert(oper
->num_children
== 3);
2441 n
= _slang_gen_select(A
, oper
);
2445 case SLANG_OPER_ASM
:
2446 return _slang_gen_asm(A
, oper
, NULL
);
2447 case SLANG_OPER_CALL
:
2448 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2450 case SLANG_OPER_RETURN
:
2451 return _slang_gen_return(A
, oper
);
2452 case SLANG_OPER_GOTO
:
2453 return new_jump(oper
->label
);
2454 case SLANG_OPER_LABEL
:
2455 return new_label(oper
->label
);
2456 case SLANG_OPER_IDENTIFIER
:
2457 return _slang_gen_variable(A
, oper
);
2459 return _slang_gen_hl_if(A
, oper
);
2460 case SLANG_OPER_FIELD
:
2461 return _slang_gen_field(A
, oper
);
2462 case SLANG_OPER_SUBSCRIPT
:
2463 return _slang_gen_subscript(A
, oper
);
2464 case SLANG_OPER_LITERAL_FLOAT
:
2466 case SLANG_OPER_LITERAL_INT
:
2468 case SLANG_OPER_LITERAL_BOOL
:
2469 return new_float_literal(oper
->literal
);
2471 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2474 assert(oper
->num_children
== 1);
2475 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2478 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2481 assert(oper
->num_children
== 1);
2482 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2485 case SLANG_OPER_PREINCREMENT
: /* ++var */
2488 assert(oper
->num_children
== 1);
2489 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2492 case SLANG_OPER_PREDECREMENT
: /* --var */
2495 assert(oper
->num_children
== 1);
2496 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2500 case SLANG_OPER_SEQUENCE
:
2502 slang_ir_node
*tree
= NULL
;
2504 for (i
= 0; i
< oper
->num_children
; i
++) {
2505 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2506 tree
= tree
? new_seq(tree
, n
) : n
;
2511 case SLANG_OPER_NONE
:
2513 case SLANG_OPER_VOID
:
2517 printf("Unhandled node type %d\n", oper
->type
);
2519 return new_node0(IR_NOP
);
2528 * Called by compiler when a global variable has been parsed/compiled.
2529 * Here we examine the variable's type to determine what kind of register
2530 * storage will be used.
2532 * A uniform such as "gl_Position" will become the register specification
2533 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2534 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2536 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2537 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2538 * actual texture unit (as specified by the user calling glUniform1i()).
2541 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2542 slang_unit_type type
)
2544 struct gl_program
*prog
= A
->program
;
2545 const char *varName
= (char *) var
->a_name
;
2546 GLboolean success
= GL_TRUE
;
2548 slang_ir_storage
*store
= NULL
;
2551 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2553 if (texIndex
!= -1) {
2555 * store->File = PROGRAM_SAMPLER
2556 * store->Index = sampler uniform location
2557 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2559 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2560 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2561 if (dbg
) printf("SAMPLER ");
2563 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2564 /* Uniform variable */
2565 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2566 * MAX2(var
->array_len
, 1);
2568 /* user-defined uniform */
2569 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2570 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2573 /* pre-defined uniform, like gl_ModelviewMatrix */
2574 /* We know it's a uniform, but don't allocate storage unless
2577 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2579 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2581 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2582 const GLint size
= 4; /* XXX fix */
2584 /* user-defined varying */
2585 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2586 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2589 /* pre-defined varying, like gl_Color or gl_TexCoord */
2590 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2591 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2593 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2594 assert(index
< FRAG_ATTRIB_MAX
);
2597 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2599 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2600 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2601 assert(index
< VERT_RESULT_MAX
);
2603 if (dbg
) printf("V/F ");
2605 if (dbg
) printf("VARYING ");
2607 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2609 /* user-defined vertex attribute */
2610 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2611 const GLint attr
= -1; /* unknown */
2612 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2615 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2616 VERT_ATTRIB_GENERIC0
+ index
, size
);
2619 /* pre-defined vertex attrib */
2620 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2621 GLint size
= 4; /* XXX? */
2623 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2625 if (dbg
) printf("ATTRIB ");
2627 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2628 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2629 GLint size
= 4; /* XXX? */
2630 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2631 if (dbg
) printf("INPUT ");
2633 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2634 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2635 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2636 GLint size
= 4; /* XXX? */
2637 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2640 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2641 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2642 GLint size
= 4; /* XXX? */
2643 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2645 if (dbg
) printf("OUTPUT ");
2647 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2648 /* pre-defined global constant, like gl_MaxLights */
2649 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2650 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2651 if (dbg
) printf("CONST ");
2654 /* ordinary variable (may be const) */
2657 /* IR node to declare the variable */
2658 n
= _slang_gen_var_decl(A
, var
);
2660 /* IR code for the var's initializer, if present */
2661 if (var
->initializer
) {
2662 slang_ir_node
*lhs
, *rhs
, *init
;
2664 /* Generate IR_MOVE instruction to initialize the variable */
2665 lhs
= new_node0(IR_VAR
);
2667 lhs
->Store
= n
->Store
;
2669 /* constant folding, etc */
2670 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2672 rhs
= _slang_gen_operation(A
, var
->initializer
);
2674 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2675 n
= new_seq(n
, init
);
2678 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
2680 _slang_free_ir_tree(n
);
2683 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2684 store
? store
->Index
: -2);
2687 var
->aux
= store
; /* save var's storage info */
2694 * Produce an IR tree from a function AST (fun->body).
2695 * Then call the code emitter to convert the IR tree into gl_program
2699 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2702 GLboolean success
= GL_TRUE
;
2704 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2705 /* we only really generate code for main, all other functions get
2708 return GL_TRUE
; /* not an error */
2712 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2715 slang_print_function(fun
, 1);
2718 /* should have been allocated earlier: */
2719 assert(A
->program
->Parameters
);
2720 assert(A
->program
->Varying
);
2721 assert(A
->vartable
);
2723 /* fold constant expressions, etc. */
2724 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2726 /* Create an end-of-function label */
2727 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
2729 /* push new vartable scope */
2730 _slang_push_var_table(A
->vartable
);
2732 /* Generate IR tree for the function body code */
2733 n
= _slang_gen_operation(A
, fun
->body
);
2735 n
= new_node1(IR_SCOPE
, n
);
2737 /* pop vartable, restore previous */
2738 _slang_pop_var_table(A
->vartable
);
2741 /* XXX record error */
2745 /* append an end-of-function-label to IR tree */
2746 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
2748 /*_slang_label_delete(A->curFuncEndLabel);*/
2749 A
->curFuncEndLabel
= NULL
;
2752 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2753 slang_print_function(fun
, 1);
2756 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2757 slang_print_ir(n
, 0);
2760 printf("************* End codegen function ************\n\n");
2763 /* Emit program instructions */
2764 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
2765 _slang_free_ir_tree(n
);
2767 /* free codegen context */
2769 _mesa_free(A->codegen);