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 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
&&
3767 !A
->allow_uniform_initializers
) {
3768 slang_info_log_error(A
->log
,
3769 "initializer for uniform %s not allowed",
3774 /* IR for the variable we're initializing */
3775 varRef
= new_var(A
, var
);
3777 slang_info_log_error(A
->log
, "out of memory");
3781 /* constant-folding, etc here */
3782 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3784 /* look for simple constant-valued variables and uniforms */
3785 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3786 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3788 if (initializer
->type
== SLANG_OPER_CALL
&&
3789 initializer
->array_constructor
) {
3790 /* array initializer */
3791 if (make_constant_array(A
, var
, initializer
))
3794 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3795 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3796 /* simple float/vector initializer */
3797 if (store
->File
== PROGRAM_UNIFORM
) {
3798 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3800 totalSize
, datatype
,
3801 initializer
->literal
);
3802 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3807 store
->File
= PROGRAM_CONSTANT
;
3808 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3810 initializer
->literal
,
3812 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3819 /* IR for initializer */
3820 init
= _slang_gen_operation(A
, initializer
);
3824 /* XXX remove this when type checking is added above */
3825 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3826 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3830 /* assign RHS to LHS */
3831 n
= new_node2(IR_COPY
, varRef
, init
);
3832 n
= new_seq(varDecl
, n
);
3835 /* no initializer */
3839 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3840 /* always need to allocate storage for uniforms at this point */
3841 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3842 totalSize
, datatype
, NULL
);
3843 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3847 printf("%s var %p %s store=%p index=%d size=%d\n",
3848 __FUNCTION__
, (void *) var
, (char *) varName
,
3849 (void *) store
, store
->Index
, store
->Size
);
3857 * Generate code for a selection expression: b ? x : y
3858 * XXX In some cases we could implement a selection expression
3859 * with an LRP instruction (use the boolean as the interpolant).
3860 * Otherwise, we use an IF/ELSE/ENDIF construct.
3862 static slang_ir_node
*
3863 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3865 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3866 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3867 slang_typeinfo type0
, type1
, type2
;
3868 int size
, isBool
, isEqual
;
3870 assert(oper
->type
== SLANG_OPER_SELECT
);
3871 assert(oper
->num_children
== 3);
3873 /* type of children[0] must be boolean */
3874 slang_typeinfo_construct(&type0
);
3875 typeof_operation(A
, &oper
->children
[0], &type0
);
3876 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3877 slang_typeinfo_destruct(&type0
);
3879 slang_info_log_error(A
->log
, "selector type is not boolean");
3883 slang_typeinfo_construct(&type1
);
3884 slang_typeinfo_construct(&type2
);
3885 typeof_operation(A
, &oper
->children
[1], &type1
);
3886 typeof_operation(A
, &oper
->children
[2], &type2
);
3887 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3888 slang_typeinfo_destruct(&type1
);
3889 slang_typeinfo_destruct(&type2
);
3891 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3895 /* size of x or y's type */
3896 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3900 tmpDecl
= _slang_gen_temporary(size
);
3902 /* the condition (child 0) */
3903 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3904 cond
= new_cond(cond
);
3906 /* if-true body (child 1) */
3907 tmpVar
= new_node0(IR_VAR
);
3908 tmpVar
->Store
= tmpDecl
->Store
;
3909 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3910 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3912 /* if-false body (child 2) */
3913 tmpVar
= new_node0(IR_VAR
);
3914 tmpVar
->Store
= tmpDecl
->Store
;
3915 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3916 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3918 ifNode
= new_if(cond
, trueNode
, falseNode
);
3921 tmpVar
= new_node0(IR_VAR
);
3922 tmpVar
->Store
= tmpDecl
->Store
;
3924 tree
= new_seq(ifNode
, tmpVar
);
3925 tree
= new_seq(tmpDecl
, tree
);
3927 /*_slang_print_ir_tree(tree, 10);*/
3933 * Generate code for &&.
3935 static slang_ir_node
*
3936 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3938 /* rewrite "a && b" as "a ? b : false" */
3939 slang_operation
*select
;
3942 select
= slang_operation_new(1);
3943 select
->type
= SLANG_OPER_SELECT
;
3944 slang_operation_add_children(select
, 3);
3946 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3947 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3948 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3950 n
= _slang_gen_select(A
, select
);
3956 * Generate code for ||.
3958 static slang_ir_node
*
3959 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3961 /* rewrite "a || b" as "a ? true : b" */
3962 slang_operation
*select
;
3965 select
= slang_operation_new(1);
3966 select
->type
= SLANG_OPER_SELECT
;
3967 slang_operation_add_children(select
, 3);
3969 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3970 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3971 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3973 n
= _slang_gen_select(A
, select
);
3979 * Generate IR tree for a return statement.
3981 static slang_ir_node
*
3982 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3984 assert(oper
->type
== SLANG_OPER_RETURN
);
3985 return new_return(A
->curFuncEndLabel
);
3991 * Determine if the given operation/expression is const-valued.
3994 _slang_is_constant_expr(const slang_operation
*oper
)
3996 slang_variable
*var
;
3999 switch (oper
->type
) {
4000 case SLANG_OPER_IDENTIFIER
:
4001 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4002 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4006 for (i
= 0; i
< oper
->num_children
; i
++) {
4007 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4017 * Check if an assignment of type t1 to t0 is legal.
4018 * XXX more cases needed.
4021 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4022 slang_operation
*op0
,
4023 slang_operation
*op1
)
4025 slang_typeinfo t0
, t1
;
4028 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4029 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4033 slang_typeinfo_construct(&t0
);
4034 typeof_operation(A
, op0
, &t0
);
4036 slang_typeinfo_construct(&t1
);
4037 typeof_operation(A
, op1
, &t1
);
4039 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4040 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4044 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4049 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4050 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4051 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4054 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4055 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4058 #if 0 /* not used just yet - causes problems elsewhere */
4059 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4060 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4064 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4065 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4068 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4069 t1
.spec
.type
== SLANG_SPEC_INT
)
4077 * Generate IR tree for a local variable declaration.
4078 * Basically do some error checking and call _slang_gen_var_decl().
4080 static slang_ir_node
*
4081 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4083 const char *varName
= (char *) oper
->a_id
;
4084 slang_variable
*var
;
4085 slang_ir_node
*varDecl
;
4086 slang_operation
*initializer
;
4088 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4089 assert(oper
->num_children
<= 1);
4092 /* lookup the variable by name */
4093 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4095 return NULL
; /* "shouldn't happen" */
4097 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4098 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4099 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4100 /* can't declare attribute/uniform vars inside functions */
4101 slang_info_log_error(A
->log
,
4102 "local variable '%s' cannot be an attribute/uniform/varying",
4109 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4114 /* check if the var has an initializer */
4115 if (oper
->num_children
> 0) {
4116 assert(oper
->num_children
== 1);
4117 initializer
= &oper
->children
[0];
4119 else if (var
->initializer
) {
4120 initializer
= var
->initializer
;
4127 /* check/compare var type and initializer type */
4128 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4129 slang_info_log_error(A
->log
, "incompatible types in assignment");
4134 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4135 slang_info_log_error(A
->log
,
4136 "const-qualified variable '%s' requires initializer",
4142 /* Generate IR node */
4143 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4152 * Generate IR tree for a reference to a variable (such as in an expression).
4153 * This is different from a variable declaration.
4155 static slang_ir_node
*
4156 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4158 /* If there's a variable associated with this oper (from inlining)
4159 * use it. Otherwise, use the oper's var id.
4161 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4162 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4165 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4168 assert(var
->declared
);
4169 n
= new_var(A
, var
);
4176 * Return the number of components actually named by the swizzle.
4177 * Recall that swizzles may have undefined/don't-care values.
4180 swizzle_size(GLuint swizzle
)
4183 for (i
= 0; i
< 4; i
++) {
4184 GLuint swz
= GET_SWZ(swizzle
, i
);
4185 size
+= (swz
>= 0 && swz
<= 3);
4191 static slang_ir_node
*
4192 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4194 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4198 n
->Store
= _slang_new_ir_storage_relative(0,
4199 swizzle_size(swizzle
),
4201 n
->Store
->Swizzle
= swizzle
;
4208 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4210 while (store
->Parent
)
4211 store
= store
->Parent
;
4213 if (!(store
->File
== PROGRAM_OUTPUT
||
4214 store
->File
== PROGRAM_TEMPORARY
||
4215 (store
->File
== PROGRAM_VARYING
&&
4216 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4226 * Walk up an IR storage path to compute the final swizzle.
4227 * This is used when we find an expression such as "foo.xz.yx".
4230 root_swizzle(const slang_ir_storage
*st
)
4232 GLuint swizzle
= st
->Swizzle
;
4233 while (st
->Parent
) {
4235 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4242 * Generate IR tree for an assignment (=).
4244 static slang_ir_node
*
4245 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4247 slang_operation
*pred
= NULL
;
4248 slang_ir_node
*n
= NULL
;
4250 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4251 /* Check that var is writeable */
4252 const char *varName
= (char *) oper
->children
[0].a_id
;
4254 = _slang_variable_locate(oper
->children
[0].locals
,
4255 oper
->children
[0].a_id
, GL_TRUE
);
4257 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
4261 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4262 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4263 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4264 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4265 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4266 slang_info_log_error(A
->log
,
4267 "illegal assignment to read-only variable '%s'",
4272 /* check if we need to predicate this assignment based on __notRetFlag */
4273 if ((var
->is_global
||
4274 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4275 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4276 /* create predicate, used below */
4277 pred
= slang_operation_new(1);
4278 pred
->type
= SLANG_OPER_IDENTIFIER
;
4279 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4280 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4284 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4285 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4286 /* Special case of: x = f(a, b)
4287 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4289 * XXX this could be even more effective if we could accomodate
4290 * cases such as "v.x = f();" - would help with typical vertex
4293 n
= _slang_gen_function_call_name(A
,
4294 (const char *) oper
->children
[1].a_id
,
4295 &oper
->children
[1], &oper
->children
[0]);
4298 slang_ir_node
*lhs
, *rhs
;
4300 /* lhs and rhs type checking */
4301 if (!_slang_assignment_compatible(A
,
4303 &oper
->children
[1])) {
4304 slang_info_log_error(A
->log
, "incompatible types in assignment");
4308 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4314 slang_info_log_error(A
->log
,
4315 "invalid left hand side for assignment");
4319 /* check that lhs is writable */
4320 if (!is_store_writable(A
, lhs
->Store
)) {
4321 slang_info_log_error(A
->log
,
4322 "illegal assignment to read-only l-value");
4326 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4328 /* convert lhs swizzle into writemask */
4329 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4330 GLuint writemask
, newSwizzle
= 0x0;
4331 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4332 /* Non-simple writemask, need to swizzle right hand side in
4333 * order to put components into the right place.
4335 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4337 n
= new_node2(IR_COPY
, lhs
, rhs
);
4345 /* predicate the assignment code on __notRetFlag */
4346 slang_ir_node
*top
, *cond
;
4348 cond
= _slang_gen_operation(A
, pred
);
4349 top
= new_if(cond
, n
, NULL
);
4357 * Generate IR tree for referencing a field in a struct (or basic vector type)
4359 static slang_ir_node
*
4360 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4364 /* type of struct */
4365 slang_typeinfo_construct(&ti
);
4366 typeof_operation(A
, &oper
->children
[0], &ti
);
4368 if (_slang_type_is_vector(ti
.spec
.type
)) {
4369 /* the field should be a swizzle */
4370 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4374 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4375 slang_info_log_error(A
->log
, "Bad swizzle");
4378 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4383 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4384 /* create new parent node with swizzle */
4386 n
= _slang_gen_swizzle(n
, swizzle
);
4389 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4390 || ti
.spec
.type
== SLANG_SPEC_INT
4391 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4392 const GLuint rows
= 1;
4396 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4397 slang_info_log_error(A
->log
, "Bad swizzle");
4399 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4403 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4404 /* create new parent node with swizzle */
4405 n
= _slang_gen_swizzle(n
, swizzle
);
4409 /* the field is a structure member (base.field) */
4410 /* oper->children[0] is the base */
4411 /* oper->a_id is the field name */
4412 slang_ir_node
*base
, *n
;
4413 slang_typeinfo field_ti
;
4414 GLint fieldSize
, fieldOffset
= -1;
4417 slang_typeinfo_construct(&field_ti
);
4418 typeof_operation(A
, oper
, &field_ti
);
4420 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4422 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4424 if (fieldSize
== 0 || fieldOffset
< 0) {
4425 const char *structName
;
4426 if (ti
.spec
._struct
)
4427 structName
= (char *) ti
.spec
._struct
->a_name
;
4429 structName
= "unknown";
4430 slang_info_log_error(A
->log
,
4431 "\"%s\" is not a member of struct \"%s\"",
4432 (char *) oper
->a_id
, structName
);
4435 assert(fieldSize
>= 0);
4437 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4439 /* error msg should have already been logged */
4443 n
= new_node1(IR_FIELD
, base
);
4447 n
->Field
= (char *) oper
->a_id
;
4449 /* Store the field's offset in storage->Index */
4450 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4460 * Gen code for array indexing.
4462 static slang_ir_node
*
4463 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4465 slang_typeinfo array_ti
;
4467 /* get array's type info */
4468 slang_typeinfo_construct(&array_ti
);
4469 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4471 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4472 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4473 /* translate the index into a swizzle/writemask: "v.x=p" */
4474 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4478 index
= (GLint
) oper
->children
[1].literal
[0];
4479 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4480 index
>= (GLint
) max
) {
4482 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4483 printf("type = %d\n", oper
->children
[1].type
);
4484 printf("index = %d, max = %d\n", index
, max
);
4485 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4486 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4493 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4495 /* use swizzle to access the element */
4496 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4500 n
= _slang_gen_swizzle(n
, swizzle
);
4506 /* conventional array */
4507 slang_typeinfo elem_ti
;
4508 slang_ir_node
*elem
, *array
, *index
;
4509 GLint elemSize
, arrayLen
;
4511 /* size of array element */
4512 slang_typeinfo_construct(&elem_ti
);
4513 typeof_operation(A
, oper
, &elem_ti
);
4514 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4516 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4517 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4519 arrayLen
= array_ti
.array_len
;
4521 slang_typeinfo_destruct(&array_ti
);
4522 slang_typeinfo_destruct(&elem_ti
);
4524 if (elemSize
<= 0) {
4525 /* unknown var or type */
4526 slang_info_log_error(A
->log
, "Undefined variable or type");
4530 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4531 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4532 if (array
&& index
) {
4534 GLint constIndex
= -1;
4535 if (index
->Opcode
== IR_FLOAT
) {
4536 constIndex
= (int) index
->Value
[0];
4537 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4538 slang_info_log_error(A
->log
,
4539 "Array index out of bounds (index=%d size=%d)",
4540 constIndex
, arrayLen
);
4541 _slang_free_ir_tree(array
);
4542 _slang_free_ir_tree(index
);
4547 if (!array
->Store
) {
4548 slang_info_log_error(A
->log
, "Invalid array");
4552 elem
= new_node2(IR_ELEMENT
, array
, index
);
4554 /* The storage info here will be updated during code emit */
4555 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4556 array
->Store
->Index
,
4558 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4562 _slang_free_ir_tree(array
);
4563 _slang_free_ir_tree(index
);
4570 static slang_ir_node
*
4571 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4572 slang_ir_opcode opcode
)
4574 slang_typeinfo t0
, t1
;
4577 slang_typeinfo_construct(&t0
);
4578 typeof_operation(A
, &oper
->children
[0], &t0
);
4580 slang_typeinfo_construct(&t1
);
4581 typeof_operation(A
, &oper
->children
[0], &t1
);
4583 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4584 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4585 slang_info_log_error(A
->log
, "Illegal array comparison");
4589 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4590 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4591 /* <, <=, >, >= can only be used with scalars */
4592 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4593 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4594 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4595 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4596 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4601 n
= new_node2(opcode
,
4602 _slang_gen_operation(A
, &oper
->children
[0]),
4603 _slang_gen_operation(A
, &oper
->children
[1]));
4605 /* result is a bool (size 1) */
4606 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4614 print_vars(slang_variable_scope
*s
)
4618 for (i
= 0; i
< s
->num_variables
; i
++) {
4620 (char*) s
->variables
[i
]->a_name
,
4621 s
->variables
[i
]->declared
);
4631 _slang_undeclare_vars(slang_variable_scope
*locals
)
4633 if (locals
->num_variables
> 0) {
4635 for (i
= 0; i
< locals
->num_variables
; i
++) {
4636 slang_variable
*v
= locals
->variables
[i
];
4637 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4638 v
->declared
= GL_FALSE
;
4646 * Generate IR tree for a slang_operation (AST node)
4648 static slang_ir_node
*
4649 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4651 switch (oper
->type
) {
4652 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4656 _slang_push_var_table(A
->vartable
);
4658 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4659 n
= _slang_gen_operation(A
, oper
);
4660 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4662 _slang_pop_var_table(A
->vartable
);
4664 /*_slang_undeclare_vars(oper->locals);*/
4665 /*print_vars(oper->locals);*/
4668 n
= new_node1(IR_SCOPE
, n
);
4673 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4674 /* list of operations */
4675 if (oper
->num_children
> 0)
4677 slang_ir_node
*n
, *tree
= NULL
;
4680 for (i
= 0; i
< oper
->num_children
; i
++) {
4681 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4683 _slang_free_ir_tree(tree
);
4684 return NULL
; /* error must have occured */
4686 tree
= new_seq(tree
, n
);
4692 return new_node0(IR_NOP
);
4695 case SLANG_OPER_EXPRESSION
:
4696 return _slang_gen_operation(A
, &oper
->children
[0]);
4698 case SLANG_OPER_FOR
:
4699 return _slang_gen_for(A
, oper
);
4701 return _slang_gen_do(A
, oper
);
4702 case SLANG_OPER_WHILE
:
4703 return _slang_gen_while(A
, oper
);
4704 case SLANG_OPER_BREAK
:
4705 if (!current_loop_oper(A
)) {
4706 slang_info_log_error(A
->log
, "'break' not in loop");
4709 return new_break(current_loop_ir(A
));
4710 case SLANG_OPER_CONTINUE
:
4711 if (!current_loop_oper(A
)) {
4712 slang_info_log_error(A
->log
, "'continue' not in loop");
4715 return _slang_gen_continue(A
, oper
);
4716 case SLANG_OPER_DISCARD
:
4717 return new_node0(IR_KILL
);
4719 case SLANG_OPER_EQUAL
:
4720 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4721 case SLANG_OPER_NOTEQUAL
:
4722 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4723 case SLANG_OPER_GREATER
:
4724 return _slang_gen_compare(A
, oper
, IR_SGT
);
4725 case SLANG_OPER_LESS
:
4726 return _slang_gen_compare(A
, oper
, IR_SLT
);
4727 case SLANG_OPER_GREATEREQUAL
:
4728 return _slang_gen_compare(A
, oper
, IR_SGE
);
4729 case SLANG_OPER_LESSEQUAL
:
4730 return _slang_gen_compare(A
, oper
, IR_SLE
);
4731 case SLANG_OPER_ADD
:
4734 assert(oper
->num_children
== 2);
4735 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4738 case SLANG_OPER_SUBTRACT
:
4741 assert(oper
->num_children
== 2);
4742 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4745 case SLANG_OPER_MULTIPLY
:
4748 assert(oper
->num_children
== 2);
4749 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4752 case SLANG_OPER_DIVIDE
:
4755 assert(oper
->num_children
== 2);
4756 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4759 case SLANG_OPER_MINUS
:
4762 assert(oper
->num_children
== 1);
4763 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4766 case SLANG_OPER_PLUS
:
4767 /* +expr --> do nothing */
4768 return _slang_gen_operation(A
, &oper
->children
[0]);
4769 case SLANG_OPER_VARIABLE_DECL
:
4770 return _slang_gen_declaration(A
, oper
);
4771 case SLANG_OPER_ASSIGN
:
4772 return _slang_gen_assignment(A
, oper
);
4773 case SLANG_OPER_ADDASSIGN
:
4776 assert(oper
->num_children
== 2);
4777 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4780 case SLANG_OPER_SUBASSIGN
:
4783 assert(oper
->num_children
== 2);
4784 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4788 case SLANG_OPER_MULASSIGN
:
4791 assert(oper
->num_children
== 2);
4792 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4795 case SLANG_OPER_DIVASSIGN
:
4798 assert(oper
->num_children
== 2);
4799 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4802 case SLANG_OPER_LOGICALAND
:
4805 assert(oper
->num_children
== 2);
4806 n
= _slang_gen_logical_and(A
, oper
);
4809 case SLANG_OPER_LOGICALOR
:
4812 assert(oper
->num_children
== 2);
4813 n
= _slang_gen_logical_or(A
, oper
);
4816 case SLANG_OPER_LOGICALXOR
:
4817 return _slang_gen_xor(A
, oper
);
4818 case SLANG_OPER_NOT
:
4819 return _slang_gen_not(A
, oper
);
4820 case SLANG_OPER_SELECT
: /* b ? x : y */
4823 assert(oper
->num_children
== 3);
4824 n
= _slang_gen_select(A
, oper
);
4828 case SLANG_OPER_ASM
:
4829 return _slang_gen_asm(A
, oper
, NULL
);
4830 case SLANG_OPER_CALL
:
4831 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4833 case SLANG_OPER_METHOD
:
4834 return _slang_gen_method_call(A
, oper
);
4835 case SLANG_OPER_RETURN
:
4836 return _slang_gen_return(A
, oper
);
4837 case SLANG_OPER_RETURN_INLINED
:
4838 return _slang_gen_return(A
, oper
);
4839 case SLANG_OPER_LABEL
:
4840 return new_label(oper
->label
);
4841 case SLANG_OPER_IDENTIFIER
:
4842 return _slang_gen_variable(A
, oper
);
4844 return _slang_gen_if(A
, oper
);
4845 case SLANG_OPER_FIELD
:
4846 return _slang_gen_struct_field(A
, oper
);
4847 case SLANG_OPER_SUBSCRIPT
:
4848 return _slang_gen_array_element(A
, oper
);
4849 case SLANG_OPER_LITERAL_FLOAT
:
4851 case SLANG_OPER_LITERAL_INT
:
4853 case SLANG_OPER_LITERAL_BOOL
:
4854 return new_float_literal(oper
->literal
, oper
->literal_size
);
4856 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4859 assert(oper
->num_children
== 1);
4860 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4863 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4866 assert(oper
->num_children
== 1);
4867 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4870 case SLANG_OPER_PREINCREMENT
: /* ++var */
4873 assert(oper
->num_children
== 1);
4874 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4877 case SLANG_OPER_PREDECREMENT
: /* --var */
4880 assert(oper
->num_children
== 1);
4881 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4885 case SLANG_OPER_NON_INLINED_CALL
:
4886 case SLANG_OPER_SEQUENCE
:
4888 slang_ir_node
*tree
= NULL
;
4890 for (i
= 0; i
< oper
->num_children
; i
++) {
4891 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4892 tree
= new_seq(tree
, n
);
4894 tree
->Store
= n
->Store
;
4896 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4897 tree
= new_function_call(tree
, oper
->label
);
4902 case SLANG_OPER_NONE
:
4903 case SLANG_OPER_VOID
:
4904 /* returning NULL here would generate an error */
4905 return new_node0(IR_NOP
);
4908 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4910 return new_node0(IR_NOP
);
4918 * Check if the given type specifier is a rectangular texture sampler.
4921 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4923 while (spec
->_array
) {
4924 spec
= spec
->_array
;
4926 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4927 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4933 * Called by compiler when a global variable has been parsed/compiled.
4934 * Here we examine the variable's type to determine what kind of register
4935 * storage will be used.
4937 * A uniform such as "gl_Position" will become the register specification
4938 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4939 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4941 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4942 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4943 * actual texture unit (as specified by the user calling glUniform1i()).
4946 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4947 slang_unit_type type
)
4949 struct gl_program
*prog
= A
->program
;
4950 const char *varName
= (char *) var
->a_name
;
4951 GLboolean success
= GL_TRUE
;
4952 slang_ir_storage
*store
= NULL
;
4954 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4955 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4956 const GLint arrayLen
= _slang_array_length(var
);
4957 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4958 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4960 var
->is_global
= GL_TRUE
;
4962 /* check for sampler2D arrays */
4963 if (texIndex
== -1 && var
->type
.specifier
._array
)
4964 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4966 if (texIndex
!= -1) {
4967 /* This is a texture sampler variable...
4968 * store->File = PROGRAM_SAMPLER
4969 * store->Index = sampler number (0..7, typically)
4970 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4972 if (var
->initializer
) {
4973 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4976 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4977 /* disallow rect samplers */
4978 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4979 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4983 (void) is_rect_sampler_spec
; /* silence warning */
4986 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4987 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4989 /* If we have a sampler array, then we need to allocate the
4990 * additional samplers to ensure we don't allocate them elsewhere.
4991 * We can't directly use _mesa_add_sampler() as that checks the
4992 * varName and gets a match, so we call _mesa_add_parameter()
4993 * directly and use the last sampler number from the call above.
4996 GLint a
= arrayLen
- 1;
4998 for (i
= 0; i
< a
; i
++) {
4999 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5000 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5001 varName
, 1, datatype
, &value
, NULL
, 0x0);
5005 if (dbg
) printf("SAMPLER ");
5007 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5008 /* Uniform variable */
5009 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5012 /* user-defined uniform */
5013 if (datatype
== GL_NONE
) {
5014 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5015 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5016 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5017 /* temporary work-around */
5018 GLenum datatype
= GL_FLOAT
;
5019 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5020 totalSize
, datatype
, NULL
);
5021 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5022 totalSize
, swizzle
);
5025 GLint a
= arrayLen
- 1;
5027 for (i
= 0; i
< a
; i
++) {
5028 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5029 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5030 varName
, 1, datatype
, &value
, NULL
, 0x0);
5034 /* XXX what we need to do is unroll the struct into its
5035 * basic types, creating a uniform variable for each.
5043 * Should produce uniforms:
5044 * "f.a" (GL_FLOAT_VEC3)
5045 * "f.b" (GL_FLOAT_VEC4)
5048 if (var
->initializer
) {
5049 slang_info_log_error(A
->log
,
5050 "unsupported initializer for uniform '%s'", varName
);
5055 slang_info_log_error(A
->log
,
5056 "invalid datatype for uniform variable %s",
5062 /* non-struct uniform */
5063 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5069 /* pre-defined uniform, like gl_ModelviewMatrix */
5070 /* We know it's a uniform, but don't allocate storage unless
5073 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5074 totalSize
, swizzle
);
5076 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5078 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5079 /* varyings must be float, vec or mat */
5080 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5081 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5082 slang_info_log_error(A
->log
,
5083 "varying '%s' must be float/vector/matrix",
5088 if (var
->initializer
) {
5089 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5095 /* user-defined varying */
5101 if (var
->type
.centroid
== SLANG_CENTROID
)
5102 flags
|= PROG_PARAM_BIT_CENTROID
;
5103 if (var
->type
.variant
== SLANG_INVARIANT
)
5104 flags
|= PROG_PARAM_BIT_INVARIANT
;
5106 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5108 swizzle
= _slang_var_swizzle(size
, 0);
5109 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5110 totalSize
, swizzle
);
5113 /* pre-defined varying, like gl_Color or gl_TexCoord */
5114 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5115 /* fragment program input */
5117 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5120 assert(index
< FRAG_ATTRIB_MAX
);
5121 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5125 /* vertex program output */
5126 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5127 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5129 assert(index
< VERT_RESULT_MAX
);
5130 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5131 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5134 if (dbg
) printf("V/F ");
5136 if (dbg
) printf("VARYING ");
5138 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5141 /* attributes must be float, vec or mat */
5142 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5143 slang_info_log_error(A
->log
,
5144 "attribute '%s' must be float/vector/matrix",
5150 /* user-defined vertex attribute */
5151 const GLint attr
= -1; /* unknown */
5152 swizzle
= _slang_var_swizzle(size
, 0);
5153 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5154 size
, datatype
, attr
);
5156 index
= VERT_ATTRIB_GENERIC0
+ index
;
5159 /* pre-defined vertex attrib */
5160 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5163 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5164 if (dbg
) printf("ATTRIB ");
5166 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5167 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5168 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5170 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5171 if (dbg
) printf("INPUT ");
5173 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5174 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5175 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5176 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5179 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5180 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5181 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5182 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5184 if (dbg
) printf("OUTPUT ");
5186 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5187 /* pre-defined global constant, like gl_MaxLights */
5188 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5189 if (dbg
) printf("CONST ");
5192 /* ordinary variable (may be const) */
5195 /* IR node to declare the variable */
5196 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5198 /* emit GPU instructions */
5199 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5201 _slang_free_ir_tree(n
);
5204 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5205 store
? store
->Index
: -2);
5208 var
->store
= store
; /* save var's storage info */
5210 var
->declared
= GL_TRUE
;
5217 * Produce an IR tree from a function AST (fun->body).
5218 * Then call the code emitter to convert the IR tree into gl_program
5222 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5225 GLboolean success
= GL_TRUE
;
5227 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5228 /* we only really generate code for main, all other functions get
5229 * inlined or codegen'd upon an actual call.
5232 /* do some basic error checking though */
5233 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5234 /* check that non-void functions actually return something */
5236 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5238 slang_info_log_error(A
->log
,
5239 "function \"%s\" has no return statement",
5240 (char *) fun
->header
.a_name
);
5242 "function \"%s\" has no return statement\n",
5243 (char *) fun
->header
.a_name
);
5248 return GL_TRUE
; /* not an error */
5252 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5253 slang_print_function(fun
, 1);
5256 /* should have been allocated earlier: */
5257 assert(A
->program
->Parameters
);
5258 assert(A
->program
->Varying
);
5259 assert(A
->vartable
);
5262 A
->UseReturnFlag
= GL_FALSE
;
5263 A
->CurFunction
= fun
;
5265 /* fold constant expressions, etc. */
5266 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5269 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5270 slang_print_function(fun
, 1);
5273 /* Create an end-of-function label */
5274 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5276 /* push new vartable scope */
5277 _slang_push_var_table(A
->vartable
);
5279 /* Generate IR tree for the function body code */
5280 n
= _slang_gen_operation(A
, fun
->body
);
5282 n
= new_node1(IR_SCOPE
, n
);
5284 /* pop vartable, restore previous */
5285 _slang_pop_var_table(A
->vartable
);
5288 /* XXX record error */
5292 /* append an end-of-function-label to IR tree */
5293 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5295 /*_slang_label_delete(A->curFuncEndLabel);*/
5296 A
->curFuncEndLabel
= NULL
;
5299 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5300 slang_print_function(fun
, 1);
5303 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5304 _slang_print_ir_tree(n
, 0);
5307 printf("************* End codegen function ************\n\n");
5310 if (A
->UnresolvedRefs
) {
5311 /* Can't codegen at this time.
5312 * At link time we'll concatenate all the vertex shaders and/or all
5313 * the fragment shaders and try recompiling.
5318 /* Emit program instructions */
5319 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5320 _slang_free_ir_tree(n
);
5322 /* free codegen context */
5324 _mesa_free(A->codegen);