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 if (_mesa_strcmp((char *) oper
->a_id
, "__returnFlag"))
1042 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
1046 /* look for a substitution */
1047 for (i
= 0; i
< substCount
; i
++) {
1048 if (v
== substOld
[i
]) {
1049 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1050 #if 0 /* DEBUG only */
1051 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
1052 assert(substNew
[i
]->var
);
1053 assert(substNew
[i
]->var
->a_name
);
1054 printf("Substitute %s with %s in id node %p\n",
1055 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
1059 printf("Substitute %s with %f in id node %p\n",
1060 (char*)v
->a_name
, substNew
[i
]->literal
[0],
1064 slang_operation_copy(oper
, substNew
[i
]);
1071 case SLANG_OPER_RETURN
:
1072 /* do return replacement here too */
1073 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1074 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
1075 slang_operation
*newReturn
;
1077 /* check if function actually has a return type */
1078 assert(A
->CurFunction
);
1079 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
1080 slang_info_log_error(A
->log
, "illegal return expression");
1084 /* generate new 'return' code' */
1085 newReturn
= gen_return_expression(A
, oper
);
1087 /* do substitutions on the "__retVal = expr" sub-tree */
1088 slang_substitute(A
, slang_oper_child(newReturn
, 0),
1089 substCount
, substOld
, substNew
, GL_FALSE
);
1091 /* install new 'return' code */
1092 slang_operation_copy(oper
, newReturn
);
1093 slang_operation_destruct(newReturn
);
1096 /* check if return value was expected */
1097 assert(A
->CurFunction
);
1098 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
1099 slang_info_log_error(A
->log
, "return statement requires an expression");
1105 case SLANG_OPER_ASSIGN
:
1106 case SLANG_OPER_SUBSCRIPT
:
1108 * child[0] can't have substitutions but child[1] can.
1110 slang_substitute(A
, &oper
->children
[0],
1111 substCount
, substOld
, substNew
, GL_TRUE
);
1112 slang_substitute(A
, &oper
->children
[1],
1113 substCount
, substOld
, substNew
, GL_FALSE
);
1115 case SLANG_OPER_FIELD
:
1116 /* XXX NEW - test */
1117 slang_substitute(A
, &oper
->children
[0],
1118 substCount
, substOld
, substNew
, GL_TRUE
);
1123 for (i
= 0; i
< oper
->num_children
; i
++)
1124 slang_substitute(A
, &oper
->children
[i
],
1125 substCount
, substOld
, substNew
, GL_FALSE
);
1132 * Produce inline code for a call to an assembly instruction.
1133 * This is typically used to compile a call to a built-in function like this:
1135 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1137 * __asm vec4_lrp __retVal, a, y, x;
1142 * r = mix(p1, p2, p3);
1152 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1154 static slang_operation
*
1155 slang_inline_asm_function(slang_assemble_ctx
*A
,
1156 slang_function
*fun
, slang_operation
*oper
)
1158 const GLuint numArgs
= oper
->num_children
;
1160 slang_operation
*inlined
;
1161 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1162 slang_variable
**substOld
;
1163 slang_operation
**substNew
;
1165 ASSERT(slang_is_asm_function(fun
));
1166 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1169 printf("Inline %s as %s\n",
1170 (char*) fun->header.a_name,
1171 (char*) fun->body->children[0].a_id);
1175 * We'll substitute formal params with actual args in the asm call.
1177 substOld
= (slang_variable
**)
1178 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1179 substNew
= (slang_operation
**)
1180 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1181 for (i
= 0; i
< numArgs
; i
++) {
1182 substOld
[i
] = fun
->parameters
->variables
[i
];
1183 substNew
[i
] = oper
->children
+ i
;
1186 /* make a copy of the code to inline */
1187 inlined
= slang_operation_new(1);
1188 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1190 /* get rid of the __retVal child */
1191 inlined
->num_children
--;
1192 for (i
= 0; i
< inlined
->num_children
; i
++) {
1193 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1197 /* now do formal->actual substitutions */
1198 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1200 _slang_free(substOld
);
1201 _slang_free(substNew
);
1204 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1205 (char *) fun
->header
.a_name
);
1206 slang_print_tree(inlined
, 3);
1207 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1215 * Inline the given function call operation.
1216 * Return a new slang_operation that corresponds to the inlined code.
1218 static slang_operation
*
1219 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1220 slang_operation
*oper
, slang_operation
*returnOper
)
1227 ParamMode
*paramMode
;
1228 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1229 const GLuint numArgs
= oper
->num_children
;
1230 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1231 slang_operation
*args
= oper
->children
;
1232 slang_operation
*inlined
, *top
;
1233 slang_variable
**substOld
;
1234 slang_operation
**substNew
;
1235 GLuint substCount
, numCopyIn
, i
;
1236 slang_function
*prevFunction
;
1237 slang_variable_scope
*newScope
= NULL
;
1240 prevFunction
= A
->CurFunction
;
1241 A
->CurFunction
= fun
;
1243 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1244 assert(fun
->param_count
== totalArgs
);
1246 /* allocate temporary arrays */
1247 paramMode
= (ParamMode
*)
1248 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1249 substOld
= (slang_variable
**)
1250 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1251 substNew
= (slang_operation
**)
1252 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1255 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1256 (char *) fun
->header
.a_name
,
1257 fun
->parameters
->num_variables
, numArgs
);
1260 if (haveRetValue
&& !returnOper
) {
1261 /* Create 3-child comma sequence for inlined code:
1262 * child[0]: declare __resultTmp
1263 * child[1]: inlined function body
1264 * child[2]: __resultTmp
1266 slang_operation
*commaSeq
;
1267 slang_operation
*declOper
= NULL
;
1268 slang_variable
*resultVar
;
1270 commaSeq
= slang_operation_new(1);
1271 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1272 assert(commaSeq
->locals
);
1273 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1274 commaSeq
->num_children
= 3;
1275 commaSeq
->children
= slang_operation_new(3);
1276 /* allocate the return var */
1277 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1279 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1280 (void*)commaSeq->locals, (char *) fun->header.a_name);
1283 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1284 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1285 resultVar
->isTemp
= GL_TRUE
;
1287 /* child[0] = __resultTmp declaration */
1288 declOper
= &commaSeq
->children
[0];
1289 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1290 declOper
->a_id
= resultVar
->a_name
;
1291 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1293 /* child[1] = function body */
1294 inlined
= &commaSeq
->children
[1];
1295 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1297 /* child[2] = __resultTmp reference */
1298 returnOper
= &commaSeq
->children
[2];
1299 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1300 returnOper
->a_id
= resultVar
->a_name
;
1301 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1306 top
= inlined
= slang_operation_new(1);
1307 /* XXXX this may be inappropriate!!!! */
1308 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1312 assert(inlined
->locals
);
1314 /* Examine the parameters, look for inout/out params, look for possible
1315 * substitutions, etc:
1316 * param type behaviour
1317 * in copy actual to local
1318 * const in substitute param with actual
1322 for (i
= 0; i
< totalArgs
; i
++) {
1323 slang_variable
*p
= fun
->parameters
->variables
[i
];
1325 printf("Param %d: %s %s \n", i,
1326 slang_type_qual_string(p->type.qualifier),
1327 (char *) p->a_name);
1329 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1330 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1331 /* an output param */
1332 slang_operation
*arg
;
1337 paramMode
[i
] = SUBST
;
1339 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1340 slang_resolve_variable(arg
);
1342 /* replace parameter 'p' with argument 'arg' */
1343 substOld
[substCount
] = p
;
1344 substNew
[substCount
] = arg
; /* will get copied */
1347 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1348 /* a constant input param */
1349 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1350 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1351 /* replace all occurances of this parameter variable with the
1352 * actual argument variable or a literal.
1354 paramMode
[i
] = SUBST
;
1355 slang_resolve_variable(&args
[i
]);
1356 substOld
[substCount
] = p
;
1357 substNew
[substCount
] = &args
[i
]; /* will get copied */
1361 paramMode
[i
] = COPY_IN
;
1365 paramMode
[i
] = COPY_IN
;
1367 assert(paramMode
[i
]);
1370 /* actual code inlining: */
1371 slang_operation_copy(inlined
, fun
->body
);
1373 /*** XXX review this */
1374 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1375 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1376 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1379 printf("======================= orig body code ======================\n");
1380 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1381 slang_print_tree(fun
->body
, 8);
1382 printf("======================= copied code =========================\n");
1383 slang_print_tree(inlined
, 8);
1386 /* do parameter substitution in inlined code: */
1387 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1390 printf("======================= subst code ==========================\n");
1391 slang_print_tree(inlined
, 8);
1392 printf("=============================================================\n");
1395 /* New prolog statements: (inserted before the inlined code)
1396 * Copy the 'in' arguments.
1399 for (i
= 0; i
< numArgs
; i
++) {
1400 if (paramMode
[i
] == COPY_IN
) {
1401 slang_variable
*p
= fun
->parameters
->variables
[i
];
1402 /* declare parameter 'p' */
1403 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1407 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1408 assert(decl
->locals
);
1409 decl
->locals
->outer_scope
= inlined
->locals
;
1410 decl
->a_id
= p
->a_name
;
1411 decl
->num_children
= 1;
1412 decl
->children
= slang_operation_new(1);
1414 /* child[0] is the var's initializer */
1415 slang_operation_copy(&decl
->children
[0], args
+ i
);
1417 /* add parameter 'p' to the local variable scope here */
1419 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1420 pCopy
->type
= p
->type
;
1421 pCopy
->a_name
= p
->a_name
;
1422 pCopy
->array_len
= p
->array_len
;
1425 newScope
= inlined
->locals
;
1430 /* Now add copies of the function's local vars to the new variable scope */
1431 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1432 slang_variable
*p
= fun
->parameters
->variables
[i
];
1433 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1434 pCopy
->type
= p
->type
;
1435 pCopy
->a_name
= p
->a_name
;
1436 pCopy
->array_len
= p
->array_len
;
1440 /* New epilog statements:
1441 * 1. Create end of function label to jump to from return statements.
1442 * 2. Copy the 'out' parameter vars
1445 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1447 inlined
->num_children
);
1448 lab
->type
= SLANG_OPER_LABEL
;
1449 lab
->label
= A
->curFuncEndLabel
;
1452 for (i
= 0; i
< totalArgs
; i
++) {
1453 if (paramMode
[i
] == COPY_OUT
) {
1454 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1455 /* actualCallVar = outParam */
1456 /*if (i > 0 || !haveRetValue)*/
1457 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1459 inlined
->num_children
);
1460 ass
->type
= SLANG_OPER_ASSIGN
;
1461 ass
->num_children
= 2;
1462 ass
->locals
->outer_scope
= inlined
->locals
;
1463 ass
->children
= slang_operation_new(2);
1464 ass
->children
[0] = args
[i
]; /*XXX copy */
1465 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1466 ass
->children
[1].a_id
= p
->a_name
;
1467 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1471 _slang_free(paramMode
);
1472 _slang_free(substOld
);
1473 _slang_free(substNew
);
1475 /* Update scoping to use the new local vars instead of the
1476 * original function's vars. This is especially important
1477 * for nested inlining.
1480 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1483 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1484 (char *) fun
->header
.a_name
,
1485 fun
->parameters
->num_variables
, numArgs
);
1486 slang_print_tree(top
, 0);
1490 A
->CurFunction
= prevFunction
;
1497 * Insert declaration for "bool __returnFlag" in given block operation.
1498 * This is used when we can't emit "early" return statements in subroutines.
1501 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1503 slang_operation
*decl
;
1505 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1506 oper
->type
== SLANG_OPER_SEQUENCE
);
1508 decl
= slang_operation_insert_child(oper
, 1);
1510 slang_generate_declaration(A
, oper
->locals
, decl
,
1511 SLANG_SPEC_BOOL
, "__returnFlag", GL_TRUE
);
1513 /*slang_print_tree(oper, 0);*/
1518 * Replace 'return' with '__returnFlag = false'.
1519 * This is used to remove 'early returns' from functions.
1522 replace_return_with_flag_set(slang_assemble_ctx
*A
, slang_operation
*oper
)
1524 slang_atom id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
1525 assert(oper
->type
== SLANG_OPER_RETURN
);
1527 /* replace 'return' with __returnFlag = false' */
1528 slang_operation_free_children(oper
);
1529 oper
->type
= SLANG_OPER_ASSIGN
;
1530 slang_operation_add_children(oper
, 2);
1532 slang_operation
*lhs
= slang_oper_child(oper
, 0);
1533 lhs
->type
= SLANG_OPER_IDENTIFIER
;
1537 slang_operation
*rhs
= slang_oper_child(oper
, 1);
1538 slang_operation_literal_bool(rhs
, GL_FALSE
);
1542 slang_variable
*var
;
1543 var
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1550 * Test if the given function body has an "early return". That is, there's
1551 * a 'return' statement that's not the very last instruction in the body.
1554 has_early_return(const slang_operation
*funcBody
)
1556 GLuint retCount
= _slang_count_node_type(funcBody
, SLANG_OPER_RETURN
);
1559 else if (retCount
== 1 && _slang_is_tail_return(funcBody
))
1567 * Emit IR code for a function call. This does one of two things:
1568 * 1. Inline the function's code
1569 * 2. Create an IR for the function's body and create a real call to it.
1571 static slang_ir_node
*
1572 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1573 slang_operation
*oper
, slang_operation
*dest
)
1576 slang_operation
*inlined
;
1577 slang_label
*prevFuncEndLabel
;
1580 prevFuncEndLabel
= A
->curFuncEndLabel
;
1581 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1582 A
->curFuncEndLabel
= _slang_label_new(name
);
1583 assert(A
->curFuncEndLabel
);
1585 if (slang_is_asm_function(fun
) && !dest
) {
1586 /* assemble assembly function - tree style */
1587 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1590 /* non-assembly function */
1591 /* We always generate an "inline-able" block of code here.
1593 * 1. insert the inline code
1594 * 2. Generate a call to the "inline" code as a subroutine
1596 const GLboolean earlyReturn
= has_early_return(fun
->body
);
1597 slang_operation
*ret
= NULL
;
1599 if (earlyReturn
&& !A
->EmitContReturn
) {
1600 A
->UseReturnFlag
= GL_TRUE
;
1603 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1607 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1609 /* check if this is a "tail" return */
1611 /* The only RETURN is the last stmt in the function, no-op it
1612 * and inline the function body.
1614 ret
->type
= SLANG_OPER_NONE
;
1617 slang_operation
*callOper
;
1618 /* The function we're calling has one or more 'return' statements.
1619 * So, we can't truly inline this function because we need to
1620 * implement 'return' with RET (and CAL).
1621 * Nevertheless, we performed "inlining" to make a new instance
1622 * of the function body to deal with static register allocation.
1624 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1625 inlined
->type
== SLANG_OPER_SEQUENCE
);
1627 if (_slang_function_has_return_value(fun
) && !dest
) {
1628 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1629 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1630 callOper
= &inlined
->children
[1];
1636 if (A
->UseReturnFlag
) {
1637 /* Early returns not supported. Create a _returnFlag variable
1638 * that's set upon 'return' and tested elsewhere to no-op any
1639 * remaining instructions in the subroutine.
1641 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1642 callOper
->type
== SLANG_OPER_SEQUENCE
);
1643 declare_return_flag(A
, callOper
);
1645 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1646 callOper
->fun
= fun
;
1647 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1655 /* Replace the function call with the inlined block (or new CALL stmt) */
1656 slang_operation_destruct(oper
);
1658 _slang_free(inlined
);
1661 assert(inlined
->locals
);
1662 printf("*** Inlined code for call to %s:\n",
1663 (char*) fun
->header
.a_name
);
1664 slang_print_tree(oper
, 10);
1668 n
= _slang_gen_operation(A
, oper
);
1670 /*_slang_label_delete(A->curFuncEndLabel);*/
1671 A
->curFuncEndLabel
= prevFuncEndLabel
;
1673 if (A
->pragmas
->Debug
) {
1675 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1676 n
->Comment
= _slang_strdup(s
);
1679 A
->UseReturnFlag
= GL_FALSE
;
1685 static slang_asm_info
*
1686 slang_find_asm_info(const char *name
)
1689 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1690 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1699 * Some write-masked assignments are simple, but others are hard.
1702 * v.xy = vec2(a, b);
1705 * v.zy = vec2(a, b);
1706 * this gets transformed/swizzled into:
1707 * v.zy = vec2(a, b).*yx* (* = don't care)
1708 * This function helps to determine simple vs. non-simple.
1711 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1713 switch (writemask
) {
1715 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1717 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1719 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1721 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1723 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1724 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1726 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1727 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1728 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1729 case WRITEMASK_XYZW
:
1730 return swizzle
== SWIZZLE_NOOP
;
1738 * Convert the given swizzle into a writemask. In some cases this
1739 * is trivial, in other cases, we'll need to also swizzle the right
1740 * hand side to put components in the right places.
1741 * See comment above for more info.
1742 * XXX this function could be simplified and should probably be renamed.
1743 * \param swizzle the incoming swizzle
1744 * \param writemaskOut returns the writemask
1745 * \param swizzleOut swizzle to apply to the right-hand-side
1746 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1749 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1750 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1752 GLuint mask
= 0x0, newSwizzle
[4];
1755 /* make new dst writemask, compute size */
1756 for (i
= 0; i
< 4; i
++) {
1757 const GLuint swz
= GET_SWZ(swizzle
, i
);
1758 if (swz
== SWIZZLE_NIL
) {
1762 assert(swz
>= 0 && swz
<= 3);
1764 if (swizzle
!= SWIZZLE_XXXX
&&
1765 swizzle
!= SWIZZLE_YYYY
&&
1766 swizzle
!= SWIZZLE_ZZZZ
&&
1767 swizzle
!= SWIZZLE_WWWW
&&
1768 (mask
& (1 << swz
))) {
1769 /* a channel can't be specified twice (ex: ".xyyz") */
1770 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1771 _mesa_swizzle_string(swizzle
, 0, 0));
1777 assert(mask
<= 0xf);
1778 size
= i
; /* number of components in mask/swizzle */
1780 *writemaskOut
= mask
;
1782 /* make new src swizzle, by inversion */
1783 for (i
= 0; i
< 4; i
++) {
1784 newSwizzle
[i
] = i
; /*identity*/
1786 for (i
= 0; i
< size
; i
++) {
1787 const GLuint swz
= GET_SWZ(swizzle
, i
);
1788 newSwizzle
[swz
] = i
;
1790 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1795 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1797 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1799 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1801 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1803 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1811 #if 0 /* not used, but don't remove just yet */
1813 * Recursively traverse 'oper' to produce a swizzle mask in the event
1814 * of any vector subscripts and swizzle suffixes.
1815 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1818 resolve_swizzle(const slang_operation
*oper
)
1820 if (oper
->type
== SLANG_OPER_FIELD
) {
1821 /* writemask from .xyzw suffix */
1823 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1824 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1828 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1829 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1833 return SWIZZLE_XYZW
;
1835 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1836 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1837 /* writemask from [index] */
1838 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1839 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1844 swizzle
= SWIZZLE_XXXX
;
1847 swizzle
= SWIZZLE_YYYY
;
1850 swizzle
= SWIZZLE_ZZZZ
;
1853 swizzle
= SWIZZLE_WWWW
;
1856 swizzle
= SWIZZLE_XYZW
;
1858 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1862 return SWIZZLE_XYZW
;
1870 * Recursively descend through swizzle nodes to find the node's storage info.
1872 static slang_ir_storage
*
1873 get_store(const slang_ir_node
*n
)
1875 if (n
->Opcode
== IR_SWIZZLE
) {
1876 return get_store(n
->Children
[0]);
1884 * Generate IR tree for an asm instruction/operation such as:
1885 * __asm vec4_dot __retVal.x, v1, v2;
1887 static slang_ir_node
*
1888 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1889 slang_operation
*dest
)
1891 const slang_asm_info
*info
;
1892 slang_ir_node
*kids
[3], *n
;
1893 GLuint j
, firstOperand
;
1895 assert(oper
->type
== SLANG_OPER_ASM
);
1897 info
= slang_find_asm_info((char *) oper
->a_id
);
1899 _mesa_problem(NULL
, "undefined __asm function %s\n",
1900 (char *) oper
->a_id
);
1903 assert(info
->NumParams
<= 3);
1905 if (info
->NumParams
== oper
->num_children
) {
1906 /* Storage for result is not specified.
1907 * Children[0], [1], [2] are the operands.
1912 /* Storage for result (child[0]) is specified.
1913 * Children[1], [2], [3] are the operands.
1918 /* assemble child(ren) */
1919 kids
[0] = kids
[1] = kids
[2] = NULL
;
1920 for (j
= 0; j
< info
->NumParams
; j
++) {
1921 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1926 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1929 /* Setup n->Store to be a particular location. Otherwise, storage
1930 * for the result (a temporary) will be allocated later.
1932 slang_operation
*dest_oper
;
1935 dest_oper
= &oper
->children
[0];
1937 n0
= _slang_gen_operation(A
, dest_oper
);
1942 n
->Store
= n0
->Store
;
1944 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1955 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1958 for (i
= 0; i
< scope
->num_functions
; i
++) {
1959 slang_function
*f
= &scope
->functions
[i
];
1960 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1961 printf(" %s (%d args)\n", name
, f
->param_count
);
1964 if (scope
->outer_scope
)
1965 print_funcs(scope
->outer_scope
, name
);
1971 * Find a function of the given name, taking 'numArgs' arguments.
1972 * This is the function we'll try to call when there is no exact match
1973 * between function parameters and call arguments.
1975 * XXX we should really create a list of candidate functions and try
1978 static slang_function
*
1979 _slang_find_function_by_argc(slang_function_scope
*scope
,
1980 const char *name
, int numArgs
)
1984 for (i
= 0; i
< scope
->num_functions
; i
++) {
1985 slang_function
*f
= &scope
->functions
[i
];
1986 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1987 int haveRetValue
= _slang_function_has_return_value(f
);
1988 if (numArgs
== f
->param_count
- haveRetValue
)
1992 scope
= scope
->outer_scope
;
1999 static slang_function
*
2000 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
2003 slang_function
*maxFunc
= NULL
;
2008 for (i
= 0; i
< scope
->num_functions
; i
++) {
2009 slang_function
*f
= &scope
->functions
[i
];
2010 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
2011 if (f
->param_count
> maxArgs
) {
2012 maxArgs
= f
->param_count
;
2017 scope
= scope
->outer_scope
;
2025 * Generate a new slang_function which is a constructor for a user-defined
2028 static slang_function
*
2029 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
2031 const GLint numFields
= str
->fields
->num_variables
;
2032 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2034 /* function header (name, return type) */
2035 fun
->header
.a_name
= str
->a_name
;
2036 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2037 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
2038 fun
->header
.type
.specifier
._struct
= str
;
2040 /* function parameters (= struct's fields) */
2043 for (i
= 0; i
< numFields
; i
++) {
2045 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2047 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2048 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
2049 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2051 fun
->param_count
= fun
->parameters
->num_variables
;
2054 /* Add __retVal to params */
2056 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2057 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2059 p
->a_name
= a_retVal
;
2060 p
->type
= fun
->header
.type
;
2061 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2065 /* function body is:
2075 slang_variable_scope
*scope
;
2076 slang_variable
*var
;
2079 fun
->body
= slang_operation_new(1);
2080 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2081 fun
->body
->num_children
= numFields
+ 2;
2082 fun
->body
->children
= slang_operation_new(numFields
+ 2);
2084 scope
= fun
->body
->locals
;
2085 scope
->outer_scope
= fun
->parameters
;
2087 /* create local var 't' */
2088 var
= slang_variable_scope_grow(scope
);
2089 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
2090 var
->type
= fun
->header
.type
;
2094 slang_operation
*decl
;
2096 decl
= &fun
->body
->children
[0];
2097 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2098 decl
->locals
= _slang_variable_scope_new(scope
);
2099 decl
->a_id
= var
->a_name
;
2102 /* assign params to fields of t */
2103 for (i
= 0; i
< numFields
; i
++) {
2104 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2106 assign
->type
= SLANG_OPER_ASSIGN
;
2107 assign
->locals
= _slang_variable_scope_new(scope
);
2108 assign
->num_children
= 2;
2109 assign
->children
= slang_operation_new(2);
2112 slang_operation
*lhs
= &assign
->children
[0];
2114 lhs
->type
= SLANG_OPER_FIELD
;
2115 lhs
->locals
= _slang_variable_scope_new(scope
);
2116 lhs
->num_children
= 1;
2117 lhs
->children
= slang_operation_new(1);
2118 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2120 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2121 lhs
->children
[0].a_id
= var
->a_name
;
2122 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2125 lhs
->children
[1].num_children
= 1;
2126 lhs
->children
[1].children
= slang_operation_new(1);
2127 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2128 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2129 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2134 slang_operation
*rhs
= &assign
->children
[1];
2136 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2137 rhs
->locals
= _slang_variable_scope_new(scope
);
2138 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2144 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2146 ret
->type
= SLANG_OPER_RETURN
;
2147 ret
->locals
= _slang_variable_scope_new(scope
);
2148 ret
->num_children
= 1;
2149 ret
->children
= slang_operation_new(1);
2150 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2151 ret
->children
[0].a_id
= var
->a_name
;
2152 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2156 slang_print_function(fun, 1);
2163 * Find/create a function (constructor) for the given structure name.
2165 static slang_function
*
2166 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2169 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2170 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2171 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2172 /* found a structure type that matches the function name */
2173 if (!str
->constructor
) {
2174 /* create the constructor function now */
2175 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2177 return str
->constructor
;
2185 * Generate a new slang_function to satisfy a call to an array constructor.
2186 * Ex: float[3](1., 2., 3.)
2188 static slang_function
*
2189 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2191 slang_type_specifier_type baseType
;
2192 slang_function
*fun
;
2195 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2199 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2201 num_elements
= oper
->num_children
;
2203 /* function header, return type */
2205 fun
->header
.a_name
= oper
->a_id
;
2206 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2207 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2208 fun
->header
.type
.specifier
._array
=
2209 slang_type_specifier_new(baseType
, NULL
, NULL
);
2210 fun
->header
.type
.array_len
= num_elements
;
2213 /* function parameters (= number of elements) */
2216 for (i
= 0; i
< num_elements
; i
++) {
2218 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2220 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2222 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2223 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2224 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2225 p
->type
.specifier
.type
= baseType
;
2227 fun
->param_count
= fun
->parameters
->num_variables
;
2230 /* Add __retVal to params */
2232 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2233 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2235 p
->a_name
= a_retVal
;
2236 p
->type
= fun
->header
.type
;
2237 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2238 p
->type
.specifier
.type
= baseType
;
2242 /* function body is:
2252 slang_variable_scope
*scope
;
2253 slang_variable
*var
;
2256 fun
->body
= slang_operation_new(1);
2257 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2258 fun
->body
->num_children
= num_elements
+ 2;
2259 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2261 scope
= fun
->body
->locals
;
2262 scope
->outer_scope
= fun
->parameters
;
2264 /* create local var 't' */
2265 var
= slang_variable_scope_grow(scope
);
2266 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2267 var
->type
= fun
->header
.type
;/*XXX copy*/
2271 slang_operation
*decl
;
2273 decl
= &fun
->body
->children
[0];
2274 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2275 decl
->locals
= _slang_variable_scope_new(scope
);
2276 decl
->a_id
= var
->a_name
;
2279 /* assign params to elements of t */
2280 for (i
= 0; i
< num_elements
; i
++) {
2281 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2283 assign
->type
= SLANG_OPER_ASSIGN
;
2284 assign
->locals
= _slang_variable_scope_new(scope
);
2285 assign
->num_children
= 2;
2286 assign
->children
= slang_operation_new(2);
2289 slang_operation
*lhs
= &assign
->children
[0];
2291 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2292 lhs
->locals
= _slang_variable_scope_new(scope
);
2293 lhs
->num_children
= 2;
2294 lhs
->children
= slang_operation_new(2);
2296 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2297 lhs
->children
[0].a_id
= var
->a_name
;
2298 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2300 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2301 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2305 slang_operation
*rhs
= &assign
->children
[1];
2307 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2308 rhs
->locals
= _slang_variable_scope_new(scope
);
2309 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2315 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2317 ret
->type
= SLANG_OPER_RETURN
;
2318 ret
->locals
= _slang_variable_scope_new(scope
);
2319 ret
->num_children
= 1;
2320 ret
->children
= slang_operation_new(1);
2321 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2322 ret
->children
[0].a_id
= var
->a_name
;
2323 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2328 slang_print_function(fun, 1);
2336 _slang_is_vec_mat_type(const char *name
)
2338 static const char *vecmat_types
[] = {
2339 "float", "int", "bool",
2340 "vec2", "vec3", "vec4",
2341 "ivec2", "ivec3", "ivec4",
2342 "bvec2", "bvec3", "bvec4",
2343 "mat2", "mat3", "mat4",
2344 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2348 for (i
= 0; vecmat_types
[i
]; i
++)
2349 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2356 * Assemble a function call, given a particular function name.
2357 * \param name the function's name (operators like '*' are possible).
2359 static slang_ir_node
*
2360 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2361 slang_operation
*oper
, slang_operation
*dest
)
2363 slang_operation
*params
= oper
->children
;
2364 const GLuint param_count
= oper
->num_children
;
2366 slang_function
*fun
;
2369 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2370 if (atom
== SLANG_ATOM_NULL
)
2373 if (oper
->array_constructor
) {
2374 /* this needs special handling */
2375 fun
= _slang_make_array_constructor(A
, oper
);
2378 /* Try to find function by name and exact argument type matching */
2379 GLboolean error
= GL_FALSE
;
2380 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2381 &A
->space
, A
->atoms
, A
->log
, &error
);
2383 slang_info_log_error(A
->log
,
2384 "Function '%s' not found (check argument types)",
2391 /* Next, try locating a constructor function for a user-defined type */
2392 fun
= _slang_locate_struct_constructor(A
, name
);
2396 * At this point, some heuristics are used to try to find a function
2397 * that matches the calling signature by means of casting or "unrolling"
2401 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2402 /* Next, if this call looks like a vec() or mat() constructor call,
2403 * try "unwinding" the args to satisfy a constructor.
2405 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2407 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2408 slang_info_log_error(A
->log
,
2409 "Function '%s' not found (check argument types)",
2416 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2417 /* Next, try casting args to the types of the formal parameters */
2418 int numArgs
= oper
->num_children
;
2419 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2420 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2421 slang_info_log_error(A
->log
,
2422 "Function '%s' not found (check argument types)",
2430 slang_info_log_error(A
->log
,
2431 "Function '%s' not found (check argument types)",
2437 /* The function body may be in another compilation unit.
2438 * We'll try concatenating the shaders and recompile at link time.
2440 A
->UnresolvedRefs
= GL_TRUE
;
2441 return new_node1(IR_NOP
, NULL
);
2444 /* type checking to be sure function's return type matches 'dest' type */
2448 slang_typeinfo_construct(&t0
);
2449 typeof_operation(A
, dest
, &t0
);
2451 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2452 slang_info_log_error(A
->log
,
2453 "Incompatible type returned by call to '%s'",
2459 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2461 if (n
&& !n
->Store
&& !dest
2462 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2463 /* setup n->Store for the result of the function call */
2464 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2465 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2466 /*printf("Alloc storage for function result, size %d \n", size);*/
2469 if (oper
->array_constructor
) {
2470 /* free the temporary array constructor function now */
2471 slang_function_destruct(fun
);
2478 static slang_ir_node
*
2479 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2481 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2483 slang_variable
*var
;
2485 /* NOTE: In GLSL 1.20, there's only one kind of method
2486 * call: array.length(). Anything else is an error.
2488 if (oper
->a_id
!= a_length
) {
2489 slang_info_log_error(A
->log
,
2490 "Undefined method call '%s'", (char *) oper
->a_id
);
2494 /* length() takes no arguments */
2495 if (oper
->num_children
> 0) {
2496 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2500 /* lookup the object/variable */
2501 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2502 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2503 slang_info_log_error(A
->log
,
2504 "Undefined object '%s'", (char *) oper
->a_obj
);
2508 /* Create a float/literal IR node encoding the array length */
2509 n
= new_node0(IR_FLOAT
);
2511 n
->Value
[0] = (float) _slang_array_length(var
);
2512 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2519 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2521 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2522 oper
->type
== SLANG_OPER_LITERAL_INT
||
2523 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2524 if (oper
->literal
[0])
2530 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2531 oper
->num_children
== 1) {
2532 return _slang_is_constant_cond(&oper
->children
[0], value
);
2539 * Test if an operation is a scalar or boolean.
2542 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2544 slang_typeinfo type
;
2547 slang_typeinfo_construct(&type
);
2548 typeof_operation(A
, oper
, &type
);
2549 size
= _slang_sizeof_type_specifier(&type
.spec
);
2550 slang_typeinfo_destruct(&type
);
2556 * Test if an operation is boolean.
2559 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2561 slang_typeinfo type
;
2564 slang_typeinfo_construct(&type
);
2565 typeof_operation(A
, oper
, &type
);
2566 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2567 slang_typeinfo_destruct(&type
);
2573 * Check if a loop contains a 'continue' statement.
2574 * Stop looking if we find a nested loop.
2577 _slang_loop_contains_continue(const slang_operation
*oper
)
2579 switch (oper
->type
) {
2580 case SLANG_OPER_CONTINUE
:
2582 case SLANG_OPER_FOR
:
2584 case SLANG_OPER_WHILE
:
2585 /* stop upon finding a nested loop */
2591 for (i
= 0; i
< oper
->num_children
; i
++) {
2592 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2593 if (_slang_loop_contains_continue(child
))
2603 * Check if a loop contains a 'continue' or 'break' statement.
2604 * Stop looking if we find a nested loop.
2607 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2609 switch (oper
->type
) {
2610 case SLANG_OPER_CONTINUE
:
2611 case SLANG_OPER_BREAK
:
2613 case SLANG_OPER_FOR
:
2615 case SLANG_OPER_WHILE
:
2616 /* stop upon finding a nested loop */
2622 for (i
= 0; i
< oper
->num_children
; i
++) {
2623 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2624 if (_slang_loop_contains_continue_or_break(child
))
2634 * Replace 'break' and 'continue' statements inside a do and while loops.
2635 * This is a recursive helper function used by
2636 * _slang_gen_do/while_without_continue().
2639 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2641 switch (oper
->type
) {
2642 case SLANG_OPER_BREAK
:
2643 /* replace 'break' with "_notBreakFlag = false; break" */
2645 slang_operation
*block
= oper
;
2646 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2647 slang_operation_add_children(block
, 2);
2649 slang_operation
*assign
= slang_oper_child(block
, 0);
2650 assign
->type
= SLANG_OPER_ASSIGN
;
2651 slang_operation_add_children(assign
, 2);
2653 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2654 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2657 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2658 slang_operation_literal_bool(rhs
, GL_FALSE
);
2662 slang_operation
*brk
= slang_oper_child(block
, 1);
2663 brk
->type
= SLANG_OPER_BREAK
;
2664 assert(!brk
->children
);
2668 case SLANG_OPER_CONTINUE
:
2669 /* convert continue into a break */
2670 oper
->type
= SLANG_OPER_BREAK
;
2672 case SLANG_OPER_FOR
:
2674 case SLANG_OPER_WHILE
:
2675 /* stop upon finding a nested loop */
2681 for (i
= 0; i
< oper
->num_children
; i
++) {
2682 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2690 * Transform a while-loop so that continue statements are converted to breaks.
2691 * Then do normal IR code generation.
2695 * while (LOOPCOND) {
2707 * bool _notBreakFlag = 1;
2708 * while (_notBreakFlag && LOOPCOND) {
2712 * break; // was continue
2715 * _notBreakFlag = 0; // was
2722 static slang_ir_node
*
2723 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2725 slang_operation
*top
;
2726 slang_operation
*innerBody
;
2728 assert(oper
->type
== SLANG_OPER_WHILE
);
2730 top
= slang_operation_new(1);
2731 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2732 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2733 slang_operation_add_children(top
, 2);
2735 /* declare: bool _notBreakFlag = true */
2737 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2738 slang_generate_declaration(A
, top
->locals
, condDecl
,
2739 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2742 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2744 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2745 outerWhile
->type
= SLANG_OPER_WHILE
;
2746 slang_operation_add_children(outerWhile
, 2);
2748 /* _notBreakFlag && LOOPCOND */
2750 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2751 cond
->type
= SLANG_OPER_LOGICALAND
;
2752 slang_operation_add_children(cond
, 2);
2754 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2755 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2758 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2759 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2765 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2766 innerDo
->type
= SLANG_OPER_DO
;
2767 slang_operation_add_children(innerDo
, 2);
2769 /* copy original do-loop body into inner do-loop's body */
2770 innerBody
= slang_oper_child(innerDo
, 0);
2771 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2772 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2774 /* inner do-loop's condition is constant/false */
2776 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2777 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2782 /* Finally, in innerBody,
2783 * replace "break" with "_notBreakFlag = 0; break"
2784 * replace "continue" with "break"
2786 replace_break_and_cont(A
, innerBody
);
2788 /*slang_print_tree(top, 0);*/
2790 return _slang_gen_operation(A
, top
);
2797 * Generate loop code using high-level IR_LOOP instruction
2799 static slang_ir_node
*
2800 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2804 * BREAK if !expr (child[0])
2805 * body code (child[1])
2807 slang_ir_node
*loop
, *breakIf
, *body
;
2808 GLboolean isConst
, constTrue
;
2810 if (!A
->EmitContReturn
) {
2811 /* We don't want to emit CONT instructions. If this while-loop has
2812 * a continue, translate it away.
2814 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2815 return _slang_gen_while_without_continue(A
, oper
);
2819 /* type-check expression */
2820 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2821 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2825 /* Check if loop condition is a constant */
2826 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2828 if (isConst
&& !constTrue
) {
2829 /* loop is never executed! */
2830 return new_node0(IR_NOP
);
2833 /* Begin new loop */
2834 loop
= new_loop(NULL
);
2836 /* save loop state */
2837 push_loop(A
, oper
, loop
);
2839 if (isConst
&& constTrue
) {
2840 /* while(nonzero constant), no conditional break */
2845 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2846 breakIf
= new_break_if_true(A
, cond
);
2848 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2849 loop
->Children
[0] = new_seq(breakIf
, body
);
2851 /* Do infinite loop detection */
2852 /* loop->List is head of linked list of break/continue nodes */
2853 if (!loop
->List
&& isConst
&& constTrue
) {
2854 /* infinite loop detected */
2856 slang_info_log_error(A
->log
, "Infinite loop detected!");
2860 /* restore loop state */
2868 * Transform a do-while-loop so that continue statements are converted to breaks.
2869 * Then do normal IR code generation.
2880 * } while (LOOPCOND);
2885 * bool _notBreakFlag = 1;
2890 * break; // was continue
2893 * _notBreakFlag = 0; // was
2897 * } while (_notBreakFlag && LOOPCOND);
2900 static slang_ir_node
*
2901 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2903 slang_operation
*top
;
2904 slang_operation
*innerBody
;
2906 assert(oper
->type
== SLANG_OPER_DO
);
2908 top
= slang_operation_new(1);
2909 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2910 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2911 slang_operation_add_children(top
, 2);
2913 /* declare: bool _notBreakFlag = true */
2915 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2916 slang_generate_declaration(A
, top
->locals
, condDecl
,
2917 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2920 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2922 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2923 outerDo
->type
= SLANG_OPER_DO
;
2924 slang_operation_add_children(outerDo
, 2);
2928 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2929 innerDo
->type
= SLANG_OPER_DO
;
2930 slang_operation_add_children(innerDo
, 2);
2932 /* copy original do-loop body into inner do-loop's body */
2933 innerBody
= slang_oper_child(innerDo
, 0);
2934 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2935 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2937 /* inner do-loop's condition is constant/false */
2939 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2940 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2944 /* _notBreakFlag && LOOPCOND */
2946 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2947 cond
->type
= SLANG_OPER_LOGICALAND
;
2948 slang_operation_add_children(cond
, 2);
2950 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2951 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2954 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2955 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2960 /* Finally, in innerBody,
2961 * replace "break" with "_notBreakFlag = 0; break"
2962 * replace "continue" with "break"
2964 replace_break_and_cont(A
, innerBody
);
2966 /*slang_print_tree(top, 0);*/
2968 return _slang_gen_operation(A
, top
);
2973 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2975 static slang_ir_node
*
2976 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2980 * body code (child[0])
2982 * BREAK if !expr (child[1])
2984 slang_ir_node
*loop
;
2985 GLboolean isConst
, constTrue
;
2987 if (!A
->EmitContReturn
) {
2988 /* We don't want to emit CONT instructions. If this do-loop has
2989 * a continue, translate it away.
2991 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2992 return _slang_gen_do_without_continue(A
, oper
);
2996 /* type-check expression */
2997 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2998 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
3002 loop
= new_loop(NULL
);
3004 /* save loop state */
3005 push_loop(A
, oper
, loop
);
3008 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
3010 /* Check if loop condition is a constant */
3011 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
3012 if (isConst
&& constTrue
) {
3013 /* do { } while(1) ==> no conditional break */
3014 loop
->Children
[1] = NULL
; /* no tail code */
3018 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3019 loop
->Children
[1] = new_break_if_true(A
, cond
);
3022 /* XXX we should do infinite loop detection, as above */
3024 /* restore loop state */
3032 * Recursively count the number of operations rooted at 'oper'.
3033 * This gives some kind of indication of the size/complexity of an operation.
3036 sizeof_operation(const slang_operation
*oper
)
3039 GLuint count
= 1; /* me */
3041 for (i
= 0; i
< oper
->num_children
; i
++) {
3042 count
+= sizeof_operation(&oper
->children
[i
]);
3053 * Determine if a for-loop can be unrolled.
3054 * At this time, only a rather narrow class of for loops can be unrolled.
3055 * See code for details.
3056 * When a loop can't be unrolled because it's too large we'll emit a
3057 * message to the log.
3060 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3064 const char *varName
;
3067 if (oper
->type
!= SLANG_OPER_FOR
)
3070 assert(oper
->num_children
== 4);
3072 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
3075 /* children[0] must be either "int i=constant" or "i=constant" */
3076 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3077 slang_variable
*var
;
3079 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
3082 varId
= oper
->children
[0].children
[0].a_id
;
3084 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3088 if (!var
->initializer
)
3090 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
3092 start
= (GLint
) var
->initializer
->literal
[0];
3094 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
3095 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
3097 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3099 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3102 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3104 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3110 /* children[1] must be "i<constant" */
3111 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
3113 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
3115 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3117 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3120 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3122 /* children[2] must be "i++" or "++i" */
3123 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3124 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3126 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3129 /* make sure the same variable name is used in all places */
3130 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3131 (oper
->children
[2].children
[0].a_id
!= varId
))
3134 varName
= (const char *) varId
;
3136 /* children[3], the loop body, can't be too large */
3137 bodySize
= sizeof_operation(&oper
->children
[3]);
3138 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3139 slang_info_log_print(A
->log
,
3140 "Note: 'for (%s ... )' body is too large/complex"
3147 return GL_FALSE
; /* degenerate case */
3149 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3150 slang_info_log_print(A
->log
,
3151 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3152 " many iterations to unroll",
3153 varName
, start
, varName
, end
, varName
);
3157 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3158 slang_info_log_print(A
->log
,
3159 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3160 " too much code to unroll",
3161 varName
, start
, varName
, end
, varName
);
3165 return GL_TRUE
; /* we can unroll the loop */
3170 * Unroll a for-loop.
3171 * First we determine the number of iterations to unroll.
3172 * Then for each iteration:
3173 * make a copy of the loop body
3174 * replace instances of the loop variable with the current iteration value
3175 * generate IR code for the body
3176 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3178 static slang_ir_node
*
3179 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3181 GLint start
, end
, iter
;
3182 slang_ir_node
*n
, *root
= NULL
;
3185 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3186 /* for (int i=0; ... */
3187 slang_variable
*var
;
3189 varId
= oper
->children
[0].children
[0].a_id
;
3190 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3192 start
= (GLint
) var
->initializer
->literal
[0];
3196 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3197 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3200 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3202 for (iter
= start
; iter
< end
; iter
++) {
3203 slang_operation
*body
;
3205 /* make a copy of the loop body */
3206 body
= slang_operation_new(1);
3210 if (!slang_operation_copy(body
, &oper
->children
[3]))
3213 /* in body, replace instances of 'varId' with literal 'iter' */
3215 slang_variable
*oldVar
;
3216 slang_operation
*newOper
;
3218 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3220 /* undeclared loop variable */
3221 slang_operation_delete(body
);
3225 newOper
= slang_operation_new(1);
3226 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3227 newOper
->literal_size
= 1;
3228 newOper
->literal
[0] = iter
;
3230 /* replace instances of the loop variable with newOper */
3231 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3234 /* do IR codegen for body */
3235 n
= _slang_gen_operation(A
, body
);
3239 root
= new_seq(root
, n
);
3241 slang_operation_delete(body
);
3249 * Replace 'continue' statement with 'break' inside a for-loop.
3250 * This is a recursive helper function used by _slang_gen_for_without_continue().
3253 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3255 switch (oper
->type
) {
3256 case SLANG_OPER_CONTINUE
:
3257 oper
->type
= SLANG_OPER_BREAK
;
3259 case SLANG_OPER_FOR
:
3261 case SLANG_OPER_WHILE
:
3262 /* stop upon finding a nested loop */
3268 for (i
= 0; i
< oper
->num_children
; i
++) {
3269 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3277 * Transform a for-loop so that continue statements are converted to breaks.
3278 * Then do normal IR code generation.
3282 * for (INIT; LOOPCOND; INCR) {
3293 * bool _condFlag = 1;
3294 * for (INIT; _condFlag; ) {
3295 * for ( ; _condFlag = LOOPCOND; INCR) {
3307 static slang_ir_node
*
3308 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3310 slang_operation
*top
;
3311 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3312 slang_operation
*lhs
, *rhs
;
3314 assert(oper
->type
== SLANG_OPER_FOR
);
3316 top
= slang_operation_new(1);
3317 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3318 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3319 slang_operation_add_children(top
, 2);
3321 /* declare: bool _condFlag = true */
3323 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3324 slang_generate_declaration(A
, top
->locals
, condDecl
,
3325 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3328 /* build outer loop: for (INIT; _condFlag; ) { */
3329 outerFor
= slang_oper_child(top
, 1);
3330 outerFor
->type
= SLANG_OPER_FOR
;
3331 slang_operation_add_children(outerFor
, 4);
3333 init
= slang_oper_child(outerFor
, 0);
3334 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3336 cond
= slang_oper_child(outerFor
, 1);
3337 cond
->type
= SLANG_OPER_IDENTIFIER
;
3338 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3340 incr
= slang_oper_child(outerFor
, 2);
3341 incr
->type
= SLANG_OPER_VOID
;
3343 /* body of the outer loop */
3345 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3347 slang_operation_add_children(block
, 2);
3348 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3350 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3352 innerFor
= slang_oper_child(block
, 0);
3354 /* make copy of orig loop */
3355 slang_operation_copy(innerFor
, oper
);
3356 assert(innerFor
->type
== SLANG_OPER_FOR
);
3357 innerFor
->locals
->outer_scope
= block
->locals
;
3359 init
= slang_oper_child(innerFor
, 0);
3360 init
->type
= SLANG_OPER_VOID
; /* leak? */
3362 cond
= slang_oper_child(innerFor
, 1);
3363 slang_operation_destruct(cond
);
3364 cond
->type
= SLANG_OPER_ASSIGN
;
3365 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3366 slang_operation_add_children(cond
, 2);
3368 lhs
= slang_oper_child(cond
, 0);
3369 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3370 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3372 rhs
= slang_oper_child(cond
, 1);
3373 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3376 /* if (_condFlag) INCR; */
3378 slang_operation
*ifop
= slang_oper_child(block
, 1);
3379 ifop
->type
= SLANG_OPER_IF
;
3380 slang_operation_add_children(ifop
, 2);
3382 /* re-use cond node build above */
3383 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3385 /* incr node from original for-loop operation */
3386 slang_operation_copy(slang_oper_child(ifop
, 1),
3387 slang_oper_child(oper
, 2));
3390 /* finally, replace "continue" with "break" in the inner for-loop */
3391 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3394 return _slang_gen_operation(A
, top
);
3400 * Generate IR for a for-loop. Unrolling will be done when possible.
3402 static slang_ir_node
*
3403 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3407 if (!A
->EmitContReturn
) {
3408 /* We don't want to emit CONT instructions. If this for-loop has
3409 * a continue, translate it away.
3411 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3412 return _slang_gen_for_without_continue(A
, oper
);
3416 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3418 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3423 assert(oper
->type
== SLANG_OPER_FOR
);
3425 /* conventional for-loop code generation */
3428 * init code (child[0])
3430 * BREAK if !expr (child[1])
3431 * body code (child[3])
3433 * incr code (child[2]) // XXX continue here
3435 slang_ir_node
*loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3436 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3437 loop
= new_loop(NULL
);
3439 /* save loop state */
3440 push_loop(A
, oper
, loop
);
3442 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3443 breakIf
= new_break_if_true(A
, cond
);
3444 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3445 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3447 loop
->Children
[0] = new_seq(breakIf
, body
);
3448 loop
->Children
[1] = incr
; /* tail code */
3450 /* restore loop state */
3453 return new_seq(init
, loop
);
3458 static slang_ir_node
*
3459 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3461 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3463 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3464 loopNode
= current_loop_ir(A
);
3466 assert(loopNode
->Opcode
== IR_LOOP
);
3468 cont
= new_node0(IR_CONT
);
3470 cont
->Parent
= loopNode
;
3471 /* insert this node at head of linked list of cont/break instructions */
3472 cont
->List
= loopNode
->List
;
3473 loopNode
->List
= cont
;
3476 n
= new_seq(incr
, cont
);
3482 * Determine if the given operation is of a specific type.
3485 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3487 if (oper
->type
== type
)
3489 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3490 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3491 oper
->num_children
== 1)
3492 return is_operation_type(&oper
->children
[0], type
);
3499 * Generate IR tree for an if/then/else conditional using high-level
3500 * IR_IF instruction.
3502 static slang_ir_node
*
3503 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3506 * eval expr (child[0])
3513 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3514 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3515 GLboolean isConst
, constTrue
;
3517 /* type-check expression */
3518 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3519 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3523 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3524 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3528 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3532 return _slang_gen_operation(A
, &oper
->children
[1]);
3535 /* if (false) ... */
3536 return _slang_gen_operation(A
, &oper
->children
[2]);
3540 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3541 cond
= new_cond(cond
);
3543 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3544 && !haveElseClause
) {
3545 /* Special case: generate a conditional break */
3546 ifBody
= new_break_if_true(A
, cond
);
3549 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3551 && current_loop_oper(A
)
3552 && current_loop_oper(A
)->type
!= SLANG_OPER_FOR
) {
3553 /* Special case: generate a conditional continue */
3554 ifBody
= new_cont_if_true(A
, cond
);
3559 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3561 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3564 ifNode
= new_if(cond
, ifBody
, elseBody
);
3571 static slang_ir_node
*
3572 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3576 assert(oper
->type
== SLANG_OPER_NOT
);
3578 /* type-check expression */
3579 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3580 slang_info_log_error(A
->log
,
3581 "scalar/boolean expression expected for '!'");
3585 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3593 static slang_ir_node
*
3594 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3596 slang_ir_node
*n1
, *n2
;
3598 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3600 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3601 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3602 slang_info_log_error(A
->log
,
3603 "scalar/boolean expressions expected for '^^'");
3607 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3610 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3613 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3618 * Generate IR node for storage of a temporary of given size.
3620 static slang_ir_node
*
3621 _slang_gen_temporary(GLint size
)
3623 slang_ir_storage
*store
;
3624 slang_ir_node
*n
= NULL
;
3626 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3628 n
= new_node0(IR_VAR_DECL
);
3641 * Generate program constants for an array.
3642 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3643 * This will allocate and initialize three vector constants, storing
3644 * the array in constant memory, not temporaries like a non-const array.
3645 * This can also be used for uniform array initializers.
3646 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3649 make_constant_array(slang_assemble_ctx
*A
,
3650 slang_variable
*var
,
3651 slang_operation
*initializer
)
3653 struct gl_program
*prog
= A
->program
;
3654 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3655 const char *varName
= (char *) var
->a_name
;
3656 const GLuint numElements
= initializer
->num_children
;
3662 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3664 size
= var
->store
->Size
;
3666 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3667 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3668 assert(initializer
->type
== SLANG_OPER_CALL
);
3669 assert(initializer
->array_constructor
);
3671 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3673 /* convert constructor params into ordinary floats */
3674 for (i
= 0; i
< numElements
; i
++) {
3675 const slang_operation
*op
= &initializer
->children
[i
];
3676 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3677 /* unsupported type for this optimization */
3681 for (j
= 0; j
< op
->literal_size
; j
++) {
3682 values
[i
* 4 + j
] = op
->literal
[j
];
3684 for ( ; j
< 4; j
++) {
3685 values
[i
* 4 + j
] = 0.0f
;
3689 /* slightly different paths for constants vs. uniforms */
3690 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3691 var
->store
->File
= PROGRAM_UNIFORM
;
3692 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3693 size
, datatype
, values
);
3696 var
->store
->File
= PROGRAM_CONSTANT
;
3697 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3700 assert(var
->store
->Size
== size
);
3710 * Generate IR node for allocating/declaring a variable (either a local or
3712 * Generally, this involves allocating an slang_ir_storage instance for the
3713 * variable, choosing a register file (temporary, constant, etc).
3714 * For ordinary variables we do not yet allocate storage though. We do that
3715 * when we find the first actual use of the variable to avoid allocating temp
3716 * regs that will never get used.
3717 * At this time, uniforms are always allocated space in this function.
3719 * \param initializer Optional initializer expression for the variable.
3721 static slang_ir_node
*
3722 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3723 slang_operation
*initializer
)
3725 const char *varName
= (const char *) var
->a_name
;
3726 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3727 slang_ir_node
*varDecl
, *n
;
3728 slang_ir_storage
*store
;
3729 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3730 gl_register_file file
;
3732 /*assert(!var->declared);*/
3733 var
->declared
= GL_TRUE
;
3735 /* determine GPU register file for simple cases */
3736 if (is_sampler_type(&var
->type
)) {
3737 file
= PROGRAM_SAMPLER
;
3739 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3740 file
= PROGRAM_UNIFORM
;
3743 file
= PROGRAM_TEMPORARY
;
3746 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3748 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3752 arrayLen
= _slang_array_length(var
);
3753 totalSize
= _slang_array_size(size
, arrayLen
);
3755 /* Allocate IR node for the declaration */
3756 varDecl
= new_node0(IR_VAR_DECL
);
3760 /* Allocate slang_ir_storage for this variable if needed.
3761 * Note that we may not actually allocate a constant or temporary register
3765 GLint index
= -7; /* TBD / unknown */
3766 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3768 return NULL
; /* out of memory */
3771 /* set the IR node's Var and Store pointers */
3773 varDecl
->Store
= var
->store
;
3778 /* if there's an initializer, generate IR for the expression */
3780 slang_ir_node
*varRef
, *init
;
3782 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3783 /* if the variable is const, the initializer must be a const
3784 * expression as well.
3787 if (!_slang_is_constant_expr(initializer
)) {
3788 slang_info_log_error(A
->log
,
3789 "initializer for %s not constant", varName
);
3795 /* IR for the variable we're initializing */
3796 varRef
= new_var(A
, var
);
3798 slang_info_log_error(A
->log
, "out of memory");
3802 /* constant-folding, etc here */
3803 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3805 /* look for simple constant-valued variables and uniforms */
3806 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3807 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3809 if (initializer
->type
== SLANG_OPER_CALL
&&
3810 initializer
->array_constructor
) {
3811 /* array initializer */
3812 if (make_constant_array(A
, var
, initializer
))
3815 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3816 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3817 /* simple float/vector initializer */
3818 if (store
->File
== PROGRAM_UNIFORM
) {
3819 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3821 totalSize
, datatype
,
3822 initializer
->literal
);
3823 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3828 store
->File
= PROGRAM_CONSTANT
;
3829 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3831 initializer
->literal
,
3833 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3840 /* IR for initializer */
3841 init
= _slang_gen_operation(A
, initializer
);
3845 /* XXX remove this when type checking is added above */
3846 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3847 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3851 /* assign RHS to LHS */
3852 n
= new_node2(IR_COPY
, varRef
, init
);
3853 n
= new_seq(varDecl
, n
);
3856 /* no initializer */
3860 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3861 /* always need to allocate storage for uniforms at this point */
3862 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3863 totalSize
, datatype
, NULL
);
3864 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3868 printf("%s var %p %s store=%p index=%d size=%d\n",
3869 __FUNCTION__
, (void *) var
, (char *) varName
,
3870 (void *) store
, store
->Index
, store
->Size
);
3878 * Generate code for a selection expression: b ? x : y
3879 * XXX In some cases we could implement a selection expression
3880 * with an LRP instruction (use the boolean as the interpolant).
3881 * Otherwise, we use an IF/ELSE/ENDIF construct.
3883 static slang_ir_node
*
3884 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3886 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3887 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3888 slang_typeinfo type0
, type1
, type2
;
3889 int size
, isBool
, isEqual
;
3891 assert(oper
->type
== SLANG_OPER_SELECT
);
3892 assert(oper
->num_children
== 3);
3894 /* type of children[0] must be boolean */
3895 slang_typeinfo_construct(&type0
);
3896 typeof_operation(A
, &oper
->children
[0], &type0
);
3897 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3898 slang_typeinfo_destruct(&type0
);
3900 slang_info_log_error(A
->log
, "selector type is not boolean");
3904 slang_typeinfo_construct(&type1
);
3905 slang_typeinfo_construct(&type2
);
3906 typeof_operation(A
, &oper
->children
[1], &type1
);
3907 typeof_operation(A
, &oper
->children
[2], &type2
);
3908 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3909 slang_typeinfo_destruct(&type1
);
3910 slang_typeinfo_destruct(&type2
);
3912 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3916 /* size of x or y's type */
3917 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3921 tmpDecl
= _slang_gen_temporary(size
);
3923 /* the condition (child 0) */
3924 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3925 cond
= new_cond(cond
);
3927 /* if-true body (child 1) */
3928 tmpVar
= new_node0(IR_VAR
);
3929 tmpVar
->Store
= tmpDecl
->Store
;
3930 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3931 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3933 /* if-false body (child 2) */
3934 tmpVar
= new_node0(IR_VAR
);
3935 tmpVar
->Store
= tmpDecl
->Store
;
3936 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3937 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3939 ifNode
= new_if(cond
, trueNode
, falseNode
);
3942 tmpVar
= new_node0(IR_VAR
);
3943 tmpVar
->Store
= tmpDecl
->Store
;
3945 tree
= new_seq(ifNode
, tmpVar
);
3946 tree
= new_seq(tmpDecl
, tree
);
3948 /*_slang_print_ir_tree(tree, 10);*/
3954 * Generate code for &&.
3956 static slang_ir_node
*
3957 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3959 /* rewrite "a && b" as "a ? b : false" */
3960 slang_operation
*select
;
3963 select
= slang_operation_new(1);
3964 select
->type
= SLANG_OPER_SELECT
;
3965 slang_operation_add_children(select
, 3);
3967 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3968 slang_operation_copy(slang_oper_child(select
, 1), &oper
->children
[1]);
3969 slang_operation_literal_bool(slang_oper_child(select
, 2), GL_FALSE
);
3971 n
= _slang_gen_select(A
, select
);
3977 * Generate code for ||.
3979 static slang_ir_node
*
3980 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3982 /* rewrite "a || b" as "a ? true : b" */
3983 slang_operation
*select
;
3986 select
= slang_operation_new(1);
3987 select
->type
= SLANG_OPER_SELECT
;
3988 slang_operation_add_children(select
, 3);
3990 slang_operation_copy(slang_oper_child(select
, 0), &oper
->children
[0]);
3991 slang_operation_literal_bool(slang_oper_child(select
, 1), GL_TRUE
);
3992 slang_operation_copy(slang_oper_child(select
, 2), &oper
->children
[1]);
3994 n
= _slang_gen_select(A
, select
);
4000 * Generate IR tree for a return statement.
4002 static slang_ir_node
*
4003 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
4005 const GLboolean haveReturnValue
4006 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
4008 assert(oper
->type
== SLANG_OPER_RETURN
);
4010 /* error checking */
4011 assert(A
->CurFunction
);
4012 if (haveReturnValue
&&
4013 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
4014 slang_info_log_error(A
->log
, "illegal return expression");
4017 else if (!haveReturnValue
&&
4018 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4019 slang_info_log_error(A
->log
, "return statement requires an expression");
4023 if (!haveReturnValue
) {
4024 if (A
->EmitContReturn
) {
4025 return new_return(A
->curFuncEndLabel
);
4028 replace_return_with_flag_set(A
, oper
);
4029 return _slang_gen_operation(A
, oper
);
4038 * return; // goto __endOfFunction
4040 slang_operation
*assign
;
4041 slang_atom a_retVal
;
4044 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
4050 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
4052 /* trying to return a value in a void-valued function */
4058 /* XXX use the gen_return_expression() function here */
4060 assign
= slang_operation_new(1);
4061 assign
->type
= SLANG_OPER_ASSIGN
;
4062 assign
->num_children
= 2;
4063 assign
->children
= slang_operation_new(2);
4064 /* lhs (__retVal) */
4065 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
4066 assign
->children
[0].a_id
= a_retVal
;
4067 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
4069 /* XXX we might be able to avoid this copy someday */
4070 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
4072 /* assemble the new code */
4073 if (A
->EmitContReturn
) {
4074 n
= new_seq(_slang_gen_operation(A
, assign
),
4075 new_return(A
->curFuncEndLabel
));
4077 else if (A
->UseReturnFlag
) {
4078 /* set __returnFlag = false; */
4079 slang_operation
*setFlag
= slang_operation_new(1);
4080 setFlag
->type
= SLANG_OPER_ASSIGN
;
4081 setFlag
->locals
->outer_scope
= oper
->locals
;
4082 slang_operation_add_children(setFlag
, 2);
4084 slang_operation
*lhs
= slang_oper_child(setFlag
, 0);
4085 lhs
->type
= SLANG_OPER_IDENTIFIER
;
4086 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
4089 slang_operation
*rhs
= slang_oper_child(setFlag
, 1);
4090 slang_operation_literal_bool(rhs
, GL_FALSE
);
4092 n
= new_seq(_slang_gen_operation(A
, assign
),
4093 _slang_gen_operation(A
, setFlag
));
4094 slang_operation_delete(setFlag
);
4097 slang_operation_delete(assign
);
4105 * Determine if the given operation/expression is const-valued.
4108 _slang_is_constant_expr(const slang_operation
*oper
)
4110 slang_variable
*var
;
4113 switch (oper
->type
) {
4114 case SLANG_OPER_IDENTIFIER
:
4115 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4116 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4120 for (i
= 0; i
< oper
->num_children
; i
++) {
4121 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4131 * Check if an assignment of type t1 to t0 is legal.
4132 * XXX more cases needed.
4135 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4136 slang_operation
*op0
,
4137 slang_operation
*op1
)
4139 slang_typeinfo t0
, t1
;
4142 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4143 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4147 slang_typeinfo_construct(&t0
);
4148 typeof_operation(A
, op0
, &t0
);
4150 slang_typeinfo_construct(&t1
);
4151 typeof_operation(A
, op1
, &t1
);
4153 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4154 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4158 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4163 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4164 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4165 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4168 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4169 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4172 #if 0 /* not used just yet - causes problems elsewhere */
4173 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4174 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4178 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4179 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4182 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4183 t1
.spec
.type
== SLANG_SPEC_INT
)
4191 * Generate IR tree for a local variable declaration.
4192 * Basically do some error checking and call _slang_gen_var_decl().
4194 static slang_ir_node
*
4195 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4197 const char *varName
= (char *) oper
->a_id
;
4198 slang_variable
*var
;
4199 slang_ir_node
*varDecl
;
4200 slang_operation
*initializer
;
4202 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4203 assert(oper
->num_children
<= 1);
4206 /* lookup the variable by name */
4207 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4209 return NULL
; /* "shouldn't happen" */
4211 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4212 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4213 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4214 /* can't declare attribute/uniform vars inside functions */
4215 slang_info_log_error(A
->log
,
4216 "local variable '%s' cannot be an attribute/uniform/varying",
4223 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4228 /* check if the var has an initializer */
4229 if (oper
->num_children
> 0) {
4230 assert(oper
->num_children
== 1);
4231 initializer
= &oper
->children
[0];
4233 else if (var
->initializer
) {
4234 initializer
= var
->initializer
;
4241 /* check/compare var type and initializer type */
4242 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4243 slang_info_log_error(A
->log
, "incompatible types in assignment");
4248 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4249 slang_info_log_error(A
->log
,
4250 "const-qualified variable '%s' requires initializer",
4256 /* Generate IR node */
4257 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4266 * Generate IR tree for a reference to a variable (such as in an expression).
4267 * This is different from a variable declaration.
4269 static slang_ir_node
*
4270 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4272 /* If there's a variable associated with this oper (from inlining)
4273 * use it. Otherwise, use the oper's var id.
4275 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4276 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4279 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4282 assert(var
->declared
);
4283 n
= new_var(A
, var
);
4290 * Return the number of components actually named by the swizzle.
4291 * Recall that swizzles may have undefined/don't-care values.
4294 swizzle_size(GLuint swizzle
)
4297 for (i
= 0; i
< 4; i
++) {
4298 GLuint swz
= GET_SWZ(swizzle
, i
);
4299 size
+= (swz
>= 0 && swz
<= 3);
4305 static slang_ir_node
*
4306 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4308 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4312 n
->Store
= _slang_new_ir_storage_relative(0,
4313 swizzle_size(swizzle
),
4315 n
->Store
->Swizzle
= swizzle
;
4322 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4324 while (store
->Parent
)
4325 store
= store
->Parent
;
4327 if (!(store
->File
== PROGRAM_OUTPUT
||
4328 store
->File
== PROGRAM_TEMPORARY
||
4329 (store
->File
== PROGRAM_VARYING
&&
4330 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4340 * Walk up an IR storage path to compute the final swizzle.
4341 * This is used when we find an expression such as "foo.xz.yx".
4344 root_swizzle(const slang_ir_storage
*st
)
4346 GLuint swizzle
= st
->Swizzle
;
4347 while (st
->Parent
) {
4349 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4356 * Generate IR tree for an assignment (=).
4358 static slang_ir_node
*
4359 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4361 slang_operation
*pred
= NULL
;
4362 slang_ir_node
*n
= NULL
;
4364 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4365 /* Check that var is writeable */
4367 = _slang_variable_locate(oper
->children
[0].locals
,
4368 oper
->children
[0].a_id
, GL_TRUE
);
4370 slang_info_log_error(A
->log
, "undefined variable '%s'",
4371 (char *) oper
->children
[0].a_id
);
4374 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4375 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4376 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4377 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4378 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4379 slang_info_log_error(A
->log
,
4380 "illegal assignment to read-only variable '%s'",
4381 (char *) oper
->children
[0].a_id
);
4385 /* check if we need to predicate this assignment based on __returnFlag */
4386 if ((var
->is_global
||
4387 var
->type
.qualifier
== SLANG_QUAL_OUT
||
4388 var
->type
.qualifier
== SLANG_QUAL_INOUT
) && A
->UseReturnFlag
) {
4389 /* create predicate, used below */
4390 pred
= slang_operation_new(1);
4391 pred
->type
= SLANG_OPER_IDENTIFIER
;
4392 pred
->a_id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
4393 pred
->locals
->outer_scope
= oper
->locals
->outer_scope
;
4397 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4398 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4399 /* Special case of: x = f(a, b)
4400 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4402 * XXX this could be even more effective if we could accomodate
4403 * cases such as "v.x = f();" - would help with typical vertex
4406 n
= _slang_gen_function_call_name(A
,
4407 (const char *) oper
->children
[1].a_id
,
4408 &oper
->children
[1], &oper
->children
[0]);
4411 slang_ir_node
*lhs
, *rhs
;
4413 /* lhs and rhs type checking */
4414 if (!_slang_assignment_compatible(A
,
4416 &oper
->children
[1])) {
4417 slang_info_log_error(A
->log
, "incompatible types in assignment");
4421 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4427 slang_info_log_error(A
->log
,
4428 "invalid left hand side for assignment");
4432 /* check that lhs is writable */
4433 if (!is_store_writable(A
, lhs
->Store
)) {
4434 slang_info_log_error(A
->log
,
4435 "illegal assignment to read-only l-value");
4439 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4441 /* convert lhs swizzle into writemask */
4442 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4443 GLuint writemask
, newSwizzle
= 0x0;
4444 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4445 /* Non-simple writemask, need to swizzle right hand side in
4446 * order to put components into the right place.
4448 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4450 n
= new_node2(IR_COPY
, lhs
, rhs
);
4458 /* predicate the assignment code on __returnFlag */
4459 slang_ir_node
*top
, *cond
;
4461 cond
= _slang_gen_operation(A
, pred
);
4462 top
= new_if(cond
, n
, NULL
);
4470 * Generate IR tree for referencing a field in a struct (or basic vector type)
4472 static slang_ir_node
*
4473 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4477 /* type of struct */
4478 slang_typeinfo_construct(&ti
);
4479 typeof_operation(A
, &oper
->children
[0], &ti
);
4481 if (_slang_type_is_vector(ti
.spec
.type
)) {
4482 /* the field should be a swizzle */
4483 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4487 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4488 slang_info_log_error(A
->log
, "Bad swizzle");
4491 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4496 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4497 /* create new parent node with swizzle */
4499 n
= _slang_gen_swizzle(n
, swizzle
);
4502 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4503 || ti
.spec
.type
== SLANG_SPEC_INT
4504 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4505 const GLuint rows
= 1;
4509 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4510 slang_info_log_error(A
->log
, "Bad swizzle");
4512 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4516 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4517 /* create new parent node with swizzle */
4518 n
= _slang_gen_swizzle(n
, swizzle
);
4522 /* the field is a structure member (base.field) */
4523 /* oper->children[0] is the base */
4524 /* oper->a_id is the field name */
4525 slang_ir_node
*base
, *n
;
4526 slang_typeinfo field_ti
;
4527 GLint fieldSize
, fieldOffset
= -1;
4530 slang_typeinfo_construct(&field_ti
);
4531 typeof_operation(A
, oper
, &field_ti
);
4533 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4535 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4537 if (fieldSize
== 0 || fieldOffset
< 0) {
4538 const char *structName
;
4539 if (ti
.spec
._struct
)
4540 structName
= (char *) ti
.spec
._struct
->a_name
;
4542 structName
= "unknown";
4543 slang_info_log_error(A
->log
,
4544 "\"%s\" is not a member of struct \"%s\"",
4545 (char *) oper
->a_id
, structName
);
4548 assert(fieldSize
>= 0);
4550 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4552 /* error msg should have already been logged */
4556 n
= new_node1(IR_FIELD
, base
);
4560 n
->Field
= (char *) oper
->a_id
;
4562 /* Store the field's offset in storage->Index */
4563 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4573 * Gen code for array indexing.
4575 static slang_ir_node
*
4576 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4578 slang_typeinfo array_ti
;
4580 /* get array's type info */
4581 slang_typeinfo_construct(&array_ti
);
4582 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4584 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4585 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4586 /* translate the index into a swizzle/writemask: "v.x=p" */
4587 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4591 index
= (GLint
) oper
->children
[1].literal
[0];
4592 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4593 index
>= (GLint
) max
) {
4595 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4596 printf("type = %d\n", oper
->children
[1].type
);
4597 printf("index = %d, max = %d\n", index
, max
);
4598 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4599 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4606 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4608 /* use swizzle to access the element */
4609 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4613 n
= _slang_gen_swizzle(n
, swizzle
);
4619 /* conventional array */
4620 slang_typeinfo elem_ti
;
4621 slang_ir_node
*elem
, *array
, *index
;
4622 GLint elemSize
, arrayLen
;
4624 /* size of array element */
4625 slang_typeinfo_construct(&elem_ti
);
4626 typeof_operation(A
, oper
, &elem_ti
);
4627 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4629 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4630 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4632 arrayLen
= array_ti
.array_len
;
4634 slang_typeinfo_destruct(&array_ti
);
4635 slang_typeinfo_destruct(&elem_ti
);
4637 if (elemSize
<= 0) {
4638 /* unknown var or type */
4639 slang_info_log_error(A
->log
, "Undefined variable or type");
4643 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4644 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4645 if (array
&& index
) {
4647 GLint constIndex
= -1;
4648 if (index
->Opcode
== IR_FLOAT
) {
4649 constIndex
= (int) index
->Value
[0];
4650 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4651 slang_info_log_error(A
->log
,
4652 "Array index out of bounds (index=%d size=%d)",
4653 constIndex
, arrayLen
);
4654 _slang_free_ir_tree(array
);
4655 _slang_free_ir_tree(index
);
4660 if (!array
->Store
) {
4661 slang_info_log_error(A
->log
, "Invalid array");
4665 elem
= new_node2(IR_ELEMENT
, array
, index
);
4667 /* The storage info here will be updated during code emit */
4668 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4669 array
->Store
->Index
,
4671 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4675 _slang_free_ir_tree(array
);
4676 _slang_free_ir_tree(index
);
4683 static slang_ir_node
*
4684 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4685 slang_ir_opcode opcode
)
4687 slang_typeinfo t0
, t1
;
4690 slang_typeinfo_construct(&t0
);
4691 typeof_operation(A
, &oper
->children
[0], &t0
);
4693 slang_typeinfo_construct(&t1
);
4694 typeof_operation(A
, &oper
->children
[0], &t1
);
4696 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4697 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4698 slang_info_log_error(A
->log
, "Illegal array comparison");
4702 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4703 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4704 /* <, <=, >, >= can only be used with scalars */
4705 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4706 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4707 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4708 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4709 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4714 n
= new_node2(opcode
,
4715 _slang_gen_operation(A
, &oper
->children
[0]),
4716 _slang_gen_operation(A
, &oper
->children
[1]));
4718 /* result is a bool (size 1) */
4719 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4727 print_vars(slang_variable_scope
*s
)
4731 for (i
= 0; i
< s
->num_variables
; i
++) {
4733 (char*) s
->variables
[i
]->a_name
,
4734 s
->variables
[i
]->declared
);
4744 _slang_undeclare_vars(slang_variable_scope
*locals
)
4746 if (locals
->num_variables
> 0) {
4748 for (i
= 0; i
< locals
->num_variables
; i
++) {
4749 slang_variable
*v
= locals
->variables
[i
];
4750 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4751 v
->declared
= GL_FALSE
;
4759 * Generate IR tree for a slang_operation (AST node)
4761 static slang_ir_node
*
4762 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4764 switch (oper
->type
) {
4765 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4769 _slang_push_var_table(A
->vartable
);
4771 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4772 n
= _slang_gen_operation(A
, oper
);
4773 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4775 _slang_pop_var_table(A
->vartable
);
4777 /*_slang_undeclare_vars(oper->locals);*/
4778 /*print_vars(oper->locals);*/
4781 n
= new_node1(IR_SCOPE
, n
);
4786 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4787 /* list of operations */
4788 if (oper
->num_children
> 0)
4790 slang_ir_node
*n
, *tree
= NULL
;
4793 for (i
= 0; i
< oper
->num_children
; i
++) {
4794 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4796 _slang_free_ir_tree(tree
);
4797 return NULL
; /* error must have occured */
4799 tree
= new_seq(tree
, n
);
4805 return new_node0(IR_NOP
);
4808 case SLANG_OPER_EXPRESSION
:
4809 return _slang_gen_operation(A
, &oper
->children
[0]);
4811 case SLANG_OPER_FOR
:
4812 return _slang_gen_for(A
, oper
);
4814 return _slang_gen_do(A
, oper
);
4815 case SLANG_OPER_WHILE
:
4816 return _slang_gen_while(A
, oper
);
4817 case SLANG_OPER_BREAK
:
4818 if (!current_loop_oper(A
)) {
4819 slang_info_log_error(A
->log
, "'break' not in loop");
4822 return new_break(current_loop_ir(A
));
4823 case SLANG_OPER_CONTINUE
:
4824 if (!current_loop_oper(A
)) {
4825 slang_info_log_error(A
->log
, "'continue' not in loop");
4828 return _slang_gen_continue(A
, oper
);
4829 case SLANG_OPER_DISCARD
:
4830 return new_node0(IR_KILL
);
4832 case SLANG_OPER_EQUAL
:
4833 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4834 case SLANG_OPER_NOTEQUAL
:
4835 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4836 case SLANG_OPER_GREATER
:
4837 return _slang_gen_compare(A
, oper
, IR_SGT
);
4838 case SLANG_OPER_LESS
:
4839 return _slang_gen_compare(A
, oper
, IR_SLT
);
4840 case SLANG_OPER_GREATEREQUAL
:
4841 return _slang_gen_compare(A
, oper
, IR_SGE
);
4842 case SLANG_OPER_LESSEQUAL
:
4843 return _slang_gen_compare(A
, oper
, IR_SLE
);
4844 case SLANG_OPER_ADD
:
4847 assert(oper
->num_children
== 2);
4848 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4851 case SLANG_OPER_SUBTRACT
:
4854 assert(oper
->num_children
== 2);
4855 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4858 case SLANG_OPER_MULTIPLY
:
4861 assert(oper
->num_children
== 2);
4862 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4865 case SLANG_OPER_DIVIDE
:
4868 assert(oper
->num_children
== 2);
4869 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4872 case SLANG_OPER_MINUS
:
4875 assert(oper
->num_children
== 1);
4876 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4879 case SLANG_OPER_PLUS
:
4880 /* +expr --> do nothing */
4881 return _slang_gen_operation(A
, &oper
->children
[0]);
4882 case SLANG_OPER_VARIABLE_DECL
:
4883 return _slang_gen_declaration(A
, oper
);
4884 case SLANG_OPER_ASSIGN
:
4885 return _slang_gen_assignment(A
, oper
);
4886 case SLANG_OPER_ADDASSIGN
:
4889 assert(oper
->num_children
== 2);
4890 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4893 case SLANG_OPER_SUBASSIGN
:
4896 assert(oper
->num_children
== 2);
4897 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4901 case SLANG_OPER_MULASSIGN
:
4904 assert(oper
->num_children
== 2);
4905 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4908 case SLANG_OPER_DIVASSIGN
:
4911 assert(oper
->num_children
== 2);
4912 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4915 case SLANG_OPER_LOGICALAND
:
4918 assert(oper
->num_children
== 2);
4919 n
= _slang_gen_logical_and(A
, oper
);
4922 case SLANG_OPER_LOGICALOR
:
4925 assert(oper
->num_children
== 2);
4926 n
= _slang_gen_logical_or(A
, oper
);
4929 case SLANG_OPER_LOGICALXOR
:
4930 return _slang_gen_xor(A
, oper
);
4931 case SLANG_OPER_NOT
:
4932 return _slang_gen_not(A
, oper
);
4933 case SLANG_OPER_SELECT
: /* b ? x : y */
4936 assert(oper
->num_children
== 3);
4937 n
= _slang_gen_select(A
, oper
);
4941 case SLANG_OPER_ASM
:
4942 return _slang_gen_asm(A
, oper
, NULL
);
4943 case SLANG_OPER_CALL
:
4944 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4946 case SLANG_OPER_METHOD
:
4947 return _slang_gen_method_call(A
, oper
);
4948 case SLANG_OPER_RETURN
:
4949 return _slang_gen_return(A
, oper
);
4950 case SLANG_OPER_LABEL
:
4951 return new_label(oper
->label
);
4952 case SLANG_OPER_IDENTIFIER
:
4953 return _slang_gen_variable(A
, oper
);
4955 return _slang_gen_if(A
, oper
);
4956 case SLANG_OPER_FIELD
:
4957 return _slang_gen_struct_field(A
, oper
);
4958 case SLANG_OPER_SUBSCRIPT
:
4959 return _slang_gen_array_element(A
, oper
);
4960 case SLANG_OPER_LITERAL_FLOAT
:
4962 case SLANG_OPER_LITERAL_INT
:
4964 case SLANG_OPER_LITERAL_BOOL
:
4965 return new_float_literal(oper
->literal
, oper
->literal_size
);
4967 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4970 assert(oper
->num_children
== 1);
4971 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4974 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4977 assert(oper
->num_children
== 1);
4978 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4981 case SLANG_OPER_PREINCREMENT
: /* ++var */
4984 assert(oper
->num_children
== 1);
4985 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4988 case SLANG_OPER_PREDECREMENT
: /* --var */
4991 assert(oper
->num_children
== 1);
4992 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4996 case SLANG_OPER_NON_INLINED_CALL
:
4997 case SLANG_OPER_SEQUENCE
:
4999 slang_ir_node
*tree
= NULL
;
5001 for (i
= 0; i
< oper
->num_children
; i
++) {
5002 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
5003 tree
= new_seq(tree
, n
);
5005 tree
->Store
= n
->Store
;
5007 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
5008 tree
= new_function_call(tree
, oper
->label
);
5013 case SLANG_OPER_NONE
:
5014 case SLANG_OPER_VOID
:
5015 /* returning NULL here would generate an error */
5016 return new_node0(IR_NOP
);
5019 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
5021 return new_node0(IR_NOP
);
5029 * Check if the given type specifier is a rectangular texture sampler.
5032 is_rect_sampler_spec(const slang_type_specifier
*spec
)
5034 while (spec
->_array
) {
5035 spec
= spec
->_array
;
5037 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
5038 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
5044 * Called by compiler when a global variable has been parsed/compiled.
5045 * Here we examine the variable's type to determine what kind of register
5046 * storage will be used.
5048 * A uniform such as "gl_Position" will become the register specification
5049 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
5050 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
5052 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
5053 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
5054 * actual texture unit (as specified by the user calling glUniform1i()).
5057 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
5058 slang_unit_type type
)
5060 struct gl_program
*prog
= A
->program
;
5061 const char *varName
= (char *) var
->a_name
;
5062 GLboolean success
= GL_TRUE
;
5063 slang_ir_storage
*store
= NULL
;
5065 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
5066 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
5067 const GLint arrayLen
= _slang_array_length(var
);
5068 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
5069 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
5071 var
->is_global
= GL_TRUE
;
5073 /* check for sampler2D arrays */
5074 if (texIndex
== -1 && var
->type
.specifier
._array
)
5075 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
5077 if (texIndex
!= -1) {
5078 /* This is a texture sampler variable...
5079 * store->File = PROGRAM_SAMPLER
5080 * store->Index = sampler number (0..7, typically)
5081 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
5083 if (var
->initializer
) {
5084 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
5087 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5088 /* disallow rect samplers */
5089 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
5090 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
5094 (void) is_rect_sampler_spec
; /* silence warning */
5097 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
5098 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
5100 /* If we have a sampler array, then we need to allocate the
5101 * additional samplers to ensure we don't allocate them elsewhere.
5102 * We can't directly use _mesa_add_sampler() as that checks the
5103 * varName and gets a match, so we call _mesa_add_parameter()
5104 * directly and use the last sampler number from the call above.
5107 GLint a
= arrayLen
- 1;
5109 for (i
= 0; i
< a
; i
++) {
5110 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
5111 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
5112 varName
, 1, datatype
, &value
, NULL
, 0x0);
5116 if (dbg
) printf("SAMPLER ");
5118 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
5119 /* Uniform variable */
5120 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
5123 /* user-defined uniform */
5124 if (datatype
== GL_NONE
) {
5125 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
5126 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5127 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5128 /* temporary work-around */
5129 GLenum datatype
= GL_FLOAT
;
5130 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5131 totalSize
, datatype
, NULL
);
5132 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5133 totalSize
, swizzle
);
5136 GLint a
= arrayLen
- 1;
5138 for (i
= 0; i
< a
; i
++) {
5139 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5140 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5141 varName
, 1, datatype
, &value
, NULL
, 0x0);
5145 /* XXX what we need to do is unroll the struct into its
5146 * basic types, creating a uniform variable for each.
5154 * Should produce uniforms:
5155 * "f.a" (GL_FLOAT_VEC3)
5156 * "f.b" (GL_FLOAT_VEC4)
5159 if (var
->initializer
) {
5160 slang_info_log_error(A
->log
,
5161 "unsupported initializer for uniform '%s'", varName
);
5166 slang_info_log_error(A
->log
,
5167 "invalid datatype for uniform variable %s",
5173 /* non-struct uniform */
5174 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5180 /* pre-defined uniform, like gl_ModelviewMatrix */
5181 /* We know it's a uniform, but don't allocate storage unless
5184 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5185 totalSize
, swizzle
);
5187 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5189 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5190 /* varyings must be float, vec or mat */
5191 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5192 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5193 slang_info_log_error(A
->log
,
5194 "varying '%s' must be float/vector/matrix",
5199 if (var
->initializer
) {
5200 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5206 /* user-defined varying */
5212 if (var
->type
.centroid
== SLANG_CENTROID
)
5213 flags
|= PROG_PARAM_BIT_CENTROID
;
5214 if (var
->type
.variant
== SLANG_INVARIANT
)
5215 flags
|= PROG_PARAM_BIT_INVARIANT
;
5217 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5219 swizzle
= _slang_var_swizzle(size
, 0);
5220 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5221 totalSize
, swizzle
);
5224 /* pre-defined varying, like gl_Color or gl_TexCoord */
5225 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5226 /* fragment program input */
5228 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5231 assert(index
< FRAG_ATTRIB_MAX
);
5232 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5236 /* vertex program output */
5237 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5238 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5240 assert(index
< VERT_RESULT_MAX
);
5241 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5242 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5245 if (dbg
) printf("V/F ");
5247 if (dbg
) printf("VARYING ");
5249 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5252 /* attributes must be float, vec or mat */
5253 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5254 slang_info_log_error(A
->log
,
5255 "attribute '%s' must be float/vector/matrix",
5261 /* user-defined vertex attribute */
5262 const GLint attr
= -1; /* unknown */
5263 swizzle
= _slang_var_swizzle(size
, 0);
5264 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5265 size
, datatype
, attr
);
5267 index
= VERT_ATTRIB_GENERIC0
+ index
;
5270 /* pre-defined vertex attrib */
5271 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5274 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5275 if (dbg
) printf("ATTRIB ");
5277 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5278 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5279 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5281 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5282 if (dbg
) printf("INPUT ");
5284 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5285 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5286 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5287 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5290 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5291 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5292 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5293 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5295 if (dbg
) printf("OUTPUT ");
5297 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5298 /* pre-defined global constant, like gl_MaxLights */
5299 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5300 if (dbg
) printf("CONST ");
5303 /* ordinary variable (may be const) */
5306 /* IR node to declare the variable */
5307 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5309 /* emit GPU instructions */
5310 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5312 _slang_free_ir_tree(n
);
5315 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5316 store
? store
->Index
: -2);
5319 var
->store
= store
; /* save var's storage info */
5321 var
->declared
= GL_TRUE
;
5328 * Produce an IR tree from a function AST (fun->body).
5329 * Then call the code emitter to convert the IR tree into gl_program
5333 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5336 GLboolean success
= GL_TRUE
;
5338 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5339 /* we only really generate code for main, all other functions get
5340 * inlined or codegen'd upon an actual call.
5343 /* do some basic error checking though */
5344 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5345 /* check that non-void functions actually return something */
5347 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5349 slang_info_log_error(A
->log
,
5350 "function \"%s\" has no return statement",
5351 (char *) fun
->header
.a_name
);
5353 "function \"%s\" has no return statement\n",
5354 (char *) fun
->header
.a_name
);
5359 return GL_TRUE
; /* not an error */
5363 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5364 slang_print_function(fun
, 1);
5367 /* should have been allocated earlier: */
5368 assert(A
->program
->Parameters
);
5369 assert(A
->program
->Varying
);
5370 assert(A
->vartable
);
5373 A
->UseReturnFlag
= GL_FALSE
;
5374 A
->CurFunction
= fun
;
5376 /* fold constant expressions, etc. */
5377 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5380 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5381 slang_print_function(fun
, 1);
5384 /* Create an end-of-function label */
5385 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5387 /* push new vartable scope */
5388 _slang_push_var_table(A
->vartable
);
5390 /* Generate IR tree for the function body code */
5391 n
= _slang_gen_operation(A
, fun
->body
);
5393 n
= new_node1(IR_SCOPE
, n
);
5395 /* pop vartable, restore previous */
5396 _slang_pop_var_table(A
->vartable
);
5399 /* XXX record error */
5403 /* append an end-of-function-label to IR tree */
5404 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5406 /*_slang_label_delete(A->curFuncEndLabel);*/
5407 A
->curFuncEndLabel
= NULL
;
5410 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5411 slang_print_function(fun
, 1);
5414 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5415 _slang_print_ir_tree(n
, 0);
5418 printf("************* End codegen function ************\n\n");
5421 if (A
->UnresolvedRefs
) {
5422 /* Can't codegen at this time.
5423 * At link time we'll concatenate all the vertex shaders and/or all
5424 * the fragment shaders and try recompiling.
5429 /* Emit program instructions */
5430 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5431 _slang_free_ir_tree(n
);
5433 /* free codegen context */
5435 _mesa_free(A->codegen);