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 * Mesa GLSL code generator. Convert AST to IR tree.
33 #include "slang_typeinfo.h"
34 #include "slang_builtin.h"
35 #include "slang_codegen.h"
36 #include "slang_compile.h"
37 #include "slang_storage.h"
38 #include "slang_error.h"
39 #include "slang_simplify.h"
40 #include "slang_emit.h"
41 #include "slang_vartable.h"
45 #include "prog_instruction.h"
46 #include "prog_parameter.h"
47 #include "prog_statevars.h"
48 #include "slang_print.h"
51 static slang_ir_node
*
52 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
56 is_sampler_type(const slang_fully_specified_type
*t
)
58 switch (t
->specifier
.type
) {
59 case SLANG_SPEC_SAMPLER1D
:
60 case SLANG_SPEC_SAMPLER2D
:
61 case SLANG_SPEC_SAMPLER3D
:
62 case SLANG_SPEC_SAMPLERCUBE
:
63 case SLANG_SPEC_SAMPLER1DSHADOW
:
64 case SLANG_SPEC_SAMPLER2DSHADOW
:
73 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
80 case SLANG_SPEC_BVEC2
:
82 case SLANG_SPEC_BVEC3
:
84 case SLANG_SPEC_BVEC4
:
88 case SLANG_SPEC_IVEC2
:
90 case SLANG_SPEC_IVEC3
:
92 case SLANG_SPEC_IVEC4
:
94 case SLANG_SPEC_FLOAT
:
100 case SLANG_SPEC_VEC4
:
102 case SLANG_SPEC_MAT2
:
104 case SLANG_SPEC_MAT3
:
106 case SLANG_SPEC_MAT4
:
108 case SLANG_SPEC_SAMPLER1D
:
109 case SLANG_SPEC_SAMPLER2D
:
110 case SLANG_SPEC_SAMPLER3D
:
111 case SLANG_SPEC_SAMPLERCUBE
:
112 case SLANG_SPEC_SAMPLER1DSHADOW
:
113 case SLANG_SPEC_SAMPLER2DSHADOW
:
114 return 1; /* special case */
115 case SLANG_SPEC_STRUCT
:
118 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
119 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
120 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
121 /* XXX verify padding */
128 case SLANG_SPEC_ARRAY
:
129 return _slang_sizeof_type_specifier(spec
->_array
);
131 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
139 * Establish the binding between a slang_ir_node and a slang_variable.
140 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
141 * The IR node must be a IR_VAR or IR_VAR_DECL node.
142 * \param n the IR node
143 * \param var the variable to associate with the IR node
146 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
150 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
151 assert(!n
->Var
|| n
->Var
== var
);
156 /* need to setup storage */
157 if (n
->Var
&& n
->Var
->aux
) {
158 /* node storage info = var storage info */
159 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
162 /* alloc new storage info */
163 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
165 n
->Var
->aux
= n
->Store
;
173 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
174 * or -1 if the type is not a sampler.
177 sampler_to_texture_index(const slang_type_specifier_type type
)
180 case SLANG_SPEC_SAMPLER1D
:
181 return TEXTURE_1D_INDEX
;
182 case SLANG_SPEC_SAMPLER2D
:
183 return TEXTURE_2D_INDEX
;
184 case SLANG_SPEC_SAMPLER3D
:
185 return TEXTURE_3D_INDEX
;
186 case SLANG_SPEC_SAMPLERCUBE
:
187 return TEXTURE_CUBE_INDEX
;
188 case SLANG_SPEC_SAMPLER1DSHADOW
:
189 return TEXTURE_1D_INDEX
; /* XXX fix */
190 case SLANG_SPEC_SAMPLER2DSHADOW
:
191 return TEXTURE_2D_INDEX
; /* XXX fix */
199 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
200 * a vertex or fragment program input variable. Return -1 if the input
202 * XXX return size too
205 _slang_input_index(const char *name
, GLenum target
)
211 static const struct input_info vertInputs
[] = {
212 { "gl_Vertex", VERT_ATTRIB_POS
},
213 { "gl_Normal", VERT_ATTRIB_NORMAL
},
214 { "gl_Color", VERT_ATTRIB_COLOR0
},
215 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
},
216 { "gl_FogCoord", VERT_ATTRIB_FOG
},
217 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
},
218 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
},
219 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
},
220 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
},
221 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
},
222 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
},
223 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
},
224 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
},
227 static const struct input_info fragInputs
[] = {
228 { "gl_FragCoord", FRAG_ATTRIB_WPOS
},
229 { "gl_Color", FRAG_ATTRIB_COL0
},
230 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
},
231 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
},
232 { "gl_TexCoord", FRAG_ATTRIB_TEX0
},
236 const struct input_info
*inputs
237 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
239 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
241 for (i
= 0; inputs
[i
].Name
; i
++) {
242 if (strcmp(inputs
[i
].Name
, name
) == 0) {
244 return inputs
[i
].Attrib
;
252 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
253 * a vertex or fragment program output variable. Return -1 for an invalid
257 _slang_output_index(const char *name
, GLenum target
)
263 static const struct output_info vertOutputs
[] = {
264 { "gl_Position", VERT_RESULT_HPOS
},
265 { "gl_FrontColor", VERT_RESULT_COL0
},
266 { "gl_BackColor", VERT_RESULT_BFC0
},
267 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
268 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
269 { "gl_TexCoord", VERT_RESULT_TEX0
}, /* XXX indexed */
270 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
271 { "gl_PointSize", VERT_RESULT_PSIZ
},
274 static const struct output_info fragOutputs
[] = {
275 { "gl_FragColor", FRAG_RESULT_COLR
},
276 { "gl_FragDepth", FRAG_RESULT_DEPR
},
280 const struct output_info
*outputs
281 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
283 for (i
= 0; outputs
[i
].Name
; i
++) {
284 if (strcmp(outputs
[i
].Name
, name
) == 0) {
286 return outputs
[i
].Attrib
;
294 /**********************************************************************/
298 * Map "_asm foo" to IR_FOO, etc.
303 slang_ir_opcode Opcode
;
304 GLuint HaveRetValue
, NumParams
;
308 static slang_asm_info AsmInfo
[] = {
310 { "vec4_add", IR_ADD
, 1, 2 },
311 { "vec4_subtract", IR_SUB
, 1, 2 },
312 { "vec4_multiply", IR_MUL
, 1, 2 },
313 { "vec4_dot", IR_DOT4
, 1, 2 },
314 { "vec3_dot", IR_DOT3
, 1, 2 },
315 { "vec3_cross", IR_CROSS
, 1, 2 },
316 { "vec4_lrp", IR_LRP
, 1, 3 },
317 { "vec4_min", IR_MIN
, 1, 2 },
318 { "vec4_max", IR_MAX
, 1, 2 },
319 { "vec4_clamp", IR_CLAMP
, 1, 3 },
320 { "vec4_seq", IR_SEQ
, 1, 2 },
321 { "vec4_sge", IR_SGE
, 1, 2 },
322 { "vec4_sgt", IR_SGT
, 1, 2 },
324 { "vec4_floor", IR_FLOOR
, 1, 1 },
325 { "vec4_frac", IR_FRAC
, 1, 1 },
326 { "vec4_abs", IR_ABS
, 1, 1 },
327 { "vec4_negate", IR_NEG
, 1, 1 },
328 { "vec4_ddx", IR_DDX
, 1, 1 },
329 { "vec4_ddy", IR_DDY
, 1, 1 },
330 /* float binary op */
331 { "float_add", IR_ADD
, 1, 2 },
332 { "float_multiply", IR_MUL
, 1, 2 },
333 { "float_divide", IR_DIV
, 1, 2 },
334 { "float_power", IR_POW
, 1, 2 },
335 /* texture / sampler */
336 { "vec4_tex1d", IR_TEX
, 1, 2 },
337 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
338 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
339 { "vec4_tex2d", IR_TEX
, 1, 2 },
340 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
341 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
342 { "vec4_tex3d", IR_TEX
, 1, 2 },
343 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
344 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
345 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
348 { "int_to_float", IR_I_TO_F
, 1, 1 },
349 { "float_to_int", IR_F_TO_I
, 1, 1 },
350 { "float_exp", IR_EXP
, 1, 1 },
351 { "float_exp2", IR_EXP2
, 1, 1 },
352 { "float_log2", IR_LOG2
, 1, 1 },
353 { "float_rsq", IR_RSQ
, 1, 1 },
354 { "float_rcp", IR_RCP
, 1, 1 },
355 { "float_sine", IR_SIN
, 1, 1 },
356 { "float_cosine", IR_COS
, 1, 1 },
357 { "float_noise1", IR_NOISE1
, 1, 1},
358 { "float_noise2", IR_NOISE2
, 1, 1},
359 { "float_noise3", IR_NOISE3
, 1, 1},
360 { "float_noise4", IR_NOISE4
, 1, 1},
362 { NULL
, IR_NOP
, 0, 0 }
367 * Recursively free an IR tree.
370 _slang_free_ir_tree(slang_ir_node
*n
)
376 for (i
= 0; i
< 3; i
++)
377 _slang_free_ir_tree(n
->Children
[i
]);
378 /* Do not free n->BranchNode since it's a child elsewhere */
384 static slang_ir_node
*
385 new_node3(slang_ir_opcode op
,
386 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
388 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
394 n
->Writemask
= WRITEMASK_XYZW
;
395 n
->InstLocation
= -1;
400 static slang_ir_node
*
401 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
403 return new_node3(op
, c0
, c1
, NULL
);
406 static slang_ir_node
*
407 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
409 return new_node3(op
, c0
, NULL
, NULL
);
412 static slang_ir_node
*
413 new_node0(slang_ir_opcode op
)
415 return new_node3(op
, NULL
, NULL
, NULL
);
419 static slang_ir_node
*
420 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
426 return new_node2(IR_SEQ
, left
, right
);
429 static slang_ir_node
*
430 new_label(slang_atom labName
)
432 slang_ir_node
*n
= new_node0(IR_LABEL
);
433 n
->Target
= (char *) labName
; /*_mesa_strdup(name);*/
437 static slang_ir_node
*
438 new_float_literal(const float v
[4])
440 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
441 slang_ir_node
*n
= new_node0(IR_FLOAT
);
442 COPY_4V(n
->Value
, v
);
443 /* allocate a storage object, but compute actual location (Index) later */
444 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
450 * \param zeroOrOne indicates if the jump is to be taken on zero, or non-zero
451 * condition code state.
452 * XXX maybe pass an IR node as second param to indicate the jump target???
454 static slang_ir_node
*
455 new_cjump(slang_atom target
, GLuint zeroOrOne
)
457 slang_ir_node
*n
= new_node0(zeroOrOne
? IR_CJUMP1
: IR_CJUMP0
);
459 n
->Target
= (char *) target
;
464 * Unconditional jump.
465 * XXX maybe pass an IR node as second param to indicate the jump target???
467 static slang_ir_node
*
468 new_jump(slang_atom target
)
470 slang_ir_node
*n
= new_node0(IR_JUMP
);
472 n
->Target
= (char *) target
;
477 static slang_ir_node
*
478 new_loop(slang_ir_node
*body
)
480 return new_node1(IR_LOOP
, body
);
484 static slang_ir_node
*
485 new_break(slang_ir_node
*loopNode
)
487 slang_ir_node
*n
= new_node0(IR_BREAK
);
489 assert(loopNode
->Opcode
== IR_LOOP
);
491 /* insert this node at head of linked list */
492 n
->BranchNode
= loopNode
->BranchNode
;
493 loopNode
->BranchNode
= n
;
500 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
502 static slang_ir_node
*
503 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
507 assert(loopNode
->Opcode
== IR_LOOP
);
508 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
510 /* insert this node at head of linked list */
511 n
->BranchNode
= loopNode
->BranchNode
;
512 loopNode
->BranchNode
= n
;
519 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
521 static slang_ir_node
*
522 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
526 assert(loopNode
->Opcode
== IR_LOOP
);
527 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
529 /* insert this node at head of linked list */
530 n
->BranchNode
= loopNode
->BranchNode
;
531 loopNode
->BranchNode
= n
;
537 static slang_ir_node
*
538 new_cont(slang_ir_node
*loopNode
)
540 slang_ir_node
*n
= new_node0(IR_CONT
);
542 assert(loopNode
->Opcode
== IR_LOOP
);
544 /* insert this node at head of linked list */
545 n
->BranchNode
= loopNode
->BranchNode
;
546 loopNode
->BranchNode
= n
;
552 static slang_ir_node
*
553 new_cond(slang_ir_node
*n
)
555 slang_ir_node
*c
= new_node1(IR_COND
, n
);
560 static slang_ir_node
*
561 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
563 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
568 * New IR_VAR node - a reference to a previously declared variable.
570 static slang_ir_node
*
571 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
574 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
578 assert(!oper
->var
|| oper
->var
== var
);
580 n
= new_node0(IR_VAR
);
582 _slang_attach_storage(n
, var
);
589 * Check if the given function is really just a wrapper for a
590 * basic assembly instruction.
593 slang_is_asm_function(const slang_function
*fun
)
595 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
596 fun
->body
->num_children
== 1 &&
597 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
605 * Produce inline code for a call to an assembly instruction.
607 static slang_operation
*
608 slang_inline_asm_function(slang_assemble_ctx
*A
,
609 slang_function
*fun
, slang_operation
*oper
)
611 const GLuint numArgs
= oper
->num_children
;
612 const slang_operation
*args
= oper
->children
;
614 slang_operation
*inlined
= slang_operation_new(1);
616 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
618 printf("Inline asm %s\n", (char*) fun->header.a_name);
620 inlined
->type
= fun
->body
->children
[0].type
;
621 inlined
->a_id
= fun
->body
->children
[0].a_id
;
622 inlined
->num_children
= numArgs
;
623 inlined
->children
= slang_operation_new(numArgs
);
625 inlined
->locals
= slang_variable_scope_copy(oper
->locals
);
627 assert(inlined
->locals
);
628 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
631 for (i
= 0; i
< numArgs
; i
++) {
632 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
640 slang_resolve_variable(slang_operation
*oper
)
642 if (oper
->type
!= SLANG_OPER_IDENTIFIER
)
645 oper
->var
= _slang_locate_variable(oper
->locals
,
646 (const slang_atom
) oper
->a_id
,
653 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
656 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
657 GLuint substCount
, slang_variable
**substOld
,
658 slang_operation
**substNew
, GLboolean isLHS
)
660 switch (oper
->type
) {
661 case SLANG_OPER_VARIABLE_DECL
:
663 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
664 oper
->a_id
, GL_TRUE
);
666 if (v
->initializer
&& oper
->num_children
== 0) {
667 /* set child of oper to copy of initializer */
668 oper
->num_children
= 1;
669 oper
->children
= slang_operation_new(1);
670 slang_operation_copy(&oper
->children
[0], v
->initializer
);
672 if (oper
->num_children
== 1) {
673 /* the initializer */
674 slang_substitute(A
, &oper
->children
[0], substCount
, substOld
, substNew
, GL_FALSE
);
678 case SLANG_OPER_IDENTIFIER
:
679 assert(oper
->num_children
== 0);
680 if (1/**!isLHS XXX FIX */) {
681 slang_atom id
= oper
->a_id
;
684 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
686 printf("var %s not found!\n", (char *) oper
->a_id
);
687 _slang_print_var_scope(oper
->locals
, 6);
693 /* look for a substitution */
694 for (i
= 0; i
< substCount
; i
++) {
695 if (v
== substOld
[i
]) {
696 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
697 #if 0 /* DEBUG only */
698 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
699 assert(substNew
[i
]->var
);
700 assert(substNew
[i
]->var
->a_name
);
701 printf("Substitute %s with %s in id node %p\n",
702 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
706 printf("Substitute %s with %f in id node %p\n",
707 (char*)v
->a_name
, substNew
[i
]->literal
[0],
711 slang_operation_copy(oper
, substNew
[i
]);
717 #if 1 /* XXX rely on default case below */
718 case SLANG_OPER_RETURN
:
719 /* do return replacement here too */
720 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
721 if (oper
->num_children
== 1) {
727 * then do substitutions on the assignment.
729 slang_operation
*blockOper
, *assignOper
, *returnOper
;
730 blockOper
= slang_operation_new(1);
731 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
732 blockOper
->num_children
= 2;
733 blockOper
->children
= slang_operation_new(2);
734 assignOper
= blockOper
->children
+ 0;
735 returnOper
= blockOper
->children
+ 1;
737 assignOper
->type
= SLANG_OPER_ASSIGN
;
738 assignOper
->num_children
= 2;
739 assignOper
->children
= slang_operation_new(2);
740 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
741 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
742 assignOper
->children
[0].locals
->outer_scope
= oper
->locals
;
743 assignOper
->locals
= oper
->locals
;
744 slang_operation_copy(&assignOper
->children
[1],
747 returnOper
->type
= SLANG_OPER_RETURN
;
748 assert(returnOper
->num_children
== 0);
750 /* do substitutions on the "__retVal = expr" sub-tree */
751 slang_substitute(A
, assignOper
,
752 substCount
, substOld
, substNew
, GL_FALSE
);
754 /* install new code */
755 slang_operation_copy(oper
, blockOper
);
756 slang_operation_destruct(blockOper
);
760 case SLANG_OPER_ASSIGN
:
761 case SLANG_OPER_SUBSCRIPT
:
763 * child[0] can't have substitutions but child[1] can.
765 slang_substitute(A
, &oper
->children
[0],
766 substCount
, substOld
, substNew
, GL_TRUE
);
767 slang_substitute(A
, &oper
->children
[1],
768 substCount
, substOld
, substNew
, GL_FALSE
);
770 case SLANG_OPER_FIELD
:
772 slang_substitute(A
, &oper
->children
[0],
773 substCount
, substOld
, substNew
, GL_TRUE
);
778 for (i
= 0; i
< oper
->num_children
; i
++)
779 slang_substitute(A
, &oper
->children
[i
],
780 substCount
, substOld
, substNew
, GL_FALSE
);
788 * Inline the given function call operation.
789 * Return a new slang_operation that corresponds to the inlined code.
791 static slang_operation
*
792 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
793 slang_operation
*oper
, slang_operation
*returnOper
)
800 ParamMode
*paramMode
;
801 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
802 const GLuint numArgs
= oper
->num_children
;
803 const GLuint totalArgs
= numArgs
+ haveRetValue
;
804 slang_operation
*args
= oper
->children
;
805 slang_operation
*inlined
, *top
;
806 slang_variable
**substOld
;
807 slang_operation
**substNew
;
808 GLuint substCount
, numCopyIn
, i
;
810 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
811 assert(fun
->param_count
== totalArgs
);
813 /* allocate temporary arrays */
814 paramMode
= (ParamMode
*)
815 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
816 substOld
= (slang_variable
**)
817 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
818 substNew
= (slang_operation
**)
819 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
822 printf("Inline call to %s (total vars=%d nparams=%d)\n",
823 (char *) fun
->header
.a_name
,
824 fun
->parameters
->num_variables
, numArgs
);
827 if (haveRetValue
&& !returnOper
) {
828 /* Create 3-child comma sequence for inlined code:
829 * child[0]: declare __resultTmp
830 * child[1]: inlined function body
831 * child[2]: __resultTmp
833 slang_operation
*commaSeq
;
834 slang_operation
*declOper
= NULL
;
835 slang_variable
*resultVar
;
837 commaSeq
= slang_operation_new(1);
838 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
839 assert(commaSeq
->locals
);
840 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
841 commaSeq
->num_children
= 3;
842 commaSeq
->children
= slang_operation_new(3);
843 /* allocate the return var */
844 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
846 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
847 (void*)commaSeq->locals, (char *) fun->header.a_name);
850 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
851 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
852 resultVar
->isTemp
= GL_TRUE
;
854 /* child[0] = __resultTmp declaration */
855 declOper
= &commaSeq
->children
[0];
856 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
857 declOper
->a_id
= resultVar
->a_name
;
858 declOper
->locals
->outer_scope
= commaSeq
->locals
; /*** ??? **/
860 /* child[1] = function body */
861 inlined
= &commaSeq
->children
[1];
862 /* XXXX this may be inappropriate!!!!: */
863 inlined
->locals
->outer_scope
= commaSeq
->locals
;
865 /* child[2] = __resultTmp reference */
866 returnOper
= &commaSeq
->children
[2];
867 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
868 returnOper
->a_id
= resultVar
->a_name
;
869 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
870 declOper
->locals
->outer_scope
= commaSeq
->locals
;
875 top
= inlined
= slang_operation_new(1);
876 /* XXXX this may be inappropriate!!!! */
877 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
881 assert(inlined
->locals
);
883 /* Examine the parameters, look for inout/out params, look for possible
884 * substitutions, etc:
885 * param type behaviour
886 * in copy actual to local
887 * const in substitute param with actual
891 for (i
= 0; i
< totalArgs
; i
++) {
892 slang_variable
*p
= fun
->parameters
->variables
[i
];
894 printf("Param %d: %s %s \n", i,
895 slang_type_qual_string(p->type.qualifier),
898 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
899 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
900 /* an output param */
901 slang_operation
*arg
;
906 paramMode
[i
] = SUBST
;
908 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
909 slang_resolve_variable(arg
);
911 /* replace parameter 'p' with argument 'arg' */
912 substOld
[substCount
] = p
;
913 substNew
[substCount
] = arg
; /* will get copied */
916 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
917 /* a constant input param */
918 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
919 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
920 /* replace all occurances of this parameter variable with the
921 * actual argument variable or a literal.
923 paramMode
[i
] = SUBST
;
924 slang_resolve_variable(&args
[i
]);
925 substOld
[substCount
] = p
;
926 substNew
[substCount
] = &args
[i
]; /* will get copied */
930 paramMode
[i
] = COPY_IN
;
934 paramMode
[i
] = COPY_IN
;
936 assert(paramMode
[i
]);
939 /* actual code inlining: */
940 slang_operation_copy(inlined
, fun
->body
);
942 /*** XXX review this */
943 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
944 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
947 printf("======================= orig body code ======================\n");
948 printf("=== params scope = %p\n", (void*) fun
->parameters
);
949 slang_print_tree(fun
->body
, 8);
950 printf("======================= copied code =========================\n");
951 slang_print_tree(inlined
, 8);
954 /* do parameter substitution in inlined code: */
955 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
958 printf("======================= subst code ==========================\n");
959 slang_print_tree(inlined
, 8);
960 printf("=============================================================\n");
963 /* New prolog statements: (inserted before the inlined code)
964 * Copy the 'in' arguments.
967 for (i
= 0; i
< numArgs
; i
++) {
968 if (paramMode
[i
] == COPY_IN
) {
969 slang_variable
*p
= fun
->parameters
->variables
[i
];
970 /* declare parameter 'p' */
971 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
975 printf("COPY_IN %s from expr\n", (char*)p->a_name);
977 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
978 assert(decl
->locals
);
979 decl
->locals
= fun
->parameters
;
980 decl
->a_id
= p
->a_name
;
981 decl
->num_children
= 1;
982 decl
->children
= slang_operation_new(1);
984 /* child[0] is the var's initializer */
985 slang_operation_copy(&decl
->children
[0], args
+ i
);
991 /* New epilog statements:
992 * 1. Create end of function label to jump to from return statements.
993 * 2. Copy the 'out' parameter vars
996 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
998 inlined
->num_children
);
999 lab
->type
= SLANG_OPER_LABEL
;
1000 lab
->a_id
= slang_atom_pool_atom(A
->atoms
,
1001 (char *) A
->CurFunction
->end_label
);
1004 for (i
= 0; i
< totalArgs
; i
++) {
1005 if (paramMode
[i
] == COPY_OUT
) {
1006 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1007 /* actualCallVar = outParam */
1008 /*if (i > 0 || !haveRetValue)*/
1009 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1011 inlined
->num_children
);
1012 ass
->type
= SLANG_OPER_ASSIGN
;
1013 ass
->num_children
= 2;
1014 ass
->locals
= _slang_variable_scope_new(inlined
->locals
);
1015 assert(ass
->locals
);
1016 ass
->children
= slang_operation_new(2);
1017 ass
->children
[0] = args
[i
]; /*XXX copy */
1018 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1019 ass
->children
[1].a_id
= p
->a_name
;
1020 ass
->children
[1].locals
= _slang_variable_scope_new(ass
->locals
);
1024 _mesa_free(paramMode
);
1025 _mesa_free(substOld
);
1026 _mesa_free(substNew
);
1029 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1030 (char *) fun
->header
.a_name
,
1031 fun
->parameters
->num_variables
, numArgs
);
1032 slang_print_tree(top
, 0);
1038 static slang_ir_node
*
1039 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1040 slang_operation
*oper
, slang_operation
*dest
)
1043 slang_operation
*inlined
;
1044 slang_function
*prevFunc
;
1046 prevFunc
= A
->CurFunction
;
1047 A
->CurFunction
= fun
;
1049 if (!A
->CurFunction
->end_label
) {
1051 sprintf(name
, "__endOfFunc_%s_", (char *) A
->CurFunction
->header
.a_name
);
1052 A
->CurFunction
->end_label
= slang_atom_pool_gen(A
->atoms
, name
);
1055 if (slang_is_asm_function(fun
) && !dest
) {
1056 /* assemble assembly function - tree style */
1057 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1060 /* non-assembly function */
1061 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1064 /* Replace the function call with the inlined block */
1066 slang_operation_construct(oper
);
1067 slang_operation_copy(oper
, inlined
);
1074 assert(inlined
->locals
);
1075 printf("*** Inlined code for call to %s:\n",
1076 (char*) fun
->header
.a_name
);
1077 slang_print_tree(oper
, 10);
1081 n
= _slang_gen_operation(A
, oper
);
1083 A
->CurFunction
->end_label
= NULL
;
1085 A
->CurFunction
= prevFunc
;
1091 static slang_asm_info
*
1092 slang_find_asm_info(const char *name
)
1095 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1096 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1105 make_writemask(char *field
)
1111 mask
|= WRITEMASK_X
;
1114 mask
|= WRITEMASK_Y
;
1117 mask
|= WRITEMASK_Z
;
1120 mask
|= WRITEMASK_W
;
1128 return WRITEMASK_XYZW
;
1135 * Generate IR tree for an asm instruction/operation such as:
1136 * __asm vec4_dot __retVal.x, v1, v2;
1138 static slang_ir_node
*
1139 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1140 slang_operation
*dest
)
1142 const slang_asm_info
*info
;
1143 slang_ir_node
*kids
[3], *n
;
1144 GLuint j
, firstOperand
;
1146 assert(oper
->type
== SLANG_OPER_ASM
);
1148 info
= slang_find_asm_info((char *) oper
->a_id
);
1150 _mesa_problem(NULL
, "undefined __asm function %s\n",
1151 (char *) oper
->a_id
);
1154 assert(info
->NumParams
<= 3);
1156 if (info
->NumParams
== oper
->num_children
) {
1157 /* Storage for result is not specified.
1158 * Children[0], [1] are the operands.
1163 /* Storage for result (child[0]) is specified.
1164 * Children[1], [2] are the operands.
1169 /* assemble child(ren) */
1170 kids
[0] = kids
[1] = kids
[2] = NULL
;
1171 for (j
= 0; j
< info
->NumParams
; j
++) {
1172 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1175 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1178 /* Setup n->Store to be a particular location. Otherwise, storage
1179 * for the result (a temporary) will be allocated later.
1181 GLuint writemask
= WRITEMASK_XYZW
;
1182 slang_operation
*dest_oper
;
1185 dest_oper
= &oper
->children
[0];
1186 while /*if*/ (dest_oper
->type
== SLANG_OPER_FIELD
) {
1188 writemask
&= /*=*/make_writemask((char*) dest_oper
->a_id
);
1189 dest_oper
= &dest_oper
->children
[0];
1192 n0
= _slang_gen_operation(A
, dest_oper
);
1196 n
->Store
= n0
->Store
;
1197 n
->Writemask
= writemask
;
1208 _slang_is_noop(const slang_operation
*oper
)
1211 oper
->type
== SLANG_OPER_VOID
||
1212 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
1220 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1223 for (i
= 0; i
< scope
->num_functions
; i
++) {
1224 slang_function
*f
= &scope
->functions
[i
];
1225 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1226 printf(" %s (%d args)\n", name
, f
->param_count
);
1229 if (scope
->outer_scope
)
1230 print_funcs(scope
->outer_scope
, name
);
1235 * Return first function in the scope that has the given name.
1236 * This is the function we'll try to call when there is no exact match
1237 * between function parameters and call arguments.
1239 * XXX we should really create a list of candidate functions and try
1242 static slang_function
*
1243 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1246 for (i
= 0; i
< scope
->num_functions
; i
++) {
1247 slang_function
*f
= &scope
->functions
[i
];
1248 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1251 if (scope
->outer_scope
)
1252 return _slang_first_function(scope
->outer_scope
, name
);
1259 * Assemble a function call, given a particular function name.
1260 * \param name the function's name (operators like '*' are possible).
1262 static slang_ir_node
*
1263 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1264 slang_operation
*oper
, slang_operation
*dest
)
1266 slang_operation
*params
= oper
->children
;
1267 const GLuint param_count
= oper
->num_children
;
1269 slang_function
*fun
;
1271 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1272 if (atom
== SLANG_ATOM_NULL
)
1276 * Use 'name' to find the function to call
1278 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1279 &A
->space
, A
->atoms
);
1281 /* A function with exactly the right parameters/types was not found.
1282 * Try adapting the parameters.
1284 fun
= _slang_first_function(A
->space
.funcs
, name
);
1285 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
)) {
1286 RETURN_ERROR2("Undefined function (or no matching parameters)",
1292 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1297 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1299 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1300 oper
->type
== SLANG_OPER_LITERAL_INT
||
1301 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1302 if (oper
->literal
[0])
1308 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1309 oper
->num_children
== 1) {
1310 return _slang_is_constant_cond(&oper
->children
[0], value
);
1318 * Generate loop code using high-level IR_LOOP instruction
1320 static slang_ir_node
*
1321 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1325 * BREAK if !expr (child[0])
1326 * body code (child[1])
1328 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1329 GLboolean isConst
, constTrue
;
1331 /* Check if loop condition is a constant */
1332 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1334 if (isConst
&& !constTrue
) {
1335 /* loop is never executed! */
1336 return new_node0(IR_NOP
);
1339 loop
= new_loop(NULL
);
1341 /* save old, push new loop */
1342 prevLoop
= A
->CurLoop
;
1345 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1346 if (isConst
&& constTrue
) {
1347 /* while(nonzero constant), no conditional break */
1351 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1353 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1354 loop
->Children
[0] = new_seq(breakIf
, body
);
1356 /* Do infinite loop detection */
1357 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1358 /* infinite loop detected */
1359 A
->CurLoop
= prevLoop
; /* clean-up */
1360 RETURN_ERROR("Infinite loop detected!", 0);
1363 /* pop loop, restore prev */
1364 A
->CurLoop
= prevLoop
;
1371 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1373 static slang_ir_node
*
1374 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1378 * body code (child[0])
1379 * BREAK if !expr (child[1])
1381 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1382 GLboolean isConst
, constTrue
;
1384 /* Check if loop condition is a constant */
1385 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1387 loop
= new_loop(NULL
);
1389 /* save old, push new loop */
1390 prevLoop
= A
->CurLoop
;
1393 body
= _slang_gen_operation(A
, &oper
->children
[0]);
1394 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1395 if (isConst
&& constTrue
) {
1396 /* while(nonzero constant), no conditional break */
1400 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1402 loop
->Children
[0] = new_seq(body
, breakIf
);
1404 /* pop loop, restore prev */
1405 A
->CurLoop
= prevLoop
;
1412 * Generate for-loop using high-level IR_LOOP instruction.
1414 static slang_ir_node
*
1415 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1420 * BREAK if !expr (child[1])
1421 * body code (child[3])
1422 * incr code (child[2]) // XXX continue here
1424 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1426 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1427 loop
= new_loop(NULL
);
1429 /* save old, push new loop */
1430 prevLoop
= A
->CurLoop
;
1433 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1434 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1435 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1436 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1437 loop
->Children
[0] = new_seq(breakIf
,
1438 new_seq(body
, incr
));
1440 /* pop loop, restore prev */
1441 A
->CurLoop
= prevLoop
;
1443 return new_seq(init
, loop
);
1448 * Generate IR tree for an if/then/else conditional using BRAnch instructions.
1450 static slang_ir_node
*
1451 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1454 * eval expr (child[0]), updating condcodes
1455 * branch if false to _else or _endif
1457 * if haveElseClause clause:
1460 * "false" code block
1463 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1464 slang_ir_node
*cond
, *bra
, *trueBody
, *endifLab
, *tree
;
1465 slang_atom elseAtom
= slang_atom_pool_gen(A
->atoms
, "__else");
1466 slang_atom endifAtom
= slang_atom_pool_gen(A
->atoms
, "__endif");
1468 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1469 cond
= new_cond(cond
);
1470 /*assert(cond->Store);*/
1471 bra
= new_cjump(haveElseClause
? elseAtom
: endifAtom
, 0);
1472 tree
= new_seq(cond
, bra
);
1474 trueBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1475 tree
= new_seq(tree
, trueBody
);
1477 if (haveElseClause
) {
1479 slang_ir_node
*jump
, *elseLab
, *falseBody
;
1480 jump
= new_jump(endifAtom
);
1481 tree
= new_seq(tree
, jump
);
1483 elseLab
= new_label(elseAtom
);
1484 tree
= new_seq(tree
, elseLab
);
1486 falseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1487 tree
= new_seq(tree
, falseBody
);
1490 endifLab
= new_label(endifAtom
);
1491 tree
= new_seq(tree
, endifLab
);
1498 * Determine if the given operation is of a specific type.
1501 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1503 if (oper
->type
== type
)
1505 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1506 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1507 oper
->num_children
== 1)
1508 return is_operation_type(&oper
->children
[0], type
);
1515 * Generate IR tree for an if/then/else conditional using high-level
1516 * IR_IF instruction.
1518 static slang_ir_node
*
1519 _slang_gen_hl_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1522 * eval expr (child[0]), updating condcodes
1529 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1530 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1532 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1533 cond
= new_cond(cond
);
1535 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1536 /* Special case: generate a conditional break */
1537 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1538 if (haveElseClause
) {
1539 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1540 return new_seq(ifBody
, elseBody
);
1544 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1545 /* Special case: generate a conditional break */
1546 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1547 if (haveElseClause
) {
1548 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1549 return new_seq(ifBody
, elseBody
);
1555 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1557 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1560 ifNode
= new_if(cond
, ifBody
, elseBody
);
1568 * Generate IR node for storage of a temporary of given size.
1570 static slang_ir_node
*
1571 _slang_gen_temporary(GLint size
)
1573 slang_ir_storage
*store
;
1576 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1578 n
= new_node0(IR_VAR_DECL
);
1591 * Generate IR node for allocating/declaring a variable.
1593 static slang_ir_node
*
1594 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1597 assert(!is_sampler_type(&var
->type
));
1598 n
= new_node0(IR_VAR_DECL
);
1600 _slang_attach_storage(n
, var
);
1603 assert(n
->Store
== var
->aux
);
1605 assert(n
->Store
->Index
< 0);
1607 n
->Store
->File
= PROGRAM_TEMPORARY
;
1608 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1609 assert(n
->Store
->Size
> 0);
1618 * Generate code for a selection expression: b ? x : y
1619 * XXX in some cases we could implement a selection expression
1620 * with an LRP instruction (use the boolean as the interpolant).
1622 static slang_ir_node
*
1623 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1625 slang_atom altAtom
= slang_atom_pool_gen(A
->atoms
, "__selectAlt");
1626 slang_atom endAtom
= slang_atom_pool_gen(A
->atoms
, "__selectEnd");
1627 slang_ir_node
*altLab
, *endLab
;
1628 slang_ir_node
*tree
, *tmpDecl
, *tmpVar
, *cond
, *cjump
, *jump
;
1629 slang_ir_node
*bodx
, *body
, *assignx
, *assigny
;
1630 slang_typeinfo type
;
1633 assert(oper
->type
== SLANG_OPER_SELECT
);
1634 assert(oper
->num_children
== 3);
1636 /* size of x or y's type */
1637 slang_typeinfo_construct(&type
);
1638 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1639 size
= _slang_sizeof_type_specifier(&type
.spec
);
1643 tmpDecl
= _slang_gen_temporary(size
);
1645 /* eval condition */
1646 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1647 cond
= new_cond(cond
);
1648 tree
= new_seq(tmpDecl
, cond
);
1650 /* jump if false to "alt" label */
1651 cjump
= new_cjump(altAtom
, 0);
1652 tree
= new_seq(tree
, cjump
);
1654 /* evaluate child 1 (x) and assign to tmp */
1655 tmpVar
= new_node0(IR_VAR
);
1656 tmpVar
->Store
= tmpDecl
->Store
;
1657 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1658 assigny
= new_node2(IR_MOVE
, tmpVar
, body
);
1659 tree
= new_seq(tree
, assigny
);
1661 /* jump to "end" label */
1662 jump
= new_jump(endAtom
);
1663 tree
= new_seq(tree
, jump
);
1666 altLab
= new_label(altAtom
);
1667 tree
= new_seq(tree
, altLab
);
1669 /* evaluate child 2 (y) and assign to tmp */
1670 tmpVar
= new_node0(IR_VAR
);
1671 tmpVar
->Store
= tmpDecl
->Store
;
1672 bodx
= _slang_gen_operation(A
, &oper
->children
[2]);
1673 assignx
= new_node2(IR_MOVE
, tmpVar
, bodx
);
1674 tree
= new_seq(tree
, assignx
);
1677 endLab
= new_label(endAtom
);
1678 tree
= new_seq(tree
, endLab
);
1681 tmpVar
= new_node0(IR_VAR
);
1682 tmpVar
->Store
= tmpDecl
->Store
;
1683 tree
= new_seq(tree
, tmpVar
);
1690 * Generate code for &&.
1692 static slang_ir_node
*
1693 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1695 /* rewrite "a && b" as "a ? b : false" */
1696 slang_operation
*select
;
1699 select
= slang_operation_new(1);
1700 select
->type
= SLANG_OPER_SELECT
;
1701 select
->num_children
= 3;
1702 select
->children
= slang_operation_new(3);
1704 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1705 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1706 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1707 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0);
1708 select
->children
[2].literal_size
= 2;
1710 n
= _slang_gen_select(A
, select
);
1713 free(select
->children
);
1721 * Generate code for ||.
1723 static slang_ir_node
*
1724 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1726 /* rewrite "a || b" as "a ? true : b" */
1727 slang_operation
*select
;
1730 select
= slang_operation_new(1);
1731 select
->type
= SLANG_OPER_SELECT
;
1732 select
->num_children
= 3;
1733 select
->children
= slang_operation_new(3);
1735 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1736 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1737 ASSIGN_4V(select
->children
[2].literal
, 1, 1, 1, 1);
1738 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1739 select
->children
[2].literal_size
= 2;
1741 n
= _slang_gen_select(A
, select
);
1744 free(select
->children
);
1753 * Generate IR tree for a return statement.
1755 static slang_ir_node
*
1756 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1758 if (oper
->num_children
== 0 ||
1759 (oper
->num_children
== 1 &&
1760 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1764 * goto __endOfFunction;
1767 slang_operation gotoOp
;
1768 slang_operation_construct(&gotoOp
);
1769 gotoOp
.type
= SLANG_OPER_GOTO
;
1770 /* XXX don't call function? */
1771 gotoOp
.a_id
= slang_atom_pool_atom(A
->atoms
,
1772 (char *) A
->CurFunction
->end_label
);
1773 /* assemble the new code */
1774 n
= _slang_gen_operation(A
, &gotoOp
);
1775 /* destroy temp code */
1776 slang_operation_destruct(&gotoOp
);
1785 * goto __endOfFunction;
1787 slang_operation
*block
, *assign
, *jump
;
1788 slang_atom a_retVal
;
1791 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1797 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1802 block
= slang_operation_new(1);
1803 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1804 block
->num_children
= 2;
1805 block
->children
= slang_operation_new(2);
1806 assert(block
->locals
);
1807 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1809 /* child[0]: __retVal = expr; */
1810 assign
= &block
->children
[0];
1811 assign
->type
= SLANG_OPER_ASSIGN
;
1812 assign
->locals
->outer_scope
= block
->locals
;
1813 assign
->num_children
= 2;
1814 assign
->children
= slang_operation_new(2);
1815 /* lhs (__retVal) */
1816 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1817 assign
->children
[0].a_id
= a_retVal
;
1818 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1820 /* XXX we might be able to avoid this copy someday */
1821 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1823 /* child[1]: goto __endOfFunction */
1824 jump
= &block
->children
[1];
1825 jump
->type
= SLANG_OPER_GOTO
;
1826 assert(A
->CurFunction
->end_label
);
1827 /* XXX don't call function? */
1828 jump
->a_id
= slang_atom_pool_atom(A
->atoms
,
1829 (char *) A
->CurFunction
->end_label
);
1832 printf("NEW RETURN:\n");
1833 slang_print_tree(block
, 0);
1836 /* assemble the new code */
1837 n
= _slang_gen_operation(A
, block
);
1838 slang_operation_delete(block
);
1845 * Generate IR tree for a variable declaration.
1847 static slang_ir_node
*
1848 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1851 slang_ir_node
*varDecl
;
1853 const char *varName
= (char *) oper
->a_id
;
1855 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1857 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1860 varDecl
= _slang_gen_var_decl(A
, v
);
1862 if (oper
->num_children
> 0) {
1863 /* child is initializer */
1864 slang_ir_node
*var
, *init
, *rhs
;
1865 assert(oper
->num_children
== 1);
1866 var
= new_var(A
, oper
, oper
->a_id
);
1868 RETURN_ERROR2("Undefined variable:", varName
, 0);
1870 /* XXX make copy of this initializer? */
1871 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1873 init
= new_node2(IR_MOVE
, var
, rhs
);
1874 /*assert(rhs->Opcode != IR_SEQ);*/
1875 n
= new_seq(varDecl
, init
);
1877 else if (v
->initializer
) {
1878 slang_ir_node
*var
, *init
, *rhs
;
1879 var
= new_var(A
, oper
, oper
->a_id
);
1881 RETURN_ERROR2("Undefined variable:", varName
, 0);
1884 /* XXX make copy of this initializer? */
1886 slang_operation dup
;
1887 slang_operation_construct(&dup
);
1888 slang_operation_copy(&dup
, v
->initializer
);
1889 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1890 rhs
= _slang_gen_operation(A
, &dup
);
1893 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1894 rhs
= _slang_gen_operation(A
, v
->initializer
);
1897 init
= new_node2(IR_MOVE
, var
, rhs
);
1899 assert(rhs->Opcode != IR_SEQ);
1901 n
= new_seq(varDecl
, init
);
1911 * Generate IR tree for a variable (such as in an expression).
1913 static slang_ir_node
*
1914 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1916 /* If there's a variable associated with this oper (from inlining)
1917 * use it. Otherwise, use the oper's var id.
1919 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1920 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1922 RETURN_ERROR2("Undefined variable:", (char *) aVar
, 0);
1929 * Some write-masked assignments are simple, but others are hard.
1932 * v.xy = vec2(a, b);
1935 * v.yz = vec2(a, b);
1936 * this would have to be transformed/swizzled into:
1937 * v.yz = vec2(a, b).*xy* (* = don't care)
1938 * Instead, we'll effectively do this:
1939 * v.y = vec2(a, b).xxxx;
1940 * v.z = vec2(a, b).yyyy;
1944 _slang_simple_writemask(GLuint writemask
)
1946 switch (writemask
) {
1953 case WRITEMASK_XYZW
:
1962 * Convert the given swizzle into a writemask. In some cases this
1963 * is trivial, in other cases, we'll need to also swizzle the right
1964 * hand side to put components in the right places.
1965 * \param swizzle the incoming swizzle
1966 * \param writemaskOut returns the writemask
1967 * \param swizzleOut swizzle to apply to the right-hand-side
1968 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1971 swizzle_to_writemask(GLuint swizzle
,
1972 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1974 GLuint mask
= 0x0, newSwizzle
[4];
1977 /* make new dst writemask, compute size */
1978 for (i
= 0; i
< 4; i
++) {
1979 const GLuint swz
= GET_SWZ(swizzle
, i
);
1980 if (swz
== SWIZZLE_NIL
) {
1984 assert(swz
>= 0 && swz
<= 3);
1987 assert(mask
<= 0xf);
1988 size
= i
; /* number of components in mask/swizzle */
1990 *writemaskOut
= mask
;
1992 /* make new src swizzle, by inversion */
1993 for (i
= 0; i
< 4; i
++) {
1994 newSwizzle
[i
] = i
; /*identity*/
1996 for (i
= 0; i
< size
; i
++) {
1997 const GLuint swz
= GET_SWZ(swizzle
, i
);
1998 newSwizzle
[swz
] = i
;
2000 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
2005 if (_slang_simple_writemask(mask
)) {
2007 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
2009 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
2011 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
2013 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
2021 static slang_ir_node
*
2022 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2024 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2026 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
2027 n
->Store
->Swizzle
= swizzle
;
2034 * Generate IR tree for an assignment (=).
2036 static slang_ir_node
*
2037 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2039 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2040 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2041 /* Special case of: x = f(a, b)
2042 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2044 * XXX this could be even more effective if we could accomodate
2045 * cases such as "v.x = f();" - would help with typical vertex
2049 n
= _slang_gen_function_call_name(A
,
2050 (const char *) oper
->children
[1].a_id
,
2051 &oper
->children
[1], &oper
->children
[0]);
2055 slang_ir_node
*n
, *lhs
, *rhs
;
2056 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2057 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2059 /* convert lhs swizzle into writemask */
2060 GLuint writemask
, newSwizzle
;
2061 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2062 &writemask
, &newSwizzle
)) {
2063 /* Non-simple writemask, need to swizzle right hand side in
2064 * order to put components into the right place.
2066 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2068 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2069 n
->Writemask
= writemask
;
2080 * Generate IR tree for referencing a field in a struct (or basic vector type)
2082 static slang_ir_node
*
2083 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2087 slang_typeinfo_construct(&ti
);
2088 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2090 if (_slang_type_is_vector(ti
.spec
.type
)) {
2091 /* the field should be a swizzle */
2092 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2096 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2097 RETURN_ERROR("Bad swizzle", 0);
2099 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2104 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2105 /* create new parent node with swizzle */
2106 n
= _slang_gen_swizzle(n
, swizzle
);
2109 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2110 const GLuint rows
= 1;
2114 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2115 RETURN_ERROR("Bad swizzle", 0);
2117 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2121 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2122 /* create new parent node with swizzle */
2123 n
= _slang_gen_swizzle(n
, swizzle
);
2127 /* the field is a structure member (base.field) */
2128 /* oper->children[0] is the base */
2129 /* oper->a_id is the field name */
2130 slang_ir_node
*base
, *n
;
2131 GLint size
= 4; /* XXX fix? */
2133 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2135 n
= new_node1(IR_FIELD
, base
);
2137 n
->Target
= (char *) oper
->a_id
;
2138 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2145 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2153 * Gen code for array indexing.
2155 static slang_ir_node
*
2156 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2158 slang_typeinfo array_ti
;
2160 /* get array's type info */
2161 slang_typeinfo_construct(&array_ti
);
2162 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2164 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2165 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2166 /* translate the index into a swizzle/writemask: "v.x=p" */
2167 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2171 index
= (GLint
) oper
->children
[1].literal
[0];
2172 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2174 RETURN_ERROR("Invalid array index for vector type", 0);
2177 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2179 /* use swizzle to access the element */
2180 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2184 n
= _slang_gen_swizzle(n
, swizzle
);
2185 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2186 n
->Writemask
= WRITEMASK_X
<< index
;
2191 /* conventional array */
2192 slang_typeinfo elem_ti
;
2193 slang_ir_node
*elem
, *array
, *index
;
2196 /* size of array element */
2197 slang_typeinfo_construct(&elem_ti
);
2198 _slang_typeof_operation(A
, oper
, &elem_ti
);
2199 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2200 assert(elemSize
>= 1);
2202 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2203 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2204 if (array
&& index
) {
2205 elem
= new_node2(IR_ELEMENT
, array
, index
);
2206 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2207 array
->Store
->Index
,
2209 /* XXX try to do some array bounds checking here */
2221 * Generate IR tree for a slang_operation (AST node)
2223 static slang_ir_node
*
2224 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2226 switch (oper
->type
) {
2227 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2231 _slang_push_var_table(A
->vartable
);
2233 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2234 n
= _slang_gen_operation(A
, oper
);
2235 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2237 _slang_pop_var_table(A
->vartable
);
2240 n
= new_node1(IR_SCOPE
, n
);
2245 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2246 /* list of operations */
2247 if (oper
->num_children
> 0)
2249 slang_ir_node
*n
, *tree
= NULL
;
2252 for (i
= 0; i
< oper
->num_children
; i
++) {
2253 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2255 _slang_free_ir_tree(tree
);
2256 return NULL
; /* error must have occured */
2258 tree
= tree
? new_seq(tree
, n
) : n
;
2262 if (oper
->locals
->num_variables
> 0) {
2265 printf("\n****** Deallocate vars in scope!\n");
2267 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2268 slang_variable
*v
= oper
->locals
->variables
+ i
;
2270 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2272 printf(" Deallocate var %s\n", (char*) v->a_name);
2274 assert(store
->File
== PROGRAM_TEMPORARY
);
2275 assert(store
->Index
>= 0);
2276 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2284 case SLANG_OPER_EXPRESSION
:
2285 return _slang_gen_operation(A
, &oper
->children
[0]);
2287 case SLANG_OPER_FOR
:
2288 return _slang_gen_for(A
, oper
);
2290 return _slang_gen_do(A
, oper
);
2291 case SLANG_OPER_WHILE
:
2292 return _slang_gen_while(A
, oper
);
2293 case SLANG_OPER_BREAK
:
2295 RETURN_ERROR("'break' not in loop", 0);
2297 return new_break(A
->CurLoop
);
2298 case SLANG_OPER_CONTINUE
:
2300 RETURN_ERROR("'continue' not in loop", 0);
2302 return new_cont(A
->CurLoop
);
2303 case SLANG_OPER_DISCARD
:
2304 return new_node0(IR_KILL
);
2306 case SLANG_OPER_EQUAL
:
2307 return new_node2(IR_SEQUAL
,
2308 _slang_gen_operation(A
, &oper
->children
[0]),
2309 _slang_gen_operation(A
, &oper
->children
[1]));
2310 case SLANG_OPER_NOTEQUAL
:
2311 return new_node2(IR_SNEQUAL
,
2312 _slang_gen_operation(A
, &oper
->children
[0]),
2313 _slang_gen_operation(A
, &oper
->children
[1]));
2314 case SLANG_OPER_GREATER
:
2315 return new_node2(IR_SGT
,
2316 _slang_gen_operation(A
, &oper
->children
[0]),
2317 _slang_gen_operation(A
, &oper
->children
[1]));
2318 case SLANG_OPER_LESS
:
2319 /* child[0] < child[1] ----> child[1] > child[0] */
2320 return new_node2(IR_SGT
,
2321 _slang_gen_operation(A
, &oper
->children
[1]),
2322 _slang_gen_operation(A
, &oper
->children
[0]));
2323 case SLANG_OPER_GREATERequal
:
2324 return new_node2(IR_SGE
,
2325 _slang_gen_operation(A
, &oper
->children
[0]),
2326 _slang_gen_operation(A
, &oper
->children
[1]));
2327 case SLANG_OPER_LESSequal
:
2328 /* child[0] <= child[1] ----> child[1] >= child[0] */
2329 return new_node2(IR_SGE
,
2330 _slang_gen_operation(A
, &oper
->children
[1]),
2331 _slang_gen_operation(A
, &oper
->children
[0]));
2332 case SLANG_OPER_ADD
:
2335 assert(oper
->num_children
== 2);
2336 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2339 case SLANG_OPER_SUBTRACT
:
2342 assert(oper
->num_children
== 2);
2343 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2346 case SLANG_OPER_MULTIPLY
:
2349 assert(oper
->num_children
== 2);
2350 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2353 case SLANG_OPER_DIVIDE
:
2356 assert(oper
->num_children
== 2);
2357 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2360 case SLANG_OPER_MINUS
:
2363 assert(oper
->num_children
== 1);
2364 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2367 case SLANG_OPER_PLUS
:
2368 /* +expr --> do nothing */
2369 return _slang_gen_operation(A
, &oper
->children
[0]);
2370 case SLANG_OPER_VARIABLE_DECL
:
2371 return _slang_gen_declaration(A
, oper
);
2372 case SLANG_OPER_ASSIGN
:
2373 return _slang_gen_assignment(A
, oper
);
2374 case SLANG_OPER_ADDASSIGN
:
2377 assert(oper
->num_children
== 2);
2378 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2381 case SLANG_OPER_SUBASSIGN
:
2384 assert(oper
->num_children
== 2);
2385 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2389 case SLANG_OPER_MULASSIGN
:
2392 assert(oper
->num_children
== 2);
2393 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2396 case SLANG_OPER_DIVASSIGN
:
2399 assert(oper
->num_children
== 2);
2400 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2403 case SLANG_OPER_LOGICALAND
:
2406 assert(oper
->num_children
== 2);
2407 n
= _slang_gen_logical_and(A
, oper
);
2410 case SLANG_OPER_LOGICALOR
:
2413 assert(oper
->num_children
== 2);
2414 n
= _slang_gen_logical_or(A
, oper
);
2417 case SLANG_OPER_LOGICALXOR
:
2420 assert(oper
->num_children
== 2);
2421 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2424 case SLANG_OPER_NOT
:
2427 assert(oper
->num_children
== 1);
2428 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2432 case SLANG_OPER_SELECT
: /* b ? x : y */
2435 assert(oper
->num_children
== 3);
2436 n
= _slang_gen_select(A
, oper
);
2440 case SLANG_OPER_ASM
:
2441 return _slang_gen_asm(A
, oper
, NULL
);
2442 case SLANG_OPER_CALL
:
2443 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2445 case SLANG_OPER_RETURN
:
2446 return _slang_gen_return(A
, oper
);
2447 case SLANG_OPER_GOTO
:
2448 return new_jump((char*) oper
->a_id
);
2449 case SLANG_OPER_LABEL
:
2450 return new_label((char*) oper
->a_id
);
2451 case SLANG_OPER_IDENTIFIER
:
2452 return _slang_gen_variable(A
, oper
);
2454 if (A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2455 return _slang_gen_hl_if(A
, oper
);
2458 /* XXX update tnl executor */
2459 return _slang_gen_if(A
, oper
);
2461 case SLANG_OPER_FIELD
:
2462 return _slang_gen_field(A
, oper
);
2463 case SLANG_OPER_SUBSCRIPT
:
2464 return _slang_gen_subscript(A
, oper
);
2465 case SLANG_OPER_LITERAL_FLOAT
:
2467 case SLANG_OPER_LITERAL_INT
:
2469 case SLANG_OPER_LITERAL_BOOL
:
2470 return new_float_literal(oper
->literal
);
2472 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2475 assert(oper
->num_children
== 1);
2476 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2479 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2482 assert(oper
->num_children
== 1);
2483 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2486 case SLANG_OPER_PREINCREMENT
: /* ++var */
2489 assert(oper
->num_children
== 1);
2490 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2493 case SLANG_OPER_PREDECREMENT
: /* --var */
2496 assert(oper
->num_children
== 1);
2497 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2501 case SLANG_OPER_SEQUENCE
:
2503 slang_ir_node
*tree
= NULL
;
2505 for (i
= 0; i
< oper
->num_children
; i
++) {
2506 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2507 tree
= tree
? new_seq(tree
, n
) : n
;
2512 case SLANG_OPER_NONE
:
2514 case SLANG_OPER_VOID
:
2518 printf("Unhandled node type %d\n", oper
->type
);
2520 return new_node0(IR_NOP
);
2529 * Called by compiler when a global variable has been parsed/compiled.
2530 * Here we examine the variable's type to determine what kind of register
2531 * storage will be used.
2533 * A uniform such as "gl_Position" will become the register specification
2534 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2535 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2537 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2538 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2539 * actual texture unit (as specified by the user calling glUniform1i()).
2542 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2543 slang_unit_type type
)
2545 struct gl_program
*prog
= A
->program
;
2546 const char *varName
= (char *) var
->a_name
;
2547 GLboolean success
= GL_TRUE
;
2549 slang_ir_storage
*store
= NULL
;
2552 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2554 if (texIndex
!= -1) {
2556 * store->File = PROGRAM_SAMPLER
2557 * store->Index = sampler uniform location
2558 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2560 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2561 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2562 if (dbg
) printf("SAMPLER ");
2564 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2565 /* Uniform variable */
2566 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2567 * MAX2(var
->array_len
, 1);
2569 /* user-defined uniform */
2570 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2571 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2574 /* pre-defined uniform, like gl_ModelviewMatrix */
2575 /* We know it's a uniform, but don't allocate storage unless
2578 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2580 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2582 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2583 const GLint size
= 4; /* XXX fix */
2585 /* user-defined varying */
2586 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2587 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2590 /* pre-defined varying, like gl_Color or gl_TexCoord */
2591 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2592 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2594 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2595 assert(index
< FRAG_ATTRIB_MAX
);
2598 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2600 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2601 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2602 assert(index
< VERT_RESULT_MAX
);
2604 if (dbg
) printf("V/F ");
2606 if (dbg
) printf("VARYING ");
2608 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2610 /* user-defined vertex attribute */
2611 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2612 const GLint attr
= -1; /* unknown */
2613 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2616 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2617 VERT_ATTRIB_GENERIC0
+ index
, size
);
2620 /* pre-defined vertex attrib */
2621 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2622 GLint size
= 4; /* XXX? */
2624 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2626 if (dbg
) printf("ATTRIB ");
2628 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2629 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2630 GLint size
= 4; /* XXX? */
2631 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2632 if (dbg
) printf("INPUT ");
2634 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2635 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2636 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2637 GLint size
= 4; /* XXX? */
2638 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2641 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2642 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2643 GLint size
= 4; /* XXX? */
2644 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2646 if (dbg
) printf("OUTPUT ");
2648 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2649 /* pre-defined global constant, like gl_MaxLights */
2650 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2651 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2652 if (dbg
) printf("CONST ");
2655 /* ordinary variable (may be const) */
2658 /* IR node to declare the variable */
2659 n
= _slang_gen_var_decl(A
, var
);
2661 /* IR code for the var's initializer, if present */
2662 if (var
->initializer
) {
2663 slang_ir_node
*lhs
, *rhs
, *init
;
2665 /* Generate IR_MOVE instruction to initialize the variable */
2666 lhs
= new_node0(IR_VAR
);
2668 lhs
->Store
= n
->Store
;
2670 /* constant folding, etc */
2671 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2673 rhs
= _slang_gen_operation(A
, var
->initializer
);
2675 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2676 n
= new_seq(n
, init
);
2679 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
);
2681 _slang_free_ir_tree(n
);
2684 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2685 store
? store
->Index
: -2);
2688 var
->aux
= store
; /* save var's storage info */
2695 * Produce an IR tree from a function AST (fun->body).
2696 * Then call the code emitter to convert the IR tree into gl_program
2700 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2702 slang_ir_node
*n
, *endLabel
;
2703 GLboolean success
= GL_TRUE
;
2705 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2706 /* we only really generate code for main, all other functions get
2709 return GL_TRUE
; /* not an error */
2713 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2716 slang_print_function(fun
, 1);
2719 /* should have been allocated earlier: */
2720 assert(A
->program
->Parameters
);
2721 assert(A
->program
->Varying
);
2722 assert(A
->vartable
);
2724 /* fold constant expressions, etc. */
2725 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2727 A
->CurFunction
= fun
;
2729 /* Create an end-of-function label */
2730 if (!A
->CurFunction
->end_label
)
2731 A
->CurFunction
->end_label
= slang_atom_pool_gen(A
->atoms
, "__endOfFunc_main_");
2733 /* push new vartable scope */
2734 _slang_push_var_table(A
->vartable
);
2736 /* Generate IR tree for the function body code */
2737 n
= _slang_gen_operation(A
, fun
->body
);
2739 n
= new_node1(IR_SCOPE
, n
);
2741 /* pop vartable, restore previous */
2742 _slang_pop_var_table(A
->vartable
);
2745 /* XXX record error */
2749 /* append an end-of-function-label to IR tree */
2750 endLabel
= new_label(fun
->end_label
);
2751 n
= new_seq(n
, endLabel
);
2753 A
->CurFunction
= NULL
;
2756 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2757 slang_print_function(fun
, 1);
2760 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2761 slang_print_ir(n
, 0);
2764 printf("************* End codegen function ************\n\n");
2767 /* Emit program instructions */
2768 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
);
2769 _slang_free_ir_tree(n
);
2771 /* free codegen context */
2773 _mesa_free(A->codegen);