2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_print.h"
47 #include "shader/prog_statevars.h"
48 #include "slang_typeinfo.h"
49 #include "slang_codegen.h"
50 #include "slang_compile.h"
51 #include "slang_label.h"
52 #include "slang_mem.h"
53 #include "slang_simplify.h"
54 #include "slang_emit.h"
55 #include "slang_vartable.h"
57 #include "slang_print.h"
60 static slang_ir_node
*
61 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
65 is_sampler_type(const slang_fully_specified_type
*t
)
67 switch (t
->specifier
.type
) {
68 case SLANG_SPEC_SAMPLER1D
:
69 case SLANG_SPEC_SAMPLER2D
:
70 case SLANG_SPEC_SAMPLER3D
:
71 case SLANG_SPEC_SAMPLERCUBE
:
72 case SLANG_SPEC_SAMPLER1DSHADOW
:
73 case SLANG_SPEC_SAMPLER2DSHADOW
:
74 case SLANG_SPEC_SAMPLER2DRECT
:
75 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
84 * Return the offset (in floats or ints) of the named field within
85 * the given struct. Return -1 if field not found.
86 * If field is NULL, return the size of the struct instead.
89 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
93 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
94 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
95 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
97 /* types larger than 1 float are register (4-float) aligned */
98 offset
= (offset
+ 3) & ~3;
100 if (field
&& v
->a_name
== field
) {
106 return -1; /* field not found */
108 return offset
; /* struct size */
113 * Return the size (in floats) of the given type specifier.
114 * If the size is greater than 4, the size should be a multiple of 4
115 * so that the correct number of 4-float registers are allocated.
116 * For example, a mat3x2 is size 12 because we want to store the
117 * 3 columns in 3 float[4] registers.
120 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
123 switch (spec
->type
) {
124 case SLANG_SPEC_VOID
:
127 case SLANG_SPEC_BOOL
:
130 case SLANG_SPEC_BVEC2
:
133 case SLANG_SPEC_BVEC3
:
136 case SLANG_SPEC_BVEC4
:
142 case SLANG_SPEC_IVEC2
:
145 case SLANG_SPEC_IVEC3
:
148 case SLANG_SPEC_IVEC4
:
151 case SLANG_SPEC_FLOAT
:
154 case SLANG_SPEC_VEC2
:
157 case SLANG_SPEC_VEC3
:
160 case SLANG_SPEC_VEC4
:
163 case SLANG_SPEC_MAT2
:
164 sz
= 2 * 4; /* 2 columns (regs) */
166 case SLANG_SPEC_MAT3
:
169 case SLANG_SPEC_MAT4
:
172 case SLANG_SPEC_MAT23
:
173 sz
= 2 * 4; /* 2 columns (regs) */
175 case SLANG_SPEC_MAT32
:
176 sz
= 3 * 4; /* 3 columns (regs) */
178 case SLANG_SPEC_MAT24
:
181 case SLANG_SPEC_MAT42
:
182 sz
= 4 * 4; /* 4 columns (regs) */
184 case SLANG_SPEC_MAT34
:
187 case SLANG_SPEC_MAT43
:
188 sz
= 4 * 4; /* 4 columns (regs) */
190 case SLANG_SPEC_SAMPLER1D
:
191 case SLANG_SPEC_SAMPLER2D
:
192 case SLANG_SPEC_SAMPLER3D
:
193 case SLANG_SPEC_SAMPLERCUBE
:
194 case SLANG_SPEC_SAMPLER1DSHADOW
:
195 case SLANG_SPEC_SAMPLER2DSHADOW
:
196 case SLANG_SPEC_SAMPLER2DRECT
:
197 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
198 sz
= 1; /* a sampler is basically just an integer index */
200 case SLANG_SPEC_STRUCT
:
201 sz
= _slang_field_offset(spec
, 0); /* special use */
203 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
206 case SLANG_SPEC_ARRAY
:
207 sz
= _slang_sizeof_type_specifier(spec
->_array
);
210 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
215 /* if size is > 4, it should be a multiple of four */
216 assert((sz
& 0x3) == 0);
223 * Establish the binding between a slang_ir_node and a slang_variable.
224 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
225 * The IR node must be a IR_VAR or IR_VAR_DECL node.
226 * \param n the IR node
227 * \param var the variable to associate with the IR node
230 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
234 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
235 assert(!n
->Var
|| n
->Var
== var
);
240 /* need to setup storage */
241 if (n
->Var
&& n
->Var
->store
) {
242 /* node storage info = var storage info */
243 n
->Store
= n
->Var
->store
;
246 /* alloc new storage info */
247 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
249 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
251 (void*) n
->Store
, n
->Store
->Size
);
254 n
->Var
->store
= n
->Store
;
255 assert(n
->Var
->store
);
262 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
263 * or -1 if the type is not a sampler.
266 sampler_to_texture_index(const slang_type_specifier_type type
)
269 case SLANG_SPEC_SAMPLER1D
:
270 return TEXTURE_1D_INDEX
;
271 case SLANG_SPEC_SAMPLER2D
:
272 return TEXTURE_2D_INDEX
;
273 case SLANG_SPEC_SAMPLER3D
:
274 return TEXTURE_3D_INDEX
;
275 case SLANG_SPEC_SAMPLERCUBE
:
276 return TEXTURE_CUBE_INDEX
;
277 case SLANG_SPEC_SAMPLER1DSHADOW
:
278 return TEXTURE_1D_INDEX
; /* XXX fix */
279 case SLANG_SPEC_SAMPLER2DSHADOW
:
280 return TEXTURE_2D_INDEX
; /* XXX fix */
281 case SLANG_SPEC_SAMPLER2DRECT
:
282 return TEXTURE_RECT_INDEX
;
283 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
284 return TEXTURE_RECT_INDEX
; /* XXX fix */
291 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
294 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
295 * a vertex or fragment program input variable. Return -1 if the input
297 * XXX return size too
300 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
307 static const struct input_info vertInputs
[] = {
308 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
309 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
310 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
311 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
312 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
313 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
314 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
315 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
316 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
317 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
318 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
319 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
320 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
321 { NULL
, 0, SWIZZLE_NOOP
}
323 static const struct input_info fragInputs
[] = {
324 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
325 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
326 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
327 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
328 /* note: we're packing several quantities into the fogcoord vector */
329 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
330 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
331 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
332 { NULL
, 0, SWIZZLE_NOOP
}
335 const struct input_info
*inputs
336 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
338 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
340 for (i
= 0; inputs
[i
].Name
; i
++) {
341 if (strcmp(inputs
[i
].Name
, name
) == 0) {
343 *swizzleOut
= inputs
[i
].Swizzle
;
344 return inputs
[i
].Attrib
;
352 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
353 * a vertex or fragment program output variable. Return -1 for an invalid
357 _slang_output_index(const char *name
, GLenum target
)
363 static const struct output_info vertOutputs
[] = {
364 { "gl_Position", VERT_RESULT_HPOS
},
365 { "gl_FrontColor", VERT_RESULT_COL0
},
366 { "gl_BackColor", VERT_RESULT_BFC0
},
367 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
368 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
369 { "gl_TexCoord", VERT_RESULT_TEX0
},
370 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
371 { "gl_PointSize", VERT_RESULT_PSIZ
},
374 static const struct output_info fragOutputs
[] = {
375 { "gl_FragColor", FRAG_RESULT_COLR
},
376 { "gl_FragDepth", FRAG_RESULT_DEPR
},
377 { "gl_FragData", FRAG_RESULT_DATA0
},
381 const struct output_info
*outputs
382 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
384 for (i
= 0; outputs
[i
].Name
; i
++) {
385 if (strcmp(outputs
[i
].Name
, name
) == 0) {
387 return outputs
[i
].Attrib
;
395 /**********************************************************************/
399 * Map "_asm foo" to IR_FOO, etc.
404 slang_ir_opcode Opcode
;
405 GLuint HaveRetValue
, NumParams
;
409 static slang_asm_info AsmInfo
[] = {
411 { "vec4_add", IR_ADD
, 1, 2 },
412 { "vec4_subtract", IR_SUB
, 1, 2 },
413 { "vec4_multiply", IR_MUL
, 1, 2 },
414 { "vec4_dot", IR_DOT4
, 1, 2 },
415 { "vec3_dot", IR_DOT3
, 1, 2 },
416 { "vec2_dot", IR_DOT2
, 1, 2 },
417 { "vec3_nrm", IR_NRM3
, 1, 1 },
418 { "vec4_nrm", IR_NRM4
, 1, 1 },
419 { "vec3_cross", IR_CROSS
, 1, 2 },
420 { "vec4_lrp", IR_LRP
, 1, 3 },
421 { "vec4_min", IR_MIN
, 1, 2 },
422 { "vec4_max", IR_MAX
, 1, 2 },
423 { "vec4_clamp", IR_CLAMP
, 1, 3 },
424 { "vec4_seq", IR_SEQUAL
, 1, 2 },
425 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
426 { "vec4_sge", IR_SGE
, 1, 2 },
427 { "vec4_sgt", IR_SGT
, 1, 2 },
428 { "vec4_sle", IR_SLE
, 1, 2 },
429 { "vec4_slt", IR_SLT
, 1, 2 },
431 { "vec4_move", IR_MOVE
, 1, 1 },
432 { "vec4_floor", IR_FLOOR
, 1, 1 },
433 { "vec4_frac", IR_FRAC
, 1, 1 },
434 { "vec4_abs", IR_ABS
, 1, 1 },
435 { "vec4_negate", IR_NEG
, 1, 1 },
436 { "vec4_ddx", IR_DDX
, 1, 1 },
437 { "vec4_ddy", IR_DDY
, 1, 1 },
438 /* float binary op */
439 { "float_power", IR_POW
, 1, 2 },
440 /* texture / sampler */
441 { "vec4_tex1d", IR_TEX
, 1, 2 },
442 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
443 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
444 { "vec4_tex2d", IR_TEX
, 1, 2 },
445 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
446 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
447 { "vec4_tex3d", IR_TEX
, 1, 2 },
448 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
449 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
450 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
451 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
452 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
455 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
456 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
457 { "float_exp", IR_EXP
, 1, 1 },
458 { "float_exp2", IR_EXP2
, 1, 1 },
459 { "float_log2", IR_LOG2
, 1, 1 },
460 { "float_rsq", IR_RSQ
, 1, 1 },
461 { "float_rcp", IR_RCP
, 1, 1 },
462 { "float_sine", IR_SIN
, 1, 1 },
463 { "float_cosine", IR_COS
, 1, 1 },
464 { "float_noise1", IR_NOISE1
, 1, 1},
465 { "float_noise2", IR_NOISE2
, 1, 1},
466 { "float_noise3", IR_NOISE3
, 1, 1},
467 { "float_noise4", IR_NOISE4
, 1, 1},
469 { NULL
, IR_NOP
, 0, 0 }
473 static slang_ir_node
*
474 new_node3(slang_ir_opcode op
,
475 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
477 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
483 n
->InstLocation
= -1;
488 static slang_ir_node
*
489 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
491 return new_node3(op
, c0
, c1
, NULL
);
494 static slang_ir_node
*
495 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
497 return new_node3(op
, c0
, NULL
, NULL
);
500 static slang_ir_node
*
501 new_node0(slang_ir_opcode op
)
503 return new_node3(op
, NULL
, NULL
, NULL
);
508 * Create sequence of two nodes.
510 static slang_ir_node
*
511 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
517 return new_node2(IR_SEQ
, left
, right
);
520 static slang_ir_node
*
521 new_label(slang_label
*label
)
523 slang_ir_node
*n
= new_node0(IR_LABEL
);
530 static slang_ir_node
*
531 new_float_literal(const float v
[4], GLuint size
)
533 slang_ir_node
*n
= new_node0(IR_FLOAT
);
535 COPY_4V(n
->Value
, v
);
536 /* allocate a storage object, but compute actual location (Index) later */
537 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
542 static slang_ir_node
*
543 new_not(slang_ir_node
*n
)
545 return new_node1(IR_NOT
, n
);
550 * Non-inlined function call.
552 static slang_ir_node
*
553 new_function_call(slang_ir_node
*code
, slang_label
*name
)
555 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
564 * Unconditional jump.
566 static slang_ir_node
*
567 new_return(slang_label
*dest
)
569 slang_ir_node
*n
= new_node0(IR_RETURN
);
577 static slang_ir_node
*
578 new_loop(slang_ir_node
*body
)
580 return new_node1(IR_LOOP
, body
);
584 static slang_ir_node
*
585 new_break(slang_ir_node
*loopNode
)
587 slang_ir_node
*n
= new_node0(IR_BREAK
);
589 assert(loopNode
->Opcode
== IR_LOOP
);
591 /* insert this node at head of linked list */
592 n
->List
= loopNode
->List
;
600 * Make new IR_BREAK_IF_TRUE.
602 static slang_ir_node
*
603 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
607 assert(loopNode
->Opcode
== IR_LOOP
);
608 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
610 /* insert this node at head of linked list */
611 n
->List
= loopNode
->List
;
619 * Make new IR_CONT_IF_TRUE node.
621 static slang_ir_node
*
622 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
626 assert(loopNode
->Opcode
== IR_LOOP
);
627 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
629 /* insert this node at head of linked list */
630 n
->List
= loopNode
->List
;
637 static slang_ir_node
*
638 new_cond(slang_ir_node
*n
)
640 slang_ir_node
*c
= new_node1(IR_COND
, n
);
645 static slang_ir_node
*
646 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
648 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
653 * New IR_VAR node - a reference to a previously declared variable.
655 static slang_ir_node
*
656 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
659 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
663 assert(var
->declared
);
665 assert(!oper
->var
|| oper
->var
== var
);
667 n
= new_node0(IR_VAR
);
669 _slang_attach_storage(n
, var
);
671 printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
679 * Check if the given function is really just a wrapper for a
680 * basic assembly instruction.
683 slang_is_asm_function(const slang_function
*fun
)
685 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
686 fun
->body
->num_children
== 1 &&
687 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
695 _slang_is_noop(const slang_operation
*oper
)
698 oper
->type
== SLANG_OPER_VOID
||
699 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
707 * Recursively search tree for a node of the given type.
709 static slang_operation
*
710 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
713 if (oper
->type
== type
)
715 for (i
= 0; i
< oper
->num_children
; i
++) {
716 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
725 * Count the number of operations of the given time rooted at 'oper'.
728 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
731 if (oper
->type
== type
) {
734 for (i
= 0; i
< oper
->num_children
; i
++) {
735 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
742 * Check if the 'return' statement found under 'oper' is a "tail return"
743 * that can be no-op'd. For example:
748 * return; // this is a no-op
751 * This is used when determining if a function can be inlined. If the
752 * 'return' is not the last statement, we can't inline the function since
753 * we still need the semantic behaviour of the 'return' but we don't want
754 * to accidentally return from the _calling_ function. We'd need to use an
755 * unconditional branch, but we don't have such a GPU instruction (not
759 _slang_is_tail_return(const slang_operation
*oper
)
761 GLuint k
= oper
->num_children
;
764 const slang_operation
*last
= &oper
->children
[k
- 1];
765 if (last
->type
== SLANG_OPER_RETURN
)
767 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
768 last
->type
== SLANG_OPER_LABEL
)
769 k
--; /* try prev child */
770 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
771 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
772 /* try sub-children */
773 return _slang_is_tail_return(last
);
783 slang_resolve_variable(slang_operation
*oper
)
785 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
786 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
792 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
795 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
796 GLuint substCount
, slang_variable
**substOld
,
797 slang_operation
**substNew
, GLboolean isLHS
)
799 switch (oper
->type
) {
800 case SLANG_OPER_VARIABLE_DECL
:
802 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
803 oper
->a_id
, GL_TRUE
);
805 if (v
->initializer
&& oper
->num_children
== 0) {
806 /* set child of oper to copy of initializer */
807 oper
->num_children
= 1;
808 oper
->children
= slang_operation_new(1);
809 slang_operation_copy(&oper
->children
[0], v
->initializer
);
811 if (oper
->num_children
== 1) {
812 /* the initializer */
813 slang_substitute(A
, &oper
->children
[0], substCount
,
814 substOld
, substNew
, GL_FALSE
);
818 case SLANG_OPER_IDENTIFIER
:
819 assert(oper
->num_children
== 0);
820 if (1/**!isLHS XXX FIX */) {
821 slang_atom id
= oper
->a_id
;
824 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
826 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
830 /* look for a substitution */
831 for (i
= 0; i
< substCount
; i
++) {
832 if (v
== substOld
[i
]) {
833 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
834 #if 0 /* DEBUG only */
835 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
836 assert(substNew
[i
]->var
);
837 assert(substNew
[i
]->var
->a_name
);
838 printf("Substitute %s with %s in id node %p\n",
839 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
843 printf("Substitute %s with %f in id node %p\n",
844 (char*)v
->a_name
, substNew
[i
]->literal
[0],
848 slang_operation_copy(oper
, substNew
[i
]);
855 case SLANG_OPER_RETURN
:
856 /* do return replacement here too */
857 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
858 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
864 * then do substitutions on the assignment.
866 slang_operation
*blockOper
, *assignOper
, *returnOper
;
868 /* check if function actually has a return type */
869 assert(A
->CurFunction
);
870 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
871 slang_info_log_error(A
->log
, "illegal return expression");
875 blockOper
= slang_operation_new(1);
876 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
877 blockOper
->num_children
= 2;
878 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
879 blockOper
->children
= slang_operation_new(2);
880 assignOper
= blockOper
->children
+ 0;
881 returnOper
= blockOper
->children
+ 1;
883 assignOper
->type
= SLANG_OPER_ASSIGN
;
884 assignOper
->num_children
= 2;
885 assignOper
->locals
->outer_scope
= blockOper
->locals
;
886 assignOper
->children
= slang_operation_new(2);
887 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
888 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
889 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
891 slang_operation_copy(&assignOper
->children
[1],
894 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
895 assert(returnOper
->num_children
== 0);
897 /* do substitutions on the "__retVal = expr" sub-tree */
898 slang_substitute(A
, assignOper
,
899 substCount
, substOld
, substNew
, GL_FALSE
);
901 /* install new code */
902 slang_operation_copy(oper
, blockOper
);
903 slang_operation_destruct(blockOper
);
906 /* check if return value was expected */
907 assert(A
->CurFunction
);
908 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
909 slang_info_log_error(A
->log
, "return statement requires an expression");
915 case SLANG_OPER_ASSIGN
:
916 case SLANG_OPER_SUBSCRIPT
:
918 * child[0] can't have substitutions but child[1] can.
920 slang_substitute(A
, &oper
->children
[0],
921 substCount
, substOld
, substNew
, GL_TRUE
);
922 slang_substitute(A
, &oper
->children
[1],
923 substCount
, substOld
, substNew
, GL_FALSE
);
925 case SLANG_OPER_FIELD
:
927 slang_substitute(A
, &oper
->children
[0],
928 substCount
, substOld
, substNew
, GL_TRUE
);
933 for (i
= 0; i
< oper
->num_children
; i
++)
934 slang_substitute(A
, &oper
->children
[i
],
935 substCount
, substOld
, substNew
, GL_FALSE
);
942 * Produce inline code for a call to an assembly instruction.
943 * This is typically used to compile a call to a built-in function like this:
945 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
947 * __asm vec4_lrp __retVal, a, y, x;
952 * r = mix(p1, p2, p3);
962 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
964 static slang_operation
*
965 slang_inline_asm_function(slang_assemble_ctx
*A
,
966 slang_function
*fun
, slang_operation
*oper
)
968 const GLuint numArgs
= oper
->num_children
;
970 slang_operation
*inlined
;
971 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
972 slang_variable
**substOld
;
973 slang_operation
**substNew
;
975 ASSERT(slang_is_asm_function(fun
));
976 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
979 printf("Inline %s as %s\n",
980 (char*) fun->header.a_name,
981 (char*) fun->body->children[0].a_id);
985 * We'll substitute formal params with actual args in the asm call.
987 substOld
= (slang_variable
**)
988 _slang_alloc(numArgs
* sizeof(slang_variable
*));
989 substNew
= (slang_operation
**)
990 _slang_alloc(numArgs
* sizeof(slang_operation
*));
991 for (i
= 0; i
< numArgs
; i
++) {
992 substOld
[i
] = fun
->parameters
->variables
[i
];
993 substNew
[i
] = oper
->children
+ i
;
996 /* make a copy of the code to inline */
997 inlined
= slang_operation_new(1);
998 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1000 /* get rid of the __retVal child */
1001 inlined
->num_children
--;
1002 for (i
= 0; i
< inlined
->num_children
; i
++) {
1003 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1007 /* now do formal->actual substitutions */
1008 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1010 _slang_free(substOld
);
1011 _slang_free(substNew
);
1014 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1015 (char *) fun
->header
.a_name
);
1016 slang_print_tree(inlined
, 3);
1017 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1025 * Inline the given function call operation.
1026 * Return a new slang_operation that corresponds to the inlined code.
1028 static slang_operation
*
1029 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1030 slang_operation
*oper
, slang_operation
*returnOper
)
1037 ParamMode
*paramMode
;
1038 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1039 const GLuint numArgs
= oper
->num_children
;
1040 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1041 slang_operation
*args
= oper
->children
;
1042 slang_operation
*inlined
, *top
;
1043 slang_variable
**substOld
;
1044 slang_operation
**substNew
;
1045 GLuint substCount
, numCopyIn
, i
;
1046 slang_function
*prevFunction
;
1047 slang_variable_scope
*newScope
= NULL
;
1050 prevFunction
= A
->CurFunction
;
1051 A
->CurFunction
= fun
;
1053 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1054 assert(fun
->param_count
== totalArgs
);
1056 /* allocate temporary arrays */
1057 paramMode
= (ParamMode
*)
1058 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1059 substOld
= (slang_variable
**)
1060 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1061 substNew
= (slang_operation
**)
1062 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1065 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1066 (char *) fun
->header
.a_name
,
1067 fun
->parameters
->num_variables
, numArgs
);
1070 if (haveRetValue
&& !returnOper
) {
1071 /* Create 3-child comma sequence for inlined code:
1072 * child[0]: declare __resultTmp
1073 * child[1]: inlined function body
1074 * child[2]: __resultTmp
1076 slang_operation
*commaSeq
;
1077 slang_operation
*declOper
= NULL
;
1078 slang_variable
*resultVar
;
1080 commaSeq
= slang_operation_new(1);
1081 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1082 assert(commaSeq
->locals
);
1083 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1084 commaSeq
->num_children
= 3;
1085 commaSeq
->children
= slang_operation_new(3);
1086 /* allocate the return var */
1087 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1089 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1090 (void*)commaSeq->locals, (char *) fun->header.a_name);
1093 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1094 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1095 resultVar
->isTemp
= GL_TRUE
;
1097 /* child[0] = __resultTmp declaration */
1098 declOper
= &commaSeq
->children
[0];
1099 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1100 declOper
->a_id
= resultVar
->a_name
;
1101 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1103 /* child[1] = function body */
1104 inlined
= &commaSeq
->children
[1];
1105 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1107 /* child[2] = __resultTmp reference */
1108 returnOper
= &commaSeq
->children
[2];
1109 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1110 returnOper
->a_id
= resultVar
->a_name
;
1111 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1116 top
= inlined
= slang_operation_new(1);
1117 /* XXXX this may be inappropriate!!!! */
1118 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1122 assert(inlined
->locals
);
1124 /* Examine the parameters, look for inout/out params, look for possible
1125 * substitutions, etc:
1126 * param type behaviour
1127 * in copy actual to local
1128 * const in substitute param with actual
1132 for (i
= 0; i
< totalArgs
; i
++) {
1133 slang_variable
*p
= fun
->parameters
->variables
[i
];
1135 printf("Param %d: %s %s \n", i,
1136 slang_type_qual_string(p->type.qualifier),
1137 (char *) p->a_name);
1139 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1140 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1141 /* an output param */
1142 slang_operation
*arg
;
1147 paramMode
[i
] = SUBST
;
1149 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1150 slang_resolve_variable(arg
);
1152 /* replace parameter 'p' with argument 'arg' */
1153 substOld
[substCount
] = p
;
1154 substNew
[substCount
] = arg
; /* will get copied */
1157 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1158 /* a constant input param */
1159 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1160 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1161 /* replace all occurances of this parameter variable with the
1162 * actual argument variable or a literal.
1164 paramMode
[i
] = SUBST
;
1165 slang_resolve_variable(&args
[i
]);
1166 substOld
[substCount
] = p
;
1167 substNew
[substCount
] = &args
[i
]; /* will get copied */
1171 paramMode
[i
] = COPY_IN
;
1175 paramMode
[i
] = COPY_IN
;
1177 assert(paramMode
[i
]);
1180 /* actual code inlining: */
1181 slang_operation_copy(inlined
, fun
->body
);
1183 /*** XXX review this */
1184 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1185 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1186 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1189 printf("======================= orig body code ======================\n");
1190 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1191 slang_print_tree(fun
->body
, 8);
1192 printf("======================= copied code =========================\n");
1193 slang_print_tree(inlined
, 8);
1196 /* do parameter substitution in inlined code: */
1197 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1200 printf("======================= subst code ==========================\n");
1201 slang_print_tree(inlined
, 8);
1202 printf("=============================================================\n");
1205 /* New prolog statements: (inserted before the inlined code)
1206 * Copy the 'in' arguments.
1209 for (i
= 0; i
< numArgs
; i
++) {
1210 if (paramMode
[i
] == COPY_IN
) {
1211 slang_variable
*p
= fun
->parameters
->variables
[i
];
1212 /* declare parameter 'p' */
1213 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1217 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1218 assert(decl
->locals
);
1219 decl
->locals
->outer_scope
= inlined
->locals
;
1220 decl
->a_id
= p
->a_name
;
1221 decl
->num_children
= 1;
1222 decl
->children
= slang_operation_new(1);
1224 /* child[0] is the var's initializer */
1225 slang_operation_copy(&decl
->children
[0], args
+ i
);
1227 /* add parameter 'p' to the local variable scope here */
1229 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1230 pCopy
->type
= p
->type
;
1231 pCopy
->a_name
= p
->a_name
;
1232 pCopy
->array_len
= p
->array_len
;
1235 newScope
= inlined
->locals
;
1240 /* Now add copies of the function's local vars to the new variable scope */
1241 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1242 slang_variable
*p
= fun
->parameters
->variables
[i
];
1243 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1244 pCopy
->type
= p
->type
;
1245 pCopy
->a_name
= p
->a_name
;
1246 pCopy
->array_len
= p
->array_len
;
1250 /* New epilog statements:
1251 * 1. Create end of function label to jump to from return statements.
1252 * 2. Copy the 'out' parameter vars
1255 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1257 inlined
->num_children
);
1258 lab
->type
= SLANG_OPER_LABEL
;
1259 lab
->label
= A
->curFuncEndLabel
;
1262 for (i
= 0; i
< totalArgs
; i
++) {
1263 if (paramMode
[i
] == COPY_OUT
) {
1264 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1265 /* actualCallVar = outParam */
1266 /*if (i > 0 || !haveRetValue)*/
1267 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1269 inlined
->num_children
);
1270 ass
->type
= SLANG_OPER_ASSIGN
;
1271 ass
->num_children
= 2;
1272 ass
->locals
->outer_scope
= inlined
->locals
;
1273 ass
->children
= slang_operation_new(2);
1274 ass
->children
[0] = args
[i
]; /*XXX copy */
1275 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1276 ass
->children
[1].a_id
= p
->a_name
;
1277 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1281 _slang_free(paramMode
);
1282 _slang_free(substOld
);
1283 _slang_free(substNew
);
1285 /* Update scoping to use the new local vars instead of the
1286 * original function's vars. This is especially important
1287 * for nested inlining.
1290 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1293 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1294 (char *) fun
->header
.a_name
,
1295 fun
->parameters
->num_variables
, numArgs
);
1296 slang_print_tree(top
, 0);
1300 A
->CurFunction
= prevFunction
;
1306 static slang_ir_node
*
1307 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1308 slang_operation
*oper
, slang_operation
*dest
)
1311 slang_operation
*inlined
;
1312 slang_label
*prevFuncEndLabel
;
1315 prevFuncEndLabel
= A
->curFuncEndLabel
;
1316 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1317 A
->curFuncEndLabel
= _slang_label_new(name
);
1318 assert(A
->curFuncEndLabel
);
1320 if (slang_is_asm_function(fun
) && !dest
) {
1321 /* assemble assembly function - tree style */
1322 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1325 /* non-assembly function */
1326 /* We always generate an "inline-able" block of code here.
1328 * 1. insert the inline code
1329 * 2. Generate a call to the "inline" code as a subroutine
1333 slang_operation
*ret
= NULL
;
1335 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1339 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1341 /* check if this is a "tail" return */
1342 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1343 _slang_is_tail_return(inlined
)) {
1344 /* The only RETURN is the last stmt in the function, no-op it
1345 * and inline the function body.
1347 ret
->type
= SLANG_OPER_NONE
;
1350 slang_operation
*callOper
;
1351 /* The function we're calling has one or more 'return' statements.
1352 * So, we can't truly inline this function because we need to
1353 * implement 'return' with RET (and CAL).
1354 * Nevertheless, we performed "inlining" to make a new instance
1355 * of the function body to deal with static register allocation.
1357 * XXX check if there's one 'return' and if it's the very last
1358 * statement in the function - we can optimize that case.
1360 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1361 inlined
->type
== SLANG_OPER_SEQUENCE
);
1363 if (_slang_function_has_return_value(fun
) && !dest
) {
1364 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1365 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1366 callOper
= &inlined
->children
[1];
1371 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1372 callOper
->fun
= fun
;
1373 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1381 /* Replace the function call with the inlined block (or new CALL stmt) */
1382 slang_operation_destruct(oper
);
1384 _slang_free(inlined
);
1387 assert(inlined
->locals
);
1388 printf("*** Inlined code for call to %s:\n",
1389 (char*) fun
->header
.a_name
);
1390 slang_print_tree(oper
, 10);
1394 n
= _slang_gen_operation(A
, oper
);
1396 /*_slang_label_delete(A->curFuncEndLabel);*/
1397 A
->curFuncEndLabel
= prevFuncEndLabel
;
1403 static slang_asm_info
*
1404 slang_find_asm_info(const char *name
)
1407 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1408 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1417 * Return the default swizzle mask for accessing a variable of the
1418 * given size (in floats). If size = 1, comp is used to identify
1419 * which component [0..3] of the register holds the variable.
1422 _slang_var_swizzle(GLint size
, GLint comp
)
1426 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1428 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1430 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1432 return SWIZZLE_XYZW
;
1438 * Some write-masked assignments are simple, but others are hard.
1441 * v.xy = vec2(a, b);
1444 * v.zy = vec2(a, b);
1445 * this gets transformed/swizzled into:
1446 * v.zy = vec2(a, b).*yx* (* = don't care)
1447 * This function helps to determine simple vs. non-simple.
1450 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1452 switch (writemask
) {
1454 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1456 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1458 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1460 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1462 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1463 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1465 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1466 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1467 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1468 case WRITEMASK_XYZW
:
1469 return swizzle
== SWIZZLE_NOOP
;
1477 * Convert the given swizzle into a writemask. In some cases this
1478 * is trivial, in other cases, we'll need to also swizzle the right
1479 * hand side to put components in the right places.
1480 * See comment above for more info.
1481 * XXX this function could be simplified and should probably be renamed.
1482 * \param swizzle the incoming swizzle
1483 * \param writemaskOut returns the writemask
1484 * \param swizzleOut swizzle to apply to the right-hand-side
1485 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1488 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1489 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1491 GLuint mask
= 0x0, newSwizzle
[4];
1494 /* make new dst writemask, compute size */
1495 for (i
= 0; i
< 4; i
++) {
1496 const GLuint swz
= GET_SWZ(swizzle
, i
);
1497 if (swz
== SWIZZLE_NIL
) {
1501 assert(swz
>= 0 && swz
<= 3);
1503 if (swizzle
!= SWIZZLE_XXXX
&&
1504 swizzle
!= SWIZZLE_YYYY
&&
1505 swizzle
!= SWIZZLE_ZZZZ
&&
1506 swizzle
!= SWIZZLE_WWWW
&&
1507 (mask
& (1 << swz
))) {
1508 /* a channel can't be specified twice (ex: ".xyyz") */
1509 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1510 _mesa_swizzle_string(swizzle
, 0, 0));
1516 assert(mask
<= 0xf);
1517 size
= i
; /* number of components in mask/swizzle */
1519 *writemaskOut
= mask
;
1521 /* make new src swizzle, by inversion */
1522 for (i
= 0; i
< 4; i
++) {
1523 newSwizzle
[i
] = i
; /*identity*/
1525 for (i
= 0; i
< size
; i
++) {
1526 const GLuint swz
= GET_SWZ(swizzle
, i
);
1527 newSwizzle
[swz
] = i
;
1529 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1534 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1536 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1538 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1540 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1542 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1551 * Recursively traverse 'oper' to produce a swizzle mask in the event
1552 * of any vector subscripts and swizzle suffixes.
1553 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1556 resolve_swizzle(const slang_operation
*oper
)
1558 if (oper
->type
== SLANG_OPER_FIELD
) {
1559 /* writemask from .xyzw suffix */
1561 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1562 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1566 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1567 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1571 return SWIZZLE_XYZW
;
1573 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1574 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1575 /* writemask from [index] */
1576 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1577 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1582 swizzle
= SWIZZLE_XXXX
;
1585 swizzle
= SWIZZLE_YYYY
;
1588 swizzle
= SWIZZLE_ZZZZ
;
1591 swizzle
= SWIZZLE_WWWW
;
1594 swizzle
= SWIZZLE_XYZW
;
1596 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1600 return SWIZZLE_XYZW
;
1606 * Recursively descend through swizzle nodes to find the node's storage info.
1608 static slang_ir_storage
*
1609 get_store(const slang_ir_node
*n
)
1611 if (n
->Opcode
== IR_SWIZZLE
) {
1612 return get_store(n
->Children
[0]);
1620 * Generate IR tree for an asm instruction/operation such as:
1621 * __asm vec4_dot __retVal.x, v1, v2;
1623 static slang_ir_node
*
1624 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1625 slang_operation
*dest
)
1627 const slang_asm_info
*info
;
1628 slang_ir_node
*kids
[3], *n
;
1629 GLuint j
, firstOperand
;
1631 assert(oper
->type
== SLANG_OPER_ASM
);
1633 info
= slang_find_asm_info((char *) oper
->a_id
);
1635 _mesa_problem(NULL
, "undefined __asm function %s\n",
1636 (char *) oper
->a_id
);
1639 assert(info
->NumParams
<= 3);
1641 if (info
->NumParams
== oper
->num_children
) {
1642 /* Storage for result is not specified.
1643 * Children[0], [1], [2] are the operands.
1648 /* Storage for result (child[0]) is specified.
1649 * Children[1], [2], [3] are the operands.
1654 /* assemble child(ren) */
1655 kids
[0] = kids
[1] = kids
[2] = NULL
;
1656 for (j
= 0; j
< info
->NumParams
; j
++) {
1657 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1662 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1665 /* Setup n->Store to be a particular location. Otherwise, storage
1666 * for the result (a temporary) will be allocated later.
1668 slang_operation
*dest_oper
;
1671 dest_oper
= &oper
->children
[0];
1673 n0
= _slang_gen_operation(A
, dest_oper
);
1678 n
->Store
= n0
->Store
;
1680 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1690 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1693 for (i
= 0; i
< scope
->num_functions
; i
++) {
1694 slang_function
*f
= &scope
->functions
[i
];
1695 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1696 printf(" %s (%d args)\n", name
, f
->param_count
);
1699 if (scope
->outer_scope
)
1700 print_funcs(scope
->outer_scope
, name
);
1705 * Find a function of the given name, taking 'numArgs' arguments.
1706 * This is the function we'll try to call when there is no exact match
1707 * between function parameters and call arguments.
1709 * XXX we should really create a list of candidate functions and try
1712 static slang_function
*
1713 _slang_find_function_by_argc(slang_function_scope
*scope
,
1714 const char *name
, int numArgs
)
1718 for (i
= 0; i
< scope
->num_functions
; i
++) {
1719 slang_function
*f
= &scope
->functions
[i
];
1720 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1721 int haveRetValue
= _slang_function_has_return_value(f
);
1722 if (numArgs
== f
->param_count
- haveRetValue
)
1726 scope
= scope
->outer_scope
;
1733 static slang_function
*
1734 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1737 slang_function
*maxFunc
= NULL
;
1742 for (i
= 0; i
< scope
->num_functions
; i
++) {
1743 slang_function
*f
= &scope
->functions
[i
];
1744 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1745 if (f
->param_count
> maxArgs
) {
1746 maxArgs
= f
->param_count
;
1751 scope
= scope
->outer_scope
;
1759 * Generate a new slang_function which is a constructor for a user-defined
1762 static slang_function
*
1763 _slang_make_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1765 const GLint numFields
= str
->fields
->num_variables
;
1767 slang_function
*fun
= (slang_function
*) _mesa_malloc(sizeof(slang_function
));
1771 slang_function_construct(fun
);
1773 /* function header (name, return type) */
1774 fun
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1775 fun
->header
.a_name
= str
->a_name
;
1776 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1777 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1778 fun
->header
.type
.specifier
._struct
= str
;
1780 /* function parameters (= struct's fields) */
1783 for (i
= 0; i
< numFields
; i
++) {
1785 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1787 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1788 *p
= *str
->fields
->variables
[i
]; /* copy the type */
1789 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1791 fun
->param_count
= fun
->parameters
->num_variables
;
1794 /* Add __retVal to params */
1796 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1797 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1799 p
->a_name
= a_retVal
;
1800 p
->type
= fun
->header
.type
;
1801 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1805 /* function body is:
1815 slang_variable_scope
*scope
;
1816 slang_variable
*var
;
1819 fun
->body
= slang_operation_new(1);
1820 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1821 fun
->body
->num_children
= numFields
+ 2;
1822 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1824 scope
= fun
->body
->locals
;
1825 scope
->outer_scope
= fun
->parameters
;
1827 /* create local var 't' */
1828 var
= slang_variable_scope_grow(scope
);
1829 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1830 var
->type
= fun
->header
.type
;
1834 slang_operation
*decl
;
1836 decl
= &fun
->body
->children
[0];
1837 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1838 decl
->locals
= _slang_variable_scope_new(scope
);
1839 decl
->a_id
= var
->a_name
;
1842 /* assign params to fields of t */
1843 for (i
= 0; i
< numFields
; i
++) {
1844 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1846 assign
->type
= SLANG_OPER_ASSIGN
;
1847 assign
->locals
= _slang_variable_scope_new(scope
);
1848 assign
->num_children
= 2;
1849 assign
->children
= slang_operation_new(2);
1852 slang_operation
*lhs
= &assign
->children
[0];
1854 lhs
->type
= SLANG_OPER_FIELD
;
1855 lhs
->locals
= _slang_variable_scope_new(scope
);
1856 lhs
->num_children
= 1;
1857 lhs
->children
= slang_operation_new(1);
1858 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1860 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1861 lhs
->children
[0].a_id
= var
->a_name
;
1862 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1865 lhs
->children
[1].num_children
= 1;
1866 lhs
->children
[1].children
= slang_operation_new(1);
1867 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1868 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1869 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1874 slang_operation
*rhs
= &assign
->children
[1];
1876 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1877 rhs
->locals
= _slang_variable_scope_new(scope
);
1878 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1884 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1886 ret
->type
= SLANG_OPER_RETURN
;
1887 ret
->locals
= _slang_variable_scope_new(scope
);
1888 ret
->num_children
= 1;
1889 ret
->children
= slang_operation_new(1);
1890 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1891 ret
->children
[0].a_id
= var
->a_name
;
1892 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1897 slang_print_function(fun, 1);
1904 * Find/create a function (constructor) for the given structure name.
1906 static slang_function
*
1907 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1910 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1911 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1912 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1913 /* found a structure type that matches the function name */
1914 if (!str
->constructor
) {
1915 /* create the constructor function now */
1916 str
->constructor
= _slang_make_constructor(A
, str
);
1918 return str
->constructor
;
1927 _slang_is_vec_mat_type(const char *name
)
1929 static const char *vecmat_types
[] = {
1930 "float", "int", "bool",
1931 "vec2", "vec3", "vec4",
1932 "ivec2", "ivec3", "ivec4",
1933 "bvec2", "bvec3", "bvec4",
1934 "mat2", "mat3", "mat4",
1935 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
1939 for (i
= 0; vecmat_types
[i
]; i
++)
1940 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
1947 * Assemble a function call, given a particular function name.
1948 * \param name the function's name (operators like '*' are possible).
1950 static slang_ir_node
*
1951 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1952 slang_operation
*oper
, slang_operation
*dest
)
1954 slang_operation
*params
= oper
->children
;
1955 const GLuint param_count
= oper
->num_children
;
1957 slang_function
*fun
;
1961 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1962 if (atom
== SLANG_ATOM_NULL
)
1966 * First, try to find function by name and exact argument type matching.
1968 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1969 &A
->space
, A
->atoms
, A
->log
, &error
);
1972 slang_info_log_error(A
->log
,
1973 "Function '%s' not found (check argument types)",
1979 /* Next, try locating a constructor function for a user-defined type */
1980 fun
= _slang_locate_struct_constructor(A
, name
);
1984 * At this point, some heuristics are used to try to find a function
1985 * that matches the calling signature by means of casting or "unrolling"
1989 if (!fun
&& _slang_is_vec_mat_type(name
)) {
1990 /* Next, if this call looks like a vec() or mat() constructor call,
1991 * try "unwinding" the args to satisfy a constructor.
1993 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
1995 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1996 slang_info_log_error(A
->log
,
1997 "Function '%s' not found (check argument types)",
2004 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2005 /* Next, try casting args to the types of the formal parameters */
2006 int numArgs
= oper
->num_children
;
2007 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2008 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2009 slang_info_log_error(A
->log
,
2010 "Function '%s' not found (check argument types)",
2018 slang_info_log_error(A
->log
,
2019 "Function '%s' not found (check argument types)",
2024 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2026 if (n
&& !n
->Store
&& !dest
2027 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2028 /* setup n->Store for the result of the function call */
2029 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2030 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2031 /*printf("Alloc storage for function result, size %d \n", size);*/
2039 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2041 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2042 oper
->type
== SLANG_OPER_LITERAL_INT
||
2043 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2044 if (oper
->literal
[0])
2050 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2051 oper
->num_children
== 1) {
2052 return _slang_is_constant_cond(&oper
->children
[0], value
);
2059 * Test if an operation is a scalar or boolean.
2062 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2064 slang_typeinfo type
;
2067 slang_typeinfo_construct(&type
);
2068 _slang_typeof_operation(A
, oper
, &type
);
2069 size
= _slang_sizeof_type_specifier(&type
.spec
);
2070 slang_typeinfo_destruct(&type
);
2076 * Test if an operation is boolean.
2079 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2081 slang_typeinfo type
;
2084 slang_typeinfo_construct(&type
);
2085 _slang_typeof_operation(A
, oper
, &type
);
2086 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2087 slang_typeinfo_destruct(&type
);
2093 * Generate loop code using high-level IR_LOOP instruction
2095 static slang_ir_node
*
2096 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2100 * BREAK if !expr (child[0])
2101 * body code (child[1])
2103 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2104 GLboolean isConst
, constTrue
;
2106 /* type-check expression */
2107 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2108 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2112 /* Check if loop condition is a constant */
2113 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2115 if (isConst
&& !constTrue
) {
2116 /* loop is never executed! */
2117 return new_node0(IR_NOP
);
2120 loop
= new_loop(NULL
);
2122 /* save old, push new loop */
2123 prevLoop
= A
->CurLoop
;
2126 if (isConst
&& constTrue
) {
2127 /* while(nonzero constant), no conditional break */
2132 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2133 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2135 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2136 loop
->Children
[0] = new_seq(breakIf
, body
);
2138 /* Do infinite loop detection */
2139 /* loop->List is head of linked list of break/continue nodes */
2140 if (!loop
->List
&& isConst
&& constTrue
) {
2141 /* infinite loop detected */
2142 A
->CurLoop
= prevLoop
; /* clean-up */
2143 slang_info_log_error(A
->log
, "Infinite loop detected!");
2147 /* pop loop, restore prev */
2148 A
->CurLoop
= prevLoop
;
2155 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2157 static slang_ir_node
*
2158 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2162 * body code (child[0])
2164 * BREAK if !expr (child[1])
2166 slang_ir_node
*prevLoop
, *loop
;
2167 GLboolean isConst
, constTrue
;
2169 /* type-check expression */
2170 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2171 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2175 loop
= new_loop(NULL
);
2177 /* save old, push new loop */
2178 prevLoop
= A
->CurLoop
;
2182 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2184 /* Check if loop condition is a constant */
2185 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2186 if (isConst
&& constTrue
) {
2187 /* do { } while(1) ==> no conditional break */
2188 loop
->Children
[1] = NULL
; /* no tail code */
2192 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2193 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2196 /* XXX we should do infinite loop detection, as above */
2198 /* pop loop, restore prev */
2199 A
->CurLoop
= prevLoop
;
2206 * Generate for-loop using high-level IR_LOOP instruction.
2208 static slang_ir_node
*
2209 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2212 * init code (child[0])
2214 * BREAK if !expr (child[1])
2215 * body code (child[3])
2217 * incr code (child[2]) // XXX continue here
2219 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2221 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2222 loop
= new_loop(NULL
);
2224 /* save old, push new loop */
2225 prevLoop
= A
->CurLoop
;
2228 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2229 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2230 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2231 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2233 loop
->Children
[0] = new_seq(breakIf
, body
);
2234 loop
->Children
[1] = incr
; /* tail code */
2236 /* pop loop, restore prev */
2237 A
->CurLoop
= prevLoop
;
2239 return new_seq(init
, loop
);
2243 static slang_ir_node
*
2244 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2246 slang_ir_node
*n
, *loopNode
;
2247 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2248 loopNode
= A
->CurLoop
;
2250 assert(loopNode
->Opcode
== IR_LOOP
);
2251 n
= new_node0(IR_CONT
);
2253 n
->Parent
= loopNode
;
2254 /* insert this node at head of linked list */
2255 n
->List
= loopNode
->List
;
2263 * Determine if the given operation is of a specific type.
2266 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2268 if (oper
->type
== type
)
2270 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2271 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2272 oper
->num_children
== 1)
2273 return is_operation_type(&oper
->children
[0], type
);
2280 * Generate IR tree for an if/then/else conditional using high-level
2281 * IR_IF instruction.
2283 static slang_ir_node
*
2284 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2287 * eval expr (child[0])
2294 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2295 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2296 GLboolean isConst
, constTrue
;
2298 /* type-check expression */
2299 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2300 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2304 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2305 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2309 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2313 return _slang_gen_operation(A
, &oper
->children
[1]);
2316 /* if (false) ... */
2317 return _slang_gen_operation(A
, &oper
->children
[2]);
2321 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2322 cond
= new_cond(cond
);
2324 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2325 && !haveElseClause
) {
2326 /* Special case: generate a conditional break */
2327 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2330 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2331 && !haveElseClause
) {
2332 /* Special case: generate a conditional break */
2333 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2338 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2340 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2343 ifNode
= new_if(cond
, ifBody
, elseBody
);
2350 static slang_ir_node
*
2351 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2355 assert(oper
->type
== SLANG_OPER_NOT
);
2357 /* type-check expression */
2358 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2359 slang_info_log_error(A
->log
,
2360 "scalar/boolean expression expected for '!'");
2364 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2372 static slang_ir_node
*
2373 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2375 slang_ir_node
*n1
, *n2
;
2377 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2379 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2380 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2381 slang_info_log_error(A
->log
,
2382 "scalar/boolean expressions expected for '^^'");
2386 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2389 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2392 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2397 * Generate IR node for storage of a temporary of given size.
2399 static slang_ir_node
*
2400 _slang_gen_temporary(GLint size
)
2402 slang_ir_storage
*store
;
2403 slang_ir_node
*n
= NULL
;
2405 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2407 n
= new_node0(IR_VAR_DECL
);
2420 * Generate IR node for allocating/declaring a variable.
2422 static slang_ir_node
*
2423 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
2427 /*assert(!var->declared);*/
2428 var
->declared
= GL_TRUE
;
2430 n
= new_node0(IR_VAR_DECL
);
2432 _slang_attach_storage(n
, var
);
2434 assert(n
->Store
== var
->store
);
2436 assert(n
->Store
->Index
< 0);
2438 if (is_sampler_type(&var
->type
)) {
2439 n
->Store
->File
= PROGRAM_SAMPLER
;
2442 n
->Store
->File
= PROGRAM_TEMPORARY
;
2445 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
2447 if (n
->Store
->Size
<= 0) {
2448 slang_info_log_error(A
->log
, "invalid declaration for '%s'",
2449 (char*) var
->a_name
);
2453 printf("%s var %p %s store=%p index=%d size=%d\n",
2454 __FUNCTION__
, (void *) var
, (char *) var
->a_name
,
2455 (void *) n
->Store
, n
->Store
->Index
, n
->Store
->Size
);
2458 if (var
->array_len
> 0) {
2459 /* this is an array */
2460 /* cannot be const-qualified */
2461 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2462 slang_info_log_error(A
->log
, "array '%s' cannot be const",
2463 (char*) var
->a_name
);
2467 /* round up element size to mult of 4 */
2468 GLint sz
= (n
->Store
->Size
+ 3) & ~3;
2469 /* mult by array size */
2470 sz
*= var
->array_len
;
2471 n
->Store
->Size
= sz
;
2475 assert(n
->Store
->Size
> 0);
2477 /* setup default swizzle for storing the variable */
2478 switch (n
->Store
->Size
) {
2480 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2481 SWIZZLE_NIL
, SWIZZLE_NIL
);
2484 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2485 SWIZZLE_Z
, SWIZZLE_NIL
);
2488 /* Note that float-sized vars may be allocated in any x/y/z/w
2489 * slot, but that won't be determined until code emit time.
2491 n
->Store
->Swizzle
= SWIZZLE_NOOP
;
2499 * Generate code for a selection expression: b ? x : y
2500 * XXX In some cases we could implement a selection expression
2501 * with an LRP instruction (use the boolean as the interpolant).
2502 * Otherwise, we use an IF/ELSE/ENDIF construct.
2504 static slang_ir_node
*
2505 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2507 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2508 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2509 slang_typeinfo type0
, type1
, type2
;
2510 int size
, isBool
, isEqual
;
2512 assert(oper
->type
== SLANG_OPER_SELECT
);
2513 assert(oper
->num_children
== 3);
2515 /* type of children[0] must be boolean */
2516 slang_typeinfo_construct(&type0
);
2517 _slang_typeof_operation(A
, &oper
->children
[0], &type0
);
2518 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2519 slang_typeinfo_destruct(&type0
);
2521 slang_info_log_error(A
->log
, "selector type is not boolean");
2525 slang_typeinfo_construct(&type1
);
2526 slang_typeinfo_construct(&type2
);
2527 _slang_typeof_operation(A
, &oper
->children
[1], &type1
);
2528 _slang_typeof_operation(A
, &oper
->children
[2], &type2
);
2529 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2530 slang_typeinfo_destruct(&type1
);
2531 slang_typeinfo_destruct(&type2
);
2533 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2537 /* size of x or y's type */
2538 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2542 tmpDecl
= _slang_gen_temporary(size
);
2544 /* the condition (child 0) */
2545 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2546 cond
= new_cond(cond
);
2548 /* if-true body (child 1) */
2549 tmpVar
= new_node0(IR_VAR
);
2550 tmpVar
->Store
= tmpDecl
->Store
;
2551 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2552 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2554 /* if-false body (child 2) */
2555 tmpVar
= new_node0(IR_VAR
);
2556 tmpVar
->Store
= tmpDecl
->Store
;
2557 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2558 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2560 ifNode
= new_if(cond
, trueNode
, falseNode
);
2563 tmpVar
= new_node0(IR_VAR
);
2564 tmpVar
->Store
= tmpDecl
->Store
;
2566 tree
= new_seq(ifNode
, tmpVar
);
2567 tree
= new_seq(tmpDecl
, tree
);
2569 /*_slang_print_ir_tree(tree, 10);*/
2575 * Generate code for &&.
2577 static slang_ir_node
*
2578 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2580 /* rewrite "a && b" as "a ? b : false" */
2581 slang_operation
*select
;
2584 select
= slang_operation_new(1);
2585 select
->type
= SLANG_OPER_SELECT
;
2586 select
->num_children
= 3;
2587 select
->children
= slang_operation_new(3);
2589 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2590 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2591 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2592 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2593 select
->children
[2].literal_size
= 1;
2595 n
= _slang_gen_select(A
, select
);
2601 * Generate code for ||.
2603 static slang_ir_node
*
2604 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2606 /* rewrite "a || b" as "a ? true : b" */
2607 slang_operation
*select
;
2610 select
= slang_operation_new(1);
2611 select
->type
= SLANG_OPER_SELECT
;
2612 select
->num_children
= 3;
2613 select
->children
= slang_operation_new(3);
2615 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2616 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
2617 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
2618 select
->children
[1].literal_size
= 1;
2619 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
2621 n
= _slang_gen_select(A
, select
);
2627 * Generate IR tree for a return statement.
2629 static slang_ir_node
*
2630 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
2632 const GLboolean haveReturnValue
2633 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
2635 /* error checking */
2636 assert(A
->CurFunction
);
2637 if (haveReturnValue
&&
2638 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
2639 slang_info_log_error(A
->log
, "illegal return expression");
2642 else if (!haveReturnValue
&&
2643 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2644 slang_info_log_error(A
->log
, "return statement requires an expression");
2648 if (!haveReturnValue
) {
2649 return new_return(A
->curFuncEndLabel
);
2657 * return; // goto __endOfFunction
2659 slang_operation
*assign
;
2660 slang_atom a_retVal
;
2663 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2669 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
2671 /* trying to return a value in a void-valued function */
2677 assign
= slang_operation_new(1);
2678 assign
->type
= SLANG_OPER_ASSIGN
;
2679 assign
->num_children
= 2;
2680 assign
->children
= slang_operation_new(2);
2681 /* lhs (__retVal) */
2682 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2683 assign
->children
[0].a_id
= a_retVal
;
2684 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2686 /* XXX we might be able to avoid this copy someday */
2687 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2689 /* assemble the new code */
2690 n
= new_seq(_slang_gen_operation(A
, assign
),
2691 new_return(A
->curFuncEndLabel
));
2693 slang_operation_delete(assign
);
2700 * Determine if the given operation/expression is const-valued.
2703 _slang_is_constant_expr(const slang_operation
*oper
)
2705 slang_variable
*var
;
2708 switch (oper
->type
) {
2709 case SLANG_OPER_IDENTIFIER
:
2710 var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2711 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
2715 for (i
= 0; i
< oper
->num_children
; i
++) {
2716 if (!_slang_is_constant_expr(&oper
->children
[i
]))
2725 * Check if an assignment of type t1 to t0 is legal.
2726 * XXX more cases needed.
2729 _slang_assignment_compatible(slang_assemble_ctx
*A
,
2730 slang_operation
*op0
,
2731 slang_operation
*op1
)
2733 slang_typeinfo t0
, t1
;
2736 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
2737 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
2741 slang_typeinfo_construct(&t0
);
2742 _slang_typeof_operation(A
, op0
, &t0
);
2744 slang_typeinfo_construct(&t1
);
2745 _slang_typeof_operation(A
, op1
, &t1
);
2747 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
2748 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
2752 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
2757 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
2758 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
2759 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
2762 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
2763 t1
.spec
.type
== SLANG_SPEC_BOOL
)
2766 #if 0 /* not used just yet - causes problems elsewhere */
2767 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
2768 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
2772 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
2773 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
2776 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
2777 t1
.spec
.type
== SLANG_SPEC_INT
)
2786 * Generate IR tree for a variable declaration.
2788 static slang_ir_node
*
2789 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
2792 slang_ir_node
*varDecl
;
2794 const char *varName
= (char *) oper
->a_id
;
2795 slang_operation
*initializer
;
2797 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
2798 assert(oper
->num_children
<= 1);
2800 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2802 return NULL
; /* "shouldn't happen" */
2804 if (v
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
2805 v
->type
.qualifier
== SLANG_QUAL_VARYING
||
2806 v
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2807 /* can't declare attribute/uniform vars inside functions */
2808 slang_info_log_error(A
->log
,
2809 "local variable '%s' cannot be an attribute/uniform/varying",
2816 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
2821 varDecl
= _slang_gen_var_decl(A
, v
);
2825 /* check if the var has an initializer */
2826 if (oper
->num_children
> 0) {
2827 assert(oper
->num_children
== 1);
2828 initializer
= &oper
->children
[0];
2830 else if (v
->initializer
) {
2831 initializer
= v
->initializer
;
2837 if (v
->type
.qualifier
== SLANG_QUAL_CONST
&& !initializer
) {
2838 slang_info_log_error(A
->log
,
2839 "const-qualified variable '%s' requires initializer",
2846 slang_ir_node
*var
, *init
;
2848 /* type check/compare var and initializer */
2849 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
2850 slang_info_log_error(A
->log
, "incompatible types in assignment");
2854 var
= new_var(A
, oper
, oper
->a_id
);
2856 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2860 if (v
->type
.qualifier
== SLANG_QUAL_CONST
) {
2861 /* if the variable is const, the initializer must be a const
2862 * expression as well.
2865 if (!_slang_is_constant_expr(initializer
)) {
2866 slang_info_log_error(A
->log
,
2867 "initializer for %s not constant", varName
);
2873 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2875 init
= _slang_gen_operation(A
, initializer
);
2879 /*assert(init->Store);*/
2881 /* XXX remove this when type checking is added above */
2882 if (init
->Store
&& var
->Store
->Size
!= init
->Store
->Size
) {
2883 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2887 n
= new_node2(IR_COPY
, var
, init
);
2888 n
= new_seq(varDecl
, n
);
2899 * Generate IR tree for a variable (such as in an expression).
2901 static slang_ir_node
*
2902 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
2904 /* If there's a variable associated with this oper (from inlining)
2905 * use it. Otherwise, use the oper's var id.
2907 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
2908 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
2910 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
2919 * Return the number of components actually named by the swizzle.
2920 * Recall that swizzles may have undefined/don't-care values.
2923 swizzle_size(GLuint swizzle
)
2926 for (i
= 0; i
< 4; i
++) {
2927 GLuint swz
= GET_SWZ(swizzle
, i
);
2928 size
+= (swz
>= 0 && swz
<= 3);
2934 static slang_ir_node
*
2935 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2937 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2941 n
->Store
= _slang_new_ir_storage_relative(0,
2942 swizzle_size(swizzle
),
2944 n
->Store
->Swizzle
= swizzle
;
2951 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
2953 while (store
->Parent
)
2954 store
= store
->Parent
;
2956 if (!(store
->File
== PROGRAM_OUTPUT
||
2957 store
->File
== PROGRAM_TEMPORARY
||
2958 (store
->File
== PROGRAM_VARYING
&&
2959 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
2969 * Generate IR tree for an assignment (=).
2971 static slang_ir_node
*
2972 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2974 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2975 /* Check that var is writeable */
2977 = _slang_locate_variable(oper
->children
[0].locals
,
2978 oper
->children
[0].a_id
, GL_TRUE
);
2980 slang_info_log_error(A
->log
, "undefined variable '%s'",
2981 (char *) oper
->children
[0].a_id
);
2984 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2985 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
2986 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
2987 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
2988 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
2989 slang_info_log_error(A
->log
,
2990 "illegal assignment to read-only variable '%s'",
2991 (char *) oper
->children
[0].a_id
);
2996 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2997 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2998 /* Special case of: x = f(a, b)
2999 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3001 * XXX this could be even more effective if we could accomodate
3002 * cases such as "v.x = f();" - would help with typical vertex
3006 n
= _slang_gen_function_call_name(A
,
3007 (const char *) oper
->children
[1].a_id
,
3008 &oper
->children
[1], &oper
->children
[0]);
3012 slang_ir_node
*n
, *lhs
, *rhs
;
3014 /* lhs and rhs type checking */
3015 if (!_slang_assignment_compatible(A
,
3017 &oper
->children
[1])) {
3018 slang_info_log_error(A
->log
, "incompatible types in assignment");
3022 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3028 slang_info_log_error(A
->log
,
3029 "invalid left hand side for assignment");
3033 /* check that lhs is writable */
3034 if (!is_store_writable(A
, lhs
->Store
)) {
3035 slang_info_log_error(A
->log
,
3036 "illegal assignment to read-only l-value");
3040 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3042 /* convert lhs swizzle into writemask */
3043 GLuint writemask
, newSwizzle
;
3044 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3045 &writemask
, &newSwizzle
)) {
3046 /* Non-simple writemask, need to swizzle right hand side in
3047 * order to put components into the right place.
3049 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3051 n
= new_node2(IR_COPY
, lhs
, rhs
);
3062 * Generate IR tree for referencing a field in a struct (or basic vector type)
3064 static slang_ir_node
*
3065 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3069 /* type of struct */
3070 slang_typeinfo_construct(&ti
);
3071 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
3073 if (_slang_type_is_vector(ti
.spec
.type
)) {
3074 /* the field should be a swizzle */
3075 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3079 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3080 slang_info_log_error(A
->log
, "Bad swizzle");
3083 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3088 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3089 /* create new parent node with swizzle */
3091 n
= _slang_gen_swizzle(n
, swizzle
);
3094 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3095 || ti
.spec
.type
== SLANG_SPEC_INT
3096 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3097 const GLuint rows
= 1;
3101 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3102 slang_info_log_error(A
->log
, "Bad swizzle");
3104 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3108 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3109 /* create new parent node with swizzle */
3110 n
= _slang_gen_swizzle(n
, swizzle
);
3114 /* the field is a structure member (base.field) */
3115 /* oper->children[0] is the base */
3116 /* oper->a_id is the field name */
3117 slang_ir_node
*base
, *n
;
3118 slang_typeinfo field_ti
;
3119 GLint fieldSize
, fieldOffset
= -1;
3122 slang_typeinfo_construct(&field_ti
);
3123 _slang_typeof_operation(A
, oper
, &field_ti
);
3125 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3127 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3129 if (fieldSize
== 0 || fieldOffset
< 0) {
3130 const char *structName
;
3131 if (ti
.spec
._struct
)
3132 structName
= (char *) ti
.spec
._struct
->a_name
;
3134 structName
= "unknown";
3135 slang_info_log_error(A
->log
,
3136 "\"%s\" is not a member of struct \"%s\"",
3137 (char *) oper
->a_id
, structName
);
3140 assert(fieldSize
>= 0);
3142 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3144 /* error msg should have already been logged */
3148 n
= new_node1(IR_FIELD
, base
);
3152 n
->Field
= (char *) oper
->a_id
;
3154 /* Store the field's offset in storage->Index */
3155 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3165 * Gen code for array indexing.
3167 static slang_ir_node
*
3168 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3170 slang_typeinfo array_ti
;
3172 /* get array's type info */
3173 slang_typeinfo_construct(&array_ti
);
3174 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
3176 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3177 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3178 /* translate the index into a swizzle/writemask: "v.x=p" */
3179 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3183 index
= (GLint
) oper
->children
[1].literal
[0];
3184 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3185 index
>= (GLint
) max
) {
3186 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3190 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3192 /* use swizzle to access the element */
3193 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3197 n
= _slang_gen_swizzle(n
, swizzle
);
3203 /* conventional array */
3204 slang_typeinfo elem_ti
;
3205 slang_ir_node
*elem
, *array
, *index
;
3206 GLint elemSize
, arrayLen
;
3208 /* size of array element */
3209 slang_typeinfo_construct(&elem_ti
);
3210 _slang_typeof_operation(A
, oper
, &elem_ti
);
3211 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3213 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3214 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3216 arrayLen
= array_ti
.array_len
;
3218 slang_typeinfo_destruct(&array_ti
);
3219 slang_typeinfo_destruct(&elem_ti
);
3221 if (elemSize
<= 0) {
3222 /* unknown var or type */
3223 slang_info_log_error(A
->log
, "Undefined variable or type");
3227 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3228 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3229 if (array
&& index
) {
3231 GLint constIndex
= -1;
3232 if (index
->Opcode
== IR_FLOAT
) {
3233 constIndex
= (int) index
->Value
[0];
3234 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3235 slang_info_log_error(A
->log
,
3236 "Array index out of bounds (index=%d size=%d)",
3237 constIndex
, arrayLen
);
3238 _slang_free_ir_tree(array
);
3239 _slang_free_ir_tree(index
);
3244 if (!array
->Store
) {
3245 slang_info_log_error(A
->log
, "Invalid array");
3249 elem
= new_node2(IR_ELEMENT
, array
, index
);
3251 /* The storage info here will be updated during code emit */
3252 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3253 array
->Store
->Index
,
3259 _slang_free_ir_tree(array
);
3260 _slang_free_ir_tree(index
);
3267 static slang_ir_node
*
3268 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3269 slang_ir_opcode opcode
)
3271 slang_typeinfo t0
, t1
;
3274 slang_typeinfo_construct(&t0
);
3275 _slang_typeof_operation(A
, &oper
->children
[0], &t0
);
3277 slang_typeinfo_construct(&t1
);
3278 _slang_typeof_operation(A
, &oper
->children
[0], &t1
);
3280 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3281 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3282 slang_info_log_error(A
->log
, "Illegal array comparison");
3286 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3287 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3288 /* <, <=, >, >= can only be used with scalars */
3289 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3290 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3291 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3292 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3293 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3298 n
= new_node2(opcode
,
3299 _slang_gen_operation(A
, &oper
->children
[0]),
3300 _slang_gen_operation(A
, &oper
->children
[1]));
3302 /* result is a bool (size 1) */
3303 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3311 print_vars(slang_variable_scope
*s
)
3315 for (i
= 0; i
< s
->num_variables
; i
++) {
3317 (char*) s
->variables
[i
]->a_name
,
3318 s
->variables
[i
]->declared
);
3328 _slang_undeclare_vars(slang_variable_scope
*locals
)
3330 if (locals
->num_variables
> 0) {
3332 for (i
= 0; i
< locals
->num_variables
; i
++) {
3333 slang_variable
*v
= locals
->variables
[i
];
3334 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3335 v
->declared
= GL_FALSE
;
3343 * Generate IR tree for a slang_operation (AST node)
3345 static slang_ir_node
*
3346 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3348 switch (oper
->type
) {
3349 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3353 _slang_push_var_table(A
->vartable
);
3355 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3356 n
= _slang_gen_operation(A
, oper
);
3357 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3359 _slang_pop_var_table(A
->vartable
);
3361 /*_slang_undeclare_vars(oper->locals);*/
3362 /*print_vars(oper->locals);*/
3365 n
= new_node1(IR_SCOPE
, n
);
3370 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3371 /* list of operations */
3372 if (oper
->num_children
> 0)
3374 slang_ir_node
*n
, *tree
= NULL
;
3377 for (i
= 0; i
< oper
->num_children
; i
++) {
3378 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3380 _slang_free_ir_tree(tree
);
3381 return NULL
; /* error must have occured */
3383 tree
= new_seq(tree
, n
);
3389 return new_node0(IR_NOP
);
3392 case SLANG_OPER_EXPRESSION
:
3393 return _slang_gen_operation(A
, &oper
->children
[0]);
3395 case SLANG_OPER_FOR
:
3396 return _slang_gen_for(A
, oper
);
3398 return _slang_gen_do(A
, oper
);
3399 case SLANG_OPER_WHILE
:
3400 return _slang_gen_while(A
, oper
);
3401 case SLANG_OPER_BREAK
:
3403 slang_info_log_error(A
->log
, "'break' not in loop");
3406 return new_break(A
->CurLoop
);
3407 case SLANG_OPER_CONTINUE
:
3409 slang_info_log_error(A
->log
, "'continue' not in loop");
3412 return _slang_gen_continue(A
, oper
);
3413 case SLANG_OPER_DISCARD
:
3414 return new_node0(IR_KILL
);
3416 case SLANG_OPER_EQUAL
:
3417 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3418 case SLANG_OPER_NOTEQUAL
:
3419 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3420 case SLANG_OPER_GREATER
:
3421 return _slang_gen_compare(A
, oper
, IR_SGT
);
3422 case SLANG_OPER_LESS
:
3423 return _slang_gen_compare(A
, oper
, IR_SLT
);
3424 case SLANG_OPER_GREATEREQUAL
:
3425 return _slang_gen_compare(A
, oper
, IR_SGE
);
3426 case SLANG_OPER_LESSEQUAL
:
3427 return _slang_gen_compare(A
, oper
, IR_SLE
);
3428 case SLANG_OPER_ADD
:
3431 assert(oper
->num_children
== 2);
3432 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3435 case SLANG_OPER_SUBTRACT
:
3438 assert(oper
->num_children
== 2);
3439 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3442 case SLANG_OPER_MULTIPLY
:
3445 assert(oper
->num_children
== 2);
3446 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3449 case SLANG_OPER_DIVIDE
:
3452 assert(oper
->num_children
== 2);
3453 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3456 case SLANG_OPER_MINUS
:
3459 assert(oper
->num_children
== 1);
3460 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3463 case SLANG_OPER_PLUS
:
3464 /* +expr --> do nothing */
3465 return _slang_gen_operation(A
, &oper
->children
[0]);
3466 case SLANG_OPER_VARIABLE_DECL
:
3467 return _slang_gen_declaration(A
, oper
);
3468 case SLANG_OPER_ASSIGN
:
3469 return _slang_gen_assignment(A
, oper
);
3470 case SLANG_OPER_ADDASSIGN
:
3473 assert(oper
->num_children
== 2);
3474 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3477 case SLANG_OPER_SUBASSIGN
:
3480 assert(oper
->num_children
== 2);
3481 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3485 case SLANG_OPER_MULASSIGN
:
3488 assert(oper
->num_children
== 2);
3489 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3492 case SLANG_OPER_DIVASSIGN
:
3495 assert(oper
->num_children
== 2);
3496 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3499 case SLANG_OPER_LOGICALAND
:
3502 assert(oper
->num_children
== 2);
3503 n
= _slang_gen_logical_and(A
, oper
);
3506 case SLANG_OPER_LOGICALOR
:
3509 assert(oper
->num_children
== 2);
3510 n
= _slang_gen_logical_or(A
, oper
);
3513 case SLANG_OPER_LOGICALXOR
:
3514 return _slang_gen_xor(A
, oper
);
3515 case SLANG_OPER_NOT
:
3516 return _slang_gen_not(A
, oper
);
3517 case SLANG_OPER_SELECT
: /* b ? x : y */
3520 assert(oper
->num_children
== 3);
3521 n
= _slang_gen_select(A
, oper
);
3525 case SLANG_OPER_ASM
:
3526 return _slang_gen_asm(A
, oper
, NULL
);
3527 case SLANG_OPER_CALL
:
3528 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3530 case SLANG_OPER_RETURN
:
3531 return _slang_gen_return(A
, oper
);
3532 case SLANG_OPER_LABEL
:
3533 return new_label(oper
->label
);
3534 case SLANG_OPER_IDENTIFIER
:
3535 return _slang_gen_variable(A
, oper
);
3537 return _slang_gen_if(A
, oper
);
3538 case SLANG_OPER_FIELD
:
3539 return _slang_gen_struct_field(A
, oper
);
3540 case SLANG_OPER_SUBSCRIPT
:
3541 return _slang_gen_array_element(A
, oper
);
3542 case SLANG_OPER_LITERAL_FLOAT
:
3544 case SLANG_OPER_LITERAL_INT
:
3546 case SLANG_OPER_LITERAL_BOOL
:
3547 return new_float_literal(oper
->literal
, oper
->literal_size
);
3549 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3552 assert(oper
->num_children
== 1);
3553 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3556 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3559 assert(oper
->num_children
== 1);
3560 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3563 case SLANG_OPER_PREINCREMENT
: /* ++var */
3566 assert(oper
->num_children
== 1);
3567 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3570 case SLANG_OPER_PREDECREMENT
: /* --var */
3573 assert(oper
->num_children
== 1);
3574 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3578 case SLANG_OPER_NON_INLINED_CALL
:
3579 case SLANG_OPER_SEQUENCE
:
3581 slang_ir_node
*tree
= NULL
;
3583 for (i
= 0; i
< oper
->num_children
; i
++) {
3584 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3585 tree
= new_seq(tree
, n
);
3587 tree
->Store
= n
->Store
;
3589 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3590 tree
= new_function_call(tree
, oper
->label
);
3595 case SLANG_OPER_NONE
:
3596 case SLANG_OPER_VOID
:
3597 /* returning NULL here would generate an error */
3598 return new_node0(IR_NOP
);
3601 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3603 return new_node0(IR_NOP
);
3611 * Compute total size of array give size of element, number of elements.
3614 array_size(GLint baseSize
, GLint arrayLen
)
3618 /* round up base type to multiple of 4 */
3619 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3629 * Called by compiler when a global variable has been parsed/compiled.
3630 * Here we examine the variable's type to determine what kind of register
3631 * storage will be used.
3633 * A uniform such as "gl_Position" will become the register specification
3634 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3635 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3637 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3638 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3639 * actual texture unit (as specified by the user calling glUniform1i()).
3642 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3643 slang_unit_type type
)
3645 struct gl_program
*prog
= A
->program
;
3646 const char *varName
= (char *) var
->a_name
;
3647 GLboolean success
= GL_TRUE
;
3648 slang_ir_storage
*store
= NULL
;
3650 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3651 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3652 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3654 if (texIndex
!= -1) {
3655 /* This is a texture sampler variable...
3656 * store->File = PROGRAM_SAMPLER
3657 * store->Index = sampler number (0..7, typically)
3658 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
3660 if (var
->initializer
) {
3661 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
3664 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
3665 /* disallow rect samplers */
3666 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
3667 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
3668 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
3673 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
3674 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
3676 if (dbg
) printf("SAMPLER ");
3678 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3679 /* Uniform variable */
3680 const GLint totalSize
= array_size(size
, var
->array_len
);
3681 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
3684 /* user-defined uniform */
3685 if (datatype
== GL_NONE
) {
3686 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
3687 /* temporary work-around */
3688 GLenum datatype
= GL_FLOAT
;
3689 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3690 totalSize
, datatype
, NULL
);
3691 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3692 totalSize
, swizzle
);
3694 /* XXX what we need to do is unroll the struct into its
3695 * basic types, creating a uniform variable for each.
3703 * Should produce uniforms:
3704 * "f.a" (GL_FLOAT_VEC3)
3705 * "f.b" (GL_FLOAT_VEC4)
3708 if (var
->initializer
) {
3709 slang_info_log_error(A
->log
,
3710 "unsupported initializer for uniform '%s'", varName
);
3715 slang_info_log_error(A
->log
,
3716 "invalid datatype for uniform variable %s",
3723 const GLfloat
*initialValues
= NULL
;
3724 if (var
->initializer
) {
3725 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3726 if (var
->initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3727 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3728 /* simple float/vector initializer */
3729 initialValues
= var
->initializer
->literal
;
3732 /* complex initializer */
3733 slang_info_log_error(A
->log
,
3734 "unsupported initializer for uniform '%s'", varName
);
3739 uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3740 totalSize
, datatype
, initialValues
);
3741 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3742 totalSize
, swizzle
);
3746 /* pre-defined uniform, like gl_ModelviewMatrix */
3747 /* We know it's a uniform, but don't allocate storage unless
3750 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
3751 totalSize
, swizzle
);
3753 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
3755 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
3756 const GLint totalSize
= array_size(size
, var
->array_len
);
3758 /* varyings must be float, vec or mat */
3759 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
3760 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
3761 slang_info_log_error(A
->log
,
3762 "varying '%s' must be float/vector/matrix",
3767 if (var
->initializer
) {
3768 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
3774 /* user-defined varying */
3775 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, totalSize
);
3776 GLuint swizzle
= _slang_var_swizzle(size
, 0);
3777 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
3778 totalSize
, swizzle
);
3781 /* pre-defined varying, like gl_Color or gl_TexCoord */
3782 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
3783 /* fragment program input */
3785 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3788 assert(index
< FRAG_ATTRIB_MAX
);
3789 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
3793 /* vertex program output */
3794 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3795 GLuint swizzle
= _slang_var_swizzle(size
, 0);
3797 assert(index
< VERT_RESULT_MAX
);
3798 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
3799 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
3802 if (dbg
) printf("V/F ");
3804 if (dbg
) printf("VARYING ");
3806 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
3809 /* attributes must be float, vec or mat */
3810 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
3811 slang_info_log_error(A
->log
,
3812 "attribute '%s' must be float/vector/matrix",
3818 /* user-defined vertex attribute */
3819 const GLint attr
= -1; /* unknown */
3820 swizzle
= _slang_var_swizzle(size
, 0);
3821 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
3822 size
, datatype
, attr
);
3824 index
= VERT_ATTRIB_GENERIC0
+ index
;
3827 /* pre-defined vertex attrib */
3828 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
3831 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3832 if (dbg
) printf("ATTRIB ");
3834 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
3835 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
3836 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3838 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3839 if (dbg
) printf("INPUT ");
3841 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
3842 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
3843 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3844 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3847 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
3848 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
3849 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
3850 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
3852 if (dbg
) printf("OUTPUT ");
3854 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
3855 /* pre-defined global constant, like gl_MaxLights */
3856 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
3857 if (dbg
) printf("CONST ");
3860 /* ordinary variable (may be const) */
3863 /* IR node to declare the variable */
3864 n
= _slang_gen_var_decl(A
, var
);
3866 /* IR code for the var's initializer, if present */
3867 if (var
->initializer
) {
3868 slang_ir_node
*lhs
, *rhs
, *init
;
3870 /* Generate IR_COPY instruction to initialize the variable */
3871 lhs
= new_node0(IR_VAR
);
3873 lhs
->Store
= n
->Store
;
3875 /* constant folding, etc */
3876 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3878 rhs
= _slang_gen_operation(A
, var
->initializer
);
3880 init
= new_node2(IR_COPY
, lhs
, rhs
);
3881 n
= new_seq(n
, init
);
3884 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
3886 _slang_free_ir_tree(n
);
3889 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
3890 store
? store
->Index
: -2);
3893 var
->store
= store
; /* save var's storage info */
3895 var
->declared
= GL_TRUE
;
3902 * Produce an IR tree from a function AST (fun->body).
3903 * Then call the code emitter to convert the IR tree into gl_program
3907 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
3910 GLboolean success
= GL_TRUE
;
3912 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
3913 /* we only really generate code for main, all other functions get
3914 * inlined or codegen'd upon an actual call.
3917 /* do some basic error checking though */
3918 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3919 /* check that non-void functions actually return something */
3921 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
3923 slang_info_log_error(A
->log
,
3924 "function \"%s\" has no return statement",
3925 (char *) fun
->header
.a_name
);
3927 "function \"%s\" has no return statement\n",
3928 (char *) fun
->header
.a_name
);
3933 return GL_TRUE
; /* not an error */
3937 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
3938 slang_print_function(fun
, 1);
3941 /* should have been allocated earlier: */
3942 assert(A
->program
->Parameters
);
3943 assert(A
->program
->Varying
);
3944 assert(A
->vartable
);
3946 A
->CurFunction
= fun
;
3948 /* fold constant expressions, etc. */
3949 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
3952 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
3953 slang_print_function(fun
, 1);
3956 /* Create an end-of-function label */
3957 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
3959 /* push new vartable scope */
3960 _slang_push_var_table(A
->vartable
);
3962 /* Generate IR tree for the function body code */
3963 n
= _slang_gen_operation(A
, fun
->body
);
3965 n
= new_node1(IR_SCOPE
, n
);
3967 /* pop vartable, restore previous */
3968 _slang_pop_var_table(A
->vartable
);
3971 /* XXX record error */
3975 /* append an end-of-function-label to IR tree */
3976 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
3978 /*_slang_label_delete(A->curFuncEndLabel);*/
3979 A
->curFuncEndLabel
= NULL
;
3982 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
3983 slang_print_function(fun
, 1);
3986 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
3987 _slang_print_ir_tree(n
, 0);
3990 printf("************* End codegen function ************\n\n");
3993 /* Emit program instructions */
3994 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
3995 _slang_free_ir_tree(n
);
3997 /* free codegen context */
3999 _mesa_free(A->codegen);