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
);
58 * Lookup a named constant and allocate storage for the parameter in
59 * the given parameter list.
60 * \param swizzleOut returns swizzle mask for accessing the constant
61 * \return position of the constant in the paramList.
64 slang_lookup_constant(const char *name
,
65 struct gl_program_parameter_list
*paramList
,
68 GLint value
= _slang_lookup_constant(name
);
70 /* XXX named constant! */
71 GLfloat fvalue
= (GLfloat
) value
;
73 pos
= _mesa_add_unnamed_constant(paramList
, &fvalue
, 1, swizzleOut
);
81 is_sampler_type(const slang_fully_specified_type
*t
)
83 switch (t
->specifier
.type
) {
84 case SLANG_SPEC_SAMPLER1D
:
85 case SLANG_SPEC_SAMPLER2D
:
86 case SLANG_SPEC_SAMPLER3D
:
87 case SLANG_SPEC_SAMPLERCUBE
:
88 case SLANG_SPEC_SAMPLER1DSHADOW
:
89 case SLANG_SPEC_SAMPLER2DSHADOW
:
98 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
100 switch (spec
->type
) {
101 case SLANG_SPEC_VOID
:
103 case SLANG_SPEC_BOOL
:
105 case SLANG_SPEC_BVEC2
:
107 case SLANG_SPEC_BVEC3
:
109 case SLANG_SPEC_BVEC4
:
113 case SLANG_SPEC_IVEC2
:
115 case SLANG_SPEC_IVEC3
:
117 case SLANG_SPEC_IVEC4
:
119 case SLANG_SPEC_FLOAT
:
121 case SLANG_SPEC_VEC2
:
123 case SLANG_SPEC_VEC3
:
125 case SLANG_SPEC_VEC4
:
127 case SLANG_SPEC_MAT2
:
129 case SLANG_SPEC_MAT3
:
131 case SLANG_SPEC_MAT4
:
133 case SLANG_SPEC_SAMPLER1D
:
134 case SLANG_SPEC_SAMPLER2D
:
135 case SLANG_SPEC_SAMPLER3D
:
136 case SLANG_SPEC_SAMPLERCUBE
:
137 case SLANG_SPEC_SAMPLER1DSHADOW
:
138 case SLANG_SPEC_SAMPLER2DSHADOW
:
139 return 1; /* special case */
140 case SLANG_SPEC_STRUCT
:
143 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
144 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
145 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
146 /* XXX verify padding */
153 case SLANG_SPEC_ARRAY
:
154 return _slang_sizeof_type_specifier(spec
->_array
);
156 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
164 * Allocate storage info for an IR node (n->Store).
165 * If n is an IR_VAR_DECL, allocate a temporary for the variable.
166 * Otherwise, if n is an IR_VAR, check if it's a uniform or constant
167 * that needs to have storage allocated.
170 slang_allocate_storage(slang_assemble_ctx
*A
, slang_ir_node
*n
)
176 /* allocate storage info for this node */
177 if (n
->Var
&& n
->Var
->aux
) {
178 /* node storage info = var storage info */
179 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
182 /* alloc new storage info */
183 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
185 n
->Var
->aux
= n
->Store
;
190 if (n
->Opcode
== IR_VAR_DECL
) {
191 /* variable declaration */
193 assert(!is_sampler_type(&n
->Var
->type
));
194 n
->Store
->File
= PROGRAM_TEMPORARY
;
195 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
196 assert(n
->Store
->Size
> 0);
200 assert(n
->Opcode
== IR_VAR
);
203 if (n
->Store
->Index
< 0) {
204 const char *varName
= (char *) n
->Var
->a_name
;
205 struct gl_program
*prog
= A
->program
;
208 if (n
->Store
->File
== PROGRAM_CONSTANT
) {
209 /* XXX compile-time constants should be converted to literals */
210 GLint i
= slang_lookup_constant(varName
, prog
->Parameters
,
213 assert(n
->Store
->Size
== 1);
222 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
223 * or -1 if the type is not a sampler.
226 sampler_to_texture_index(const slang_type_specifier_type type
)
229 case SLANG_SPEC_SAMPLER1D
:
230 return TEXTURE_1D_INDEX
;
231 case SLANG_SPEC_SAMPLER2D
:
232 return TEXTURE_2D_INDEX
;
233 case SLANG_SPEC_SAMPLER3D
:
234 return TEXTURE_3D_INDEX
;
235 case SLANG_SPEC_SAMPLERCUBE
:
236 return TEXTURE_CUBE_INDEX
;
237 case SLANG_SPEC_SAMPLER1DSHADOW
:
238 return TEXTURE_1D_INDEX
; /* XXX fix */
239 case SLANG_SPEC_SAMPLER2DSHADOW
:
240 return TEXTURE_2D_INDEX
; /* XXX fix */
248 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
249 * a vertex or fragment program input variable. Return -1 if the input
251 * XXX return size too
254 _slang_input_index(const char *name
, GLenum target
)
260 static const struct input_info vertInputs
[] = {
261 { "gl_Vertex", VERT_ATTRIB_POS
},
262 { "gl_Normal", VERT_ATTRIB_NORMAL
},
263 { "gl_Color", VERT_ATTRIB_COLOR0
},
264 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
},
265 { "gl_FogCoord", VERT_ATTRIB_FOG
},
266 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
},
267 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
},
268 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
},
269 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
},
270 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
},
271 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
},
272 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
},
273 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
},
276 static const struct input_info fragInputs
[] = {
277 { "gl_FragCoord", FRAG_ATTRIB_WPOS
},
278 { "gl_Color", FRAG_ATTRIB_COL0
},
279 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
},
280 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
},
281 { "gl_TexCoord", FRAG_ATTRIB_TEX0
},
285 const struct input_info
*inputs
286 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
288 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
290 for (i
= 0; inputs
[i
].Name
; i
++) {
291 if (strcmp(inputs
[i
].Name
, name
) == 0) {
293 return inputs
[i
].Attrib
;
301 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
302 * a vertex or fragment program output variable. Return -1 for an invalid
306 _slang_output_index(const char *name
, GLenum target
)
312 static const struct output_info vertOutputs
[] = {
313 { "gl_Position", VERT_RESULT_HPOS
},
314 { "gl_FrontColor", VERT_RESULT_COL0
},
315 { "gl_BackColor", VERT_RESULT_BFC0
},
316 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
317 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
318 { "gl_TexCoord", VERT_RESULT_TEX0
}, /* XXX indexed */
319 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
320 { "gl_PointSize", VERT_RESULT_PSIZ
},
323 static const struct output_info fragOutputs
[] = {
324 { "gl_FragColor", FRAG_RESULT_COLR
},
325 { "gl_FragDepth", FRAG_RESULT_DEPR
},
329 const struct output_info
*outputs
330 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
332 for (i
= 0; outputs
[i
].Name
; i
++) {
333 if (strcmp(outputs
[i
].Name
, name
) == 0) {
335 return outputs
[i
].Attrib
;
343 /**********************************************************************/
347 * Map "_asm foo" to IR_FOO, etc.
352 slang_ir_opcode Opcode
;
353 GLuint HaveRetValue
, NumParams
;
357 static slang_asm_info AsmInfo
[] = {
359 { "vec4_add", IR_ADD
, 1, 2 },
360 { "vec4_subtract", IR_SUB
, 1, 2 },
361 { "vec4_multiply", IR_MUL
, 1, 2 },
362 { "vec4_dot", IR_DOT4
, 1, 2 },
363 { "vec3_dot", IR_DOT3
, 1, 2 },
364 { "vec3_cross", IR_CROSS
, 1, 2 },
365 { "vec4_lrp", IR_LRP
, 1, 3 },
366 { "vec4_min", IR_MIN
, 1, 2 },
367 { "vec4_max", IR_MAX
, 1, 2 },
368 { "vec4_clamp", IR_CLAMP
, 1, 3 },
369 { "vec4_seq", IR_SEQ
, 1, 2 },
370 { "vec4_sge", IR_SGE
, 1, 2 },
371 { "vec4_sgt", IR_SGT
, 1, 2 },
373 { "vec4_floor", IR_FLOOR
, 1, 1 },
374 { "vec4_frac", IR_FRAC
, 1, 1 },
375 { "vec4_abs", IR_ABS
, 1, 1 },
376 { "vec4_negate", IR_NEG
, 1, 1 },
377 { "vec4_ddx", IR_DDX
, 1, 1 },
378 { "vec4_ddy", IR_DDY
, 1, 1 },
379 /* float binary op */
380 { "float_add", IR_ADD
, 1, 2 },
381 { "float_multiply", IR_MUL
, 1, 2 },
382 { "float_divide", IR_DIV
, 1, 2 },
383 { "float_power", IR_POW
, 1, 2 },
384 /* texture / sampler */
385 { "vec4_tex1d", IR_TEX
, 1, 2 },
386 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
387 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
388 { "vec4_tex2d", IR_TEX
, 1, 2 },
389 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
390 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
391 { "vec4_tex3d", IR_TEX
, 1, 2 },
392 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
393 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
394 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
397 { "int_to_float", IR_I_TO_F
, 1, 1 },
398 { "float_to_int", IR_F_TO_I
, 1, 1 },
399 { "float_exp", IR_EXP
, 1, 1 },
400 { "float_exp2", IR_EXP2
, 1, 1 },
401 { "float_log2", IR_LOG2
, 1, 1 },
402 { "float_rsq", IR_RSQ
, 1, 1 },
403 { "float_rcp", IR_RCP
, 1, 1 },
404 { "float_sine", IR_SIN
, 1, 1 },
405 { "float_cosine", IR_COS
, 1, 1 },
406 { "float_noise1", IR_NOISE1
, 1, 1},
407 { "float_noise2", IR_NOISE2
, 1, 1},
408 { "float_noise3", IR_NOISE3
, 1, 1},
409 { "float_noise4", IR_NOISE4
, 1, 1},
411 { NULL
, IR_NOP
, 0, 0 }
416 * Recursively free an IR tree.
419 _slang_free_ir_tree(slang_ir_node
*n
)
425 for (i
= 0; i
< 3; i
++)
426 _slang_free_ir_tree(n
->Children
[i
]);
427 /* Do not free n->BranchNode since it's a child elsewhere */
433 static slang_ir_node
*
434 new_node3(slang_ir_opcode op
,
435 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
437 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
443 n
->Writemask
= WRITEMASK_XYZW
;
444 n
->InstLocation
= -1;
449 static slang_ir_node
*
450 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
452 return new_node3(op
, c0
, c1
, NULL
);
455 static slang_ir_node
*
456 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
458 return new_node3(op
, c0
, NULL
, NULL
);
461 static slang_ir_node
*
462 new_node0(slang_ir_opcode op
)
464 return new_node3(op
, NULL
, NULL
, NULL
);
468 static slang_ir_node
*
469 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
475 return new_node2(IR_SEQ
, left
, right
);
478 static slang_ir_node
*
479 new_label(slang_atom labName
)
481 slang_ir_node
*n
= new_node0(IR_LABEL
);
482 n
->Target
= (char *) labName
; /*_mesa_strdup(name);*/
486 static slang_ir_node
*
487 new_float_literal(const float v
[4])
489 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
490 slang_ir_node
*n
= new_node0(IR_FLOAT
);
491 COPY_4V(n
->Value
, v
);
492 /* allocate a storage object, but compute actual location (Index) later */
493 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
499 * \param zeroOrOne indicates if the jump is to be taken on zero, or non-zero
500 * condition code state.
501 * XXX maybe pass an IR node as second param to indicate the jump target???
503 static slang_ir_node
*
504 new_cjump(slang_atom target
, GLuint zeroOrOne
)
506 slang_ir_node
*n
= new_node0(zeroOrOne
? IR_CJUMP1
: IR_CJUMP0
);
508 n
->Target
= (char *) target
;
513 * Unconditional jump.
514 * XXX maybe pass an IR node as second param to indicate the jump target???
516 static slang_ir_node
*
517 new_jump(slang_atom target
)
519 slang_ir_node
*n
= new_node0(IR_JUMP
);
521 n
->Target
= (char *) target
;
526 static slang_ir_node
*
527 new_loop(slang_ir_node
*body
)
529 return new_node1(IR_LOOP
, body
);
533 static slang_ir_node
*
534 new_break(slang_ir_node
*loopNode
)
536 slang_ir_node
*n
= new_node0(IR_BREAK
);
538 assert(loopNode
->Opcode
== IR_LOOP
);
540 /* insert this node at head of linked list */
541 n
->BranchNode
= loopNode
->BranchNode
;
542 loopNode
->BranchNode
= n
;
549 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
551 static slang_ir_node
*
552 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
556 assert(loopNode
->Opcode
== IR_LOOP
);
557 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
559 /* insert this node at head of linked list */
560 n
->BranchNode
= loopNode
->BranchNode
;
561 loopNode
->BranchNode
= n
;
568 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
570 static slang_ir_node
*
571 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
575 assert(loopNode
->Opcode
== IR_LOOP
);
576 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
578 /* insert this node at head of linked list */
579 n
->BranchNode
= loopNode
->BranchNode
;
580 loopNode
->BranchNode
= n
;
586 static slang_ir_node
*
587 new_cont(slang_ir_node
*loopNode
)
589 slang_ir_node
*n
= new_node0(IR_CONT
);
591 assert(loopNode
->Opcode
== IR_LOOP
);
593 /* insert this node at head of linked list */
594 n
->BranchNode
= loopNode
->BranchNode
;
595 loopNode
->BranchNode
= n
;
601 static slang_ir_node
*
602 new_cond(slang_ir_node
*n
)
604 slang_ir_node
*c
= new_node1(IR_COND
, n
);
609 static slang_ir_node
*
610 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
612 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
617 * New IR_VAR node - a reference to a previously declared variable.
619 static slang_ir_node
*
620 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
622 slang_variable
*v
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
623 slang_ir_node
*n
= new_node0(IR_VAR
);
626 assert(!oper
->var
|| oper
->var
== v
);
629 slang_allocate_storage(A
, n
);
636 * Check if the given function is really just a wrapper for a
637 * basic assembly instruction.
640 slang_is_asm_function(const slang_function
*fun
)
642 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
643 fun
->body
->num_children
== 1 &&
644 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
652 * Produce inline code for a call to an assembly instruction.
654 static slang_operation
*
655 slang_inline_asm_function(slang_assemble_ctx
*A
,
656 slang_function
*fun
, slang_operation
*oper
)
658 const GLuint numArgs
= oper
->num_children
;
659 const slang_operation
*args
= oper
->children
;
661 slang_operation
*inlined
= slang_operation_new(1);
663 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
665 printf("Inline asm %s\n", (char*) fun->header.a_name);
667 inlined
->type
= fun
->body
->children
[0].type
;
668 inlined
->a_id
= fun
->body
->children
[0].a_id
;
669 inlined
->num_children
= numArgs
;
670 inlined
->children
= slang_operation_new(numArgs
);
672 inlined
->locals
= slang_variable_scope_copy(oper
->locals
);
674 assert(inlined
->locals
);
675 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
678 for (i
= 0; i
< numArgs
; i
++) {
679 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
687 slang_resolve_variable(slang_operation
*oper
)
689 if (oper
->type
!= SLANG_OPER_IDENTIFIER
)
692 oper
->var
= _slang_locate_variable(oper
->locals
,
693 (const slang_atom
) oper
->a_id
,
696 oper
->var
->used
= GL_TRUE
;
702 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
705 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
706 GLuint substCount
, slang_variable
**substOld
,
707 slang_operation
**substNew
, GLboolean isLHS
)
709 switch (oper
->type
) {
710 case SLANG_OPER_VARIABLE_DECL
:
712 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
713 oper
->a_id
, GL_TRUE
);
715 if (v
->initializer
&& oper
->num_children
== 0) {
716 /* set child of oper to copy of initializer */
717 oper
->num_children
= 1;
718 oper
->children
= slang_operation_new(1);
719 slang_operation_copy(&oper
->children
[0], v
->initializer
);
721 if (oper
->num_children
== 1) {
722 /* the initializer */
723 slang_substitute(A
, &oper
->children
[0], substCount
, substOld
, substNew
, GL_FALSE
);
727 case SLANG_OPER_IDENTIFIER
:
728 assert(oper
->num_children
== 0);
729 if (1/**!isLHS XXX FIX */) {
730 slang_atom id
= oper
->a_id
;
733 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
735 printf("var %s not found!\n", (char *) oper
->a_id
);
736 _slang_print_var_scope(oper
->locals
, 6);
742 /* look for a substitution */
743 for (i
= 0; i
< substCount
; i
++) {
744 if (v
== substOld
[i
]) {
745 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
746 #if 0 /* DEBUG only */
747 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
748 assert(substNew
[i
]->var
);
749 assert(substNew
[i
]->var
->a_name
);
750 printf("Substitute %s with %s in id node %p\n",
751 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
755 printf("Substitute %s with %f in id node %p\n",
756 (char*)v
->a_name
, substNew
[i
]->literal
[0],
760 slang_operation_copy(oper
, substNew
[i
]);
766 #if 1 /* XXX rely on default case below */
767 case SLANG_OPER_RETURN
:
768 /* do return replacement here too */
769 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
770 if (oper
->num_children
== 1) {
776 * then do substitutions on the assignment.
778 slang_operation
*blockOper
, *assignOper
, *returnOper
;
779 blockOper
= slang_operation_new(1);
780 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
781 blockOper
->num_children
= 2;
782 blockOper
->children
= slang_operation_new(2);
783 assignOper
= blockOper
->children
+ 0;
784 returnOper
= blockOper
->children
+ 1;
786 assignOper
->type
= SLANG_OPER_ASSIGN
;
787 assignOper
->num_children
= 2;
788 assignOper
->children
= slang_operation_new(2);
789 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
790 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
791 assignOper
->children
[0].locals
->outer_scope
= oper
->locals
;
792 assignOper
->locals
= oper
->locals
;
793 slang_operation_copy(&assignOper
->children
[1],
796 returnOper
->type
= SLANG_OPER_RETURN
;
797 assert(returnOper
->num_children
== 0);
799 /* do substitutions on the "__retVal = expr" sub-tree */
800 slang_substitute(A
, assignOper
,
801 substCount
, substOld
, substNew
, GL_FALSE
);
803 /* install new code */
804 slang_operation_copy(oper
, blockOper
);
805 slang_operation_destruct(blockOper
);
809 case SLANG_OPER_ASSIGN
:
810 case SLANG_OPER_SUBSCRIPT
:
812 * child[0] can't have substitutions but child[1] can.
814 slang_substitute(A
, &oper
->children
[0],
815 substCount
, substOld
, substNew
, GL_TRUE
);
816 slang_substitute(A
, &oper
->children
[1],
817 substCount
, substOld
, substNew
, GL_FALSE
);
819 case SLANG_OPER_FIELD
:
821 slang_substitute(A
, &oper
->children
[0],
822 substCount
, substOld
, substNew
, GL_TRUE
);
827 for (i
= 0; i
< oper
->num_children
; i
++)
828 slang_substitute(A
, &oper
->children
[i
],
829 substCount
, substOld
, substNew
, GL_FALSE
);
837 * Inline the given function call operation.
838 * Return a new slang_operation that corresponds to the inlined code.
840 static slang_operation
*
841 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
842 slang_operation
*oper
, slang_operation
*returnOper
)
849 ParamMode
*paramMode
;
850 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
851 const GLuint numArgs
= oper
->num_children
;
852 const GLuint totalArgs
= numArgs
+ haveRetValue
;
853 slang_operation
*args
= oper
->children
;
854 slang_operation
*inlined
, *top
;
855 slang_variable
**substOld
;
856 slang_operation
**substNew
;
857 GLuint substCount
, numCopyIn
, i
;
859 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
860 assert(fun
->param_count
== totalArgs
);
862 /* allocate temporary arrays */
863 paramMode
= (ParamMode
*)
864 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
865 substOld
= (slang_variable
**)
866 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
867 substNew
= (slang_operation
**)
868 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
871 printf("Inline call to %s (total vars=%d nparams=%d)\n",
872 (char *) fun
->header
.a_name
,
873 fun
->parameters
->num_variables
, numArgs
);
876 if (haveRetValue
&& !returnOper
) {
877 /* Create 3-child comma sequence for inlined code:
878 * child[0]: declare __resultTmp
879 * child[1]: inlined function body
880 * child[2]: __resultTmp
882 slang_operation
*commaSeq
;
883 slang_operation
*declOper
= NULL
;
884 slang_variable
*resultVar
;
886 commaSeq
= slang_operation_new(1);
887 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
888 assert(commaSeq
->locals
);
889 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
890 commaSeq
->num_children
= 3;
891 commaSeq
->children
= slang_operation_new(3);
892 /* allocate the return var */
893 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
895 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
896 (void*)commaSeq->locals, (char *) fun->header.a_name);
899 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
900 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
901 resultVar
->isTemp
= GL_TRUE
;
903 /* child[0] = __resultTmp declaration */
904 declOper
= &commaSeq
->children
[0];
905 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
906 declOper
->a_id
= resultVar
->a_name
;
907 declOper
->locals
->outer_scope
= commaSeq
->locals
; /*** ??? **/
909 /* child[1] = function body */
910 inlined
= &commaSeq
->children
[1];
911 /* XXXX this may be inappropriate!!!!: */
912 inlined
->locals
->outer_scope
= commaSeq
->locals
;
914 /* child[2] = __resultTmp reference */
915 returnOper
= &commaSeq
->children
[2];
916 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
917 returnOper
->a_id
= resultVar
->a_name
;
918 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
919 declOper
->locals
->outer_scope
= commaSeq
->locals
;
924 top
= inlined
= slang_operation_new(1);
925 /* XXXX this may be inappropriate!!!! */
926 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
930 assert(inlined
->locals
);
932 /* Examine the parameters, look for inout/out params, look for possible
933 * substitutions, etc:
934 * param type behaviour
935 * in copy actual to local
936 * const in substitute param with actual
940 for (i
= 0; i
< totalArgs
; i
++) {
941 slang_variable
*p
= fun
->parameters
->variables
[i
];
943 printf("Param %d: %s %s \n", i,
944 slang_type_qual_string(p->type.qualifier),
947 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
948 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
949 /* an output param */
950 slang_operation
*arg
;
955 paramMode
[i
] = SUBST
;
957 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
958 slang_resolve_variable(arg
);
960 /* replace parameter 'p' with argument 'arg' */
961 substOld
[substCount
] = p
;
962 substNew
[substCount
] = arg
; /* will get copied */
965 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
966 /* a constant input param */
967 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
968 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
969 /* replace all occurances of this parameter variable with the
970 * actual argument variable or a literal.
972 paramMode
[i
] = SUBST
;
973 slang_resolve_variable(&args
[i
]);
974 substOld
[substCount
] = p
;
975 substNew
[substCount
] = &args
[i
]; /* will get copied */
979 paramMode
[i
] = COPY_IN
;
983 paramMode
[i
] = COPY_IN
;
985 assert(paramMode
[i
]);
988 /* actual code inlining: */
989 slang_operation_copy(inlined
, fun
->body
);
991 /*** XXX review this */
992 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
993 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
996 printf("======================= orig body code ======================\n");
997 printf("=== params scope = %p\n", (void*) fun
->parameters
);
998 slang_print_tree(fun
->body
, 8);
999 printf("======================= copied code =========================\n");
1000 slang_print_tree(inlined
, 8);
1003 /* do parameter substitution in inlined code: */
1004 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1007 printf("======================= subst code ==========================\n");
1008 slang_print_tree(inlined
, 8);
1009 printf("=============================================================\n");
1012 /* New prolog statements: (inserted before the inlined code)
1013 * Copy the 'in' arguments.
1016 for (i
= 0; i
< numArgs
; i
++) {
1017 if (paramMode
[i
] == COPY_IN
) {
1018 slang_variable
*p
= fun
->parameters
->variables
[i
];
1019 /* declare parameter 'p' */
1020 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1024 printf("COPY_IN %s from expr\n", (char*)p->a_name);
1026 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1027 assert(decl
->locals
);
1028 decl
->locals
= fun
->parameters
;
1029 decl
->a_id
= p
->a_name
;
1030 decl
->num_children
= 1;
1031 decl
->children
= slang_operation_new(1);
1033 /* child[0] is the var's initializer */
1034 slang_operation_copy(&decl
->children
[0], args
+ i
);
1040 /* New epilog statements:
1041 * 1. Create end of function label to jump to from return statements.
1042 * 2. Copy the 'out' parameter vars
1045 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1047 inlined
->num_children
);
1048 lab
->type
= SLANG_OPER_LABEL
;
1049 lab
->a_id
= slang_atom_pool_atom(A
->atoms
,
1050 (char *) A
->CurFunction
->end_label
);
1053 for (i
= 0; i
< totalArgs
; i
++) {
1054 if (paramMode
[i
] == COPY_OUT
) {
1055 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1056 /* actualCallVar = outParam */
1057 /*if (i > 0 || !haveRetValue)*/
1058 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1060 inlined
->num_children
);
1061 ass
->type
= SLANG_OPER_ASSIGN
;
1062 ass
->num_children
= 2;
1063 ass
->locals
= _slang_variable_scope_new(inlined
->locals
);
1064 assert(ass
->locals
);
1065 ass
->children
= slang_operation_new(2);
1066 ass
->children
[0] = args
[i
]; /*XXX copy */
1067 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1068 ass
->children
[1].a_id
= p
->a_name
;
1069 ass
->children
[1].locals
= _slang_variable_scope_new(ass
->locals
);
1073 _mesa_free(paramMode
);
1074 _mesa_free(substOld
);
1075 _mesa_free(substNew
);
1078 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1079 (char *) fun
->header
.a_name
,
1080 fun
->parameters
->num_variables
, numArgs
);
1081 slang_print_tree(top
, 0);
1087 static slang_ir_node
*
1088 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1089 slang_operation
*oper
, slang_operation
*dest
)
1092 slang_operation
*inlined
;
1093 slang_function
*prevFunc
;
1095 prevFunc
= A
->CurFunction
;
1096 A
->CurFunction
= fun
;
1098 if (!A
->CurFunction
->end_label
) {
1100 sprintf(name
, "__endOfFunc_%s_", (char *) A
->CurFunction
->header
.a_name
);
1101 A
->CurFunction
->end_label
= slang_atom_pool_gen(A
->atoms
, name
);
1104 if (slang_is_asm_function(fun
) && !dest
) {
1105 /* assemble assembly function - tree style */
1106 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1109 /* non-assembly function */
1110 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1113 /* Replace the function call with the inlined block */
1115 slang_operation_construct(oper
);
1116 slang_operation_copy(oper
, inlined
);
1123 assert(inlined
->locals
);
1124 printf("*** Inlined code for call to %s:\n",
1125 (char*) fun
->header
.a_name
);
1126 slang_print_tree(oper
, 10);
1130 n
= _slang_gen_operation(A
, oper
);
1132 A
->CurFunction
->end_label
= NULL
;
1134 A
->CurFunction
= prevFunc
;
1140 static slang_asm_info
*
1141 slang_find_asm_info(const char *name
)
1144 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1145 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1154 make_writemask(char *field
)
1160 mask
|= WRITEMASK_X
;
1163 mask
|= WRITEMASK_Y
;
1166 mask
|= WRITEMASK_Z
;
1169 mask
|= WRITEMASK_W
;
1177 return WRITEMASK_XYZW
;
1184 * Generate IR tree for an asm instruction/operation such as:
1185 * __asm vec4_dot __retVal.x, v1, v2;
1187 static slang_ir_node
*
1188 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1189 slang_operation
*dest
)
1191 const slang_asm_info
*info
;
1192 slang_ir_node
*kids
[3], *n
;
1193 GLuint j
, firstOperand
;
1195 assert(oper
->type
== SLANG_OPER_ASM
);
1197 info
= slang_find_asm_info((char *) oper
->a_id
);
1199 _mesa_problem(NULL
, "undefined __asm function %s\n",
1200 (char *) oper
->a_id
);
1203 assert(info
->NumParams
<= 3);
1205 if (info
->NumParams
== oper
->num_children
) {
1206 /* Storage for result is not specified.
1207 * Children[0], [1] are the operands.
1212 /* Storage for result (child[0]) is specified.
1213 * Children[1], [2] are the operands.
1218 /* assemble child(ren) */
1219 kids
[0] = kids
[1] = kids
[2] = NULL
;
1220 for (j
= 0; j
< info
->NumParams
; j
++) {
1221 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1224 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1227 /* Setup n->Store to be a particular location. Otherwise, storage
1228 * for the result (a temporary) will be allocated later.
1230 GLuint writemask
= WRITEMASK_XYZW
;
1231 slang_operation
*dest_oper
;
1234 dest_oper
= &oper
->children
[0];
1235 while /*if*/ (dest_oper
->type
== SLANG_OPER_FIELD
) {
1237 writemask
&= /*=*/make_writemask((char*) dest_oper
->a_id
);
1238 dest_oper
= &dest_oper
->children
[0];
1241 n0
= _slang_gen_operation(A
, dest_oper
);
1245 n
->Store
= n0
->Store
;
1246 n
->Writemask
= writemask
;
1257 _slang_is_noop(const slang_operation
*oper
)
1260 oper
->type
== SLANG_OPER_VOID
||
1261 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
1269 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1272 for (i
= 0; i
< scope
->num_functions
; i
++) {
1273 slang_function
*f
= &scope
->functions
[i
];
1274 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1275 printf(" %s (%d args)\n", name
, f
->param_count
);
1278 if (scope
->outer_scope
)
1279 print_funcs(scope
->outer_scope
, name
);
1284 * Return first function in the scope that has the given name.
1285 * This is the function we'll try to call when there is no exact match
1286 * between function parameters and call arguments.
1288 * XXX we should really create a list of candidate functions and try
1291 static slang_function
*
1292 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1295 for (i
= 0; i
< scope
->num_functions
; i
++) {
1296 slang_function
*f
= &scope
->functions
[i
];
1297 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1300 if (scope
->outer_scope
)
1301 return _slang_first_function(scope
->outer_scope
, name
);
1308 * Assemble a function call, given a particular function name.
1309 * \param name the function's name (operators like '*' are possible).
1311 static slang_ir_node
*
1312 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1313 slang_operation
*oper
, slang_operation
*dest
)
1315 slang_operation
*params
= oper
->children
;
1316 const GLuint param_count
= oper
->num_children
;
1318 slang_function
*fun
;
1320 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1321 if (atom
== SLANG_ATOM_NULL
)
1325 * Use 'name' to find the function to call
1327 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1328 &A
->space
, A
->atoms
);
1330 /* A function with exactly the right parameters/types was not found.
1331 * Try adapting the parameters.
1333 fun
= _slang_first_function(A
->space
.funcs
, name
);
1334 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
)) {
1335 RETURN_ERROR2("Undefined function (or no matching parameters)",
1341 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1346 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1348 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1349 oper
->type
== SLANG_OPER_LITERAL_INT
||
1350 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1351 if (oper
->literal
[0])
1357 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1358 oper
->num_children
== 1) {
1359 return _slang_is_constant_cond(&oper
->children
[0], value
);
1367 * Generate loop code using high-level IR_LOOP instruction
1369 static slang_ir_node
*
1370 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1374 * BREAK if !expr (child[0])
1375 * body code (child[1])
1377 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1378 GLboolean isConst
, constTrue
;
1380 /* Check if loop condition is a constant */
1381 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1383 if (isConst
&& !constTrue
) {
1384 /* loop is never executed! */
1385 return new_node0(IR_NOP
);
1388 loop
= new_loop(NULL
);
1390 /* save old, push new loop */
1391 prevLoop
= A
->CurLoop
;
1394 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1395 if (isConst
&& constTrue
) {
1396 /* while(nonzero constant), no conditional break */
1400 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1402 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1403 loop
->Children
[0] = new_seq(breakIf
, body
);
1405 /* Do infinite loop detection */
1406 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1407 /* infinite loop detected */
1408 A
->CurLoop
= prevLoop
; /* clean-up */
1409 RETURN_ERROR("Infinite loop detected!", 0);
1412 /* pop loop, restore prev */
1413 A
->CurLoop
= prevLoop
;
1420 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1422 static slang_ir_node
*
1423 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1427 * body code (child[0])
1428 * BREAK if !expr (child[1])
1430 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1431 GLboolean isConst
, constTrue
;
1433 /* Check if loop condition is a constant */
1434 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1436 loop
= new_loop(NULL
);
1438 /* save old, push new loop */
1439 prevLoop
= A
->CurLoop
;
1442 body
= _slang_gen_operation(A
, &oper
->children
[0]);
1443 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1444 if (isConst
&& constTrue
) {
1445 /* while(nonzero constant), no conditional break */
1449 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1451 loop
->Children
[0] = new_seq(body
, breakIf
);
1453 /* pop loop, restore prev */
1454 A
->CurLoop
= prevLoop
;
1461 * Generate for-loop using high-level IR_LOOP instruction.
1463 static slang_ir_node
*
1464 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1469 * BREAK if !expr (child[1])
1470 * body code (child[3])
1471 * incr code (child[2]) // XXX continue here
1473 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1475 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1476 loop
= new_loop(NULL
);
1478 /* save old, push new loop */
1479 prevLoop
= A
->CurLoop
;
1482 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1483 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1484 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1485 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1486 loop
->Children
[0] = new_seq(breakIf
,
1487 new_seq(body
, incr
));
1489 /* pop loop, restore prev */
1490 A
->CurLoop
= prevLoop
;
1492 return new_seq(init
, loop
);
1497 * Generate IR tree for an if/then/else conditional using BRAnch instructions.
1499 static slang_ir_node
*
1500 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1503 * eval expr (child[0]), updating condcodes
1504 * branch if false to _else or _endif
1506 * if haveElseClause clause:
1509 * "false" code block
1512 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1513 slang_ir_node
*cond
, *bra
, *trueBody
, *endifLab
, *tree
;
1514 slang_atom elseAtom
= slang_atom_pool_gen(A
->atoms
, "__else");
1515 slang_atom endifAtom
= slang_atom_pool_gen(A
->atoms
, "__endif");
1517 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1518 cond
= new_cond(cond
);
1519 /*assert(cond->Store);*/
1520 bra
= new_cjump(haveElseClause
? elseAtom
: endifAtom
, 0);
1521 tree
= new_seq(cond
, bra
);
1523 trueBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1524 tree
= new_seq(tree
, trueBody
);
1526 if (haveElseClause
) {
1528 slang_ir_node
*jump
, *elseLab
, *falseBody
;
1529 jump
= new_jump(endifAtom
);
1530 tree
= new_seq(tree
, jump
);
1532 elseLab
= new_label(elseAtom
);
1533 tree
= new_seq(tree
, elseLab
);
1535 falseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1536 tree
= new_seq(tree
, falseBody
);
1539 endifLab
= new_label(endifAtom
);
1540 tree
= new_seq(tree
, endifLab
);
1547 * Determine if the given operation is of a specific type.
1550 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1552 if (oper
->type
== type
)
1554 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1555 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1556 oper
->num_children
== 1)
1557 return is_operation_type(&oper
->children
[0], type
);
1564 * Generate IR tree for an if/then/else conditional using high-level
1565 * IR_IF instruction.
1567 static slang_ir_node
*
1568 _slang_gen_hl_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1571 * eval expr (child[0]), updating condcodes
1578 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1579 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1581 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1582 cond
= new_cond(cond
);
1584 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1585 /* Special case: generate a conditional break */
1586 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1587 if (haveElseClause
) {
1588 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1589 return new_seq(ifBody
, elseBody
);
1593 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1594 /* Special case: generate a conditional break */
1595 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1596 if (haveElseClause
) {
1597 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1598 return new_seq(ifBody
, elseBody
);
1604 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1606 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1609 ifNode
= new_if(cond
, ifBody
, elseBody
);
1617 * Generate IR node for storage of a temporary of given size.
1619 static slang_ir_node
*
1620 _slang_gen_temporary(GLint size
)
1622 slang_ir_storage
*store
;
1625 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1627 n
= new_node0(IR_VAR_DECL
);
1640 * Generate IR node for allocating/declaring a variable.
1642 static slang_ir_node
*
1643 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1646 n
= new_node0(IR_VAR_DECL
);
1649 slang_allocate_storage(A
, n
);
1651 assert(n
->Store
->Index
< 0);
1652 assert(n
->Store
->Size
> 0);
1654 assert(n
->Store
== var
->aux
);
1663 * Generate code for a selection expression: b ? x : y
1664 * XXX in some cases we could implement a selection expression
1665 * with an LRP instruction (use the boolean as the interpolant).
1667 static slang_ir_node
*
1668 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1670 slang_atom altAtom
= slang_atom_pool_gen(A
->atoms
, "__selectAlt");
1671 slang_atom endAtom
= slang_atom_pool_gen(A
->atoms
, "__selectEnd");
1672 slang_ir_node
*altLab
, *endLab
;
1673 slang_ir_node
*tree
, *tmpDecl
, *tmpVar
, *cond
, *cjump
, *jump
;
1674 slang_ir_node
*bodx
, *body
, *assignx
, *assigny
;
1675 slang_typeinfo type
;
1678 assert(oper
->type
== SLANG_OPER_SELECT
);
1679 assert(oper
->num_children
== 3);
1681 /* size of x or y's type */
1682 slang_typeinfo_construct(&type
);
1683 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1684 size
= _slang_sizeof_type_specifier(&type
.spec
);
1688 tmpDecl
= _slang_gen_temporary(size
);
1690 /* eval condition */
1691 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1692 cond
= new_cond(cond
);
1693 tree
= new_seq(tmpDecl
, cond
);
1695 /* jump if false to "alt" label */
1696 cjump
= new_cjump(altAtom
, 0);
1697 tree
= new_seq(tree
, cjump
);
1699 /* evaluate child 1 (x) and assign to tmp */
1700 tmpVar
= new_node0(IR_VAR
);
1701 tmpVar
->Store
= tmpDecl
->Store
;
1702 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1703 assigny
= new_node2(IR_MOVE
, tmpVar
, body
);
1704 tree
= new_seq(tree
, assigny
);
1706 /* jump to "end" label */
1707 jump
= new_jump(endAtom
);
1708 tree
= new_seq(tree
, jump
);
1711 altLab
= new_label(altAtom
);
1712 tree
= new_seq(tree
, altLab
);
1714 /* evaluate child 2 (y) and assign to tmp */
1715 tmpVar
= new_node0(IR_VAR
);
1716 tmpVar
->Store
= tmpDecl
->Store
;
1717 bodx
= _slang_gen_operation(A
, &oper
->children
[2]);
1718 assignx
= new_node2(IR_MOVE
, tmpVar
, bodx
);
1719 tree
= new_seq(tree
, assignx
);
1722 endLab
= new_label(endAtom
);
1723 tree
= new_seq(tree
, endLab
);
1726 tmpVar
= new_node0(IR_VAR
);
1727 tmpVar
->Store
= tmpDecl
->Store
;
1728 tree
= new_seq(tree
, tmpVar
);
1735 * Generate code for &&.
1737 static slang_ir_node
*
1738 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1740 /* rewrite "a && b" as "a ? b : false" */
1741 slang_operation
*select
;
1744 select
= slang_operation_new(1);
1745 select
->type
= SLANG_OPER_SELECT
;
1746 select
->num_children
= 3;
1747 select
->children
= slang_operation_new(3);
1749 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1750 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1751 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1752 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0);
1753 select
->children
[2].literal_size
= 2;
1755 n
= _slang_gen_select(A
, select
);
1758 free(select
->children
);
1766 * Generate code for ||.
1768 static slang_ir_node
*
1769 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1771 /* rewrite "a || b" as "a ? true : b" */
1772 slang_operation
*select
;
1775 select
= slang_operation_new(1);
1776 select
->type
= SLANG_OPER_SELECT
;
1777 select
->num_children
= 3;
1778 select
->children
= slang_operation_new(3);
1780 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1781 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1782 ASSIGN_4V(select
->children
[2].literal
, 1, 1, 1, 1);
1783 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1784 select
->children
[2].literal_size
= 2;
1786 n
= _slang_gen_select(A
, select
);
1789 free(select
->children
);
1798 * Generate IR tree for a return statement.
1800 static slang_ir_node
*
1801 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1803 if (oper
->num_children
== 0 ||
1804 (oper
->num_children
== 1 &&
1805 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1809 * goto __endOfFunction;
1812 slang_operation gotoOp
;
1813 slang_operation_construct(&gotoOp
);
1814 gotoOp
.type
= SLANG_OPER_GOTO
;
1815 /* XXX don't call function? */
1816 gotoOp
.a_id
= slang_atom_pool_atom(A
->atoms
,
1817 (char *) A
->CurFunction
->end_label
);
1818 /* assemble the new code */
1819 n
= _slang_gen_operation(A
, &gotoOp
);
1820 /* destroy temp code */
1821 slang_operation_destruct(&gotoOp
);
1830 * goto __endOfFunction;
1832 slang_operation
*block
, *assign
, *jump
;
1833 slang_atom a_retVal
;
1836 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1842 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1847 block
= slang_operation_new(1);
1848 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1849 block
->num_children
= 2;
1850 block
->children
= slang_operation_new(2);
1851 assert(block
->locals
);
1852 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1854 /* child[0]: __retVal = expr; */
1855 assign
= &block
->children
[0];
1856 assign
->type
= SLANG_OPER_ASSIGN
;
1857 assign
->locals
->outer_scope
= block
->locals
;
1858 assign
->num_children
= 2;
1859 assign
->children
= slang_operation_new(2);
1860 /* lhs (__retVal) */
1861 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1862 assign
->children
[0].a_id
= a_retVal
;
1863 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1865 /* XXX we might be able to avoid this copy someday */
1866 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1868 /* child[1]: goto __endOfFunction */
1869 jump
= &block
->children
[1];
1870 jump
->type
= SLANG_OPER_GOTO
;
1871 assert(A
->CurFunction
->end_label
);
1872 /* XXX don't call function? */
1873 jump
->a_id
= slang_atom_pool_atom(A
->atoms
,
1874 (char *) A
->CurFunction
->end_label
);
1877 printf("NEW RETURN:\n");
1878 slang_print_tree(block
, 0);
1881 /* assemble the new code */
1882 n
= _slang_gen_operation(A
, block
);
1883 slang_operation_delete(block
);
1890 * Generate IR tree for a variable declaration.
1892 static slang_ir_node
*
1893 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1896 slang_ir_node
*varDecl
;
1898 const char *varName
= (char *) oper
->a_id
;
1900 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1902 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1905 varDecl
= _slang_gen_var_decl(A
, v
);
1907 if (oper
->num_children
> 0) {
1908 /* child is initializer */
1909 slang_ir_node
*var
, *init
, *rhs
;
1910 assert(oper
->num_children
== 1);
1911 var
= new_var(A
, oper
, oper
->a_id
);
1913 RETURN_ERROR2("Undefined variable:", varName
, 0);
1915 /* XXX make copy of this initializer? */
1916 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1918 init
= new_node2(IR_MOVE
, var
, rhs
);
1919 /*assert(rhs->Opcode != IR_SEQ);*/
1920 n
= new_seq(varDecl
, init
);
1922 else if (v
->initializer
) {
1923 slang_ir_node
*var
, *init
, *rhs
;
1924 var
= new_var(A
, oper
, oper
->a_id
);
1926 RETURN_ERROR2("Undefined variable:", varName
, 0);
1929 /* XXX make copy of this initializer? */
1931 slang_operation dup
;
1932 slang_operation_construct(&dup
);
1933 slang_operation_copy(&dup
, v
->initializer
);
1934 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1935 rhs
= _slang_gen_operation(A
, &dup
);
1938 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1939 rhs
= _slang_gen_operation(A
, v
->initializer
);
1942 init
= new_node2(IR_MOVE
, var
, rhs
);
1944 assert(rhs->Opcode != IR_SEQ);
1946 n
= new_seq(varDecl
, init
);
1956 * Generate IR tree for a variable (such as in an expression).
1958 static slang_ir_node
*
1959 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1961 /* If there's a variable associated with this oper (from inlining)
1962 * use it. Otherwise, use the oper's var id.
1964 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1965 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1967 RETURN_ERROR2("Undefined variable:", (char *) aVar
, 0);
1974 * Some write-masked assignments are simple, but others are hard.
1977 * v.xy = vec2(a, b);
1980 * v.yz = vec2(a, b);
1981 * this would have to be transformed/swizzled into:
1982 * v.yz = vec2(a, b).*xy* (* = don't care)
1983 * Instead, we'll effectively do this:
1984 * v.y = vec2(a, b).xxxx;
1985 * v.z = vec2(a, b).yyyy;
1989 _slang_simple_writemask(GLuint writemask
)
1991 switch (writemask
) {
1998 case WRITEMASK_XYZW
:
2007 * Convert the given swizzle into a writemask. In some cases this
2008 * is trivial, in other cases, we'll need to also swizzle the right
2009 * hand side to put components in the right places.
2010 * \param swizzle the incoming swizzle
2011 * \param writemaskOut returns the writemask
2012 * \param swizzleOut swizzle to apply to the right-hand-side
2013 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
2016 swizzle_to_writemask(GLuint swizzle
,
2017 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
2019 GLuint mask
= 0x0, newSwizzle
[4];
2022 /* make new dst writemask, compute size */
2023 for (i
= 0; i
< 4; i
++) {
2024 const GLuint swz
= GET_SWZ(swizzle
, i
);
2025 if (swz
== SWIZZLE_NIL
) {
2029 assert(swz
>= 0 && swz
<= 3);
2032 assert(mask
<= 0xf);
2033 size
= i
; /* number of components in mask/swizzle */
2035 *writemaskOut
= mask
;
2037 /* make new src swizzle, by inversion */
2038 for (i
= 0; i
< 4; i
++) {
2039 newSwizzle
[i
] = i
; /*identity*/
2041 for (i
= 0; i
< size
; i
++) {
2042 const GLuint swz
= GET_SWZ(swizzle
, i
);
2043 newSwizzle
[swz
] = i
;
2045 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
2050 if (_slang_simple_writemask(mask
)) {
2052 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
2054 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
2056 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
2058 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
2066 static slang_ir_node
*
2067 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2069 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2071 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
2072 n
->Store
->Swizzle
= swizzle
;
2079 * Generate IR tree for an assignment (=).
2081 static slang_ir_node
*
2082 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2084 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2085 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2086 /* Special case of: x = f(a, b)
2087 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2089 * XXX this could be even more effective if we could accomodate
2090 * cases such as "v.x = f();" - would help with typical vertex
2094 n
= _slang_gen_function_call_name(A
,
2095 (const char *) oper
->children
[1].a_id
,
2096 &oper
->children
[1], &oper
->children
[0]);
2100 slang_ir_node
*n
, *lhs
, *rhs
;
2101 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2102 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2104 /* convert lhs swizzle into writemask */
2105 GLuint writemask
, newSwizzle
;
2106 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2107 &writemask
, &newSwizzle
)) {
2108 /* Non-simple writemask, need to swizzle right hand side in
2109 * order to put components into the right place.
2111 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2113 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2114 n
->Writemask
= writemask
;
2125 * Generate IR tree for referencing a field in a struct (or basic vector type)
2127 static slang_ir_node
*
2128 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2132 slang_typeinfo_construct(&ti
);
2133 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2135 if (_slang_type_is_vector(ti
.spec
.type
)) {
2136 /* the field should be a swizzle */
2137 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2141 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2142 RETURN_ERROR("Bad swizzle", 0);
2144 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2149 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2150 /* create new parent node with swizzle */
2151 n
= _slang_gen_swizzle(n
, swizzle
);
2154 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2155 const GLuint rows
= 1;
2159 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2160 RETURN_ERROR("Bad swizzle", 0);
2162 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2166 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2167 /* create new parent node with swizzle */
2168 n
= _slang_gen_swizzle(n
, swizzle
);
2172 /* the field is a structure member (base.field) */
2173 /* oper->children[0] is the base */
2174 /* oper->a_id is the field name */
2175 slang_ir_node
*base
, *n
;
2176 GLint size
= 4; /* XXX fix? */
2178 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2180 n
= new_node1(IR_FIELD
, base
);
2182 n
->Target
= (char *) oper
->a_id
;
2183 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2190 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2198 * Gen code for array indexing.
2200 static slang_ir_node
*
2201 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2203 slang_typeinfo array_ti
;
2205 /* get array's type info */
2206 slang_typeinfo_construct(&array_ti
);
2207 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2209 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2210 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2211 /* translate the index into a swizzle/writemask: "v.x=p" */
2212 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2216 index
= (GLint
) oper
->children
[1].literal
[0];
2217 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2219 RETURN_ERROR("Invalid array index for vector type", 0);
2222 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2224 /* use swizzle to access the element */
2225 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2229 n
= _slang_gen_swizzle(n
, swizzle
);
2230 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2231 n
->Writemask
= WRITEMASK_X
<< index
;
2236 /* conventional array */
2237 slang_typeinfo elem_ti
;
2238 slang_ir_node
*elem
, *array
, *index
;
2241 /* size of array element */
2242 slang_typeinfo_construct(&elem_ti
);
2243 _slang_typeof_operation(A
, oper
, &elem_ti
);
2244 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2245 assert(elemSize
>= 1);
2247 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2248 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2249 if (array
&& index
) {
2250 elem
= new_node2(IR_ELEMENT
, array
, index
);
2251 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2252 array
->Store
->Index
,
2254 /* XXX try to do some array bounds checking here */
2266 * Generate IR tree for a slang_operation (AST node)
2268 static slang_ir_node
*
2269 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2271 switch (oper
->type
) {
2272 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2276 _slang_push_var_table(A
->vartable
);
2278 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2279 n
= _slang_gen_operation(A
, oper
);
2280 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2282 _slang_pop_var_table(A
->vartable
);
2285 n
= new_node1(IR_SCOPE
, n
);
2290 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2291 /* list of operations */
2292 if (oper
->num_children
> 0)
2294 slang_ir_node
*n
, *tree
= NULL
;
2297 for (i
= 0; i
< oper
->num_children
; i
++) {
2298 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2300 _slang_free_ir_tree(tree
);
2301 return NULL
; /* error must have occured */
2303 tree
= tree
? new_seq(tree
, n
) : n
;
2307 if (oper
->locals
->num_variables
> 0) {
2310 printf("\n****** Deallocate vars in scope!\n");
2312 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2313 slang_variable
*v
= oper
->locals
->variables
+ i
;
2315 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2317 printf(" Deallocate var %s\n", (char*) v->a_name);
2319 assert(store
->File
== PROGRAM_TEMPORARY
);
2320 assert(store
->Index
>= 0);
2321 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2329 case SLANG_OPER_EXPRESSION
:
2330 return _slang_gen_operation(A
, &oper
->children
[0]);
2332 case SLANG_OPER_FOR
:
2333 return _slang_gen_for(A
, oper
);
2335 return _slang_gen_do(A
, oper
);
2336 case SLANG_OPER_WHILE
:
2337 return _slang_gen_while(A
, oper
);
2338 case SLANG_OPER_BREAK
:
2340 RETURN_ERROR("'break' not in loop", 0);
2342 return new_break(A
->CurLoop
);
2343 case SLANG_OPER_CONTINUE
:
2345 RETURN_ERROR("'continue' not in loop", 0);
2347 return new_cont(A
->CurLoop
);
2348 case SLANG_OPER_DISCARD
:
2349 return new_node0(IR_KILL
);
2351 case SLANG_OPER_EQUAL
:
2352 return new_node2(IR_SEQUAL
,
2353 _slang_gen_operation(A
, &oper
->children
[0]),
2354 _slang_gen_operation(A
, &oper
->children
[1]));
2355 case SLANG_OPER_NOTEQUAL
:
2356 return new_node2(IR_SNEQUAL
,
2357 _slang_gen_operation(A
, &oper
->children
[0]),
2358 _slang_gen_operation(A
, &oper
->children
[1]));
2359 case SLANG_OPER_GREATER
:
2360 return new_node2(IR_SGT
,
2361 _slang_gen_operation(A
, &oper
->children
[0]),
2362 _slang_gen_operation(A
, &oper
->children
[1]));
2363 case SLANG_OPER_LESS
:
2364 /* child[0] < child[1] ----> child[1] > child[0] */
2365 return new_node2(IR_SGT
,
2366 _slang_gen_operation(A
, &oper
->children
[1]),
2367 _slang_gen_operation(A
, &oper
->children
[0]));
2368 case SLANG_OPER_GREATERequal
:
2369 return new_node2(IR_SGE
,
2370 _slang_gen_operation(A
, &oper
->children
[0]),
2371 _slang_gen_operation(A
, &oper
->children
[1]));
2372 case SLANG_OPER_LESSequal
:
2373 /* child[0] <= child[1] ----> child[1] >= child[0] */
2374 return new_node2(IR_SGE
,
2375 _slang_gen_operation(A
, &oper
->children
[1]),
2376 _slang_gen_operation(A
, &oper
->children
[0]));
2377 case SLANG_OPER_ADD
:
2380 assert(oper
->num_children
== 2);
2381 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2384 case SLANG_OPER_SUBTRACT
:
2387 assert(oper
->num_children
== 2);
2388 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2391 case SLANG_OPER_MULTIPLY
:
2394 assert(oper
->num_children
== 2);
2395 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2398 case SLANG_OPER_DIVIDE
:
2401 assert(oper
->num_children
== 2);
2402 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2405 case SLANG_OPER_MINUS
:
2408 assert(oper
->num_children
== 1);
2409 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2412 case SLANG_OPER_PLUS
:
2413 /* +expr --> do nothing */
2414 return _slang_gen_operation(A
, &oper
->children
[0]);
2415 case SLANG_OPER_VARIABLE_DECL
:
2416 return _slang_gen_declaration(A
, oper
);
2417 case SLANG_OPER_ASSIGN
:
2418 return _slang_gen_assignment(A
, oper
);
2419 case SLANG_OPER_ADDASSIGN
:
2422 assert(oper
->num_children
== 2);
2423 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2426 case SLANG_OPER_SUBASSIGN
:
2429 assert(oper
->num_children
== 2);
2430 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2434 case SLANG_OPER_MULASSIGN
:
2437 assert(oper
->num_children
== 2);
2438 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2441 case SLANG_OPER_DIVASSIGN
:
2444 assert(oper
->num_children
== 2);
2445 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2448 case SLANG_OPER_LOGICALAND
:
2451 assert(oper
->num_children
== 2);
2452 n
= _slang_gen_logical_and(A
, oper
);
2455 case SLANG_OPER_LOGICALOR
:
2458 assert(oper
->num_children
== 2);
2459 n
= _slang_gen_logical_or(A
, oper
);
2462 case SLANG_OPER_LOGICALXOR
:
2465 assert(oper
->num_children
== 2);
2466 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2469 case SLANG_OPER_NOT
:
2472 assert(oper
->num_children
== 1);
2473 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2477 case SLANG_OPER_SELECT
: /* b ? x : y */
2480 assert(oper
->num_children
== 3);
2481 n
= _slang_gen_select(A
, oper
);
2485 case SLANG_OPER_ASM
:
2486 return _slang_gen_asm(A
, oper
, NULL
);
2487 case SLANG_OPER_CALL
:
2488 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2490 case SLANG_OPER_RETURN
:
2491 return _slang_gen_return(A
, oper
);
2492 case SLANG_OPER_GOTO
:
2493 return new_jump((char*) oper
->a_id
);
2494 case SLANG_OPER_LABEL
:
2495 return new_label((char*) oper
->a_id
);
2496 case SLANG_OPER_IDENTIFIER
:
2497 return _slang_gen_variable(A
, oper
);
2499 if (A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2500 return _slang_gen_hl_if(A
, oper
);
2503 /* XXX update tnl executor */
2504 return _slang_gen_if(A
, oper
);
2506 case SLANG_OPER_FIELD
:
2507 return _slang_gen_field(A
, oper
);
2508 case SLANG_OPER_SUBSCRIPT
:
2509 return _slang_gen_subscript(A
, oper
);
2510 case SLANG_OPER_LITERAL_FLOAT
:
2512 case SLANG_OPER_LITERAL_INT
:
2514 case SLANG_OPER_LITERAL_BOOL
:
2515 return new_float_literal(oper
->literal
);
2517 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2520 assert(oper
->num_children
== 1);
2521 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2524 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2527 assert(oper
->num_children
== 1);
2528 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2531 case SLANG_OPER_PREINCREMENT
: /* ++var */
2534 assert(oper
->num_children
== 1);
2535 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2538 case SLANG_OPER_PREDECREMENT
: /* --var */
2541 assert(oper
->num_children
== 1);
2542 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2546 case SLANG_OPER_SEQUENCE
:
2548 slang_ir_node
*tree
= NULL
;
2550 for (i
= 0; i
< oper
->num_children
; i
++) {
2551 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2552 tree
= tree
? new_seq(tree
, n
) : n
;
2557 case SLANG_OPER_NONE
:
2559 case SLANG_OPER_VOID
:
2563 printf("Unhandled node type %d\n", oper
->type
);
2565 return new_node0(IR_NOP
);
2574 * Called by compiler when a global variable has been parsed/compiled.
2575 * Here we examine the variable's type to determine what kind of register
2576 * storage will be used.
2578 * A uniform such as "gl_Position" will become the register specification
2579 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2580 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2582 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2583 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2584 * actual texture unit (as specified by the user calling glUniform1i()).
2587 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2588 slang_unit_type type
)
2590 struct gl_program
*prog
= A
->program
;
2591 const char *varName
= (char *) var
->a_name
;
2592 GLboolean success
= GL_TRUE
;
2594 slang_ir_storage
*store
= NULL
;
2597 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2599 if (texIndex
!= -1) {
2601 * store->File = PROGRAM_SAMPLER
2602 * store->Index = sampler uniform location
2603 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2605 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2606 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2607 if (dbg
) printf("SAMPLER ");
2609 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2610 /* Uniform variable */
2611 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2612 * MAX2(var
->array_len
, 1);
2614 /* user-defined uniform */
2615 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2616 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2619 /* pre-defined uniform, like gl_ModelviewMatrix */
2620 /* We know it's a uniform, but don't allocate storage unless
2623 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2625 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2627 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2628 const GLint size
= 4; /* XXX fix */
2630 /* user-defined varying */
2631 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2632 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2635 /* pre-defined varying, like gl_Color or gl_TexCoord */
2636 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2637 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2639 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2640 assert(index
< FRAG_ATTRIB_MAX
);
2643 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2645 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2646 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2647 assert(index
< VERT_RESULT_MAX
);
2649 if (dbg
) printf("V/F ");
2651 if (dbg
) printf("VARYING ");
2653 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2655 /* user-defined vertex attribute */
2656 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2657 const GLint attr
= -1; /* unknown */
2658 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2661 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2662 VERT_ATTRIB_GENERIC0
+ index
, size
);
2665 /* pre-defined vertex attrib */
2666 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2667 GLint size
= 4; /* XXX? */
2669 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2671 if (dbg
) printf("ATTRIB ");
2673 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2674 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2675 GLint size
= 4; /* XXX? */
2676 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2677 if (dbg
) printf("INPUT ");
2679 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2680 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2681 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2682 GLint size
= 4; /* XXX? */
2683 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2686 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2687 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2688 GLint size
= 4; /* XXX? */
2689 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2691 if (dbg
) printf("OUTPUT ");
2693 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2694 /* pre-defined global constant, like gl_MaxLights */
2695 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2696 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2697 if (dbg
) printf("CONST ");
2700 /* ordinary variable (may be const) */
2703 /* IR node to declare the variable */
2704 n
= _slang_gen_var_decl(A
, var
);
2706 /* IR code for the var's initializer, if present */
2707 if (var
->initializer
) {
2708 slang_ir_node
*lhs
, *rhs
, *init
;
2710 /* Generate IR_MOVE instruction to initialize the variable */
2711 lhs
= new_node0(IR_VAR
);
2713 lhs
->Store
= n
->Store
;
2715 /* constant folding, etc */
2716 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2718 rhs
= _slang_gen_operation(A
, var
->initializer
);
2720 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2721 n
= new_seq(n
, init
);
2724 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
);
2726 _slang_free_ir_tree(n
);
2729 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2730 store
? store
->Index
: -2);
2733 var
->aux
= store
; /* save var's storage info */
2740 * Produce an IR tree from a function AST (fun->body).
2741 * Then call the code emitter to convert the IR tree into gl_program
2745 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2747 slang_ir_node
*n
, *endLabel
;
2748 GLboolean success
= GL_TRUE
;
2750 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2751 /* we only really generate code for main, all other functions get
2754 return GL_TRUE
; /* not an error */
2758 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2761 slang_print_function(fun
, 1);
2764 /* should have been allocated earlier: */
2765 assert(A
->program
->Parameters
);
2766 assert(A
->program
->Varying
);
2767 assert(A
->vartable
);
2769 /* fold constant expressions, etc. */
2770 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2772 A
->CurFunction
= fun
;
2774 /* Create an end-of-function label */
2775 if (!A
->CurFunction
->end_label
)
2776 A
->CurFunction
->end_label
= slang_atom_pool_gen(A
->atoms
, "__endOfFunc_main_");
2778 /* push new vartable scope */
2779 _slang_push_var_table(A
->vartable
);
2781 /* Generate IR tree for the function body code */
2782 n
= _slang_gen_operation(A
, fun
->body
);
2784 n
= new_node1(IR_SCOPE
, n
);
2786 /* pop vartable, restore previous */
2787 _slang_pop_var_table(A
->vartable
);
2790 /* XXX record error */
2794 /* append an end-of-function-label to IR tree */
2795 endLabel
= new_label(fun
->end_label
);
2796 n
= new_seq(n
, endLabel
);
2798 A
->CurFunction
= NULL
;
2801 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2802 slang_print_function(fun
, 1);
2805 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2806 slang_print_ir(n
, 0);
2809 printf("************* End codegen function ************\n\n");
2812 /* Emit program instructions */
2813 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
);
2814 _slang_free_ir_tree(n
);
2816 /* free codegen context */
2818 _mesa_free(A->codegen);