2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_print.h"
47 #include "shader/prog_statevars.h"
48 #include "slang_typeinfo.h"
49 #include "slang_builtin.h"
50 #include "slang_codegen.h"
51 #include "slang_compile.h"
52 #include "slang_label.h"
53 #include "slang_mem.h"
54 #include "slang_simplify.h"
55 #include "slang_emit.h"
56 #include "slang_vartable.h"
58 #include "slang_print.h"
61 /** Max iterations to unroll */
62 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS
= 32;
64 /** Max for-loop body size (in slang operations) to unroll */
65 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
67 /** Max for-loop body complexity to unroll.
68 * We'll compute complexity as the product of the number of iterations
69 * and the size of the body. So long-ish loops with very simple bodies
70 * can be unrolled, as well as short loops with larger bodies.
72 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY
= 256;
76 static slang_ir_node
*
77 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
80 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
81 GLuint substCount
, slang_variable
**substOld
,
82 slang_operation
**substNew
, GLboolean isLHS
);
86 * Retrieves type information about an operation.
87 * Returns GL_TRUE on success.
88 * Returns GL_FALSE otherwise.
91 typeof_operation(const struct slang_assemble_ctx_
*A
,
95 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
100 is_sampler_type(const slang_fully_specified_type
*t
)
102 switch (t
->specifier
.type
) {
103 case SLANG_SPEC_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 { NULL
, IR_NOP
, 0, 0 }
510 static slang_ir_node
*
511 new_node3(slang_ir_opcode op
,
512 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
514 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
520 n
->InstLocation
= -1;
525 static slang_ir_node
*
526 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
528 return new_node3(op
, c0
, c1
, NULL
);
531 static slang_ir_node
*
532 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
534 return new_node3(op
, c0
, NULL
, NULL
);
537 static slang_ir_node
*
538 new_node0(slang_ir_opcode op
)
540 return new_node3(op
, NULL
, NULL
, NULL
);
545 * Create sequence of two nodes.
547 static slang_ir_node
*
548 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
554 return new_node2(IR_SEQ
, left
, right
);
557 static slang_ir_node
*
558 new_label(slang_label
*label
)
560 slang_ir_node
*n
= new_node0(IR_LABEL
);
567 static slang_ir_node
*
568 new_float_literal(const float v
[4], GLuint size
)
570 slang_ir_node
*n
= new_node0(IR_FLOAT
);
572 COPY_4V(n
->Value
, v
);
573 /* allocate a storage object, but compute actual location (Index) later */
574 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
579 static slang_ir_node
*
580 new_not(slang_ir_node
*n
)
582 return new_node1(IR_NOT
, n
);
587 * Non-inlined function call.
589 static slang_ir_node
*
590 new_function_call(slang_ir_node
*code
, slang_label
*name
)
592 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
601 * Unconditional jump.
603 static slang_ir_node
*
604 new_return(slang_label
*dest
)
606 slang_ir_node
*n
= new_node0(IR_RETURN
);
614 static slang_ir_node
*
615 new_loop(slang_ir_node
*body
)
617 return new_node1(IR_LOOP
, body
);
621 static slang_ir_node
*
622 new_break(slang_ir_node
*loopNode
)
624 slang_ir_node
*n
= new_node0(IR_BREAK
);
626 assert(loopNode
->Opcode
== IR_LOOP
);
628 /* insert this node at head of linked list of cont/break instructions */
629 n
->List
= loopNode
->List
;
637 * Make new IR_BREAK_IF_TRUE.
639 static slang_ir_node
*
640 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
642 slang_ir_node
*loopNode
= current_loop_ir(A
);
645 assert(loopNode
->Opcode
== IR_LOOP
);
646 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
648 /* insert this node at head of linked list of cont/break instructions */
649 n
->List
= loopNode
->List
;
657 * Make new IR_CONT_IF_TRUE node.
659 static slang_ir_node
*
660 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
662 slang_ir_node
*loopNode
= current_loop_ir(A
);
665 assert(loopNode
->Opcode
== IR_LOOP
);
666 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
668 n
->Parent
= loopNode
; /* pointer to containing loop */
669 /* insert this node at head of linked list of cont/break instructions */
670 n
->List
= loopNode
->List
;
677 static slang_ir_node
*
678 new_cond(slang_ir_node
*n
)
680 slang_ir_node
*c
= new_node1(IR_COND
, n
);
685 static slang_ir_node
*
686 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
688 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
693 * New IR_VAR node - a reference to a previously declared variable.
695 static slang_ir_node
*
696 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
698 slang_ir_node
*n
= new_node0(IR_VAR
);
705 /* Set IR node's Var and Store pointers */
707 n
->Store
= var
->store
;
714 * Check if the given function is really just a wrapper for a
715 * basic assembly instruction.
718 slang_is_asm_function(const slang_function
*fun
)
720 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
721 fun
->body
->num_children
== 1 &&
722 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
730 _slang_is_noop(const slang_operation
*oper
)
733 oper
->type
== SLANG_OPER_VOID
||
734 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
742 * Recursively search tree for a node of the given type.
745 static slang_operation
*
746 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
749 if (oper
->type
== type
)
751 for (i
= 0; i
< oper
->num_children
; i
++) {
752 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
762 * Count the number of operations of the given time rooted at 'oper'.
765 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
768 if (oper
->type
== type
) {
771 for (i
= 0; i
< oper
->num_children
; i
++) {
772 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
779 * Check if the 'return' statement found under 'oper' is a "tail return"
780 * that can be no-op'd. For example:
785 * return; // this is a no-op
788 * This is used when determining if a function can be inlined. If the
789 * 'return' is not the last statement, we can't inline the function since
790 * we still need the semantic behaviour of the 'return' but we don't want
791 * to accidentally return from the _calling_ function. We'd need to use an
792 * unconditional branch, but we don't have such a GPU instruction (not
796 _slang_is_tail_return(const slang_operation
*oper
)
798 GLuint k
= oper
->num_children
;
801 const slang_operation
*last
= &oper
->children
[k
- 1];
802 if (last
->type
== SLANG_OPER_RETURN
)
804 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
805 last
->type
== SLANG_OPER_LABEL
)
806 k
--; /* try prev child */
807 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
808 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
809 /* try sub-children */
810 return _slang_is_tail_return(last
);
820 * Generate a variable declaration opeartion.
821 * I.e.: generate AST code for "bool flag = false;"
824 slang_generate_declaration(slang_assemble_ctx
*A
,
825 slang_variable_scope
*scope
,
826 slang_operation
*decl
,
827 slang_type_specifier_type type
,
833 assert(type
== SLANG_SPEC_BOOL
||
834 type
== SLANG_SPEC_INT
);
836 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
838 var
= slang_variable_scope_grow(scope
);
840 slang_fully_specified_type_construct(&var
->type
);
842 var
->type
.specifier
.type
= type
;
843 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
844 decl
->a_id
= var
->a_name
;
845 var
->initializer
= slang_operation_new(1);
846 slang_operation_literal_bool(var
->initializer
, initValue
);
851 slang_resolve_variable(slang_operation
*oper
)
853 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
854 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
860 * Rewrite AST code for "return expression;".
862 * We return values from functions by assinging the returned value to
863 * the hidden __retVal variable which is an extra 'out' parameter we add
864 * to the function signature.
865 * This code basically converts "return expr;" into "__retVal = expr; return;"
867 * \return the new AST code.
869 static slang_operation
*
870 gen_return_with_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
872 slang_operation
*blockOper
, *assignOper
;
874 assert(oper
->type
== SLANG_OPER_RETURN
);
876 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
877 slang_info_log_error(A
->log
, "illegal return expression");
881 blockOper
= slang_operation_new(1);
882 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
883 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
884 slang_operation_add_children(blockOper
, 2);
886 if (A
->UseReturnFlag
) {
895 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
896 ifOper
->type
= SLANG_OPER_IF
;
897 slang_operation_add_children(ifOper
, 3);
899 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
900 cond
->type
= SLANG_OPER_IDENTIFIER
;
901 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
904 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
905 elseOper
->type
= SLANG_OPER_VOID
;
907 assignOper
= slang_oper_child(ifOper
, 1);
910 slang_operation
*setOper
= slang_oper_child(blockOper
, 1);
911 setOper
->type
= SLANG_OPER_ASSIGN
;
912 slang_operation_add_children(setOper
, 2);
914 slang_operation
*lhs
= slang_oper_child(setOper
, 0);
915 lhs
->type
= SLANG_OPER_IDENTIFIER
;
916 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
919 slang_operation
*rhs
= slang_oper_child(setOper
, 1);
920 slang_operation_literal_bool(rhs
, GL_FALSE
);
931 assignOper
= slang_oper_child(blockOper
, 0);
933 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
934 returnOper
->type
= SLANG_OPER_RETURN_INLINED
;
935 assert(returnOper
->num_children
== 0);
939 /* __retVal = expression; */
940 assignOper
->type
= SLANG_OPER_ASSIGN
;
941 slang_operation_add_children(assignOper
, 2);
943 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
944 lhs
->type
= SLANG_OPER_IDENTIFIER
;
945 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
948 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
949 slang_operation_copy(rhs
, &oper
->children
[0]);
952 /*blockOper->locals->outer_scope = oper->locals->outer_scope;*/
954 /*slang_print_tree(blockOper, 0);*/
961 * Rewrite AST code for "return;" (no expression).
963 static slang_operation
*
964 gen_return_without_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
966 slang_operation
*newRet
;
968 assert(oper
->type
== SLANG_OPER_RETURN
);
970 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
971 slang_info_log_error(A
->log
, "return statement requires an expression");
975 if (A
->UseReturnFlag
) {
980 newRet
= slang_operation_new(1);
981 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
982 newRet
->type
= SLANG_OPER_ASSIGN
;
983 slang_operation_add_children(newRet
, 2);
985 slang_operation
*lhs
= slang_oper_child(newRet
, 0);
986 lhs
->type
= SLANG_OPER_IDENTIFIER
;
987 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
990 slang_operation
*rhs
= slang_oper_child(newRet
, 1);
991 slang_operation_literal_bool(rhs
, GL_FALSE
);
999 newRet
= slang_operation_new(1);
1000 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1001 newRet
->type
= SLANG_OPER_RETURN_INLINED
;
1004 /*slang_print_tree(newRet, 0);*/
1013 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1016 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
1017 GLuint substCount
, slang_variable
**substOld
,
1018 slang_operation
**substNew
, GLboolean isLHS
)
1020 switch (oper
->type
) {
1021 case SLANG_OPER_VARIABLE_DECL
:
1023 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
1024 oper
->a_id
, GL_TRUE
);
1026 if (v
->initializer
&& oper
->num_children
== 0) {
1027 /* set child of oper to copy of initializer */
1028 oper
->num_children
= 1;
1029 oper
->children
= slang_operation_new(1);
1030 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1032 if (oper
->num_children
== 1) {
1033 /* the initializer */
1034 slang_substitute(A
, &oper
->children
[0], substCount
,
1035 substOld
, substNew
, GL_FALSE
);
1039 case SLANG_OPER_IDENTIFIER
:
1040 assert(oper
->num_children
== 0);
1041 if (1/**!isLHS XXX FIX */) {
1042 slang_atom id
= oper
->a_id
;
1045 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1047 if (strcmp((char *) oper
->a_id
, "__notRetFlag"))
1048 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1052 /* look for a substitution */
1053 for (i
= 0; i
< substCount
; i
++) {
1054 if (v
== substOld
[i
]) {
1055 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1056 #if 0 /* DEBUG only */
1057 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1058 assert(substNew
[i
]->var
);
1059 assert(substNew
[i
]->var
->a_name
);
1060 printf("Substitute %s with %s in id node %p\n",
1061 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1065 printf("Substitute %s with %f in id node %p\n",
1066 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1070 slang_operation_copy(oper
, substNew
[i
]);
1077 case SLANG_OPER_RETURN
:
1079 slang_operation
*newReturn
;
1080 /* generate new 'return' code' */
1081 if (slang_oper_child(oper
, 0)->type
== SLANG_OPER_VOID
)
1082 newReturn
= gen_return_without_expression(A
, oper
);
1084 newReturn
= gen_return_with_expression(A
, oper
);
1089 /* do substitutions on the new 'return' code */
1090 slang_substitute(A
, newReturn
,
1091 substCount
, substOld
, substNew
, GL_FALSE
);
1093 /* install new 'return' code */
1094 slang_operation_copy(oper
, newReturn
);
1095 slang_operation_destruct(newReturn
);
1099 case SLANG_OPER_ASSIGN
:
1100 case SLANG_OPER_SUBSCRIPT
:
1102 * child[0] can't have substitutions but child[1] can.
1104 slang_substitute(A
, &oper
->children
[0],
1105 substCount
, substOld
, substNew
, GL_TRUE
);
1106 slang_substitute(A
, &oper
->children
[1],
1107 substCount
, substOld
, substNew
, GL_FALSE
);
1109 case SLANG_OPER_FIELD
:
1110 /* XXX NEW - test */
1111 slang_substitute(A
, &oper
->children
[0],
1112 substCount
, substOld
, substNew
, GL_TRUE
);
1117 for (i
= 0; i
< oper
->num_children
; i
++)
1118 slang_substitute(A
, &oper
->children
[i
],
1119 substCount
, substOld
, substNew
, GL_FALSE
);
1126 * Produce inline code for a call to an assembly instruction.
1127 * This is typically used to compile a call to a built-in function like this:
1129 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1131 * __asm vec4_lrp __retVal, a, y, x;
1136 * r = mix(p1, p2, p3);
1146 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1148 static slang_operation
*
1149 slang_inline_asm_function(slang_assemble_ctx
*A
,
1150 slang_function
*fun
, slang_operation
*oper
)
1152 const GLuint numArgs
= oper
->num_children
;
1154 slang_operation
*inlined
;
1155 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1156 slang_variable
**substOld
;
1157 slang_operation
**substNew
;
1159 ASSERT(slang_is_asm_function(fun
));
1160 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1163 printf("Inline %s as %s\n",
1164 (char*) fun->header.a_name,
1165 (char*) fun->body->children[0].a_id);
1169 * We'll substitute formal params with actual args in the asm call.
1171 substOld
= (slang_variable
**)
1172 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1173 substNew
= (slang_operation
**)
1174 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1175 for (i
= 0; i
< numArgs
; i
++) {
1176 substOld
[i
] = fun
->parameters
->variables
[i
];
1177 substNew
[i
] = oper
->children
+ i
;
1180 /* make a copy of the code to inline */
1181 inlined
= slang_operation_new(1);
1182 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1184 /* get rid of the __retVal child */
1185 inlined
->num_children
--;
1186 for (i
= 0; i
< inlined
->num_children
; i
++) {
1187 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1191 /* now do formal->actual substitutions */
1192 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1194 _slang_free(substOld
);
1195 _slang_free(substNew
);
1198 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1199 (char *) fun
->header
.a_name
);
1200 slang_print_tree(inlined
, 3);
1201 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1209 * Inline the given function call operation.
1210 * Return a new slang_operation that corresponds to the inlined code.
1212 static slang_operation
*
1213 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1214 slang_operation
*oper
, slang_operation
*returnOper
)
1221 ParamMode
*paramMode
;
1222 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1223 const GLuint numArgs
= oper
->num_children
;
1224 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1225 slang_operation
*args
= oper
->children
;
1226 slang_operation
*inlined
, *top
;
1227 slang_variable
**substOld
;
1228 slang_operation
**substNew
;
1229 GLuint substCount
, numCopyIn
, i
;
1230 slang_function
*prevFunction
;
1231 slang_variable_scope
*newScope
= NULL
;
1234 prevFunction
= A
->CurFunction
;
1235 A
->CurFunction
= fun
;
1237 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1238 assert(fun
->param_count
== totalArgs
);
1240 /* allocate temporary arrays */
1241 paramMode
= (ParamMode
*)
1242 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1243 substOld
= (slang_variable
**)
1244 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1245 substNew
= (slang_operation
**)
1246 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1249 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1250 (char *) fun
->header
.a_name
,
1251 fun
->parameters
->num_variables
, numArgs
);
1254 if (haveRetValue
&& !returnOper
) {
1255 /* Create 3-child comma sequence for inlined code:
1256 * child[0]: declare __resultTmp
1257 * child[1]: inlined function body
1258 * child[2]: __resultTmp
1260 slang_operation
*commaSeq
;
1261 slang_operation
*declOper
= NULL
;
1262 slang_variable
*resultVar
;
1264 commaSeq
= slang_operation_new(1);
1265 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1266 assert(commaSeq
->locals
);
1267 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1268 commaSeq
->num_children
= 3;
1269 commaSeq
->children
= slang_operation_new(3);
1270 /* allocate the return var */
1271 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1273 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1274 (void*)commaSeq->locals, (char *) fun->header.a_name);
1277 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1278 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1279 resultVar
->isTemp
= GL_TRUE
;
1281 /* child[0] = __resultTmp declaration */
1282 declOper
= &commaSeq
->children
[0];
1283 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1284 declOper
->a_id
= resultVar
->a_name
;
1285 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1287 /* child[1] = function body */
1288 inlined
= &commaSeq
->children
[1];
1289 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1291 /* child[2] = __resultTmp reference */
1292 returnOper
= &commaSeq
->children
[2];
1293 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1294 returnOper
->a_id
= resultVar
->a_name
;
1295 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1300 top
= inlined
= slang_operation_new(1);
1301 /* XXXX this may be inappropriate!!!! */
1302 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1306 assert(inlined
->locals
);
1308 /* Examine the parameters, look for inout/out params, look for possible
1309 * substitutions, etc:
1310 * param type behaviour
1311 * in copy actual to local
1312 * const in substitute param with actual
1316 for (i
= 0; i
< totalArgs
; i
++) {
1317 slang_variable
*p
= fun
->parameters
->variables
[i
];
1319 printf("Param %d: %s %s \n", i,
1320 slang_type_qual_string(p->type.qualifier),
1321 (char *) p->a_name);
1323 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1324 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1325 /* an output param */
1326 slang_operation
*arg
;
1331 paramMode
[i
] = SUBST
;
1333 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1334 slang_resolve_variable(arg
);
1336 /* replace parameter 'p' with argument 'arg' */
1337 substOld
[substCount
] = p
;
1338 substNew
[substCount
] = arg
; /* will get copied */
1341 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1342 /* a constant input param */
1343 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1344 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
||
1345 args
[i
].type
== SLANG_OPER_SUBSCRIPT
) {
1346 /* replace all occurances of this parameter variable with the
1347 * actual argument variable or a literal.
1349 paramMode
[i
] = SUBST
;
1350 slang_resolve_variable(&args
[i
]);
1351 substOld
[substCount
] = p
;
1352 substNew
[substCount
] = &args
[i
]; /* will get copied */
1356 paramMode
[i
] = COPY_IN
;
1360 paramMode
[i
] = COPY_IN
;
1362 assert(paramMode
[i
]);
1365 /* actual code inlining: */
1366 slang_operation_copy(inlined
, fun
->body
);
1368 /*** XXX review this */
1369 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1370 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1371 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1374 printf("======================= orig body code ======================\n");
1375 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1376 slang_print_tree(fun
->body
, 8);
1377 printf("======================= copied code =========================\n");
1378 slang_print_tree(inlined
, 8);
1381 /* do parameter substitution in inlined code: */
1382 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1385 printf("======================= subst code ==========================\n");
1386 slang_print_tree(inlined
, 8);
1387 printf("=============================================================\n");
1390 /* New prolog statements: (inserted before the inlined code)
1391 * Copy the 'in' arguments.
1394 for (i
= 0; i
< numArgs
; i
++) {
1395 if (paramMode
[i
] == COPY_IN
) {
1396 slang_variable
*p
= fun
->parameters
->variables
[i
];
1397 /* declare parameter 'p' */
1398 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1402 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1403 assert(decl
->locals
);
1404 decl
->locals
->outer_scope
= inlined
->locals
;
1405 decl
->a_id
= p
->a_name
;
1406 decl
->num_children
= 1;
1407 decl
->children
= slang_operation_new(1);
1409 /* child[0] is the var's initializer */
1410 slang_operation_copy(&decl
->children
[0], args
+ i
);
1412 /* add parameter 'p' to the local variable scope here */
1414 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1415 pCopy
->type
= p
->type
;
1416 pCopy
->a_name
= p
->a_name
;
1417 pCopy
->array_len
= p
->array_len
;
1420 newScope
= inlined
->locals
;
1425 /* Now add copies of the function's local vars to the new variable scope */
1426 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1427 slang_variable
*p
= fun
->parameters
->variables
[i
];
1428 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1429 pCopy
->type
= p
->type
;
1430 pCopy
->a_name
= p
->a_name
;
1431 pCopy
->array_len
= p
->array_len
;
1435 /* New epilog statements:
1436 * 1. Create end of function label to jump to from return statements.
1437 * 2. Copy the 'out' parameter vars
1440 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1442 inlined
->num_children
);
1443 lab
->type
= SLANG_OPER_LABEL
;
1444 lab
->label
= A
->curFuncEndLabel
;
1447 for (i
= 0; i
< totalArgs
; i
++) {
1448 if (paramMode
[i
] == COPY_OUT
) {
1449 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1450 /* actualCallVar = outParam */
1451 /*if (i > 0 || !haveRetValue)*/
1452 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1454 inlined
->num_children
);
1455 ass
->type
= SLANG_OPER_ASSIGN
;
1456 ass
->num_children
= 2;
1457 ass
->locals
->outer_scope
= inlined
->locals
;
1458 ass
->children
= slang_operation_new(2);
1459 ass
->children
[0] = args
[i
]; /*XXX copy */
1460 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1461 ass
->children
[1].a_id
= p
->a_name
;
1462 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1466 _slang_free(paramMode
);
1467 _slang_free(substOld
);
1468 _slang_free(substNew
);
1470 /* Update scoping to use the new local vars instead of the
1471 * original function's vars. This is especially important
1472 * for nested inlining.
1475 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1478 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1479 (char *) fun
->header
.a_name
,
1480 fun
->parameters
->num_variables
, numArgs
);
1481 slang_print_tree(top
, 0);
1485 A
->CurFunction
= prevFunction
;
1492 * Insert declaration for "bool __notRetFlag" in given block operation.
1493 * This is used when we can't emit "early" return statements in subroutines.
1496 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1498 slang_operation
*decl
;
1500 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1501 oper
->type
== SLANG_OPER_SEQUENCE
);
1503 decl
= slang_operation_insert_child(oper
, 1);
1505 slang_generate_declaration(A
, oper
->locals
, decl
,
1506 SLANG_SPEC_BOOL
, "__notRetFlag", GL_TRUE
);
1508 /*slang_print_tree(oper, 0);*/
1513 * Recursively replace instances of the old node type with the new type.
1516 replace_node_type(slang_operation
*oper
, slang_operation_type oldType
,
1517 slang_operation_type newType
)
1521 if (oper
->type
== oldType
)
1522 oper
->type
= newType
;
1524 for (i
= 0; i
< slang_oper_num_children(oper
); i
++) {
1525 replace_node_type(slang_oper_child(oper
, i
), oldType
, newType
);
1532 * Test if the given function body has an "early return". That is, there's
1533 * a 'return' statement that's not the very last instruction in the body.
1536 has_early_return(const slang_operation
*funcBody
)
1538 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1541 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1549 * Emit IR code for a function call. This does one of two things:
1550 * 1. Inline the function's code
1551 * 2. Create an IR for the function's body and create a real call to it.
1553 static slang_ir_node
*
1554 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1555 slang_operation
*oper
, slang_operation
*dest
)
1558 slang_operation
*instance
;
1559 slang_label
*prevFuncEndLabel
;
1562 prevFuncEndLabel
= A
->curFuncEndLabel
;
1563 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1564 A
->curFuncEndLabel
= _slang_label_new(name
);
1565 assert(A
->curFuncEndLabel
);
1568 * 'instance' is basically a copy of the function's body with various
1572 if (slang_is_asm_function(fun
) && !dest
) {
1573 /* assemble assembly function - tree style */
1574 instance
= slang_inline_asm_function(A
, fun
, oper
);
1577 /* non-assembly function */
1578 /* We always generate an "inline-able" block of code here.
1580 * 1. insert the inline code
1581 * 2. Generate a call to the "inline" code as a subroutine
1583 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1585 if (earlyReturn
&& !A
->EmitContReturn
) {
1586 A
->UseReturnFlag
= GL_TRUE
;
1589 instance
= slang_inline_function_call(A
, fun
, oper
, dest
);
1594 /* The function we're calling has one or more 'return' statements
1595 * that prevent us from inlining the function's code.
1597 * In this case, change the function's body type from
1598 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1599 * During code emit this will result in a true subroutine call.
1601 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1603 slang_operation
*callOper
;
1605 assert(instance
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1606 instance
->type
== SLANG_OPER_SEQUENCE
);
1608 if (_slang_function_has_return_value(fun
) && !dest
) {
1609 assert(instance
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1610 assert(instance
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1611 callOper
= &instance
->children
[1];
1614 callOper
= instance
;
1617 if (A
->UseReturnFlag
) {
1618 /* Early returns not supported. Create a _returnFlag variable
1619 * that's set upon 'return' and tested elsewhere to no-op any
1620 * remaining instructions in the subroutine.
1622 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1623 callOper
->type
== SLANG_OPER_SEQUENCE
);
1624 declare_return_flag(A
, callOper
);
1627 /* We can emit real 'return' statements. If we generated any
1628 * 'inline return' statements during function instantiation,
1629 * change them back to regular 'return' statements.
1631 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1635 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1636 callOper
->fun
= fun
;
1637 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1640 /* If there are any 'return' statements remaining, they're at the
1641 * very end of the function and can effectively become no-ops.
1643 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1651 /* Replace the function call with the instance block (or new CALL stmt) */
1652 slang_operation_destruct(oper
);
1654 _slang_free(instance
);
1657 assert(instance
->locals
);
1658 printf("*** Inlined code for call to %s:\n", (char*) fun
->header
.a_name
);
1659 slang_print_tree(oper
, 10);
1663 n
= _slang_gen_operation(A
, oper
);
1665 /*_slang_label_delete(A->curFuncEndLabel);*/
1666 A
->curFuncEndLabel
= prevFuncEndLabel
;
1668 if (A
->pragmas
->Debug
) {
1670 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1671 n
->Comment
= _slang_strdup(s
);
1674 A
->UseReturnFlag
= GL_FALSE
;
1680 static slang_asm_info
*
1681 slang_find_asm_info(const char *name
)
1684 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1685 if (strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1694 * Some write-masked assignments are simple, but others are hard.
1697 * v.xy = vec2(a, b);
1700 * v.zy = vec2(a, b);
1701 * this gets transformed/swizzled into:
1702 * v.zy = vec2(a, b).*yx* (* = don't care)
1703 * This function helps to determine simple vs. non-simple.
1706 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1708 switch (writemask
) {
1710 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1712 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1714 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1716 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1718 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1719 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1721 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1722 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1723 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1724 case WRITEMASK_XYZW
:
1725 return swizzle
== SWIZZLE_NOOP
;
1733 * Convert the given swizzle into a writemask. In some cases this
1734 * is trivial, in other cases, we'll need to also swizzle the right
1735 * hand side to put components in the right places.
1736 * See comment above for more info.
1737 * XXX this function could be simplified and should probably be renamed.
1738 * \param swizzle the incoming swizzle
1739 * \param writemaskOut returns the writemask
1740 * \param swizzleOut swizzle to apply to the right-hand-side
1741 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1744 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1745 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1747 GLuint mask
= 0x0, newSwizzle
[4];
1750 /* make new dst writemask, compute size */
1751 for (i
= 0; i
< 4; i
++) {
1752 const GLuint swz
= GET_SWZ(swizzle
, i
);
1753 if (swz
== SWIZZLE_NIL
) {
1757 assert(swz
>= 0 && swz
<= 3);
1759 if (swizzle
!= SWIZZLE_XXXX
&&
1760 swizzle
!= SWIZZLE_YYYY
&&
1761 swizzle
!= SWIZZLE_ZZZZ
&&
1762 swizzle
!= SWIZZLE_WWWW
&&
1763 (mask
& (1 << swz
))) {
1764 /* a channel can't be specified twice (ex: ".xyyz") */
1765 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1766 _mesa_swizzle_string(swizzle
, 0, 0));
1772 assert(mask
<= 0xf);
1773 size
= i
; /* number of components in mask/swizzle */
1775 *writemaskOut
= mask
;
1777 /* make new src swizzle, by inversion */
1778 for (i
= 0; i
< 4; i
++) {
1779 newSwizzle
[i
] = i
; /*identity*/
1781 for (i
= 0; i
< size
; i
++) {
1782 const GLuint swz
= GET_SWZ(swizzle
, i
);
1783 newSwizzle
[swz
] = i
;
1785 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1790 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1792 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1794 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1796 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1798 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1806 #if 0 /* not used, but don't remove just yet */
1808 * Recursively traverse 'oper' to produce a swizzle mask in the event
1809 * of any vector subscripts and swizzle suffixes.
1810 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1813 resolve_swizzle(const slang_operation
*oper
)
1815 if (oper
->type
== SLANG_OPER_FIELD
) {
1816 /* writemask from .xyzw suffix */
1818 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1819 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1823 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1824 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1828 return SWIZZLE_XYZW
;
1830 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1831 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1832 /* writemask from [index] */
1833 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1834 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1839 swizzle
= SWIZZLE_XXXX
;
1842 swizzle
= SWIZZLE_YYYY
;
1845 swizzle
= SWIZZLE_ZZZZ
;
1848 swizzle
= SWIZZLE_WWWW
;
1851 swizzle
= SWIZZLE_XYZW
;
1853 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1857 return SWIZZLE_XYZW
;
1865 * Recursively descend through swizzle nodes to find the node's storage info.
1867 static slang_ir_storage
*
1868 get_store(const slang_ir_node
*n
)
1870 if (n
->Opcode
== IR_SWIZZLE
) {
1871 return get_store(n
->Children
[0]);
1879 * Generate IR tree for an asm instruction/operation such as:
1880 * __asm vec4_dot __retVal.x, v1, v2;
1882 static slang_ir_node
*
1883 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1884 slang_operation
*dest
)
1886 const slang_asm_info
*info
;
1887 slang_ir_node
*kids
[3], *n
;
1888 GLuint j
, firstOperand
;
1890 assert(oper
->type
== SLANG_OPER_ASM
);
1892 info
= slang_find_asm_info((char *) oper
->a_id
);
1894 _mesa_problem(NULL
, "undefined __asm function %s\n",
1895 (char *) oper
->a_id
);
1898 assert(info
->NumParams
<= 3);
1900 if (info
->NumParams
== oper
->num_children
) {
1901 /* Storage for result is not specified.
1902 * Children[0], [1], [2] are the operands.
1907 /* Storage for result (child[0]) is specified.
1908 * Children[1], [2], [3] are the operands.
1913 /* assemble child(ren) */
1914 kids
[0] = kids
[1] = kids
[2] = NULL
;
1915 for (j
= 0; j
< info
->NumParams
; j
++) {
1916 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1921 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1924 /* Setup n->Store to be a particular location. Otherwise, storage
1925 * for the result (a temporary) will be allocated later.
1927 slang_operation
*dest_oper
;
1930 dest_oper
= &oper
->children
[0];
1932 n0
= _slang_gen_operation(A
, dest_oper
);
1937 n
->Store
= n0
->Store
;
1939 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1950 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1953 for (i
= 0; i
< scope
->num_functions
; i
++) {
1954 slang_function
*f
= &scope
->functions
[i
];
1955 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1956 printf(" %s (%d args)\n", name
, f
->param_count
);
1959 if (scope
->outer_scope
)
1960 print_funcs(scope
->outer_scope
, name
);
1966 * Find a function of the given name, taking 'numArgs' arguments.
1967 * This is the function we'll try to call when there is no exact match
1968 * between function parameters and call arguments.
1970 * XXX we should really create a list of candidate functions and try
1973 static slang_function
*
1974 _slang_find_function_by_argc(slang_function_scope
*scope
,
1975 const char *name
, int numArgs
)
1979 for (i
= 0; i
< scope
->num_functions
; i
++) {
1980 slang_function
*f
= &scope
->functions
[i
];
1981 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1982 int haveRetValue
= _slang_function_has_return_value(f
);
1983 if (numArgs
== f
->param_count
- haveRetValue
)
1987 scope
= scope
->outer_scope
;
1994 static slang_function
*
1995 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1998 slang_function
*maxFunc
= NULL
;
2003 for (i
= 0; i
< scope
->num_functions
; i
++) {
2004 slang_function
*f
= &scope
->functions
[i
];
2005 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2006 if (f
->param_count
> maxArgs
) {
2007 maxArgs
= f
->param_count
;
2012 scope
= scope
->outer_scope
;
2020 * Generate a new slang_function which is a constructor for a user-defined
2023 static slang_function
*
2024 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2026 const GLint numFields
= str
->fields
->num_variables
;
2027 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2029 /* function header (name, return type) */
2030 fun
->header
.a_name
= str
->a_name
;
2031 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2032 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2033 fun
->header
.type
.specifier
._struct
= str
;
2035 /* function parameters (= struct's fields) */
2038 for (i
= 0; i
< numFields
; i
++) {
2040 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2042 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2043 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2044 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2046 fun
->param_count
= fun
->parameters
->num_variables
;
2049 /* Add __retVal to params */
2051 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2052 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2054 p
->a_name
= a_retVal
;
2055 p
->type
= fun
->header
.type
;
2056 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2060 /* function body is:
2070 slang_variable_scope
*scope
;
2071 slang_variable
*var
;
2074 fun
->body
= slang_operation_new(1);
2075 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2076 fun
->body
->num_children
= numFields
+ 2;
2077 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2079 scope
= fun
->body
->locals
;
2080 scope
->outer_scope
= fun
->parameters
;
2082 /* create local var 't' */
2083 var
= slang_variable_scope_grow(scope
);
2084 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2085 var
->type
= fun
->header
.type
;
2089 slang_operation
*decl
;
2091 decl
= &fun
->body
->children
[0];
2092 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2093 decl
->locals
= _slang_variable_scope_new(scope
);
2094 decl
->a_id
= var
->a_name
;
2097 /* assign params to fields of t */
2098 for (i
= 0; i
< numFields
; i
++) {
2099 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2101 assign
->type
= SLANG_OPER_ASSIGN
;
2102 assign
->locals
= _slang_variable_scope_new(scope
);
2103 assign
->num_children
= 2;
2104 assign
->children
= slang_operation_new(2);
2107 slang_operation
*lhs
= &assign
->children
[0];
2109 lhs
->type
= SLANG_OPER_FIELD
;
2110 lhs
->locals
= _slang_variable_scope_new(scope
);
2111 lhs
->num_children
= 1;
2112 lhs
->children
= slang_operation_new(1);
2113 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2115 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2116 lhs
->children
[0].a_id
= var
->a_name
;
2117 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2120 lhs
->children
[1].num_children
= 1;
2121 lhs
->children
[1].children
= slang_operation_new(1);
2122 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2123 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2124 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2129 slang_operation
*rhs
= &assign
->children
[1];
2131 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2132 rhs
->locals
= _slang_variable_scope_new(scope
);
2133 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2139 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2141 ret
->type
= SLANG_OPER_RETURN
;
2142 ret
->locals
= _slang_variable_scope_new(scope
);
2143 ret
->num_children
= 1;
2144 ret
->children
= slang_operation_new(1);
2145 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2146 ret
->children
[0].a_id
= var
->a_name
;
2147 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2151 slang_print_function(fun, 1);
2158 * Find/create a function (constructor) for the given structure name.
2160 static slang_function
*
2161 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2164 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2165 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2166 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2167 /* found a structure type that matches the function name */
2168 if (!str
->constructor
) {
2169 /* create the constructor function now */
2170 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2172 return str
->constructor
;
2180 * Generate a new slang_function to satisfy a call to an array constructor.
2181 * Ex: float[3](1., 2., 3.)
2183 static slang_function
*
2184 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2186 slang_type_specifier_type baseType
;
2187 slang_function
*fun
;
2190 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2194 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2196 num_elements
= oper
->num_children
;
2198 /* function header, return type */
2200 fun
->header
.a_name
= oper
->a_id
;
2201 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2202 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2203 fun
->header
.type
.specifier
._array
=
2204 slang_type_specifier_new(baseType
, NULL
, NULL
);
2205 fun
->header
.type
.array_len
= num_elements
;
2208 /* function parameters (= number of elements) */
2211 for (i
= 0; i
< num_elements
; i
++) {
2213 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2215 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2217 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2218 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2219 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2220 p
->type
.specifier
.type
= baseType
;
2222 fun
->param_count
= fun
->parameters
->num_variables
;
2225 /* Add __retVal to params */
2227 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2228 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2230 p
->a_name
= a_retVal
;
2231 p
->type
= fun
->header
.type
;
2232 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2233 p
->type
.specifier
.type
= baseType
;
2237 /* function body is:
2247 slang_variable_scope
*scope
;
2248 slang_variable
*var
;
2251 fun
->body
= slang_operation_new(1);
2252 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2253 fun
->body
->num_children
= num_elements
+ 2;
2254 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2256 scope
= fun
->body
->locals
;
2257 scope
->outer_scope
= fun
->parameters
;
2259 /* create local var 't' */
2260 var
= slang_variable_scope_grow(scope
);
2261 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2262 var
->type
= fun
->header
.type
;/*XXX copy*/
2266 slang_operation
*decl
;
2268 decl
= &fun
->body
->children
[0];
2269 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2270 decl
->locals
= _slang_variable_scope_new(scope
);
2271 decl
->a_id
= var
->a_name
;
2274 /* assign params to elements of t */
2275 for (i
= 0; i
< num_elements
; i
++) {
2276 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2278 assign
->type
= SLANG_OPER_ASSIGN
;
2279 assign
->locals
= _slang_variable_scope_new(scope
);
2280 assign
->num_children
= 2;
2281 assign
->children
= slang_operation_new(2);
2284 slang_operation
*lhs
= &assign
->children
[0];
2286 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2287 lhs
->locals
= _slang_variable_scope_new(scope
);
2288 lhs
->num_children
= 2;
2289 lhs
->children
= slang_operation_new(2);
2291 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2292 lhs
->children
[0].a_id
= var
->a_name
;
2293 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2295 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2296 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2300 slang_operation
*rhs
= &assign
->children
[1];
2302 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2303 rhs
->locals
= _slang_variable_scope_new(scope
);
2304 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2310 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2312 ret
->type
= SLANG_OPER_RETURN
;
2313 ret
->locals
= _slang_variable_scope_new(scope
);
2314 ret
->num_children
= 1;
2315 ret
->children
= slang_operation_new(1);
2316 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2317 ret
->children
[0].a_id
= var
->a_name
;
2318 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2323 slang_print_function(fun, 1);
2331 _slang_is_vec_mat_type(const char *name
)
2333 static const char *vecmat_types
[] = {
2334 "float", "int", "bool",
2335 "vec2", "vec3", "vec4",
2336 "ivec2", "ivec3", "ivec4",
2337 "bvec2", "bvec3", "bvec4",
2338 "mat2", "mat3", "mat4",
2339 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2343 for (i
= 0; vecmat_types
[i
]; i
++)
2344 if (strcmp(name
, vecmat_types
[i
]) == 0)
2351 * Assemble a function call, given a particular function name.
2352 * \param name the function's name (operators like '*' are possible).
2354 static slang_ir_node
*
2355 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2356 slang_operation
*oper
, slang_operation
*dest
)
2358 slang_operation
*params
= oper
->children
;
2359 const GLuint param_count
= oper
->num_children
;
2361 slang_function
*fun
;
2364 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2365 if (atom
== SLANG_ATOM_NULL
)
2368 if (oper
->array_constructor
) {
2369 /* this needs special handling */
2370 fun
= _slang_make_array_constructor(A
, oper
);
2373 /* Try to find function by name and exact argument type matching */
2374 GLboolean error
= GL_FALSE
;
2375 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2376 &A
->space
, A
->atoms
, A
->log
, &error
);
2378 slang_info_log_error(A
->log
,
2379 "Function '%s' not found (check argument types)",
2386 /* Next, try locating a constructor function for a user-defined type */
2387 fun
= _slang_locate_struct_constructor(A
, name
);
2391 * At this point, some heuristics are used to try to find a function
2392 * that matches the calling signature by means of casting or "unrolling"
2396 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2397 /* Next, if this call looks like a vec() or mat() constructor call,
2398 * try "unwinding" the args to satisfy a constructor.
2400 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2402 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2403 slang_info_log_error(A
->log
,
2404 "Function '%s' not found (check argument types)",
2411 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2412 /* Next, try casting args to the types of the formal parameters */
2413 int numArgs
= oper
->num_children
;
2414 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2415 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2416 slang_info_log_error(A
->log
,
2417 "Function '%s' not found (check argument types)",
2425 slang_info_log_error(A
->log
,
2426 "Function '%s' not found (check argument types)",
2432 /* The function body may be in another compilation unit.
2433 * We'll try concatenating the shaders and recompile at link time.
2435 A
->UnresolvedRefs
= GL_TRUE
;
2436 return new_node1(IR_NOP
, NULL
);
2439 /* type checking to be sure function's return type matches 'dest' type */
2443 slang_typeinfo_construct(&t0
);
2444 typeof_operation(A
, dest
, &t0
);
2446 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2447 slang_info_log_error(A
->log
,
2448 "Incompatible type returned by call to '%s'",
2454 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2456 if (n
&& !n
->Store
&& !dest
2457 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2458 /* setup n->Store for the result of the function call */
2459 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2460 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2461 /*printf("Alloc storage for function result, size %d \n", size);*/
2464 if (oper
->array_constructor
) {
2465 /* free the temporary array constructor function now */
2466 slang_function_destruct(fun
);
2473 static slang_ir_node
*
2474 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2476 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2478 slang_variable
*var
;
2480 /* NOTE: In GLSL 1.20, there's only one kind of method
2481 * call: array.length(). Anything else is an error.
2483 if (oper
->a_id
!= a_length
) {
2484 slang_info_log_error(A
->log
,
2485 "Undefined method call '%s'", (char *) oper
->a_id
);
2489 /* length() takes no arguments */
2490 if (oper
->num_children
> 0) {
2491 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2495 /* lookup the object/variable */
2496 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2497 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2498 slang_info_log_error(A
->log
,
2499 "Undefined object '%s'", (char *) oper
->a_obj
);
2503 /* Create a float/literal IR node encoding the array length */
2504 n
= new_node0(IR_FLOAT
);
2506 n
->Value
[0] = (float) _slang_array_length(var
);
2507 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2514 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2516 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2517 oper
->type
== SLANG_OPER_LITERAL_INT
||
2518 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2519 if (oper
->literal
[0])
2525 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2526 oper
->num_children
== 1) {
2527 return _slang_is_constant_cond(&oper
->children
[0], value
);
2534 * Test if an operation is a scalar or boolean.
2537 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2539 slang_typeinfo type
;
2542 slang_typeinfo_construct(&type
);
2543 typeof_operation(A
, oper
, &type
);
2544 size
= _slang_sizeof_type_specifier(&type
.spec
);
2545 slang_typeinfo_destruct(&type
);
2551 * Test if an operation is boolean.
2554 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2556 slang_typeinfo type
;
2559 slang_typeinfo_construct(&type
);
2560 typeof_operation(A
, oper
, &type
);
2561 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2562 slang_typeinfo_destruct(&type
);
2568 * Check if a loop contains a 'continue' statement.
2569 * Stop looking if we find a nested loop.
2572 _slang_loop_contains_continue(const slang_operation
*oper
)
2574 switch (oper
->type
) {
2575 case SLANG_OPER_CONTINUE
:
2577 case SLANG_OPER_FOR
:
2579 case SLANG_OPER_WHILE
:
2580 /* stop upon finding a nested loop */
2586 for (i
= 0; i
< oper
->num_children
; i
++) {
2587 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2588 if (_slang_loop_contains_continue(child
))
2598 * Check if a loop contains a 'continue' or 'break' statement.
2599 * Stop looking if we find a nested loop.
2602 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2604 switch (oper
->type
) {
2605 case SLANG_OPER_CONTINUE
:
2606 case SLANG_OPER_BREAK
:
2608 case SLANG_OPER_FOR
:
2610 case SLANG_OPER_WHILE
:
2611 /* stop upon finding a nested loop */
2617 for (i
= 0; i
< oper
->num_children
; i
++) {
2618 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2619 if (_slang_loop_contains_continue_or_break(child
))
2629 * Replace 'break' and 'continue' statements inside a do and while loops.
2630 * This is a recursive helper function used by
2631 * _slang_gen_do/while_without_continue().
2634 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2636 switch (oper
->type
) {
2637 case SLANG_OPER_BREAK
:
2638 /* replace 'break' with "_notBreakFlag = false; break" */
2640 slang_operation
*block
= oper
;
2641 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2642 slang_operation_add_children(block
, 2);
2644 slang_operation
*assign
= slang_oper_child(block
, 0);
2645 assign
->type
= SLANG_OPER_ASSIGN
;
2646 slang_operation_add_children(assign
, 2);
2648 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2649 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2652 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2653 slang_operation_literal_bool(rhs
, GL_FALSE
);
2657 slang_operation
*brk
= slang_oper_child(block
, 1);
2658 brk
->type
= SLANG_OPER_BREAK
;
2659 assert(!brk
->children
);
2663 case SLANG_OPER_CONTINUE
:
2664 /* convert continue into a break */
2665 oper
->type
= SLANG_OPER_BREAK
;
2667 case SLANG_OPER_FOR
:
2669 case SLANG_OPER_WHILE
:
2670 /* stop upon finding a nested loop */
2676 for (i
= 0; i
< oper
->num_children
; i
++) {
2677 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2685 * Transform a while-loop so that continue statements are converted to breaks.
2686 * Then do normal IR code generation.
2690 * while (LOOPCOND) {
2702 * bool _notBreakFlag = 1;
2703 * while (_notBreakFlag && LOOPCOND) {
2707 * break; // was continue
2710 * _notBreakFlag = 0; // was
2717 static slang_ir_node
*
2718 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2720 slang_operation
*top
;
2721 slang_operation
*innerBody
;
2723 assert(oper
->type
== SLANG_OPER_WHILE
);
2725 top
= slang_operation_new(1);
2726 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2727 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2728 slang_operation_add_children(top
, 2);
2730 /* declare: bool _notBreakFlag = true */
2732 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2733 slang_generate_declaration(A
, top
->locals
, condDecl
,
2734 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2737 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2739 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2740 outerWhile
->type
= SLANG_OPER_WHILE
;
2741 slang_operation_add_children(outerWhile
, 2);
2743 /* _notBreakFlag && LOOPCOND */
2745 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2746 cond
->type
= SLANG_OPER_LOGICALAND
;
2747 slang_operation_add_children(cond
, 2);
2749 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2750 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2753 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2754 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2760 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2761 innerDo
->type
= SLANG_OPER_DO
;
2762 slang_operation_add_children(innerDo
, 2);
2764 /* copy original do-loop body into inner do-loop's body */
2765 innerBody
= slang_oper_child(innerDo
, 0);
2766 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2767 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2769 /* inner do-loop's condition is constant/false */
2771 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2772 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2777 /* Finally, in innerBody,
2778 * replace "break" with "_notBreakFlag = 0; break"
2779 * replace "continue" with "break"
2781 replace_break_and_cont(A
, innerBody
);
2783 /*slang_print_tree(top, 0);*/
2785 return _slang_gen_operation(A
, top
);
2792 * Generate loop code using high-level IR_LOOP instruction
2794 static slang_ir_node
*
2795 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2799 * BREAK if !expr (child[0])
2800 * body code (child[1])
2802 slang_ir_node
*loop
, *breakIf
, *body
;
2803 GLboolean isConst
, constTrue
= GL_FALSE
;
2805 if (!A
->EmitContReturn
) {
2806 /* We don't want to emit CONT instructions. If this while-loop has
2807 * a continue, translate it away.
2809 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2810 return _slang_gen_while_without_continue(A
, oper
);
2814 /* type-check expression */
2815 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2816 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2820 /* Check if loop condition is a constant */
2821 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2823 if (isConst
&& !constTrue
) {
2824 /* loop is never executed! */
2825 return new_node0(IR_NOP
);
2828 /* Begin new loop */
2829 loop
= new_loop(NULL
);
2831 /* save loop state */
2832 push_loop(A
, oper
, loop
);
2834 if (isConst
&& constTrue
) {
2835 /* while(nonzero constant), no conditional break */
2840 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2841 breakIf
= new_break_if_true(A
, cond
);
2843 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2844 loop
->Children
[0] = new_seq(breakIf
, body
);
2846 /* Do infinite loop detection */
2847 /* loop->List is head of linked list of break/continue nodes */
2848 if (!loop
->List
&& isConst
&& constTrue
) {
2849 /* infinite loop detected */
2851 slang_info_log_error(A
->log
, "Infinite loop detected!");
2855 /* restore loop state */
2863 * Transform a do-while-loop so that continue statements are converted to breaks.
2864 * Then do normal IR code generation.
2875 * } while (LOOPCOND);
2880 * bool _notBreakFlag = 1;
2885 * break; // was continue
2888 * _notBreakFlag = 0; // was
2892 * } while (_notBreakFlag && LOOPCOND);
2895 static slang_ir_node
*
2896 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2898 slang_operation
*top
;
2899 slang_operation
*innerBody
;
2901 assert(oper
->type
== SLANG_OPER_DO
);
2903 top
= slang_operation_new(1);
2904 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2905 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2906 slang_operation_add_children(top
, 2);
2908 /* declare: bool _notBreakFlag = true */
2910 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2911 slang_generate_declaration(A
, top
->locals
, condDecl
,
2912 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2915 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2917 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2918 outerDo
->type
= SLANG_OPER_DO
;
2919 slang_operation_add_children(outerDo
, 2);
2923 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2924 innerDo
->type
= SLANG_OPER_DO
;
2925 slang_operation_add_children(innerDo
, 2);
2927 /* copy original do-loop body into inner do-loop's body */
2928 innerBody
= slang_oper_child(innerDo
, 0);
2929 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2930 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2932 /* inner do-loop's condition is constant/false */
2934 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2935 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2939 /* _notBreakFlag && LOOPCOND */
2941 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2942 cond
->type
= SLANG_OPER_LOGICALAND
;
2943 slang_operation_add_children(cond
, 2);
2945 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2946 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2949 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2950 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2955 /* Finally, in innerBody,
2956 * replace "break" with "_notBreakFlag = 0; break"
2957 * replace "continue" with "break"
2959 replace_break_and_cont(A
, innerBody
);
2961 /*slang_print_tree(top, 0);*/
2963 return _slang_gen_operation(A
, top
);
2968 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2970 static slang_ir_node
*
2971 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2975 * body code (child[0])
2977 * BREAK if !expr (child[1])
2979 slang_ir_node
*loop
;
2980 GLboolean isConst
, constTrue
;
2982 if (!A
->EmitContReturn
) {
2983 /* We don't want to emit CONT instructions. If this do-loop has
2984 * a continue, translate it away.
2986 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2987 return _slang_gen_do_without_continue(A
, oper
);
2991 /* type-check expression */
2992 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2993 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2997 loop
= new_loop(NULL
);
2999 /* save loop state */
3000 push_loop(A
, oper
, loop
);
3003 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3005 /* Check if loop condition is a constant */
3006 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3007 if (isConst
&& constTrue
) {
3008 /* do { } while(1) ==> no conditional break */
3009 loop
->Children
[1] = NULL
; /* no tail code */
3013 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3014 loop
->Children
[1] = new_break_if_true(A
, cond
);
3017 /* XXX we should do infinite loop detection, as above */
3019 /* restore loop state */
3027 * Recursively count the number of operations rooted at 'oper'.
3028 * This gives some kind of indication of the size/complexity of an operation.
3031 sizeof_operation(const slang_operation
*oper
)
3034 GLuint count
= 1; /* me */
3036 for (i
= 0; i
< oper
->num_children
; i
++) {
3037 count
+= sizeof_operation(&oper
->children
[i
]);
3048 * Determine if a for-loop can be unrolled.
3049 * At this time, only a rather narrow class of for loops can be unrolled.
3050 * See code for details.
3051 * When a loop can't be unrolled because it's too large we'll emit a
3052 * message to the log.
3055 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3059 const char *varName
;
3062 if (oper
->type
!= SLANG_OPER_FOR
)
3065 assert(oper
->num_children
== 4);
3067 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3070 /* children[0] must be either "int i=constant" or "i=constant" */
3071 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3072 slang_variable
*var
;
3074 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3077 varId
= oper
->children
[0].children
[0].a_id
;
3079 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3083 if (!var
->initializer
)
3085 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3087 start
= (GLint
) var
->initializer
->literal
[0];
3089 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3090 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3092 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3094 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3097 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3099 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3105 /* children[1] must be "i<constant" */
3106 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3108 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3110 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3112 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3115 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3117 /* children[2] must be "i++" or "++i" */
3118 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3119 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3121 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3124 /* make sure the same variable name is used in all places */
3125 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3126 (oper
->children
[2].children
[0].a_id
!= varId
))
3129 varName
= (const char *) varId
;
3131 /* children[3], the loop body, can't be too large */
3132 bodySize
= sizeof_operation(&oper
->children
[3]);
3133 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3134 slang_info_log_print(A
->log
,
3135 "Note: 'for (%s ... )' body is too large/complex"
3142 return GL_FALSE
; /* degenerate case */
3144 if ((GLuint
)(end
- start
) > MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3145 slang_info_log_print(A
->log
,
3146 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3147 " many iterations to unroll",
3148 varName
, start
, varName
, end
, varName
);
3152 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3153 slang_info_log_print(A
->log
,
3154 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3155 " too much code to unroll",
3156 varName
, start
, varName
, end
, varName
);
3160 return GL_TRUE
; /* we can unroll the loop */
3165 * Unroll a for-loop.
3166 * First we determine the number of iterations to unroll.
3167 * Then for each iteration:
3168 * make a copy of the loop body
3169 * replace instances of the loop variable with the current iteration value
3170 * generate IR code for the body
3171 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3173 static slang_ir_node
*
3174 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3176 GLint start
, end
, iter
;
3177 slang_ir_node
*n
, *root
= NULL
;
3180 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3181 /* for (int i=0; ... */
3182 slang_variable
*var
;
3184 varId
= oper
->children
[0].children
[0].a_id
;
3185 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3187 start
= (GLint
) var
->initializer
->literal
[0];
3191 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3192 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3195 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3197 for (iter
= start
; iter
< end
; iter
++) {
3198 slang_operation
*body
;
3200 /* make a copy of the loop body */
3201 body
= slang_operation_new(1);
3205 if (!slang_operation_copy(body
, &oper
->children
[3]))
3208 /* in body, replace instances of 'varId' with literal 'iter' */
3210 slang_variable
*oldVar
;
3211 slang_operation
*newOper
;
3213 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3215 /* undeclared loop variable */
3216 slang_operation_delete(body
);
3220 newOper
= slang_operation_new(1);
3221 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3222 newOper
->literal_size
= 1;
3223 newOper
->literal
[0] = (GLfloat
) iter
;
3225 /* replace instances of the loop variable with newOper */
3226 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3229 /* do IR codegen for body */
3230 n
= _slang_gen_operation(A
, body
);
3234 root
= new_seq(root
, n
);
3236 slang_operation_delete(body
);
3244 * Replace 'continue' statement with 'break' inside a for-loop.
3245 * This is a recursive helper function used by _slang_gen_for_without_continue().
3248 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3250 switch (oper
->type
) {
3251 case SLANG_OPER_CONTINUE
:
3252 oper
->type
= SLANG_OPER_BREAK
;
3254 case SLANG_OPER_FOR
:
3256 case SLANG_OPER_WHILE
:
3257 /* stop upon finding a nested loop */
3263 for (i
= 0; i
< oper
->num_children
; i
++) {
3264 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3272 * Transform a for-loop so that continue statements are converted to breaks.
3273 * Then do normal IR code generation.
3277 * for (INIT; LOOPCOND; INCR) {
3288 * bool _condFlag = 1;
3289 * for (INIT; _condFlag; ) {
3290 * for ( ; _condFlag = LOOPCOND; INCR) {
3302 static slang_ir_node
*
3303 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3305 slang_operation
*top
;
3306 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3307 slang_operation
*lhs
, *rhs
;
3309 assert(oper
->type
== SLANG_OPER_FOR
);
3311 top
= slang_operation_new(1);
3312 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3313 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3314 slang_operation_add_children(top
, 2);
3316 /* declare: bool _condFlag = true */
3318 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3319 slang_generate_declaration(A
, top
->locals
, condDecl
,
3320 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3323 /* build outer loop: for (INIT; _condFlag; ) { */
3324 outerFor
= slang_oper_child(top
, 1);
3325 outerFor
->type
= SLANG_OPER_FOR
;
3326 slang_operation_add_children(outerFor
, 4);
3328 init
= slang_oper_child(outerFor
, 0);
3329 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3331 cond
= slang_oper_child(outerFor
, 1);
3332 cond
->type
= SLANG_OPER_IDENTIFIER
;
3333 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3335 incr
= slang_oper_child(outerFor
, 2);
3336 incr
->type
= SLANG_OPER_VOID
;
3338 /* body of the outer loop */
3340 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3342 slang_operation_add_children(block
, 2);
3343 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3345 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3347 innerFor
= slang_oper_child(block
, 0);
3349 /* make copy of orig loop */
3350 slang_operation_copy(innerFor
, oper
);
3351 assert(innerFor
->type
== SLANG_OPER_FOR
);
3352 innerFor
->locals
->outer_scope
= block
->locals
;
3354 init
= slang_oper_child(innerFor
, 0);
3355 init
->type
= SLANG_OPER_VOID
; /* leak? */
3357 cond
= slang_oper_child(innerFor
, 1);
3358 slang_operation_destruct(cond
);
3359 cond
->type
= SLANG_OPER_ASSIGN
;
3360 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3361 slang_operation_add_children(cond
, 2);
3363 lhs
= slang_oper_child(cond
, 0);
3364 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3365 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3367 rhs
= slang_oper_child(cond
, 1);
3368 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3371 /* if (_condFlag) INCR; */
3373 slang_operation
*ifop
= slang_oper_child(block
, 1);
3374 ifop
->type
= SLANG_OPER_IF
;
3375 slang_operation_add_children(ifop
, 2);
3377 /* re-use cond node build above */
3378 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3380 /* incr node from original for-loop operation */
3381 slang_operation_copy(slang_oper_child(ifop
, 1),
3382 slang_oper_child(oper
, 2));
3385 /* finally, replace "continue" with "break" in the inner for-loop */
3386 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3389 return _slang_gen_operation(A
, top
);
3395 * Generate IR for a for-loop. Unrolling will be done when possible.
3397 static slang_ir_node
*
3398 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3402 if (!A
->EmitContReturn
) {
3403 /* We don't want to emit CONT instructions. If this for-loop has
3404 * a continue, translate it away.
3406 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3407 return _slang_gen_for_without_continue(A
, oper
);
3411 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3413 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3418 assert(oper
->type
== SLANG_OPER_FOR
);
3420 /* conventional for-loop code generation */
3423 * init code (child[0])
3425 * BREAK if !expr (child[1])
3426 * body code (child[3])
3428 * incr code (child[2]) // XXX continue here
3430 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3431 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3432 loop
= new_loop(NULL
);
3434 /* save loop state */
3435 push_loop(A
, oper
, loop
);
3437 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3438 breakIf
= new_break_if_true(A
, cond
);
3439 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3440 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3442 loop
->Children
[0] = new_seq(breakIf
, body
);
3443 loop
->Children
[1] = incr
; /* tail code */
3445 /* restore loop state */
3448 return new_seq(init
, loop
);
3453 static slang_ir_node
*
3454 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3456 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3458 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3459 loopNode
= current_loop_ir(A
);
3461 assert(loopNode
->Opcode
== IR_LOOP
);
3463 cont
= new_node0(IR_CONT
);
3465 cont
->Parent
= loopNode
;
3466 /* insert this node at head of linked list of cont/break instructions */
3467 cont
->List
= loopNode
->List
;
3468 loopNode
->List
= cont
;
3471 n
= new_seq(incr
, cont
);
3477 * Determine if the given operation is of a specific type.
3480 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3482 if (oper
->type
== type
)
3484 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3485 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3486 oper
->num_children
== 1)
3487 return is_operation_type(&oper
->children
[0], type
);
3494 * Generate IR tree for an if/then/else conditional using high-level
3495 * IR_IF instruction.
3497 static slang_ir_node
*
3498 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3501 * eval expr (child[0])
3508 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3509 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3510 GLboolean isConst
, constTrue
;
3512 /* type-check expression */
3513 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3514 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3518 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3519 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3523 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3527 return _slang_gen_operation(A
, &oper
->children
[1]);
3530 /* if (false) ... */
3531 return _slang_gen_operation(A
, &oper
->children
[2]);
3535 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3536 cond
= new_cond(cond
);
3538 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3539 && !haveElseClause
) {
3540 /* Special case: generate a conditional break */
3541 ifBody
= new_break_if_true(A
, cond
);
3544 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3546 && current_loop_oper(A
)
3547 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3548 /* Special case: generate a conditional continue */
3549 ifBody
= new_cont_if_true(A
, cond
);
3554 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3556 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3559 ifNode
= new_if(cond
, ifBody
, elseBody
);
3566 static slang_ir_node
*
3567 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3571 assert(oper
->type
== SLANG_OPER_NOT
);
3573 /* type-check expression */
3574 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3575 slang_info_log_error(A
->log
,
3576 "scalar/boolean expression expected for '!'");
3580 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3588 static slang_ir_node
*
3589 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3591 slang_ir_node
*n1
, *n2
;
3593 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3595 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3596 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3597 slang_info_log_error(A
->log
,
3598 "scalar/boolean expressions expected for '^^'");
3602 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3605 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3608 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3613 * Generate IR node for storage of a temporary of given size.
3615 static slang_ir_node
*
3616 _slang_gen_temporary(GLint size
)
3618 slang_ir_storage
*store
;
3619 slang_ir_node
*n
= NULL
;
3621 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3623 n
= new_node0(IR_VAR_DECL
);
3636 * Generate program constants for an array.
3637 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3638 * This will allocate and initialize three vector constants, storing
3639 * the array in constant memory, not temporaries like a non-const array.
3640 * This can also be used for uniform array initializers.
3641 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3644 make_constant_array(slang_assemble_ctx
*A
,
3645 slang_variable
*var
,
3646 slang_operation
*initializer
)
3648 struct gl_program
*prog
= A
->program
;
3649 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3650 const char *varName
= (char *) var
->a_name
;
3651 const GLuint numElements
= initializer
->num_children
;
3657 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3659 size
= var
->store
->Size
;
3661 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3662 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3663 assert(initializer
->type
== SLANG_OPER_CALL
);
3664 assert(initializer
->array_constructor
);
3666 values
= (GLfloat
*) malloc(numElements
* 4 * sizeof(GLfloat
));
3668 /* convert constructor params into ordinary floats */
3669 for (i
= 0; i
< numElements
; i
++) {
3670 const slang_operation
*op
= &initializer
->children
[i
];
3671 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3672 /* unsupported type for this optimization */
3676 for (j
= 0; j
< op
->literal_size
; j
++) {
3677 values
[i
* 4 + j
] = op
->literal
[j
];
3679 for ( ; j
< 4; j
++) {
3680 values
[i
* 4 + j
] = 0.0f
;
3684 /* slightly different paths for constants vs. uniforms */
3685 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3686 var
->store
->File
= PROGRAM_UNIFORM
;
3687 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3688 size
, datatype
, values
);
3691 var
->store
->File
= PROGRAM_CONSTANT
;
3692 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3695 assert(var
->store
->Size
== size
);
3705 * Generate IR node for allocating/declaring a variable (either a local or
3707 * Generally, this involves allocating an slang_ir_storage instance for the
3708 * variable, choosing a register file (temporary, constant, etc).
3709 * For ordinary variables we do not yet allocate storage though. We do that
3710 * when we find the first actual use of the variable to avoid allocating temp
3711 * regs that will never get used.
3712 * At this time, uniforms are always allocated space in this function.
3714 * \param initializer Optional initializer expression for the variable.
3716 static slang_ir_node
*
3717 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3718 slang_operation
*initializer
)
3720 const char *varName
= (const char *) var
->a_name
;
3721 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3722 slang_ir_node
*varDecl
, *n
;
3723 slang_ir_storage
*store
;
3724 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3725 gl_register_file file
;
3727 /*assert(!var->declared);*/
3728 var
->declared
= GL_TRUE
;
3730 /* determine GPU register file for simple cases */
3731 if (is_sampler_type(&var
->type
)) {
3732 file
= PROGRAM_SAMPLER
;
3734 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3735 file
= PROGRAM_UNIFORM
;
3738 file
= PROGRAM_TEMPORARY
;
3741 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3743 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3747 arrayLen
= _slang_array_length(var
);
3748 totalSize
= _slang_array_size(size
, arrayLen
);
3750 /* Allocate IR node for the declaration */
3751 varDecl
= new_node0(IR_VAR_DECL
);
3755 /* Allocate slang_ir_storage for this variable if needed.
3756 * Note that we may not actually allocate a constant or temporary register
3760 GLint index
= -7; /* TBD / unknown */
3761 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3763 return NULL
; /* out of memory */
3766 /* set the IR node's Var and Store pointers */
3768 varDecl
->Store
= var
->store
;
3773 /* if there's an initializer, generate IR for the expression */
3775 slang_ir_node
*varRef
, *init
;
3777 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3778 /* if the variable is const, the initializer must be a const
3779 * expression as well.
3782 if (!_slang_is_constant_expr(initializer
)) {
3783 slang_info_log_error(A
->log
,
3784 "initializer for %s not constant", varName
);
3790 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
&&
3791 !A
->allow_uniform_initializers
) {
3792 slang_info_log_error(A
->log
,
3793 "initializer for uniform %s not allowed",
3798 /* IR for the variable we're initializing */
3799 varRef
= new_var(A
, var
);
3801 slang_info_log_error(A
->log
, "out of memory");
3805 /* constant-folding, etc here */
3806 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3808 /* look for simple constant-valued variables and uniforms */
3809 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3810 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3812 if (initializer
->type
== SLANG_OPER_CALL
&&
3813 initializer
->array_constructor
) {
3814 /* array initializer */
3815 if (make_constant_array(A
, var
, initializer
))
3818 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3819 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3820 /* simple float/vector initializer */
3821 if (store
->File
== PROGRAM_UNIFORM
) {
3822 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3824 totalSize
, datatype
,
3825 initializer
->literal
);
3826 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3831 store
->File
= PROGRAM_CONSTANT
;
3832 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3834 initializer
->literal
,
3836 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3843 /* IR for initializer */
3844 init
= _slang_gen_operation(A
, initializer
);
3848 /* XXX remove this when type checking is added above */
3849 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3850 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3854 /* assign RHS to LHS */
3855 n
= new_node2(IR_COPY
, varRef
, init
);
3856 n
= new_seq(varDecl
, n
);
3859 /* no initializer */
3863 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3864 /* always need to allocate storage for uniforms at this point */
3865 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3866 totalSize
, datatype
, NULL
);
3867 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3871 printf("%s var %p %s store=%p index=%d size=%d\n",
3872 __FUNCTION__
, (void *) var
, (char *) varName
,
3873 (void *) store
, store
->Index
, store
->Size
);
3881 * Generate code for a selection expression: b ? x : y
3882 * XXX In some cases we could implement a selection expression
3883 * with an LRP instruction (use the boolean as the interpolant).
3884 * Otherwise, we use an IF/ELSE/ENDIF construct.
3886 static slang_ir_node
*
3887 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3889 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3890 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3891 slang_typeinfo type0
, type1
, type2
;
3892 int size
, isBool
, isEqual
;
3894 assert(oper
->type
== SLANG_OPER_SELECT
);
3895 assert(oper
->num_children
== 3);
3897 /* type of children[0] must be boolean */
3898 slang_typeinfo_construct(&type0
);
3899 typeof_operation(A
, &oper
->children
[0], &type0
);
3900 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3901 slang_typeinfo_destruct(&type0
);
3903 slang_info_log_error(A
->log
, "selector type is not boolean");
3907 slang_typeinfo_construct(&type1
);
3908 slang_typeinfo_construct(&type2
);
3909 typeof_operation(A
, &oper
->children
[1], &type1
);
3910 typeof_operation(A
, &oper
->children
[2], &type2
);
3911 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3912 slang_typeinfo_destruct(&type1
);
3913 slang_typeinfo_destruct(&type2
);
3915 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3919 /* size of x or y's type */
3920 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3924 tmpDecl
= _slang_gen_temporary(size
);
3926 /* the condition (child 0) */
3927 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3928 cond
= new_cond(cond
);
3930 /* if-true body (child 1) */
3931 tmpVar
= new_node0(IR_VAR
);
3932 tmpVar
->Store
= tmpDecl
->Store
;
3933 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3934 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3936 /* if-false body (child 2) */
3937 tmpVar
= new_node0(IR_VAR
);
3938 tmpVar
->Store
= tmpDecl
->Store
;
3939 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3940 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3942 ifNode
= new_if(cond
, trueNode
, falseNode
);
3945 tmpVar
= new_node0(IR_VAR
);
3946 tmpVar
->Store
= tmpDecl
->Store
;
3948 tree
= new_seq(ifNode
, tmpVar
);
3949 tree
= new_seq(tmpDecl
, tree
);
3951 /*_slang_print_ir_tree(tree, 10);*/
3957 * Generate code for &&.
3959 static slang_ir_node
*
3960 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3962 /* rewrite "a && b" as "a ? b : false" */
3963 slang_operation
*select
;
3966 select
= slang_operation_new(1);
3967 select
->type
= SLANG_OPER_SELECT
;
3968 slang_operation_add_children(select
, 3);
3970 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3971 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3972 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3974 n
= _slang_gen_select(A
, select
);
3980 * Generate code for ||.
3982 static slang_ir_node
*
3983 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3985 /* rewrite "a || b" as "a ? true : b" */
3986 slang_operation
*select
;
3989 select
= slang_operation_new(1);
3990 select
->type
= SLANG_OPER_SELECT
;
3991 slang_operation_add_children(select
, 3);
3993 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3994 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3995 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3997 n
= _slang_gen_select(A
, select
);
4003 * Generate IR tree for a return statement.
4005 static slang_ir_node
*
4006 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4008 assert(oper
->type
== SLANG_OPER_RETURN
);
4009 return new_return(A
->curFuncEndLabel
);
4015 * Determine if the given operation/expression is const-valued.
4018 _slang_is_constant_expr(const slang_operation
*oper
)
4020 slang_variable
*var
;
4023 switch (oper
->type
) {
4024 case SLANG_OPER_IDENTIFIER
:
4025 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4026 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4030 for (i
= 0; i
< oper
->num_children
; i
++) {
4031 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4041 * Check if an assignment of type t1 to t0 is legal.
4042 * XXX more cases needed.
4045 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4046 slang_operation
*op0
,
4047 slang_operation
*op1
)
4049 slang_typeinfo t0
, t1
;
4052 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4053 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4057 slang_typeinfo_construct(&t0
);
4058 typeof_operation(A
, op0
, &t0
);
4060 slang_typeinfo_construct(&t1
);
4061 typeof_operation(A
, op1
, &t1
);
4063 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4064 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4068 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4073 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4074 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4075 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4078 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4079 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4082 #if 0 /* not used just yet - causes problems elsewhere */
4083 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4084 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4088 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4089 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4092 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4093 t1
.spec
.type
== SLANG_SPEC_INT
)
4101 * Generate IR tree for a local variable declaration.
4102 * Basically do some error checking and call _slang_gen_var_decl().
4104 static slang_ir_node
*
4105 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4107 const char *varName
= (char *) oper
->a_id
;
4108 slang_variable
*var
;
4109 slang_ir_node
*varDecl
;
4110 slang_operation
*initializer
;
4112 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4113 assert(oper
->num_children
<= 1);
4116 /* lookup the variable by name */
4117 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4119 return NULL
; /* "shouldn't happen" */
4121 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4122 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4123 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4124 /* can't declare attribute/uniform vars inside functions */
4125 slang_info_log_error(A
->log
,
4126 "local variable '%s' cannot be an attribute/uniform/varying",
4133 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4138 /* check if the var has an initializer */
4139 if (oper
->num_children
> 0) {
4140 assert(oper
->num_children
== 1);
4141 initializer
= &oper
->children
[0];
4143 else if (var
->initializer
) {
4144 initializer
= var
->initializer
;
4151 /* check/compare var type and initializer type */
4152 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4153 slang_info_log_error(A
->log
, "incompatible types in assignment");
4158 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4159 slang_info_log_error(A
->log
,
4160 "const-qualified variable '%s' requires initializer",
4166 /* Generate IR node */
4167 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4176 * Generate IR tree for a reference to a variable (such as in an expression).
4177 * This is different from a variable declaration.
4179 static slang_ir_node
*
4180 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4182 /* If there's a variable associated with this oper (from inlining)
4183 * use it. Otherwise, use the oper's var id.
4185 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4186 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4189 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4192 assert(var
->declared
);
4193 n
= new_var(A
, var
);
4200 * Return the number of components actually named by the swizzle.
4201 * Recall that swizzles may have undefined/don't-care values.
4204 swizzle_size(GLuint swizzle
)
4207 for (i
= 0; i
< 4; i
++) {
4208 GLuint swz
= GET_SWZ(swizzle
, i
);
4209 size
+= (swz
>= 0 && swz
<= 3);
4215 static slang_ir_node
*
4216 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4218 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4222 n
->Store
= _slang_new_ir_storage_relative(0,
4223 swizzle_size(swizzle
),
4225 n
->Store
->Swizzle
= swizzle
;
4232 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4234 while (store
->Parent
)
4235 store
= store
->Parent
;
4237 if (!(store
->File
== PROGRAM_OUTPUT
||
4238 store
->File
== PROGRAM_TEMPORARY
||
4239 (store
->File
== PROGRAM_VARYING
&&
4240 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4250 * Walk up an IR storage path to compute the final swizzle.
4251 * This is used when we find an expression such as "foo.xz.yx".
4254 root_swizzle(const slang_ir_storage
*st
)
4256 GLuint swizzle
= st
->Swizzle
;
4257 while (st
->Parent
) {
4259 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4266 * Generate IR tree for an assignment (=).
4268 static slang_ir_node
*
4269 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4271 slang_operation
*pred
= NULL
;
4272 slang_ir_node
*n
= NULL
;
4274 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4275 /* Check that var is writeable */
4276 const char *varName
= (char *) oper
->children
[0].a_id
;
4278 = _slang_variable_locate(oper
->children
[0].locals
,
4279 oper
->children
[0].a_id
, GL_TRUE
);
4281 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
4285 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4286 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4287 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4288 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4289 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4290 slang_info_log_error(A
->log
,
4291 "illegal assignment to read-only variable '%s'",
4296 /* check if we need to predicate this assignment based on __notRetFlag */
4297 if ((var
->is_global
||
4298 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4299 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4300 /* create predicate, used below */
4301 pred
= slang_operation_new(1);
4302 pred
->type
= SLANG_OPER_IDENTIFIER
;
4303 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4304 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4308 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4309 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4310 /* Special case of: x = f(a, b)
4311 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4313 * XXX this could be even more effective if we could accomodate
4314 * cases such as "v.x = f();" - would help with typical vertex
4317 n
= _slang_gen_function_call_name(A
,
4318 (const char *) oper
->children
[1].a_id
,
4319 &oper
->children
[1], &oper
->children
[0]);
4322 slang_ir_node
*lhs
, *rhs
;
4324 /* lhs and rhs type checking */
4325 if (!_slang_assignment_compatible(A
,
4327 &oper
->children
[1])) {
4328 slang_info_log_error(A
->log
, "incompatible types in assignment");
4332 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4338 slang_info_log_error(A
->log
,
4339 "invalid left hand side for assignment");
4343 /* check that lhs is writable */
4344 if (!is_store_writable(A
, lhs
->Store
)) {
4345 slang_info_log_error(A
->log
,
4346 "illegal assignment to read-only l-value");
4350 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4352 /* convert lhs swizzle into writemask */
4353 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4354 GLuint writemask
, newSwizzle
= 0x0;
4355 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4356 /* Non-simple writemask, need to swizzle right hand side in
4357 * order to put components into the right place.
4359 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4361 n
= new_node2(IR_COPY
, lhs
, rhs
);
4369 /* predicate the assignment code on __notRetFlag */
4370 slang_ir_node
*top
, *cond
;
4372 cond
= _slang_gen_operation(A
, pred
);
4373 top
= new_if(cond
, n
, NULL
);
4381 * Generate IR tree for referencing a field in a struct (or basic vector type)
4383 static slang_ir_node
*
4384 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4388 /* type of struct */
4389 slang_typeinfo_construct(&ti
);
4390 typeof_operation(A
, &oper
->children
[0], &ti
);
4392 if (_slang_type_is_vector(ti
.spec
.type
)) {
4393 /* the field should be a swizzle */
4394 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4398 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4399 slang_info_log_error(A
->log
, "Bad swizzle");
4402 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4407 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4408 /* create new parent node with swizzle */
4410 n
= _slang_gen_swizzle(n
, swizzle
);
4413 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4414 || ti
.spec
.type
== SLANG_SPEC_INT
4415 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4416 const GLuint rows
= 1;
4420 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4421 slang_info_log_error(A
->log
, "Bad swizzle");
4423 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4427 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4428 /* create new parent node with swizzle */
4429 n
= _slang_gen_swizzle(n
, swizzle
);
4433 /* the field is a structure member (base.field) */
4434 /* oper->children[0] is the base */
4435 /* oper->a_id is the field name */
4436 slang_ir_node
*base
, *n
;
4437 slang_typeinfo field_ti
;
4438 GLint fieldSize
, fieldOffset
= -1;
4441 slang_typeinfo_construct(&field_ti
);
4442 typeof_operation(A
, oper
, &field_ti
);
4444 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4446 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4448 if (fieldSize
== 0 || fieldOffset
< 0) {
4449 const char *structName
;
4450 if (ti
.spec
._struct
)
4451 structName
= (char *) ti
.spec
._struct
->a_name
;
4453 structName
= "unknown";
4454 slang_info_log_error(A
->log
,
4455 "\"%s\" is not a member of struct \"%s\"",
4456 (char *) oper
->a_id
, structName
);
4459 assert(fieldSize
>= 0);
4461 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4463 /* error msg should have already been logged */
4467 n
= new_node1(IR_FIELD
, base
);
4471 n
->Field
= (char *) oper
->a_id
;
4473 /* Store the field's offset in storage->Index */
4474 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4484 * Gen code for array indexing.
4486 static slang_ir_node
*
4487 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4489 slang_typeinfo array_ti
;
4491 /* get array's type info */
4492 slang_typeinfo_construct(&array_ti
);
4493 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4495 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4496 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4497 /* translate the index into a swizzle/writemask: "v.x=p" */
4498 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4502 index
= (GLint
) oper
->children
[1].literal
[0];
4503 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4504 index
>= (GLint
) max
) {
4506 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4507 printf("type = %d\n", oper
->children
[1].type
);
4508 printf("index = %d, max = %d\n", index
, max
);
4509 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4510 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4517 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4519 /* use swizzle to access the element */
4520 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4524 n
= _slang_gen_swizzle(n
, swizzle
);
4530 /* conventional array */
4531 slang_typeinfo elem_ti
;
4532 slang_ir_node
*elem
, *array
, *index
;
4533 GLint elemSize
, arrayLen
;
4535 /* size of array element */
4536 slang_typeinfo_construct(&elem_ti
);
4537 typeof_operation(A
, oper
, &elem_ti
);
4538 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4540 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4541 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4543 arrayLen
= array_ti
.array_len
;
4545 slang_typeinfo_destruct(&array_ti
);
4546 slang_typeinfo_destruct(&elem_ti
);
4548 if (elemSize
<= 0) {
4549 /* unknown var or type */
4550 slang_info_log_error(A
->log
, "Undefined variable or type");
4554 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4555 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4556 if (array
&& index
) {
4558 GLint constIndex
= -1;
4559 if (index
->Opcode
== IR_FLOAT
) {
4560 constIndex
= (int) index
->Value
[0];
4561 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4562 slang_info_log_error(A
->log
,
4563 "Array index out of bounds (index=%d size=%d)",
4564 constIndex
, arrayLen
);
4565 _slang_free_ir_tree(array
);
4566 _slang_free_ir_tree(index
);
4571 if (!array
->Store
) {
4572 slang_info_log_error(A
->log
, "Invalid array");
4576 elem
= new_node2(IR_ELEMENT
, array
, index
);
4578 /* The storage info here will be updated during code emit */
4579 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4580 array
->Store
->Index
,
4582 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4586 _slang_free_ir_tree(array
);
4587 _slang_free_ir_tree(index
);
4594 static slang_ir_node
*
4595 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4596 slang_ir_opcode opcode
)
4598 slang_typeinfo t0
, t1
;
4601 slang_typeinfo_construct(&t0
);
4602 typeof_operation(A
, &oper
->children
[0], &t0
);
4604 slang_typeinfo_construct(&t1
);
4605 typeof_operation(A
, &oper
->children
[0], &t1
);
4607 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4608 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4609 slang_info_log_error(A
->log
, "Illegal array comparison");
4613 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4614 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4615 /* <, <=, >, >= can only be used with scalars */
4616 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4617 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4618 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4619 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4620 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4625 n
= new_node2(opcode
,
4626 _slang_gen_operation(A
, &oper
->children
[0]),
4627 _slang_gen_operation(A
, &oper
->children
[1]));
4629 /* result is a bool (size 1) */
4630 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4638 print_vars(slang_variable_scope
*s
)
4642 for (i
= 0; i
< s
->num_variables
; i
++) {
4644 (char*) s
->variables
[i
]->a_name
,
4645 s
->variables
[i
]->declared
);
4655 _slang_undeclare_vars(slang_variable_scope
*locals
)
4657 if (locals
->num_variables
> 0) {
4659 for (i
= 0; i
< locals
->num_variables
; i
++) {
4660 slang_variable
*v
= locals
->variables
[i
];
4661 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4662 v
->declared
= GL_FALSE
;
4670 * Generate IR tree for a slang_operation (AST node)
4672 static slang_ir_node
*
4673 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4675 switch (oper
->type
) {
4676 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4680 _slang_push_var_table(A
->vartable
);
4682 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4683 n
= _slang_gen_operation(A
, oper
);
4684 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4686 _slang_pop_var_table(A
->vartable
);
4688 /*_slang_undeclare_vars(oper->locals);*/
4689 /*print_vars(oper->locals);*/
4692 n
= new_node1(IR_SCOPE
, n
);
4697 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4698 /* list of operations */
4699 if (oper
->num_children
> 0)
4701 slang_ir_node
*n
, *tree
= NULL
;
4704 for (i
= 0; i
< oper
->num_children
; i
++) {
4705 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4707 _slang_free_ir_tree(tree
);
4708 return NULL
; /* error must have occured */
4710 tree
= new_seq(tree
, n
);
4716 return new_node0(IR_NOP
);
4719 case SLANG_OPER_EXPRESSION
:
4720 return _slang_gen_operation(A
, &oper
->children
[0]);
4722 case SLANG_OPER_FOR
:
4723 return _slang_gen_for(A
, oper
);
4725 return _slang_gen_do(A
, oper
);
4726 case SLANG_OPER_WHILE
:
4727 return _slang_gen_while(A
, oper
);
4728 case SLANG_OPER_BREAK
:
4729 if (!current_loop_oper(A
)) {
4730 slang_info_log_error(A
->log
, "'break' not in loop");
4733 return new_break(current_loop_ir(A
));
4734 case SLANG_OPER_CONTINUE
:
4735 if (!current_loop_oper(A
)) {
4736 slang_info_log_error(A
->log
, "'continue' not in loop");
4739 return _slang_gen_continue(A
, oper
);
4740 case SLANG_OPER_DISCARD
:
4741 return new_node0(IR_KILL
);
4743 case SLANG_OPER_EQUAL
:
4744 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4745 case SLANG_OPER_NOTEQUAL
:
4746 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4747 case SLANG_OPER_GREATER
:
4748 return _slang_gen_compare(A
, oper
, IR_SGT
);
4749 case SLANG_OPER_LESS
:
4750 return _slang_gen_compare(A
, oper
, IR_SLT
);
4751 case SLANG_OPER_GREATEREQUAL
:
4752 return _slang_gen_compare(A
, oper
, IR_SGE
);
4753 case SLANG_OPER_LESSEQUAL
:
4754 return _slang_gen_compare(A
, oper
, IR_SLE
);
4755 case SLANG_OPER_ADD
:
4758 assert(oper
->num_children
== 2);
4759 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4762 case SLANG_OPER_SUBTRACT
:
4765 assert(oper
->num_children
== 2);
4766 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4769 case SLANG_OPER_MULTIPLY
:
4772 assert(oper
->num_children
== 2);
4773 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4776 case SLANG_OPER_DIVIDE
:
4779 assert(oper
->num_children
== 2);
4780 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4783 case SLANG_OPER_MINUS
:
4786 assert(oper
->num_children
== 1);
4787 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4790 case SLANG_OPER_PLUS
:
4791 /* +expr --> do nothing */
4792 return _slang_gen_operation(A
, &oper
->children
[0]);
4793 case SLANG_OPER_VARIABLE_DECL
:
4794 return _slang_gen_declaration(A
, oper
);
4795 case SLANG_OPER_ASSIGN
:
4796 return _slang_gen_assignment(A
, oper
);
4797 case SLANG_OPER_ADDASSIGN
:
4800 assert(oper
->num_children
== 2);
4801 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4804 case SLANG_OPER_SUBASSIGN
:
4807 assert(oper
->num_children
== 2);
4808 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4812 case SLANG_OPER_MULASSIGN
:
4815 assert(oper
->num_children
== 2);
4816 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4819 case SLANG_OPER_DIVASSIGN
:
4822 assert(oper
->num_children
== 2);
4823 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4826 case SLANG_OPER_LOGICALAND
:
4829 assert(oper
->num_children
== 2);
4830 n
= _slang_gen_logical_and(A
, oper
);
4833 case SLANG_OPER_LOGICALOR
:
4836 assert(oper
->num_children
== 2);
4837 n
= _slang_gen_logical_or(A
, oper
);
4840 case SLANG_OPER_LOGICALXOR
:
4841 return _slang_gen_xor(A
, oper
);
4842 case SLANG_OPER_NOT
:
4843 return _slang_gen_not(A
, oper
);
4844 case SLANG_OPER_SELECT
: /* b ? x : y */
4847 assert(oper
->num_children
== 3);
4848 n
= _slang_gen_select(A
, oper
);
4852 case SLANG_OPER_ASM
:
4853 return _slang_gen_asm(A
, oper
, NULL
);
4854 case SLANG_OPER_CALL
:
4855 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4857 case SLANG_OPER_METHOD
:
4858 return _slang_gen_method_call(A
, oper
);
4859 case SLANG_OPER_RETURN
:
4860 return _slang_gen_return(A
, oper
);
4861 case SLANG_OPER_RETURN_INLINED
:
4862 return _slang_gen_return(A
, oper
);
4863 case SLANG_OPER_LABEL
:
4864 return new_label(oper
->label
);
4865 case SLANG_OPER_IDENTIFIER
:
4866 return _slang_gen_variable(A
, oper
);
4868 return _slang_gen_if(A
, oper
);
4869 case SLANG_OPER_FIELD
:
4870 return _slang_gen_struct_field(A
, oper
);
4871 case SLANG_OPER_SUBSCRIPT
:
4872 return _slang_gen_array_element(A
, oper
);
4873 case SLANG_OPER_LITERAL_FLOAT
:
4875 case SLANG_OPER_LITERAL_INT
:
4877 case SLANG_OPER_LITERAL_BOOL
:
4878 return new_float_literal(oper
->literal
, oper
->literal_size
);
4880 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4883 assert(oper
->num_children
== 1);
4884 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4887 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4890 assert(oper
->num_children
== 1);
4891 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4894 case SLANG_OPER_PREINCREMENT
: /* ++var */
4897 assert(oper
->num_children
== 1);
4898 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4901 case SLANG_OPER_PREDECREMENT
: /* --var */
4904 assert(oper
->num_children
== 1);
4905 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4909 case SLANG_OPER_NON_INLINED_CALL
:
4910 case SLANG_OPER_SEQUENCE
:
4912 slang_ir_node
*tree
= NULL
;
4914 for (i
= 0; i
< oper
->num_children
; i
++) {
4915 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4916 tree
= new_seq(tree
, n
);
4918 tree
->Store
= n
->Store
;
4920 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4921 tree
= new_function_call(tree
, oper
->label
);
4926 case SLANG_OPER_NONE
:
4927 case SLANG_OPER_VOID
:
4928 /* returning NULL here would generate an error */
4929 return new_node0(IR_NOP
);
4932 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4934 return new_node0(IR_NOP
);
4942 * Check if the given type specifier is a rectangular texture sampler.
4945 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4947 while (spec
->_array
) {
4948 spec
= spec
->_array
;
4950 return spec
->type
== SLANG_SPEC_SAMPLER_RECT
||
4951 spec
->type
== SLANG_SPEC_SAMPLER_RECT_SHADOW
;
4957 * Called by compiler when a global variable has been parsed/compiled.
4958 * Here we examine the variable's type to determine what kind of register
4959 * storage will be used.
4961 * A uniform such as "gl_Position" will become the register specification
4962 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4963 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4965 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4966 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4967 * actual texture unit (as specified by the user calling glUniform1i()).
4970 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4971 slang_unit_type type
)
4973 struct gl_program
*prog
= A
->program
;
4974 const char *varName
= (char *) var
->a_name
;
4975 GLboolean success
= GL_TRUE
;
4976 slang_ir_storage
*store
= NULL
;
4978 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4979 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4980 const GLint arrayLen
= _slang_array_length(var
);
4981 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4982 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4984 var
->is_global
= GL_TRUE
;
4986 /* check for sampler2D arrays */
4987 if (texIndex
== -1 && var
->type
.specifier
._array
)
4988 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4990 if (texIndex
!= -1) {
4991 /* This is a texture sampler variable...
4992 * store->File = PROGRAM_SAMPLER
4993 * store->Index = sampler number (0..7, typically)
4994 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4996 if (var
->initializer
) {
4997 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5000 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5001 /* disallow rect samplers */
5002 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
5003 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5007 (void) is_rect_sampler_spec
; /* silence warning */
5010 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5011 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5013 /* If we have a sampler array, then we need to allocate the
5014 * additional samplers to ensure we don't allocate them elsewhere.
5015 * We can't directly use _mesa_add_sampler() as that checks the
5016 * varName and gets a match, so we call _mesa_add_parameter()
5017 * directly and use the last sampler number from the call above.
5020 GLint a
= arrayLen
- 1;
5022 for (i
= 0; i
< a
; i
++) {
5023 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5024 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5025 varName
, 1, datatype
, &value
, NULL
, 0x0);
5029 if (dbg
) printf("SAMPLER ");
5031 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5032 /* Uniform variable */
5033 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5036 /* user-defined uniform */
5037 if (datatype
== GL_NONE
) {
5038 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5039 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5040 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5041 /* temporary work-around */
5042 GLenum datatype
= GL_FLOAT
;
5043 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5044 totalSize
, datatype
, NULL
);
5045 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5046 totalSize
, swizzle
);
5049 GLint a
= arrayLen
- 1;
5051 for (i
= 0; i
< a
; i
++) {
5052 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5053 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5054 varName
, 1, datatype
, &value
, NULL
, 0x0);
5058 /* XXX what we need to do is unroll the struct into its
5059 * basic types, creating a uniform variable for each.
5067 * Should produce uniforms:
5068 * "f.a" (GL_FLOAT_VEC3)
5069 * "f.b" (GL_FLOAT_VEC4)
5072 if (var
->initializer
) {
5073 slang_info_log_error(A
->log
,
5074 "unsupported initializer for uniform '%s'", varName
);
5079 slang_info_log_error(A
->log
,
5080 "invalid datatype for uniform variable %s",
5086 /* non-struct uniform */
5087 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5093 /* pre-defined uniform, like gl_ModelviewMatrix */
5094 /* We know it's a uniform, but don't allocate storage unless
5097 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5098 totalSize
, swizzle
);
5100 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5102 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5103 /* varyings must be float, vec or mat */
5104 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5105 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5106 slang_info_log_error(A
->log
,
5107 "varying '%s' must be float/vector/matrix",
5112 if (var
->initializer
) {
5113 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5119 /* user-defined varying */
5125 if (var
->type
.centroid
== SLANG_CENTROID
)
5126 flags
|= PROG_PARAM_BIT_CENTROID
;
5127 if (var
->type
.variant
== SLANG_INVARIANT
)
5128 flags
|= PROG_PARAM_BIT_INVARIANT
;
5130 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5132 swizzle
= _slang_var_swizzle(size
, 0);
5133 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5134 totalSize
, swizzle
);
5137 /* pre-defined varying, like gl_Color or gl_TexCoord */
5138 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5139 /* fragment program input */
5141 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5144 assert(index
< FRAG_ATTRIB_MAX
);
5145 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5149 /* vertex program output */
5150 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5151 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5153 assert(index
< VERT_RESULT_MAX
);
5154 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5155 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5158 if (dbg
) printf("V/F ");
5160 if (dbg
) printf("VARYING ");
5162 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5165 /* attributes must be float, vec or mat */
5166 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5167 slang_info_log_error(A
->log
,
5168 "attribute '%s' must be float/vector/matrix",
5174 /* user-defined vertex attribute */
5175 const GLint attr
= -1; /* unknown */
5176 swizzle
= _slang_var_swizzle(size
, 0);
5177 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5178 size
, datatype
, attr
);
5180 index
= VERT_ATTRIB_GENERIC0
+ index
;
5183 /* pre-defined vertex attrib */
5184 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5187 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5188 if (dbg
) printf("ATTRIB ");
5190 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5191 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5192 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5194 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5195 if (dbg
) printf("INPUT ");
5197 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5198 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5199 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5200 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5203 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5204 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5205 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5206 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5208 if (dbg
) printf("OUTPUT ");
5210 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5211 /* pre-defined global constant, like gl_MaxLights */
5212 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5213 if (dbg
) printf("CONST ");
5216 /* ordinary variable (may be const) */
5219 /* IR node to declare the variable */
5220 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5222 /* emit GPU instructions */
5223 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5225 _slang_free_ir_tree(n
);
5228 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5229 store
? store
->Index
: -2);
5232 var
->store
= store
; /* save var's storage info */
5234 var
->declared
= GL_TRUE
;
5241 * Produce an IR tree from a function AST (fun->body).
5242 * Then call the code emitter to convert the IR tree into gl_program
5246 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5249 GLboolean success
= GL_TRUE
;
5251 if (strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5252 /* we only really generate code for main, all other functions get
5253 * inlined or codegen'd upon an actual call.
5256 /* do some basic error checking though */
5257 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5258 /* check that non-void functions actually return something */
5260 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5262 slang_info_log_error(A
->log
,
5263 "function \"%s\" has no return statement",
5264 (char *) fun
->header
.a_name
);
5266 "function \"%s\" has no return statement\n",
5267 (char *) fun
->header
.a_name
);
5272 return GL_TRUE
; /* not an error */
5276 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5277 slang_print_function(fun
, 1);
5280 /* should have been allocated earlier: */
5281 assert(A
->program
->Parameters
);
5282 assert(A
->program
->Varying
);
5283 assert(A
->vartable
);
5286 A
->UseReturnFlag
= GL_FALSE
;
5287 A
->CurFunction
= fun
;
5289 /* fold constant expressions, etc. */
5290 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5293 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5294 slang_print_function(fun
, 1);
5297 /* Create an end-of-function label */
5298 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5300 /* push new vartable scope */
5301 _slang_push_var_table(A
->vartable
);
5303 /* Generate IR tree for the function body code */
5304 n
= _slang_gen_operation(A
, fun
->body
);
5306 n
= new_node1(IR_SCOPE
, n
);
5308 /* pop vartable, restore previous */
5309 _slang_pop_var_table(A
->vartable
);
5312 /* XXX record error */
5316 /* append an end-of-function-label to IR tree */
5317 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5319 /*_slang_label_delete(A->curFuncEndLabel);*/
5320 A
->curFuncEndLabel
= NULL
;
5323 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5324 slang_print_function(fun
, 1);
5327 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5328 _slang_print_ir_tree(n
, 0);
5331 printf("************* End codegen function ************\n\n");
5334 if (A
->UnresolvedRefs
) {
5335 /* Can't codegen at this time.
5336 * At link time we'll concatenate all the vertex shaders and/or all
5337 * the fragment shaders and try recompiling.
5342 /* Emit program instructions */
5343 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5344 _slang_free_ir_tree(n
);
5346 /* free codegen context */