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 "program/program.h"
44 #include "program/prog_instruction.h"
45 #include "program/prog_parameter.h"
46 #include "program/prog_print.h"
47 #include "program/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_SAMPLER_1D
:
104 case SLANG_SPEC_SAMPLER_2D
:
105 case SLANG_SPEC_SAMPLER_3D
:
106 case SLANG_SPEC_SAMPLER_CUBE
:
107 case SLANG_SPEC_SAMPLER_1D_SHADOW
:
108 case SLANG_SPEC_SAMPLER_2D_SHADOW
:
109 case SLANG_SPEC_SAMPLER_RECT
:
110 case SLANG_SPEC_SAMPLER_RECT_SHADOW
:
111 case SLANG_SPEC_SAMPLER_1D_ARRAY
:
112 case SLANG_SPEC_SAMPLER_2D_ARRAY
:
113 case SLANG_SPEC_SAMPLER_1D_ARRAY_SHADOW
:
114 case SLANG_SPEC_SAMPLER_2D_ARRAY_SHADOW
:
123 * Return the offset (in floats or ints) of the named field within
124 * the given struct. Return -1 if field not found.
125 * If field is NULL, return the size of the struct instead.
128 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
132 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
133 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
134 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
136 /* types larger than 1 float are register (4-float) aligned */
137 offset
= (offset
+ 3) & ~3;
139 if (field
&& v
->a_name
== field
) {
145 return -1; /* field not found */
147 return offset
; /* struct size */
152 * Return the size (in floats) of the given type specifier.
153 * If the size is greater than 4, the size should be a multiple of 4
154 * so that the correct number of 4-float registers are allocated.
155 * For example, a mat3x2 is size 12 because we want to store the
156 * 3 columns in 3 float[4] registers.
159 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
162 switch (spec
->type
) {
163 case SLANG_SPEC_VOID
:
166 case SLANG_SPEC_BOOL
:
169 case SLANG_SPEC_BVEC2
:
172 case SLANG_SPEC_BVEC3
:
175 case SLANG_SPEC_BVEC4
:
181 case SLANG_SPEC_IVEC2
:
184 case SLANG_SPEC_IVEC3
:
187 case SLANG_SPEC_IVEC4
:
190 case SLANG_SPEC_FLOAT
:
193 case SLANG_SPEC_VEC2
:
196 case SLANG_SPEC_VEC3
:
199 case SLANG_SPEC_VEC4
:
202 case SLANG_SPEC_MAT2
:
203 sz
= 2 * 4; /* 2 columns (regs) */
205 case SLANG_SPEC_MAT3
:
208 case SLANG_SPEC_MAT4
:
211 case SLANG_SPEC_MAT23
:
212 sz
= 2 * 4; /* 2 columns (regs) */
214 case SLANG_SPEC_MAT32
:
215 sz
= 3 * 4; /* 3 columns (regs) */
217 case SLANG_SPEC_MAT24
:
220 case SLANG_SPEC_MAT42
:
221 sz
= 4 * 4; /* 4 columns (regs) */
223 case SLANG_SPEC_MAT34
:
226 case SLANG_SPEC_MAT43
:
227 sz
= 4 * 4; /* 4 columns (regs) */
229 case SLANG_SPEC_SAMPLER_1D
:
230 case SLANG_SPEC_SAMPLER_2D
:
231 case SLANG_SPEC_SAMPLER_3D
:
232 case SLANG_SPEC_SAMPLER_CUBE
:
233 case SLANG_SPEC_SAMPLER_1D_SHADOW
:
234 case SLANG_SPEC_SAMPLER_2D_SHADOW
:
235 case SLANG_SPEC_SAMPLER_RECT
:
236 case SLANG_SPEC_SAMPLER_RECT_SHADOW
:
237 case SLANG_SPEC_SAMPLER_1D_ARRAY
:
238 case SLANG_SPEC_SAMPLER_2D_ARRAY
:
239 case SLANG_SPEC_SAMPLER_1D_ARRAY_SHADOW
:
240 case SLANG_SPEC_SAMPLER_2D_ARRAY_SHADOW
:
241 sz
= 1; /* a sampler is basically just an integer index */
243 case SLANG_SPEC_STRUCT
:
244 sz
= _slang_field_offset(spec
, 0); /* special use */
246 /* 1-float structs are actually troublesome to deal with since they
247 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
248 * ensure the object is placed at R.x
253 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
256 case SLANG_SPEC_ARRAY
:
257 sz
= _slang_sizeof_type_specifier(spec
->_array
);
260 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
265 /* if size is > 4, it should be a multiple of four */
266 assert((sz
& 0x3) == 0);
273 * Query variable/array length (number of elements).
274 * This is slightly non-trivial because there are two ways to express
275 * arrays: "float x[3]" vs. "float[3] x".
276 * \return the length of the array for the given variable, or 0 if not an array
279 _slang_array_length(const slang_variable
*var
)
281 if (var
->type
.array_len
> 0) {
282 /* Ex: float[4] x; */
283 return var
->type
.array_len
;
285 if (var
->array_len
> 0) {
286 /* Ex: float x[4]; */
287 return var
->array_len
;
294 * Compute total size of array give size of element, number of elements.
295 * \return size in floats
298 _slang_array_size(GLint elemSize
, GLint arrayLen
)
301 assert(elemSize
> 0);
303 /* round up base type to multiple of 4 */
304 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
314 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
315 * or -1 if the type is not a sampler.
318 sampler_to_texture_index(const slang_type_specifier_type type
)
321 case SLANG_SPEC_SAMPLER_1D
:
322 return TEXTURE_1D_INDEX
;
323 case SLANG_SPEC_SAMPLER_2D
:
324 return TEXTURE_2D_INDEX
;
325 case SLANG_SPEC_SAMPLER_3D
:
326 return TEXTURE_3D_INDEX
;
327 case SLANG_SPEC_SAMPLER_CUBE
:
328 return TEXTURE_CUBE_INDEX
;
329 case SLANG_SPEC_SAMPLER_1D_SHADOW
:
330 return TEXTURE_1D_INDEX
; /* XXX fix */
331 case SLANG_SPEC_SAMPLER_2D_SHADOW
:
332 return TEXTURE_2D_INDEX
; /* XXX fix */
333 case SLANG_SPEC_SAMPLER_RECT
:
334 return TEXTURE_RECT_INDEX
;
335 case SLANG_SPEC_SAMPLER_RECT_SHADOW
:
336 return TEXTURE_RECT_INDEX
; /* XXX fix */
337 case SLANG_SPEC_SAMPLER_1D_ARRAY
:
338 return TEXTURE_1D_ARRAY_INDEX
;
339 case SLANG_SPEC_SAMPLER_2D_ARRAY
:
340 return TEXTURE_2D_ARRAY_INDEX
;
341 case SLANG_SPEC_SAMPLER_1D_ARRAY_SHADOW
:
342 return TEXTURE_1D_ARRAY_INDEX
;
343 case SLANG_SPEC_SAMPLER_2D_ARRAY_SHADOW
:
344 return TEXTURE_2D_ARRAY_INDEX
;
351 /** helper to build a SLANG_OPER_IDENTIFIER node */
353 slang_operation_identifier(slang_operation
*oper
,
354 slang_assemble_ctx
*A
,
357 oper
->type
= SLANG_OPER_IDENTIFIER
;
358 oper
->a_id
= slang_atom_pool_atom(A
->atoms
, name
);
363 * Called when we begin code/IR generation for a new while/do/for loop.
366 push_loop(slang_assemble_ctx
*A
, slang_operation
*loopOper
, slang_ir_node
*loopIR
)
368 A
->LoopOperStack
[A
->LoopDepth
] = loopOper
;
369 A
->LoopIRStack
[A
->LoopDepth
] = loopIR
;
375 * Called when we end code/IR generation for a new while/do/for loop.
378 pop_loop(slang_assemble_ctx
*A
)
380 assert(A
->LoopDepth
> 0);
386 * Return pointer to slang_operation for the loop we're currently inside,
387 * or NULL if not in a loop.
389 static const slang_operation
*
390 current_loop_oper(const slang_assemble_ctx
*A
)
392 if (A
->LoopDepth
> 0)
393 return A
->LoopOperStack
[A
->LoopDepth
- 1];
400 * Return pointer to slang_ir_node for the loop we're currently inside,
401 * or NULL if not in a loop.
403 static slang_ir_node
*
404 current_loop_ir(const slang_assemble_ctx
*A
)
406 if (A
->LoopDepth
> 0)
407 return A
->LoopIRStack
[A
->LoopDepth
- 1];
413 /**********************************************************************/
417 * Map "_asm foo" to IR_FOO, etc.
422 slang_ir_opcode Opcode
;
423 GLuint HaveRetValue
, NumParams
;
427 static slang_asm_info AsmInfo
[] = {
429 { "vec4_add", IR_ADD
, 1, 2 },
430 { "vec4_subtract", IR_SUB
, 1, 2 },
431 { "vec4_multiply", IR_MUL
, 1, 2 },
432 { "vec4_dot", IR_DOT4
, 1, 2 },
433 { "vec3_dot", IR_DOT3
, 1, 2 },
434 { "vec2_dot", IR_DOT2
, 1, 2 },
435 { "vec3_nrm", IR_NRM3
, 1, 1 },
436 { "vec4_nrm", IR_NRM4
, 1, 1 },
437 { "vec3_cross", IR_CROSS
, 1, 2 },
438 { "vec4_lrp", IR_LRP
, 1, 3 },
439 { "vec4_min", IR_MIN
, 1, 2 },
440 { "vec4_max", IR_MAX
, 1, 2 },
441 { "vec4_cmp", IR_CMP
, 1, 3 },
442 { "vec4_clamp", IR_CLAMP
, 1, 3 },
443 { "vec4_seq", IR_SEQUAL
, 1, 2 },
444 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
445 { "vec4_sge", IR_SGE
, 1, 2 },
446 { "vec4_sgt", IR_SGT
, 1, 2 },
447 { "vec4_sle", IR_SLE
, 1, 2 },
448 { "vec4_slt", IR_SLT
, 1, 2 },
450 { "vec4_move", IR_MOVE
, 1, 1 },
451 { "vec4_floor", IR_FLOOR
, 1, 1 },
452 { "vec4_frac", IR_FRAC
, 1, 1 },
453 { "vec4_abs", IR_ABS
, 1, 1 },
454 { "vec4_negate", IR_NEG
, 1, 1 },
455 { "vec4_ddx", IR_DDX
, 1, 1 },
456 { "vec4_ddy", IR_DDY
, 1, 1 },
457 /* float binary op */
458 { "float_power", IR_POW
, 1, 2 },
459 /* texture / sampler */
460 { "vec4_tex_1d", IR_TEX
, 1, 2 },
461 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
462 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
463 { "vec4_tex_2d", IR_TEX
, 1, 2 },
464 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
465 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
466 { "vec4_tex_3d", IR_TEX
, 1, 2 },
467 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
468 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
469 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
470 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
471 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
472 { "vec4_tex_1d_array", IR_TEX
, 1, 2 },
473 { "vec4_tex_1d_array_bias", IR_TEXB
, 1, 2 },
474 { "vec4_tex_1d_array_shadow", IR_TEX
, 1, 2 },
475 { "vec4_tex_1d_array_bias_shadow", IR_TEXB
, 1, 2 },
476 { "vec4_tex_2d_array", IR_TEX
, 1, 2 },
477 { "vec4_tex_2d_array_bias", IR_TEXB
, 1, 2 },
478 { "vec4_tex_2d_array_shadow", IR_TEX
, 1, 2 },
479 { "vec4_tex_2d_array_bias_shadow", IR_TEXB
, 1, 2 },
481 /* texture / sampler but with shadow comparison */
482 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
483 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
484 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
485 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
486 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
487 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
488 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
489 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
492 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
493 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
494 { "float_exp", IR_EXP
, 1, 1 },
495 { "float_exp2", IR_EXP2
, 1, 1 },
496 { "float_log2", IR_LOG2
, 1, 1 },
497 { "float_rsq", IR_RSQ
, 1, 1 },
498 { "float_rcp", IR_RCP
, 1, 1 },
499 { "float_sine", IR_SIN
, 1, 1 },
500 { "float_cosine", IR_COS
, 1, 1 },
501 { "float_noise1", IR_NOISE1
, 1, 1},
502 { "float_noise2", IR_NOISE2
, 1, 1},
503 { "float_noise3", IR_NOISE3
, 1, 1},
504 { "float_noise4", IR_NOISE4
, 1, 1},
506 { "emit_vertex", IR_EMIT_VERTEX
, 0, 0},
507 { "end_primitive", IR_END_PRIMITIVE
, 0, 0},
509 { NULL
, IR_NOP
, 0, 0 }
513 static slang_ir_node
*
514 new_node3(slang_ir_opcode op
,
515 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
517 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
523 n
->InstLocation
= -1;
528 static slang_ir_node
*
529 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
531 return new_node3(op
, c0
, c1
, NULL
);
534 static slang_ir_node
*
535 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
537 return new_node3(op
, c0
, NULL
, NULL
);
540 static slang_ir_node
*
541 new_node0(slang_ir_opcode op
)
543 return new_node3(op
, NULL
, NULL
, NULL
);
548 * Create sequence of two nodes.
550 static slang_ir_node
*
551 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
557 return new_node2(IR_SEQ
, left
, right
);
560 static slang_ir_node
*
561 new_label(slang_label
*label
)
563 slang_ir_node
*n
= new_node0(IR_LABEL
);
570 static slang_ir_node
*
571 new_float_literal(const float v
[4], GLuint size
)
573 slang_ir_node
*n
= new_node0(IR_FLOAT
);
575 COPY_4V(n
->Value
, v
);
576 /* allocate a storage object, but compute actual location (Index) later */
577 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
582 static slang_ir_node
*
583 new_not(slang_ir_node
*n
)
585 return new_node1(IR_NOT
, n
);
590 * Non-inlined function call.
592 static slang_ir_node
*
593 new_function_call(slang_ir_node
*code
, slang_label
*name
)
595 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
604 * Unconditional jump.
606 static slang_ir_node
*
607 new_return(slang_label
*dest
)
609 slang_ir_node
*n
= new_node0(IR_RETURN
);
617 static slang_ir_node
*
618 new_loop(slang_ir_node
*body
)
620 return new_node1(IR_LOOP
, body
);
624 static slang_ir_node
*
625 new_break(slang_ir_node
*loopNode
)
627 slang_ir_node
*n
= new_node0(IR_BREAK
);
629 assert(loopNode
->Opcode
== IR_LOOP
);
631 /* insert this node at head of linked list of cont/break instructions */
632 n
->List
= loopNode
->List
;
640 * Make new IR_BREAK_IF_TRUE.
642 static slang_ir_node
*
643 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
645 slang_ir_node
*loopNode
= current_loop_ir(A
);
648 assert(loopNode
->Opcode
== IR_LOOP
);
649 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
651 /* insert this node at head of linked list of cont/break instructions */
652 n
->List
= loopNode
->List
;
660 * Make new IR_CONT_IF_TRUE node.
662 static slang_ir_node
*
663 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
665 slang_ir_node
*loopNode
= current_loop_ir(A
);
668 assert(loopNode
->Opcode
== IR_LOOP
);
669 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
671 n
->Parent
= loopNode
; /* pointer to containing loop */
672 /* insert this node at head of linked list of cont/break instructions */
673 n
->List
= loopNode
->List
;
680 static slang_ir_node
*
681 new_cond(slang_ir_node
*n
)
683 slang_ir_node
*c
= new_node1(IR_COND
, n
);
688 static slang_ir_node
*
689 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
691 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
696 * New IR_VAR node - a reference to a previously declared variable.
698 static slang_ir_node
*
699 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
701 slang_ir_node
*n
= new_node0(IR_VAR
);
708 /* Set IR node's Var and Store pointers */
710 n
->Store
= var
->store
;
717 * Check if the given function is really just a wrapper for a
718 * basic assembly instruction.
721 slang_is_asm_function(const slang_function
*fun
)
723 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
724 fun
->body
->num_children
== 1 &&
725 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
733 _slang_is_noop(const slang_operation
*oper
)
736 oper
->type
== SLANG_OPER_VOID
||
737 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
745 * Recursively search tree for a node of the given type.
748 static slang_operation
*
749 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
752 if (oper
->type
== type
)
754 for (i
= 0; i
< oper
->num_children
; i
++) {
755 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
765 * Count the number of operations of the given time rooted at 'oper'.
768 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
771 if (oper
->type
== type
) {
774 for (i
= 0; i
< oper
->num_children
; i
++) {
775 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
782 * Check if the 'return' statement found under 'oper' is a "tail return"
783 * that can be no-op'd. For example:
788 * return; // this is a no-op
791 * This is used when determining if a function can be inlined. If the
792 * 'return' is not the last statement, we can't inline the function since
793 * we still need the semantic behaviour of the 'return' but we don't want
794 * to accidentally return from the _calling_ function. We'd need to use an
795 * unconditional branch, but we don't have such a GPU instruction (not
799 _slang_is_tail_return(const slang_operation
*oper
)
801 GLuint k
= oper
->num_children
;
804 const slang_operation
*last
= &oper
->children
[k
- 1];
805 if (last
->type
== SLANG_OPER_RETURN
)
807 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
808 last
->type
== SLANG_OPER_LABEL
)
809 k
--; /* try prev child */
810 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
811 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
812 /* try sub-children */
813 return _slang_is_tail_return(last
);
823 * Generate a variable declaration opeartion.
824 * I.e.: generate AST code for "bool flag = false;"
827 slang_generate_declaration(slang_assemble_ctx
*A
,
828 slang_variable_scope
*scope
,
829 slang_operation
*decl
,
830 slang_type_specifier_type type
,
836 assert(type
== SLANG_SPEC_BOOL
||
837 type
== SLANG_SPEC_INT
);
839 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
841 var
= slang_variable_scope_grow(scope
);
843 slang_fully_specified_type_construct(&var
->type
);
845 var
->type
.specifier
.type
= type
;
846 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
847 decl
->a_id
= var
->a_name
;
848 var
->initializer
= slang_operation_new(1);
849 slang_operation_literal_bool(var
->initializer
, initValue
);
854 slang_resolve_variable(slang_operation
*oper
)
856 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
857 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
863 * Rewrite AST code for "return expression;".
865 * We return values from functions by assinging the returned value to
866 * the hidden __retVal variable which is an extra 'out' parameter we add
867 * to the function signature.
868 * This code basically converts "return expr;" into "__retVal = expr; return;"
870 * \return the new AST code.
872 static slang_operation
*
873 gen_return_with_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
875 slang_operation
*blockOper
, *assignOper
;
877 assert(oper
->type
== SLANG_OPER_RETURN
);
879 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
880 slang_info_log_error(A
->log
, "illegal return expression");
884 blockOper
= slang_operation_new(1);
885 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
886 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
887 slang_operation_add_children(blockOper
, 2);
889 if (A
->UseReturnFlag
) {
898 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
899 ifOper
->type
= SLANG_OPER_IF
;
900 slang_operation_add_children(ifOper
, 3);
902 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
903 cond
->type
= SLANG_OPER_IDENTIFIER
;
904 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
907 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
908 elseOper
->type
= SLANG_OPER_VOID
;
910 assignOper
= slang_oper_child(ifOper
, 1);
913 slang_operation
*setOper
= slang_oper_child(blockOper
, 1);
914 setOper
->type
= SLANG_OPER_ASSIGN
;
915 slang_operation_add_children(setOper
, 2);
917 slang_operation
*lhs
= slang_oper_child(setOper
, 0);
918 lhs
->type
= SLANG_OPER_IDENTIFIER
;
919 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
922 slang_operation
*rhs
= slang_oper_child(setOper
, 1);
923 slang_operation_literal_bool(rhs
, GL_FALSE
);
934 assignOper
= slang_oper_child(blockOper
, 0);
936 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
937 returnOper
->type
= SLANG_OPER_RETURN_INLINED
;
938 assert(returnOper
->num_children
== 0);
942 /* __retVal = expression; */
943 assignOper
->type
= SLANG_OPER_ASSIGN
;
944 slang_operation_add_children(assignOper
, 2);
946 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
947 lhs
->type
= SLANG_OPER_IDENTIFIER
;
948 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
951 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
952 slang_operation_copy(rhs
, &oper
->children
[0]);
955 /*blockOper->locals->outer_scope = oper->locals->outer_scope;*/
957 /*slang_print_tree(blockOper, 0);*/
964 * Rewrite AST code for "return;" (no expression).
966 static slang_operation
*
967 gen_return_without_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
969 slang_operation
*newRet
;
971 assert(oper
->type
== SLANG_OPER_RETURN
);
973 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
974 slang_info_log_error(A
->log
, "return statement requires an expression");
978 if (A
->UseReturnFlag
) {
983 newRet
= slang_operation_new(1);
984 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
985 newRet
->type
= SLANG_OPER_ASSIGN
;
986 slang_operation_add_children(newRet
, 2);
988 slang_operation
*lhs
= slang_oper_child(newRet
, 0);
989 lhs
->type
= SLANG_OPER_IDENTIFIER
;
990 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
993 slang_operation
*rhs
= slang_oper_child(newRet
, 1);
994 slang_operation_literal_bool(rhs
, GL_FALSE
);
1002 newRet
= slang_operation_new(1);
1003 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1004 newRet
->type
= SLANG_OPER_RETURN_INLINED
;
1007 /*slang_print_tree(newRet, 0);*/
1016 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1019 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
1020 GLuint substCount
, slang_variable
**substOld
,
1021 slang_operation
**substNew
, GLboolean isLHS
)
1023 switch (oper
->type
) {
1024 case SLANG_OPER_VARIABLE_DECL
:
1026 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
1027 oper
->a_id
, GL_TRUE
);
1029 if (v
->initializer
&& oper
->num_children
== 0) {
1030 /* set child of oper to copy of initializer */
1031 oper
->num_children
= 1;
1032 oper
->children
= slang_operation_new(1);
1033 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1035 if (oper
->num_children
== 1) {
1036 /* the initializer */
1037 slang_substitute(A
, &oper
->children
[0], substCount
,
1038 substOld
, substNew
, GL_FALSE
);
1042 case SLANG_OPER_IDENTIFIER
:
1043 assert(oper
->num_children
== 0);
1044 if (1/**!isLHS XXX FIX */) {
1045 slang_atom id
= oper
->a_id
;
1048 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1051 if (strcmp((char *) oper
->a_id
, "__notRetFlag"))
1052 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1057 /* look for a substitution */
1058 for (i
= 0; i
< substCount
; i
++) {
1059 if (v
== substOld
[i
]) {
1060 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1061 #if 0 /* DEBUG only */
1062 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1063 assert(substNew
[i
]->var
);
1064 assert(substNew
[i
]->var
->a_name
);
1065 printf("Substitute %s with %s in id node %p\n",
1066 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1070 printf("Substitute %s with %f in id node %p\n",
1071 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1075 slang_operation_copy(oper
, substNew
[i
]);
1082 case SLANG_OPER_RETURN
:
1084 slang_operation
*newReturn
;
1085 /* generate new 'return' code' */
1086 if (slang_oper_child(oper
, 0)->type
== SLANG_OPER_VOID
)
1087 newReturn
= gen_return_without_expression(A
, oper
);
1089 newReturn
= gen_return_with_expression(A
, oper
);
1094 /* do substitutions on the new 'return' code */
1095 slang_substitute(A
, newReturn
,
1096 substCount
, substOld
, substNew
, GL_FALSE
);
1098 /* install new 'return' code */
1099 slang_operation_copy(oper
, newReturn
);
1100 slang_operation_destruct(newReturn
);
1104 case SLANG_OPER_ASSIGN
:
1105 case SLANG_OPER_SUBSCRIPT
:
1107 * child[0] can't have substitutions but child[1] can.
1109 slang_substitute(A
, &oper
->children
[0],
1110 substCount
, substOld
, substNew
, GL_TRUE
);
1111 slang_substitute(A
, &oper
->children
[1],
1112 substCount
, substOld
, substNew
, GL_FALSE
);
1114 case SLANG_OPER_FIELD
:
1115 /* XXX NEW - test */
1116 slang_substitute(A
, &oper
->children
[0],
1117 substCount
, substOld
, substNew
, GL_TRUE
);
1122 for (i
= 0; i
< oper
->num_children
; i
++)
1123 slang_substitute(A
, &oper
->children
[i
],
1124 substCount
, substOld
, substNew
, GL_FALSE
);
1131 * Produce inline code for a call to an assembly instruction.
1132 * This is typically used to compile a call to a built-in function like this:
1134 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1136 * __asm vec4_lrp __retVal, a, y, x;
1141 * r = mix(p1, p2, p3);
1151 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1153 static slang_operation
*
1154 slang_inline_asm_function(slang_assemble_ctx
*A
,
1155 slang_function
*fun
, slang_operation
*oper
)
1157 const GLuint numArgs
= oper
->num_children
;
1159 slang_operation
*inlined
;
1160 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1161 slang_variable
**substOld
;
1162 slang_operation
**substNew
;
1164 ASSERT(slang_is_asm_function(fun
));
1165 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1168 printf("Inline %s as %s\n",
1169 (char*) fun->header.a_name,
1170 (char*) fun->body->children[0].a_id);
1174 * We'll substitute formal params with actual args in the asm call.
1176 substOld
= (slang_variable
**)
1177 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1178 substNew
= (slang_operation
**)
1179 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1180 for (i
= 0; i
< numArgs
; i
++) {
1181 substOld
[i
] = fun
->parameters
->variables
[i
];
1182 substNew
[i
] = oper
->children
+ i
;
1185 /* make a copy of the code to inline */
1186 inlined
= slang_operation_new(1);
1187 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1189 /* get rid of the __retVal child */
1190 inlined
->num_children
--;
1191 for (i
= 0; i
< inlined
->num_children
; i
++) {
1192 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1196 /* now do formal->actual substitutions */
1197 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1199 _slang_free(substOld
);
1200 _slang_free(substNew
);
1203 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1204 (char *) fun
->header
.a_name
);
1205 slang_print_tree(inlined
, 3);
1206 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1214 * Inline the given function call operation.
1215 * Return a new slang_operation that corresponds to the inlined code.
1217 static slang_operation
*
1218 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1219 slang_operation
*oper
, slang_operation
*returnOper
)
1226 ParamMode
*paramMode
;
1227 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1228 const GLuint numArgs
= oper
->num_children
;
1229 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1230 slang_operation
*args
= oper
->children
;
1231 slang_operation
*inlined
, *top
;
1232 slang_variable
**substOld
;
1233 slang_operation
**substNew
;
1234 GLuint substCount
, numCopyIn
, i
;
1235 slang_function
*prevFunction
;
1236 slang_variable_scope
*newScope
= NULL
;
1239 prevFunction
= A
->CurFunction
;
1240 A
->CurFunction
= fun
;
1242 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1243 assert(fun
->param_count
== totalArgs
);
1245 /* allocate temporary arrays */
1246 paramMode
= (ParamMode
*)
1247 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1248 substOld
= (slang_variable
**)
1249 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1250 substNew
= (slang_operation
**)
1251 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1254 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1255 (char *) fun
->header
.a_name
,
1256 fun
->parameters
->num_variables
, numArgs
);
1259 if (haveRetValue
&& !returnOper
) {
1260 /* Create 3-child comma sequence for inlined code:
1261 * child[0]: declare __resultTmp
1262 * child[1]: inlined function body
1263 * child[2]: __resultTmp
1265 slang_operation
*commaSeq
;
1266 slang_operation
*declOper
= NULL
;
1267 slang_variable
*resultVar
;
1269 commaSeq
= slang_operation_new(1);
1270 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1271 assert(commaSeq
->locals
);
1272 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1273 commaSeq
->num_children
= 3;
1274 commaSeq
->children
= slang_operation_new(3);
1275 /* allocate the return var */
1276 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1278 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1279 (void*)commaSeq->locals, (char *) fun->header.a_name);
1282 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1283 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1284 resultVar
->isTemp
= GL_TRUE
;
1286 /* child[0] = __resultTmp declaration */
1287 declOper
= &commaSeq
->children
[0];
1288 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1289 declOper
->a_id
= resultVar
->a_name
;
1290 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1292 /* child[1] = function body */
1293 inlined
= &commaSeq
->children
[1];
1294 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1296 /* child[2] = __resultTmp reference */
1297 returnOper
= &commaSeq
->children
[2];
1298 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1299 returnOper
->a_id
= resultVar
->a_name
;
1300 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1305 top
= inlined
= slang_operation_new(1);
1306 /* XXXX this may be inappropriate!!!! */
1307 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1311 assert(inlined
->locals
);
1313 /* Examine the parameters, look for inout/out params, look for possible
1314 * substitutions, etc:
1315 * param type behaviour
1316 * in copy actual to local
1317 * const in substitute param with actual
1321 for (i
= 0; i
< totalArgs
; i
++) {
1322 slang_variable
*p
= fun
->parameters
->variables
[i
];
1324 printf("Param %d: %s %s \n", i,
1325 slang_type_qual_string(p->type.qualifier),
1326 (char *) p->a_name);
1328 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1329 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1330 /* an output param */
1331 slang_operation
*arg
;
1336 paramMode
[i
] = SUBST
;
1338 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1339 slang_resolve_variable(arg
);
1341 /* replace parameter 'p' with argument 'arg' */
1342 substOld
[substCount
] = p
;
1343 substNew
[substCount
] = arg
; /* will get copied */
1346 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1347 /* a constant input param */
1348 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1349 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
||
1350 args
[i
].type
== SLANG_OPER_SUBSCRIPT
) {
1351 /* replace all occurances of this parameter variable with the
1352 * actual argument variable or a literal.
1354 paramMode
[i
] = SUBST
;
1355 slang_resolve_variable(&args
[i
]);
1356 substOld
[substCount
] = p
;
1357 substNew
[substCount
] = &args
[i
]; /* will get copied */
1361 paramMode
[i
] = COPY_IN
;
1365 paramMode
[i
] = COPY_IN
;
1367 assert(paramMode
[i
]);
1370 /* actual code inlining: */
1371 slang_operation_copy(inlined
, fun
->body
);
1373 /*** XXX review this */
1374 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1375 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1376 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1379 printf("======================= orig body code ======================\n");
1380 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1381 slang_print_tree(fun
->body
, 8);
1382 printf("======================= copied code =========================\n");
1383 slang_print_tree(inlined
, 8);
1386 /* do parameter substitution in inlined code: */
1387 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1390 printf("======================= subst code ==========================\n");
1391 slang_print_tree(inlined
, 8);
1392 printf("=============================================================\n");
1395 /* New prolog statements: (inserted before the inlined code)
1396 * Copy the 'in' arguments.
1399 for (i
= 0; i
< numArgs
; i
++) {
1400 if (paramMode
[i
] == COPY_IN
) {
1401 slang_variable
*p
= fun
->parameters
->variables
[i
];
1402 /* declare parameter 'p' */
1403 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1407 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1408 assert(decl
->locals
);
1409 decl
->locals
->outer_scope
= inlined
->locals
;
1410 decl
->a_id
= p
->a_name
;
1411 decl
->num_children
= 1;
1412 decl
->children
= slang_operation_new(1);
1414 /* child[0] is the var's initializer */
1415 slang_operation_copy(&decl
->children
[0], args
+ i
);
1417 /* add parameter 'p' to the local variable scope here */
1419 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1420 pCopy
->type
= p
->type
;
1421 pCopy
->a_name
= p
->a_name
;
1422 pCopy
->array_len
= p
->array_len
;
1425 newScope
= inlined
->locals
;
1430 /* Now add copies of the function's local vars to the new variable scope */
1431 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1432 slang_variable
*p
= fun
->parameters
->variables
[i
];
1433 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1434 pCopy
->type
= p
->type
;
1435 pCopy
->a_name
= p
->a_name
;
1436 pCopy
->array_len
= p
->array_len
;
1440 /* New epilog statements:
1441 * 1. Create end of function label to jump to from return statements.
1442 * 2. Copy the 'out' parameter vars
1445 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1447 inlined
->num_children
);
1448 lab
->type
= SLANG_OPER_LABEL
;
1449 lab
->label
= A
->curFuncEndLabel
;
1452 for (i
= 0; i
< totalArgs
; i
++) {
1453 if (paramMode
[i
] == COPY_OUT
) {
1454 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1455 /* actualCallVar = outParam */
1456 /*if (i > 0 || !haveRetValue)*/
1457 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1459 inlined
->num_children
);
1460 ass
->type
= SLANG_OPER_ASSIGN
;
1461 ass
->num_children
= 2;
1462 ass
->locals
->outer_scope
= inlined
->locals
;
1463 ass
->children
= slang_operation_new(2);
1464 ass
->children
[0] = args
[i
]; /*XXX copy */
1465 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1466 ass
->children
[1].a_id
= p
->a_name
;
1467 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1471 _slang_free(paramMode
);
1472 _slang_free(substOld
);
1473 _slang_free(substNew
);
1475 /* Update scoping to use the new local vars instead of the
1476 * original function's vars. This is especially important
1477 * for nested inlining.
1480 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1483 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1484 (char *) fun
->header
.a_name
,
1485 fun
->parameters
->num_variables
, numArgs
);
1486 slang_print_tree(top
, 0);
1490 A
->CurFunction
= prevFunction
;
1497 * Insert declaration for "bool __notRetFlag" in given block operation.
1498 * This is used when we can't emit "early" return statements in subroutines.
1501 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1503 slang_operation
*decl
;
1505 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1506 oper
->type
== SLANG_OPER_SEQUENCE
);
1508 decl
= slang_operation_insert_child(oper
, 1);
1510 slang_generate_declaration(A
, oper
->locals
, decl
,
1511 SLANG_SPEC_BOOL
, "__notRetFlag", GL_TRUE
);
1513 /*slang_print_tree(oper, 0);*/
1518 * Recursively replace instances of the old node type with the new type.
1521 replace_node_type(slang_operation
*oper
, slang_operation_type oldType
,
1522 slang_operation_type newType
)
1526 if (oper
->type
== oldType
)
1527 oper
->type
= newType
;
1529 for (i
= 0; i
< slang_oper_num_children(oper
); i
++) {
1530 replace_node_type(slang_oper_child(oper
, i
), oldType
, newType
);
1537 * Test if the given function body has an "early return". That is, there's
1538 * a 'return' statement that's not the very last instruction in the body.
1541 has_early_return(const slang_operation
*funcBody
)
1543 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1546 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1554 * Emit IR code for a function call. This does one of two things:
1555 * 1. Inline the function's code
1556 * 2. Create an IR for the function's body and create a real call to it.
1558 static slang_ir_node
*
1559 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1560 slang_operation
*oper
, slang_operation
*dest
)
1563 slang_operation
*instance
;
1564 slang_label
*prevFuncEndLabel
;
1567 prevFuncEndLabel
= A
->curFuncEndLabel
;
1568 _mesa_snprintf(name
, sizeof(name
), "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1569 A
->curFuncEndLabel
= _slang_label_new(name
);
1570 assert(A
->curFuncEndLabel
);
1573 * 'instance' is basically a copy of the function's body with various
1577 if (slang_is_asm_function(fun
) && !dest
) {
1578 /* assemble assembly function - tree style */
1579 instance
= slang_inline_asm_function(A
, fun
, oper
);
1582 /* non-assembly function */
1583 /* We always generate an "inline-able" block of code here.
1585 * 1. insert the inline code
1586 * 2. Generate a call to the "inline" code as a subroutine
1588 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1590 if (earlyReturn
&& !A
->EmitContReturn
) {
1591 A
->UseReturnFlag
= GL_TRUE
;
1594 instance
= slang_inline_function_call(A
, fun
, oper
, dest
);
1599 /* The function we're calling has one or more 'return' statements
1600 * that prevent us from inlining the function's code.
1602 * In this case, change the function's body type from
1603 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1604 * During code emit this will result in a true subroutine call.
1606 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1608 slang_operation
*callOper
;
1610 assert(instance
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1611 instance
->type
== SLANG_OPER_SEQUENCE
);
1613 if (_slang_function_has_return_value(fun
) && !dest
) {
1614 assert(instance
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1615 assert(instance
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1616 callOper
= &instance
->children
[1];
1619 callOper
= instance
;
1622 if (A
->UseReturnFlag
) {
1623 /* Early returns not supported. Create a _returnFlag variable
1624 * that's set upon 'return' and tested elsewhere to no-op any
1625 * remaining instructions in the subroutine.
1627 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1628 callOper
->type
== SLANG_OPER_SEQUENCE
);
1629 declare_return_flag(A
, callOper
);
1632 /* We can emit real 'return' statements. If we generated any
1633 * 'inline return' statements during function instantiation,
1634 * change them back to regular 'return' statements.
1636 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1640 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1641 callOper
->fun
= fun
;
1642 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1645 /* If there are any 'return' statements remaining, they're at the
1646 * very end of the function and can effectively become no-ops.
1648 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1656 /* Replace the function call with the instance block (or new CALL stmt) */
1657 slang_operation_destruct(oper
);
1659 _slang_free(instance
);
1662 assert(instance
->locals
);
1663 printf("*** Inlined code for call to %s:\n", (char*) fun
->header
.a_name
);
1664 slang_print_tree(oper
, 10);
1668 n
= _slang_gen_operation(A
, oper
);
1670 /*_slang_label_delete(A->curFuncEndLabel);*/
1671 A
->curFuncEndLabel
= prevFuncEndLabel
;
1673 if (A
->pragmas
->Debug
) {
1675 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1676 n
->Comment
= _slang_strdup(s
);
1679 A
->UseReturnFlag
= GL_FALSE
;
1685 static slang_asm_info
*
1686 slang_find_asm_info(const char *name
)
1689 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1690 if (strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1699 * Some write-masked assignments are simple, but others are hard.
1702 * v.xy = vec2(a, b);
1705 * v.zy = vec2(a, b);
1706 * this gets transformed/swizzled into:
1707 * v.zy = vec2(a, b).*yx* (* = don't care)
1708 * This function helps to determine simple vs. non-simple.
1711 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1713 switch (writemask
) {
1715 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1717 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1719 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1721 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1723 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1724 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1726 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1727 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1728 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1729 case WRITEMASK_XYZW
:
1730 return swizzle
== SWIZZLE_NOOP
;
1738 * Convert the given swizzle into a writemask. In some cases this
1739 * is trivial, in other cases, we'll need to also swizzle the right
1740 * hand side to put components in the right places.
1741 * See comment above for more info.
1742 * XXX this function could be simplified and should probably be renamed.
1743 * \param swizzle the incoming swizzle
1744 * \param writemaskOut returns the writemask
1745 * \param swizzleOut swizzle to apply to the right-hand-side
1746 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1749 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1750 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1752 GLuint mask
= 0x0, newSwizzle
[4];
1755 /* make new dst writemask, compute size */
1756 for (i
= 0; i
< 4; i
++) {
1757 const GLuint swz
= GET_SWZ(swizzle
, i
);
1758 if (swz
== SWIZZLE_NIL
) {
1764 if (swizzle
!= SWIZZLE_XXXX
&&
1765 swizzle
!= SWIZZLE_YYYY
&&
1766 swizzle
!= SWIZZLE_ZZZZ
&&
1767 swizzle
!= SWIZZLE_WWWW
&&
1768 (mask
& (1 << swz
))) {
1769 /* a channel can't be specified twice (ex: ".xyyz") */
1770 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1771 _mesa_swizzle_string(swizzle
, 0, 0));
1777 assert(mask
<= 0xf);
1778 size
= i
; /* number of components in mask/swizzle */
1780 *writemaskOut
= mask
;
1782 /* make new src swizzle, by inversion */
1783 for (i
= 0; i
< 4; i
++) {
1784 newSwizzle
[i
] = i
; /*identity*/
1786 for (i
= 0; i
< size
; i
++) {
1787 const GLuint swz
= GET_SWZ(swizzle
, i
);
1788 newSwizzle
[swz
] = i
;
1790 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1795 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1797 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1799 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1801 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1803 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1811 #if 0 /* not used, but don't remove just yet */
1813 * Recursively traverse 'oper' to produce a swizzle mask in the event
1814 * of any vector subscripts and swizzle suffixes.
1815 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1818 resolve_swizzle(const slang_operation
*oper
)
1820 if (oper
->type
== SLANG_OPER_FIELD
) {
1821 /* writemask from .xyzw suffix */
1823 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1824 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1828 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1829 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1833 return SWIZZLE_XYZW
;
1835 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1836 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1837 /* writemask from [index] */
1838 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1839 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1844 swizzle
= SWIZZLE_XXXX
;
1847 swizzle
= SWIZZLE_YYYY
;
1850 swizzle
= SWIZZLE_ZZZZ
;
1853 swizzle
= SWIZZLE_WWWW
;
1856 swizzle
= SWIZZLE_XYZW
;
1858 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1862 return SWIZZLE_XYZW
;
1870 * Recursively descend through swizzle nodes to find the node's storage info.
1872 static slang_ir_storage
*
1873 get_store(const slang_ir_node
*n
)
1875 if (n
->Opcode
== IR_SWIZZLE
) {
1876 return get_store(n
->Children
[0]);
1884 * Generate IR tree for an asm instruction/operation such as:
1885 * __asm vec4_dot __retVal.x, v1, v2;
1887 static slang_ir_node
*
1888 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1889 slang_operation
*dest
)
1891 const slang_asm_info
*info
;
1892 slang_ir_node
*kids
[3], *n
;
1893 GLuint j
, firstOperand
;
1895 assert(oper
->type
== SLANG_OPER_ASM
);
1897 info
= slang_find_asm_info((char *) oper
->a_id
);
1899 _mesa_problem(NULL
, "undefined __asm function %s\n",
1900 (char *) oper
->a_id
);
1904 assert(info
->NumParams
<= 3);
1906 if (info
->NumParams
== oper
->num_children
) {
1907 /* Storage for result is not specified.
1908 * Children[0], [1], [2] are the operands.
1913 /* Storage for result (child[0]) is specified.
1914 * Children[1], [2], [3] are the operands.
1919 /* assemble child(ren) */
1920 kids
[0] = kids
[1] = kids
[2] = NULL
;
1921 for (j
= 0; j
< info
->NumParams
; j
++) {
1922 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1927 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1930 /* Setup n->Store to be a particular location. Otherwise, storage
1931 * for the result (a temporary) will be allocated later.
1933 slang_operation
*dest_oper
;
1936 dest_oper
= &oper
->children
[0];
1938 n0
= _slang_gen_operation(A
, dest_oper
);
1943 n
->Store
= n0
->Store
;
1945 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1956 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1959 for (i
= 0; i
< scope
->num_functions
; i
++) {
1960 slang_function
*f
= &scope
->functions
[i
];
1961 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1962 printf(" %s (%d args)\n", name
, f
->param_count
);
1965 if (scope
->outer_scope
)
1966 print_funcs(scope
->outer_scope
, name
);
1972 * Find a function of the given name, taking 'numArgs' arguments.
1973 * This is the function we'll try to call when there is no exact match
1974 * between function parameters and call arguments.
1976 * XXX we should really create a list of candidate functions and try
1979 static slang_function
*
1980 _slang_find_function_by_argc(slang_function_scope
*scope
,
1981 const char *name
, int numArgs
)
1985 for (i
= 0; i
< scope
->num_functions
; i
++) {
1986 slang_function
*f
= &scope
->functions
[i
];
1987 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1988 int haveRetValue
= _slang_function_has_return_value(f
);
1989 if (numArgs
== f
->param_count
- haveRetValue
)
1993 scope
= scope
->outer_scope
;
2000 static slang_function
*
2001 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
2004 slang_function
*maxFunc
= NULL
;
2009 for (i
= 0; i
< scope
->num_functions
; i
++) {
2010 slang_function
*f
= &scope
->functions
[i
];
2011 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2012 if (f
->param_count
> maxArgs
) {
2013 maxArgs
= f
->param_count
;
2018 scope
= scope
->outer_scope
;
2026 * Generate a new slang_function which is a constructor for a user-defined
2029 static slang_function
*
2030 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2032 const GLint numFields
= str
->fields
->num_variables
;
2033 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2035 /* function header (name, return type) */
2036 fun
->header
.a_name
= str
->a_name
;
2037 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2038 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2039 fun
->header
.type
.specifier
._struct
= str
;
2041 /* function parameters (= struct's fields) */
2044 for (i
= 0; i
< numFields
; i
++) {
2046 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2048 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2049 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2050 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2052 fun
->param_count
= fun
->parameters
->num_variables
;
2055 /* Add __retVal to params */
2057 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2058 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2060 p
->a_name
= a_retVal
;
2061 p
->type
= fun
->header
.type
;
2062 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2066 /* function body is:
2076 slang_variable_scope
*scope
;
2077 slang_variable
*var
;
2080 fun
->body
= slang_operation_new(1);
2081 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2082 fun
->body
->num_children
= numFields
+ 2;
2083 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2085 scope
= fun
->body
->locals
;
2086 scope
->outer_scope
= fun
->parameters
;
2088 /* create local var 't' */
2089 var
= slang_variable_scope_grow(scope
);
2090 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2091 var
->type
= fun
->header
.type
;
2095 slang_operation
*decl
;
2097 decl
= &fun
->body
->children
[0];
2098 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2099 decl
->locals
= _slang_variable_scope_new(scope
);
2100 decl
->a_id
= var
->a_name
;
2103 /* assign params to fields of t */
2104 for (i
= 0; i
< numFields
; i
++) {
2105 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2107 assign
->type
= SLANG_OPER_ASSIGN
;
2108 assign
->locals
= _slang_variable_scope_new(scope
);
2109 assign
->num_children
= 2;
2110 assign
->children
= slang_operation_new(2);
2113 slang_operation
*lhs
= &assign
->children
[0];
2115 lhs
->type
= SLANG_OPER_FIELD
;
2116 lhs
->locals
= _slang_variable_scope_new(scope
);
2117 lhs
->num_children
= 1;
2118 lhs
->children
= slang_operation_new(1);
2119 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2121 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2122 lhs
->children
[0].a_id
= var
->a_name
;
2123 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2126 lhs
->children
[1].num_children
= 1;
2127 lhs
->children
[1].children
= slang_operation_new(1);
2128 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2129 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2130 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2135 slang_operation
*rhs
= &assign
->children
[1];
2137 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2138 rhs
->locals
= _slang_variable_scope_new(scope
);
2139 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2145 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2147 ret
->type
= SLANG_OPER_RETURN
;
2148 ret
->locals
= _slang_variable_scope_new(scope
);
2149 ret
->num_children
= 1;
2150 ret
->children
= slang_operation_new(1);
2151 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2152 ret
->children
[0].a_id
= var
->a_name
;
2153 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2157 slang_print_function(fun, 1);
2164 * Find/create a function (constructor) for the given structure name.
2166 static slang_function
*
2167 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2170 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2171 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2172 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2173 /* found a structure type that matches the function name */
2174 if (!str
->constructor
) {
2175 /* create the constructor function now */
2176 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2178 return str
->constructor
;
2186 * Generate a new slang_function to satisfy a call to an array constructor.
2187 * Ex: float[3](1., 2., 3.)
2189 static slang_function
*
2190 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2192 slang_type_specifier_type baseType
;
2193 slang_function
*fun
;
2196 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2200 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2202 num_elements
= oper
->num_children
;
2204 /* function header, return type */
2206 fun
->header
.a_name
= oper
->a_id
;
2207 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2208 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2209 fun
->header
.type
.specifier
._array
=
2210 slang_type_specifier_new(baseType
, NULL
, NULL
);
2211 fun
->header
.type
.array_len
= num_elements
;
2214 /* function parameters (= number of elements) */
2217 for (i
= 0; i
< num_elements
; i
++) {
2219 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2221 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2223 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2224 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2225 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2226 p
->type
.specifier
.type
= baseType
;
2228 fun
->param_count
= fun
->parameters
->num_variables
;
2231 /* Add __retVal to params */
2233 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2234 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2236 p
->a_name
= a_retVal
;
2237 p
->type
= fun
->header
.type
;
2238 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2239 p
->type
.specifier
.type
= baseType
;
2243 /* function body is:
2253 slang_variable_scope
*scope
;
2254 slang_variable
*var
;
2257 fun
->body
= slang_operation_new(1);
2258 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2259 fun
->body
->num_children
= num_elements
+ 2;
2260 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2262 scope
= fun
->body
->locals
;
2263 scope
->outer_scope
= fun
->parameters
;
2265 /* create local var 't' */
2266 var
= slang_variable_scope_grow(scope
);
2267 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2268 var
->type
= fun
->header
.type
;/*XXX copy*/
2272 slang_operation
*decl
;
2274 decl
= &fun
->body
->children
[0];
2275 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2276 decl
->locals
= _slang_variable_scope_new(scope
);
2277 decl
->a_id
= var
->a_name
;
2280 /* assign params to elements of t */
2281 for (i
= 0; i
< num_elements
; i
++) {
2282 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2284 assign
->type
= SLANG_OPER_ASSIGN
;
2285 assign
->locals
= _slang_variable_scope_new(scope
);
2286 assign
->num_children
= 2;
2287 assign
->children
= slang_operation_new(2);
2290 slang_operation
*lhs
= &assign
->children
[0];
2292 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2293 lhs
->locals
= _slang_variable_scope_new(scope
);
2294 lhs
->num_children
= 2;
2295 lhs
->children
= slang_operation_new(2);
2297 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2298 lhs
->children
[0].a_id
= var
->a_name
;
2299 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2301 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2302 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2306 slang_operation
*rhs
= &assign
->children
[1];
2308 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2309 rhs
->locals
= _slang_variable_scope_new(scope
);
2310 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2316 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2318 ret
->type
= SLANG_OPER_RETURN
;
2319 ret
->locals
= _slang_variable_scope_new(scope
);
2320 ret
->num_children
= 1;
2321 ret
->children
= slang_operation_new(1);
2322 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2323 ret
->children
[0].a_id
= var
->a_name
;
2324 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2329 slang_print_function(fun, 1);
2337 _slang_is_vec_mat_type(const char *name
)
2339 static const char *vecmat_types
[] = {
2340 "float", "int", "bool",
2341 "vec2", "vec3", "vec4",
2342 "ivec2", "ivec3", "ivec4",
2343 "bvec2", "bvec3", "bvec4",
2344 "mat2", "mat3", "mat4",
2345 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2349 for (i
= 0; vecmat_types
[i
]; i
++)
2350 if (strcmp(name
, vecmat_types
[i
]) == 0)
2357 * Assemble a function call, given a particular function name.
2358 * \param name the function's name (operators like '*' are possible).
2360 static slang_ir_node
*
2361 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2362 slang_operation
*oper
, slang_operation
*dest
)
2364 slang_operation
*params
= oper
->children
;
2365 const GLuint param_count
= oper
->num_children
;
2367 slang_function
*fun
;
2370 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2371 if (atom
== SLANG_ATOM_NULL
)
2374 if (oper
->array_constructor
) {
2375 /* this needs special handling */
2376 fun
= _slang_make_array_constructor(A
, oper
);
2379 /* Try to find function by name and exact argument type matching */
2380 GLboolean error
= GL_FALSE
;
2381 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2382 &A
->space
, A
->atoms
, A
->log
, &error
);
2384 slang_info_log_error(A
->log
,
2385 "Function '%s' not found (check argument types)",
2392 /* Next, try locating a constructor function for a user-defined type */
2393 fun
= _slang_locate_struct_constructor(A
, name
);
2397 * At this point, some heuristics are used to try to find a function
2398 * that matches the calling signature by means of casting or "unrolling"
2402 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2403 /* Next, if this call looks like a vec() or mat() constructor call,
2404 * try "unwinding" the args to satisfy a constructor.
2406 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2408 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2409 slang_info_log_error(A
->log
,
2410 "Function '%s' not found (check argument types)",
2417 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2418 /* Next, try casting args to the types of the formal parameters */
2419 int numArgs
= oper
->num_children
;
2420 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2421 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2422 slang_info_log_error(A
->log
,
2423 "Function '%s' not found (check argument types)",
2431 slang_info_log_error(A
->log
,
2432 "Function '%s' not found (check argument types)",
2438 /* The function body may be in another compilation unit.
2439 * We'll try concatenating the shaders and recompile at link time.
2441 A
->UnresolvedRefs
= GL_TRUE
;
2442 return new_node1(IR_NOP
, NULL
);
2445 /* type checking to be sure function's return type matches 'dest' type */
2449 slang_typeinfo_construct(&t0
);
2450 typeof_operation(A
, dest
, &t0
);
2452 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2453 slang_info_log_error(A
->log
,
2454 "Incompatible type returned by call to '%s'",
2460 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2462 if (n
&& !n
->Store
&& !dest
2463 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2464 /* setup n->Store for the result of the function call */
2465 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2466 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2467 /*printf("Alloc storage for function result, size %d \n", size);*/
2470 if (oper
->array_constructor
) {
2471 /* free the temporary array constructor function now */
2472 slang_function_destruct(fun
);
2479 static slang_ir_node
*
2480 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2482 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2484 slang_variable
*var
;
2486 /* NOTE: In GLSL 1.20, there's only one kind of method
2487 * call: array.length(). Anything else is an error.
2489 if (oper
->a_id
!= a_length
) {
2490 slang_info_log_error(A
->log
,
2491 "Undefined method call '%s'", (char *) oper
->a_id
);
2495 /* length() takes no arguments */
2496 if (oper
->num_children
> 0) {
2497 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2501 /* lookup the object/variable */
2502 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2503 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2504 slang_info_log_error(A
->log
,
2505 "Undefined object '%s'", (char *) oper
->a_obj
);
2509 /* Create a float/literal IR node encoding the array length */
2510 n
= new_node0(IR_FLOAT
);
2512 n
->Value
[0] = (float) _slang_array_length(var
);
2513 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2520 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2522 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2523 oper
->type
== SLANG_OPER_LITERAL_INT
||
2524 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2525 if (oper
->literal
[0])
2531 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2532 oper
->num_children
== 1) {
2533 return _slang_is_constant_cond(&oper
->children
[0], value
);
2540 * Test if an operation is a scalar or boolean.
2543 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2545 slang_typeinfo type
;
2548 slang_typeinfo_construct(&type
);
2549 typeof_operation(A
, oper
, &type
);
2550 size
= _slang_sizeof_type_specifier(&type
.spec
);
2551 slang_typeinfo_destruct(&type
);
2557 * Test if an operation is boolean.
2560 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2562 slang_typeinfo type
;
2565 slang_typeinfo_construct(&type
);
2566 typeof_operation(A
, oper
, &type
);
2567 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2568 slang_typeinfo_destruct(&type
);
2574 * Check if a loop contains a 'continue' statement.
2575 * Stop looking if we find a nested loop.
2578 _slang_loop_contains_continue(const slang_operation
*oper
)
2580 switch (oper
->type
) {
2581 case SLANG_OPER_CONTINUE
:
2583 case SLANG_OPER_FOR
:
2585 case SLANG_OPER_WHILE
:
2586 /* stop upon finding a nested loop */
2592 for (i
= 0; i
< oper
->num_children
; i
++) {
2593 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2594 if (_slang_loop_contains_continue(child
))
2604 * Check if a loop contains a 'continue' or 'break' statement.
2605 * Stop looking if we find a nested loop.
2608 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2610 switch (oper
->type
) {
2611 case SLANG_OPER_CONTINUE
:
2612 case SLANG_OPER_BREAK
:
2614 case SLANG_OPER_FOR
:
2616 case SLANG_OPER_WHILE
:
2617 /* stop upon finding a nested loop */
2623 for (i
= 0; i
< oper
->num_children
; i
++) {
2624 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2625 if (_slang_loop_contains_continue_or_break(child
))
2635 * Replace 'break' and 'continue' statements inside a do and while loops.
2636 * This is a recursive helper function used by
2637 * _slang_gen_do/while_without_continue().
2640 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2642 switch (oper
->type
) {
2643 case SLANG_OPER_BREAK
:
2644 /* replace 'break' with "_notBreakFlag = false; break" */
2646 slang_operation
*block
= oper
;
2647 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2648 slang_operation_add_children(block
, 2);
2650 slang_operation
*assign
= slang_oper_child(block
, 0);
2651 assign
->type
= SLANG_OPER_ASSIGN
;
2652 slang_operation_add_children(assign
, 2);
2654 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2655 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2658 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2659 slang_operation_literal_bool(rhs
, GL_FALSE
);
2663 slang_operation
*brk
= slang_oper_child(block
, 1);
2664 brk
->type
= SLANG_OPER_BREAK
;
2665 assert(!brk
->children
);
2669 case SLANG_OPER_CONTINUE
:
2670 /* convert continue into a break */
2671 oper
->type
= SLANG_OPER_BREAK
;
2673 case SLANG_OPER_FOR
:
2675 case SLANG_OPER_WHILE
:
2676 /* stop upon finding a nested loop */
2682 for (i
= 0; i
< oper
->num_children
; i
++) {
2683 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2691 * Transform a while-loop so that continue statements are converted to breaks.
2692 * Then do normal IR code generation.
2696 * while (LOOPCOND) {
2708 * bool _notBreakFlag = 1;
2709 * while (_notBreakFlag && LOOPCOND) {
2713 * break; // was continue
2716 * _notBreakFlag = 0; // was
2723 static slang_ir_node
*
2724 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2726 slang_operation
*top
;
2727 slang_operation
*innerBody
;
2729 assert(oper
->type
== SLANG_OPER_WHILE
);
2731 top
= slang_operation_new(1);
2732 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2733 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2734 slang_operation_add_children(top
, 2);
2736 /* declare: bool _notBreakFlag = true */
2738 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2739 slang_generate_declaration(A
, top
->locals
, condDecl
,
2740 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2743 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2745 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2746 outerWhile
->type
= SLANG_OPER_WHILE
;
2747 slang_operation_add_children(outerWhile
, 2);
2749 /* _notBreakFlag && LOOPCOND */
2751 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2752 cond
->type
= SLANG_OPER_LOGICALAND
;
2753 slang_operation_add_children(cond
, 2);
2755 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2756 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2759 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2760 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2766 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2767 innerDo
->type
= SLANG_OPER_DO
;
2768 slang_operation_add_children(innerDo
, 2);
2770 /* copy original do-loop body into inner do-loop's body */
2771 innerBody
= slang_oper_child(innerDo
, 0);
2772 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2773 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2775 /* inner do-loop's condition is constant/false */
2777 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2778 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2783 /* Finally, in innerBody,
2784 * replace "break" with "_notBreakFlag = 0; break"
2785 * replace "continue" with "break"
2787 replace_break_and_cont(A
, innerBody
);
2789 /*slang_print_tree(top, 0);*/
2791 return _slang_gen_operation(A
, top
);
2798 * Generate loop code using high-level IR_LOOP instruction
2800 static slang_ir_node
*
2801 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2805 * BREAK if !expr (child[0])
2806 * body code (child[1])
2808 slang_ir_node
*loop
, *breakIf
, *body
;
2809 GLboolean isConst
, constTrue
= GL_FALSE
;
2811 if (!A
->EmitContReturn
) {
2812 /* We don't want to emit CONT instructions. If this while-loop has
2813 * a continue, translate it away.
2815 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2816 return _slang_gen_while_without_continue(A
, oper
);
2820 /* type-check expression */
2821 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2822 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2826 /* Check if loop condition is a constant */
2827 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2829 if (isConst
&& !constTrue
) {
2830 /* loop is never executed! */
2831 return new_node0(IR_NOP
);
2834 /* Begin new loop */
2835 loop
= new_loop(NULL
);
2837 /* save loop state */
2838 push_loop(A
, oper
, loop
);
2840 if (isConst
&& constTrue
) {
2841 /* while(nonzero constant), no conditional break */
2846 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2847 breakIf
= new_break_if_true(A
, cond
);
2849 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2850 loop
->Children
[0] = new_seq(breakIf
, body
);
2852 /* Do infinite loop detection */
2853 /* loop->List is head of linked list of break/continue nodes */
2854 if (!loop
->List
&& isConst
&& constTrue
) {
2855 /* infinite loop detected */
2857 slang_info_log_error(A
->log
, "Infinite loop detected!");
2861 /* restore loop state */
2869 * Transform a do-while-loop so that continue statements are converted to breaks.
2870 * Then do normal IR code generation.
2881 * } while (LOOPCOND);
2886 * bool _notBreakFlag = 1;
2891 * break; // was continue
2894 * _notBreakFlag = 0; // was
2898 * } while (_notBreakFlag && LOOPCOND);
2901 static slang_ir_node
*
2902 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2904 slang_operation
*top
;
2905 slang_operation
*innerBody
;
2907 assert(oper
->type
== SLANG_OPER_DO
);
2909 top
= slang_operation_new(1);
2910 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2911 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2912 slang_operation_add_children(top
, 2);
2914 /* declare: bool _notBreakFlag = true */
2916 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2917 slang_generate_declaration(A
, top
->locals
, condDecl
,
2918 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2921 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2923 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2924 outerDo
->type
= SLANG_OPER_DO
;
2925 slang_operation_add_children(outerDo
, 2);
2929 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2930 innerDo
->type
= SLANG_OPER_DO
;
2931 slang_operation_add_children(innerDo
, 2);
2933 /* copy original do-loop body into inner do-loop's body */
2934 innerBody
= slang_oper_child(innerDo
, 0);
2935 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2936 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2938 /* inner do-loop's condition is constant/false */
2940 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2941 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2945 /* _notBreakFlag && LOOPCOND */
2947 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2948 cond
->type
= SLANG_OPER_LOGICALAND
;
2949 slang_operation_add_children(cond
, 2);
2951 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2952 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2955 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2956 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2961 /* Finally, in innerBody,
2962 * replace "break" with "_notBreakFlag = 0; break"
2963 * replace "continue" with "break"
2965 replace_break_and_cont(A
, innerBody
);
2967 /*slang_print_tree(top, 0);*/
2969 return _slang_gen_operation(A
, top
);
2974 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2976 static slang_ir_node
*
2977 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2981 * body code (child[0])
2983 * BREAK if !expr (child[1])
2985 slang_ir_node
*loop
;
2986 GLboolean isConst
, constTrue
;
2988 if (!A
->EmitContReturn
) {
2989 /* We don't want to emit CONT instructions. If this do-loop has
2990 * a continue, translate it away.
2992 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2993 return _slang_gen_do_without_continue(A
, oper
);
2997 /* type-check expression */
2998 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2999 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
3003 loop
= new_loop(NULL
);
3005 /* save loop state */
3006 push_loop(A
, oper
, loop
);
3009 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3011 /* Check if loop condition is a constant */
3012 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3013 if (isConst
&& constTrue
) {
3014 /* do { } while(1) ==> no conditional break */
3015 loop
->Children
[1] = NULL
; /* no tail code */
3019 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3020 loop
->Children
[1] = new_break_if_true(A
, cond
);
3023 /* XXX we should do infinite loop detection, as above */
3025 /* restore loop state */
3033 * Recursively count the number of operations rooted at 'oper'.
3034 * This gives some kind of indication of the size/complexity of an operation.
3037 sizeof_operation(const slang_operation
*oper
)
3040 GLuint count
= 1; /* me */
3042 for (i
= 0; i
< oper
->num_children
; i
++) {
3043 count
+= sizeof_operation(&oper
->children
[i
]);
3054 * Determine if a for-loop can be unrolled.
3055 * At this time, only a rather narrow class of for loops can be unrolled.
3056 * See code for details.
3057 * When a loop can't be unrolled because it's too large we'll emit a
3058 * message to the log.
3061 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3065 const char *varName
;
3068 if (oper
->type
!= SLANG_OPER_FOR
)
3071 assert(oper
->num_children
== 4);
3073 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3076 /* children[0] must be either "int i=constant" or "i=constant" */
3077 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3078 slang_variable
*var
;
3080 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3083 varId
= oper
->children
[0].children
[0].a_id
;
3085 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3089 if (!var
->initializer
)
3091 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3093 start
= (GLint
) var
->initializer
->literal
[0];
3095 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3096 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3098 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3100 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3103 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3105 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3111 /* children[1] must be "i<constant" */
3112 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3114 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3116 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3118 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3121 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3123 /* children[2] must be "i++" or "++i" */
3124 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3125 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3127 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3130 /* make sure the same variable name is used in all places */
3131 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3132 (oper
->children
[2].children
[0].a_id
!= varId
))
3135 varName
= (const char *) varId
;
3137 /* children[3], the loop body, can't be too large */
3138 bodySize
= sizeof_operation(&oper
->children
[3]);
3139 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3140 slang_info_log_print(A
->log
,
3141 "Note: 'for (%s ... )' body is too large/complex"
3148 return GL_FALSE
; /* degenerate case */
3150 if ((GLuint
)(end
- start
) > MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3151 slang_info_log_print(A
->log
,
3152 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3153 " many iterations to unroll",
3154 varName
, start
, varName
, end
, varName
);
3158 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3159 slang_info_log_print(A
->log
,
3160 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3161 " too much code to unroll",
3162 varName
, start
, varName
, end
, varName
);
3166 return GL_TRUE
; /* we can unroll the loop */
3171 * Unroll a for-loop.
3172 * First we determine the number of iterations to unroll.
3173 * Then for each iteration:
3174 * make a copy of the loop body
3175 * replace instances of the loop variable with the current iteration value
3176 * generate IR code for the body
3177 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3179 static slang_ir_node
*
3180 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3182 GLint start
, end
, iter
;
3183 slang_ir_node
*n
, *root
= NULL
;
3186 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3187 /* for (int i=0; ... */
3188 slang_variable
*var
;
3190 varId
= oper
->children
[0].children
[0].a_id
;
3191 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3194 start
= (GLint
) var
->initializer
->literal
[0];
3198 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3199 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3202 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3204 for (iter
= start
; iter
< end
; iter
++) {
3205 slang_operation
*body
;
3207 /* make a copy of the loop body */
3208 body
= slang_operation_new(1);
3212 if (!slang_operation_copy(body
, &oper
->children
[3]))
3215 /* in body, replace instances of 'varId' with literal 'iter' */
3217 slang_variable
*oldVar
;
3218 slang_operation
*newOper
;
3220 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3222 /* undeclared loop variable */
3223 slang_operation_delete(body
);
3227 newOper
= slang_operation_new(1);
3228 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3229 newOper
->literal_size
= 1;
3230 newOper
->literal
[0] = (GLfloat
) iter
;
3232 /* replace instances of the loop variable with newOper */
3233 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3236 /* do IR codegen for body */
3237 n
= _slang_gen_operation(A
, body
);
3241 root
= new_seq(root
, n
);
3243 slang_operation_delete(body
);
3251 * Replace 'continue' statement with 'break' inside a for-loop.
3252 * This is a recursive helper function used by _slang_gen_for_without_continue().
3255 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3257 switch (oper
->type
) {
3258 case SLANG_OPER_CONTINUE
:
3259 oper
->type
= SLANG_OPER_BREAK
;
3261 case SLANG_OPER_FOR
:
3263 case SLANG_OPER_WHILE
:
3264 /* stop upon finding a nested loop */
3270 for (i
= 0; i
< oper
->num_children
; i
++) {
3271 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3279 * Transform a for-loop so that continue statements are converted to breaks.
3280 * Then do normal IR code generation.
3284 * for (INIT; LOOPCOND; INCR) {
3295 * bool _condFlag = 1;
3296 * for (INIT; _condFlag; ) {
3297 * for ( ; _condFlag = LOOPCOND; INCR) {
3309 static slang_ir_node
*
3310 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3312 slang_operation
*top
;
3313 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3314 slang_operation
*lhs
, *rhs
;
3316 assert(oper
->type
== SLANG_OPER_FOR
);
3318 top
= slang_operation_new(1);
3319 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3320 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3321 slang_operation_add_children(top
, 2);
3323 /* declare: bool _condFlag = true */
3325 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3326 slang_generate_declaration(A
, top
->locals
, condDecl
,
3327 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3330 /* build outer loop: for (INIT; _condFlag; ) { */
3331 outerFor
= slang_oper_child(top
, 1);
3332 outerFor
->type
= SLANG_OPER_FOR
;
3333 slang_operation_add_children(outerFor
, 4);
3335 init
= slang_oper_child(outerFor
, 0);
3336 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3338 cond
= slang_oper_child(outerFor
, 1);
3339 cond
->type
= SLANG_OPER_IDENTIFIER
;
3340 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3342 incr
= slang_oper_child(outerFor
, 2);
3343 incr
->type
= SLANG_OPER_VOID
;
3345 /* body of the outer loop */
3347 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3349 slang_operation_add_children(block
, 2);
3350 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3352 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3354 innerFor
= slang_oper_child(block
, 0);
3356 /* make copy of orig loop */
3357 slang_operation_copy(innerFor
, oper
);
3358 assert(innerFor
->type
== SLANG_OPER_FOR
);
3359 innerFor
->locals
->outer_scope
= block
->locals
;
3361 init
= slang_oper_child(innerFor
, 0);
3362 init
->type
= SLANG_OPER_VOID
; /* leak? */
3364 cond
= slang_oper_child(innerFor
, 1);
3365 slang_operation_destruct(cond
);
3366 cond
->type
= SLANG_OPER_ASSIGN
;
3367 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3368 slang_operation_add_children(cond
, 2);
3370 lhs
= slang_oper_child(cond
, 0);
3371 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3372 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3374 rhs
= slang_oper_child(cond
, 1);
3375 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3378 /* if (_condFlag) INCR; */
3380 slang_operation
*ifop
= slang_oper_child(block
, 1);
3381 ifop
->type
= SLANG_OPER_IF
;
3382 slang_operation_add_children(ifop
, 2);
3384 /* re-use cond node build above */
3385 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3387 /* incr node from original for-loop operation */
3388 slang_operation_copy(slang_oper_child(ifop
, 1),
3389 slang_oper_child(oper
, 2));
3392 /* finally, replace "continue" with "break" in the inner for-loop */
3393 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3396 return _slang_gen_operation(A
, top
);
3402 * Generate IR for a for-loop. Unrolling will be done when possible.
3404 static slang_ir_node
*
3405 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3409 if (!A
->EmitContReturn
) {
3410 /* We don't want to emit CONT instructions. If this for-loop has
3411 * a continue, translate it away.
3413 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3414 return _slang_gen_for_without_continue(A
, oper
);
3418 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3420 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3425 assert(oper
->type
== SLANG_OPER_FOR
);
3427 /* conventional for-loop code generation */
3430 * init code (child[0])
3432 * BREAK if !expr (child[1])
3433 * body code (child[3])
3435 * incr code (child[2]) // XXX continue here
3437 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3438 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3439 loop
= new_loop(NULL
);
3441 /* save loop state */
3442 push_loop(A
, oper
, loop
);
3444 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3445 breakIf
= new_break_if_true(A
, cond
);
3446 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3447 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3449 loop
->Children
[0] = new_seq(breakIf
, body
);
3450 loop
->Children
[1] = incr
; /* tail code */
3452 /* restore loop state */
3455 return new_seq(init
, loop
);
3460 static slang_ir_node
*
3461 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3463 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3465 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3466 loopNode
= current_loop_ir(A
);
3468 assert(loopNode
->Opcode
== IR_LOOP
);
3470 cont
= new_node0(IR_CONT
);
3472 cont
->Parent
= loopNode
;
3473 /* insert this node at head of linked list of cont/break instructions */
3474 cont
->List
= loopNode
->List
;
3475 loopNode
->List
= cont
;
3478 n
= new_seq(incr
, cont
);
3484 * Determine if the given operation is of a specific type.
3487 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3489 if (oper
->type
== type
)
3491 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3492 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3493 oper
->num_children
== 1)
3494 return is_operation_type(&oper
->children
[0], type
);
3501 * Generate IR tree for an if/then/else conditional using high-level
3502 * IR_IF instruction.
3504 static slang_ir_node
*
3505 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3508 * eval expr (child[0])
3515 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3516 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3517 GLboolean isConst
, constTrue
;
3519 /* type-check expression */
3520 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3521 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3525 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3526 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3530 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3534 return _slang_gen_operation(A
, &oper
->children
[1]);
3537 /* if (false) ... */
3538 return _slang_gen_operation(A
, &oper
->children
[2]);
3542 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3543 cond
= new_cond(cond
);
3545 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3546 && !haveElseClause
) {
3547 /* Special case: generate a conditional break */
3548 ifBody
= new_break_if_true(A
, cond
);
3551 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3553 && current_loop_oper(A
)
3554 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3555 /* Special case: generate a conditional continue */
3556 ifBody
= new_cont_if_true(A
, cond
);
3561 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3563 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3566 ifNode
= new_if(cond
, ifBody
, elseBody
);
3573 static slang_ir_node
*
3574 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3578 assert(oper
->type
== SLANG_OPER_NOT
);
3580 /* type-check expression */
3581 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3582 slang_info_log_error(A
->log
,
3583 "scalar/boolean expression expected for '!'");
3587 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3595 static slang_ir_node
*
3596 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3598 slang_ir_node
*n1
, *n2
;
3600 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3602 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3603 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3604 slang_info_log_error(A
->log
,
3605 "scalar/boolean expressions expected for '^^'");
3609 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3612 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3615 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3620 * Generate IR node for storage of a temporary of given size.
3622 static slang_ir_node
*
3623 _slang_gen_temporary(GLint size
)
3625 slang_ir_storage
*store
;
3626 slang_ir_node
*n
= NULL
;
3628 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3630 n
= new_node0(IR_VAR_DECL
);
3643 * Generate program constants for an array.
3644 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3645 * This will allocate and initialize three vector constants, storing
3646 * the array in constant memory, not temporaries like a non-const array.
3647 * This can also be used for uniform array initializers.
3648 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3651 make_constant_array(slang_assemble_ctx
*A
,
3652 slang_variable
*var
,
3653 slang_operation
*initializer
)
3655 struct gl_program
*prog
= A
->program
;
3656 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3657 const char *varName
= (char *) var
->a_name
;
3658 const GLuint numElements
= initializer
->num_children
;
3664 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3666 size
= var
->store
->Size
;
3668 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3669 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3670 assert(initializer
->type
== SLANG_OPER_CALL
);
3671 assert(initializer
->array_constructor
);
3673 values
= (GLfloat
*) malloc(numElements
* 4 * sizeof(GLfloat
));
3675 /* convert constructor params into ordinary floats */
3676 for (i
= 0; i
< numElements
; i
++) {
3677 const slang_operation
*op
= &initializer
->children
[i
];
3678 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3679 /* unsupported type for this optimization */
3683 for (j
= 0; j
< op
->literal_size
; j
++) {
3684 values
[i
* 4 + j
] = op
->literal
[j
];
3686 for ( ; j
< 4; j
++) {
3687 values
[i
* 4 + j
] = 0.0f
;
3691 /* slightly different paths for constants vs. uniforms */
3692 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3693 var
->store
->File
= PROGRAM_UNIFORM
;
3694 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3695 size
, datatype
, values
);
3698 var
->store
->File
= PROGRAM_CONSTANT
;
3699 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3702 assert(var
->store
->Size
== size
);
3712 * Generate IR node for allocating/declaring a variable (either a local or
3714 * Generally, this involves allocating an slang_ir_storage instance for the
3715 * variable, choosing a register file (temporary, constant, etc).
3716 * For ordinary variables we do not yet allocate storage though. We do that
3717 * when we find the first actual use of the variable to avoid allocating temp
3718 * regs that will never get used.
3719 * At this time, uniforms are always allocated space in this function.
3721 * \param initializer Optional initializer expression for the variable.
3723 static slang_ir_node
*
3724 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3725 slang_operation
*initializer
)
3727 const char *varName
= (const char *) var
->a_name
;
3728 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3729 slang_ir_node
*varDecl
, *n
;
3730 slang_ir_storage
*store
;
3731 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3732 gl_register_file file
;
3734 /*assert(!var->declared);*/
3735 var
->declared
= GL_TRUE
;
3737 /* determine GPU register file for simple cases */
3738 if (is_sampler_type(&var
->type
)) {
3739 file
= PROGRAM_SAMPLER
;
3741 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3742 file
= PROGRAM_UNIFORM
;
3745 file
= PROGRAM_TEMPORARY
;
3748 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3750 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3754 arrayLen
= _slang_array_length(var
);
3755 totalSize
= _slang_array_size(size
, arrayLen
);
3757 /* Allocate IR node for the declaration */
3758 varDecl
= new_node0(IR_VAR_DECL
);
3762 /* Allocate slang_ir_storage for this variable if needed.
3763 * Note that we may not actually allocate a constant or temporary register
3767 GLint index
= -7; /* TBD / unknown */
3768 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3770 return NULL
; /* out of memory */
3773 /* set the IR node's Var and Store pointers */
3775 varDecl
->Store
= var
->store
;
3780 /* if there's an initializer, generate IR for the expression */
3782 slang_ir_node
*varRef
, *init
;
3784 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3785 /* if the variable is const, the initializer must be a const
3786 * expression as well.
3789 if (!_slang_is_constant_expr(initializer
)) {
3790 slang_info_log_error(A
->log
,
3791 "initializer for %s not constant", varName
);
3797 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
&&
3798 !A
->allow_uniform_initializers
) {
3799 slang_info_log_error(A
->log
,
3800 "initializer for uniform %s not allowed",
3805 /* IR for the variable we're initializing */
3806 varRef
= new_var(A
, var
);
3808 slang_info_log_error(A
->log
, "out of memory");
3812 /* constant-folding, etc here */
3813 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3815 /* look for simple constant-valued variables and uniforms */
3816 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3817 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3819 if (initializer
->type
== SLANG_OPER_CALL
&&
3820 initializer
->array_constructor
) {
3821 /* array initializer */
3822 if (make_constant_array(A
, var
, initializer
))
3825 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3826 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3827 /* simple float/vector initializer */
3828 if (store
->File
== PROGRAM_UNIFORM
) {
3829 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3831 totalSize
, datatype
,
3832 initializer
->literal
);
3833 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3838 store
->File
= PROGRAM_CONSTANT
;
3839 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3841 initializer
->literal
,
3843 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3850 /* IR for initializer */
3851 init
= _slang_gen_operation(A
, initializer
);
3855 /* XXX remove this when type checking is added above */
3856 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3857 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3861 /* assign RHS to LHS */
3862 n
= new_node2(IR_COPY
, varRef
, init
);
3863 n
= new_seq(varDecl
, n
);
3866 /* no initializer */
3870 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3871 /* always need to allocate storage for uniforms at this point */
3872 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3873 totalSize
, datatype
, NULL
);
3874 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3878 printf("%s var %p %s store=%p index=%d size=%d\n",
3879 __FUNCTION__
, (void *) var
, (char *) varName
,
3880 (void *) store
, store
->Index
, store
->Size
);
3888 * Generate code for a selection expression: b ? x : y
3889 * XXX In some cases we could implement a selection expression
3890 * with an LRP instruction (use the boolean as the interpolant).
3891 * Otherwise, we use an IF/ELSE/ENDIF construct.
3893 static slang_ir_node
*
3894 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3896 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3897 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3898 slang_typeinfo type0
, type1
, type2
;
3899 int size
, isBool
, isEqual
;
3901 assert(oper
->type
== SLANG_OPER_SELECT
);
3902 assert(oper
->num_children
== 3);
3904 /* type of children[0] must be boolean */
3905 slang_typeinfo_construct(&type0
);
3906 typeof_operation(A
, &oper
->children
[0], &type0
);
3907 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3908 slang_typeinfo_destruct(&type0
);
3910 slang_info_log_error(A
->log
, "selector type is not boolean");
3914 slang_typeinfo_construct(&type1
);
3915 slang_typeinfo_construct(&type2
);
3916 typeof_operation(A
, &oper
->children
[1], &type1
);
3917 typeof_operation(A
, &oper
->children
[2], &type2
);
3918 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3919 slang_typeinfo_destruct(&type1
);
3920 slang_typeinfo_destruct(&type2
);
3922 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3926 /* size of x or y's type */
3927 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3931 tmpDecl
= _slang_gen_temporary(size
);
3933 /* the condition (child 0) */
3934 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3935 cond
= new_cond(cond
);
3937 /* if-true body (child 1) */
3938 tmpVar
= new_node0(IR_VAR
);
3939 tmpVar
->Store
= tmpDecl
->Store
;
3940 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3941 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3943 /* if-false body (child 2) */
3944 tmpVar
= new_node0(IR_VAR
);
3945 tmpVar
->Store
= tmpDecl
->Store
;
3946 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3947 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3949 ifNode
= new_if(cond
, trueNode
, falseNode
);
3952 tmpVar
= new_node0(IR_VAR
);
3953 tmpVar
->Store
= tmpDecl
->Store
;
3955 tree
= new_seq(ifNode
, tmpVar
);
3956 tree
= new_seq(tmpDecl
, tree
);
3958 /*_slang_print_ir_tree(tree, 10);*/
3964 * Generate code for &&.
3966 static slang_ir_node
*
3967 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3969 /* rewrite "a && b" as "a ? b : false" */
3970 slang_operation
*select
;
3973 select
= slang_operation_new(1);
3974 select
->type
= SLANG_OPER_SELECT
;
3975 slang_operation_add_children(select
, 3);
3977 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3978 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3979 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3981 n
= _slang_gen_select(A
, select
);
3987 * Generate code for ||.
3989 static slang_ir_node
*
3990 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3992 /* rewrite "a || b" as "a ? true : b" */
3993 slang_operation
*select
;
3996 select
= slang_operation_new(1);
3997 select
->type
= SLANG_OPER_SELECT
;
3998 slang_operation_add_children(select
, 3);
4000 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
4001 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
4002 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
4004 n
= _slang_gen_select(A
, select
);
4010 * Generate IR tree for a return statement.
4012 static slang_ir_node
*
4013 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4015 assert(oper
->type
== SLANG_OPER_RETURN
);
4016 return new_return(A
->curFuncEndLabel
);
4022 * Determine if the given operation/expression is const-valued.
4025 _slang_is_constant_expr(const slang_operation
*oper
)
4027 slang_variable
*var
;
4030 switch (oper
->type
) {
4031 case SLANG_OPER_IDENTIFIER
:
4032 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4033 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4037 for (i
= 0; i
< oper
->num_children
; i
++) {
4038 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4048 * Check if an assignment of type t1 to t0 is legal.
4049 * XXX more cases needed.
4052 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4053 slang_operation
*op0
,
4054 slang_operation
*op1
)
4056 slang_typeinfo t0
, t1
;
4059 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4060 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4064 slang_typeinfo_construct(&t0
);
4065 typeof_operation(A
, op0
, &t0
);
4067 slang_typeinfo_construct(&t1
);
4068 typeof_operation(A
, op1
, &t1
);
4070 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4071 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4075 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4080 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4081 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4082 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4085 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4086 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4089 #if 0 /* not used just yet - causes problems elsewhere */
4090 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4091 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4095 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4096 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4099 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4100 t1
.spec
.type
== SLANG_SPEC_INT
)
4108 * Generate IR tree for a local variable declaration.
4109 * Basically do some error checking and call _slang_gen_var_decl().
4111 static slang_ir_node
*
4112 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4114 const char *varName
= (char *) oper
->a_id
;
4115 slang_variable
*var
;
4116 slang_ir_node
*varDecl
;
4117 slang_operation
*initializer
;
4119 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4120 assert(oper
->num_children
<= 1);
4123 /* lookup the variable by name */
4124 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4126 return NULL
; /* "shouldn't happen" */
4128 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4129 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4130 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4131 /* can't declare attribute/uniform vars inside functions */
4132 slang_info_log_error(A
->log
,
4133 "local variable '%s' cannot be an attribute/uniform/varying",
4140 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4145 /* check if the var has an initializer */
4146 if (oper
->num_children
> 0) {
4147 assert(oper
->num_children
== 1);
4148 initializer
= &oper
->children
[0];
4150 else if (var
->initializer
) {
4151 initializer
= var
->initializer
;
4158 /* check/compare var type and initializer type */
4159 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4160 slang_info_log_error(A
->log
, "incompatible types in assignment");
4165 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4166 slang_info_log_error(A
->log
,
4167 "const-qualified variable '%s' requires initializer",
4173 /* Generate IR node */
4174 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4183 * Generate IR tree for a reference to a variable (such as in an expression).
4184 * This is different from a variable declaration.
4186 static slang_ir_node
*
4187 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4189 /* If there's a variable associated with this oper (from inlining)
4190 * use it. Otherwise, use the oper's var id.
4192 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4193 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4195 if (!var
|| !var
->declared
) {
4196 /* Geometry shaders set gl_VerticesIn at link time
4197 * so we need to wait with resolving this variable
4199 if (A
->program
->Target
== MESA_GEOMETRY_PROGRAM
&&
4200 !strcmp((char*)name
, "gl_VerticesIn") ){
4201 A
->UnresolvedRefs
= GL_TRUE
;
4204 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4207 n
= new_var(A
, var
);
4214 * Return the number of components actually named by the swizzle.
4215 * Recall that swizzles may have undefined/don't-care values.
4218 swizzle_size(GLuint swizzle
)
4221 for (i
= 0; i
< 4; i
++) {
4222 GLuint swz
= GET_SWZ(swizzle
, i
);
4229 static slang_ir_node
*
4230 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4232 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4236 n
->Store
= _slang_new_ir_storage_relative(0,
4237 swizzle_size(swizzle
),
4240 n
->Store
->Swizzle
= swizzle
;
4247 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4249 while (store
->Parent
)
4250 store
= store
->Parent
;
4252 if (!(store
->File
== PROGRAM_OUTPUT
||
4253 store
->File
== PROGRAM_TEMPORARY
||
4254 (store
->File
== PROGRAM_VARYING
&&
4255 (A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
||
4256 A
->program
->Target
== MESA_GEOMETRY_PROGRAM
)))) {
4266 * Walk up an IR storage path to compute the final swizzle.
4267 * This is used when we find an expression such as "foo.xz.yx".
4270 root_swizzle(const slang_ir_storage
*st
)
4272 GLuint swizzle
= st
->Swizzle
;
4273 while (st
->Parent
) {
4275 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4282 * Generate IR tree for an assignment (=).
4284 static slang_ir_node
*
4285 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4287 slang_operation
*pred
= NULL
;
4288 slang_ir_node
*n
= NULL
;
4290 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4291 /* Check that var is writeable */
4292 const char *varName
= (char *) oper
->children
[0].a_id
;
4294 = _slang_variable_locate(oper
->children
[0].locals
,
4295 oper
->children
[0].a_id
, GL_TRUE
);
4297 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
4301 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4302 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4303 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4304 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4305 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4306 slang_info_log_error(A
->log
,
4307 "illegal assignment to read-only variable '%s'",
4312 /* check if we need to predicate this assignment based on __notRetFlag */
4313 if ((var
->is_global
||
4314 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4315 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4316 /* create predicate, used below */
4317 pred
= slang_operation_new(1);
4318 pred
->type
= SLANG_OPER_IDENTIFIER
;
4319 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4320 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4324 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4325 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4326 /* Special case of: x = f(a, b)
4327 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4329 * XXX this could be even more effective if we could accomodate
4330 * cases such as "v.x = f();" - would help with typical vertex
4333 n
= _slang_gen_function_call_name(A
,
4334 (const char *) oper
->children
[1].a_id
,
4335 &oper
->children
[1], &oper
->children
[0]);
4338 slang_ir_node
*lhs
, *rhs
;
4340 /* lhs and rhs type checking */
4341 if (!_slang_assignment_compatible(A
,
4343 &oper
->children
[1])) {
4344 slang_info_log_error(A
->log
, "incompatible types in assignment");
4348 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4354 slang_info_log_error(A
->log
,
4355 "invalid left hand side for assignment");
4359 /* check that lhs is writable */
4360 if (!is_store_writable(A
, lhs
->Store
)) {
4361 slang_info_log_error(A
->log
,
4362 "illegal assignment to read-only l-value");
4366 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4368 /* convert lhs swizzle into writemask */
4369 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4370 GLuint writemask
, newSwizzle
= 0x0;
4371 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4372 /* Non-simple writemask, need to swizzle right hand side in
4373 * order to put components into the right place.
4375 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4377 n
= new_node2(IR_COPY
, lhs
, rhs
);
4385 /* predicate the assignment code on __notRetFlag */
4386 slang_ir_node
*top
, *cond
;
4388 cond
= _slang_gen_operation(A
, pred
);
4389 top
= new_if(cond
, n
, NULL
);
4397 * Generate IR tree for referencing a field in a struct (or basic vector type)
4399 static slang_ir_node
*
4400 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4404 /* type of struct */
4405 slang_typeinfo_construct(&ti
);
4406 typeof_operation(A
, &oper
->children
[0], &ti
);
4408 if (_slang_type_is_vector(ti
.spec
.type
)) {
4409 /* the field should be a swizzle */
4410 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4414 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4415 slang_info_log_error(A
->log
, "Bad swizzle");
4418 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4423 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4424 /* create new parent node with swizzle */
4426 n
= _slang_gen_swizzle(n
, swizzle
);
4429 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4430 || ti
.spec
.type
== SLANG_SPEC_INT
4431 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4432 const GLuint rows
= 1;
4436 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4437 slang_info_log_error(A
->log
, "Bad swizzle");
4439 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4443 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4444 /* create new parent node with swizzle */
4445 n
= _slang_gen_swizzle(n
, swizzle
);
4449 /* the field is a structure member (base.field) */
4450 /* oper->children[0] is the base */
4451 /* oper->a_id is the field name */
4452 slang_ir_node
*base
, *n
;
4453 slang_typeinfo field_ti
;
4454 GLint fieldSize
, fieldOffset
= -1;
4457 slang_typeinfo_construct(&field_ti
);
4458 typeof_operation(A
, oper
, &field_ti
);
4460 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4462 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4464 if (fieldSize
== 0 || fieldOffset
< 0) {
4465 const char *structName
;
4466 if (ti
.spec
._struct
)
4467 structName
= (char *) ti
.spec
._struct
->a_name
;
4469 structName
= "unknown";
4470 slang_info_log_error(A
->log
,
4471 "\"%s\" is not a member of struct \"%s\"",
4472 (char *) oper
->a_id
, structName
);
4475 assert(fieldSize
>= 0);
4477 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4479 /* error msg should have already been logged */
4483 n
= new_node1(IR_FIELD
, base
);
4487 n
->Field
= (char *) oper
->a_id
;
4489 /* Store the field's offset in storage->Index */
4490 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4500 * Gen code for array indexing.
4502 static slang_ir_node
*
4503 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4505 slang_typeinfo array_ti
;
4507 /* get array's type info */
4508 slang_typeinfo_construct(&array_ti
);
4509 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4511 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4512 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4513 /* translate the index into a swizzle/writemask: "v.x=p" */
4514 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4518 index
= (GLint
) oper
->children
[1].literal
[0];
4519 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4520 index
>= (GLint
) max
) {
4522 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4523 printf("type = %d\n", oper
->children
[1].type
);
4524 printf("index = %d, max = %d\n", index
, max
);
4525 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4526 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4533 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4535 /* use swizzle to access the element */
4536 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4540 n
= _slang_gen_swizzle(n
, swizzle
);
4545 /* conventional array */
4546 slang_typeinfo elem_ti
;
4547 slang_ir_node
*elem
, *array
, *index
;
4548 GLint elemSize
, arrayLen
;
4550 /* size of array element */
4551 slang_typeinfo_construct(&elem_ti
);
4552 typeof_operation(A
, oper
, &elem_ti
);
4553 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4555 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4556 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4558 arrayLen
= array_ti
.array_len
;
4560 slang_typeinfo_destruct(&array_ti
);
4561 slang_typeinfo_destruct(&elem_ti
);
4563 if (elemSize
<= 0) {
4564 /* unknown var or type */
4565 slang_info_log_error(A
->log
, "Undefined variable or type");
4569 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4570 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4571 if (array
&& index
) {
4573 GLint constIndex
= -1;
4574 if (index
->Opcode
== IR_FLOAT
) {
4575 constIndex
= (int) index
->Value
[0];
4576 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4577 slang_info_log_error(A
->log
,
4578 "Array index out of bounds (index=%d size=%d)",
4579 constIndex
, arrayLen
);
4580 _slang_free_ir_tree(array
);
4581 _slang_free_ir_tree(index
);
4586 if (!array
->Store
) {
4587 slang_info_log_error(A
->log
, "Invalid array");
4591 elem
= new_node2(IR_ELEMENT
, array
, index
);
4593 /* The storage info here will be updated during code emit */
4594 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4595 array
->Store
->Index
,
4597 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4601 _slang_free_ir_tree(array
);
4602 _slang_free_ir_tree(index
);
4609 static slang_ir_node
*
4610 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4611 slang_ir_opcode opcode
)
4613 slang_typeinfo t0
, t1
;
4616 slang_typeinfo_construct(&t0
);
4617 typeof_operation(A
, &oper
->children
[0], &t0
);
4619 slang_typeinfo_construct(&t1
);
4620 typeof_operation(A
, &oper
->children
[0], &t1
);
4622 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4623 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4624 slang_info_log_error(A
->log
, "Illegal array comparison");
4628 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4629 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4630 /* <, <=, >, >= can only be used with scalars */
4631 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4632 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4633 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4634 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4635 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4640 n
= new_node2(opcode
,
4641 _slang_gen_operation(A
, &oper
->children
[0]),
4642 _slang_gen_operation(A
, &oper
->children
[1]));
4644 /* result is a bool (size 1) */
4645 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4653 print_vars(slang_variable_scope
*s
)
4657 for (i
= 0; i
< s
->num_variables
; i
++) {
4659 (char*) s
->variables
[i
]->a_name
,
4660 s
->variables
[i
]->declared
);
4670 _slang_undeclare_vars(slang_variable_scope
*locals
)
4672 if (locals
->num_variables
> 0) {
4674 for (i
= 0; i
< locals
->num_variables
; i
++) {
4675 slang_variable
*v
= locals
->variables
[i
];
4676 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4677 v
->declared
= GL_FALSE
;
4685 * Generate IR tree for a slang_operation (AST node)
4687 static slang_ir_node
*
4688 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4690 switch (oper
->type
) {
4691 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4695 _slang_push_var_table(A
->vartable
);
4697 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4698 n
= _slang_gen_operation(A
, oper
);
4699 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4701 _slang_pop_var_table(A
->vartable
);
4703 /*_slang_undeclare_vars(oper->locals);*/
4704 /*print_vars(oper->locals);*/
4707 n
= new_node1(IR_SCOPE
, n
);
4712 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4713 /* list of operations */
4714 if (oper
->num_children
> 0)
4716 slang_ir_node
*n
, *tree
= NULL
;
4719 for (i
= 0; i
< oper
->num_children
; i
++) {
4720 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4722 _slang_free_ir_tree(tree
);
4723 return NULL
; /* error must have occured */
4725 tree
= new_seq(tree
, n
);
4731 return new_node0(IR_NOP
);
4734 case SLANG_OPER_EXPRESSION
:
4735 return _slang_gen_operation(A
, &oper
->children
[0]);
4737 case SLANG_OPER_FOR
:
4738 return _slang_gen_for(A
, oper
);
4740 return _slang_gen_do(A
, oper
);
4741 case SLANG_OPER_WHILE
:
4742 return _slang_gen_while(A
, oper
);
4743 case SLANG_OPER_BREAK
:
4744 if (!current_loop_oper(A
)) {
4745 slang_info_log_error(A
->log
, "'break' not in loop");
4748 return new_break(current_loop_ir(A
));
4749 case SLANG_OPER_CONTINUE
:
4750 if (!current_loop_oper(A
)) {
4751 slang_info_log_error(A
->log
, "'continue' not in loop");
4754 return _slang_gen_continue(A
, oper
);
4755 case SLANG_OPER_DISCARD
:
4756 return new_node0(IR_KILL
);
4758 case SLANG_OPER_EQUAL
:
4759 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4760 case SLANG_OPER_NOTEQUAL
:
4761 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4762 case SLANG_OPER_GREATER
:
4763 return _slang_gen_compare(A
, oper
, IR_SGT
);
4764 case SLANG_OPER_LESS
:
4765 return _slang_gen_compare(A
, oper
, IR_SLT
);
4766 case SLANG_OPER_GREATEREQUAL
:
4767 return _slang_gen_compare(A
, oper
, IR_SGE
);
4768 case SLANG_OPER_LESSEQUAL
:
4769 return _slang_gen_compare(A
, oper
, IR_SLE
);
4770 case SLANG_OPER_ADD
:
4773 assert(oper
->num_children
== 2);
4774 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4777 case SLANG_OPER_SUBTRACT
:
4780 assert(oper
->num_children
== 2);
4781 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4784 case SLANG_OPER_MULTIPLY
:
4787 assert(oper
->num_children
== 2);
4788 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4791 case SLANG_OPER_DIVIDE
:
4794 assert(oper
->num_children
== 2);
4795 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4798 case SLANG_OPER_MINUS
:
4801 assert(oper
->num_children
== 1);
4802 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4805 case SLANG_OPER_PLUS
:
4806 /* +expr --> do nothing */
4807 return _slang_gen_operation(A
, &oper
->children
[0]);
4808 case SLANG_OPER_VARIABLE_DECL
:
4809 return _slang_gen_declaration(A
, oper
);
4810 case SLANG_OPER_ASSIGN
:
4811 return _slang_gen_assignment(A
, oper
);
4812 case SLANG_OPER_ADDASSIGN
:
4815 assert(oper
->num_children
== 2);
4816 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4819 case SLANG_OPER_SUBASSIGN
:
4822 assert(oper
->num_children
== 2);
4823 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4827 case SLANG_OPER_MULASSIGN
:
4830 assert(oper
->num_children
== 2);
4831 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4834 case SLANG_OPER_DIVASSIGN
:
4837 assert(oper
->num_children
== 2);
4838 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4841 case SLANG_OPER_LOGICALAND
:
4844 assert(oper
->num_children
== 2);
4845 n
= _slang_gen_logical_and(A
, oper
);
4848 case SLANG_OPER_LOGICALOR
:
4851 assert(oper
->num_children
== 2);
4852 n
= _slang_gen_logical_or(A
, oper
);
4855 case SLANG_OPER_LOGICALXOR
:
4856 return _slang_gen_xor(A
, oper
);
4857 case SLANG_OPER_NOT
:
4858 return _slang_gen_not(A
, oper
);
4859 case SLANG_OPER_SELECT
: /* b ? x : y */
4862 assert(oper
->num_children
== 3);
4863 n
= _slang_gen_select(A
, oper
);
4867 case SLANG_OPER_ASM
:
4868 return _slang_gen_asm(A
, oper
, NULL
);
4869 case SLANG_OPER_CALL
:
4870 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4872 case SLANG_OPER_METHOD
:
4873 return _slang_gen_method_call(A
, oper
);
4874 case SLANG_OPER_RETURN
:
4875 return _slang_gen_return(A
, oper
);
4876 case SLANG_OPER_RETURN_INLINED
:
4877 return _slang_gen_return(A
, oper
);
4878 case SLANG_OPER_LABEL
:
4879 return new_label(oper
->label
);
4880 case SLANG_OPER_IDENTIFIER
:
4881 return _slang_gen_variable(A
, oper
);
4883 return _slang_gen_if(A
, oper
);
4884 case SLANG_OPER_FIELD
:
4885 return _slang_gen_struct_field(A
, oper
);
4886 case SLANG_OPER_SUBSCRIPT
:
4887 return _slang_gen_array_element(A
, oper
);
4888 case SLANG_OPER_LITERAL_FLOAT
:
4890 case SLANG_OPER_LITERAL_INT
:
4892 case SLANG_OPER_LITERAL_BOOL
:
4893 return new_float_literal(oper
->literal
, oper
->literal_size
);
4895 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4898 assert(oper
->num_children
== 1);
4899 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4902 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4905 assert(oper
->num_children
== 1);
4906 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4909 case SLANG_OPER_PREINCREMENT
: /* ++var */
4912 assert(oper
->num_children
== 1);
4913 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4916 case SLANG_OPER_PREDECREMENT
: /* --var */
4919 assert(oper
->num_children
== 1);
4920 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4924 case SLANG_OPER_NON_INLINED_CALL
:
4925 case SLANG_OPER_SEQUENCE
:
4927 slang_ir_node
*tree
= NULL
;
4929 for (i
= 0; i
< oper
->num_children
; i
++) {
4930 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4931 tree
= new_seq(tree
, n
);
4933 tree
->Store
= n
->Store
;
4935 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4936 tree
= new_function_call(tree
, oper
->label
);
4941 case SLANG_OPER_NONE
:
4942 case SLANG_OPER_VOID
:
4943 /* returning NULL here would generate an error */
4944 return new_node0(IR_NOP
);
4947 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4949 return new_node0(IR_NOP
);
4957 * Check if the given type specifier is a rectangular texture sampler.
4960 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4962 while (spec
->_array
) {
4963 spec
= spec
->_array
;
4965 return spec
->type
== SLANG_SPEC_SAMPLER_RECT
||
4966 spec
->type
== SLANG_SPEC_SAMPLER_RECT_SHADOW
;
4972 * Called by compiler when a global variable has been parsed/compiled.
4973 * Here we examine the variable's type to determine what kind of register
4974 * storage will be used.
4976 * A uniform such as "gl_Position" will become the register specification
4977 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4978 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4980 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4981 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4982 * actual texture unit (as specified by the user calling glUniform1i()).
4985 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4986 slang_unit_type type
)
4988 GET_CURRENT_CONTEXT(ctx
);
4989 struct gl_program
*prog
= A
->program
;
4990 const char *varName
= (char *) var
->a_name
;
4991 GLboolean success
= GL_TRUE
;
4992 slang_ir_storage
*store
= NULL
;
4994 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4995 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4996 const GLint arrayLen
= _slang_array_length(var
);
4997 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4998 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
5000 var
->is_global
= GL_TRUE
;
5002 /* check for sampler2D arrays */
5003 if (texIndex
== -1 && var
->type
.specifier
._array
)
5004 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
5006 if (texIndex
!= -1) {
5007 /* This is a texture sampler variable...
5008 * store->File = PROGRAM_SAMPLER
5009 * store->Index = sampler number (0..7, typically)
5010 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
5012 if (var
->initializer
) {
5013 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5016 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5017 /* disallow rect samplers */
5018 if (ctx
->API
== API_OPENGLES2
&&
5019 is_rect_sampler_spec(&var
->type
.specifier
)) {
5020 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5024 (void) is_rect_sampler_spec
; /* silence warning */
5028 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5029 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5031 /* If we have a sampler array, then we need to allocate the
5032 * additional samplers to ensure we don't allocate them elsewhere.
5033 * We can't directly use _mesa_add_sampler() as that checks the
5034 * varName and gets a match, so we call _mesa_add_parameter()
5035 * directly and use the last sampler number from the call above.
5038 GLint a
= arrayLen
- 1;
5040 for (i
= 0; i
< a
; i
++) {
5041 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5042 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5043 varName
, 1, datatype
, &value
, NULL
, 0x0);
5047 if (dbg
) printf("SAMPLER ");
5049 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5050 /* Uniform variable */
5051 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5054 /* user-defined uniform */
5055 if (datatype
== GL_NONE
) {
5056 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5057 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5058 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5059 /* temporary work-around */
5060 GLenum datatype
= GL_FLOAT
;
5061 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5062 totalSize
, datatype
, NULL
);
5063 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5064 totalSize
, swizzle
);
5067 GLint a
= arrayLen
- 1;
5069 for (i
= 0; i
< a
; i
++) {
5070 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5071 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5072 varName
, 1, datatype
, &value
, NULL
, 0x0);
5076 /* XXX what we need to do is unroll the struct into its
5077 * basic types, creating a uniform variable for each.
5085 * Should produce uniforms:
5086 * "f.a" (GL_FLOAT_VEC3)
5087 * "f.b" (GL_FLOAT_VEC4)
5090 if (var
->initializer
) {
5091 slang_info_log_error(A
->log
,
5092 "unsupported initializer for uniform '%s'", varName
);
5097 slang_info_log_error(A
->log
,
5098 "invalid datatype for uniform variable %s",
5104 /* non-struct uniform */
5105 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5111 /* pre-defined uniform, like gl_ModelviewMatrix */
5112 /* We know it's a uniform, but don't allocate storage unless
5115 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5116 totalSize
, swizzle
);
5118 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5120 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5121 /* varyings must be float, vec or mat */
5122 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5123 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5124 slang_info_log_error(A
->log
,
5125 "varying '%s' must be float/vector/matrix",
5130 if (var
->initializer
) {
5131 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5137 /* user-defined varying */
5143 if (var
->type
.centroid
== SLANG_CENTROID
)
5144 flags
|= PROG_PARAM_BIT_CENTROID
;
5145 if (var
->type
.variant
== SLANG_INVARIANT
)
5146 flags
|= PROG_PARAM_BIT_INVARIANT
;
5148 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5149 totalSize
, GL_NONE
, flags
);
5150 swizzle
= _slang_var_swizzle(size
, 0);
5151 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5152 totalSize
, swizzle
);
5155 /* pre-defined varying, like gl_Color or gl_TexCoord */
5156 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5157 /* fragment program input */
5159 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5162 assert(index
< FRAG_ATTRIB_MAX
);
5163 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5165 } else if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5166 /* vertex program output */
5167 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5168 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5170 assert(index
< VERT_RESULT_MAX
);
5171 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5172 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5175 /* geometry program input */
5176 GLboolean is_array
= GL_FALSE
;
5178 GLint index
= _slang_input_index(varName
, MESA_GEOMETRY_PROGRAM
,
5179 &swizzle
, &is_array
);
5181 /* geometry program output */
5182 index
= _slang_output_index(varName
, MESA_GEOMETRY_PROGRAM
);
5183 swizzle
= _slang_var_swizzle(size
, 0);
5186 assert(index
< GEOM_RESULT_MAX
);
5188 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5192 /* assert(index < GEOM_ATTRIB_MAX); */
5194 store
= _slang_new_ir_storage_2d(PROGRAM_INPUT
, 0, index
,
5197 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5201 if (dbg
) printf("V/F ");
5203 if (dbg
) printf("VARYING ");
5205 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5208 /* attributes must be float, vec or mat */
5209 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5210 slang_info_log_error(A
->log
,
5211 "attribute '%s' must be float/vector/matrix",
5217 /* user-defined vertex attribute */
5218 const GLint attr
= -1; /* unknown */
5219 swizzle
= _slang_var_swizzle(size
, 0);
5220 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5221 size
, datatype
, attr
);
5223 index
= VERT_ATTRIB_GENERIC0
+ index
;
5226 /* pre-defined vertex attrib */
5227 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
, NULL
);
5230 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5231 if (dbg
) printf("ATTRIB ");
5233 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5234 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5235 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5236 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5238 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5239 } else if (type
== SLANG_UNIT_GEOMETRY_BUILTIN
) {
5241 GLint index
= _slang_input_index(varName
, MESA_GEOMETRY_PROGRAM
,
5242 &swizzle
, &is_array
);
5244 store
= _slang_new_ir_storage_2d(PROGRAM_INPUT
, 0, index
, size
, swizzle
);
5246 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5248 if (dbg
) printf("INPUT ");
5250 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5251 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5252 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5253 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5254 } else if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5255 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5256 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5257 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5258 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5260 GLint index
= _slang_output_index(varName
, MESA_GEOMETRY_PROGRAM
);
5261 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5262 assert(type
== SLANG_UNIT_GEOMETRY_BUILTIN
);
5263 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5265 if (dbg
) printf("OUTPUT ");
5267 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5268 /* pre-defined global constant, like gl_MaxLights */
5269 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5270 if (dbg
) printf("CONST ");
5273 /* ordinary variable (may be const) */
5276 /* IR node to declare the variable */
5277 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5279 /* emit GPU instructions */
5280 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5282 _slang_free_ir_tree(n
);
5285 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5286 store
? store
->Index
: -2);
5289 var
->store
= store
; /* save var's storage info */
5291 var
->declared
= GL_TRUE
;
5298 * Produce an IR tree from a function AST (fun->body).
5299 * Then call the code emitter to convert the IR tree into gl_program
5303 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5306 GLboolean success
= GL_TRUE
;
5308 if (strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5309 /* we only really generate code for main, all other functions get
5310 * inlined or codegen'd upon an actual call.
5313 /* do some basic error checking though */
5314 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5315 /* check that non-void functions actually return something */
5317 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5319 slang_info_log_error(A
->log
,
5320 "function \"%s\" has no return statement",
5321 (char *) fun
->header
.a_name
);
5323 "function \"%s\" has no return statement\n",
5324 (char *) fun
->header
.a_name
);
5329 return GL_TRUE
; /* not an error */
5333 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5334 slang_print_function(fun
, 1);
5337 /* should have been allocated earlier: */
5338 assert(A
->program
->Parameters
);
5339 assert(A
->program
->Varying
);
5340 assert(A
->vartable
);
5343 A
->UseReturnFlag
= GL_FALSE
;
5344 A
->CurFunction
= fun
;
5346 /* fold constant expressions, etc. */
5347 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5350 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5351 slang_print_function(fun
, 1);
5354 /* Create an end-of-function label */
5355 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5357 /* push new vartable scope */
5358 _slang_push_var_table(A
->vartable
);
5360 /* Generate IR tree for the function body code */
5361 n
= _slang_gen_operation(A
, fun
->body
);
5363 n
= new_node1(IR_SCOPE
, n
);
5365 /* pop vartable, restore previous */
5366 _slang_pop_var_table(A
->vartable
);
5369 /* XXX record error */
5373 /* append an end-of-function-label to IR tree */
5374 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5376 /*_slang_label_delete(A->curFuncEndLabel);*/
5377 A
->curFuncEndLabel
= NULL
;
5380 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5381 slang_print_function(fun
, 1);
5384 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5385 _slang_print_ir_tree(n
, 0);
5388 printf("************* End codegen function ************\n\n");
5391 if (A
->UnresolvedRefs
) {
5392 /* Can't codegen at this time.
5393 * At link time we'll concatenate all the vertex shaders and/or all
5394 * the fragment shaders and try recompiling.
5399 /* Emit program instructions */
5400 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5401 _slang_free_ir_tree(n
);
5403 /* free codegen context */