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
= 32;
63 /** Max for-loop body size (in slang operations) to unroll */
64 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
66 /** Max for-loop body complexity to unroll.
67 * We'll compute complexity as the product of the number of iterations
68 * and the size of the body. So long-ish loops with very simple bodies
69 * can be unrolled, as well as short loops with larger bodies.
71 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY
= 256;
75 static slang_ir_node
*
76 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
80 * Retrieves type information about an operation.
81 * Returns GL_TRUE on success.
82 * Returns GL_FALSE otherwise.
85 typeof_operation(const struct slang_assemble_ctx_
*A
,
89 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
94 is_sampler_type(const slang_fully_specified_type
*t
)
96 switch (t
->specifier
.type
) {
97 case SLANG_SPEC_SAMPLER1D
:
98 case SLANG_SPEC_SAMPLER2D
:
99 case SLANG_SPEC_SAMPLER3D
:
100 case SLANG_SPEC_SAMPLERCUBE
:
101 case SLANG_SPEC_SAMPLER1DSHADOW
:
102 case SLANG_SPEC_SAMPLER2DSHADOW
:
103 case SLANG_SPEC_SAMPLER2DRECT
:
104 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
113 * Return the offset (in floats or ints) of the named field within
114 * the given struct. Return -1 if field not found.
115 * If field is NULL, return the size of the struct instead.
118 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
122 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
123 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
124 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
126 /* types larger than 1 float are register (4-float) aligned */
127 offset
= (offset
+ 3) & ~3;
129 if (field
&& v
->a_name
== field
) {
135 return -1; /* field not found */
137 return offset
; /* struct size */
142 * Return the size (in floats) of the given type specifier.
143 * If the size is greater than 4, the size should be a multiple of 4
144 * so that the correct number of 4-float registers are allocated.
145 * For example, a mat3x2 is size 12 because we want to store the
146 * 3 columns in 3 float[4] registers.
149 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
152 switch (spec
->type
) {
153 case SLANG_SPEC_VOID
:
156 case SLANG_SPEC_BOOL
:
159 case SLANG_SPEC_BVEC2
:
162 case SLANG_SPEC_BVEC3
:
165 case SLANG_SPEC_BVEC4
:
171 case SLANG_SPEC_IVEC2
:
174 case SLANG_SPEC_IVEC3
:
177 case SLANG_SPEC_IVEC4
:
180 case SLANG_SPEC_FLOAT
:
183 case SLANG_SPEC_VEC2
:
186 case SLANG_SPEC_VEC3
:
189 case SLANG_SPEC_VEC4
:
192 case SLANG_SPEC_MAT2
:
193 sz
= 2 * 4; /* 2 columns (regs) */
195 case SLANG_SPEC_MAT3
:
198 case SLANG_SPEC_MAT4
:
201 case SLANG_SPEC_MAT23
:
202 sz
= 2 * 4; /* 2 columns (regs) */
204 case SLANG_SPEC_MAT32
:
205 sz
= 3 * 4; /* 3 columns (regs) */
207 case SLANG_SPEC_MAT24
:
210 case SLANG_SPEC_MAT42
:
211 sz
= 4 * 4; /* 4 columns (regs) */
213 case SLANG_SPEC_MAT34
:
216 case SLANG_SPEC_MAT43
:
217 sz
= 4 * 4; /* 4 columns (regs) */
219 case SLANG_SPEC_SAMPLER1D
:
220 case SLANG_SPEC_SAMPLER2D
:
221 case SLANG_SPEC_SAMPLER3D
:
222 case SLANG_SPEC_SAMPLERCUBE
:
223 case SLANG_SPEC_SAMPLER1DSHADOW
:
224 case SLANG_SPEC_SAMPLER2DSHADOW
:
225 case SLANG_SPEC_SAMPLER2DRECT
:
226 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
227 sz
= 1; /* a sampler is basically just an integer index */
229 case SLANG_SPEC_STRUCT
:
230 sz
= _slang_field_offset(spec
, 0); /* special use */
232 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
235 case SLANG_SPEC_ARRAY
:
236 sz
= _slang_sizeof_type_specifier(spec
->_array
);
239 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
244 /* if size is > 4, it should be a multiple of four */
245 assert((sz
& 0x3) == 0);
252 * Query variable/array length (number of elements).
253 * This is slightly non-trivial because there are two ways to express
254 * arrays: "float x[3]" vs. "float[3] x".
255 * \return the length of the array for the given variable, or 0 if not an array
258 _slang_array_length(const slang_variable
*var
)
260 if (var
->type
.array_len
> 0) {
261 /* Ex: float[4] x; */
262 return var
->type
.array_len
;
264 if (var
->array_len
> 0) {
265 /* Ex: float x[4]; */
266 return var
->array_len
;
273 * Compute total size of array give size of element, number of elements.
274 * \return size in floats
277 _slang_array_size(GLint elemSize
, GLint arrayLen
)
280 assert(elemSize
> 0);
282 /* round up base type to multiple of 4 */
283 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
294 * Establish the binding between a slang_ir_node and a slang_variable.
295 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
296 * The IR node must be a IR_VAR or IR_VAR_DECL node.
297 * \param n the IR node
298 * \param var the variable to associate with the IR node
301 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
305 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
306 assert(!n
->Var
|| n
->Var
== var
);
311 /* need to setup storage */
312 if (n
->Var
&& n
->Var
->store
) {
313 /* node storage info = var storage info */
314 n
->Store
= n
->Var
->store
;
317 /* alloc new storage info */
318 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
320 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
322 (void*) n
->Store
, n
->Store
->Size
);
325 n
->Var
->store
= n
->Store
;
326 assert(n
->Var
->store
);
333 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
334 * or -1 if the type is not a sampler.
337 sampler_to_texture_index(const slang_type_specifier_type type
)
340 case SLANG_SPEC_SAMPLER1D
:
341 return TEXTURE_1D_INDEX
;
342 case SLANG_SPEC_SAMPLER2D
:
343 return TEXTURE_2D_INDEX
;
344 case SLANG_SPEC_SAMPLER3D
:
345 return TEXTURE_3D_INDEX
;
346 case SLANG_SPEC_SAMPLERCUBE
:
347 return TEXTURE_CUBE_INDEX
;
348 case SLANG_SPEC_SAMPLER1DSHADOW
:
349 return TEXTURE_1D_INDEX
; /* XXX fix */
350 case SLANG_SPEC_SAMPLER2DSHADOW
:
351 return TEXTURE_2D_INDEX
; /* XXX fix */
352 case SLANG_SPEC_SAMPLER2DRECT
:
353 return TEXTURE_RECT_INDEX
;
354 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
355 return TEXTURE_RECT_INDEX
; /* XXX fix */
362 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
365 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
366 * a vertex or fragment program input variable. Return -1 if the input
368 * XXX return size too
371 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
378 static const struct input_info vertInputs
[] = {
379 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
380 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
381 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
382 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
383 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
384 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
385 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
386 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
387 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
388 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
389 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
390 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
391 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
392 { NULL
, 0, SWIZZLE_NOOP
}
394 static const struct input_info fragInputs
[] = {
395 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
396 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
397 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
398 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
399 /* note: we're packing several quantities into the fogcoord vector */
400 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
401 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
402 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
403 { NULL
, 0, SWIZZLE_NOOP
}
406 const struct input_info
*inputs
407 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
409 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
411 for (i
= 0; inputs
[i
].Name
; i
++) {
412 if (strcmp(inputs
[i
].Name
, name
) == 0) {
414 *swizzleOut
= inputs
[i
].Swizzle
;
415 return inputs
[i
].Attrib
;
423 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
424 * a vertex or fragment program output variable. Return -1 for an invalid
428 _slang_output_index(const char *name
, GLenum target
)
434 static const struct output_info vertOutputs
[] = {
435 { "gl_Position", VERT_RESULT_HPOS
},
436 { "gl_FrontColor", VERT_RESULT_COL0
},
437 { "gl_BackColor", VERT_RESULT_BFC0
},
438 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
439 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
440 { "gl_TexCoord", VERT_RESULT_TEX0
},
441 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
442 { "gl_PointSize", VERT_RESULT_PSIZ
},
445 static const struct output_info fragOutputs
[] = {
446 { "gl_FragColor", FRAG_RESULT_COLR
},
447 { "gl_FragDepth", FRAG_RESULT_DEPR
},
448 { "gl_FragData", FRAG_RESULT_DATA0
},
452 const struct output_info
*outputs
453 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
455 for (i
= 0; outputs
[i
].Name
; i
++) {
456 if (strcmp(outputs
[i
].Name
, name
) == 0) {
458 return outputs
[i
].Attrib
;
466 /**********************************************************************/
470 * Map "_asm foo" to IR_FOO, etc.
475 slang_ir_opcode Opcode
;
476 GLuint HaveRetValue
, NumParams
;
480 static slang_asm_info AsmInfo
[] = {
482 { "vec4_add", IR_ADD
, 1, 2 },
483 { "vec4_subtract", IR_SUB
, 1, 2 },
484 { "vec4_multiply", IR_MUL
, 1, 2 },
485 { "vec4_dot", IR_DOT4
, 1, 2 },
486 { "vec3_dot", IR_DOT3
, 1, 2 },
487 { "vec2_dot", IR_DOT2
, 1, 2 },
488 { "vec3_nrm", IR_NRM3
, 1, 1 },
489 { "vec4_nrm", IR_NRM4
, 1, 1 },
490 { "vec3_cross", IR_CROSS
, 1, 2 },
491 { "vec4_lrp", IR_LRP
, 1, 3 },
492 { "vec4_min", IR_MIN
, 1, 2 },
493 { "vec4_max", IR_MAX
, 1, 2 },
494 { "vec4_clamp", IR_CLAMP
, 1, 3 },
495 { "vec4_seq", IR_SEQUAL
, 1, 2 },
496 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
497 { "vec4_sge", IR_SGE
, 1, 2 },
498 { "vec4_sgt", IR_SGT
, 1, 2 },
499 { "vec4_sle", IR_SLE
, 1, 2 },
500 { "vec4_slt", IR_SLT
, 1, 2 },
502 { "vec4_move", IR_MOVE
, 1, 1 },
503 { "vec4_floor", IR_FLOOR
, 1, 1 },
504 { "vec4_frac", IR_FRAC
, 1, 1 },
505 { "vec4_abs", IR_ABS
, 1, 1 },
506 { "vec4_negate", IR_NEG
, 1, 1 },
507 { "vec4_ddx", IR_DDX
, 1, 1 },
508 { "vec4_ddy", IR_DDY
, 1, 1 },
509 /* float binary op */
510 { "float_power", IR_POW
, 1, 2 },
511 /* texture / sampler */
512 { "vec4_tex1d", IR_TEX
, 1, 2 },
513 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
514 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
515 { "vec4_tex2d", IR_TEX
, 1, 2 },
516 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
517 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
518 { "vec4_tex3d", IR_TEX
, 1, 2 },
519 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
520 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
521 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
522 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
523 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
526 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
527 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
528 { "float_exp", IR_EXP
, 1, 1 },
529 { "float_exp2", IR_EXP2
, 1, 1 },
530 { "float_log2", IR_LOG2
, 1, 1 },
531 { "float_rsq", IR_RSQ
, 1, 1 },
532 { "float_rcp", IR_RCP
, 1, 1 },
533 { "float_sine", IR_SIN
, 1, 1 },
534 { "float_cosine", IR_COS
, 1, 1 },
535 { "float_noise1", IR_NOISE1
, 1, 1},
536 { "float_noise2", IR_NOISE2
, 1, 1},
537 { "float_noise3", IR_NOISE3
, 1, 1},
538 { "float_noise4", IR_NOISE4
, 1, 1},
540 { NULL
, IR_NOP
, 0, 0 }
544 static slang_ir_node
*
545 new_node3(slang_ir_opcode op
,
546 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
548 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
554 n
->InstLocation
= -1;
559 static slang_ir_node
*
560 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
562 return new_node3(op
, c0
, c1
, NULL
);
565 static slang_ir_node
*
566 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
568 return new_node3(op
, c0
, NULL
, NULL
);
571 static slang_ir_node
*
572 new_node0(slang_ir_opcode op
)
574 return new_node3(op
, NULL
, NULL
, NULL
);
579 * Create sequence of two nodes.
581 static slang_ir_node
*
582 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
588 return new_node2(IR_SEQ
, left
, right
);
591 static slang_ir_node
*
592 new_label(slang_label
*label
)
594 slang_ir_node
*n
= new_node0(IR_LABEL
);
601 static slang_ir_node
*
602 new_float_literal(const float v
[4], GLuint size
)
604 slang_ir_node
*n
= new_node0(IR_FLOAT
);
606 COPY_4V(n
->Value
, v
);
607 /* allocate a storage object, but compute actual location (Index) later */
608 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
613 static slang_ir_node
*
614 new_not(slang_ir_node
*n
)
616 return new_node1(IR_NOT
, n
);
621 * Non-inlined function call.
623 static slang_ir_node
*
624 new_function_call(slang_ir_node
*code
, slang_label
*name
)
626 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
635 * Unconditional jump.
637 static slang_ir_node
*
638 new_return(slang_label
*dest
)
640 slang_ir_node
*n
= new_node0(IR_RETURN
);
648 static slang_ir_node
*
649 new_loop(slang_ir_node
*body
)
651 return new_node1(IR_LOOP
, body
);
655 static slang_ir_node
*
656 new_break(slang_ir_node
*loopNode
)
658 slang_ir_node
*n
= new_node0(IR_BREAK
);
660 assert(loopNode
->Opcode
== IR_LOOP
);
662 /* insert this node at head of linked list */
663 n
->List
= loopNode
->List
;
671 * Make new IR_BREAK_IF_TRUE.
673 static slang_ir_node
*
674 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
678 assert(loopNode
->Opcode
== IR_LOOP
);
679 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
681 /* insert this node at head of linked list */
682 n
->List
= loopNode
->List
;
690 * Make new IR_CONT_IF_TRUE node.
692 static slang_ir_node
*
693 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
697 assert(loopNode
->Opcode
== IR_LOOP
);
698 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
700 /* insert this node at head of linked list */
701 n
->List
= loopNode
->List
;
708 static slang_ir_node
*
709 new_cond(slang_ir_node
*n
)
711 slang_ir_node
*c
= new_node1(IR_COND
, n
);
716 static slang_ir_node
*
717 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
719 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
724 * New IR_VAR node - a reference to a previously declared variable.
726 static slang_ir_node
*
727 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
729 slang_ir_node
*n
= new_node0(IR_VAR
);
731 _slang_attach_storage(n
, var
);
738 * Check if the given function is really just a wrapper for a
739 * basic assembly instruction.
742 slang_is_asm_function(const slang_function
*fun
)
744 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
745 fun
->body
->num_children
== 1 &&
746 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
754 _slang_is_noop(const slang_operation
*oper
)
757 oper
->type
== SLANG_OPER_VOID
||
758 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
766 * Recursively search tree for a node of the given type.
768 static slang_operation
*
769 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
772 if (oper
->type
== type
)
774 for (i
= 0; i
< oper
->num_children
; i
++) {
775 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
784 * Count the number of operations of the given time rooted at 'oper'.
787 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
790 if (oper
->type
== type
) {
793 for (i
= 0; i
< oper
->num_children
; i
++) {
794 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
801 * Check if the 'return' statement found under 'oper' is a "tail return"
802 * that can be no-op'd. For example:
807 * return; // this is a no-op
810 * This is used when determining if a function can be inlined. If the
811 * 'return' is not the last statement, we can't inline the function since
812 * we still need the semantic behaviour of the 'return' but we don't want
813 * to accidentally return from the _calling_ function. We'd need to use an
814 * unconditional branch, but we don't have such a GPU instruction (not
818 _slang_is_tail_return(const slang_operation
*oper
)
820 GLuint k
= oper
->num_children
;
823 const slang_operation
*last
= &oper
->children
[k
- 1];
824 if (last
->type
== SLANG_OPER_RETURN
)
826 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
827 last
->type
== SLANG_OPER_LABEL
)
828 k
--; /* try prev child */
829 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
830 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
831 /* try sub-children */
832 return _slang_is_tail_return(last
);
842 slang_resolve_variable(slang_operation
*oper
)
844 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
845 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
851 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
854 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
855 GLuint substCount
, slang_variable
**substOld
,
856 slang_operation
**substNew
, GLboolean isLHS
)
858 switch (oper
->type
) {
859 case SLANG_OPER_VARIABLE_DECL
:
861 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
862 oper
->a_id
, GL_TRUE
);
864 if (v
->initializer
&& oper
->num_children
== 0) {
865 /* set child of oper to copy of initializer */
866 oper
->num_children
= 1;
867 oper
->children
= slang_operation_new(1);
868 slang_operation_copy(&oper
->children
[0], v
->initializer
);
870 if (oper
->num_children
== 1) {
871 /* the initializer */
872 slang_substitute(A
, &oper
->children
[0], substCount
,
873 substOld
, substNew
, GL_FALSE
);
877 case SLANG_OPER_IDENTIFIER
:
878 assert(oper
->num_children
== 0);
879 if (1/**!isLHS XXX FIX */) {
880 slang_atom id
= oper
->a_id
;
883 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
885 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
889 /* look for a substitution */
890 for (i
= 0; i
< substCount
; i
++) {
891 if (v
== substOld
[i
]) {
892 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
893 #if 0 /* DEBUG only */
894 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
895 assert(substNew
[i
]->var
);
896 assert(substNew
[i
]->var
->a_name
);
897 printf("Substitute %s with %s in id node %p\n",
898 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
902 printf("Substitute %s with %f in id node %p\n",
903 (char*)v
->a_name
, substNew
[i
]->literal
[0],
907 slang_operation_copy(oper
, substNew
[i
]);
914 case SLANG_OPER_RETURN
:
915 /* do return replacement here too */
916 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
917 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
923 * then do substitutions on the assignment.
925 slang_operation
*blockOper
, *assignOper
, *returnOper
;
927 /* check if function actually has a return type */
928 assert(A
->CurFunction
);
929 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
930 slang_info_log_error(A
->log
, "illegal return expression");
934 blockOper
= slang_operation_new(1);
935 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
936 blockOper
->num_children
= 2;
937 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
938 blockOper
->children
= slang_operation_new(2);
939 assignOper
= blockOper
->children
+ 0;
940 returnOper
= blockOper
->children
+ 1;
942 assignOper
->type
= SLANG_OPER_ASSIGN
;
943 assignOper
->num_children
= 2;
944 assignOper
->locals
->outer_scope
= blockOper
->locals
;
945 assignOper
->children
= slang_operation_new(2);
946 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
947 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
948 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
950 slang_operation_copy(&assignOper
->children
[1],
953 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
954 assert(returnOper
->num_children
== 0);
956 /* do substitutions on the "__retVal = expr" sub-tree */
957 slang_substitute(A
, assignOper
,
958 substCount
, substOld
, substNew
, GL_FALSE
);
960 /* install new code */
961 slang_operation_copy(oper
, blockOper
);
962 slang_operation_destruct(blockOper
);
965 /* check if return value was expected */
966 assert(A
->CurFunction
);
967 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
968 slang_info_log_error(A
->log
, "return statement requires an expression");
974 case SLANG_OPER_ASSIGN
:
975 case SLANG_OPER_SUBSCRIPT
:
977 * child[0] can't have substitutions but child[1] can.
979 slang_substitute(A
, &oper
->children
[0],
980 substCount
, substOld
, substNew
, GL_TRUE
);
981 slang_substitute(A
, &oper
->children
[1],
982 substCount
, substOld
, substNew
, GL_FALSE
);
984 case SLANG_OPER_FIELD
:
986 slang_substitute(A
, &oper
->children
[0],
987 substCount
, substOld
, substNew
, GL_TRUE
);
992 for (i
= 0; i
< oper
->num_children
; i
++)
993 slang_substitute(A
, &oper
->children
[i
],
994 substCount
, substOld
, substNew
, GL_FALSE
);
1001 * Produce inline code for a call to an assembly instruction.
1002 * This is typically used to compile a call to a built-in function like this:
1004 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1006 * __asm vec4_lrp __retVal, a, y, x;
1011 * r = mix(p1, p2, p3);
1021 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1023 static slang_operation
*
1024 slang_inline_asm_function(slang_assemble_ctx
*A
,
1025 slang_function
*fun
, slang_operation
*oper
)
1027 const GLuint numArgs
= oper
->num_children
;
1029 slang_operation
*inlined
;
1030 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1031 slang_variable
**substOld
;
1032 slang_operation
**substNew
;
1034 ASSERT(slang_is_asm_function(fun
));
1035 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1038 printf("Inline %s as %s\n",
1039 (char*) fun->header.a_name,
1040 (char*) fun->body->children[0].a_id);
1044 * We'll substitute formal params with actual args in the asm call.
1046 substOld
= (slang_variable
**)
1047 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1048 substNew
= (slang_operation
**)
1049 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1050 for (i
= 0; i
< numArgs
; i
++) {
1051 substOld
[i
] = fun
->parameters
->variables
[i
];
1052 substNew
[i
] = oper
->children
+ i
;
1055 /* make a copy of the code to inline */
1056 inlined
= slang_operation_new(1);
1057 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1059 /* get rid of the __retVal child */
1060 inlined
->num_children
--;
1061 for (i
= 0; i
< inlined
->num_children
; i
++) {
1062 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1066 /* now do formal->actual substitutions */
1067 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1069 _slang_free(substOld
);
1070 _slang_free(substNew
);
1073 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1074 (char *) fun
->header
.a_name
);
1075 slang_print_tree(inlined
, 3);
1076 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1084 * Inline the given function call operation.
1085 * Return a new slang_operation that corresponds to the inlined code.
1087 static slang_operation
*
1088 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1089 slang_operation
*oper
, slang_operation
*returnOper
)
1096 ParamMode
*paramMode
;
1097 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1098 const GLuint numArgs
= oper
->num_children
;
1099 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1100 slang_operation
*args
= oper
->children
;
1101 slang_operation
*inlined
, *top
;
1102 slang_variable
**substOld
;
1103 slang_operation
**substNew
;
1104 GLuint substCount
, numCopyIn
, i
;
1105 slang_function
*prevFunction
;
1106 slang_variable_scope
*newScope
= NULL
;
1109 prevFunction
= A
->CurFunction
;
1110 A
->CurFunction
= fun
;
1112 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1113 assert(fun
->param_count
== totalArgs
);
1115 /* allocate temporary arrays */
1116 paramMode
= (ParamMode
*)
1117 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1118 substOld
= (slang_variable
**)
1119 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1120 substNew
= (slang_operation
**)
1121 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1124 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1125 (char *) fun
->header
.a_name
,
1126 fun
->parameters
->num_variables
, numArgs
);
1129 if (haveRetValue
&& !returnOper
) {
1130 /* Create 3-child comma sequence for inlined code:
1131 * child[0]: declare __resultTmp
1132 * child[1]: inlined function body
1133 * child[2]: __resultTmp
1135 slang_operation
*commaSeq
;
1136 slang_operation
*declOper
= NULL
;
1137 slang_variable
*resultVar
;
1139 commaSeq
= slang_operation_new(1);
1140 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1141 assert(commaSeq
->locals
);
1142 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1143 commaSeq
->num_children
= 3;
1144 commaSeq
->children
= slang_operation_new(3);
1145 /* allocate the return var */
1146 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1148 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1149 (void*)commaSeq->locals, (char *) fun->header.a_name);
1152 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1153 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1154 resultVar
->isTemp
= GL_TRUE
;
1156 /* child[0] = __resultTmp declaration */
1157 declOper
= &commaSeq
->children
[0];
1158 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1159 declOper
->a_id
= resultVar
->a_name
;
1160 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1162 /* child[1] = function body */
1163 inlined
= &commaSeq
->children
[1];
1164 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1166 /* child[2] = __resultTmp reference */
1167 returnOper
= &commaSeq
->children
[2];
1168 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1169 returnOper
->a_id
= resultVar
->a_name
;
1170 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1175 top
= inlined
= slang_operation_new(1);
1176 /* XXXX this may be inappropriate!!!! */
1177 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1181 assert(inlined
->locals
);
1183 /* Examine the parameters, look for inout/out params, look for possible
1184 * substitutions, etc:
1185 * param type behaviour
1186 * in copy actual to local
1187 * const in substitute param with actual
1191 for (i
= 0; i
< totalArgs
; i
++) {
1192 slang_variable
*p
= fun
->parameters
->variables
[i
];
1194 printf("Param %d: %s %s \n", i,
1195 slang_type_qual_string(p->type.qualifier),
1196 (char *) p->a_name);
1198 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1199 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1200 /* an output param */
1201 slang_operation
*arg
;
1206 paramMode
[i
] = SUBST
;
1208 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1209 slang_resolve_variable(arg
);
1211 /* replace parameter 'p' with argument 'arg' */
1212 substOld
[substCount
] = p
;
1213 substNew
[substCount
] = arg
; /* will get copied */
1216 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1217 /* a constant input param */
1218 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1219 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1220 /* replace all occurances of this parameter variable with the
1221 * actual argument variable or a literal.
1223 paramMode
[i
] = SUBST
;
1224 slang_resolve_variable(&args
[i
]);
1225 substOld
[substCount
] = p
;
1226 substNew
[substCount
] = &args
[i
]; /* will get copied */
1230 paramMode
[i
] = COPY_IN
;
1234 paramMode
[i
] = COPY_IN
;
1236 assert(paramMode
[i
]);
1239 /* actual code inlining: */
1240 slang_operation_copy(inlined
, fun
->body
);
1242 /*** XXX review this */
1243 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1244 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1245 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1248 printf("======================= orig body code ======================\n");
1249 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1250 slang_print_tree(fun
->body
, 8);
1251 printf("======================= copied code =========================\n");
1252 slang_print_tree(inlined
, 8);
1255 /* do parameter substitution in inlined code: */
1256 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1259 printf("======================= subst code ==========================\n");
1260 slang_print_tree(inlined
, 8);
1261 printf("=============================================================\n");
1264 /* New prolog statements: (inserted before the inlined code)
1265 * Copy the 'in' arguments.
1268 for (i
= 0; i
< numArgs
; i
++) {
1269 if (paramMode
[i
] == COPY_IN
) {
1270 slang_variable
*p
= fun
->parameters
->variables
[i
];
1271 /* declare parameter 'p' */
1272 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1276 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1277 assert(decl
->locals
);
1278 decl
->locals
->outer_scope
= inlined
->locals
;
1279 decl
->a_id
= p
->a_name
;
1280 decl
->num_children
= 1;
1281 decl
->children
= slang_operation_new(1);
1283 /* child[0] is the var's initializer */
1284 slang_operation_copy(&decl
->children
[0], args
+ i
);
1286 /* add parameter 'p' to the local variable scope here */
1288 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1289 pCopy
->type
= p
->type
;
1290 pCopy
->a_name
= p
->a_name
;
1291 pCopy
->array_len
= p
->array_len
;
1294 newScope
= inlined
->locals
;
1299 /* Now add copies of the function's local vars to the new variable scope */
1300 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1301 slang_variable
*p
= fun
->parameters
->variables
[i
];
1302 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1303 pCopy
->type
= p
->type
;
1304 pCopy
->a_name
= p
->a_name
;
1305 pCopy
->array_len
= p
->array_len
;
1309 /* New epilog statements:
1310 * 1. Create end of function label to jump to from return statements.
1311 * 2. Copy the 'out' parameter vars
1314 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1316 inlined
->num_children
);
1317 lab
->type
= SLANG_OPER_LABEL
;
1318 lab
->label
= A
->curFuncEndLabel
;
1321 for (i
= 0; i
< totalArgs
; i
++) {
1322 if (paramMode
[i
] == COPY_OUT
) {
1323 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1324 /* actualCallVar = outParam */
1325 /*if (i > 0 || !haveRetValue)*/
1326 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1328 inlined
->num_children
);
1329 ass
->type
= SLANG_OPER_ASSIGN
;
1330 ass
->num_children
= 2;
1331 ass
->locals
->outer_scope
= inlined
->locals
;
1332 ass
->children
= slang_operation_new(2);
1333 ass
->children
[0] = args
[i
]; /*XXX copy */
1334 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1335 ass
->children
[1].a_id
= p
->a_name
;
1336 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1340 _slang_free(paramMode
);
1341 _slang_free(substOld
);
1342 _slang_free(substNew
);
1344 /* Update scoping to use the new local vars instead of the
1345 * original function's vars. This is especially important
1346 * for nested inlining.
1349 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1352 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1353 (char *) fun
->header
.a_name
,
1354 fun
->parameters
->num_variables
, numArgs
);
1355 slang_print_tree(top
, 0);
1359 A
->CurFunction
= prevFunction
;
1365 static slang_ir_node
*
1366 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1367 slang_operation
*oper
, slang_operation
*dest
)
1370 slang_operation
*inlined
;
1371 slang_label
*prevFuncEndLabel
;
1374 prevFuncEndLabel
= A
->curFuncEndLabel
;
1375 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1376 A
->curFuncEndLabel
= _slang_label_new(name
);
1377 assert(A
->curFuncEndLabel
);
1379 if (slang_is_asm_function(fun
) && !dest
) {
1380 /* assemble assembly function - tree style */
1381 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1384 /* non-assembly function */
1385 /* We always generate an "inline-able" block of code here.
1387 * 1. insert the inline code
1388 * 2. Generate a call to the "inline" code as a subroutine
1392 slang_operation
*ret
= NULL
;
1394 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1398 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1400 /* check if this is a "tail" return */
1401 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1402 _slang_is_tail_return(inlined
)) {
1403 /* The only RETURN is the last stmt in the function, no-op it
1404 * and inline the function body.
1406 ret
->type
= SLANG_OPER_NONE
;
1409 slang_operation
*callOper
;
1410 /* The function we're calling has one or more 'return' statements.
1411 * So, we can't truly inline this function because we need to
1412 * implement 'return' with RET (and CAL).
1413 * Nevertheless, we performed "inlining" to make a new instance
1414 * of the function body to deal with static register allocation.
1416 * XXX check if there's one 'return' and if it's the very last
1417 * statement in the function - we can optimize that case.
1419 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1420 inlined
->type
== SLANG_OPER_SEQUENCE
);
1422 if (_slang_function_has_return_value(fun
) && !dest
) {
1423 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1424 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1425 callOper
= &inlined
->children
[1];
1430 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1431 callOper
->fun
= fun
;
1432 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1440 /* Replace the function call with the inlined block (or new CALL stmt) */
1441 slang_operation_destruct(oper
);
1443 _slang_free(inlined
);
1446 assert(inlined
->locals
);
1447 printf("*** Inlined code for call to %s:\n",
1448 (char*) fun
->header
.a_name
);
1449 slang_print_tree(oper
, 10);
1453 n
= _slang_gen_operation(A
, oper
);
1455 /*_slang_label_delete(A->curFuncEndLabel);*/
1456 A
->curFuncEndLabel
= prevFuncEndLabel
;
1462 static slang_asm_info
*
1463 slang_find_asm_info(const char *name
)
1466 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1467 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1476 * Some write-masked assignments are simple, but others are hard.
1479 * v.xy = vec2(a, b);
1482 * v.zy = vec2(a, b);
1483 * this gets transformed/swizzled into:
1484 * v.zy = vec2(a, b).*yx* (* = don't care)
1485 * This function helps to determine simple vs. non-simple.
1488 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1490 switch (writemask
) {
1492 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1494 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1496 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1498 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1500 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1501 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1503 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1504 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1505 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1506 case WRITEMASK_XYZW
:
1507 return swizzle
== SWIZZLE_NOOP
;
1515 * Convert the given swizzle into a writemask. In some cases this
1516 * is trivial, in other cases, we'll need to also swizzle the right
1517 * hand side to put components in the right places.
1518 * See comment above for more info.
1519 * XXX this function could be simplified and should probably be renamed.
1520 * \param swizzle the incoming swizzle
1521 * \param writemaskOut returns the writemask
1522 * \param swizzleOut swizzle to apply to the right-hand-side
1523 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1526 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1527 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1529 GLuint mask
= 0x0, newSwizzle
[4];
1532 /* make new dst writemask, compute size */
1533 for (i
= 0; i
< 4; i
++) {
1534 const GLuint swz
= GET_SWZ(swizzle
, i
);
1535 if (swz
== SWIZZLE_NIL
) {
1539 assert(swz
>= 0 && swz
<= 3);
1541 if (swizzle
!= SWIZZLE_XXXX
&&
1542 swizzle
!= SWIZZLE_YYYY
&&
1543 swizzle
!= SWIZZLE_ZZZZ
&&
1544 swizzle
!= SWIZZLE_WWWW
&&
1545 (mask
& (1 << swz
))) {
1546 /* a channel can't be specified twice (ex: ".xyyz") */
1547 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1548 _mesa_swizzle_string(swizzle
, 0, 0));
1554 assert(mask
<= 0xf);
1555 size
= i
; /* number of components in mask/swizzle */
1557 *writemaskOut
= mask
;
1559 /* make new src swizzle, by inversion */
1560 for (i
= 0; i
< 4; i
++) {
1561 newSwizzle
[i
] = i
; /*identity*/
1563 for (i
= 0; i
< size
; i
++) {
1564 const GLuint swz
= GET_SWZ(swizzle
, i
);
1565 newSwizzle
[swz
] = i
;
1567 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1572 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1574 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1576 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1578 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1580 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1588 #if 0 /* not used, but don't remove just yet */
1590 * Recursively traverse 'oper' to produce a swizzle mask in the event
1591 * of any vector subscripts and swizzle suffixes.
1592 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1595 resolve_swizzle(const slang_operation
*oper
)
1597 if (oper
->type
== SLANG_OPER_FIELD
) {
1598 /* writemask from .xyzw suffix */
1600 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1601 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1605 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1606 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1610 return SWIZZLE_XYZW
;
1612 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1613 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1614 /* writemask from [index] */
1615 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1616 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1621 swizzle
= SWIZZLE_XXXX
;
1624 swizzle
= SWIZZLE_YYYY
;
1627 swizzle
= SWIZZLE_ZZZZ
;
1630 swizzle
= SWIZZLE_WWWW
;
1633 swizzle
= SWIZZLE_XYZW
;
1635 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1639 return SWIZZLE_XYZW
;
1647 * Recursively descend through swizzle nodes to find the node's storage info.
1649 static slang_ir_storage
*
1650 get_store(const slang_ir_node
*n
)
1652 if (n
->Opcode
== IR_SWIZZLE
) {
1653 return get_store(n
->Children
[0]);
1661 * Generate IR tree for an asm instruction/operation such as:
1662 * __asm vec4_dot __retVal.x, v1, v2;
1664 static slang_ir_node
*
1665 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1666 slang_operation
*dest
)
1668 const slang_asm_info
*info
;
1669 slang_ir_node
*kids
[3], *n
;
1670 GLuint j
, firstOperand
;
1672 assert(oper
->type
== SLANG_OPER_ASM
);
1674 info
= slang_find_asm_info((char *) oper
->a_id
);
1676 _mesa_problem(NULL
, "undefined __asm function %s\n",
1677 (char *) oper
->a_id
);
1680 assert(info
->NumParams
<= 3);
1682 if (info
->NumParams
== oper
->num_children
) {
1683 /* Storage for result is not specified.
1684 * Children[0], [1], [2] are the operands.
1689 /* Storage for result (child[0]) is specified.
1690 * Children[1], [2], [3] are the operands.
1695 /* assemble child(ren) */
1696 kids
[0] = kids
[1] = kids
[2] = NULL
;
1697 for (j
= 0; j
< info
->NumParams
; j
++) {
1698 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1703 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1706 /* Setup n->Store to be a particular location. Otherwise, storage
1707 * for the result (a temporary) will be allocated later.
1709 slang_operation
*dest_oper
;
1712 dest_oper
= &oper
->children
[0];
1714 n0
= _slang_gen_operation(A
, dest_oper
);
1719 n
->Store
= n0
->Store
;
1721 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1732 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1735 for (i
= 0; i
< scope
->num_functions
; i
++) {
1736 slang_function
*f
= &scope
->functions
[i
];
1737 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1738 printf(" %s (%d args)\n", name
, f
->param_count
);
1741 if (scope
->outer_scope
)
1742 print_funcs(scope
->outer_scope
, name
);
1748 * Find a function of the given name, taking 'numArgs' arguments.
1749 * This is the function we'll try to call when there is no exact match
1750 * between function parameters and call arguments.
1752 * XXX we should really create a list of candidate functions and try
1755 static slang_function
*
1756 _slang_find_function_by_argc(slang_function_scope
*scope
,
1757 const char *name
, int numArgs
)
1761 for (i
= 0; i
< scope
->num_functions
; i
++) {
1762 slang_function
*f
= &scope
->functions
[i
];
1763 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1764 int haveRetValue
= _slang_function_has_return_value(f
);
1765 if (numArgs
== f
->param_count
- haveRetValue
)
1769 scope
= scope
->outer_scope
;
1776 static slang_function
*
1777 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1780 slang_function
*maxFunc
= NULL
;
1785 for (i
= 0; i
< scope
->num_functions
; i
++) {
1786 slang_function
*f
= &scope
->functions
[i
];
1787 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1788 if (f
->param_count
> maxArgs
) {
1789 maxArgs
= f
->param_count
;
1794 scope
= scope
->outer_scope
;
1802 * Generate a new slang_function which is a constructor for a user-defined
1805 static slang_function
*
1806 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1808 const GLint numFields
= str
->fields
->num_variables
;
1809 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1811 /* function header (name, return type) */
1812 fun
->header
.a_name
= str
->a_name
;
1813 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1814 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1815 fun
->header
.type
.specifier
._struct
= str
;
1817 /* function parameters (= struct's fields) */
1820 for (i
= 0; i
< numFields
; i
++) {
1822 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1824 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1825 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1826 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1828 fun
->param_count
= fun
->parameters
->num_variables
;
1831 /* Add __retVal to params */
1833 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1834 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1836 p
->a_name
= a_retVal
;
1837 p
->type
= fun
->header
.type
;
1838 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1842 /* function body is:
1852 slang_variable_scope
*scope
;
1853 slang_variable
*var
;
1856 fun
->body
= slang_operation_new(1);
1857 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1858 fun
->body
->num_children
= numFields
+ 2;
1859 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1861 scope
= fun
->body
->locals
;
1862 scope
->outer_scope
= fun
->parameters
;
1864 /* create local var 't' */
1865 var
= slang_variable_scope_grow(scope
);
1866 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1867 var
->type
= fun
->header
.type
;
1871 slang_operation
*decl
;
1873 decl
= &fun
->body
->children
[0];
1874 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1875 decl
->locals
= _slang_variable_scope_new(scope
);
1876 decl
->a_id
= var
->a_name
;
1879 /* assign params to fields of t */
1880 for (i
= 0; i
< numFields
; i
++) {
1881 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1883 assign
->type
= SLANG_OPER_ASSIGN
;
1884 assign
->locals
= _slang_variable_scope_new(scope
);
1885 assign
->num_children
= 2;
1886 assign
->children
= slang_operation_new(2);
1889 slang_operation
*lhs
= &assign
->children
[0];
1891 lhs
->type
= SLANG_OPER_FIELD
;
1892 lhs
->locals
= _slang_variable_scope_new(scope
);
1893 lhs
->num_children
= 1;
1894 lhs
->children
= slang_operation_new(1);
1895 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1897 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1898 lhs
->children
[0].a_id
= var
->a_name
;
1899 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1902 lhs
->children
[1].num_children
= 1;
1903 lhs
->children
[1].children
= slang_operation_new(1);
1904 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1905 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1906 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1911 slang_operation
*rhs
= &assign
->children
[1];
1913 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1914 rhs
->locals
= _slang_variable_scope_new(scope
);
1915 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1921 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1923 ret
->type
= SLANG_OPER_RETURN
;
1924 ret
->locals
= _slang_variable_scope_new(scope
);
1925 ret
->num_children
= 1;
1926 ret
->children
= slang_operation_new(1);
1927 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1928 ret
->children
[0].a_id
= var
->a_name
;
1929 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1933 slang_print_function(fun, 1);
1940 * Find/create a function (constructor) for the given structure name.
1942 static slang_function
*
1943 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1946 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1947 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1948 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1949 /* found a structure type that matches the function name */
1950 if (!str
->constructor
) {
1951 /* create the constructor function now */
1952 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1954 return str
->constructor
;
1962 * Generate a new slang_function to satisfy a call to an array constructor.
1963 * Ex: float[3](1., 2., 3.)
1965 static slang_function
*
1966 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1968 slang_type_specifier_type baseType
;
1969 slang_function
*fun
;
1972 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1976 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1978 num_elements
= oper
->num_children
;
1980 /* function header, return type */
1982 fun
->header
.a_name
= oper
->a_id
;
1983 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1984 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1985 fun
->header
.type
.specifier
._array
=
1986 slang_type_specifier_new(baseType
, NULL
, NULL
);
1987 fun
->header
.type
.array_len
= num_elements
;
1990 /* function parameters (= number of elements) */
1993 for (i
= 0; i
< num_elements
; i
++) {
1995 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1997 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1999 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2000 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2001 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2002 p
->type
.specifier
.type
= baseType
;
2004 fun
->param_count
= fun
->parameters
->num_variables
;
2007 /* Add __retVal to params */
2009 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2010 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2012 p
->a_name
= a_retVal
;
2013 p
->type
= fun
->header
.type
;
2014 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2015 p
->type
.specifier
.type
= baseType
;
2019 /* function body is:
2029 slang_variable_scope
*scope
;
2030 slang_variable
*var
;
2033 fun
->body
= slang_operation_new(1);
2034 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2035 fun
->body
->num_children
= num_elements
+ 2;
2036 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2038 scope
= fun
->body
->locals
;
2039 scope
->outer_scope
= fun
->parameters
;
2041 /* create local var 't' */
2042 var
= slang_variable_scope_grow(scope
);
2043 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2044 var
->type
= fun
->header
.type
;/*XXX copy*/
2048 slang_operation
*decl
;
2050 decl
= &fun
->body
->children
[0];
2051 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2052 decl
->locals
= _slang_variable_scope_new(scope
);
2053 decl
->a_id
= var
->a_name
;
2056 /* assign params to elements of t */
2057 for (i
= 0; i
< num_elements
; i
++) {
2058 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2060 assign
->type
= SLANG_OPER_ASSIGN
;
2061 assign
->locals
= _slang_variable_scope_new(scope
);
2062 assign
->num_children
= 2;
2063 assign
->children
= slang_operation_new(2);
2066 slang_operation
*lhs
= &assign
->children
[0];
2068 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2069 lhs
->locals
= _slang_variable_scope_new(scope
);
2070 lhs
->num_children
= 2;
2071 lhs
->children
= slang_operation_new(2);
2073 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2074 lhs
->children
[0].a_id
= var
->a_name
;
2075 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2077 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2078 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2082 slang_operation
*rhs
= &assign
->children
[1];
2084 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2085 rhs
->locals
= _slang_variable_scope_new(scope
);
2086 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2092 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2094 ret
->type
= SLANG_OPER_RETURN
;
2095 ret
->locals
= _slang_variable_scope_new(scope
);
2096 ret
->num_children
= 1;
2097 ret
->children
= slang_operation_new(1);
2098 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2099 ret
->children
[0].a_id
= var
->a_name
;
2100 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2105 slang_print_function(fun, 1);
2113 _slang_is_vec_mat_type(const char *name
)
2115 static const char *vecmat_types
[] = {
2116 "float", "int", "bool",
2117 "vec2", "vec3", "vec4",
2118 "ivec2", "ivec3", "ivec4",
2119 "bvec2", "bvec3", "bvec4",
2120 "mat2", "mat3", "mat4",
2121 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2125 for (i
= 0; vecmat_types
[i
]; i
++)
2126 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2133 * Assemble a function call, given a particular function name.
2134 * \param name the function's name (operators like '*' are possible).
2136 static slang_ir_node
*
2137 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2138 slang_operation
*oper
, slang_operation
*dest
)
2140 slang_operation
*params
= oper
->children
;
2141 const GLuint param_count
= oper
->num_children
;
2143 slang_function
*fun
;
2146 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2147 if (atom
== SLANG_ATOM_NULL
)
2150 if (oper
->array_constructor
) {
2151 /* this needs special handling */
2152 fun
= _slang_make_array_constructor(A
, oper
);
2155 /* Try to find function by name and exact argument type matching */
2156 GLboolean error
= GL_FALSE
;
2157 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2158 &A
->space
, A
->atoms
, A
->log
, &error
);
2160 slang_info_log_error(A
->log
,
2161 "Function '%s' not found (check argument types)",
2168 /* Next, try locating a constructor function for a user-defined type */
2169 fun
= _slang_locate_struct_constructor(A
, name
);
2173 * At this point, some heuristics are used to try to find a function
2174 * that matches the calling signature by means of casting or "unrolling"
2178 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2179 /* Next, if this call looks like a vec() or mat() constructor call,
2180 * try "unwinding" the args to satisfy a constructor.
2182 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2184 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2185 slang_info_log_error(A
->log
,
2186 "Function '%s' not found (check argument types)",
2193 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2194 /* Next, try casting args to the types of the formal parameters */
2195 int numArgs
= oper
->num_children
;
2196 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2197 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2198 slang_info_log_error(A
->log
,
2199 "Function '%s' not found (check argument types)",
2207 slang_info_log_error(A
->log
,
2208 "Function '%s' not found (check argument types)",
2213 slang_info_log_error(A
->log
,
2214 "Function '%s' prototyped but not defined. "
2215 "Separate compilation units not supported.",
2220 /* type checking to be sure function's return type matches 'dest' type */
2224 slang_typeinfo_construct(&t0
);
2225 typeof_operation(A
, dest
, &t0
);
2227 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2228 slang_info_log_error(A
->log
,
2229 "Incompatible type returned by call to '%s'",
2235 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2237 if (n
&& !n
->Store
&& !dest
2238 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2239 /* setup n->Store for the result of the function call */
2240 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2241 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2242 /*printf("Alloc storage for function result, size %d \n", size);*/
2245 if (oper
->array_constructor
) {
2246 /* free the temporary array constructor function now */
2247 slang_function_destruct(fun
);
2254 static slang_ir_node
*
2255 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2257 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2259 slang_variable
*var
;
2261 /* NOTE: In GLSL 1.20, there's only one kind of method
2262 * call: array.length(). Anything else is an error.
2264 if (oper
->a_id
!= a_length
) {
2265 slang_info_log_error(A
->log
,
2266 "Undefined method call '%s'", (char *) oper
->a_id
);
2270 /* length() takes no arguments */
2271 if (oper
->num_children
> 0) {
2272 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2276 /* lookup the object/variable */
2277 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2278 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2279 slang_info_log_error(A
->log
,
2280 "Undefined object '%s'", (char *) oper
->a_obj
);
2284 /* Create a float/literal IR node encoding the array length */
2285 n
= new_node0(IR_FLOAT
);
2287 n
->Value
[0] = (float) _slang_array_length(var
);
2288 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2295 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2297 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2298 oper
->type
== SLANG_OPER_LITERAL_INT
||
2299 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2300 if (oper
->literal
[0])
2306 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2307 oper
->num_children
== 1) {
2308 return _slang_is_constant_cond(&oper
->children
[0], value
);
2315 * Test if an operation is a scalar or boolean.
2318 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2320 slang_typeinfo type
;
2323 slang_typeinfo_construct(&type
);
2324 typeof_operation(A
, oper
, &type
);
2325 size
= _slang_sizeof_type_specifier(&type
.spec
);
2326 slang_typeinfo_destruct(&type
);
2332 * Test if an operation is boolean.
2335 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2337 slang_typeinfo type
;
2340 slang_typeinfo_construct(&type
);
2341 typeof_operation(A
, oper
, &type
);
2342 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2343 slang_typeinfo_destruct(&type
);
2349 * Generate loop code using high-level IR_LOOP instruction
2351 static slang_ir_node
*
2352 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2356 * BREAK if !expr (child[0])
2357 * body code (child[1])
2359 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2360 GLboolean isConst
, constTrue
;
2362 /* type-check expression */
2363 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2364 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2368 /* Check if loop condition is a constant */
2369 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2371 if (isConst
&& !constTrue
) {
2372 /* loop is never executed! */
2373 return new_node0(IR_NOP
);
2376 loop
= new_loop(NULL
);
2378 /* save old, push new loop */
2379 prevLoop
= A
->CurLoop
;
2382 if (isConst
&& constTrue
) {
2383 /* while(nonzero constant), no conditional break */
2388 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2389 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2391 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2392 loop
->Children
[0] = new_seq(breakIf
, body
);
2394 /* Do infinite loop detection */
2395 /* loop->List is head of linked list of break/continue nodes */
2396 if (!loop
->List
&& isConst
&& constTrue
) {
2397 /* infinite loop detected */
2398 A
->CurLoop
= prevLoop
; /* clean-up */
2399 slang_info_log_error(A
->log
, "Infinite loop detected!");
2403 /* pop loop, restore prev */
2404 A
->CurLoop
= prevLoop
;
2411 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2413 static slang_ir_node
*
2414 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2418 * body code (child[0])
2420 * BREAK if !expr (child[1])
2422 slang_ir_node
*prevLoop
, *loop
;
2423 GLboolean isConst
, constTrue
;
2425 /* type-check expression */
2426 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2427 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2431 loop
= new_loop(NULL
);
2433 /* save old, push new loop */
2434 prevLoop
= A
->CurLoop
;
2438 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2440 /* Check if loop condition is a constant */
2441 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2442 if (isConst
&& constTrue
) {
2443 /* do { } while(1) ==> no conditional break */
2444 loop
->Children
[1] = NULL
; /* no tail code */
2448 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2449 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2452 /* XXX we should do infinite loop detection, as above */
2454 /* pop loop, restore prev */
2455 A
->CurLoop
= prevLoop
;
2462 * Recursively count the number of operations rooted at 'oper'.
2463 * This gives some kind of indication of the size/complexity of an operation.
2466 sizeof_operation(const slang_operation
*oper
)
2469 GLuint count
= 1; /* me */
2471 for (i
= 0; i
< oper
->num_children
; i
++) {
2472 count
+= sizeof_operation(&oper
->children
[i
]);
2483 * Determine if a for-loop can be unrolled.
2484 * At this time, only a rather narrow class of for loops can be unrolled.
2485 * See code for details.
2486 * When a loop can't be unrolled because it's too large we'll emit a
2487 * message to the log.
2490 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2494 const char *varName
;
2497 assert(oper
->type
== SLANG_OPER_FOR
);
2498 assert(oper
->num_children
== 4);
2500 /* children[0] must be either "int i=constant" or "i=constant" */
2501 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2502 slang_variable
*var
;
2504 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2507 varId
= oper
->children
[0].children
[0].a_id
;
2509 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2513 if (!var
->initializer
)
2515 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2517 start
= (GLint
) var
->initializer
->literal
[0];
2519 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2520 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2522 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2524 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2527 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2529 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2535 /* children[1] must be "i<constant" */
2536 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2538 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2540 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2542 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2545 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2547 /* children[2] must be "i++" or "++i" */
2548 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2549 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2551 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2554 /* make sure the same variable name is used in all places */
2555 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2556 (oper
->children
[2].children
[0].a_id
!= varId
))
2559 varName
= (const char *) varId
;
2561 /* children[3], the loop body, can't be too large */
2562 bodySize
= sizeof_operation(&oper
->children
[3]);
2563 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2564 slang_info_log_print(A
->log
,
2565 "Note: 'for (%s ... )' body is too large/complex"
2572 return GL_FALSE
; /* degenerate case */
2574 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2575 slang_info_log_print(A
->log
,
2576 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2577 " many iterations to unroll",
2578 varName
, start
, varName
, end
, varName
);
2582 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2583 slang_info_log_print(A
->log
,
2584 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2585 " too much code to unroll",
2586 varName
, start
, varName
, end
, varName
);
2590 return GL_TRUE
; /* we can unroll the loop */
2595 * Unroll a for-loop.
2596 * First we determine the number of iterations to unroll.
2597 * Then for each iteration:
2598 * make a copy of the loop body
2599 * replace instances of the loop variable with the current iteration value
2600 * generate IR code for the body
2601 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2603 static slang_ir_node
*
2604 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2606 GLint start
, end
, iter
;
2607 slang_ir_node
*n
, *root
= NULL
;
2610 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2611 /* for (int i=0; ... */
2612 slang_variable
*var
;
2614 varId
= oper
->children
[0].children
[0].a_id
;
2615 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2617 start
= (GLint
) var
->initializer
->literal
[0];
2621 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2622 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2625 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2627 for (iter
= start
; iter
< end
; iter
++) {
2628 slang_operation
*body
;
2630 /* make a copy of the loop body */
2631 body
= slang_operation_new(1);
2635 if (!slang_operation_copy(body
, &oper
->children
[3]))
2638 /* in body, replace instances of 'varId' with literal 'iter' */
2640 slang_variable
*oldVar
;
2641 slang_operation
*newOper
;
2643 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2645 /* undeclared loop variable */
2646 slang_operation_delete(body
);
2650 newOper
= slang_operation_new(1);
2651 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2652 newOper
->literal_size
= 1;
2653 newOper
->literal
[0] = iter
;
2655 /* replace instances of the loop variable with newOper */
2656 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2659 /* do IR codegen for body */
2660 n
= _slang_gen_operation(A
, body
);
2661 root
= new_seq(root
, n
);
2663 slang_operation_delete(body
);
2671 * Generate IR for a for-loop. Unrolling will be done when possible.
2673 static slang_ir_node
*
2674 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2676 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2679 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2684 /* conventional for-loop code generation */
2687 * init code (child[0])
2689 * BREAK if !expr (child[1])
2690 * body code (child[3])
2692 * incr code (child[2]) // XXX continue here
2694 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2695 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2696 loop
= new_loop(NULL
);
2698 /* save old, push new loop */
2699 prevLoop
= A
->CurLoop
;
2702 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2703 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2704 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2705 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2707 loop
->Children
[0] = new_seq(breakIf
, body
);
2708 loop
->Children
[1] = incr
; /* tail code */
2710 /* pop loop, restore prev */
2711 A
->CurLoop
= prevLoop
;
2713 return new_seq(init
, loop
);
2718 static slang_ir_node
*
2719 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2721 slang_ir_node
*n
, *loopNode
;
2722 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2723 loopNode
= A
->CurLoop
;
2725 assert(loopNode
->Opcode
== IR_LOOP
);
2726 n
= new_node0(IR_CONT
);
2728 n
->Parent
= loopNode
;
2729 /* insert this node at head of linked list */
2730 n
->List
= loopNode
->List
;
2738 * Determine if the given operation is of a specific type.
2741 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2743 if (oper
->type
== type
)
2745 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2746 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2747 oper
->num_children
== 1)
2748 return is_operation_type(&oper
->children
[0], type
);
2755 * Generate IR tree for an if/then/else conditional using high-level
2756 * IR_IF instruction.
2758 static slang_ir_node
*
2759 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2762 * eval expr (child[0])
2769 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2770 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2771 GLboolean isConst
, constTrue
;
2773 /* type-check expression */
2774 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2775 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2779 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2780 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2784 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2788 return _slang_gen_operation(A
, &oper
->children
[1]);
2791 /* if (false) ... */
2792 return _slang_gen_operation(A
, &oper
->children
[2]);
2796 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2797 cond
= new_cond(cond
);
2799 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2800 && !haveElseClause
) {
2801 /* Special case: generate a conditional break */
2802 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2805 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2806 && !haveElseClause
) {
2807 /* Special case: generate a conditional break */
2808 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2813 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2815 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2818 ifNode
= new_if(cond
, ifBody
, elseBody
);
2825 static slang_ir_node
*
2826 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2830 assert(oper
->type
== SLANG_OPER_NOT
);
2832 /* type-check expression */
2833 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2834 slang_info_log_error(A
->log
,
2835 "scalar/boolean expression expected for '!'");
2839 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2847 static slang_ir_node
*
2848 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2850 slang_ir_node
*n1
, *n2
;
2852 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2854 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2855 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2856 slang_info_log_error(A
->log
,
2857 "scalar/boolean expressions expected for '^^'");
2861 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2864 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2867 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2872 * Generate IR node for storage of a temporary of given size.
2874 static slang_ir_node
*
2875 _slang_gen_temporary(GLint size
)
2877 slang_ir_storage
*store
;
2878 slang_ir_node
*n
= NULL
;
2880 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2882 n
= new_node0(IR_VAR_DECL
);
2895 * Generate program constants for an array.
2896 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2897 * This will allocate and initialize three vector constants, storing
2898 * the array in constant memory, not temporaries like a non-const array.
2899 * This can also be used for uniform array initializers.
2900 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2903 make_constant_array(slang_assemble_ctx
*A
,
2904 slang_variable
*var
,
2905 slang_operation
*initializer
)
2907 struct gl_program
*prog
= A
->program
;
2908 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2909 const char *varName
= (char *) var
->a_name
;
2910 const GLuint numElements
= initializer
->num_children
;
2916 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2918 size
= var
->store
->Size
;
2920 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2921 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2922 assert(initializer
->type
== SLANG_OPER_CALL
);
2923 assert(initializer
->array_constructor
);
2925 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2927 /* convert constructor params into ordinary floats */
2928 for (i
= 0; i
< numElements
; i
++) {
2929 const slang_operation
*op
= &initializer
->children
[i
];
2930 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2931 /* unsupported type for this optimization */
2935 for (j
= 0; j
< op
->literal_size
; j
++) {
2936 values
[i
* 4 + j
] = op
->literal
[j
];
2938 for ( ; j
< 4; j
++) {
2939 values
[i
* 4 + j
] = 0.0f
;
2943 /* slightly different paths for constants vs. uniforms */
2944 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2945 var
->store
->File
= PROGRAM_UNIFORM
;
2946 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2947 size
, datatype
, values
);
2950 var
->store
->File
= PROGRAM_CONSTANT
;
2951 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2954 assert(var
->store
->Size
== size
);
2964 * Generate IR node for allocating/declaring a variable (either a local or
2966 * Generally, this involves allocating an slang_ir_storage instance for the
2967 * variable, choosing a register file (temporary, constant, etc).
2968 * For ordinary variables we do not yet allocate storage though. We do that
2969 * when we find the first actual use of the variable to avoid allocating temp
2970 * regs that will never get used.
2971 * At this time, uniforms are always allocated space in this function.
2973 * \param initializer Optional initializer expression for the variable.
2975 static slang_ir_node
*
2976 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2977 slang_operation
*initializer
)
2979 const char *varName
= (const char *) var
->a_name
;
2980 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2981 slang_ir_node
*varDecl
, *n
;
2982 slang_ir_storage
*store
;
2983 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
2984 enum register_file file
;
2986 /*assert(!var->declared);*/
2987 var
->declared
= GL_TRUE
;
2989 /* determine GPU register file for simple cases */
2990 if (is_sampler_type(&var
->type
)) {
2991 file
= PROGRAM_SAMPLER
;
2993 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2994 file
= PROGRAM_UNIFORM
;
2997 file
= PROGRAM_TEMPORARY
;
3000 totalSize
= size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3002 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3006 arrayLen
= _slang_array_length(var
);
3007 totalSize
= _slang_array_size(size
, arrayLen
);
3009 /* Allocate IR node for the declaration */
3010 varDecl
= new_node0(IR_VAR_DECL
);
3014 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
3016 assert(varDecl
->Store
== var
->store
);
3017 assert(varDecl
->Store
);
3018 assert(varDecl
->Store
->Index
< 0);
3021 assert(store
== varDecl
->Store
);
3024 /* Fill in storage fields which we now know. store->Index/Swizzle may be
3025 * set for some cases below. Otherwise, store->Index/Swizzle will be set
3029 store
->Size
= totalSize
;
3031 /* if there's an initializer, generate IR for the expression */
3033 slang_ir_node
*varRef
, *init
;
3035 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3036 /* if the variable is const, the initializer must be a const
3037 * expression as well.
3040 if (!_slang_is_constant_expr(initializer
)) {
3041 slang_info_log_error(A
->log
,
3042 "initializer for %s not constant", varName
);
3048 /* IR for the variable we're initializing */
3049 varRef
= new_var(A
, var
);
3051 slang_info_log_error(A
->log
, "out of memory");
3055 /* constant-folding, etc here */
3056 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3058 /* look for simple constant-valued variables and uniforms */
3059 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3060 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3062 if (initializer
->type
== SLANG_OPER_CALL
&&
3063 initializer
->array_constructor
) {
3064 /* array initializer */
3065 if (make_constant_array(A
, var
, initializer
))
3068 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3069 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3070 /* simple float/vector initializer */
3071 if (store
->File
== PROGRAM_UNIFORM
) {
3072 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3074 totalSize
, datatype
,
3075 initializer
->literal
);
3076 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3081 store
->File
= PROGRAM_CONSTANT
;
3082 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3084 initializer
->literal
,
3086 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3093 /* IR for initializer */
3094 init
= _slang_gen_operation(A
, initializer
);
3098 /* XXX remove this when type checking is added above */
3099 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3100 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3104 /* assign RHS to LHS */
3105 n
= new_node2(IR_COPY
, varRef
, init
);
3106 n
= new_seq(varDecl
, n
);
3109 /* no initializer */
3113 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3114 /* always need to allocate storage for uniforms at this point */
3115 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3116 totalSize
, datatype
, NULL
);
3117 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3121 printf("%s var %p %s store=%p index=%d size=%d\n",
3122 __FUNCTION__
, (void *) var
, (char *) varName
,
3123 (void *) store
, store
->Index
, store
->Size
);
3131 * Generate code for a selection expression: b ? x : y
3132 * XXX In some cases we could implement a selection expression
3133 * with an LRP instruction (use the boolean as the interpolant).
3134 * Otherwise, we use an IF/ELSE/ENDIF construct.
3136 static slang_ir_node
*
3137 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3139 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3140 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3141 slang_typeinfo type0
, type1
, type2
;
3142 int size
, isBool
, isEqual
;
3144 assert(oper
->type
== SLANG_OPER_SELECT
);
3145 assert(oper
->num_children
== 3);
3147 /* type of children[0] must be boolean */
3148 slang_typeinfo_construct(&type0
);
3149 typeof_operation(A
, &oper
->children
[0], &type0
);
3150 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3151 slang_typeinfo_destruct(&type0
);
3153 slang_info_log_error(A
->log
, "selector type is not boolean");
3157 slang_typeinfo_construct(&type1
);
3158 slang_typeinfo_construct(&type2
);
3159 typeof_operation(A
, &oper
->children
[1], &type1
);
3160 typeof_operation(A
, &oper
->children
[2], &type2
);
3161 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3162 slang_typeinfo_destruct(&type1
);
3163 slang_typeinfo_destruct(&type2
);
3165 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3169 /* size of x or y's type */
3170 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3174 tmpDecl
= _slang_gen_temporary(size
);
3176 /* the condition (child 0) */
3177 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3178 cond
= new_cond(cond
);
3180 /* if-true body (child 1) */
3181 tmpVar
= new_node0(IR_VAR
);
3182 tmpVar
->Store
= tmpDecl
->Store
;
3183 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3184 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3186 /* if-false body (child 2) */
3187 tmpVar
= new_node0(IR_VAR
);
3188 tmpVar
->Store
= tmpDecl
->Store
;
3189 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3190 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3192 ifNode
= new_if(cond
, trueNode
, falseNode
);
3195 tmpVar
= new_node0(IR_VAR
);
3196 tmpVar
->Store
= tmpDecl
->Store
;
3198 tree
= new_seq(ifNode
, tmpVar
);
3199 tree
= new_seq(tmpDecl
, tree
);
3201 /*_slang_print_ir_tree(tree, 10);*/
3207 * Generate code for &&.
3209 static slang_ir_node
*
3210 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3212 /* rewrite "a && b" as "a ? b : false" */
3213 slang_operation
*select
;
3216 select
= slang_operation_new(1);
3217 select
->type
= SLANG_OPER_SELECT
;
3218 select
->num_children
= 3;
3219 select
->children
= slang_operation_new(3);
3221 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3222 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3223 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3224 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3225 select
->children
[2].literal_size
= 1;
3227 n
= _slang_gen_select(A
, select
);
3233 * Generate code for ||.
3235 static slang_ir_node
*
3236 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3238 /* rewrite "a || b" as "a ? true : b" */
3239 slang_operation
*select
;
3242 select
= slang_operation_new(1);
3243 select
->type
= SLANG_OPER_SELECT
;
3244 select
->num_children
= 3;
3245 select
->children
= slang_operation_new(3);
3247 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3248 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3249 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3250 select
->children
[1].literal_size
= 1;
3251 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3253 n
= _slang_gen_select(A
, select
);
3259 * Generate IR tree for a return statement.
3261 static slang_ir_node
*
3262 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3264 const GLboolean haveReturnValue
3265 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3267 /* error checking */
3268 assert(A
->CurFunction
);
3269 if (haveReturnValue
&&
3270 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3271 slang_info_log_error(A
->log
, "illegal return expression");
3274 else if (!haveReturnValue
&&
3275 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3276 slang_info_log_error(A
->log
, "return statement requires an expression");
3280 if (!haveReturnValue
) {
3281 return new_return(A
->curFuncEndLabel
);
3289 * return; // goto __endOfFunction
3291 slang_operation
*assign
;
3292 slang_atom a_retVal
;
3295 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3301 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3303 /* trying to return a value in a void-valued function */
3309 assign
= slang_operation_new(1);
3310 assign
->type
= SLANG_OPER_ASSIGN
;
3311 assign
->num_children
= 2;
3312 assign
->children
= slang_operation_new(2);
3313 /* lhs (__retVal) */
3314 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3315 assign
->children
[0].a_id
= a_retVal
;
3316 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3318 /* XXX we might be able to avoid this copy someday */
3319 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3321 /* assemble the new code */
3322 n
= new_seq(_slang_gen_operation(A
, assign
),
3323 new_return(A
->curFuncEndLabel
));
3325 slang_operation_delete(assign
);
3333 * Determine if the given operation/expression is const-valued.
3336 _slang_is_constant_expr(const slang_operation
*oper
)
3338 slang_variable
*var
;
3341 switch (oper
->type
) {
3342 case SLANG_OPER_IDENTIFIER
:
3343 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3344 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3348 for (i
= 0; i
< oper
->num_children
; i
++) {
3349 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3359 * Check if an assignment of type t1 to t0 is legal.
3360 * XXX more cases needed.
3363 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3364 slang_operation
*op0
,
3365 slang_operation
*op1
)
3367 slang_typeinfo t0
, t1
;
3370 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3371 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3375 slang_typeinfo_construct(&t0
);
3376 typeof_operation(A
, op0
, &t0
);
3378 slang_typeinfo_construct(&t1
);
3379 typeof_operation(A
, op1
, &t1
);
3381 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3382 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3386 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3391 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3392 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3393 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3396 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3397 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3400 #if 0 /* not used just yet - causes problems elsewhere */
3401 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3402 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3406 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3407 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3410 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3411 t1
.spec
.type
== SLANG_SPEC_INT
)
3419 * Generate IR tree for a local variable declaration.
3420 * Basically do some error checking and call _slang_gen_var_decl().
3422 static slang_ir_node
*
3423 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3425 const char *varName
= (char *) oper
->a_id
;
3426 slang_variable
*var
;
3427 slang_ir_node
*varDecl
;
3428 slang_operation
*initializer
;
3430 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3431 assert(oper
->num_children
<= 1);
3433 /* lookup the variable by name */
3434 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3436 return NULL
; /* "shouldn't happen" */
3438 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3439 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3440 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3441 /* can't declare attribute/uniform vars inside functions */
3442 slang_info_log_error(A
->log
,
3443 "local variable '%s' cannot be an attribute/uniform/varying",
3450 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3455 /* check if the var has an initializer */
3456 if (oper
->num_children
> 0) {
3457 assert(oper
->num_children
== 1);
3458 initializer
= &oper
->children
[0];
3460 else if (var
->initializer
) {
3461 initializer
= var
->initializer
;
3468 /* check/compare var type and initializer type */
3469 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3470 slang_info_log_error(A
->log
, "incompatible types in assignment");
3475 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3476 slang_info_log_error(A
->log
,
3477 "const-qualified variable '%s' requires initializer",
3483 /* Generate IR node */
3484 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3493 * Generate IR tree for a reference to a variable (such as in an expression).
3494 * This is different from a variable declaration.
3496 static slang_ir_node
*
3497 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3499 /* If there's a variable associated with this oper (from inlining)
3500 * use it. Otherwise, use the oper's var id.
3502 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3503 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3506 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3509 assert(var
->declared
);
3510 n
= new_var(A
, var
);
3517 * Return the number of components actually named by the swizzle.
3518 * Recall that swizzles may have undefined/don't-care values.
3521 swizzle_size(GLuint swizzle
)
3524 for (i
= 0; i
< 4; i
++) {
3525 GLuint swz
= GET_SWZ(swizzle
, i
);
3526 size
+= (swz
>= 0 && swz
<= 3);
3532 static slang_ir_node
*
3533 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3535 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3539 n
->Store
= _slang_new_ir_storage_relative(0,
3540 swizzle_size(swizzle
),
3542 n
->Store
->Swizzle
= swizzle
;
3549 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3551 while (store
->Parent
)
3552 store
= store
->Parent
;
3554 if (!(store
->File
== PROGRAM_OUTPUT
||
3555 store
->File
== PROGRAM_TEMPORARY
||
3556 (store
->File
== PROGRAM_VARYING
&&
3557 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3567 * Walk up an IR storage path to compute the final swizzle.
3568 * This is used when we find an expression such as "foo.xz.yx".
3571 root_swizzle(const slang_ir_storage
*st
)
3573 GLuint swizzle
= st
->Swizzle
;
3574 while (st
->Parent
) {
3576 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3583 * Generate IR tree for an assignment (=).
3585 static slang_ir_node
*
3586 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3588 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3589 /* Check that var is writeable */
3591 = _slang_variable_locate(oper
->children
[0].locals
,
3592 oper
->children
[0].a_id
, GL_TRUE
);
3594 slang_info_log_error(A
->log
, "undefined variable '%s'",
3595 (char *) oper
->children
[0].a_id
);
3598 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3599 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3600 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3601 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3602 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3603 slang_info_log_error(A
->log
,
3604 "illegal assignment to read-only variable '%s'",
3605 (char *) oper
->children
[0].a_id
);
3610 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3611 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3612 /* Special case of: x = f(a, b)
3613 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3615 * XXX this could be even more effective if we could accomodate
3616 * cases such as "v.x = f();" - would help with typical vertex
3620 n
= _slang_gen_function_call_name(A
,
3621 (const char *) oper
->children
[1].a_id
,
3622 &oper
->children
[1], &oper
->children
[0]);
3626 slang_ir_node
*n
, *lhs
, *rhs
;
3628 /* lhs and rhs type checking */
3629 if (!_slang_assignment_compatible(A
,
3631 &oper
->children
[1])) {
3632 slang_info_log_error(A
->log
, "incompatible types in assignment");
3636 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3642 slang_info_log_error(A
->log
,
3643 "invalid left hand side for assignment");
3647 /* check that lhs is writable */
3648 if (!is_store_writable(A
, lhs
->Store
)) {
3649 slang_info_log_error(A
->log
,
3650 "illegal assignment to read-only l-value");
3654 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3656 /* convert lhs swizzle into writemask */
3657 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3658 GLuint writemask
, newSwizzle
;
3659 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3660 /* Non-simple writemask, need to swizzle right hand side in
3661 * order to put components into the right place.
3663 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3665 n
= new_node2(IR_COPY
, lhs
, rhs
);
3676 * Generate IR tree for referencing a field in a struct (or basic vector type)
3678 static slang_ir_node
*
3679 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3683 /* type of struct */
3684 slang_typeinfo_construct(&ti
);
3685 typeof_operation(A
, &oper
->children
[0], &ti
);
3687 if (_slang_type_is_vector(ti
.spec
.type
)) {
3688 /* the field should be a swizzle */
3689 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3693 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3694 slang_info_log_error(A
->log
, "Bad swizzle");
3697 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3702 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3703 /* create new parent node with swizzle */
3705 n
= _slang_gen_swizzle(n
, swizzle
);
3708 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3709 || ti
.spec
.type
== SLANG_SPEC_INT
3710 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3711 const GLuint rows
= 1;
3715 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3716 slang_info_log_error(A
->log
, "Bad swizzle");
3718 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3722 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3723 /* create new parent node with swizzle */
3724 n
= _slang_gen_swizzle(n
, swizzle
);
3728 /* the field is a structure member (base.field) */
3729 /* oper->children[0] is the base */
3730 /* oper->a_id is the field name */
3731 slang_ir_node
*base
, *n
;
3732 slang_typeinfo field_ti
;
3733 GLint fieldSize
, fieldOffset
= -1;
3736 slang_typeinfo_construct(&field_ti
);
3737 typeof_operation(A
, oper
, &field_ti
);
3739 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3741 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3743 if (fieldSize
== 0 || fieldOffset
< 0) {
3744 const char *structName
;
3745 if (ti
.spec
._struct
)
3746 structName
= (char *) ti
.spec
._struct
->a_name
;
3748 structName
= "unknown";
3749 slang_info_log_error(A
->log
,
3750 "\"%s\" is not a member of struct \"%s\"",
3751 (char *) oper
->a_id
, structName
);
3754 assert(fieldSize
>= 0);
3756 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3758 /* error msg should have already been logged */
3762 n
= new_node1(IR_FIELD
, base
);
3766 n
->Field
= (char *) oper
->a_id
;
3768 /* Store the field's offset in storage->Index */
3769 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3779 * Gen code for array indexing.
3781 static slang_ir_node
*
3782 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3784 slang_typeinfo array_ti
;
3786 /* get array's type info */
3787 slang_typeinfo_construct(&array_ti
);
3788 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3790 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3791 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3792 /* translate the index into a swizzle/writemask: "v.x=p" */
3793 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3797 index
= (GLint
) oper
->children
[1].literal
[0];
3798 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3799 index
>= (GLint
) max
) {
3801 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3802 printf("type = %d\n", oper
->children
[1].type
);
3803 printf("index = %d, max = %d\n", index
, max
);
3804 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3805 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3812 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3814 /* use swizzle to access the element */
3815 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3819 n
= _slang_gen_swizzle(n
, swizzle
);
3825 /* conventional array */
3826 slang_typeinfo elem_ti
;
3827 slang_ir_node
*elem
, *array
, *index
;
3828 GLint elemSize
, arrayLen
;
3830 /* size of array element */
3831 slang_typeinfo_construct(&elem_ti
);
3832 typeof_operation(A
, oper
, &elem_ti
);
3833 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3835 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3836 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3838 arrayLen
= array_ti
.array_len
;
3840 slang_typeinfo_destruct(&array_ti
);
3841 slang_typeinfo_destruct(&elem_ti
);
3843 if (elemSize
<= 0) {
3844 /* unknown var or type */
3845 slang_info_log_error(A
->log
, "Undefined variable or type");
3849 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3850 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3851 if (array
&& index
) {
3853 GLint constIndex
= -1;
3854 if (index
->Opcode
== IR_FLOAT
) {
3855 constIndex
= (int) index
->Value
[0];
3856 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3857 slang_info_log_error(A
->log
,
3858 "Array index out of bounds (index=%d size=%d)",
3859 constIndex
, arrayLen
);
3860 _slang_free_ir_tree(array
);
3861 _slang_free_ir_tree(index
);
3866 if (!array
->Store
) {
3867 slang_info_log_error(A
->log
, "Invalid array");
3871 elem
= new_node2(IR_ELEMENT
, array
, index
);
3873 /* The storage info here will be updated during code emit */
3874 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3875 array
->Store
->Index
,
3877 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3881 _slang_free_ir_tree(array
);
3882 _slang_free_ir_tree(index
);
3889 static slang_ir_node
*
3890 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3891 slang_ir_opcode opcode
)
3893 slang_typeinfo t0
, t1
;
3896 slang_typeinfo_construct(&t0
);
3897 typeof_operation(A
, &oper
->children
[0], &t0
);
3899 slang_typeinfo_construct(&t1
);
3900 typeof_operation(A
, &oper
->children
[0], &t1
);
3902 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3903 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3904 slang_info_log_error(A
->log
, "Illegal array comparison");
3908 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3909 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3910 /* <, <=, >, >= can only be used with scalars */
3911 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3912 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3913 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3914 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3915 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3920 n
= new_node2(opcode
,
3921 _slang_gen_operation(A
, &oper
->children
[0]),
3922 _slang_gen_operation(A
, &oper
->children
[1]));
3924 /* result is a bool (size 1) */
3925 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3933 print_vars(slang_variable_scope
*s
)
3937 for (i
= 0; i
< s
->num_variables
; i
++) {
3939 (char*) s
->variables
[i
]->a_name
,
3940 s
->variables
[i
]->declared
);
3950 _slang_undeclare_vars(slang_variable_scope
*locals
)
3952 if (locals
->num_variables
> 0) {
3954 for (i
= 0; i
< locals
->num_variables
; i
++) {
3955 slang_variable
*v
= locals
->variables
[i
];
3956 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3957 v
->declared
= GL_FALSE
;
3965 * Generate IR tree for a slang_operation (AST node)
3967 static slang_ir_node
*
3968 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3970 switch (oper
->type
) {
3971 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3975 _slang_push_var_table(A
->vartable
);
3977 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3978 n
= _slang_gen_operation(A
, oper
);
3979 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3981 _slang_pop_var_table(A
->vartable
);
3983 /*_slang_undeclare_vars(oper->locals);*/
3984 /*print_vars(oper->locals);*/
3987 n
= new_node1(IR_SCOPE
, n
);
3992 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3993 /* list of operations */
3994 if (oper
->num_children
> 0)
3996 slang_ir_node
*n
, *tree
= NULL
;
3999 for (i
= 0; i
< oper
->num_children
; i
++) {
4000 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4002 _slang_free_ir_tree(tree
);
4003 return NULL
; /* error must have occured */
4005 tree
= new_seq(tree
, n
);
4011 return new_node0(IR_NOP
);
4014 case SLANG_OPER_EXPRESSION
:
4015 return _slang_gen_operation(A
, &oper
->children
[0]);
4017 case SLANG_OPER_FOR
:
4018 return _slang_gen_for(A
, oper
);
4020 return _slang_gen_do(A
, oper
);
4021 case SLANG_OPER_WHILE
:
4022 return _slang_gen_while(A
, oper
);
4023 case SLANG_OPER_BREAK
:
4025 slang_info_log_error(A
->log
, "'break' not in loop");
4028 return new_break(A
->CurLoop
);
4029 case SLANG_OPER_CONTINUE
:
4031 slang_info_log_error(A
->log
, "'continue' not in loop");
4034 return _slang_gen_continue(A
, oper
);
4035 case SLANG_OPER_DISCARD
:
4036 return new_node0(IR_KILL
);
4038 case SLANG_OPER_EQUAL
:
4039 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4040 case SLANG_OPER_NOTEQUAL
:
4041 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4042 case SLANG_OPER_GREATER
:
4043 return _slang_gen_compare(A
, oper
, IR_SGT
);
4044 case SLANG_OPER_LESS
:
4045 return _slang_gen_compare(A
, oper
, IR_SLT
);
4046 case SLANG_OPER_GREATEREQUAL
:
4047 return _slang_gen_compare(A
, oper
, IR_SGE
);
4048 case SLANG_OPER_LESSEQUAL
:
4049 return _slang_gen_compare(A
, oper
, IR_SLE
);
4050 case SLANG_OPER_ADD
:
4053 assert(oper
->num_children
== 2);
4054 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4057 case SLANG_OPER_SUBTRACT
:
4060 assert(oper
->num_children
== 2);
4061 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4064 case SLANG_OPER_MULTIPLY
:
4067 assert(oper
->num_children
== 2);
4068 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4071 case SLANG_OPER_DIVIDE
:
4074 assert(oper
->num_children
== 2);
4075 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4078 case SLANG_OPER_MINUS
:
4081 assert(oper
->num_children
== 1);
4082 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4085 case SLANG_OPER_PLUS
:
4086 /* +expr --> do nothing */
4087 return _slang_gen_operation(A
, &oper
->children
[0]);
4088 case SLANG_OPER_VARIABLE_DECL
:
4089 return _slang_gen_declaration(A
, oper
);
4090 case SLANG_OPER_ASSIGN
:
4091 return _slang_gen_assignment(A
, oper
);
4092 case SLANG_OPER_ADDASSIGN
:
4095 assert(oper
->num_children
== 2);
4096 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4099 case SLANG_OPER_SUBASSIGN
:
4102 assert(oper
->num_children
== 2);
4103 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4107 case SLANG_OPER_MULASSIGN
:
4110 assert(oper
->num_children
== 2);
4111 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4114 case SLANG_OPER_DIVASSIGN
:
4117 assert(oper
->num_children
== 2);
4118 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4121 case SLANG_OPER_LOGICALAND
:
4124 assert(oper
->num_children
== 2);
4125 n
= _slang_gen_logical_and(A
, oper
);
4128 case SLANG_OPER_LOGICALOR
:
4131 assert(oper
->num_children
== 2);
4132 n
= _slang_gen_logical_or(A
, oper
);
4135 case SLANG_OPER_LOGICALXOR
:
4136 return _slang_gen_xor(A
, oper
);
4137 case SLANG_OPER_NOT
:
4138 return _slang_gen_not(A
, oper
);
4139 case SLANG_OPER_SELECT
: /* b ? x : y */
4142 assert(oper
->num_children
== 3);
4143 n
= _slang_gen_select(A
, oper
);
4147 case SLANG_OPER_ASM
:
4148 return _slang_gen_asm(A
, oper
, NULL
);
4149 case SLANG_OPER_CALL
:
4150 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4152 case SLANG_OPER_METHOD
:
4153 return _slang_gen_method_call(A
, oper
);
4154 case SLANG_OPER_RETURN
:
4155 return _slang_gen_return(A
, oper
);
4156 case SLANG_OPER_LABEL
:
4157 return new_label(oper
->label
);
4158 case SLANG_OPER_IDENTIFIER
:
4159 return _slang_gen_variable(A
, oper
);
4161 return _slang_gen_if(A
, oper
);
4162 case SLANG_OPER_FIELD
:
4163 return _slang_gen_struct_field(A
, oper
);
4164 case SLANG_OPER_SUBSCRIPT
:
4165 return _slang_gen_array_element(A
, oper
);
4166 case SLANG_OPER_LITERAL_FLOAT
:
4168 case SLANG_OPER_LITERAL_INT
:
4170 case SLANG_OPER_LITERAL_BOOL
:
4171 return new_float_literal(oper
->literal
, oper
->literal_size
);
4173 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4176 assert(oper
->num_children
== 1);
4177 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4180 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4183 assert(oper
->num_children
== 1);
4184 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4187 case SLANG_OPER_PREINCREMENT
: /* ++var */
4190 assert(oper
->num_children
== 1);
4191 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4194 case SLANG_OPER_PREDECREMENT
: /* --var */
4197 assert(oper
->num_children
== 1);
4198 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4202 case SLANG_OPER_NON_INLINED_CALL
:
4203 case SLANG_OPER_SEQUENCE
:
4205 slang_ir_node
*tree
= NULL
;
4207 for (i
= 0; i
< oper
->num_children
; i
++) {
4208 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4209 tree
= new_seq(tree
, n
);
4211 tree
->Store
= n
->Store
;
4213 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4214 tree
= new_function_call(tree
, oper
->label
);
4219 case SLANG_OPER_NONE
:
4220 case SLANG_OPER_VOID
:
4221 /* returning NULL here would generate an error */
4222 return new_node0(IR_NOP
);
4225 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4227 return new_node0(IR_NOP
);
4235 * Called by compiler when a global variable has been parsed/compiled.
4236 * Here we examine the variable's type to determine what kind of register
4237 * storage will be used.
4239 * A uniform such as "gl_Position" will become the register specification
4240 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4241 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4243 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4244 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4245 * actual texture unit (as specified by the user calling glUniform1i()).
4248 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4249 slang_unit_type type
)
4251 struct gl_program
*prog
= A
->program
;
4252 const char *varName
= (char *) var
->a_name
;
4253 GLboolean success
= GL_TRUE
;
4254 slang_ir_storage
*store
= NULL
;
4256 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4257 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4258 const GLint arrayLen
= _slang_array_length(var
);
4259 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4260 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4262 /* check for sampler2D arrays */
4263 if (texIndex
== -1 && var
->type
.specifier
._array
)
4264 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4266 if (texIndex
!= -1) {
4267 /* This is a texture sampler variable...
4268 * store->File = PROGRAM_SAMPLER
4269 * store->Index = sampler number (0..7, typically)
4270 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4272 if (var
->initializer
) {
4273 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4276 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4277 /* disallow rect samplers */
4278 if ((var
->type
.specifier
._array
&&
4279 (var
->type
.specifier
._array
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4280 var
->type
.specifier
._array
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
)) ||
4281 (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4282 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
)) {
4283 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4288 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4289 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4291 /* If we have a sampler array, then we need to allocate the
4292 * additional samplers to ensure we don't allocate them elsewhere.
4293 * We can't directly use _mesa_add_sampler() as that checks the
4294 * varName and gets a match, so we call _mesa_add_parameter()
4295 * directly and use the last sampler number from the call above.
4298 GLint a
= arrayLen
- 1;
4300 for (i
= 0; i
< a
; i
++) {
4301 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4302 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4303 varName
, 1, datatype
, &value
, NULL
, 0x0);
4307 if (dbg
) printf("SAMPLER ");
4309 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4310 /* Uniform variable */
4311 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4314 /* user-defined uniform */
4315 if (datatype
== GL_NONE
) {
4316 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4317 /* temporary work-around */
4318 GLenum datatype
= GL_FLOAT
;
4319 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4320 totalSize
, datatype
, NULL
);
4321 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4322 totalSize
, swizzle
);
4324 /* XXX what we need to do is unroll the struct into its
4325 * basic types, creating a uniform variable for each.
4333 * Should produce uniforms:
4334 * "f.a" (GL_FLOAT_VEC3)
4335 * "f.b" (GL_FLOAT_VEC4)
4338 if (var
->initializer
) {
4339 slang_info_log_error(A
->log
,
4340 "unsupported initializer for uniform '%s'", varName
);
4345 slang_info_log_error(A
->log
,
4346 "invalid datatype for uniform variable %s",
4352 /* non-struct uniform */
4353 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4359 /* pre-defined uniform, like gl_ModelviewMatrix */
4360 /* We know it's a uniform, but don't allocate storage unless
4363 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4364 totalSize
, swizzle
);
4366 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4368 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4369 /* varyings must be float, vec or mat */
4370 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4371 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4372 slang_info_log_error(A
->log
,
4373 "varying '%s' must be float/vector/matrix",
4378 if (var
->initializer
) {
4379 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4385 /* user-defined varying */
4391 if (var
->type
.centroid
== SLANG_CENTROID
)
4392 flags
|= PROG_PARAM_BIT_CENTROID
;
4393 if (var
->type
.variant
== SLANG_INVARIANT
)
4394 flags
|= PROG_PARAM_BIT_INVARIANT
;
4396 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4398 swizzle
= _slang_var_swizzle(size
, 0);
4399 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4400 totalSize
, swizzle
);
4403 /* pre-defined varying, like gl_Color or gl_TexCoord */
4404 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4405 /* fragment program input */
4407 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4410 assert(index
< FRAG_ATTRIB_MAX
);
4411 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4415 /* vertex program output */
4416 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4417 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4419 assert(index
< VERT_RESULT_MAX
);
4420 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4421 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4424 if (dbg
) printf("V/F ");
4426 if (dbg
) printf("VARYING ");
4428 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4431 /* attributes must be float, vec or mat */
4432 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4433 slang_info_log_error(A
->log
,
4434 "attribute '%s' must be float/vector/matrix",
4440 /* user-defined vertex attribute */
4441 const GLint attr
= -1; /* unknown */
4442 swizzle
= _slang_var_swizzle(size
, 0);
4443 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4444 size
, datatype
, attr
);
4446 index
= VERT_ATTRIB_GENERIC0
+ index
;
4449 /* pre-defined vertex attrib */
4450 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4453 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4454 if (dbg
) printf("ATTRIB ");
4456 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4457 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4458 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4460 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4461 if (dbg
) printf("INPUT ");
4463 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4464 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4465 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4466 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4469 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4470 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4471 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4472 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4474 if (dbg
) printf("OUTPUT ");
4476 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4477 /* pre-defined global constant, like gl_MaxLights */
4478 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4479 if (dbg
) printf("CONST ");
4482 /* ordinary variable (may be const) */
4485 /* IR node to declare the variable */
4486 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4488 /* emit GPU instructions */
4489 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4491 _slang_free_ir_tree(n
);
4494 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4495 store
? store
->Index
: -2);
4498 var
->store
= store
; /* save var's storage info */
4500 var
->declared
= GL_TRUE
;
4507 * Produce an IR tree from a function AST (fun->body).
4508 * Then call the code emitter to convert the IR tree into gl_program
4512 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4515 GLboolean success
= GL_TRUE
;
4517 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4518 /* we only really generate code for main, all other functions get
4519 * inlined or codegen'd upon an actual call.
4522 /* do some basic error checking though */
4523 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4524 /* check that non-void functions actually return something */
4526 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4528 slang_info_log_error(A
->log
,
4529 "function \"%s\" has no return statement",
4530 (char *) fun
->header
.a_name
);
4532 "function \"%s\" has no return statement\n",
4533 (char *) fun
->header
.a_name
);
4538 return GL_TRUE
; /* not an error */
4542 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4543 slang_print_function(fun
, 1);
4546 /* should have been allocated earlier: */
4547 assert(A
->program
->Parameters
);
4548 assert(A
->program
->Varying
);
4549 assert(A
->vartable
);
4551 A
->CurFunction
= fun
;
4553 /* fold constant expressions, etc. */
4554 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4557 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4558 slang_print_function(fun
, 1);
4561 /* Create an end-of-function label */
4562 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4564 /* push new vartable scope */
4565 _slang_push_var_table(A
->vartable
);
4567 /* Generate IR tree for the function body code */
4568 n
= _slang_gen_operation(A
, fun
->body
);
4570 n
= new_node1(IR_SCOPE
, n
);
4572 /* pop vartable, restore previous */
4573 _slang_pop_var_table(A
->vartable
);
4576 /* XXX record error */
4580 /* append an end-of-function-label to IR tree */
4581 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4583 /*_slang_label_delete(A->curFuncEndLabel);*/
4584 A
->curFuncEndLabel
= NULL
;
4587 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4588 slang_print_function(fun
, 1);
4591 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4592 _slang_print_ir_tree(n
, 0);
4595 printf("************* End codegen function ************\n\n");
4598 /* Emit program instructions */
4599 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4600 _slang_free_ir_tree(n
);
4602 /* free codegen context */
4604 _mesa_free(A->codegen);