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 _mesa_snprintf(name
, sizeof(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
) {
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
);
1899 assert(info
->NumParams
<= 3);
1901 if (info
->NumParams
== oper
->num_children
) {
1902 /* Storage for result is not specified.
1903 * Children[0], [1], [2] are the operands.
1908 /* Storage for result (child[0]) is specified.
1909 * Children[1], [2], [3] are the operands.
1914 /* assemble child(ren) */
1915 kids
[0] = kids
[1] = kids
[2] = NULL
;
1916 for (j
= 0; j
< info
->NumParams
; j
++) {
1917 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1922 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1925 /* Setup n->Store to be a particular location. Otherwise, storage
1926 * for the result (a temporary) will be allocated later.
1928 slang_operation
*dest_oper
;
1931 dest_oper
= &oper
->children
[0];
1933 n0
= _slang_gen_operation(A
, dest_oper
);
1938 n
->Store
= n0
->Store
;
1940 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1951 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1954 for (i
= 0; i
< scope
->num_functions
; i
++) {
1955 slang_function
*f
= &scope
->functions
[i
];
1956 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1957 printf(" %s (%d args)\n", name
, f
->param_count
);
1960 if (scope
->outer_scope
)
1961 print_funcs(scope
->outer_scope
, name
);
1967 * Find a function of the given name, taking 'numArgs' arguments.
1968 * This is the function we'll try to call when there is no exact match
1969 * between function parameters and call arguments.
1971 * XXX we should really create a list of candidate functions and try
1974 static slang_function
*
1975 _slang_find_function_by_argc(slang_function_scope
*scope
,
1976 const char *name
, int numArgs
)
1980 for (i
= 0; i
< scope
->num_functions
; i
++) {
1981 slang_function
*f
= &scope
->functions
[i
];
1982 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1983 int haveRetValue
= _slang_function_has_return_value(f
);
1984 if (numArgs
== f
->param_count
- haveRetValue
)
1988 scope
= scope
->outer_scope
;
1995 static slang_function
*
1996 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1999 slang_function
*maxFunc
= NULL
;
2004 for (i
= 0; i
< scope
->num_functions
; i
++) {
2005 slang_function
*f
= &scope
->functions
[i
];
2006 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2007 if (f
->param_count
> maxArgs
) {
2008 maxArgs
= f
->param_count
;
2013 scope
= scope
->outer_scope
;
2021 * Generate a new slang_function which is a constructor for a user-defined
2024 static slang_function
*
2025 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2027 const GLint numFields
= str
->fields
->num_variables
;
2028 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2030 /* function header (name, return type) */
2031 fun
->header
.a_name
= str
->a_name
;
2032 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2033 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2034 fun
->header
.type
.specifier
._struct
= str
;
2036 /* function parameters (= struct's fields) */
2039 for (i
= 0; i
< numFields
; i
++) {
2041 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2043 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2044 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2045 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2047 fun
->param_count
= fun
->parameters
->num_variables
;
2050 /* Add __retVal to params */
2052 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2053 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2055 p
->a_name
= a_retVal
;
2056 p
->type
= fun
->header
.type
;
2057 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2061 /* function body is:
2071 slang_variable_scope
*scope
;
2072 slang_variable
*var
;
2075 fun
->body
= slang_operation_new(1);
2076 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2077 fun
->body
->num_children
= numFields
+ 2;
2078 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2080 scope
= fun
->body
->locals
;
2081 scope
->outer_scope
= fun
->parameters
;
2083 /* create local var 't' */
2084 var
= slang_variable_scope_grow(scope
);
2085 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2086 var
->type
= fun
->header
.type
;
2090 slang_operation
*decl
;
2092 decl
= &fun
->body
->children
[0];
2093 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2094 decl
->locals
= _slang_variable_scope_new(scope
);
2095 decl
->a_id
= var
->a_name
;
2098 /* assign params to fields of t */
2099 for (i
= 0; i
< numFields
; i
++) {
2100 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2102 assign
->type
= SLANG_OPER_ASSIGN
;
2103 assign
->locals
= _slang_variable_scope_new(scope
);
2104 assign
->num_children
= 2;
2105 assign
->children
= slang_operation_new(2);
2108 slang_operation
*lhs
= &assign
->children
[0];
2110 lhs
->type
= SLANG_OPER_FIELD
;
2111 lhs
->locals
= _slang_variable_scope_new(scope
);
2112 lhs
->num_children
= 1;
2113 lhs
->children
= slang_operation_new(1);
2114 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2116 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2117 lhs
->children
[0].a_id
= var
->a_name
;
2118 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2121 lhs
->children
[1].num_children
= 1;
2122 lhs
->children
[1].children
= slang_operation_new(1);
2123 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2124 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2125 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2130 slang_operation
*rhs
= &assign
->children
[1];
2132 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2133 rhs
->locals
= _slang_variable_scope_new(scope
);
2134 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2140 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2142 ret
->type
= SLANG_OPER_RETURN
;
2143 ret
->locals
= _slang_variable_scope_new(scope
);
2144 ret
->num_children
= 1;
2145 ret
->children
= slang_operation_new(1);
2146 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2147 ret
->children
[0].a_id
= var
->a_name
;
2148 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2152 slang_print_function(fun, 1);
2159 * Find/create a function (constructor) for the given structure name.
2161 static slang_function
*
2162 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2165 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2166 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2167 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2168 /* found a structure type that matches the function name */
2169 if (!str
->constructor
) {
2170 /* create the constructor function now */
2171 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2173 return str
->constructor
;
2181 * Generate a new slang_function to satisfy a call to an array constructor.
2182 * Ex: float[3](1., 2., 3.)
2184 static slang_function
*
2185 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2187 slang_type_specifier_type baseType
;
2188 slang_function
*fun
;
2191 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2195 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2197 num_elements
= oper
->num_children
;
2199 /* function header, return type */
2201 fun
->header
.a_name
= oper
->a_id
;
2202 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2203 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2204 fun
->header
.type
.specifier
._array
=
2205 slang_type_specifier_new(baseType
, NULL
, NULL
);
2206 fun
->header
.type
.array_len
= num_elements
;
2209 /* function parameters (= number of elements) */
2212 for (i
= 0; i
< num_elements
; i
++) {
2214 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2216 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2218 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2219 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2220 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2221 p
->type
.specifier
.type
= baseType
;
2223 fun
->param_count
= fun
->parameters
->num_variables
;
2226 /* Add __retVal to params */
2228 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2229 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2231 p
->a_name
= a_retVal
;
2232 p
->type
= fun
->header
.type
;
2233 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2234 p
->type
.specifier
.type
= baseType
;
2238 /* function body is:
2248 slang_variable_scope
*scope
;
2249 slang_variable
*var
;
2252 fun
->body
= slang_operation_new(1);
2253 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2254 fun
->body
->num_children
= num_elements
+ 2;
2255 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2257 scope
= fun
->body
->locals
;
2258 scope
->outer_scope
= fun
->parameters
;
2260 /* create local var 't' */
2261 var
= slang_variable_scope_grow(scope
);
2262 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2263 var
->type
= fun
->header
.type
;/*XXX copy*/
2267 slang_operation
*decl
;
2269 decl
= &fun
->body
->children
[0];
2270 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2271 decl
->locals
= _slang_variable_scope_new(scope
);
2272 decl
->a_id
= var
->a_name
;
2275 /* assign params to elements of t */
2276 for (i
= 0; i
< num_elements
; i
++) {
2277 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2279 assign
->type
= SLANG_OPER_ASSIGN
;
2280 assign
->locals
= _slang_variable_scope_new(scope
);
2281 assign
->num_children
= 2;
2282 assign
->children
= slang_operation_new(2);
2285 slang_operation
*lhs
= &assign
->children
[0];
2287 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2288 lhs
->locals
= _slang_variable_scope_new(scope
);
2289 lhs
->num_children
= 2;
2290 lhs
->children
= slang_operation_new(2);
2292 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2293 lhs
->children
[0].a_id
= var
->a_name
;
2294 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2296 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2297 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2301 slang_operation
*rhs
= &assign
->children
[1];
2303 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2304 rhs
->locals
= _slang_variable_scope_new(scope
);
2305 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2311 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2313 ret
->type
= SLANG_OPER_RETURN
;
2314 ret
->locals
= _slang_variable_scope_new(scope
);
2315 ret
->num_children
= 1;
2316 ret
->children
= slang_operation_new(1);
2317 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2318 ret
->children
[0].a_id
= var
->a_name
;
2319 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2324 slang_print_function(fun, 1);
2332 _slang_is_vec_mat_type(const char *name
)
2334 static const char *vecmat_types
[] = {
2335 "float", "int", "bool",
2336 "vec2", "vec3", "vec4",
2337 "ivec2", "ivec3", "ivec4",
2338 "bvec2", "bvec3", "bvec4",
2339 "mat2", "mat3", "mat4",
2340 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2344 for (i
= 0; vecmat_types
[i
]; i
++)
2345 if (strcmp(name
, vecmat_types
[i
]) == 0)
2352 * Assemble a function call, given a particular function name.
2353 * \param name the function's name (operators like '*' are possible).
2355 static slang_ir_node
*
2356 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2357 slang_operation
*oper
, slang_operation
*dest
)
2359 slang_operation
*params
= oper
->children
;
2360 const GLuint param_count
= oper
->num_children
;
2362 slang_function
*fun
;
2365 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2366 if (atom
== SLANG_ATOM_NULL
)
2369 if (oper
->array_constructor
) {
2370 /* this needs special handling */
2371 fun
= _slang_make_array_constructor(A
, oper
);
2374 /* Try to find function by name and exact argument type matching */
2375 GLboolean error
= GL_FALSE
;
2376 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2377 &A
->space
, A
->atoms
, A
->log
, &error
);
2379 slang_info_log_error(A
->log
,
2380 "Function '%s' not found (check argument types)",
2387 /* Next, try locating a constructor function for a user-defined type */
2388 fun
= _slang_locate_struct_constructor(A
, name
);
2392 * At this point, some heuristics are used to try to find a function
2393 * that matches the calling signature by means of casting or "unrolling"
2397 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2398 /* Next, if this call looks like a vec() or mat() constructor call,
2399 * try "unwinding" the args to satisfy a constructor.
2401 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2403 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2404 slang_info_log_error(A
->log
,
2405 "Function '%s' not found (check argument types)",
2412 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2413 /* Next, try casting args to the types of the formal parameters */
2414 int numArgs
= oper
->num_children
;
2415 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2416 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2417 slang_info_log_error(A
->log
,
2418 "Function '%s' not found (check argument types)",
2426 slang_info_log_error(A
->log
,
2427 "Function '%s' not found (check argument types)",
2433 /* The function body may be in another compilation unit.
2434 * We'll try concatenating the shaders and recompile at link time.
2436 A
->UnresolvedRefs
= GL_TRUE
;
2437 return new_node1(IR_NOP
, NULL
);
2440 /* type checking to be sure function's return type matches 'dest' type */
2444 slang_typeinfo_construct(&t0
);
2445 typeof_operation(A
, dest
, &t0
);
2447 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2448 slang_info_log_error(A
->log
,
2449 "Incompatible type returned by call to '%s'",
2455 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2457 if (n
&& !n
->Store
&& !dest
2458 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2459 /* setup n->Store for the result of the function call */
2460 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2461 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2462 /*printf("Alloc storage for function result, size %d \n", size);*/
2465 if (oper
->array_constructor
) {
2466 /* free the temporary array constructor function now */
2467 slang_function_destruct(fun
);
2474 static slang_ir_node
*
2475 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2477 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2479 slang_variable
*var
;
2481 /* NOTE: In GLSL 1.20, there's only one kind of method
2482 * call: array.length(). Anything else is an error.
2484 if (oper
->a_id
!= a_length
) {
2485 slang_info_log_error(A
->log
,
2486 "Undefined method call '%s'", (char *) oper
->a_id
);
2490 /* length() takes no arguments */
2491 if (oper
->num_children
> 0) {
2492 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2496 /* lookup the object/variable */
2497 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2498 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2499 slang_info_log_error(A
->log
,
2500 "Undefined object '%s'", (char *) oper
->a_obj
);
2504 /* Create a float/literal IR node encoding the array length */
2505 n
= new_node0(IR_FLOAT
);
2507 n
->Value
[0] = (float) _slang_array_length(var
);
2508 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2515 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2517 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2518 oper
->type
== SLANG_OPER_LITERAL_INT
||
2519 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2520 if (oper
->literal
[0])
2526 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2527 oper
->num_children
== 1) {
2528 return _slang_is_constant_cond(&oper
->children
[0], value
);
2535 * Test if an operation is a scalar or boolean.
2538 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2540 slang_typeinfo type
;
2543 slang_typeinfo_construct(&type
);
2544 typeof_operation(A
, oper
, &type
);
2545 size
= _slang_sizeof_type_specifier(&type
.spec
);
2546 slang_typeinfo_destruct(&type
);
2552 * Test if an operation is boolean.
2555 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2557 slang_typeinfo type
;
2560 slang_typeinfo_construct(&type
);
2561 typeof_operation(A
, oper
, &type
);
2562 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2563 slang_typeinfo_destruct(&type
);
2569 * Check if a loop contains a 'continue' statement.
2570 * Stop looking if we find a nested loop.
2573 _slang_loop_contains_continue(const slang_operation
*oper
)
2575 switch (oper
->type
) {
2576 case SLANG_OPER_CONTINUE
:
2578 case SLANG_OPER_FOR
:
2580 case SLANG_OPER_WHILE
:
2581 /* stop upon finding a nested loop */
2587 for (i
= 0; i
< oper
->num_children
; i
++) {
2588 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2589 if (_slang_loop_contains_continue(child
))
2599 * Check if a loop contains a 'continue' or 'break' statement.
2600 * Stop looking if we find a nested loop.
2603 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2605 switch (oper
->type
) {
2606 case SLANG_OPER_CONTINUE
:
2607 case SLANG_OPER_BREAK
:
2609 case SLANG_OPER_FOR
:
2611 case SLANG_OPER_WHILE
:
2612 /* stop upon finding a nested loop */
2618 for (i
= 0; i
< oper
->num_children
; i
++) {
2619 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2620 if (_slang_loop_contains_continue_or_break(child
))
2630 * Replace 'break' and 'continue' statements inside a do and while loops.
2631 * This is a recursive helper function used by
2632 * _slang_gen_do/while_without_continue().
2635 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2637 switch (oper
->type
) {
2638 case SLANG_OPER_BREAK
:
2639 /* replace 'break' with "_notBreakFlag = false; break" */
2641 slang_operation
*block
= oper
;
2642 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2643 slang_operation_add_children(block
, 2);
2645 slang_operation
*assign
= slang_oper_child(block
, 0);
2646 assign
->type
= SLANG_OPER_ASSIGN
;
2647 slang_operation_add_children(assign
, 2);
2649 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2650 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2653 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2654 slang_operation_literal_bool(rhs
, GL_FALSE
);
2658 slang_operation
*brk
= slang_oper_child(block
, 1);
2659 brk
->type
= SLANG_OPER_BREAK
;
2660 assert(!brk
->children
);
2664 case SLANG_OPER_CONTINUE
:
2665 /* convert continue into a break */
2666 oper
->type
= SLANG_OPER_BREAK
;
2668 case SLANG_OPER_FOR
:
2670 case SLANG_OPER_WHILE
:
2671 /* stop upon finding a nested loop */
2677 for (i
= 0; i
< oper
->num_children
; i
++) {
2678 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2686 * Transform a while-loop so that continue statements are converted to breaks.
2687 * Then do normal IR code generation.
2691 * while (LOOPCOND) {
2703 * bool _notBreakFlag = 1;
2704 * while (_notBreakFlag && LOOPCOND) {
2708 * break; // was continue
2711 * _notBreakFlag = 0; // was
2718 static slang_ir_node
*
2719 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2721 slang_operation
*top
;
2722 slang_operation
*innerBody
;
2724 assert(oper
->type
== SLANG_OPER_WHILE
);
2726 top
= slang_operation_new(1);
2727 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2728 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2729 slang_operation_add_children(top
, 2);
2731 /* declare: bool _notBreakFlag = true */
2733 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2734 slang_generate_declaration(A
, top
->locals
, condDecl
,
2735 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2738 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2740 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2741 outerWhile
->type
= SLANG_OPER_WHILE
;
2742 slang_operation_add_children(outerWhile
, 2);
2744 /* _notBreakFlag && LOOPCOND */
2746 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2747 cond
->type
= SLANG_OPER_LOGICALAND
;
2748 slang_operation_add_children(cond
, 2);
2750 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2751 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2754 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2755 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2761 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2762 innerDo
->type
= SLANG_OPER_DO
;
2763 slang_operation_add_children(innerDo
, 2);
2765 /* copy original do-loop body into inner do-loop's body */
2766 innerBody
= slang_oper_child(innerDo
, 0);
2767 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2768 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2770 /* inner do-loop's condition is constant/false */
2772 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2773 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2778 /* Finally, in innerBody,
2779 * replace "break" with "_notBreakFlag = 0; break"
2780 * replace "continue" with "break"
2782 replace_break_and_cont(A
, innerBody
);
2784 /*slang_print_tree(top, 0);*/
2786 return _slang_gen_operation(A
, top
);
2793 * Generate loop code using high-level IR_LOOP instruction
2795 static slang_ir_node
*
2796 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2800 * BREAK if !expr (child[0])
2801 * body code (child[1])
2803 slang_ir_node
*loop
, *breakIf
, *body
;
2804 GLboolean isConst
, constTrue
= GL_FALSE
;
2806 if (!A
->EmitContReturn
) {
2807 /* We don't want to emit CONT instructions. If this while-loop has
2808 * a continue, translate it away.
2810 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2811 return _slang_gen_while_without_continue(A
, oper
);
2815 /* type-check expression */
2816 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2817 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2821 /* Check if loop condition is a constant */
2822 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2824 if (isConst
&& !constTrue
) {
2825 /* loop is never executed! */
2826 return new_node0(IR_NOP
);
2829 /* Begin new loop */
2830 loop
= new_loop(NULL
);
2832 /* save loop state */
2833 push_loop(A
, oper
, loop
);
2835 if (isConst
&& constTrue
) {
2836 /* while(nonzero constant), no conditional break */
2841 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2842 breakIf
= new_break_if_true(A
, cond
);
2844 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2845 loop
->Children
[0] = new_seq(breakIf
, body
);
2847 /* Do infinite loop detection */
2848 /* loop->List is head of linked list of break/continue nodes */
2849 if (!loop
->List
&& isConst
&& constTrue
) {
2850 /* infinite loop detected */
2852 slang_info_log_error(A
->log
, "Infinite loop detected!");
2856 /* restore loop state */
2864 * Transform a do-while-loop so that continue statements are converted to breaks.
2865 * Then do normal IR code generation.
2876 * } while (LOOPCOND);
2881 * bool _notBreakFlag = 1;
2886 * break; // was continue
2889 * _notBreakFlag = 0; // was
2893 * } while (_notBreakFlag && LOOPCOND);
2896 static slang_ir_node
*
2897 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2899 slang_operation
*top
;
2900 slang_operation
*innerBody
;
2902 assert(oper
->type
== SLANG_OPER_DO
);
2904 top
= slang_operation_new(1);
2905 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2906 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2907 slang_operation_add_children(top
, 2);
2909 /* declare: bool _notBreakFlag = true */
2911 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2912 slang_generate_declaration(A
, top
->locals
, condDecl
,
2913 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2916 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2918 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2919 outerDo
->type
= SLANG_OPER_DO
;
2920 slang_operation_add_children(outerDo
, 2);
2924 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2925 innerDo
->type
= SLANG_OPER_DO
;
2926 slang_operation_add_children(innerDo
, 2);
2928 /* copy original do-loop body into inner do-loop's body */
2929 innerBody
= slang_oper_child(innerDo
, 0);
2930 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2931 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2933 /* inner do-loop's condition is constant/false */
2935 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2936 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2940 /* _notBreakFlag && LOOPCOND */
2942 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2943 cond
->type
= SLANG_OPER_LOGICALAND
;
2944 slang_operation_add_children(cond
, 2);
2946 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2947 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2950 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2951 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2956 /* Finally, in innerBody,
2957 * replace "break" with "_notBreakFlag = 0; break"
2958 * replace "continue" with "break"
2960 replace_break_and_cont(A
, innerBody
);
2962 /*slang_print_tree(top, 0);*/
2964 return _slang_gen_operation(A
, top
);
2969 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2971 static slang_ir_node
*
2972 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2976 * body code (child[0])
2978 * BREAK if !expr (child[1])
2980 slang_ir_node
*loop
;
2981 GLboolean isConst
, constTrue
;
2983 if (!A
->EmitContReturn
) {
2984 /* We don't want to emit CONT instructions. If this do-loop has
2985 * a continue, translate it away.
2987 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2988 return _slang_gen_do_without_continue(A
, oper
);
2992 /* type-check expression */
2993 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2994 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2998 loop
= new_loop(NULL
);
3000 /* save loop state */
3001 push_loop(A
, oper
, loop
);
3004 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3006 /* Check if loop condition is a constant */
3007 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3008 if (isConst
&& constTrue
) {
3009 /* do { } while(1) ==> no conditional break */
3010 loop
->Children
[1] = NULL
; /* no tail code */
3014 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3015 loop
->Children
[1] = new_break_if_true(A
, cond
);
3018 /* XXX we should do infinite loop detection, as above */
3020 /* restore loop state */
3028 * Recursively count the number of operations rooted at 'oper'.
3029 * This gives some kind of indication of the size/complexity of an operation.
3032 sizeof_operation(const slang_operation
*oper
)
3035 GLuint count
= 1; /* me */
3037 for (i
= 0; i
< oper
->num_children
; i
++) {
3038 count
+= sizeof_operation(&oper
->children
[i
]);
3049 * Determine if a for-loop can be unrolled.
3050 * At this time, only a rather narrow class of for loops can be unrolled.
3051 * See code for details.
3052 * When a loop can't be unrolled because it's too large we'll emit a
3053 * message to the log.
3056 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3060 const char *varName
;
3063 if (oper
->type
!= SLANG_OPER_FOR
)
3066 assert(oper
->num_children
== 4);
3068 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3071 /* children[0] must be either "int i=constant" or "i=constant" */
3072 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3073 slang_variable
*var
;
3075 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3078 varId
= oper
->children
[0].children
[0].a_id
;
3080 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3084 if (!var
->initializer
)
3086 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3088 start
= (GLint
) var
->initializer
->literal
[0];
3090 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3091 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3093 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3095 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3098 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3100 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3106 /* children[1] must be "i<constant" */
3107 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3109 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3111 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3113 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3116 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3118 /* children[2] must be "i++" or "++i" */
3119 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3120 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3122 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3125 /* make sure the same variable name is used in all places */
3126 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3127 (oper
->children
[2].children
[0].a_id
!= varId
))
3130 varName
= (const char *) varId
;
3132 /* children[3], the loop body, can't be too large */
3133 bodySize
= sizeof_operation(&oper
->children
[3]);
3134 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3135 slang_info_log_print(A
->log
,
3136 "Note: 'for (%s ... )' body is too large/complex"
3143 return GL_FALSE
; /* degenerate case */
3145 if ((GLuint
)(end
- start
) > MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3146 slang_info_log_print(A
->log
,
3147 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3148 " many iterations to unroll",
3149 varName
, start
, varName
, end
, varName
);
3153 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3154 slang_info_log_print(A
->log
,
3155 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3156 " too much code to unroll",
3157 varName
, start
, varName
, end
, varName
);
3161 return GL_TRUE
; /* we can unroll the loop */
3166 * Unroll a for-loop.
3167 * First we determine the number of iterations to unroll.
3168 * Then for each iteration:
3169 * make a copy of the loop body
3170 * replace instances of the loop variable with the current iteration value
3171 * generate IR code for the body
3172 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3174 static slang_ir_node
*
3175 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3177 GLint start
, end
, iter
;
3178 slang_ir_node
*n
, *root
= NULL
;
3181 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3182 /* for (int i=0; ... */
3183 slang_variable
*var
;
3185 varId
= oper
->children
[0].children
[0].a_id
;
3186 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3189 start
= (GLint
) var
->initializer
->literal
[0];
3193 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3194 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3197 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3199 for (iter
= start
; iter
< end
; iter
++) {
3200 slang_operation
*body
;
3202 /* make a copy of the loop body */
3203 body
= slang_operation_new(1);
3207 if (!slang_operation_copy(body
, &oper
->children
[3]))
3210 /* in body, replace instances of 'varId' with literal 'iter' */
3212 slang_variable
*oldVar
;
3213 slang_operation
*newOper
;
3215 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3217 /* undeclared loop variable */
3218 slang_operation_delete(body
);
3222 newOper
= slang_operation_new(1);
3223 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3224 newOper
->literal_size
= 1;
3225 newOper
->literal
[0] = (GLfloat
) iter
;
3227 /* replace instances of the loop variable with newOper */
3228 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3231 /* do IR codegen for body */
3232 n
= _slang_gen_operation(A
, body
);
3236 root
= new_seq(root
, n
);
3238 slang_operation_delete(body
);
3246 * Replace 'continue' statement with 'break' inside a for-loop.
3247 * This is a recursive helper function used by _slang_gen_for_without_continue().
3250 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3252 switch (oper
->type
) {
3253 case SLANG_OPER_CONTINUE
:
3254 oper
->type
= SLANG_OPER_BREAK
;
3256 case SLANG_OPER_FOR
:
3258 case SLANG_OPER_WHILE
:
3259 /* stop upon finding a nested loop */
3265 for (i
= 0; i
< oper
->num_children
; i
++) {
3266 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3274 * Transform a for-loop so that continue statements are converted to breaks.
3275 * Then do normal IR code generation.
3279 * for (INIT; LOOPCOND; INCR) {
3290 * bool _condFlag = 1;
3291 * for (INIT; _condFlag; ) {
3292 * for ( ; _condFlag = LOOPCOND; INCR) {
3304 static slang_ir_node
*
3305 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3307 slang_operation
*top
;
3308 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3309 slang_operation
*lhs
, *rhs
;
3311 assert(oper
->type
== SLANG_OPER_FOR
);
3313 top
= slang_operation_new(1);
3314 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3315 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3316 slang_operation_add_children(top
, 2);
3318 /* declare: bool _condFlag = true */
3320 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3321 slang_generate_declaration(A
, top
->locals
, condDecl
,
3322 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3325 /* build outer loop: for (INIT; _condFlag; ) { */
3326 outerFor
= slang_oper_child(top
, 1);
3327 outerFor
->type
= SLANG_OPER_FOR
;
3328 slang_operation_add_children(outerFor
, 4);
3330 init
= slang_oper_child(outerFor
, 0);
3331 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3333 cond
= slang_oper_child(outerFor
, 1);
3334 cond
->type
= SLANG_OPER_IDENTIFIER
;
3335 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3337 incr
= slang_oper_child(outerFor
, 2);
3338 incr
->type
= SLANG_OPER_VOID
;
3340 /* body of the outer loop */
3342 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3344 slang_operation_add_children(block
, 2);
3345 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3347 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3349 innerFor
= slang_oper_child(block
, 0);
3351 /* make copy of orig loop */
3352 slang_operation_copy(innerFor
, oper
);
3353 assert(innerFor
->type
== SLANG_OPER_FOR
);
3354 innerFor
->locals
->outer_scope
= block
->locals
;
3356 init
= slang_oper_child(innerFor
, 0);
3357 init
->type
= SLANG_OPER_VOID
; /* leak? */
3359 cond
= slang_oper_child(innerFor
, 1);
3360 slang_operation_destruct(cond
);
3361 cond
->type
= SLANG_OPER_ASSIGN
;
3362 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3363 slang_operation_add_children(cond
, 2);
3365 lhs
= slang_oper_child(cond
, 0);
3366 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3367 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3369 rhs
= slang_oper_child(cond
, 1);
3370 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3373 /* if (_condFlag) INCR; */
3375 slang_operation
*ifop
= slang_oper_child(block
, 1);
3376 ifop
->type
= SLANG_OPER_IF
;
3377 slang_operation_add_children(ifop
, 2);
3379 /* re-use cond node build above */
3380 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3382 /* incr node from original for-loop operation */
3383 slang_operation_copy(slang_oper_child(ifop
, 1),
3384 slang_oper_child(oper
, 2));
3387 /* finally, replace "continue" with "break" in the inner for-loop */
3388 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3391 return _slang_gen_operation(A
, top
);
3397 * Generate IR for a for-loop. Unrolling will be done when possible.
3399 static slang_ir_node
*
3400 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3404 if (!A
->EmitContReturn
) {
3405 /* We don't want to emit CONT instructions. If this for-loop has
3406 * a continue, translate it away.
3408 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3409 return _slang_gen_for_without_continue(A
, oper
);
3413 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3415 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3420 assert(oper
->type
== SLANG_OPER_FOR
);
3422 /* conventional for-loop code generation */
3425 * init code (child[0])
3427 * BREAK if !expr (child[1])
3428 * body code (child[3])
3430 * incr code (child[2]) // XXX continue here
3432 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3433 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3434 loop
= new_loop(NULL
);
3436 /* save loop state */
3437 push_loop(A
, oper
, loop
);
3439 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3440 breakIf
= new_break_if_true(A
, cond
);
3441 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3442 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3444 loop
->Children
[0] = new_seq(breakIf
, body
);
3445 loop
->Children
[1] = incr
; /* tail code */
3447 /* restore loop state */
3450 return new_seq(init
, loop
);
3455 static slang_ir_node
*
3456 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3458 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3460 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3461 loopNode
= current_loop_ir(A
);
3463 assert(loopNode
->Opcode
== IR_LOOP
);
3465 cont
= new_node0(IR_CONT
);
3467 cont
->Parent
= loopNode
;
3468 /* insert this node at head of linked list of cont/break instructions */
3469 cont
->List
= loopNode
->List
;
3470 loopNode
->List
= cont
;
3473 n
= new_seq(incr
, cont
);
3479 * Determine if the given operation is of a specific type.
3482 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3484 if (oper
->type
== type
)
3486 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3487 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3488 oper
->num_children
== 1)
3489 return is_operation_type(&oper
->children
[0], type
);
3496 * Generate IR tree for an if/then/else conditional using high-level
3497 * IR_IF instruction.
3499 static slang_ir_node
*
3500 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3503 * eval expr (child[0])
3510 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3511 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3512 GLboolean isConst
, constTrue
;
3514 /* type-check expression */
3515 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3516 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3520 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3521 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3525 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3529 return _slang_gen_operation(A
, &oper
->children
[1]);
3532 /* if (false) ... */
3533 return _slang_gen_operation(A
, &oper
->children
[2]);
3537 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3538 cond
= new_cond(cond
);
3540 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3541 && !haveElseClause
) {
3542 /* Special case: generate a conditional break */
3543 ifBody
= new_break_if_true(A
, cond
);
3546 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3548 && current_loop_oper(A
)
3549 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3550 /* Special case: generate a conditional continue */
3551 ifBody
= new_cont_if_true(A
, cond
);
3556 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3558 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3561 ifNode
= new_if(cond
, ifBody
, elseBody
);
3568 static slang_ir_node
*
3569 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3573 assert(oper
->type
== SLANG_OPER_NOT
);
3575 /* type-check expression */
3576 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3577 slang_info_log_error(A
->log
,
3578 "scalar/boolean expression expected for '!'");
3582 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3590 static slang_ir_node
*
3591 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3593 slang_ir_node
*n1
, *n2
;
3595 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3597 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3598 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3599 slang_info_log_error(A
->log
,
3600 "scalar/boolean expressions expected for '^^'");
3604 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3607 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3610 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3615 * Generate IR node for storage of a temporary of given size.
3617 static slang_ir_node
*
3618 _slang_gen_temporary(GLint size
)
3620 slang_ir_storage
*store
;
3621 slang_ir_node
*n
= NULL
;
3623 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3625 n
= new_node0(IR_VAR_DECL
);
3638 * Generate program constants for an array.
3639 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3640 * This will allocate and initialize three vector constants, storing
3641 * the array in constant memory, not temporaries like a non-const array.
3642 * This can also be used for uniform array initializers.
3643 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3646 make_constant_array(slang_assemble_ctx
*A
,
3647 slang_variable
*var
,
3648 slang_operation
*initializer
)
3650 struct gl_program
*prog
= A
->program
;
3651 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3652 const char *varName
= (char *) var
->a_name
;
3653 const GLuint numElements
= initializer
->num_children
;
3659 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3661 size
= var
->store
->Size
;
3663 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3664 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3665 assert(initializer
->type
== SLANG_OPER_CALL
);
3666 assert(initializer
->array_constructor
);
3668 values
= (GLfloat
*) malloc(numElements
* 4 * sizeof(GLfloat
));
3670 /* convert constructor params into ordinary floats */
3671 for (i
= 0; i
< numElements
; i
++) {
3672 const slang_operation
*op
= &initializer
->children
[i
];
3673 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3674 /* unsupported type for this optimization */
3678 for (j
= 0; j
< op
->literal_size
; j
++) {
3679 values
[i
* 4 + j
] = op
->literal
[j
];
3681 for ( ; j
< 4; j
++) {
3682 values
[i
* 4 + j
] = 0.0f
;
3686 /* slightly different paths for constants vs. uniforms */
3687 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3688 var
->store
->File
= PROGRAM_UNIFORM
;
3689 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3690 size
, datatype
, values
);
3693 var
->store
->File
= PROGRAM_CONSTANT
;
3694 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3697 assert(var
->store
->Size
== size
);
3707 * Generate IR node for allocating/declaring a variable (either a local or
3709 * Generally, this involves allocating an slang_ir_storage instance for the
3710 * variable, choosing a register file (temporary, constant, etc).
3711 * For ordinary variables we do not yet allocate storage though. We do that
3712 * when we find the first actual use of the variable to avoid allocating temp
3713 * regs that will never get used.
3714 * At this time, uniforms are always allocated space in this function.
3716 * \param initializer Optional initializer expression for the variable.
3718 static slang_ir_node
*
3719 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3720 slang_operation
*initializer
)
3722 const char *varName
= (const char *) var
->a_name
;
3723 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3724 slang_ir_node
*varDecl
, *n
;
3725 slang_ir_storage
*store
;
3726 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3727 gl_register_file file
;
3729 /*assert(!var->declared);*/
3730 var
->declared
= GL_TRUE
;
3732 /* determine GPU register file for simple cases */
3733 if (is_sampler_type(&var
->type
)) {
3734 file
= PROGRAM_SAMPLER
;
3736 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3737 file
= PROGRAM_UNIFORM
;
3740 file
= PROGRAM_TEMPORARY
;
3743 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3745 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3749 arrayLen
= _slang_array_length(var
);
3750 totalSize
= _slang_array_size(size
, arrayLen
);
3752 /* Allocate IR node for the declaration */
3753 varDecl
= new_node0(IR_VAR_DECL
);
3757 /* Allocate slang_ir_storage for this variable if needed.
3758 * Note that we may not actually allocate a constant or temporary register
3762 GLint index
= -7; /* TBD / unknown */
3763 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3765 return NULL
; /* out of memory */
3768 /* set the IR node's Var and Store pointers */
3770 varDecl
->Store
= var
->store
;
3775 /* if there's an initializer, generate IR for the expression */
3777 slang_ir_node
*varRef
, *init
;
3779 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3780 /* if the variable is const, the initializer must be a const
3781 * expression as well.
3784 if (!_slang_is_constant_expr(initializer
)) {
3785 slang_info_log_error(A
->log
,
3786 "initializer for %s not constant", varName
);
3792 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
&&
3793 !A
->allow_uniform_initializers
) {
3794 slang_info_log_error(A
->log
,
3795 "initializer for uniform %s not allowed",
3800 /* IR for the variable we're initializing */
3801 varRef
= new_var(A
, var
);
3803 slang_info_log_error(A
->log
, "out of memory");
3807 /* constant-folding, etc here */
3808 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3810 /* look for simple constant-valued variables and uniforms */
3811 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3812 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3814 if (initializer
->type
== SLANG_OPER_CALL
&&
3815 initializer
->array_constructor
) {
3816 /* array initializer */
3817 if (make_constant_array(A
, var
, initializer
))
3820 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3821 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3822 /* simple float/vector initializer */
3823 if (store
->File
== PROGRAM_UNIFORM
) {
3824 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3826 totalSize
, datatype
,
3827 initializer
->literal
);
3828 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3833 store
->File
= PROGRAM_CONSTANT
;
3834 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3836 initializer
->literal
,
3838 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3845 /* IR for initializer */
3846 init
= _slang_gen_operation(A
, initializer
);
3850 /* XXX remove this when type checking is added above */
3851 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3852 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3856 /* assign RHS to LHS */
3857 n
= new_node2(IR_COPY
, varRef
, init
);
3858 n
= new_seq(varDecl
, n
);
3861 /* no initializer */
3865 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3866 /* always need to allocate storage for uniforms at this point */
3867 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3868 totalSize
, datatype
, NULL
);
3869 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3873 printf("%s var %p %s store=%p index=%d size=%d\n",
3874 __FUNCTION__
, (void *) var
, (char *) varName
,
3875 (void *) store
, store
->Index
, store
->Size
);
3883 * Generate code for a selection expression: b ? x : y
3884 * XXX In some cases we could implement a selection expression
3885 * with an LRP instruction (use the boolean as the interpolant).
3886 * Otherwise, we use an IF/ELSE/ENDIF construct.
3888 static slang_ir_node
*
3889 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3891 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3892 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3893 slang_typeinfo type0
, type1
, type2
;
3894 int size
, isBool
, isEqual
;
3896 assert(oper
->type
== SLANG_OPER_SELECT
);
3897 assert(oper
->num_children
== 3);
3899 /* type of children[0] must be boolean */
3900 slang_typeinfo_construct(&type0
);
3901 typeof_operation(A
, &oper
->children
[0], &type0
);
3902 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3903 slang_typeinfo_destruct(&type0
);
3905 slang_info_log_error(A
->log
, "selector type is not boolean");
3909 slang_typeinfo_construct(&type1
);
3910 slang_typeinfo_construct(&type2
);
3911 typeof_operation(A
, &oper
->children
[1], &type1
);
3912 typeof_operation(A
, &oper
->children
[2], &type2
);
3913 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3914 slang_typeinfo_destruct(&type1
);
3915 slang_typeinfo_destruct(&type2
);
3917 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3921 /* size of x or y's type */
3922 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3926 tmpDecl
= _slang_gen_temporary(size
);
3928 /* the condition (child 0) */
3929 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3930 cond
= new_cond(cond
);
3932 /* if-true body (child 1) */
3933 tmpVar
= new_node0(IR_VAR
);
3934 tmpVar
->Store
= tmpDecl
->Store
;
3935 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3936 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3938 /* if-false body (child 2) */
3939 tmpVar
= new_node0(IR_VAR
);
3940 tmpVar
->Store
= tmpDecl
->Store
;
3941 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3942 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3944 ifNode
= new_if(cond
, trueNode
, falseNode
);
3947 tmpVar
= new_node0(IR_VAR
);
3948 tmpVar
->Store
= tmpDecl
->Store
;
3950 tree
= new_seq(ifNode
, tmpVar
);
3951 tree
= new_seq(tmpDecl
, tree
);
3953 /*_slang_print_ir_tree(tree, 10);*/
3959 * Generate code for &&.
3961 static slang_ir_node
*
3962 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3964 /* rewrite "a && b" as "a ? b : false" */
3965 slang_operation
*select
;
3968 select
= slang_operation_new(1);
3969 select
->type
= SLANG_OPER_SELECT
;
3970 slang_operation_add_children(select
, 3);
3972 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3973 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3974 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3976 n
= _slang_gen_select(A
, select
);
3982 * Generate code for ||.
3984 static slang_ir_node
*
3985 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3987 /* rewrite "a || b" as "a ? true : b" */
3988 slang_operation
*select
;
3991 select
= slang_operation_new(1);
3992 select
->type
= SLANG_OPER_SELECT
;
3993 slang_operation_add_children(select
, 3);
3995 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3996 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3997 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3999 n
= _slang_gen_select(A
, select
);
4005 * Generate IR tree for a return statement.
4007 static slang_ir_node
*
4008 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4010 assert(oper
->type
== SLANG_OPER_RETURN
);
4011 return new_return(A
->curFuncEndLabel
);
4017 * Determine if the given operation/expression is const-valued.
4020 _slang_is_constant_expr(const slang_operation
*oper
)
4022 slang_variable
*var
;
4025 switch (oper
->type
) {
4026 case SLANG_OPER_IDENTIFIER
:
4027 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4028 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4032 for (i
= 0; i
< oper
->num_children
; i
++) {
4033 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4043 * Check if an assignment of type t1 to t0 is legal.
4044 * XXX more cases needed.
4047 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4048 slang_operation
*op0
,
4049 slang_operation
*op1
)
4051 slang_typeinfo t0
, t1
;
4054 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4055 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4059 slang_typeinfo_construct(&t0
);
4060 typeof_operation(A
, op0
, &t0
);
4062 slang_typeinfo_construct(&t1
);
4063 typeof_operation(A
, op1
, &t1
);
4065 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4066 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4070 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4075 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4076 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4077 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4080 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4081 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4084 #if 0 /* not used just yet - causes problems elsewhere */
4085 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4086 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4090 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4091 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4094 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4095 t1
.spec
.type
== SLANG_SPEC_INT
)
4103 * Generate IR tree for a local variable declaration.
4104 * Basically do some error checking and call _slang_gen_var_decl().
4106 static slang_ir_node
*
4107 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4109 const char *varName
= (char *) oper
->a_id
;
4110 slang_variable
*var
;
4111 slang_ir_node
*varDecl
;
4112 slang_operation
*initializer
;
4114 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4115 assert(oper
->num_children
<= 1);
4118 /* lookup the variable by name */
4119 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4121 return NULL
; /* "shouldn't happen" */
4123 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4124 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4125 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4126 /* can't declare attribute/uniform vars inside functions */
4127 slang_info_log_error(A
->log
,
4128 "local variable '%s' cannot be an attribute/uniform/varying",
4135 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4140 /* check if the var has an initializer */
4141 if (oper
->num_children
> 0) {
4142 assert(oper
->num_children
== 1);
4143 initializer
= &oper
->children
[0];
4145 else if (var
->initializer
) {
4146 initializer
= var
->initializer
;
4153 /* check/compare var type and initializer type */
4154 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4155 slang_info_log_error(A
->log
, "incompatible types in assignment");
4160 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4161 slang_info_log_error(A
->log
,
4162 "const-qualified variable '%s' requires initializer",
4168 /* Generate IR node */
4169 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4178 * Generate IR tree for a reference to a variable (such as in an expression).
4179 * This is different from a variable declaration.
4181 static slang_ir_node
*
4182 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4184 /* If there's a variable associated with this oper (from inlining)
4185 * use it. Otherwise, use the oper's var id.
4187 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4188 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4190 if (!var
|| !var
->declared
) {
4191 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4194 n
= new_var(A
, var
);
4201 * Return the number of components actually named by the swizzle.
4202 * Recall that swizzles may have undefined/don't-care values.
4205 swizzle_size(GLuint swizzle
)
4208 for (i
= 0; i
< 4; i
++) {
4209 GLuint swz
= GET_SWZ(swizzle
, i
);
4216 static slang_ir_node
*
4217 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4219 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4223 n
->Store
= _slang_new_ir_storage_relative(0,
4224 swizzle_size(swizzle
),
4227 n
->Store
->Swizzle
= swizzle
;
4234 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4236 while (store
->Parent
)
4237 store
= store
->Parent
;
4239 if (!(store
->File
== PROGRAM_OUTPUT
||
4240 store
->File
== PROGRAM_TEMPORARY
||
4241 (store
->File
== PROGRAM_VARYING
&&
4242 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4252 * Walk up an IR storage path to compute the final swizzle.
4253 * This is used when we find an expression such as "foo.xz.yx".
4256 root_swizzle(const slang_ir_storage
*st
)
4258 GLuint swizzle
= st
->Swizzle
;
4259 while (st
->Parent
) {
4261 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4268 * Generate IR tree for an assignment (=).
4270 static slang_ir_node
*
4271 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4273 slang_operation
*pred
= NULL
;
4274 slang_ir_node
*n
= NULL
;
4276 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4277 /* Check that var is writeable */
4278 const char *varName
= (char *) oper
->children
[0].a_id
;
4280 = _slang_variable_locate(oper
->children
[0].locals
,
4281 oper
->children
[0].a_id
, GL_TRUE
);
4283 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
4287 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4288 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4289 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4290 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4291 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4292 slang_info_log_error(A
->log
,
4293 "illegal assignment to read-only variable '%s'",
4298 /* check if we need to predicate this assignment based on __notRetFlag */
4299 if ((var
->is_global
||
4300 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4301 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4302 /* create predicate, used below */
4303 pred
= slang_operation_new(1);
4304 pred
->type
= SLANG_OPER_IDENTIFIER
;
4305 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4306 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4310 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4311 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4312 /* Special case of: x = f(a, b)
4313 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4315 * XXX this could be even more effective if we could accomodate
4316 * cases such as "v.x = f();" - would help with typical vertex
4319 n
= _slang_gen_function_call_name(A
,
4320 (const char *) oper
->children
[1].a_id
,
4321 &oper
->children
[1], &oper
->children
[0]);
4324 slang_ir_node
*lhs
, *rhs
;
4326 /* lhs and rhs type checking */
4327 if (!_slang_assignment_compatible(A
,
4329 &oper
->children
[1])) {
4330 slang_info_log_error(A
->log
, "incompatible types in assignment");
4334 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4340 slang_info_log_error(A
->log
,
4341 "invalid left hand side for assignment");
4345 /* check that lhs is writable */
4346 if (!is_store_writable(A
, lhs
->Store
)) {
4347 slang_info_log_error(A
->log
,
4348 "illegal assignment to read-only l-value");
4352 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4354 /* convert lhs swizzle into writemask */
4355 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4356 GLuint writemask
, newSwizzle
= 0x0;
4357 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4358 /* Non-simple writemask, need to swizzle right hand side in
4359 * order to put components into the right place.
4361 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4363 n
= new_node2(IR_COPY
, lhs
, rhs
);
4371 /* predicate the assignment code on __notRetFlag */
4372 slang_ir_node
*top
, *cond
;
4374 cond
= _slang_gen_operation(A
, pred
);
4375 top
= new_if(cond
, n
, NULL
);
4383 * Generate IR tree for referencing a field in a struct (or basic vector type)
4385 static slang_ir_node
*
4386 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4390 /* type of struct */
4391 slang_typeinfo_construct(&ti
);
4392 typeof_operation(A
, &oper
->children
[0], &ti
);
4394 if (_slang_type_is_vector(ti
.spec
.type
)) {
4395 /* the field should be a swizzle */
4396 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4400 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4401 slang_info_log_error(A
->log
, "Bad swizzle");
4404 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4409 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4410 /* create new parent node with swizzle */
4412 n
= _slang_gen_swizzle(n
, swizzle
);
4415 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4416 || ti
.spec
.type
== SLANG_SPEC_INT
4417 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4418 const GLuint rows
= 1;
4422 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4423 slang_info_log_error(A
->log
, "Bad swizzle");
4425 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4429 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4430 /* create new parent node with swizzle */
4431 n
= _slang_gen_swizzle(n
, swizzle
);
4435 /* the field is a structure member (base.field) */
4436 /* oper->children[0] is the base */
4437 /* oper->a_id is the field name */
4438 slang_ir_node
*base
, *n
;
4439 slang_typeinfo field_ti
;
4440 GLint fieldSize
, fieldOffset
= -1;
4443 slang_typeinfo_construct(&field_ti
);
4444 typeof_operation(A
, oper
, &field_ti
);
4446 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4448 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4450 if (fieldSize
== 0 || fieldOffset
< 0) {
4451 const char *structName
;
4452 if (ti
.spec
._struct
)
4453 structName
= (char *) ti
.spec
._struct
->a_name
;
4455 structName
= "unknown";
4456 slang_info_log_error(A
->log
,
4457 "\"%s\" is not a member of struct \"%s\"",
4458 (char *) oper
->a_id
, structName
);
4461 assert(fieldSize
>= 0);
4463 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4465 /* error msg should have already been logged */
4469 n
= new_node1(IR_FIELD
, base
);
4473 n
->Field
= (char *) oper
->a_id
;
4475 /* Store the field's offset in storage->Index */
4476 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4486 * Gen code for array indexing.
4488 static slang_ir_node
*
4489 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4491 slang_typeinfo array_ti
;
4493 /* get array's type info */
4494 slang_typeinfo_construct(&array_ti
);
4495 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4497 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4498 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4499 /* translate the index into a swizzle/writemask: "v.x=p" */
4500 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4504 index
= (GLint
) oper
->children
[1].literal
[0];
4505 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4506 index
>= (GLint
) max
) {
4508 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4509 printf("type = %d\n", oper
->children
[1].type
);
4510 printf("index = %d, max = %d\n", index
, max
);
4511 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4512 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4519 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4521 /* use swizzle to access the element */
4522 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4526 n
= _slang_gen_swizzle(n
, swizzle
);
4531 /* conventional array */
4532 slang_typeinfo elem_ti
;
4533 slang_ir_node
*elem
, *array
, *index
;
4534 GLint elemSize
, arrayLen
;
4536 /* size of array element */
4537 slang_typeinfo_construct(&elem_ti
);
4538 typeof_operation(A
, oper
, &elem_ti
);
4539 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4541 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4542 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4544 arrayLen
= array_ti
.array_len
;
4546 slang_typeinfo_destruct(&array_ti
);
4547 slang_typeinfo_destruct(&elem_ti
);
4549 if (elemSize
<= 0) {
4550 /* unknown var or type */
4551 slang_info_log_error(A
->log
, "Undefined variable or type");
4555 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4556 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4557 if (array
&& index
) {
4559 GLint constIndex
= -1;
4560 if (index
->Opcode
== IR_FLOAT
) {
4561 constIndex
= (int) index
->Value
[0];
4562 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4563 slang_info_log_error(A
->log
,
4564 "Array index out of bounds (index=%d size=%d)",
4565 constIndex
, arrayLen
);
4566 _slang_free_ir_tree(array
);
4567 _slang_free_ir_tree(index
);
4572 if (!array
->Store
) {
4573 slang_info_log_error(A
->log
, "Invalid array");
4577 elem
= new_node2(IR_ELEMENT
, array
, index
);
4579 /* The storage info here will be updated during code emit */
4580 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4581 array
->Store
->Index
,
4583 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4587 _slang_free_ir_tree(array
);
4588 _slang_free_ir_tree(index
);
4595 static slang_ir_node
*
4596 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4597 slang_ir_opcode opcode
)
4599 slang_typeinfo t0
, t1
;
4602 slang_typeinfo_construct(&t0
);
4603 typeof_operation(A
, &oper
->children
[0], &t0
);
4605 slang_typeinfo_construct(&t1
);
4606 typeof_operation(A
, &oper
->children
[0], &t1
);
4608 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4609 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4610 slang_info_log_error(A
->log
, "Illegal array comparison");
4614 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4615 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4616 /* <, <=, >, >= can only be used with scalars */
4617 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4618 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4619 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4620 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4621 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4626 n
= new_node2(opcode
,
4627 _slang_gen_operation(A
, &oper
->children
[0]),
4628 _slang_gen_operation(A
, &oper
->children
[1]));
4630 /* result is a bool (size 1) */
4631 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4639 print_vars(slang_variable_scope
*s
)
4643 for (i
= 0; i
< s
->num_variables
; i
++) {
4645 (char*) s
->variables
[i
]->a_name
,
4646 s
->variables
[i
]->declared
);
4656 _slang_undeclare_vars(slang_variable_scope
*locals
)
4658 if (locals
->num_variables
> 0) {
4660 for (i
= 0; i
< locals
->num_variables
; i
++) {
4661 slang_variable
*v
= locals
->variables
[i
];
4662 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4663 v
->declared
= GL_FALSE
;
4671 * Generate IR tree for a slang_operation (AST node)
4673 static slang_ir_node
*
4674 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4676 switch (oper
->type
) {
4677 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4681 _slang_push_var_table(A
->vartable
);
4683 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4684 n
= _slang_gen_operation(A
, oper
);
4685 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4687 _slang_pop_var_table(A
->vartable
);
4689 /*_slang_undeclare_vars(oper->locals);*/
4690 /*print_vars(oper->locals);*/
4693 n
= new_node1(IR_SCOPE
, n
);
4698 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4699 /* list of operations */
4700 if (oper
->num_children
> 0)
4702 slang_ir_node
*n
, *tree
= NULL
;
4705 for (i
= 0; i
< oper
->num_children
; i
++) {
4706 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4708 _slang_free_ir_tree(tree
);
4709 return NULL
; /* error must have occured */
4711 tree
= new_seq(tree
, n
);
4717 return new_node0(IR_NOP
);
4720 case SLANG_OPER_EXPRESSION
:
4721 return _slang_gen_operation(A
, &oper
->children
[0]);
4723 case SLANG_OPER_FOR
:
4724 return _slang_gen_for(A
, oper
);
4726 return _slang_gen_do(A
, oper
);
4727 case SLANG_OPER_WHILE
:
4728 return _slang_gen_while(A
, oper
);
4729 case SLANG_OPER_BREAK
:
4730 if (!current_loop_oper(A
)) {
4731 slang_info_log_error(A
->log
, "'break' not in loop");
4734 return new_break(current_loop_ir(A
));
4735 case SLANG_OPER_CONTINUE
:
4736 if (!current_loop_oper(A
)) {
4737 slang_info_log_error(A
->log
, "'continue' not in loop");
4740 return _slang_gen_continue(A
, oper
);
4741 case SLANG_OPER_DISCARD
:
4742 return new_node0(IR_KILL
);
4744 case SLANG_OPER_EQUAL
:
4745 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4746 case SLANG_OPER_NOTEQUAL
:
4747 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4748 case SLANG_OPER_GREATER
:
4749 return _slang_gen_compare(A
, oper
, IR_SGT
);
4750 case SLANG_OPER_LESS
:
4751 return _slang_gen_compare(A
, oper
, IR_SLT
);
4752 case SLANG_OPER_GREATEREQUAL
:
4753 return _slang_gen_compare(A
, oper
, IR_SGE
);
4754 case SLANG_OPER_LESSEQUAL
:
4755 return _slang_gen_compare(A
, oper
, IR_SLE
);
4756 case SLANG_OPER_ADD
:
4759 assert(oper
->num_children
== 2);
4760 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4763 case SLANG_OPER_SUBTRACT
:
4766 assert(oper
->num_children
== 2);
4767 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4770 case SLANG_OPER_MULTIPLY
:
4773 assert(oper
->num_children
== 2);
4774 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4777 case SLANG_OPER_DIVIDE
:
4780 assert(oper
->num_children
== 2);
4781 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4784 case SLANG_OPER_MINUS
:
4787 assert(oper
->num_children
== 1);
4788 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4791 case SLANG_OPER_PLUS
:
4792 /* +expr --> do nothing */
4793 return _slang_gen_operation(A
, &oper
->children
[0]);
4794 case SLANG_OPER_VARIABLE_DECL
:
4795 return _slang_gen_declaration(A
, oper
);
4796 case SLANG_OPER_ASSIGN
:
4797 return _slang_gen_assignment(A
, oper
);
4798 case SLANG_OPER_ADDASSIGN
:
4801 assert(oper
->num_children
== 2);
4802 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4805 case SLANG_OPER_SUBASSIGN
:
4808 assert(oper
->num_children
== 2);
4809 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4813 case SLANG_OPER_MULASSIGN
:
4816 assert(oper
->num_children
== 2);
4817 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4820 case SLANG_OPER_DIVASSIGN
:
4823 assert(oper
->num_children
== 2);
4824 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4827 case SLANG_OPER_LOGICALAND
:
4830 assert(oper
->num_children
== 2);
4831 n
= _slang_gen_logical_and(A
, oper
);
4834 case SLANG_OPER_LOGICALOR
:
4837 assert(oper
->num_children
== 2);
4838 n
= _slang_gen_logical_or(A
, oper
);
4841 case SLANG_OPER_LOGICALXOR
:
4842 return _slang_gen_xor(A
, oper
);
4843 case SLANG_OPER_NOT
:
4844 return _slang_gen_not(A
, oper
);
4845 case SLANG_OPER_SELECT
: /* b ? x : y */
4848 assert(oper
->num_children
== 3);
4849 n
= _slang_gen_select(A
, oper
);
4853 case SLANG_OPER_ASM
:
4854 return _slang_gen_asm(A
, oper
, NULL
);
4855 case SLANG_OPER_CALL
:
4856 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4858 case SLANG_OPER_METHOD
:
4859 return _slang_gen_method_call(A
, oper
);
4860 case SLANG_OPER_RETURN
:
4861 return _slang_gen_return(A
, oper
);
4862 case SLANG_OPER_RETURN_INLINED
:
4863 return _slang_gen_return(A
, oper
);
4864 case SLANG_OPER_LABEL
:
4865 return new_label(oper
->label
);
4866 case SLANG_OPER_IDENTIFIER
:
4867 return _slang_gen_variable(A
, oper
);
4869 return _slang_gen_if(A
, oper
);
4870 case SLANG_OPER_FIELD
:
4871 return _slang_gen_struct_field(A
, oper
);
4872 case SLANG_OPER_SUBSCRIPT
:
4873 return _slang_gen_array_element(A
, oper
);
4874 case SLANG_OPER_LITERAL_FLOAT
:
4876 case SLANG_OPER_LITERAL_INT
:
4878 case SLANG_OPER_LITERAL_BOOL
:
4879 return new_float_literal(oper
->literal
, oper
->literal_size
);
4881 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4884 assert(oper
->num_children
== 1);
4885 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4888 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4891 assert(oper
->num_children
== 1);
4892 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4895 case SLANG_OPER_PREINCREMENT
: /* ++var */
4898 assert(oper
->num_children
== 1);
4899 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4902 case SLANG_OPER_PREDECREMENT
: /* --var */
4905 assert(oper
->num_children
== 1);
4906 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4910 case SLANG_OPER_NON_INLINED_CALL
:
4911 case SLANG_OPER_SEQUENCE
:
4913 slang_ir_node
*tree
= NULL
;
4915 for (i
= 0; i
< oper
->num_children
; i
++) {
4916 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4917 tree
= new_seq(tree
, n
);
4919 tree
->Store
= n
->Store
;
4921 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4922 tree
= new_function_call(tree
, oper
->label
);
4927 case SLANG_OPER_NONE
:
4928 case SLANG_OPER_VOID
:
4929 /* returning NULL here would generate an error */
4930 return new_node0(IR_NOP
);
4933 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4935 return new_node0(IR_NOP
);
4943 * Check if the given type specifier is a rectangular texture sampler.
4946 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4948 while (spec
->_array
) {
4949 spec
= spec
->_array
;
4951 return spec
->type
== SLANG_SPEC_SAMPLER_RECT
||
4952 spec
->type
== SLANG_SPEC_SAMPLER_RECT_SHADOW
;
4958 * Called by compiler when a global variable has been parsed/compiled.
4959 * Here we examine the variable's type to determine what kind of register
4960 * storage will be used.
4962 * A uniform such as "gl_Position" will become the register specification
4963 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4964 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4966 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4967 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4968 * actual texture unit (as specified by the user calling glUniform1i()).
4971 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4972 slang_unit_type type
)
4974 GET_CURRENT_CONTEXT(ctx
);
4975 struct gl_program
*prog
= A
->program
;
4976 const char *varName
= (char *) var
->a_name
;
4977 GLboolean success
= GL_TRUE
;
4978 slang_ir_storage
*store
= NULL
;
4980 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4981 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4982 const GLint arrayLen
= _slang_array_length(var
);
4983 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4984 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4986 var
->is_global
= GL_TRUE
;
4988 /* check for sampler2D arrays */
4989 if (texIndex
== -1 && var
->type
.specifier
._array
)
4990 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4992 if (texIndex
!= -1) {
4993 /* This is a texture sampler variable...
4994 * store->File = PROGRAM_SAMPLER
4995 * store->Index = sampler number (0..7, typically)
4996 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4998 if (var
->initializer
) {
4999 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5002 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5003 /* disallow rect samplers */
5004 if (ctx
->API
== API_OPENGLES2
&&
5005 is_rect_sampler_spec(&var
->type
.specifier
)) {
5006 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5010 (void) is_rect_sampler_spec
; /* silence warning */
5014 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5015 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5017 /* If we have a sampler array, then we need to allocate the
5018 * additional samplers to ensure we don't allocate them elsewhere.
5019 * We can't directly use _mesa_add_sampler() as that checks the
5020 * varName and gets a match, so we call _mesa_add_parameter()
5021 * directly and use the last sampler number from the call above.
5024 GLint a
= arrayLen
- 1;
5026 for (i
= 0; i
< a
; i
++) {
5027 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5028 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5029 varName
, 1, datatype
, &value
, NULL
, 0x0);
5033 if (dbg
) printf("SAMPLER ");
5035 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5036 /* Uniform variable */
5037 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5040 /* user-defined uniform */
5041 if (datatype
== GL_NONE
) {
5042 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5043 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5044 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5045 /* temporary work-around */
5046 GLenum datatype
= GL_FLOAT
;
5047 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5048 totalSize
, datatype
, NULL
);
5049 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5050 totalSize
, swizzle
);
5053 GLint a
= arrayLen
- 1;
5055 for (i
= 0; i
< a
; i
++) {
5056 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5057 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5058 varName
, 1, datatype
, &value
, NULL
, 0x0);
5062 /* XXX what we need to do is unroll the struct into its
5063 * basic types, creating a uniform variable for each.
5071 * Should produce uniforms:
5072 * "f.a" (GL_FLOAT_VEC3)
5073 * "f.b" (GL_FLOAT_VEC4)
5076 if (var
->initializer
) {
5077 slang_info_log_error(A
->log
,
5078 "unsupported initializer for uniform '%s'", varName
);
5083 slang_info_log_error(A
->log
,
5084 "invalid datatype for uniform variable %s",
5090 /* non-struct uniform */
5091 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5097 /* pre-defined uniform, like gl_ModelviewMatrix */
5098 /* We know it's a uniform, but don't allocate storage unless
5101 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5102 totalSize
, swizzle
);
5104 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5106 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5107 /* varyings must be float, vec or mat */
5108 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5109 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5110 slang_info_log_error(A
->log
,
5111 "varying '%s' must be float/vector/matrix",
5116 if (var
->initializer
) {
5117 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5123 /* user-defined varying */
5129 if (var
->type
.centroid
== SLANG_CENTROID
)
5130 flags
|= PROG_PARAM_BIT_CENTROID
;
5131 if (var
->type
.variant
== SLANG_INVARIANT
)
5132 flags
|= PROG_PARAM_BIT_INVARIANT
;
5134 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5135 totalSize
, GL_NONE
, flags
);
5136 swizzle
= _slang_var_swizzle(size
, 0);
5137 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5138 totalSize
, swizzle
);
5141 /* pre-defined varying, like gl_Color or gl_TexCoord */
5142 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5143 /* fragment program input */
5145 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5148 assert(index
< FRAG_ATTRIB_MAX
);
5149 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5153 /* vertex program output */
5154 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5155 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5157 assert(index
< VERT_RESULT_MAX
);
5158 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5159 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5162 if (dbg
) printf("V/F ");
5164 if (dbg
) printf("VARYING ");
5166 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5169 /* attributes must be float, vec or mat */
5170 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5171 slang_info_log_error(A
->log
,
5172 "attribute '%s' must be float/vector/matrix",
5178 /* user-defined vertex attribute */
5179 const GLint attr
= -1; /* unknown */
5180 swizzle
= _slang_var_swizzle(size
, 0);
5181 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5182 size
, datatype
, attr
);
5184 index
= VERT_ATTRIB_GENERIC0
+ index
;
5187 /* pre-defined vertex attrib */
5188 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5191 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5192 if (dbg
) printf("ATTRIB ");
5194 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5195 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5196 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5198 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5199 if (dbg
) printf("INPUT ");
5201 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5202 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5203 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5204 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5207 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5208 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5209 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5210 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5212 if (dbg
) printf("OUTPUT ");
5214 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5215 /* pre-defined global constant, like gl_MaxLights */
5216 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5217 if (dbg
) printf("CONST ");
5220 /* ordinary variable (may be const) */
5223 /* IR node to declare the variable */
5224 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5226 /* emit GPU instructions */
5227 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5229 _slang_free_ir_tree(n
);
5232 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5233 store
? store
->Index
: -2);
5236 var
->store
= store
; /* save var's storage info */
5238 var
->declared
= GL_TRUE
;
5245 * Produce an IR tree from a function AST (fun->body).
5246 * Then call the code emitter to convert the IR tree into gl_program
5250 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5253 GLboolean success
= GL_TRUE
;
5255 if (strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5256 /* we only really generate code for main, all other functions get
5257 * inlined or codegen'd upon an actual call.
5260 /* do some basic error checking though */
5261 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5262 /* check that non-void functions actually return something */
5264 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5266 slang_info_log_error(A
->log
,
5267 "function \"%s\" has no return statement",
5268 (char *) fun
->header
.a_name
);
5270 "function \"%s\" has no return statement\n",
5271 (char *) fun
->header
.a_name
);
5276 return GL_TRUE
; /* not an error */
5280 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5281 slang_print_function(fun
, 1);
5284 /* should have been allocated earlier: */
5285 assert(A
->program
->Parameters
);
5286 assert(A
->program
->Varying
);
5287 assert(A
->vartable
);
5290 A
->UseReturnFlag
= GL_FALSE
;
5291 A
->CurFunction
= fun
;
5293 /* fold constant expressions, etc. */
5294 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5297 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5298 slang_print_function(fun
, 1);
5301 /* Create an end-of-function label */
5302 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5304 /* push new vartable scope */
5305 _slang_push_var_table(A
->vartable
);
5307 /* Generate IR tree for the function body code */
5308 n
= _slang_gen_operation(A
, fun
->body
);
5310 n
= new_node1(IR_SCOPE
, n
);
5312 /* pop vartable, restore previous */
5313 _slang_pop_var_table(A
->vartable
);
5316 /* XXX record error */
5320 /* append an end-of-function-label to IR tree */
5321 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5323 /*_slang_label_delete(A->curFuncEndLabel);*/
5324 A
->curFuncEndLabel
= NULL
;
5327 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5328 slang_print_function(fun
, 1);
5331 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5332 _slang_print_ir_tree(n
, 0);
5335 printf("************* End codegen function ************\n\n");
5338 if (A
->UnresolvedRefs
) {
5339 /* Can't codegen at this time.
5340 * At link time we'll concatenate all the vertex shaders and/or all
5341 * the fragment shaders and try recompiling.
5346 /* Emit program instructions */
5347 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5348 _slang_free_ir_tree(n
);
5350 /* free codegen context */