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_codegen.h"
50 #include "slang_compile.h"
51 #include "slang_label.h"
52 #include "slang_mem.h"
53 #include "slang_simplify.h"
54 #include "slang_emit.h"
55 #include "slang_vartable.h"
57 #include "slang_print.h"
60 /** Max iterations to unroll */
61 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS
= 32;
63 /** Max for-loop body size (in slang operations) to unroll */
64 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
66 /** Max for-loop body complexity to unroll.
67 * We'll compute complexity as the product of the number of iterations
68 * and the size of the body. So long-ish loops with very simple bodies
69 * can be unrolled, as well as short loops with larger bodies.
71 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY
= 256;
75 static slang_ir_node
*
76 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
79 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
80 GLuint substCount
, slang_variable
**substOld
,
81 slang_operation
**substNew
, GLboolean isLHS
);
85 * Retrieves type information about an operation.
86 * Returns GL_TRUE on success.
87 * Returns GL_FALSE otherwise.
90 typeof_operation(const struct slang_assemble_ctx_
*A
,
94 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
99 is_sampler_type(const slang_fully_specified_type
*t
)
101 switch (t
->specifier
.type
) {
102 case SLANG_SPEC_SAMPLER1D
:
103 case SLANG_SPEC_SAMPLER2D
:
104 case SLANG_SPEC_SAMPLER3D
:
105 case SLANG_SPEC_SAMPLERCUBE
:
106 case SLANG_SPEC_SAMPLER1DSHADOW
:
107 case SLANG_SPEC_SAMPLER2DSHADOW
:
108 case SLANG_SPEC_SAMPLER2DRECT
:
109 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
118 * Return the offset (in floats or ints) of the named field within
119 * the given struct. Return -1 if field not found.
120 * If field is NULL, return the size of the struct instead.
123 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
127 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
128 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
129 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
131 /* types larger than 1 float are register (4-float) aligned */
132 offset
= (offset
+ 3) & ~3;
134 if (field
&& v
->a_name
== field
) {
140 return -1; /* field not found */
142 return offset
; /* struct size */
147 * Return the size (in floats) of the given type specifier.
148 * If the size is greater than 4, the size should be a multiple of 4
149 * so that the correct number of 4-float registers are allocated.
150 * For example, a mat3x2 is size 12 because we want to store the
151 * 3 columns in 3 float[4] registers.
154 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
157 switch (spec
->type
) {
158 case SLANG_SPEC_VOID
:
161 case SLANG_SPEC_BOOL
:
164 case SLANG_SPEC_BVEC2
:
167 case SLANG_SPEC_BVEC3
:
170 case SLANG_SPEC_BVEC4
:
176 case SLANG_SPEC_IVEC2
:
179 case SLANG_SPEC_IVEC3
:
182 case SLANG_SPEC_IVEC4
:
185 case SLANG_SPEC_FLOAT
:
188 case SLANG_SPEC_VEC2
:
191 case SLANG_SPEC_VEC3
:
194 case SLANG_SPEC_VEC4
:
197 case SLANG_SPEC_MAT2
:
198 sz
= 2 * 4; /* 2 columns (regs) */
200 case SLANG_SPEC_MAT3
:
203 case SLANG_SPEC_MAT4
:
206 case SLANG_SPEC_MAT23
:
207 sz
= 2 * 4; /* 2 columns (regs) */
209 case SLANG_SPEC_MAT32
:
210 sz
= 3 * 4; /* 3 columns (regs) */
212 case SLANG_SPEC_MAT24
:
215 case SLANG_SPEC_MAT42
:
216 sz
= 4 * 4; /* 4 columns (regs) */
218 case SLANG_SPEC_MAT34
:
221 case SLANG_SPEC_MAT43
:
222 sz
= 4 * 4; /* 4 columns (regs) */
224 case SLANG_SPEC_SAMPLER1D
:
225 case SLANG_SPEC_SAMPLER2D
:
226 case SLANG_SPEC_SAMPLER3D
:
227 case SLANG_SPEC_SAMPLERCUBE
:
228 case SLANG_SPEC_SAMPLER1DSHADOW
:
229 case SLANG_SPEC_SAMPLER2DSHADOW
:
230 case SLANG_SPEC_SAMPLER2DRECT
:
231 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
232 sz
= 1; /* a sampler is basically just an integer index */
234 case SLANG_SPEC_STRUCT
:
235 sz
= _slang_field_offset(spec
, 0); /* special use */
237 /* 1-float structs are actually troublesome to deal with since they
238 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
239 * ensure the object is placed at R.x
244 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
247 case SLANG_SPEC_ARRAY
:
248 sz
= _slang_sizeof_type_specifier(spec
->_array
);
251 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
256 /* if size is > 4, it should be a multiple of four */
257 assert((sz
& 0x3) == 0);
264 * Query variable/array length (number of elements).
265 * This is slightly non-trivial because there are two ways to express
266 * arrays: "float x[3]" vs. "float[3] x".
267 * \return the length of the array for the given variable, or 0 if not an array
270 _slang_array_length(const slang_variable
*var
)
272 if (var
->type
.array_len
> 0) {
273 /* Ex: float[4] x; */
274 return var
->type
.array_len
;
276 if (var
->array_len
> 0) {
277 /* Ex: float x[4]; */
278 return var
->array_len
;
285 * Compute total size of array give size of element, number of elements.
286 * \return size in floats
289 _slang_array_size(GLint elemSize
, GLint arrayLen
)
292 assert(elemSize
> 0);
294 /* round up base type to multiple of 4 */
295 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
305 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
306 * or -1 if the type is not a sampler.
309 sampler_to_texture_index(const slang_type_specifier_type type
)
312 case SLANG_SPEC_SAMPLER1D
:
313 return TEXTURE_1D_INDEX
;
314 case SLANG_SPEC_SAMPLER2D
:
315 return TEXTURE_2D_INDEX
;
316 case SLANG_SPEC_SAMPLER3D
:
317 return TEXTURE_3D_INDEX
;
318 case SLANG_SPEC_SAMPLERCUBE
:
319 return TEXTURE_CUBE_INDEX
;
320 case SLANG_SPEC_SAMPLER1DSHADOW
:
321 return TEXTURE_1D_INDEX
; /* XXX fix */
322 case SLANG_SPEC_SAMPLER2DSHADOW
:
323 return TEXTURE_2D_INDEX
; /* XXX fix */
324 case SLANG_SPEC_SAMPLER2DRECT
:
325 return TEXTURE_RECT_INDEX
;
326 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
327 return TEXTURE_RECT_INDEX
; /* XXX fix */
334 /** helper to build a SLANG_OPER_IDENTIFIER node */
336 slang_operation_identifier(slang_operation
*oper
,
337 slang_assemble_ctx
*A
,
340 oper
->type
= SLANG_OPER_IDENTIFIER
;
341 oper
->a_id
= slang_atom_pool_atom(A
->atoms
, name
);
345 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
348 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
349 * a vertex or fragment program input variable. Return -1 if the input
351 * XXX return size too
354 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
361 static const struct input_info vertInputs
[] = {
362 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
363 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
364 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
365 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
366 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
367 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
368 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
369 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
370 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
371 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
372 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
373 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
374 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
375 { NULL
, 0, SWIZZLE_NOOP
}
377 static const struct input_info fragInputs
[] = {
378 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
379 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
380 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
381 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
382 /* note: we're packing several quantities into the fogcoord vector */
383 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
384 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
385 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
386 { NULL
, 0, SWIZZLE_NOOP
}
389 const struct input_info
*inputs
390 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
392 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
394 for (i
= 0; inputs
[i
].Name
; i
++) {
395 if (strcmp(inputs
[i
].Name
, name
) == 0) {
397 *swizzleOut
= inputs
[i
].Swizzle
;
398 return inputs
[i
].Attrib
;
406 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
407 * a vertex or fragment program output variable. Return -1 for an invalid
411 _slang_output_index(const char *name
, GLenum target
)
417 static const struct output_info vertOutputs
[] = {
418 { "gl_Position", VERT_RESULT_HPOS
},
419 { "gl_FrontColor", VERT_RESULT_COL0
},
420 { "gl_BackColor", VERT_RESULT_BFC0
},
421 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
422 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
423 { "gl_TexCoord", VERT_RESULT_TEX0
},
424 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
425 { "gl_PointSize", VERT_RESULT_PSIZ
},
428 static const struct output_info fragOutputs
[] = {
429 { "gl_FragColor", FRAG_RESULT_COLOR
},
430 { "gl_FragDepth", FRAG_RESULT_DEPTH
},
431 { "gl_FragData", FRAG_RESULT_DATA0
},
435 const struct output_info
*outputs
436 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
438 for (i
= 0; outputs
[i
].Name
; i
++) {
439 if (strcmp(outputs
[i
].Name
, name
) == 0) {
441 return outputs
[i
].Attrib
;
449 * Called when we begin code/IR generation for a new while/do/for loop.
452 push_loop(slang_assemble_ctx
*A
, slang_operation
*loopOper
, slang_ir_node
*loopIR
)
454 A
->LoopOperStack
[A
->LoopDepth
] = loopOper
;
455 A
->LoopIRStack
[A
->LoopDepth
] = loopIR
;
461 * Called when we end code/IR generation for a new while/do/for loop.
464 pop_loop(slang_assemble_ctx
*A
)
466 assert(A
->LoopDepth
> 0);
472 * Return pointer to slang_operation for the loop we're currently inside,
473 * or NULL if not in a loop.
475 static const slang_operation
*
476 current_loop_oper(const slang_assemble_ctx
*A
)
478 if (A
->LoopDepth
> 0)
479 return A
->LoopOperStack
[A
->LoopDepth
- 1];
486 * Return pointer to slang_ir_node for the loop we're currently inside,
487 * or NULL if not in a loop.
489 static slang_ir_node
*
490 current_loop_ir(const slang_assemble_ctx
*A
)
492 if (A
->LoopDepth
> 0)
493 return A
->LoopIRStack
[A
->LoopDepth
- 1];
499 /**********************************************************************/
503 * Map "_asm foo" to IR_FOO, etc.
508 slang_ir_opcode Opcode
;
509 GLuint HaveRetValue
, NumParams
;
513 static slang_asm_info AsmInfo
[] = {
515 { "vec4_add", IR_ADD
, 1, 2 },
516 { "vec4_subtract", IR_SUB
, 1, 2 },
517 { "vec4_multiply", IR_MUL
, 1, 2 },
518 { "vec4_dot", IR_DOT4
, 1, 2 },
519 { "vec3_dot", IR_DOT3
, 1, 2 },
520 { "vec2_dot", IR_DOT2
, 1, 2 },
521 { "vec3_nrm", IR_NRM3
, 1, 1 },
522 { "vec4_nrm", IR_NRM4
, 1, 1 },
523 { "vec3_cross", IR_CROSS
, 1, 2 },
524 { "vec4_lrp", IR_LRP
, 1, 3 },
525 { "vec4_min", IR_MIN
, 1, 2 },
526 { "vec4_max", IR_MAX
, 1, 2 },
527 { "vec4_clamp", IR_CLAMP
, 1, 3 },
528 { "vec4_seq", IR_SEQUAL
, 1, 2 },
529 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
530 { "vec4_sge", IR_SGE
, 1, 2 },
531 { "vec4_sgt", IR_SGT
, 1, 2 },
532 { "vec4_sle", IR_SLE
, 1, 2 },
533 { "vec4_slt", IR_SLT
, 1, 2 },
535 { "vec4_move", IR_MOVE
, 1, 1 },
536 { "vec4_floor", IR_FLOOR
, 1, 1 },
537 { "vec4_frac", IR_FRAC
, 1, 1 },
538 { "vec4_abs", IR_ABS
, 1, 1 },
539 { "vec4_negate", IR_NEG
, 1, 1 },
540 { "vec4_ddx", IR_DDX
, 1, 1 },
541 { "vec4_ddy", IR_DDY
, 1, 1 },
542 /* float binary op */
543 { "float_power", IR_POW
, 1, 2 },
544 /* texture / sampler */
545 { "vec4_tex_1d", IR_TEX
, 1, 2 },
546 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
547 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
548 { "vec4_tex_2d", IR_TEX
, 1, 2 },
549 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
550 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
551 { "vec4_tex_3d", IR_TEX
, 1, 2 },
552 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
553 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
554 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
555 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
556 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
558 /* texture / sampler but with shadow comparison */
559 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
560 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
561 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
562 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
563 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
564 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
565 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
566 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
569 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
570 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
571 { "float_exp", IR_EXP
, 1, 1 },
572 { "float_exp2", IR_EXP2
, 1, 1 },
573 { "float_log2", IR_LOG2
, 1, 1 },
574 { "float_rsq", IR_RSQ
, 1, 1 },
575 { "float_rcp", IR_RCP
, 1, 1 },
576 { "float_sine", IR_SIN
, 1, 1 },
577 { "float_cosine", IR_COS
, 1, 1 },
578 { "float_noise1", IR_NOISE1
, 1, 1},
579 { "float_noise2", IR_NOISE2
, 1, 1},
580 { "float_noise3", IR_NOISE3
, 1, 1},
581 { "float_noise4", IR_NOISE4
, 1, 1},
583 { NULL
, IR_NOP
, 0, 0 }
587 static slang_ir_node
*
588 new_node3(slang_ir_opcode op
,
589 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
591 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
597 n
->InstLocation
= -1;
602 static slang_ir_node
*
603 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
605 return new_node3(op
, c0
, c1
, NULL
);
608 static slang_ir_node
*
609 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
611 return new_node3(op
, c0
, NULL
, NULL
);
614 static slang_ir_node
*
615 new_node0(slang_ir_opcode op
)
617 return new_node3(op
, NULL
, NULL
, NULL
);
622 * Create sequence of two nodes.
624 static slang_ir_node
*
625 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
631 return new_node2(IR_SEQ
, left
, right
);
634 static slang_ir_node
*
635 new_label(slang_label
*label
)
637 slang_ir_node
*n
= new_node0(IR_LABEL
);
644 static slang_ir_node
*
645 new_float_literal(const float v
[4], GLuint size
)
647 slang_ir_node
*n
= new_node0(IR_FLOAT
);
649 COPY_4V(n
->Value
, v
);
650 /* allocate a storage object, but compute actual location (Index) later */
651 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
656 static slang_ir_node
*
657 new_not(slang_ir_node
*n
)
659 return new_node1(IR_NOT
, n
);
664 * Non-inlined function call.
666 static slang_ir_node
*
667 new_function_call(slang_ir_node
*code
, slang_label
*name
)
669 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
678 * Unconditional jump.
680 static slang_ir_node
*
681 new_return(slang_label
*dest
)
683 slang_ir_node
*n
= new_node0(IR_RETURN
);
691 static slang_ir_node
*
692 new_loop(slang_ir_node
*body
)
694 return new_node1(IR_LOOP
, body
);
698 static slang_ir_node
*
699 new_break(slang_ir_node
*loopNode
)
701 slang_ir_node
*n
= new_node0(IR_BREAK
);
703 assert(loopNode
->Opcode
== IR_LOOP
);
705 /* insert this node at head of linked list of cont/break instructions */
706 n
->List
= loopNode
->List
;
714 * Make new IR_BREAK_IF_TRUE.
716 static slang_ir_node
*
717 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
719 slang_ir_node
*loopNode
= current_loop_ir(A
);
722 assert(loopNode
->Opcode
== IR_LOOP
);
723 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
725 /* insert this node at head of linked list of cont/break instructions */
726 n
->List
= loopNode
->List
;
734 * Make new IR_CONT_IF_TRUE node.
736 static slang_ir_node
*
737 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
739 slang_ir_node
*loopNode
= current_loop_ir(A
);
742 assert(loopNode
->Opcode
== IR_LOOP
);
743 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
745 n
->Parent
= loopNode
; /* pointer to containing loop */
746 /* insert this node at head of linked list of cont/break instructions */
747 n
->List
= loopNode
->List
;
754 static slang_ir_node
*
755 new_cond(slang_ir_node
*n
)
757 slang_ir_node
*c
= new_node1(IR_COND
, n
);
762 static slang_ir_node
*
763 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
765 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
770 * New IR_VAR node - a reference to a previously declared variable.
772 static slang_ir_node
*
773 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
775 slang_ir_node
*n
= new_node0(IR_VAR
);
782 /* Set IR node's Var and Store pointers */
784 n
->Store
= var
->store
;
791 * Check if the given function is really just a wrapper for a
792 * basic assembly instruction.
795 slang_is_asm_function(const slang_function
*fun
)
797 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
798 fun
->body
->num_children
== 1 &&
799 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
807 _slang_is_noop(const slang_operation
*oper
)
810 oper
->type
== SLANG_OPER_VOID
||
811 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
819 * Recursively search tree for a node of the given type.
822 static slang_operation
*
823 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
826 if (oper
->type
== type
)
828 for (i
= 0; i
< oper
->num_children
; i
++) {
829 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
839 * Count the number of operations of the given time rooted at 'oper'.
842 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
845 if (oper
->type
== type
) {
848 for (i
= 0; i
< oper
->num_children
; i
++) {
849 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
856 * Check if the 'return' statement found under 'oper' is a "tail return"
857 * that can be no-op'd. For example:
862 * return; // this is a no-op
865 * This is used when determining if a function can be inlined. If the
866 * 'return' is not the last statement, we can't inline the function since
867 * we still need the semantic behaviour of the 'return' but we don't want
868 * to accidentally return from the _calling_ function. We'd need to use an
869 * unconditional branch, but we don't have such a GPU instruction (not
873 _slang_is_tail_return(const slang_operation
*oper
)
875 GLuint k
= oper
->num_children
;
878 const slang_operation
*last
= &oper
->children
[k
- 1];
879 if (last
->type
== SLANG_OPER_RETURN
)
881 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
882 last
->type
== SLANG_OPER_LABEL
)
883 k
--; /* try prev child */
884 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
885 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
886 /* try sub-children */
887 return _slang_is_tail_return(last
);
897 * Generate a variable declaration opeartion.
898 * I.e.: generate AST code for "bool flag = false;"
901 slang_generate_declaration(slang_assemble_ctx
*A
,
902 slang_variable_scope
*scope
,
903 slang_operation
*decl
,
904 slang_type_specifier_type type
,
910 assert(type
== SLANG_SPEC_BOOL
||
911 type
== SLANG_SPEC_INT
);
913 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
915 var
= slang_variable_scope_grow(scope
);
917 slang_fully_specified_type_construct(&var
->type
);
919 var
->type
.specifier
.type
= type
;
920 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
921 decl
->a_id
= var
->a_name
;
922 var
->initializer
= slang_operation_new(1);
923 slang_operation_literal_bool(var
->initializer
, initValue
);
928 slang_resolve_variable(slang_operation
*oper
)
930 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
931 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
937 * Rewrite AST code for "return expression;".
939 * We return values from functions by assinging the returned value to
940 * the hidden __retVal variable which is an extra 'out' parameter we add
941 * to the function signature.
942 * This code basically converts "return expr;" into "__retVal = expr; return;"
944 * \return the new AST code.
946 static slang_operation
*
947 gen_return_with_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
949 slang_operation
*blockOper
, *assignOper
;
951 assert(oper
->type
== SLANG_OPER_RETURN
);
953 blockOper
= slang_operation_new(1);
954 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
955 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
956 slang_operation_add_children(blockOper
, 2);
958 if (A
->UseReturnFlag
) {
967 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
968 ifOper
->type
= SLANG_OPER_IF
;
969 slang_operation_add_children(ifOper
, 3);
971 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
972 cond
->type
= SLANG_OPER_IDENTIFIER
;
973 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
976 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
977 elseOper
->type
= SLANG_OPER_VOID
;
979 assignOper
= slang_oper_child(ifOper
, 1);
982 slang_operation
*setOper
= slang_oper_child(blockOper
, 1);
983 setOper
->type
= SLANG_OPER_ASSIGN
;
984 slang_operation_add_children(setOper
, 2);
986 slang_operation
*lhs
= slang_oper_child(setOper
, 0);
987 lhs
->type
= SLANG_OPER_IDENTIFIER
;
988 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
991 slang_operation
*rhs
= slang_oper_child(setOper
, 1);
992 slang_operation_literal_bool(rhs
, GL_FALSE
);
1003 assignOper
= slang_oper_child(blockOper
, 0);
1005 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
1006 returnOper
->type
= SLANG_OPER_RETURN_INLINED
;
1007 assert(returnOper
->num_children
== 0);
1011 /* __retVal = expression; */
1012 assignOper
->type
= SLANG_OPER_ASSIGN
;
1013 slang_operation_add_children(assignOper
, 2);
1015 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
1016 lhs
->type
= SLANG_OPER_IDENTIFIER
;
1017 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1020 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
1021 slang_operation_copy(rhs
, &oper
->children
[0]);
1024 ///blockOper->locals->outer_scope = oper->locals->outer_scope;
1026 /*slang_print_tree(blockOper, 0);*/
1033 * Rewrite AST code for "return;" (no expression).
1035 static slang_operation
*
1036 gen_return_without_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
1038 slang_operation
*newRet
;
1040 assert(oper
->type
== SLANG_OPER_RETURN
);
1042 if (A
->UseReturnFlag
) {
1047 newRet
= slang_operation_new(1);
1048 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1049 newRet
->type
= SLANG_OPER_ASSIGN
;
1050 slang_operation_add_children(newRet
, 2);
1052 slang_operation
*lhs
= slang_oper_child(newRet
, 0);
1053 lhs
->type
= SLANG_OPER_IDENTIFIER
;
1054 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
1057 slang_operation
*rhs
= slang_oper_child(newRet
, 1);
1058 slang_operation_literal_bool(rhs
, GL_FALSE
);
1066 newRet
= slang_operation_new(1);
1067 newRet
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1068 newRet
->type
= SLANG_OPER_RETURN_INLINED
;
1071 /*slang_print_tree(newRet, 0);*/
1080 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1083 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
1084 GLuint substCount
, slang_variable
**substOld
,
1085 slang_operation
**substNew
, GLboolean isLHS
)
1087 switch (oper
->type
) {
1088 case SLANG_OPER_VARIABLE_DECL
:
1090 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
1091 oper
->a_id
, GL_TRUE
);
1093 if (v
->initializer
&& oper
->num_children
== 0) {
1094 /* set child of oper to copy of initializer */
1095 oper
->num_children
= 1;
1096 oper
->children
= slang_operation_new(1);
1097 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1099 if (oper
->num_children
== 1) {
1100 /* the initializer */
1101 slang_substitute(A
, &oper
->children
[0], substCount
,
1102 substOld
, substNew
, GL_FALSE
);
1106 case SLANG_OPER_IDENTIFIER
:
1107 assert(oper
->num_children
== 0);
1108 if (1/**!isLHS XXX FIX */) {
1109 slang_atom id
= oper
->a_id
;
1112 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1114 if (_mesa_strcmp((char *) oper
->a_id
, "__notRetFlag"))
1115 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1119 /* look for a substitution */
1120 for (i
= 0; i
< substCount
; i
++) {
1121 if (v
== substOld
[i
]) {
1122 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1123 #if 0 /* DEBUG only */
1124 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1125 assert(substNew
[i
]->var
);
1126 assert(substNew
[i
]->var
->a_name
);
1127 printf("Substitute %s with %s in id node %p\n",
1128 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1132 printf("Substitute %s with %f in id node %p\n",
1133 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1137 slang_operation_copy(oper
, substNew
[i
]);
1144 case SLANG_OPER_RETURN
:
1146 slang_operation
*newReturn
;
1147 /* generate new 'return' code' */
1148 if (slang_oper_child(oper
, 0)->type
== SLANG_OPER_VOID
)
1149 newReturn
= gen_return_without_expression(A
, oper
);
1151 newReturn
= gen_return_with_expression(A
, oper
);
1153 /* do substitutions on the new 'return' code */
1154 slang_substitute(A
, newReturn
,
1155 substCount
, substOld
, substNew
, GL_FALSE
);
1157 /* install new 'return' code */
1158 slang_operation_copy(oper
, newReturn
);
1159 slang_operation_destruct(newReturn
);
1163 case SLANG_OPER_ASSIGN
:
1164 case SLANG_OPER_SUBSCRIPT
:
1166 * child[0] can't have substitutions but child[1] can.
1168 slang_substitute(A
, &oper
->children
[0],
1169 substCount
, substOld
, substNew
, GL_TRUE
);
1170 slang_substitute(A
, &oper
->children
[1],
1171 substCount
, substOld
, substNew
, GL_FALSE
);
1173 case SLANG_OPER_FIELD
:
1174 /* XXX NEW - test */
1175 slang_substitute(A
, &oper
->children
[0],
1176 substCount
, substOld
, substNew
, GL_TRUE
);
1181 for (i
= 0; i
< oper
->num_children
; i
++)
1182 slang_substitute(A
, &oper
->children
[i
],
1183 substCount
, substOld
, substNew
, GL_FALSE
);
1190 * Produce inline code for a call to an assembly instruction.
1191 * This is typically used to compile a call to a built-in function like this:
1193 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1195 * __asm vec4_lrp __retVal, a, y, x;
1200 * r = mix(p1, p2, p3);
1210 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1212 static slang_operation
*
1213 slang_inline_asm_function(slang_assemble_ctx
*A
,
1214 slang_function
*fun
, slang_operation
*oper
)
1216 const GLuint numArgs
= oper
->num_children
;
1218 slang_operation
*inlined
;
1219 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1220 slang_variable
**substOld
;
1221 slang_operation
**substNew
;
1223 ASSERT(slang_is_asm_function(fun
));
1224 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1227 printf("Inline %s as %s\n",
1228 (char*) fun->header.a_name,
1229 (char*) fun->body->children[0].a_id);
1233 * We'll substitute formal params with actual args in the asm call.
1235 substOld
= (slang_variable
**)
1236 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1237 substNew
= (slang_operation
**)
1238 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1239 for (i
= 0; i
< numArgs
; i
++) {
1240 substOld
[i
] = fun
->parameters
->variables
[i
];
1241 substNew
[i
] = oper
->children
+ i
;
1244 /* make a copy of the code to inline */
1245 inlined
= slang_operation_new(1);
1246 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1248 /* get rid of the __retVal child */
1249 inlined
->num_children
--;
1250 for (i
= 0; i
< inlined
->num_children
; i
++) {
1251 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1255 /* now do formal->actual substitutions */
1256 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1258 _slang_free(substOld
);
1259 _slang_free(substNew
);
1262 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1263 (char *) fun
->header
.a_name
);
1264 slang_print_tree(inlined
, 3);
1265 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1273 * Inline the given function call operation.
1274 * Return a new slang_operation that corresponds to the inlined code.
1276 static slang_operation
*
1277 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1278 slang_operation
*oper
, slang_operation
*returnOper
)
1285 ParamMode
*paramMode
;
1286 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1287 const GLuint numArgs
= oper
->num_children
;
1288 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1289 slang_operation
*args
= oper
->children
;
1290 slang_operation
*inlined
, *top
;
1291 slang_variable
**substOld
;
1292 slang_operation
**substNew
;
1293 GLuint substCount
, numCopyIn
, i
;
1294 slang_function
*prevFunction
;
1295 slang_variable_scope
*newScope
= NULL
;
1298 prevFunction
= A
->CurFunction
;
1299 A
->CurFunction
= fun
;
1301 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1302 assert(fun
->param_count
== totalArgs
);
1304 /* allocate temporary arrays */
1305 paramMode
= (ParamMode
*)
1306 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1307 substOld
= (slang_variable
**)
1308 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1309 substNew
= (slang_operation
**)
1310 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1313 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1314 (char *) fun
->header
.a_name
,
1315 fun
->parameters
->num_variables
, numArgs
);
1318 if (haveRetValue
&& !returnOper
) {
1319 /* Create 3-child comma sequence for inlined code:
1320 * child[0]: declare __resultTmp
1321 * child[1]: inlined function body
1322 * child[2]: __resultTmp
1324 slang_operation
*commaSeq
;
1325 slang_operation
*declOper
= NULL
;
1326 slang_variable
*resultVar
;
1328 commaSeq
= slang_operation_new(1);
1329 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1330 assert(commaSeq
->locals
);
1331 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1332 commaSeq
->num_children
= 3;
1333 commaSeq
->children
= slang_operation_new(3);
1334 /* allocate the return var */
1335 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1337 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1338 (void*)commaSeq->locals, (char *) fun->header.a_name);
1341 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1342 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1343 resultVar
->isTemp
= GL_TRUE
;
1345 /* child[0] = __resultTmp declaration */
1346 declOper
= &commaSeq
->children
[0];
1347 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1348 declOper
->a_id
= resultVar
->a_name
;
1349 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1351 /* child[1] = function body */
1352 inlined
= &commaSeq
->children
[1];
1353 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1355 /* child[2] = __resultTmp reference */
1356 returnOper
= &commaSeq
->children
[2];
1357 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1358 returnOper
->a_id
= resultVar
->a_name
;
1359 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1364 top
= inlined
= slang_operation_new(1);
1365 /* XXXX this may be inappropriate!!!! */
1366 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1370 assert(inlined
->locals
);
1372 /* Examine the parameters, look for inout/out params, look for possible
1373 * substitutions, etc:
1374 * param type behaviour
1375 * in copy actual to local
1376 * const in substitute param with actual
1380 for (i
= 0; i
< totalArgs
; i
++) {
1381 slang_variable
*p
= fun
->parameters
->variables
[i
];
1383 printf("Param %d: %s %s \n", i,
1384 slang_type_qual_string(p->type.qualifier),
1385 (char *) p->a_name);
1387 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1388 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1389 /* an output param */
1390 slang_operation
*arg
;
1395 paramMode
[i
] = SUBST
;
1397 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1398 slang_resolve_variable(arg
);
1400 /* replace parameter 'p' with argument 'arg' */
1401 substOld
[substCount
] = p
;
1402 substNew
[substCount
] = arg
; /* will get copied */
1405 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1406 /* a constant input param */
1407 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1408 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1409 /* replace all occurances of this parameter variable with the
1410 * actual argument variable or a literal.
1412 paramMode
[i
] = SUBST
;
1413 slang_resolve_variable(&args
[i
]);
1414 substOld
[substCount
] = p
;
1415 substNew
[substCount
] = &args
[i
]; /* will get copied */
1419 paramMode
[i
] = COPY_IN
;
1423 paramMode
[i
] = COPY_IN
;
1425 assert(paramMode
[i
]);
1428 /* actual code inlining: */
1429 slang_operation_copy(inlined
, fun
->body
);
1431 /*** XXX review this */
1432 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1433 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1434 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1437 printf("======================= orig body code ======================\n");
1438 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1439 slang_print_tree(fun
->body
, 8);
1440 printf("======================= copied code =========================\n");
1441 slang_print_tree(inlined
, 8);
1444 /* do parameter substitution in inlined code: */
1445 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1448 printf("======================= subst code ==========================\n");
1449 slang_print_tree(inlined
, 8);
1450 printf("=============================================================\n");
1453 /* New prolog statements: (inserted before the inlined code)
1454 * Copy the 'in' arguments.
1457 for (i
= 0; i
< numArgs
; i
++) {
1458 if (paramMode
[i
] == COPY_IN
) {
1459 slang_variable
*p
= fun
->parameters
->variables
[i
];
1460 /* declare parameter 'p' */
1461 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1465 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1466 assert(decl
->locals
);
1467 decl
->locals
->outer_scope
= inlined
->locals
;
1468 decl
->a_id
= p
->a_name
;
1469 decl
->num_children
= 1;
1470 decl
->children
= slang_operation_new(1);
1472 /* child[0] is the var's initializer */
1473 slang_operation_copy(&decl
->children
[0], args
+ i
);
1475 /* add parameter 'p' to the local variable scope here */
1477 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1478 pCopy
->type
= p
->type
;
1479 pCopy
->a_name
= p
->a_name
;
1480 pCopy
->array_len
= p
->array_len
;
1483 newScope
= inlined
->locals
;
1488 /* Now add copies of the function's local vars to the new variable scope */
1489 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1490 slang_variable
*p
= fun
->parameters
->variables
[i
];
1491 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1492 pCopy
->type
= p
->type
;
1493 pCopy
->a_name
= p
->a_name
;
1494 pCopy
->array_len
= p
->array_len
;
1498 /* New epilog statements:
1499 * 1. Create end of function label to jump to from return statements.
1500 * 2. Copy the 'out' parameter vars
1503 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1505 inlined
->num_children
);
1506 lab
->type
= SLANG_OPER_LABEL
;
1507 lab
->label
= A
->curFuncEndLabel
;
1510 for (i
= 0; i
< totalArgs
; i
++) {
1511 if (paramMode
[i
] == COPY_OUT
) {
1512 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1513 /* actualCallVar = outParam */
1514 /*if (i > 0 || !haveRetValue)*/
1515 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1517 inlined
->num_children
);
1518 ass
->type
= SLANG_OPER_ASSIGN
;
1519 ass
->num_children
= 2;
1520 ass
->locals
->outer_scope
= inlined
->locals
;
1521 ass
->children
= slang_operation_new(2);
1522 ass
->children
[0] = args
[i
]; /*XXX copy */
1523 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1524 ass
->children
[1].a_id
= p
->a_name
;
1525 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1529 _slang_free(paramMode
);
1530 _slang_free(substOld
);
1531 _slang_free(substNew
);
1533 /* Update scoping to use the new local vars instead of the
1534 * original function's vars. This is especially important
1535 * for nested inlining.
1538 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1541 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1542 (char *) fun
->header
.a_name
,
1543 fun
->parameters
->num_variables
, numArgs
);
1544 slang_print_tree(top
, 0);
1548 A
->CurFunction
= prevFunction
;
1555 * Insert declaration for "bool __notRetFlag" in given block operation.
1556 * This is used when we can't emit "early" return statements in subroutines.
1559 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1561 slang_operation
*decl
;
1563 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1564 oper
->type
== SLANG_OPER_SEQUENCE
);
1566 decl
= slang_operation_insert_child(oper
, 1);
1568 slang_generate_declaration(A
, oper
->locals
, decl
,
1569 SLANG_SPEC_BOOL
, "__notRetFlag", GL_TRUE
);
1571 /*slang_print_tree(oper, 0);*/
1576 * Recursively replace instances of the old node type with the new type.
1579 replace_node_type(slang_operation
*oper
, slang_operation_type oldType
,
1580 slang_operation_type newType
)
1584 if (oper
->type
== oldType
)
1585 oper
->type
= newType
;
1587 for (i
= 0; i
< slang_oper_num_children(oper
); i
++) {
1588 replace_node_type(slang_oper_child(oper
, i
), oldType
, newType
);
1595 * Test if the given function body has an "early return". That is, there's
1596 * a 'return' statement that's not the very last instruction in the body.
1599 has_early_return(const slang_operation
*funcBody
)
1601 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1604 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1612 * Emit IR code for a function call. This does one of two things:
1613 * 1. Inline the function's code
1614 * 2. Create an IR for the function's body and create a real call to it.
1616 static slang_ir_node
*
1617 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1618 slang_operation
*oper
, slang_operation
*dest
)
1621 slang_operation
*instance
;
1622 slang_label
*prevFuncEndLabel
;
1625 prevFuncEndLabel
= A
->curFuncEndLabel
;
1626 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1627 A
->curFuncEndLabel
= _slang_label_new(name
);
1628 assert(A
->curFuncEndLabel
);
1631 * 'instance' is basically a copy of the function's body with various
1635 if (slang_is_asm_function(fun
) && !dest
) {
1636 /* assemble assembly function - tree style */
1637 instance
= slang_inline_asm_function(A
, fun
, oper
);
1640 /* non-assembly function */
1641 /* We always generate an "inline-able" block of code here.
1643 * 1. insert the inline code
1644 * 2. Generate a call to the "inline" code as a subroutine
1646 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1648 if (earlyReturn
&& !A
->EmitContReturn
) {
1649 A
->UseReturnFlag
= GL_TRUE
;
1652 instance
= slang_inline_function_call(A
, fun
, oper
, dest
);
1657 /* The function we're calling has one or more 'return' statements
1658 * that prevent us from inlining the function's code.
1660 * In this case, change the function's body type from
1661 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1662 * During code emit this will result in a true subroutine call.
1664 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1666 slang_operation
*callOper
;
1668 assert(instance
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1669 instance
->type
== SLANG_OPER_SEQUENCE
);
1671 if (_slang_function_has_return_value(fun
) && !dest
) {
1672 assert(instance
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1673 assert(instance
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1674 callOper
= &instance
->children
[1];
1677 callOper
= instance
;
1680 if (A
->UseReturnFlag
) {
1681 /* Early returns not supported. Create a _returnFlag variable
1682 * that's set upon 'return' and tested elsewhere to no-op any
1683 * remaining instructions in the subroutine.
1685 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1686 callOper
->type
== SLANG_OPER_SEQUENCE
);
1687 declare_return_flag(A
, callOper
);
1690 /* We can emit real 'return' statements. If we generated any
1691 * 'inline return' statements during function instantiation,
1692 * change them back to regular 'return' statements.
1694 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1698 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1699 callOper
->fun
= fun
;
1700 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1703 /* If there are any 'return' statements remaining, they're at the
1704 * very end of the function and can effectively become no-ops.
1706 replace_node_type(instance
, SLANG_OPER_RETURN_INLINED
,
1714 /* Replace the function call with the instance block (or new CALL stmt) */
1715 slang_operation_destruct(oper
);
1717 _slang_free(instance
);
1720 assert(instance
->locals
);
1721 printf("*** Inlined code for call to %s:\n", (char*) fun
->header
.a_name
);
1722 slang_print_tree(oper
, 10);
1726 n
= _slang_gen_operation(A
, oper
);
1728 /*_slang_label_delete(A->curFuncEndLabel);*/
1729 A
->curFuncEndLabel
= prevFuncEndLabel
;
1731 if (A
->pragmas
->Debug
) {
1733 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1734 n
->Comment
= _slang_strdup(s
);
1737 A
->UseReturnFlag
= GL_FALSE
;
1743 static slang_asm_info
*
1744 slang_find_asm_info(const char *name
)
1747 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1748 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1757 * Some write-masked assignments are simple, but others are hard.
1760 * v.xy = vec2(a, b);
1763 * v.zy = vec2(a, b);
1764 * this gets transformed/swizzled into:
1765 * v.zy = vec2(a, b).*yx* (* = don't care)
1766 * This function helps to determine simple vs. non-simple.
1769 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1771 switch (writemask
) {
1773 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1775 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1777 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1779 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1781 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1782 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1784 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1785 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1786 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1787 case WRITEMASK_XYZW
:
1788 return swizzle
== SWIZZLE_NOOP
;
1796 * Convert the given swizzle into a writemask. In some cases this
1797 * is trivial, in other cases, we'll need to also swizzle the right
1798 * hand side to put components in the right places.
1799 * See comment above for more info.
1800 * XXX this function could be simplified and should probably be renamed.
1801 * \param swizzle the incoming swizzle
1802 * \param writemaskOut returns the writemask
1803 * \param swizzleOut swizzle to apply to the right-hand-side
1804 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1807 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1808 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1810 GLuint mask
= 0x0, newSwizzle
[4];
1813 /* make new dst writemask, compute size */
1814 for (i
= 0; i
< 4; i
++) {
1815 const GLuint swz
= GET_SWZ(swizzle
, i
);
1816 if (swz
== SWIZZLE_NIL
) {
1820 assert(swz
>= 0 && swz
<= 3);
1822 if (swizzle
!= SWIZZLE_XXXX
&&
1823 swizzle
!= SWIZZLE_YYYY
&&
1824 swizzle
!= SWIZZLE_ZZZZ
&&
1825 swizzle
!= SWIZZLE_WWWW
&&
1826 (mask
& (1 << swz
))) {
1827 /* a channel can't be specified twice (ex: ".xyyz") */
1828 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1829 _mesa_swizzle_string(swizzle
, 0, 0));
1835 assert(mask
<= 0xf);
1836 size
= i
; /* number of components in mask/swizzle */
1838 *writemaskOut
= mask
;
1840 /* make new src swizzle, by inversion */
1841 for (i
= 0; i
< 4; i
++) {
1842 newSwizzle
[i
] = i
; /*identity*/
1844 for (i
= 0; i
< size
; i
++) {
1845 const GLuint swz
= GET_SWZ(swizzle
, i
);
1846 newSwizzle
[swz
] = i
;
1848 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1853 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1855 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1857 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1859 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1861 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1869 #if 0 /* not used, but don't remove just yet */
1871 * Recursively traverse 'oper' to produce a swizzle mask in the event
1872 * of any vector subscripts and swizzle suffixes.
1873 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1876 resolve_swizzle(const slang_operation
*oper
)
1878 if (oper
->type
== SLANG_OPER_FIELD
) {
1879 /* writemask from .xyzw suffix */
1881 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1882 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1886 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1887 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1891 return SWIZZLE_XYZW
;
1893 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1894 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1895 /* writemask from [index] */
1896 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1897 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1902 swizzle
= SWIZZLE_XXXX
;
1905 swizzle
= SWIZZLE_YYYY
;
1908 swizzle
= SWIZZLE_ZZZZ
;
1911 swizzle
= SWIZZLE_WWWW
;
1914 swizzle
= SWIZZLE_XYZW
;
1916 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1920 return SWIZZLE_XYZW
;
1928 * Recursively descend through swizzle nodes to find the node's storage info.
1930 static slang_ir_storage
*
1931 get_store(const slang_ir_node
*n
)
1933 if (n
->Opcode
== IR_SWIZZLE
) {
1934 return get_store(n
->Children
[0]);
1942 * Generate IR tree for an asm instruction/operation such as:
1943 * __asm vec4_dot __retVal.x, v1, v2;
1945 static slang_ir_node
*
1946 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1947 slang_operation
*dest
)
1949 const slang_asm_info
*info
;
1950 slang_ir_node
*kids
[3], *n
;
1951 GLuint j
, firstOperand
;
1953 assert(oper
->type
== SLANG_OPER_ASM
);
1955 info
= slang_find_asm_info((char *) oper
->a_id
);
1957 _mesa_problem(NULL
, "undefined __asm function %s\n",
1958 (char *) oper
->a_id
);
1961 assert(info
->NumParams
<= 3);
1963 if (info
->NumParams
== oper
->num_children
) {
1964 /* Storage for result is not specified.
1965 * Children[0], [1], [2] are the operands.
1970 /* Storage for result (child[0]) is specified.
1971 * Children[1], [2], [3] are the operands.
1976 /* assemble child(ren) */
1977 kids
[0] = kids
[1] = kids
[2] = NULL
;
1978 for (j
= 0; j
< info
->NumParams
; j
++) {
1979 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1984 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1987 /* Setup n->Store to be a particular location. Otherwise, storage
1988 * for the result (a temporary) will be allocated later.
1990 slang_operation
*dest_oper
;
1993 dest_oper
= &oper
->children
[0];
1995 n0
= _slang_gen_operation(A
, dest_oper
);
2000 n
->Store
= n0
->Store
;
2002 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
2013 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
2016 for (i
= 0; i
< scope
->num_functions
; i
++) {
2017 slang_function
*f
= &scope
->functions
[i
];
2018 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
2019 printf(" %s (%d args)\n", name
, f
->param_count
);
2022 if (scope
->outer_scope
)
2023 print_funcs(scope
->outer_scope
, name
);
2029 * Find a function of the given name, taking 'numArgs' arguments.
2030 * This is the function we'll try to call when there is no exact match
2031 * between function parameters and call arguments.
2033 * XXX we should really create a list of candidate functions and try
2036 static slang_function
*
2037 _slang_find_function_by_argc(slang_function_scope
*scope
,
2038 const char *name
, int numArgs
)
2042 for (i
= 0; i
< scope
->num_functions
; i
++) {
2043 slang_function
*f
= &scope
->functions
[i
];
2044 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2045 int haveRetValue
= _slang_function_has_return_value(f
);
2046 if (numArgs
== f
->param_count
- haveRetValue
)
2050 scope
= scope
->outer_scope
;
2057 static slang_function
*
2058 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
2061 slang_function
*maxFunc
= NULL
;
2066 for (i
= 0; i
< scope
->num_functions
; i
++) {
2067 slang_function
*f
= &scope
->functions
[i
];
2068 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2069 if (f
->param_count
> maxArgs
) {
2070 maxArgs
= f
->param_count
;
2075 scope
= scope
->outer_scope
;
2083 * Generate a new slang_function which is a constructor for a user-defined
2086 static slang_function
*
2087 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2089 const GLint numFields
= str
->fields
->num_variables
;
2090 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2092 /* function header (name, return type) */
2093 fun
->header
.a_name
= str
->a_name
;
2094 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2095 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2096 fun
->header
.type
.specifier
._struct
= str
;
2098 /* function parameters (= struct's fields) */
2101 for (i
= 0; i
< numFields
; i
++) {
2103 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2105 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2106 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2107 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2109 fun
->param_count
= fun
->parameters
->num_variables
;
2112 /* Add __retVal to params */
2114 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2115 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2117 p
->a_name
= a_retVal
;
2118 p
->type
= fun
->header
.type
;
2119 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2123 /* function body is:
2133 slang_variable_scope
*scope
;
2134 slang_variable
*var
;
2137 fun
->body
= slang_operation_new(1);
2138 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2139 fun
->body
->num_children
= numFields
+ 2;
2140 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2142 scope
= fun
->body
->locals
;
2143 scope
->outer_scope
= fun
->parameters
;
2145 /* create local var 't' */
2146 var
= slang_variable_scope_grow(scope
);
2147 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2148 var
->type
= fun
->header
.type
;
2152 slang_operation
*decl
;
2154 decl
= &fun
->body
->children
[0];
2155 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2156 decl
->locals
= _slang_variable_scope_new(scope
);
2157 decl
->a_id
= var
->a_name
;
2160 /* assign params to fields of t */
2161 for (i
= 0; i
< numFields
; i
++) {
2162 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2164 assign
->type
= SLANG_OPER_ASSIGN
;
2165 assign
->locals
= _slang_variable_scope_new(scope
);
2166 assign
->num_children
= 2;
2167 assign
->children
= slang_operation_new(2);
2170 slang_operation
*lhs
= &assign
->children
[0];
2172 lhs
->type
= SLANG_OPER_FIELD
;
2173 lhs
->locals
= _slang_variable_scope_new(scope
);
2174 lhs
->num_children
= 1;
2175 lhs
->children
= slang_operation_new(1);
2176 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2178 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2179 lhs
->children
[0].a_id
= var
->a_name
;
2180 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2183 lhs
->children
[1].num_children
= 1;
2184 lhs
->children
[1].children
= slang_operation_new(1);
2185 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2186 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2187 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2192 slang_operation
*rhs
= &assign
->children
[1];
2194 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2195 rhs
->locals
= _slang_variable_scope_new(scope
);
2196 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2202 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2204 ret
->type
= SLANG_OPER_RETURN
;
2205 ret
->locals
= _slang_variable_scope_new(scope
);
2206 ret
->num_children
= 1;
2207 ret
->children
= slang_operation_new(1);
2208 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2209 ret
->children
[0].a_id
= var
->a_name
;
2210 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2214 slang_print_function(fun, 1);
2221 * Find/create a function (constructor) for the given structure name.
2223 static slang_function
*
2224 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2227 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2228 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2229 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2230 /* found a structure type that matches the function name */
2231 if (!str
->constructor
) {
2232 /* create the constructor function now */
2233 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2235 return str
->constructor
;
2243 * Generate a new slang_function to satisfy a call to an array constructor.
2244 * Ex: float[3](1., 2., 3.)
2246 static slang_function
*
2247 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2249 slang_type_specifier_type baseType
;
2250 slang_function
*fun
;
2253 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2257 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2259 num_elements
= oper
->num_children
;
2261 /* function header, return type */
2263 fun
->header
.a_name
= oper
->a_id
;
2264 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2265 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2266 fun
->header
.type
.specifier
._array
=
2267 slang_type_specifier_new(baseType
, NULL
, NULL
);
2268 fun
->header
.type
.array_len
= num_elements
;
2271 /* function parameters (= number of elements) */
2274 for (i
= 0; i
< num_elements
; i
++) {
2276 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2278 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2280 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2281 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2282 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2283 p
->type
.specifier
.type
= baseType
;
2285 fun
->param_count
= fun
->parameters
->num_variables
;
2288 /* Add __retVal to params */
2290 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2291 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2293 p
->a_name
= a_retVal
;
2294 p
->type
= fun
->header
.type
;
2295 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2296 p
->type
.specifier
.type
= baseType
;
2300 /* function body is:
2310 slang_variable_scope
*scope
;
2311 slang_variable
*var
;
2314 fun
->body
= slang_operation_new(1);
2315 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2316 fun
->body
->num_children
= num_elements
+ 2;
2317 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2319 scope
= fun
->body
->locals
;
2320 scope
->outer_scope
= fun
->parameters
;
2322 /* create local var 't' */
2323 var
= slang_variable_scope_grow(scope
);
2324 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2325 var
->type
= fun
->header
.type
;/*XXX copy*/
2329 slang_operation
*decl
;
2331 decl
= &fun
->body
->children
[0];
2332 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2333 decl
->locals
= _slang_variable_scope_new(scope
);
2334 decl
->a_id
= var
->a_name
;
2337 /* assign params to elements of t */
2338 for (i
= 0; i
< num_elements
; i
++) {
2339 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2341 assign
->type
= SLANG_OPER_ASSIGN
;
2342 assign
->locals
= _slang_variable_scope_new(scope
);
2343 assign
->num_children
= 2;
2344 assign
->children
= slang_operation_new(2);
2347 slang_operation
*lhs
= &assign
->children
[0];
2349 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2350 lhs
->locals
= _slang_variable_scope_new(scope
);
2351 lhs
->num_children
= 2;
2352 lhs
->children
= slang_operation_new(2);
2354 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2355 lhs
->children
[0].a_id
= var
->a_name
;
2356 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2358 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2359 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2363 slang_operation
*rhs
= &assign
->children
[1];
2365 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2366 rhs
->locals
= _slang_variable_scope_new(scope
);
2367 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2373 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2375 ret
->type
= SLANG_OPER_RETURN
;
2376 ret
->locals
= _slang_variable_scope_new(scope
);
2377 ret
->num_children
= 1;
2378 ret
->children
= slang_operation_new(1);
2379 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2380 ret
->children
[0].a_id
= var
->a_name
;
2381 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2386 slang_print_function(fun, 1);
2394 _slang_is_vec_mat_type(const char *name
)
2396 static const char *vecmat_types
[] = {
2397 "float", "int", "bool",
2398 "vec2", "vec3", "vec4",
2399 "ivec2", "ivec3", "ivec4",
2400 "bvec2", "bvec3", "bvec4",
2401 "mat2", "mat3", "mat4",
2402 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2406 for (i
= 0; vecmat_types
[i
]; i
++)
2407 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2414 * Assemble a function call, given a particular function name.
2415 * \param name the function's name (operators like '*' are possible).
2417 static slang_ir_node
*
2418 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2419 slang_operation
*oper
, slang_operation
*dest
)
2421 slang_operation
*params
= oper
->children
;
2422 const GLuint param_count
= oper
->num_children
;
2424 slang_function
*fun
;
2427 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2428 if (atom
== SLANG_ATOM_NULL
)
2431 if (oper
->array_constructor
) {
2432 /* this needs special handling */
2433 fun
= _slang_make_array_constructor(A
, oper
);
2436 /* Try to find function by name and exact argument type matching */
2437 GLboolean error
= GL_FALSE
;
2438 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2439 &A
->space
, A
->atoms
, A
->log
, &error
);
2441 slang_info_log_error(A
->log
,
2442 "Function '%s' not found (check argument types)",
2449 /* Next, try locating a constructor function for a user-defined type */
2450 fun
= _slang_locate_struct_constructor(A
, name
);
2454 * At this point, some heuristics are used to try to find a function
2455 * that matches the calling signature by means of casting or "unrolling"
2459 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2460 /* Next, if this call looks like a vec() or mat() constructor call,
2461 * try "unwinding" the args to satisfy a constructor.
2463 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2465 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2466 slang_info_log_error(A
->log
,
2467 "Function '%s' not found (check argument types)",
2474 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2475 /* Next, try casting args to the types of the formal parameters */
2476 int numArgs
= oper
->num_children
;
2477 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2478 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2479 slang_info_log_error(A
->log
,
2480 "Function '%s' not found (check argument types)",
2488 slang_info_log_error(A
->log
,
2489 "Function '%s' not found (check argument types)",
2495 /* The function body may be in another compilation unit.
2496 * We'll try concatenating the shaders and recompile at link time.
2498 A
->UnresolvedRefs
= GL_TRUE
;
2499 return new_node1(IR_NOP
, NULL
);
2502 /* type checking to be sure function's return type matches 'dest' type */
2506 slang_typeinfo_construct(&t0
);
2507 typeof_operation(A
, dest
, &t0
);
2509 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2510 slang_info_log_error(A
->log
,
2511 "Incompatible type returned by call to '%s'",
2517 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2519 if (n
&& !n
->Store
&& !dest
2520 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2521 /* setup n->Store for the result of the function call */
2522 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2523 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2524 /*printf("Alloc storage for function result, size %d \n", size);*/
2527 if (oper
->array_constructor
) {
2528 /* free the temporary array constructor function now */
2529 slang_function_destruct(fun
);
2536 static slang_ir_node
*
2537 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2539 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2541 slang_variable
*var
;
2543 /* NOTE: In GLSL 1.20, there's only one kind of method
2544 * call: array.length(). Anything else is an error.
2546 if (oper
->a_id
!= a_length
) {
2547 slang_info_log_error(A
->log
,
2548 "Undefined method call '%s'", (char *) oper
->a_id
);
2552 /* length() takes no arguments */
2553 if (oper
->num_children
> 0) {
2554 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2558 /* lookup the object/variable */
2559 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2560 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2561 slang_info_log_error(A
->log
,
2562 "Undefined object '%s'", (char *) oper
->a_obj
);
2566 /* Create a float/literal IR node encoding the array length */
2567 n
= new_node0(IR_FLOAT
);
2569 n
->Value
[0] = (float) _slang_array_length(var
);
2570 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2577 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2579 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2580 oper
->type
== SLANG_OPER_LITERAL_INT
||
2581 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2582 if (oper
->literal
[0])
2588 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2589 oper
->num_children
== 1) {
2590 return _slang_is_constant_cond(&oper
->children
[0], value
);
2597 * Test if an operation is a scalar or boolean.
2600 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2602 slang_typeinfo type
;
2605 slang_typeinfo_construct(&type
);
2606 typeof_operation(A
, oper
, &type
);
2607 size
= _slang_sizeof_type_specifier(&type
.spec
);
2608 slang_typeinfo_destruct(&type
);
2614 * Test if an operation is boolean.
2617 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2619 slang_typeinfo type
;
2622 slang_typeinfo_construct(&type
);
2623 typeof_operation(A
, oper
, &type
);
2624 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2625 slang_typeinfo_destruct(&type
);
2631 * Check if a loop contains a 'continue' statement.
2632 * Stop looking if we find a nested loop.
2635 _slang_loop_contains_continue(const slang_operation
*oper
)
2637 switch (oper
->type
) {
2638 case SLANG_OPER_CONTINUE
:
2640 case SLANG_OPER_FOR
:
2642 case SLANG_OPER_WHILE
:
2643 /* stop upon finding a nested loop */
2649 for (i
= 0; i
< oper
->num_children
; i
++) {
2650 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2651 if (_slang_loop_contains_continue(child
))
2661 * Check if a loop contains a 'continue' or 'break' statement.
2662 * Stop looking if we find a nested loop.
2665 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2667 switch (oper
->type
) {
2668 case SLANG_OPER_CONTINUE
:
2669 case SLANG_OPER_BREAK
:
2671 case SLANG_OPER_FOR
:
2673 case SLANG_OPER_WHILE
:
2674 /* stop upon finding a nested loop */
2680 for (i
= 0; i
< oper
->num_children
; i
++) {
2681 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2682 if (_slang_loop_contains_continue_or_break(child
))
2692 * Replace 'break' and 'continue' statements inside a do and while loops.
2693 * This is a recursive helper function used by
2694 * _slang_gen_do/while_without_continue().
2697 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2699 switch (oper
->type
) {
2700 case SLANG_OPER_BREAK
:
2701 /* replace 'break' with "_notBreakFlag = false; break" */
2703 slang_operation
*block
= oper
;
2704 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2705 slang_operation_add_children(block
, 2);
2707 slang_operation
*assign
= slang_oper_child(block
, 0);
2708 assign
->type
= SLANG_OPER_ASSIGN
;
2709 slang_operation_add_children(assign
, 2);
2711 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2712 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2715 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2716 slang_operation_literal_bool(rhs
, GL_FALSE
);
2720 slang_operation
*brk
= slang_oper_child(block
, 1);
2721 brk
->type
= SLANG_OPER_BREAK
;
2722 assert(!brk
->children
);
2726 case SLANG_OPER_CONTINUE
:
2727 /* convert continue into a break */
2728 oper
->type
= SLANG_OPER_BREAK
;
2730 case SLANG_OPER_FOR
:
2732 case SLANG_OPER_WHILE
:
2733 /* stop upon finding a nested loop */
2739 for (i
= 0; i
< oper
->num_children
; i
++) {
2740 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2748 * Transform a while-loop so that continue statements are converted to breaks.
2749 * Then do normal IR code generation.
2753 * while (LOOPCOND) {
2765 * bool _notBreakFlag = 1;
2766 * while (_notBreakFlag && LOOPCOND) {
2770 * break; // was continue
2773 * _notBreakFlag = 0; // was
2780 static slang_ir_node
*
2781 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2783 slang_operation
*top
;
2784 slang_operation
*innerBody
;
2786 assert(oper
->type
== SLANG_OPER_WHILE
);
2788 top
= slang_operation_new(1);
2789 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2790 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2791 slang_operation_add_children(top
, 2);
2793 /* declare: bool _notBreakFlag = true */
2795 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2796 slang_generate_declaration(A
, top
->locals
, condDecl
,
2797 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2800 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2802 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2803 outerWhile
->type
= SLANG_OPER_WHILE
;
2804 slang_operation_add_children(outerWhile
, 2);
2806 /* _notBreakFlag && LOOPCOND */
2808 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2809 cond
->type
= SLANG_OPER_LOGICALAND
;
2810 slang_operation_add_children(cond
, 2);
2812 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2813 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2816 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2817 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2823 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2824 innerDo
->type
= SLANG_OPER_DO
;
2825 slang_operation_add_children(innerDo
, 2);
2827 /* copy original do-loop body into inner do-loop's body */
2828 innerBody
= slang_oper_child(innerDo
, 0);
2829 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2830 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2832 /* inner do-loop's condition is constant/false */
2834 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2835 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2840 /* Finally, in innerBody,
2841 * replace "break" with "_notBreakFlag = 0; break"
2842 * replace "continue" with "break"
2844 replace_break_and_cont(A
, innerBody
);
2846 /*slang_print_tree(top, 0);*/
2848 return _slang_gen_operation(A
, top
);
2855 * Generate loop code using high-level IR_LOOP instruction
2857 static slang_ir_node
*
2858 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2862 * BREAK if !expr (child[0])
2863 * body code (child[1])
2865 slang_ir_node
*loop
, *breakIf
, *body
;
2866 GLboolean isConst
, constTrue
;
2868 if (!A
->EmitContReturn
) {
2869 /* We don't want to emit CONT instructions. If this while-loop has
2870 * a continue, translate it away.
2872 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2873 return _slang_gen_while_without_continue(A
, oper
);
2877 /* type-check expression */
2878 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2879 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2883 /* Check if loop condition is a constant */
2884 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2886 if (isConst
&& !constTrue
) {
2887 /* loop is never executed! */
2888 return new_node0(IR_NOP
);
2891 /* Begin new loop */
2892 loop
= new_loop(NULL
);
2894 /* save loop state */
2895 push_loop(A
, oper
, loop
);
2897 if (isConst
&& constTrue
) {
2898 /* while(nonzero constant), no conditional break */
2903 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2904 breakIf
= new_break_if_true(A
, cond
);
2906 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2907 loop
->Children
[0] = new_seq(breakIf
, body
);
2909 /* Do infinite loop detection */
2910 /* loop->List is head of linked list of break/continue nodes */
2911 if (!loop
->List
&& isConst
&& constTrue
) {
2912 /* infinite loop detected */
2914 slang_info_log_error(A
->log
, "Infinite loop detected!");
2918 /* restore loop state */
2926 * Transform a do-while-loop so that continue statements are converted to breaks.
2927 * Then do normal IR code generation.
2938 * } while (LOOPCOND);
2943 * bool _notBreakFlag = 1;
2948 * break; // was continue
2951 * _notBreakFlag = 0; // was
2955 * } while (_notBreakFlag && LOOPCOND);
2958 static slang_ir_node
*
2959 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2961 slang_operation
*top
;
2962 slang_operation
*innerBody
;
2964 assert(oper
->type
== SLANG_OPER_DO
);
2966 top
= slang_operation_new(1);
2967 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2968 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2969 slang_operation_add_children(top
, 2);
2971 /* declare: bool _notBreakFlag = true */
2973 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2974 slang_generate_declaration(A
, top
->locals
, condDecl
,
2975 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2978 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2980 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2981 outerDo
->type
= SLANG_OPER_DO
;
2982 slang_operation_add_children(outerDo
, 2);
2986 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2987 innerDo
->type
= SLANG_OPER_DO
;
2988 slang_operation_add_children(innerDo
, 2);
2990 /* copy original do-loop body into inner do-loop's body */
2991 innerBody
= slang_oper_child(innerDo
, 0);
2992 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2993 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2995 /* inner do-loop's condition is constant/false */
2997 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2998 slang_operation_literal_bool(constFalse
, GL_FALSE
);
3002 /* _notBreakFlag && LOOPCOND */
3004 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
3005 cond
->type
= SLANG_OPER_LOGICALAND
;
3006 slang_operation_add_children(cond
, 2);
3008 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
3009 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
3012 slang_operation
*origCond
= slang_oper_child(cond
, 1);
3013 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
3018 /* Finally, in innerBody,
3019 * replace "break" with "_notBreakFlag = 0; break"
3020 * replace "continue" with "break"
3022 replace_break_and_cont(A
, innerBody
);
3024 /*slang_print_tree(top, 0);*/
3026 return _slang_gen_operation(A
, top
);
3031 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
3033 static slang_ir_node
*
3034 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
3038 * body code (child[0])
3040 * BREAK if !expr (child[1])
3042 slang_ir_node
*loop
;
3043 GLboolean isConst
, constTrue
;
3045 if (!A
->EmitContReturn
) {
3046 /* We don't want to emit CONT instructions. If this do-loop has
3047 * a continue, translate it away.
3049 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
3050 return _slang_gen_do_without_continue(A
, oper
);
3054 /* type-check expression */
3055 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
3056 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
3060 loop
= new_loop(NULL
);
3062 /* save loop state */
3063 push_loop(A
, oper
, loop
);
3066 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3068 /* Check if loop condition is a constant */
3069 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3070 if (isConst
&& constTrue
) {
3071 /* do { } while(1) ==> no conditional break */
3072 loop
->Children
[1] = NULL
; /* no tail code */
3076 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3077 loop
->Children
[1] = new_break_if_true(A
, cond
);
3080 /* XXX we should do infinite loop detection, as above */
3082 /* restore loop state */
3090 * Recursively count the number of operations rooted at 'oper'.
3091 * This gives some kind of indication of the size/complexity of an operation.
3094 sizeof_operation(const slang_operation
*oper
)
3097 GLuint count
= 1; /* me */
3099 for (i
= 0; i
< oper
->num_children
; i
++) {
3100 count
+= sizeof_operation(&oper
->children
[i
]);
3111 * Determine if a for-loop can be unrolled.
3112 * At this time, only a rather narrow class of for loops can be unrolled.
3113 * See code for details.
3114 * When a loop can't be unrolled because it's too large we'll emit a
3115 * message to the log.
3118 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3122 const char *varName
;
3125 if (oper
->type
!= SLANG_OPER_FOR
)
3128 assert(oper
->num_children
== 4);
3130 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3133 /* children[0] must be either "int i=constant" or "i=constant" */
3134 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3135 slang_variable
*var
;
3137 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3140 varId
= oper
->children
[0].children
[0].a_id
;
3142 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3146 if (!var
->initializer
)
3148 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3150 start
= (GLint
) var
->initializer
->literal
[0];
3152 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3153 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3155 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3157 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3160 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3162 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3168 /* children[1] must be "i<constant" */
3169 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3171 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3173 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3175 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3178 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3180 /* children[2] must be "i++" or "++i" */
3181 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3182 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3184 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3187 /* make sure the same variable name is used in all places */
3188 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3189 (oper
->children
[2].children
[0].a_id
!= varId
))
3192 varName
= (const char *) varId
;
3194 /* children[3], the loop body, can't be too large */
3195 bodySize
= sizeof_operation(&oper
->children
[3]);
3196 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3197 slang_info_log_print(A
->log
,
3198 "Note: 'for (%s ... )' body is too large/complex"
3205 return GL_FALSE
; /* degenerate case */
3207 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3208 slang_info_log_print(A
->log
,
3209 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3210 " many iterations to unroll",
3211 varName
, start
, varName
, end
, varName
);
3215 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3216 slang_info_log_print(A
->log
,
3217 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3218 " too much code to unroll",
3219 varName
, start
, varName
, end
, varName
);
3223 return GL_TRUE
; /* we can unroll the loop */
3228 * Unroll a for-loop.
3229 * First we determine the number of iterations to unroll.
3230 * Then for each iteration:
3231 * make a copy of the loop body
3232 * replace instances of the loop variable with the current iteration value
3233 * generate IR code for the body
3234 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3236 static slang_ir_node
*
3237 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3239 GLint start
, end
, iter
;
3240 slang_ir_node
*n
, *root
= NULL
;
3243 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3244 /* for (int i=0; ... */
3245 slang_variable
*var
;
3247 varId
= oper
->children
[0].children
[0].a_id
;
3248 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3250 start
= (GLint
) var
->initializer
->literal
[0];
3254 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3255 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3258 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3260 for (iter
= start
; iter
< end
; iter
++) {
3261 slang_operation
*body
;
3263 /* make a copy of the loop body */
3264 body
= slang_operation_new(1);
3268 if (!slang_operation_copy(body
, &oper
->children
[3]))
3271 /* in body, replace instances of 'varId' with literal 'iter' */
3273 slang_variable
*oldVar
;
3274 slang_operation
*newOper
;
3276 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3278 /* undeclared loop variable */
3279 slang_operation_delete(body
);
3283 newOper
= slang_operation_new(1);
3284 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3285 newOper
->literal_size
= 1;
3286 newOper
->literal
[0] = iter
;
3288 /* replace instances of the loop variable with newOper */
3289 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3292 /* do IR codegen for body */
3293 n
= _slang_gen_operation(A
, body
);
3297 root
= new_seq(root
, n
);
3299 slang_operation_delete(body
);
3307 * Replace 'continue' statement with 'break' inside a for-loop.
3308 * This is a recursive helper function used by _slang_gen_for_without_continue().
3311 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3313 switch (oper
->type
) {
3314 case SLANG_OPER_CONTINUE
:
3315 oper
->type
= SLANG_OPER_BREAK
;
3317 case SLANG_OPER_FOR
:
3319 case SLANG_OPER_WHILE
:
3320 /* stop upon finding a nested loop */
3326 for (i
= 0; i
< oper
->num_children
; i
++) {
3327 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3335 * Transform a for-loop so that continue statements are converted to breaks.
3336 * Then do normal IR code generation.
3340 * for (INIT; LOOPCOND; INCR) {
3351 * bool _condFlag = 1;
3352 * for (INIT; _condFlag; ) {
3353 * for ( ; _condFlag = LOOPCOND; INCR) {
3365 static slang_ir_node
*
3366 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3368 slang_operation
*top
;
3369 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3370 slang_operation
*lhs
, *rhs
;
3372 assert(oper
->type
== SLANG_OPER_FOR
);
3374 top
= slang_operation_new(1);
3375 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3376 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3377 slang_operation_add_children(top
, 2);
3379 /* declare: bool _condFlag = true */
3381 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3382 slang_generate_declaration(A
, top
->locals
, condDecl
,
3383 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3386 /* build outer loop: for (INIT; _condFlag; ) { */
3387 outerFor
= slang_oper_child(top
, 1);
3388 outerFor
->type
= SLANG_OPER_FOR
;
3389 slang_operation_add_children(outerFor
, 4);
3391 init
= slang_oper_child(outerFor
, 0);
3392 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3394 cond
= slang_oper_child(outerFor
, 1);
3395 cond
->type
= SLANG_OPER_IDENTIFIER
;
3396 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3398 incr
= slang_oper_child(outerFor
, 2);
3399 incr
->type
= SLANG_OPER_VOID
;
3401 /* body of the outer loop */
3403 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3405 slang_operation_add_children(block
, 2);
3406 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3408 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3410 innerFor
= slang_oper_child(block
, 0);
3412 /* make copy of orig loop */
3413 slang_operation_copy(innerFor
, oper
);
3414 assert(innerFor
->type
== SLANG_OPER_FOR
);
3415 innerFor
->locals
->outer_scope
= block
->locals
;
3417 init
= slang_oper_child(innerFor
, 0);
3418 init
->type
= SLANG_OPER_VOID
; /* leak? */
3420 cond
= slang_oper_child(innerFor
, 1);
3421 slang_operation_destruct(cond
);
3422 cond
->type
= SLANG_OPER_ASSIGN
;
3423 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3424 slang_operation_add_children(cond
, 2);
3426 lhs
= slang_oper_child(cond
, 0);
3427 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3428 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3430 rhs
= slang_oper_child(cond
, 1);
3431 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3434 /* if (_condFlag) INCR; */
3436 slang_operation
*ifop
= slang_oper_child(block
, 1);
3437 ifop
->type
= SLANG_OPER_IF
;
3438 slang_operation_add_children(ifop
, 2);
3440 /* re-use cond node build above */
3441 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3443 /* incr node from original for-loop operation */
3444 slang_operation_copy(slang_oper_child(ifop
, 1),
3445 slang_oper_child(oper
, 2));
3448 /* finally, replace "continue" with "break" in the inner for-loop */
3449 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3452 return _slang_gen_operation(A
, top
);
3458 * Generate IR for a for-loop. Unrolling will be done when possible.
3460 static slang_ir_node
*
3461 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3465 if (!A
->EmitContReturn
) {
3466 /* We don't want to emit CONT instructions. If this for-loop has
3467 * a continue, translate it away.
3469 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3470 return _slang_gen_for_without_continue(A
, oper
);
3474 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3476 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3481 assert(oper
->type
== SLANG_OPER_FOR
);
3483 /* conventional for-loop code generation */
3486 * init code (child[0])
3488 * BREAK if !expr (child[1])
3489 * body code (child[3])
3491 * incr code (child[2]) // XXX continue here
3493 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3494 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3495 loop
= new_loop(NULL
);
3497 /* save loop state */
3498 push_loop(A
, oper
, loop
);
3500 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3501 breakIf
= new_break_if_true(A
, cond
);
3502 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3503 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3505 loop
->Children
[0] = new_seq(breakIf
, body
);
3506 loop
->Children
[1] = incr
; /* tail code */
3508 /* restore loop state */
3511 return new_seq(init
, loop
);
3516 static slang_ir_node
*
3517 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3519 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3521 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3522 loopNode
= current_loop_ir(A
);
3524 assert(loopNode
->Opcode
== IR_LOOP
);
3526 cont
= new_node0(IR_CONT
);
3528 cont
->Parent
= loopNode
;
3529 /* insert this node at head of linked list of cont/break instructions */
3530 cont
->List
= loopNode
->List
;
3531 loopNode
->List
= cont
;
3534 n
= new_seq(incr
, cont
);
3540 * Determine if the given operation is of a specific type.
3543 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3545 if (oper
->type
== type
)
3547 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3548 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3549 oper
->num_children
== 1)
3550 return is_operation_type(&oper
->children
[0], type
);
3557 * Generate IR tree for an if/then/else conditional using high-level
3558 * IR_IF instruction.
3560 static slang_ir_node
*
3561 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3564 * eval expr (child[0])
3571 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3572 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3573 GLboolean isConst
, constTrue
;
3575 /* type-check expression */
3576 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3577 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3581 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3582 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3586 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3590 return _slang_gen_operation(A
, &oper
->children
[1]);
3593 /* if (false) ... */
3594 return _slang_gen_operation(A
, &oper
->children
[2]);
3598 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3599 cond
= new_cond(cond
);
3601 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3602 && !haveElseClause
) {
3603 /* Special case: generate a conditional break */
3604 ifBody
= new_break_if_true(A
, cond
);
3607 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3609 && current_loop_oper(A
)
3610 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3611 /* Special case: generate a conditional continue */
3612 ifBody
= new_cont_if_true(A
, cond
);
3617 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3619 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3622 ifNode
= new_if(cond
, ifBody
, elseBody
);
3629 static slang_ir_node
*
3630 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3634 assert(oper
->type
== SLANG_OPER_NOT
);
3636 /* type-check expression */
3637 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3638 slang_info_log_error(A
->log
,
3639 "scalar/boolean expression expected for '!'");
3643 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3651 static slang_ir_node
*
3652 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3654 slang_ir_node
*n1
, *n2
;
3656 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3658 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3659 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3660 slang_info_log_error(A
->log
,
3661 "scalar/boolean expressions expected for '^^'");
3665 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3668 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3671 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3676 * Generate IR node for storage of a temporary of given size.
3678 static slang_ir_node
*
3679 _slang_gen_temporary(GLint size
)
3681 slang_ir_storage
*store
;
3682 slang_ir_node
*n
= NULL
;
3684 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3686 n
= new_node0(IR_VAR_DECL
);
3699 * Generate program constants for an array.
3700 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3701 * This will allocate and initialize three vector constants, storing
3702 * the array in constant memory, not temporaries like a non-const array.
3703 * This can also be used for uniform array initializers.
3704 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3707 make_constant_array(slang_assemble_ctx
*A
,
3708 slang_variable
*var
,
3709 slang_operation
*initializer
)
3711 struct gl_program
*prog
= A
->program
;
3712 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3713 const char *varName
= (char *) var
->a_name
;
3714 const GLuint numElements
= initializer
->num_children
;
3720 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3722 size
= var
->store
->Size
;
3724 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3725 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3726 assert(initializer
->type
== SLANG_OPER_CALL
);
3727 assert(initializer
->array_constructor
);
3729 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3731 /* convert constructor params into ordinary floats */
3732 for (i
= 0; i
< numElements
; i
++) {
3733 const slang_operation
*op
= &initializer
->children
[i
];
3734 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3735 /* unsupported type for this optimization */
3739 for (j
= 0; j
< op
->literal_size
; j
++) {
3740 values
[i
* 4 + j
] = op
->literal
[j
];
3742 for ( ; j
< 4; j
++) {
3743 values
[i
* 4 + j
] = 0.0f
;
3747 /* slightly different paths for constants vs. uniforms */
3748 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3749 var
->store
->File
= PROGRAM_UNIFORM
;
3750 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3751 size
, datatype
, values
);
3754 var
->store
->File
= PROGRAM_CONSTANT
;
3755 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3758 assert(var
->store
->Size
== size
);
3768 * Generate IR node for allocating/declaring a variable (either a local or
3770 * Generally, this involves allocating an slang_ir_storage instance for the
3771 * variable, choosing a register file (temporary, constant, etc).
3772 * For ordinary variables we do not yet allocate storage though. We do that
3773 * when we find the first actual use of the variable to avoid allocating temp
3774 * regs that will never get used.
3775 * At this time, uniforms are always allocated space in this function.
3777 * \param initializer Optional initializer expression for the variable.
3779 static slang_ir_node
*
3780 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3781 slang_operation
*initializer
)
3783 const char *varName
= (const char *) var
->a_name
;
3784 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3785 slang_ir_node
*varDecl
, *n
;
3786 slang_ir_storage
*store
;
3787 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3788 gl_register_file file
;
3790 /*assert(!var->declared);*/
3791 var
->declared
= GL_TRUE
;
3793 /* determine GPU register file for simple cases */
3794 if (is_sampler_type(&var
->type
)) {
3795 file
= PROGRAM_SAMPLER
;
3797 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3798 file
= PROGRAM_UNIFORM
;
3801 file
= PROGRAM_TEMPORARY
;
3804 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3806 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3810 arrayLen
= _slang_array_length(var
);
3811 totalSize
= _slang_array_size(size
, arrayLen
);
3813 /* Allocate IR node for the declaration */
3814 varDecl
= new_node0(IR_VAR_DECL
);
3818 /* Allocate slang_ir_storage for this variable if needed.
3819 * Note that we may not actually allocate a constant or temporary register
3823 GLint index
= -7; /* TBD / unknown */
3824 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3826 return NULL
; /* out of memory */
3829 /* set the IR node's Var and Store pointers */
3831 varDecl
->Store
= var
->store
;
3836 /* if there's an initializer, generate IR for the expression */
3838 slang_ir_node
*varRef
, *init
;
3840 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3841 /* if the variable is const, the initializer must be a const
3842 * expression as well.
3845 if (!_slang_is_constant_expr(initializer
)) {
3846 slang_info_log_error(A
->log
,
3847 "initializer for %s not constant", varName
);
3853 /* IR for the variable we're initializing */
3854 varRef
= new_var(A
, var
);
3856 slang_info_log_error(A
->log
, "out of memory");
3860 /* constant-folding, etc here */
3861 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3863 /* look for simple constant-valued variables and uniforms */
3864 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3865 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3867 if (initializer
->type
== SLANG_OPER_CALL
&&
3868 initializer
->array_constructor
) {
3869 /* array initializer */
3870 if (make_constant_array(A
, var
, initializer
))
3873 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3874 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3875 /* simple float/vector initializer */
3876 if (store
->File
== PROGRAM_UNIFORM
) {
3877 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3879 totalSize
, datatype
,
3880 initializer
->literal
);
3881 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3886 store
->File
= PROGRAM_CONSTANT
;
3887 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3889 initializer
->literal
,
3891 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3898 /* IR for initializer */
3899 init
= _slang_gen_operation(A
, initializer
);
3903 /* XXX remove this when type checking is added above */
3904 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3905 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3909 /* assign RHS to LHS */
3910 n
= new_node2(IR_COPY
, varRef
, init
);
3911 n
= new_seq(varDecl
, n
);
3914 /* no initializer */
3918 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3919 /* always need to allocate storage for uniforms at this point */
3920 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3921 totalSize
, datatype
, NULL
);
3922 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3926 printf("%s var %p %s store=%p index=%d size=%d\n",
3927 __FUNCTION__
, (void *) var
, (char *) varName
,
3928 (void *) store
, store
->Index
, store
->Size
);
3936 * Generate code for a selection expression: b ? x : y
3937 * XXX In some cases we could implement a selection expression
3938 * with an LRP instruction (use the boolean as the interpolant).
3939 * Otherwise, we use an IF/ELSE/ENDIF construct.
3941 static slang_ir_node
*
3942 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3944 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3945 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3946 slang_typeinfo type0
, type1
, type2
;
3947 int size
, isBool
, isEqual
;
3949 assert(oper
->type
== SLANG_OPER_SELECT
);
3950 assert(oper
->num_children
== 3);
3952 /* type of children[0] must be boolean */
3953 slang_typeinfo_construct(&type0
);
3954 typeof_operation(A
, &oper
->children
[0], &type0
);
3955 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3956 slang_typeinfo_destruct(&type0
);
3958 slang_info_log_error(A
->log
, "selector type is not boolean");
3962 slang_typeinfo_construct(&type1
);
3963 slang_typeinfo_construct(&type2
);
3964 typeof_operation(A
, &oper
->children
[1], &type1
);
3965 typeof_operation(A
, &oper
->children
[2], &type2
);
3966 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3967 slang_typeinfo_destruct(&type1
);
3968 slang_typeinfo_destruct(&type2
);
3970 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3974 /* size of x or y's type */
3975 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3979 tmpDecl
= _slang_gen_temporary(size
);
3981 /* the condition (child 0) */
3982 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3983 cond
= new_cond(cond
);
3985 /* if-true body (child 1) */
3986 tmpVar
= new_node0(IR_VAR
);
3987 tmpVar
->Store
= tmpDecl
->Store
;
3988 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3989 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3991 /* if-false body (child 2) */
3992 tmpVar
= new_node0(IR_VAR
);
3993 tmpVar
->Store
= tmpDecl
->Store
;
3994 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3995 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3997 ifNode
= new_if(cond
, trueNode
, falseNode
);
4000 tmpVar
= new_node0(IR_VAR
);
4001 tmpVar
->Store
= tmpDecl
->Store
;
4003 tree
= new_seq(ifNode
, tmpVar
);
4004 tree
= new_seq(tmpDecl
, tree
);
4006 /*_slang_print_ir_tree(tree, 10);*/
4012 * Generate code for &&.
4014 static slang_ir_node
*
4015 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
4017 /* rewrite "a && b" as "a ? b : false" */
4018 slang_operation
*select
;
4021 select
= slang_operation_new(1);
4022 select
->type
= SLANG_OPER_SELECT
;
4023 slang_operation_add_children(select
, 3);
4025 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
4026 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
4027 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
4029 n
= _slang_gen_select(A
, select
);
4035 * Generate code for ||.
4037 static slang_ir_node
*
4038 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
4040 /* rewrite "a || b" as "a ? true : b" */
4041 slang_operation
*select
;
4044 select
= slang_operation_new(1);
4045 select
->type
= SLANG_OPER_SELECT
;
4046 slang_operation_add_children(select
, 3);
4048 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
4049 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
4050 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
4052 n
= _slang_gen_select(A
, select
);
4058 * Generate IR tree for a return statement.
4060 static slang_ir_node
*
4061 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4063 const GLboolean haveReturnValue
4064 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
4066 assert(oper
->type
== SLANG_OPER_RETURN
||
4067 oper
->type
== SLANG_OPER_RETURN_INLINED
);
4069 /* error checking */
4070 if (oper
->type
== SLANG_OPER_RETURN
) {
4071 assert(A
->CurFunction
);
4073 if (haveReturnValue
&&
4074 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
4075 slang_info_log_error(A
->log
, "illegal return expression");
4078 else if (!haveReturnValue
&&
4079 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4080 slang_info_log_error(A
->log
, "return statement requires an expression");
4085 return new_return(A
->curFuncEndLabel
);
4091 * Determine if the given operation/expression is const-valued.
4094 _slang_is_constant_expr(const slang_operation
*oper
)
4096 slang_variable
*var
;
4099 switch (oper
->type
) {
4100 case SLANG_OPER_IDENTIFIER
:
4101 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4102 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4106 for (i
= 0; i
< oper
->num_children
; i
++) {
4107 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4117 * Check if an assignment of type t1 to t0 is legal.
4118 * XXX more cases needed.
4121 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4122 slang_operation
*op0
,
4123 slang_operation
*op1
)
4125 slang_typeinfo t0
, t1
;
4128 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4129 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4133 slang_typeinfo_construct(&t0
);
4134 typeof_operation(A
, op0
, &t0
);
4136 slang_typeinfo_construct(&t1
);
4137 typeof_operation(A
, op1
, &t1
);
4139 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4140 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4144 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4149 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4150 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4151 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4154 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4155 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4158 #if 0 /* not used just yet - causes problems elsewhere */
4159 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4160 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4164 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4165 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4168 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4169 t1
.spec
.type
== SLANG_SPEC_INT
)
4177 * Generate IR tree for a local variable declaration.
4178 * Basically do some error checking and call _slang_gen_var_decl().
4180 static slang_ir_node
*
4181 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4183 const char *varName
= (char *) oper
->a_id
;
4184 slang_variable
*var
;
4185 slang_ir_node
*varDecl
;
4186 slang_operation
*initializer
;
4188 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4189 assert(oper
->num_children
<= 1);
4192 /* lookup the variable by name */
4193 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4195 return NULL
; /* "shouldn't happen" */
4197 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4198 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4199 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4200 /* can't declare attribute/uniform vars inside functions */
4201 slang_info_log_error(A
->log
,
4202 "local variable '%s' cannot be an attribute/uniform/varying",
4209 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4214 /* check if the var has an initializer */
4215 if (oper
->num_children
> 0) {
4216 assert(oper
->num_children
== 1);
4217 initializer
= &oper
->children
[0];
4219 else if (var
->initializer
) {
4220 initializer
= var
->initializer
;
4227 /* check/compare var type and initializer type */
4228 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4229 slang_info_log_error(A
->log
, "incompatible types in assignment");
4234 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4235 slang_info_log_error(A
->log
,
4236 "const-qualified variable '%s' requires initializer",
4242 /* Generate IR node */
4243 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4252 * Generate IR tree for a reference to a variable (such as in an expression).
4253 * This is different from a variable declaration.
4255 static slang_ir_node
*
4256 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4258 /* If there's a variable associated with this oper (from inlining)
4259 * use it. Otherwise, use the oper's var id.
4261 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4262 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4265 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4268 assert(var
->declared
);
4269 n
= new_var(A
, var
);
4276 * Return the number of components actually named by the swizzle.
4277 * Recall that swizzles may have undefined/don't-care values.
4280 swizzle_size(GLuint swizzle
)
4283 for (i
= 0; i
< 4; i
++) {
4284 GLuint swz
= GET_SWZ(swizzle
, i
);
4285 size
+= (swz
>= 0 && swz
<= 3);
4291 static slang_ir_node
*
4292 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4294 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4298 n
->Store
= _slang_new_ir_storage_relative(0,
4299 swizzle_size(swizzle
),
4301 n
->Store
->Swizzle
= swizzle
;
4308 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4310 while (store
->Parent
)
4311 store
= store
->Parent
;
4313 if (!(store
->File
== PROGRAM_OUTPUT
||
4314 store
->File
== PROGRAM_TEMPORARY
||
4315 (store
->File
== PROGRAM_VARYING
&&
4316 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4326 * Walk up an IR storage path to compute the final swizzle.
4327 * This is used when we find an expression such as "foo.xz.yx".
4330 root_swizzle(const slang_ir_storage
*st
)
4332 GLuint swizzle
= st
->Swizzle
;
4333 while (st
->Parent
) {
4335 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4342 * Generate IR tree for an assignment (=).
4344 static slang_ir_node
*
4345 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4347 slang_operation
*pred
= NULL
;
4348 slang_ir_node
*n
= NULL
;
4350 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4351 /* Check that var is writeable */
4353 = _slang_variable_locate(oper
->children
[0].locals
,
4354 oper
->children
[0].a_id
, GL_TRUE
);
4356 slang_info_log_error(A
->log
, "undefined variable '%s'",
4357 (char *) oper
->children
[0].a_id
);
4360 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4361 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4362 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4363 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4364 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4365 slang_info_log_error(A
->log
,
4366 "illegal assignment to read-only variable '%s'",
4367 (char *) oper
->children
[0].a_id
);
4371 /* check if we need to predicate this assignment based on __notRetFlag */
4372 if ((var
->is_global
||
4373 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4374 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4375 /* create predicate, used below */
4376 pred
= slang_operation_new(1);
4377 pred
->type
= SLANG_OPER_IDENTIFIER
;
4378 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__notRetFlag");
4379 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4383 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4384 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4385 /* Special case of: x = f(a, b)
4386 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4388 * XXX this could be even more effective if we could accomodate
4389 * cases such as "v.x = f();" - would help with typical vertex
4392 n
= _slang_gen_function_call_name(A
,
4393 (const char *) oper
->children
[1].a_id
,
4394 &oper
->children
[1], &oper
->children
[0]);
4397 slang_ir_node
*lhs
, *rhs
;
4399 /* lhs and rhs type checking */
4400 if (!_slang_assignment_compatible(A
,
4402 &oper
->children
[1])) {
4403 slang_info_log_error(A
->log
, "incompatible types in assignment");
4407 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4413 slang_info_log_error(A
->log
,
4414 "invalid left hand side for assignment");
4418 /* check that lhs is writable */
4419 if (!is_store_writable(A
, lhs
->Store
)) {
4420 slang_info_log_error(A
->log
,
4421 "illegal assignment to read-only l-value");
4425 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4427 /* convert lhs swizzle into writemask */
4428 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4429 GLuint writemask
, newSwizzle
= 0x0;
4430 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4431 /* Non-simple writemask, need to swizzle right hand side in
4432 * order to put components into the right place.
4434 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4436 n
= new_node2(IR_COPY
, lhs
, rhs
);
4444 /* predicate the assignment code on __notRetFlag */
4445 slang_ir_node
*top
, *cond
;
4447 cond
= _slang_gen_operation(A
, pred
);
4448 top
= new_if(cond
, n
, NULL
);
4456 * Generate IR tree for referencing a field in a struct (or basic vector type)
4458 static slang_ir_node
*
4459 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4463 /* type of struct */
4464 slang_typeinfo_construct(&ti
);
4465 typeof_operation(A
, &oper
->children
[0], &ti
);
4467 if (_slang_type_is_vector(ti
.spec
.type
)) {
4468 /* the field should be a swizzle */
4469 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4473 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4474 slang_info_log_error(A
->log
, "Bad swizzle");
4477 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4482 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4483 /* create new parent node with swizzle */
4485 n
= _slang_gen_swizzle(n
, swizzle
);
4488 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4489 || ti
.spec
.type
== SLANG_SPEC_INT
4490 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4491 const GLuint rows
= 1;
4495 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4496 slang_info_log_error(A
->log
, "Bad swizzle");
4498 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4502 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4503 /* create new parent node with swizzle */
4504 n
= _slang_gen_swizzle(n
, swizzle
);
4508 /* the field is a structure member (base.field) */
4509 /* oper->children[0] is the base */
4510 /* oper->a_id is the field name */
4511 slang_ir_node
*base
, *n
;
4512 slang_typeinfo field_ti
;
4513 GLint fieldSize
, fieldOffset
= -1;
4516 slang_typeinfo_construct(&field_ti
);
4517 typeof_operation(A
, oper
, &field_ti
);
4519 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4521 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4523 if (fieldSize
== 0 || fieldOffset
< 0) {
4524 const char *structName
;
4525 if (ti
.spec
._struct
)
4526 structName
= (char *) ti
.spec
._struct
->a_name
;
4528 structName
= "unknown";
4529 slang_info_log_error(A
->log
,
4530 "\"%s\" is not a member of struct \"%s\"",
4531 (char *) oper
->a_id
, structName
);
4534 assert(fieldSize
>= 0);
4536 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4538 /* error msg should have already been logged */
4542 n
= new_node1(IR_FIELD
, base
);
4546 n
->Field
= (char *) oper
->a_id
;
4548 /* Store the field's offset in storage->Index */
4549 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4559 * Gen code for array indexing.
4561 static slang_ir_node
*
4562 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4564 slang_typeinfo array_ti
;
4566 /* get array's type info */
4567 slang_typeinfo_construct(&array_ti
);
4568 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4570 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4571 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4572 /* translate the index into a swizzle/writemask: "v.x=p" */
4573 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4577 index
= (GLint
) oper
->children
[1].literal
[0];
4578 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4579 index
>= (GLint
) max
) {
4581 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4582 printf("type = %d\n", oper
->children
[1].type
);
4583 printf("index = %d, max = %d\n", index
, max
);
4584 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4585 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4592 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4594 /* use swizzle to access the element */
4595 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4599 n
= _slang_gen_swizzle(n
, swizzle
);
4605 /* conventional array */
4606 slang_typeinfo elem_ti
;
4607 slang_ir_node
*elem
, *array
, *index
;
4608 GLint elemSize
, arrayLen
;
4610 /* size of array element */
4611 slang_typeinfo_construct(&elem_ti
);
4612 typeof_operation(A
, oper
, &elem_ti
);
4613 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4615 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4616 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4618 arrayLen
= array_ti
.array_len
;
4620 slang_typeinfo_destruct(&array_ti
);
4621 slang_typeinfo_destruct(&elem_ti
);
4623 if (elemSize
<= 0) {
4624 /* unknown var or type */
4625 slang_info_log_error(A
->log
, "Undefined variable or type");
4629 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4630 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4631 if (array
&& index
) {
4633 GLint constIndex
= -1;
4634 if (index
->Opcode
== IR_FLOAT
) {
4635 constIndex
= (int) index
->Value
[0];
4636 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4637 slang_info_log_error(A
->log
,
4638 "Array index out of bounds (index=%d size=%d)",
4639 constIndex
, arrayLen
);
4640 _slang_free_ir_tree(array
);
4641 _slang_free_ir_tree(index
);
4646 if (!array
->Store
) {
4647 slang_info_log_error(A
->log
, "Invalid array");
4651 elem
= new_node2(IR_ELEMENT
, array
, index
);
4653 /* The storage info here will be updated during code emit */
4654 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4655 array
->Store
->Index
,
4657 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4661 _slang_free_ir_tree(array
);
4662 _slang_free_ir_tree(index
);
4669 static slang_ir_node
*
4670 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4671 slang_ir_opcode opcode
)
4673 slang_typeinfo t0
, t1
;
4676 slang_typeinfo_construct(&t0
);
4677 typeof_operation(A
, &oper
->children
[0], &t0
);
4679 slang_typeinfo_construct(&t1
);
4680 typeof_operation(A
, &oper
->children
[0], &t1
);
4682 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4683 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4684 slang_info_log_error(A
->log
, "Illegal array comparison");
4688 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4689 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4690 /* <, <=, >, >= can only be used with scalars */
4691 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4692 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4693 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4694 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4695 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4700 n
= new_node2(opcode
,
4701 _slang_gen_operation(A
, &oper
->children
[0]),
4702 _slang_gen_operation(A
, &oper
->children
[1]));
4704 /* result is a bool (size 1) */
4705 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4713 print_vars(slang_variable_scope
*s
)
4717 for (i
= 0; i
< s
->num_variables
; i
++) {
4719 (char*) s
->variables
[i
]->a_name
,
4720 s
->variables
[i
]->declared
);
4730 _slang_undeclare_vars(slang_variable_scope
*locals
)
4732 if (locals
->num_variables
> 0) {
4734 for (i
= 0; i
< locals
->num_variables
; i
++) {
4735 slang_variable
*v
= locals
->variables
[i
];
4736 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4737 v
->declared
= GL_FALSE
;
4745 * Generate IR tree for a slang_operation (AST node)
4747 static slang_ir_node
*
4748 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4750 switch (oper
->type
) {
4751 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4755 _slang_push_var_table(A
->vartable
);
4757 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4758 n
= _slang_gen_operation(A
, oper
);
4759 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4761 _slang_pop_var_table(A
->vartable
);
4763 /*_slang_undeclare_vars(oper->locals);*/
4764 /*print_vars(oper->locals);*/
4767 n
= new_node1(IR_SCOPE
, n
);
4772 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4773 /* list of operations */
4774 if (oper
->num_children
> 0)
4776 slang_ir_node
*n
, *tree
= NULL
;
4779 for (i
= 0; i
< oper
->num_children
; i
++) {
4780 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4782 _slang_free_ir_tree(tree
);
4783 return NULL
; /* error must have occured */
4785 tree
= new_seq(tree
, n
);
4791 return new_node0(IR_NOP
);
4794 case SLANG_OPER_EXPRESSION
:
4795 return _slang_gen_operation(A
, &oper
->children
[0]);
4797 case SLANG_OPER_FOR
:
4798 return _slang_gen_for(A
, oper
);
4800 return _slang_gen_do(A
, oper
);
4801 case SLANG_OPER_WHILE
:
4802 return _slang_gen_while(A
, oper
);
4803 case SLANG_OPER_BREAK
:
4804 if (!current_loop_oper(A
)) {
4805 slang_info_log_error(A
->log
, "'break' not in loop");
4808 return new_break(current_loop_ir(A
));
4809 case SLANG_OPER_CONTINUE
:
4810 if (!current_loop_oper(A
)) {
4811 slang_info_log_error(A
->log
, "'continue' not in loop");
4814 return _slang_gen_continue(A
, oper
);
4815 case SLANG_OPER_DISCARD
:
4816 return new_node0(IR_KILL
);
4818 case SLANG_OPER_EQUAL
:
4819 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4820 case SLANG_OPER_NOTEQUAL
:
4821 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4822 case SLANG_OPER_GREATER
:
4823 return _slang_gen_compare(A
, oper
, IR_SGT
);
4824 case SLANG_OPER_LESS
:
4825 return _slang_gen_compare(A
, oper
, IR_SLT
);
4826 case SLANG_OPER_GREATEREQUAL
:
4827 return _slang_gen_compare(A
, oper
, IR_SGE
);
4828 case SLANG_OPER_LESSEQUAL
:
4829 return _slang_gen_compare(A
, oper
, IR_SLE
);
4830 case SLANG_OPER_ADD
:
4833 assert(oper
->num_children
== 2);
4834 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4837 case SLANG_OPER_SUBTRACT
:
4840 assert(oper
->num_children
== 2);
4841 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4844 case SLANG_OPER_MULTIPLY
:
4847 assert(oper
->num_children
== 2);
4848 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4851 case SLANG_OPER_DIVIDE
:
4854 assert(oper
->num_children
== 2);
4855 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4858 case SLANG_OPER_MINUS
:
4861 assert(oper
->num_children
== 1);
4862 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4865 case SLANG_OPER_PLUS
:
4866 /* +expr --> do nothing */
4867 return _slang_gen_operation(A
, &oper
->children
[0]);
4868 case SLANG_OPER_VARIABLE_DECL
:
4869 return _slang_gen_declaration(A
, oper
);
4870 case SLANG_OPER_ASSIGN
:
4871 return _slang_gen_assignment(A
, oper
);
4872 case SLANG_OPER_ADDASSIGN
:
4875 assert(oper
->num_children
== 2);
4876 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4879 case SLANG_OPER_SUBASSIGN
:
4882 assert(oper
->num_children
== 2);
4883 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4887 case SLANG_OPER_MULASSIGN
:
4890 assert(oper
->num_children
== 2);
4891 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4894 case SLANG_OPER_DIVASSIGN
:
4897 assert(oper
->num_children
== 2);
4898 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4901 case SLANG_OPER_LOGICALAND
:
4904 assert(oper
->num_children
== 2);
4905 n
= _slang_gen_logical_and(A
, oper
);
4908 case SLANG_OPER_LOGICALOR
:
4911 assert(oper
->num_children
== 2);
4912 n
= _slang_gen_logical_or(A
, oper
);
4915 case SLANG_OPER_LOGICALXOR
:
4916 return _slang_gen_xor(A
, oper
);
4917 case SLANG_OPER_NOT
:
4918 return _slang_gen_not(A
, oper
);
4919 case SLANG_OPER_SELECT
: /* b ? x : y */
4922 assert(oper
->num_children
== 3);
4923 n
= _slang_gen_select(A
, oper
);
4927 case SLANG_OPER_ASM
:
4928 return _slang_gen_asm(A
, oper
, NULL
);
4929 case SLANG_OPER_CALL
:
4930 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4932 case SLANG_OPER_METHOD
:
4933 return _slang_gen_method_call(A
, oper
);
4934 case SLANG_OPER_RETURN
:
4935 return _slang_gen_return(A
, oper
);
4936 case SLANG_OPER_RETURN_INLINED
:
4937 return _slang_gen_return(A
, oper
);
4938 case SLANG_OPER_LABEL
:
4939 return new_label(oper
->label
);
4940 case SLANG_OPER_IDENTIFIER
:
4941 return _slang_gen_variable(A
, oper
);
4943 return _slang_gen_if(A
, oper
);
4944 case SLANG_OPER_FIELD
:
4945 return _slang_gen_struct_field(A
, oper
);
4946 case SLANG_OPER_SUBSCRIPT
:
4947 return _slang_gen_array_element(A
, oper
);
4948 case SLANG_OPER_LITERAL_FLOAT
:
4950 case SLANG_OPER_LITERAL_INT
:
4952 case SLANG_OPER_LITERAL_BOOL
:
4953 return new_float_literal(oper
->literal
, oper
->literal_size
);
4955 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4958 assert(oper
->num_children
== 1);
4959 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4962 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4965 assert(oper
->num_children
== 1);
4966 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4969 case SLANG_OPER_PREINCREMENT
: /* ++var */
4972 assert(oper
->num_children
== 1);
4973 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4976 case SLANG_OPER_PREDECREMENT
: /* --var */
4979 assert(oper
->num_children
== 1);
4980 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4984 case SLANG_OPER_NON_INLINED_CALL
:
4985 case SLANG_OPER_SEQUENCE
:
4987 slang_ir_node
*tree
= NULL
;
4989 for (i
= 0; i
< oper
->num_children
; i
++) {
4990 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4991 tree
= new_seq(tree
, n
);
4993 tree
->Store
= n
->Store
;
4995 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4996 tree
= new_function_call(tree
, oper
->label
);
5001 case SLANG_OPER_NONE
:
5002 case SLANG_OPER_VOID
:
5003 /* returning NULL here would generate an error */
5004 return new_node0(IR_NOP
);
5007 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
5009 return new_node0(IR_NOP
);
5017 * Check if the given type specifier is a rectangular texture sampler.
5020 is_rect_sampler_spec(const slang_type_specifier
*spec
)
5022 while (spec
->_array
) {
5023 spec
= spec
->_array
;
5025 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
5026 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
5032 * Called by compiler when a global variable has been parsed/compiled.
5033 * Here we examine the variable's type to determine what kind of register
5034 * storage will be used.
5036 * A uniform such as "gl_Position" will become the register specification
5037 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
5038 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
5040 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
5041 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
5042 * actual texture unit (as specified by the user calling glUniform1i()).
5045 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
5046 slang_unit_type type
)
5048 struct gl_program
*prog
= A
->program
;
5049 const char *varName
= (char *) var
->a_name
;
5050 GLboolean success
= GL_TRUE
;
5051 slang_ir_storage
*store
= NULL
;
5053 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
5054 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
5055 const GLint arrayLen
= _slang_array_length(var
);
5056 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
5057 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
5059 var
->is_global
= GL_TRUE
;
5061 /* check for sampler2D arrays */
5062 if (texIndex
== -1 && var
->type
.specifier
._array
)
5063 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
5065 if (texIndex
!= -1) {
5066 /* This is a texture sampler variable...
5067 * store->File = PROGRAM_SAMPLER
5068 * store->Index = sampler number (0..7, typically)
5069 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
5071 if (var
->initializer
) {
5072 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5075 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5076 /* disallow rect samplers */
5077 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
5078 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5082 (void) is_rect_sampler_spec
; /* silence warning */
5085 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5086 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5088 /* If we have a sampler array, then we need to allocate the
5089 * additional samplers to ensure we don't allocate them elsewhere.
5090 * We can't directly use _mesa_add_sampler() as that checks the
5091 * varName and gets a match, so we call _mesa_add_parameter()
5092 * directly and use the last sampler number from the call above.
5095 GLint a
= arrayLen
- 1;
5097 for (i
= 0; i
< a
; i
++) {
5098 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5099 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5100 varName
, 1, datatype
, &value
, NULL
, 0x0);
5104 if (dbg
) printf("SAMPLER ");
5106 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5107 /* Uniform variable */
5108 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5111 /* user-defined uniform */
5112 if (datatype
== GL_NONE
) {
5113 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5114 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5115 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5116 /* temporary work-around */
5117 GLenum datatype
= GL_FLOAT
;
5118 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5119 totalSize
, datatype
, NULL
);
5120 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5121 totalSize
, swizzle
);
5124 GLint a
= arrayLen
- 1;
5126 for (i
= 0; i
< a
; i
++) {
5127 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5128 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5129 varName
, 1, datatype
, &value
, NULL
, 0x0);
5133 /* XXX what we need to do is unroll the struct into its
5134 * basic types, creating a uniform variable for each.
5142 * Should produce uniforms:
5143 * "f.a" (GL_FLOAT_VEC3)
5144 * "f.b" (GL_FLOAT_VEC4)
5147 if (var
->initializer
) {
5148 slang_info_log_error(A
->log
,
5149 "unsupported initializer for uniform '%s'", varName
);
5154 slang_info_log_error(A
->log
,
5155 "invalid datatype for uniform variable %s",
5161 /* non-struct uniform */
5162 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5168 /* pre-defined uniform, like gl_ModelviewMatrix */
5169 /* We know it's a uniform, but don't allocate storage unless
5172 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5173 totalSize
, swizzle
);
5175 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5177 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5178 /* varyings must be float, vec or mat */
5179 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5180 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5181 slang_info_log_error(A
->log
,
5182 "varying '%s' must be float/vector/matrix",
5187 if (var
->initializer
) {
5188 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5194 /* user-defined varying */
5200 if (var
->type
.centroid
== SLANG_CENTROID
)
5201 flags
|= PROG_PARAM_BIT_CENTROID
;
5202 if (var
->type
.variant
== SLANG_INVARIANT
)
5203 flags
|= PROG_PARAM_BIT_INVARIANT
;
5205 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5207 swizzle
= _slang_var_swizzle(size
, 0);
5208 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5209 totalSize
, swizzle
);
5212 /* pre-defined varying, like gl_Color or gl_TexCoord */
5213 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5214 /* fragment program input */
5216 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5219 assert(index
< FRAG_ATTRIB_MAX
);
5220 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5224 /* vertex program output */
5225 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5226 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5228 assert(index
< VERT_RESULT_MAX
);
5229 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5230 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5233 if (dbg
) printf("V/F ");
5235 if (dbg
) printf("VARYING ");
5237 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5240 /* attributes must be float, vec or mat */
5241 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5242 slang_info_log_error(A
->log
,
5243 "attribute '%s' must be float/vector/matrix",
5249 /* user-defined vertex attribute */
5250 const GLint attr
= -1; /* unknown */
5251 swizzle
= _slang_var_swizzle(size
, 0);
5252 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5253 size
, datatype
, attr
);
5255 index
= VERT_ATTRIB_GENERIC0
+ index
;
5258 /* pre-defined vertex attrib */
5259 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5262 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5263 if (dbg
) printf("ATTRIB ");
5265 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5266 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5267 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5269 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5270 if (dbg
) printf("INPUT ");
5272 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5273 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5274 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5275 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5278 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5279 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5280 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5281 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5283 if (dbg
) printf("OUTPUT ");
5285 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5286 /* pre-defined global constant, like gl_MaxLights */
5287 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5288 if (dbg
) printf("CONST ");
5291 /* ordinary variable (may be const) */
5294 /* IR node to declare the variable */
5295 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5297 /* emit GPU instructions */
5298 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5300 _slang_free_ir_tree(n
);
5303 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5304 store
? store
->Index
: -2);
5307 var
->store
= store
; /* save var's storage info */
5309 var
->declared
= GL_TRUE
;
5316 * Produce an IR tree from a function AST (fun->body).
5317 * Then call the code emitter to convert the IR tree into gl_program
5321 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5324 GLboolean success
= GL_TRUE
;
5326 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5327 /* we only really generate code for main, all other functions get
5328 * inlined or codegen'd upon an actual call.
5331 /* do some basic error checking though */
5332 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5333 /* check that non-void functions actually return something */
5335 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5337 slang_info_log_error(A
->log
,
5338 "function \"%s\" has no return statement",
5339 (char *) fun
->header
.a_name
);
5341 "function \"%s\" has no return statement\n",
5342 (char *) fun
->header
.a_name
);
5347 return GL_TRUE
; /* not an error */
5351 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5352 slang_print_function(fun
, 1);
5355 /* should have been allocated earlier: */
5356 assert(A
->program
->Parameters
);
5357 assert(A
->program
->Varying
);
5358 assert(A
->vartable
);
5361 A
->UseReturnFlag
= GL_FALSE
;
5362 A
->CurFunction
= fun
;
5364 /* fold constant expressions, etc. */
5365 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5368 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5369 slang_print_function(fun
, 1);
5372 /* Create an end-of-function label */
5373 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5375 /* push new vartable scope */
5376 _slang_push_var_table(A
->vartable
);
5378 /* Generate IR tree for the function body code */
5379 n
= _slang_gen_operation(A
, fun
->body
);
5381 n
= new_node1(IR_SCOPE
, n
);
5383 /* pop vartable, restore previous */
5384 _slang_pop_var_table(A
->vartable
);
5387 /* XXX record error */
5391 /* append an end-of-function-label to IR tree */
5392 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5394 /*_slang_label_delete(A->curFuncEndLabel);*/
5395 A
->curFuncEndLabel
= NULL
;
5398 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5399 slang_print_function(fun
, 1);
5402 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5403 _slang_print_ir_tree(n
, 0);
5406 printf("************* End codegen function ************\n\n");
5409 if (A
->UnresolvedRefs
) {
5410 /* Can't codegen at this time.
5411 * At link time we'll concatenate all the vertex shaders and/or all
5412 * the fragment shaders and try recompiling.
5417 /* Emit program instructions */
5418 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5419 _slang_free_ir_tree(n
);
5421 /* free codegen context */
5423 _mesa_free(A->codegen);