2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. 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 /** Max iterations to unroll */
61 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS
= 4;
63 /** Max for-loop body size (in slang operations) to unroll */
64 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
68 static slang_ir_node
*
69 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
73 * Retrieves type information about an operation.
74 * Returns GL_TRUE on success.
75 * Returns GL_FALSE otherwise.
78 typeof_operation(const struct slang_assemble_ctx_
*A
,
82 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
87 is_sampler_type(const slang_fully_specified_type
*t
)
89 switch (t
->specifier
.type
) {
90 case SLANG_SPEC_SAMPLER1D
:
91 case SLANG_SPEC_SAMPLER2D
:
92 case SLANG_SPEC_SAMPLER3D
:
93 case SLANG_SPEC_SAMPLERCUBE
:
94 case SLANG_SPEC_SAMPLER1DSHADOW
:
95 case SLANG_SPEC_SAMPLER2DSHADOW
:
96 case SLANG_SPEC_SAMPLER2DRECT
:
97 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
106 * Return the offset (in floats or ints) of the named field within
107 * the given struct. Return -1 if field not found.
108 * If field is NULL, return the size of the struct instead.
111 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
115 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
116 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
117 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
119 /* types larger than 1 float are register (4-float) aligned */
120 offset
= (offset
+ 3) & ~3;
122 if (field
&& v
->a_name
== field
) {
128 return -1; /* field not found */
130 return offset
; /* struct size */
135 * Return the size (in floats) of the given type specifier.
136 * If the size is greater than 4, the size should be a multiple of 4
137 * so that the correct number of 4-float registers are allocated.
138 * For example, a mat3x2 is size 12 because we want to store the
139 * 3 columns in 3 float[4] registers.
142 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
145 switch (spec
->type
) {
146 case SLANG_SPEC_VOID
:
149 case SLANG_SPEC_BOOL
:
152 case SLANG_SPEC_BVEC2
:
155 case SLANG_SPEC_BVEC3
:
158 case SLANG_SPEC_BVEC4
:
164 case SLANG_SPEC_IVEC2
:
167 case SLANG_SPEC_IVEC3
:
170 case SLANG_SPEC_IVEC4
:
173 case SLANG_SPEC_FLOAT
:
176 case SLANG_SPEC_VEC2
:
179 case SLANG_SPEC_VEC3
:
182 case SLANG_SPEC_VEC4
:
185 case SLANG_SPEC_MAT2
:
186 sz
= 2 * 4; /* 2 columns (regs) */
188 case SLANG_SPEC_MAT3
:
191 case SLANG_SPEC_MAT4
:
194 case SLANG_SPEC_MAT23
:
195 sz
= 2 * 4; /* 2 columns (regs) */
197 case SLANG_SPEC_MAT32
:
198 sz
= 3 * 4; /* 3 columns (regs) */
200 case SLANG_SPEC_MAT24
:
203 case SLANG_SPEC_MAT42
:
204 sz
= 4 * 4; /* 4 columns (regs) */
206 case SLANG_SPEC_MAT34
:
209 case SLANG_SPEC_MAT43
:
210 sz
= 4 * 4; /* 4 columns (regs) */
212 case SLANG_SPEC_SAMPLER1D
:
213 case SLANG_SPEC_SAMPLER2D
:
214 case SLANG_SPEC_SAMPLER3D
:
215 case SLANG_SPEC_SAMPLERCUBE
:
216 case SLANG_SPEC_SAMPLER1DSHADOW
:
217 case SLANG_SPEC_SAMPLER2DSHADOW
:
218 case SLANG_SPEC_SAMPLER2DRECT
:
219 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
220 sz
= 1; /* a sampler is basically just an integer index */
222 case SLANG_SPEC_STRUCT
:
223 sz
= _slang_field_offset(spec
, 0); /* special use */
225 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
228 case SLANG_SPEC_ARRAY
:
229 sz
= _slang_sizeof_type_specifier(spec
->_array
);
232 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
237 /* if size is > 4, it should be a multiple of four */
238 assert((sz
& 0x3) == 0);
245 * Query variable/array length (number of elements).
246 * This is slightly non-trivial because there are two ways to express
247 * arrays: "float x[3]" vs. "float[3] x".
248 * \return the length of the array for the given variable, or 0 if not an array
251 _slang_array_length(const slang_variable
*var
)
253 if (var
->type
.array_len
> 0) {
254 /* Ex: float[4] x; */
255 return var
->type
.array_len
;
257 if (var
->array_len
> 0) {
258 /* Ex: float x[4]; */
259 return var
->array_len
;
266 * Compute total size of array give size of element, number of elements.
267 * \return size in floats
270 _slang_array_size(GLint elemSize
, GLint arrayLen
)
273 assert(elemSize
> 0);
275 /* round up base type to multiple of 4 */
276 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
287 * Establish the binding between a slang_ir_node and a slang_variable.
288 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
289 * The IR node must be a IR_VAR or IR_VAR_DECL node.
290 * \param n the IR node
291 * \param var the variable to associate with the IR node
294 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
298 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
299 assert(!n
->Var
|| n
->Var
== var
);
304 /* need to setup storage */
305 if (n
->Var
&& n
->Var
->store
) {
306 /* node storage info = var storage info */
307 n
->Store
= n
->Var
->store
;
310 /* alloc new storage info */
311 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
313 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
315 (void*) n
->Store
, n
->Store
->Size
);
318 n
->Var
->store
= n
->Store
;
319 assert(n
->Var
->store
);
326 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
327 * or -1 if the type is not a sampler.
330 sampler_to_texture_index(const slang_type_specifier_type type
)
333 case SLANG_SPEC_SAMPLER1D
:
334 return TEXTURE_1D_INDEX
;
335 case SLANG_SPEC_SAMPLER2D
:
336 return TEXTURE_2D_INDEX
;
337 case SLANG_SPEC_SAMPLER3D
:
338 return TEXTURE_3D_INDEX
;
339 case SLANG_SPEC_SAMPLERCUBE
:
340 return TEXTURE_CUBE_INDEX
;
341 case SLANG_SPEC_SAMPLER1DSHADOW
:
342 return TEXTURE_1D_INDEX
; /* XXX fix */
343 case SLANG_SPEC_SAMPLER2DSHADOW
:
344 return TEXTURE_2D_INDEX
; /* XXX fix */
345 case SLANG_SPEC_SAMPLER2DRECT
:
346 return TEXTURE_RECT_INDEX
;
347 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
348 return TEXTURE_RECT_INDEX
; /* XXX fix */
355 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
358 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
359 * a vertex or fragment program input variable. Return -1 if the input
361 * XXX return size too
364 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
371 static const struct input_info vertInputs
[] = {
372 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
373 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
374 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
375 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
376 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
377 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
378 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
379 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
380 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
381 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
382 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
383 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
384 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
385 { NULL
, 0, SWIZZLE_NOOP
}
387 static const struct input_info fragInputs
[] = {
388 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
389 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
390 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
391 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
392 /* note: we're packing several quantities into the fogcoord vector */
393 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
394 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
395 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
396 { NULL
, 0, SWIZZLE_NOOP
}
399 const struct input_info
*inputs
400 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
402 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
404 for (i
= 0; inputs
[i
].Name
; i
++) {
405 if (strcmp(inputs
[i
].Name
, name
) == 0) {
407 *swizzleOut
= inputs
[i
].Swizzle
;
408 return inputs
[i
].Attrib
;
416 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
417 * a vertex or fragment program output variable. Return -1 for an invalid
421 _slang_output_index(const char *name
, GLenum target
)
427 static const struct output_info vertOutputs
[] = {
428 { "gl_Position", VERT_RESULT_HPOS
},
429 { "gl_FrontColor", VERT_RESULT_COL0
},
430 { "gl_BackColor", VERT_RESULT_BFC0
},
431 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
432 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
433 { "gl_TexCoord", VERT_RESULT_TEX0
},
434 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
435 { "gl_PointSize", VERT_RESULT_PSIZ
},
438 static const struct output_info fragOutputs
[] = {
439 { "gl_FragColor", FRAG_RESULT_COLR
},
440 { "gl_FragDepth", FRAG_RESULT_DEPR
},
441 { "gl_FragData", FRAG_RESULT_DATA0
},
445 const struct output_info
*outputs
446 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
448 for (i
= 0; outputs
[i
].Name
; i
++) {
449 if (strcmp(outputs
[i
].Name
, name
) == 0) {
451 return outputs
[i
].Attrib
;
459 /**********************************************************************/
463 * Map "_asm foo" to IR_FOO, etc.
468 slang_ir_opcode Opcode
;
469 GLuint HaveRetValue
, NumParams
;
473 static slang_asm_info AsmInfo
[] = {
475 { "vec4_add", IR_ADD
, 1, 2 },
476 { "vec4_subtract", IR_SUB
, 1, 2 },
477 { "vec4_multiply", IR_MUL
, 1, 2 },
478 { "vec4_dot", IR_DOT4
, 1, 2 },
479 { "vec3_dot", IR_DOT3
, 1, 2 },
480 { "vec2_dot", IR_DOT2
, 1, 2 },
481 { "vec3_nrm", IR_NRM3
, 1, 1 },
482 { "vec4_nrm", IR_NRM4
, 1, 1 },
483 { "vec3_cross", IR_CROSS
, 1, 2 },
484 { "vec4_lrp", IR_LRP
, 1, 3 },
485 { "vec4_min", IR_MIN
, 1, 2 },
486 { "vec4_max", IR_MAX
, 1, 2 },
487 { "vec4_clamp", IR_CLAMP
, 1, 3 },
488 { "vec4_seq", IR_SEQUAL
, 1, 2 },
489 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
490 { "vec4_sge", IR_SGE
, 1, 2 },
491 { "vec4_sgt", IR_SGT
, 1, 2 },
492 { "vec4_sle", IR_SLE
, 1, 2 },
493 { "vec4_slt", IR_SLT
, 1, 2 },
495 { "vec4_move", IR_MOVE
, 1, 1 },
496 { "vec4_floor", IR_FLOOR
, 1, 1 },
497 { "vec4_frac", IR_FRAC
, 1, 1 },
498 { "vec4_abs", IR_ABS
, 1, 1 },
499 { "vec4_negate", IR_NEG
, 1, 1 },
500 { "vec4_ddx", IR_DDX
, 1, 1 },
501 { "vec4_ddy", IR_DDY
, 1, 1 },
502 /* float binary op */
503 { "float_power", IR_POW
, 1, 2 },
504 /* texture / sampler */
505 { "vec4_tex1d", IR_TEX
, 1, 2 },
506 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
507 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
508 { "vec4_tex2d", IR_TEX
, 1, 2 },
509 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
510 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
511 { "vec4_tex3d", IR_TEX
, 1, 2 },
512 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
513 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
514 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
515 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
516 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
519 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
520 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
521 { "float_exp", IR_EXP
, 1, 1 },
522 { "float_exp2", IR_EXP2
, 1, 1 },
523 { "float_log2", IR_LOG2
, 1, 1 },
524 { "float_rsq", IR_RSQ
, 1, 1 },
525 { "float_rcp", IR_RCP
, 1, 1 },
526 { "float_sine", IR_SIN
, 1, 1 },
527 { "float_cosine", IR_COS
, 1, 1 },
528 { "float_noise1", IR_NOISE1
, 1, 1},
529 { "float_noise2", IR_NOISE2
, 1, 1},
530 { "float_noise3", IR_NOISE3
, 1, 1},
531 { "float_noise4", IR_NOISE4
, 1, 1},
533 { NULL
, IR_NOP
, 0, 0 }
537 static slang_ir_node
*
538 new_node3(slang_ir_opcode op
,
539 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
541 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
547 n
->InstLocation
= -1;
552 static slang_ir_node
*
553 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
555 return new_node3(op
, c0
, c1
, NULL
);
558 static slang_ir_node
*
559 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
561 return new_node3(op
, c0
, NULL
, NULL
);
564 static slang_ir_node
*
565 new_node0(slang_ir_opcode op
)
567 return new_node3(op
, NULL
, NULL
, NULL
);
572 * Create sequence of two nodes.
574 static slang_ir_node
*
575 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
581 return new_node2(IR_SEQ
, left
, right
);
584 static slang_ir_node
*
585 new_label(slang_label
*label
)
587 slang_ir_node
*n
= new_node0(IR_LABEL
);
594 static slang_ir_node
*
595 new_float_literal(const float v
[4], GLuint size
)
597 slang_ir_node
*n
= new_node0(IR_FLOAT
);
599 COPY_4V(n
->Value
, v
);
600 /* allocate a storage object, but compute actual location (Index) later */
601 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
606 static slang_ir_node
*
607 new_not(slang_ir_node
*n
)
609 return new_node1(IR_NOT
, n
);
614 * Non-inlined function call.
616 static slang_ir_node
*
617 new_function_call(slang_ir_node
*code
, slang_label
*name
)
619 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
628 * Unconditional jump.
630 static slang_ir_node
*
631 new_return(slang_label
*dest
)
633 slang_ir_node
*n
= new_node0(IR_RETURN
);
641 static slang_ir_node
*
642 new_loop(slang_ir_node
*body
)
644 return new_node1(IR_LOOP
, body
);
648 static slang_ir_node
*
649 new_break(slang_ir_node
*loopNode
)
651 slang_ir_node
*n
= new_node0(IR_BREAK
);
653 assert(loopNode
->Opcode
== IR_LOOP
);
655 /* insert this node at head of linked list */
656 n
->List
= loopNode
->List
;
664 * Make new IR_BREAK_IF_TRUE.
666 static slang_ir_node
*
667 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
671 assert(loopNode
->Opcode
== IR_LOOP
);
672 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
674 /* insert this node at head of linked list */
675 n
->List
= loopNode
->List
;
683 * Make new IR_CONT_IF_TRUE node.
685 static slang_ir_node
*
686 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
690 assert(loopNode
->Opcode
== IR_LOOP
);
691 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
693 /* insert this node at head of linked list */
694 n
->List
= loopNode
->List
;
701 static slang_ir_node
*
702 new_cond(slang_ir_node
*n
)
704 slang_ir_node
*c
= new_node1(IR_COND
, n
);
709 static slang_ir_node
*
710 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
712 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
717 * New IR_VAR node - a reference to a previously declared variable.
719 static slang_ir_node
*
720 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
722 slang_ir_node
*n
= new_node0(IR_VAR
);
724 _slang_attach_storage(n
, var
);
731 * Check if the given function is really just a wrapper for a
732 * basic assembly instruction.
735 slang_is_asm_function(const slang_function
*fun
)
737 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
738 fun
->body
->num_children
== 1 &&
739 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
747 _slang_is_noop(const slang_operation
*oper
)
750 oper
->type
== SLANG_OPER_VOID
||
751 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
759 * Recursively search tree for a node of the given type.
761 static slang_operation
*
762 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
765 if (oper
->type
== type
)
767 for (i
= 0; i
< oper
->num_children
; i
++) {
768 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
777 * Count the number of operations of the given time rooted at 'oper'.
780 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
783 if (oper
->type
== type
) {
786 for (i
= 0; i
< oper
->num_children
; i
++) {
787 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
794 * Check if the 'return' statement found under 'oper' is a "tail return"
795 * that can be no-op'd. For example:
800 * return; // this is a no-op
803 * This is used when determining if a function can be inlined. If the
804 * 'return' is not the last statement, we can't inline the function since
805 * we still need the semantic behaviour of the 'return' but we don't want
806 * to accidentally return from the _calling_ function. We'd need to use an
807 * unconditional branch, but we don't have such a GPU instruction (not
811 _slang_is_tail_return(const slang_operation
*oper
)
813 GLuint k
= oper
->num_children
;
816 const slang_operation
*last
= &oper
->children
[k
- 1];
817 if (last
->type
== SLANG_OPER_RETURN
)
819 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
820 last
->type
== SLANG_OPER_LABEL
)
821 k
--; /* try prev child */
822 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
823 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
824 /* try sub-children */
825 return _slang_is_tail_return(last
);
835 slang_resolve_variable(slang_operation
*oper
)
837 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
838 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
844 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
847 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
848 GLuint substCount
, slang_variable
**substOld
,
849 slang_operation
**substNew
, GLboolean isLHS
)
851 switch (oper
->type
) {
852 case SLANG_OPER_VARIABLE_DECL
:
854 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
855 oper
->a_id
, GL_TRUE
);
857 if (v
->initializer
&& oper
->num_children
== 0) {
858 /* set child of oper to copy of initializer */
859 oper
->num_children
= 1;
860 oper
->children
= slang_operation_new(1);
861 slang_operation_copy(&oper
->children
[0], v
->initializer
);
863 if (oper
->num_children
== 1) {
864 /* the initializer */
865 slang_substitute(A
, &oper
->children
[0], substCount
,
866 substOld
, substNew
, GL_FALSE
);
870 case SLANG_OPER_IDENTIFIER
:
871 assert(oper
->num_children
== 0);
872 if (1/**!isLHS XXX FIX */) {
873 slang_atom id
= oper
->a_id
;
876 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
878 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
882 /* look for a substitution */
883 for (i
= 0; i
< substCount
; i
++) {
884 if (v
== substOld
[i
]) {
885 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
886 #if 0 /* DEBUG only */
887 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
888 assert(substNew
[i
]->var
);
889 assert(substNew
[i
]->var
->a_name
);
890 printf("Substitute %s with %s in id node %p\n",
891 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
895 printf("Substitute %s with %f in id node %p\n",
896 (char*)v
->a_name
, substNew
[i
]->literal
[0],
900 slang_operation_copy(oper
, substNew
[i
]);
907 case SLANG_OPER_RETURN
:
908 /* do return replacement here too */
909 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
910 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
916 * then do substitutions on the assignment.
918 slang_operation
*blockOper
, *assignOper
, *returnOper
;
920 /* check if function actually has a return type */
921 assert(A
->CurFunction
);
922 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
923 slang_info_log_error(A
->log
, "illegal return expression");
927 blockOper
= slang_operation_new(1);
928 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
929 blockOper
->num_children
= 2;
930 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
931 blockOper
->children
= slang_operation_new(2);
932 assignOper
= blockOper
->children
+ 0;
933 returnOper
= blockOper
->children
+ 1;
935 assignOper
->type
= SLANG_OPER_ASSIGN
;
936 assignOper
->num_children
= 2;
937 assignOper
->locals
->outer_scope
= blockOper
->locals
;
938 assignOper
->children
= slang_operation_new(2);
939 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
940 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
941 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
943 slang_operation_copy(&assignOper
->children
[1],
946 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
947 assert(returnOper
->num_children
== 0);
949 /* do substitutions on the "__retVal = expr" sub-tree */
950 slang_substitute(A
, assignOper
,
951 substCount
, substOld
, substNew
, GL_FALSE
);
953 /* install new code */
954 slang_operation_copy(oper
, blockOper
);
955 slang_operation_destruct(blockOper
);
958 /* check if return value was expected */
959 assert(A
->CurFunction
);
960 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
961 slang_info_log_error(A
->log
, "return statement requires an expression");
967 case SLANG_OPER_ASSIGN
:
968 case SLANG_OPER_SUBSCRIPT
:
970 * child[0] can't have substitutions but child[1] can.
972 slang_substitute(A
, &oper
->children
[0],
973 substCount
, substOld
, substNew
, GL_TRUE
);
974 slang_substitute(A
, &oper
->children
[1],
975 substCount
, substOld
, substNew
, GL_FALSE
);
977 case SLANG_OPER_FIELD
:
979 slang_substitute(A
, &oper
->children
[0],
980 substCount
, substOld
, substNew
, GL_TRUE
);
985 for (i
= 0; i
< oper
->num_children
; i
++)
986 slang_substitute(A
, &oper
->children
[i
],
987 substCount
, substOld
, substNew
, GL_FALSE
);
994 * Produce inline code for a call to an assembly instruction.
995 * This is typically used to compile a call to a built-in function like this:
997 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
999 * __asm vec4_lrp __retVal, a, y, x;
1004 * r = mix(p1, p2, p3);
1014 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1016 static slang_operation
*
1017 slang_inline_asm_function(slang_assemble_ctx
*A
,
1018 slang_function
*fun
, slang_operation
*oper
)
1020 const GLuint numArgs
= oper
->num_children
;
1022 slang_operation
*inlined
;
1023 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1024 slang_variable
**substOld
;
1025 slang_operation
**substNew
;
1027 ASSERT(slang_is_asm_function(fun
));
1028 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1031 printf("Inline %s as %s\n",
1032 (char*) fun->header.a_name,
1033 (char*) fun->body->children[0].a_id);
1037 * We'll substitute formal params with actual args in the asm call.
1039 substOld
= (slang_variable
**)
1040 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1041 substNew
= (slang_operation
**)
1042 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1043 for (i
= 0; i
< numArgs
; i
++) {
1044 substOld
[i
] = fun
->parameters
->variables
[i
];
1045 substNew
[i
] = oper
->children
+ i
;
1048 /* make a copy of the code to inline */
1049 inlined
= slang_operation_new(1);
1050 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1052 /* get rid of the __retVal child */
1053 inlined
->num_children
--;
1054 for (i
= 0; i
< inlined
->num_children
; i
++) {
1055 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1059 /* now do formal->actual substitutions */
1060 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1062 _slang_free(substOld
);
1063 _slang_free(substNew
);
1066 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1067 (char *) fun
->header
.a_name
);
1068 slang_print_tree(inlined
, 3);
1069 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1077 * Inline the given function call operation.
1078 * Return a new slang_operation that corresponds to the inlined code.
1080 static slang_operation
*
1081 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1082 slang_operation
*oper
, slang_operation
*returnOper
)
1089 ParamMode
*paramMode
;
1090 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1091 const GLuint numArgs
= oper
->num_children
;
1092 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1093 slang_operation
*args
= oper
->children
;
1094 slang_operation
*inlined
, *top
;
1095 slang_variable
**substOld
;
1096 slang_operation
**substNew
;
1097 GLuint substCount
, numCopyIn
, i
;
1098 slang_function
*prevFunction
;
1099 slang_variable_scope
*newScope
= NULL
;
1102 prevFunction
= A
->CurFunction
;
1103 A
->CurFunction
= fun
;
1105 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1106 assert(fun
->param_count
== totalArgs
);
1108 /* allocate temporary arrays */
1109 paramMode
= (ParamMode
*)
1110 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1111 substOld
= (slang_variable
**)
1112 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1113 substNew
= (slang_operation
**)
1114 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1117 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1118 (char *) fun
->header
.a_name
,
1119 fun
->parameters
->num_variables
, numArgs
);
1122 if (haveRetValue
&& !returnOper
) {
1123 /* Create 3-child comma sequence for inlined code:
1124 * child[0]: declare __resultTmp
1125 * child[1]: inlined function body
1126 * child[2]: __resultTmp
1128 slang_operation
*commaSeq
;
1129 slang_operation
*declOper
= NULL
;
1130 slang_variable
*resultVar
;
1132 commaSeq
= slang_operation_new(1);
1133 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1134 assert(commaSeq
->locals
);
1135 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1136 commaSeq
->num_children
= 3;
1137 commaSeq
->children
= slang_operation_new(3);
1138 /* allocate the return var */
1139 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1141 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1142 (void*)commaSeq->locals, (char *) fun->header.a_name);
1145 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1146 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1147 resultVar
->isTemp
= GL_TRUE
;
1149 /* child[0] = __resultTmp declaration */
1150 declOper
= &commaSeq
->children
[0];
1151 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1152 declOper
->a_id
= resultVar
->a_name
;
1153 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1155 /* child[1] = function body */
1156 inlined
= &commaSeq
->children
[1];
1157 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1159 /* child[2] = __resultTmp reference */
1160 returnOper
= &commaSeq
->children
[2];
1161 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1162 returnOper
->a_id
= resultVar
->a_name
;
1163 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1168 top
= inlined
= slang_operation_new(1);
1169 /* XXXX this may be inappropriate!!!! */
1170 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1174 assert(inlined
->locals
);
1176 /* Examine the parameters, look for inout/out params, look for possible
1177 * substitutions, etc:
1178 * param type behaviour
1179 * in copy actual to local
1180 * const in substitute param with actual
1184 for (i
= 0; i
< totalArgs
; i
++) {
1185 slang_variable
*p
= fun
->parameters
->variables
[i
];
1187 printf("Param %d: %s %s \n", i,
1188 slang_type_qual_string(p->type.qualifier),
1189 (char *) p->a_name);
1191 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1192 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1193 /* an output param */
1194 slang_operation
*arg
;
1199 paramMode
[i
] = SUBST
;
1201 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1202 slang_resolve_variable(arg
);
1204 /* replace parameter 'p' with argument 'arg' */
1205 substOld
[substCount
] = p
;
1206 substNew
[substCount
] = arg
; /* will get copied */
1209 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1210 /* a constant input param */
1211 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1212 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1213 /* replace all occurances of this parameter variable with the
1214 * actual argument variable or a literal.
1216 paramMode
[i
] = SUBST
;
1217 slang_resolve_variable(&args
[i
]);
1218 substOld
[substCount
] = p
;
1219 substNew
[substCount
] = &args
[i
]; /* will get copied */
1223 paramMode
[i
] = COPY_IN
;
1227 paramMode
[i
] = COPY_IN
;
1229 assert(paramMode
[i
]);
1232 /* actual code inlining: */
1233 slang_operation_copy(inlined
, fun
->body
);
1235 /*** XXX review this */
1236 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1237 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1238 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1241 printf("======================= orig body code ======================\n");
1242 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1243 slang_print_tree(fun
->body
, 8);
1244 printf("======================= copied code =========================\n");
1245 slang_print_tree(inlined
, 8);
1248 /* do parameter substitution in inlined code: */
1249 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1252 printf("======================= subst code ==========================\n");
1253 slang_print_tree(inlined
, 8);
1254 printf("=============================================================\n");
1257 /* New prolog statements: (inserted before the inlined code)
1258 * Copy the 'in' arguments.
1261 for (i
= 0; i
< numArgs
; i
++) {
1262 if (paramMode
[i
] == COPY_IN
) {
1263 slang_variable
*p
= fun
->parameters
->variables
[i
];
1264 /* declare parameter 'p' */
1265 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1269 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1270 assert(decl
->locals
);
1271 decl
->locals
->outer_scope
= inlined
->locals
;
1272 decl
->a_id
= p
->a_name
;
1273 decl
->num_children
= 1;
1274 decl
->children
= slang_operation_new(1);
1276 /* child[0] is the var's initializer */
1277 slang_operation_copy(&decl
->children
[0], args
+ i
);
1279 /* add parameter 'p' to the local variable scope here */
1281 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1282 pCopy
->type
= p
->type
;
1283 pCopy
->a_name
= p
->a_name
;
1284 pCopy
->array_len
= p
->array_len
;
1287 newScope
= inlined
->locals
;
1292 /* Now add copies of the function's local vars to the new variable scope */
1293 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1294 slang_variable
*p
= fun
->parameters
->variables
[i
];
1295 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1296 pCopy
->type
= p
->type
;
1297 pCopy
->a_name
= p
->a_name
;
1298 pCopy
->array_len
= p
->array_len
;
1302 /* New epilog statements:
1303 * 1. Create end of function label to jump to from return statements.
1304 * 2. Copy the 'out' parameter vars
1307 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1309 inlined
->num_children
);
1310 lab
->type
= SLANG_OPER_LABEL
;
1311 lab
->label
= A
->curFuncEndLabel
;
1314 for (i
= 0; i
< totalArgs
; i
++) {
1315 if (paramMode
[i
] == COPY_OUT
) {
1316 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1317 /* actualCallVar = outParam */
1318 /*if (i > 0 || !haveRetValue)*/
1319 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1321 inlined
->num_children
);
1322 ass
->type
= SLANG_OPER_ASSIGN
;
1323 ass
->num_children
= 2;
1324 ass
->locals
->outer_scope
= inlined
->locals
;
1325 ass
->children
= slang_operation_new(2);
1326 ass
->children
[0] = args
[i
]; /*XXX copy */
1327 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1328 ass
->children
[1].a_id
= p
->a_name
;
1329 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1333 _slang_free(paramMode
);
1334 _slang_free(substOld
);
1335 _slang_free(substNew
);
1337 /* Update scoping to use the new local vars instead of the
1338 * original function's vars. This is especially important
1339 * for nested inlining.
1342 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1345 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1346 (char *) fun
->header
.a_name
,
1347 fun
->parameters
->num_variables
, numArgs
);
1348 slang_print_tree(top
, 0);
1352 A
->CurFunction
= prevFunction
;
1358 static slang_ir_node
*
1359 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1360 slang_operation
*oper
, slang_operation
*dest
)
1363 slang_operation
*inlined
;
1364 slang_label
*prevFuncEndLabel
;
1367 prevFuncEndLabel
= A
->curFuncEndLabel
;
1368 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1369 A
->curFuncEndLabel
= _slang_label_new(name
);
1370 assert(A
->curFuncEndLabel
);
1372 if (slang_is_asm_function(fun
) && !dest
) {
1373 /* assemble assembly function - tree style */
1374 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1377 /* non-assembly function */
1378 /* We always generate an "inline-able" block of code here.
1380 * 1. insert the inline code
1381 * 2. Generate a call to the "inline" code as a subroutine
1385 slang_operation
*ret
= NULL
;
1387 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1391 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1393 /* check if this is a "tail" return */
1394 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1395 _slang_is_tail_return(inlined
)) {
1396 /* The only RETURN is the last stmt in the function, no-op it
1397 * and inline the function body.
1399 ret
->type
= SLANG_OPER_NONE
;
1402 slang_operation
*callOper
;
1403 /* The function we're calling has one or more 'return' statements.
1404 * So, we can't truly inline this function because we need to
1405 * implement 'return' with RET (and CAL).
1406 * Nevertheless, we performed "inlining" to make a new instance
1407 * of the function body to deal with static register allocation.
1409 * XXX check if there's one 'return' and if it's the very last
1410 * statement in the function - we can optimize that case.
1412 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1413 inlined
->type
== SLANG_OPER_SEQUENCE
);
1415 if (_slang_function_has_return_value(fun
) && !dest
) {
1416 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1417 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1418 callOper
= &inlined
->children
[1];
1423 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1424 callOper
->fun
= fun
;
1425 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1433 /* Replace the function call with the inlined block (or new CALL stmt) */
1434 slang_operation_destruct(oper
);
1436 _slang_free(inlined
);
1439 assert(inlined
->locals
);
1440 printf("*** Inlined code for call to %s:\n",
1441 (char*) fun
->header
.a_name
);
1442 slang_print_tree(oper
, 10);
1446 n
= _slang_gen_operation(A
, oper
);
1448 /*_slang_label_delete(A->curFuncEndLabel);*/
1449 A
->curFuncEndLabel
= prevFuncEndLabel
;
1455 static slang_asm_info
*
1456 slang_find_asm_info(const char *name
)
1459 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1460 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1469 * Some write-masked assignments are simple, but others are hard.
1472 * v.xy = vec2(a, b);
1475 * v.zy = vec2(a, b);
1476 * this gets transformed/swizzled into:
1477 * v.zy = vec2(a, b).*yx* (* = don't care)
1478 * This function helps to determine simple vs. non-simple.
1481 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1483 switch (writemask
) {
1485 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1487 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1489 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1491 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1493 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1494 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1496 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1497 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1498 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1499 case WRITEMASK_XYZW
:
1500 return swizzle
== SWIZZLE_NOOP
;
1508 * Convert the given swizzle into a writemask. In some cases this
1509 * is trivial, in other cases, we'll need to also swizzle the right
1510 * hand side to put components in the right places.
1511 * See comment above for more info.
1512 * XXX this function could be simplified and should probably be renamed.
1513 * \param swizzle the incoming swizzle
1514 * \param writemaskOut returns the writemask
1515 * \param swizzleOut swizzle to apply to the right-hand-side
1516 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1519 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1520 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1522 GLuint mask
= 0x0, newSwizzle
[4];
1525 /* make new dst writemask, compute size */
1526 for (i
= 0; i
< 4; i
++) {
1527 const GLuint swz
= GET_SWZ(swizzle
, i
);
1528 if (swz
== SWIZZLE_NIL
) {
1532 assert(swz
>= 0 && swz
<= 3);
1534 if (swizzle
!= SWIZZLE_XXXX
&&
1535 swizzle
!= SWIZZLE_YYYY
&&
1536 swizzle
!= SWIZZLE_ZZZZ
&&
1537 swizzle
!= SWIZZLE_WWWW
&&
1538 (mask
& (1 << swz
))) {
1539 /* a channel can't be specified twice (ex: ".xyyz") */
1540 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1541 _mesa_swizzle_string(swizzle
, 0, 0));
1547 assert(mask
<= 0xf);
1548 size
= i
; /* number of components in mask/swizzle */
1550 *writemaskOut
= mask
;
1552 /* make new src swizzle, by inversion */
1553 for (i
= 0; i
< 4; i
++) {
1554 newSwizzle
[i
] = i
; /*identity*/
1556 for (i
= 0; i
< size
; i
++) {
1557 const GLuint swz
= GET_SWZ(swizzle
, i
);
1558 newSwizzle
[swz
] = i
;
1560 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1565 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1567 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1569 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1571 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1573 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1582 * Recursively traverse 'oper' to produce a swizzle mask in the event
1583 * of any vector subscripts and swizzle suffixes.
1584 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1587 resolve_swizzle(const slang_operation
*oper
)
1589 if (oper
->type
== SLANG_OPER_FIELD
) {
1590 /* writemask from .xyzw suffix */
1592 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1593 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1597 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1598 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1602 return SWIZZLE_XYZW
;
1604 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1605 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1606 /* writemask from [index] */
1607 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1608 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1613 swizzle
= SWIZZLE_XXXX
;
1616 swizzle
= SWIZZLE_YYYY
;
1619 swizzle
= SWIZZLE_ZZZZ
;
1622 swizzle
= SWIZZLE_WWWW
;
1625 swizzle
= SWIZZLE_XYZW
;
1627 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1631 return SWIZZLE_XYZW
;
1637 * Recursively descend through swizzle nodes to find the node's storage info.
1639 static slang_ir_storage
*
1640 get_store(const slang_ir_node
*n
)
1642 if (n
->Opcode
== IR_SWIZZLE
) {
1643 return get_store(n
->Children
[0]);
1651 * Generate IR tree for an asm instruction/operation such as:
1652 * __asm vec4_dot __retVal.x, v1, v2;
1654 static slang_ir_node
*
1655 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1656 slang_operation
*dest
)
1658 const slang_asm_info
*info
;
1659 slang_ir_node
*kids
[3], *n
;
1660 GLuint j
, firstOperand
;
1662 assert(oper
->type
== SLANG_OPER_ASM
);
1664 info
= slang_find_asm_info((char *) oper
->a_id
);
1666 _mesa_problem(NULL
, "undefined __asm function %s\n",
1667 (char *) oper
->a_id
);
1670 assert(info
->NumParams
<= 3);
1672 if (info
->NumParams
== oper
->num_children
) {
1673 /* Storage for result is not specified.
1674 * Children[0], [1], [2] are the operands.
1679 /* Storage for result (child[0]) is specified.
1680 * Children[1], [2], [3] are the operands.
1685 /* assemble child(ren) */
1686 kids
[0] = kids
[1] = kids
[2] = NULL
;
1687 for (j
= 0; j
< info
->NumParams
; j
++) {
1688 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1693 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1696 /* Setup n->Store to be a particular location. Otherwise, storage
1697 * for the result (a temporary) will be allocated later.
1699 slang_operation
*dest_oper
;
1702 dest_oper
= &oper
->children
[0];
1704 n0
= _slang_gen_operation(A
, dest_oper
);
1709 n
->Store
= n0
->Store
;
1711 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1721 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1724 for (i
= 0; i
< scope
->num_functions
; i
++) {
1725 slang_function
*f
= &scope
->functions
[i
];
1726 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1727 printf(" %s (%d args)\n", name
, f
->param_count
);
1730 if (scope
->outer_scope
)
1731 print_funcs(scope
->outer_scope
, name
);
1736 * Find a function of the given name, taking 'numArgs' arguments.
1737 * This is the function we'll try to call when there is no exact match
1738 * between function parameters and call arguments.
1740 * XXX we should really create a list of candidate functions and try
1743 static slang_function
*
1744 _slang_find_function_by_argc(slang_function_scope
*scope
,
1745 const char *name
, int numArgs
)
1749 for (i
= 0; i
< scope
->num_functions
; i
++) {
1750 slang_function
*f
= &scope
->functions
[i
];
1751 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1752 int haveRetValue
= _slang_function_has_return_value(f
);
1753 if (numArgs
== f
->param_count
- haveRetValue
)
1757 scope
= scope
->outer_scope
;
1764 static slang_function
*
1765 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1768 slang_function
*maxFunc
= NULL
;
1773 for (i
= 0; i
< scope
->num_functions
; i
++) {
1774 slang_function
*f
= &scope
->functions
[i
];
1775 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1776 if (f
->param_count
> maxArgs
) {
1777 maxArgs
= f
->param_count
;
1782 scope
= scope
->outer_scope
;
1790 * Generate a new slang_function which is a constructor for a user-defined
1793 static slang_function
*
1794 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1796 const GLint numFields
= str
->fields
->num_variables
;
1797 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1799 /* function header (name, return type) */
1800 fun
->header
.a_name
= str
->a_name
;
1801 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1802 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1803 fun
->header
.type
.specifier
._struct
= str
;
1805 /* function parameters (= struct's fields) */
1808 for (i
= 0; i
< numFields
; i
++) {
1810 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1812 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1813 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1814 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1816 fun
->param_count
= fun
->parameters
->num_variables
;
1819 /* Add __retVal to params */
1821 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1822 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1824 p
->a_name
= a_retVal
;
1825 p
->type
= fun
->header
.type
;
1826 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1830 /* function body is:
1840 slang_variable_scope
*scope
;
1841 slang_variable
*var
;
1844 fun
->body
= slang_operation_new(1);
1845 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1846 fun
->body
->num_children
= numFields
+ 2;
1847 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1849 scope
= fun
->body
->locals
;
1850 scope
->outer_scope
= fun
->parameters
;
1852 /* create local var 't' */
1853 var
= slang_variable_scope_grow(scope
);
1854 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1855 var
->type
= fun
->header
.type
;
1859 slang_operation
*decl
;
1861 decl
= &fun
->body
->children
[0];
1862 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1863 decl
->locals
= _slang_variable_scope_new(scope
);
1864 decl
->a_id
= var
->a_name
;
1867 /* assign params to fields of t */
1868 for (i
= 0; i
< numFields
; i
++) {
1869 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1871 assign
->type
= SLANG_OPER_ASSIGN
;
1872 assign
->locals
= _slang_variable_scope_new(scope
);
1873 assign
->num_children
= 2;
1874 assign
->children
= slang_operation_new(2);
1877 slang_operation
*lhs
= &assign
->children
[0];
1879 lhs
->type
= SLANG_OPER_FIELD
;
1880 lhs
->locals
= _slang_variable_scope_new(scope
);
1881 lhs
->num_children
= 1;
1882 lhs
->children
= slang_operation_new(1);
1883 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1885 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1886 lhs
->children
[0].a_id
= var
->a_name
;
1887 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1890 lhs
->children
[1].num_children
= 1;
1891 lhs
->children
[1].children
= slang_operation_new(1);
1892 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1893 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1894 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1899 slang_operation
*rhs
= &assign
->children
[1];
1901 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1902 rhs
->locals
= _slang_variable_scope_new(scope
);
1903 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1909 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1911 ret
->type
= SLANG_OPER_RETURN
;
1912 ret
->locals
= _slang_variable_scope_new(scope
);
1913 ret
->num_children
= 1;
1914 ret
->children
= slang_operation_new(1);
1915 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1916 ret
->children
[0].a_id
= var
->a_name
;
1917 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1921 slang_print_function(fun, 1);
1928 * Find/create a function (constructor) for the given structure name.
1930 static slang_function
*
1931 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1934 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1935 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1936 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1937 /* found a structure type that matches the function name */
1938 if (!str
->constructor
) {
1939 /* create the constructor function now */
1940 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1942 return str
->constructor
;
1950 * Generate a new slang_function to satisfy a call to an array constructor.
1951 * Ex: float[3](1., 2., 3.)
1953 static slang_function
*
1954 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1956 slang_type_specifier_type baseType
;
1957 slang_function
*fun
;
1960 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1964 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1966 num_elements
= oper
->num_children
;
1968 /* function header, return type */
1970 fun
->header
.a_name
= oper
->a_id
;
1971 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1972 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1973 fun
->header
.type
.specifier
._array
=
1974 slang_type_specifier_new(baseType
, NULL
, NULL
);
1975 fun
->header
.type
.array_len
= num_elements
;
1978 /* function parameters (= number of elements) */
1981 for (i
= 0; i
< num_elements
; i
++) {
1983 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1985 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1987 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
1988 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1989 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1990 p
->type
.specifier
.type
= baseType
;
1992 fun
->param_count
= fun
->parameters
->num_variables
;
1995 /* Add __retVal to params */
1997 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1998 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2000 p
->a_name
= a_retVal
;
2001 p
->type
= fun
->header
.type
;
2002 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2003 p
->type
.specifier
.type
= baseType
;
2007 /* function body is:
2017 slang_variable_scope
*scope
;
2018 slang_variable
*var
;
2021 fun
->body
= slang_operation_new(1);
2022 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2023 fun
->body
->num_children
= num_elements
+ 2;
2024 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2026 scope
= fun
->body
->locals
;
2027 scope
->outer_scope
= fun
->parameters
;
2029 /* create local var 't' */
2030 var
= slang_variable_scope_grow(scope
);
2031 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2032 var
->type
= fun
->header
.type
;/*XXX copy*/
2036 slang_operation
*decl
;
2038 decl
= &fun
->body
->children
[0];
2039 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2040 decl
->locals
= _slang_variable_scope_new(scope
);
2041 decl
->a_id
= var
->a_name
;
2044 /* assign params to elements of t */
2045 for (i
= 0; i
< num_elements
; i
++) {
2046 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2048 assign
->type
= SLANG_OPER_ASSIGN
;
2049 assign
->locals
= _slang_variable_scope_new(scope
);
2050 assign
->num_children
= 2;
2051 assign
->children
= slang_operation_new(2);
2054 slang_operation
*lhs
= &assign
->children
[0];
2056 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2057 lhs
->locals
= _slang_variable_scope_new(scope
);
2058 lhs
->num_children
= 2;
2059 lhs
->children
= slang_operation_new(2);
2061 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2062 lhs
->children
[0].a_id
= var
->a_name
;
2063 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2065 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2066 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2070 slang_operation
*rhs
= &assign
->children
[1];
2072 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2073 rhs
->locals
= _slang_variable_scope_new(scope
);
2074 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2080 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2082 ret
->type
= SLANG_OPER_RETURN
;
2083 ret
->locals
= _slang_variable_scope_new(scope
);
2084 ret
->num_children
= 1;
2085 ret
->children
= slang_operation_new(1);
2086 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2087 ret
->children
[0].a_id
= var
->a_name
;
2088 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2093 slang_print_function(fun, 1);
2101 _slang_is_vec_mat_type(const char *name
)
2103 static const char *vecmat_types
[] = {
2104 "float", "int", "bool",
2105 "vec2", "vec3", "vec4",
2106 "ivec2", "ivec3", "ivec4",
2107 "bvec2", "bvec3", "bvec4",
2108 "mat2", "mat3", "mat4",
2109 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2113 for (i
= 0; vecmat_types
[i
]; i
++)
2114 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2121 * Assemble a function call, given a particular function name.
2122 * \param name the function's name (operators like '*' are possible).
2124 static slang_ir_node
*
2125 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2126 slang_operation
*oper
, slang_operation
*dest
)
2128 slang_operation
*params
= oper
->children
;
2129 const GLuint param_count
= oper
->num_children
;
2131 slang_function
*fun
;
2134 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2135 if (atom
== SLANG_ATOM_NULL
)
2138 if (oper
->array_constructor
) {
2139 /* this needs special handling */
2140 fun
= _slang_make_array_constructor(A
, oper
);
2143 /* Try to find function by name and exact argument type matching */
2144 GLboolean error
= GL_FALSE
;
2145 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2146 &A
->space
, A
->atoms
, A
->log
, &error
);
2148 slang_info_log_error(A
->log
,
2149 "Function '%s' not found (check argument types)",
2156 /* Next, try locating a constructor function for a user-defined type */
2157 fun
= _slang_locate_struct_constructor(A
, name
);
2161 * At this point, some heuristics are used to try to find a function
2162 * that matches the calling signature by means of casting or "unrolling"
2166 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2167 /* Next, if this call looks like a vec() or mat() constructor call,
2168 * try "unwinding" the args to satisfy a constructor.
2170 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2172 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2173 slang_info_log_error(A
->log
,
2174 "Function '%s' not found (check argument types)",
2181 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2182 /* Next, try casting args to the types of the formal parameters */
2183 int numArgs
= oper
->num_children
;
2184 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2185 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2186 slang_info_log_error(A
->log
,
2187 "Function '%s' not found (check argument types)",
2195 slang_info_log_error(A
->log
,
2196 "Function '%s' not found (check argument types)",
2201 slang_info_log_error(A
->log
,
2202 "Function '%s' prototyped but not defined. "
2203 "Separate compilation units not supported.",
2208 /* type checking to be sure function's return type matches 'dest' type */
2212 slang_typeinfo_construct(&t0
);
2213 typeof_operation(A
, dest
, &t0
);
2215 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2216 slang_info_log_error(A
->log
,
2217 "Incompatible type returned by call to '%s'",
2223 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2225 if (n
&& !n
->Store
&& !dest
2226 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2227 /* setup n->Store for the result of the function call */
2228 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2229 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2230 /*printf("Alloc storage for function result, size %d \n", size);*/
2233 if (oper
->array_constructor
) {
2234 /* free the temporary array constructor function now */
2235 slang_function_destruct(fun
);
2242 static slang_ir_node
*
2243 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2245 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2247 slang_variable
*var
;
2249 /* NOTE: In GLSL 1.20, there's only one kind of method
2250 * call: array.length(). Anything else is an error.
2252 if (oper
->a_id
!= a_length
) {
2253 slang_info_log_error(A
->log
,
2254 "Undefined method call '%s'", (char *) oper
->a_id
);
2258 /* length() takes no arguments */
2259 if (oper
->num_children
> 0) {
2260 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2264 /* lookup the object/variable */
2265 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2266 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2267 slang_info_log_error(A
->log
,
2268 "Undefined object '%s'", (char *) oper
->a_obj
);
2272 /* Create a float/literal IR node encoding the array length */
2273 n
= new_node0(IR_FLOAT
);
2275 n
->Value
[0] = (float) _slang_array_length(var
);
2276 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2283 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2285 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2286 oper
->type
== SLANG_OPER_LITERAL_INT
||
2287 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2288 if (oper
->literal
[0])
2294 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2295 oper
->num_children
== 1) {
2296 return _slang_is_constant_cond(&oper
->children
[0], value
);
2303 * Test if an operation is a scalar or boolean.
2306 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2308 slang_typeinfo type
;
2311 slang_typeinfo_construct(&type
);
2312 typeof_operation(A
, oper
, &type
);
2313 size
= _slang_sizeof_type_specifier(&type
.spec
);
2314 slang_typeinfo_destruct(&type
);
2320 * Test if an operation is boolean.
2323 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2325 slang_typeinfo type
;
2328 slang_typeinfo_construct(&type
);
2329 typeof_operation(A
, oper
, &type
);
2330 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2331 slang_typeinfo_destruct(&type
);
2337 * Generate loop code using high-level IR_LOOP instruction
2339 static slang_ir_node
*
2340 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2344 * BREAK if !expr (child[0])
2345 * body code (child[1])
2347 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2348 GLboolean isConst
, constTrue
;
2350 /* type-check expression */
2351 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2352 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2356 /* Check if loop condition is a constant */
2357 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2359 if (isConst
&& !constTrue
) {
2360 /* loop is never executed! */
2361 return new_node0(IR_NOP
);
2364 loop
= new_loop(NULL
);
2366 /* save old, push new loop */
2367 prevLoop
= A
->CurLoop
;
2370 if (isConst
&& constTrue
) {
2371 /* while(nonzero constant), no conditional break */
2376 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2377 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2379 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2380 loop
->Children
[0] = new_seq(breakIf
, body
);
2382 /* Do infinite loop detection */
2383 /* loop->List is head of linked list of break/continue nodes */
2384 if (!loop
->List
&& isConst
&& constTrue
) {
2385 /* infinite loop detected */
2386 A
->CurLoop
= prevLoop
; /* clean-up */
2387 slang_info_log_error(A
->log
, "Infinite loop detected!");
2391 /* pop loop, restore prev */
2392 A
->CurLoop
= prevLoop
;
2399 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2401 static slang_ir_node
*
2402 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2406 * body code (child[0])
2408 * BREAK if !expr (child[1])
2410 slang_ir_node
*prevLoop
, *loop
;
2411 GLboolean isConst
, constTrue
;
2413 /* type-check expression */
2414 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2415 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2419 loop
= new_loop(NULL
);
2421 /* save old, push new loop */
2422 prevLoop
= A
->CurLoop
;
2426 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2428 /* Check if loop condition is a constant */
2429 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2430 if (isConst
&& constTrue
) {
2431 /* do { } while(1) ==> no conditional break */
2432 loop
->Children
[1] = NULL
; /* no tail code */
2436 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2437 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2440 /* XXX we should do infinite loop detection, as above */
2442 /* pop loop, restore prev */
2443 A
->CurLoop
= prevLoop
;
2450 * Recursively count the number of operations rooted at 'oper'.
2451 * This gives some kind of indication of the size/complexity of an operation.
2454 sizeof_operation(const slang_operation
*oper
)
2457 GLuint count
= 1; /* me */
2459 for (i
= 0; i
< oper
->num_children
; i
++) {
2460 count
+= sizeof_operation(&oper
->children
[i
]);
2471 * Determine if a for-loop can be unrolled.
2472 * At this time, only a rather narrow class of for loops can be unrolled.
2473 * See code for details.
2474 * When a loop can't be unrolled because it's too large we'll emit a
2475 * message to the log.
2478 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2482 const char *varName
;
2484 assert(oper
->type
== SLANG_OPER_FOR
);
2485 assert(oper
->num_children
== 4);
2487 /* children[0] must be "i=constant" */
2488 if (oper
->children
[0].type
!= SLANG_OPER_EXPRESSION
)
2490 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2492 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2494 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2497 /* children[1] must be "i<constant" */
2498 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2500 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2502 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2504 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2507 /* children[2] must be "i++" or "++i" */
2508 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2509 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2511 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2514 /* make sure the same variable name is used in all places */
2515 if ((oper
->children
[0].children
[0].children
[0].a_id
!=
2516 oper
->children
[1].children
[0].children
[0].a_id
) ||
2517 (oper
->children
[0].children
[0].children
[0].a_id
!=
2518 oper
->children
[2].children
[0].a_id
))
2521 varName
= (const char *) oper
->children
[0].children
[0].children
[0].a_id
;
2523 /* children[3], the loop body, can't be too large */
2524 bodySize
= sizeof_operation(&oper
->children
[3]);
2525 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2526 slang_info_log_print(A
->log
,
2527 "Note: 'for (%s ... )' body is too large/complex"
2533 /* get/check loop iteration limits */
2534 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2535 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2536 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2537 slang_info_log_print(A
->log
,
2538 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2539 " many iterations to unroll",
2540 varName
, start
, varName
, end
, varName
);
2544 return GL_TRUE
; /* we can unroll the loop */
2549 * Unroll a for-loop.
2550 * First we determine the number of iterations to unroll.
2551 * Then for each iteration:
2552 * make a copy of the loop body
2553 * replace instances of the loop variable with the current iteration value
2554 * generate IR code for the body
2555 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2557 static slang_ir_node
*
2558 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2560 GLint start
, end
, iter
;
2561 slang_ir_node
*n
, *root
= NULL
;
2563 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2564 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2566 for (iter
= start
; iter
< end
; iter
++) {
2567 slang_operation
*body
;
2570 /* make a copy of the loop body */
2571 body
= slang_operation_new(1);
2575 if (!slang_operation_copy(body
, &oper
->children
[3]))
2578 id
= oper
->children
[0].children
[0].children
[0].a_id
;
2580 /* in body, replace instances of 'id' with literal 'iter' */
2582 slang_variable
*oldVar
;
2583 slang_operation
*newOper
;
2585 oldVar
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
2587 /* undeclared loop variable */
2588 slang_operation_delete(body
);
2592 newOper
= slang_operation_new(1);
2593 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2594 newOper
->literal_size
= 1;
2595 newOper
->literal
[0] = iter
;
2597 /* replace instances of the loop variable with newOper */
2598 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2601 /* do IR codegen for body */
2602 n
= _slang_gen_operation(A
, body
);
2603 root
= new_seq(root
, n
);
2605 slang_operation_delete(body
);
2613 * Generate IR for a for-loop. Unrolling will be done when possible.
2615 static slang_ir_node
*
2616 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2618 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2621 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2626 /* conventional for-loop code generation */
2629 * init code (child[0])
2631 * BREAK if !expr (child[1])
2632 * body code (child[3])
2634 * incr code (child[2]) // XXX continue here
2636 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2637 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2638 loop
= new_loop(NULL
);
2640 /* save old, push new loop */
2641 prevLoop
= A
->CurLoop
;
2644 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2645 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2646 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2647 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2649 loop
->Children
[0] = new_seq(breakIf
, body
);
2650 loop
->Children
[1] = incr
; /* tail code */
2652 /* pop loop, restore prev */
2653 A
->CurLoop
= prevLoop
;
2655 return new_seq(init
, loop
);
2660 static slang_ir_node
*
2661 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2663 slang_ir_node
*n
, *loopNode
;
2664 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2665 loopNode
= A
->CurLoop
;
2667 assert(loopNode
->Opcode
== IR_LOOP
);
2668 n
= new_node0(IR_CONT
);
2670 n
->Parent
= loopNode
;
2671 /* insert this node at head of linked list */
2672 n
->List
= loopNode
->List
;
2680 * Determine if the given operation is of a specific type.
2683 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2685 if (oper
->type
== type
)
2687 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2688 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2689 oper
->num_children
== 1)
2690 return is_operation_type(&oper
->children
[0], type
);
2697 * Generate IR tree for an if/then/else conditional using high-level
2698 * IR_IF instruction.
2700 static slang_ir_node
*
2701 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2704 * eval expr (child[0])
2711 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2712 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2713 GLboolean isConst
, constTrue
;
2715 /* type-check expression */
2716 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2717 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2721 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2722 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2726 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2730 return _slang_gen_operation(A
, &oper
->children
[1]);
2733 /* if (false) ... */
2734 return _slang_gen_operation(A
, &oper
->children
[2]);
2738 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2739 cond
= new_cond(cond
);
2741 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2742 && !haveElseClause
) {
2743 /* Special case: generate a conditional break */
2744 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2747 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2748 && !haveElseClause
) {
2749 /* Special case: generate a conditional break */
2750 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2755 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2757 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2760 ifNode
= new_if(cond
, ifBody
, elseBody
);
2767 static slang_ir_node
*
2768 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2772 assert(oper
->type
== SLANG_OPER_NOT
);
2774 /* type-check expression */
2775 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2776 slang_info_log_error(A
->log
,
2777 "scalar/boolean expression expected for '!'");
2781 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2789 static slang_ir_node
*
2790 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2792 slang_ir_node
*n1
, *n2
;
2794 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2796 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2797 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2798 slang_info_log_error(A
->log
,
2799 "scalar/boolean expressions expected for '^^'");
2803 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2806 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2809 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2814 * Generate IR node for storage of a temporary of given size.
2816 static slang_ir_node
*
2817 _slang_gen_temporary(GLint size
)
2819 slang_ir_storage
*store
;
2820 slang_ir_node
*n
= NULL
;
2822 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2824 n
= new_node0(IR_VAR_DECL
);
2837 * Generate program constants for an array.
2838 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2839 * This will allocate and initialize three vector constants, storing
2840 * the array in constant memory, not temporaries like a non-const array.
2841 * This can also be used for uniform array initializers.
2842 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2845 make_constant_array(slang_assemble_ctx
*A
,
2846 slang_variable
*var
,
2847 slang_operation
*initializer
)
2849 struct gl_program
*prog
= A
->program
;
2850 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2851 const char *varName
= (char *) var
->a_name
;
2852 const GLuint numElements
= initializer
->num_children
;
2858 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2860 size
= var
->store
->Size
;
2862 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2863 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2864 assert(initializer
->type
== SLANG_OPER_CALL
);
2865 assert(initializer
->array_constructor
);
2867 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2869 /* convert constructor params into ordinary floats */
2870 for (i
= 0; i
< numElements
; i
++) {
2871 const slang_operation
*op
= &initializer
->children
[i
];
2872 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2873 /* unsupported type for this optimization */
2877 for (j
= 0; j
< op
->literal_size
; j
++) {
2878 values
[i
* 4 + j
] = op
->literal
[j
];
2880 for ( ; j
< 4; j
++) {
2881 values
[i
* 4 + j
] = 0.0f
;
2885 /* slightly different paths for constants vs. uniforms */
2886 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2887 var
->store
->File
= PROGRAM_UNIFORM
;
2888 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2889 size
, datatype
, values
);
2892 var
->store
->File
= PROGRAM_CONSTANT
;
2893 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2896 assert(var
->store
->Size
== size
);
2906 * Generate IR node for allocating/declaring a variable (either a local or
2908 * Generally, this involves allocating an slang_ir_storage instance for the
2909 * variable, choosing a register file (temporary, constant, etc).
2910 * For ordinary variables we do not yet allocate storage though. We do that
2911 * when we find the first actual use of the variable to avoid allocating temp
2912 * regs that will never get used.
2913 * At this time, uniforms are always allocated space in this function.
2915 * \param initializer Optional initializer expression for the variable.
2917 static slang_ir_node
*
2918 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2919 slang_operation
*initializer
)
2921 const char *varName
= (const char *) var
->a_name
;
2922 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2923 slang_ir_node
*varDecl
, *n
;
2924 slang_ir_storage
*store
;
2925 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
2926 enum register_file file
;
2928 /*assert(!var->declared);*/
2929 var
->declared
= GL_TRUE
;
2931 /* determine GPU register file for simple cases */
2932 if (is_sampler_type(&var
->type
)) {
2933 file
= PROGRAM_SAMPLER
;
2935 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2936 file
= PROGRAM_UNIFORM
;
2939 file
= PROGRAM_TEMPORARY
;
2942 totalSize
= size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2944 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
2948 arrayLen
= _slang_array_length(var
);
2949 totalSize
= _slang_array_size(size
, arrayLen
);
2951 /* Allocate IR node for the declaration */
2952 varDecl
= new_node0(IR_VAR_DECL
);
2956 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
2958 assert(varDecl
->Store
== var
->store
);
2959 assert(varDecl
->Store
);
2960 assert(varDecl
->Store
->Index
< 0);
2963 assert(store
== varDecl
->Store
);
2966 /* Fill in storage fields which we now know. store->Index/Swizzle may be
2967 * set for some cases below. Otherwise, store->Index/Swizzle will be set
2971 store
->Size
= totalSize
;
2973 /* if there's an initializer, generate IR for the expression */
2975 slang_ir_node
*varRef
, *init
;
2977 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2978 /* if the variable is const, the initializer must be a const
2979 * expression as well.
2982 if (!_slang_is_constant_expr(initializer
)) {
2983 slang_info_log_error(A
->log
,
2984 "initializer for %s not constant", varName
);
2990 /* IR for the variable we're initializing */
2991 varRef
= new_var(A
, var
);
2993 slang_info_log_error(A
->log
, "out of memory");
2997 /* constant-folding, etc here */
2998 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3000 /* look for simple constant-valued variables and uniforms */
3001 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3002 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3004 if (initializer
->type
== SLANG_OPER_CALL
&&
3005 initializer
->array_constructor
) {
3006 /* array initializer */
3007 if (make_constant_array(A
, var
, initializer
))
3010 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3011 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3012 /* simple float/vector initializer */
3013 if (store
->File
== PROGRAM_UNIFORM
) {
3014 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3016 totalSize
, datatype
,
3017 initializer
->literal
);
3018 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3023 store
->File
= PROGRAM_CONSTANT
;
3024 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3026 initializer
->literal
,
3028 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3035 /* IR for initializer */
3036 init
= _slang_gen_operation(A
, initializer
);
3040 /* XXX remove this when type checking is added above */
3041 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3042 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3046 /* assign RHS to LHS */
3047 n
= new_node2(IR_COPY
, varRef
, init
);
3048 n
= new_seq(varDecl
, n
);
3051 /* no initializer */
3055 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3056 /* always need to allocate storage for uniforms at this point */
3057 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3058 totalSize
, datatype
, NULL
);
3059 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3063 printf("%s var %p %s store=%p index=%d size=%d\n",
3064 __FUNCTION__
, (void *) var
, (char *) varName
,
3065 (void *) store
, store
->Index
, store
->Size
);
3073 * Generate code for a selection expression: b ? x : y
3074 * XXX In some cases we could implement a selection expression
3075 * with an LRP instruction (use the boolean as the interpolant).
3076 * Otherwise, we use an IF/ELSE/ENDIF construct.
3078 static slang_ir_node
*
3079 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3081 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3082 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3083 slang_typeinfo type0
, type1
, type2
;
3084 int size
, isBool
, isEqual
;
3086 assert(oper
->type
== SLANG_OPER_SELECT
);
3087 assert(oper
->num_children
== 3);
3089 /* type of children[0] must be boolean */
3090 slang_typeinfo_construct(&type0
);
3091 typeof_operation(A
, &oper
->children
[0], &type0
);
3092 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3093 slang_typeinfo_destruct(&type0
);
3095 slang_info_log_error(A
->log
, "selector type is not boolean");
3099 slang_typeinfo_construct(&type1
);
3100 slang_typeinfo_construct(&type2
);
3101 typeof_operation(A
, &oper
->children
[1], &type1
);
3102 typeof_operation(A
, &oper
->children
[2], &type2
);
3103 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3104 slang_typeinfo_destruct(&type1
);
3105 slang_typeinfo_destruct(&type2
);
3107 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3111 /* size of x or y's type */
3112 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3116 tmpDecl
= _slang_gen_temporary(size
);
3118 /* the condition (child 0) */
3119 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3120 cond
= new_cond(cond
);
3122 /* if-true body (child 1) */
3123 tmpVar
= new_node0(IR_VAR
);
3124 tmpVar
->Store
= tmpDecl
->Store
;
3125 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3126 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3128 /* if-false body (child 2) */
3129 tmpVar
= new_node0(IR_VAR
);
3130 tmpVar
->Store
= tmpDecl
->Store
;
3131 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3132 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3134 ifNode
= new_if(cond
, trueNode
, falseNode
);
3137 tmpVar
= new_node0(IR_VAR
);
3138 tmpVar
->Store
= tmpDecl
->Store
;
3140 tree
= new_seq(ifNode
, tmpVar
);
3141 tree
= new_seq(tmpDecl
, tree
);
3143 /*_slang_print_ir_tree(tree, 10);*/
3149 * Generate code for &&.
3151 static slang_ir_node
*
3152 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3154 /* rewrite "a && b" as "a ? b : false" */
3155 slang_operation
*select
;
3158 select
= slang_operation_new(1);
3159 select
->type
= SLANG_OPER_SELECT
;
3160 select
->num_children
= 3;
3161 select
->children
= slang_operation_new(3);
3163 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3164 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3165 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3166 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3167 select
->children
[2].literal_size
= 1;
3169 n
= _slang_gen_select(A
, select
);
3175 * Generate code for ||.
3177 static slang_ir_node
*
3178 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3180 /* rewrite "a || b" as "a ? true : b" */
3181 slang_operation
*select
;
3184 select
= slang_operation_new(1);
3185 select
->type
= SLANG_OPER_SELECT
;
3186 select
->num_children
= 3;
3187 select
->children
= slang_operation_new(3);
3189 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3190 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3191 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3192 select
->children
[1].literal_size
= 1;
3193 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3195 n
= _slang_gen_select(A
, select
);
3201 * Generate IR tree for a return statement.
3203 static slang_ir_node
*
3204 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3206 const GLboolean haveReturnValue
3207 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3209 /* error checking */
3210 assert(A
->CurFunction
);
3211 if (haveReturnValue
&&
3212 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3213 slang_info_log_error(A
->log
, "illegal return expression");
3216 else if (!haveReturnValue
&&
3217 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3218 slang_info_log_error(A
->log
, "return statement requires an expression");
3222 if (!haveReturnValue
) {
3223 return new_return(A
->curFuncEndLabel
);
3231 * return; // goto __endOfFunction
3233 slang_operation
*assign
;
3234 slang_atom a_retVal
;
3237 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3243 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3245 /* trying to return a value in a void-valued function */
3251 assign
= slang_operation_new(1);
3252 assign
->type
= SLANG_OPER_ASSIGN
;
3253 assign
->num_children
= 2;
3254 assign
->children
= slang_operation_new(2);
3255 /* lhs (__retVal) */
3256 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3257 assign
->children
[0].a_id
= a_retVal
;
3258 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3260 /* XXX we might be able to avoid this copy someday */
3261 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3263 /* assemble the new code */
3264 n
= new_seq(_slang_gen_operation(A
, assign
),
3265 new_return(A
->curFuncEndLabel
));
3267 slang_operation_delete(assign
);
3274 * Determine if the given operation/expression is const-valued.
3277 _slang_is_constant_expr(const slang_operation
*oper
)
3279 slang_variable
*var
;
3282 switch (oper
->type
) {
3283 case SLANG_OPER_IDENTIFIER
:
3284 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3285 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3289 for (i
= 0; i
< oper
->num_children
; i
++) {
3290 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3299 * Check if an assignment of type t1 to t0 is legal.
3300 * XXX more cases needed.
3303 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3304 slang_operation
*op0
,
3305 slang_operation
*op1
)
3307 slang_typeinfo t0
, t1
;
3310 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3311 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3315 slang_typeinfo_construct(&t0
);
3316 typeof_operation(A
, op0
, &t0
);
3318 slang_typeinfo_construct(&t1
);
3319 typeof_operation(A
, op1
, &t1
);
3321 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3322 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3326 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3331 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3332 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3333 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3336 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3337 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3340 #if 0 /* not used just yet - causes problems elsewhere */
3341 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3342 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3346 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3347 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3350 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3351 t1
.spec
.type
== SLANG_SPEC_INT
)
3359 * Generate IR tree for a local variable declaration.
3360 * Basically do some error checking and call _slang_gen_var_decl().
3362 static slang_ir_node
*
3363 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3365 const char *varName
= (char *) oper
->a_id
;
3366 slang_variable
*var
;
3367 slang_ir_node
*varDecl
;
3368 slang_operation
*initializer
;
3370 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3371 assert(oper
->num_children
<= 1);
3373 /* lookup the variable by name */
3374 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3376 return NULL
; /* "shouldn't happen" */
3378 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3379 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3380 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3381 /* can't declare attribute/uniform vars inside functions */
3382 slang_info_log_error(A
->log
,
3383 "local variable '%s' cannot be an attribute/uniform/varying",
3390 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3395 /* check if the var has an initializer */
3396 if (oper
->num_children
> 0) {
3397 assert(oper
->num_children
== 1);
3398 initializer
= &oper
->children
[0];
3400 else if (var
->initializer
) {
3401 initializer
= var
->initializer
;
3408 /* check/compare var type and initializer type */
3409 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3410 slang_info_log_error(A
->log
, "incompatible types in assignment");
3415 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3416 slang_info_log_error(A
->log
,
3417 "const-qualified variable '%s' requires initializer",
3423 /* Generate IR node */
3424 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3433 * Generate IR tree for a reference to a variable (such as in an expression).
3434 * This is different from a variable declaration.
3436 static slang_ir_node
*
3437 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3439 /* If there's a variable associated with this oper (from inlining)
3440 * use it. Otherwise, use the oper's var id.
3442 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3443 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3446 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3449 assert(var
->declared
);
3450 n
= new_var(A
, var
);
3457 * Return the number of components actually named by the swizzle.
3458 * Recall that swizzles may have undefined/don't-care values.
3461 swizzle_size(GLuint swizzle
)
3464 for (i
= 0; i
< 4; i
++) {
3465 GLuint swz
= GET_SWZ(swizzle
, i
);
3466 size
+= (swz
>= 0 && swz
<= 3);
3472 static slang_ir_node
*
3473 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3475 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3479 n
->Store
= _slang_new_ir_storage_relative(0,
3480 swizzle_size(swizzle
),
3482 n
->Store
->Swizzle
= swizzle
;
3489 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3491 while (store
->Parent
)
3492 store
= store
->Parent
;
3494 if (!(store
->File
== PROGRAM_OUTPUT
||
3495 store
->File
== PROGRAM_TEMPORARY
||
3496 (store
->File
== PROGRAM_VARYING
&&
3497 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3507 * Walk up an IR storage path to compute the final swizzle.
3508 * This is used when we find an expression such as "foo.xz.yx".
3511 root_swizzle(const slang_ir_storage
*st
)
3513 GLuint swizzle
= st
->Swizzle
;
3514 while (st
->Parent
) {
3516 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3523 * Generate IR tree for an assignment (=).
3525 static slang_ir_node
*
3526 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3528 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3529 /* Check that var is writeable */
3531 = _slang_variable_locate(oper
->children
[0].locals
,
3532 oper
->children
[0].a_id
, GL_TRUE
);
3534 slang_info_log_error(A
->log
, "undefined variable '%s'",
3535 (char *) oper
->children
[0].a_id
);
3538 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3539 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3540 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3541 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3542 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3543 slang_info_log_error(A
->log
,
3544 "illegal assignment to read-only variable '%s'",
3545 (char *) oper
->children
[0].a_id
);
3550 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3551 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3552 /* Special case of: x = f(a, b)
3553 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3555 * XXX this could be even more effective if we could accomodate
3556 * cases such as "v.x = f();" - would help with typical vertex
3560 n
= _slang_gen_function_call_name(A
,
3561 (const char *) oper
->children
[1].a_id
,
3562 &oper
->children
[1], &oper
->children
[0]);
3566 slang_ir_node
*n
, *lhs
, *rhs
;
3568 /* lhs and rhs type checking */
3569 if (!_slang_assignment_compatible(A
,
3571 &oper
->children
[1])) {
3572 slang_info_log_error(A
->log
, "incompatible types in assignment");
3576 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3582 slang_info_log_error(A
->log
,
3583 "invalid left hand side for assignment");
3587 /* check that lhs is writable */
3588 if (!is_store_writable(A
, lhs
->Store
)) {
3589 slang_info_log_error(A
->log
,
3590 "illegal assignment to read-only l-value");
3594 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3596 /* convert lhs swizzle into writemask */
3597 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3598 GLuint writemask
, newSwizzle
;
3599 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3600 /* Non-simple writemask, need to swizzle right hand side in
3601 * order to put components into the right place.
3603 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3605 n
= new_node2(IR_COPY
, lhs
, rhs
);
3616 * Generate IR tree for referencing a field in a struct (or basic vector type)
3618 static slang_ir_node
*
3619 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3623 /* type of struct */
3624 slang_typeinfo_construct(&ti
);
3625 typeof_operation(A
, &oper
->children
[0], &ti
);
3627 if (_slang_type_is_vector(ti
.spec
.type
)) {
3628 /* the field should be a swizzle */
3629 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3633 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3634 slang_info_log_error(A
->log
, "Bad swizzle");
3637 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3642 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3643 /* create new parent node with swizzle */
3645 n
= _slang_gen_swizzle(n
, swizzle
);
3648 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3649 || ti
.spec
.type
== SLANG_SPEC_INT
3650 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3651 const GLuint rows
= 1;
3655 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3656 slang_info_log_error(A
->log
, "Bad swizzle");
3658 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3662 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3663 /* create new parent node with swizzle */
3664 n
= _slang_gen_swizzle(n
, swizzle
);
3668 /* the field is a structure member (base.field) */
3669 /* oper->children[0] is the base */
3670 /* oper->a_id is the field name */
3671 slang_ir_node
*base
, *n
;
3672 slang_typeinfo field_ti
;
3673 GLint fieldSize
, fieldOffset
= -1;
3676 slang_typeinfo_construct(&field_ti
);
3677 typeof_operation(A
, oper
, &field_ti
);
3679 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3681 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3683 if (fieldSize
== 0 || fieldOffset
< 0) {
3684 const char *structName
;
3685 if (ti
.spec
._struct
)
3686 structName
= (char *) ti
.spec
._struct
->a_name
;
3688 structName
= "unknown";
3689 slang_info_log_error(A
->log
,
3690 "\"%s\" is not a member of struct \"%s\"",
3691 (char *) oper
->a_id
, structName
);
3694 assert(fieldSize
>= 0);
3696 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3698 /* error msg should have already been logged */
3702 n
= new_node1(IR_FIELD
, base
);
3706 n
->Field
= (char *) oper
->a_id
;
3708 /* Store the field's offset in storage->Index */
3709 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3719 * Gen code for array indexing.
3721 static slang_ir_node
*
3722 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3724 slang_typeinfo array_ti
;
3726 /* get array's type info */
3727 slang_typeinfo_construct(&array_ti
);
3728 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3730 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3731 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3732 /* translate the index into a swizzle/writemask: "v.x=p" */
3733 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3737 index
= (GLint
) oper
->children
[1].literal
[0];
3738 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3739 index
>= (GLint
) max
) {
3741 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3742 printf("type = %d\n", oper
->children
[1].type
);
3743 printf("index = %d, max = %d\n", index
, max
);
3744 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3745 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3752 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3754 /* use swizzle to access the element */
3755 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3759 n
= _slang_gen_swizzle(n
, swizzle
);
3765 /* conventional array */
3766 slang_typeinfo elem_ti
;
3767 slang_ir_node
*elem
, *array
, *index
;
3768 GLint elemSize
, arrayLen
;
3770 /* size of array element */
3771 slang_typeinfo_construct(&elem_ti
);
3772 typeof_operation(A
, oper
, &elem_ti
);
3773 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3775 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3776 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3778 arrayLen
= array_ti
.array_len
;
3780 slang_typeinfo_destruct(&array_ti
);
3781 slang_typeinfo_destruct(&elem_ti
);
3783 if (elemSize
<= 0) {
3784 /* unknown var or type */
3785 slang_info_log_error(A
->log
, "Undefined variable or type");
3789 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3790 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3791 if (array
&& index
) {
3793 GLint constIndex
= -1;
3794 if (index
->Opcode
== IR_FLOAT
) {
3795 constIndex
= (int) index
->Value
[0];
3796 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3797 slang_info_log_error(A
->log
,
3798 "Array index out of bounds (index=%d size=%d)",
3799 constIndex
, arrayLen
);
3800 _slang_free_ir_tree(array
);
3801 _slang_free_ir_tree(index
);
3806 if (!array
->Store
) {
3807 slang_info_log_error(A
->log
, "Invalid array");
3811 elem
= new_node2(IR_ELEMENT
, array
, index
);
3813 /* The storage info here will be updated during code emit */
3814 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3815 array
->Store
->Index
,
3817 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3821 _slang_free_ir_tree(array
);
3822 _slang_free_ir_tree(index
);
3829 static slang_ir_node
*
3830 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3831 slang_ir_opcode opcode
)
3833 slang_typeinfo t0
, t1
;
3836 slang_typeinfo_construct(&t0
);
3837 typeof_operation(A
, &oper
->children
[0], &t0
);
3839 slang_typeinfo_construct(&t1
);
3840 typeof_operation(A
, &oper
->children
[0], &t1
);
3842 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3843 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3844 slang_info_log_error(A
->log
, "Illegal array comparison");
3848 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3849 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3850 /* <, <=, >, >= can only be used with scalars */
3851 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3852 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3853 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3854 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3855 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3860 n
= new_node2(opcode
,
3861 _slang_gen_operation(A
, &oper
->children
[0]),
3862 _slang_gen_operation(A
, &oper
->children
[1]));
3864 /* result is a bool (size 1) */
3865 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3873 print_vars(slang_variable_scope
*s
)
3877 for (i
= 0; i
< s
->num_variables
; i
++) {
3879 (char*) s
->variables
[i
]->a_name
,
3880 s
->variables
[i
]->declared
);
3890 _slang_undeclare_vars(slang_variable_scope
*locals
)
3892 if (locals
->num_variables
> 0) {
3894 for (i
= 0; i
< locals
->num_variables
; i
++) {
3895 slang_variable
*v
= locals
->variables
[i
];
3896 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3897 v
->declared
= GL_FALSE
;
3905 * Generate IR tree for a slang_operation (AST node)
3907 static slang_ir_node
*
3908 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3910 switch (oper
->type
) {
3911 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3915 _slang_push_var_table(A
->vartable
);
3917 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3918 n
= _slang_gen_operation(A
, oper
);
3919 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3921 _slang_pop_var_table(A
->vartable
);
3923 /*_slang_undeclare_vars(oper->locals);*/
3924 /*print_vars(oper->locals);*/
3927 n
= new_node1(IR_SCOPE
, n
);
3932 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3933 /* list of operations */
3934 if (oper
->num_children
> 0)
3936 slang_ir_node
*n
, *tree
= NULL
;
3939 for (i
= 0; i
< oper
->num_children
; i
++) {
3940 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3942 _slang_free_ir_tree(tree
);
3943 return NULL
; /* error must have occured */
3945 tree
= new_seq(tree
, n
);
3951 return new_node0(IR_NOP
);
3954 case SLANG_OPER_EXPRESSION
:
3955 return _slang_gen_operation(A
, &oper
->children
[0]);
3957 case SLANG_OPER_FOR
:
3958 return _slang_gen_for(A
, oper
);
3960 return _slang_gen_do(A
, oper
);
3961 case SLANG_OPER_WHILE
:
3962 return _slang_gen_while(A
, oper
);
3963 case SLANG_OPER_BREAK
:
3965 slang_info_log_error(A
->log
, "'break' not in loop");
3968 return new_break(A
->CurLoop
);
3969 case SLANG_OPER_CONTINUE
:
3971 slang_info_log_error(A
->log
, "'continue' not in loop");
3974 return _slang_gen_continue(A
, oper
);
3975 case SLANG_OPER_DISCARD
:
3976 return new_node0(IR_KILL
);
3978 case SLANG_OPER_EQUAL
:
3979 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3980 case SLANG_OPER_NOTEQUAL
:
3981 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3982 case SLANG_OPER_GREATER
:
3983 return _slang_gen_compare(A
, oper
, IR_SGT
);
3984 case SLANG_OPER_LESS
:
3985 return _slang_gen_compare(A
, oper
, IR_SLT
);
3986 case SLANG_OPER_GREATEREQUAL
:
3987 return _slang_gen_compare(A
, oper
, IR_SGE
);
3988 case SLANG_OPER_LESSEQUAL
:
3989 return _slang_gen_compare(A
, oper
, IR_SLE
);
3990 case SLANG_OPER_ADD
:
3993 assert(oper
->num_children
== 2);
3994 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3997 case SLANG_OPER_SUBTRACT
:
4000 assert(oper
->num_children
== 2);
4001 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4004 case SLANG_OPER_MULTIPLY
:
4007 assert(oper
->num_children
== 2);
4008 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4011 case SLANG_OPER_DIVIDE
:
4014 assert(oper
->num_children
== 2);
4015 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4018 case SLANG_OPER_MINUS
:
4021 assert(oper
->num_children
== 1);
4022 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4025 case SLANG_OPER_PLUS
:
4026 /* +expr --> do nothing */
4027 return _slang_gen_operation(A
, &oper
->children
[0]);
4028 case SLANG_OPER_VARIABLE_DECL
:
4029 return _slang_gen_declaration(A
, oper
);
4030 case SLANG_OPER_ASSIGN
:
4031 return _slang_gen_assignment(A
, oper
);
4032 case SLANG_OPER_ADDASSIGN
:
4035 assert(oper
->num_children
== 2);
4036 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4039 case SLANG_OPER_SUBASSIGN
:
4042 assert(oper
->num_children
== 2);
4043 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4047 case SLANG_OPER_MULASSIGN
:
4050 assert(oper
->num_children
== 2);
4051 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4054 case SLANG_OPER_DIVASSIGN
:
4057 assert(oper
->num_children
== 2);
4058 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4061 case SLANG_OPER_LOGICALAND
:
4064 assert(oper
->num_children
== 2);
4065 n
= _slang_gen_logical_and(A
, oper
);
4068 case SLANG_OPER_LOGICALOR
:
4071 assert(oper
->num_children
== 2);
4072 n
= _slang_gen_logical_or(A
, oper
);
4075 case SLANG_OPER_LOGICALXOR
:
4076 return _slang_gen_xor(A
, oper
);
4077 case SLANG_OPER_NOT
:
4078 return _slang_gen_not(A
, oper
);
4079 case SLANG_OPER_SELECT
: /* b ? x : y */
4082 assert(oper
->num_children
== 3);
4083 n
= _slang_gen_select(A
, oper
);
4087 case SLANG_OPER_ASM
:
4088 return _slang_gen_asm(A
, oper
, NULL
);
4089 case SLANG_OPER_CALL
:
4090 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4092 case SLANG_OPER_METHOD
:
4093 return _slang_gen_method_call(A
, oper
);
4094 case SLANG_OPER_RETURN
:
4095 return _slang_gen_return(A
, oper
);
4096 case SLANG_OPER_LABEL
:
4097 return new_label(oper
->label
);
4098 case SLANG_OPER_IDENTIFIER
:
4099 return _slang_gen_variable(A
, oper
);
4101 return _slang_gen_if(A
, oper
);
4102 case SLANG_OPER_FIELD
:
4103 return _slang_gen_struct_field(A
, oper
);
4104 case SLANG_OPER_SUBSCRIPT
:
4105 return _slang_gen_array_element(A
, oper
);
4106 case SLANG_OPER_LITERAL_FLOAT
:
4108 case SLANG_OPER_LITERAL_INT
:
4110 case SLANG_OPER_LITERAL_BOOL
:
4111 return new_float_literal(oper
->literal
, oper
->literal_size
);
4113 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4116 assert(oper
->num_children
== 1);
4117 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4120 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4123 assert(oper
->num_children
== 1);
4124 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4127 case SLANG_OPER_PREINCREMENT
: /* ++var */
4130 assert(oper
->num_children
== 1);
4131 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4134 case SLANG_OPER_PREDECREMENT
: /* --var */
4137 assert(oper
->num_children
== 1);
4138 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4142 case SLANG_OPER_NON_INLINED_CALL
:
4143 case SLANG_OPER_SEQUENCE
:
4145 slang_ir_node
*tree
= NULL
;
4147 for (i
= 0; i
< oper
->num_children
; i
++) {
4148 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4149 tree
= new_seq(tree
, n
);
4151 tree
->Store
= n
->Store
;
4153 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4154 tree
= new_function_call(tree
, oper
->label
);
4159 case SLANG_OPER_NONE
:
4160 case SLANG_OPER_VOID
:
4161 /* returning NULL here would generate an error */
4162 return new_node0(IR_NOP
);
4165 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4167 return new_node0(IR_NOP
);
4175 * Called by compiler when a global variable has been parsed/compiled.
4176 * Here we examine the variable's type to determine what kind of register
4177 * storage will be used.
4179 * A uniform such as "gl_Position" will become the register specification
4180 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4181 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4183 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4184 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4185 * actual texture unit (as specified by the user calling glUniform1i()).
4188 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4189 slang_unit_type type
)
4191 struct gl_program
*prog
= A
->program
;
4192 const char *varName
= (char *) var
->a_name
;
4193 GLboolean success
= GL_TRUE
;
4194 slang_ir_storage
*store
= NULL
;
4196 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4197 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4198 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4199 const GLint arrayLen
= _slang_array_length(var
);
4200 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4202 if (texIndex
!= -1) {
4203 /* This is a texture sampler variable...
4204 * store->File = PROGRAM_SAMPLER
4205 * store->Index = sampler number (0..7, typically)
4206 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4208 if (var
->initializer
) {
4209 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4212 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4213 /* disallow rect samplers */
4214 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4215 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
4216 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4221 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4222 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4224 if (dbg
) printf("SAMPLER ");
4226 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4227 /* Uniform variable */
4228 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4231 /* user-defined uniform */
4232 if (datatype
== GL_NONE
) {
4233 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4234 /* temporary work-around */
4235 GLenum datatype
= GL_FLOAT
;
4236 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4237 totalSize
, datatype
, NULL
);
4238 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4239 totalSize
, swizzle
);
4241 /* XXX what we need to do is unroll the struct into its
4242 * basic types, creating a uniform variable for each.
4250 * Should produce uniforms:
4251 * "f.a" (GL_FLOAT_VEC3)
4252 * "f.b" (GL_FLOAT_VEC4)
4255 if (var
->initializer
) {
4256 slang_info_log_error(A
->log
,
4257 "unsupported initializer for uniform '%s'", varName
);
4262 slang_info_log_error(A
->log
,
4263 "invalid datatype for uniform variable %s",
4269 /* non-struct uniform */
4270 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4276 /* pre-defined uniform, like gl_ModelviewMatrix */
4277 /* We know it's a uniform, but don't allocate storage unless
4280 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4281 totalSize
, swizzle
);
4283 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4285 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4286 /* varyings must be float, vec or mat */
4287 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4288 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4289 slang_info_log_error(A
->log
,
4290 "varying '%s' must be float/vector/matrix",
4295 if (var
->initializer
) {
4296 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4302 /* user-defined varying */
4308 if (var
->type
.centroid
== SLANG_CENTROID
)
4309 flags
|= PROG_PARAM_BIT_CENTROID
;
4310 if (var
->type
.variant
== SLANG_INVARIANT
)
4311 flags
|= PROG_PARAM_BIT_INVARIANT
;
4313 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4315 swizzle
= _slang_var_swizzle(size
, 0);
4316 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4317 totalSize
, swizzle
);
4320 /* pre-defined varying, like gl_Color or gl_TexCoord */
4321 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4322 /* fragment program input */
4324 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4327 assert(index
< FRAG_ATTRIB_MAX
);
4328 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4332 /* vertex program output */
4333 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4334 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4336 assert(index
< VERT_RESULT_MAX
);
4337 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4338 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4341 if (dbg
) printf("V/F ");
4343 if (dbg
) printf("VARYING ");
4345 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4348 /* attributes must be float, vec or mat */
4349 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4350 slang_info_log_error(A
->log
,
4351 "attribute '%s' must be float/vector/matrix",
4357 /* user-defined vertex attribute */
4358 const GLint attr
= -1; /* unknown */
4359 swizzle
= _slang_var_swizzle(size
, 0);
4360 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4361 size
, datatype
, attr
);
4363 index
= VERT_ATTRIB_GENERIC0
+ index
;
4366 /* pre-defined vertex attrib */
4367 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4370 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4371 if (dbg
) printf("ATTRIB ");
4373 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4374 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4375 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4377 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4378 if (dbg
) printf("INPUT ");
4380 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4381 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4382 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4383 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4386 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4387 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4388 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4389 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4391 if (dbg
) printf("OUTPUT ");
4393 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4394 /* pre-defined global constant, like gl_MaxLights */
4395 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4396 if (dbg
) printf("CONST ");
4399 /* ordinary variable (may be const) */
4402 /* IR node to declare the variable */
4403 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4405 /* emit GPU instructions */
4406 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4408 _slang_free_ir_tree(n
);
4411 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4412 store
? store
->Index
: -2);
4415 var
->store
= store
; /* save var's storage info */
4417 var
->declared
= GL_TRUE
;
4424 * Produce an IR tree from a function AST (fun->body).
4425 * Then call the code emitter to convert the IR tree into gl_program
4429 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4432 GLboolean success
= GL_TRUE
;
4434 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4435 /* we only really generate code for main, all other functions get
4436 * inlined or codegen'd upon an actual call.
4439 /* do some basic error checking though */
4440 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4441 /* check that non-void functions actually return something */
4443 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4445 slang_info_log_error(A
->log
,
4446 "function \"%s\" has no return statement",
4447 (char *) fun
->header
.a_name
);
4449 "function \"%s\" has no return statement\n",
4450 (char *) fun
->header
.a_name
);
4455 return GL_TRUE
; /* not an error */
4459 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4460 slang_print_function(fun
, 1);
4463 /* should have been allocated earlier: */
4464 assert(A
->program
->Parameters
);
4465 assert(A
->program
->Varying
);
4466 assert(A
->vartable
);
4468 A
->CurFunction
= fun
;
4470 /* fold constant expressions, etc. */
4471 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4474 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4475 slang_print_function(fun
, 1);
4478 /* Create an end-of-function label */
4479 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4481 /* push new vartable scope */
4482 _slang_push_var_table(A
->vartable
);
4484 /* Generate IR tree for the function body code */
4485 n
= _slang_gen_operation(A
, fun
->body
);
4487 n
= new_node1(IR_SCOPE
, n
);
4489 /* pop vartable, restore previous */
4490 _slang_pop_var_table(A
->vartable
);
4493 /* XXX record error */
4497 /* append an end-of-function-label to IR tree */
4498 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4500 /*_slang_label_delete(A->curFuncEndLabel);*/
4501 A
->curFuncEndLabel
= NULL
;
4504 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4505 slang_print_function(fun
, 1);
4508 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4509 _slang_print_ir_tree(n
, 0);
4512 printf("************* End codegen function ************\n\n");
4515 /* Emit program instructions */
4516 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4517 _slang_free_ir_tree(n
);
4519 /* free codegen context */
4521 _mesa_free(A->codegen);