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 { "vec3_cross", IR_CROSS
, 1, 2 },
417 { "vec4_lrp", IR_LRP
, 1, 3 },
418 { "vec4_min", IR_MIN
, 1, 2 },
419 { "vec4_max", IR_MAX
, 1, 2 },
420 { "vec4_clamp", IR_CLAMP
, 1, 3 },
421 { "vec4_seq", IR_SEQUAL
, 1, 2 },
422 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
423 { "vec4_sge", IR_SGE
, 1, 2 },
424 { "vec4_sgt", IR_SGT
, 1, 2 },
425 { "vec4_sle", IR_SLE
, 1, 2 },
426 { "vec4_slt", IR_SLT
, 1, 2 },
428 { "vec4_move", IR_MOVE
, 1, 1 },
429 { "vec4_floor", IR_FLOOR
, 1, 1 },
430 { "vec4_frac", IR_FRAC
, 1, 1 },
431 { "vec4_abs", IR_ABS
, 1, 1 },
432 { "vec4_negate", IR_NEG
, 1, 1 },
433 { "vec4_ddx", IR_DDX
, 1, 1 },
434 { "vec4_ddy", IR_DDY
, 1, 1 },
435 /* float binary op */
436 { "float_power", IR_POW
, 1, 2 },
437 /* texture / sampler */
438 { "vec4_tex1d", IR_TEX
, 1, 2 },
439 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
440 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
441 { "vec4_tex2d", IR_TEX
, 1, 2 },
442 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
443 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
444 { "vec4_tex3d", IR_TEX
, 1, 2 },
445 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
446 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
447 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
448 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
449 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
452 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
453 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
454 { "float_exp", IR_EXP
, 1, 1 },
455 { "float_exp2", IR_EXP2
, 1, 1 },
456 { "float_log2", IR_LOG2
, 1, 1 },
457 { "float_rsq", IR_RSQ
, 1, 1 },
458 { "float_rcp", IR_RCP
, 1, 1 },
459 { "float_sine", IR_SIN
, 1, 1 },
460 { "float_cosine", IR_COS
, 1, 1 },
461 { "float_noise1", IR_NOISE1
, 1, 1},
462 { "float_noise2", IR_NOISE2
, 1, 1},
463 { "float_noise3", IR_NOISE3
, 1, 1},
464 { "float_noise4", IR_NOISE4
, 1, 1},
466 { NULL
, IR_NOP
, 0, 0 }
470 static slang_ir_node
*
471 new_node3(slang_ir_opcode op
,
472 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
474 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
480 n
->InstLocation
= -1;
485 static slang_ir_node
*
486 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
488 return new_node3(op
, c0
, c1
, NULL
);
491 static slang_ir_node
*
492 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
494 return new_node3(op
, c0
, NULL
, NULL
);
497 static slang_ir_node
*
498 new_node0(slang_ir_opcode op
)
500 return new_node3(op
, NULL
, NULL
, NULL
);
505 * Create sequence of two nodes.
507 static slang_ir_node
*
508 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
514 return new_node2(IR_SEQ
, left
, right
);
517 static slang_ir_node
*
518 new_label(slang_label
*label
)
520 slang_ir_node
*n
= new_node0(IR_LABEL
);
527 static slang_ir_node
*
528 new_float_literal(const float v
[4], GLuint size
)
530 slang_ir_node
*n
= new_node0(IR_FLOAT
);
532 COPY_4V(n
->Value
, v
);
533 /* allocate a storage object, but compute actual location (Index) later */
534 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
539 static slang_ir_node
*
540 new_not(slang_ir_node
*n
)
542 return new_node1(IR_NOT
, n
);
547 * Non-inlined function call.
549 static slang_ir_node
*
550 new_function_call(slang_ir_node
*code
, slang_label
*name
)
552 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
561 * Unconditional jump.
563 static slang_ir_node
*
564 new_return(slang_label
*dest
)
566 slang_ir_node
*n
= new_node0(IR_RETURN
);
574 static slang_ir_node
*
575 new_loop(slang_ir_node
*body
)
577 return new_node1(IR_LOOP
, body
);
581 static slang_ir_node
*
582 new_break(slang_ir_node
*loopNode
)
584 slang_ir_node
*n
= new_node0(IR_BREAK
);
586 assert(loopNode
->Opcode
== IR_LOOP
);
588 /* insert this node at head of linked list */
589 n
->List
= loopNode
->List
;
597 * Make new IR_BREAK_IF_TRUE.
599 static slang_ir_node
*
600 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
604 assert(loopNode
->Opcode
== IR_LOOP
);
605 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
607 /* insert this node at head of linked list */
608 n
->List
= loopNode
->List
;
616 * Make new IR_CONT_IF_TRUE node.
618 static slang_ir_node
*
619 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
623 assert(loopNode
->Opcode
== IR_LOOP
);
624 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
626 /* insert this node at head of linked list */
627 n
->List
= loopNode
->List
;
634 static slang_ir_node
*
635 new_cond(slang_ir_node
*n
)
637 slang_ir_node
*c
= new_node1(IR_COND
, n
);
642 static slang_ir_node
*
643 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
645 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
650 * New IR_VAR node - a reference to a previously declared variable.
652 static slang_ir_node
*
653 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
656 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
660 assert(var
->declared
);
662 assert(!oper
->var
|| oper
->var
== var
);
664 n
= new_node0(IR_VAR
);
666 _slang_attach_storage(n
, var
);
668 printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
676 * Check if the given function is really just a wrapper for a
677 * basic assembly instruction.
680 slang_is_asm_function(const slang_function
*fun
)
682 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
683 fun
->body
->num_children
== 1 &&
684 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
692 _slang_is_noop(const slang_operation
*oper
)
695 oper
->type
== SLANG_OPER_VOID
||
696 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
704 * Recursively search tree for a node of the given type.
706 static slang_operation
*
707 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
710 if (oper
->type
== type
)
712 for (i
= 0; i
< oper
->num_children
; i
++) {
713 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
722 * Count the number of operations of the given time rooted at 'oper'.
725 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
728 if (oper
->type
== type
) {
731 for (i
= 0; i
< oper
->num_children
; i
++) {
732 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
739 * Check if the 'return' statement found under 'oper' is a "tail return"
740 * that can be no-op'd. For example:
745 * return; // this is a no-op
748 * This is used when determining if a function can be inlined. If the
749 * 'return' is not the last statement, we can't inline the function since
750 * we still need the semantic behaviour of the 'return' but we don't want
751 * to accidentally return from the _calling_ function. We'd need to use an
752 * unconditional branch, but we don't have such a GPU instruction (not
756 _slang_is_tail_return(const slang_operation
*oper
)
758 GLuint k
= oper
->num_children
;
761 const slang_operation
*last
= &oper
->children
[k
- 1];
762 if (last
->type
== SLANG_OPER_RETURN
)
764 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
765 last
->type
== SLANG_OPER_LABEL
)
766 k
--; /* try prev child */
767 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
768 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
769 /* try sub-children */
770 return _slang_is_tail_return(last
);
780 slang_resolve_variable(slang_operation
*oper
)
782 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
783 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
789 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
792 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
793 GLuint substCount
, slang_variable
**substOld
,
794 slang_operation
**substNew
, GLboolean isLHS
)
796 switch (oper
->type
) {
797 case SLANG_OPER_VARIABLE_DECL
:
799 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
800 oper
->a_id
, GL_TRUE
);
802 if (v
->initializer
&& oper
->num_children
== 0) {
803 /* set child of oper to copy of initializer */
804 oper
->num_children
= 1;
805 oper
->children
= slang_operation_new(1);
806 slang_operation_copy(&oper
->children
[0], v
->initializer
);
808 if (oper
->num_children
== 1) {
809 /* the initializer */
810 slang_substitute(A
, &oper
->children
[0], substCount
,
811 substOld
, substNew
, GL_FALSE
);
815 case SLANG_OPER_IDENTIFIER
:
816 assert(oper
->num_children
== 0);
817 if (1/**!isLHS XXX FIX */) {
818 slang_atom id
= oper
->a_id
;
821 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
823 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
827 /* look for a substitution */
828 for (i
= 0; i
< substCount
; i
++) {
829 if (v
== substOld
[i
]) {
830 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
831 #if 0 /* DEBUG only */
832 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
833 assert(substNew
[i
]->var
);
834 assert(substNew
[i
]->var
->a_name
);
835 printf("Substitute %s with %s in id node %p\n",
836 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
840 printf("Substitute %s with %f in id node %p\n",
841 (char*)v
->a_name
, substNew
[i
]->literal
[0],
845 slang_operation_copy(oper
, substNew
[i
]);
852 case SLANG_OPER_RETURN
:
853 /* do return replacement here too */
854 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
855 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
861 * then do substitutions on the assignment.
863 slang_operation
*blockOper
, *assignOper
, *returnOper
;
865 /* check if function actually has a return type */
866 assert(A
->CurFunction
);
867 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
868 slang_info_log_error(A
->log
, "illegal return expression");
872 blockOper
= slang_operation_new(1);
873 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
874 blockOper
->num_children
= 2;
875 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
876 blockOper
->children
= slang_operation_new(2);
877 assignOper
= blockOper
->children
+ 0;
878 returnOper
= blockOper
->children
+ 1;
880 assignOper
->type
= SLANG_OPER_ASSIGN
;
881 assignOper
->num_children
= 2;
882 assignOper
->locals
->outer_scope
= blockOper
->locals
;
883 assignOper
->children
= slang_operation_new(2);
884 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
885 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
886 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
888 slang_operation_copy(&assignOper
->children
[1],
891 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
892 assert(returnOper
->num_children
== 0);
894 /* do substitutions on the "__retVal = expr" sub-tree */
895 slang_substitute(A
, assignOper
,
896 substCount
, substOld
, substNew
, GL_FALSE
);
898 /* install new code */
899 slang_operation_copy(oper
, blockOper
);
900 slang_operation_destruct(blockOper
);
903 /* check if return value was expected */
904 assert(A
->CurFunction
);
905 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
906 slang_info_log_error(A
->log
, "return statement requires an expression");
912 case SLANG_OPER_ASSIGN
:
913 case SLANG_OPER_SUBSCRIPT
:
915 * child[0] can't have substitutions but child[1] can.
917 slang_substitute(A
, &oper
->children
[0],
918 substCount
, substOld
, substNew
, GL_TRUE
);
919 slang_substitute(A
, &oper
->children
[1],
920 substCount
, substOld
, substNew
, GL_FALSE
);
922 case SLANG_OPER_FIELD
:
924 slang_substitute(A
, &oper
->children
[0],
925 substCount
, substOld
, substNew
, GL_TRUE
);
930 for (i
= 0; i
< oper
->num_children
; i
++)
931 slang_substitute(A
, &oper
->children
[i
],
932 substCount
, substOld
, substNew
, GL_FALSE
);
939 * Produce inline code for a call to an assembly instruction.
940 * This is typically used to compile a call to a built-in function like this:
942 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
944 * __asm vec4_lrp __retVal, a, y, x;
949 * r = mix(p1, p2, p3);
959 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
961 static slang_operation
*
962 slang_inline_asm_function(slang_assemble_ctx
*A
,
963 slang_function
*fun
, slang_operation
*oper
)
965 const GLuint numArgs
= oper
->num_children
;
967 slang_operation
*inlined
;
968 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
969 slang_variable
**substOld
;
970 slang_operation
**substNew
;
972 ASSERT(slang_is_asm_function(fun
));
973 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
976 printf("Inline %s as %s\n",
977 (char*) fun->header.a_name,
978 (char*) fun->body->children[0].a_id);
982 * We'll substitute formal params with actual args in the asm call.
984 substOld
= (slang_variable
**)
985 _slang_alloc(numArgs
* sizeof(slang_variable
*));
986 substNew
= (slang_operation
**)
987 _slang_alloc(numArgs
* sizeof(slang_operation
*));
988 for (i
= 0; i
< numArgs
; i
++) {
989 substOld
[i
] = fun
->parameters
->variables
[i
];
990 substNew
[i
] = oper
->children
+ i
;
993 /* make a copy of the code to inline */
994 inlined
= slang_operation_new(1);
995 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
997 /* get rid of the __retVal child */
998 inlined
->num_children
--;
999 for (i
= 0; i
< inlined
->num_children
; i
++) {
1000 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1004 /* now do formal->actual substitutions */
1005 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1007 _slang_free(substOld
);
1008 _slang_free(substNew
);
1011 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1012 (char *) fun
->header
.a_name
);
1013 slang_print_tree(inlined
, 3);
1014 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1022 * Inline the given function call operation.
1023 * Return a new slang_operation that corresponds to the inlined code.
1025 static slang_operation
*
1026 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1027 slang_operation
*oper
, slang_operation
*returnOper
)
1034 ParamMode
*paramMode
;
1035 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1036 const GLuint numArgs
= oper
->num_children
;
1037 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1038 slang_operation
*args
= oper
->children
;
1039 slang_operation
*inlined
, *top
;
1040 slang_variable
**substOld
;
1041 slang_operation
**substNew
;
1042 GLuint substCount
, numCopyIn
, i
;
1043 slang_function
*prevFunction
;
1044 slang_variable_scope
*newScope
= NULL
;
1047 prevFunction
= A
->CurFunction
;
1048 A
->CurFunction
= fun
;
1050 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1051 assert(fun
->param_count
== totalArgs
);
1053 /* allocate temporary arrays */
1054 paramMode
= (ParamMode
*)
1055 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1056 substOld
= (slang_variable
**)
1057 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1058 substNew
= (slang_operation
**)
1059 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1062 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1063 (char *) fun
->header
.a_name
,
1064 fun
->parameters
->num_variables
, numArgs
);
1067 if (haveRetValue
&& !returnOper
) {
1068 /* Create 3-child comma sequence for inlined code:
1069 * child[0]: declare __resultTmp
1070 * child[1]: inlined function body
1071 * child[2]: __resultTmp
1073 slang_operation
*commaSeq
;
1074 slang_operation
*declOper
= NULL
;
1075 slang_variable
*resultVar
;
1077 commaSeq
= slang_operation_new(1);
1078 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1079 assert(commaSeq
->locals
);
1080 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1081 commaSeq
->num_children
= 3;
1082 commaSeq
->children
= slang_operation_new(3);
1083 /* allocate the return var */
1084 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1086 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1087 (void*)commaSeq->locals, (char *) fun->header.a_name);
1090 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1091 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1092 resultVar
->isTemp
= GL_TRUE
;
1094 /* child[0] = __resultTmp declaration */
1095 declOper
= &commaSeq
->children
[0];
1096 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1097 declOper
->a_id
= resultVar
->a_name
;
1098 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1100 /* child[1] = function body */
1101 inlined
= &commaSeq
->children
[1];
1102 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1104 /* child[2] = __resultTmp reference */
1105 returnOper
= &commaSeq
->children
[2];
1106 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1107 returnOper
->a_id
= resultVar
->a_name
;
1108 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1113 top
= inlined
= slang_operation_new(1);
1114 /* XXXX this may be inappropriate!!!! */
1115 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1119 assert(inlined
->locals
);
1121 /* Examine the parameters, look for inout/out params, look for possible
1122 * substitutions, etc:
1123 * param type behaviour
1124 * in copy actual to local
1125 * const in substitute param with actual
1129 for (i
= 0; i
< totalArgs
; i
++) {
1130 slang_variable
*p
= fun
->parameters
->variables
[i
];
1132 printf("Param %d: %s %s \n", i,
1133 slang_type_qual_string(p->type.qualifier),
1134 (char *) p->a_name);
1136 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1137 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1138 /* an output param */
1139 slang_operation
*arg
;
1144 paramMode
[i
] = SUBST
;
1146 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1147 slang_resolve_variable(arg
);
1149 /* replace parameter 'p' with argument 'arg' */
1150 substOld
[substCount
] = p
;
1151 substNew
[substCount
] = arg
; /* will get copied */
1154 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1155 /* a constant input param */
1156 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1157 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1158 /* replace all occurances of this parameter variable with the
1159 * actual argument variable or a literal.
1161 paramMode
[i
] = SUBST
;
1162 slang_resolve_variable(&args
[i
]);
1163 substOld
[substCount
] = p
;
1164 substNew
[substCount
] = &args
[i
]; /* will get copied */
1168 paramMode
[i
] = COPY_IN
;
1172 paramMode
[i
] = COPY_IN
;
1174 assert(paramMode
[i
]);
1177 /* actual code inlining: */
1178 slang_operation_copy(inlined
, fun
->body
);
1180 /*** XXX review this */
1181 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1182 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1183 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1186 printf("======================= orig body code ======================\n");
1187 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1188 slang_print_tree(fun
->body
, 8);
1189 printf("======================= copied code =========================\n");
1190 slang_print_tree(inlined
, 8);
1193 /* do parameter substitution in inlined code: */
1194 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1197 printf("======================= subst code ==========================\n");
1198 slang_print_tree(inlined
, 8);
1199 printf("=============================================================\n");
1202 /* New prolog statements: (inserted before the inlined code)
1203 * Copy the 'in' arguments.
1206 for (i
= 0; i
< numArgs
; i
++) {
1207 if (paramMode
[i
] == COPY_IN
) {
1208 slang_variable
*p
= fun
->parameters
->variables
[i
];
1209 /* declare parameter 'p' */
1210 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1214 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1215 assert(decl
->locals
);
1216 decl
->locals
->outer_scope
= inlined
->locals
;
1217 decl
->a_id
= p
->a_name
;
1218 decl
->num_children
= 1;
1219 decl
->children
= slang_operation_new(1);
1221 /* child[0] is the var's initializer */
1222 slang_operation_copy(&decl
->children
[0], args
+ i
);
1224 /* add parameter 'p' to the local variable scope here */
1226 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1227 pCopy
->type
= p
->type
;
1228 pCopy
->a_name
= p
->a_name
;
1229 pCopy
->array_len
= p
->array_len
;
1232 newScope
= inlined
->locals
;
1237 /* Now add copies of the function's local vars to the new variable scope */
1238 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1239 slang_variable
*p
= fun
->parameters
->variables
[i
];
1240 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1241 pCopy
->type
= p
->type
;
1242 pCopy
->a_name
= p
->a_name
;
1243 pCopy
->array_len
= p
->array_len
;
1247 /* New epilog statements:
1248 * 1. Create end of function label to jump to from return statements.
1249 * 2. Copy the 'out' parameter vars
1252 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1254 inlined
->num_children
);
1255 lab
->type
= SLANG_OPER_LABEL
;
1256 lab
->label
= A
->curFuncEndLabel
;
1259 for (i
= 0; i
< totalArgs
; i
++) {
1260 if (paramMode
[i
] == COPY_OUT
) {
1261 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1262 /* actualCallVar = outParam */
1263 /*if (i > 0 || !haveRetValue)*/
1264 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1266 inlined
->num_children
);
1267 ass
->type
= SLANG_OPER_ASSIGN
;
1268 ass
->num_children
= 2;
1269 ass
->locals
->outer_scope
= inlined
->locals
;
1270 ass
->children
= slang_operation_new(2);
1271 ass
->children
[0] = args
[i
]; /*XXX copy */
1272 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1273 ass
->children
[1].a_id
= p
->a_name
;
1274 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1278 _slang_free(paramMode
);
1279 _slang_free(substOld
);
1280 _slang_free(substNew
);
1282 /* Update scoping to use the new local vars instead of the
1283 * original function's vars. This is especially important
1284 * for nested inlining.
1287 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1290 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1291 (char *) fun
->header
.a_name
,
1292 fun
->parameters
->num_variables
, numArgs
);
1293 slang_print_tree(top
, 0);
1297 A
->CurFunction
= prevFunction
;
1303 static slang_ir_node
*
1304 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1305 slang_operation
*oper
, slang_operation
*dest
)
1308 slang_operation
*inlined
;
1309 slang_label
*prevFuncEndLabel
;
1312 prevFuncEndLabel
= A
->curFuncEndLabel
;
1313 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1314 A
->curFuncEndLabel
= _slang_label_new(name
);
1315 assert(A
->curFuncEndLabel
);
1317 if (slang_is_asm_function(fun
) && !dest
) {
1318 /* assemble assembly function - tree style */
1319 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1322 /* non-assembly function */
1323 /* We always generate an "inline-able" block of code here.
1325 * 1. insert the inline code
1326 * 2. Generate a call to the "inline" code as a subroutine
1330 slang_operation
*ret
= NULL
;
1332 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1336 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1338 /* check if this is a "tail" return */
1339 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1340 _slang_is_tail_return(inlined
)) {
1341 /* The only RETURN is the last stmt in the function, no-op it
1342 * and inline the function body.
1344 ret
->type
= SLANG_OPER_NONE
;
1347 slang_operation
*callOper
;
1348 /* The function we're calling has one or more 'return' statements.
1349 * So, we can't truly inline this function because we need to
1350 * implement 'return' with RET (and CAL).
1351 * Nevertheless, we performed "inlining" to make a new instance
1352 * of the function body to deal with static register allocation.
1354 * XXX check if there's one 'return' and if it's the very last
1355 * statement in the function - we can optimize that case.
1357 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1358 inlined
->type
== SLANG_OPER_SEQUENCE
);
1360 if (_slang_function_has_return_value(fun
) && !dest
) {
1361 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1362 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1363 callOper
= &inlined
->children
[1];
1368 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1369 callOper
->fun
= fun
;
1370 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1378 /* Replace the function call with the inlined block (or new CALL stmt) */
1379 slang_operation_destruct(oper
);
1381 _slang_free(inlined
);
1384 assert(inlined
->locals
);
1385 printf("*** Inlined code for call to %s:\n",
1386 (char*) fun
->header
.a_name
);
1387 slang_print_tree(oper
, 10);
1391 n
= _slang_gen_operation(A
, oper
);
1393 /*_slang_label_delete(A->curFuncEndLabel);*/
1394 A
->curFuncEndLabel
= prevFuncEndLabel
;
1400 static slang_asm_info
*
1401 slang_find_asm_info(const char *name
)
1404 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1405 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1414 * Return the default swizzle mask for accessing a variable of the
1415 * given size (in floats). If size = 1, comp is used to identify
1416 * which component [0..3] of the register holds the variable.
1419 _slang_var_swizzle(GLint size
, GLint comp
)
1423 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1425 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1427 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1429 return SWIZZLE_XYZW
;
1435 * Some write-masked assignments are simple, but others are hard.
1438 * v.xy = vec2(a, b);
1441 * v.zy = vec2(a, b);
1442 * this gets transformed/swizzled into:
1443 * v.zy = vec2(a, b).*yx* (* = don't care)
1444 * This function helps to determine simple vs. non-simple.
1447 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1449 switch (writemask
) {
1451 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1453 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1455 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1457 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1459 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1460 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1462 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1463 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1464 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1465 case WRITEMASK_XYZW
:
1466 return swizzle
== SWIZZLE_NOOP
;
1474 * Convert the given swizzle into a writemask. In some cases this
1475 * is trivial, in other cases, we'll need to also swizzle the right
1476 * hand side to put components in the right places.
1477 * See comment above for more info.
1478 * XXX this function could be simplified and should probably be renamed.
1479 * \param swizzle the incoming swizzle
1480 * \param writemaskOut returns the writemask
1481 * \param swizzleOut swizzle to apply to the right-hand-side
1482 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1485 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1486 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1488 GLuint mask
= 0x0, newSwizzle
[4];
1491 /* make new dst writemask, compute size */
1492 for (i
= 0; i
< 4; i
++) {
1493 const GLuint swz
= GET_SWZ(swizzle
, i
);
1494 if (swz
== SWIZZLE_NIL
) {
1498 assert(swz
>= 0 && swz
<= 3);
1500 if (swizzle
!= SWIZZLE_XXXX
&&
1501 swizzle
!= SWIZZLE_YYYY
&&
1502 swizzle
!= SWIZZLE_ZZZZ
&&
1503 swizzle
!= SWIZZLE_WWWW
&&
1504 (mask
& (1 << swz
))) {
1505 /* a channel can't be specified twice (ex: ".xyyz") */
1506 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1507 _mesa_swizzle_string(swizzle
, 0, 0));
1513 assert(mask
<= 0xf);
1514 size
= i
; /* number of components in mask/swizzle */
1516 *writemaskOut
= mask
;
1518 /* make new src swizzle, by inversion */
1519 for (i
= 0; i
< 4; i
++) {
1520 newSwizzle
[i
] = i
; /*identity*/
1522 for (i
= 0; i
< size
; i
++) {
1523 const GLuint swz
= GET_SWZ(swizzle
, i
);
1524 newSwizzle
[swz
] = i
;
1526 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1531 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1533 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1535 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1537 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1539 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1548 * Recursively traverse 'oper' to produce a swizzle mask in the event
1549 * of any vector subscripts and swizzle suffixes.
1550 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1553 resolve_swizzle(const slang_operation
*oper
)
1555 if (oper
->type
== SLANG_OPER_FIELD
) {
1556 /* writemask from .xyzw suffix */
1558 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1559 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1563 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1564 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1568 return SWIZZLE_XYZW
;
1570 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1571 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1572 /* writemask from [index] */
1573 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1574 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1579 swizzle
= SWIZZLE_XXXX
;
1582 swizzle
= SWIZZLE_YYYY
;
1585 swizzle
= SWIZZLE_ZZZZ
;
1588 swizzle
= SWIZZLE_WWWW
;
1591 swizzle
= SWIZZLE_XYZW
;
1593 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1597 return SWIZZLE_XYZW
;
1603 * Recursively descend through swizzle nodes to find the node's storage info.
1605 static slang_ir_storage
*
1606 get_store(const slang_ir_node
*n
)
1608 if (n
->Opcode
== IR_SWIZZLE
) {
1609 return get_store(n
->Children
[0]);
1617 * Generate IR tree for an asm instruction/operation such as:
1618 * __asm vec4_dot __retVal.x, v1, v2;
1620 static slang_ir_node
*
1621 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1622 slang_operation
*dest
)
1624 const slang_asm_info
*info
;
1625 slang_ir_node
*kids
[3], *n
;
1626 GLuint j
, firstOperand
;
1628 assert(oper
->type
== SLANG_OPER_ASM
);
1630 info
= slang_find_asm_info((char *) oper
->a_id
);
1632 _mesa_problem(NULL
, "undefined __asm function %s\n",
1633 (char *) oper
->a_id
);
1636 assert(info
->NumParams
<= 3);
1638 if (info
->NumParams
== oper
->num_children
) {
1639 /* Storage for result is not specified.
1640 * Children[0], [1], [2] are the operands.
1645 /* Storage for result (child[0]) is specified.
1646 * Children[1], [2], [3] are the operands.
1651 /* assemble child(ren) */
1652 kids
[0] = kids
[1] = kids
[2] = NULL
;
1653 for (j
= 0; j
< info
->NumParams
; j
++) {
1654 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1659 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1662 /* Setup n->Store to be a particular location. Otherwise, storage
1663 * for the result (a temporary) will be allocated later.
1665 slang_operation
*dest_oper
;
1668 dest_oper
= &oper
->children
[0];
1670 n0
= _slang_gen_operation(A
, dest_oper
);
1675 n
->Store
= n0
->Store
;
1677 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1687 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1690 for (i
= 0; i
< scope
->num_functions
; i
++) {
1691 slang_function
*f
= &scope
->functions
[i
];
1692 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1693 printf(" %s (%d args)\n", name
, f
->param_count
);
1696 if (scope
->outer_scope
)
1697 print_funcs(scope
->outer_scope
, name
);
1702 * Find a function of the given name, taking 'numArgs' arguments.
1703 * This is the function we'll try to call when there is no exact match
1704 * between function parameters and call arguments.
1706 * XXX we should really create a list of candidate functions and try
1709 static slang_function
*
1710 _slang_find_function_by_argc(slang_function_scope
*scope
,
1711 const char *name
, int numArgs
)
1715 for (i
= 0; i
< scope
->num_functions
; i
++) {
1716 slang_function
*f
= &scope
->functions
[i
];
1717 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1718 int haveRetValue
= _slang_function_has_return_value(f
);
1719 if (numArgs
== f
->param_count
- haveRetValue
)
1723 scope
= scope
->outer_scope
;
1730 static slang_function
*
1731 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1734 slang_function
*maxFunc
= NULL
;
1739 for (i
= 0; i
< scope
->num_functions
; i
++) {
1740 slang_function
*f
= &scope
->functions
[i
];
1741 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1742 if (f
->param_count
> maxArgs
) {
1743 maxArgs
= f
->param_count
;
1748 scope
= scope
->outer_scope
;
1756 * Generate a new slang_function which is a constructor for a user-defined
1759 static slang_function
*
1760 _slang_make_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1762 const GLint numFields
= str
->fields
->num_variables
;
1764 slang_function
*fun
= (slang_function
*) _mesa_malloc(sizeof(slang_function
));
1768 slang_function_construct(fun
);
1770 /* function header (name, return type) */
1771 fun
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1772 fun
->header
.a_name
= str
->a_name
;
1773 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1774 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1775 fun
->header
.type
.specifier
._struct
= str
;
1777 /* function parameters (= struct's fields) */
1780 for (i
= 0; i
< numFields
; i
++) {
1782 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1784 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1785 *p
= *str
->fields
->variables
[i
]; /* copy the type */
1786 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1788 fun
->param_count
= fun
->parameters
->num_variables
;
1791 /* Add __retVal to params */
1793 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1794 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1796 p
->a_name
= a_retVal
;
1797 p
->type
= fun
->header
.type
;
1798 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1802 /* function body is:
1812 slang_variable_scope
*scope
;
1813 slang_variable
*var
;
1816 fun
->body
= slang_operation_new(1);
1817 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1818 fun
->body
->num_children
= numFields
+ 2;
1819 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1821 scope
= fun
->body
->locals
;
1822 scope
->outer_scope
= fun
->parameters
;
1824 /* create local var 't' */
1825 var
= slang_variable_scope_grow(scope
);
1826 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1827 var
->type
= fun
->header
.type
;
1831 slang_operation
*decl
;
1833 decl
= &fun
->body
->children
[0];
1834 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1835 decl
->locals
= _slang_variable_scope_new(scope
);
1836 decl
->a_id
= var
->a_name
;
1839 /* assign params to fields of t */
1840 for (i
= 0; i
< numFields
; i
++) {
1841 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1843 assign
->type
= SLANG_OPER_ASSIGN
;
1844 assign
->locals
= _slang_variable_scope_new(scope
);
1845 assign
->num_children
= 2;
1846 assign
->children
= slang_operation_new(2);
1849 slang_operation
*lhs
= &assign
->children
[0];
1851 lhs
->type
= SLANG_OPER_FIELD
;
1852 lhs
->locals
= _slang_variable_scope_new(scope
);
1853 lhs
->num_children
= 1;
1854 lhs
->children
= slang_operation_new(1);
1855 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1857 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1858 lhs
->children
[0].a_id
= var
->a_name
;
1859 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1862 lhs
->children
[1].num_children
= 1;
1863 lhs
->children
[1].children
= slang_operation_new(1);
1864 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1865 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1866 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1871 slang_operation
*rhs
= &assign
->children
[1];
1873 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1874 rhs
->locals
= _slang_variable_scope_new(scope
);
1875 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1881 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1883 ret
->type
= SLANG_OPER_RETURN
;
1884 ret
->locals
= _slang_variable_scope_new(scope
);
1885 ret
->num_children
= 1;
1886 ret
->children
= slang_operation_new(1);
1887 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1888 ret
->children
[0].a_id
= var
->a_name
;
1889 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1894 slang_print_function(fun, 1);
1901 * Find/create a function (constructor) for the given structure name.
1903 static slang_function
*
1904 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1907 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1908 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1909 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1910 /* found a structure type that matches the function name */
1911 if (!str
->constructor
) {
1912 /* create the constructor function now */
1913 str
->constructor
= _slang_make_constructor(A
, str
);
1915 return str
->constructor
;
1924 _slang_is_vec_mat_type(const char *name
)
1926 static const char *vecmat_types
[] = {
1927 "float", "int", "bool",
1928 "vec2", "vec3", "vec4",
1929 "ivec2", "ivec3", "ivec4",
1930 "bvec2", "bvec3", "bvec4",
1931 "mat2", "mat3", "mat4",
1932 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
1936 for (i
= 0; vecmat_types
[i
]; i
++)
1937 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
1944 * Assemble a function call, given a particular function name.
1945 * \param name the function's name (operators like '*' are possible).
1947 static slang_ir_node
*
1948 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1949 slang_operation
*oper
, slang_operation
*dest
)
1951 slang_operation
*params
= oper
->children
;
1952 const GLuint param_count
= oper
->num_children
;
1954 slang_function
*fun
;
1958 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1959 if (atom
== SLANG_ATOM_NULL
)
1963 * First, try to find function by name and exact argument type matching.
1965 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1966 &A
->space
, A
->atoms
, A
->log
, &error
);
1969 slang_info_log_error(A
->log
,
1970 "Function '%s' not found (check argument types)",
1976 /* Next, try locating a constructor function for a user-defined type */
1977 fun
= _slang_locate_struct_constructor(A
, name
);
1981 * At this point, some heuristics are used to try to find a function
1982 * that matches the calling signature by means of casting or "unrolling"
1986 if (!fun
&& _slang_is_vec_mat_type(name
)) {
1987 /* Next, if this call looks like a vec() or mat() constructor call,
1988 * try "unwinding" the args to satisfy a constructor.
1990 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
1992 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1993 slang_info_log_error(A
->log
,
1994 "Function '%s' not found (check argument types)",
2001 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2002 /* Next, try casting args to the types of the formal parameters */
2003 int numArgs
= oper
->num_children
;
2004 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2005 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2006 slang_info_log_error(A
->log
,
2007 "Function '%s' not found (check argument types)",
2015 slang_info_log_error(A
->log
,
2016 "Function '%s' not found (check argument types)",
2021 slang_info_log_error(A
->log
,
2022 "Function '%s' prototyped but not defined. "
2023 "Separate compilation units not supported.",
2028 /* type checking to be sure function's return type matches 'dest' type */
2032 slang_typeinfo_construct(&t0
);
2033 _slang_typeof_operation(A
, dest
, &t0
);
2035 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2036 slang_info_log_error(A
->log
,
2037 "Incompatible type returned by call to '%s'",
2043 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2045 if (n
&& !n
->Store
&& !dest
2046 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2047 /* setup n->Store for the result of the function call */
2048 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2049 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2050 /*printf("Alloc storage for function result, size %d \n", size);*/
2057 static slang_ir_node
*
2058 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2060 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2062 slang_variable
*var
;
2064 /* NOTE: In GLSL 1.20, there's only one kind of method
2065 * call: array.length(). Anything else is an error.
2067 if (oper
->a_id
!= a_length
) {
2068 slang_info_log_error(A
->log
,
2069 "Undefined method call '%s'", (char *) oper
->a_id
);
2073 /* length() takes no arguments */
2074 if (oper
->num_children
> 0) {
2075 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2079 /* lookup the object/variable */
2080 var
= _slang_locate_variable(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2081 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2082 slang_info_log_error(A
->log
,
2083 "Undefined object '%s'", (char *) oper
->a_obj
);
2087 /* Create a float/literal IR node encoding the array length */
2088 n
= new_node0(IR_FLOAT
);
2090 n
->Value
[0] = (float) var
->array_len
;
2091 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2098 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2100 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2101 oper
->type
== SLANG_OPER_LITERAL_INT
||
2102 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2103 if (oper
->literal
[0])
2109 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2110 oper
->num_children
== 1) {
2111 return _slang_is_constant_cond(&oper
->children
[0], value
);
2118 * Test if an operation is a scalar or boolean.
2121 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2123 slang_typeinfo type
;
2126 slang_typeinfo_construct(&type
);
2127 _slang_typeof_operation(A
, oper
, &type
);
2128 size
= _slang_sizeof_type_specifier(&type
.spec
);
2129 slang_typeinfo_destruct(&type
);
2135 * Test if an operation is boolean.
2138 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2140 slang_typeinfo type
;
2143 slang_typeinfo_construct(&type
);
2144 _slang_typeof_operation(A
, oper
, &type
);
2145 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2146 slang_typeinfo_destruct(&type
);
2152 * Generate loop code using high-level IR_LOOP instruction
2154 static slang_ir_node
*
2155 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2159 * BREAK if !expr (child[0])
2160 * body code (child[1])
2162 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2163 GLboolean isConst
, constTrue
;
2165 /* type-check expression */
2166 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2167 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2171 /* Check if loop condition is a constant */
2172 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2174 if (isConst
&& !constTrue
) {
2175 /* loop is never executed! */
2176 return new_node0(IR_NOP
);
2179 loop
= new_loop(NULL
);
2181 /* save old, push new loop */
2182 prevLoop
= A
->CurLoop
;
2185 if (isConst
&& constTrue
) {
2186 /* while(nonzero constant), no conditional break */
2191 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2192 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2194 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2195 loop
->Children
[0] = new_seq(breakIf
, body
);
2197 /* Do infinite loop detection */
2198 /* loop->List is head of linked list of break/continue nodes */
2199 if (!loop
->List
&& isConst
&& constTrue
) {
2200 /* infinite loop detected */
2201 A
->CurLoop
= prevLoop
; /* clean-up */
2202 slang_info_log_error(A
->log
, "Infinite loop detected!");
2206 /* pop loop, restore prev */
2207 A
->CurLoop
= prevLoop
;
2214 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2216 static slang_ir_node
*
2217 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2221 * body code (child[0])
2223 * BREAK if !expr (child[1])
2225 slang_ir_node
*prevLoop
, *loop
;
2226 GLboolean isConst
, constTrue
;
2228 /* type-check expression */
2229 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2230 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2234 loop
= new_loop(NULL
);
2236 /* save old, push new loop */
2237 prevLoop
= A
->CurLoop
;
2241 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2243 /* Check if loop condition is a constant */
2244 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2245 if (isConst
&& constTrue
) {
2246 /* do { } while(1) ==> no conditional break */
2247 loop
->Children
[1] = NULL
; /* no tail code */
2251 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2252 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2255 /* XXX we should do infinite loop detection, as above */
2257 /* pop loop, restore prev */
2258 A
->CurLoop
= prevLoop
;
2265 * Generate for-loop using high-level IR_LOOP instruction.
2267 static slang_ir_node
*
2268 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2271 * init code (child[0])
2273 * BREAK if !expr (child[1])
2274 * body code (child[3])
2276 * incr code (child[2]) // XXX continue here
2278 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2280 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2281 loop
= new_loop(NULL
);
2283 /* save old, push new loop */
2284 prevLoop
= A
->CurLoop
;
2287 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2288 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2289 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2290 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2292 loop
->Children
[0] = new_seq(breakIf
, body
);
2293 loop
->Children
[1] = incr
; /* tail code */
2295 /* pop loop, restore prev */
2296 A
->CurLoop
= prevLoop
;
2298 return new_seq(init
, loop
);
2302 static slang_ir_node
*
2303 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2305 slang_ir_node
*n
, *loopNode
;
2306 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2307 loopNode
= A
->CurLoop
;
2309 assert(loopNode
->Opcode
== IR_LOOP
);
2310 n
= new_node0(IR_CONT
);
2312 n
->Parent
= loopNode
;
2313 /* insert this node at head of linked list */
2314 n
->List
= loopNode
->List
;
2322 * Determine if the given operation is of a specific type.
2325 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2327 if (oper
->type
== type
)
2329 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2330 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2331 oper
->num_children
== 1)
2332 return is_operation_type(&oper
->children
[0], type
);
2339 * Generate IR tree for an if/then/else conditional using high-level
2340 * IR_IF instruction.
2342 static slang_ir_node
*
2343 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2346 * eval expr (child[0])
2353 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2354 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2355 GLboolean isConst
, constTrue
;
2357 /* type-check expression */
2358 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2359 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2363 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2364 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2368 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2372 return _slang_gen_operation(A
, &oper
->children
[1]);
2375 /* if (false) ... */
2376 return _slang_gen_operation(A
, &oper
->children
[2]);
2380 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2381 cond
= new_cond(cond
);
2383 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2384 && !haveElseClause
) {
2385 /* Special case: generate a conditional break */
2386 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2389 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2390 && !haveElseClause
) {
2391 /* Special case: generate a conditional break */
2392 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2397 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2399 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2402 ifNode
= new_if(cond
, ifBody
, elseBody
);
2409 static slang_ir_node
*
2410 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2414 assert(oper
->type
== SLANG_OPER_NOT
);
2416 /* type-check expression */
2417 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2418 slang_info_log_error(A
->log
,
2419 "scalar/boolean expression expected for '!'");
2423 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2431 static slang_ir_node
*
2432 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2434 slang_ir_node
*n1
, *n2
;
2436 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2438 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2439 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2440 slang_info_log_error(A
->log
,
2441 "scalar/boolean expressions expected for '^^'");
2445 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2448 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2451 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2456 * Generate IR node for storage of a temporary of given size.
2458 static slang_ir_node
*
2459 _slang_gen_temporary(GLint size
)
2461 slang_ir_storage
*store
;
2462 slang_ir_node
*n
= NULL
;
2464 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2466 n
= new_node0(IR_VAR_DECL
);
2479 * Generate IR node for allocating/declaring a variable.
2481 static slang_ir_node
*
2482 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
2486 /*assert(!var->declared);*/
2487 var
->declared
= GL_TRUE
;
2489 n
= new_node0(IR_VAR_DECL
);
2491 _slang_attach_storage(n
, var
);
2493 assert(n
->Store
== var
->store
);
2495 assert(n
->Store
->Index
< 0);
2497 if (is_sampler_type(&var
->type
)) {
2498 n
->Store
->File
= PROGRAM_SAMPLER
;
2501 n
->Store
->File
= PROGRAM_TEMPORARY
;
2504 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
2506 if (n
->Store
->Size
<= 0) {
2507 slang_info_log_error(A
->log
, "invalid declaration for '%s'",
2508 (char*) var
->a_name
);
2512 printf("%s var %p %s store=%p index=%d size=%d\n",
2513 __FUNCTION__
, (void *) var
, (char *) var
->a_name
,
2514 (void *) n
->Store
, n
->Store
->Index
, n
->Store
->Size
);
2517 if (var
->array_len
> 0) {
2518 /* this is an array */
2519 /* cannot be const-qualified */
2520 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2521 slang_info_log_error(A
->log
, "array '%s' cannot be const",
2522 (char*) var
->a_name
);
2526 /* round up element size to mult of 4 */
2527 GLint sz
= (n
->Store
->Size
+ 3) & ~3;
2528 /* mult by array size */
2529 sz
*= var
->array_len
;
2530 n
->Store
->Size
= sz
;
2534 assert(n
->Store
->Size
> 0);
2536 /* setup default swizzle for storing the variable */
2537 switch (n
->Store
->Size
) {
2539 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2540 SWIZZLE_NIL
, SWIZZLE_NIL
);
2543 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2544 SWIZZLE_Z
, SWIZZLE_NIL
);
2547 /* Note that float-sized vars may be allocated in any x/y/z/w
2548 * slot, but that won't be determined until code emit time.
2550 n
->Store
->Swizzle
= SWIZZLE_NOOP
;
2558 * Generate code for a selection expression: b ? x : y
2559 * XXX In some cases we could implement a selection expression
2560 * with an LRP instruction (use the boolean as the interpolant).
2561 * Otherwise, we use an IF/ELSE/ENDIF construct.
2563 static slang_ir_node
*
2564 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2566 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2567 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2568 slang_typeinfo type0
, type1
, type2
;
2569 int size
, isBool
, isEqual
;
2571 assert(oper
->type
== SLANG_OPER_SELECT
);
2572 assert(oper
->num_children
== 3);
2574 /* type of children[0] must be boolean */
2575 slang_typeinfo_construct(&type0
);
2576 _slang_typeof_operation(A
, &oper
->children
[0], &type0
);
2577 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2578 slang_typeinfo_destruct(&type0
);
2580 slang_info_log_error(A
->log
, "selector type is not boolean");
2584 slang_typeinfo_construct(&type1
);
2585 slang_typeinfo_construct(&type2
);
2586 _slang_typeof_operation(A
, &oper
->children
[1], &type1
);
2587 _slang_typeof_operation(A
, &oper
->children
[2], &type2
);
2588 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2589 slang_typeinfo_destruct(&type1
);
2590 slang_typeinfo_destruct(&type2
);
2592 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2596 /* size of x or y's type */
2597 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2601 tmpDecl
= _slang_gen_temporary(size
);
2603 /* the condition (child 0) */
2604 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2605 cond
= new_cond(cond
);
2607 /* if-true body (child 1) */
2608 tmpVar
= new_node0(IR_VAR
);
2609 tmpVar
->Store
= tmpDecl
->Store
;
2610 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2611 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2613 /* if-false body (child 2) */
2614 tmpVar
= new_node0(IR_VAR
);
2615 tmpVar
->Store
= tmpDecl
->Store
;
2616 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2617 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2619 ifNode
= new_if(cond
, trueNode
, falseNode
);
2622 tmpVar
= new_node0(IR_VAR
);
2623 tmpVar
->Store
= tmpDecl
->Store
;
2625 tree
= new_seq(ifNode
, tmpVar
);
2626 tree
= new_seq(tmpDecl
, tree
);
2628 /*_slang_print_ir_tree(tree, 10);*/
2634 * Generate code for &&.
2636 static slang_ir_node
*
2637 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2639 /* rewrite "a && b" as "a ? b : false" */
2640 slang_operation
*select
;
2643 select
= slang_operation_new(1);
2644 select
->type
= SLANG_OPER_SELECT
;
2645 select
->num_children
= 3;
2646 select
->children
= slang_operation_new(3);
2648 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2649 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2650 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2651 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2652 select
->children
[2].literal_size
= 1;
2654 n
= _slang_gen_select(A
, select
);
2660 * Generate code for ||.
2662 static slang_ir_node
*
2663 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2665 /* rewrite "a || b" as "a ? true : b" */
2666 slang_operation
*select
;
2669 select
= slang_operation_new(1);
2670 select
->type
= SLANG_OPER_SELECT
;
2671 select
->num_children
= 3;
2672 select
->children
= slang_operation_new(3);
2674 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2675 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
2676 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
2677 select
->children
[1].literal_size
= 1;
2678 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
2680 n
= _slang_gen_select(A
, select
);
2686 * Generate IR tree for a return statement.
2688 static slang_ir_node
*
2689 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
2691 const GLboolean haveReturnValue
2692 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
2694 /* error checking */
2695 assert(A
->CurFunction
);
2696 if (haveReturnValue
&&
2697 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
2698 slang_info_log_error(A
->log
, "illegal return expression");
2701 else if (!haveReturnValue
&&
2702 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2703 slang_info_log_error(A
->log
, "return statement requires an expression");
2707 if (!haveReturnValue
) {
2708 return new_return(A
->curFuncEndLabel
);
2716 * return; // goto __endOfFunction
2718 slang_operation
*assign
;
2719 slang_atom a_retVal
;
2722 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2728 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
2730 /* trying to return a value in a void-valued function */
2736 assign
= slang_operation_new(1);
2737 assign
->type
= SLANG_OPER_ASSIGN
;
2738 assign
->num_children
= 2;
2739 assign
->children
= slang_operation_new(2);
2740 /* lhs (__retVal) */
2741 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2742 assign
->children
[0].a_id
= a_retVal
;
2743 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2745 /* XXX we might be able to avoid this copy someday */
2746 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2748 /* assemble the new code */
2749 n
= new_seq(_slang_gen_operation(A
, assign
),
2750 new_return(A
->curFuncEndLabel
));
2752 slang_operation_delete(assign
);
2759 * Determine if the given operation/expression is const-valued.
2762 _slang_is_constant_expr(const slang_operation
*oper
)
2764 slang_variable
*var
;
2767 switch (oper
->type
) {
2768 case SLANG_OPER_IDENTIFIER
:
2769 var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2770 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
2774 for (i
= 0; i
< oper
->num_children
; i
++) {
2775 if (!_slang_is_constant_expr(&oper
->children
[i
]))
2784 * Check if an assignment of type t1 to t0 is legal.
2785 * XXX more cases needed.
2788 _slang_assignment_compatible(slang_assemble_ctx
*A
,
2789 slang_operation
*op0
,
2790 slang_operation
*op1
)
2792 slang_typeinfo t0
, t1
;
2795 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
2796 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
2800 slang_typeinfo_construct(&t0
);
2801 _slang_typeof_operation(A
, op0
, &t0
);
2803 slang_typeinfo_construct(&t1
);
2804 _slang_typeof_operation(A
, op1
, &t1
);
2806 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
2807 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
2811 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
2816 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
2817 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
2818 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
2821 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
2822 t1
.spec
.type
== SLANG_SPEC_BOOL
)
2825 #if 0 /* not used just yet - causes problems elsewhere */
2826 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
2827 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
2831 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
2832 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
2835 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
2836 t1
.spec
.type
== SLANG_SPEC_INT
)
2845 * Generate IR tree for a variable declaration.
2847 static slang_ir_node
*
2848 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
2851 slang_ir_node
*varDecl
;
2853 const char *varName
= (char *) oper
->a_id
;
2854 slang_operation
*initializer
;
2856 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
2857 assert(oper
->num_children
<= 1);
2859 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2861 return NULL
; /* "shouldn't happen" */
2863 if (v
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
2864 v
->type
.qualifier
== SLANG_QUAL_VARYING
||
2865 v
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2866 /* can't declare attribute/uniform vars inside functions */
2867 slang_info_log_error(A
->log
,
2868 "local variable '%s' cannot be an attribute/uniform/varying",
2875 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
2880 varDecl
= _slang_gen_var_decl(A
, v
);
2884 /* check if the var has an initializer */
2885 if (oper
->num_children
> 0) {
2886 assert(oper
->num_children
== 1);
2887 initializer
= &oper
->children
[0];
2889 else if (v
->initializer
) {
2890 initializer
= v
->initializer
;
2896 if (v
->type
.qualifier
== SLANG_QUAL_CONST
&& !initializer
) {
2897 slang_info_log_error(A
->log
,
2898 "const-qualified variable '%s' requires initializer",
2905 slang_ir_node
*var
, *init
;
2907 /* type check/compare var and initializer */
2908 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
2909 slang_info_log_error(A
->log
, "incompatible types in assignment");
2913 var
= new_var(A
, oper
, oper
->a_id
);
2915 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2919 if (v
->type
.qualifier
== SLANG_QUAL_CONST
) {
2920 /* if the variable is const, the initializer must be a const
2921 * expression as well.
2924 if (!_slang_is_constant_expr(initializer
)) {
2925 slang_info_log_error(A
->log
,
2926 "initializer for %s not constant", varName
);
2932 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2934 init
= _slang_gen_operation(A
, initializer
);
2938 /*assert(init->Store);*/
2940 /* XXX remove this when type checking is added above */
2941 if (init
->Store
&& var
->Store
->Size
!= init
->Store
->Size
) {
2942 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2946 n
= new_node2(IR_COPY
, var
, init
);
2947 n
= new_seq(varDecl
, n
);
2958 * Generate IR tree for a variable (such as in an expression).
2960 static slang_ir_node
*
2961 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
2963 /* If there's a variable associated with this oper (from inlining)
2964 * use it. Otherwise, use the oper's var id.
2966 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
2967 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
2969 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
2978 * Return the number of components actually named by the swizzle.
2979 * Recall that swizzles may have undefined/don't-care values.
2982 swizzle_size(GLuint swizzle
)
2985 for (i
= 0; i
< 4; i
++) {
2986 GLuint swz
= GET_SWZ(swizzle
, i
);
2987 size
+= (swz
>= 0 && swz
<= 3);
2993 static slang_ir_node
*
2994 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2996 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3000 n
->Store
= _slang_new_ir_storage_relative(0,
3001 swizzle_size(swizzle
),
3003 n
->Store
->Swizzle
= swizzle
;
3010 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3012 while (store
->Parent
)
3013 store
= store
->Parent
;
3015 if (!(store
->File
== PROGRAM_OUTPUT
||
3016 store
->File
== PROGRAM_TEMPORARY
||
3017 (store
->File
== PROGRAM_VARYING
&&
3018 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3028 * Generate IR tree for an assignment (=).
3030 static slang_ir_node
*
3031 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3033 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3034 /* Check that var is writeable */
3036 = _slang_locate_variable(oper
->children
[0].locals
,
3037 oper
->children
[0].a_id
, GL_TRUE
);
3039 slang_info_log_error(A
->log
, "undefined variable '%s'",
3040 (char *) oper
->children
[0].a_id
);
3043 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3044 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3045 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3046 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3047 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3048 slang_info_log_error(A
->log
,
3049 "illegal assignment to read-only variable '%s'",
3050 (char *) oper
->children
[0].a_id
);
3055 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3056 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3057 /* Special case of: x = f(a, b)
3058 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3060 * XXX this could be even more effective if we could accomodate
3061 * cases such as "v.x = f();" - would help with typical vertex
3065 n
= _slang_gen_function_call_name(A
,
3066 (const char *) oper
->children
[1].a_id
,
3067 &oper
->children
[1], &oper
->children
[0]);
3071 slang_ir_node
*n
, *lhs
, *rhs
;
3073 /* lhs and rhs type checking */
3074 if (!_slang_assignment_compatible(A
,
3076 &oper
->children
[1])) {
3077 slang_info_log_error(A
->log
, "incompatible types in assignment");
3081 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3087 slang_info_log_error(A
->log
,
3088 "invalid left hand side for assignment");
3092 /* check that lhs is writable */
3093 if (!is_store_writable(A
, lhs
->Store
)) {
3094 slang_info_log_error(A
->log
,
3095 "illegal assignment to read-only l-value");
3099 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3101 /* convert lhs swizzle into writemask */
3102 GLuint writemask
, newSwizzle
;
3103 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3104 &writemask
, &newSwizzle
)) {
3105 /* Non-simple writemask, need to swizzle right hand side in
3106 * order to put components into the right place.
3108 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3110 n
= new_node2(IR_COPY
, lhs
, rhs
);
3121 * Generate IR tree for referencing a field in a struct (or basic vector type)
3123 static slang_ir_node
*
3124 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3128 /* type of struct */
3129 slang_typeinfo_construct(&ti
);
3130 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
3132 if (_slang_type_is_vector(ti
.spec
.type
)) {
3133 /* the field should be a swizzle */
3134 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3138 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3139 slang_info_log_error(A
->log
, "Bad swizzle");
3142 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3147 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3148 /* create new parent node with swizzle */
3150 n
= _slang_gen_swizzle(n
, swizzle
);
3153 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3154 || ti
.spec
.type
== SLANG_SPEC_INT
3155 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3156 const GLuint rows
= 1;
3160 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3161 slang_info_log_error(A
->log
, "Bad swizzle");
3163 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3167 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3168 /* create new parent node with swizzle */
3169 n
= _slang_gen_swizzle(n
, swizzle
);
3173 /* the field is a structure member (base.field) */
3174 /* oper->children[0] is the base */
3175 /* oper->a_id is the field name */
3176 slang_ir_node
*base
, *n
;
3177 slang_typeinfo field_ti
;
3178 GLint fieldSize
, fieldOffset
= -1;
3181 slang_typeinfo_construct(&field_ti
);
3182 _slang_typeof_operation(A
, oper
, &field_ti
);
3184 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3186 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3188 if (fieldSize
== 0 || fieldOffset
< 0) {
3189 const char *structName
;
3190 if (ti
.spec
._struct
)
3191 structName
= (char *) ti
.spec
._struct
->a_name
;
3193 structName
= "unknown";
3194 slang_info_log_error(A
->log
,
3195 "\"%s\" is not a member of struct \"%s\"",
3196 (char *) oper
->a_id
, structName
);
3199 assert(fieldSize
>= 0);
3201 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3203 /* error msg should have already been logged */
3207 n
= new_node1(IR_FIELD
, base
);
3211 n
->Field
= (char *) oper
->a_id
;
3213 /* Store the field's offset in storage->Index */
3214 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3224 * Gen code for array indexing.
3226 static slang_ir_node
*
3227 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3229 slang_typeinfo array_ti
;
3231 /* get array's type info */
3232 slang_typeinfo_construct(&array_ti
);
3233 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
3235 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3236 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3237 /* translate the index into a swizzle/writemask: "v.x=p" */
3238 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3242 index
= (GLint
) oper
->children
[1].literal
[0];
3243 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3244 index
>= (GLint
) max
) {
3245 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3249 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3251 /* use swizzle to access the element */
3252 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3256 n
= _slang_gen_swizzle(n
, swizzle
);
3262 /* conventional array */
3263 slang_typeinfo elem_ti
;
3264 slang_ir_node
*elem
, *array
, *index
;
3265 GLint elemSize
, arrayLen
;
3267 /* size of array element */
3268 slang_typeinfo_construct(&elem_ti
);
3269 _slang_typeof_operation(A
, oper
, &elem_ti
);
3270 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3272 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3273 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3275 arrayLen
= array_ti
.array_len
;
3277 slang_typeinfo_destruct(&array_ti
);
3278 slang_typeinfo_destruct(&elem_ti
);
3280 if (elemSize
<= 0) {
3281 /* unknown var or type */
3282 slang_info_log_error(A
->log
, "Undefined variable or type");
3286 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3287 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3288 if (array
&& index
) {
3290 GLint constIndex
= -1;
3291 if (index
->Opcode
== IR_FLOAT
) {
3292 constIndex
= (int) index
->Value
[0];
3293 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3294 slang_info_log_error(A
->log
,
3295 "Array index out of bounds (index=%d size=%d)",
3296 constIndex
, arrayLen
);
3297 _slang_free_ir_tree(array
);
3298 _slang_free_ir_tree(index
);
3303 if (!array
->Store
) {
3304 slang_info_log_error(A
->log
, "Invalid array");
3308 elem
= new_node2(IR_ELEMENT
, array
, index
);
3310 /* The storage info here will be updated during code emit */
3311 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3312 array
->Store
->Index
,
3318 _slang_free_ir_tree(array
);
3319 _slang_free_ir_tree(index
);
3326 static slang_ir_node
*
3327 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3328 slang_ir_opcode opcode
)
3330 slang_typeinfo t0
, t1
;
3333 slang_typeinfo_construct(&t0
);
3334 _slang_typeof_operation(A
, &oper
->children
[0], &t0
);
3336 slang_typeinfo_construct(&t1
);
3337 _slang_typeof_operation(A
, &oper
->children
[0], &t1
);
3339 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3340 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3341 slang_info_log_error(A
->log
, "Illegal array comparison");
3345 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3346 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3347 /* <, <=, >, >= can only be used with scalars */
3348 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3349 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3350 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3351 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3352 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3357 n
= new_node2(opcode
,
3358 _slang_gen_operation(A
, &oper
->children
[0]),
3359 _slang_gen_operation(A
, &oper
->children
[1]));
3361 /* result is a bool (size 1) */
3362 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3370 print_vars(slang_variable_scope
*s
)
3374 for (i
= 0; i
< s
->num_variables
; i
++) {
3376 (char*) s
->variables
[i
]->a_name
,
3377 s
->variables
[i
]->declared
);
3387 _slang_undeclare_vars(slang_variable_scope
*locals
)
3389 if (locals
->num_variables
> 0) {
3391 for (i
= 0; i
< locals
->num_variables
; i
++) {
3392 slang_variable
*v
= locals
->variables
[i
];
3393 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3394 v
->declared
= GL_FALSE
;
3402 * Generate IR tree for a slang_operation (AST node)
3404 static slang_ir_node
*
3405 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3407 switch (oper
->type
) {
3408 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3412 _slang_push_var_table(A
->vartable
);
3414 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3415 n
= _slang_gen_operation(A
, oper
);
3416 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3418 _slang_pop_var_table(A
->vartable
);
3420 /*_slang_undeclare_vars(oper->locals);*/
3421 /*print_vars(oper->locals);*/
3424 n
= new_node1(IR_SCOPE
, n
);
3429 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3430 /* list of operations */
3431 if (oper
->num_children
> 0)
3433 slang_ir_node
*n
, *tree
= NULL
;
3436 for (i
= 0; i
< oper
->num_children
; i
++) {
3437 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3439 _slang_free_ir_tree(tree
);
3440 return NULL
; /* error must have occured */
3442 tree
= new_seq(tree
, n
);
3448 return new_node0(IR_NOP
);
3451 case SLANG_OPER_EXPRESSION
:
3452 return _slang_gen_operation(A
, &oper
->children
[0]);
3454 case SLANG_OPER_FOR
:
3455 return _slang_gen_for(A
, oper
);
3457 return _slang_gen_do(A
, oper
);
3458 case SLANG_OPER_WHILE
:
3459 return _slang_gen_while(A
, oper
);
3460 case SLANG_OPER_BREAK
:
3462 slang_info_log_error(A
->log
, "'break' not in loop");
3465 return new_break(A
->CurLoop
);
3466 case SLANG_OPER_CONTINUE
:
3468 slang_info_log_error(A
->log
, "'continue' not in loop");
3471 return _slang_gen_continue(A
, oper
);
3472 case SLANG_OPER_DISCARD
:
3473 return new_node0(IR_KILL
);
3475 case SLANG_OPER_EQUAL
:
3476 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3477 case SLANG_OPER_NOTEQUAL
:
3478 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3479 case SLANG_OPER_GREATER
:
3480 return _slang_gen_compare(A
, oper
, IR_SGT
);
3481 case SLANG_OPER_LESS
:
3482 return _slang_gen_compare(A
, oper
, IR_SLT
);
3483 case SLANG_OPER_GREATEREQUAL
:
3484 return _slang_gen_compare(A
, oper
, IR_SGE
);
3485 case SLANG_OPER_LESSEQUAL
:
3486 return _slang_gen_compare(A
, oper
, IR_SLE
);
3487 case SLANG_OPER_ADD
:
3490 assert(oper
->num_children
== 2);
3491 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3494 case SLANG_OPER_SUBTRACT
:
3497 assert(oper
->num_children
== 2);
3498 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3501 case SLANG_OPER_MULTIPLY
:
3504 assert(oper
->num_children
== 2);
3505 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3508 case SLANG_OPER_DIVIDE
:
3511 assert(oper
->num_children
== 2);
3512 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3515 case SLANG_OPER_MINUS
:
3518 assert(oper
->num_children
== 1);
3519 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3522 case SLANG_OPER_PLUS
:
3523 /* +expr --> do nothing */
3524 return _slang_gen_operation(A
, &oper
->children
[0]);
3525 case SLANG_OPER_VARIABLE_DECL
:
3526 return _slang_gen_declaration(A
, oper
);
3527 case SLANG_OPER_ASSIGN
:
3528 return _slang_gen_assignment(A
, oper
);
3529 case SLANG_OPER_ADDASSIGN
:
3532 assert(oper
->num_children
== 2);
3533 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3536 case SLANG_OPER_SUBASSIGN
:
3539 assert(oper
->num_children
== 2);
3540 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3544 case SLANG_OPER_MULASSIGN
:
3547 assert(oper
->num_children
== 2);
3548 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3551 case SLANG_OPER_DIVASSIGN
:
3554 assert(oper
->num_children
== 2);
3555 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3558 case SLANG_OPER_LOGICALAND
:
3561 assert(oper
->num_children
== 2);
3562 n
= _slang_gen_logical_and(A
, oper
);
3565 case SLANG_OPER_LOGICALOR
:
3568 assert(oper
->num_children
== 2);
3569 n
= _slang_gen_logical_or(A
, oper
);
3572 case SLANG_OPER_LOGICALXOR
:
3573 return _slang_gen_xor(A
, oper
);
3574 case SLANG_OPER_NOT
:
3575 return _slang_gen_not(A
, oper
);
3576 case SLANG_OPER_SELECT
: /* b ? x : y */
3579 assert(oper
->num_children
== 3);
3580 n
= _slang_gen_select(A
, oper
);
3584 case SLANG_OPER_ASM
:
3585 return _slang_gen_asm(A
, oper
, NULL
);
3586 case SLANG_OPER_CALL
:
3587 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3589 case SLANG_OPER_METHOD
:
3590 return _slang_gen_method_call(A
, oper
);
3591 case SLANG_OPER_RETURN
:
3592 return _slang_gen_return(A
, oper
);
3593 case SLANG_OPER_LABEL
:
3594 return new_label(oper
->label
);
3595 case SLANG_OPER_IDENTIFIER
:
3596 return _slang_gen_variable(A
, oper
);
3598 return _slang_gen_if(A
, oper
);
3599 case SLANG_OPER_FIELD
:
3600 return _slang_gen_struct_field(A
, oper
);
3601 case SLANG_OPER_SUBSCRIPT
:
3602 return _slang_gen_array_element(A
, oper
);
3603 case SLANG_OPER_LITERAL_FLOAT
:
3605 case SLANG_OPER_LITERAL_INT
:
3607 case SLANG_OPER_LITERAL_BOOL
:
3608 return new_float_literal(oper
->literal
, oper
->literal_size
);
3610 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3613 assert(oper
->num_children
== 1);
3614 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3617 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3620 assert(oper
->num_children
== 1);
3621 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3624 case SLANG_OPER_PREINCREMENT
: /* ++var */
3627 assert(oper
->num_children
== 1);
3628 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3631 case SLANG_OPER_PREDECREMENT
: /* --var */
3634 assert(oper
->num_children
== 1);
3635 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3639 case SLANG_OPER_NON_INLINED_CALL
:
3640 case SLANG_OPER_SEQUENCE
:
3642 slang_ir_node
*tree
= NULL
;
3644 for (i
= 0; i
< oper
->num_children
; i
++) {
3645 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3646 tree
= new_seq(tree
, n
);
3648 tree
->Store
= n
->Store
;
3650 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3651 tree
= new_function_call(tree
, oper
->label
);
3656 case SLANG_OPER_NONE
:
3657 case SLANG_OPER_VOID
:
3658 /* returning NULL here would generate an error */
3659 return new_node0(IR_NOP
);
3662 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3664 return new_node0(IR_NOP
);
3672 * Compute total size of array give size of element, number of elements.
3675 array_size(GLint baseSize
, GLint arrayLen
)
3679 /* round up base type to multiple of 4 */
3680 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3690 * Called by compiler when a global variable has been parsed/compiled.
3691 * Here we examine the variable's type to determine what kind of register
3692 * storage will be used.
3694 * A uniform such as "gl_Position" will become the register specification
3695 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3696 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3698 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3699 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3700 * actual texture unit (as specified by the user calling glUniform1i()).
3703 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3704 slang_unit_type type
)
3706 struct gl_program
*prog
= A
->program
;
3707 const char *varName
= (char *) var
->a_name
;
3708 GLboolean success
= GL_TRUE
;
3709 slang_ir_storage
*store
= NULL
;
3711 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3712 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3713 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3715 if (texIndex
!= -1) {
3716 /* This is a texture sampler variable...
3717 * store->File = PROGRAM_SAMPLER
3718 * store->Index = sampler number (0..7, typically)
3719 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
3721 if (var
->initializer
) {
3722 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
3725 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
3726 /* disallow rect samplers */
3727 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
3728 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
3729 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
3734 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
3735 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
3737 if (dbg
) printf("SAMPLER ");
3739 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3740 /* Uniform variable */
3741 const GLint totalSize
= array_size(size
, var
->array_len
);
3742 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
3745 /* user-defined uniform */
3746 if (datatype
== GL_NONE
) {
3747 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
3748 /* temporary work-around */
3749 GLenum datatype
= GL_FLOAT
;
3750 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3751 totalSize
, datatype
, NULL
);
3752 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3753 totalSize
, swizzle
);
3755 /* XXX what we need to do is unroll the struct into its
3756 * basic types, creating a uniform variable for each.
3764 * Should produce uniforms:
3765 * "f.a" (GL_FLOAT_VEC3)
3766 * "f.b" (GL_FLOAT_VEC4)
3769 if (var
->initializer
) {
3770 slang_info_log_error(A
->log
,
3771 "unsupported initializer for uniform '%s'", varName
);
3776 slang_info_log_error(A
->log
,
3777 "invalid datatype for uniform variable %s",
3784 const GLfloat
*initialValues
= NULL
;
3785 if (var
->initializer
) {
3786 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3787 if (var
->initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3788 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3789 /* simple float/vector initializer */
3790 initialValues
= var
->initializer
->literal
;
3793 /* complex initializer */
3794 slang_info_log_error(A
->log
,
3795 "unsupported initializer for uniform '%s'", varName
);
3800 uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3801 totalSize
, datatype
, initialValues
);
3802 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3803 totalSize
, swizzle
);
3807 /* pre-defined uniform, like gl_ModelviewMatrix */
3808 /* We know it's a uniform, but don't allocate storage unless
3811 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
3812 totalSize
, swizzle
);
3814 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
3816 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
3817 const GLint totalSize
= array_size(size
, var
->array_len
);
3819 /* varyings must be float, vec or mat */
3820 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
3821 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
3822 slang_info_log_error(A
->log
,
3823 "varying '%s' must be float/vector/matrix",
3828 if (var
->initializer
) {
3829 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
3835 /* user-defined varying */
3841 if (var
->type
.centroid
== SLANG_CENTROID
)
3842 flags
|= PROG_PARAM_BIT_CENTROID
;
3843 if (var
->type
.variant
== SLANG_INVARIANT
)
3844 flags
|= PROG_PARAM_BIT_INVARIANT
;
3846 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
3848 swizzle
= _slang_var_swizzle(size
, 0);
3849 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
3850 totalSize
, swizzle
);
3853 /* pre-defined varying, like gl_Color or gl_TexCoord */
3854 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
3855 /* fragment program input */
3857 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3860 assert(index
< FRAG_ATTRIB_MAX
);
3861 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
3865 /* vertex program output */
3866 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3867 GLuint swizzle
= _slang_var_swizzle(size
, 0);
3869 assert(index
< VERT_RESULT_MAX
);
3870 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
3871 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
3874 if (dbg
) printf("V/F ");
3876 if (dbg
) printf("VARYING ");
3878 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
3881 /* attributes must be float, vec or mat */
3882 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
3883 slang_info_log_error(A
->log
,
3884 "attribute '%s' must be float/vector/matrix",
3890 /* user-defined vertex attribute */
3891 const GLint attr
= -1; /* unknown */
3892 swizzle
= _slang_var_swizzle(size
, 0);
3893 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
3894 size
, datatype
, attr
);
3896 index
= VERT_ATTRIB_GENERIC0
+ index
;
3899 /* pre-defined vertex attrib */
3900 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
3903 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3904 if (dbg
) printf("ATTRIB ");
3906 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
3907 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
3908 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3910 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3911 if (dbg
) printf("INPUT ");
3913 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
3914 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
3915 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3916 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3919 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
3920 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
3921 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
3922 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
3924 if (dbg
) printf("OUTPUT ");
3926 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
3927 /* pre-defined global constant, like gl_MaxLights */
3928 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
3929 if (dbg
) printf("CONST ");
3932 /* ordinary variable (may be const) */
3935 /* IR node to declare the variable */
3936 n
= _slang_gen_var_decl(A
, var
);
3938 /* IR code for the var's initializer, if present */
3939 if (var
->initializer
) {
3940 slang_ir_node
*lhs
, *rhs
, *init
;
3942 /* Generate IR_COPY instruction to initialize the variable */
3943 lhs
= new_node0(IR_VAR
);
3945 lhs
->Store
= n
->Store
;
3947 /* constant folding, etc */
3948 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3950 rhs
= _slang_gen_operation(A
, var
->initializer
);
3952 init
= new_node2(IR_COPY
, lhs
, rhs
);
3953 n
= new_seq(n
, init
);
3956 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
3958 _slang_free_ir_tree(n
);
3961 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
3962 store
? store
->Index
: -2);
3965 var
->store
= store
; /* save var's storage info */
3967 var
->declared
= GL_TRUE
;
3974 * Produce an IR tree from a function AST (fun->body).
3975 * Then call the code emitter to convert the IR tree into gl_program
3979 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
3982 GLboolean success
= GL_TRUE
;
3984 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
3985 /* we only really generate code for main, all other functions get
3986 * inlined or codegen'd upon an actual call.
3989 /* do some basic error checking though */
3990 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3991 /* check that non-void functions actually return something */
3993 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
3995 slang_info_log_error(A
->log
,
3996 "function \"%s\" has no return statement",
3997 (char *) fun
->header
.a_name
);
3999 "function \"%s\" has no return statement\n",
4000 (char *) fun
->header
.a_name
);
4005 return GL_TRUE
; /* not an error */
4009 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4010 slang_print_function(fun
, 1);
4013 /* should have been allocated earlier: */
4014 assert(A
->program
->Parameters
);
4015 assert(A
->program
->Varying
);
4016 assert(A
->vartable
);
4018 A
->CurFunction
= fun
;
4020 /* fold constant expressions, etc. */
4021 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4024 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4025 slang_print_function(fun
, 1);
4028 /* Create an end-of-function label */
4029 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4031 /* push new vartable scope */
4032 _slang_push_var_table(A
->vartable
);
4034 /* Generate IR tree for the function body code */
4035 n
= _slang_gen_operation(A
, fun
->body
);
4037 n
= new_node1(IR_SCOPE
, n
);
4039 /* pop vartable, restore previous */
4040 _slang_pop_var_table(A
->vartable
);
4043 /* XXX record error */
4047 /* append an end-of-function-label to IR tree */
4048 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4050 /*_slang_label_delete(A->curFuncEndLabel);*/
4051 A
->curFuncEndLabel
= NULL
;
4054 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4055 slang_print_function(fun
, 1);
4058 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4059 _slang_print_ir_tree(n
, 0);
4062 printf("************* End codegen function ************\n\n");
4065 /* Emit program instructions */
4066 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4067 _slang_free_ir_tree(n
);
4069 /* free codegen context */
4071 _mesa_free(A->codegen);