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_builtin.h"
50 #include "slang_codegen.h"
51 #include "slang_compile.h"
52 #include "slang_label.h"
53 #include "slang_mem.h"
54 #include "slang_simplify.h"
55 #include "slang_emit.h"
56 #include "slang_vartable.h"
58 #include "slang_print.h"
61 /** Max iterations to unroll */
62 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS
= 32;
64 /** Max for-loop body size (in slang operations) to unroll */
65 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
67 /** Max for-loop body complexity to unroll.
68 * We'll compute complexity as the product of the number of iterations
69 * and the size of the body. So long-ish loops with very simple bodies
70 * can be unrolled, as well as short loops with larger bodies.
72 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY
= 256;
76 static slang_ir_node
*
77 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
80 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
81 GLuint substCount
, slang_variable
**substOld
,
82 slang_operation
**substNew
, GLboolean isLHS
);
86 * Retrieves type information about an operation.
87 * Returns GL_TRUE on success.
88 * Returns GL_FALSE otherwise.
91 typeof_operation(const struct slang_assemble_ctx_
*A
,
95 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
100 is_sampler_type(const slang_fully_specified_type
*t
)
102 switch (t
->specifier
.type
) {
103 case SLANG_SPEC_SAMPLER1D
:
104 case SLANG_SPEC_SAMPLER2D
:
105 case SLANG_SPEC_SAMPLER3D
:
106 case SLANG_SPEC_SAMPLERCUBE
:
107 case SLANG_SPEC_SAMPLER1DSHADOW
:
108 case SLANG_SPEC_SAMPLER2DSHADOW
:
109 case SLANG_SPEC_SAMPLER2DRECT
:
110 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
119 * Return the offset (in floats or ints) of the named field within
120 * the given struct. Return -1 if field not found.
121 * If field is NULL, return the size of the struct instead.
124 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
128 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
129 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
130 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
132 /* types larger than 1 float are register (4-float) aligned */
133 offset
= (offset
+ 3) & ~3;
135 if (field
&& v
->a_name
== field
) {
141 return -1; /* field not found */
143 return offset
; /* struct size */
148 * Return the size (in floats) of the given type specifier.
149 * If the size is greater than 4, the size should be a multiple of 4
150 * so that the correct number of 4-float registers are allocated.
151 * For example, a mat3x2 is size 12 because we want to store the
152 * 3 columns in 3 float[4] registers.
155 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
158 switch (spec
->type
) {
159 case SLANG_SPEC_VOID
:
162 case SLANG_SPEC_BOOL
:
165 case SLANG_SPEC_BVEC2
:
168 case SLANG_SPEC_BVEC3
:
171 case SLANG_SPEC_BVEC4
:
177 case SLANG_SPEC_IVEC2
:
180 case SLANG_SPEC_IVEC3
:
183 case SLANG_SPEC_IVEC4
:
186 case SLANG_SPEC_FLOAT
:
189 case SLANG_SPEC_VEC2
:
192 case SLANG_SPEC_VEC3
:
195 case SLANG_SPEC_VEC4
:
198 case SLANG_SPEC_MAT2
:
199 sz
= 2 * 4; /* 2 columns (regs) */
201 case SLANG_SPEC_MAT3
:
204 case SLANG_SPEC_MAT4
:
207 case SLANG_SPEC_MAT23
:
208 sz
= 2 * 4; /* 2 columns (regs) */
210 case SLANG_SPEC_MAT32
:
211 sz
= 3 * 4; /* 3 columns (regs) */
213 case SLANG_SPEC_MAT24
:
216 case SLANG_SPEC_MAT42
:
217 sz
= 4 * 4; /* 4 columns (regs) */
219 case SLANG_SPEC_MAT34
:
222 case SLANG_SPEC_MAT43
:
223 sz
= 4 * 4; /* 4 columns (regs) */
225 case SLANG_SPEC_SAMPLER1D
:
226 case SLANG_SPEC_SAMPLER2D
:
227 case SLANG_SPEC_SAMPLER3D
:
228 case SLANG_SPEC_SAMPLERCUBE
:
229 case SLANG_SPEC_SAMPLER1DSHADOW
:
230 case SLANG_SPEC_SAMPLER2DSHADOW
:
231 case SLANG_SPEC_SAMPLER2DRECT
:
232 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
233 sz
= 1; /* a sampler is basically just an integer index */
235 case SLANG_SPEC_STRUCT
:
236 sz
= _slang_field_offset(spec
, 0); /* special use */
238 /* 1-float structs are actually troublesome to deal with since they
239 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
240 * ensure the object is placed at R.x
245 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
248 case SLANG_SPEC_ARRAY
:
249 sz
= _slang_sizeof_type_specifier(spec
->_array
);
252 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
257 /* if size is > 4, it should be a multiple of four */
258 assert((sz
& 0x3) == 0);
265 * Query variable/array length (number of elements).
266 * This is slightly non-trivial because there are two ways to express
267 * arrays: "float x[3]" vs. "float[3] x".
268 * \return the length of the array for the given variable, or 0 if not an array
271 _slang_array_length(const slang_variable
*var
)
273 if (var
->type
.array_len
> 0) {
274 /* Ex: float[4] x; */
275 return var
->type
.array_len
;
277 if (var
->array_len
> 0) {
278 /* Ex: float x[4]; */
279 return var
->array_len
;
286 * Compute total size of array give size of element, number of elements.
287 * \return size in floats
290 _slang_array_size(GLint elemSize
, GLint arrayLen
)
293 assert(elemSize
> 0);
295 /* round up base type to multiple of 4 */
296 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
306 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
307 * or -1 if the type is not a sampler.
310 sampler_to_texture_index(const slang_type_specifier_type type
)
313 case SLANG_SPEC_SAMPLER1D
:
314 return TEXTURE_1D_INDEX
;
315 case SLANG_SPEC_SAMPLER2D
:
316 return TEXTURE_2D_INDEX
;
317 case SLANG_SPEC_SAMPLER3D
:
318 return TEXTURE_3D_INDEX
;
319 case SLANG_SPEC_SAMPLERCUBE
:
320 return TEXTURE_CUBE_INDEX
;
321 case SLANG_SPEC_SAMPLER1DSHADOW
:
322 return TEXTURE_1D_INDEX
; /* XXX fix */
323 case SLANG_SPEC_SAMPLER2DSHADOW
:
324 return TEXTURE_2D_INDEX
; /* XXX fix */
325 case SLANG_SPEC_SAMPLER2DRECT
:
326 return TEXTURE_RECT_INDEX
;
327 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
328 return TEXTURE_RECT_INDEX
; /* XXX fix */
335 /** helper to build a SLANG_OPER_IDENTIFIER node */
337 slang_operation_identifier(slang_operation
*oper
,
338 slang_assemble_ctx
*A
,
341 oper
->type
= SLANG_OPER_IDENTIFIER
;
342 oper
->a_id
= slang_atom_pool_atom(A
->atoms
, name
);
347 * Called when we begin code/IR generation for a new while/do/for loop.
350 push_loop(slang_assemble_ctx
*A
, slang_operation
*loopOper
, slang_ir_node
*loopIR
)
352 A
->LoopOperStack
[A
->LoopDepth
] = loopOper
;
353 A
->LoopIRStack
[A
->LoopDepth
] = loopIR
;
359 * Called when we end code/IR generation for a new while/do/for loop.
362 pop_loop(slang_assemble_ctx
*A
)
364 assert(A
->LoopDepth
> 0);
370 * Return pointer to slang_operation for the loop we're currently inside,
371 * or NULL if not in a loop.
373 static const slang_operation
*
374 current_loop_oper(const slang_assemble_ctx
*A
)
376 if (A
->LoopDepth
> 0)
377 return A
->LoopOperStack
[A
->LoopDepth
- 1];
384 * Return pointer to slang_ir_node for the loop we're currently inside,
385 * or NULL if not in a loop.
387 static slang_ir_node
*
388 current_loop_ir(const slang_assemble_ctx
*A
)
390 if (A
->LoopDepth
> 0)
391 return A
->LoopIRStack
[A
->LoopDepth
- 1];
397 /**********************************************************************/
401 * Map "_asm foo" to IR_FOO, etc.
406 slang_ir_opcode Opcode
;
407 GLuint HaveRetValue
, NumParams
;
411 static slang_asm_info AsmInfo
[] = {
413 { "vec4_add", IR_ADD
, 1, 2 },
414 { "vec4_subtract", IR_SUB
, 1, 2 },
415 { "vec4_multiply", IR_MUL
, 1, 2 },
416 { "vec4_dot", IR_DOT4
, 1, 2 },
417 { "vec3_dot", IR_DOT3
, 1, 2 },
418 { "vec2_dot", IR_DOT2
, 1, 2 },
419 { "vec3_nrm", IR_NRM3
, 1, 1 },
420 { "vec4_nrm", IR_NRM4
, 1, 1 },
421 { "vec3_cross", IR_CROSS
, 1, 2 },
422 { "vec4_lrp", IR_LRP
, 1, 3 },
423 { "vec4_min", IR_MIN
, 1, 2 },
424 { "vec4_max", IR_MAX
, 1, 2 },
425 { "vec4_clamp", IR_CLAMP
, 1, 3 },
426 { "vec4_seq", IR_SEQUAL
, 1, 2 },
427 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
428 { "vec4_sge", IR_SGE
, 1, 2 },
429 { "vec4_sgt", IR_SGT
, 1, 2 },
430 { "vec4_sle", IR_SLE
, 1, 2 },
431 { "vec4_slt", IR_SLT
, 1, 2 },
433 { "vec4_move", IR_MOVE
, 1, 1 },
434 { "vec4_floor", IR_FLOOR
, 1, 1 },
435 { "vec4_frac", IR_FRAC
, 1, 1 },
436 { "vec4_abs", IR_ABS
, 1, 1 },
437 { "vec4_negate", IR_NEG
, 1, 1 },
438 { "vec4_ddx", IR_DDX
, 1, 1 },
439 { "vec4_ddy", IR_DDY
, 1, 1 },
440 /* float binary op */
441 { "float_power", IR_POW
, 1, 2 },
442 /* texture / sampler */
443 { "vec4_tex_1d", IR_TEX
, 1, 2 },
444 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
445 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
446 { "vec4_tex_2d", IR_TEX
, 1, 2 },
447 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
448 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
449 { "vec4_tex_3d", IR_TEX
, 1, 2 },
450 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
451 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
452 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
453 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
454 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
456 /* texture / sampler but with shadow comparison */
457 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
458 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
459 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
460 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
461 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
462 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
463 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
464 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
467 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
468 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
469 { "float_exp", IR_EXP
, 1, 1 },
470 { "float_exp2", IR_EXP2
, 1, 1 },
471 { "float_log2", IR_LOG2
, 1, 1 },
472 { "float_rsq", IR_RSQ
, 1, 1 },
473 { "float_rcp", IR_RCP
, 1, 1 },
474 { "float_sine", IR_SIN
, 1, 1 },
475 { "float_cosine", IR_COS
, 1, 1 },
476 { "float_noise1", IR_NOISE1
, 1, 1},
477 { "float_noise2", IR_NOISE2
, 1, 1},
478 { "float_noise3", IR_NOISE3
, 1, 1},
479 { "float_noise4", IR_NOISE4
, 1, 1},
481 { NULL
, IR_NOP
, 0, 0 }
485 static slang_ir_node
*
486 new_node3(slang_ir_opcode op
,
487 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
489 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
495 n
->InstLocation
= -1;
500 static slang_ir_node
*
501 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
503 return new_node3(op
, c0
, c1
, NULL
);
506 static slang_ir_node
*
507 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
509 return new_node3(op
, c0
, NULL
, NULL
);
512 static slang_ir_node
*
513 new_node0(slang_ir_opcode op
)
515 return new_node3(op
, NULL
, NULL
, NULL
);
520 * Create sequence of two nodes.
522 static slang_ir_node
*
523 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
529 return new_node2(IR_SEQ
, left
, right
);
532 static slang_ir_node
*
533 new_label(slang_label
*label
)
535 slang_ir_node
*n
= new_node0(IR_LABEL
);
542 static slang_ir_node
*
543 new_float_literal(const float v
[4], GLuint size
)
545 slang_ir_node
*n
= new_node0(IR_FLOAT
);
547 COPY_4V(n
->Value
, v
);
548 /* allocate a storage object, but compute actual location (Index) later */
549 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
554 static slang_ir_node
*
555 new_not(slang_ir_node
*n
)
557 return new_node1(IR_NOT
, n
);
562 * Non-inlined function call.
564 static slang_ir_node
*
565 new_function_call(slang_ir_node
*code
, slang_label
*name
)
567 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
576 * Unconditional jump.
578 static slang_ir_node
*
579 new_return(slang_label
*dest
)
581 slang_ir_node
*n
= new_node0(IR_RETURN
);
589 static slang_ir_node
*
590 new_loop(slang_ir_node
*body
)
592 return new_node1(IR_LOOP
, body
);
596 static slang_ir_node
*
597 new_break(slang_ir_node
*loopNode
)
599 slang_ir_node
*n
= new_node0(IR_BREAK
);
601 assert(loopNode
->Opcode
== IR_LOOP
);
603 /* insert this node at head of linked list of cont/break instructions */
604 n
->List
= loopNode
->List
;
612 * Make new IR_BREAK_IF_TRUE.
614 static slang_ir_node
*
615 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
617 slang_ir_node
*loopNode
= current_loop_ir(A
);
620 assert(loopNode
->Opcode
== IR_LOOP
);
621 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
623 /* insert this node at head of linked list of cont/break instructions */
624 n
->List
= loopNode
->List
;
632 * Make new IR_CONT_IF_TRUE node.
634 static slang_ir_node
*
635 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
637 slang_ir_node
*loopNode
= current_loop_ir(A
);
640 assert(loopNode
->Opcode
== IR_LOOP
);
641 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
643 n
->Parent
= loopNode
; /* pointer to containing loop */
644 /* insert this node at head of linked list of cont/break instructions */
645 n
->List
= loopNode
->List
;
652 static slang_ir_node
*
653 new_cond(slang_ir_node
*n
)
655 slang_ir_node
*c
= new_node1(IR_COND
, n
);
660 static slang_ir_node
*
661 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
663 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
668 * New IR_VAR node - a reference to a previously declared variable.
670 static slang_ir_node
*
671 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
673 slang_ir_node
*n
= new_node0(IR_VAR
);
680 /* Set IR node's Var and Store pointers */
682 n
->Store
= var
->store
;
689 * Check if the given function is really just a wrapper for a
690 * basic assembly instruction.
693 slang_is_asm_function(const slang_function
*fun
)
695 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
696 fun
->body
->num_children
== 1 &&
697 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
705 _slang_is_noop(const slang_operation
*oper
)
708 oper
->type
== SLANG_OPER_VOID
||
709 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
717 * Recursively search tree for a node of the given type.
720 static slang_operation
*
721 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
724 if (oper
->type
== type
)
726 for (i
= 0; i
< oper
->num_children
; i
++) {
727 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
737 * Count the number of operations of the given time rooted at 'oper'.
740 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
743 if (oper
->type
== type
) {
746 for (i
= 0; i
< oper
->num_children
; i
++) {
747 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
754 * Check if the 'return' statement found under 'oper' is a "tail return"
755 * that can be no-op'd. For example:
760 * return; // this is a no-op
763 * This is used when determining if a function can be inlined. If the
764 * 'return' is not the last statement, we can't inline the function since
765 * we still need the semantic behaviour of the 'return' but we don't want
766 * to accidentally return from the _calling_ function. We'd need to use an
767 * unconditional branch, but we don't have such a GPU instruction (not
771 _slang_is_tail_return(const slang_operation
*oper
)
773 GLuint k
= oper
->num_children
;
776 const slang_operation
*last
= &oper
->children
[k
- 1];
777 if (last
->type
== SLANG_OPER_RETURN
)
779 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
780 last
->type
== SLANG_OPER_LABEL
)
781 k
--; /* try prev child */
782 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
783 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
784 /* try sub-children */
785 return _slang_is_tail_return(last
);
795 * Generate a variable declaration opeartion.
796 * I.e.: generate AST code for "bool flag = false;"
799 slang_generate_declaration(slang_assemble_ctx
*A
,
800 slang_variable_scope
*scope
,
801 slang_operation
*decl
,
802 slang_type_specifier_type type
,
808 assert(type
== SLANG_SPEC_BOOL
||
809 type
== SLANG_SPEC_INT
);
811 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
813 var
= slang_variable_scope_grow(scope
);
815 slang_fully_specified_type_construct(&var
->type
);
817 var
->type
.specifier
.type
= type
;
818 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
819 decl
->a_id
= var
->a_name
;
820 var
->initializer
= slang_operation_new(1);
821 slang_operation_literal_bool(var
->initializer
, initValue
);
826 slang_resolve_variable(slang_operation
*oper
)
828 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
829 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
835 * Rewrite AST code for "return expression;".
837 * We return values from functions by assinging the returned value to
838 * the hidden __retVal variable which is an extra 'out' parameter we add
839 * to the function signature.
840 * This code basically converts "return expr;" into "__retVal = expr; return;"
842 * \return the new AST code.
844 static slang_operation
*
845 gen_return_with_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
847 slang_operation
*blockOper
, *assignOper
;
849 assert(oper
->type
== SLANG_OPER_RETURN
);
851 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
852 slang_info_log_error(A
->log
, "illegal return expression");
856 blockOper
= slang_operation_new(1);
857 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
858 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
859 slang_operation_add_children(blockOper
, 2);
861 if (A
->UseReturnFlag
) {
870 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
871 ifOper
->type
= SLANG_OPER_IF
;
872 slang_operation_add_children(ifOper
, 3);
874 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
875 cond
->type
= SLANG_OPER_IDENTIFIER
;
876 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
879 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
880 elseOper
->type
= SLANG_OPER_VOID
;
882 assignOper
= slang_oper_child(ifOper
, 1);
885 slang_operation
*setOper
= slang_oper_child(blockOper
, 1);
886 setOper
->type
= SLANG_OPER_ASSIGN
;
887 slang_operation_add_children(setOper
, 2);
889 slang_operation
*lhs
= slang_oper_child(setOper
, 0);
890 lhs
->type
= SLANG_OPER_IDENTIFIER
;
891 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
894 slang_operation
*rhs
= slang_oper_child(setOper
, 1);
895 slang_operation_literal_bool(rhs
, GL_FALSE
);
906 assignOper
= slang_oper_child(blockOper
, 0);
908 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
909 returnOper
->type
= SLANG_OPER_RETURN_INLINED
;
910 assert(returnOper
->num_children
== 0);
914 /* __retVal = expression; */
915 assignOper
->type
= SLANG_OPER_ASSIGN
;
916 slang_operation_add_children(assignOper
, 2);
918 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
919 lhs
->type
= SLANG_OPER_IDENTIFIER
;
920 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
923 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
924 slang_operation_copy(rhs
, &oper
->children
[0]);
927 ///blockOper->locals->outer_scope = oper->locals->outer_scope;
929 /*slang_print_tree(blockOper, 0);*/
936 * Rewrite AST code for "return;" (no expression).
938 static slang_operation
*
939 gen_return_without_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
941 slang_operation
*newRet
;
943 assert(oper
->type
== SLANG_OPER_RETURN
);
945 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
946 slang_info_log_error(A
->log
, "return statement requires an expression");
950 if (A
->UseReturnFlag
) {
955 newRet
= slang_operation_new(1);
956 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
957 newRet
->type
= SLANG_OPER_ASSIGN
;
958 slang_operation_add_children(newRet
, 2);
960 slang_operation
*lhs
= slang_oper_child(newRet
, 0);
961 lhs
->type
= SLANG_OPER_IDENTIFIER
;
962 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
965 slang_operation
*rhs
= slang_oper_child(newRet
, 1);
966 slang_operation_literal_bool(rhs
, GL_FALSE
);
974 newRet
= slang_operation_new(1);
975 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
976 newRet
->type
= SLANG_OPER_RETURN_INLINED
;
979 /*slang_print_tree(newRet, 0);*/
988 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
991 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
992 GLuint substCount
, slang_variable
**substOld
,
993 slang_operation
**substNew
, GLboolean isLHS
)
995 switch (oper
->type
) {
996 case SLANG_OPER_VARIABLE_DECL
:
998 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
999 oper
->a_id
, GL_TRUE
);
1001 if (v
->initializer
&& oper
->num_children
== 0) {
1002 /* set child of oper to copy of initializer */
1003 oper
->num_children
= 1;
1004 oper
->children
= slang_operation_new(1);
1005 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1007 if (oper
->num_children
== 1) {
1008 /* the initializer */
1009 slang_substitute(A
, &oper
->children
[0], substCount
,
1010 substOld
, substNew
, GL_FALSE
);
1014 case SLANG_OPER_IDENTIFIER
:
1015 assert(oper
->num_children
== 0);
1016 if (1/**!isLHS XXX FIX */) {
1017 slang_atom id
= oper
->a_id
;
1020 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1022 if (_mesa_strcmp((char *) oper
->a_id
, "__notRetFlag"))
1023 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1027 /* look for a substitution */
1028 for (i
= 0; i
< substCount
; i
++) {
1029 if (v
== substOld
[i
]) {
1030 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1031 #if 0 /* DEBUG only */
1032 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1033 assert(substNew
[i
]->var
);
1034 assert(substNew
[i
]->var
->a_name
);
1035 printf("Substitute %s with %s in id node %p\n",
1036 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1040 printf("Substitute %s with %f in id node %p\n",
1041 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1045 slang_operation_copy(oper
, substNew
[i
]);
1052 case SLANG_OPER_RETURN
:
1054 slang_operation
*newReturn
;
1055 /* generate new 'return' code' */
1056 if (slang_oper_child(oper
, 0)->type
== SLANG_OPER_VOID
)
1057 newReturn
= gen_return_without_expression(A
, oper
);
1059 newReturn
= gen_return_with_expression(A
, oper
);
1064 /* do substitutions on the new 'return' code */
1065 slang_substitute(A
, newReturn
,
1066 substCount
, substOld
, substNew
, GL_FALSE
);
1068 /* install new 'return' code */
1069 slang_operation_copy(oper
, newReturn
);
1070 slang_operation_destruct(newReturn
);
1074 case SLANG_OPER_ASSIGN
:
1075 case SLANG_OPER_SUBSCRIPT
:
1077 * child[0] can't have substitutions but child[1] can.
1079 slang_substitute(A
, &oper
->children
[0],
1080 substCount
, substOld
, substNew
, GL_TRUE
);
1081 slang_substitute(A
, &oper
->children
[1],
1082 substCount
, substOld
, substNew
, GL_FALSE
);
1084 case SLANG_OPER_FIELD
:
1085 /* XXX NEW - test */
1086 slang_substitute(A
, &oper
->children
[0],
1087 substCount
, substOld
, substNew
, GL_TRUE
);
1092 for (i
= 0; i
< oper
->num_children
; i
++)
1093 slang_substitute(A
, &oper
->children
[i
],
1094 substCount
, substOld
, substNew
, GL_FALSE
);
1101 * Produce inline code for a call to an assembly instruction.
1102 * This is typically used to compile a call to a built-in function like this:
1104 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1106 * __asm vec4_lrp __retVal, a, y, x;
1111 * r = mix(p1, p2, p3);
1121 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1123 static slang_operation
*
1124 slang_inline_asm_function(slang_assemble_ctx
*A
,
1125 slang_function
*fun
, slang_operation
*oper
)
1127 const GLuint numArgs
= oper
->num_children
;
1129 slang_operation
*inlined
;
1130 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1131 slang_variable
**substOld
;
1132 slang_operation
**substNew
;
1134 ASSERT(slang_is_asm_function(fun
));
1135 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1138 printf("Inline %s as %s\n",
1139 (char*) fun->header.a_name,
1140 (char*) fun->body->children[0].a_id);
1144 * We'll substitute formal params with actual args in the asm call.
1146 substOld
= (slang_variable
**)
1147 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1148 substNew
= (slang_operation
**)
1149 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1150 for (i
= 0; i
< numArgs
; i
++) {
1151 substOld
[i
] = fun
->parameters
->variables
[i
];
1152 substNew
[i
] = oper
->children
+ i
;
1155 /* make a copy of the code to inline */
1156 inlined
= slang_operation_new(1);
1157 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1159 /* get rid of the __retVal child */
1160 inlined
->num_children
--;
1161 for (i
= 0; i
< inlined
->num_children
; i
++) {
1162 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1166 /* now do formal->actual substitutions */
1167 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1169 _slang_free(substOld
);
1170 _slang_free(substNew
);
1173 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1174 (char *) fun
->header
.a_name
);
1175 slang_print_tree(inlined
, 3);
1176 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1184 * Inline the given function call operation.
1185 * Return a new slang_operation that corresponds to the inlined code.
1187 static slang_operation
*
1188 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1189 slang_operation
*oper
, slang_operation
*returnOper
)
1196 ParamMode
*paramMode
;
1197 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1198 const GLuint numArgs
= oper
->num_children
;
1199 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1200 slang_operation
*args
= oper
->children
;
1201 slang_operation
*inlined
, *top
;
1202 slang_variable
**substOld
;
1203 slang_operation
**substNew
;
1204 GLuint substCount
, numCopyIn
, i
;
1205 slang_function
*prevFunction
;
1206 slang_variable_scope
*newScope
= NULL
;
1209 prevFunction
= A
->CurFunction
;
1210 A
->CurFunction
= fun
;
1212 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1213 assert(fun
->param_count
== totalArgs
);
1215 /* allocate temporary arrays */
1216 paramMode
= (ParamMode
*)
1217 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1218 substOld
= (slang_variable
**)
1219 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1220 substNew
= (slang_operation
**)
1221 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1224 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1225 (char *) fun
->header
.a_name
,
1226 fun
->parameters
->num_variables
, numArgs
);
1229 if (haveRetValue
&& !returnOper
) {
1230 /* Create 3-child comma sequence for inlined code:
1231 * child[0]: declare __resultTmp
1232 * child[1]: inlined function body
1233 * child[2]: __resultTmp
1235 slang_operation
*commaSeq
;
1236 slang_operation
*declOper
= NULL
;
1237 slang_variable
*resultVar
;
1239 commaSeq
= slang_operation_new(1);
1240 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1241 assert(commaSeq
->locals
);
1242 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1243 commaSeq
->num_children
= 3;
1244 commaSeq
->children
= slang_operation_new(3);
1245 /* allocate the return var */
1246 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1248 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1249 (void*)commaSeq->locals, (char *) fun->header.a_name);
1252 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1253 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1254 resultVar
->isTemp
= GL_TRUE
;
1256 /* child[0] = __resultTmp declaration */
1257 declOper
= &commaSeq
->children
[0];
1258 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1259 declOper
->a_id
= resultVar
->a_name
;
1260 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1262 /* child[1] = function body */
1263 inlined
= &commaSeq
->children
[1];
1264 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1266 /* child[2] = __resultTmp reference */
1267 returnOper
= &commaSeq
->children
[2];
1268 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1269 returnOper
->a_id
= resultVar
->a_name
;
1270 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1275 top
= inlined
= slang_operation_new(1);
1276 /* XXXX this may be inappropriate!!!! */
1277 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1281 assert(inlined
->locals
);
1283 /* Examine the parameters, look for inout/out params, look for possible
1284 * substitutions, etc:
1285 * param type behaviour
1286 * in copy actual to local
1287 * const in substitute param with actual
1291 for (i
= 0; i
< totalArgs
; i
++) {
1292 slang_variable
*p
= fun
->parameters
->variables
[i
];
1294 printf("Param %d: %s %s \n", i,
1295 slang_type_qual_string(p->type.qualifier),
1296 (char *) p->a_name);
1298 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1299 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1300 /* an output param */
1301 slang_operation
*arg
;
1306 paramMode
[i
] = SUBST
;
1308 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1309 slang_resolve_variable(arg
);
1311 /* replace parameter 'p' with argument 'arg' */
1312 substOld
[substCount
] = p
;
1313 substNew
[substCount
] = arg
; /* will get copied */
1316 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1317 /* a constant input param */
1318 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1319 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
||
1320 args
[i
].type
== SLANG_OPER_SUBSCRIPT
) {
1321 /* replace all occurances of this parameter variable with the
1322 * actual argument variable or a literal.
1324 paramMode
[i
] = SUBST
;
1325 slang_resolve_variable(&args
[i
]);
1326 substOld
[substCount
] = p
;
1327 substNew
[substCount
] = &args
[i
]; /* will get copied */
1331 paramMode
[i
] = COPY_IN
;
1335 paramMode
[i
] = COPY_IN
;
1337 assert(paramMode
[i
]);
1340 /* actual code inlining: */
1341 slang_operation_copy(inlined
, fun
->body
);
1343 /*** XXX review this */
1344 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1345 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1346 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1349 printf("======================= orig body code ======================\n");
1350 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1351 slang_print_tree(fun
->body
, 8);
1352 printf("======================= copied code =========================\n");
1353 slang_print_tree(inlined
, 8);
1356 /* do parameter substitution in inlined code: */
1357 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1360 printf("======================= subst code ==========================\n");
1361 slang_print_tree(inlined
, 8);
1362 printf("=============================================================\n");
1365 /* New prolog statements: (inserted before the inlined code)
1366 * Copy the 'in' arguments.
1369 for (i
= 0; i
< numArgs
; i
++) {
1370 if (paramMode
[i
] == COPY_IN
) {
1371 slang_variable
*p
= fun
->parameters
->variables
[i
];
1372 /* declare parameter 'p' */
1373 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1377 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1378 assert(decl
->locals
);
1379 decl
->locals
->outer_scope
= inlined
->locals
;
1380 decl
->a_id
= p
->a_name
;
1381 decl
->num_children
= 1;
1382 decl
->children
= slang_operation_new(1);
1384 /* child[0] is the var's initializer */
1385 slang_operation_copy(&decl
->children
[0], args
+ i
);
1387 /* add parameter 'p' to the local variable scope here */
1389 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1390 pCopy
->type
= p
->type
;
1391 pCopy
->a_name
= p
->a_name
;
1392 pCopy
->array_len
= p
->array_len
;
1395 newScope
= inlined
->locals
;
1400 /* Now add copies of the function's local vars to the new variable scope */
1401 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1402 slang_variable
*p
= fun
->parameters
->variables
[i
];
1403 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1404 pCopy
->type
= p
->type
;
1405 pCopy
->a_name
= p
->a_name
;
1406 pCopy
->array_len
= p
->array_len
;
1410 /* New epilog statements:
1411 * 1. Create end of function label to jump to from return statements.
1412 * 2. Copy the 'out' parameter vars
1415 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1417 inlined
->num_children
);
1418 lab
->type
= SLANG_OPER_LABEL
;
1419 lab
->label
= A
->curFuncEndLabel
;
1422 for (i
= 0; i
< totalArgs
; i
++) {
1423 if (paramMode
[i
] == COPY_OUT
) {
1424 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1425 /* actualCallVar = outParam */
1426 /*if (i > 0 || !haveRetValue)*/
1427 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1429 inlined
->num_children
);
1430 ass
->type
= SLANG_OPER_ASSIGN
;
1431 ass
->num_children
= 2;
1432 ass
->locals
->outer_scope
= inlined
->locals
;
1433 ass
->children
= slang_operation_new(2);
1434 ass
->children
[0] = args
[i
]; /*XXX copy */
1435 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1436 ass
->children
[1].a_id
= p
->a_name
;
1437 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1441 _slang_free(paramMode
);
1442 _slang_free(substOld
);
1443 _slang_free(substNew
);
1445 /* Update scoping to use the new local vars instead of the
1446 * original function's vars. This is especially important
1447 * for nested inlining.
1450 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1453 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1454 (char *) fun
->header
.a_name
,
1455 fun
->parameters
->num_variables
, numArgs
);
1456 slang_print_tree(top
, 0);
1460 A
->CurFunction
= prevFunction
;
1467 * Insert declaration for "bool __notRetFlag" in given block operation.
1468 * This is used when we can't emit "early" return statements in subroutines.
1471 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1473 slang_operation
*decl
;
1475 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1476 oper
->type
== SLANG_OPER_SEQUENCE
);
1478 decl
= slang_operation_insert_child(oper
, 1);
1480 slang_generate_declaration(A
, oper
->locals
, decl
,
1481 SLANG_SPEC_BOOL
, "__notRetFlag", GL_TRUE
);
1483 /*slang_print_tree(oper, 0);*/
1488 * Recursively replace instances of the old node type with the new type.
1491 replace_node_type(slang_operation
*oper
, slang_operation_type oldType
,
1492 slang_operation_type newType
)
1496 if (oper
->type
== oldType
)
1497 oper
->type
= newType
;
1499 for (i
= 0; i
< slang_oper_num_children(oper
); i
++) {
1500 replace_node_type(slang_oper_child(oper
, i
), oldType
, newType
);
1507 * Test if the given function body has an "early return". That is, there's
1508 * a 'return' statement that's not the very last instruction in the body.
1511 has_early_return(const slang_operation
*funcBody
)
1513 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1516 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1524 * Emit IR code for a function call. This does one of two things:
1525 * 1. Inline the function's code
1526 * 2. Create an IR for the function's body and create a real call to it.
1528 static slang_ir_node
*
1529 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1530 slang_operation
*oper
, slang_operation
*dest
)
1533 slang_operation
*instance
;
1534 slang_label
*prevFuncEndLabel
;
1537 prevFuncEndLabel
= A
->curFuncEndLabel
;
1538 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1539 A
->curFuncEndLabel
= _slang_label_new(name
);
1540 assert(A
->curFuncEndLabel
);
1543 * 'instance' is basically a copy of the function's body with various
1547 if (slang_is_asm_function(fun
) && !dest
) {
1548 /* assemble assembly function - tree style */
1549 instance
= slang_inline_asm_function(A
, fun
, oper
);
1552 /* non-assembly function */
1553 /* We always generate an "inline-able" block of code here.
1555 * 1. insert the inline code
1556 * 2. Generate a call to the "inline" code as a subroutine
1558 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1560 if (earlyReturn
&& !A
->EmitContReturn
) {
1561 A
->UseReturnFlag
= GL_TRUE
;
1564 instance
= slang_inline_function_call(A
, fun
, oper
, dest
);
1569 /* The function we're calling has one or more 'return' statements
1570 * that prevent us from inlining the function's code.
1572 * In this case, change the function's body type from
1573 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1574 * During code emit this will result in a true subroutine call.
1576 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1578 slang_operation
*callOper
;
1580 assert(instance
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1581 instance
->type
== SLANG_OPER_SEQUENCE
);
1583 if (_slang_function_has_return_value(fun
) && !dest
) {
1584 assert(instance
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1585 assert(instance
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1586 callOper
= &instance
->children
[1];
1589 callOper
= instance
;
1592 if (A
->UseReturnFlag
) {
1593 /* Early returns not supported. Create a _returnFlag variable
1594 * that's set upon 'return' and tested elsewhere to no-op any
1595 * remaining instructions in the subroutine.
1597 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1598 callOper
->type
== SLANG_OPER_SEQUENCE
);
1599 declare_return_flag(A
, callOper
);
1602 /* We can emit real 'return' statements. If we generated any
1603 * 'inline return' statements during function instantiation,
1604 * change them back to regular 'return' statements.
1606 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1610 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1611 callOper
->fun
= fun
;
1612 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1615 /* If there are any 'return' statements remaining, they're at the
1616 * very end of the function and can effectively become no-ops.
1618 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1626 /* Replace the function call with the instance block (or new CALL stmt) */
1627 slang_operation_destruct(oper
);
1629 _slang_free(instance
);
1632 assert(instance
->locals
);
1633 printf("*** Inlined code for call to %s:\n", (char*) fun
->header
.a_name
);
1634 slang_print_tree(oper
, 10);
1638 n
= _slang_gen_operation(A
, oper
);
1640 /*_slang_label_delete(A->curFuncEndLabel);*/
1641 A
->curFuncEndLabel
= prevFuncEndLabel
;
1643 if (A
->pragmas
->Debug
) {
1645 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1646 n
->Comment
= _slang_strdup(s
);
1649 A
->UseReturnFlag
= GL_FALSE
;
1655 static slang_asm_info
*
1656 slang_find_asm_info(const char *name
)
1659 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1660 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1669 * Some write-masked assignments are simple, but others are hard.
1672 * v.xy = vec2(a, b);
1675 * v.zy = vec2(a, b);
1676 * this gets transformed/swizzled into:
1677 * v.zy = vec2(a, b).*yx* (* = don't care)
1678 * This function helps to determine simple vs. non-simple.
1681 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1683 switch (writemask
) {
1685 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1687 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1689 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1691 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1693 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1694 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1696 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1697 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1698 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1699 case WRITEMASK_XYZW
:
1700 return swizzle
== SWIZZLE_NOOP
;
1708 * Convert the given swizzle into a writemask. In some cases this
1709 * is trivial, in other cases, we'll need to also swizzle the right
1710 * hand side to put components in the right places.
1711 * See comment above for more info.
1712 * XXX this function could be simplified and should probably be renamed.
1713 * \param swizzle the incoming swizzle
1714 * \param writemaskOut returns the writemask
1715 * \param swizzleOut swizzle to apply to the right-hand-side
1716 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1719 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1720 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1722 GLuint mask
= 0x0, newSwizzle
[4];
1725 /* make new dst writemask, compute size */
1726 for (i
= 0; i
< 4; i
++) {
1727 const GLuint swz
= GET_SWZ(swizzle
, i
);
1728 if (swz
== SWIZZLE_NIL
) {
1732 assert(swz
>= 0 && swz
<= 3);
1734 if (swizzle
!= SWIZZLE_XXXX
&&
1735 swizzle
!= SWIZZLE_YYYY
&&
1736 swizzle
!= SWIZZLE_ZZZZ
&&
1737 swizzle
!= SWIZZLE_WWWW
&&
1738 (mask
& (1 << swz
))) {
1739 /* a channel can't be specified twice (ex: ".xyyz") */
1740 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1741 _mesa_swizzle_string(swizzle
, 0, 0));
1747 assert(mask
<= 0xf);
1748 size
= i
; /* number of components in mask/swizzle */
1750 *writemaskOut
= mask
;
1752 /* make new src swizzle, by inversion */
1753 for (i
= 0; i
< 4; i
++) {
1754 newSwizzle
[i
] = i
; /*identity*/
1756 for (i
= 0; i
< size
; i
++) {
1757 const GLuint swz
= GET_SWZ(swizzle
, i
);
1758 newSwizzle
[swz
] = i
;
1760 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1765 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1767 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1769 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1771 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1773 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1781 #if 0 /* not used, but don't remove just yet */
1783 * Recursively traverse 'oper' to produce a swizzle mask in the event
1784 * of any vector subscripts and swizzle suffixes.
1785 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1788 resolve_swizzle(const slang_operation
*oper
)
1790 if (oper
->type
== SLANG_OPER_FIELD
) {
1791 /* writemask from .xyzw suffix */
1793 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1794 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1798 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1799 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1803 return SWIZZLE_XYZW
;
1805 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1806 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1807 /* writemask from [index] */
1808 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1809 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1814 swizzle
= SWIZZLE_XXXX
;
1817 swizzle
= SWIZZLE_YYYY
;
1820 swizzle
= SWIZZLE_ZZZZ
;
1823 swizzle
= SWIZZLE_WWWW
;
1826 swizzle
= SWIZZLE_XYZW
;
1828 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1832 return SWIZZLE_XYZW
;
1840 * Recursively descend through swizzle nodes to find the node's storage info.
1842 static slang_ir_storage
*
1843 get_store(const slang_ir_node
*n
)
1845 if (n
->Opcode
== IR_SWIZZLE
) {
1846 return get_store(n
->Children
[0]);
1854 * Generate IR tree for an asm instruction/operation such as:
1855 * __asm vec4_dot __retVal.x, v1, v2;
1857 static slang_ir_node
*
1858 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1859 slang_operation
*dest
)
1861 const slang_asm_info
*info
;
1862 slang_ir_node
*kids
[3], *n
;
1863 GLuint j
, firstOperand
;
1865 assert(oper
->type
== SLANG_OPER_ASM
);
1867 info
= slang_find_asm_info((char *) oper
->a_id
);
1869 _mesa_problem(NULL
, "undefined __asm function %s\n",
1870 (char *) oper
->a_id
);
1873 assert(info
->NumParams
<= 3);
1875 if (info
->NumParams
== oper
->num_children
) {
1876 /* Storage for result is not specified.
1877 * Children[0], [1], [2] are the operands.
1882 /* Storage for result (child[0]) is specified.
1883 * Children[1], [2], [3] are the operands.
1888 /* assemble child(ren) */
1889 kids
[0] = kids
[1] = kids
[2] = NULL
;
1890 for (j
= 0; j
< info
->NumParams
; j
++) {
1891 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1896 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1899 /* Setup n->Store to be a particular location. Otherwise, storage
1900 * for the result (a temporary) will be allocated later.
1902 slang_operation
*dest_oper
;
1905 dest_oper
= &oper
->children
[0];
1907 n0
= _slang_gen_operation(A
, dest_oper
);
1912 n
->Store
= n0
->Store
;
1914 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1925 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1928 for (i
= 0; i
< scope
->num_functions
; i
++) {
1929 slang_function
*f
= &scope
->functions
[i
];
1930 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1931 printf(" %s (%d args)\n", name
, f
->param_count
);
1934 if (scope
->outer_scope
)
1935 print_funcs(scope
->outer_scope
, name
);
1941 * Find a function of the given name, taking 'numArgs' arguments.
1942 * This is the function we'll try to call when there is no exact match
1943 * between function parameters and call arguments.
1945 * XXX we should really create a list of candidate functions and try
1948 static slang_function
*
1949 _slang_find_function_by_argc(slang_function_scope
*scope
,
1950 const char *name
, int numArgs
)
1954 for (i
= 0; i
< scope
->num_functions
; i
++) {
1955 slang_function
*f
= &scope
->functions
[i
];
1956 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1957 int haveRetValue
= _slang_function_has_return_value(f
);
1958 if (numArgs
== f
->param_count
- haveRetValue
)
1962 scope
= scope
->outer_scope
;
1969 static slang_function
*
1970 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1973 slang_function
*maxFunc
= NULL
;
1978 for (i
= 0; i
< scope
->num_functions
; i
++) {
1979 slang_function
*f
= &scope
->functions
[i
];
1980 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1981 if (f
->param_count
> maxArgs
) {
1982 maxArgs
= f
->param_count
;
1987 scope
= scope
->outer_scope
;
1995 * Generate a new slang_function which is a constructor for a user-defined
1998 static slang_function
*
1999 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2001 const GLint numFields
= str
->fields
->num_variables
;
2002 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2004 /* function header (name, return type) */
2005 fun
->header
.a_name
= str
->a_name
;
2006 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2007 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2008 fun
->header
.type
.specifier
._struct
= str
;
2010 /* function parameters (= struct's fields) */
2013 for (i
= 0; i
< numFields
; i
++) {
2015 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2017 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2018 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2019 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2021 fun
->param_count
= fun
->parameters
->num_variables
;
2024 /* Add __retVal to params */
2026 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2027 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2029 p
->a_name
= a_retVal
;
2030 p
->type
= fun
->header
.type
;
2031 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2035 /* function body is:
2045 slang_variable_scope
*scope
;
2046 slang_variable
*var
;
2049 fun
->body
= slang_operation_new(1);
2050 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2051 fun
->body
->num_children
= numFields
+ 2;
2052 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2054 scope
= fun
->body
->locals
;
2055 scope
->outer_scope
= fun
->parameters
;
2057 /* create local var 't' */
2058 var
= slang_variable_scope_grow(scope
);
2059 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2060 var
->type
= fun
->header
.type
;
2064 slang_operation
*decl
;
2066 decl
= &fun
->body
->children
[0];
2067 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2068 decl
->locals
= _slang_variable_scope_new(scope
);
2069 decl
->a_id
= var
->a_name
;
2072 /* assign params to fields of t */
2073 for (i
= 0; i
< numFields
; i
++) {
2074 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2076 assign
->type
= SLANG_OPER_ASSIGN
;
2077 assign
->locals
= _slang_variable_scope_new(scope
);
2078 assign
->num_children
= 2;
2079 assign
->children
= slang_operation_new(2);
2082 slang_operation
*lhs
= &assign
->children
[0];
2084 lhs
->type
= SLANG_OPER_FIELD
;
2085 lhs
->locals
= _slang_variable_scope_new(scope
);
2086 lhs
->num_children
= 1;
2087 lhs
->children
= slang_operation_new(1);
2088 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2090 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2091 lhs
->children
[0].a_id
= var
->a_name
;
2092 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2095 lhs
->children
[1].num_children
= 1;
2096 lhs
->children
[1].children
= slang_operation_new(1);
2097 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2098 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2099 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2104 slang_operation
*rhs
= &assign
->children
[1];
2106 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2107 rhs
->locals
= _slang_variable_scope_new(scope
);
2108 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2114 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2116 ret
->type
= SLANG_OPER_RETURN
;
2117 ret
->locals
= _slang_variable_scope_new(scope
);
2118 ret
->num_children
= 1;
2119 ret
->children
= slang_operation_new(1);
2120 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2121 ret
->children
[0].a_id
= var
->a_name
;
2122 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2126 slang_print_function(fun, 1);
2133 * Find/create a function (constructor) for the given structure name.
2135 static slang_function
*
2136 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2139 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2140 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2141 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2142 /* found a structure type that matches the function name */
2143 if (!str
->constructor
) {
2144 /* create the constructor function now */
2145 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2147 return str
->constructor
;
2155 * Generate a new slang_function to satisfy a call to an array constructor.
2156 * Ex: float[3](1., 2., 3.)
2158 static slang_function
*
2159 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2161 slang_type_specifier_type baseType
;
2162 slang_function
*fun
;
2165 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2169 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2171 num_elements
= oper
->num_children
;
2173 /* function header, return type */
2175 fun
->header
.a_name
= oper
->a_id
;
2176 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2177 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2178 fun
->header
.type
.specifier
._array
=
2179 slang_type_specifier_new(baseType
, NULL
, NULL
);
2180 fun
->header
.type
.array_len
= num_elements
;
2183 /* function parameters (= number of elements) */
2186 for (i
= 0; i
< num_elements
; i
++) {
2188 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2190 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2192 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2193 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2194 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2195 p
->type
.specifier
.type
= baseType
;
2197 fun
->param_count
= fun
->parameters
->num_variables
;
2200 /* Add __retVal to params */
2202 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2203 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2205 p
->a_name
= a_retVal
;
2206 p
->type
= fun
->header
.type
;
2207 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2208 p
->type
.specifier
.type
= baseType
;
2212 /* function body is:
2222 slang_variable_scope
*scope
;
2223 slang_variable
*var
;
2226 fun
->body
= slang_operation_new(1);
2227 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2228 fun
->body
->num_children
= num_elements
+ 2;
2229 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2231 scope
= fun
->body
->locals
;
2232 scope
->outer_scope
= fun
->parameters
;
2234 /* create local var 't' */
2235 var
= slang_variable_scope_grow(scope
);
2236 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2237 var
->type
= fun
->header
.type
;/*XXX copy*/
2241 slang_operation
*decl
;
2243 decl
= &fun
->body
->children
[0];
2244 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2245 decl
->locals
= _slang_variable_scope_new(scope
);
2246 decl
->a_id
= var
->a_name
;
2249 /* assign params to elements of t */
2250 for (i
= 0; i
< num_elements
; i
++) {
2251 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2253 assign
->type
= SLANG_OPER_ASSIGN
;
2254 assign
->locals
= _slang_variable_scope_new(scope
);
2255 assign
->num_children
= 2;
2256 assign
->children
= slang_operation_new(2);
2259 slang_operation
*lhs
= &assign
->children
[0];
2261 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2262 lhs
->locals
= _slang_variable_scope_new(scope
);
2263 lhs
->num_children
= 2;
2264 lhs
->children
= slang_operation_new(2);
2266 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2267 lhs
->children
[0].a_id
= var
->a_name
;
2268 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2270 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2271 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2275 slang_operation
*rhs
= &assign
->children
[1];
2277 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2278 rhs
->locals
= _slang_variable_scope_new(scope
);
2279 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2285 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2287 ret
->type
= SLANG_OPER_RETURN
;
2288 ret
->locals
= _slang_variable_scope_new(scope
);
2289 ret
->num_children
= 1;
2290 ret
->children
= slang_operation_new(1);
2291 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2292 ret
->children
[0].a_id
= var
->a_name
;
2293 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2298 slang_print_function(fun, 1);
2306 _slang_is_vec_mat_type(const char *name
)
2308 static const char *vecmat_types
[] = {
2309 "float", "int", "bool",
2310 "vec2", "vec3", "vec4",
2311 "ivec2", "ivec3", "ivec4",
2312 "bvec2", "bvec3", "bvec4",
2313 "mat2", "mat3", "mat4",
2314 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2318 for (i
= 0; vecmat_types
[i
]; i
++)
2319 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2326 * Assemble a function call, given a particular function name.
2327 * \param name the function's name (operators like '*' are possible).
2329 static slang_ir_node
*
2330 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2331 slang_operation
*oper
, slang_operation
*dest
)
2333 slang_operation
*params
= oper
->children
;
2334 const GLuint param_count
= oper
->num_children
;
2336 slang_function
*fun
;
2339 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2340 if (atom
== SLANG_ATOM_NULL
)
2343 if (oper
->array_constructor
) {
2344 /* this needs special handling */
2345 fun
= _slang_make_array_constructor(A
, oper
);
2348 /* Try to find function by name and exact argument type matching */
2349 GLboolean error
= GL_FALSE
;
2350 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2351 &A
->space
, A
->atoms
, A
->log
, &error
);
2353 slang_info_log_error(A
->log
,
2354 "Function '%s' not found (check argument types)",
2361 /* Next, try locating a constructor function for a user-defined type */
2362 fun
= _slang_locate_struct_constructor(A
, name
);
2366 * At this point, some heuristics are used to try to find a function
2367 * that matches the calling signature by means of casting or "unrolling"
2371 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2372 /* Next, if this call looks like a vec() or mat() constructor call,
2373 * try "unwinding" the args to satisfy a constructor.
2375 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2377 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2378 slang_info_log_error(A
->log
,
2379 "Function '%s' not found (check argument types)",
2386 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2387 /* Next, try casting args to the types of the formal parameters */
2388 int numArgs
= oper
->num_children
;
2389 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2390 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2391 slang_info_log_error(A
->log
,
2392 "Function '%s' not found (check argument types)",
2400 slang_info_log_error(A
->log
,
2401 "Function '%s' not found (check argument types)",
2407 /* The function body may be in another compilation unit.
2408 * We'll try concatenating the shaders and recompile at link time.
2410 A
->UnresolvedRefs
= GL_TRUE
;
2411 return new_node1(IR_NOP
, NULL
);
2414 /* type checking to be sure function's return type matches 'dest' type */
2418 slang_typeinfo_construct(&t0
);
2419 typeof_operation(A
, dest
, &t0
);
2421 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2422 slang_info_log_error(A
->log
,
2423 "Incompatible type returned by call to '%s'",
2429 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2431 if (n
&& !n
->Store
&& !dest
2432 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2433 /* setup n->Store for the result of the function call */
2434 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2435 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2436 /*printf("Alloc storage for function result, size %d \n", size);*/
2439 if (oper
->array_constructor
) {
2440 /* free the temporary array constructor function now */
2441 slang_function_destruct(fun
);
2448 static slang_ir_node
*
2449 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2451 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2453 slang_variable
*var
;
2455 /* NOTE: In GLSL 1.20, there's only one kind of method
2456 * call: array.length(). Anything else is an error.
2458 if (oper
->a_id
!= a_length
) {
2459 slang_info_log_error(A
->log
,
2460 "Undefined method call '%s'", (char *) oper
->a_id
);
2464 /* length() takes no arguments */
2465 if (oper
->num_children
> 0) {
2466 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2470 /* lookup the object/variable */
2471 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2472 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2473 slang_info_log_error(A
->log
,
2474 "Undefined object '%s'", (char *) oper
->a_obj
);
2478 /* Create a float/literal IR node encoding the array length */
2479 n
= new_node0(IR_FLOAT
);
2481 n
->Value
[0] = (float) _slang_array_length(var
);
2482 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2489 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2491 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2492 oper
->type
== SLANG_OPER_LITERAL_INT
||
2493 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2494 if (oper
->literal
[0])
2500 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2501 oper
->num_children
== 1) {
2502 return _slang_is_constant_cond(&oper
->children
[0], value
);
2509 * Test if an operation is a scalar or boolean.
2512 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2514 slang_typeinfo type
;
2517 slang_typeinfo_construct(&type
);
2518 typeof_operation(A
, oper
, &type
);
2519 size
= _slang_sizeof_type_specifier(&type
.spec
);
2520 slang_typeinfo_destruct(&type
);
2526 * Test if an operation is boolean.
2529 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2531 slang_typeinfo type
;
2534 slang_typeinfo_construct(&type
);
2535 typeof_operation(A
, oper
, &type
);
2536 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2537 slang_typeinfo_destruct(&type
);
2543 * Check if a loop contains a 'continue' statement.
2544 * Stop looking if we find a nested loop.
2547 _slang_loop_contains_continue(const slang_operation
*oper
)
2549 switch (oper
->type
) {
2550 case SLANG_OPER_CONTINUE
:
2552 case SLANG_OPER_FOR
:
2554 case SLANG_OPER_WHILE
:
2555 /* stop upon finding a nested loop */
2561 for (i
= 0; i
< oper
->num_children
; i
++) {
2562 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2563 if (_slang_loop_contains_continue(child
))
2573 * Check if a loop contains a 'continue' or 'break' statement.
2574 * Stop looking if we find a nested loop.
2577 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2579 switch (oper
->type
) {
2580 case SLANG_OPER_CONTINUE
:
2581 case SLANG_OPER_BREAK
:
2583 case SLANG_OPER_FOR
:
2585 case SLANG_OPER_WHILE
:
2586 /* stop upon finding a nested loop */
2592 for (i
= 0; i
< oper
->num_children
; i
++) {
2593 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2594 if (_slang_loop_contains_continue_or_break(child
))
2604 * Replace 'break' and 'continue' statements inside a do and while loops.
2605 * This is a recursive helper function used by
2606 * _slang_gen_do/while_without_continue().
2609 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2611 switch (oper
->type
) {
2612 case SLANG_OPER_BREAK
:
2613 /* replace 'break' with "_notBreakFlag = false; break" */
2615 slang_operation
*block
= oper
;
2616 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2617 slang_operation_add_children(block
, 2);
2619 slang_operation
*assign
= slang_oper_child(block
, 0);
2620 assign
->type
= SLANG_OPER_ASSIGN
;
2621 slang_operation_add_children(assign
, 2);
2623 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2624 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2627 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2628 slang_operation_literal_bool(rhs
, GL_FALSE
);
2632 slang_operation
*brk
= slang_oper_child(block
, 1);
2633 brk
->type
= SLANG_OPER_BREAK
;
2634 assert(!brk
->children
);
2638 case SLANG_OPER_CONTINUE
:
2639 /* convert continue into a break */
2640 oper
->type
= SLANG_OPER_BREAK
;
2642 case SLANG_OPER_FOR
:
2644 case SLANG_OPER_WHILE
:
2645 /* stop upon finding a nested loop */
2651 for (i
= 0; i
< oper
->num_children
; i
++) {
2652 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2660 * Transform a while-loop so that continue statements are converted to breaks.
2661 * Then do normal IR code generation.
2665 * while (LOOPCOND) {
2677 * bool _notBreakFlag = 1;
2678 * while (_notBreakFlag && LOOPCOND) {
2682 * break; // was continue
2685 * _notBreakFlag = 0; // was
2692 static slang_ir_node
*
2693 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2695 slang_operation
*top
;
2696 slang_operation
*innerBody
;
2698 assert(oper
->type
== SLANG_OPER_WHILE
);
2700 top
= slang_operation_new(1);
2701 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2702 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2703 slang_operation_add_children(top
, 2);
2705 /* declare: bool _notBreakFlag = true */
2707 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2708 slang_generate_declaration(A
, top
->locals
, condDecl
,
2709 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2712 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2714 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2715 outerWhile
->type
= SLANG_OPER_WHILE
;
2716 slang_operation_add_children(outerWhile
, 2);
2718 /* _notBreakFlag && LOOPCOND */
2720 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2721 cond
->type
= SLANG_OPER_LOGICALAND
;
2722 slang_operation_add_children(cond
, 2);
2724 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2725 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2728 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2729 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2735 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2736 innerDo
->type
= SLANG_OPER_DO
;
2737 slang_operation_add_children(innerDo
, 2);
2739 /* copy original do-loop body into inner do-loop's body */
2740 innerBody
= slang_oper_child(innerDo
, 0);
2741 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2742 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2744 /* inner do-loop's condition is constant/false */
2746 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2747 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2752 /* Finally, in innerBody,
2753 * replace "break" with "_notBreakFlag = 0; break"
2754 * replace "continue" with "break"
2756 replace_break_and_cont(A
, innerBody
);
2758 /*slang_print_tree(top, 0);*/
2760 return _slang_gen_operation(A
, top
);
2767 * Generate loop code using high-level IR_LOOP instruction
2769 static slang_ir_node
*
2770 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2774 * BREAK if !expr (child[0])
2775 * body code (child[1])
2777 slang_ir_node
*loop
, *breakIf
, *body
;
2778 GLboolean isConst
, constTrue
= GL_FALSE
;
2780 if (!A
->EmitContReturn
) {
2781 /* We don't want to emit CONT instructions. If this while-loop has
2782 * a continue, translate it away.
2784 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2785 return _slang_gen_while_without_continue(A
, oper
);
2789 /* type-check expression */
2790 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2791 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2795 /* Check if loop condition is a constant */
2796 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2798 if (isConst
&& !constTrue
) {
2799 /* loop is never executed! */
2800 return new_node0(IR_NOP
);
2803 /* Begin new loop */
2804 loop
= new_loop(NULL
);
2806 /* save loop state */
2807 push_loop(A
, oper
, loop
);
2809 if (isConst
&& constTrue
) {
2810 /* while(nonzero constant), no conditional break */
2815 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2816 breakIf
= new_break_if_true(A
, cond
);
2818 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2819 loop
->Children
[0] = new_seq(breakIf
, body
);
2821 /* Do infinite loop detection */
2822 /* loop->List is head of linked list of break/continue nodes */
2823 if (!loop
->List
&& isConst
&& constTrue
) {
2824 /* infinite loop detected */
2826 slang_info_log_error(A
->log
, "Infinite loop detected!");
2830 /* restore loop state */
2838 * Transform a do-while-loop so that continue statements are converted to breaks.
2839 * Then do normal IR code generation.
2850 * } while (LOOPCOND);
2855 * bool _notBreakFlag = 1;
2860 * break; // was continue
2863 * _notBreakFlag = 0; // was
2867 * } while (_notBreakFlag && LOOPCOND);
2870 static slang_ir_node
*
2871 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2873 slang_operation
*top
;
2874 slang_operation
*innerBody
;
2876 assert(oper
->type
== SLANG_OPER_DO
);
2878 top
= slang_operation_new(1);
2879 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2880 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2881 slang_operation_add_children(top
, 2);
2883 /* declare: bool _notBreakFlag = true */
2885 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2886 slang_generate_declaration(A
, top
->locals
, condDecl
,
2887 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2890 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2892 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2893 outerDo
->type
= SLANG_OPER_DO
;
2894 slang_operation_add_children(outerDo
, 2);
2898 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2899 innerDo
->type
= SLANG_OPER_DO
;
2900 slang_operation_add_children(innerDo
, 2);
2902 /* copy original do-loop body into inner do-loop's body */
2903 innerBody
= slang_oper_child(innerDo
, 0);
2904 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2905 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2907 /* inner do-loop's condition is constant/false */
2909 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2910 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2914 /* _notBreakFlag && LOOPCOND */
2916 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2917 cond
->type
= SLANG_OPER_LOGICALAND
;
2918 slang_operation_add_children(cond
, 2);
2920 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2921 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2924 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2925 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2930 /* Finally, in innerBody,
2931 * replace "break" with "_notBreakFlag = 0; break"
2932 * replace "continue" with "break"
2934 replace_break_and_cont(A
, innerBody
);
2936 /*slang_print_tree(top, 0);*/
2938 return _slang_gen_operation(A
, top
);
2943 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2945 static slang_ir_node
*
2946 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2950 * body code (child[0])
2952 * BREAK if !expr (child[1])
2954 slang_ir_node
*loop
;
2955 GLboolean isConst
, constTrue
;
2957 if (!A
->EmitContReturn
) {
2958 /* We don't want to emit CONT instructions. If this do-loop has
2959 * a continue, translate it away.
2961 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2962 return _slang_gen_do_without_continue(A
, oper
);
2966 /* type-check expression */
2967 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2968 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2972 loop
= new_loop(NULL
);
2974 /* save loop state */
2975 push_loop(A
, oper
, loop
);
2978 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2980 /* Check if loop condition is a constant */
2981 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2982 if (isConst
&& constTrue
) {
2983 /* do { } while(1) ==> no conditional break */
2984 loop
->Children
[1] = NULL
; /* no tail code */
2988 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2989 loop
->Children
[1] = new_break_if_true(A
, cond
);
2992 /* XXX we should do infinite loop detection, as above */
2994 /* restore loop state */
3002 * Recursively count the number of operations rooted at 'oper'.
3003 * This gives some kind of indication of the size/complexity of an operation.
3006 sizeof_operation(const slang_operation
*oper
)
3009 GLuint count
= 1; /* me */
3011 for (i
= 0; i
< oper
->num_children
; i
++) {
3012 count
+= sizeof_operation(&oper
->children
[i
]);
3023 * Determine if a for-loop can be unrolled.
3024 * At this time, only a rather narrow class of for loops can be unrolled.
3025 * See code for details.
3026 * When a loop can't be unrolled because it's too large we'll emit a
3027 * message to the log.
3030 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3034 const char *varName
;
3037 if (oper
->type
!= SLANG_OPER_FOR
)
3040 assert(oper
->num_children
== 4);
3042 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3045 /* children[0] must be either "int i=constant" or "i=constant" */
3046 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3047 slang_variable
*var
;
3049 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3052 varId
= oper
->children
[0].children
[0].a_id
;
3054 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3058 if (!var
->initializer
)
3060 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3062 start
= (GLint
) var
->initializer
->literal
[0];
3064 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3065 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3067 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3069 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3072 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3074 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3080 /* children[1] must be "i<constant" */
3081 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3083 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3085 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3087 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3090 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3092 /* children[2] must be "i++" or "++i" */
3093 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3094 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3096 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3099 /* make sure the same variable name is used in all places */
3100 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3101 (oper
->children
[2].children
[0].a_id
!= varId
))
3104 varName
= (const char *) varId
;
3106 /* children[3], the loop body, can't be too large */
3107 bodySize
= sizeof_operation(&oper
->children
[3]);
3108 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3109 slang_info_log_print(A
->log
,
3110 "Note: 'for (%s ... )' body is too large/complex"
3117 return GL_FALSE
; /* degenerate case */
3119 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3120 slang_info_log_print(A
->log
,
3121 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3122 " many iterations to unroll",
3123 varName
, start
, varName
, end
, varName
);
3127 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3128 slang_info_log_print(A
->log
,
3129 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3130 " too much code to unroll",
3131 varName
, start
, varName
, end
, varName
);
3135 return GL_TRUE
; /* we can unroll the loop */
3140 * Unroll a for-loop.
3141 * First we determine the number of iterations to unroll.
3142 * Then for each iteration:
3143 * make a copy of the loop body
3144 * replace instances of the loop variable with the current iteration value
3145 * generate IR code for the body
3146 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3148 static slang_ir_node
*
3149 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3151 GLint start
, end
, iter
;
3152 slang_ir_node
*n
, *root
= NULL
;
3155 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3156 /* for (int i=0; ... */
3157 slang_variable
*var
;
3159 varId
= oper
->children
[0].children
[0].a_id
;
3160 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3162 start
= (GLint
) var
->initializer
->literal
[0];
3166 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3167 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3170 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3172 for (iter
= start
; iter
< end
; iter
++) {
3173 slang_operation
*body
;
3175 /* make a copy of the loop body */
3176 body
= slang_operation_new(1);
3180 if (!slang_operation_copy(body
, &oper
->children
[3]))
3183 /* in body, replace instances of 'varId' with literal 'iter' */
3185 slang_variable
*oldVar
;
3186 slang_operation
*newOper
;
3188 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3190 /* undeclared loop variable */
3191 slang_operation_delete(body
);
3195 newOper
= slang_operation_new(1);
3196 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3197 newOper
->literal_size
= 1;
3198 newOper
->literal
[0] = iter
;
3200 /* replace instances of the loop variable with newOper */
3201 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3204 /* do IR codegen for body */
3205 n
= _slang_gen_operation(A
, body
);
3209 root
= new_seq(root
, n
);
3211 slang_operation_delete(body
);
3219 * Replace 'continue' statement with 'break' inside a for-loop.
3220 * This is a recursive helper function used by _slang_gen_for_without_continue().
3223 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3225 switch (oper
->type
) {
3226 case SLANG_OPER_CONTINUE
:
3227 oper
->type
= SLANG_OPER_BREAK
;
3229 case SLANG_OPER_FOR
:
3231 case SLANG_OPER_WHILE
:
3232 /* stop upon finding a nested loop */
3238 for (i
= 0; i
< oper
->num_children
; i
++) {
3239 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3247 * Transform a for-loop so that continue statements are converted to breaks.
3248 * Then do normal IR code generation.
3252 * for (INIT; LOOPCOND; INCR) {
3263 * bool _condFlag = 1;
3264 * for (INIT; _condFlag; ) {
3265 * for ( ; _condFlag = LOOPCOND; INCR) {
3277 static slang_ir_node
*
3278 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3280 slang_operation
*top
;
3281 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3282 slang_operation
*lhs
, *rhs
;
3284 assert(oper
->type
== SLANG_OPER_FOR
);
3286 top
= slang_operation_new(1);
3287 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3288 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3289 slang_operation_add_children(top
, 2);
3291 /* declare: bool _condFlag = true */
3293 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3294 slang_generate_declaration(A
, top
->locals
, condDecl
,
3295 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3298 /* build outer loop: for (INIT; _condFlag; ) { */
3299 outerFor
= slang_oper_child(top
, 1);
3300 outerFor
->type
= SLANG_OPER_FOR
;
3301 slang_operation_add_children(outerFor
, 4);
3303 init
= slang_oper_child(outerFor
, 0);
3304 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3306 cond
= slang_oper_child(outerFor
, 1);
3307 cond
->type
= SLANG_OPER_IDENTIFIER
;
3308 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3310 incr
= slang_oper_child(outerFor
, 2);
3311 incr
->type
= SLANG_OPER_VOID
;
3313 /* body of the outer loop */
3315 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3317 slang_operation_add_children(block
, 2);
3318 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3320 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3322 innerFor
= slang_oper_child(block
, 0);
3324 /* make copy of orig loop */
3325 slang_operation_copy(innerFor
, oper
);
3326 assert(innerFor
->type
== SLANG_OPER_FOR
);
3327 innerFor
->locals
->outer_scope
= block
->locals
;
3329 init
= slang_oper_child(innerFor
, 0);
3330 init
->type
= SLANG_OPER_VOID
; /* leak? */
3332 cond
= slang_oper_child(innerFor
, 1);
3333 slang_operation_destruct(cond
);
3334 cond
->type
= SLANG_OPER_ASSIGN
;
3335 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3336 slang_operation_add_children(cond
, 2);
3338 lhs
= slang_oper_child(cond
, 0);
3339 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3340 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3342 rhs
= slang_oper_child(cond
, 1);
3343 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3346 /* if (_condFlag) INCR; */
3348 slang_operation
*ifop
= slang_oper_child(block
, 1);
3349 ifop
->type
= SLANG_OPER_IF
;
3350 slang_operation_add_children(ifop
, 2);
3352 /* re-use cond node build above */
3353 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3355 /* incr node from original for-loop operation */
3356 slang_operation_copy(slang_oper_child(ifop
, 1),
3357 slang_oper_child(oper
, 2));
3360 /* finally, replace "continue" with "break" in the inner for-loop */
3361 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3364 return _slang_gen_operation(A
, top
);
3370 * Generate IR for a for-loop. Unrolling will be done when possible.
3372 static slang_ir_node
*
3373 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3377 if (!A
->EmitContReturn
) {
3378 /* We don't want to emit CONT instructions. If this for-loop has
3379 * a continue, translate it away.
3381 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3382 return _slang_gen_for_without_continue(A
, oper
);
3386 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3388 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3393 assert(oper
->type
== SLANG_OPER_FOR
);
3395 /* conventional for-loop code generation */
3398 * init code (child[0])
3400 * BREAK if !expr (child[1])
3401 * body code (child[3])
3403 * incr code (child[2]) // XXX continue here
3405 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3406 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3407 loop
= new_loop(NULL
);
3409 /* save loop state */
3410 push_loop(A
, oper
, loop
);
3412 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3413 breakIf
= new_break_if_true(A
, cond
);
3414 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3415 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3417 loop
->Children
[0] = new_seq(breakIf
, body
);
3418 loop
->Children
[1] = incr
; /* tail code */
3420 /* restore loop state */
3423 return new_seq(init
, loop
);
3428 static slang_ir_node
*
3429 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3431 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3433 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3434 loopNode
= current_loop_ir(A
);
3436 assert(loopNode
->Opcode
== IR_LOOP
);
3438 cont
= new_node0(IR_CONT
);
3440 cont
->Parent
= loopNode
;
3441 /* insert this node at head of linked list of cont/break instructions */
3442 cont
->List
= loopNode
->List
;
3443 loopNode
->List
= cont
;
3446 n
= new_seq(incr
, cont
);
3452 * Determine if the given operation is of a specific type.
3455 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3457 if (oper
->type
== type
)
3459 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3460 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3461 oper
->num_children
== 1)
3462 return is_operation_type(&oper
->children
[0], type
);
3469 * Generate IR tree for an if/then/else conditional using high-level
3470 * IR_IF instruction.
3472 static slang_ir_node
*
3473 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3476 * eval expr (child[0])
3483 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3484 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3485 GLboolean isConst
, constTrue
;
3487 /* type-check expression */
3488 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3489 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3493 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3494 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3498 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3502 return _slang_gen_operation(A
, &oper
->children
[1]);
3505 /* if (false) ... */
3506 return _slang_gen_operation(A
, &oper
->children
[2]);
3510 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3511 cond
= new_cond(cond
);
3513 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3514 && !haveElseClause
) {
3515 /* Special case: generate a conditional break */
3516 ifBody
= new_break_if_true(A
, cond
);
3519 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3521 && current_loop_oper(A
)
3522 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3523 /* Special case: generate a conditional continue */
3524 ifBody
= new_cont_if_true(A
, cond
);
3529 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3531 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3534 ifNode
= new_if(cond
, ifBody
, elseBody
);
3541 static slang_ir_node
*
3542 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3546 assert(oper
->type
== SLANG_OPER_NOT
);
3548 /* type-check expression */
3549 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3550 slang_info_log_error(A
->log
,
3551 "scalar/boolean expression expected for '!'");
3555 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3563 static slang_ir_node
*
3564 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3566 slang_ir_node
*n1
, *n2
;
3568 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3570 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3571 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3572 slang_info_log_error(A
->log
,
3573 "scalar/boolean expressions expected for '^^'");
3577 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3580 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3583 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3588 * Generate IR node for storage of a temporary of given size.
3590 static slang_ir_node
*
3591 _slang_gen_temporary(GLint size
)
3593 slang_ir_storage
*store
;
3594 slang_ir_node
*n
= NULL
;
3596 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3598 n
= new_node0(IR_VAR_DECL
);
3611 * Generate program constants for an array.
3612 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3613 * This will allocate and initialize three vector constants, storing
3614 * the array in constant memory, not temporaries like a non-const array.
3615 * This can also be used for uniform array initializers.
3616 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3619 make_constant_array(slang_assemble_ctx
*A
,
3620 slang_variable
*var
,
3621 slang_operation
*initializer
)
3623 struct gl_program
*prog
= A
->program
;
3624 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3625 const char *varName
= (char *) var
->a_name
;
3626 const GLuint numElements
= initializer
->num_children
;
3632 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3634 size
= var
->store
->Size
;
3636 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3637 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3638 assert(initializer
->type
== SLANG_OPER_CALL
);
3639 assert(initializer
->array_constructor
);
3641 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3643 /* convert constructor params into ordinary floats */
3644 for (i
= 0; i
< numElements
; i
++) {
3645 const slang_operation
*op
= &initializer
->children
[i
];
3646 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3647 /* unsupported type for this optimization */
3651 for (j
= 0; j
< op
->literal_size
; j
++) {
3652 values
[i
* 4 + j
] = op
->literal
[j
];
3654 for ( ; j
< 4; j
++) {
3655 values
[i
* 4 + j
] = 0.0f
;
3659 /* slightly different paths for constants vs. uniforms */
3660 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3661 var
->store
->File
= PROGRAM_UNIFORM
;
3662 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3663 size
, datatype
, values
);
3666 var
->store
->File
= PROGRAM_CONSTANT
;
3667 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3670 assert(var
->store
->Size
== size
);
3680 * Generate IR node for allocating/declaring a variable (either a local or
3682 * Generally, this involves allocating an slang_ir_storage instance for the
3683 * variable, choosing a register file (temporary, constant, etc).
3684 * For ordinary variables we do not yet allocate storage though. We do that
3685 * when we find the first actual use of the variable to avoid allocating temp
3686 * regs that will never get used.
3687 * At this time, uniforms are always allocated space in this function.
3689 * \param initializer Optional initializer expression for the variable.
3691 static slang_ir_node
*
3692 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3693 slang_operation
*initializer
)
3695 const char *varName
= (const char *) var
->a_name
;
3696 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3697 slang_ir_node
*varDecl
, *n
;
3698 slang_ir_storage
*store
;
3699 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3700 gl_register_file file
;
3702 /*assert(!var->declared);*/
3703 var
->declared
= GL_TRUE
;
3705 /* determine GPU register file for simple cases */
3706 if (is_sampler_type(&var
->type
)) {
3707 file
= PROGRAM_SAMPLER
;
3709 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3710 file
= PROGRAM_UNIFORM
;
3713 file
= PROGRAM_TEMPORARY
;
3716 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3718 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3722 arrayLen
= _slang_array_length(var
);
3723 totalSize
= _slang_array_size(size
, arrayLen
);
3725 /* Allocate IR node for the declaration */
3726 varDecl
= new_node0(IR_VAR_DECL
);
3730 /* Allocate slang_ir_storage for this variable if needed.
3731 * Note that we may not actually allocate a constant or temporary register
3735 GLint index
= -7; /* TBD / unknown */
3736 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3738 return NULL
; /* out of memory */
3741 /* set the IR node's Var and Store pointers */
3743 varDecl
->Store
= var
->store
;
3748 /* if there's an initializer, generate IR for the expression */
3750 slang_ir_node
*varRef
, *init
;
3752 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3753 /* if the variable is const, the initializer must be a const
3754 * expression as well.
3757 if (!_slang_is_constant_expr(initializer
)) {
3758 slang_info_log_error(A
->log
,
3759 "initializer for %s not constant", varName
);
3765 /* IR for the variable we're initializing */
3766 varRef
= new_var(A
, var
);
3768 slang_info_log_error(A
->log
, "out of memory");
3772 /* constant-folding, etc here */
3773 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3775 /* look for simple constant-valued variables and uniforms */
3776 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3777 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3779 if (initializer
->type
== SLANG_OPER_CALL
&&
3780 initializer
->array_constructor
) {
3781 /* array initializer */
3782 if (make_constant_array(A
, var
, initializer
))
3785 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3786 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3787 /* simple float/vector initializer */
3788 if (store
->File
== PROGRAM_UNIFORM
) {
3789 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3791 totalSize
, datatype
,
3792 initializer
->literal
);
3793 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3798 store
->File
= PROGRAM_CONSTANT
;
3799 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3801 initializer
->literal
,
3803 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3810 /* IR for initializer */
3811 init
= _slang_gen_operation(A
, initializer
);
3815 /* XXX remove this when type checking is added above */
3816 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3817 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3821 /* assign RHS to LHS */
3822 n
= new_node2(IR_COPY
, varRef
, init
);
3823 n
= new_seq(varDecl
, n
);
3826 /* no initializer */
3830 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3831 /* always need to allocate storage for uniforms at this point */
3832 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3833 totalSize
, datatype
, NULL
);
3834 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3838 printf("%s var %p %s store=%p index=%d size=%d\n",
3839 __FUNCTION__
, (void *) var
, (char *) varName
,
3840 (void *) store
, store
->Index
, store
->Size
);
3848 * Generate code for a selection expression: b ? x : y
3849 * XXX In some cases we could implement a selection expression
3850 * with an LRP instruction (use the boolean as the interpolant).
3851 * Otherwise, we use an IF/ELSE/ENDIF construct.
3853 static slang_ir_node
*
3854 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3856 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3857 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3858 slang_typeinfo type0
, type1
, type2
;
3859 int size
, isBool
, isEqual
;
3861 assert(oper
->type
== SLANG_OPER_SELECT
);
3862 assert(oper
->num_children
== 3);
3864 /* type of children[0] must be boolean */
3865 slang_typeinfo_construct(&type0
);
3866 typeof_operation(A
, &oper
->children
[0], &type0
);
3867 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3868 slang_typeinfo_destruct(&type0
);
3870 slang_info_log_error(A
->log
, "selector type is not boolean");
3874 slang_typeinfo_construct(&type1
);
3875 slang_typeinfo_construct(&type2
);
3876 typeof_operation(A
, &oper
->children
[1], &type1
);
3877 typeof_operation(A
, &oper
->children
[2], &type2
);
3878 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3879 slang_typeinfo_destruct(&type1
);
3880 slang_typeinfo_destruct(&type2
);
3882 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3886 /* size of x or y's type */
3887 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3891 tmpDecl
= _slang_gen_temporary(size
);
3893 /* the condition (child 0) */
3894 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3895 cond
= new_cond(cond
);
3897 /* if-true body (child 1) */
3898 tmpVar
= new_node0(IR_VAR
);
3899 tmpVar
->Store
= tmpDecl
->Store
;
3900 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3901 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3903 /* if-false body (child 2) */
3904 tmpVar
= new_node0(IR_VAR
);
3905 tmpVar
->Store
= tmpDecl
->Store
;
3906 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3907 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3909 ifNode
= new_if(cond
, trueNode
, falseNode
);
3912 tmpVar
= new_node0(IR_VAR
);
3913 tmpVar
->Store
= tmpDecl
->Store
;
3915 tree
= new_seq(ifNode
, tmpVar
);
3916 tree
= new_seq(tmpDecl
, tree
);
3918 /*_slang_print_ir_tree(tree, 10);*/
3924 * Generate code for &&.
3926 static slang_ir_node
*
3927 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3929 /* rewrite "a && b" as "a ? b : false" */
3930 slang_operation
*select
;
3933 select
= slang_operation_new(1);
3934 select
->type
= SLANG_OPER_SELECT
;
3935 slang_operation_add_children(select
, 3);
3937 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3938 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3939 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3941 n
= _slang_gen_select(A
, select
);
3947 * Generate code for ||.
3949 static slang_ir_node
*
3950 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3952 /* rewrite "a || b" as "a ? true : b" */
3953 slang_operation
*select
;
3956 select
= slang_operation_new(1);
3957 select
->type
= SLANG_OPER_SELECT
;
3958 slang_operation_add_children(select
, 3);
3960 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3961 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3962 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3964 n
= _slang_gen_select(A
, select
);
3970 * Generate IR tree for a return statement.
3972 static slang_ir_node
*
3973 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3975 assert(oper
->type
== SLANG_OPER_RETURN
);
3976 return new_return(A
->curFuncEndLabel
);
3982 * Determine if the given operation/expression is const-valued.
3985 _slang_is_constant_expr(const slang_operation
*oper
)
3987 slang_variable
*var
;
3990 switch (oper
->type
) {
3991 case SLANG_OPER_IDENTIFIER
:
3992 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3993 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3997 for (i
= 0; i
< oper
->num_children
; i
++) {
3998 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4008 * Check if an assignment of type t1 to t0 is legal.
4009 * XXX more cases needed.
4012 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4013 slang_operation
*op0
,
4014 slang_operation
*op1
)
4016 slang_typeinfo t0
, t1
;
4019 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4020 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4024 slang_typeinfo_construct(&t0
);
4025 typeof_operation(A
, op0
, &t0
);
4027 slang_typeinfo_construct(&t1
);
4028 typeof_operation(A
, op1
, &t1
);
4030 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4031 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4035 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4040 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4041 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4042 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4045 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4046 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4049 #if 0 /* not used just yet - causes problems elsewhere */
4050 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4051 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4055 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4056 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4059 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4060 t1
.spec
.type
== SLANG_SPEC_INT
)
4068 * Generate IR tree for a local variable declaration.
4069 * Basically do some error checking and call _slang_gen_var_decl().
4071 static slang_ir_node
*
4072 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4074 const char *varName
= (char *) oper
->a_id
;
4075 slang_variable
*var
;
4076 slang_ir_node
*varDecl
;
4077 slang_operation
*initializer
;
4079 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4080 assert(oper
->num_children
<= 1);
4083 /* lookup the variable by name */
4084 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4086 return NULL
; /* "shouldn't happen" */
4088 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4089 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4090 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4091 /* can't declare attribute/uniform vars inside functions */
4092 slang_info_log_error(A
->log
,
4093 "local variable '%s' cannot be an attribute/uniform/varying",
4100 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4105 /* check if the var has an initializer */
4106 if (oper
->num_children
> 0) {
4107 assert(oper
->num_children
== 1);
4108 initializer
= &oper
->children
[0];
4110 else if (var
->initializer
) {
4111 initializer
= var
->initializer
;
4118 /* check/compare var type and initializer type */
4119 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4120 slang_info_log_error(A
->log
, "incompatible types in assignment");
4125 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4126 slang_info_log_error(A
->log
,
4127 "const-qualified variable '%s' requires initializer",
4133 /* Generate IR node */
4134 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4143 * Generate IR tree for a reference to a variable (such as in an expression).
4144 * This is different from a variable declaration.
4146 static slang_ir_node
*
4147 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4149 /* If there's a variable associated with this oper (from inlining)
4150 * use it. Otherwise, use the oper's var id.
4152 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4153 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4156 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4159 assert(var
->declared
);
4160 n
= new_var(A
, var
);
4167 * Return the number of components actually named by the swizzle.
4168 * Recall that swizzles may have undefined/don't-care values.
4171 swizzle_size(GLuint swizzle
)
4174 for (i
= 0; i
< 4; i
++) {
4175 GLuint swz
= GET_SWZ(swizzle
, i
);
4176 size
+= (swz
>= 0 && swz
<= 3);
4182 static slang_ir_node
*
4183 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4185 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4189 n
->Store
= _slang_new_ir_storage_relative(0,
4190 swizzle_size(swizzle
),
4192 n
->Store
->Swizzle
= swizzle
;
4199 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4201 while (store
->Parent
)
4202 store
= store
->Parent
;
4204 if (!(store
->File
== PROGRAM_OUTPUT
||
4205 store
->File
== PROGRAM_TEMPORARY
||
4206 (store
->File
== PROGRAM_VARYING
&&
4207 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4217 * Walk up an IR storage path to compute the final swizzle.
4218 * This is used when we find an expression such as "foo.xz.yx".
4221 root_swizzle(const slang_ir_storage
*st
)
4223 GLuint swizzle
= st
->Swizzle
;
4224 while (st
->Parent
) {
4226 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4233 * Generate IR tree for an assignment (=).
4235 static slang_ir_node
*
4236 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4238 slang_operation
*pred
= NULL
;
4239 slang_ir_node
*n
= NULL
;
4241 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4242 /* Check that var is writeable */
4244 = _slang_variable_locate(oper
->children
[0].locals
,
4245 oper
->children
[0].a_id
, GL_TRUE
);
4247 slang_info_log_error(A
->log
, "undefined variable '%s'",
4248 (char *) oper
->children
[0].a_id
);
4251 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4252 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4253 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4254 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4255 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4256 slang_info_log_error(A
->log
,
4257 "illegal assignment to read-only variable '%s'",
4258 (char *) oper
->children
[0].a_id
);
4262 /* check if we need to predicate this assignment based on __notRetFlag */
4263 if ((var
->is_global
||
4264 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4265 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4266 /* create predicate, used below */
4267 pred
= slang_operation_new(1);
4268 pred
->type
= SLANG_OPER_IDENTIFIER
;
4269 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4270 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4274 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4275 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4276 /* Special case of: x = f(a, b)
4277 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4279 * XXX this could be even more effective if we could accomodate
4280 * cases such as "v.x = f();" - would help with typical vertex
4283 n
= _slang_gen_function_call_name(A
,
4284 (const char *) oper
->children
[1].a_id
,
4285 &oper
->children
[1], &oper
->children
[0]);
4288 slang_ir_node
*lhs
, *rhs
;
4290 /* lhs and rhs type checking */
4291 if (!_slang_assignment_compatible(A
,
4293 &oper
->children
[1])) {
4294 slang_info_log_error(A
->log
, "incompatible types in assignment");
4298 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4304 slang_info_log_error(A
->log
,
4305 "invalid left hand side for assignment");
4309 /* check that lhs is writable */
4310 if (!is_store_writable(A
, lhs
->Store
)) {
4311 slang_info_log_error(A
->log
,
4312 "illegal assignment to read-only l-value");
4316 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4318 /* convert lhs swizzle into writemask */
4319 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4320 GLuint writemask
, newSwizzle
= 0x0;
4321 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4322 /* Non-simple writemask, need to swizzle right hand side in
4323 * order to put components into the right place.
4325 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4327 n
= new_node2(IR_COPY
, lhs
, rhs
);
4335 /* predicate the assignment code on __notRetFlag */
4336 slang_ir_node
*top
, *cond
;
4338 cond
= _slang_gen_operation(A
, pred
);
4339 top
= new_if(cond
, n
, NULL
);
4347 * Generate IR tree for referencing a field in a struct (or basic vector type)
4349 static slang_ir_node
*
4350 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4354 /* type of struct */
4355 slang_typeinfo_construct(&ti
);
4356 typeof_operation(A
, &oper
->children
[0], &ti
);
4358 if (_slang_type_is_vector(ti
.spec
.type
)) {
4359 /* the field should be a swizzle */
4360 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4364 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4365 slang_info_log_error(A
->log
, "Bad swizzle");
4368 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4373 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4374 /* create new parent node with swizzle */
4376 n
= _slang_gen_swizzle(n
, swizzle
);
4379 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4380 || ti
.spec
.type
== SLANG_SPEC_INT
4381 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4382 const GLuint rows
= 1;
4386 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4387 slang_info_log_error(A
->log
, "Bad swizzle");
4389 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4393 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4394 /* create new parent node with swizzle */
4395 n
= _slang_gen_swizzle(n
, swizzle
);
4399 /* the field is a structure member (base.field) */
4400 /* oper->children[0] is the base */
4401 /* oper->a_id is the field name */
4402 slang_ir_node
*base
, *n
;
4403 slang_typeinfo field_ti
;
4404 GLint fieldSize
, fieldOffset
= -1;
4407 slang_typeinfo_construct(&field_ti
);
4408 typeof_operation(A
, oper
, &field_ti
);
4410 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4412 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4414 if (fieldSize
== 0 || fieldOffset
< 0) {
4415 const char *structName
;
4416 if (ti
.spec
._struct
)
4417 structName
= (char *) ti
.spec
._struct
->a_name
;
4419 structName
= "unknown";
4420 slang_info_log_error(A
->log
,
4421 "\"%s\" is not a member of struct \"%s\"",
4422 (char *) oper
->a_id
, structName
);
4425 assert(fieldSize
>= 0);
4427 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4429 /* error msg should have already been logged */
4433 n
= new_node1(IR_FIELD
, base
);
4437 n
->Field
= (char *) oper
->a_id
;
4439 /* Store the field's offset in storage->Index */
4440 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4450 * Gen code for array indexing.
4452 static slang_ir_node
*
4453 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4455 slang_typeinfo array_ti
;
4457 /* get array's type info */
4458 slang_typeinfo_construct(&array_ti
);
4459 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4461 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4462 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4463 /* translate the index into a swizzle/writemask: "v.x=p" */
4464 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4468 index
= (GLint
) oper
->children
[1].literal
[0];
4469 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4470 index
>= (GLint
) max
) {
4472 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4473 printf("type = %d\n", oper
->children
[1].type
);
4474 printf("index = %d, max = %d\n", index
, max
);
4475 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4476 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4483 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4485 /* use swizzle to access the element */
4486 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4490 n
= _slang_gen_swizzle(n
, swizzle
);
4496 /* conventional array */
4497 slang_typeinfo elem_ti
;
4498 slang_ir_node
*elem
, *array
, *index
;
4499 GLint elemSize
, arrayLen
;
4501 /* size of array element */
4502 slang_typeinfo_construct(&elem_ti
);
4503 typeof_operation(A
, oper
, &elem_ti
);
4504 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4506 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4507 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4509 arrayLen
= array_ti
.array_len
;
4511 slang_typeinfo_destruct(&array_ti
);
4512 slang_typeinfo_destruct(&elem_ti
);
4514 if (elemSize
<= 0) {
4515 /* unknown var or type */
4516 slang_info_log_error(A
->log
, "Undefined variable or type");
4520 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4521 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4522 if (array
&& index
) {
4524 GLint constIndex
= -1;
4525 if (index
->Opcode
== IR_FLOAT
) {
4526 constIndex
= (int) index
->Value
[0];
4527 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4528 slang_info_log_error(A
->log
,
4529 "Array index out of bounds (index=%d size=%d)",
4530 constIndex
, arrayLen
);
4531 _slang_free_ir_tree(array
);
4532 _slang_free_ir_tree(index
);
4537 if (!array
->Store
) {
4538 slang_info_log_error(A
->log
, "Invalid array");
4542 elem
= new_node2(IR_ELEMENT
, array
, index
);
4544 /* The storage info here will be updated during code emit */
4545 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4546 array
->Store
->Index
,
4548 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4552 _slang_free_ir_tree(array
);
4553 _slang_free_ir_tree(index
);
4560 static slang_ir_node
*
4561 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4562 slang_ir_opcode opcode
)
4564 slang_typeinfo t0
, t1
;
4567 slang_typeinfo_construct(&t0
);
4568 typeof_operation(A
, &oper
->children
[0], &t0
);
4570 slang_typeinfo_construct(&t1
);
4571 typeof_operation(A
, &oper
->children
[0], &t1
);
4573 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4574 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4575 slang_info_log_error(A
->log
, "Illegal array comparison");
4579 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4580 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4581 /* <, <=, >, >= can only be used with scalars */
4582 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4583 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4584 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4585 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4586 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4591 n
= new_node2(opcode
,
4592 _slang_gen_operation(A
, &oper
->children
[0]),
4593 _slang_gen_operation(A
, &oper
->children
[1]));
4595 /* result is a bool (size 1) */
4596 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4604 print_vars(slang_variable_scope
*s
)
4608 for (i
= 0; i
< s
->num_variables
; i
++) {
4610 (char*) s
->variables
[i
]->a_name
,
4611 s
->variables
[i
]->declared
);
4621 _slang_undeclare_vars(slang_variable_scope
*locals
)
4623 if (locals
->num_variables
> 0) {
4625 for (i
= 0; i
< locals
->num_variables
; i
++) {
4626 slang_variable
*v
= locals
->variables
[i
];
4627 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4628 v
->declared
= GL_FALSE
;
4636 * Generate IR tree for a slang_operation (AST node)
4638 static slang_ir_node
*
4639 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4641 switch (oper
->type
) {
4642 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4646 _slang_push_var_table(A
->vartable
);
4648 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4649 n
= _slang_gen_operation(A
, oper
);
4650 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4652 _slang_pop_var_table(A
->vartable
);
4654 /*_slang_undeclare_vars(oper->locals);*/
4655 /*print_vars(oper->locals);*/
4658 n
= new_node1(IR_SCOPE
, n
);
4663 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4664 /* list of operations */
4665 if (oper
->num_children
> 0)
4667 slang_ir_node
*n
, *tree
= NULL
;
4670 for (i
= 0; i
< oper
->num_children
; i
++) {
4671 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4673 _slang_free_ir_tree(tree
);
4674 return NULL
; /* error must have occured */
4676 tree
= new_seq(tree
, n
);
4682 return new_node0(IR_NOP
);
4685 case SLANG_OPER_EXPRESSION
:
4686 return _slang_gen_operation(A
, &oper
->children
[0]);
4688 case SLANG_OPER_FOR
:
4689 return _slang_gen_for(A
, oper
);
4691 return _slang_gen_do(A
, oper
);
4692 case SLANG_OPER_WHILE
:
4693 return _slang_gen_while(A
, oper
);
4694 case SLANG_OPER_BREAK
:
4695 if (!current_loop_oper(A
)) {
4696 slang_info_log_error(A
->log
, "'break' not in loop");
4699 return new_break(current_loop_ir(A
));
4700 case SLANG_OPER_CONTINUE
:
4701 if (!current_loop_oper(A
)) {
4702 slang_info_log_error(A
->log
, "'continue' not in loop");
4705 return _slang_gen_continue(A
, oper
);
4706 case SLANG_OPER_DISCARD
:
4707 return new_node0(IR_KILL
);
4709 case SLANG_OPER_EQUAL
:
4710 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4711 case SLANG_OPER_NOTEQUAL
:
4712 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4713 case SLANG_OPER_GREATER
:
4714 return _slang_gen_compare(A
, oper
, IR_SGT
);
4715 case SLANG_OPER_LESS
:
4716 return _slang_gen_compare(A
, oper
, IR_SLT
);
4717 case SLANG_OPER_GREATEREQUAL
:
4718 return _slang_gen_compare(A
, oper
, IR_SGE
);
4719 case SLANG_OPER_LESSEQUAL
:
4720 return _slang_gen_compare(A
, oper
, IR_SLE
);
4721 case SLANG_OPER_ADD
:
4724 assert(oper
->num_children
== 2);
4725 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4728 case SLANG_OPER_SUBTRACT
:
4731 assert(oper
->num_children
== 2);
4732 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4735 case SLANG_OPER_MULTIPLY
:
4738 assert(oper
->num_children
== 2);
4739 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4742 case SLANG_OPER_DIVIDE
:
4745 assert(oper
->num_children
== 2);
4746 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4749 case SLANG_OPER_MINUS
:
4752 assert(oper
->num_children
== 1);
4753 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4756 case SLANG_OPER_PLUS
:
4757 /* +expr --> do nothing */
4758 return _slang_gen_operation(A
, &oper
->children
[0]);
4759 case SLANG_OPER_VARIABLE_DECL
:
4760 return _slang_gen_declaration(A
, oper
);
4761 case SLANG_OPER_ASSIGN
:
4762 return _slang_gen_assignment(A
, oper
);
4763 case SLANG_OPER_ADDASSIGN
:
4766 assert(oper
->num_children
== 2);
4767 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4770 case SLANG_OPER_SUBASSIGN
:
4773 assert(oper
->num_children
== 2);
4774 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4778 case SLANG_OPER_MULASSIGN
:
4781 assert(oper
->num_children
== 2);
4782 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4785 case SLANG_OPER_DIVASSIGN
:
4788 assert(oper
->num_children
== 2);
4789 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4792 case SLANG_OPER_LOGICALAND
:
4795 assert(oper
->num_children
== 2);
4796 n
= _slang_gen_logical_and(A
, oper
);
4799 case SLANG_OPER_LOGICALOR
:
4802 assert(oper
->num_children
== 2);
4803 n
= _slang_gen_logical_or(A
, oper
);
4806 case SLANG_OPER_LOGICALXOR
:
4807 return _slang_gen_xor(A
, oper
);
4808 case SLANG_OPER_NOT
:
4809 return _slang_gen_not(A
, oper
);
4810 case SLANG_OPER_SELECT
: /* b ? x : y */
4813 assert(oper
->num_children
== 3);
4814 n
= _slang_gen_select(A
, oper
);
4818 case SLANG_OPER_ASM
:
4819 return _slang_gen_asm(A
, oper
, NULL
);
4820 case SLANG_OPER_CALL
:
4821 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4823 case SLANG_OPER_METHOD
:
4824 return _slang_gen_method_call(A
, oper
);
4825 case SLANG_OPER_RETURN
:
4826 return _slang_gen_return(A
, oper
);
4827 case SLANG_OPER_RETURN_INLINED
:
4828 return _slang_gen_return(A
, oper
);
4829 case SLANG_OPER_LABEL
:
4830 return new_label(oper
->label
);
4831 case SLANG_OPER_IDENTIFIER
:
4832 return _slang_gen_variable(A
, oper
);
4834 return _slang_gen_if(A
, oper
);
4835 case SLANG_OPER_FIELD
:
4836 return _slang_gen_struct_field(A
, oper
);
4837 case SLANG_OPER_SUBSCRIPT
:
4838 return _slang_gen_array_element(A
, oper
);
4839 case SLANG_OPER_LITERAL_FLOAT
:
4841 case SLANG_OPER_LITERAL_INT
:
4843 case SLANG_OPER_LITERAL_BOOL
:
4844 return new_float_literal(oper
->literal
, oper
->literal_size
);
4846 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4849 assert(oper
->num_children
== 1);
4850 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4853 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4856 assert(oper
->num_children
== 1);
4857 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4860 case SLANG_OPER_PREINCREMENT
: /* ++var */
4863 assert(oper
->num_children
== 1);
4864 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4867 case SLANG_OPER_PREDECREMENT
: /* --var */
4870 assert(oper
->num_children
== 1);
4871 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4875 case SLANG_OPER_NON_INLINED_CALL
:
4876 case SLANG_OPER_SEQUENCE
:
4878 slang_ir_node
*tree
= NULL
;
4880 for (i
= 0; i
< oper
->num_children
; i
++) {
4881 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4882 tree
= new_seq(tree
, n
);
4884 tree
->Store
= n
->Store
;
4886 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4887 tree
= new_function_call(tree
, oper
->label
);
4892 case SLANG_OPER_NONE
:
4893 case SLANG_OPER_VOID
:
4894 /* returning NULL here would generate an error */
4895 return new_node0(IR_NOP
);
4898 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4900 return new_node0(IR_NOP
);
4908 * Check if the given type specifier is a rectangular texture sampler.
4911 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4913 while (spec
->_array
) {
4914 spec
= spec
->_array
;
4916 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4917 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4923 * Called by compiler when a global variable has been parsed/compiled.
4924 * Here we examine the variable's type to determine what kind of register
4925 * storage will be used.
4927 * A uniform such as "gl_Position" will become the register specification
4928 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4929 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4931 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4932 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4933 * actual texture unit (as specified by the user calling glUniform1i()).
4936 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4937 slang_unit_type type
)
4939 struct gl_program
*prog
= A
->program
;
4940 const char *varName
= (char *) var
->a_name
;
4941 GLboolean success
= GL_TRUE
;
4942 slang_ir_storage
*store
= NULL
;
4944 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4945 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4946 const GLint arrayLen
= _slang_array_length(var
);
4947 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4948 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4950 var
->is_global
= GL_TRUE
;
4952 /* check for sampler2D arrays */
4953 if (texIndex
== -1 && var
->type
.specifier
._array
)
4954 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4956 if (texIndex
!= -1) {
4957 /* This is a texture sampler variable...
4958 * store->File = PROGRAM_SAMPLER
4959 * store->Index = sampler number (0..7, typically)
4960 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4962 if (var
->initializer
) {
4963 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4966 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4967 /* disallow rect samplers */
4968 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4969 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4973 (void) is_rect_sampler_spec
; /* silence warning */
4976 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4977 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4979 /* If we have a sampler array, then we need to allocate the
4980 * additional samplers to ensure we don't allocate them elsewhere.
4981 * We can't directly use _mesa_add_sampler() as that checks the
4982 * varName and gets a match, so we call _mesa_add_parameter()
4983 * directly and use the last sampler number from the call above.
4986 GLint a
= arrayLen
- 1;
4988 for (i
= 0; i
< a
; i
++) {
4989 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4990 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4991 varName
, 1, datatype
, &value
, NULL
, 0x0);
4995 if (dbg
) printf("SAMPLER ");
4997 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4998 /* Uniform variable */
4999 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5002 /* user-defined uniform */
5003 if (datatype
== GL_NONE
) {
5004 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5005 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5006 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5007 /* temporary work-around */
5008 GLenum datatype
= GL_FLOAT
;
5009 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5010 totalSize
, datatype
, NULL
);
5011 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5012 totalSize
, swizzle
);
5015 GLint a
= arrayLen
- 1;
5017 for (i
= 0; i
< a
; i
++) {
5018 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5019 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5020 varName
, 1, datatype
, &value
, NULL
, 0x0);
5024 /* XXX what we need to do is unroll the struct into its
5025 * basic types, creating a uniform variable for each.
5033 * Should produce uniforms:
5034 * "f.a" (GL_FLOAT_VEC3)
5035 * "f.b" (GL_FLOAT_VEC4)
5038 if (var
->initializer
) {
5039 slang_info_log_error(A
->log
,
5040 "unsupported initializer for uniform '%s'", varName
);
5045 slang_info_log_error(A
->log
,
5046 "invalid datatype for uniform variable %s",
5052 /* non-struct uniform */
5053 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5059 /* pre-defined uniform, like gl_ModelviewMatrix */
5060 /* We know it's a uniform, but don't allocate storage unless
5063 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5064 totalSize
, swizzle
);
5066 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5068 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5069 /* varyings must be float, vec or mat */
5070 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5071 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5072 slang_info_log_error(A
->log
,
5073 "varying '%s' must be float/vector/matrix",
5078 if (var
->initializer
) {
5079 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5085 /* user-defined varying */
5091 if (var
->type
.centroid
== SLANG_CENTROID
)
5092 flags
|= PROG_PARAM_BIT_CENTROID
;
5093 if (var
->type
.variant
== SLANG_INVARIANT
)
5094 flags
|= PROG_PARAM_BIT_INVARIANT
;
5096 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5098 swizzle
= _slang_var_swizzle(size
, 0);
5099 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5100 totalSize
, swizzle
);
5103 /* pre-defined varying, like gl_Color or gl_TexCoord */
5104 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5105 /* fragment program input */
5107 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5110 assert(index
< FRAG_ATTRIB_MAX
);
5111 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5115 /* vertex program output */
5116 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5117 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5119 assert(index
< VERT_RESULT_MAX
);
5120 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5121 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5124 if (dbg
) printf("V/F ");
5126 if (dbg
) printf("VARYING ");
5128 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5131 /* attributes must be float, vec or mat */
5132 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5133 slang_info_log_error(A
->log
,
5134 "attribute '%s' must be float/vector/matrix",
5140 /* user-defined vertex attribute */
5141 const GLint attr
= -1; /* unknown */
5142 swizzle
= _slang_var_swizzle(size
, 0);
5143 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5144 size
, datatype
, attr
);
5146 index
= VERT_ATTRIB_GENERIC0
+ index
;
5149 /* pre-defined vertex attrib */
5150 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5153 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5154 if (dbg
) printf("ATTRIB ");
5156 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5157 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5158 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5160 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5161 if (dbg
) printf("INPUT ");
5163 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5164 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5165 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5166 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5169 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5170 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5171 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5172 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5174 if (dbg
) printf("OUTPUT ");
5176 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5177 /* pre-defined global constant, like gl_MaxLights */
5178 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5179 if (dbg
) printf("CONST ");
5182 /* ordinary variable (may be const) */
5185 /* IR node to declare the variable */
5186 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5188 /* emit GPU instructions */
5189 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5191 _slang_free_ir_tree(n
);
5194 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5195 store
? store
->Index
: -2);
5198 var
->store
= store
; /* save var's storage info */
5200 var
->declared
= GL_TRUE
;
5207 * Produce an IR tree from a function AST (fun->body).
5208 * Then call the code emitter to convert the IR tree into gl_program
5212 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5215 GLboolean success
= GL_TRUE
;
5217 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5218 /* we only really generate code for main, all other functions get
5219 * inlined or codegen'd upon an actual call.
5222 /* do some basic error checking though */
5223 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5224 /* check that non-void functions actually return something */
5226 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5228 slang_info_log_error(A
->log
,
5229 "function \"%s\" has no return statement",
5230 (char *) fun
->header
.a_name
);
5232 "function \"%s\" has no return statement\n",
5233 (char *) fun
->header
.a_name
);
5238 return GL_TRUE
; /* not an error */
5242 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5243 slang_print_function(fun
, 1);
5246 /* should have been allocated earlier: */
5247 assert(A
->program
->Parameters
);
5248 assert(A
->program
->Varying
);
5249 assert(A
->vartable
);
5252 A
->UseReturnFlag
= GL_FALSE
;
5253 A
->CurFunction
= fun
;
5255 /* fold constant expressions, etc. */
5256 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5259 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5260 slang_print_function(fun
, 1);
5263 /* Create an end-of-function label */
5264 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5266 /* push new vartable scope */
5267 _slang_push_var_table(A
->vartable
);
5269 /* Generate IR tree for the function body code */
5270 n
= _slang_gen_operation(A
, fun
->body
);
5272 n
= new_node1(IR_SCOPE
, n
);
5274 /* pop vartable, restore previous */
5275 _slang_pop_var_table(A
->vartable
);
5278 /* XXX record error */
5282 /* append an end-of-function-label to IR tree */
5283 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5285 /*_slang_label_delete(A->curFuncEndLabel);*/
5286 A
->curFuncEndLabel
= NULL
;
5289 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5290 slang_print_function(fun
, 1);
5293 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5294 _slang_print_ir_tree(n
, 0);
5297 printf("************* End codegen function ************\n\n");
5300 if (A
->UnresolvedRefs
) {
5301 /* Can't codegen at this time.
5302 * At link time we'll concatenate all the vertex shaders and/or all
5303 * the fragment shaders and try recompiling.
5308 /* Emit program instructions */
5309 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5310 _slang_free_ir_tree(n
);
5312 /* free codegen context */
5314 _mesa_free(A->codegen);