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.
821 static slang_operation
*
822 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
825 if (oper
->type
== type
)
827 for (i
= 0; i
< oper
->num_children
; i
++) {
828 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
837 * Count the number of operations of the given time rooted at 'oper'.
840 _slang_count_node_type(const slang_operation
*oper
, slang_operation_type type
)
843 if (oper
->type
== type
) {
846 for (i
= 0; i
< oper
->num_children
; i
++) {
847 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
854 * Check if the 'return' statement found under 'oper' is a "tail return"
855 * that can be no-op'd. For example:
860 * return; // this is a no-op
863 * This is used when determining if a function can be inlined. If the
864 * 'return' is not the last statement, we can't inline the function since
865 * we still need the semantic behaviour of the 'return' but we don't want
866 * to accidentally return from the _calling_ function. We'd need to use an
867 * unconditional branch, but we don't have such a GPU instruction (not
871 _slang_is_tail_return(const slang_operation
*oper
)
873 GLuint k
= oper
->num_children
;
876 const slang_operation
*last
= &oper
->children
[k
- 1];
877 if (last
->type
== SLANG_OPER_RETURN
)
879 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
880 last
->type
== SLANG_OPER_LABEL
)
881 k
--; /* try prev child */
882 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
883 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
884 /* try sub-children */
885 return _slang_is_tail_return(last
);
895 * Generate a variable declaration opeartion.
896 * I.e.: generate AST code for "bool flag = false;"
899 slang_generate_declaration(slang_assemble_ctx
*A
,
900 slang_variable_scope
*scope
,
901 slang_operation
*decl
,
902 slang_type_specifier_type type
,
908 assert(type
== SLANG_SPEC_BOOL
||
909 type
== SLANG_SPEC_INT
);
911 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
913 var
= slang_variable_scope_grow(scope
);
915 slang_fully_specified_type_construct(&var
->type
);
917 var
->type
.specifier
.type
= type
;
918 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
919 decl
->a_id
= var
->a_name
;
920 var
->initializer
= slang_operation_new(1);
921 slang_operation_literal_bool(var
->initializer
, initValue
);
926 slang_resolve_variable(slang_operation
*oper
)
928 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
929 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
935 * Generate code for "return expr;"
936 * We return values from functions by assinging the returned value to
937 * the hidden __retVal variable which is an extra 'out' parameter we add
938 * to the function signature.
939 * This code basically converts "return expr;" into "__retVal = expr; return;"
940 * \return the new AST code.
942 static slang_operation
*
943 gen_return_expression(slang_assemble_ctx
*A
, slang_operation
*oper
)
945 slang_operation
*blockOper
;
947 blockOper
= slang_operation_new(1);
948 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
949 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
950 slang_operation_add_children(blockOper
, 2);
952 /* if EmitContReturn:
953 * if (!__returnFlag) {
954 * build: __retVal = expr;
957 * build: __retVal = expr;
960 slang_operation
*assignOper
;
962 if (A
->UseReturnFlag
) {
963 slang_operation
*ifOper
= slang_oper_child(blockOper
, 0);
964 ifOper
->type
= SLANG_OPER_IF
;
965 slang_operation_add_children(ifOper
, 3);
967 slang_operation
*cond
= slang_oper_child(ifOper
, 0);
968 cond
->type
= SLANG_OPER_IDENTIFIER
;
969 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
972 slang_operation
*elseOper
= slang_oper_child(ifOper
, 2);
973 elseOper
->type
= SLANG_OPER_VOID
;
975 assignOper
= slang_oper_child(ifOper
, 1);
978 assignOper
= slang_oper_child(blockOper
, 0);
981 assignOper
->type
= SLANG_OPER_ASSIGN
;
982 slang_operation_add_children(assignOper
, 2);
984 slang_operation
*lhs
= slang_oper_child(assignOper
, 0);
985 lhs
->type
= SLANG_OPER_IDENTIFIER
;
986 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
989 slang_operation
*rhs
= slang_oper_child(assignOper
, 1);
990 slang_operation_copy(rhs
, &oper
->children
[0]);
994 /* build: return; (with no return value) */
996 slang_operation
*returnOper
= slang_oper_child(blockOper
, 1);
997 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
998 assert(returnOper
->num_children
== 0);
1007 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1010 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
1011 GLuint substCount
, slang_variable
**substOld
,
1012 slang_operation
**substNew
, GLboolean isLHS
)
1014 switch (oper
->type
) {
1015 case SLANG_OPER_VARIABLE_DECL
:
1017 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
1018 oper
->a_id
, GL_TRUE
);
1020 if (v
->initializer
&& oper
->num_children
== 0) {
1021 /* set child of oper to copy of initializer */
1022 oper
->num_children
= 1;
1023 oper
->children
= slang_operation_new(1);
1024 slang_operation_copy(&oper
->children
[0], v
->initializer
);
1026 if (oper
->num_children
== 1) {
1027 /* the initializer */
1028 slang_substitute(A
, &oper
->children
[0], substCount
,
1029 substOld
, substNew
, GL_FALSE
);
1033 case SLANG_OPER_IDENTIFIER
:
1034 assert(oper
->num_children
== 0);
1035 if (1/**!isLHS XXX FIX */) {
1036 slang_atom id
= oper
->a_id
;
1039 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1041 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1045 /* look for a substitution */
1046 for (i
= 0; i
< substCount
; i
++) {
1047 if (v
== substOld
[i
]) {
1048 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1049 #if 0 /* DEBUG only */
1050 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1051 assert(substNew
[i
]->var
);
1052 assert(substNew
[i
]->var
->a_name
);
1053 printf("Substitute %s with %s in id node %p\n",
1054 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1058 printf("Substitute %s with %f in id node %p\n",
1059 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1063 slang_operation_copy(oper
, substNew
[i
]);
1070 case SLANG_OPER_RETURN
:
1071 /* do return replacement here too */
1072 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1073 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
1074 slang_operation
*newReturn
;
1076 /* check if function actually has a return type */
1077 assert(A
->CurFunction
);
1078 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
1079 slang_info_log_error(A
->log
, "illegal return expression");
1083 /* generate new 'return' code' */
1084 newReturn
= gen_return_expression(A
, oper
);
1086 /* do substitutions on the "__retVal = expr" sub-tree */
1087 slang_substitute(A
, slang_oper_child(newReturn
, 0),
1088 substCount
, substOld
, substNew
, GL_FALSE
);
1090 /* install new 'return' code */
1091 slang_operation_copy(oper
, newReturn
);
1092 slang_operation_destruct(newReturn
);
1095 /* check if return value was expected */
1096 assert(A
->CurFunction
);
1097 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
1098 slang_info_log_error(A
->log
, "return statement requires an expression");
1104 case SLANG_OPER_ASSIGN
:
1105 case SLANG_OPER_SUBSCRIPT
:
1107 * child[0] can't have substitutions but child[1] can.
1109 slang_substitute(A
, &oper
->children
[0],
1110 substCount
, substOld
, substNew
, GL_TRUE
);
1111 slang_substitute(A
, &oper
->children
[1],
1112 substCount
, substOld
, substNew
, GL_FALSE
);
1114 case SLANG_OPER_FIELD
:
1115 /* XXX NEW - test */
1116 slang_substitute(A
, &oper
->children
[0],
1117 substCount
, substOld
, substNew
, GL_TRUE
);
1122 for (i
= 0; i
< oper
->num_children
; i
++)
1123 slang_substitute(A
, &oper
->children
[i
],
1124 substCount
, substOld
, substNew
, GL_FALSE
);
1131 * Produce inline code for a call to an assembly instruction.
1132 * This is typically used to compile a call to a built-in function like this:
1134 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1136 * __asm vec4_lrp __retVal, a, y, x;
1141 * r = mix(p1, p2, p3);
1151 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1153 static slang_operation
*
1154 slang_inline_asm_function(slang_assemble_ctx
*A
,
1155 slang_function
*fun
, slang_operation
*oper
)
1157 const GLuint numArgs
= oper
->num_children
;
1159 slang_operation
*inlined
;
1160 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1161 slang_variable
**substOld
;
1162 slang_operation
**substNew
;
1164 ASSERT(slang_is_asm_function(fun
));
1165 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1168 printf("Inline %s as %s\n",
1169 (char*) fun->header.a_name,
1170 (char*) fun->body->children[0].a_id);
1174 * We'll substitute formal params with actual args in the asm call.
1176 substOld
= (slang_variable
**)
1177 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1178 substNew
= (slang_operation
**)
1179 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1180 for (i
= 0; i
< numArgs
; i
++) {
1181 substOld
[i
] = fun
->parameters
->variables
[i
];
1182 substNew
[i
] = oper
->children
+ i
;
1185 /* make a copy of the code to inline */
1186 inlined
= slang_operation_new(1);
1187 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1189 /* get rid of the __retVal child */
1190 inlined
->num_children
--;
1191 for (i
= 0; i
< inlined
->num_children
; i
++) {
1192 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1196 /* now do formal->actual substitutions */
1197 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1199 _slang_free(substOld
);
1200 _slang_free(substNew
);
1203 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1204 (char *) fun
->header
.a_name
);
1205 slang_print_tree(inlined
, 3);
1206 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1214 * Inline the given function call operation.
1215 * Return a new slang_operation that corresponds to the inlined code.
1217 static slang_operation
*
1218 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1219 slang_operation
*oper
, slang_operation
*returnOper
)
1226 ParamMode
*paramMode
;
1227 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1228 const GLuint numArgs
= oper
->num_children
;
1229 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1230 slang_operation
*args
= oper
->children
;
1231 slang_operation
*inlined
, *top
;
1232 slang_variable
**substOld
;
1233 slang_operation
**substNew
;
1234 GLuint substCount
, numCopyIn
, i
;
1235 slang_function
*prevFunction
;
1236 slang_variable_scope
*newScope
= NULL
;
1239 prevFunction
= A
->CurFunction
;
1240 A
->CurFunction
= fun
;
1242 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1243 assert(fun
->param_count
== totalArgs
);
1245 /* allocate temporary arrays */
1246 paramMode
= (ParamMode
*)
1247 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1248 substOld
= (slang_variable
**)
1249 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1250 substNew
= (slang_operation
**)
1251 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1254 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1255 (char *) fun
->header
.a_name
,
1256 fun
->parameters
->num_variables
, numArgs
);
1259 if (haveRetValue
&& !returnOper
) {
1260 /* Create 3-child comma sequence for inlined code:
1261 * child[0]: declare __resultTmp
1262 * child[1]: inlined function body
1263 * child[2]: __resultTmp
1265 slang_operation
*commaSeq
;
1266 slang_operation
*declOper
= NULL
;
1267 slang_variable
*resultVar
;
1269 commaSeq
= slang_operation_new(1);
1270 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1271 assert(commaSeq
->locals
);
1272 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1273 commaSeq
->num_children
= 3;
1274 commaSeq
->children
= slang_operation_new(3);
1275 /* allocate the return var */
1276 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1278 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1279 (void*)commaSeq->locals, (char *) fun->header.a_name);
1282 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1283 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1284 resultVar
->isTemp
= GL_TRUE
;
1286 /* child[0] = __resultTmp declaration */
1287 declOper
= &commaSeq
->children
[0];
1288 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1289 declOper
->a_id
= resultVar
->a_name
;
1290 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1292 /* child[1] = function body */
1293 inlined
= &commaSeq
->children
[1];
1294 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1296 /* child[2] = __resultTmp reference */
1297 returnOper
= &commaSeq
->children
[2];
1298 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1299 returnOper
->a_id
= resultVar
->a_name
;
1300 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1305 top
= inlined
= slang_operation_new(1);
1306 /* XXXX this may be inappropriate!!!! */
1307 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1311 assert(inlined
->locals
);
1313 /* Examine the parameters, look for inout/out params, look for possible
1314 * substitutions, etc:
1315 * param type behaviour
1316 * in copy actual to local
1317 * const in substitute param with actual
1321 for (i
= 0; i
< totalArgs
; i
++) {
1322 slang_variable
*p
= fun
->parameters
->variables
[i
];
1324 printf("Param %d: %s %s \n", i,
1325 slang_type_qual_string(p->type.qualifier),
1326 (char *) p->a_name);
1328 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1329 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1330 /* an output param */
1331 slang_operation
*arg
;
1336 paramMode
[i
] = SUBST
;
1338 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1339 slang_resolve_variable(arg
);
1341 /* replace parameter 'p' with argument 'arg' */
1342 substOld
[substCount
] = p
;
1343 substNew
[substCount
] = arg
; /* will get copied */
1346 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1347 /* a constant input param */
1348 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1349 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1350 /* replace all occurances of this parameter variable with the
1351 * actual argument variable or a literal.
1353 paramMode
[i
] = SUBST
;
1354 slang_resolve_variable(&args
[i
]);
1355 substOld
[substCount
] = p
;
1356 substNew
[substCount
] = &args
[i
]; /* will get copied */
1360 paramMode
[i
] = COPY_IN
;
1364 paramMode
[i
] = COPY_IN
;
1366 assert(paramMode
[i
]);
1369 /* actual code inlining: */
1370 slang_operation_copy(inlined
, fun
->body
);
1372 /*** XXX review this */
1373 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1374 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1375 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1378 printf("======================= orig body code ======================\n");
1379 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1380 slang_print_tree(fun
->body
, 8);
1381 printf("======================= copied code =========================\n");
1382 slang_print_tree(inlined
, 8);
1385 /* do parameter substitution in inlined code: */
1386 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1389 printf("======================= subst code ==========================\n");
1390 slang_print_tree(inlined
, 8);
1391 printf("=============================================================\n");
1394 /* New prolog statements: (inserted before the inlined code)
1395 * Copy the 'in' arguments.
1398 for (i
= 0; i
< numArgs
; i
++) {
1399 if (paramMode
[i
] == COPY_IN
) {
1400 slang_variable
*p
= fun
->parameters
->variables
[i
];
1401 /* declare parameter 'p' */
1402 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1406 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1407 assert(decl
->locals
);
1408 decl
->locals
->outer_scope
= inlined
->locals
;
1409 decl
->a_id
= p
->a_name
;
1410 decl
->num_children
= 1;
1411 decl
->children
= slang_operation_new(1);
1413 /* child[0] is the var's initializer */
1414 slang_operation_copy(&decl
->children
[0], args
+ i
);
1416 /* add parameter 'p' to the local variable scope here */
1418 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1419 pCopy
->type
= p
->type
;
1420 pCopy
->a_name
= p
->a_name
;
1421 pCopy
->array_len
= p
->array_len
;
1424 newScope
= inlined
->locals
;
1429 /* Now add copies of the function's local vars to the new variable scope */
1430 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1431 slang_variable
*p
= fun
->parameters
->variables
[i
];
1432 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1433 pCopy
->type
= p
->type
;
1434 pCopy
->a_name
= p
->a_name
;
1435 pCopy
->array_len
= p
->array_len
;
1439 /* New epilog statements:
1440 * 1. Create end of function label to jump to from return statements.
1441 * 2. Copy the 'out' parameter vars
1444 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1446 inlined
->num_children
);
1447 lab
->type
= SLANG_OPER_LABEL
;
1448 lab
->label
= A
->curFuncEndLabel
;
1451 for (i
= 0; i
< totalArgs
; i
++) {
1452 if (paramMode
[i
] == COPY_OUT
) {
1453 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1454 /* actualCallVar = outParam */
1455 /*if (i > 0 || !haveRetValue)*/
1456 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1458 inlined
->num_children
);
1459 ass
->type
= SLANG_OPER_ASSIGN
;
1460 ass
->num_children
= 2;
1461 ass
->locals
->outer_scope
= inlined
->locals
;
1462 ass
->children
= slang_operation_new(2);
1463 ass
->children
[0] = args
[i
]; /*XXX copy */
1464 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1465 ass
->children
[1].a_id
= p
->a_name
;
1466 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1470 _slang_free(paramMode
);
1471 _slang_free(substOld
);
1472 _slang_free(substNew
);
1474 /* Update scoping to use the new local vars instead of the
1475 * original function's vars. This is especially important
1476 * for nested inlining.
1479 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1482 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1483 (char *) fun
->header
.a_name
,
1484 fun
->parameters
->num_variables
, numArgs
);
1485 slang_print_tree(top
, 0);
1489 A
->CurFunction
= prevFunction
;
1496 * Insert declaration for "bool __returnFlag" in given block operation.
1497 * This is used when we can't emit "early" return statements in subroutines.
1500 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1502 slang_operation
*decl
;
1504 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1505 oper
->type
== SLANG_OPER_SEQUENCE
);
1507 decl
= slang_operation_insert_child(oper
, 1);
1509 slang_generate_declaration(A
, oper
->locals
, decl
,
1510 SLANG_SPEC_BOOL
, "__returnFlag", GL_TRUE
);
1512 /*slang_print_tree(oper, 0);*/
1517 * Replace 'return' with '__returnFlag = false'.
1518 * This is used to remove 'early returns' from functions.
1521 replace_return_with_flag_set(slang_assemble_ctx
*A
, slang_operation
*oper
)
1523 slang_atom id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
1524 assert(oper
->type
== SLANG_OPER_RETURN
);
1526 /* replace 'return' with __returnFlag = false' */
1527 slang_operation_free_children(oper
);
1528 oper
->type
= SLANG_OPER_ASSIGN
;
1529 slang_operation_add_children(oper
, 2);
1531 slang_operation
*lhs
= slang_oper_child(oper
, 0);
1532 lhs
->type
= SLANG_OPER_IDENTIFIER
;
1536 slang_operation
*rhs
= slang_oper_child(oper
, 1);
1537 slang_operation_literal_bool(rhs
, GL_FALSE
);
1541 slang_variable
*var
;
1542 var
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1549 * Test if the given function body has an "early return". That is, there's
1550 * a 'return' statement that's not the very last instruction in the body.
1553 has_early_return(const slang_operation
*funcBody
)
1555 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1558 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1566 * Emit IR code for a function call. This does one of two things:
1567 * 1. Inline the function's code
1568 * 2. Create an IR for the function's body and create a real call to it.
1570 static slang_ir_node
*
1571 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1572 slang_operation
*oper
, slang_operation
*dest
)
1575 slang_operation
*inlined
;
1576 slang_label
*prevFuncEndLabel
;
1579 prevFuncEndLabel
= A
->curFuncEndLabel
;
1580 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1581 A
->curFuncEndLabel
= _slang_label_new(name
);
1582 assert(A
->curFuncEndLabel
);
1584 if (slang_is_asm_function(fun
) && !dest
) {
1585 /* assemble assembly function - tree style */
1586 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1589 /* non-assembly function */
1590 /* We always generate an "inline-able" block of code here.
1592 * 1. insert the inline code
1593 * 2. Generate a call to the "inline" code as a subroutine
1595 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1596 slang_operation
*ret
= NULL
;
1598 if (earlyReturn
&& !A
->EmitContReturn
) {
1599 A
->UseReturnFlag
= GL_TRUE
;
1602 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1606 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1608 /* check if this is a "tail" return */
1610 /* The only RETURN is the last stmt in the function, no-op it
1611 * and inline the function body.
1613 ret
->type
= SLANG_OPER_NONE
;
1616 slang_operation
*callOper
;
1617 /* The function we're calling has one or more 'return' statements.
1618 * So, we can't truly inline this function because we need to
1619 * implement 'return' with RET (and CAL).
1620 * Nevertheless, we performed "inlining" to make a new instance
1621 * of the function body to deal with static register allocation.
1623 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1624 inlined
->type
== SLANG_OPER_SEQUENCE
);
1626 if (_slang_function_has_return_value(fun
) && !dest
) {
1627 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1628 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1629 callOper
= &inlined
->children
[1];
1635 if (A
->UseReturnFlag
) {
1636 /* Early returns not supported. Create a _returnFlag variable
1637 * that's set upon 'return' and tested elsewhere to no-op any
1638 * remaining instructions in the subroutine.
1640 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1641 callOper
->type
== SLANG_OPER_SEQUENCE
);
1642 declare_return_flag(A
, callOper
);
1644 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1645 callOper
->fun
= fun
;
1646 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1654 /* Replace the function call with the inlined block (or new CALL stmt) */
1655 slang_operation_destruct(oper
);
1657 _slang_free(inlined
);
1660 assert(inlined
->locals
);
1661 printf("*** Inlined code for call to %s:\n",
1662 (char*) fun
->header
.a_name
);
1663 slang_print_tree(oper
, 10);
1667 n
= _slang_gen_operation(A
, oper
);
1669 /*_slang_label_delete(A->curFuncEndLabel);*/
1670 A
->curFuncEndLabel
= prevFuncEndLabel
;
1672 if (A
->pragmas
->Debug
) {
1674 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1675 n
->Comment
= _slang_strdup(s
);
1682 static slang_asm_info
*
1683 slang_find_asm_info(const char *name
)
1686 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1687 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1696 * Some write-masked assignments are simple, but others are hard.
1699 * v.xy = vec2(a, b);
1702 * v.zy = vec2(a, b);
1703 * this gets transformed/swizzled into:
1704 * v.zy = vec2(a, b).*yx* (* = don't care)
1705 * This function helps to determine simple vs. non-simple.
1708 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1710 switch (writemask
) {
1712 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1714 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1716 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1718 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1720 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1721 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1723 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1724 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1725 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1726 case WRITEMASK_XYZW
:
1727 return swizzle
== SWIZZLE_NOOP
;
1735 * Convert the given swizzle into a writemask. In some cases this
1736 * is trivial, in other cases, we'll need to also swizzle the right
1737 * hand side to put components in the right places.
1738 * See comment above for more info.
1739 * XXX this function could be simplified and should probably be renamed.
1740 * \param swizzle the incoming swizzle
1741 * \param writemaskOut returns the writemask
1742 * \param swizzleOut swizzle to apply to the right-hand-side
1743 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1746 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1747 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1749 GLuint mask
= 0x0, newSwizzle
[4];
1752 /* make new dst writemask, compute size */
1753 for (i
= 0; i
< 4; i
++) {
1754 const GLuint swz
= GET_SWZ(swizzle
, i
);
1755 if (swz
== SWIZZLE_NIL
) {
1759 assert(swz
>= 0 && swz
<= 3);
1761 if (swizzle
!= SWIZZLE_XXXX
&&
1762 swizzle
!= SWIZZLE_YYYY
&&
1763 swizzle
!= SWIZZLE_ZZZZ
&&
1764 swizzle
!= SWIZZLE_WWWW
&&
1765 (mask
& (1 << swz
))) {
1766 /* a channel can't be specified twice (ex: ".xyyz") */
1767 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1768 _mesa_swizzle_string(swizzle
, 0, 0));
1774 assert(mask
<= 0xf);
1775 size
= i
; /* number of components in mask/swizzle */
1777 *writemaskOut
= mask
;
1779 /* make new src swizzle, by inversion */
1780 for (i
= 0; i
< 4; i
++) {
1781 newSwizzle
[i
] = i
; /*identity*/
1783 for (i
= 0; i
< size
; i
++) {
1784 const GLuint swz
= GET_SWZ(swizzle
, i
);
1785 newSwizzle
[swz
] = i
;
1787 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1792 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1794 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1796 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1798 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1800 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1808 #if 0 /* not used, but don't remove just yet */
1810 * Recursively traverse 'oper' to produce a swizzle mask in the event
1811 * of any vector subscripts and swizzle suffixes.
1812 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1815 resolve_swizzle(const slang_operation
*oper
)
1817 if (oper
->type
== SLANG_OPER_FIELD
) {
1818 /* writemask from .xyzw suffix */
1820 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1821 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1825 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1826 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1830 return SWIZZLE_XYZW
;
1832 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1833 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1834 /* writemask from [index] */
1835 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1836 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1841 swizzle
= SWIZZLE_XXXX
;
1844 swizzle
= SWIZZLE_YYYY
;
1847 swizzle
= SWIZZLE_ZZZZ
;
1850 swizzle
= SWIZZLE_WWWW
;
1853 swizzle
= SWIZZLE_XYZW
;
1855 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1859 return SWIZZLE_XYZW
;
1867 * Recursively descend through swizzle nodes to find the node's storage info.
1869 static slang_ir_storage
*
1870 get_store(const slang_ir_node
*n
)
1872 if (n
->Opcode
== IR_SWIZZLE
) {
1873 return get_store(n
->Children
[0]);
1881 * Generate IR tree for an asm instruction/operation such as:
1882 * __asm vec4_dot __retVal.x, v1, v2;
1884 static slang_ir_node
*
1885 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1886 slang_operation
*dest
)
1888 const slang_asm_info
*info
;
1889 slang_ir_node
*kids
[3], *n
;
1890 GLuint j
, firstOperand
;
1892 assert(oper
->type
== SLANG_OPER_ASM
);
1894 info
= slang_find_asm_info((char *) oper
->a_id
);
1896 _mesa_problem(NULL
, "undefined __asm function %s\n",
1897 (char *) oper
->a_id
);
1900 assert(info
->NumParams
<= 3);
1902 if (info
->NumParams
== oper
->num_children
) {
1903 /* Storage for result is not specified.
1904 * Children[0], [1], [2] are the operands.
1909 /* Storage for result (child[0]) is specified.
1910 * Children[1], [2], [3] are the operands.
1915 /* assemble child(ren) */
1916 kids
[0] = kids
[1] = kids
[2] = NULL
;
1917 for (j
= 0; j
< info
->NumParams
; j
++) {
1918 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1923 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1926 /* Setup n->Store to be a particular location. Otherwise, storage
1927 * for the result (a temporary) will be allocated later.
1929 slang_operation
*dest_oper
;
1932 dest_oper
= &oper
->children
[0];
1934 n0
= _slang_gen_operation(A
, dest_oper
);
1939 n
->Store
= n0
->Store
;
1941 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1952 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1955 for (i
= 0; i
< scope
->num_functions
; i
++) {
1956 slang_function
*f
= &scope
->functions
[i
];
1957 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1958 printf(" %s (%d args)\n", name
, f
->param_count
);
1961 if (scope
->outer_scope
)
1962 print_funcs(scope
->outer_scope
, name
);
1968 * Find a function of the given name, taking 'numArgs' arguments.
1969 * This is the function we'll try to call when there is no exact match
1970 * between function parameters and call arguments.
1972 * XXX we should really create a list of candidate functions and try
1975 static slang_function
*
1976 _slang_find_function_by_argc(slang_function_scope
*scope
,
1977 const char *name
, int numArgs
)
1981 for (i
= 0; i
< scope
->num_functions
; i
++) {
1982 slang_function
*f
= &scope
->functions
[i
];
1983 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1984 int haveRetValue
= _slang_function_has_return_value(f
);
1985 if (numArgs
== f
->param_count
- haveRetValue
)
1989 scope
= scope
->outer_scope
;
1996 static slang_function
*
1997 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
2000 slang_function
*maxFunc
= NULL
;
2005 for (i
= 0; i
< scope
->num_functions
; i
++) {
2006 slang_function
*f
= &scope
->functions
[i
];
2007 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2008 if (f
->param_count
> maxArgs
) {
2009 maxArgs
= f
->param_count
;
2014 scope
= scope
->outer_scope
;
2022 * Generate a new slang_function which is a constructor for a user-defined
2025 static slang_function
*
2026 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2028 const GLint numFields
= str
->fields
->num_variables
;
2029 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2031 /* function header (name, return type) */
2032 fun
->header
.a_name
= str
->a_name
;
2033 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2034 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2035 fun
->header
.type
.specifier
._struct
= str
;
2037 /* function parameters (= struct's fields) */
2040 for (i
= 0; i
< numFields
; i
++) {
2042 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2044 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2045 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2046 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2048 fun
->param_count
= fun
->parameters
->num_variables
;
2051 /* Add __retVal to params */
2053 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2054 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2056 p
->a_name
= a_retVal
;
2057 p
->type
= fun
->header
.type
;
2058 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2062 /* function body is:
2072 slang_variable_scope
*scope
;
2073 slang_variable
*var
;
2076 fun
->body
= slang_operation_new(1);
2077 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2078 fun
->body
->num_children
= numFields
+ 2;
2079 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2081 scope
= fun
->body
->locals
;
2082 scope
->outer_scope
= fun
->parameters
;
2084 /* create local var 't' */
2085 var
= slang_variable_scope_grow(scope
);
2086 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2087 var
->type
= fun
->header
.type
;
2091 slang_operation
*decl
;
2093 decl
= &fun
->body
->children
[0];
2094 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2095 decl
->locals
= _slang_variable_scope_new(scope
);
2096 decl
->a_id
= var
->a_name
;
2099 /* assign params to fields of t */
2100 for (i
= 0; i
< numFields
; i
++) {
2101 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2103 assign
->type
= SLANG_OPER_ASSIGN
;
2104 assign
->locals
= _slang_variable_scope_new(scope
);
2105 assign
->num_children
= 2;
2106 assign
->children
= slang_operation_new(2);
2109 slang_operation
*lhs
= &assign
->children
[0];
2111 lhs
->type
= SLANG_OPER_FIELD
;
2112 lhs
->locals
= _slang_variable_scope_new(scope
);
2113 lhs
->num_children
= 1;
2114 lhs
->children
= slang_operation_new(1);
2115 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2117 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2118 lhs
->children
[0].a_id
= var
->a_name
;
2119 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2122 lhs
->children
[1].num_children
= 1;
2123 lhs
->children
[1].children
= slang_operation_new(1);
2124 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2125 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2126 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2131 slang_operation
*rhs
= &assign
->children
[1];
2133 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2134 rhs
->locals
= _slang_variable_scope_new(scope
);
2135 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2141 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2143 ret
->type
= SLANG_OPER_RETURN
;
2144 ret
->locals
= _slang_variable_scope_new(scope
);
2145 ret
->num_children
= 1;
2146 ret
->children
= slang_operation_new(1);
2147 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2148 ret
->children
[0].a_id
= var
->a_name
;
2149 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2153 slang_print_function(fun, 1);
2160 * Find/create a function (constructor) for the given structure name.
2162 static slang_function
*
2163 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2166 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2167 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2168 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2169 /* found a structure type that matches the function name */
2170 if (!str
->constructor
) {
2171 /* create the constructor function now */
2172 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2174 return str
->constructor
;
2182 * Generate a new slang_function to satisfy a call to an array constructor.
2183 * Ex: float[3](1., 2., 3.)
2185 static slang_function
*
2186 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2188 slang_type_specifier_type baseType
;
2189 slang_function
*fun
;
2192 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2196 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2198 num_elements
= oper
->num_children
;
2200 /* function header, return type */
2202 fun
->header
.a_name
= oper
->a_id
;
2203 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2204 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2205 fun
->header
.type
.specifier
._array
=
2206 slang_type_specifier_new(baseType
, NULL
, NULL
);
2207 fun
->header
.type
.array_len
= num_elements
;
2210 /* function parameters (= number of elements) */
2213 for (i
= 0; i
< num_elements
; i
++) {
2215 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2217 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2219 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2220 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2221 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2222 p
->type
.specifier
.type
= baseType
;
2224 fun
->param_count
= fun
->parameters
->num_variables
;
2227 /* Add __retVal to params */
2229 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2230 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2232 p
->a_name
= a_retVal
;
2233 p
->type
= fun
->header
.type
;
2234 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2235 p
->type
.specifier
.type
= baseType
;
2239 /* function body is:
2249 slang_variable_scope
*scope
;
2250 slang_variable
*var
;
2253 fun
->body
= slang_operation_new(1);
2254 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2255 fun
->body
->num_children
= num_elements
+ 2;
2256 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2258 scope
= fun
->body
->locals
;
2259 scope
->outer_scope
= fun
->parameters
;
2261 /* create local var 't' */
2262 var
= slang_variable_scope_grow(scope
);
2263 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2264 var
->type
= fun
->header
.type
;/*XXX copy*/
2268 slang_operation
*decl
;
2270 decl
= &fun
->body
->children
[0];
2271 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2272 decl
->locals
= _slang_variable_scope_new(scope
);
2273 decl
->a_id
= var
->a_name
;
2276 /* assign params to elements of t */
2277 for (i
= 0; i
< num_elements
; i
++) {
2278 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2280 assign
->type
= SLANG_OPER_ASSIGN
;
2281 assign
->locals
= _slang_variable_scope_new(scope
);
2282 assign
->num_children
= 2;
2283 assign
->children
= slang_operation_new(2);
2286 slang_operation
*lhs
= &assign
->children
[0];
2288 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2289 lhs
->locals
= _slang_variable_scope_new(scope
);
2290 lhs
->num_children
= 2;
2291 lhs
->children
= slang_operation_new(2);
2293 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2294 lhs
->children
[0].a_id
= var
->a_name
;
2295 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2297 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2298 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2302 slang_operation
*rhs
= &assign
->children
[1];
2304 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2305 rhs
->locals
= _slang_variable_scope_new(scope
);
2306 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2312 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2314 ret
->type
= SLANG_OPER_RETURN
;
2315 ret
->locals
= _slang_variable_scope_new(scope
);
2316 ret
->num_children
= 1;
2317 ret
->children
= slang_operation_new(1);
2318 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2319 ret
->children
[0].a_id
= var
->a_name
;
2320 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2325 slang_print_function(fun, 1);
2333 _slang_is_vec_mat_type(const char *name
)
2335 static const char *vecmat_types
[] = {
2336 "float", "int", "bool",
2337 "vec2", "vec3", "vec4",
2338 "ivec2", "ivec3", "ivec4",
2339 "bvec2", "bvec3", "bvec4",
2340 "mat2", "mat3", "mat4",
2341 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2345 for (i
= 0; vecmat_types
[i
]; i
++)
2346 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2353 * Assemble a function call, given a particular function name.
2354 * \param name the function's name (operators like '*' are possible).
2356 static slang_ir_node
*
2357 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2358 slang_operation
*oper
, slang_operation
*dest
)
2360 slang_operation
*params
= oper
->children
;
2361 const GLuint param_count
= oper
->num_children
;
2363 slang_function
*fun
;
2366 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2367 if (atom
== SLANG_ATOM_NULL
)
2370 if (oper
->array_constructor
) {
2371 /* this needs special handling */
2372 fun
= _slang_make_array_constructor(A
, oper
);
2375 /* Try to find function by name and exact argument type matching */
2376 GLboolean error
= GL_FALSE
;
2377 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2378 &A
->space
, A
->atoms
, A
->log
, &error
);
2380 slang_info_log_error(A
->log
,
2381 "Function '%s' not found (check argument types)",
2388 /* Next, try locating a constructor function for a user-defined type */
2389 fun
= _slang_locate_struct_constructor(A
, name
);
2393 * At this point, some heuristics are used to try to find a function
2394 * that matches the calling signature by means of casting or "unrolling"
2398 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2399 /* Next, if this call looks like a vec() or mat() constructor call,
2400 * try "unwinding" the args to satisfy a constructor.
2402 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2404 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2405 slang_info_log_error(A
->log
,
2406 "Function '%s' not found (check argument types)",
2413 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2414 /* Next, try casting args to the types of the formal parameters */
2415 int numArgs
= oper
->num_children
;
2416 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2417 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2418 slang_info_log_error(A
->log
,
2419 "Function '%s' not found (check argument types)",
2427 slang_info_log_error(A
->log
,
2428 "Function '%s' not found (check argument types)",
2434 /* The function body may be in another compilation unit.
2435 * We'll try concatenating the shaders and recompile at link time.
2437 A
->UnresolvedRefs
= GL_TRUE
;
2438 return new_node1(IR_NOP
, NULL
);
2441 /* type checking to be sure function's return type matches 'dest' type */
2445 slang_typeinfo_construct(&t0
);
2446 typeof_operation(A
, dest
, &t0
);
2448 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2449 slang_info_log_error(A
->log
,
2450 "Incompatible type returned by call to '%s'",
2456 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2458 if (n
&& !n
->Store
&& !dest
2459 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2460 /* setup n->Store for the result of the function call */
2461 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2462 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2463 /*printf("Alloc storage for function result, size %d \n", size);*/
2466 if (oper
->array_constructor
) {
2467 /* free the temporary array constructor function now */
2468 slang_function_destruct(fun
);
2475 static slang_ir_node
*
2476 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2478 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2480 slang_variable
*var
;
2482 /* NOTE: In GLSL 1.20, there's only one kind of method
2483 * call: array.length(). Anything else is an error.
2485 if (oper
->a_id
!= a_length
) {
2486 slang_info_log_error(A
->log
,
2487 "Undefined method call '%s'", (char *) oper
->a_id
);
2491 /* length() takes no arguments */
2492 if (oper
->num_children
> 0) {
2493 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2497 /* lookup the object/variable */
2498 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2499 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2500 slang_info_log_error(A
->log
,
2501 "Undefined object '%s'", (char *) oper
->a_obj
);
2505 /* Create a float/literal IR node encoding the array length */
2506 n
= new_node0(IR_FLOAT
);
2508 n
->Value
[0] = (float) _slang_array_length(var
);
2509 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2516 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2518 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2519 oper
->type
== SLANG_OPER_LITERAL_INT
||
2520 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2521 if (oper
->literal
[0])
2527 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2528 oper
->num_children
== 1) {
2529 return _slang_is_constant_cond(&oper
->children
[0], value
);
2536 * Test if an operation is a scalar or boolean.
2539 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2541 slang_typeinfo type
;
2544 slang_typeinfo_construct(&type
);
2545 typeof_operation(A
, oper
, &type
);
2546 size
= _slang_sizeof_type_specifier(&type
.spec
);
2547 slang_typeinfo_destruct(&type
);
2553 * Test if an operation is boolean.
2556 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2558 slang_typeinfo type
;
2561 slang_typeinfo_construct(&type
);
2562 typeof_operation(A
, oper
, &type
);
2563 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2564 slang_typeinfo_destruct(&type
);
2570 * Check if a loop contains a 'continue' statement.
2571 * Stop looking if we find a nested loop.
2574 _slang_loop_contains_continue(const slang_operation
*oper
)
2576 switch (oper
->type
) {
2577 case SLANG_OPER_CONTINUE
:
2579 case SLANG_OPER_FOR
:
2581 case SLANG_OPER_WHILE
:
2582 /* stop upon finding a nested loop */
2588 for (i
= 0; i
< oper
->num_children
; i
++) {
2589 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2590 if (_slang_loop_contains_continue(child
))
2600 * Check if a loop contains a 'continue' or 'break' statement.
2601 * Stop looking if we find a nested loop.
2604 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2606 switch (oper
->type
) {
2607 case SLANG_OPER_CONTINUE
:
2608 case SLANG_OPER_BREAK
:
2610 case SLANG_OPER_FOR
:
2612 case SLANG_OPER_WHILE
:
2613 /* stop upon finding a nested loop */
2619 for (i
= 0; i
< oper
->num_children
; i
++) {
2620 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2621 if (_slang_loop_contains_continue_or_break(child
))
2631 * Replace 'break' and 'continue' statements inside a do and while loops.
2632 * This is a recursive helper function used by
2633 * _slang_gen_do/while_without_continue().
2636 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2638 switch (oper
->type
) {
2639 case SLANG_OPER_BREAK
:
2640 /* replace 'break' with "_notBreakFlag = false; break" */
2642 slang_operation
*block
= oper
;
2643 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2644 slang_operation_add_children(block
, 2);
2646 slang_operation
*assign
= slang_oper_child(block
, 0);
2647 assign
->type
= SLANG_OPER_ASSIGN
;
2648 slang_operation_add_children(assign
, 2);
2650 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2651 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2654 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2655 slang_operation_literal_bool(rhs
, GL_FALSE
);
2659 slang_operation
*brk
= slang_oper_child(block
, 1);
2660 brk
->type
= SLANG_OPER_BREAK
;
2661 assert(!brk
->children
);
2665 case SLANG_OPER_CONTINUE
:
2666 /* convert continue into a break */
2667 oper
->type
= SLANG_OPER_BREAK
;
2669 case SLANG_OPER_FOR
:
2671 case SLANG_OPER_WHILE
:
2672 /* stop upon finding a nested loop */
2678 for (i
= 0; i
< oper
->num_children
; i
++) {
2679 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2687 * Transform a while-loop so that continue statements are converted to breaks.
2688 * Then do normal IR code generation.
2692 * while (LOOPCOND) {
2704 * bool _notBreakFlag = 1;
2705 * while (_notBreakFlag && LOOPCOND) {
2709 * break; // was continue
2712 * _notBreakFlag = 0; // was
2719 static slang_ir_node
*
2720 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2722 slang_operation
*top
;
2723 slang_operation
*innerBody
;
2725 assert(oper
->type
== SLANG_OPER_WHILE
);
2727 top
= slang_operation_new(1);
2728 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2729 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2730 slang_operation_add_children(top
, 2);
2732 /* declare: bool _notBreakFlag = true */
2734 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2735 slang_generate_declaration(A
, top
->locals
, condDecl
,
2736 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2739 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2741 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2742 outerWhile
->type
= SLANG_OPER_WHILE
;
2743 slang_operation_add_children(outerWhile
, 2);
2745 /* _notBreakFlag && LOOPCOND */
2747 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2748 cond
->type
= SLANG_OPER_LOGICALAND
;
2749 slang_operation_add_children(cond
, 2);
2751 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2752 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2755 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2756 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2762 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2763 innerDo
->type
= SLANG_OPER_DO
;
2764 slang_operation_add_children(innerDo
, 2);
2766 /* copy original do-loop body into inner do-loop's body */
2767 innerBody
= slang_oper_child(innerDo
, 0);
2768 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2769 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2771 /* inner do-loop's condition is constant/false */
2773 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2774 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2779 /* Finally, in innerBody,
2780 * replace "break" with "_notBreakFlag = 0; break"
2781 * replace "continue" with "break"
2783 replace_break_and_cont(A
, innerBody
);
2785 /*slang_print_tree(top, 0);*/
2787 return _slang_gen_operation(A
, top
);
2794 * Generate loop code using high-level IR_LOOP instruction
2796 static slang_ir_node
*
2797 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2801 * BREAK if !expr (child[0])
2802 * body code (child[1])
2804 slang_ir_node
*loop
, *breakIf
, *body
;
2805 GLboolean isConst
, constTrue
;
2807 if (!A
->EmitContReturn
) {
2808 /* We don't want to emit CONT instructions. If this while-loop has
2809 * a continue, translate it away.
2811 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2812 return _slang_gen_while_without_continue(A
, oper
);
2816 /* type-check expression */
2817 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2818 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2822 /* Check if loop condition is a constant */
2823 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2825 if (isConst
&& !constTrue
) {
2826 /* loop is never executed! */
2827 return new_node0(IR_NOP
);
2830 /* Begin new loop */
2831 loop
= new_loop(NULL
);
2833 /* save loop state */
2834 push_loop(A
, oper
, loop
);
2836 if (isConst
&& constTrue
) {
2837 /* while(nonzero constant), no conditional break */
2842 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2843 breakIf
= new_break_if_true(A
, cond
);
2845 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2846 loop
->Children
[0] = new_seq(breakIf
, body
);
2848 /* Do infinite loop detection */
2849 /* loop->List is head of linked list of break/continue nodes */
2850 if (!loop
->List
&& isConst
&& constTrue
) {
2851 /* infinite loop detected */
2853 slang_info_log_error(A
->log
, "Infinite loop detected!");
2857 /* restore loop state */
2865 * Transform a do-while-loop so that continue statements are converted to breaks.
2866 * Then do normal IR code generation.
2877 * } while (LOOPCOND);
2882 * bool _notBreakFlag = 1;
2887 * break; // was continue
2890 * _notBreakFlag = 0; // was
2894 * } while (_notBreakFlag && LOOPCOND);
2897 static slang_ir_node
*
2898 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2900 slang_operation
*top
;
2901 slang_operation
*innerBody
;
2903 assert(oper
->type
== SLANG_OPER_DO
);
2905 top
= slang_operation_new(1);
2906 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2907 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2908 slang_operation_add_children(top
, 2);
2910 /* declare: bool _notBreakFlag = true */
2912 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2913 slang_generate_declaration(A
, top
->locals
, condDecl
,
2914 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2917 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2919 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2920 outerDo
->type
= SLANG_OPER_DO
;
2921 slang_operation_add_children(outerDo
, 2);
2925 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2926 innerDo
->type
= SLANG_OPER_DO
;
2927 slang_operation_add_children(innerDo
, 2);
2929 /* copy original do-loop body into inner do-loop's body */
2930 innerBody
= slang_oper_child(innerDo
, 0);
2931 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2932 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2934 /* inner do-loop's condition is constant/false */
2936 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2937 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2941 /* _notBreakFlag && LOOPCOND */
2943 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2944 cond
->type
= SLANG_OPER_LOGICALAND
;
2945 slang_operation_add_children(cond
, 2);
2947 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2948 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2951 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2952 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2957 /* Finally, in innerBody,
2958 * replace "break" with "_notBreakFlag = 0; break"
2959 * replace "continue" with "break"
2961 replace_break_and_cont(A
, innerBody
);
2963 /*slang_print_tree(top, 0);*/
2965 return _slang_gen_operation(A
, top
);
2970 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2972 static slang_ir_node
*
2973 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2977 * body code (child[0])
2979 * BREAK if !expr (child[1])
2981 slang_ir_node
*loop
;
2982 GLboolean isConst
, constTrue
;
2984 if (!A
->EmitContReturn
) {
2985 /* We don't want to emit CONT instructions. If this do-loop has
2986 * a continue, translate it away.
2988 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2989 return _slang_gen_do_without_continue(A
, oper
);
2993 /* type-check expression */
2994 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2995 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2999 loop
= new_loop(NULL
);
3001 /* save loop state */
3002 push_loop(A
, oper
, loop
);
3005 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3007 /* Check if loop condition is a constant */
3008 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3009 if (isConst
&& constTrue
) {
3010 /* do { } while(1) ==> no conditional break */
3011 loop
->Children
[1] = NULL
; /* no tail code */
3015 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3016 loop
->Children
[1] = new_break_if_true(A
, cond
);
3019 /* XXX we should do infinite loop detection, as above */
3021 /* restore loop state */
3029 * Recursively count the number of operations rooted at 'oper'.
3030 * This gives some kind of indication of the size/complexity of an operation.
3033 sizeof_operation(const slang_operation
*oper
)
3036 GLuint count
= 1; /* me */
3038 for (i
= 0; i
< oper
->num_children
; i
++) {
3039 count
+= sizeof_operation(&oper
->children
[i
]);
3050 * Determine if a for-loop can be unrolled.
3051 * At this time, only a rather narrow class of for loops can be unrolled.
3052 * See code for details.
3053 * When a loop can't be unrolled because it's too large we'll emit a
3054 * message to the log.
3057 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3061 const char *varName
;
3064 if (oper
->type
!= SLANG_OPER_FOR
)
3067 assert(oper
->num_children
== 4);
3069 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3072 /* children[0] must be either "int i=constant" or "i=constant" */
3073 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3074 slang_variable
*var
;
3076 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3079 varId
= oper
->children
[0].children
[0].a_id
;
3081 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3085 if (!var
->initializer
)
3087 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3089 start
= (GLint
) var
->initializer
->literal
[0];
3091 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3092 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3094 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3096 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3099 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3101 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3107 /* children[1] must be "i<constant" */
3108 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3110 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3112 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3114 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3117 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3119 /* children[2] must be "i++" or "++i" */
3120 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3121 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3123 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3126 /* make sure the same variable name is used in all places */
3127 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3128 (oper
->children
[2].children
[0].a_id
!= varId
))
3131 varName
= (const char *) varId
;
3133 /* children[3], the loop body, can't be too large */
3134 bodySize
= sizeof_operation(&oper
->children
[3]);
3135 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3136 slang_info_log_print(A
->log
,
3137 "Note: 'for (%s ... )' body is too large/complex"
3144 return GL_FALSE
; /* degenerate case */
3146 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3147 slang_info_log_print(A
->log
,
3148 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3149 " many iterations to unroll",
3150 varName
, start
, varName
, end
, varName
);
3154 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3155 slang_info_log_print(A
->log
,
3156 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3157 " too much code to unroll",
3158 varName
, start
, varName
, end
, varName
);
3162 return GL_TRUE
; /* we can unroll the loop */
3167 * Unroll a for-loop.
3168 * First we determine the number of iterations to unroll.
3169 * Then for each iteration:
3170 * make a copy of the loop body
3171 * replace instances of the loop variable with the current iteration value
3172 * generate IR code for the body
3173 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3175 static slang_ir_node
*
3176 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3178 GLint start
, end
, iter
;
3179 slang_ir_node
*n
, *root
= NULL
;
3182 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3183 /* for (int i=0; ... */
3184 slang_variable
*var
;
3186 varId
= oper
->children
[0].children
[0].a_id
;
3187 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3189 start
= (GLint
) var
->initializer
->literal
[0];
3193 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3194 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3197 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3199 for (iter
= start
; iter
< end
; iter
++) {
3200 slang_operation
*body
;
3202 /* make a copy of the loop body */
3203 body
= slang_operation_new(1);
3207 if (!slang_operation_copy(body
, &oper
->children
[3]))
3210 /* in body, replace instances of 'varId' with literal 'iter' */
3212 slang_variable
*oldVar
;
3213 slang_operation
*newOper
;
3215 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3217 /* undeclared loop variable */
3218 slang_operation_delete(body
);
3222 newOper
= slang_operation_new(1);
3223 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3224 newOper
->literal_size
= 1;
3225 newOper
->literal
[0] = iter
;
3227 /* replace instances of the loop variable with newOper */
3228 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3231 /* do IR codegen for body */
3232 n
= _slang_gen_operation(A
, body
);
3236 root
= new_seq(root
, n
);
3238 slang_operation_delete(body
);
3246 * Replace 'continue' statement with 'break' inside a for-loop.
3247 * This is a recursive helper function used by _slang_gen_for_without_continue().
3250 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3252 switch (oper
->type
) {
3253 case SLANG_OPER_CONTINUE
:
3254 oper
->type
= SLANG_OPER_BREAK
;
3256 case SLANG_OPER_FOR
:
3258 case SLANG_OPER_WHILE
:
3259 /* stop upon finding a nested loop */
3265 for (i
= 0; i
< oper
->num_children
; i
++) {
3266 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3274 * Transform a for-loop so that continue statements are converted to breaks.
3275 * Then do normal IR code generation.
3279 * for (INIT; LOOPCOND; INCR) {
3290 * bool _condFlag = 1;
3291 * for (INIT; _condFlag; ) {
3292 * for ( ; _condFlag = LOOPCOND; INCR) {
3304 static slang_ir_node
*
3305 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3307 slang_operation
*top
;
3308 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3309 slang_operation
*lhs
, *rhs
;
3311 assert(oper
->type
== SLANG_OPER_FOR
);
3313 top
= slang_operation_new(1);
3314 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3315 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3316 slang_operation_add_children(top
, 2);
3318 /* declare: bool _condFlag = true */
3320 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3321 slang_generate_declaration(A
, top
->locals
, condDecl
,
3322 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3325 /* build outer loop: for (INIT; _condFlag; ) { */
3326 outerFor
= slang_oper_child(top
, 1);
3327 outerFor
->type
= SLANG_OPER_FOR
;
3328 slang_operation_add_children(outerFor
, 4);
3330 init
= slang_oper_child(outerFor
, 0);
3331 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3333 cond
= slang_oper_child(outerFor
, 1);
3334 cond
->type
= SLANG_OPER_IDENTIFIER
;
3335 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3337 incr
= slang_oper_child(outerFor
, 2);
3338 incr
->type
= SLANG_OPER_VOID
;
3340 /* body of the outer loop */
3342 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3344 slang_operation_add_children(block
, 2);
3345 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3347 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3349 innerFor
= slang_oper_child(block
, 0);
3351 /* make copy of orig loop */
3352 slang_operation_copy(innerFor
, oper
);
3353 assert(innerFor
->type
== SLANG_OPER_FOR
);
3354 innerFor
->locals
->outer_scope
= block
->locals
;
3356 init
= slang_oper_child(innerFor
, 0);
3357 init
->type
= SLANG_OPER_VOID
; /* leak? */
3359 cond
= slang_oper_child(innerFor
, 1);
3360 slang_operation_destruct(cond
);
3361 cond
->type
= SLANG_OPER_ASSIGN
;
3362 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3363 slang_operation_add_children(cond
, 2);
3365 lhs
= slang_oper_child(cond
, 0);
3366 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3367 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3369 rhs
= slang_oper_child(cond
, 1);
3370 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3373 /* if (_condFlag) INCR; */
3375 slang_operation
*ifop
= slang_oper_child(block
, 1);
3376 ifop
->type
= SLANG_OPER_IF
;
3377 slang_operation_add_children(ifop
, 2);
3379 /* re-use cond node build above */
3380 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3382 /* incr node from original for-loop operation */
3383 slang_operation_copy(slang_oper_child(ifop
, 1),
3384 slang_oper_child(oper
, 2));
3387 /* finally, replace "continue" with "break" in the inner for-loop */
3388 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3391 return _slang_gen_operation(A
, top
);
3397 * Generate IR for a for-loop. Unrolling will be done when possible.
3399 static slang_ir_node
*
3400 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3404 if (!A
->EmitContReturn
) {
3405 /* We don't want to emit CONT instructions. If this for-loop has
3406 * a continue, translate it away.
3408 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3409 return _slang_gen_for_without_continue(A
, oper
);
3413 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3415 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3420 assert(oper
->type
== SLANG_OPER_FOR
);
3422 /* conventional for-loop code generation */
3425 * init code (child[0])
3427 * BREAK if !expr (child[1])
3428 * body code (child[3])
3430 * incr code (child[2]) // XXX continue here
3432 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3433 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3434 loop
= new_loop(NULL
);
3436 /* save loop state */
3437 push_loop(A
, oper
, loop
);
3439 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3440 breakIf
= new_break_if_true(A
, cond
);
3441 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3442 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3444 loop
->Children
[0] = new_seq(breakIf
, body
);
3445 loop
->Children
[1] = incr
; /* tail code */
3447 /* restore loop state */
3450 return new_seq(init
, loop
);
3455 static slang_ir_node
*
3456 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3458 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3460 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3461 loopNode
= current_loop_ir(A
);
3463 assert(loopNode
->Opcode
== IR_LOOP
);
3465 cont
= new_node0(IR_CONT
);
3467 cont
->Parent
= loopNode
;
3468 /* insert this node at head of linked list of cont/break instructions */
3469 cont
->List
= loopNode
->List
;
3470 loopNode
->List
= cont
;
3473 n
= new_seq(incr
, cont
);
3479 * Determine if the given operation is of a specific type.
3482 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3484 if (oper
->type
== type
)
3486 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3487 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3488 oper
->num_children
== 1)
3489 return is_operation_type(&oper
->children
[0], type
);
3496 * Generate IR tree for an if/then/else conditional using high-level
3497 * IR_IF instruction.
3499 static slang_ir_node
*
3500 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3503 * eval expr (child[0])
3510 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3511 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3512 GLboolean isConst
, constTrue
;
3514 /* type-check expression */
3515 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3516 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3520 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3521 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3525 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3529 return _slang_gen_operation(A
, &oper
->children
[1]);
3532 /* if (false) ... */
3533 return _slang_gen_operation(A
, &oper
->children
[2]);
3537 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3538 cond
= new_cond(cond
);
3540 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3541 && !haveElseClause
) {
3542 /* Special case: generate a conditional break */
3543 ifBody
= new_break_if_true(A
, cond
);
3546 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3548 && current_loop_oper(A
)
3549 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3550 /* Special case: generate a conditional continue */
3551 ifBody
= new_cont_if_true(A
, cond
);
3556 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3558 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3561 ifNode
= new_if(cond
, ifBody
, elseBody
);
3568 static slang_ir_node
*
3569 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3573 assert(oper
->type
== SLANG_OPER_NOT
);
3575 /* type-check expression */
3576 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3577 slang_info_log_error(A
->log
,
3578 "scalar/boolean expression expected for '!'");
3582 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3590 static slang_ir_node
*
3591 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3593 slang_ir_node
*n1
, *n2
;
3595 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3597 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3598 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3599 slang_info_log_error(A
->log
,
3600 "scalar/boolean expressions expected for '^^'");
3604 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3607 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3610 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3615 * Generate IR node for storage of a temporary of given size.
3617 static slang_ir_node
*
3618 _slang_gen_temporary(GLint size
)
3620 slang_ir_storage
*store
;
3621 slang_ir_node
*n
= NULL
;
3623 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3625 n
= new_node0(IR_VAR_DECL
);
3638 * Generate program constants for an array.
3639 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3640 * This will allocate and initialize three vector constants, storing
3641 * the array in constant memory, not temporaries like a non-const array.
3642 * This can also be used for uniform array initializers.
3643 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3646 make_constant_array(slang_assemble_ctx
*A
,
3647 slang_variable
*var
,
3648 slang_operation
*initializer
)
3650 struct gl_program
*prog
= A
->program
;
3651 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3652 const char *varName
= (char *) var
->a_name
;
3653 const GLuint numElements
= initializer
->num_children
;
3659 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3661 size
= var
->store
->Size
;
3663 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3664 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3665 assert(initializer
->type
== SLANG_OPER_CALL
);
3666 assert(initializer
->array_constructor
);
3668 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3670 /* convert constructor params into ordinary floats */
3671 for (i
= 0; i
< numElements
; i
++) {
3672 const slang_operation
*op
= &initializer
->children
[i
];
3673 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3674 /* unsupported type for this optimization */
3678 for (j
= 0; j
< op
->literal_size
; j
++) {
3679 values
[i
* 4 + j
] = op
->literal
[j
];
3681 for ( ; j
< 4; j
++) {
3682 values
[i
* 4 + j
] = 0.0f
;
3686 /* slightly different paths for constants vs. uniforms */
3687 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3688 var
->store
->File
= PROGRAM_UNIFORM
;
3689 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3690 size
, datatype
, values
);
3693 var
->store
->File
= PROGRAM_CONSTANT
;
3694 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3697 assert(var
->store
->Size
== size
);
3707 * Generate IR node for allocating/declaring a variable (either a local or
3709 * Generally, this involves allocating an slang_ir_storage instance for the
3710 * variable, choosing a register file (temporary, constant, etc).
3711 * For ordinary variables we do not yet allocate storage though. We do that
3712 * when we find the first actual use of the variable to avoid allocating temp
3713 * regs that will never get used.
3714 * At this time, uniforms are always allocated space in this function.
3716 * \param initializer Optional initializer expression for the variable.
3718 static slang_ir_node
*
3719 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3720 slang_operation
*initializer
)
3722 const char *varName
= (const char *) var
->a_name
;
3723 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3724 slang_ir_node
*varDecl
, *n
;
3725 slang_ir_storage
*store
;
3726 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3727 gl_register_file file
;
3729 /*assert(!var->declared);*/
3730 var
->declared
= GL_TRUE
;
3732 /* determine GPU register file for simple cases */
3733 if (is_sampler_type(&var
->type
)) {
3734 file
= PROGRAM_SAMPLER
;
3736 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3737 file
= PROGRAM_UNIFORM
;
3740 file
= PROGRAM_TEMPORARY
;
3743 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3745 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3749 arrayLen
= _slang_array_length(var
);
3750 totalSize
= _slang_array_size(size
, arrayLen
);
3752 /* Allocate IR node for the declaration */
3753 varDecl
= new_node0(IR_VAR_DECL
);
3757 /* Allocate slang_ir_storage for this variable if needed.
3758 * Note that we may not actually allocate a constant or temporary register
3762 GLint index
= -7; /* TBD / unknown */
3763 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3765 return NULL
; /* out of memory */
3768 /* set the IR node's Var and Store pointers */
3770 varDecl
->Store
= var
->store
;
3775 /* if there's an initializer, generate IR for the expression */
3777 slang_ir_node
*varRef
, *init
;
3779 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3780 /* if the variable is const, the initializer must be a const
3781 * expression as well.
3784 if (!_slang_is_constant_expr(initializer
)) {
3785 slang_info_log_error(A
->log
,
3786 "initializer for %s not constant", varName
);
3792 /* IR for the variable we're initializing */
3793 varRef
= new_var(A
, var
);
3795 slang_info_log_error(A
->log
, "out of memory");
3799 /* constant-folding, etc here */
3800 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3802 /* look for simple constant-valued variables and uniforms */
3803 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3804 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3806 if (initializer
->type
== SLANG_OPER_CALL
&&
3807 initializer
->array_constructor
) {
3808 /* array initializer */
3809 if (make_constant_array(A
, var
, initializer
))
3812 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3813 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3814 /* simple float/vector initializer */
3815 if (store
->File
== PROGRAM_UNIFORM
) {
3816 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3818 totalSize
, datatype
,
3819 initializer
->literal
);
3820 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3825 store
->File
= PROGRAM_CONSTANT
;
3826 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3828 initializer
->literal
,
3830 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3837 /* IR for initializer */
3838 init
= _slang_gen_operation(A
, initializer
);
3842 /* XXX remove this when type checking is added above */
3843 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3844 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3848 /* assign RHS to LHS */
3849 n
= new_node2(IR_COPY
, varRef
, init
);
3850 n
= new_seq(varDecl
, n
);
3853 /* no initializer */
3857 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3858 /* always need to allocate storage for uniforms at this point */
3859 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3860 totalSize
, datatype
, NULL
);
3861 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3865 printf("%s var %p %s store=%p index=%d size=%d\n",
3866 __FUNCTION__
, (void *) var
, (char *) varName
,
3867 (void *) store
, store
->Index
, store
->Size
);
3875 * Generate code for a selection expression: b ? x : y
3876 * XXX In some cases we could implement a selection expression
3877 * with an LRP instruction (use the boolean as the interpolant).
3878 * Otherwise, we use an IF/ELSE/ENDIF construct.
3880 static slang_ir_node
*
3881 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3883 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3884 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3885 slang_typeinfo type0
, type1
, type2
;
3886 int size
, isBool
, isEqual
;
3888 assert(oper
->type
== SLANG_OPER_SELECT
);
3889 assert(oper
->num_children
== 3);
3891 /* type of children[0] must be boolean */
3892 slang_typeinfo_construct(&type0
);
3893 typeof_operation(A
, &oper
->children
[0], &type0
);
3894 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3895 slang_typeinfo_destruct(&type0
);
3897 slang_info_log_error(A
->log
, "selector type is not boolean");
3901 slang_typeinfo_construct(&type1
);
3902 slang_typeinfo_construct(&type2
);
3903 typeof_operation(A
, &oper
->children
[1], &type1
);
3904 typeof_operation(A
, &oper
->children
[2], &type2
);
3905 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3906 slang_typeinfo_destruct(&type1
);
3907 slang_typeinfo_destruct(&type2
);
3909 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3913 /* size of x or y's type */
3914 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3918 tmpDecl
= _slang_gen_temporary(size
);
3920 /* the condition (child 0) */
3921 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3922 cond
= new_cond(cond
);
3924 /* if-true body (child 1) */
3925 tmpVar
= new_node0(IR_VAR
);
3926 tmpVar
->Store
= tmpDecl
->Store
;
3927 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3928 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3930 /* if-false body (child 2) */
3931 tmpVar
= new_node0(IR_VAR
);
3932 tmpVar
->Store
= tmpDecl
->Store
;
3933 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3934 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3936 ifNode
= new_if(cond
, trueNode
, falseNode
);
3939 tmpVar
= new_node0(IR_VAR
);
3940 tmpVar
->Store
= tmpDecl
->Store
;
3942 tree
= new_seq(ifNode
, tmpVar
);
3943 tree
= new_seq(tmpDecl
, tree
);
3945 /*_slang_print_ir_tree(tree, 10);*/
3951 * Generate code for &&.
3953 static slang_ir_node
*
3954 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3956 /* rewrite "a && b" as "a ? b : false" */
3957 slang_operation
*select
;
3960 select
= slang_operation_new(1);
3961 select
->type
= SLANG_OPER_SELECT
;
3962 slang_operation_add_children(select
, 3);
3964 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3965 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3966 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3968 n
= _slang_gen_select(A
, select
);
3974 * Generate code for ||.
3976 static slang_ir_node
*
3977 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3979 /* rewrite "a || b" as "a ? true : b" */
3980 slang_operation
*select
;
3983 select
= slang_operation_new(1);
3984 select
->type
= SLANG_OPER_SELECT
;
3985 slang_operation_add_children(select
, 3);
3987 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3988 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3989 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3991 n
= _slang_gen_select(A
, select
);
3997 * Generate IR tree for a return statement.
3999 static slang_ir_node
*
4000 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4002 const GLboolean haveReturnValue
4003 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
4005 assert(oper
->type
== SLANG_OPER_RETURN
);
4007 /* error checking */
4008 assert(A
->CurFunction
);
4009 if (haveReturnValue
&&
4010 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
4011 slang_info_log_error(A
->log
, "illegal return expression");
4014 else if (!haveReturnValue
&&
4015 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4016 slang_info_log_error(A
->log
, "return statement requires an expression");
4020 if (!haveReturnValue
) {
4021 if (A
->EmitContReturn
) {
4022 return new_return(A
->curFuncEndLabel
);
4025 replace_return_with_flag_set(A
, oper
);
4026 return _slang_gen_operation(A
, oper
);
4035 * return; // goto __endOfFunction
4037 slang_operation
*assign
;
4038 slang_atom a_retVal
;
4041 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
4047 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
4049 /* trying to return a value in a void-valued function */
4055 /* XXX use the gen_return_expression() function here */
4057 assign
= slang_operation_new(1);
4058 assign
->type
= SLANG_OPER_ASSIGN
;
4059 assign
->num_children
= 2;
4060 assign
->children
= slang_operation_new(2);
4061 /* lhs (__retVal) */
4062 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
4063 assign
->children
[0].a_id
= a_retVal
;
4064 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
4066 /* XXX we might be able to avoid this copy someday */
4067 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
4069 /* assemble the new code */
4070 if (A
->EmitContReturn
) {
4071 n
= new_seq(_slang_gen_operation(A
, assign
),
4072 new_return(A
->curFuncEndLabel
));
4074 else if (A
->UseReturnFlag
) {
4075 /* set __returnFlag = false; */
4076 slang_operation
*setFlag
= slang_operation_new(1);
4077 setFlag
->type
= SLANG_OPER_ASSIGN
;
4078 setFlag
->locals
->outer_scope
= oper
->locals
;
4079 slang_operation_add_children(setFlag
, 2);
4081 slang_operation
*lhs
= slang_oper_child(setFlag
, 0);
4082 lhs
->type
= SLANG_OPER_IDENTIFIER
;
4083 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
4086 slang_operation
*rhs
= slang_oper_child(setFlag
, 1);
4087 slang_operation_literal_bool(rhs
, GL_FALSE
);
4089 n
= new_seq(_slang_gen_operation(A
, assign
),
4090 _slang_gen_operation(A
, setFlag
));
4091 slang_operation_delete(setFlag
);
4094 slang_operation_delete(assign
);
4102 * Determine if the given operation/expression is const-valued.
4105 _slang_is_constant_expr(const slang_operation
*oper
)
4107 slang_variable
*var
;
4110 switch (oper
->type
) {
4111 case SLANG_OPER_IDENTIFIER
:
4112 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4113 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4117 for (i
= 0; i
< oper
->num_children
; i
++) {
4118 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4128 * Check if an assignment of type t1 to t0 is legal.
4129 * XXX more cases needed.
4132 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4133 slang_operation
*op0
,
4134 slang_operation
*op1
)
4136 slang_typeinfo t0
, t1
;
4139 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4140 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4144 slang_typeinfo_construct(&t0
);
4145 typeof_operation(A
, op0
, &t0
);
4147 slang_typeinfo_construct(&t1
);
4148 typeof_operation(A
, op1
, &t1
);
4150 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4151 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4155 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4160 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4161 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4162 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4165 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4166 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4169 #if 0 /* not used just yet - causes problems elsewhere */
4170 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4171 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4175 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4176 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4179 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4180 t1
.spec
.type
== SLANG_SPEC_INT
)
4188 * Generate IR tree for a local variable declaration.
4189 * Basically do some error checking and call _slang_gen_var_decl().
4191 static slang_ir_node
*
4192 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4194 const char *varName
= (char *) oper
->a_id
;
4195 slang_variable
*var
;
4196 slang_ir_node
*varDecl
;
4197 slang_operation
*initializer
;
4199 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4200 assert(oper
->num_children
<= 1);
4203 /* lookup the variable by name */
4204 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4206 return NULL
; /* "shouldn't happen" */
4208 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4209 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4210 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4211 /* can't declare attribute/uniform vars inside functions */
4212 slang_info_log_error(A
->log
,
4213 "local variable '%s' cannot be an attribute/uniform/varying",
4220 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4225 /* check if the var has an initializer */
4226 if (oper
->num_children
> 0) {
4227 assert(oper
->num_children
== 1);
4228 initializer
= &oper
->children
[0];
4230 else if (var
->initializer
) {
4231 initializer
= var
->initializer
;
4238 /* check/compare var type and initializer type */
4239 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4240 slang_info_log_error(A
->log
, "incompatible types in assignment");
4245 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4246 slang_info_log_error(A
->log
,
4247 "const-qualified variable '%s' requires initializer",
4253 /* Generate IR node */
4254 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4263 * Generate IR tree for a reference to a variable (such as in an expression).
4264 * This is different from a variable declaration.
4266 static slang_ir_node
*
4267 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4269 /* If there's a variable associated with this oper (from inlining)
4270 * use it. Otherwise, use the oper's var id.
4272 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4273 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4276 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4279 assert(var
->declared
);
4280 n
= new_var(A
, var
);
4287 * Return the number of components actually named by the swizzle.
4288 * Recall that swizzles may have undefined/don't-care values.
4291 swizzle_size(GLuint swizzle
)
4294 for (i
= 0; i
< 4; i
++) {
4295 GLuint swz
= GET_SWZ(swizzle
, i
);
4296 size
+= (swz
>= 0 && swz
<= 3);
4302 static slang_ir_node
*
4303 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4305 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4309 n
->Store
= _slang_new_ir_storage_relative(0,
4310 swizzle_size(swizzle
),
4312 n
->Store
->Swizzle
= swizzle
;
4319 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4321 while (store
->Parent
)
4322 store
= store
->Parent
;
4324 if (!(store
->File
== PROGRAM_OUTPUT
||
4325 store
->File
== PROGRAM_TEMPORARY
||
4326 (store
->File
== PROGRAM_VARYING
&&
4327 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4337 * Walk up an IR storage path to compute the final swizzle.
4338 * This is used when we find an expression such as "foo.xz.yx".
4341 root_swizzle(const slang_ir_storage
*st
)
4343 GLuint swizzle
= st
->Swizzle
;
4344 while (st
->Parent
) {
4346 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4353 * Generate IR tree for an assignment (=).
4355 static slang_ir_node
*
4356 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4358 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4359 /* Check that var is writeable */
4361 = _slang_variable_locate(oper
->children
[0].locals
,
4362 oper
->children
[0].a_id
, GL_TRUE
);
4364 slang_info_log_error(A
->log
, "undefined variable '%s'",
4365 (char *) oper
->children
[0].a_id
);
4368 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4369 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4370 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4371 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4372 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4373 slang_info_log_error(A
->log
,
4374 "illegal assignment to read-only variable '%s'",
4375 (char *) oper
->children
[0].a_id
);
4380 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4381 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4382 /* Special case of: x = f(a, b)
4383 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4385 * XXX this could be even more effective if we could accomodate
4386 * cases such as "v.x = f();" - would help with typical vertex
4390 n
= _slang_gen_function_call_name(A
,
4391 (const char *) oper
->children
[1].a_id
,
4392 &oper
->children
[1], &oper
->children
[0]);
4396 slang_ir_node
*n
, *lhs
, *rhs
;
4398 /* lhs and rhs type checking */
4399 if (!_slang_assignment_compatible(A
,
4401 &oper
->children
[1])) {
4402 slang_info_log_error(A
->log
, "incompatible types in assignment");
4406 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4412 slang_info_log_error(A
->log
,
4413 "invalid left hand side for assignment");
4417 /* check that lhs is writable */
4418 if (!is_store_writable(A
, lhs
->Store
)) {
4419 slang_info_log_error(A
->log
,
4420 "illegal assignment to read-only l-value");
4424 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4426 /* convert lhs swizzle into writemask */
4427 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4428 GLuint writemask
, newSwizzle
= 0x0;
4429 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4430 /* Non-simple writemask, need to swizzle right hand side in
4431 * order to put components into the right place.
4433 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4435 n
= new_node2(IR_COPY
, lhs
, rhs
);
4446 * Generate IR tree for referencing a field in a struct (or basic vector type)
4448 static slang_ir_node
*
4449 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4453 /* type of struct */
4454 slang_typeinfo_construct(&ti
);
4455 typeof_operation(A
, &oper
->children
[0], &ti
);
4457 if (_slang_type_is_vector(ti
.spec
.type
)) {
4458 /* the field should be a swizzle */
4459 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4463 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4464 slang_info_log_error(A
->log
, "Bad swizzle");
4467 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4472 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4473 /* create new parent node with swizzle */
4475 n
= _slang_gen_swizzle(n
, swizzle
);
4478 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4479 || ti
.spec
.type
== SLANG_SPEC_INT
4480 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4481 const GLuint rows
= 1;
4485 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4486 slang_info_log_error(A
->log
, "Bad swizzle");
4488 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4492 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4493 /* create new parent node with swizzle */
4494 n
= _slang_gen_swizzle(n
, swizzle
);
4498 /* the field is a structure member (base.field) */
4499 /* oper->children[0] is the base */
4500 /* oper->a_id is the field name */
4501 slang_ir_node
*base
, *n
;
4502 slang_typeinfo field_ti
;
4503 GLint fieldSize
, fieldOffset
= -1;
4506 slang_typeinfo_construct(&field_ti
);
4507 typeof_operation(A
, oper
, &field_ti
);
4509 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4511 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4513 if (fieldSize
== 0 || fieldOffset
< 0) {
4514 const char *structName
;
4515 if (ti
.spec
._struct
)
4516 structName
= (char *) ti
.spec
._struct
->a_name
;
4518 structName
= "unknown";
4519 slang_info_log_error(A
->log
,
4520 "\"%s\" is not a member of struct \"%s\"",
4521 (char *) oper
->a_id
, structName
);
4524 assert(fieldSize
>= 0);
4526 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4528 /* error msg should have already been logged */
4532 n
= new_node1(IR_FIELD
, base
);
4536 n
->Field
= (char *) oper
->a_id
;
4538 /* Store the field's offset in storage->Index */
4539 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4549 * Gen code for array indexing.
4551 static slang_ir_node
*
4552 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4554 slang_typeinfo array_ti
;
4556 /* get array's type info */
4557 slang_typeinfo_construct(&array_ti
);
4558 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4560 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4561 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4562 /* translate the index into a swizzle/writemask: "v.x=p" */
4563 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4567 index
= (GLint
) oper
->children
[1].literal
[0];
4568 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4569 index
>= (GLint
) max
) {
4571 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4572 printf("type = %d\n", oper
->children
[1].type
);
4573 printf("index = %d, max = %d\n", index
, max
);
4574 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4575 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4582 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4584 /* use swizzle to access the element */
4585 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4589 n
= _slang_gen_swizzle(n
, swizzle
);
4595 /* conventional array */
4596 slang_typeinfo elem_ti
;
4597 slang_ir_node
*elem
, *array
, *index
;
4598 GLint elemSize
, arrayLen
;
4600 /* size of array element */
4601 slang_typeinfo_construct(&elem_ti
);
4602 typeof_operation(A
, oper
, &elem_ti
);
4603 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4605 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4606 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4608 arrayLen
= array_ti
.array_len
;
4610 slang_typeinfo_destruct(&array_ti
);
4611 slang_typeinfo_destruct(&elem_ti
);
4613 if (elemSize
<= 0) {
4614 /* unknown var or type */
4615 slang_info_log_error(A
->log
, "Undefined variable or type");
4619 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4620 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4621 if (array
&& index
) {
4623 GLint constIndex
= -1;
4624 if (index
->Opcode
== IR_FLOAT
) {
4625 constIndex
= (int) index
->Value
[0];
4626 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4627 slang_info_log_error(A
->log
,
4628 "Array index out of bounds (index=%d size=%d)",
4629 constIndex
, arrayLen
);
4630 _slang_free_ir_tree(array
);
4631 _slang_free_ir_tree(index
);
4636 if (!array
->Store
) {
4637 slang_info_log_error(A
->log
, "Invalid array");
4641 elem
= new_node2(IR_ELEMENT
, array
, index
);
4643 /* The storage info here will be updated during code emit */
4644 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4645 array
->Store
->Index
,
4647 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4651 _slang_free_ir_tree(array
);
4652 _slang_free_ir_tree(index
);
4659 static slang_ir_node
*
4660 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4661 slang_ir_opcode opcode
)
4663 slang_typeinfo t0
, t1
;
4666 slang_typeinfo_construct(&t0
);
4667 typeof_operation(A
, &oper
->children
[0], &t0
);
4669 slang_typeinfo_construct(&t1
);
4670 typeof_operation(A
, &oper
->children
[0], &t1
);
4672 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4673 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4674 slang_info_log_error(A
->log
, "Illegal array comparison");
4678 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4679 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4680 /* <, <=, >, >= can only be used with scalars */
4681 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4682 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4683 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4684 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4685 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4690 n
= new_node2(opcode
,
4691 _slang_gen_operation(A
, &oper
->children
[0]),
4692 _slang_gen_operation(A
, &oper
->children
[1]));
4694 /* result is a bool (size 1) */
4695 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4703 print_vars(slang_variable_scope
*s
)
4707 for (i
= 0; i
< s
->num_variables
; i
++) {
4709 (char*) s
->variables
[i
]->a_name
,
4710 s
->variables
[i
]->declared
);
4720 _slang_undeclare_vars(slang_variable_scope
*locals
)
4722 if (locals
->num_variables
> 0) {
4724 for (i
= 0; i
< locals
->num_variables
; i
++) {
4725 slang_variable
*v
= locals
->variables
[i
];
4726 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4727 v
->declared
= GL_FALSE
;
4735 * Generate IR tree for a slang_operation (AST node)
4737 static slang_ir_node
*
4738 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4740 switch (oper
->type
) {
4741 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4745 _slang_push_var_table(A
->vartable
);
4747 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4748 n
= _slang_gen_operation(A
, oper
);
4749 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4751 _slang_pop_var_table(A
->vartable
);
4753 /*_slang_undeclare_vars(oper->locals);*/
4754 /*print_vars(oper->locals);*/
4757 n
= new_node1(IR_SCOPE
, n
);
4762 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4763 /* list of operations */
4764 if (oper
->num_children
> 0)
4766 slang_ir_node
*n
, *tree
= NULL
;
4769 for (i
= 0; i
< oper
->num_children
; i
++) {
4770 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4772 _slang_free_ir_tree(tree
);
4773 return NULL
; /* error must have occured */
4775 tree
= new_seq(tree
, n
);
4781 return new_node0(IR_NOP
);
4784 case SLANG_OPER_EXPRESSION
:
4785 return _slang_gen_operation(A
, &oper
->children
[0]);
4787 case SLANG_OPER_FOR
:
4788 return _slang_gen_for(A
, oper
);
4790 return _slang_gen_do(A
, oper
);
4791 case SLANG_OPER_WHILE
:
4792 return _slang_gen_while(A
, oper
);
4793 case SLANG_OPER_BREAK
:
4794 if (!current_loop_oper(A
)) {
4795 slang_info_log_error(A
->log
, "'break' not in loop");
4798 return new_break(current_loop_ir(A
));
4799 case SLANG_OPER_CONTINUE
:
4800 if (!current_loop_oper(A
)) {
4801 slang_info_log_error(A
->log
, "'continue' not in loop");
4804 return _slang_gen_continue(A
, oper
);
4805 case SLANG_OPER_DISCARD
:
4806 return new_node0(IR_KILL
);
4808 case SLANG_OPER_EQUAL
:
4809 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4810 case SLANG_OPER_NOTEQUAL
:
4811 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4812 case SLANG_OPER_GREATER
:
4813 return _slang_gen_compare(A
, oper
, IR_SGT
);
4814 case SLANG_OPER_LESS
:
4815 return _slang_gen_compare(A
, oper
, IR_SLT
);
4816 case SLANG_OPER_GREATEREQUAL
:
4817 return _slang_gen_compare(A
, oper
, IR_SGE
);
4818 case SLANG_OPER_LESSEQUAL
:
4819 return _slang_gen_compare(A
, oper
, IR_SLE
);
4820 case SLANG_OPER_ADD
:
4823 assert(oper
->num_children
== 2);
4824 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4827 case SLANG_OPER_SUBTRACT
:
4830 assert(oper
->num_children
== 2);
4831 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4834 case SLANG_OPER_MULTIPLY
:
4837 assert(oper
->num_children
== 2);
4838 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4841 case SLANG_OPER_DIVIDE
:
4844 assert(oper
->num_children
== 2);
4845 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4848 case SLANG_OPER_MINUS
:
4851 assert(oper
->num_children
== 1);
4852 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4855 case SLANG_OPER_PLUS
:
4856 /* +expr --> do nothing */
4857 return _slang_gen_operation(A
, &oper
->children
[0]);
4858 case SLANG_OPER_VARIABLE_DECL
:
4859 return _slang_gen_declaration(A
, oper
);
4860 case SLANG_OPER_ASSIGN
:
4861 return _slang_gen_assignment(A
, oper
);
4862 case SLANG_OPER_ADDASSIGN
:
4865 assert(oper
->num_children
== 2);
4866 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4869 case SLANG_OPER_SUBASSIGN
:
4872 assert(oper
->num_children
== 2);
4873 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4877 case SLANG_OPER_MULASSIGN
:
4880 assert(oper
->num_children
== 2);
4881 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4884 case SLANG_OPER_DIVASSIGN
:
4887 assert(oper
->num_children
== 2);
4888 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4891 case SLANG_OPER_LOGICALAND
:
4894 assert(oper
->num_children
== 2);
4895 n
= _slang_gen_logical_and(A
, oper
);
4898 case SLANG_OPER_LOGICALOR
:
4901 assert(oper
->num_children
== 2);
4902 n
= _slang_gen_logical_or(A
, oper
);
4905 case SLANG_OPER_LOGICALXOR
:
4906 return _slang_gen_xor(A
, oper
);
4907 case SLANG_OPER_NOT
:
4908 return _slang_gen_not(A
, oper
);
4909 case SLANG_OPER_SELECT
: /* b ? x : y */
4912 assert(oper
->num_children
== 3);
4913 n
= _slang_gen_select(A
, oper
);
4917 case SLANG_OPER_ASM
:
4918 return _slang_gen_asm(A
, oper
, NULL
);
4919 case SLANG_OPER_CALL
:
4920 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4922 case SLANG_OPER_METHOD
:
4923 return _slang_gen_method_call(A
, oper
);
4924 case SLANG_OPER_RETURN
:
4925 return _slang_gen_return(A
, oper
);
4926 case SLANG_OPER_LABEL
:
4927 return new_label(oper
->label
);
4928 case SLANG_OPER_IDENTIFIER
:
4929 return _slang_gen_variable(A
, oper
);
4931 return _slang_gen_if(A
, oper
);
4932 case SLANG_OPER_FIELD
:
4933 return _slang_gen_struct_field(A
, oper
);
4934 case SLANG_OPER_SUBSCRIPT
:
4935 return _slang_gen_array_element(A
, oper
);
4936 case SLANG_OPER_LITERAL_FLOAT
:
4938 case SLANG_OPER_LITERAL_INT
:
4940 case SLANG_OPER_LITERAL_BOOL
:
4941 return new_float_literal(oper
->literal
, oper
->literal_size
);
4943 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4946 assert(oper
->num_children
== 1);
4947 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4950 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4953 assert(oper
->num_children
== 1);
4954 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4957 case SLANG_OPER_PREINCREMENT
: /* ++var */
4960 assert(oper
->num_children
== 1);
4961 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4964 case SLANG_OPER_PREDECREMENT
: /* --var */
4967 assert(oper
->num_children
== 1);
4968 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4972 case SLANG_OPER_NON_INLINED_CALL
:
4973 case SLANG_OPER_SEQUENCE
:
4975 slang_ir_node
*tree
= NULL
;
4977 for (i
= 0; i
< oper
->num_children
; i
++) {
4978 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4979 tree
= new_seq(tree
, n
);
4981 tree
->Store
= n
->Store
;
4983 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4984 tree
= new_function_call(tree
, oper
->label
);
4989 case SLANG_OPER_NONE
:
4990 case SLANG_OPER_VOID
:
4991 /* returning NULL here would generate an error */
4992 return new_node0(IR_NOP
);
4995 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4997 return new_node0(IR_NOP
);
5005 * Check if the given type specifier is a rectangular texture sampler.
5008 is_rect_sampler_spec(const slang_type_specifier
*spec
)
5010 while (spec
->_array
) {
5011 spec
= spec
->_array
;
5013 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
5014 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
5020 * Called by compiler when a global variable has been parsed/compiled.
5021 * Here we examine the variable's type to determine what kind of register
5022 * storage will be used.
5024 * A uniform such as "gl_Position" will become the register specification
5025 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
5026 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
5028 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
5029 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
5030 * actual texture unit (as specified by the user calling glUniform1i()).
5033 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
5034 slang_unit_type type
)
5036 struct gl_program
*prog
= A
->program
;
5037 const char *varName
= (char *) var
->a_name
;
5038 GLboolean success
= GL_TRUE
;
5039 slang_ir_storage
*store
= NULL
;
5041 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
5042 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
5043 const GLint arrayLen
= _slang_array_length(var
);
5044 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
5045 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
5047 /* check for sampler2D arrays */
5048 if (texIndex
== -1 && var
->type
.specifier
._array
)
5049 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
5051 if (texIndex
!= -1) {
5052 /* This is a texture sampler variable...
5053 * store->File = PROGRAM_SAMPLER
5054 * store->Index = sampler number (0..7, typically)
5055 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
5057 if (var
->initializer
) {
5058 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5061 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5062 /* disallow rect samplers */
5063 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
5064 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5068 (void) is_rect_sampler_spec
; /* silence warning */
5071 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5072 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5074 /* If we have a sampler array, then we need to allocate the
5075 * additional samplers to ensure we don't allocate them elsewhere.
5076 * We can't directly use _mesa_add_sampler() as that checks the
5077 * varName and gets a match, so we call _mesa_add_parameter()
5078 * directly and use the last sampler number from the call above.
5081 GLint a
= arrayLen
- 1;
5083 for (i
= 0; i
< a
; i
++) {
5084 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5085 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5086 varName
, 1, datatype
, &value
, NULL
, 0x0);
5090 if (dbg
) printf("SAMPLER ");
5092 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5093 /* Uniform variable */
5094 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5097 /* user-defined uniform */
5098 if (datatype
== GL_NONE
) {
5099 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5100 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5101 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5102 /* temporary work-around */
5103 GLenum datatype
= GL_FLOAT
;
5104 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5105 totalSize
, datatype
, NULL
);
5106 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5107 totalSize
, swizzle
);
5110 GLint a
= arrayLen
- 1;
5112 for (i
= 0; i
< a
; i
++) {
5113 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5114 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5115 varName
, 1, datatype
, &value
, NULL
, 0x0);
5119 /* XXX what we need to do is unroll the struct into its
5120 * basic types, creating a uniform variable for each.
5128 * Should produce uniforms:
5129 * "f.a" (GL_FLOAT_VEC3)
5130 * "f.b" (GL_FLOAT_VEC4)
5133 if (var
->initializer
) {
5134 slang_info_log_error(A
->log
,
5135 "unsupported initializer for uniform '%s'", varName
);
5140 slang_info_log_error(A
->log
,
5141 "invalid datatype for uniform variable %s",
5147 /* non-struct uniform */
5148 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5154 /* pre-defined uniform, like gl_ModelviewMatrix */
5155 /* We know it's a uniform, but don't allocate storage unless
5158 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5159 totalSize
, swizzle
);
5161 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5163 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5164 /* varyings must be float, vec or mat */
5165 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5166 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5167 slang_info_log_error(A
->log
,
5168 "varying '%s' must be float/vector/matrix",
5173 if (var
->initializer
) {
5174 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5180 /* user-defined varying */
5186 if (var
->type
.centroid
== SLANG_CENTROID
)
5187 flags
|= PROG_PARAM_BIT_CENTROID
;
5188 if (var
->type
.variant
== SLANG_INVARIANT
)
5189 flags
|= PROG_PARAM_BIT_INVARIANT
;
5191 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5193 swizzle
= _slang_var_swizzle(size
, 0);
5194 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5195 totalSize
, swizzle
);
5198 /* pre-defined varying, like gl_Color or gl_TexCoord */
5199 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5200 /* fragment program input */
5202 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5205 assert(index
< FRAG_ATTRIB_MAX
);
5206 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5210 /* vertex program output */
5211 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5212 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5214 assert(index
< VERT_RESULT_MAX
);
5215 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5216 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5219 if (dbg
) printf("V/F ");
5221 if (dbg
) printf("VARYING ");
5223 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5226 /* attributes must be float, vec or mat */
5227 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5228 slang_info_log_error(A
->log
,
5229 "attribute '%s' must be float/vector/matrix",
5235 /* user-defined vertex attribute */
5236 const GLint attr
= -1; /* unknown */
5237 swizzle
= _slang_var_swizzle(size
, 0);
5238 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5239 size
, datatype
, attr
);
5241 index
= VERT_ATTRIB_GENERIC0
+ index
;
5244 /* pre-defined vertex attrib */
5245 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5248 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5249 if (dbg
) printf("ATTRIB ");
5251 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5252 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5253 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5255 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5256 if (dbg
) printf("INPUT ");
5258 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5259 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5260 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5261 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5264 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5265 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5266 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5267 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5269 if (dbg
) printf("OUTPUT ");
5271 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5272 /* pre-defined global constant, like gl_MaxLights */
5273 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5274 if (dbg
) printf("CONST ");
5277 /* ordinary variable (may be const) */
5280 /* IR node to declare the variable */
5281 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5283 /* emit GPU instructions */
5284 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5286 _slang_free_ir_tree(n
);
5289 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5290 store
? store
->Index
: -2);
5293 var
->store
= store
; /* save var's storage info */
5295 var
->declared
= GL_TRUE
;
5302 * Produce an IR tree from a function AST (fun->body).
5303 * Then call the code emitter to convert the IR tree into gl_program
5307 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5310 GLboolean success
= GL_TRUE
;
5312 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5313 /* we only really generate code for main, all other functions get
5314 * inlined or codegen'd upon an actual call.
5317 /* do some basic error checking though */
5318 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5319 /* check that non-void functions actually return something */
5321 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5323 slang_info_log_error(A
->log
,
5324 "function \"%s\" has no return statement",
5325 (char *) fun
->header
.a_name
);
5327 "function \"%s\" has no return statement\n",
5328 (char *) fun
->header
.a_name
);
5333 return GL_TRUE
; /* not an error */
5337 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5338 slang_print_function(fun
, 1);
5341 /* should have been allocated earlier: */
5342 assert(A
->program
->Parameters
);
5343 assert(A
->program
->Varying
);
5344 assert(A
->vartable
);
5347 A
->UseReturnFlag
= GL_FALSE
;
5348 A
->CurFunction
= fun
;
5350 /* fold constant expressions, etc. */
5351 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5354 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5355 slang_print_function(fun
, 1);
5358 /* Create an end-of-function label */
5359 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5361 /* push new vartable scope */
5362 _slang_push_var_table(A
->vartable
);
5364 /* Generate IR tree for the function body code */
5365 n
= _slang_gen_operation(A
, fun
->body
);
5367 n
= new_node1(IR_SCOPE
, n
);
5369 /* pop vartable, restore previous */
5370 _slang_pop_var_table(A
->vartable
);
5373 /* XXX record error */
5377 /* append an end-of-function-label to IR tree */
5378 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5380 /*_slang_label_delete(A->curFuncEndLabel);*/
5381 A
->curFuncEndLabel
= NULL
;
5384 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5385 slang_print_function(fun
, 1);
5388 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5389 _slang_print_ir_tree(n
, 0);
5392 printf("************* End codegen function ************\n\n");
5395 if (A
->UnresolvedRefs
) {
5396 /* Can't codegen at this time.
5397 * At link time we'll concatenate all the vertex shaders and/or all
5398 * the fragment shaders and try recompiling.
5403 /* Emit program instructions */
5404 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5405 _slang_free_ir_tree(n
);
5407 /* free codegen context */
5409 _mesa_free(A->codegen);