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_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).
1564 * Otherwise, we use an IF/ELSE/ENDIF construct.
1566 static slang_ir_node
*
1567 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1569 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
1570 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
1571 slang_typeinfo type
;
1574 assert(oper
->type
== SLANG_OPER_SELECT
);
1575 assert(oper
->num_children
== 3);
1577 /* size of x or y's type */
1578 slang_typeinfo_construct(&type
);
1579 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1580 size
= _slang_sizeof_type_specifier(&type
.spec
);
1584 tmpDecl
= _slang_gen_temporary(size
);
1586 /* the condition (child 0) */
1587 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1588 cond
= new_cond(cond
);
1590 /* if-true body (child 1) */
1591 tmpVar
= new_node0(IR_VAR
);
1592 tmpVar
->Store
= tmpDecl
->Store
;
1593 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
1594 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
1596 /* if-false body (child 2) */
1597 tmpVar
= new_node0(IR_VAR
);
1598 tmpVar
->Store
= tmpDecl
->Store
;
1599 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
1600 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
1602 ifNode
= new_if(cond
, trueNode
, falseNode
);
1605 tmpVar
= new_node0(IR_VAR
);
1606 tmpVar
->Store
= tmpDecl
->Store
;
1608 tree
= new_seq(ifNode
, tmpVar
);
1609 tree
= new_seq(tmpDecl
, tree
);
1611 slang_print_ir(tree
, 10);
1617 * Generate code for &&.
1619 static slang_ir_node
*
1620 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1622 /* rewrite "a && b" as "a ? b : false" */
1623 slang_operation
*select
;
1626 select
= slang_operation_new(1);
1627 select
->type
= SLANG_OPER_SELECT
;
1628 select
->num_children
= 3;
1629 select
->children
= slang_operation_new(3);
1631 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1632 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1633 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1634 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1635 select
->children
[2].literal_size
= 1;
1637 n
= _slang_gen_select(A
, select
);
1640 free(select
->children
);
1648 * Generate code for ||.
1650 static slang_ir_node
*
1651 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1653 /* rewrite "a || b" as "a ? true : b" */
1654 slang_operation
*select
;
1657 select
= slang_operation_new(1);
1658 select
->type
= SLANG_OPER_SELECT
;
1659 select
->num_children
= 3;
1660 select
->children
= slang_operation_new(3);
1662 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1663 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1664 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1665 select
->children
[1].literal_size
= 1;
1666 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1668 n
= _slang_gen_select(A
, select
);
1671 free(select
->children
);
1679 * Generate IR tree for a return statement.
1681 static slang_ir_node
*
1682 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1684 if (oper
->num_children
== 0 ||
1685 (oper
->num_children
== 1 &&
1686 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1690 * goto __endOfFunction;
1693 slang_operation gotoOp
;
1694 slang_operation_construct(&gotoOp
);
1695 gotoOp
.type
= SLANG_OPER_GOTO
;
1696 gotoOp
.label
= A
->curFuncEndLabel
;
1697 assert(gotoOp
.label
);
1699 /* assemble the new code */
1700 n
= _slang_gen_operation(A
, &gotoOp
);
1701 /* destroy temp code */
1702 slang_operation_destruct(&gotoOp
);
1711 * goto __endOfFunction;
1713 slang_operation
*block
, *assign
, *jump
;
1714 slang_atom a_retVal
;
1717 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1723 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1728 block
= slang_operation_new(1);
1729 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1730 assert(block
->locals
);
1731 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1732 block
->num_children
= 2;
1733 block
->children
= slang_operation_new(2);
1735 /* child[0]: __retVal = expr; */
1736 assign
= &block
->children
[0];
1737 assign
->type
= SLANG_OPER_ASSIGN
;
1738 assign
->locals
->outer_scope
= block
->locals
;
1739 assign
->num_children
= 2;
1740 assign
->children
= slang_operation_new(2);
1741 /* lhs (__retVal) */
1742 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1743 assign
->children
[0].a_id
= a_retVal
;
1744 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1746 /* XXX we might be able to avoid this copy someday */
1747 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1749 /* child[1]: goto __endOfFunction */
1750 jump
= &block
->children
[1];
1751 jump
->type
= SLANG_OPER_GOTO
;
1752 assert(A
->curFuncEndLabel
);
1753 /* XXX don't call function? */
1754 jump
->label
= A
->curFuncEndLabel
;
1755 assert(jump
->label
);
1758 printf("NEW RETURN:\n");
1759 slang_print_tree(block
, 0);
1762 /* assemble the new code */
1763 n
= _slang_gen_operation(A
, block
);
1764 slang_operation_delete(block
);
1771 * Generate IR tree for a variable declaration.
1773 static slang_ir_node
*
1774 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1777 slang_ir_node
*varDecl
;
1779 const char *varName
= (char *) oper
->a_id
;
1781 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1783 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1786 varDecl
= _slang_gen_var_decl(A
, v
);
1788 if (oper
->num_children
> 0) {
1789 /* child is initializer */
1790 slang_ir_node
*var
, *init
, *rhs
;
1791 assert(oper
->num_children
== 1);
1792 var
= new_var(A
, oper
, oper
->a_id
);
1794 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1797 /* XXX make copy of this initializer? */
1798 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1800 init
= new_node2(IR_MOVE
, var
, rhs
);
1801 /*assert(rhs->Opcode != IR_SEQ);*/
1802 n
= new_seq(varDecl
, init
);
1804 else if (v
->initializer
) {
1805 slang_ir_node
*var
, *init
, *rhs
;
1806 var
= new_var(A
, oper
, oper
->a_id
);
1808 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1812 /* XXX make copy of this initializer? */
1814 slang_operation dup
;
1815 slang_operation_construct(&dup
);
1816 slang_operation_copy(&dup
, v
->initializer
);
1817 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1818 rhs
= _slang_gen_operation(A
, &dup
);
1821 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1822 rhs
= _slang_gen_operation(A
, v
->initializer
);
1825 init
= new_node2(IR_MOVE
, var
, rhs
);
1827 assert(rhs->Opcode != IR_SEQ);
1829 n
= new_seq(varDecl
, init
);
1839 * Generate IR tree for a variable (such as in an expression).
1841 static slang_ir_node
*
1842 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1844 /* If there's a variable associated with this oper (from inlining)
1845 * use it. Otherwise, use the oper's var id.
1847 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1848 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1850 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
1858 * Some write-masked assignments are simple, but others are hard.
1861 * v.xy = vec2(a, b);
1864 * v.zy = vec2(a, b);
1865 * this gets transformed/swizzled into:
1866 * v.zy = vec2(a, b).*yx* (* = don't care)
1867 * This function helps to determine simple vs. non-simple.
1870 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1872 switch (writemask
) {
1874 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1876 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1878 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1880 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1882 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1883 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1885 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1886 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1887 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1888 case WRITEMASK_XYZW
:
1889 return swizzle
== SWIZZLE_NOOP
;
1897 * Convert the given swizzle into a writemask. In some cases this
1898 * is trivial, in other cases, we'll need to also swizzle the right
1899 * hand side to put components in the right places.
1900 * \param swizzle the incoming swizzle
1901 * \param writemaskOut returns the writemask
1902 * \param swizzleOut swizzle to apply to the right-hand-side
1903 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1906 swizzle_to_writemask(GLuint swizzle
,
1907 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1909 GLuint mask
= 0x0, newSwizzle
[4];
1912 /* make new dst writemask, compute size */
1913 for (i
= 0; i
< 4; i
++) {
1914 const GLuint swz
= GET_SWZ(swizzle
, i
);
1915 if (swz
== SWIZZLE_NIL
) {
1919 assert(swz
>= 0 && swz
<= 3);
1922 assert(mask
<= 0xf);
1923 size
= i
; /* number of components in mask/swizzle */
1925 *writemaskOut
= mask
;
1927 /* make new src swizzle, by inversion */
1928 for (i
= 0; i
< 4; i
++) {
1929 newSwizzle
[i
] = i
; /*identity*/
1931 for (i
= 0; i
< size
; i
++) {
1932 const GLuint swz
= GET_SWZ(swizzle
, i
);
1933 newSwizzle
[swz
] = i
;
1935 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1940 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1942 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1944 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1946 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1948 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1956 static slang_ir_node
*
1957 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
1959 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
1961 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
1962 n
->Store
->Swizzle
= swizzle
;
1969 * Generate IR tree for an assignment (=).
1971 static slang_ir_node
*
1972 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
1974 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
1975 oper
->children
[1].type
== SLANG_OPER_CALL
) {
1976 /* Special case of: x = f(a, b)
1977 * Replace with f(a, b, x) (where x == hidden __retVal out param)
1979 * XXX this could be even more effective if we could accomodate
1980 * cases such as "v.x = f();" - would help with typical vertex
1984 n
= _slang_gen_function_call_name(A
,
1985 (const char *) oper
->children
[1].a_id
,
1986 &oper
->children
[1], &oper
->children
[0]);
1990 slang_ir_node
*n
, *lhs
, *rhs
;
1991 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1992 if (lhs
->Store
->File
!= PROGRAM_OUTPUT
&&
1993 lhs
->Store
->File
!= PROGRAM_TEMPORARY
&&
1994 lhs
->Store
->File
!= PROGRAM_VARYING
&&
1995 lhs
->Store
->File
!= PROGRAM_UNDEFINED
) {
1996 slang_info_log_error(A
->log
, "Assignment to read-only variable");
2000 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2002 /* convert lhs swizzle into writemask */
2003 GLuint writemask
, newSwizzle
;
2004 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2005 &writemask
, &newSwizzle
)) {
2006 /* Non-simple writemask, need to swizzle right hand side in
2007 * order to put components into the right place.
2009 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2011 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2012 n
->Writemask
= writemask
;
2023 * Generate IR tree for referencing a field in a struct (or basic vector type)
2025 static slang_ir_node
*
2026 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2030 slang_typeinfo_construct(&ti
);
2031 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2033 if (_slang_type_is_vector(ti
.spec
.type
)) {
2034 /* the field should be a swizzle */
2035 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2039 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2040 slang_info_log_error(A
->log
, "Bad swizzle");
2042 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2047 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2048 /* create new parent node with swizzle */
2049 n
= _slang_gen_swizzle(n
, swizzle
);
2052 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2053 const GLuint rows
= 1;
2057 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2058 slang_info_log_error(A
->log
, "Bad swizzle");
2060 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2064 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2065 /* create new parent node with swizzle */
2066 n
= _slang_gen_swizzle(n
, swizzle
);
2070 /* the field is a structure member (base.field) */
2071 /* oper->children[0] is the base */
2072 /* oper->a_id is the field name */
2073 slang_ir_node
*base
, *n
;
2074 GLint size
= 4; /* XXX fix? */
2076 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2078 /* error previously found */
2082 n
= new_node1(IR_FIELD
, base
);
2084 n
->Field
= (char *) oper
->a_id
;
2085 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2092 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2100 * Gen code for array indexing.
2102 static slang_ir_node
*
2103 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2105 slang_typeinfo array_ti
;
2107 /* get array's type info */
2108 slang_typeinfo_construct(&array_ti
);
2109 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2111 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2112 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2113 /* translate the index into a swizzle/writemask: "v.x=p" */
2114 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2118 index
= (GLint
) oper
->children
[1].literal
[0];
2119 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2121 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2125 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2127 /* use swizzle to access the element */
2128 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2132 n
= _slang_gen_swizzle(n
, swizzle
);
2133 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2134 n
->Writemask
= WRITEMASK_X
<< index
;
2139 /* conventional array */
2140 slang_typeinfo elem_ti
;
2141 slang_ir_node
*elem
, *array
, *index
;
2144 /* size of array element */
2145 slang_typeinfo_construct(&elem_ti
);
2146 _slang_typeof_operation(A
, oper
, &elem_ti
);
2147 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2148 if (elemSize
<= 0) {
2149 /* unknown var or type */
2150 slang_info_log_error(A
->log
, "Undefined var or type");
2154 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2155 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2156 if (array
&& index
) {
2157 elem
= new_node2(IR_ELEMENT
, array
, index
);
2158 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2159 array
->Store
->Index
,
2161 /* XXX try to do some array bounds checking here */
2172 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2173 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2174 * Since matrices are stored in column-major order, the second form of
2175 * multiplication is much more efficient (just 4 dot products).
2178 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2180 static const struct {
2182 const char *tranpose
;
2184 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2185 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2186 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2187 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2188 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2192 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2193 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2195 for (i
= 0; matrices
[i
].orig
; i
++) {
2196 if (oper
->children
[0].a_id
2197 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2199 _mesa_printf("Replace %s with %s\n",
2200 matrices[i].orig, matrices[i].tranpose);
2202 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2203 oper
->children
[0].a_id
2204 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2205 /* finally, swap the operands */
2206 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2215 * Generate IR tree for a slang_operation (AST node)
2217 static slang_ir_node
*
2218 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2220 switch (oper
->type
) {
2221 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2225 _slang_push_var_table(A
->vartable
);
2227 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2228 n
= _slang_gen_operation(A
, oper
);
2229 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2231 _slang_pop_var_table(A
->vartable
);
2234 n
= new_node1(IR_SCOPE
, n
);
2239 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2240 /* list of operations */
2241 if (oper
->num_children
> 0)
2243 slang_ir_node
*n
, *tree
= NULL
;
2246 for (i
= 0; i
< oper
->num_children
; i
++) {
2247 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2249 _slang_free_ir_tree(tree
);
2250 return NULL
; /* error must have occured */
2252 tree
= tree
? new_seq(tree
, n
) : n
;
2256 if (oper
->locals
->num_variables
> 0) {
2259 printf("\n****** Deallocate vars in scope!\n");
2261 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2262 slang_variable
*v
= oper
->locals
->variables
+ i
;
2264 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2266 printf(" Deallocate var %s\n", (char*) v->a_name);
2268 assert(store
->File
== PROGRAM_TEMPORARY
);
2269 assert(store
->Index
>= 0);
2270 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2278 case SLANG_OPER_EXPRESSION
:
2279 return _slang_gen_operation(A
, &oper
->children
[0]);
2281 case SLANG_OPER_FOR
:
2282 return _slang_gen_for(A
, oper
);
2284 return _slang_gen_do(A
, oper
);
2285 case SLANG_OPER_WHILE
:
2286 return _slang_gen_while(A
, oper
);
2287 case SLANG_OPER_BREAK
:
2289 slang_info_log_error(A
->log
, "'break' not in loop");
2291 return new_break(A
->CurLoop
);
2292 case SLANG_OPER_CONTINUE
:
2294 slang_info_log_error(A
->log
, "'continue' not in loop");
2296 return new_cont(A
->CurLoop
);
2297 case SLANG_OPER_DISCARD
:
2298 return new_node0(IR_KILL
);
2300 case SLANG_OPER_EQUAL
:
2301 return new_node2(IR_SEQUAL
,
2302 _slang_gen_operation(A
, &oper
->children
[0]),
2303 _slang_gen_operation(A
, &oper
->children
[1]));
2304 case SLANG_OPER_NOTEQUAL
:
2305 return new_node2(IR_SNEQUAL
,
2306 _slang_gen_operation(A
, &oper
->children
[0]),
2307 _slang_gen_operation(A
, &oper
->children
[1]));
2308 case SLANG_OPER_GREATER
:
2309 return new_node2(IR_SGT
,
2310 _slang_gen_operation(A
, &oper
->children
[0]),
2311 _slang_gen_operation(A
, &oper
->children
[1]));
2312 case SLANG_OPER_LESS
:
2313 /* child[0] < child[1] ----> child[1] > child[0] */
2314 return new_node2(IR_SGT
,
2315 _slang_gen_operation(A
, &oper
->children
[1]),
2316 _slang_gen_operation(A
, &oper
->children
[0]));
2317 case SLANG_OPER_GREATEREQUAL
:
2318 return new_node2(IR_SGE
,
2319 _slang_gen_operation(A
, &oper
->children
[0]),
2320 _slang_gen_operation(A
, &oper
->children
[1]));
2321 case SLANG_OPER_LESSEQUAL
:
2322 /* child[0] <= child[1] ----> child[1] >= child[0] */
2323 return new_node2(IR_SGE
,
2324 _slang_gen_operation(A
, &oper
->children
[1]),
2325 _slang_gen_operation(A
, &oper
->children
[0]));
2326 case SLANG_OPER_ADD
:
2329 assert(oper
->num_children
== 2);
2330 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2333 case SLANG_OPER_SUBTRACT
:
2336 assert(oper
->num_children
== 2);
2337 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2340 case SLANG_OPER_MULTIPLY
:
2343 assert(oper
->num_children
== 2);
2344 _slang_check_matmul_optimization(A
, oper
);
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_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
, A
->log
);
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
)
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 /* Create an end-of-function label */
2717 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
2719 /* push new vartable scope */
2720 _slang_push_var_table(A
->vartable
);
2722 /* Generate IR tree for the function body code */
2723 n
= _slang_gen_operation(A
, fun
->body
);
2725 n
= new_node1(IR_SCOPE
, n
);
2727 /* pop vartable, restore previous */
2728 _slang_pop_var_table(A
->vartable
);
2731 /* XXX record error */
2735 /* append an end-of-function-label to IR tree */
2736 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
2738 /*_slang_label_delete(A->curFuncEndLabel);*/
2739 A
->curFuncEndLabel
= NULL
;
2742 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2743 slang_print_function(fun
, 1);
2746 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2747 slang_print_ir(n
, 0);
2750 printf("************* End codegen function ************\n\n");
2753 /* Emit program instructions */
2754 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
2755 _slang_free_ir_tree(n
);
2757 /* free codegen context */
2759 _mesa_free(A->codegen);