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
);
80 * Retrieves type information about an operation.
81 * Returns GL_TRUE on success.
82 * Returns GL_FALSE otherwise.
85 typeof_operation(const struct slang_assemble_ctx_
*A
,
89 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
94 is_sampler_type(const slang_fully_specified_type
*t
)
96 switch (t
->specifier
.type
) {
97 case SLANG_SPEC_SAMPLER1D
:
98 case SLANG_SPEC_SAMPLER2D
:
99 case SLANG_SPEC_SAMPLER3D
:
100 case SLANG_SPEC_SAMPLERCUBE
:
101 case SLANG_SPEC_SAMPLER1DSHADOW
:
102 case SLANG_SPEC_SAMPLER2DSHADOW
:
103 case SLANG_SPEC_SAMPLER2DRECT
:
104 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
113 * Return the offset (in floats or ints) of the named field within
114 * the given struct. Return -1 if field not found.
115 * If field is NULL, return the size of the struct instead.
118 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
122 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
123 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
124 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
126 /* types larger than 1 float are register (4-float) aligned */
127 offset
= (offset
+ 3) & ~3;
129 if (field
&& v
->a_name
== field
) {
135 return -1; /* field not found */
137 return offset
; /* struct size */
142 * Return the size (in floats) of the given type specifier.
143 * If the size is greater than 4, the size should be a multiple of 4
144 * so that the correct number of 4-float registers are allocated.
145 * For example, a mat3x2 is size 12 because we want to store the
146 * 3 columns in 3 float[4] registers.
149 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
152 switch (spec
->type
) {
153 case SLANG_SPEC_VOID
:
156 case SLANG_SPEC_BOOL
:
159 case SLANG_SPEC_BVEC2
:
162 case SLANG_SPEC_BVEC3
:
165 case SLANG_SPEC_BVEC4
:
171 case SLANG_SPEC_IVEC2
:
174 case SLANG_SPEC_IVEC3
:
177 case SLANG_SPEC_IVEC4
:
180 case SLANG_SPEC_FLOAT
:
183 case SLANG_SPEC_VEC2
:
186 case SLANG_SPEC_VEC3
:
189 case SLANG_SPEC_VEC4
:
192 case SLANG_SPEC_MAT2
:
193 sz
= 2 * 4; /* 2 columns (regs) */
195 case SLANG_SPEC_MAT3
:
198 case SLANG_SPEC_MAT4
:
201 case SLANG_SPEC_MAT23
:
202 sz
= 2 * 4; /* 2 columns (regs) */
204 case SLANG_SPEC_MAT32
:
205 sz
= 3 * 4; /* 3 columns (regs) */
207 case SLANG_SPEC_MAT24
:
210 case SLANG_SPEC_MAT42
:
211 sz
= 4 * 4; /* 4 columns (regs) */
213 case SLANG_SPEC_MAT34
:
216 case SLANG_SPEC_MAT43
:
217 sz
= 4 * 4; /* 4 columns (regs) */
219 case SLANG_SPEC_SAMPLER1D
:
220 case SLANG_SPEC_SAMPLER2D
:
221 case SLANG_SPEC_SAMPLER3D
:
222 case SLANG_SPEC_SAMPLERCUBE
:
223 case SLANG_SPEC_SAMPLER1DSHADOW
:
224 case SLANG_SPEC_SAMPLER2DSHADOW
:
225 case SLANG_SPEC_SAMPLER2DRECT
:
226 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
227 sz
= 1; /* a sampler is basically just an integer index */
229 case SLANG_SPEC_STRUCT
:
230 sz
= _slang_field_offset(spec
, 0); /* special use */
232 /* 1-float structs are actually troublesome to deal with since they
233 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
234 * ensure the object is placed at R.x
239 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
242 case SLANG_SPEC_ARRAY
:
243 sz
= _slang_sizeof_type_specifier(spec
->_array
);
246 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
251 /* if size is > 4, it should be a multiple of four */
252 assert((sz
& 0x3) == 0);
259 * Query variable/array length (number of elements).
260 * This is slightly non-trivial because there are two ways to express
261 * arrays: "float x[3]" vs. "float[3] x".
262 * \return the length of the array for the given variable, or 0 if not an array
265 _slang_array_length(const slang_variable
*var
)
267 if (var
->type
.array_len
> 0) {
268 /* Ex: float[4] x; */
269 return var
->type
.array_len
;
271 if (var
->array_len
> 0) {
272 /* Ex: float x[4]; */
273 return var
->array_len
;
280 * Compute total size of array give size of element, number of elements.
281 * \return size in floats
284 _slang_array_size(GLint elemSize
, GLint arrayLen
)
287 assert(elemSize
> 0);
289 /* round up base type to multiple of 4 */
290 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
300 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
301 * or -1 if the type is not a sampler.
304 sampler_to_texture_index(const slang_type_specifier_type type
)
307 case SLANG_SPEC_SAMPLER1D
:
308 return TEXTURE_1D_INDEX
;
309 case SLANG_SPEC_SAMPLER2D
:
310 return TEXTURE_2D_INDEX
;
311 case SLANG_SPEC_SAMPLER3D
:
312 return TEXTURE_3D_INDEX
;
313 case SLANG_SPEC_SAMPLERCUBE
:
314 return TEXTURE_CUBE_INDEX
;
315 case SLANG_SPEC_SAMPLER1DSHADOW
:
316 return TEXTURE_1D_INDEX
; /* XXX fix */
317 case SLANG_SPEC_SAMPLER2DSHADOW
:
318 return TEXTURE_2D_INDEX
; /* XXX fix */
319 case SLANG_SPEC_SAMPLER2DRECT
:
320 return TEXTURE_RECT_INDEX
;
321 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
322 return TEXTURE_RECT_INDEX
; /* XXX fix */
329 /** helper to build a SLANG_OPER_IDENTIFIER node */
331 slang_operation_identifier(slang_operation
*oper
,
332 slang_assemble_ctx
*A
,
335 oper
->type
= SLANG_OPER_IDENTIFIER
;
336 oper
->a_id
= slang_atom_pool_atom(A
->atoms
, name
);
340 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
343 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
344 * a vertex or fragment program input variable. Return -1 if the input
346 * XXX return size too
349 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
356 static const struct input_info vertInputs
[] = {
357 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
358 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
359 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
360 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
361 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
362 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
363 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
364 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
365 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
366 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
367 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
368 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
369 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
370 { NULL
, 0, SWIZZLE_NOOP
}
372 static const struct input_info fragInputs
[] = {
373 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
374 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
375 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
376 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
377 /* note: we're packing several quantities into the fogcoord vector */
378 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
379 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
380 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
381 { NULL
, 0, SWIZZLE_NOOP
}
384 const struct input_info
*inputs
385 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
387 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
389 for (i
= 0; inputs
[i
].Name
; i
++) {
390 if (strcmp(inputs
[i
].Name
, name
) == 0) {
392 *swizzleOut
= inputs
[i
].Swizzle
;
393 return inputs
[i
].Attrib
;
401 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
402 * a vertex or fragment program output variable. Return -1 for an invalid
406 _slang_output_index(const char *name
, GLenum target
)
412 static const struct output_info vertOutputs
[] = {
413 { "gl_Position", VERT_RESULT_HPOS
},
414 { "gl_FrontColor", VERT_RESULT_COL0
},
415 { "gl_BackColor", VERT_RESULT_BFC0
},
416 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
417 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
418 { "gl_TexCoord", VERT_RESULT_TEX0
},
419 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
420 { "gl_PointSize", VERT_RESULT_PSIZ
},
423 static const struct output_info fragOutputs
[] = {
424 { "gl_FragColor", FRAG_RESULT_COLOR
},
425 { "gl_FragDepth", FRAG_RESULT_DEPTH
},
426 { "gl_FragData", FRAG_RESULT_DATA0
},
430 const struct output_info
*outputs
431 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
433 for (i
= 0; outputs
[i
].Name
; i
++) {
434 if (strcmp(outputs
[i
].Name
, name
) == 0) {
436 return outputs
[i
].Attrib
;
444 /**********************************************************************/
448 * Map "_asm foo" to IR_FOO, etc.
453 slang_ir_opcode Opcode
;
454 GLuint HaveRetValue
, NumParams
;
458 static slang_asm_info AsmInfo
[] = {
460 { "vec4_add", IR_ADD
, 1, 2 },
461 { "vec4_subtract", IR_SUB
, 1, 2 },
462 { "vec4_multiply", IR_MUL
, 1, 2 },
463 { "vec4_dot", IR_DOT4
, 1, 2 },
464 { "vec3_dot", IR_DOT3
, 1, 2 },
465 { "vec2_dot", IR_DOT2
, 1, 2 },
466 { "vec3_nrm", IR_NRM3
, 1, 1 },
467 { "vec4_nrm", IR_NRM4
, 1, 1 },
468 { "vec3_cross", IR_CROSS
, 1, 2 },
469 { "vec4_lrp", IR_LRP
, 1, 3 },
470 { "vec4_min", IR_MIN
, 1, 2 },
471 { "vec4_max", IR_MAX
, 1, 2 },
472 { "vec4_clamp", IR_CLAMP
, 1, 3 },
473 { "vec4_seq", IR_SEQUAL
, 1, 2 },
474 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
475 { "vec4_sge", IR_SGE
, 1, 2 },
476 { "vec4_sgt", IR_SGT
, 1, 2 },
477 { "vec4_sle", IR_SLE
, 1, 2 },
478 { "vec4_slt", IR_SLT
, 1, 2 },
480 { "vec4_move", IR_MOVE
, 1, 1 },
481 { "vec4_floor", IR_FLOOR
, 1, 1 },
482 { "vec4_frac", IR_FRAC
, 1, 1 },
483 { "vec4_abs", IR_ABS
, 1, 1 },
484 { "vec4_negate", IR_NEG
, 1, 1 },
485 { "vec4_ddx", IR_DDX
, 1, 1 },
486 { "vec4_ddy", IR_DDY
, 1, 1 },
487 /* float binary op */
488 { "float_power", IR_POW
, 1, 2 },
489 /* texture / sampler */
490 { "vec4_tex_1d", IR_TEX
, 1, 2 },
491 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
492 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
493 { "vec4_tex_2d", IR_TEX
, 1, 2 },
494 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
495 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
496 { "vec4_tex_3d", IR_TEX
, 1, 2 },
497 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
498 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
499 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
500 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
501 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
503 /* texture / sampler but with shadow comparison */
504 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
505 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
506 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
507 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
508 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
509 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
510 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
511 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
514 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
515 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
516 { "float_exp", IR_EXP
, 1, 1 },
517 { "float_exp2", IR_EXP2
, 1, 1 },
518 { "float_log2", IR_LOG2
, 1, 1 },
519 { "float_rsq", IR_RSQ
, 1, 1 },
520 { "float_rcp", IR_RCP
, 1, 1 },
521 { "float_sine", IR_SIN
, 1, 1 },
522 { "float_cosine", IR_COS
, 1, 1 },
523 { "float_noise1", IR_NOISE1
, 1, 1},
524 { "float_noise2", IR_NOISE2
, 1, 1},
525 { "float_noise3", IR_NOISE3
, 1, 1},
526 { "float_noise4", IR_NOISE4
, 1, 1},
528 { NULL
, IR_NOP
, 0, 0 }
532 static slang_ir_node
*
533 new_node3(slang_ir_opcode op
,
534 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
536 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
542 n
->InstLocation
= -1;
547 static slang_ir_node
*
548 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
550 return new_node3(op
, c0
, c1
, NULL
);
553 static slang_ir_node
*
554 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
556 return new_node3(op
, c0
, NULL
, NULL
);
559 static slang_ir_node
*
560 new_node0(slang_ir_opcode op
)
562 return new_node3(op
, NULL
, NULL
, NULL
);
567 * Create sequence of two nodes.
569 static slang_ir_node
*
570 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
576 return new_node2(IR_SEQ
, left
, right
);
579 static slang_ir_node
*
580 new_label(slang_label
*label
)
582 slang_ir_node
*n
= new_node0(IR_LABEL
);
589 static slang_ir_node
*
590 new_float_literal(const float v
[4], GLuint size
)
592 slang_ir_node
*n
= new_node0(IR_FLOAT
);
594 COPY_4V(n
->Value
, v
);
595 /* allocate a storage object, but compute actual location (Index) later */
596 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
601 static slang_ir_node
*
602 new_not(slang_ir_node
*n
)
604 return new_node1(IR_NOT
, n
);
609 * Non-inlined function call.
611 static slang_ir_node
*
612 new_function_call(slang_ir_node
*code
, slang_label
*name
)
614 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
623 * Unconditional jump.
625 static slang_ir_node
*
626 new_return(slang_label
*dest
)
628 slang_ir_node
*n
= new_node0(IR_RETURN
);
636 static slang_ir_node
*
637 new_loop(slang_ir_node
*body
)
639 return new_node1(IR_LOOP
, body
);
643 static slang_ir_node
*
644 new_break(slang_ir_node
*loopNode
)
646 slang_ir_node
*n
= new_node0(IR_BREAK
);
648 assert(loopNode
->Opcode
== IR_LOOP
);
650 /* insert this node at head of linked list of cont/break instructions */
651 n
->List
= loopNode
->List
;
659 * Make new IR_BREAK_IF_TRUE.
661 static slang_ir_node
*
662 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
664 slang_ir_node
*loopNode
= A
->CurLoop
;
667 assert(loopNode
->Opcode
== IR_LOOP
);
668 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
670 /* insert this node at head of linked list of cont/break instructions */
671 n
->List
= loopNode
->List
;
679 * Make new IR_CONT_IF_TRUE node.
681 static slang_ir_node
*
682 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
684 slang_ir_node
*loopNode
= A
->CurLoop
;
687 assert(loopNode
->Opcode
== IR_LOOP
);
688 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
690 n
->Parent
= loopNode
; /* pointer to containing loop */
691 /* insert this node at head of linked list of cont/break instructions */
692 n
->List
= loopNode
->List
;
699 static slang_ir_node
*
700 new_cond(slang_ir_node
*n
)
702 slang_ir_node
*c
= new_node1(IR_COND
, n
);
707 static slang_ir_node
*
708 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
710 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
715 * New IR_VAR node - a reference to a previously declared variable.
717 static slang_ir_node
*
718 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
720 slang_ir_node
*n
= new_node0(IR_VAR
);
727 /* Set IR node's Var and Store pointers */
729 n
->Store
= var
->store
;
736 * Check if the given function is really just a wrapper for a
737 * basic assembly instruction.
740 slang_is_asm_function(const slang_function
*fun
)
742 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
743 fun
->body
->num_children
== 1 &&
744 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
752 _slang_is_noop(const slang_operation
*oper
)
755 oper
->type
== SLANG_OPER_VOID
||
756 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
764 * Recursively search tree for a node of the given type.
766 static slang_operation
*
767 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
770 if (oper
->type
== type
)
772 for (i
= 0; i
< oper
->num_children
; i
++) {
773 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
782 * Count the number of operations of the given time rooted at 'oper'.
785 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
788 if (oper
->type
== type
) {
791 for (i
= 0; i
< oper
->num_children
; i
++) {
792 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
799 * Check if the 'return' statement found under 'oper' is a "tail return"
800 * that can be no-op'd. For example:
805 * return; // this is a no-op
808 * This is used when determining if a function can be inlined. If the
809 * 'return' is not the last statement, we can't inline the function since
810 * we still need the semantic behaviour of the 'return' but we don't want
811 * to accidentally return from the _calling_ function. We'd need to use an
812 * unconditional branch, but we don't have such a GPU instruction (not
816 _slang_is_tail_return(const slang_operation
*oper
)
818 GLuint k
= oper
->num_children
;
821 const slang_operation
*last
= &oper
->children
[k
- 1];
822 if (last
->type
== SLANG_OPER_RETURN
)
824 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
825 last
->type
== SLANG_OPER_LABEL
)
826 k
--; /* try prev child */
827 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
828 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
829 /* try sub-children */
830 return _slang_is_tail_return(last
);
840 * Generate a variable declaration opeartion.
841 * I.e.: generate AST code for "bool flag = false;"
844 slang_generate_declaration(slang_assemble_ctx
*A
,
845 slang_variable_scope
*scope
,
846 slang_operation
*decl
,
847 slang_type_specifier_type type
,
853 assert(type
== SLANG_SPEC_BOOL
||
854 type
== SLANG_SPEC_INT
);
856 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
858 var
= slang_variable_scope_grow(scope
);
860 slang_fully_specified_type_construct(&var
->type
);
862 var
->type
.specifier
.type
= type
;
863 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
864 decl
->a_id
= var
->a_name
;
865 var
->initializer
= slang_operation_new(1);
866 slang_operation_literal_bool(var
->initializer
, initValue
);
871 slang_resolve_variable(slang_operation
*oper
)
873 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
874 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
880 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
883 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
884 GLuint substCount
, slang_variable
**substOld
,
885 slang_operation
**substNew
, GLboolean isLHS
)
887 switch (oper
->type
) {
888 case SLANG_OPER_VARIABLE_DECL
:
890 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
891 oper
->a_id
, GL_TRUE
);
893 if (v
->initializer
&& oper
->num_children
== 0) {
894 /* set child of oper to copy of initializer */
895 oper
->num_children
= 1;
896 oper
->children
= slang_operation_new(1);
897 slang_operation_copy(&oper
->children
[0], v
->initializer
);
899 if (oper
->num_children
== 1) {
900 /* the initializer */
901 slang_substitute(A
, &oper
->children
[0], substCount
,
902 substOld
, substNew
, GL_FALSE
);
906 case SLANG_OPER_IDENTIFIER
:
907 assert(oper
->num_children
== 0);
908 if (1/**!isLHS XXX FIX */) {
909 slang_atom id
= oper
->a_id
;
912 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
914 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
918 /* look for a substitution */
919 for (i
= 0; i
< substCount
; i
++) {
920 if (v
== substOld
[i
]) {
921 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
922 #if 0 /* DEBUG only */
923 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
924 assert(substNew
[i
]->var
);
925 assert(substNew
[i
]->var
->a_name
);
926 printf("Substitute %s with %s in id node %p\n",
927 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
931 printf("Substitute %s with %f in id node %p\n",
932 (char*)v
->a_name
, substNew
[i
]->literal
[0],
936 slang_operation_copy(oper
, substNew
[i
]);
943 case SLANG_OPER_RETURN
:
944 /* do return replacement here too */
945 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
946 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
952 * then do substitutions on the assignment.
954 slang_operation
*blockOper
, *assignOper
, *returnOper
;
956 /* check if function actually has a return type */
957 assert(A
->CurFunction
);
958 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
959 slang_info_log_error(A
->log
, "illegal return expression");
963 blockOper
= slang_operation_new(1);
964 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
965 blockOper
->num_children
= 2;
966 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
967 blockOper
->children
= slang_operation_new(2);
968 assignOper
= blockOper
->children
+ 0;
969 returnOper
= blockOper
->children
+ 1;
971 assignOper
->type
= SLANG_OPER_ASSIGN
;
972 assignOper
->num_children
= 2;
973 assignOper
->locals
->outer_scope
= blockOper
->locals
;
974 assignOper
->children
= slang_operation_new(2);
975 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
976 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
977 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
979 slang_operation_copy(&assignOper
->children
[1],
982 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
983 returnOper
->locals
->outer_scope
= blockOper
->locals
;
985 assert(returnOper
->num_children
== 0);
987 /* do substitutions on the "__retVal = expr" sub-tree */
988 slang_substitute(A
, assignOper
,
989 substCount
, substOld
, substNew
, GL_FALSE
);
991 /* install new code */
992 slang_operation_copy(oper
, blockOper
);
993 slang_operation_destruct(blockOper
);
996 /* check if return value was expected */
997 assert(A
->CurFunction
);
998 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
999 slang_info_log_error(A
->log
, "return statement requires an expression");
1005 case SLANG_OPER_ASSIGN
:
1006 case SLANG_OPER_SUBSCRIPT
:
1008 * child[0] can't have substitutions but child[1] can.
1010 slang_substitute(A
, &oper
->children
[0],
1011 substCount
, substOld
, substNew
, GL_TRUE
);
1012 slang_substitute(A
, &oper
->children
[1],
1013 substCount
, substOld
, substNew
, GL_FALSE
);
1015 case SLANG_OPER_FIELD
:
1016 /* XXX NEW - test */
1017 slang_substitute(A
, &oper
->children
[0],
1018 substCount
, substOld
, substNew
, GL_TRUE
);
1023 for (i
= 0; i
< oper
->num_children
; i
++)
1024 slang_substitute(A
, &oper
->children
[i
],
1025 substCount
, substOld
, substNew
, GL_FALSE
);
1032 * Produce inline code for a call to an assembly instruction.
1033 * This is typically used to compile a call to a built-in function like this:
1035 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1037 * __asm vec4_lrp __retVal, a, y, x;
1042 * r = mix(p1, p2, p3);
1052 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1054 static slang_operation
*
1055 slang_inline_asm_function(slang_assemble_ctx
*A
,
1056 slang_function
*fun
, slang_operation
*oper
)
1058 const GLuint numArgs
= oper
->num_children
;
1060 slang_operation
*inlined
;
1061 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1062 slang_variable
**substOld
;
1063 slang_operation
**substNew
;
1065 ASSERT(slang_is_asm_function(fun
));
1066 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1069 printf("Inline %s as %s\n",
1070 (char*) fun->header.a_name,
1071 (char*) fun->body->children[0].a_id);
1075 * We'll substitute formal params with actual args in the asm call.
1077 substOld
= (slang_variable
**)
1078 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1079 substNew
= (slang_operation
**)
1080 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1081 for (i
= 0; i
< numArgs
; i
++) {
1082 substOld
[i
] = fun
->parameters
->variables
[i
];
1083 substNew
[i
] = oper
->children
+ i
;
1086 /* make a copy of the code to inline */
1087 inlined
= slang_operation_new(1);
1088 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1090 /* get rid of the __retVal child */
1091 inlined
->num_children
--;
1092 for (i
= 0; i
< inlined
->num_children
; i
++) {
1093 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1097 /* now do formal->actual substitutions */
1098 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1100 _slang_free(substOld
);
1101 _slang_free(substNew
);
1104 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1105 (char *) fun
->header
.a_name
);
1106 slang_print_tree(inlined
, 3);
1107 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1115 * Inline the given function call operation.
1116 * Return a new slang_operation that corresponds to the inlined code.
1118 static slang_operation
*
1119 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1120 slang_operation
*oper
, slang_operation
*returnOper
)
1127 ParamMode
*paramMode
;
1128 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1129 const GLuint numArgs
= oper
->num_children
;
1130 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1131 slang_operation
*args
= oper
->children
;
1132 slang_operation
*inlined
, *top
;
1133 slang_variable
**substOld
;
1134 slang_operation
**substNew
;
1135 GLuint substCount
, numCopyIn
, i
;
1136 slang_function
*prevFunction
;
1137 slang_variable_scope
*newScope
= NULL
;
1140 prevFunction
= A
->CurFunction
;
1141 A
->CurFunction
= fun
;
1143 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1144 assert(fun
->param_count
== totalArgs
);
1146 /* allocate temporary arrays */
1147 paramMode
= (ParamMode
*)
1148 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1149 substOld
= (slang_variable
**)
1150 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1151 substNew
= (slang_operation
**)
1152 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1155 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1156 (char *) fun
->header
.a_name
,
1157 fun
->parameters
->num_variables
, numArgs
);
1160 if (haveRetValue
&& !returnOper
) {
1161 /* Create 3-child comma sequence for inlined code:
1162 * child[0]: declare __resultTmp
1163 * child[1]: inlined function body
1164 * child[2]: __resultTmp
1166 slang_operation
*commaSeq
;
1167 slang_operation
*declOper
= NULL
;
1168 slang_variable
*resultVar
;
1170 commaSeq
= slang_operation_new(1);
1171 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1172 assert(commaSeq
->locals
);
1173 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1174 commaSeq
->num_children
= 3;
1175 commaSeq
->children
= slang_operation_new(3);
1176 /* allocate the return var */
1177 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1179 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1180 (void*)commaSeq->locals, (char *) fun->header.a_name);
1183 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1184 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1185 resultVar
->isTemp
= GL_TRUE
;
1187 /* child[0] = __resultTmp declaration */
1188 declOper
= &commaSeq
->children
[0];
1189 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1190 declOper
->a_id
= resultVar
->a_name
;
1191 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1193 /* child[1] = function body */
1194 inlined
= &commaSeq
->children
[1];
1195 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1197 /* child[2] = __resultTmp reference */
1198 returnOper
= &commaSeq
->children
[2];
1199 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1200 returnOper
->a_id
= resultVar
->a_name
;
1201 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1206 top
= inlined
= slang_operation_new(1);
1207 /* XXXX this may be inappropriate!!!! */
1208 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1212 assert(inlined
->locals
);
1214 /* Examine the parameters, look for inout/out params, look for possible
1215 * substitutions, etc:
1216 * param type behaviour
1217 * in copy actual to local
1218 * const in substitute param with actual
1222 for (i
= 0; i
< totalArgs
; i
++) {
1223 slang_variable
*p
= fun
->parameters
->variables
[i
];
1225 printf("Param %d: %s %s \n", i,
1226 slang_type_qual_string(p->type.qualifier),
1227 (char *) p->a_name);
1229 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1230 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1231 /* an output param */
1232 slang_operation
*arg
;
1237 paramMode
[i
] = SUBST
;
1239 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1240 slang_resolve_variable(arg
);
1242 /* replace parameter 'p' with argument 'arg' */
1243 substOld
[substCount
] = p
;
1244 substNew
[substCount
] = arg
; /* will get copied */
1247 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1248 /* a constant input param */
1249 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1250 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1251 /* replace all occurances of this parameter variable with the
1252 * actual argument variable or a literal.
1254 paramMode
[i
] = SUBST
;
1255 slang_resolve_variable(&args
[i
]);
1256 substOld
[substCount
] = p
;
1257 substNew
[substCount
] = &args
[i
]; /* will get copied */
1261 paramMode
[i
] = COPY_IN
;
1265 paramMode
[i
] = COPY_IN
;
1267 assert(paramMode
[i
]);
1270 /* actual code inlining: */
1271 slang_operation_copy(inlined
, fun
->body
);
1273 /*** XXX review this */
1274 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1275 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1276 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1279 printf("======================= orig body code ======================\n");
1280 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1281 slang_print_tree(fun
->body
, 8);
1282 printf("======================= copied code =========================\n");
1283 slang_print_tree(inlined
, 8);
1286 /* do parameter substitution in inlined code: */
1287 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1290 printf("======================= subst code ==========================\n");
1291 slang_print_tree(inlined
, 8);
1292 printf("=============================================================\n");
1295 /* New prolog statements: (inserted before the inlined code)
1296 * Copy the 'in' arguments.
1299 for (i
= 0; i
< numArgs
; i
++) {
1300 if (paramMode
[i
] == COPY_IN
) {
1301 slang_variable
*p
= fun
->parameters
->variables
[i
];
1302 /* declare parameter 'p' */
1303 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1307 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1308 assert(decl
->locals
);
1309 decl
->locals
->outer_scope
= inlined
->locals
;
1310 decl
->a_id
= p
->a_name
;
1311 decl
->num_children
= 1;
1312 decl
->children
= slang_operation_new(1);
1314 /* child[0] is the var's initializer */
1315 slang_operation_copy(&decl
->children
[0], args
+ i
);
1317 /* add parameter 'p' to the local variable scope here */
1319 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1320 pCopy
->type
= p
->type
;
1321 pCopy
->a_name
= p
->a_name
;
1322 pCopy
->array_len
= p
->array_len
;
1325 newScope
= inlined
->locals
;
1330 /* Now add copies of the function's local vars to the new variable scope */
1331 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1332 slang_variable
*p
= fun
->parameters
->variables
[i
];
1333 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1334 pCopy
->type
= p
->type
;
1335 pCopy
->a_name
= p
->a_name
;
1336 pCopy
->array_len
= p
->array_len
;
1340 /* New epilog statements:
1341 * 1. Create end of function label to jump to from return statements.
1342 * 2. Copy the 'out' parameter vars
1345 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1347 inlined
->num_children
);
1348 lab
->type
= SLANG_OPER_LABEL
;
1349 lab
->label
= A
->curFuncEndLabel
;
1352 for (i
= 0; i
< totalArgs
; i
++) {
1353 if (paramMode
[i
] == COPY_OUT
) {
1354 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1355 /* actualCallVar = outParam */
1356 /*if (i > 0 || !haveRetValue)*/
1357 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1359 inlined
->num_children
);
1360 ass
->type
= SLANG_OPER_ASSIGN
;
1361 ass
->num_children
= 2;
1362 ass
->locals
->outer_scope
= inlined
->locals
;
1363 ass
->children
= slang_operation_new(2);
1364 ass
->children
[0] = args
[i
]; /*XXX copy */
1365 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1366 ass
->children
[1].a_id
= p
->a_name
;
1367 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1371 _slang_free(paramMode
);
1372 _slang_free(substOld
);
1373 _slang_free(substNew
);
1375 /* Update scoping to use the new local vars instead of the
1376 * original function's vars. This is especially important
1377 * for nested inlining.
1380 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1383 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1384 (char *) fun
->header
.a_name
,
1385 fun
->parameters
->num_variables
, numArgs
);
1386 slang_print_tree(top
, 0);
1390 A
->CurFunction
= prevFunction
;
1397 * Insert declaration for "bool __returnFlag" in given block operation.
1398 * This is used when we can't emit "early" return statements in subroutines.
1401 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1403 slang_operation
*decl
;
1405 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1406 oper
->type
== SLANG_OPER_SEQUENCE
);
1408 decl
= slang_operation_insert_child(oper
, 1);
1410 slang_generate_declaration(A
, oper
->locals
, decl
,
1411 SLANG_SPEC_BOOL
, "__returnFlag", GL_FALSE
);
1413 slang_print_tree(oper
, 0);
1418 * Replace 'return' with '__returnFlag = true'.
1419 * This is used to remove 'early returns' from functions.
1422 replace_return_with_flag_set(slang_assemble_ctx
*A
, slang_operation
*oper
)
1424 slang_atom id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
1425 assert(oper
->type
== SLANG_OPER_RETURN
);
1427 /* replace 'return' with __returnFlag = true' */
1428 slang_operation_free_children(oper
);
1429 oper
->type
= SLANG_OPER_ASSIGN
;
1430 slang_operation_add_children(oper
, 2);
1432 slang_operation
*lhs
= slang_oper_child(oper
, 0);
1433 lhs
->type
= SLANG_OPER_IDENTIFIER
;
1437 slang_operation
*rhs
= slang_oper_child(oper
, 1);
1438 slang_operation_literal_bool(rhs
, GL_TRUE
);
1442 slang_variable
*var
;
1443 var
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
1450 static slang_ir_node
*
1451 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1452 slang_operation
*oper
, slang_operation
*dest
)
1455 slang_operation
*inlined
;
1456 slang_label
*prevFuncEndLabel
;
1459 prevFuncEndLabel
= A
->curFuncEndLabel
;
1460 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1461 A
->curFuncEndLabel
= _slang_label_new(name
);
1462 assert(A
->curFuncEndLabel
);
1464 if (slang_is_asm_function(fun
) && !dest
) {
1465 /* assemble assembly function - tree style */
1466 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1469 /* non-assembly function */
1470 /* We always generate an "inline-able" block of code here.
1472 * 1. insert the inline code
1473 * 2. Generate a call to the "inline" code as a subroutine
1477 slang_operation
*ret
= NULL
;
1479 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1483 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1485 /* check if this is a "tail" return */
1486 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1487 _slang_is_tail_return(inlined
)) {
1488 /* The only RETURN is the last stmt in the function, no-op it
1489 * and inline the function body.
1491 ret
->type
= SLANG_OPER_NONE
;
1494 slang_operation
*callOper
;
1495 /* The function we're calling has one or more 'return' statements.
1496 * So, we can't truly inline this function because we need to
1497 * implement 'return' with RET (and CAL).
1498 * Nevertheless, we performed "inlining" to make a new instance
1499 * of the function body to deal with static register allocation.
1501 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1502 inlined
->type
== SLANG_OPER_SEQUENCE
);
1504 if (_slang_function_has_return_value(fun
) && !dest
) {
1505 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1506 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1507 callOper
= &inlined
->children
[1];
1513 if (!A
->EmitContReturn
) {
1514 /* Early returns not supported. Create a _returnFlag variable
1515 * that's set upon 'return' and tested elsewhere to no-op any
1516 * remaining instructions in the subroutine.
1518 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1519 callOper
->type
== SLANG_OPER_SEQUENCE
);
1520 declare_return_flag(A
, callOper
);
1522 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1523 callOper
->fun
= fun
;
1524 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1532 /* Replace the function call with the inlined block (or new CALL stmt) */
1533 slang_operation_destruct(oper
);
1535 _slang_free(inlined
);
1538 assert(inlined
->locals
);
1539 printf("*** Inlined code for call to %s:\n",
1540 (char*) fun
->header
.a_name
);
1541 slang_print_tree(oper
, 10);
1545 n
= _slang_gen_operation(A
, oper
);
1547 /*_slang_label_delete(A->curFuncEndLabel);*/
1548 A
->curFuncEndLabel
= prevFuncEndLabel
;
1550 if (A
->pragmas
->Debug
) {
1552 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1553 n
->Comment
= _slang_strdup(s
);
1560 static slang_asm_info
*
1561 slang_find_asm_info(const char *name
)
1564 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1565 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1574 * Some write-masked assignments are simple, but others are hard.
1577 * v.xy = vec2(a, b);
1580 * v.zy = vec2(a, b);
1581 * this gets transformed/swizzled into:
1582 * v.zy = vec2(a, b).*yx* (* = don't care)
1583 * This function helps to determine simple vs. non-simple.
1586 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1588 switch (writemask
) {
1590 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1592 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1594 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1596 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1598 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1599 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1601 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1602 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1603 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1604 case WRITEMASK_XYZW
:
1605 return swizzle
== SWIZZLE_NOOP
;
1613 * Convert the given swizzle into a writemask. In some cases this
1614 * is trivial, in other cases, we'll need to also swizzle the right
1615 * hand side to put components in the right places.
1616 * See comment above for more info.
1617 * XXX this function could be simplified and should probably be renamed.
1618 * \param swizzle the incoming swizzle
1619 * \param writemaskOut returns the writemask
1620 * \param swizzleOut swizzle to apply to the right-hand-side
1621 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1624 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1625 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1627 GLuint mask
= 0x0, newSwizzle
[4];
1630 /* make new dst writemask, compute size */
1631 for (i
= 0; i
< 4; i
++) {
1632 const GLuint swz
= GET_SWZ(swizzle
, i
);
1633 if (swz
== SWIZZLE_NIL
) {
1637 assert(swz
>= 0 && swz
<= 3);
1639 if (swizzle
!= SWIZZLE_XXXX
&&
1640 swizzle
!= SWIZZLE_YYYY
&&
1641 swizzle
!= SWIZZLE_ZZZZ
&&
1642 swizzle
!= SWIZZLE_WWWW
&&
1643 (mask
& (1 << swz
))) {
1644 /* a channel can't be specified twice (ex: ".xyyz") */
1645 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1646 _mesa_swizzle_string(swizzle
, 0, 0));
1652 assert(mask
<= 0xf);
1653 size
= i
; /* number of components in mask/swizzle */
1655 *writemaskOut
= mask
;
1657 /* make new src swizzle, by inversion */
1658 for (i
= 0; i
< 4; i
++) {
1659 newSwizzle
[i
] = i
; /*identity*/
1661 for (i
= 0; i
< size
; i
++) {
1662 const GLuint swz
= GET_SWZ(swizzle
, i
);
1663 newSwizzle
[swz
] = i
;
1665 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1670 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1672 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1674 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1676 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1678 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1686 #if 0 /* not used, but don't remove just yet */
1688 * Recursively traverse 'oper' to produce a swizzle mask in the event
1689 * of any vector subscripts and swizzle suffixes.
1690 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1693 resolve_swizzle(const slang_operation
*oper
)
1695 if (oper
->type
== SLANG_OPER_FIELD
) {
1696 /* writemask from .xyzw suffix */
1698 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1699 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1703 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1704 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1708 return SWIZZLE_XYZW
;
1710 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1711 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1712 /* writemask from [index] */
1713 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1714 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1719 swizzle
= SWIZZLE_XXXX
;
1722 swizzle
= SWIZZLE_YYYY
;
1725 swizzle
= SWIZZLE_ZZZZ
;
1728 swizzle
= SWIZZLE_WWWW
;
1731 swizzle
= SWIZZLE_XYZW
;
1733 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1737 return SWIZZLE_XYZW
;
1745 * Recursively descend through swizzle nodes to find the node's storage info.
1747 static slang_ir_storage
*
1748 get_store(const slang_ir_node
*n
)
1750 if (n
->Opcode
== IR_SWIZZLE
) {
1751 return get_store(n
->Children
[0]);
1759 * Generate IR tree for an asm instruction/operation such as:
1760 * __asm vec4_dot __retVal.x, v1, v2;
1762 static slang_ir_node
*
1763 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1764 slang_operation
*dest
)
1766 const slang_asm_info
*info
;
1767 slang_ir_node
*kids
[3], *n
;
1768 GLuint j
, firstOperand
;
1770 assert(oper
->type
== SLANG_OPER_ASM
);
1772 info
= slang_find_asm_info((char *) oper
->a_id
);
1774 _mesa_problem(NULL
, "undefined __asm function %s\n",
1775 (char *) oper
->a_id
);
1778 assert(info
->NumParams
<= 3);
1780 if (info
->NumParams
== oper
->num_children
) {
1781 /* Storage for result is not specified.
1782 * Children[0], [1], [2] are the operands.
1787 /* Storage for result (child[0]) is specified.
1788 * Children[1], [2], [3] are the operands.
1793 /* assemble child(ren) */
1794 kids
[0] = kids
[1] = kids
[2] = NULL
;
1795 for (j
= 0; j
< info
->NumParams
; j
++) {
1796 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1801 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1804 /* Setup n->Store to be a particular location. Otherwise, storage
1805 * for the result (a temporary) will be allocated later.
1807 slang_operation
*dest_oper
;
1810 dest_oper
= &oper
->children
[0];
1812 n0
= _slang_gen_operation(A
, dest_oper
);
1817 n
->Store
= n0
->Store
;
1819 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1830 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1833 for (i
= 0; i
< scope
->num_functions
; i
++) {
1834 slang_function
*f
= &scope
->functions
[i
];
1835 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1836 printf(" %s (%d args)\n", name
, f
->param_count
);
1839 if (scope
->outer_scope
)
1840 print_funcs(scope
->outer_scope
, name
);
1846 * Find a function of the given name, taking 'numArgs' arguments.
1847 * This is the function we'll try to call when there is no exact match
1848 * between function parameters and call arguments.
1850 * XXX we should really create a list of candidate functions and try
1853 static slang_function
*
1854 _slang_find_function_by_argc(slang_function_scope
*scope
,
1855 const char *name
, int numArgs
)
1859 for (i
= 0; i
< scope
->num_functions
; i
++) {
1860 slang_function
*f
= &scope
->functions
[i
];
1861 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1862 int haveRetValue
= _slang_function_has_return_value(f
);
1863 if (numArgs
== f
->param_count
- haveRetValue
)
1867 scope
= scope
->outer_scope
;
1874 static slang_function
*
1875 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1878 slang_function
*maxFunc
= NULL
;
1883 for (i
= 0; i
< scope
->num_functions
; i
++) {
1884 slang_function
*f
= &scope
->functions
[i
];
1885 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1886 if (f
->param_count
> maxArgs
) {
1887 maxArgs
= f
->param_count
;
1892 scope
= scope
->outer_scope
;
1900 * Generate a new slang_function which is a constructor for a user-defined
1903 static slang_function
*
1904 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1906 const GLint numFields
= str
->fields
->num_variables
;
1907 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1909 /* function header (name, return type) */
1910 fun
->header
.a_name
= str
->a_name
;
1911 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1912 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1913 fun
->header
.type
.specifier
._struct
= str
;
1915 /* function parameters (= struct's fields) */
1918 for (i
= 0; i
< numFields
; i
++) {
1920 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1922 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1923 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1924 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1926 fun
->param_count
= fun
->parameters
->num_variables
;
1929 /* Add __retVal to params */
1931 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1932 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1934 p
->a_name
= a_retVal
;
1935 p
->type
= fun
->header
.type
;
1936 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1940 /* function body is:
1950 slang_variable_scope
*scope
;
1951 slang_variable
*var
;
1954 fun
->body
= slang_operation_new(1);
1955 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1956 fun
->body
->num_children
= numFields
+ 2;
1957 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1959 scope
= fun
->body
->locals
;
1960 scope
->outer_scope
= fun
->parameters
;
1962 /* create local var 't' */
1963 var
= slang_variable_scope_grow(scope
);
1964 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1965 var
->type
= fun
->header
.type
;
1969 slang_operation
*decl
;
1971 decl
= &fun
->body
->children
[0];
1972 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1973 decl
->locals
= _slang_variable_scope_new(scope
);
1974 decl
->a_id
= var
->a_name
;
1977 /* assign params to fields of t */
1978 for (i
= 0; i
< numFields
; i
++) {
1979 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1981 assign
->type
= SLANG_OPER_ASSIGN
;
1982 assign
->locals
= _slang_variable_scope_new(scope
);
1983 assign
->num_children
= 2;
1984 assign
->children
= slang_operation_new(2);
1987 slang_operation
*lhs
= &assign
->children
[0];
1989 lhs
->type
= SLANG_OPER_FIELD
;
1990 lhs
->locals
= _slang_variable_scope_new(scope
);
1991 lhs
->num_children
= 1;
1992 lhs
->children
= slang_operation_new(1);
1993 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1995 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1996 lhs
->children
[0].a_id
= var
->a_name
;
1997 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2000 lhs
->children
[1].num_children
= 1;
2001 lhs
->children
[1].children
= slang_operation_new(1);
2002 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
2003 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
2004 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
2009 slang_operation
*rhs
= &assign
->children
[1];
2011 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2012 rhs
->locals
= _slang_variable_scope_new(scope
);
2013 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
2019 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
2021 ret
->type
= SLANG_OPER_RETURN
;
2022 ret
->locals
= _slang_variable_scope_new(scope
);
2023 ret
->num_children
= 1;
2024 ret
->children
= slang_operation_new(1);
2025 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2026 ret
->children
[0].a_id
= var
->a_name
;
2027 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2031 slang_print_function(fun, 1);
2038 * Find/create a function (constructor) for the given structure name.
2040 static slang_function
*
2041 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2044 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2045 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2046 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2047 /* found a structure type that matches the function name */
2048 if (!str
->constructor
) {
2049 /* create the constructor function now */
2050 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2052 return str
->constructor
;
2060 * Generate a new slang_function to satisfy a call to an array constructor.
2061 * Ex: float[3](1., 2., 3.)
2063 static slang_function
*
2064 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2066 slang_type_specifier_type baseType
;
2067 slang_function
*fun
;
2070 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2074 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2076 num_elements
= oper
->num_children
;
2078 /* function header, return type */
2080 fun
->header
.a_name
= oper
->a_id
;
2081 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2082 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2083 fun
->header
.type
.specifier
._array
=
2084 slang_type_specifier_new(baseType
, NULL
, NULL
);
2085 fun
->header
.type
.array_len
= num_elements
;
2088 /* function parameters (= number of elements) */
2091 for (i
= 0; i
< num_elements
; i
++) {
2093 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2095 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2097 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2098 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2099 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2100 p
->type
.specifier
.type
= baseType
;
2102 fun
->param_count
= fun
->parameters
->num_variables
;
2105 /* Add __retVal to params */
2107 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2108 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2110 p
->a_name
= a_retVal
;
2111 p
->type
= fun
->header
.type
;
2112 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2113 p
->type
.specifier
.type
= baseType
;
2117 /* function body is:
2127 slang_variable_scope
*scope
;
2128 slang_variable
*var
;
2131 fun
->body
= slang_operation_new(1);
2132 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2133 fun
->body
->num_children
= num_elements
+ 2;
2134 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2136 scope
= fun
->body
->locals
;
2137 scope
->outer_scope
= fun
->parameters
;
2139 /* create local var 't' */
2140 var
= slang_variable_scope_grow(scope
);
2141 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2142 var
->type
= fun
->header
.type
;/*XXX copy*/
2146 slang_operation
*decl
;
2148 decl
= &fun
->body
->children
[0];
2149 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2150 decl
->locals
= _slang_variable_scope_new(scope
);
2151 decl
->a_id
= var
->a_name
;
2154 /* assign params to elements of t */
2155 for (i
= 0; i
< num_elements
; i
++) {
2156 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2158 assign
->type
= SLANG_OPER_ASSIGN
;
2159 assign
->locals
= _slang_variable_scope_new(scope
);
2160 assign
->num_children
= 2;
2161 assign
->children
= slang_operation_new(2);
2164 slang_operation
*lhs
= &assign
->children
[0];
2166 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2167 lhs
->locals
= _slang_variable_scope_new(scope
);
2168 lhs
->num_children
= 2;
2169 lhs
->children
= slang_operation_new(2);
2171 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2172 lhs
->children
[0].a_id
= var
->a_name
;
2173 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2175 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2176 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2180 slang_operation
*rhs
= &assign
->children
[1];
2182 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2183 rhs
->locals
= _slang_variable_scope_new(scope
);
2184 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2190 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2192 ret
->type
= SLANG_OPER_RETURN
;
2193 ret
->locals
= _slang_variable_scope_new(scope
);
2194 ret
->num_children
= 1;
2195 ret
->children
= slang_operation_new(1);
2196 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2197 ret
->children
[0].a_id
= var
->a_name
;
2198 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2203 slang_print_function(fun, 1);
2211 _slang_is_vec_mat_type(const char *name
)
2213 static const char *vecmat_types
[] = {
2214 "float", "int", "bool",
2215 "vec2", "vec3", "vec4",
2216 "ivec2", "ivec3", "ivec4",
2217 "bvec2", "bvec3", "bvec4",
2218 "mat2", "mat3", "mat4",
2219 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2223 for (i
= 0; vecmat_types
[i
]; i
++)
2224 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2231 * Assemble a function call, given a particular function name.
2232 * \param name the function's name (operators like '*' are possible).
2234 static slang_ir_node
*
2235 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2236 slang_operation
*oper
, slang_operation
*dest
)
2238 slang_operation
*params
= oper
->children
;
2239 const GLuint param_count
= oper
->num_children
;
2241 slang_function
*fun
;
2244 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2245 if (atom
== SLANG_ATOM_NULL
)
2248 if (oper
->array_constructor
) {
2249 /* this needs special handling */
2250 fun
= _slang_make_array_constructor(A
, oper
);
2253 /* Try to find function by name and exact argument type matching */
2254 GLboolean error
= GL_FALSE
;
2255 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2256 &A
->space
, A
->atoms
, A
->log
, &error
);
2258 slang_info_log_error(A
->log
,
2259 "Function '%s' not found (check argument types)",
2266 /* Next, try locating a constructor function for a user-defined type */
2267 fun
= _slang_locate_struct_constructor(A
, name
);
2271 * At this point, some heuristics are used to try to find a function
2272 * that matches the calling signature by means of casting or "unrolling"
2276 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2277 /* Next, if this call looks like a vec() or mat() constructor call,
2278 * try "unwinding" the args to satisfy a constructor.
2280 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2282 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2283 slang_info_log_error(A
->log
,
2284 "Function '%s' not found (check argument types)",
2291 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2292 /* Next, try casting args to the types of the formal parameters */
2293 int numArgs
= oper
->num_children
;
2294 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2295 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2296 slang_info_log_error(A
->log
,
2297 "Function '%s' not found (check argument types)",
2305 slang_info_log_error(A
->log
,
2306 "Function '%s' not found (check argument types)",
2312 /* The function body may be in another compilation unit.
2313 * We'll try concatenating the shaders and recompile at link time.
2315 A
->UnresolvedRefs
= GL_TRUE
;
2316 return new_node1(IR_NOP
, NULL
);
2319 /* type checking to be sure function's return type matches 'dest' type */
2323 slang_typeinfo_construct(&t0
);
2324 typeof_operation(A
, dest
, &t0
);
2326 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2327 slang_info_log_error(A
->log
,
2328 "Incompatible type returned by call to '%s'",
2334 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2336 if (n
&& !n
->Store
&& !dest
2337 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2338 /* setup n->Store for the result of the function call */
2339 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2340 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2341 /*printf("Alloc storage for function result, size %d \n", size);*/
2344 if (oper
->array_constructor
) {
2345 /* free the temporary array constructor function now */
2346 slang_function_destruct(fun
);
2353 static slang_ir_node
*
2354 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2356 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2358 slang_variable
*var
;
2360 /* NOTE: In GLSL 1.20, there's only one kind of method
2361 * call: array.length(). Anything else is an error.
2363 if (oper
->a_id
!= a_length
) {
2364 slang_info_log_error(A
->log
,
2365 "Undefined method call '%s'", (char *) oper
->a_id
);
2369 /* length() takes no arguments */
2370 if (oper
->num_children
> 0) {
2371 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2375 /* lookup the object/variable */
2376 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2377 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2378 slang_info_log_error(A
->log
,
2379 "Undefined object '%s'", (char *) oper
->a_obj
);
2383 /* Create a float/literal IR node encoding the array length */
2384 n
= new_node0(IR_FLOAT
);
2386 n
->Value
[0] = (float) _slang_array_length(var
);
2387 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2394 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2396 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2397 oper
->type
== SLANG_OPER_LITERAL_INT
||
2398 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2399 if (oper
->literal
[0])
2405 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2406 oper
->num_children
== 1) {
2407 return _slang_is_constant_cond(&oper
->children
[0], value
);
2414 * Test if an operation is a scalar or boolean.
2417 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2419 slang_typeinfo type
;
2422 slang_typeinfo_construct(&type
);
2423 typeof_operation(A
, oper
, &type
);
2424 size
= _slang_sizeof_type_specifier(&type
.spec
);
2425 slang_typeinfo_destruct(&type
);
2431 * Test if an operation is boolean.
2434 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2436 slang_typeinfo type
;
2439 slang_typeinfo_construct(&type
);
2440 typeof_operation(A
, oper
, &type
);
2441 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2442 slang_typeinfo_destruct(&type
);
2448 * Check if a loop contains a 'continue' statement.
2449 * Stop looking if we find a nested loop.
2452 _slang_loop_contains_continue(const slang_operation
*oper
)
2454 switch (oper
->type
) {
2455 case SLANG_OPER_CONTINUE
:
2457 case SLANG_OPER_FOR
:
2459 case SLANG_OPER_WHILE
:
2460 /* stop upon finding a nested loop */
2466 for (i
= 0; i
< oper
->num_children
; i
++) {
2467 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2468 if (_slang_loop_contains_continue(child
))
2478 * Check if a loop contains a 'continue' or 'break' statement.
2479 * Stop looking if we find a nested loop.
2482 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2484 switch (oper
->type
) {
2485 case SLANG_OPER_CONTINUE
:
2486 case SLANG_OPER_BREAK
:
2488 case SLANG_OPER_FOR
:
2490 case SLANG_OPER_WHILE
:
2491 /* stop upon finding a nested loop */
2497 for (i
= 0; i
< oper
->num_children
; i
++) {
2498 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2499 if (_slang_loop_contains_continue_or_break(child
))
2509 * Replace 'break' and 'continue' statements inside a do and while loops.
2510 * This is a recursive helper function used by
2511 * _slang_gen_do/while_without_continue().
2514 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2516 switch (oper
->type
) {
2517 case SLANG_OPER_BREAK
:
2518 /* replace 'break' with "_notBreakFlag = false; break" */
2520 slang_operation
*block
= oper
;
2521 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2522 slang_operation_add_children(block
, 2);
2524 slang_operation
*assign
= slang_oper_child(block
, 0);
2525 assign
->type
= SLANG_OPER_ASSIGN
;
2526 slang_operation_add_children(assign
, 2);
2528 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2529 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2532 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2533 slang_operation_literal_bool(rhs
, GL_FALSE
);
2537 slang_operation
*brk
= slang_oper_child(block
, 1);
2538 brk
->type
= SLANG_OPER_BREAK
;
2539 assert(!brk
->children
);
2543 case SLANG_OPER_CONTINUE
:
2544 /* convert continue into a break */
2545 oper
->type
= SLANG_OPER_BREAK
;
2547 case SLANG_OPER_FOR
:
2549 case SLANG_OPER_WHILE
:
2550 /* stop upon finding a nested loop */
2556 for (i
= 0; i
< oper
->num_children
; i
++) {
2557 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2565 * Transform a while-loop so that continue statements are converted to breaks.
2566 * Then do normal IR code generation.
2570 * while (LOOPCOND) {
2582 * bool _notBreakFlag = 1;
2583 * while (_notBreakFlag && LOOPCOND) {
2587 * break; // was continue
2590 * _notBreakFlag = 0; // was
2597 static slang_ir_node
*
2598 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2600 slang_operation
*top
;
2601 slang_operation
*innerBody
;
2603 assert(oper
->type
== SLANG_OPER_WHILE
);
2605 top
= slang_operation_new(1);
2606 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2607 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2608 slang_operation_add_children(top
, 2);
2610 /* declare: bool _notBreakFlag = true */
2612 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2613 slang_generate_declaration(A
, top
->locals
, condDecl
,
2614 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2617 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2619 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2620 outerWhile
->type
= SLANG_OPER_WHILE
;
2621 slang_operation_add_children(outerWhile
, 2);
2623 /* _notBreakFlag && LOOPCOND */
2625 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2626 cond
->type
= SLANG_OPER_LOGICALAND
;
2627 slang_operation_add_children(cond
, 2);
2629 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2630 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2633 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2634 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2640 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2641 innerDo
->type
= SLANG_OPER_DO
;
2642 slang_operation_add_children(innerDo
, 2);
2644 /* copy original do-loop body into inner do-loop's body */
2645 innerBody
= slang_oper_child(innerDo
, 0);
2646 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2647 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2649 /* inner do-loop's condition is constant/false */
2651 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2652 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2657 /* Finally, in innerBody,
2658 * replace "break" with "_notBreakFlag = 0; break"
2659 * replace "continue" with "break"
2661 replace_break_and_cont(A
, innerBody
);
2663 /*slang_print_tree(top, 0);*/
2665 return _slang_gen_operation(A
, top
);
2672 * Generate loop code using high-level IR_LOOP instruction
2674 static slang_ir_node
*
2675 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2679 * BREAK if !expr (child[0])
2680 * body code (child[1])
2682 const slang_operation
*prevLoopOper
;
2683 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2684 GLboolean isConst
, constTrue
;
2686 if (!A
->EmitContReturn
) {
2687 /* We don't want to emit CONT instructions. If this while-loop has
2688 * a continue, translate it away.
2690 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2691 return _slang_gen_while_without_continue(A
, oper
);
2695 /* type-check expression */
2696 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2697 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2701 /* Check if loop condition is a constant */
2702 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2704 if (isConst
&& !constTrue
) {
2705 /* loop is never executed! */
2706 return new_node0(IR_NOP
);
2709 loop
= new_loop(NULL
);
2711 /* save old, push new loop */
2712 prevLoop
= A
->CurLoop
;
2714 prevLoopOper
= A
->CurLoopOper
;
2715 A
->CurLoopOper
= oper
;
2717 if (isConst
&& constTrue
) {
2718 /* while(nonzero constant), no conditional break */
2723 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2724 breakIf
= new_break_if_true(A
, cond
);
2726 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2727 loop
->Children
[0] = new_seq(breakIf
, body
);
2729 /* Do infinite loop detection */
2730 /* loop->List is head of linked list of break/continue nodes */
2731 if (!loop
->List
&& isConst
&& constTrue
) {
2732 /* infinite loop detected */
2733 A
->CurLoop
= prevLoop
; /* clean-up */
2734 slang_info_log_error(A
->log
, "Infinite loop detected!");
2738 /* pop loop, restore prev */
2739 A
->CurLoop
= prevLoop
;
2740 A
->CurLoopOper
= prevLoopOper
;
2747 * Transform a do-while-loop so that continue statements are converted to breaks.
2748 * Then do normal IR code generation.
2759 * } while (LOOPCOND);
2764 * bool _notBreakFlag = 1;
2769 * break; // was continue
2772 * _notBreakFlag = 0; // was
2776 * } while (_notBreakFlag && LOOPCOND);
2779 static slang_ir_node
*
2780 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2782 slang_operation
*top
;
2783 slang_operation
*innerBody
;
2785 assert(oper
->type
== SLANG_OPER_DO
);
2787 top
= slang_operation_new(1);
2788 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2789 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2790 slang_operation_add_children(top
, 2);
2792 /* declare: bool _notBreakFlag = true */
2794 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2795 slang_generate_declaration(A
, top
->locals
, condDecl
,
2796 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2799 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2801 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2802 outerDo
->type
= SLANG_OPER_DO
;
2803 slang_operation_add_children(outerDo
, 2);
2807 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2808 innerDo
->type
= SLANG_OPER_DO
;
2809 slang_operation_add_children(innerDo
, 2);
2811 /* copy original do-loop body into inner do-loop's body */
2812 innerBody
= slang_oper_child(innerDo
, 0);
2813 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2814 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2816 /* inner do-loop's condition is constant/false */
2818 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2819 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2823 /* _notBreakFlag && LOOPCOND */
2825 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2826 cond
->type
= SLANG_OPER_LOGICALAND
;
2827 slang_operation_add_children(cond
, 2);
2829 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2830 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2833 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2834 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2839 /* Finally, in innerBody,
2840 * replace "break" with "_notBreakFlag = 0; break"
2841 * replace "continue" with "break"
2843 replace_break_and_cont(A
, innerBody
);
2845 /*slang_print_tree(top, 0);*/
2847 return _slang_gen_operation(A
, top
);
2852 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2854 static slang_ir_node
*
2855 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2859 * body code (child[0])
2861 * BREAK if !expr (child[1])
2863 const slang_operation
*prevLoopOper
;
2864 slang_ir_node
*prevLoop
, *loop
;
2865 GLboolean isConst
, constTrue
;
2867 if (!A
->EmitContReturn
) {
2868 /* We don't want to emit CONT instructions. If this do-loop has
2869 * a continue, translate it away.
2871 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2872 return _slang_gen_do_without_continue(A
, oper
);
2876 /* type-check expression */
2877 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2878 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2882 loop
= new_loop(NULL
);
2884 /* save old, push new loop */
2885 prevLoop
= A
->CurLoop
;
2887 prevLoopOper
= A
->CurLoopOper
;
2888 A
->CurLoopOper
= oper
;
2891 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2893 /* Check if loop condition is a constant */
2894 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2895 if (isConst
&& constTrue
) {
2896 /* do { } while(1) ==> no conditional break */
2897 loop
->Children
[1] = NULL
; /* no tail code */
2901 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2902 loop
->Children
[1] = new_break_if_true(A
, cond
);
2905 /* XXX we should do infinite loop detection, as above */
2907 /* pop loop, restore prev */
2908 A
->CurLoop
= prevLoop
;
2909 A
->CurLoopOper
= prevLoopOper
;
2916 * Recursively count the number of operations rooted at 'oper'.
2917 * This gives some kind of indication of the size/complexity of an operation.
2920 sizeof_operation(const slang_operation
*oper
)
2923 GLuint count
= 1; /* me */
2925 for (i
= 0; i
< oper
->num_children
; i
++) {
2926 count
+= sizeof_operation(&oper
->children
[i
]);
2937 * Determine if a for-loop can be unrolled.
2938 * At this time, only a rather narrow class of for loops can be unrolled.
2939 * See code for details.
2940 * When a loop can't be unrolled because it's too large we'll emit a
2941 * message to the log.
2944 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2948 const char *varName
;
2951 if (oper
->type
!= SLANG_OPER_FOR
)
2954 assert(oper
->num_children
== 4);
2956 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
2959 /* children[0] must be either "int i=constant" or "i=constant" */
2960 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2961 slang_variable
*var
;
2963 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2966 varId
= oper
->children
[0].children
[0].a_id
;
2968 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2972 if (!var
->initializer
)
2974 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2976 start
= (GLint
) var
->initializer
->literal
[0];
2978 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2979 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2981 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2983 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2986 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2988 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2994 /* children[1] must be "i<constant" */
2995 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2997 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2999 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3001 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
3004 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3006 /* children[2] must be "i++" or "++i" */
3007 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
3008 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
3010 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
3013 /* make sure the same variable name is used in all places */
3014 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
3015 (oper
->children
[2].children
[0].a_id
!= varId
))
3018 varName
= (const char *) varId
;
3020 /* children[3], the loop body, can't be too large */
3021 bodySize
= sizeof_operation(&oper
->children
[3]);
3022 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
3023 slang_info_log_print(A
->log
,
3024 "Note: 'for (%s ... )' body is too large/complex"
3031 return GL_FALSE
; /* degenerate case */
3033 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3034 slang_info_log_print(A
->log
,
3035 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3036 " many iterations to unroll",
3037 varName
, start
, varName
, end
, varName
);
3041 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3042 slang_info_log_print(A
->log
,
3043 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3044 " too much code to unroll",
3045 varName
, start
, varName
, end
, varName
);
3049 return GL_TRUE
; /* we can unroll the loop */
3054 * Unroll a for-loop.
3055 * First we determine the number of iterations to unroll.
3056 * Then for each iteration:
3057 * make a copy of the loop body
3058 * replace instances of the loop variable with the current iteration value
3059 * generate IR code for the body
3060 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3062 static slang_ir_node
*
3063 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3065 GLint start
, end
, iter
;
3066 slang_ir_node
*n
, *root
= NULL
;
3069 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3070 /* for (int i=0; ... */
3071 slang_variable
*var
;
3073 varId
= oper
->children
[0].children
[0].a_id
;
3074 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3076 start
= (GLint
) var
->initializer
->literal
[0];
3080 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3081 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3084 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3086 for (iter
= start
; iter
< end
; iter
++) {
3087 slang_operation
*body
;
3089 /* make a copy of the loop body */
3090 body
= slang_operation_new(1);
3094 if (!slang_operation_copy(body
, &oper
->children
[3]))
3097 /* in body, replace instances of 'varId' with literal 'iter' */
3099 slang_variable
*oldVar
;
3100 slang_operation
*newOper
;
3102 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3104 /* undeclared loop variable */
3105 slang_operation_delete(body
);
3109 newOper
= slang_operation_new(1);
3110 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3111 newOper
->literal_size
= 1;
3112 newOper
->literal
[0] = iter
;
3114 /* replace instances of the loop variable with newOper */
3115 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3118 /* do IR codegen for body */
3119 n
= _slang_gen_operation(A
, body
);
3123 root
= new_seq(root
, n
);
3125 slang_operation_delete(body
);
3133 * Replace 'continue' statement with 'break' inside a for-loop.
3134 * This is a recursive helper function used by _slang_gen_for_without_continue().
3137 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3139 switch (oper
->type
) {
3140 case SLANG_OPER_CONTINUE
:
3141 oper
->type
= SLANG_OPER_BREAK
;
3143 case SLANG_OPER_FOR
:
3145 case SLANG_OPER_WHILE
:
3146 /* stop upon finding a nested loop */
3152 for (i
= 0; i
< oper
->num_children
; i
++) {
3153 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3161 * Transform a for-loop so that continue statements are converted to breaks.
3162 * Then do normal IR code generation.
3166 * for (INIT; LOOPCOND; INCR) {
3177 * bool _condFlag = 1;
3178 * for (INIT; _condFlag; ) {
3179 * for ( ; _condFlag = LOOPCOND; INCR) {
3191 static slang_ir_node
*
3192 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3194 slang_operation
*top
;
3195 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3196 slang_operation
*lhs
, *rhs
;
3198 assert(oper
->type
== SLANG_OPER_FOR
);
3200 top
= slang_operation_new(1);
3201 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3202 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3203 slang_operation_add_children(top
, 2);
3205 /* declare: bool _condFlag = true */
3207 slang_operation
*condDecl
= slang_oper_child(top
, 0);
3208 slang_generate_declaration(A
, top
->locals
, condDecl
,
3209 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3212 /* build outer loop: for (INIT; _condFlag; ) { */
3213 outerFor
= slang_oper_child(top
, 1);
3214 outerFor
->type
= SLANG_OPER_FOR
;
3215 slang_operation_add_children(outerFor
, 4);
3217 init
= slang_oper_child(outerFor
, 0);
3218 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3220 cond
= slang_oper_child(outerFor
, 1);
3221 cond
->type
= SLANG_OPER_IDENTIFIER
;
3222 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3224 incr
= slang_oper_child(outerFor
, 2);
3225 incr
->type
= SLANG_OPER_VOID
;
3227 /* body of the outer loop */
3229 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3231 slang_operation_add_children(block
, 2);
3232 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3234 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3236 innerFor
= slang_oper_child(block
, 0);
3238 /* make copy of orig loop */
3239 slang_operation_copy(innerFor
, oper
);
3240 assert(innerFor
->type
== SLANG_OPER_FOR
);
3241 innerFor
->locals
->outer_scope
= block
->locals
;
3243 init
= slang_oper_child(innerFor
, 0);
3244 init
->type
= SLANG_OPER_VOID
; /* leak? */
3246 cond
= slang_oper_child(innerFor
, 1);
3247 slang_operation_destruct(cond
);
3248 cond
->type
= SLANG_OPER_ASSIGN
;
3249 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3250 slang_operation_add_children(cond
, 2);
3252 lhs
= slang_oper_child(cond
, 0);
3253 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3254 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3256 rhs
= slang_oper_child(cond
, 1);
3257 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3260 /* if (_condFlag) INCR; */
3262 slang_operation
*ifop
= slang_oper_child(block
, 1);
3263 ifop
->type
= SLANG_OPER_IF
;
3264 slang_operation_add_children(ifop
, 2);
3266 /* re-use cond node build above */
3267 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3269 /* incr node from original for-loop operation */
3270 slang_operation_copy(slang_oper_child(ifop
, 1),
3271 slang_oper_child(oper
, 2));
3274 /* finally, replace "continue" with "break" in the inner for-loop */
3275 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3278 return _slang_gen_operation(A
, top
);
3284 * Generate IR for a for-loop. Unrolling will be done when possible.
3286 static slang_ir_node
*
3287 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3291 if (!A
->EmitContReturn
) {
3292 /* We don't want to emit CONT instructions. If this for-loop has
3293 * a continue, translate it away.
3295 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3296 return _slang_gen_for_without_continue(A
, oper
);
3300 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3302 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3307 assert(oper
->type
== SLANG_OPER_FOR
);
3309 /* conventional for-loop code generation */
3312 * init code (child[0])
3314 * BREAK if !expr (child[1])
3315 * body code (child[3])
3317 * incr code (child[2]) // XXX continue here
3319 const slang_operation
*prevLoopOper
;
3320 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3321 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3322 loop
= new_loop(NULL
);
3324 /* save old, push new loop */
3325 prevLoop
= A
->CurLoop
;
3327 prevLoopOper
= A
->CurLoopOper
;
3328 A
->CurLoopOper
= oper
;
3330 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3331 breakIf
= new_break_if_true(A
, cond
);
3332 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3333 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3335 loop
->Children
[0] = new_seq(breakIf
, body
);
3336 loop
->Children
[1] = incr
; /* tail code */
3338 /* pop loop, restore prev */
3339 A
->CurLoop
= prevLoop
;
3340 A
->CurLoopOper
= prevLoopOper
;
3342 return new_seq(init
, loop
);
3347 static slang_ir_node
*
3348 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3350 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3352 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3353 loopNode
= A
->CurLoop
;
3355 assert(loopNode
->Opcode
== IR_LOOP
);
3357 cont
= new_node0(IR_CONT
);
3359 cont
->Parent
= loopNode
;
3360 /* insert this node at head of linked list of cont/break instructions */
3361 cont
->List
= loopNode
->List
;
3362 loopNode
->List
= cont
;
3365 n
= new_seq(incr
, cont
);
3371 * Determine if the given operation is of a specific type.
3374 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3376 if (oper
->type
== type
)
3378 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3379 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3380 oper
->num_children
== 1)
3381 return is_operation_type(&oper
->children
[0], type
);
3388 * Generate IR tree for an if/then/else conditional using high-level
3389 * IR_IF instruction.
3391 static slang_ir_node
*
3392 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3395 * eval expr (child[0])
3402 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3403 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3404 GLboolean isConst
, constTrue
;
3406 /* type-check expression */
3407 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3408 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3412 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3413 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3417 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3421 return _slang_gen_operation(A
, &oper
->children
[1]);
3424 /* if (false) ... */
3425 return _slang_gen_operation(A
, &oper
->children
[2]);
3429 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3430 cond
= new_cond(cond
);
3432 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3433 && !haveElseClause
) {
3434 /* Special case: generate a conditional break */
3435 if (!A
->CurLoop
) /* probably trying to unroll */
3437 ifBody
= new_break_if_true(A
, cond
);
3440 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3443 && A
->CurLoopOper
->type
!= SLANG_OPER_FOR
) {
3444 /* Special case: generate a conditional continue */
3445 if (!A
->CurLoop
) /* probably trying to unroll */
3447 ifBody
= new_cont_if_true(A
, cond
);
3452 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3454 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3457 ifNode
= new_if(cond
, ifBody
, elseBody
);
3464 static slang_ir_node
*
3465 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3469 assert(oper
->type
== SLANG_OPER_NOT
);
3471 /* type-check expression */
3472 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3473 slang_info_log_error(A
->log
,
3474 "scalar/boolean expression expected for '!'");
3478 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3486 static slang_ir_node
*
3487 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3489 slang_ir_node
*n1
, *n2
;
3491 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3493 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3494 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3495 slang_info_log_error(A
->log
,
3496 "scalar/boolean expressions expected for '^^'");
3500 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3503 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3506 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3511 * Generate IR node for storage of a temporary of given size.
3513 static slang_ir_node
*
3514 _slang_gen_temporary(GLint size
)
3516 slang_ir_storage
*store
;
3517 slang_ir_node
*n
= NULL
;
3519 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3521 n
= new_node0(IR_VAR_DECL
);
3534 * Generate program constants for an array.
3535 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3536 * This will allocate and initialize three vector constants, storing
3537 * the array in constant memory, not temporaries like a non-const array.
3538 * This can also be used for uniform array initializers.
3539 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3542 make_constant_array(slang_assemble_ctx
*A
,
3543 slang_variable
*var
,
3544 slang_operation
*initializer
)
3546 struct gl_program
*prog
= A
->program
;
3547 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3548 const char *varName
= (char *) var
->a_name
;
3549 const GLuint numElements
= initializer
->num_children
;
3555 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3557 size
= var
->store
->Size
;
3559 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3560 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3561 assert(initializer
->type
== SLANG_OPER_CALL
);
3562 assert(initializer
->array_constructor
);
3564 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3566 /* convert constructor params into ordinary floats */
3567 for (i
= 0; i
< numElements
; i
++) {
3568 const slang_operation
*op
= &initializer
->children
[i
];
3569 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3570 /* unsupported type for this optimization */
3574 for (j
= 0; j
< op
->literal_size
; j
++) {
3575 values
[i
* 4 + j
] = op
->literal
[j
];
3577 for ( ; j
< 4; j
++) {
3578 values
[i
* 4 + j
] = 0.0f
;
3582 /* slightly different paths for constants vs. uniforms */
3583 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3584 var
->store
->File
= PROGRAM_UNIFORM
;
3585 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3586 size
, datatype
, values
);
3589 var
->store
->File
= PROGRAM_CONSTANT
;
3590 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3593 assert(var
->store
->Size
== size
);
3603 * Generate IR node for allocating/declaring a variable (either a local or
3605 * Generally, this involves allocating an slang_ir_storage instance for the
3606 * variable, choosing a register file (temporary, constant, etc).
3607 * For ordinary variables we do not yet allocate storage though. We do that
3608 * when we find the first actual use of the variable to avoid allocating temp
3609 * regs that will never get used.
3610 * At this time, uniforms are always allocated space in this function.
3612 * \param initializer Optional initializer expression for the variable.
3614 static slang_ir_node
*
3615 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3616 slang_operation
*initializer
)
3618 const char *varName
= (const char *) var
->a_name
;
3619 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3620 slang_ir_node
*varDecl
, *n
;
3621 slang_ir_storage
*store
;
3622 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3623 gl_register_file file
;
3625 /*assert(!var->declared);*/
3626 var
->declared
= GL_TRUE
;
3628 /* determine GPU register file for simple cases */
3629 if (is_sampler_type(&var
->type
)) {
3630 file
= PROGRAM_SAMPLER
;
3632 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3633 file
= PROGRAM_UNIFORM
;
3636 file
= PROGRAM_TEMPORARY
;
3639 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3641 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3645 arrayLen
= _slang_array_length(var
);
3646 totalSize
= _slang_array_size(size
, arrayLen
);
3648 /* Allocate IR node for the declaration */
3649 varDecl
= new_node0(IR_VAR_DECL
);
3653 /* Allocate slang_ir_storage for this variable if needed.
3654 * Note that we may not actually allocate a constant or temporary register
3658 GLint index
= -7; /* TBD / unknown */
3659 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3661 return NULL
; /* out of memory */
3664 /* set the IR node's Var and Store pointers */
3666 varDecl
->Store
= var
->store
;
3671 /* if there's an initializer, generate IR for the expression */
3673 slang_ir_node
*varRef
, *init
;
3675 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3676 /* if the variable is const, the initializer must be a const
3677 * expression as well.
3680 if (!_slang_is_constant_expr(initializer
)) {
3681 slang_info_log_error(A
->log
,
3682 "initializer for %s not constant", varName
);
3688 /* IR for the variable we're initializing */
3689 varRef
= new_var(A
, var
);
3691 slang_info_log_error(A
->log
, "out of memory");
3695 /* constant-folding, etc here */
3696 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3698 /* look for simple constant-valued variables and uniforms */
3699 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3700 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3702 if (initializer
->type
== SLANG_OPER_CALL
&&
3703 initializer
->array_constructor
) {
3704 /* array initializer */
3705 if (make_constant_array(A
, var
, initializer
))
3708 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3709 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3710 /* simple float/vector initializer */
3711 if (store
->File
== PROGRAM_UNIFORM
) {
3712 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3714 totalSize
, datatype
,
3715 initializer
->literal
);
3716 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3721 store
->File
= PROGRAM_CONSTANT
;
3722 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3724 initializer
->literal
,
3726 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3733 /* IR for initializer */
3734 init
= _slang_gen_operation(A
, initializer
);
3738 /* XXX remove this when type checking is added above */
3739 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3740 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3744 /* assign RHS to LHS */
3745 n
= new_node2(IR_COPY
, varRef
, init
);
3746 n
= new_seq(varDecl
, n
);
3749 /* no initializer */
3753 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3754 /* always need to allocate storage for uniforms at this point */
3755 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3756 totalSize
, datatype
, NULL
);
3757 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3761 printf("%s var %p %s store=%p index=%d size=%d\n",
3762 __FUNCTION__
, (void *) var
, (char *) varName
,
3763 (void *) store
, store
->Index
, store
->Size
);
3771 * Generate code for a selection expression: b ? x : y
3772 * XXX In some cases we could implement a selection expression
3773 * with an LRP instruction (use the boolean as the interpolant).
3774 * Otherwise, we use an IF/ELSE/ENDIF construct.
3776 static slang_ir_node
*
3777 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3779 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3780 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3781 slang_typeinfo type0
, type1
, type2
;
3782 int size
, isBool
, isEqual
;
3784 assert(oper
->type
== SLANG_OPER_SELECT
);
3785 assert(oper
->num_children
== 3);
3787 /* type of children[0] must be boolean */
3788 slang_typeinfo_construct(&type0
);
3789 typeof_operation(A
, &oper
->children
[0], &type0
);
3790 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3791 slang_typeinfo_destruct(&type0
);
3793 slang_info_log_error(A
->log
, "selector type is not boolean");
3797 slang_typeinfo_construct(&type1
);
3798 slang_typeinfo_construct(&type2
);
3799 typeof_operation(A
, &oper
->children
[1], &type1
);
3800 typeof_operation(A
, &oper
->children
[2], &type2
);
3801 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3802 slang_typeinfo_destruct(&type1
);
3803 slang_typeinfo_destruct(&type2
);
3805 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3809 /* size of x or y's type */
3810 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3814 tmpDecl
= _slang_gen_temporary(size
);
3816 /* the condition (child 0) */
3817 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3818 cond
= new_cond(cond
);
3820 /* if-true body (child 1) */
3821 tmpVar
= new_node0(IR_VAR
);
3822 tmpVar
->Store
= tmpDecl
->Store
;
3823 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3824 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3826 /* if-false body (child 2) */
3827 tmpVar
= new_node0(IR_VAR
);
3828 tmpVar
->Store
= tmpDecl
->Store
;
3829 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3830 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3832 ifNode
= new_if(cond
, trueNode
, falseNode
);
3835 tmpVar
= new_node0(IR_VAR
);
3836 tmpVar
->Store
= tmpDecl
->Store
;
3838 tree
= new_seq(ifNode
, tmpVar
);
3839 tree
= new_seq(tmpDecl
, tree
);
3841 /*_slang_print_ir_tree(tree, 10);*/
3847 * Generate code for &&.
3849 static slang_ir_node
*
3850 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3852 /* rewrite "a && b" as "a ? b : false" */
3853 slang_operation
*select
;
3856 select
= slang_operation_new(1);
3857 select
->type
= SLANG_OPER_SELECT
;
3858 select
->num_children
= 3;
3859 select
->children
= slang_operation_new(3);
3861 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3862 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3863 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3864 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3865 select
->children
[2].literal_size
= 1;
3867 n
= _slang_gen_select(A
, select
);
3873 * Generate code for ||.
3875 static slang_ir_node
*
3876 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3878 /* rewrite "a || b" as "a ? true : b" */
3879 slang_operation
*select
;
3882 select
= slang_operation_new(1);
3883 select
->type
= SLANG_OPER_SELECT
;
3884 select
->num_children
= 3;
3885 select
->children
= slang_operation_new(3);
3887 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3888 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3889 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3890 select
->children
[1].literal_size
= 1;
3891 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3893 n
= _slang_gen_select(A
, select
);
3899 * Generate IR tree for a return statement.
3901 static slang_ir_node
*
3902 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3904 const GLboolean haveReturnValue
3905 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3907 assert(oper
->type
== SLANG_OPER_RETURN
);
3909 /* error checking */
3910 assert(A
->CurFunction
);
3911 if (haveReturnValue
&&
3912 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3913 slang_info_log_error(A
->log
, "illegal return expression");
3916 else if (!haveReturnValue
&&
3917 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3918 slang_info_log_error(A
->log
, "return statement requires an expression");
3922 if (!haveReturnValue
) {
3923 if (A
->EmitContReturn
) {
3924 return new_return(A
->curFuncEndLabel
);
3927 replace_return_with_flag_set(A
, oper
);
3928 return _slang_gen_operation(A
, oper
);
3937 * return; // goto __endOfFunction
3939 slang_operation
*assign
;
3940 slang_atom a_retVal
;
3943 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3949 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3951 /* trying to return a value in a void-valued function */
3957 assign
= slang_operation_new(1);
3958 assign
->type
= SLANG_OPER_ASSIGN
;
3959 assign
->num_children
= 2;
3960 assign
->children
= slang_operation_new(2);
3961 /* lhs (__retVal) */
3962 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3963 assign
->children
[0].a_id
= a_retVal
;
3964 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3966 /* XXX we might be able to avoid this copy someday */
3967 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3969 /* assemble the new code */
3970 if (A
->EmitContReturn
) {
3971 n
= new_seq(_slang_gen_operation(A
, assign
),
3972 new_return(A
->curFuncEndLabel
));
3975 slang_operation
*setFlag
= slang_operation_new(1);
3976 setFlag
->type
= SLANG_OPER_ASSIGN
;
3977 setFlag
->locals
->outer_scope
= oper
->locals
;
3978 slang_operation_add_children(setFlag
, 2);
3980 slang_operation
*lhs
= slang_oper_child(setFlag
, 0);
3981 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3982 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "__returnFlag");
3985 slang_operation
*rhs
= slang_oper_child(setFlag
, 1);
3986 slang_operation_literal_bool(rhs
, GL_TRUE
);
3988 n
= new_seq(_slang_gen_operation(A
, assign
),
3989 _slang_gen_operation(A
, setFlag
));
3990 slang_operation_delete(setFlag
);
3993 slang_operation_delete(assign
);
4001 * Determine if the given operation/expression is const-valued.
4004 _slang_is_constant_expr(const slang_operation
*oper
)
4006 slang_variable
*var
;
4009 switch (oper
->type
) {
4010 case SLANG_OPER_IDENTIFIER
:
4011 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4012 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
4016 for (i
= 0; i
< oper
->num_children
; i
++) {
4017 if (!_slang_is_constant_expr(&oper
->children
[i
]))
4027 * Check if an assignment of type t1 to t0 is legal.
4028 * XXX more cases needed.
4031 _slang_assignment_compatible(slang_assemble_ctx
*A
,
4032 slang_operation
*op0
,
4033 slang_operation
*op1
)
4035 slang_typeinfo t0
, t1
;
4038 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
4039 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
4043 slang_typeinfo_construct(&t0
);
4044 typeof_operation(A
, op0
, &t0
);
4046 slang_typeinfo_construct(&t1
);
4047 typeof_operation(A
, op1
, &t1
);
4049 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
4050 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
4054 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4059 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
4060 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4061 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4064 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4065 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4068 #if 0 /* not used just yet - causes problems elsewhere */
4069 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4070 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4074 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4075 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4078 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4079 t1
.spec
.type
== SLANG_SPEC_INT
)
4087 * Generate IR tree for a local variable declaration.
4088 * Basically do some error checking and call _slang_gen_var_decl().
4090 static slang_ir_node
*
4091 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4093 const char *varName
= (char *) oper
->a_id
;
4094 slang_variable
*var
;
4095 slang_ir_node
*varDecl
;
4096 slang_operation
*initializer
;
4098 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4099 assert(oper
->num_children
<= 1);
4102 /* lookup the variable by name */
4103 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4105 return NULL
; /* "shouldn't happen" */
4107 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4108 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4109 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4110 /* can't declare attribute/uniform vars inside functions */
4111 slang_info_log_error(A
->log
,
4112 "local variable '%s' cannot be an attribute/uniform/varying",
4119 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4124 /* check if the var has an initializer */
4125 if (oper
->num_children
> 0) {
4126 assert(oper
->num_children
== 1);
4127 initializer
= &oper
->children
[0];
4129 else if (var
->initializer
) {
4130 initializer
= var
->initializer
;
4137 /* check/compare var type and initializer type */
4138 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4139 slang_info_log_error(A
->log
, "incompatible types in assignment");
4144 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4145 slang_info_log_error(A
->log
,
4146 "const-qualified variable '%s' requires initializer",
4152 /* Generate IR node */
4153 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4162 * Generate IR tree for a reference to a variable (such as in an expression).
4163 * This is different from a variable declaration.
4165 static slang_ir_node
*
4166 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4168 /* If there's a variable associated with this oper (from inlining)
4169 * use it. Otherwise, use the oper's var id.
4171 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4172 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4175 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4178 assert(var
->declared
);
4179 n
= new_var(A
, var
);
4186 * Return the number of components actually named by the swizzle.
4187 * Recall that swizzles may have undefined/don't-care values.
4190 swizzle_size(GLuint swizzle
)
4193 for (i
= 0; i
< 4; i
++) {
4194 GLuint swz
= GET_SWZ(swizzle
, i
);
4195 size
+= (swz
>= 0 && swz
<= 3);
4201 static slang_ir_node
*
4202 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4204 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4208 n
->Store
= _slang_new_ir_storage_relative(0,
4209 swizzle_size(swizzle
),
4211 n
->Store
->Swizzle
= swizzle
;
4218 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4220 while (store
->Parent
)
4221 store
= store
->Parent
;
4223 if (!(store
->File
== PROGRAM_OUTPUT
||
4224 store
->File
== PROGRAM_TEMPORARY
||
4225 (store
->File
== PROGRAM_VARYING
&&
4226 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4236 * Walk up an IR storage path to compute the final swizzle.
4237 * This is used when we find an expression such as "foo.xz.yx".
4240 root_swizzle(const slang_ir_storage
*st
)
4242 GLuint swizzle
= st
->Swizzle
;
4243 while (st
->Parent
) {
4245 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4252 * Generate IR tree for an assignment (=).
4254 static slang_ir_node
*
4255 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4257 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4258 /* Check that var is writeable */
4260 = _slang_variable_locate(oper
->children
[0].locals
,
4261 oper
->children
[0].a_id
, GL_TRUE
);
4263 slang_info_log_error(A
->log
, "undefined variable '%s'",
4264 (char *) oper
->children
[0].a_id
);
4267 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4268 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4269 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4270 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4271 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4272 slang_info_log_error(A
->log
,
4273 "illegal assignment to read-only variable '%s'",
4274 (char *) oper
->children
[0].a_id
);
4279 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4280 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4281 /* Special case of: x = f(a, b)
4282 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4284 * XXX this could be even more effective if we could accomodate
4285 * cases such as "v.x = f();" - would help with typical vertex
4289 n
= _slang_gen_function_call_name(A
,
4290 (const char *) oper
->children
[1].a_id
,
4291 &oper
->children
[1], &oper
->children
[0]);
4295 slang_ir_node
*n
, *lhs
, *rhs
;
4297 /* lhs and rhs type checking */
4298 if (!_slang_assignment_compatible(A
,
4300 &oper
->children
[1])) {
4301 slang_info_log_error(A
->log
, "incompatible types in assignment");
4305 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4311 slang_info_log_error(A
->log
,
4312 "invalid left hand side for assignment");
4316 /* check that lhs is writable */
4317 if (!is_store_writable(A
, lhs
->Store
)) {
4318 slang_info_log_error(A
->log
,
4319 "illegal assignment to read-only l-value");
4323 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4325 /* convert lhs swizzle into writemask */
4326 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4327 GLuint writemask
, newSwizzle
= 0x0;
4328 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4329 /* Non-simple writemask, need to swizzle right hand side in
4330 * order to put components into the right place.
4332 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4334 n
= new_node2(IR_COPY
, lhs
, rhs
);
4345 * Generate IR tree for referencing a field in a struct (or basic vector type)
4347 static slang_ir_node
*
4348 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4352 /* type of struct */
4353 slang_typeinfo_construct(&ti
);
4354 typeof_operation(A
, &oper
->children
[0], &ti
);
4356 if (_slang_type_is_vector(ti
.spec
.type
)) {
4357 /* the field should be a swizzle */
4358 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4362 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4363 slang_info_log_error(A
->log
, "Bad swizzle");
4366 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4371 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4372 /* create new parent node with swizzle */
4374 n
= _slang_gen_swizzle(n
, swizzle
);
4377 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4378 || ti
.spec
.type
== SLANG_SPEC_INT
4379 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4380 const GLuint rows
= 1;
4384 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4385 slang_info_log_error(A
->log
, "Bad swizzle");
4387 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4391 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4392 /* create new parent node with swizzle */
4393 n
= _slang_gen_swizzle(n
, swizzle
);
4397 /* the field is a structure member (base.field) */
4398 /* oper->children[0] is the base */
4399 /* oper->a_id is the field name */
4400 slang_ir_node
*base
, *n
;
4401 slang_typeinfo field_ti
;
4402 GLint fieldSize
, fieldOffset
= -1;
4405 slang_typeinfo_construct(&field_ti
);
4406 typeof_operation(A
, oper
, &field_ti
);
4408 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4410 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4412 if (fieldSize
== 0 || fieldOffset
< 0) {
4413 const char *structName
;
4414 if (ti
.spec
._struct
)
4415 structName
= (char *) ti
.spec
._struct
->a_name
;
4417 structName
= "unknown";
4418 slang_info_log_error(A
->log
,
4419 "\"%s\" is not a member of struct \"%s\"",
4420 (char *) oper
->a_id
, structName
);
4423 assert(fieldSize
>= 0);
4425 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4427 /* error msg should have already been logged */
4431 n
= new_node1(IR_FIELD
, base
);
4435 n
->Field
= (char *) oper
->a_id
;
4437 /* Store the field's offset in storage->Index */
4438 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4448 * Gen code for array indexing.
4450 static slang_ir_node
*
4451 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4453 slang_typeinfo array_ti
;
4455 /* get array's type info */
4456 slang_typeinfo_construct(&array_ti
);
4457 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4459 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4460 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4461 /* translate the index into a swizzle/writemask: "v.x=p" */
4462 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4466 index
= (GLint
) oper
->children
[1].literal
[0];
4467 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4468 index
>= (GLint
) max
) {
4470 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4471 printf("type = %d\n", oper
->children
[1].type
);
4472 printf("index = %d, max = %d\n", index
, max
);
4473 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4474 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4481 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4483 /* use swizzle to access the element */
4484 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4488 n
= _slang_gen_swizzle(n
, swizzle
);
4494 /* conventional array */
4495 slang_typeinfo elem_ti
;
4496 slang_ir_node
*elem
, *array
, *index
;
4497 GLint elemSize
, arrayLen
;
4499 /* size of array element */
4500 slang_typeinfo_construct(&elem_ti
);
4501 typeof_operation(A
, oper
, &elem_ti
);
4502 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4504 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4505 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4507 arrayLen
= array_ti
.array_len
;
4509 slang_typeinfo_destruct(&array_ti
);
4510 slang_typeinfo_destruct(&elem_ti
);
4512 if (elemSize
<= 0) {
4513 /* unknown var or type */
4514 slang_info_log_error(A
->log
, "Undefined variable or type");
4518 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4519 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4520 if (array
&& index
) {
4522 GLint constIndex
= -1;
4523 if (index
->Opcode
== IR_FLOAT
) {
4524 constIndex
= (int) index
->Value
[0];
4525 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4526 slang_info_log_error(A
->log
,
4527 "Array index out of bounds (index=%d size=%d)",
4528 constIndex
, arrayLen
);
4529 _slang_free_ir_tree(array
);
4530 _slang_free_ir_tree(index
);
4535 if (!array
->Store
) {
4536 slang_info_log_error(A
->log
, "Invalid array");
4540 elem
= new_node2(IR_ELEMENT
, array
, index
);
4542 /* The storage info here will be updated during code emit */
4543 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4544 array
->Store
->Index
,
4546 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4550 _slang_free_ir_tree(array
);
4551 _slang_free_ir_tree(index
);
4558 static slang_ir_node
*
4559 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4560 slang_ir_opcode opcode
)
4562 slang_typeinfo t0
, t1
;
4565 slang_typeinfo_construct(&t0
);
4566 typeof_operation(A
, &oper
->children
[0], &t0
);
4568 slang_typeinfo_construct(&t1
);
4569 typeof_operation(A
, &oper
->children
[0], &t1
);
4571 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4572 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4573 slang_info_log_error(A
->log
, "Illegal array comparison");
4577 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4578 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4579 /* <, <=, >, >= can only be used with scalars */
4580 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4581 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4582 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4583 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4584 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4589 n
= new_node2(opcode
,
4590 _slang_gen_operation(A
, &oper
->children
[0]),
4591 _slang_gen_operation(A
, &oper
->children
[1]));
4593 /* result is a bool (size 1) */
4594 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4602 print_vars(slang_variable_scope
*s
)
4606 for (i
= 0; i
< s
->num_variables
; i
++) {
4608 (char*) s
->variables
[i
]->a_name
,
4609 s
->variables
[i
]->declared
);
4619 _slang_undeclare_vars(slang_variable_scope
*locals
)
4621 if (locals
->num_variables
> 0) {
4623 for (i
= 0; i
< locals
->num_variables
; i
++) {
4624 slang_variable
*v
= locals
->variables
[i
];
4625 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4626 v
->declared
= GL_FALSE
;
4634 * Generate IR tree for a slang_operation (AST node)
4636 static slang_ir_node
*
4637 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4639 switch (oper
->type
) {
4640 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4644 _slang_push_var_table(A
->vartable
);
4646 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4647 n
= _slang_gen_operation(A
, oper
);
4648 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4650 _slang_pop_var_table(A
->vartable
);
4652 /*_slang_undeclare_vars(oper->locals);*/
4653 /*print_vars(oper->locals);*/
4656 n
= new_node1(IR_SCOPE
, n
);
4661 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4662 /* list of operations */
4663 if (oper
->num_children
> 0)
4665 slang_ir_node
*n
, *tree
= NULL
;
4668 for (i
= 0; i
< oper
->num_children
; i
++) {
4669 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4671 _slang_free_ir_tree(tree
);
4672 return NULL
; /* error must have occured */
4674 tree
= new_seq(tree
, n
);
4680 return new_node0(IR_NOP
);
4683 case SLANG_OPER_EXPRESSION
:
4684 return _slang_gen_operation(A
, &oper
->children
[0]);
4686 case SLANG_OPER_FOR
:
4687 return _slang_gen_for(A
, oper
);
4689 return _slang_gen_do(A
, oper
);
4690 case SLANG_OPER_WHILE
:
4691 return _slang_gen_while(A
, oper
);
4692 case SLANG_OPER_BREAK
:
4694 slang_info_log_error(A
->log
, "'break' not in loop");
4697 return new_break(A
->CurLoop
);
4698 case SLANG_OPER_CONTINUE
:
4700 slang_info_log_error(A
->log
, "'continue' not in loop");
4703 return _slang_gen_continue(A
, oper
);
4704 case SLANG_OPER_DISCARD
:
4705 return new_node0(IR_KILL
);
4707 case SLANG_OPER_EQUAL
:
4708 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4709 case SLANG_OPER_NOTEQUAL
:
4710 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4711 case SLANG_OPER_GREATER
:
4712 return _slang_gen_compare(A
, oper
, IR_SGT
);
4713 case SLANG_OPER_LESS
:
4714 return _slang_gen_compare(A
, oper
, IR_SLT
);
4715 case SLANG_OPER_GREATEREQUAL
:
4716 return _slang_gen_compare(A
, oper
, IR_SGE
);
4717 case SLANG_OPER_LESSEQUAL
:
4718 return _slang_gen_compare(A
, oper
, IR_SLE
);
4719 case SLANG_OPER_ADD
:
4722 assert(oper
->num_children
== 2);
4723 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4726 case SLANG_OPER_SUBTRACT
:
4729 assert(oper
->num_children
== 2);
4730 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4733 case SLANG_OPER_MULTIPLY
:
4736 assert(oper
->num_children
== 2);
4737 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4740 case SLANG_OPER_DIVIDE
:
4743 assert(oper
->num_children
== 2);
4744 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4747 case SLANG_OPER_MINUS
:
4750 assert(oper
->num_children
== 1);
4751 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4754 case SLANG_OPER_PLUS
:
4755 /* +expr --> do nothing */
4756 return _slang_gen_operation(A
, &oper
->children
[0]);
4757 case SLANG_OPER_VARIABLE_DECL
:
4758 return _slang_gen_declaration(A
, oper
);
4759 case SLANG_OPER_ASSIGN
:
4760 return _slang_gen_assignment(A
, oper
);
4761 case SLANG_OPER_ADDASSIGN
:
4764 assert(oper
->num_children
== 2);
4765 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4768 case SLANG_OPER_SUBASSIGN
:
4771 assert(oper
->num_children
== 2);
4772 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4776 case SLANG_OPER_MULASSIGN
:
4779 assert(oper
->num_children
== 2);
4780 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4783 case SLANG_OPER_DIVASSIGN
:
4786 assert(oper
->num_children
== 2);
4787 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4790 case SLANG_OPER_LOGICALAND
:
4793 assert(oper
->num_children
== 2);
4794 n
= _slang_gen_logical_and(A
, oper
);
4797 case SLANG_OPER_LOGICALOR
:
4800 assert(oper
->num_children
== 2);
4801 n
= _slang_gen_logical_or(A
, oper
);
4804 case SLANG_OPER_LOGICALXOR
:
4805 return _slang_gen_xor(A
, oper
);
4806 case SLANG_OPER_NOT
:
4807 return _slang_gen_not(A
, oper
);
4808 case SLANG_OPER_SELECT
: /* b ? x : y */
4811 assert(oper
->num_children
== 3);
4812 n
= _slang_gen_select(A
, oper
);
4816 case SLANG_OPER_ASM
:
4817 return _slang_gen_asm(A
, oper
, NULL
);
4818 case SLANG_OPER_CALL
:
4819 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4821 case SLANG_OPER_METHOD
:
4822 return _slang_gen_method_call(A
, oper
);
4823 case SLANG_OPER_RETURN
:
4824 return _slang_gen_return(A
, oper
);
4825 case SLANG_OPER_LABEL
:
4826 return new_label(oper
->label
);
4827 case SLANG_OPER_IDENTIFIER
:
4828 return _slang_gen_variable(A
, oper
);
4830 return _slang_gen_if(A
, oper
);
4831 case SLANG_OPER_FIELD
:
4832 return _slang_gen_struct_field(A
, oper
);
4833 case SLANG_OPER_SUBSCRIPT
:
4834 return _slang_gen_array_element(A
, oper
);
4835 case SLANG_OPER_LITERAL_FLOAT
:
4837 case SLANG_OPER_LITERAL_INT
:
4839 case SLANG_OPER_LITERAL_BOOL
:
4840 return new_float_literal(oper
->literal
, oper
->literal_size
);
4842 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4845 assert(oper
->num_children
== 1);
4846 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4849 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4852 assert(oper
->num_children
== 1);
4853 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4856 case SLANG_OPER_PREINCREMENT
: /* ++var */
4859 assert(oper
->num_children
== 1);
4860 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4863 case SLANG_OPER_PREDECREMENT
: /* --var */
4866 assert(oper
->num_children
== 1);
4867 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4871 case SLANG_OPER_NON_INLINED_CALL
:
4872 case SLANG_OPER_SEQUENCE
:
4874 slang_ir_node
*tree
= NULL
;
4876 for (i
= 0; i
< oper
->num_children
; i
++) {
4877 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4878 tree
= new_seq(tree
, n
);
4880 tree
->Store
= n
->Store
;
4882 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4883 tree
= new_function_call(tree
, oper
->label
);
4888 case SLANG_OPER_NONE
:
4889 case SLANG_OPER_VOID
:
4890 /* returning NULL here would generate an error */
4891 return new_node0(IR_NOP
);
4894 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4896 return new_node0(IR_NOP
);
4904 * Check if the given type specifier is a rectangular texture sampler.
4907 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4909 while (spec
->_array
) {
4910 spec
= spec
->_array
;
4912 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4913 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4919 * Called by compiler when a global variable has been parsed/compiled.
4920 * Here we examine the variable's type to determine what kind of register
4921 * storage will be used.
4923 * A uniform such as "gl_Position" will become the register specification
4924 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4925 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4927 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4928 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4929 * actual texture unit (as specified by the user calling glUniform1i()).
4932 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4933 slang_unit_type type
)
4935 struct gl_program
*prog
= A
->program
;
4936 const char *varName
= (char *) var
->a_name
;
4937 GLboolean success
= GL_TRUE
;
4938 slang_ir_storage
*store
= NULL
;
4940 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4941 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4942 const GLint arrayLen
= _slang_array_length(var
);
4943 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4944 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4946 /* check for sampler2D arrays */
4947 if (texIndex
== -1 && var
->type
.specifier
._array
)
4948 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4950 if (texIndex
!= -1) {
4951 /* This is a texture sampler variable...
4952 * store->File = PROGRAM_SAMPLER
4953 * store->Index = sampler number (0..7, typically)
4954 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4956 if (var
->initializer
) {
4957 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4960 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4961 /* disallow rect samplers */
4962 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4963 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4967 (void) is_rect_sampler_spec
; /* silence warning */
4970 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4971 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4973 /* If we have a sampler array, then we need to allocate the
4974 * additional samplers to ensure we don't allocate them elsewhere.
4975 * We can't directly use _mesa_add_sampler() as that checks the
4976 * varName and gets a match, so we call _mesa_add_parameter()
4977 * directly and use the last sampler number from the call above.
4980 GLint a
= arrayLen
- 1;
4982 for (i
= 0; i
< a
; i
++) {
4983 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4984 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4985 varName
, 1, datatype
, &value
, NULL
, 0x0);
4989 if (dbg
) printf("SAMPLER ");
4991 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4992 /* Uniform variable */
4993 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4996 /* user-defined uniform */
4997 if (datatype
== GL_NONE
) {
4998 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
4999 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
5000 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
5001 /* temporary work-around */
5002 GLenum datatype
= GL_FLOAT
;
5003 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
5004 totalSize
, datatype
, NULL
);
5005 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
5006 totalSize
, swizzle
);
5009 GLint a
= arrayLen
- 1;
5011 for (i
= 0; i
< a
; i
++) {
5012 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
5013 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
5014 varName
, 1, datatype
, &value
, NULL
, 0x0);
5018 /* XXX what we need to do is unroll the struct into its
5019 * basic types, creating a uniform variable for each.
5027 * Should produce uniforms:
5028 * "f.a" (GL_FLOAT_VEC3)
5029 * "f.b" (GL_FLOAT_VEC4)
5032 if (var
->initializer
) {
5033 slang_info_log_error(A
->log
,
5034 "unsupported initializer for uniform '%s'", varName
);
5039 slang_info_log_error(A
->log
,
5040 "invalid datatype for uniform variable %s",
5046 /* non-struct uniform */
5047 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
5053 /* pre-defined uniform, like gl_ModelviewMatrix */
5054 /* We know it's a uniform, but don't allocate storage unless
5057 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
5058 totalSize
, swizzle
);
5060 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5062 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5063 /* varyings must be float, vec or mat */
5064 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5065 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5066 slang_info_log_error(A
->log
,
5067 "varying '%s' must be float/vector/matrix",
5072 if (var
->initializer
) {
5073 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5079 /* user-defined varying */
5085 if (var
->type
.centroid
== SLANG_CENTROID
)
5086 flags
|= PROG_PARAM_BIT_CENTROID
;
5087 if (var
->type
.variant
== SLANG_INVARIANT
)
5088 flags
|= PROG_PARAM_BIT_INVARIANT
;
5090 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5092 swizzle
= _slang_var_swizzle(size
, 0);
5093 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5094 totalSize
, swizzle
);
5097 /* pre-defined varying, like gl_Color or gl_TexCoord */
5098 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5099 /* fragment program input */
5101 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5104 assert(index
< FRAG_ATTRIB_MAX
);
5105 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5109 /* vertex program output */
5110 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5111 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5113 assert(index
< VERT_RESULT_MAX
);
5114 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5115 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5118 if (dbg
) printf("V/F ");
5120 if (dbg
) printf("VARYING ");
5122 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5125 /* attributes must be float, vec or mat */
5126 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5127 slang_info_log_error(A
->log
,
5128 "attribute '%s' must be float/vector/matrix",
5134 /* user-defined vertex attribute */
5135 const GLint attr
= -1; /* unknown */
5136 swizzle
= _slang_var_swizzle(size
, 0);
5137 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5138 size
, datatype
, attr
);
5140 index
= VERT_ATTRIB_GENERIC0
+ index
;
5143 /* pre-defined vertex attrib */
5144 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5147 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5148 if (dbg
) printf("ATTRIB ");
5150 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5151 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5152 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5154 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5155 if (dbg
) printf("INPUT ");
5157 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5158 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5159 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5160 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5163 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5164 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5165 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5166 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5168 if (dbg
) printf("OUTPUT ");
5170 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5171 /* pre-defined global constant, like gl_MaxLights */
5172 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5173 if (dbg
) printf("CONST ");
5176 /* ordinary variable (may be const) */
5179 /* IR node to declare the variable */
5180 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5182 /* emit GPU instructions */
5183 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5185 _slang_free_ir_tree(n
);
5188 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5189 store
? store
->Index
: -2);
5192 var
->store
= store
; /* save var's storage info */
5194 var
->declared
= GL_TRUE
;
5201 * Produce an IR tree from a function AST (fun->body).
5202 * Then call the code emitter to convert the IR tree into gl_program
5206 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5209 GLboolean success
= GL_TRUE
;
5211 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5212 /* we only really generate code for main, all other functions get
5213 * inlined or codegen'd upon an actual call.
5216 /* do some basic error checking though */
5217 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5218 /* check that non-void functions actually return something */
5220 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5222 slang_info_log_error(A
->log
,
5223 "function \"%s\" has no return statement",
5224 (char *) fun
->header
.a_name
);
5226 "function \"%s\" has no return statement\n",
5227 (char *) fun
->header
.a_name
);
5232 return GL_TRUE
; /* not an error */
5236 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5237 slang_print_function(fun
, 1);
5240 /* should have been allocated earlier: */
5241 assert(A
->program
->Parameters
);
5242 assert(A
->program
->Varying
);
5243 assert(A
->vartable
);
5245 A
->CurLoopOper
= NULL
;
5246 A
->CurFunction
= fun
;
5248 /* fold constant expressions, etc. */
5249 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5252 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5253 slang_print_function(fun
, 1);
5256 /* Create an end-of-function label */
5257 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5259 /* push new vartable scope */
5260 _slang_push_var_table(A
->vartable
);
5262 /* Generate IR tree for the function body code */
5263 n
= _slang_gen_operation(A
, fun
->body
);
5265 n
= new_node1(IR_SCOPE
, n
);
5267 /* pop vartable, restore previous */
5268 _slang_pop_var_table(A
->vartable
);
5271 /* XXX record error */
5275 /* append an end-of-function-label to IR tree */
5276 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5278 /*_slang_label_delete(A->curFuncEndLabel);*/
5279 A
->curFuncEndLabel
= NULL
;
5282 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5283 slang_print_function(fun
, 1);
5286 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5287 _slang_print_ir_tree(n
, 0);
5290 printf("************* End codegen function ************\n\n");
5293 if (A
->UnresolvedRefs
) {
5294 /* Can't codegen at this time.
5295 * At link time we'll concatenate all the vertex shaders and/or all
5296 * the fragment shaders and try recompiling.
5301 /* Emit program instructions */
5302 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5303 _slang_free_ir_tree(n
);
5305 /* free codegen context */
5307 _mesa_free(A->codegen);