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_cmp", IR_CMP
, 1, 3 },
426 { "vec4_clamp", IR_CLAMP
, 1, 3 },
427 { "vec4_seq", IR_SEQUAL
, 1, 2 },
428 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
429 { "vec4_sge", IR_SGE
, 1, 2 },
430 { "vec4_sgt", IR_SGT
, 1, 2 },
431 { "vec4_sle", IR_SLE
, 1, 2 },
432 { "vec4_slt", IR_SLT
, 1, 2 },
434 { "vec4_move", IR_MOVE
, 1, 1 },
435 { "vec4_floor", IR_FLOOR
, 1, 1 },
436 { "vec4_frac", IR_FRAC
, 1, 1 },
437 { "vec4_abs", IR_ABS
, 1, 1 },
438 { "vec4_negate", IR_NEG
, 1, 1 },
439 { "vec4_ddx", IR_DDX
, 1, 1 },
440 { "vec4_ddy", IR_DDY
, 1, 1 },
441 /* float binary op */
442 { "float_power", IR_POW
, 1, 2 },
443 /* texture / sampler */
444 { "vec4_tex_1d", IR_TEX
, 1, 2 },
445 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
446 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
447 { "vec4_tex_2d", IR_TEX
, 1, 2 },
448 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
449 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
450 { "vec4_tex_3d", IR_TEX
, 1, 2 },
451 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
452 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
453 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
454 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
455 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
457 /* texture / sampler but with shadow comparison */
458 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
459 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
460 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
461 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
462 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
463 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
464 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
465 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
468 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
469 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
470 { "float_exp", IR_EXP
, 1, 1 },
471 { "float_exp2", IR_EXP2
, 1, 1 },
472 { "float_log2", IR_LOG2
, 1, 1 },
473 { "float_rsq", IR_RSQ
, 1, 1 },
474 { "float_rcp", IR_RCP
, 1, 1 },
475 { "float_sine", IR_SIN
, 1, 1 },
476 { "float_cosine", IR_COS
, 1, 1 },
477 { "float_noise1", IR_NOISE1
, 1, 1},
478 { "float_noise2", IR_NOISE2
, 1, 1},
479 { "float_noise3", IR_NOISE3
, 1, 1},
480 { "float_noise4", IR_NOISE4
, 1, 1},
482 { NULL
, IR_NOP
, 0, 0 }
486 static slang_ir_node
*
487 new_node3(slang_ir_opcode op
,
488 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
490 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
496 n
->InstLocation
= -1;
501 static slang_ir_node
*
502 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
504 return new_node3(op
, c0
, c1
, NULL
);
507 static slang_ir_node
*
508 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
510 return new_node3(op
, c0
, NULL
, NULL
);
513 static slang_ir_node
*
514 new_node0(slang_ir_opcode op
)
516 return new_node3(op
, NULL
, NULL
, NULL
);
521 * Create sequence of two nodes.
523 static slang_ir_node
*
524 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
530 return new_node2(IR_SEQ
, left
, right
);
533 static slang_ir_node
*
534 new_label(slang_label
*label
)
536 slang_ir_node
*n
= new_node0(IR_LABEL
);
543 static slang_ir_node
*
544 new_float_literal(const float v
[4], GLuint size
)
546 slang_ir_node
*n
= new_node0(IR_FLOAT
);
548 COPY_4V(n
->Value
, v
);
549 /* allocate a storage object, but compute actual location (Index) later */
550 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
555 static slang_ir_node
*
556 new_not(slang_ir_node
*n
)
558 return new_node1(IR_NOT
, n
);
563 * Non-inlined function call.
565 static slang_ir_node
*
566 new_function_call(slang_ir_node
*code
, slang_label
*name
)
568 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
577 * Unconditional jump.
579 static slang_ir_node
*
580 new_return(slang_label
*dest
)
582 slang_ir_node
*n
= new_node0(IR_RETURN
);
590 static slang_ir_node
*
591 new_loop(slang_ir_node
*body
)
593 return new_node1(IR_LOOP
, body
);
597 static slang_ir_node
*
598 new_break(slang_ir_node
*loopNode
)
600 slang_ir_node
*n
= new_node0(IR_BREAK
);
602 assert(loopNode
->Opcode
== IR_LOOP
);
604 /* insert this node at head of linked list of cont/break instructions */
605 n
->List
= loopNode
->List
;
613 * Make new IR_BREAK_IF_TRUE.
615 static slang_ir_node
*
616 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
618 slang_ir_node
*loopNode
= current_loop_ir(A
);
621 assert(loopNode
->Opcode
== IR_LOOP
);
622 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
624 /* insert this node at head of linked list of cont/break instructions */
625 n
->List
= loopNode
->List
;
633 * Make new IR_CONT_IF_TRUE node.
635 static slang_ir_node
*
636 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
638 slang_ir_node
*loopNode
= current_loop_ir(A
);
641 assert(loopNode
->Opcode
== IR_LOOP
);
642 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
644 n
->Parent
= loopNode
; /* pointer to containing loop */
645 /* insert this node at head of linked list of cont/break instructions */
646 n
->List
= loopNode
->List
;
653 static slang_ir_node
*
654 new_cond(slang_ir_node
*n
)
656 slang_ir_node
*c
= new_node1(IR_COND
, n
);
661 static slang_ir_node
*
662 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
664 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
669 * New IR_VAR node - a reference to a previously declared variable.
671 static slang_ir_node
*
672 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
674 slang_ir_node
*n
= new_node0(IR_VAR
);
681 /* Set IR node's Var and Store pointers */
683 n
->Store
= var
->store
;
690 * Check if the given function is really just a wrapper for a
691 * basic assembly instruction.
694 slang_is_asm_function(const slang_function
*fun
)
696 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
697 fun
->body
->num_children
== 1 &&
698 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
706 _slang_is_noop(const slang_operation
*oper
)
709 oper
->type
== SLANG_OPER_VOID
||
710 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
718 * Recursively search tree for a node of the given type.
721 static slang_operation
*
722 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
725 if (oper
->type
== type
)
727 for (i
= 0; i
< oper
->num_children
; i
++) {
728 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
738 * Count the number of operations of the given time rooted at 'oper'.
741 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
744 if (oper
->type
== type
) {
747 for (i
= 0; i
< oper
->num_children
; i
++) {
748 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
755 * Check if the 'return' statement found under 'oper' is a "tail return"
756 * that can be no-op'd. For example:
761 * return; // this is a no-op
764 * This is used when determining if a function can be inlined. If the
765 * 'return' is not the last statement, we can't inline the function since
766 * we still need the semantic behaviour of the 'return' but we don't want
767 * to accidentally return from the _calling_ function. We'd need to use an
768 * unconditional branch, but we don't have such a GPU instruction (not
772 _slang_is_tail_return(const slang_operation
*oper
)
774 GLuint k
= oper
->num_children
;
777 const slang_operation
*last
= &oper
->children
[k
- 1];
778 if (last
->type
== SLANG_OPER_RETURN
)
780 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
781 last
->type
== SLANG_OPER_LABEL
)
782 k
--; /* try prev child */
783 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
784 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
785 /* try sub-children */
786 return _slang_is_tail_return(last
);
796 * Generate a variable declaration opeartion.
797 * I.e.: generate AST code for "bool flag = false;"
800 slang_generate_declaration(slang_assemble_ctx
*A
,
801 slang_variable_scope
*scope
,
802 slang_operation
*decl
,
803 slang_type_specifier_type type
,
809 assert(type
== SLANG_SPEC_BOOL
||
810 type
== SLANG_SPEC_INT
);
812 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
814 var
= slang_variable_scope_grow(scope
);
816 slang_fully_specified_type_construct(&var
->type
);
818 var
->type
.specifier
.type
= type
;
819 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
820 decl
->a_id
= var
->a_name
;
821 var
->initializer
= slang_operation_new(1);
822 slang_operation_literal_bool(var
->initializer
, initValue
);
827 slang_resolve_variable(slang_operation
*oper
)
829 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
830 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
836 * Rewrite AST code for "return expression;".
838 * We return values from functions by assinging the returned value to
839 * the hidden __retVal variable which is an extra 'out' parameter we add
840 * to the function signature.
841 * This code basically converts "return expr;" into "__retVal = expr; return;"
843 * \return the new AST code.
845 static slang_operation
*
846 gen_return_with_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
848 slang_operation
*blockOper
, *assignOper
;
850 assert(oper
->type
== SLANG_OPER_RETURN
);
852 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
853 slang_info_log_error(A
->log
, "illegal return expression");
857 blockOper
= slang_operation_new(1);
858 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
859 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
860 slang_operation_add_children(blockOper
, 2);
862 if (A
->UseReturnFlag
) {
871 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
872 ifOper
->type
= SLANG_OPER_IF
;
873 slang_operation_add_children(ifOper
, 3);
875 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
876 cond
->type
= SLANG_OPER_IDENTIFIER
;
877 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
880 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
881 elseOper
->type
= SLANG_OPER_VOID
;
883 assignOper
= slang_oper_child(ifOper
, 1);
886 slang_operation
*setOper
= slang_oper_child(blockOper
, 1);
887 setOper
->type
= SLANG_OPER_ASSIGN
;
888 slang_operation_add_children(setOper
, 2);
890 slang_operation
*lhs
= slang_oper_child(setOper
, 0);
891 lhs
->type
= SLANG_OPER_IDENTIFIER
;
892 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
895 slang_operation
*rhs
= slang_oper_child(setOper
, 1);
896 slang_operation_literal_bool(rhs
, GL_FALSE
);
907 assignOper
= slang_oper_child(blockOper
, 0);
909 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
910 returnOper
->type
= SLANG_OPER_RETURN_INLINED
;
911 assert(returnOper
->num_children
== 0);
915 /* __retVal = expression; */
916 assignOper
->type
= SLANG_OPER_ASSIGN
;
917 slang_operation_add_children(assignOper
, 2);
919 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
920 lhs
->type
= SLANG_OPER_IDENTIFIER
;
921 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
924 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
925 slang_operation_copy(rhs
, &oper
->children
[0]);
928 ///blockOper->locals->outer_scope = oper->locals->outer_scope;
930 /*slang_print_tree(blockOper, 0);*/
937 * Rewrite AST code for "return;" (no expression).
939 static slang_operation
*
940 gen_return_without_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
942 slang_operation
*newRet
;
944 assert(oper
->type
== SLANG_OPER_RETURN
);
946 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
947 slang_info_log_error(A
->log
, "return statement requires an expression");
951 if (A
->UseReturnFlag
) {
956 newRet
= slang_operation_new(1);
957 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
958 newRet
->type
= SLANG_OPER_ASSIGN
;
959 slang_operation_add_children(newRet
, 2);
961 slang_operation
*lhs
= slang_oper_child(newRet
, 0);
962 lhs
->type
= SLANG_OPER_IDENTIFIER
;
963 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
966 slang_operation
*rhs
= slang_oper_child(newRet
, 1);
967 slang_operation_literal_bool(rhs
, GL_FALSE
);
975 newRet
= slang_operation_new(1);
976 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
977 newRet
->type
= SLANG_OPER_RETURN_INLINED
;
980 /*slang_print_tree(newRet, 0);*/
989 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
992 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
993 GLuint substCount
, slang_variable
**substOld
,
994 slang_operation
**substNew
, GLboolean isLHS
)
996 switch (oper
->type
) {
997 case SLANG_OPER_VARIABLE_DECL
:
999 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
1000 oper
->a_id
, GL_TRUE
);
1002 if (v
->initializer
&& oper
->num_children
== 0) {
1003 /* set child of oper to copy of initializer */
1004 oper
->num_children
= 1;
1005 oper
->children
= slang_operation_new(1);
1006 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1008 if (oper
->num_children
== 1) {
1009 /* the initializer */
1010 slang_substitute(A
, &oper
->children
[0], substCount
,
1011 substOld
, substNew
, GL_FALSE
);
1015 case SLANG_OPER_IDENTIFIER
:
1016 assert(oper
->num_children
== 0);
1017 if (1/**!isLHS XXX FIX */) {
1018 slang_atom id
= oper
->a_id
;
1021 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1023 if (_mesa_strcmp((char *) oper
->a_id
, "__notRetFlag"))
1024 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1028 /* look for a substitution */
1029 for (i
= 0; i
< substCount
; i
++) {
1030 if (v
== substOld
[i
]) {
1031 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1032 #if 0 /* DEBUG only */
1033 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1034 assert(substNew
[i
]->var
);
1035 assert(substNew
[i
]->var
->a_name
);
1036 printf("Substitute %s with %s in id node %p\n",
1037 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1041 printf("Substitute %s with %f in id node %p\n",
1042 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1046 slang_operation_copy(oper
, substNew
[i
]);
1053 case SLANG_OPER_RETURN
:
1055 slang_operation
*newReturn
;
1056 /* generate new 'return' code' */
1057 if (slang_oper_child(oper
, 0)->type
== SLANG_OPER_VOID
)
1058 newReturn
= gen_return_without_expression(A
, oper
);
1060 newReturn
= gen_return_with_expression(A
, oper
);
1065 /* do substitutions on the new 'return' code */
1066 slang_substitute(A
, newReturn
,
1067 substCount
, substOld
, substNew
, GL_FALSE
);
1069 /* install new 'return' code */
1070 slang_operation_copy(oper
, newReturn
);
1071 slang_operation_destruct(newReturn
);
1075 case SLANG_OPER_ASSIGN
:
1076 case SLANG_OPER_SUBSCRIPT
:
1078 * child[0] can't have substitutions but child[1] can.
1080 slang_substitute(A
, &oper
->children
[0],
1081 substCount
, substOld
, substNew
, GL_TRUE
);
1082 slang_substitute(A
, &oper
->children
[1],
1083 substCount
, substOld
, substNew
, GL_FALSE
);
1085 case SLANG_OPER_FIELD
:
1086 /* XXX NEW - test */
1087 slang_substitute(A
, &oper
->children
[0],
1088 substCount
, substOld
, substNew
, GL_TRUE
);
1093 for (i
= 0; i
< oper
->num_children
; i
++)
1094 slang_substitute(A
, &oper
->children
[i
],
1095 substCount
, substOld
, substNew
, GL_FALSE
);
1102 * Produce inline code for a call to an assembly instruction.
1103 * This is typically used to compile a call to a built-in function like this:
1105 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1107 * __asm vec4_lrp __retVal, a, y, x;
1112 * r = mix(p1, p2, p3);
1122 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1124 static slang_operation
*
1125 slang_inline_asm_function(slang_assemble_ctx
*A
,
1126 slang_function
*fun
, slang_operation
*oper
)
1128 const GLuint numArgs
= oper
->num_children
;
1130 slang_operation
*inlined
;
1131 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1132 slang_variable
**substOld
;
1133 slang_operation
**substNew
;
1135 ASSERT(slang_is_asm_function(fun
));
1136 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1139 printf("Inline %s as %s\n",
1140 (char*) fun->header.a_name,
1141 (char*) fun->body->children[0].a_id);
1145 * We'll substitute formal params with actual args in the asm call.
1147 substOld
= (slang_variable
**)
1148 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1149 substNew
= (slang_operation
**)
1150 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1151 for (i
= 0; i
< numArgs
; i
++) {
1152 substOld
[i
] = fun
->parameters
->variables
[i
];
1153 substNew
[i
] = oper
->children
+ i
;
1156 /* make a copy of the code to inline */
1157 inlined
= slang_operation_new(1);
1158 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1160 /* get rid of the __retVal child */
1161 inlined
->num_children
--;
1162 for (i
= 0; i
< inlined
->num_children
; i
++) {
1163 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1167 /* now do formal->actual substitutions */
1168 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1170 _slang_free(substOld
);
1171 _slang_free(substNew
);
1174 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1175 (char *) fun
->header
.a_name
);
1176 slang_print_tree(inlined
, 3);
1177 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1185 * Inline the given function call operation.
1186 * Return a new slang_operation that corresponds to the inlined code.
1188 static slang_operation
*
1189 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1190 slang_operation
*oper
, slang_operation
*returnOper
)
1197 ParamMode
*paramMode
;
1198 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1199 const GLuint numArgs
= oper
->num_children
;
1200 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1201 slang_operation
*args
= oper
->children
;
1202 slang_operation
*inlined
, *top
;
1203 slang_variable
**substOld
;
1204 slang_operation
**substNew
;
1205 GLuint substCount
, numCopyIn
, i
;
1206 slang_function
*prevFunction
;
1207 slang_variable_scope
*newScope
= NULL
;
1210 prevFunction
= A
->CurFunction
;
1211 A
->CurFunction
= fun
;
1213 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1214 assert(fun
->param_count
== totalArgs
);
1216 /* allocate temporary arrays */
1217 paramMode
= (ParamMode
*)
1218 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1219 substOld
= (slang_variable
**)
1220 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1221 substNew
= (slang_operation
**)
1222 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1225 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1226 (char *) fun
->header
.a_name
,
1227 fun
->parameters
->num_variables
, numArgs
);
1230 if (haveRetValue
&& !returnOper
) {
1231 /* Create 3-child comma sequence for inlined code:
1232 * child[0]: declare __resultTmp
1233 * child[1]: inlined function body
1234 * child[2]: __resultTmp
1236 slang_operation
*commaSeq
;
1237 slang_operation
*declOper
= NULL
;
1238 slang_variable
*resultVar
;
1240 commaSeq
= slang_operation_new(1);
1241 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1242 assert(commaSeq
->locals
);
1243 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1244 commaSeq
->num_children
= 3;
1245 commaSeq
->children
= slang_operation_new(3);
1246 /* allocate the return var */
1247 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1249 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1250 (void*)commaSeq->locals, (char *) fun->header.a_name);
1253 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1254 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1255 resultVar
->isTemp
= GL_TRUE
;
1257 /* child[0] = __resultTmp declaration */
1258 declOper
= &commaSeq
->children
[0];
1259 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1260 declOper
->a_id
= resultVar
->a_name
;
1261 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1263 /* child[1] = function body */
1264 inlined
= &commaSeq
->children
[1];
1265 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1267 /* child[2] = __resultTmp reference */
1268 returnOper
= &commaSeq
->children
[2];
1269 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1270 returnOper
->a_id
= resultVar
->a_name
;
1271 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1276 top
= inlined
= slang_operation_new(1);
1277 /* XXXX this may be inappropriate!!!! */
1278 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1282 assert(inlined
->locals
);
1284 /* Examine the parameters, look for inout/out params, look for possible
1285 * substitutions, etc:
1286 * param type behaviour
1287 * in copy actual to local
1288 * const in substitute param with actual
1292 for (i
= 0; i
< totalArgs
; i
++) {
1293 slang_variable
*p
= fun
->parameters
->variables
[i
];
1295 printf("Param %d: %s %s \n", i,
1296 slang_type_qual_string(p->type.qualifier),
1297 (char *) p->a_name);
1299 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1300 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1301 /* an output param */
1302 slang_operation
*arg
;
1307 paramMode
[i
] = SUBST
;
1309 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1310 slang_resolve_variable(arg
);
1312 /* replace parameter 'p' with argument 'arg' */
1313 substOld
[substCount
] = p
;
1314 substNew
[substCount
] = arg
; /* will get copied */
1317 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1318 /* a constant input param */
1319 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1320 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
||
1321 args
[i
].type
== SLANG_OPER_SUBSCRIPT
) {
1322 /* replace all occurances of this parameter variable with the
1323 * actual argument variable or a literal.
1325 paramMode
[i
] = SUBST
;
1326 slang_resolve_variable(&args
[i
]);
1327 substOld
[substCount
] = p
;
1328 substNew
[substCount
] = &args
[i
]; /* will get copied */
1332 paramMode
[i
] = COPY_IN
;
1336 paramMode
[i
] = COPY_IN
;
1338 assert(paramMode
[i
]);
1341 /* actual code inlining: */
1342 slang_operation_copy(inlined
, fun
->body
);
1344 /*** XXX review this */
1345 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1346 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1347 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1350 printf("======================= orig body code ======================\n");
1351 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1352 slang_print_tree(fun
->body
, 8);
1353 printf("======================= copied code =========================\n");
1354 slang_print_tree(inlined
, 8);
1357 /* do parameter substitution in inlined code: */
1358 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1361 printf("======================= subst code ==========================\n");
1362 slang_print_tree(inlined
, 8);
1363 printf("=============================================================\n");
1366 /* New prolog statements: (inserted before the inlined code)
1367 * Copy the 'in' arguments.
1370 for (i
= 0; i
< numArgs
; i
++) {
1371 if (paramMode
[i
] == COPY_IN
) {
1372 slang_variable
*p
= fun
->parameters
->variables
[i
];
1373 /* declare parameter 'p' */
1374 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1378 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1379 assert(decl
->locals
);
1380 decl
->locals
->outer_scope
= inlined
->locals
;
1381 decl
->a_id
= p
->a_name
;
1382 decl
->num_children
= 1;
1383 decl
->children
= slang_operation_new(1);
1385 /* child[0] is the var's initializer */
1386 slang_operation_copy(&decl
->children
[0], args
+ i
);
1388 /* add parameter 'p' to the local variable scope here */
1390 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1391 pCopy
->type
= p
->type
;
1392 pCopy
->a_name
= p
->a_name
;
1393 pCopy
->array_len
= p
->array_len
;
1396 newScope
= inlined
->locals
;
1401 /* Now add copies of the function's local vars to the new variable scope */
1402 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1403 slang_variable
*p
= fun
->parameters
->variables
[i
];
1404 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1405 pCopy
->type
= p
->type
;
1406 pCopy
->a_name
= p
->a_name
;
1407 pCopy
->array_len
= p
->array_len
;
1411 /* New epilog statements:
1412 * 1. Create end of function label to jump to from return statements.
1413 * 2. Copy the 'out' parameter vars
1416 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1418 inlined
->num_children
);
1419 lab
->type
= SLANG_OPER_LABEL
;
1420 lab
->label
= A
->curFuncEndLabel
;
1423 for (i
= 0; i
< totalArgs
; i
++) {
1424 if (paramMode
[i
] == COPY_OUT
) {
1425 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1426 /* actualCallVar = outParam */
1427 /*if (i > 0 || !haveRetValue)*/
1428 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1430 inlined
->num_children
);
1431 ass
->type
= SLANG_OPER_ASSIGN
;
1432 ass
->num_children
= 2;
1433 ass
->locals
->outer_scope
= inlined
->locals
;
1434 ass
->children
= slang_operation_new(2);
1435 ass
->children
[0] = args
[i
]; /*XXX copy */
1436 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1437 ass
->children
[1].a_id
= p
->a_name
;
1438 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1442 _slang_free(paramMode
);
1443 _slang_free(substOld
);
1444 _slang_free(substNew
);
1446 /* Update scoping to use the new local vars instead of the
1447 * original function's vars. This is especially important
1448 * for nested inlining.
1451 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1454 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1455 (char *) fun
->header
.a_name
,
1456 fun
->parameters
->num_variables
, numArgs
);
1457 slang_print_tree(top
, 0);
1461 A
->CurFunction
= prevFunction
;
1468 * Insert declaration for "bool __notRetFlag" in given block operation.
1469 * This is used when we can't emit "early" return statements in subroutines.
1472 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1474 slang_operation
*decl
;
1476 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1477 oper
->type
== SLANG_OPER_SEQUENCE
);
1479 decl
= slang_operation_insert_child(oper
, 1);
1481 slang_generate_declaration(A
, oper
->locals
, decl
,
1482 SLANG_SPEC_BOOL
, "__notRetFlag", GL_TRUE
);
1484 /*slang_print_tree(oper, 0);*/
1489 * Recursively replace instances of the old node type with the new type.
1492 replace_node_type(slang_operation
*oper
, slang_operation_type oldType
,
1493 slang_operation_type newType
)
1497 if (oper
->type
== oldType
)
1498 oper
->type
= newType
;
1500 for (i
= 0; i
< slang_oper_num_children(oper
); i
++) {
1501 replace_node_type(slang_oper_child(oper
, i
), oldType
, newType
);
1508 * Test if the given function body has an "early return". That is, there's
1509 * a 'return' statement that's not the very last instruction in the body.
1512 has_early_return(const slang_operation
*funcBody
)
1514 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1517 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1525 * Emit IR code for a function call. This does one of two things:
1526 * 1. Inline the function's code
1527 * 2. Create an IR for the function's body and create a real call to it.
1529 static slang_ir_node
*
1530 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1531 slang_operation
*oper
, slang_operation
*dest
)
1534 slang_operation
*instance
;
1535 slang_label
*prevFuncEndLabel
;
1538 prevFuncEndLabel
= A
->curFuncEndLabel
;
1539 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1540 A
->curFuncEndLabel
= _slang_label_new(name
);
1541 assert(A
->curFuncEndLabel
);
1544 * 'instance' is basically a copy of the function's body with various
1548 if (slang_is_asm_function(fun
) && !dest
) {
1549 /* assemble assembly function - tree style */
1550 instance
= slang_inline_asm_function(A
, fun
, oper
);
1553 /* non-assembly function */
1554 /* We always generate an "inline-able" block of code here.
1556 * 1. insert the inline code
1557 * 2. Generate a call to the "inline" code as a subroutine
1559 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1561 if (earlyReturn
&& !A
->EmitContReturn
) {
1562 A
->UseReturnFlag
= GL_TRUE
;
1565 instance
= slang_inline_function_call(A
, fun
, oper
, dest
);
1570 /* The function we're calling has one or more 'return' statements
1571 * that prevent us from inlining the function's code.
1573 * In this case, change the function's body type from
1574 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1575 * During code emit this will result in a true subroutine call.
1577 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1579 slang_operation
*callOper
;
1581 assert(instance
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1582 instance
->type
== SLANG_OPER_SEQUENCE
);
1584 if (_slang_function_has_return_value(fun
) && !dest
) {
1585 assert(instance
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1586 assert(instance
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1587 callOper
= &instance
->children
[1];
1590 callOper
= instance
;
1593 if (A
->UseReturnFlag
) {
1594 /* Early returns not supported. Create a _returnFlag variable
1595 * that's set upon 'return' and tested elsewhere to no-op any
1596 * remaining instructions in the subroutine.
1598 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1599 callOper
->type
== SLANG_OPER_SEQUENCE
);
1600 declare_return_flag(A
, callOper
);
1603 /* We can emit real 'return' statements. If we generated any
1604 * 'inline return' statements during function instantiation,
1605 * change them back to regular 'return' statements.
1607 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1611 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1612 callOper
->fun
= fun
;
1613 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1616 /* If there are any 'return' statements remaining, they're at the
1617 * very end of the function and can effectively become no-ops.
1619 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1627 /* Replace the function call with the instance block (or new CALL stmt) */
1628 slang_operation_destruct(oper
);
1630 _slang_free(instance
);
1633 assert(instance
->locals
);
1634 printf("*** Inlined code for call to %s:\n", (char*) fun
->header
.a_name
);
1635 slang_print_tree(oper
, 10);
1639 n
= _slang_gen_operation(A
, oper
);
1641 /*_slang_label_delete(A->curFuncEndLabel);*/
1642 A
->curFuncEndLabel
= prevFuncEndLabel
;
1644 if (A
->pragmas
->Debug
) {
1646 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1647 n
->Comment
= _slang_strdup(s
);
1650 A
->UseReturnFlag
= GL_FALSE
;
1656 static slang_asm_info
*
1657 slang_find_asm_info(const char *name
)
1660 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1661 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1670 * Some write-masked assignments are simple, but others are hard.
1673 * v.xy = vec2(a, b);
1676 * v.zy = vec2(a, b);
1677 * this gets transformed/swizzled into:
1678 * v.zy = vec2(a, b).*yx* (* = don't care)
1679 * This function helps to determine simple vs. non-simple.
1682 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1684 switch (writemask
) {
1686 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1688 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1690 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1692 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1694 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1695 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1697 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1698 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1699 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1700 case WRITEMASK_XYZW
:
1701 return swizzle
== SWIZZLE_NOOP
;
1709 * Convert the given swizzle into a writemask. In some cases this
1710 * is trivial, in other cases, we'll need to also swizzle the right
1711 * hand side to put components in the right places.
1712 * See comment above for more info.
1713 * XXX this function could be simplified and should probably be renamed.
1714 * \param swizzle the incoming swizzle
1715 * \param writemaskOut returns the writemask
1716 * \param swizzleOut swizzle to apply to the right-hand-side
1717 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1720 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1721 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1723 GLuint mask
= 0x0, newSwizzle
[4];
1726 /* make new dst writemask, compute size */
1727 for (i
= 0; i
< 4; i
++) {
1728 const GLuint swz
= GET_SWZ(swizzle
, i
);
1729 if (swz
== SWIZZLE_NIL
) {
1733 assert(swz
>= 0 && swz
<= 3);
1735 if (swizzle
!= SWIZZLE_XXXX
&&
1736 swizzle
!= SWIZZLE_YYYY
&&
1737 swizzle
!= SWIZZLE_ZZZZ
&&
1738 swizzle
!= SWIZZLE_WWWW
&&
1739 (mask
& (1 << swz
))) {
1740 /* a channel can't be specified twice (ex: ".xyyz") */
1741 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1742 _mesa_swizzle_string(swizzle
, 0, 0));
1748 assert(mask
<= 0xf);
1749 size
= i
; /* number of components in mask/swizzle */
1751 *writemaskOut
= mask
;
1753 /* make new src swizzle, by inversion */
1754 for (i
= 0; i
< 4; i
++) {
1755 newSwizzle
[i
] = i
; /*identity*/
1757 for (i
= 0; i
< size
; i
++) {
1758 const GLuint swz
= GET_SWZ(swizzle
, i
);
1759 newSwizzle
[swz
] = i
;
1761 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1766 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1768 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1770 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1772 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1774 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1782 #if 0 /* not used, but don't remove just yet */
1784 * Recursively traverse 'oper' to produce a swizzle mask in the event
1785 * of any vector subscripts and swizzle suffixes.
1786 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1789 resolve_swizzle(const slang_operation
*oper
)
1791 if (oper
->type
== SLANG_OPER_FIELD
) {
1792 /* writemask from .xyzw suffix */
1794 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1795 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1799 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1800 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1804 return SWIZZLE_XYZW
;
1806 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1807 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1808 /* writemask from [index] */
1809 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1810 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1815 swizzle
= SWIZZLE_XXXX
;
1818 swizzle
= SWIZZLE_YYYY
;
1821 swizzle
= SWIZZLE_ZZZZ
;
1824 swizzle
= SWIZZLE_WWWW
;
1827 swizzle
= SWIZZLE_XYZW
;
1829 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1833 return SWIZZLE_XYZW
;
1841 * Recursively descend through swizzle nodes to find the node's storage info.
1843 static slang_ir_storage
*
1844 get_store(const slang_ir_node
*n
)
1846 if (n
->Opcode
== IR_SWIZZLE
) {
1847 return get_store(n
->Children
[0]);
1855 * Generate IR tree for an asm instruction/operation such as:
1856 * __asm vec4_dot __retVal.x, v1, v2;
1858 static slang_ir_node
*
1859 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1860 slang_operation
*dest
)
1862 const slang_asm_info
*info
;
1863 slang_ir_node
*kids
[3], *n
;
1864 GLuint j
, firstOperand
;
1866 assert(oper
->type
== SLANG_OPER_ASM
);
1868 info
= slang_find_asm_info((char *) oper
->a_id
);
1870 _mesa_problem(NULL
, "undefined __asm function %s\n",
1871 (char *) oper
->a_id
);
1874 assert(info
->NumParams
<= 3);
1876 if (info
->NumParams
== oper
->num_children
) {
1877 /* Storage for result is not specified.
1878 * Children[0], [1], [2] are the operands.
1883 /* Storage for result (child[0]) is specified.
1884 * Children[1], [2], [3] are the operands.
1889 /* assemble child(ren) */
1890 kids
[0] = kids
[1] = kids
[2] = NULL
;
1891 for (j
= 0; j
< info
->NumParams
; j
++) {
1892 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1897 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1900 /* Setup n->Store to be a particular location. Otherwise, storage
1901 * for the result (a temporary) will be allocated later.
1903 slang_operation
*dest_oper
;
1906 dest_oper
= &oper
->children
[0];
1908 n0
= _slang_gen_operation(A
, dest_oper
);
1913 n
->Store
= n0
->Store
;
1915 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1926 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1929 for (i
= 0; i
< scope
->num_functions
; i
++) {
1930 slang_function
*f
= &scope
->functions
[i
];
1931 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1932 printf(" %s (%d args)\n", name
, f
->param_count
);
1935 if (scope
->outer_scope
)
1936 print_funcs(scope
->outer_scope
, name
);
1942 * Find a function of the given name, taking 'numArgs' arguments.
1943 * This is the function we'll try to call when there is no exact match
1944 * between function parameters and call arguments.
1946 * XXX we should really create a list of candidate functions and try
1949 static slang_function
*
1950 _slang_find_function_by_argc(slang_function_scope
*scope
,
1951 const char *name
, int numArgs
)
1955 for (i
= 0; i
< scope
->num_functions
; i
++) {
1956 slang_function
*f
= &scope
->functions
[i
];
1957 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1958 int haveRetValue
= _slang_function_has_return_value(f
);
1959 if (numArgs
== f
->param_count
- haveRetValue
)
1963 scope
= scope
->outer_scope
;
1970 static slang_function
*
1971 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1974 slang_function
*maxFunc
= NULL
;
1979 for (i
= 0; i
< scope
->num_functions
; i
++) {
1980 slang_function
*f
= &scope
->functions
[i
];
1981 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1982 if (f
->param_count
> maxArgs
) {
1983 maxArgs
= f
->param_count
;
1988 scope
= scope
->outer_scope
;
1996 * Generate a new slang_function which is a constructor for a user-defined
1999 static slang_function
*
2000 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2002 const GLint numFields
= str
->fields
->num_variables
;
2003 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2005 /* function header (name, return type) */
2006 fun
->header
.a_name
= str
->a_name
;
2007 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2008 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2009 fun
->header
.type
.specifier
._struct
= str
;
2011 /* function parameters (= struct's fields) */
2014 for (i
= 0; i
< numFields
; i
++) {
2016 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2018 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2019 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2020 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2022 fun
->param_count
= fun
->parameters
->num_variables
;
2025 /* Add __retVal to params */
2027 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2028 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2030 p
->a_name
= a_retVal
;
2031 p
->type
= fun
->header
.type
;
2032 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2036 /* function body is:
2046 slang_variable_scope
*scope
;
2047 slang_variable
*var
;
2050 fun
->body
= slang_operation_new(1);
2051 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2052 fun
->body
->num_children
= numFields
+ 2;
2053 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2055 scope
= fun
->body
->locals
;
2056 scope
->outer_scope
= fun
->parameters
;
2058 /* create local var 't' */
2059 var
= slang_variable_scope_grow(scope
);
2060 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2061 var
->type
= fun
->header
.type
;
2065 slang_operation
*decl
;
2067 decl
= &fun
->body
->children
[0];
2068 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2069 decl
->locals
= _slang_variable_scope_new(scope
);
2070 decl
->a_id
= var
->a_name
;
2073 /* assign params to fields of t */
2074 for (i
= 0; i
< numFields
; i
++) {
2075 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2077 assign
->type
= SLANG_OPER_ASSIGN
;
2078 assign
->locals
= _slang_variable_scope_new(scope
);
2079 assign
->num_children
= 2;
2080 assign
->children
= slang_operation_new(2);
2083 slang_operation
*lhs
= &assign
->children
[0];
2085 lhs
->type
= SLANG_OPER_FIELD
;
2086 lhs
->locals
= _slang_variable_scope_new(scope
);
2087 lhs
->num_children
= 1;
2088 lhs
->children
= slang_operation_new(1);
2089 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2091 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2092 lhs
->children
[0].a_id
= var
->a_name
;
2093 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2096 lhs
->children
[1].num_children
= 1;
2097 lhs
->children
[1].children
= slang_operation_new(1);
2098 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2099 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2100 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2105 slang_operation
*rhs
= &assign
->children
[1];
2107 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2108 rhs
->locals
= _slang_variable_scope_new(scope
);
2109 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2115 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2117 ret
->type
= SLANG_OPER_RETURN
;
2118 ret
->locals
= _slang_variable_scope_new(scope
);
2119 ret
->num_children
= 1;
2120 ret
->children
= slang_operation_new(1);
2121 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2122 ret
->children
[0].a_id
= var
->a_name
;
2123 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2127 slang_print_function(fun, 1);
2134 * Find/create a function (constructor) for the given structure name.
2136 static slang_function
*
2137 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2140 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2141 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2142 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2143 /* found a structure type that matches the function name */
2144 if (!str
->constructor
) {
2145 /* create the constructor function now */
2146 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2148 return str
->constructor
;
2156 * Generate a new slang_function to satisfy a call to an array constructor.
2157 * Ex: float[3](1., 2., 3.)
2159 static slang_function
*
2160 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2162 slang_type_specifier_type baseType
;
2163 slang_function
*fun
;
2166 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2170 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2172 num_elements
= oper
->num_children
;
2174 /* function header, return type */
2176 fun
->header
.a_name
= oper
->a_id
;
2177 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2178 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2179 fun
->header
.type
.specifier
._array
=
2180 slang_type_specifier_new(baseType
, NULL
, NULL
);
2181 fun
->header
.type
.array_len
= num_elements
;
2184 /* function parameters (= number of elements) */
2187 for (i
= 0; i
< num_elements
; i
++) {
2189 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2191 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2193 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2194 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2195 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2196 p
->type
.specifier
.type
= baseType
;
2198 fun
->param_count
= fun
->parameters
->num_variables
;
2201 /* Add __retVal to params */
2203 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2204 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2206 p
->a_name
= a_retVal
;
2207 p
->type
= fun
->header
.type
;
2208 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2209 p
->type
.specifier
.type
= baseType
;
2213 /* function body is:
2223 slang_variable_scope
*scope
;
2224 slang_variable
*var
;
2227 fun
->body
= slang_operation_new(1);
2228 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2229 fun
->body
->num_children
= num_elements
+ 2;
2230 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2232 scope
= fun
->body
->locals
;
2233 scope
->outer_scope
= fun
->parameters
;
2235 /* create local var 't' */
2236 var
= slang_variable_scope_grow(scope
);
2237 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2238 var
->type
= fun
->header
.type
;/*XXX copy*/
2242 slang_operation
*decl
;
2244 decl
= &fun
->body
->children
[0];
2245 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2246 decl
->locals
= _slang_variable_scope_new(scope
);
2247 decl
->a_id
= var
->a_name
;
2250 /* assign params to elements of t */
2251 for (i
= 0; i
< num_elements
; i
++) {
2252 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2254 assign
->type
= SLANG_OPER_ASSIGN
;
2255 assign
->locals
= _slang_variable_scope_new(scope
);
2256 assign
->num_children
= 2;
2257 assign
->children
= slang_operation_new(2);
2260 slang_operation
*lhs
= &assign
->children
[0];
2262 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2263 lhs
->locals
= _slang_variable_scope_new(scope
);
2264 lhs
->num_children
= 2;
2265 lhs
->children
= slang_operation_new(2);
2267 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2268 lhs
->children
[0].a_id
= var
->a_name
;
2269 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2271 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2272 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2276 slang_operation
*rhs
= &assign
->children
[1];
2278 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2279 rhs
->locals
= _slang_variable_scope_new(scope
);
2280 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2286 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2288 ret
->type
= SLANG_OPER_RETURN
;
2289 ret
->locals
= _slang_variable_scope_new(scope
);
2290 ret
->num_children
= 1;
2291 ret
->children
= slang_operation_new(1);
2292 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2293 ret
->children
[0].a_id
= var
->a_name
;
2294 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2299 slang_print_function(fun, 1);
2307 _slang_is_vec_mat_type(const char *name
)
2309 static const char *vecmat_types
[] = {
2310 "float", "int", "bool",
2311 "vec2", "vec3", "vec4",
2312 "ivec2", "ivec3", "ivec4",
2313 "bvec2", "bvec3", "bvec4",
2314 "mat2", "mat3", "mat4",
2315 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2319 for (i
= 0; vecmat_types
[i
]; i
++)
2320 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2327 * Assemble a function call, given a particular function name.
2328 * \param name the function's name (operators like '*' are possible).
2330 static slang_ir_node
*
2331 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2332 slang_operation
*oper
, slang_operation
*dest
)
2334 slang_operation
*params
= oper
->children
;
2335 const GLuint param_count
= oper
->num_children
;
2337 slang_function
*fun
;
2340 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2341 if (atom
== SLANG_ATOM_NULL
)
2344 if (oper
->array_constructor
) {
2345 /* this needs special handling */
2346 fun
= _slang_make_array_constructor(A
, oper
);
2349 /* Try to find function by name and exact argument type matching */
2350 GLboolean error
= GL_FALSE
;
2351 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2352 &A
->space
, A
->atoms
, A
->log
, &error
);
2354 slang_info_log_error(A
->log
,
2355 "Function '%s' not found (check argument types)",
2362 /* Next, try locating a constructor function for a user-defined type */
2363 fun
= _slang_locate_struct_constructor(A
, name
);
2367 * At this point, some heuristics are used to try to find a function
2368 * that matches the calling signature by means of casting or "unrolling"
2372 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2373 /* Next, if this call looks like a vec() or mat() constructor call,
2374 * try "unwinding" the args to satisfy a constructor.
2376 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2378 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2379 slang_info_log_error(A
->log
,
2380 "Function '%s' not found (check argument types)",
2387 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2388 /* Next, try casting args to the types of the formal parameters */
2389 int numArgs
= oper
->num_children
;
2390 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2391 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2392 slang_info_log_error(A
->log
,
2393 "Function '%s' not found (check argument types)",
2401 slang_info_log_error(A
->log
,
2402 "Function '%s' not found (check argument types)",
2408 /* The function body may be in another compilation unit.
2409 * We'll try concatenating the shaders and recompile at link time.
2411 A
->UnresolvedRefs
= GL_TRUE
;
2412 return new_node1(IR_NOP
, NULL
);
2415 /* type checking to be sure function's return type matches 'dest' type */
2419 slang_typeinfo_construct(&t0
);
2420 typeof_operation(A
, dest
, &t0
);
2422 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2423 slang_info_log_error(A
->log
,
2424 "Incompatible type returned by call to '%s'",
2430 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2432 if (n
&& !n
->Store
&& !dest
2433 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2434 /* setup n->Store for the result of the function call */
2435 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2436 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2437 /*printf("Alloc storage for function result, size %d \n", size);*/
2440 if (oper
->array_constructor
) {
2441 /* free the temporary array constructor function now */
2442 slang_function_destruct(fun
);
2449 static slang_ir_node
*
2450 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2452 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2454 slang_variable
*var
;
2456 /* NOTE: In GLSL 1.20, there's only one kind of method
2457 * call: array.length(). Anything else is an error.
2459 if (oper
->a_id
!= a_length
) {
2460 slang_info_log_error(A
->log
,
2461 "Undefined method call '%s'", (char *) oper
->a_id
);
2465 /* length() takes no arguments */
2466 if (oper
->num_children
> 0) {
2467 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2471 /* lookup the object/variable */
2472 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2473 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2474 slang_info_log_error(A
->log
,
2475 "Undefined object '%s'", (char *) oper
->a_obj
);
2479 /* Create a float/literal IR node encoding the array length */
2480 n
= new_node0(IR_FLOAT
);
2482 n
->Value
[0] = (float) _slang_array_length(var
);
2483 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2490 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2492 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2493 oper
->type
== SLANG_OPER_LITERAL_INT
||
2494 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2495 if (oper
->literal
[0])
2501 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2502 oper
->num_children
== 1) {
2503 return _slang_is_constant_cond(&oper
->children
[0], value
);
2510 * Test if an operation is a scalar or boolean.
2513 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2515 slang_typeinfo type
;
2518 slang_typeinfo_construct(&type
);
2519 typeof_operation(A
, oper
, &type
);
2520 size
= _slang_sizeof_type_specifier(&type
.spec
);
2521 slang_typeinfo_destruct(&type
);
2527 * Test if an operation is boolean.
2530 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2532 slang_typeinfo type
;
2535 slang_typeinfo_construct(&type
);
2536 typeof_operation(A
, oper
, &type
);
2537 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2538 slang_typeinfo_destruct(&type
);
2544 * Check if a loop contains a 'continue' statement.
2545 * Stop looking if we find a nested loop.
2548 _slang_loop_contains_continue(const slang_operation
*oper
)
2550 switch (oper
->type
) {
2551 case SLANG_OPER_CONTINUE
:
2553 case SLANG_OPER_FOR
:
2555 case SLANG_OPER_WHILE
:
2556 /* stop upon finding a nested loop */
2562 for (i
= 0; i
< oper
->num_children
; i
++) {
2563 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2564 if (_slang_loop_contains_continue(child
))
2574 * Check if a loop contains a 'continue' or 'break' statement.
2575 * Stop looking if we find a nested loop.
2578 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2580 switch (oper
->type
) {
2581 case SLANG_OPER_CONTINUE
:
2582 case SLANG_OPER_BREAK
:
2584 case SLANG_OPER_FOR
:
2586 case SLANG_OPER_WHILE
:
2587 /* stop upon finding a nested loop */
2593 for (i
= 0; i
< oper
->num_children
; i
++) {
2594 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2595 if (_slang_loop_contains_continue_or_break(child
))
2605 * Replace 'break' and 'continue' statements inside a do and while loops.
2606 * This is a recursive helper function used by
2607 * _slang_gen_do/while_without_continue().
2610 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2612 switch (oper
->type
) {
2613 case SLANG_OPER_BREAK
:
2614 /* replace 'break' with "_notBreakFlag = false; break" */
2616 slang_operation
*block
= oper
;
2617 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2618 slang_operation_add_children(block
, 2);
2620 slang_operation
*assign
= slang_oper_child(block
, 0);
2621 assign
->type
= SLANG_OPER_ASSIGN
;
2622 slang_operation_add_children(assign
, 2);
2624 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2625 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2628 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2629 slang_operation_literal_bool(rhs
, GL_FALSE
);
2633 slang_operation
*brk
= slang_oper_child(block
, 1);
2634 brk
->type
= SLANG_OPER_BREAK
;
2635 assert(!brk
->children
);
2639 case SLANG_OPER_CONTINUE
:
2640 /* convert continue into a break */
2641 oper
->type
= SLANG_OPER_BREAK
;
2643 case SLANG_OPER_FOR
:
2645 case SLANG_OPER_WHILE
:
2646 /* stop upon finding a nested loop */
2652 for (i
= 0; i
< oper
->num_children
; i
++) {
2653 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2661 * Transform a while-loop so that continue statements are converted to breaks.
2662 * Then do normal IR code generation.
2666 * while (LOOPCOND) {
2678 * bool _notBreakFlag = 1;
2679 * while (_notBreakFlag && LOOPCOND) {
2683 * break; // was continue
2686 * _notBreakFlag = 0; // was
2693 static slang_ir_node
*
2694 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2696 slang_operation
*top
;
2697 slang_operation
*innerBody
;
2699 assert(oper
->type
== SLANG_OPER_WHILE
);
2701 top
= slang_operation_new(1);
2702 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2703 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2704 slang_operation_add_children(top
, 2);
2706 /* declare: bool _notBreakFlag = true */
2708 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2709 slang_generate_declaration(A
, top
->locals
, condDecl
,
2710 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2713 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2715 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2716 outerWhile
->type
= SLANG_OPER_WHILE
;
2717 slang_operation_add_children(outerWhile
, 2);
2719 /* _notBreakFlag && LOOPCOND */
2721 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2722 cond
->type
= SLANG_OPER_LOGICALAND
;
2723 slang_operation_add_children(cond
, 2);
2725 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2726 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2729 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2730 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2736 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2737 innerDo
->type
= SLANG_OPER_DO
;
2738 slang_operation_add_children(innerDo
, 2);
2740 /* copy original do-loop body into inner do-loop's body */
2741 innerBody
= slang_oper_child(innerDo
, 0);
2742 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2743 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2745 /* inner do-loop's condition is constant/false */
2747 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2748 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2753 /* Finally, in innerBody,
2754 * replace "break" with "_notBreakFlag = 0; break"
2755 * replace "continue" with "break"
2757 replace_break_and_cont(A
, innerBody
);
2759 /*slang_print_tree(top, 0);*/
2761 return _slang_gen_operation(A
, top
);
2768 * Generate loop code using high-level IR_LOOP instruction
2770 static slang_ir_node
*
2771 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2775 * BREAK if !expr (child[0])
2776 * body code (child[1])
2778 slang_ir_node
*loop
, *breakIf
, *body
;
2779 GLboolean isConst
, constTrue
= GL_FALSE
;
2781 if (!A
->EmitContReturn
) {
2782 /* We don't want to emit CONT instructions. If this while-loop has
2783 * a continue, translate it away.
2785 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2786 return _slang_gen_while_without_continue(A
, oper
);
2790 /* type-check expression */
2791 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2792 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2796 /* Check if loop condition is a constant */
2797 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2799 if (isConst
&& !constTrue
) {
2800 /* loop is never executed! */
2801 return new_node0(IR_NOP
);
2804 /* Begin new loop */
2805 loop
= new_loop(NULL
);
2807 /* save loop state */
2808 push_loop(A
, oper
, loop
);
2810 if (isConst
&& constTrue
) {
2811 /* while(nonzero constant), no conditional break */
2816 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2817 breakIf
= new_break_if_true(A
, cond
);
2819 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2820 loop
->Children
[0] = new_seq(breakIf
, body
);
2822 /* Do infinite loop detection */
2823 /* loop->List is head of linked list of break/continue nodes */
2824 if (!loop
->List
&& isConst
&& constTrue
) {
2825 /* infinite loop detected */
2827 slang_info_log_error(A
->log
, "Infinite loop detected!");
2831 /* restore loop state */
2839 * Transform a do-while-loop so that continue statements are converted to breaks.
2840 * Then do normal IR code generation.
2851 * } while (LOOPCOND);
2856 * bool _notBreakFlag = 1;
2861 * break; // was continue
2864 * _notBreakFlag = 0; // was
2868 * } while (_notBreakFlag && LOOPCOND);
2871 static slang_ir_node
*
2872 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2874 slang_operation
*top
;
2875 slang_operation
*innerBody
;
2877 assert(oper
->type
== SLANG_OPER_DO
);
2879 top
= slang_operation_new(1);
2880 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2881 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2882 slang_operation_add_children(top
, 2);
2884 /* declare: bool _notBreakFlag = true */
2886 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2887 slang_generate_declaration(A
, top
->locals
, condDecl
,
2888 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2891 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2893 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2894 outerDo
->type
= SLANG_OPER_DO
;
2895 slang_operation_add_children(outerDo
, 2);
2899 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2900 innerDo
->type
= SLANG_OPER_DO
;
2901 slang_operation_add_children(innerDo
, 2);
2903 /* copy original do-loop body into inner do-loop's body */
2904 innerBody
= slang_oper_child(innerDo
, 0);
2905 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2906 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2908 /* inner do-loop's condition is constant/false */
2910 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2911 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2915 /* _notBreakFlag && LOOPCOND */
2917 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2918 cond
->type
= SLANG_OPER_LOGICALAND
;
2919 slang_operation_add_children(cond
, 2);
2921 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2922 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2925 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2926 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2931 /* Finally, in innerBody,
2932 * replace "break" with "_notBreakFlag = 0; break"
2933 * replace "continue" with "break"
2935 replace_break_and_cont(A
, innerBody
);
2937 /*slang_print_tree(top, 0);*/
2939 return _slang_gen_operation(A
, top
);
2944 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2946 static slang_ir_node
*
2947 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2951 * body code (child[0])
2953 * BREAK if !expr (child[1])
2955 slang_ir_node
*loop
;
2956 GLboolean isConst
, constTrue
;
2958 if (!A
->EmitContReturn
) {
2959 /* We don't want to emit CONT instructions. If this do-loop has
2960 * a continue, translate it away.
2962 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2963 return _slang_gen_do_without_continue(A
, oper
);
2967 /* type-check expression */
2968 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2969 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2973 loop
= new_loop(NULL
);
2975 /* save loop state */
2976 push_loop(A
, oper
, loop
);
2979 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2981 /* Check if loop condition is a constant */
2982 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2983 if (isConst
&& constTrue
) {
2984 /* do { } while(1) ==> no conditional break */
2985 loop
->Children
[1] = NULL
; /* no tail code */
2989 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2990 loop
->Children
[1] = new_break_if_true(A
, cond
);
2993 /* XXX we should do infinite loop detection, as above */
2995 /* restore loop state */
3003 * Recursively count the number of operations rooted at 'oper'.
3004 * This gives some kind of indication of the size/complexity of an operation.
3007 sizeof_operation(const slang_operation
*oper
)
3010 GLuint count
= 1; /* me */
3012 for (i
= 0; i
< oper
->num_children
; i
++) {
3013 count
+= sizeof_operation(&oper
->children
[i
]);
3024 * Determine if a for-loop can be unrolled.
3025 * At this time, only a rather narrow class of for loops can be unrolled.
3026 * See code for details.
3027 * When a loop can't be unrolled because it's too large we'll emit a
3028 * message to the log.
3031 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3035 const char *varName
;
3038 if (oper
->type
!= SLANG_OPER_FOR
)
3041 assert(oper
->num_children
== 4);
3043 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3046 /* children[0] must be either "int i=constant" or "i=constant" */
3047 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3048 slang_variable
*var
;
3050 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3053 varId
= oper
->children
[0].children
[0].a_id
;
3055 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3059 if (!var
->initializer
)
3061 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3063 start
= (GLint
) var
->initializer
->literal
[0];
3065 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3066 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3068 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3070 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3073 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3075 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3081 /* children[1] must be "i<constant" */
3082 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3084 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3086 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3088 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3091 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3093 /* children[2] must be "i++" or "++i" */
3094 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3095 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3097 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3100 /* make sure the same variable name is used in all places */
3101 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3102 (oper
->children
[2].children
[0].a_id
!= varId
))
3105 varName
= (const char *) varId
;
3107 /* children[3], the loop body, can't be too large */
3108 bodySize
= sizeof_operation(&oper
->children
[3]);
3109 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3110 slang_info_log_print(A
->log
,
3111 "Note: 'for (%s ... )' body is too large/complex"
3118 return GL_FALSE
; /* degenerate case */
3120 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3121 slang_info_log_print(A
->log
,
3122 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3123 " many iterations to unroll",
3124 varName
, start
, varName
, end
, varName
);
3128 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3129 slang_info_log_print(A
->log
,
3130 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3131 " too much code to unroll",
3132 varName
, start
, varName
, end
, varName
);
3136 return GL_TRUE
; /* we can unroll the loop */
3141 * Unroll a for-loop.
3142 * First we determine the number of iterations to unroll.
3143 * Then for each iteration:
3144 * make a copy of the loop body
3145 * replace instances of the loop variable with the current iteration value
3146 * generate IR code for the body
3147 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3149 static slang_ir_node
*
3150 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3152 GLint start
, end
, iter
;
3153 slang_ir_node
*n
, *root
= NULL
;
3156 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3157 /* for (int i=0; ... */
3158 slang_variable
*var
;
3160 varId
= oper
->children
[0].children
[0].a_id
;
3161 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3163 start
= (GLint
) var
->initializer
->literal
[0];
3167 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3168 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3171 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3173 for (iter
= start
; iter
< end
; iter
++) {
3174 slang_operation
*body
;
3176 /* make a copy of the loop body */
3177 body
= slang_operation_new(1);
3181 if (!slang_operation_copy(body
, &oper
->children
[3]))
3184 /* in body, replace instances of 'varId' with literal 'iter' */
3186 slang_variable
*oldVar
;
3187 slang_operation
*newOper
;
3189 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3191 /* undeclared loop variable */
3192 slang_operation_delete(body
);
3196 newOper
= slang_operation_new(1);
3197 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3198 newOper
->literal_size
= 1;
3199 newOper
->literal
[0] = iter
;
3201 /* replace instances of the loop variable with newOper */
3202 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3205 /* do IR codegen for body */
3206 n
= _slang_gen_operation(A
, body
);
3210 root
= new_seq(root
, n
);
3212 slang_operation_delete(body
);
3220 * Replace 'continue' statement with 'break' inside a for-loop.
3221 * This is a recursive helper function used by _slang_gen_for_without_continue().
3224 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3226 switch (oper
->type
) {
3227 case SLANG_OPER_CONTINUE
:
3228 oper
->type
= SLANG_OPER_BREAK
;
3230 case SLANG_OPER_FOR
:
3232 case SLANG_OPER_WHILE
:
3233 /* stop upon finding a nested loop */
3239 for (i
= 0; i
< oper
->num_children
; i
++) {
3240 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3248 * Transform a for-loop so that continue statements are converted to breaks.
3249 * Then do normal IR code generation.
3253 * for (INIT; LOOPCOND; INCR) {
3264 * bool _condFlag = 1;
3265 * for (INIT; _condFlag; ) {
3266 * for ( ; _condFlag = LOOPCOND; INCR) {
3278 static slang_ir_node
*
3279 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3281 slang_operation
*top
;
3282 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3283 slang_operation
*lhs
, *rhs
;
3285 assert(oper
->type
== SLANG_OPER_FOR
);
3287 top
= slang_operation_new(1);
3288 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3289 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3290 slang_operation_add_children(top
, 2);
3292 /* declare: bool _condFlag = true */
3294 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3295 slang_generate_declaration(A
, top
->locals
, condDecl
,
3296 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3299 /* build outer loop: for (INIT; _condFlag; ) { */
3300 outerFor
= slang_oper_child(top
, 1);
3301 outerFor
->type
= SLANG_OPER_FOR
;
3302 slang_operation_add_children(outerFor
, 4);
3304 init
= slang_oper_child(outerFor
, 0);
3305 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3307 cond
= slang_oper_child(outerFor
, 1);
3308 cond
->type
= SLANG_OPER_IDENTIFIER
;
3309 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3311 incr
= slang_oper_child(outerFor
, 2);
3312 incr
->type
= SLANG_OPER_VOID
;
3314 /* body of the outer loop */
3316 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3318 slang_operation_add_children(block
, 2);
3319 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3321 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3323 innerFor
= slang_oper_child(block
, 0);
3325 /* make copy of orig loop */
3326 slang_operation_copy(innerFor
, oper
);
3327 assert(innerFor
->type
== SLANG_OPER_FOR
);
3328 innerFor
->locals
->outer_scope
= block
->locals
;
3330 init
= slang_oper_child(innerFor
, 0);
3331 init
->type
= SLANG_OPER_VOID
; /* leak? */
3333 cond
= slang_oper_child(innerFor
, 1);
3334 slang_operation_destruct(cond
);
3335 cond
->type
= SLANG_OPER_ASSIGN
;
3336 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3337 slang_operation_add_children(cond
, 2);
3339 lhs
= slang_oper_child(cond
, 0);
3340 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3341 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3343 rhs
= slang_oper_child(cond
, 1);
3344 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3347 /* if (_condFlag) INCR; */
3349 slang_operation
*ifop
= slang_oper_child(block
, 1);
3350 ifop
->type
= SLANG_OPER_IF
;
3351 slang_operation_add_children(ifop
, 2);
3353 /* re-use cond node build above */
3354 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3356 /* incr node from original for-loop operation */
3357 slang_operation_copy(slang_oper_child(ifop
, 1),
3358 slang_oper_child(oper
, 2));
3361 /* finally, replace "continue" with "break" in the inner for-loop */
3362 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3365 return _slang_gen_operation(A
, top
);
3371 * Generate IR for a for-loop. Unrolling will be done when possible.
3373 static slang_ir_node
*
3374 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3378 if (!A
->EmitContReturn
) {
3379 /* We don't want to emit CONT instructions. If this for-loop has
3380 * a continue, translate it away.
3382 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3383 return _slang_gen_for_without_continue(A
, oper
);
3387 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3389 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3394 assert(oper
->type
== SLANG_OPER_FOR
);
3396 /* conventional for-loop code generation */
3399 * init code (child[0])
3401 * BREAK if !expr (child[1])
3402 * body code (child[3])
3404 * incr code (child[2]) // XXX continue here
3406 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3407 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3408 loop
= new_loop(NULL
);
3410 /* save loop state */
3411 push_loop(A
, oper
, loop
);
3413 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3414 breakIf
= new_break_if_true(A
, cond
);
3415 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3416 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3418 loop
->Children
[0] = new_seq(breakIf
, body
);
3419 loop
->Children
[1] = incr
; /* tail code */
3421 /* restore loop state */
3424 return new_seq(init
, loop
);
3429 static slang_ir_node
*
3430 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3432 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3434 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3435 loopNode
= current_loop_ir(A
);
3437 assert(loopNode
->Opcode
== IR_LOOP
);
3439 cont
= new_node0(IR_CONT
);
3441 cont
->Parent
= loopNode
;
3442 /* insert this node at head of linked list of cont/break instructions */
3443 cont
->List
= loopNode
->List
;
3444 loopNode
->List
= cont
;
3447 n
= new_seq(incr
, cont
);
3453 * Determine if the given operation is of a specific type.
3456 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3458 if (oper
->type
== type
)
3460 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3461 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3462 oper
->num_children
== 1)
3463 return is_operation_type(&oper
->children
[0], type
);
3470 * Generate IR tree for an if/then/else conditional using high-level
3471 * IR_IF instruction.
3473 static slang_ir_node
*
3474 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3477 * eval expr (child[0])
3484 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3485 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3486 GLboolean isConst
, constTrue
;
3488 /* type-check expression */
3489 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3490 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3494 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3495 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3499 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3503 return _slang_gen_operation(A
, &oper
->children
[1]);
3506 /* if (false) ... */
3507 return _slang_gen_operation(A
, &oper
->children
[2]);
3511 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3512 cond
= new_cond(cond
);
3514 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3515 && !haveElseClause
) {
3516 /* Special case: generate a conditional break */
3517 ifBody
= new_break_if_true(A
, cond
);
3520 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3522 && current_loop_oper(A
)
3523 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3524 /* Special case: generate a conditional continue */
3525 ifBody
= new_cont_if_true(A
, cond
);
3530 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3532 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3535 ifNode
= new_if(cond
, ifBody
, elseBody
);
3542 static slang_ir_node
*
3543 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3547 assert(oper
->type
== SLANG_OPER_NOT
);
3549 /* type-check expression */
3550 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3551 slang_info_log_error(A
->log
,
3552 "scalar/boolean expression expected for '!'");
3556 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3564 static slang_ir_node
*
3565 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3567 slang_ir_node
*n1
, *n2
;
3569 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3571 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3572 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3573 slang_info_log_error(A
->log
,
3574 "scalar/boolean expressions expected for '^^'");
3578 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3581 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3584 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3589 * Generate IR node for storage of a temporary of given size.
3591 static slang_ir_node
*
3592 _slang_gen_temporary(GLint size
)
3594 slang_ir_storage
*store
;
3595 slang_ir_node
*n
= NULL
;
3597 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3599 n
= new_node0(IR_VAR_DECL
);
3612 * Generate program constants for an array.
3613 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3614 * This will allocate and initialize three vector constants, storing
3615 * the array in constant memory, not temporaries like a non-const array.
3616 * This can also be used for uniform array initializers.
3617 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3620 make_constant_array(slang_assemble_ctx
*A
,
3621 slang_variable
*var
,
3622 slang_operation
*initializer
)
3624 struct gl_program
*prog
= A
->program
;
3625 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3626 const char *varName
= (char *) var
->a_name
;
3627 const GLuint numElements
= initializer
->num_children
;
3633 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3635 size
= var
->store
->Size
;
3637 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3638 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3639 assert(initializer
->type
== SLANG_OPER_CALL
);
3640 assert(initializer
->array_constructor
);
3642 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3644 /* convert constructor params into ordinary floats */
3645 for (i
= 0; i
< numElements
; i
++) {
3646 const slang_operation
*op
= &initializer
->children
[i
];
3647 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3648 /* unsupported type for this optimization */
3652 for (j
= 0; j
< op
->literal_size
; j
++) {
3653 values
[i
* 4 + j
] = op
->literal
[j
];
3655 for ( ; j
< 4; j
++) {
3656 values
[i
* 4 + j
] = 0.0f
;
3660 /* slightly different paths for constants vs. uniforms */
3661 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3662 var
->store
->File
= PROGRAM_UNIFORM
;
3663 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3664 size
, datatype
, values
);
3667 var
->store
->File
= PROGRAM_CONSTANT
;
3668 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3671 assert(var
->store
->Size
== size
);
3681 * Generate IR node for allocating/declaring a variable (either a local or
3683 * Generally, this involves allocating an slang_ir_storage instance for the
3684 * variable, choosing a register file (temporary, constant, etc).
3685 * For ordinary variables we do not yet allocate storage though. We do that
3686 * when we find the first actual use of the variable to avoid allocating temp
3687 * regs that will never get used.
3688 * At this time, uniforms are always allocated space in this function.
3690 * \param initializer Optional initializer expression for the variable.
3692 static slang_ir_node
*
3693 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3694 slang_operation
*initializer
)
3696 const char *varName
= (const char *) var
->a_name
;
3697 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3698 slang_ir_node
*varDecl
, *n
;
3699 slang_ir_storage
*store
;
3700 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3701 gl_register_file file
;
3703 /*assert(!var->declared);*/
3704 var
->declared
= GL_TRUE
;
3706 /* determine GPU register file for simple cases */
3707 if (is_sampler_type(&var
->type
)) {
3708 file
= PROGRAM_SAMPLER
;
3710 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3711 file
= PROGRAM_UNIFORM
;
3714 file
= PROGRAM_TEMPORARY
;
3717 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3719 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3723 arrayLen
= _slang_array_length(var
);
3724 totalSize
= _slang_array_size(size
, arrayLen
);
3726 /* Allocate IR node for the declaration */
3727 varDecl
= new_node0(IR_VAR_DECL
);
3731 /* Allocate slang_ir_storage for this variable if needed.
3732 * Note that we may not actually allocate a constant or temporary register
3736 GLint index
= -7; /* TBD / unknown */
3737 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3739 return NULL
; /* out of memory */
3742 /* set the IR node's Var and Store pointers */
3744 varDecl
->Store
= var
->store
;
3749 /* if there's an initializer, generate IR for the expression */
3751 slang_ir_node
*varRef
, *init
;
3753 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3754 /* if the variable is const, the initializer must be a const
3755 * expression as well.
3758 if (!_slang_is_constant_expr(initializer
)) {
3759 slang_info_log_error(A
->log
,
3760 "initializer for %s not constant", varName
);
3766 /* IR for the variable we're initializing */
3767 varRef
= new_var(A
, var
);
3769 slang_info_log_error(A
->log
, "out of memory");
3773 /* constant-folding, etc here */
3774 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3776 /* look for simple constant-valued variables and uniforms */
3777 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3778 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3780 if (initializer
->type
== SLANG_OPER_CALL
&&
3781 initializer
->array_constructor
) {
3782 /* array initializer */
3783 if (make_constant_array(A
, var
, initializer
))
3786 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3787 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3788 /* simple float/vector initializer */
3789 if (store
->File
== PROGRAM_UNIFORM
) {
3790 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3792 totalSize
, datatype
,
3793 initializer
->literal
);
3794 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3799 store
->File
= PROGRAM_CONSTANT
;
3800 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3802 initializer
->literal
,
3804 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3811 /* IR for initializer */
3812 init
= _slang_gen_operation(A
, initializer
);
3816 /* XXX remove this when type checking is added above */
3817 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3818 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3822 /* assign RHS to LHS */
3823 n
= new_node2(IR_COPY
, varRef
, init
);
3824 n
= new_seq(varDecl
, n
);
3827 /* no initializer */
3831 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3832 /* always need to allocate storage for uniforms at this point */
3833 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3834 totalSize
, datatype
, NULL
);
3835 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3839 printf("%s var %p %s store=%p index=%d size=%d\n",
3840 __FUNCTION__
, (void *) var
, (char *) varName
,
3841 (void *) store
, store
->Index
, store
->Size
);
3849 * Generate code for a selection expression: b ? x : y
3850 * XXX In some cases we could implement a selection expression
3851 * with an LRP instruction (use the boolean as the interpolant).
3852 * Otherwise, we use an IF/ELSE/ENDIF construct.
3854 static slang_ir_node
*
3855 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3857 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3858 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3859 slang_typeinfo type0
, type1
, type2
;
3860 int size
, isBool
, isEqual
;
3862 assert(oper
->type
== SLANG_OPER_SELECT
);
3863 assert(oper
->num_children
== 3);
3865 /* type of children[0] must be boolean */
3866 slang_typeinfo_construct(&type0
);
3867 typeof_operation(A
, &oper
->children
[0], &type0
);
3868 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3869 slang_typeinfo_destruct(&type0
);
3871 slang_info_log_error(A
->log
, "selector type is not boolean");
3875 slang_typeinfo_construct(&type1
);
3876 slang_typeinfo_construct(&type2
);
3877 typeof_operation(A
, &oper
->children
[1], &type1
);
3878 typeof_operation(A
, &oper
->children
[2], &type2
);
3879 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3880 slang_typeinfo_destruct(&type1
);
3881 slang_typeinfo_destruct(&type2
);
3883 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3887 /* size of x or y's type */
3888 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3892 tmpDecl
= _slang_gen_temporary(size
);
3894 /* the condition (child 0) */
3895 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3896 cond
= new_cond(cond
);
3898 /* if-true body (child 1) */
3899 tmpVar
= new_node0(IR_VAR
);
3900 tmpVar
->Store
= tmpDecl
->Store
;
3901 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3902 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3904 /* if-false body (child 2) */
3905 tmpVar
= new_node0(IR_VAR
);
3906 tmpVar
->Store
= tmpDecl
->Store
;
3907 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3908 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3910 ifNode
= new_if(cond
, trueNode
, falseNode
);
3913 tmpVar
= new_node0(IR_VAR
);
3914 tmpVar
->Store
= tmpDecl
->Store
;
3916 tree
= new_seq(ifNode
, tmpVar
);
3917 tree
= new_seq(tmpDecl
, tree
);
3919 /*_slang_print_ir_tree(tree, 10);*/
3925 * Generate code for &&.
3927 static slang_ir_node
*
3928 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3930 /* rewrite "a && b" as "a ? b : false" */
3931 slang_operation
*select
;
3934 select
= slang_operation_new(1);
3935 select
->type
= SLANG_OPER_SELECT
;
3936 slang_operation_add_children(select
, 3);
3938 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3939 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3940 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3942 n
= _slang_gen_select(A
, select
);
3948 * Generate code for ||.
3950 static slang_ir_node
*
3951 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3953 /* rewrite "a || b" as "a ? true : b" */
3954 slang_operation
*select
;
3957 select
= slang_operation_new(1);
3958 select
->type
= SLANG_OPER_SELECT
;
3959 slang_operation_add_children(select
, 3);
3961 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3962 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3963 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3965 n
= _slang_gen_select(A
, select
);
3971 * Generate IR tree for a return statement.
3973 static slang_ir_node
*
3974 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3976 assert(oper
->type
== SLANG_OPER_RETURN
);
3977 return new_return(A
->curFuncEndLabel
);
3983 * Determine if the given operation/expression is const-valued.
3986 _slang_is_constant_expr(const slang_operation
*oper
)
3988 slang_variable
*var
;
3991 switch (oper
->type
) {
3992 case SLANG_OPER_IDENTIFIER
:
3993 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3994 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3998 for (i
= 0; i
< oper
->num_children
; i
++) {
3999 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4009 * Check if an assignment of type t1 to t0 is legal.
4010 * XXX more cases needed.
4013 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4014 slang_operation
*op0
,
4015 slang_operation
*op1
)
4017 slang_typeinfo t0
, t1
;
4020 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4021 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4025 slang_typeinfo_construct(&t0
);
4026 typeof_operation(A
, op0
, &t0
);
4028 slang_typeinfo_construct(&t1
);
4029 typeof_operation(A
, op1
, &t1
);
4031 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4032 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4036 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4041 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4042 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4043 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4046 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4047 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4050 #if 0 /* not used just yet - causes problems elsewhere */
4051 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4052 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4056 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4057 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4060 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4061 t1
.spec
.type
== SLANG_SPEC_INT
)
4069 * Generate IR tree for a local variable declaration.
4070 * Basically do some error checking and call _slang_gen_var_decl().
4072 static slang_ir_node
*
4073 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4075 const char *varName
= (char *) oper
->a_id
;
4076 slang_variable
*var
;
4077 slang_ir_node
*varDecl
;
4078 slang_operation
*initializer
;
4080 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4081 assert(oper
->num_children
<= 1);
4084 /* lookup the variable by name */
4085 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4087 return NULL
; /* "shouldn't happen" */
4089 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4090 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4091 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4092 /* can't declare attribute/uniform vars inside functions */
4093 slang_info_log_error(A
->log
,
4094 "local variable '%s' cannot be an attribute/uniform/varying",
4101 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4106 /* check if the var has an initializer */
4107 if (oper
->num_children
> 0) {
4108 assert(oper
->num_children
== 1);
4109 initializer
= &oper
->children
[0];
4111 else if (var
->initializer
) {
4112 initializer
= var
->initializer
;
4119 /* check/compare var type and initializer type */
4120 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4121 slang_info_log_error(A
->log
, "incompatible types in assignment");
4126 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4127 slang_info_log_error(A
->log
,
4128 "const-qualified variable '%s' requires initializer",
4134 /* Generate IR node */
4135 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4144 * Generate IR tree for a reference to a variable (such as in an expression).
4145 * This is different from a variable declaration.
4147 static slang_ir_node
*
4148 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4150 /* If there's a variable associated with this oper (from inlining)
4151 * use it. Otherwise, use the oper's var id.
4153 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4154 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4157 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4160 assert(var
->declared
);
4161 n
= new_var(A
, var
);
4168 * Return the number of components actually named by the swizzle.
4169 * Recall that swizzles may have undefined/don't-care values.
4172 swizzle_size(GLuint swizzle
)
4175 for (i
= 0; i
< 4; i
++) {
4176 GLuint swz
= GET_SWZ(swizzle
, i
);
4177 size
+= (swz
>= 0 && swz
<= 3);
4183 static slang_ir_node
*
4184 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4186 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4190 n
->Store
= _slang_new_ir_storage_relative(0,
4191 swizzle_size(swizzle
),
4193 n
->Store
->Swizzle
= swizzle
;
4200 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4202 while (store
->Parent
)
4203 store
= store
->Parent
;
4205 if (!(store
->File
== PROGRAM_OUTPUT
||
4206 store
->File
== PROGRAM_TEMPORARY
||
4207 (store
->File
== PROGRAM_VARYING
&&
4208 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4218 * Walk up an IR storage path to compute the final swizzle.
4219 * This is used when we find an expression such as "foo.xz.yx".
4222 root_swizzle(const slang_ir_storage
*st
)
4224 GLuint swizzle
= st
->Swizzle
;
4225 while (st
->Parent
) {
4227 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4234 * Generate IR tree for an assignment (=).
4236 static slang_ir_node
*
4237 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4239 slang_operation
*pred
= NULL
;
4240 slang_ir_node
*n
= NULL
;
4242 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4243 /* Check that var is writeable */
4245 = _slang_variable_locate(oper
->children
[0].locals
,
4246 oper
->children
[0].a_id
, GL_TRUE
);
4248 slang_info_log_error(A
->log
, "undefined variable '%s'",
4249 (char *) oper
->children
[0].a_id
);
4252 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4253 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4254 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4255 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4256 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4257 slang_info_log_error(A
->log
,
4258 "illegal assignment to read-only variable '%s'",
4259 (char *) oper
->children
[0].a_id
);
4263 /* check if we need to predicate this assignment based on __notRetFlag */
4264 if ((var
->is_global
||
4265 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4266 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4267 /* create predicate, used below */
4268 pred
= slang_operation_new(1);
4269 pred
->type
= SLANG_OPER_IDENTIFIER
;
4270 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4271 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4275 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4276 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4277 /* Special case of: x = f(a, b)
4278 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4280 * XXX this could be even more effective if we could accomodate
4281 * cases such as "v.x = f();" - would help with typical vertex
4284 n
= _slang_gen_function_call_name(A
,
4285 (const char *) oper
->children
[1].a_id
,
4286 &oper
->children
[1], &oper
->children
[0]);
4289 slang_ir_node
*lhs
, *rhs
;
4291 /* lhs and rhs type checking */
4292 if (!_slang_assignment_compatible(A
,
4294 &oper
->children
[1])) {
4295 slang_info_log_error(A
->log
, "incompatible types in assignment");
4299 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4305 slang_info_log_error(A
->log
,
4306 "invalid left hand side for assignment");
4310 /* check that lhs is writable */
4311 if (!is_store_writable(A
, lhs
->Store
)) {
4312 slang_info_log_error(A
->log
,
4313 "illegal assignment to read-only l-value");
4317 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4319 /* convert lhs swizzle into writemask */
4320 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4321 GLuint writemask
, newSwizzle
= 0x0;
4322 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4323 /* Non-simple writemask, need to swizzle right hand side in
4324 * order to put components into the right place.
4326 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4328 n
= new_node2(IR_COPY
, lhs
, rhs
);
4336 /* predicate the assignment code on __notRetFlag */
4337 slang_ir_node
*top
, *cond
;
4339 cond
= _slang_gen_operation(A
, pred
);
4340 top
= new_if(cond
, n
, NULL
);
4348 * Generate IR tree for referencing a field in a struct (or basic vector type)
4350 static slang_ir_node
*
4351 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4355 /* type of struct */
4356 slang_typeinfo_construct(&ti
);
4357 typeof_operation(A
, &oper
->children
[0], &ti
);
4359 if (_slang_type_is_vector(ti
.spec
.type
)) {
4360 /* the field should be a swizzle */
4361 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4365 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4366 slang_info_log_error(A
->log
, "Bad swizzle");
4369 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4374 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4375 /* create new parent node with swizzle */
4377 n
= _slang_gen_swizzle(n
, swizzle
);
4380 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4381 || ti
.spec
.type
== SLANG_SPEC_INT
4382 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4383 const GLuint rows
= 1;
4387 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4388 slang_info_log_error(A
->log
, "Bad swizzle");
4390 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4394 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4395 /* create new parent node with swizzle */
4396 n
= _slang_gen_swizzle(n
, swizzle
);
4400 /* the field is a structure member (base.field) */
4401 /* oper->children[0] is the base */
4402 /* oper->a_id is the field name */
4403 slang_ir_node
*base
, *n
;
4404 slang_typeinfo field_ti
;
4405 GLint fieldSize
, fieldOffset
= -1;
4408 slang_typeinfo_construct(&field_ti
);
4409 typeof_operation(A
, oper
, &field_ti
);
4411 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4413 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4415 if (fieldSize
== 0 || fieldOffset
< 0) {
4416 const char *structName
;
4417 if (ti
.spec
._struct
)
4418 structName
= (char *) ti
.spec
._struct
->a_name
;
4420 structName
= "unknown";
4421 slang_info_log_error(A
->log
,
4422 "\"%s\" is not a member of struct \"%s\"",
4423 (char *) oper
->a_id
, structName
);
4426 assert(fieldSize
>= 0);
4428 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4430 /* error msg should have already been logged */
4434 n
= new_node1(IR_FIELD
, base
);
4438 n
->Field
= (char *) oper
->a_id
;
4440 /* Store the field's offset in storage->Index */
4441 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4451 * Gen code for array indexing.
4453 static slang_ir_node
*
4454 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4456 slang_typeinfo array_ti
;
4458 /* get array's type info */
4459 slang_typeinfo_construct(&array_ti
);
4460 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4462 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4463 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4464 /* translate the index into a swizzle/writemask: "v.x=p" */
4465 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4469 index
= (GLint
) oper
->children
[1].literal
[0];
4470 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4471 index
>= (GLint
) max
) {
4473 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4474 printf("type = %d\n", oper
->children
[1].type
);
4475 printf("index = %d, max = %d\n", index
, max
);
4476 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4477 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4484 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4486 /* use swizzle to access the element */
4487 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4491 n
= _slang_gen_swizzle(n
, swizzle
);
4497 /* conventional array */
4498 slang_typeinfo elem_ti
;
4499 slang_ir_node
*elem
, *array
, *index
;
4500 GLint elemSize
, arrayLen
;
4502 /* size of array element */
4503 slang_typeinfo_construct(&elem_ti
);
4504 typeof_operation(A
, oper
, &elem_ti
);
4505 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4507 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4508 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4510 arrayLen
= array_ti
.array_len
;
4512 slang_typeinfo_destruct(&array_ti
);
4513 slang_typeinfo_destruct(&elem_ti
);
4515 if (elemSize
<= 0) {
4516 /* unknown var or type */
4517 slang_info_log_error(A
->log
, "Undefined variable or type");
4521 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4522 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4523 if (array
&& index
) {
4525 GLint constIndex
= -1;
4526 if (index
->Opcode
== IR_FLOAT
) {
4527 constIndex
= (int) index
->Value
[0];
4528 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4529 slang_info_log_error(A
->log
,
4530 "Array index out of bounds (index=%d size=%d)",
4531 constIndex
, arrayLen
);
4532 _slang_free_ir_tree(array
);
4533 _slang_free_ir_tree(index
);
4538 if (!array
->Store
) {
4539 slang_info_log_error(A
->log
, "Invalid array");
4543 elem
= new_node2(IR_ELEMENT
, array
, index
);
4545 /* The storage info here will be updated during code emit */
4546 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4547 array
->Store
->Index
,
4549 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4553 _slang_free_ir_tree(array
);
4554 _slang_free_ir_tree(index
);
4561 static slang_ir_node
*
4562 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4563 slang_ir_opcode opcode
)
4565 slang_typeinfo t0
, t1
;
4568 slang_typeinfo_construct(&t0
);
4569 typeof_operation(A
, &oper
->children
[0], &t0
);
4571 slang_typeinfo_construct(&t1
);
4572 typeof_operation(A
, &oper
->children
[0], &t1
);
4574 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4575 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4576 slang_info_log_error(A
->log
, "Illegal array comparison");
4580 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4581 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4582 /* <, <=, >, >= can only be used with scalars */
4583 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4584 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4585 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4586 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4587 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4592 n
= new_node2(opcode
,
4593 _slang_gen_operation(A
, &oper
->children
[0]),
4594 _slang_gen_operation(A
, &oper
->children
[1]));
4596 /* result is a bool (size 1) */
4597 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4605 print_vars(slang_variable_scope
*s
)
4609 for (i
= 0; i
< s
->num_variables
; i
++) {
4611 (char*) s
->variables
[i
]->a_name
,
4612 s
->variables
[i
]->declared
);
4622 _slang_undeclare_vars(slang_variable_scope
*locals
)
4624 if (locals
->num_variables
> 0) {
4626 for (i
= 0; i
< locals
->num_variables
; i
++) {
4627 slang_variable
*v
= locals
->variables
[i
];
4628 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4629 v
->declared
= GL_FALSE
;
4637 * Generate IR tree for a slang_operation (AST node)
4639 static slang_ir_node
*
4640 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4642 switch (oper
->type
) {
4643 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4647 _slang_push_var_table(A
->vartable
);
4649 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4650 n
= _slang_gen_operation(A
, oper
);
4651 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4653 _slang_pop_var_table(A
->vartable
);
4655 /*_slang_undeclare_vars(oper->locals);*/
4656 /*print_vars(oper->locals);*/
4659 n
= new_node1(IR_SCOPE
, n
);
4664 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4665 /* list of operations */
4666 if (oper
->num_children
> 0)
4668 slang_ir_node
*n
, *tree
= NULL
;
4671 for (i
= 0; i
< oper
->num_children
; i
++) {
4672 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4674 _slang_free_ir_tree(tree
);
4675 return NULL
; /* error must have occured */
4677 tree
= new_seq(tree
, n
);
4683 return new_node0(IR_NOP
);
4686 case SLANG_OPER_EXPRESSION
:
4687 return _slang_gen_operation(A
, &oper
->children
[0]);
4689 case SLANG_OPER_FOR
:
4690 return _slang_gen_for(A
, oper
);
4692 return _slang_gen_do(A
, oper
);
4693 case SLANG_OPER_WHILE
:
4694 return _slang_gen_while(A
, oper
);
4695 case SLANG_OPER_BREAK
:
4696 if (!current_loop_oper(A
)) {
4697 slang_info_log_error(A
->log
, "'break' not in loop");
4700 return new_break(current_loop_ir(A
));
4701 case SLANG_OPER_CONTINUE
:
4702 if (!current_loop_oper(A
)) {
4703 slang_info_log_error(A
->log
, "'continue' not in loop");
4706 return _slang_gen_continue(A
, oper
);
4707 case SLANG_OPER_DISCARD
:
4708 return new_node0(IR_KILL
);
4710 case SLANG_OPER_EQUAL
:
4711 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4712 case SLANG_OPER_NOTEQUAL
:
4713 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4714 case SLANG_OPER_GREATER
:
4715 return _slang_gen_compare(A
, oper
, IR_SGT
);
4716 case SLANG_OPER_LESS
:
4717 return _slang_gen_compare(A
, oper
, IR_SLT
);
4718 case SLANG_OPER_GREATEREQUAL
:
4719 return _slang_gen_compare(A
, oper
, IR_SGE
);
4720 case SLANG_OPER_LESSEQUAL
:
4721 return _slang_gen_compare(A
, oper
, IR_SLE
);
4722 case SLANG_OPER_ADD
:
4725 assert(oper
->num_children
== 2);
4726 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4729 case SLANG_OPER_SUBTRACT
:
4732 assert(oper
->num_children
== 2);
4733 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4736 case SLANG_OPER_MULTIPLY
:
4739 assert(oper
->num_children
== 2);
4740 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4743 case SLANG_OPER_DIVIDE
:
4746 assert(oper
->num_children
== 2);
4747 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4750 case SLANG_OPER_MINUS
:
4753 assert(oper
->num_children
== 1);
4754 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4757 case SLANG_OPER_PLUS
:
4758 /* +expr --> do nothing */
4759 return _slang_gen_operation(A
, &oper
->children
[0]);
4760 case SLANG_OPER_VARIABLE_DECL
:
4761 return _slang_gen_declaration(A
, oper
);
4762 case SLANG_OPER_ASSIGN
:
4763 return _slang_gen_assignment(A
, oper
);
4764 case SLANG_OPER_ADDASSIGN
:
4767 assert(oper
->num_children
== 2);
4768 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4771 case SLANG_OPER_SUBASSIGN
:
4774 assert(oper
->num_children
== 2);
4775 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4779 case SLANG_OPER_MULASSIGN
:
4782 assert(oper
->num_children
== 2);
4783 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4786 case SLANG_OPER_DIVASSIGN
:
4789 assert(oper
->num_children
== 2);
4790 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4793 case SLANG_OPER_LOGICALAND
:
4796 assert(oper
->num_children
== 2);
4797 n
= _slang_gen_logical_and(A
, oper
);
4800 case SLANG_OPER_LOGICALOR
:
4803 assert(oper
->num_children
== 2);
4804 n
= _slang_gen_logical_or(A
, oper
);
4807 case SLANG_OPER_LOGICALXOR
:
4808 return _slang_gen_xor(A
, oper
);
4809 case SLANG_OPER_NOT
:
4810 return _slang_gen_not(A
, oper
);
4811 case SLANG_OPER_SELECT
: /* b ? x : y */
4814 assert(oper
->num_children
== 3);
4815 n
= _slang_gen_select(A
, oper
);
4819 case SLANG_OPER_ASM
:
4820 return _slang_gen_asm(A
, oper
, NULL
);
4821 case SLANG_OPER_CALL
:
4822 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4824 case SLANG_OPER_METHOD
:
4825 return _slang_gen_method_call(A
, oper
);
4826 case SLANG_OPER_RETURN
:
4827 return _slang_gen_return(A
, oper
);
4828 case SLANG_OPER_RETURN_INLINED
:
4829 return _slang_gen_return(A
, oper
);
4830 case SLANG_OPER_LABEL
:
4831 return new_label(oper
->label
);
4832 case SLANG_OPER_IDENTIFIER
:
4833 return _slang_gen_variable(A
, oper
);
4835 return _slang_gen_if(A
, oper
);
4836 case SLANG_OPER_FIELD
:
4837 return _slang_gen_struct_field(A
, oper
);
4838 case SLANG_OPER_SUBSCRIPT
:
4839 return _slang_gen_array_element(A
, oper
);
4840 case SLANG_OPER_LITERAL_FLOAT
:
4842 case SLANG_OPER_LITERAL_INT
:
4844 case SLANG_OPER_LITERAL_BOOL
:
4845 return new_float_literal(oper
->literal
, oper
->literal_size
);
4847 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4850 assert(oper
->num_children
== 1);
4851 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4854 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4857 assert(oper
->num_children
== 1);
4858 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4861 case SLANG_OPER_PREINCREMENT
: /* ++var */
4864 assert(oper
->num_children
== 1);
4865 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4868 case SLANG_OPER_PREDECREMENT
: /* --var */
4871 assert(oper
->num_children
== 1);
4872 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4876 case SLANG_OPER_NON_INLINED_CALL
:
4877 case SLANG_OPER_SEQUENCE
:
4879 slang_ir_node
*tree
= NULL
;
4881 for (i
= 0; i
< oper
->num_children
; i
++) {
4882 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4883 tree
= new_seq(tree
, n
);
4885 tree
->Store
= n
->Store
;
4887 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4888 tree
= new_function_call(tree
, oper
->label
);
4893 case SLANG_OPER_NONE
:
4894 case SLANG_OPER_VOID
:
4895 /* returning NULL here would generate an error */
4896 return new_node0(IR_NOP
);
4899 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4901 return new_node0(IR_NOP
);
4909 * Check if the given type specifier is a rectangular texture sampler.
4912 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4914 while (spec
->_array
) {
4915 spec
= spec
->_array
;
4917 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4918 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4924 * Called by compiler when a global variable has been parsed/compiled.
4925 * Here we examine the variable's type to determine what kind of register
4926 * storage will be used.
4928 * A uniform such as "gl_Position" will become the register specification
4929 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4930 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4932 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4933 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4934 * actual texture unit (as specified by the user calling glUniform1i()).
4937 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4938 slang_unit_type type
)
4940 struct gl_program
*prog
= A
->program
;
4941 const char *varName
= (char *) var
->a_name
;
4942 GLboolean success
= GL_TRUE
;
4943 slang_ir_storage
*store
= NULL
;
4945 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4946 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4947 const GLint arrayLen
= _slang_array_length(var
);
4948 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4949 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4951 var
->is_global
= GL_TRUE
;
4953 /* check for sampler2D arrays */
4954 if (texIndex
== -1 && var
->type
.specifier
._array
)
4955 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4957 if (texIndex
!= -1) {
4958 /* This is a texture sampler variable...
4959 * store->File = PROGRAM_SAMPLER
4960 * store->Index = sampler number (0..7, typically)
4961 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4963 if (var
->initializer
) {
4964 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4967 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4968 /* disallow rect samplers */
4969 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4970 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4974 (void) is_rect_sampler_spec
; /* silence warning */
4977 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4978 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4980 /* If we have a sampler array, then we need to allocate the
4981 * additional samplers to ensure we don't allocate them elsewhere.
4982 * We can't directly use _mesa_add_sampler() as that checks the
4983 * varName and gets a match, so we call _mesa_add_parameter()
4984 * directly and use the last sampler number from the call above.
4987 GLint a
= arrayLen
- 1;
4989 for (i
= 0; i
< a
; i
++) {
4990 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4991 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4992 varName
, 1, datatype
, &value
, NULL
, 0x0);
4996 if (dbg
) printf("SAMPLER ");
4998 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4999 /* Uniform variable */
5000 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5003 /* user-defined uniform */
5004 if (datatype
== GL_NONE
) {
5005 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5006 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5007 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5008 /* temporary work-around */
5009 GLenum datatype
= GL_FLOAT
;
5010 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5011 totalSize
, datatype
, NULL
);
5012 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5013 totalSize
, swizzle
);
5016 GLint a
= arrayLen
- 1;
5018 for (i
= 0; i
< a
; i
++) {
5019 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5020 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5021 varName
, 1, datatype
, &value
, NULL
, 0x0);
5025 /* XXX what we need to do is unroll the struct into its
5026 * basic types, creating a uniform variable for each.
5034 * Should produce uniforms:
5035 * "f.a" (GL_FLOAT_VEC3)
5036 * "f.b" (GL_FLOAT_VEC4)
5039 if (var
->initializer
) {
5040 slang_info_log_error(A
->log
,
5041 "unsupported initializer for uniform '%s'", varName
);
5046 slang_info_log_error(A
->log
,
5047 "invalid datatype for uniform variable %s",
5053 /* non-struct uniform */
5054 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5060 /* pre-defined uniform, like gl_ModelviewMatrix */
5061 /* We know it's a uniform, but don't allocate storage unless
5064 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5065 totalSize
, swizzle
);
5067 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5069 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5070 /* varyings must be float, vec or mat */
5071 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5072 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5073 slang_info_log_error(A
->log
,
5074 "varying '%s' must be float/vector/matrix",
5079 if (var
->initializer
) {
5080 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5086 /* user-defined varying */
5092 if (var
->type
.centroid
== SLANG_CENTROID
)
5093 flags
|= PROG_PARAM_BIT_CENTROID
;
5094 if (var
->type
.variant
== SLANG_INVARIANT
)
5095 flags
|= PROG_PARAM_BIT_INVARIANT
;
5097 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5099 swizzle
= _slang_var_swizzle(size
, 0);
5100 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5101 totalSize
, swizzle
);
5104 /* pre-defined varying, like gl_Color or gl_TexCoord */
5105 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5106 /* fragment program input */
5108 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5111 assert(index
< FRAG_ATTRIB_MAX
);
5112 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5116 /* vertex program output */
5117 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5118 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5120 assert(index
< VERT_RESULT_MAX
);
5121 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5122 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5125 if (dbg
) printf("V/F ");
5127 if (dbg
) printf("VARYING ");
5129 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5132 /* attributes must be float, vec or mat */
5133 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5134 slang_info_log_error(A
->log
,
5135 "attribute '%s' must be float/vector/matrix",
5141 /* user-defined vertex attribute */
5142 const GLint attr
= -1; /* unknown */
5143 swizzle
= _slang_var_swizzle(size
, 0);
5144 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5145 size
, datatype
, attr
);
5147 index
= VERT_ATTRIB_GENERIC0
+ index
;
5150 /* pre-defined vertex attrib */
5151 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5154 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5155 if (dbg
) printf("ATTRIB ");
5157 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5158 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5159 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5161 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5162 if (dbg
) printf("INPUT ");
5164 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5165 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5166 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5167 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5170 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5171 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5172 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5173 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5175 if (dbg
) printf("OUTPUT ");
5177 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5178 /* pre-defined global constant, like gl_MaxLights */
5179 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5180 if (dbg
) printf("CONST ");
5183 /* ordinary variable (may be const) */
5186 /* IR node to declare the variable */
5187 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5189 /* emit GPU instructions */
5190 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5192 _slang_free_ir_tree(n
);
5195 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5196 store
? store
->Index
: -2);
5199 var
->store
= store
; /* save var's storage info */
5201 var
->declared
= GL_TRUE
;
5208 * Produce an IR tree from a function AST (fun->body).
5209 * Then call the code emitter to convert the IR tree into gl_program
5213 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5216 GLboolean success
= GL_TRUE
;
5218 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5219 /* we only really generate code for main, all other functions get
5220 * inlined or codegen'd upon an actual call.
5223 /* do some basic error checking though */
5224 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5225 /* check that non-void functions actually return something */
5227 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5229 slang_info_log_error(A
->log
,
5230 "function \"%s\" has no return statement",
5231 (char *) fun
->header
.a_name
);
5233 "function \"%s\" has no return statement\n",
5234 (char *) fun
->header
.a_name
);
5239 return GL_TRUE
; /* not an error */
5243 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5244 slang_print_function(fun
, 1);
5247 /* should have been allocated earlier: */
5248 assert(A
->program
->Parameters
);
5249 assert(A
->program
->Varying
);
5250 assert(A
->vartable
);
5253 A
->UseReturnFlag
= GL_FALSE
;
5254 A
->CurFunction
= fun
;
5256 /* fold constant expressions, etc. */
5257 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5260 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5261 slang_print_function(fun
, 1);
5264 /* Create an end-of-function label */
5265 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5267 /* push new vartable scope */
5268 _slang_push_var_table(A
->vartable
);
5270 /* Generate IR tree for the function body code */
5271 n
= _slang_gen_operation(A
, fun
->body
);
5273 n
= new_node1(IR_SCOPE
, n
);
5275 /* pop vartable, restore previous */
5276 _slang_pop_var_table(A
->vartable
);
5279 /* XXX record error */
5283 /* append an end-of-function-label to IR tree */
5284 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5286 /*_slang_label_delete(A->curFuncEndLabel);*/
5287 A
->curFuncEndLabel
= NULL
;
5290 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5291 slang_print_function(fun
, 1);
5294 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5295 _slang_print_ir_tree(n
, 0);
5298 printf("************* End codegen function ************\n\n");
5301 if (A
->UnresolvedRefs
) {
5302 /* Can't codegen at this time.
5303 * At link time we'll concatenate all the vertex shaders and/or all
5304 * the fragment shaders and try recompiling.
5309 /* Emit program instructions */
5310 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5311 _slang_free_ir_tree(n
);
5313 /* free codegen context */
5315 _mesa_free(A->codegen);