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 slang_resolve_variable(slang_operation
*oper
)
842 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
843 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
849 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
852 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
853 GLuint substCount
, slang_variable
**substOld
,
854 slang_operation
**substNew
, GLboolean isLHS
)
856 switch (oper
->type
) {
857 case SLANG_OPER_VARIABLE_DECL
:
859 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
860 oper
->a_id
, GL_TRUE
);
862 if (v
->initializer
&& oper
->num_children
== 0) {
863 /* set child of oper to copy of initializer */
864 oper
->num_children
= 1;
865 oper
->children
= slang_operation_new(1);
866 slang_operation_copy(&oper
->children
[0], v
->initializer
);
868 if (oper
->num_children
== 1) {
869 /* the initializer */
870 slang_substitute(A
, &oper
->children
[0], substCount
,
871 substOld
, substNew
, GL_FALSE
);
875 case SLANG_OPER_IDENTIFIER
:
876 assert(oper
->num_children
== 0);
877 if (1/**!isLHS XXX FIX */) {
878 slang_atom id
= oper
->a_id
;
881 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
883 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
887 /* look for a substitution */
888 for (i
= 0; i
< substCount
; i
++) {
889 if (v
== substOld
[i
]) {
890 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
891 #if 0 /* DEBUG only */
892 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
893 assert(substNew
[i
]->var
);
894 assert(substNew
[i
]->var
->a_name
);
895 printf("Substitute %s with %s in id node %p\n",
896 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
900 printf("Substitute %s with %f in id node %p\n",
901 (char*)v
->a_name
, substNew
[i
]->literal
[0],
905 slang_operation_copy(oper
, substNew
[i
]);
912 case SLANG_OPER_RETURN
:
913 /* do return replacement here too */
914 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
915 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
921 * then do substitutions on the assignment.
923 slang_operation
*blockOper
, *assignOper
, *returnOper
;
925 /* check if function actually has a return type */
926 assert(A
->CurFunction
);
927 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
928 slang_info_log_error(A
->log
, "illegal return expression");
932 blockOper
= slang_operation_new(1);
933 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
934 blockOper
->num_children
= 2;
935 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
936 blockOper
->children
= slang_operation_new(2);
937 assignOper
= blockOper
->children
+ 0;
938 returnOper
= blockOper
->children
+ 1;
940 assignOper
->type
= SLANG_OPER_ASSIGN
;
941 assignOper
->num_children
= 2;
942 assignOper
->locals
->outer_scope
= blockOper
->locals
;
943 assignOper
->children
= slang_operation_new(2);
944 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
945 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
946 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
948 slang_operation_copy(&assignOper
->children
[1],
951 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
952 assert(returnOper
->num_children
== 0);
954 /* do substitutions on the "__retVal = expr" sub-tree */
955 slang_substitute(A
, assignOper
,
956 substCount
, substOld
, substNew
, GL_FALSE
);
958 /* install new code */
959 slang_operation_copy(oper
, blockOper
);
960 slang_operation_destruct(blockOper
);
963 /* check if return value was expected */
964 assert(A
->CurFunction
);
965 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
966 slang_info_log_error(A
->log
, "return statement requires an expression");
972 case SLANG_OPER_ASSIGN
:
973 case SLANG_OPER_SUBSCRIPT
:
975 * child[0] can't have substitutions but child[1] can.
977 slang_substitute(A
, &oper
->children
[0],
978 substCount
, substOld
, substNew
, GL_TRUE
);
979 slang_substitute(A
, &oper
->children
[1],
980 substCount
, substOld
, substNew
, GL_FALSE
);
982 case SLANG_OPER_FIELD
:
984 slang_substitute(A
, &oper
->children
[0],
985 substCount
, substOld
, substNew
, GL_TRUE
);
990 for (i
= 0; i
< oper
->num_children
; i
++)
991 slang_substitute(A
, &oper
->children
[i
],
992 substCount
, substOld
, substNew
, GL_FALSE
);
999 * Produce inline code for a call to an assembly instruction.
1000 * This is typically used to compile a call to a built-in function like this:
1002 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1004 * __asm vec4_lrp __retVal, a, y, x;
1009 * r = mix(p1, p2, p3);
1019 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1021 static slang_operation
*
1022 slang_inline_asm_function(slang_assemble_ctx
*A
,
1023 slang_function
*fun
, slang_operation
*oper
)
1025 const GLuint numArgs
= oper
->num_children
;
1027 slang_operation
*inlined
;
1028 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1029 slang_variable
**substOld
;
1030 slang_operation
**substNew
;
1032 ASSERT(slang_is_asm_function(fun
));
1033 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1036 printf("Inline %s as %s\n",
1037 (char*) fun->header.a_name,
1038 (char*) fun->body->children[0].a_id);
1042 * We'll substitute formal params with actual args in the asm call.
1044 substOld
= (slang_variable
**)
1045 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1046 substNew
= (slang_operation
**)
1047 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1048 for (i
= 0; i
< numArgs
; i
++) {
1049 substOld
[i
] = fun
->parameters
->variables
[i
];
1050 substNew
[i
] = oper
->children
+ i
;
1053 /* make a copy of the code to inline */
1054 inlined
= slang_operation_new(1);
1055 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1057 /* get rid of the __retVal child */
1058 inlined
->num_children
--;
1059 for (i
= 0; i
< inlined
->num_children
; i
++) {
1060 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1064 /* now do formal->actual substitutions */
1065 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1067 _slang_free(substOld
);
1068 _slang_free(substNew
);
1071 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1072 (char *) fun
->header
.a_name
);
1073 slang_print_tree(inlined
, 3);
1074 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1082 * Inline the given function call operation.
1083 * Return a new slang_operation that corresponds to the inlined code.
1085 static slang_operation
*
1086 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1087 slang_operation
*oper
, slang_operation
*returnOper
)
1094 ParamMode
*paramMode
;
1095 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1096 const GLuint numArgs
= oper
->num_children
;
1097 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1098 slang_operation
*args
= oper
->children
;
1099 slang_operation
*inlined
, *top
;
1100 slang_variable
**substOld
;
1101 slang_operation
**substNew
;
1102 GLuint substCount
, numCopyIn
, i
;
1103 slang_function
*prevFunction
;
1104 slang_variable_scope
*newScope
= NULL
;
1107 prevFunction
= A
->CurFunction
;
1108 A
->CurFunction
= fun
;
1110 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1111 assert(fun
->param_count
== totalArgs
);
1113 /* allocate temporary arrays */
1114 paramMode
= (ParamMode
*)
1115 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1116 substOld
= (slang_variable
**)
1117 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1118 substNew
= (slang_operation
**)
1119 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1122 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1123 (char *) fun
->header
.a_name
,
1124 fun
->parameters
->num_variables
, numArgs
);
1127 if (haveRetValue
&& !returnOper
) {
1128 /* Create 3-child comma sequence for inlined code:
1129 * child[0]: declare __resultTmp
1130 * child[1]: inlined function body
1131 * child[2]: __resultTmp
1133 slang_operation
*commaSeq
;
1134 slang_operation
*declOper
= NULL
;
1135 slang_variable
*resultVar
;
1137 commaSeq
= slang_operation_new(1);
1138 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1139 assert(commaSeq
->locals
);
1140 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1141 commaSeq
->num_children
= 3;
1142 commaSeq
->children
= slang_operation_new(3);
1143 /* allocate the return var */
1144 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1146 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1147 (void*)commaSeq->locals, (char *) fun->header.a_name);
1150 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1151 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1152 resultVar
->isTemp
= GL_TRUE
;
1154 /* child[0] = __resultTmp declaration */
1155 declOper
= &commaSeq
->children
[0];
1156 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1157 declOper
->a_id
= resultVar
->a_name
;
1158 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1160 /* child[1] = function body */
1161 inlined
= &commaSeq
->children
[1];
1162 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1164 /* child[2] = __resultTmp reference */
1165 returnOper
= &commaSeq
->children
[2];
1166 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1167 returnOper
->a_id
= resultVar
->a_name
;
1168 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1173 top
= inlined
= slang_operation_new(1);
1174 /* XXXX this may be inappropriate!!!! */
1175 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1179 assert(inlined
->locals
);
1181 /* Examine the parameters, look for inout/out params, look for possible
1182 * substitutions, etc:
1183 * param type behaviour
1184 * in copy actual to local
1185 * const in substitute param with actual
1189 for (i
= 0; i
< totalArgs
; i
++) {
1190 slang_variable
*p
= fun
->parameters
->variables
[i
];
1192 printf("Param %d: %s %s \n", i,
1193 slang_type_qual_string(p->type.qualifier),
1194 (char *) p->a_name);
1196 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1197 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1198 /* an output param */
1199 slang_operation
*arg
;
1204 paramMode
[i
] = SUBST
;
1206 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1207 slang_resolve_variable(arg
);
1209 /* replace parameter 'p' with argument 'arg' */
1210 substOld
[substCount
] = p
;
1211 substNew
[substCount
] = arg
; /* will get copied */
1214 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1215 /* a constant input param */
1216 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1217 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1218 /* replace all occurances of this parameter variable with the
1219 * actual argument variable or a literal.
1221 paramMode
[i
] = SUBST
;
1222 slang_resolve_variable(&args
[i
]);
1223 substOld
[substCount
] = p
;
1224 substNew
[substCount
] = &args
[i
]; /* will get copied */
1228 paramMode
[i
] = COPY_IN
;
1232 paramMode
[i
] = COPY_IN
;
1234 assert(paramMode
[i
]);
1237 /* actual code inlining: */
1238 slang_operation_copy(inlined
, fun
->body
);
1240 /*** XXX review this */
1241 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1242 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1243 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1246 printf("======================= orig body code ======================\n");
1247 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1248 slang_print_tree(fun
->body
, 8);
1249 printf("======================= copied code =========================\n");
1250 slang_print_tree(inlined
, 8);
1253 /* do parameter substitution in inlined code: */
1254 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1257 printf("======================= subst code ==========================\n");
1258 slang_print_tree(inlined
, 8);
1259 printf("=============================================================\n");
1262 /* New prolog statements: (inserted before the inlined code)
1263 * Copy the 'in' arguments.
1266 for (i
= 0; i
< numArgs
; i
++) {
1267 if (paramMode
[i
] == COPY_IN
) {
1268 slang_variable
*p
= fun
->parameters
->variables
[i
];
1269 /* declare parameter 'p' */
1270 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1274 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1275 assert(decl
->locals
);
1276 decl
->locals
->outer_scope
= inlined
->locals
;
1277 decl
->a_id
= p
->a_name
;
1278 decl
->num_children
= 1;
1279 decl
->children
= slang_operation_new(1);
1281 /* child[0] is the var's initializer */
1282 slang_operation_copy(&decl
->children
[0], args
+ i
);
1284 /* add parameter 'p' to the local variable scope here */
1286 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1287 pCopy
->type
= p
->type
;
1288 pCopy
->a_name
= p
->a_name
;
1289 pCopy
->array_len
= p
->array_len
;
1292 newScope
= inlined
->locals
;
1297 /* Now add copies of the function's local vars to the new variable scope */
1298 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1299 slang_variable
*p
= fun
->parameters
->variables
[i
];
1300 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1301 pCopy
->type
= p
->type
;
1302 pCopy
->a_name
= p
->a_name
;
1303 pCopy
->array_len
= p
->array_len
;
1307 /* New epilog statements:
1308 * 1. Create end of function label to jump to from return statements.
1309 * 2. Copy the 'out' parameter vars
1312 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1314 inlined
->num_children
);
1315 lab
->type
= SLANG_OPER_LABEL
;
1316 lab
->label
= A
->curFuncEndLabel
;
1319 for (i
= 0; i
< totalArgs
; i
++) {
1320 if (paramMode
[i
] == COPY_OUT
) {
1321 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1322 /* actualCallVar = outParam */
1323 /*if (i > 0 || !haveRetValue)*/
1324 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1326 inlined
->num_children
);
1327 ass
->type
= SLANG_OPER_ASSIGN
;
1328 ass
->num_children
= 2;
1329 ass
->locals
->outer_scope
= inlined
->locals
;
1330 ass
->children
= slang_operation_new(2);
1331 ass
->children
[0] = args
[i
]; /*XXX copy */
1332 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1333 ass
->children
[1].a_id
= p
->a_name
;
1334 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1338 _slang_free(paramMode
);
1339 _slang_free(substOld
);
1340 _slang_free(substNew
);
1342 /* Update scoping to use the new local vars instead of the
1343 * original function's vars. This is especially important
1344 * for nested inlining.
1347 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1350 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1351 (char *) fun
->header
.a_name
,
1352 fun
->parameters
->num_variables
, numArgs
);
1353 slang_print_tree(top
, 0);
1357 A
->CurFunction
= prevFunction
;
1363 static slang_ir_node
*
1364 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1365 slang_operation
*oper
, slang_operation
*dest
)
1368 slang_operation
*inlined
;
1369 slang_label
*prevFuncEndLabel
;
1372 prevFuncEndLabel
= A
->curFuncEndLabel
;
1373 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1374 A
->curFuncEndLabel
= _slang_label_new(name
);
1375 assert(A
->curFuncEndLabel
);
1377 if (slang_is_asm_function(fun
) && !dest
) {
1378 /* assemble assembly function - tree style */
1379 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1382 /* non-assembly function */
1383 /* We always generate an "inline-able" block of code here.
1385 * 1. insert the inline code
1386 * 2. Generate a call to the "inline" code as a subroutine
1390 slang_operation
*ret
= NULL
;
1392 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1396 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1398 /* check if this is a "tail" return */
1399 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1400 _slang_is_tail_return(inlined
)) {
1401 /* The only RETURN is the last stmt in the function, no-op it
1402 * and inline the function body.
1404 ret
->type
= SLANG_OPER_NONE
;
1407 slang_operation
*callOper
;
1408 /* The function we're calling has one or more 'return' statements.
1409 * So, we can't truly inline this function because we need to
1410 * implement 'return' with RET (and CAL).
1411 * Nevertheless, we performed "inlining" to make a new instance
1412 * of the function body to deal with static register allocation.
1414 * XXX check if there's one 'return' and if it's the very last
1415 * statement in the function - we can optimize that case.
1417 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1418 inlined
->type
== SLANG_OPER_SEQUENCE
);
1420 if (_slang_function_has_return_value(fun
) && !dest
) {
1421 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1422 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1423 callOper
= &inlined
->children
[1];
1428 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1429 callOper
->fun
= fun
;
1430 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1438 /* Replace the function call with the inlined block (or new CALL stmt) */
1439 slang_operation_destruct(oper
);
1441 _slang_free(inlined
);
1444 assert(inlined
->locals
);
1445 printf("*** Inlined code for call to %s:\n",
1446 (char*) fun
->header
.a_name
);
1447 slang_print_tree(oper
, 10);
1451 n
= _slang_gen_operation(A
, oper
);
1453 /*_slang_label_delete(A->curFuncEndLabel);*/
1454 A
->curFuncEndLabel
= prevFuncEndLabel
;
1456 if (A
->pragmas
->Debug
) {
1458 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1459 n
->Comment
= _slang_strdup(s
);
1466 static slang_asm_info
*
1467 slang_find_asm_info(const char *name
)
1470 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1471 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1480 * Some write-masked assignments are simple, but others are hard.
1483 * v.xy = vec2(a, b);
1486 * v.zy = vec2(a, b);
1487 * this gets transformed/swizzled into:
1488 * v.zy = vec2(a, b).*yx* (* = don't care)
1489 * This function helps to determine simple vs. non-simple.
1492 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1494 switch (writemask
) {
1496 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1498 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1500 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1502 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1504 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1505 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1507 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1508 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1509 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1510 case WRITEMASK_XYZW
:
1511 return swizzle
== SWIZZLE_NOOP
;
1519 * Convert the given swizzle into a writemask. In some cases this
1520 * is trivial, in other cases, we'll need to also swizzle the right
1521 * hand side to put components in the right places.
1522 * See comment above for more info.
1523 * XXX this function could be simplified and should probably be renamed.
1524 * \param swizzle the incoming swizzle
1525 * \param writemaskOut returns the writemask
1526 * \param swizzleOut swizzle to apply to the right-hand-side
1527 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1530 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1531 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1533 GLuint mask
= 0x0, newSwizzle
[4];
1536 /* make new dst writemask, compute size */
1537 for (i
= 0; i
< 4; i
++) {
1538 const GLuint swz
= GET_SWZ(swizzle
, i
);
1539 if (swz
== SWIZZLE_NIL
) {
1543 assert(swz
>= 0 && swz
<= 3);
1545 if (swizzle
!= SWIZZLE_XXXX
&&
1546 swizzle
!= SWIZZLE_YYYY
&&
1547 swizzle
!= SWIZZLE_ZZZZ
&&
1548 swizzle
!= SWIZZLE_WWWW
&&
1549 (mask
& (1 << swz
))) {
1550 /* a channel can't be specified twice (ex: ".xyyz") */
1551 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1552 _mesa_swizzle_string(swizzle
, 0, 0));
1558 assert(mask
<= 0xf);
1559 size
= i
; /* number of components in mask/swizzle */
1561 *writemaskOut
= mask
;
1563 /* make new src swizzle, by inversion */
1564 for (i
= 0; i
< 4; i
++) {
1565 newSwizzle
[i
] = i
; /*identity*/
1567 for (i
= 0; i
< size
; i
++) {
1568 const GLuint swz
= GET_SWZ(swizzle
, i
);
1569 newSwizzle
[swz
] = i
;
1571 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1576 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1578 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1580 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1582 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1584 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1592 #if 0 /* not used, but don't remove just yet */
1594 * Recursively traverse 'oper' to produce a swizzle mask in the event
1595 * of any vector subscripts and swizzle suffixes.
1596 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1599 resolve_swizzle(const slang_operation
*oper
)
1601 if (oper
->type
== SLANG_OPER_FIELD
) {
1602 /* writemask from .xyzw suffix */
1604 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1605 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1609 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1610 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1614 return SWIZZLE_XYZW
;
1616 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1617 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1618 /* writemask from [index] */
1619 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1620 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1625 swizzle
= SWIZZLE_XXXX
;
1628 swizzle
= SWIZZLE_YYYY
;
1631 swizzle
= SWIZZLE_ZZZZ
;
1634 swizzle
= SWIZZLE_WWWW
;
1637 swizzle
= SWIZZLE_XYZW
;
1639 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1643 return SWIZZLE_XYZW
;
1651 * Recursively descend through swizzle nodes to find the node's storage info.
1653 static slang_ir_storage
*
1654 get_store(const slang_ir_node
*n
)
1656 if (n
->Opcode
== IR_SWIZZLE
) {
1657 return get_store(n
->Children
[0]);
1665 * Generate IR tree for an asm instruction/operation such as:
1666 * __asm vec4_dot __retVal.x, v1, v2;
1668 static slang_ir_node
*
1669 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1670 slang_operation
*dest
)
1672 const slang_asm_info
*info
;
1673 slang_ir_node
*kids
[3], *n
;
1674 GLuint j
, firstOperand
;
1676 assert(oper
->type
== SLANG_OPER_ASM
);
1678 info
= slang_find_asm_info((char *) oper
->a_id
);
1680 _mesa_problem(NULL
, "undefined __asm function %s\n",
1681 (char *) oper
->a_id
);
1684 assert(info
->NumParams
<= 3);
1686 if (info
->NumParams
== oper
->num_children
) {
1687 /* Storage for result is not specified.
1688 * Children[0], [1], [2] are the operands.
1693 /* Storage for result (child[0]) is specified.
1694 * Children[1], [2], [3] are the operands.
1699 /* assemble child(ren) */
1700 kids
[0] = kids
[1] = kids
[2] = NULL
;
1701 for (j
= 0; j
< info
->NumParams
; j
++) {
1702 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1707 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1710 /* Setup n->Store to be a particular location. Otherwise, storage
1711 * for the result (a temporary) will be allocated later.
1713 slang_operation
*dest_oper
;
1716 dest_oper
= &oper
->children
[0];
1718 n0
= _slang_gen_operation(A
, dest_oper
);
1723 n
->Store
= n0
->Store
;
1725 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1736 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1739 for (i
= 0; i
< scope
->num_functions
; i
++) {
1740 slang_function
*f
= &scope
->functions
[i
];
1741 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1742 printf(" %s (%d args)\n", name
, f
->param_count
);
1745 if (scope
->outer_scope
)
1746 print_funcs(scope
->outer_scope
, name
);
1752 * Find a function of the given name, taking 'numArgs' arguments.
1753 * This is the function we'll try to call when there is no exact match
1754 * between function parameters and call arguments.
1756 * XXX we should really create a list of candidate functions and try
1759 static slang_function
*
1760 _slang_find_function_by_argc(slang_function_scope
*scope
,
1761 const char *name
, int numArgs
)
1765 for (i
= 0; i
< scope
->num_functions
; i
++) {
1766 slang_function
*f
= &scope
->functions
[i
];
1767 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1768 int haveRetValue
= _slang_function_has_return_value(f
);
1769 if (numArgs
== f
->param_count
- haveRetValue
)
1773 scope
= scope
->outer_scope
;
1780 static slang_function
*
1781 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1784 slang_function
*maxFunc
= NULL
;
1789 for (i
= 0; i
< scope
->num_functions
; i
++) {
1790 slang_function
*f
= &scope
->functions
[i
];
1791 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1792 if (f
->param_count
> maxArgs
) {
1793 maxArgs
= f
->param_count
;
1798 scope
= scope
->outer_scope
;
1806 * Generate a new slang_function which is a constructor for a user-defined
1809 static slang_function
*
1810 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1812 const GLint numFields
= str
->fields
->num_variables
;
1813 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1815 /* function header (name, return type) */
1816 fun
->header
.a_name
= str
->a_name
;
1817 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1818 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1819 fun
->header
.type
.specifier
._struct
= str
;
1821 /* function parameters (= struct's fields) */
1824 for (i
= 0; i
< numFields
; i
++) {
1826 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1828 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1829 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1830 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1832 fun
->param_count
= fun
->parameters
->num_variables
;
1835 /* Add __retVal to params */
1837 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1838 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1840 p
->a_name
= a_retVal
;
1841 p
->type
= fun
->header
.type
;
1842 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1846 /* function body is:
1856 slang_variable_scope
*scope
;
1857 slang_variable
*var
;
1860 fun
->body
= slang_operation_new(1);
1861 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1862 fun
->body
->num_children
= numFields
+ 2;
1863 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1865 scope
= fun
->body
->locals
;
1866 scope
->outer_scope
= fun
->parameters
;
1868 /* create local var 't' */
1869 var
= slang_variable_scope_grow(scope
);
1870 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1871 var
->type
= fun
->header
.type
;
1875 slang_operation
*decl
;
1877 decl
= &fun
->body
->children
[0];
1878 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1879 decl
->locals
= _slang_variable_scope_new(scope
);
1880 decl
->a_id
= var
->a_name
;
1883 /* assign params to fields of t */
1884 for (i
= 0; i
< numFields
; i
++) {
1885 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1887 assign
->type
= SLANG_OPER_ASSIGN
;
1888 assign
->locals
= _slang_variable_scope_new(scope
);
1889 assign
->num_children
= 2;
1890 assign
->children
= slang_operation_new(2);
1893 slang_operation
*lhs
= &assign
->children
[0];
1895 lhs
->type
= SLANG_OPER_FIELD
;
1896 lhs
->locals
= _slang_variable_scope_new(scope
);
1897 lhs
->num_children
= 1;
1898 lhs
->children
= slang_operation_new(1);
1899 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1901 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1902 lhs
->children
[0].a_id
= var
->a_name
;
1903 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1906 lhs
->children
[1].num_children
= 1;
1907 lhs
->children
[1].children
= slang_operation_new(1);
1908 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1909 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1910 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1915 slang_operation
*rhs
= &assign
->children
[1];
1917 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1918 rhs
->locals
= _slang_variable_scope_new(scope
);
1919 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1925 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1927 ret
->type
= SLANG_OPER_RETURN
;
1928 ret
->locals
= _slang_variable_scope_new(scope
);
1929 ret
->num_children
= 1;
1930 ret
->children
= slang_operation_new(1);
1931 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1932 ret
->children
[0].a_id
= var
->a_name
;
1933 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1937 slang_print_function(fun, 1);
1944 * Find/create a function (constructor) for the given structure name.
1946 static slang_function
*
1947 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1950 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1951 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1952 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1953 /* found a structure type that matches the function name */
1954 if (!str
->constructor
) {
1955 /* create the constructor function now */
1956 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1958 return str
->constructor
;
1966 * Generate a new slang_function to satisfy a call to an array constructor.
1967 * Ex: float[3](1., 2., 3.)
1969 static slang_function
*
1970 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1972 slang_type_specifier_type baseType
;
1973 slang_function
*fun
;
1976 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1980 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1982 num_elements
= oper
->num_children
;
1984 /* function header, return type */
1986 fun
->header
.a_name
= oper
->a_id
;
1987 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1988 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1989 fun
->header
.type
.specifier
._array
=
1990 slang_type_specifier_new(baseType
, NULL
, NULL
);
1991 fun
->header
.type
.array_len
= num_elements
;
1994 /* function parameters (= number of elements) */
1997 for (i
= 0; i
< num_elements
; i
++) {
1999 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2001 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2003 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2004 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2005 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2006 p
->type
.specifier
.type
= baseType
;
2008 fun
->param_count
= fun
->parameters
->num_variables
;
2011 /* Add __retVal to params */
2013 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2014 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2016 p
->a_name
= a_retVal
;
2017 p
->type
= fun
->header
.type
;
2018 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2019 p
->type
.specifier
.type
= baseType
;
2023 /* function body is:
2033 slang_variable_scope
*scope
;
2034 slang_variable
*var
;
2037 fun
->body
= slang_operation_new(1);
2038 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2039 fun
->body
->num_children
= num_elements
+ 2;
2040 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2042 scope
= fun
->body
->locals
;
2043 scope
->outer_scope
= fun
->parameters
;
2045 /* create local var 't' */
2046 var
= slang_variable_scope_grow(scope
);
2047 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2048 var
->type
= fun
->header
.type
;/*XXX copy*/
2052 slang_operation
*decl
;
2054 decl
= &fun
->body
->children
[0];
2055 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2056 decl
->locals
= _slang_variable_scope_new(scope
);
2057 decl
->a_id
= var
->a_name
;
2060 /* assign params to elements of t */
2061 for (i
= 0; i
< num_elements
; i
++) {
2062 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2064 assign
->type
= SLANG_OPER_ASSIGN
;
2065 assign
->locals
= _slang_variable_scope_new(scope
);
2066 assign
->num_children
= 2;
2067 assign
->children
= slang_operation_new(2);
2070 slang_operation
*lhs
= &assign
->children
[0];
2072 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2073 lhs
->locals
= _slang_variable_scope_new(scope
);
2074 lhs
->num_children
= 2;
2075 lhs
->children
= slang_operation_new(2);
2077 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2078 lhs
->children
[0].a_id
= var
->a_name
;
2079 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2081 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2082 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2086 slang_operation
*rhs
= &assign
->children
[1];
2088 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2089 rhs
->locals
= _slang_variable_scope_new(scope
);
2090 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2096 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2098 ret
->type
= SLANG_OPER_RETURN
;
2099 ret
->locals
= _slang_variable_scope_new(scope
);
2100 ret
->num_children
= 1;
2101 ret
->children
= slang_operation_new(1);
2102 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2103 ret
->children
[0].a_id
= var
->a_name
;
2104 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2109 slang_print_function(fun, 1);
2117 _slang_is_vec_mat_type(const char *name
)
2119 static const char *vecmat_types
[] = {
2120 "float", "int", "bool",
2121 "vec2", "vec3", "vec4",
2122 "ivec2", "ivec3", "ivec4",
2123 "bvec2", "bvec3", "bvec4",
2124 "mat2", "mat3", "mat4",
2125 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2129 for (i
= 0; vecmat_types
[i
]; i
++)
2130 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2137 * Assemble a function call, given a particular function name.
2138 * \param name the function's name (operators like '*' are possible).
2140 static slang_ir_node
*
2141 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2142 slang_operation
*oper
, slang_operation
*dest
)
2144 slang_operation
*params
= oper
->children
;
2145 const GLuint param_count
= oper
->num_children
;
2147 slang_function
*fun
;
2150 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2151 if (atom
== SLANG_ATOM_NULL
)
2154 if (oper
->array_constructor
) {
2155 /* this needs special handling */
2156 fun
= _slang_make_array_constructor(A
, oper
);
2159 /* Try to find function by name and exact argument type matching */
2160 GLboolean error
= GL_FALSE
;
2161 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2162 &A
->space
, A
->atoms
, A
->log
, &error
);
2164 slang_info_log_error(A
->log
,
2165 "Function '%s' not found (check argument types)",
2172 /* Next, try locating a constructor function for a user-defined type */
2173 fun
= _slang_locate_struct_constructor(A
, name
);
2177 * At this point, some heuristics are used to try to find a function
2178 * that matches the calling signature by means of casting or "unrolling"
2182 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2183 /* Next, if this call looks like a vec() or mat() constructor call,
2184 * try "unwinding" the args to satisfy a constructor.
2186 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2188 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2189 slang_info_log_error(A
->log
,
2190 "Function '%s' not found (check argument types)",
2197 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2198 /* Next, try casting args to the types of the formal parameters */
2199 int numArgs
= oper
->num_children
;
2200 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2201 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2202 slang_info_log_error(A
->log
,
2203 "Function '%s' not found (check argument types)",
2211 slang_info_log_error(A
->log
,
2212 "Function '%s' not found (check argument types)",
2218 /* The function body may be in another compilation unit.
2219 * We'll try concatenating the shaders and recompile at link time.
2221 A
->UnresolvedRefs
= GL_TRUE
;
2222 return new_node1(IR_NOP
, NULL
);
2225 /* type checking to be sure function's return type matches 'dest' type */
2229 slang_typeinfo_construct(&t0
);
2230 typeof_operation(A
, dest
, &t0
);
2232 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2233 slang_info_log_error(A
->log
,
2234 "Incompatible type returned by call to '%s'",
2240 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2242 if (n
&& !n
->Store
&& !dest
2243 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2244 /* setup n->Store for the result of the function call */
2245 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2246 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2247 /*printf("Alloc storage for function result, size %d \n", size);*/
2250 if (oper
->array_constructor
) {
2251 /* free the temporary array constructor function now */
2252 slang_function_destruct(fun
);
2259 static slang_ir_node
*
2260 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2262 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2264 slang_variable
*var
;
2266 /* NOTE: In GLSL 1.20, there's only one kind of method
2267 * call: array.length(). Anything else is an error.
2269 if (oper
->a_id
!= a_length
) {
2270 slang_info_log_error(A
->log
,
2271 "Undefined method call '%s'", (char *) oper
->a_id
);
2275 /* length() takes no arguments */
2276 if (oper
->num_children
> 0) {
2277 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2281 /* lookup the object/variable */
2282 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2283 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2284 slang_info_log_error(A
->log
,
2285 "Undefined object '%s'", (char *) oper
->a_obj
);
2289 /* Create a float/literal IR node encoding the array length */
2290 n
= new_node0(IR_FLOAT
);
2292 n
->Value
[0] = (float) _slang_array_length(var
);
2293 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2300 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2302 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2303 oper
->type
== SLANG_OPER_LITERAL_INT
||
2304 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2305 if (oper
->literal
[0])
2311 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2312 oper
->num_children
== 1) {
2313 return _slang_is_constant_cond(&oper
->children
[0], value
);
2320 * Test if an operation is a scalar or boolean.
2323 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2325 slang_typeinfo type
;
2328 slang_typeinfo_construct(&type
);
2329 typeof_operation(A
, oper
, &type
);
2330 size
= _slang_sizeof_type_specifier(&type
.spec
);
2331 slang_typeinfo_destruct(&type
);
2337 * Test if an operation is boolean.
2340 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2342 slang_typeinfo type
;
2345 slang_typeinfo_construct(&type
);
2346 typeof_operation(A
, oper
, &type
);
2347 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2348 slang_typeinfo_destruct(&type
);
2354 * Check if a loop contains a 'continue' statement.
2355 * Stop looking if we find a nested loop.
2358 _slang_loop_contains_continue(const slang_operation
*oper
)
2360 switch (oper
->type
) {
2361 case SLANG_OPER_CONTINUE
:
2363 case SLANG_OPER_FOR
:
2365 case SLANG_OPER_WHILE
:
2366 /* stop upon finding a nested loop */
2372 for (i
= 0; i
< oper
->num_children
; i
++) {
2373 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2374 if (_slang_loop_contains_continue(child
))
2384 * Check if a loop contains a 'continue' or 'break' statement.
2385 * Stop looking if we find a nested loop.
2388 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2390 switch (oper
->type
) {
2391 case SLANG_OPER_CONTINUE
:
2392 case SLANG_OPER_BREAK
:
2394 case SLANG_OPER_FOR
:
2396 case SLANG_OPER_WHILE
:
2397 /* stop upon finding a nested loop */
2403 for (i
= 0; i
< oper
->num_children
; i
++) {
2404 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2405 if (_slang_loop_contains_continue_or_break(child
))
2414 static slang_ir_node
*
2415 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2418 slang_operation
*top
;
2419 slang_operation
*innerBody
;
2421 assert(oper
->type
== SLANG_OPER_DO
);
2423 top
= slang_operation_new(1);
2424 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2425 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2426 slang_operation_add_children(top
, 2);
2428 /* declare: bool _notBreakFlag = true */
2430 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2431 slang_variable
*var
;
2433 condDecl
->type
= SLANG_OPER_VARIABLE_DECL
;
2434 var
= slang_variable_scope_grow(top
->locals
);
2435 slang_fully_specified_type_construct(&var
->type
);
2436 var
->type
.specifier
.type
= SLANG_SPEC_BOOL
;
2437 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "_notBreakFlag");
2438 condDecl
->a_id
= var
->a_name
;
2439 var
->initializer
= slang_operation_new(1);
2440 slang_operation_literal_bool(var
->initializer
, GL_TRUE
);
2443 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2445 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2446 outerDo
->type
= SLANG_OPER_DO
;
2447 slang_operation_add_children(outerDo
, 2);
2451 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2452 innerDo
->type
= SLANG_OPER_DO
;
2453 slang_operation_add_children(innerDo
, 2);
2455 /* copy original do-loop body into inner do-loop's body */
2456 innerBody
= slang_oper_child(innerDo
, 0);
2457 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2458 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2460 /* inner do-loop's condition is constant/false */
2462 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2463 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2467 /* _notBreakFlag && LOOPCOND */
2469 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2470 cond
->type
= SLANG_OPER_LOGICALAND
;
2471 slang_operation_add_children(cond
, 2);
2473 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2474 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2477 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2478 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2483 /* Finally, in innerBody,
2484 * replace "break" with "_notBreakFlag = 0; break"
2485 * replace "continue" with "break"
2487 replace_break_and_cont(A
, innerBody
);
2489 slang_print_tree(top
, 0);
2491 return _slang_gen_operation(A
, top
);
2498 * Generate loop code using high-level IR_LOOP instruction
2500 static slang_ir_node
*
2501 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2505 * BREAK if !expr (child[0])
2506 * body code (child[1])
2508 const slang_operation
*prevLoopOper
;
2509 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2510 GLboolean isConst
, constTrue
;
2512 if (!A
->EmitContReturn
) {
2513 /* We don't want to emit CONT instructions. If this while-loop has
2514 * a continue, translate it away.
2516 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2517 return _slang_gen_while_without_continue(A
, oper
);
2521 /* type-check expression */
2522 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2523 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2527 /* Check if loop condition is a constant */
2528 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2530 if (isConst
&& !constTrue
) {
2531 /* loop is never executed! */
2532 return new_node0(IR_NOP
);
2535 loop
= new_loop(NULL
);
2537 /* save old, push new loop */
2538 prevLoop
= A
->CurLoop
;
2540 prevLoopOper
= A
->CurLoopOper
;
2541 A
->CurLoopOper
= oper
;
2543 if (isConst
&& constTrue
) {
2544 /* while(nonzero constant), no conditional break */
2549 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2550 breakIf
= new_break_if_true(A
, cond
);
2552 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2553 loop
->Children
[0] = new_seq(breakIf
, body
);
2555 /* Do infinite loop detection */
2556 /* loop->List is head of linked list of break/continue nodes */
2557 if (!loop
->List
&& isConst
&& constTrue
) {
2558 /* infinite loop detected */
2559 A
->CurLoop
= prevLoop
; /* clean-up */
2560 slang_info_log_error(A
->log
, "Infinite loop detected!");
2564 /* pop loop, restore prev */
2565 A
->CurLoop
= prevLoop
;
2566 A
->CurLoopOper
= prevLoopOper
;
2573 * Replace 'break' and 'continue' statements inside a do-while loop.
2574 * This is a recursive helper function used by _slang_gen_do_without_continue().
2577 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2579 switch (oper
->type
) {
2580 case SLANG_OPER_BREAK
:
2581 /* replace 'break' with "_notBreakFlag = false; break" */
2583 slang_operation
*block
= oper
;
2584 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2585 slang_operation_add_children(block
, 2);
2587 slang_operation
*assign
= slang_oper_child(block
, 0);
2588 assign
->type
= SLANG_OPER_ASSIGN
;
2589 slang_operation_add_children(assign
, 2);
2591 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2592 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2595 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2596 slang_operation_literal_bool(rhs
, GL_FALSE
);
2600 slang_operation
*brk
= slang_oper_child(block
, 1);
2601 brk
->type
= SLANG_OPER_BREAK
;
2602 assert(!brk
->children
);
2606 case SLANG_OPER_CONTINUE
:
2607 /* convert continue into a break */
2608 oper
->type
= SLANG_OPER_BREAK
;
2610 case SLANG_OPER_FOR
:
2612 case SLANG_OPER_WHILE
:
2613 /* stop upon finding a nested loop */
2619 for (i
= 0; i
< oper
->num_children
; i
++) {
2620 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2628 * Transform a do-while-loop so that continue statements are converted to breaks.
2629 * Then do normal IR code generation.
2640 * } while (LOOPCOND);
2645 * bool _notBreakFlag = 1;
2650 * break; // was continue
2653 * _notBreakFlag = 0; // was
2657 * } while (_notBreakFlag && LOOPCOND);
2660 static slang_ir_node
*
2661 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2663 slang_operation
*top
;
2664 slang_operation
*innerBody
;
2666 assert(oper
->type
== SLANG_OPER_DO
);
2668 top
= slang_operation_new(1);
2669 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2670 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2671 slang_operation_add_children(top
, 2);
2673 /* declare: bool _notBreakFlag = true */
2675 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2676 slang_variable
*var
;
2678 condDecl
->type
= SLANG_OPER_VARIABLE_DECL
;
2679 var
= slang_variable_scope_grow(top
->locals
);
2680 slang_fully_specified_type_construct(&var
->type
);
2681 var
->type
.specifier
.type
= SLANG_SPEC_BOOL
;
2682 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "_notBreakFlag");
2683 condDecl
->a_id
= var
->a_name
;
2684 var
->initializer
= slang_operation_new(1);
2685 slang_operation_literal_bool(var
->initializer
, GL_TRUE
);
2688 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2690 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2691 outerDo
->type
= SLANG_OPER_DO
;
2692 slang_operation_add_children(outerDo
, 2);
2696 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2697 innerDo
->type
= SLANG_OPER_DO
;
2698 slang_operation_add_children(innerDo
, 2);
2700 /* copy original do-loop body into inner do-loop's body */
2701 innerBody
= slang_oper_child(innerDo
, 0);
2702 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2703 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2705 /* inner do-loop's condition is constant/false */
2707 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2708 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2712 /* _notBreakFlag && LOOPCOND */
2714 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2715 cond
->type
= SLANG_OPER_LOGICALAND
;
2716 slang_operation_add_children(cond
, 2);
2718 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2719 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2722 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2723 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2728 /* Finally, in innerBody,
2729 * replace "break" with "_notBreakFlag = 0; break"
2730 * replace "continue" with "break"
2732 replace_break_and_cont(A
, innerBody
);
2734 slang_print_tree(top
, 0);
2736 return _slang_gen_operation(A
, top
);
2741 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2743 static slang_ir_node
*
2744 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2748 * body code (child[0])
2750 * BREAK if !expr (child[1])
2752 const slang_operation
*prevLoopOper
;
2753 slang_ir_node
*prevLoop
, *loop
;
2754 GLboolean isConst
, constTrue
;
2756 if (!A
->EmitContReturn
) {
2757 /* We don't want to emit CONT instructions. If this do-loop has
2758 * a continue, translate it away.
2760 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2761 return _slang_gen_do_without_continue(A
, oper
);
2765 /* type-check expression */
2766 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2767 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2771 loop
= new_loop(NULL
);
2773 /* save old, push new loop */
2774 prevLoop
= A
->CurLoop
;
2776 prevLoopOper
= A
->CurLoopOper
;
2777 A
->CurLoopOper
= oper
;
2780 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2782 /* Check if loop condition is a constant */
2783 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2784 if (isConst
&& constTrue
) {
2785 /* do { } while(1) ==> no conditional break */
2786 loop
->Children
[1] = NULL
; /* no tail code */
2790 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2791 loop
->Children
[1] = new_break_if_true(A
, cond
);
2794 /* XXX we should do infinite loop detection, as above */
2796 /* pop loop, restore prev */
2797 A
->CurLoop
= prevLoop
;
2798 A
->CurLoopOper
= prevLoopOper
;
2805 * Recursively count the number of operations rooted at 'oper'.
2806 * This gives some kind of indication of the size/complexity of an operation.
2809 sizeof_operation(const slang_operation
*oper
)
2812 GLuint count
= 1; /* me */
2814 for (i
= 0; i
< oper
->num_children
; i
++) {
2815 count
+= sizeof_operation(&oper
->children
[i
]);
2826 * Determine if a for-loop can be unrolled.
2827 * At this time, only a rather narrow class of for loops can be unrolled.
2828 * See code for details.
2829 * When a loop can't be unrolled because it's too large we'll emit a
2830 * message to the log.
2833 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2837 const char *varName
;
2840 if (oper
->type
!= SLANG_OPER_FOR
)
2843 assert(oper
->num_children
== 4);
2845 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
2848 /* children[0] must be either "int i=constant" or "i=constant" */
2849 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2850 slang_variable
*var
;
2852 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2855 varId
= oper
->children
[0].children
[0].a_id
;
2857 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2861 if (!var
->initializer
)
2863 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2865 start
= (GLint
) var
->initializer
->literal
[0];
2867 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2868 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2870 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2872 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2875 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2877 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2883 /* children[1] must be "i<constant" */
2884 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2886 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2888 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2890 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2893 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2895 /* children[2] must be "i++" or "++i" */
2896 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2897 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2899 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2902 /* make sure the same variable name is used in all places */
2903 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2904 (oper
->children
[2].children
[0].a_id
!= varId
))
2907 varName
= (const char *) varId
;
2909 /* children[3], the loop body, can't be too large */
2910 bodySize
= sizeof_operation(&oper
->children
[3]);
2911 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2912 slang_info_log_print(A
->log
,
2913 "Note: 'for (%s ... )' body is too large/complex"
2920 return GL_FALSE
; /* degenerate case */
2922 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2923 slang_info_log_print(A
->log
,
2924 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2925 " many iterations to unroll",
2926 varName
, start
, varName
, end
, varName
);
2930 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2931 slang_info_log_print(A
->log
,
2932 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2933 " too much code to unroll",
2934 varName
, start
, varName
, end
, varName
);
2938 return GL_TRUE
; /* we can unroll the loop */
2943 * Unroll a for-loop.
2944 * First we determine the number of iterations to unroll.
2945 * Then for each iteration:
2946 * make a copy of the loop body
2947 * replace instances of the loop variable with the current iteration value
2948 * generate IR code for the body
2949 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2951 static slang_ir_node
*
2952 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2954 GLint start
, end
, iter
;
2955 slang_ir_node
*n
, *root
= NULL
;
2958 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2959 /* for (int i=0; ... */
2960 slang_variable
*var
;
2962 varId
= oper
->children
[0].children
[0].a_id
;
2963 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2965 start
= (GLint
) var
->initializer
->literal
[0];
2969 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2970 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2973 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2975 for (iter
= start
; iter
< end
; iter
++) {
2976 slang_operation
*body
;
2978 /* make a copy of the loop body */
2979 body
= slang_operation_new(1);
2983 if (!slang_operation_copy(body
, &oper
->children
[3]))
2986 /* in body, replace instances of 'varId' with literal 'iter' */
2988 slang_variable
*oldVar
;
2989 slang_operation
*newOper
;
2991 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2993 /* undeclared loop variable */
2994 slang_operation_delete(body
);
2998 newOper
= slang_operation_new(1);
2999 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3000 newOper
->literal_size
= 1;
3001 newOper
->literal
[0] = iter
;
3003 /* replace instances of the loop variable with newOper */
3004 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3007 /* do IR codegen for body */
3008 n
= _slang_gen_operation(A
, body
);
3012 root
= new_seq(root
, n
);
3014 slang_operation_delete(body
);
3022 * Replace 'continue' statement with 'break' inside a for-loop.
3023 * This is a recursive helper function used by _slang_gen_for_without_continue().
3026 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3028 switch (oper
->type
) {
3029 case SLANG_OPER_CONTINUE
:
3030 oper
->type
= SLANG_OPER_BREAK
;
3032 case SLANG_OPER_FOR
:
3034 case SLANG_OPER_WHILE
:
3035 /* stop upon finding a nested loop */
3041 for (i
= 0; i
< oper
->num_children
; i
++) {
3042 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3050 * Transform a for-loop so that continue statements are converted to breaks.
3051 * Then do normal IR code generation.
3055 * for (INIT; LOOPCOND; INCR) {
3066 * bool _condFlag = 1;
3067 * for (INIT; _condFlag; ) {
3068 * for ( ; _condFlag = LOOPCOND; INCR) {
3080 static slang_ir_node
*
3081 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3083 slang_operation
*top
;
3084 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3085 slang_operation
*lhs
, *rhs
;
3087 assert(oper
->type
== SLANG_OPER_FOR
);
3089 top
= slang_operation_new(1);
3090 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3091 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3092 slang_operation_add_children(top
, 2);
3094 /* declare: bool _condFlag = true */
3096 slang_operation
*condDecl
;
3097 slang_variable
*var
;
3099 condDecl
= slang_oper_child(top
, 0);
3100 condDecl
->type
= SLANG_OPER_VARIABLE_DECL
;
3101 var
= slang_variable_scope_grow(top
->locals
);
3102 slang_fully_specified_type_construct(&var
->type
);
3103 var
->type
.specifier
.type
= SLANG_SPEC_BOOL
;
3104 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3105 condDecl
->a_id
= var
->a_name
;
3106 var
->initializer
= slang_operation_new(1);
3107 slang_operation_literal_bool(var
->initializer
, GL_TRUE
);
3110 /* build outer loop: for (INIT; _condFlag; ) { */
3111 outerFor
= slang_oper_child(top
, 1);
3112 outerFor
->type
= SLANG_OPER_FOR
;
3113 slang_operation_add_children(outerFor
, 4);
3115 init
= slang_oper_child(outerFor
, 0);
3116 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3118 cond
= slang_oper_child(outerFor
, 1);
3119 cond
->type
= SLANG_OPER_IDENTIFIER
;
3120 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3122 incr
= slang_oper_child(outerFor
, 2);
3123 incr
->type
= SLANG_OPER_VOID
;
3125 /* body of the outer loop */
3127 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3129 slang_operation_add_children(block
, 2);
3130 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3132 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3134 innerFor
= slang_oper_child(block
, 0);
3136 /* make copy of orig loop */
3137 slang_operation_copy(innerFor
, oper
);
3138 assert(innerFor
->type
== SLANG_OPER_FOR
);
3139 innerFor
->locals
->outer_scope
= block
->locals
;
3141 init
= slang_oper_child(innerFor
, 0);
3142 init
->type
= SLANG_OPER_VOID
; /* leak? */
3144 cond
= slang_oper_child(innerFor
, 1);
3145 slang_operation_destruct(cond
);
3146 cond
->type
= SLANG_OPER_ASSIGN
;
3147 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3148 slang_operation_add_children(cond
, 2);
3150 lhs
= slang_oper_child(cond
, 0);
3151 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3152 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3154 rhs
= slang_oper_child(cond
, 1);
3155 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3158 /* if (_condFlag) INCR; */
3160 slang_operation
*ifop
= slang_oper_child(block
, 1);
3161 ifop
->type
= SLANG_OPER_IF
;
3162 slang_operation_add_children(ifop
, 2);
3164 /* re-use cond node build above */
3165 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3167 /* incr node from original for-loop operation */
3168 slang_operation_copy(slang_oper_child(ifop
, 1),
3169 slang_oper_child(oper
, 2));
3172 /* finally, replace "continue" with "break" in the inner for-loop */
3173 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3176 return _slang_gen_operation(A
, top
);
3182 * Generate IR for a for-loop. Unrolling will be done when possible.
3184 static slang_ir_node
*
3185 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3189 if (!A
->EmitContReturn
) {
3190 /* We don't want to emit CONT instructions. If this for-loop has
3191 * a continue, translate it away.
3193 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3194 return _slang_gen_for_without_continue(A
, oper
);
3198 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3200 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3205 assert(oper
->type
== SLANG_OPER_FOR
);
3207 /* conventional for-loop code generation */
3210 * init code (child[0])
3212 * BREAK if !expr (child[1])
3213 * body code (child[3])
3215 * incr code (child[2]) // XXX continue here
3217 const slang_operation
*prevLoopOper
;
3218 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3219 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3220 loop
= new_loop(NULL
);
3222 /* save old, push new loop */
3223 prevLoop
= A
->CurLoop
;
3225 prevLoopOper
= A
->CurLoopOper
;
3226 A
->CurLoopOper
= oper
;
3228 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3229 breakIf
= new_break_if_true(A
, cond
);
3230 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3231 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3233 loop
->Children
[0] = new_seq(breakIf
, body
);
3234 loop
->Children
[1] = incr
; /* tail code */
3236 /* pop loop, restore prev */
3237 A
->CurLoop
= prevLoop
;
3238 A
->CurLoopOper
= prevLoopOper
;
3240 return new_seq(init
, loop
);
3245 static slang_ir_node
*
3246 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3248 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3250 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3251 loopNode
= A
->CurLoop
;
3253 assert(loopNode
->Opcode
== IR_LOOP
);
3255 cont
= new_node0(IR_CONT
);
3257 cont
->Parent
= loopNode
;
3258 /* insert this node at head of linked list of cont/break instructions */
3259 cont
->List
= loopNode
->List
;
3260 loopNode
->List
= cont
;
3263 n
= new_seq(incr
, cont
);
3269 * Determine if the given operation is of a specific type.
3272 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3274 if (oper
->type
== type
)
3276 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3277 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3278 oper
->num_children
== 1)
3279 return is_operation_type(&oper
->children
[0], type
);
3286 * Generate IR tree for an if/then/else conditional using high-level
3287 * IR_IF instruction.
3289 static slang_ir_node
*
3290 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3293 * eval expr (child[0])
3300 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3301 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3302 GLboolean isConst
, constTrue
;
3304 /* type-check expression */
3305 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3306 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3310 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3311 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3315 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3319 return _slang_gen_operation(A
, &oper
->children
[1]);
3322 /* if (false) ... */
3323 return _slang_gen_operation(A
, &oper
->children
[2]);
3327 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3328 cond
= new_cond(cond
);
3330 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3331 && !haveElseClause
) {
3332 /* Special case: generate a conditional break */
3333 if (!A
->CurLoop
) /* probably trying to unroll */
3335 ifBody
= new_break_if_true(A
, cond
);
3338 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3341 && A
->CurLoopOper
->type
!= SLANG_OPER_FOR
) {
3342 /* Special case: generate a conditional continue */
3343 if (!A
->CurLoop
) /* probably trying to unroll */
3345 ifBody
= new_cont_if_true(A
, cond
);
3350 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3352 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3355 ifNode
= new_if(cond
, ifBody
, elseBody
);
3362 static slang_ir_node
*
3363 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3367 assert(oper
->type
== SLANG_OPER_NOT
);
3369 /* type-check expression */
3370 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3371 slang_info_log_error(A
->log
,
3372 "scalar/boolean expression expected for '!'");
3376 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3384 static slang_ir_node
*
3385 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3387 slang_ir_node
*n1
, *n2
;
3389 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3391 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3392 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3393 slang_info_log_error(A
->log
,
3394 "scalar/boolean expressions expected for '^^'");
3398 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3401 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3404 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3409 * Generate IR node for storage of a temporary of given size.
3411 static slang_ir_node
*
3412 _slang_gen_temporary(GLint size
)
3414 slang_ir_storage
*store
;
3415 slang_ir_node
*n
= NULL
;
3417 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3419 n
= new_node0(IR_VAR_DECL
);
3432 * Generate program constants for an array.
3433 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3434 * This will allocate and initialize three vector constants, storing
3435 * the array in constant memory, not temporaries like a non-const array.
3436 * This can also be used for uniform array initializers.
3437 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3440 make_constant_array(slang_assemble_ctx
*A
,
3441 slang_variable
*var
,
3442 slang_operation
*initializer
)
3444 struct gl_program
*prog
= A
->program
;
3445 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3446 const char *varName
= (char *) var
->a_name
;
3447 const GLuint numElements
= initializer
->num_children
;
3453 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3455 size
= var
->store
->Size
;
3457 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3458 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3459 assert(initializer
->type
== SLANG_OPER_CALL
);
3460 assert(initializer
->array_constructor
);
3462 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3464 /* convert constructor params into ordinary floats */
3465 for (i
= 0; i
< numElements
; i
++) {
3466 const slang_operation
*op
= &initializer
->children
[i
];
3467 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3468 /* unsupported type for this optimization */
3472 for (j
= 0; j
< op
->literal_size
; j
++) {
3473 values
[i
* 4 + j
] = op
->literal
[j
];
3475 for ( ; j
< 4; j
++) {
3476 values
[i
* 4 + j
] = 0.0f
;
3480 /* slightly different paths for constants vs. uniforms */
3481 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3482 var
->store
->File
= PROGRAM_UNIFORM
;
3483 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3484 size
, datatype
, values
);
3487 var
->store
->File
= PROGRAM_CONSTANT
;
3488 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3491 assert(var
->store
->Size
== size
);
3501 * Generate IR node for allocating/declaring a variable (either a local or
3503 * Generally, this involves allocating an slang_ir_storage instance for the
3504 * variable, choosing a register file (temporary, constant, etc).
3505 * For ordinary variables we do not yet allocate storage though. We do that
3506 * when we find the first actual use of the variable to avoid allocating temp
3507 * regs that will never get used.
3508 * At this time, uniforms are always allocated space in this function.
3510 * \param initializer Optional initializer expression for the variable.
3512 static slang_ir_node
*
3513 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3514 slang_operation
*initializer
)
3516 const char *varName
= (const char *) var
->a_name
;
3517 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3518 slang_ir_node
*varDecl
, *n
;
3519 slang_ir_storage
*store
;
3520 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3521 gl_register_file file
;
3523 /*assert(!var->declared);*/
3524 var
->declared
= GL_TRUE
;
3526 /* determine GPU register file for simple cases */
3527 if (is_sampler_type(&var
->type
)) {
3528 file
= PROGRAM_SAMPLER
;
3530 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3531 file
= PROGRAM_UNIFORM
;
3534 file
= PROGRAM_TEMPORARY
;
3537 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3539 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3543 arrayLen
= _slang_array_length(var
);
3544 totalSize
= _slang_array_size(size
, arrayLen
);
3546 /* Allocate IR node for the declaration */
3547 varDecl
= new_node0(IR_VAR_DECL
);
3551 /* Allocate slang_ir_storage for this variable if needed.
3552 * Note that we may not actually allocate a constant or temporary register
3556 GLint index
= -7; /* TBD / unknown */
3557 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3559 return NULL
; /* out of memory */
3562 /* set the IR node's Var and Store pointers */
3564 varDecl
->Store
= var
->store
;
3569 /* if there's an initializer, generate IR for the expression */
3571 slang_ir_node
*varRef
, *init
;
3573 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3574 /* if the variable is const, the initializer must be a const
3575 * expression as well.
3578 if (!_slang_is_constant_expr(initializer
)) {
3579 slang_info_log_error(A
->log
,
3580 "initializer for %s not constant", varName
);
3586 /* IR for the variable we're initializing */
3587 varRef
= new_var(A
, var
);
3589 slang_info_log_error(A
->log
, "out of memory");
3593 /* constant-folding, etc here */
3594 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3596 /* look for simple constant-valued variables and uniforms */
3597 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3598 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3600 if (initializer
->type
== SLANG_OPER_CALL
&&
3601 initializer
->array_constructor
) {
3602 /* array initializer */
3603 if (make_constant_array(A
, var
, initializer
))
3606 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3607 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3608 /* simple float/vector initializer */
3609 if (store
->File
== PROGRAM_UNIFORM
) {
3610 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3612 totalSize
, datatype
,
3613 initializer
->literal
);
3614 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3619 store
->File
= PROGRAM_CONSTANT
;
3620 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3622 initializer
->literal
,
3624 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3631 /* IR for initializer */
3632 init
= _slang_gen_operation(A
, initializer
);
3636 /* XXX remove this when type checking is added above */
3637 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3638 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3642 /* assign RHS to LHS */
3643 n
= new_node2(IR_COPY
, varRef
, init
);
3644 n
= new_seq(varDecl
, n
);
3647 /* no initializer */
3651 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3652 /* always need to allocate storage for uniforms at this point */
3653 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3654 totalSize
, datatype
, NULL
);
3655 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3659 printf("%s var %p %s store=%p index=%d size=%d\n",
3660 __FUNCTION__
, (void *) var
, (char *) varName
,
3661 (void *) store
, store
->Index
, store
->Size
);
3669 * Generate code for a selection expression: b ? x : y
3670 * XXX In some cases we could implement a selection expression
3671 * with an LRP instruction (use the boolean as the interpolant).
3672 * Otherwise, we use an IF/ELSE/ENDIF construct.
3674 static slang_ir_node
*
3675 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3677 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3678 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3679 slang_typeinfo type0
, type1
, type2
;
3680 int size
, isBool
, isEqual
;
3682 assert(oper
->type
== SLANG_OPER_SELECT
);
3683 assert(oper
->num_children
== 3);
3685 /* type of children[0] must be boolean */
3686 slang_typeinfo_construct(&type0
);
3687 typeof_operation(A
, &oper
->children
[0], &type0
);
3688 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3689 slang_typeinfo_destruct(&type0
);
3691 slang_info_log_error(A
->log
, "selector type is not boolean");
3695 slang_typeinfo_construct(&type1
);
3696 slang_typeinfo_construct(&type2
);
3697 typeof_operation(A
, &oper
->children
[1], &type1
);
3698 typeof_operation(A
, &oper
->children
[2], &type2
);
3699 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3700 slang_typeinfo_destruct(&type1
);
3701 slang_typeinfo_destruct(&type2
);
3703 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3707 /* size of x or y's type */
3708 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3712 tmpDecl
= _slang_gen_temporary(size
);
3714 /* the condition (child 0) */
3715 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3716 cond
= new_cond(cond
);
3718 /* if-true body (child 1) */
3719 tmpVar
= new_node0(IR_VAR
);
3720 tmpVar
->Store
= tmpDecl
->Store
;
3721 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3722 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3724 /* if-false body (child 2) */
3725 tmpVar
= new_node0(IR_VAR
);
3726 tmpVar
->Store
= tmpDecl
->Store
;
3727 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3728 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3730 ifNode
= new_if(cond
, trueNode
, falseNode
);
3733 tmpVar
= new_node0(IR_VAR
);
3734 tmpVar
->Store
= tmpDecl
->Store
;
3736 tree
= new_seq(ifNode
, tmpVar
);
3737 tree
= new_seq(tmpDecl
, tree
);
3739 /*_slang_print_ir_tree(tree, 10);*/
3745 * Generate code for &&.
3747 static slang_ir_node
*
3748 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3750 /* rewrite "a && b" as "a ? b : false" */
3751 slang_operation
*select
;
3754 select
= slang_operation_new(1);
3755 select
->type
= SLANG_OPER_SELECT
;
3756 select
->num_children
= 3;
3757 select
->children
= slang_operation_new(3);
3759 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3760 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3761 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3762 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3763 select
->children
[2].literal_size
= 1;
3765 n
= _slang_gen_select(A
, select
);
3771 * Generate code for ||.
3773 static slang_ir_node
*
3774 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3776 /* rewrite "a || b" as "a ? true : b" */
3777 slang_operation
*select
;
3780 select
= slang_operation_new(1);
3781 select
->type
= SLANG_OPER_SELECT
;
3782 select
->num_children
= 3;
3783 select
->children
= slang_operation_new(3);
3785 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3786 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3787 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3788 select
->children
[1].literal_size
= 1;
3789 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3791 n
= _slang_gen_select(A
, select
);
3797 * Generate IR tree for a return statement.
3799 static slang_ir_node
*
3800 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3802 const GLboolean haveReturnValue
3803 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3805 /* error checking */
3806 assert(A
->CurFunction
);
3807 if (haveReturnValue
&&
3808 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3809 slang_info_log_error(A
->log
, "illegal return expression");
3812 else if (!haveReturnValue
&&
3813 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3814 slang_info_log_error(A
->log
, "return statement requires an expression");
3818 if (!haveReturnValue
) {
3819 return new_return(A
->curFuncEndLabel
);
3827 * return; // goto __endOfFunction
3829 slang_operation
*assign
;
3830 slang_atom a_retVal
;
3833 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3839 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3841 /* trying to return a value in a void-valued function */
3847 assign
= slang_operation_new(1);
3848 assign
->type
= SLANG_OPER_ASSIGN
;
3849 assign
->num_children
= 2;
3850 assign
->children
= slang_operation_new(2);
3851 /* lhs (__retVal) */
3852 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3853 assign
->children
[0].a_id
= a_retVal
;
3854 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3856 /* XXX we might be able to avoid this copy someday */
3857 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3859 /* assemble the new code */
3860 n
= new_seq(_slang_gen_operation(A
, assign
),
3861 new_return(A
->curFuncEndLabel
));
3863 slang_operation_delete(assign
);
3871 * Determine if the given operation/expression is const-valued.
3874 _slang_is_constant_expr(const slang_operation
*oper
)
3876 slang_variable
*var
;
3879 switch (oper
->type
) {
3880 case SLANG_OPER_IDENTIFIER
:
3881 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3882 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3886 for (i
= 0; i
< oper
->num_children
; i
++) {
3887 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3897 * Check if an assignment of type t1 to t0 is legal.
3898 * XXX more cases needed.
3901 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3902 slang_operation
*op0
,
3903 slang_operation
*op1
)
3905 slang_typeinfo t0
, t1
;
3908 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3909 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3913 slang_typeinfo_construct(&t0
);
3914 typeof_operation(A
, op0
, &t0
);
3916 slang_typeinfo_construct(&t1
);
3917 typeof_operation(A
, op1
, &t1
);
3919 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3920 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3924 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3929 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3930 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3931 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3934 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3935 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3938 #if 0 /* not used just yet - causes problems elsewhere */
3939 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3940 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3944 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3945 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3948 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3949 t1
.spec
.type
== SLANG_SPEC_INT
)
3957 * Generate IR tree for a local variable declaration.
3958 * Basically do some error checking and call _slang_gen_var_decl().
3960 static slang_ir_node
*
3961 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3963 const char *varName
= (char *) oper
->a_id
;
3964 slang_variable
*var
;
3965 slang_ir_node
*varDecl
;
3966 slang_operation
*initializer
;
3968 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3969 assert(oper
->num_children
<= 1);
3972 /* lookup the variable by name */
3973 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3975 return NULL
; /* "shouldn't happen" */
3977 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3978 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3979 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3980 /* can't declare attribute/uniform vars inside functions */
3981 slang_info_log_error(A
->log
,
3982 "local variable '%s' cannot be an attribute/uniform/varying",
3989 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3994 /* check if the var has an initializer */
3995 if (oper
->num_children
> 0) {
3996 assert(oper
->num_children
== 1);
3997 initializer
= &oper
->children
[0];
3999 else if (var
->initializer
) {
4000 initializer
= var
->initializer
;
4007 /* check/compare var type and initializer type */
4008 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4009 slang_info_log_error(A
->log
, "incompatible types in assignment");
4014 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4015 slang_info_log_error(A
->log
,
4016 "const-qualified variable '%s' requires initializer",
4022 /* Generate IR node */
4023 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4032 * Generate IR tree for a reference to a variable (such as in an expression).
4033 * This is different from a variable declaration.
4035 static slang_ir_node
*
4036 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4038 /* If there's a variable associated with this oper (from inlining)
4039 * use it. Otherwise, use the oper's var id.
4041 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4042 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4045 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4048 assert(var
->declared
);
4049 n
= new_var(A
, var
);
4056 * Return the number of components actually named by the swizzle.
4057 * Recall that swizzles may have undefined/don't-care values.
4060 swizzle_size(GLuint swizzle
)
4063 for (i
= 0; i
< 4; i
++) {
4064 GLuint swz
= GET_SWZ(swizzle
, i
);
4065 size
+= (swz
>= 0 && swz
<= 3);
4071 static slang_ir_node
*
4072 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4074 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4078 n
->Store
= _slang_new_ir_storage_relative(0,
4079 swizzle_size(swizzle
),
4081 n
->Store
->Swizzle
= swizzle
;
4088 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4090 while (store
->Parent
)
4091 store
= store
->Parent
;
4093 if (!(store
->File
== PROGRAM_OUTPUT
||
4094 store
->File
== PROGRAM_TEMPORARY
||
4095 (store
->File
== PROGRAM_VARYING
&&
4096 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4106 * Walk up an IR storage path to compute the final swizzle.
4107 * This is used when we find an expression such as "foo.xz.yx".
4110 root_swizzle(const slang_ir_storage
*st
)
4112 GLuint swizzle
= st
->Swizzle
;
4113 while (st
->Parent
) {
4115 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4122 * Generate IR tree for an assignment (=).
4124 static slang_ir_node
*
4125 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4127 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4128 /* Check that var is writeable */
4130 = _slang_variable_locate(oper
->children
[0].locals
,
4131 oper
->children
[0].a_id
, GL_TRUE
);
4133 slang_info_log_error(A
->log
, "undefined variable '%s'",
4134 (char *) oper
->children
[0].a_id
);
4137 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4138 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4139 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4140 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4141 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4142 slang_info_log_error(A
->log
,
4143 "illegal assignment to read-only variable '%s'",
4144 (char *) oper
->children
[0].a_id
);
4149 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4150 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4151 /* Special case of: x = f(a, b)
4152 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4154 * XXX this could be even more effective if we could accomodate
4155 * cases such as "v.x = f();" - would help with typical vertex
4159 n
= _slang_gen_function_call_name(A
,
4160 (const char *) oper
->children
[1].a_id
,
4161 &oper
->children
[1], &oper
->children
[0]);
4165 slang_ir_node
*n
, *lhs
, *rhs
;
4167 /* lhs and rhs type checking */
4168 if (!_slang_assignment_compatible(A
,
4170 &oper
->children
[1])) {
4171 slang_info_log_error(A
->log
, "incompatible types in assignment");
4175 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4181 slang_info_log_error(A
->log
,
4182 "invalid left hand side for assignment");
4186 /* check that lhs is writable */
4187 if (!is_store_writable(A
, lhs
->Store
)) {
4188 slang_info_log_error(A
->log
,
4189 "illegal assignment to read-only l-value");
4193 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4195 /* convert lhs swizzle into writemask */
4196 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4197 GLuint writemask
, newSwizzle
= 0x0;
4198 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4199 /* Non-simple writemask, need to swizzle right hand side in
4200 * order to put components into the right place.
4202 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4204 n
= new_node2(IR_COPY
, lhs
, rhs
);
4215 * Generate IR tree for referencing a field in a struct (or basic vector type)
4217 static slang_ir_node
*
4218 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4222 /* type of struct */
4223 slang_typeinfo_construct(&ti
);
4224 typeof_operation(A
, &oper
->children
[0], &ti
);
4226 if (_slang_type_is_vector(ti
.spec
.type
)) {
4227 /* the field should be a swizzle */
4228 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4232 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4233 slang_info_log_error(A
->log
, "Bad swizzle");
4236 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4241 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4242 /* create new parent node with swizzle */
4244 n
= _slang_gen_swizzle(n
, swizzle
);
4247 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4248 || ti
.spec
.type
== SLANG_SPEC_INT
4249 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4250 const GLuint rows
= 1;
4254 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4255 slang_info_log_error(A
->log
, "Bad swizzle");
4257 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4261 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4262 /* create new parent node with swizzle */
4263 n
= _slang_gen_swizzle(n
, swizzle
);
4267 /* the field is a structure member (base.field) */
4268 /* oper->children[0] is the base */
4269 /* oper->a_id is the field name */
4270 slang_ir_node
*base
, *n
;
4271 slang_typeinfo field_ti
;
4272 GLint fieldSize
, fieldOffset
= -1;
4275 slang_typeinfo_construct(&field_ti
);
4276 typeof_operation(A
, oper
, &field_ti
);
4278 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4280 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4282 if (fieldSize
== 0 || fieldOffset
< 0) {
4283 const char *structName
;
4284 if (ti
.spec
._struct
)
4285 structName
= (char *) ti
.spec
._struct
->a_name
;
4287 structName
= "unknown";
4288 slang_info_log_error(A
->log
,
4289 "\"%s\" is not a member of struct \"%s\"",
4290 (char *) oper
->a_id
, structName
);
4293 assert(fieldSize
>= 0);
4295 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4297 /* error msg should have already been logged */
4301 n
= new_node1(IR_FIELD
, base
);
4305 n
->Field
= (char *) oper
->a_id
;
4307 /* Store the field's offset in storage->Index */
4308 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4318 * Gen code for array indexing.
4320 static slang_ir_node
*
4321 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4323 slang_typeinfo array_ti
;
4325 /* get array's type info */
4326 slang_typeinfo_construct(&array_ti
);
4327 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4329 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4330 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4331 /* translate the index into a swizzle/writemask: "v.x=p" */
4332 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4336 index
= (GLint
) oper
->children
[1].literal
[0];
4337 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4338 index
>= (GLint
) max
) {
4340 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4341 printf("type = %d\n", oper
->children
[1].type
);
4342 printf("index = %d, max = %d\n", index
, max
);
4343 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4344 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4351 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4353 /* use swizzle to access the element */
4354 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4358 n
= _slang_gen_swizzle(n
, swizzle
);
4364 /* conventional array */
4365 slang_typeinfo elem_ti
;
4366 slang_ir_node
*elem
, *array
, *index
;
4367 GLint elemSize
, arrayLen
;
4369 /* size of array element */
4370 slang_typeinfo_construct(&elem_ti
);
4371 typeof_operation(A
, oper
, &elem_ti
);
4372 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4374 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4375 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4377 arrayLen
= array_ti
.array_len
;
4379 slang_typeinfo_destruct(&array_ti
);
4380 slang_typeinfo_destruct(&elem_ti
);
4382 if (elemSize
<= 0) {
4383 /* unknown var or type */
4384 slang_info_log_error(A
->log
, "Undefined variable or type");
4388 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4389 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4390 if (array
&& index
) {
4392 GLint constIndex
= -1;
4393 if (index
->Opcode
== IR_FLOAT
) {
4394 constIndex
= (int) index
->Value
[0];
4395 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4396 slang_info_log_error(A
->log
,
4397 "Array index out of bounds (index=%d size=%d)",
4398 constIndex
, arrayLen
);
4399 _slang_free_ir_tree(array
);
4400 _slang_free_ir_tree(index
);
4405 if (!array
->Store
) {
4406 slang_info_log_error(A
->log
, "Invalid array");
4410 elem
= new_node2(IR_ELEMENT
, array
, index
);
4412 /* The storage info here will be updated during code emit */
4413 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4414 array
->Store
->Index
,
4416 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4420 _slang_free_ir_tree(array
);
4421 _slang_free_ir_tree(index
);
4428 static slang_ir_node
*
4429 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4430 slang_ir_opcode opcode
)
4432 slang_typeinfo t0
, t1
;
4435 slang_typeinfo_construct(&t0
);
4436 typeof_operation(A
, &oper
->children
[0], &t0
);
4438 slang_typeinfo_construct(&t1
);
4439 typeof_operation(A
, &oper
->children
[0], &t1
);
4441 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4442 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4443 slang_info_log_error(A
->log
, "Illegal array comparison");
4447 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4448 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4449 /* <, <=, >, >= can only be used with scalars */
4450 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4451 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4452 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4453 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4454 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4459 n
= new_node2(opcode
,
4460 _slang_gen_operation(A
, &oper
->children
[0]),
4461 _slang_gen_operation(A
, &oper
->children
[1]));
4463 /* result is a bool (size 1) */
4464 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4472 print_vars(slang_variable_scope
*s
)
4476 for (i
= 0; i
< s
->num_variables
; i
++) {
4478 (char*) s
->variables
[i
]->a_name
,
4479 s
->variables
[i
]->declared
);
4489 _slang_undeclare_vars(slang_variable_scope
*locals
)
4491 if (locals
->num_variables
> 0) {
4493 for (i
= 0; i
< locals
->num_variables
; i
++) {
4494 slang_variable
*v
= locals
->variables
[i
];
4495 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4496 v
->declared
= GL_FALSE
;
4504 * Generate IR tree for a slang_operation (AST node)
4506 static slang_ir_node
*
4507 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4509 switch (oper
->type
) {
4510 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4514 _slang_push_var_table(A
->vartable
);
4516 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4517 n
= _slang_gen_operation(A
, oper
);
4518 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4520 _slang_pop_var_table(A
->vartable
);
4522 /*_slang_undeclare_vars(oper->locals);*/
4523 /*print_vars(oper->locals);*/
4526 n
= new_node1(IR_SCOPE
, n
);
4531 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4532 /* list of operations */
4533 if (oper
->num_children
> 0)
4535 slang_ir_node
*n
, *tree
= NULL
;
4538 for (i
= 0; i
< oper
->num_children
; i
++) {
4539 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4541 _slang_free_ir_tree(tree
);
4542 return NULL
; /* error must have occured */
4544 tree
= new_seq(tree
, n
);
4550 return new_node0(IR_NOP
);
4553 case SLANG_OPER_EXPRESSION
:
4554 return _slang_gen_operation(A
, &oper
->children
[0]);
4556 case SLANG_OPER_FOR
:
4557 return _slang_gen_for(A
, oper
);
4559 return _slang_gen_do(A
, oper
);
4560 case SLANG_OPER_WHILE
:
4561 return _slang_gen_while(A
, oper
);
4562 case SLANG_OPER_BREAK
:
4564 slang_info_log_error(A
->log
, "'break' not in loop");
4567 return new_break(A
->CurLoop
);
4568 case SLANG_OPER_CONTINUE
:
4570 slang_info_log_error(A
->log
, "'continue' not in loop");
4573 return _slang_gen_continue(A
, oper
);
4574 case SLANG_OPER_DISCARD
:
4575 return new_node0(IR_KILL
);
4577 case SLANG_OPER_EQUAL
:
4578 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4579 case SLANG_OPER_NOTEQUAL
:
4580 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4581 case SLANG_OPER_GREATER
:
4582 return _slang_gen_compare(A
, oper
, IR_SGT
);
4583 case SLANG_OPER_LESS
:
4584 return _slang_gen_compare(A
, oper
, IR_SLT
);
4585 case SLANG_OPER_GREATEREQUAL
:
4586 return _slang_gen_compare(A
, oper
, IR_SGE
);
4587 case SLANG_OPER_LESSEQUAL
:
4588 return _slang_gen_compare(A
, oper
, IR_SLE
);
4589 case SLANG_OPER_ADD
:
4592 assert(oper
->num_children
== 2);
4593 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4596 case SLANG_OPER_SUBTRACT
:
4599 assert(oper
->num_children
== 2);
4600 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4603 case SLANG_OPER_MULTIPLY
:
4606 assert(oper
->num_children
== 2);
4607 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4610 case SLANG_OPER_DIVIDE
:
4613 assert(oper
->num_children
== 2);
4614 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4617 case SLANG_OPER_MINUS
:
4620 assert(oper
->num_children
== 1);
4621 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4624 case SLANG_OPER_PLUS
:
4625 /* +expr --> do nothing */
4626 return _slang_gen_operation(A
, &oper
->children
[0]);
4627 case SLANG_OPER_VARIABLE_DECL
:
4628 return _slang_gen_declaration(A
, oper
);
4629 case SLANG_OPER_ASSIGN
:
4630 return _slang_gen_assignment(A
, oper
);
4631 case SLANG_OPER_ADDASSIGN
:
4634 assert(oper
->num_children
== 2);
4635 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4638 case SLANG_OPER_SUBASSIGN
:
4641 assert(oper
->num_children
== 2);
4642 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4646 case SLANG_OPER_MULASSIGN
:
4649 assert(oper
->num_children
== 2);
4650 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4653 case SLANG_OPER_DIVASSIGN
:
4656 assert(oper
->num_children
== 2);
4657 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4660 case SLANG_OPER_LOGICALAND
:
4663 assert(oper
->num_children
== 2);
4664 n
= _slang_gen_logical_and(A
, oper
);
4667 case SLANG_OPER_LOGICALOR
:
4670 assert(oper
->num_children
== 2);
4671 n
= _slang_gen_logical_or(A
, oper
);
4674 case SLANG_OPER_LOGICALXOR
:
4675 return _slang_gen_xor(A
, oper
);
4676 case SLANG_OPER_NOT
:
4677 return _slang_gen_not(A
, oper
);
4678 case SLANG_OPER_SELECT
: /* b ? x : y */
4681 assert(oper
->num_children
== 3);
4682 n
= _slang_gen_select(A
, oper
);
4686 case SLANG_OPER_ASM
:
4687 return _slang_gen_asm(A
, oper
, NULL
);
4688 case SLANG_OPER_CALL
:
4689 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4691 case SLANG_OPER_METHOD
:
4692 return _slang_gen_method_call(A
, oper
);
4693 case SLANG_OPER_RETURN
:
4694 return _slang_gen_return(A
, oper
);
4695 case SLANG_OPER_LABEL
:
4696 return new_label(oper
->label
);
4697 case SLANG_OPER_IDENTIFIER
:
4698 return _slang_gen_variable(A
, oper
);
4700 return _slang_gen_if(A
, oper
);
4701 case SLANG_OPER_FIELD
:
4702 return _slang_gen_struct_field(A
, oper
);
4703 case SLANG_OPER_SUBSCRIPT
:
4704 return _slang_gen_array_element(A
, oper
);
4705 case SLANG_OPER_LITERAL_FLOAT
:
4707 case SLANG_OPER_LITERAL_INT
:
4709 case SLANG_OPER_LITERAL_BOOL
:
4710 return new_float_literal(oper
->literal
, oper
->literal_size
);
4712 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4715 assert(oper
->num_children
== 1);
4716 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4719 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4722 assert(oper
->num_children
== 1);
4723 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4726 case SLANG_OPER_PREINCREMENT
: /* ++var */
4729 assert(oper
->num_children
== 1);
4730 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4733 case SLANG_OPER_PREDECREMENT
: /* --var */
4736 assert(oper
->num_children
== 1);
4737 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4741 case SLANG_OPER_NON_INLINED_CALL
:
4742 case SLANG_OPER_SEQUENCE
:
4744 slang_ir_node
*tree
= NULL
;
4746 for (i
= 0; i
< oper
->num_children
; i
++) {
4747 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4748 tree
= new_seq(tree
, n
);
4750 tree
->Store
= n
->Store
;
4752 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4753 tree
= new_function_call(tree
, oper
->label
);
4758 case SLANG_OPER_NONE
:
4759 case SLANG_OPER_VOID
:
4760 /* returning NULL here would generate an error */
4761 return new_node0(IR_NOP
);
4764 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4766 return new_node0(IR_NOP
);
4774 * Check if the given type specifier is a rectangular texture sampler.
4777 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4779 while (spec
->_array
) {
4780 spec
= spec
->_array
;
4782 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4783 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4789 * Called by compiler when a global variable has been parsed/compiled.
4790 * Here we examine the variable's type to determine what kind of register
4791 * storage will be used.
4793 * A uniform such as "gl_Position" will become the register specification
4794 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4795 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4797 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4798 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4799 * actual texture unit (as specified by the user calling glUniform1i()).
4802 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4803 slang_unit_type type
)
4805 struct gl_program
*prog
= A
->program
;
4806 const char *varName
= (char *) var
->a_name
;
4807 GLboolean success
= GL_TRUE
;
4808 slang_ir_storage
*store
= NULL
;
4810 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4811 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4812 const GLint arrayLen
= _slang_array_length(var
);
4813 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4814 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4816 /* check for sampler2D arrays */
4817 if (texIndex
== -1 && var
->type
.specifier
._array
)
4818 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4820 if (texIndex
!= -1) {
4821 /* This is a texture sampler variable...
4822 * store->File = PROGRAM_SAMPLER
4823 * store->Index = sampler number (0..7, typically)
4824 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4826 if (var
->initializer
) {
4827 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4830 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4831 /* disallow rect samplers */
4832 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4833 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4837 (void) is_rect_sampler_spec
; /* silence warning */
4840 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4841 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4843 /* If we have a sampler array, then we need to allocate the
4844 * additional samplers to ensure we don't allocate them elsewhere.
4845 * We can't directly use _mesa_add_sampler() as that checks the
4846 * varName and gets a match, so we call _mesa_add_parameter()
4847 * directly and use the last sampler number from the call above.
4850 GLint a
= arrayLen
- 1;
4852 for (i
= 0; i
< a
; i
++) {
4853 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4854 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4855 varName
, 1, datatype
, &value
, NULL
, 0x0);
4859 if (dbg
) printf("SAMPLER ");
4861 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4862 /* Uniform variable */
4863 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4866 /* user-defined uniform */
4867 if (datatype
== GL_NONE
) {
4868 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
4869 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
4870 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
4871 /* temporary work-around */
4872 GLenum datatype
= GL_FLOAT
;
4873 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4874 totalSize
, datatype
, NULL
);
4875 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4876 totalSize
, swizzle
);
4879 GLint a
= arrayLen
- 1;
4881 for (i
= 0; i
< a
; i
++) {
4882 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
4883 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
4884 varName
, 1, datatype
, &value
, NULL
, 0x0);
4888 /* XXX what we need to do is unroll the struct into its
4889 * basic types, creating a uniform variable for each.
4897 * Should produce uniforms:
4898 * "f.a" (GL_FLOAT_VEC3)
4899 * "f.b" (GL_FLOAT_VEC4)
4902 if (var
->initializer
) {
4903 slang_info_log_error(A
->log
,
4904 "unsupported initializer for uniform '%s'", varName
);
4909 slang_info_log_error(A
->log
,
4910 "invalid datatype for uniform variable %s",
4916 /* non-struct uniform */
4917 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4923 /* pre-defined uniform, like gl_ModelviewMatrix */
4924 /* We know it's a uniform, but don't allocate storage unless
4927 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4928 totalSize
, swizzle
);
4930 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4932 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4933 /* varyings must be float, vec or mat */
4934 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4935 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4936 slang_info_log_error(A
->log
,
4937 "varying '%s' must be float/vector/matrix",
4942 if (var
->initializer
) {
4943 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4949 /* user-defined varying */
4955 if (var
->type
.centroid
== SLANG_CENTROID
)
4956 flags
|= PROG_PARAM_BIT_CENTROID
;
4957 if (var
->type
.variant
== SLANG_INVARIANT
)
4958 flags
|= PROG_PARAM_BIT_INVARIANT
;
4960 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4962 swizzle
= _slang_var_swizzle(size
, 0);
4963 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4964 totalSize
, swizzle
);
4967 /* pre-defined varying, like gl_Color or gl_TexCoord */
4968 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4969 /* fragment program input */
4971 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4974 assert(index
< FRAG_ATTRIB_MAX
);
4975 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4979 /* vertex program output */
4980 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4981 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4983 assert(index
< VERT_RESULT_MAX
);
4984 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4985 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4988 if (dbg
) printf("V/F ");
4990 if (dbg
) printf("VARYING ");
4992 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4995 /* attributes must be float, vec or mat */
4996 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4997 slang_info_log_error(A
->log
,
4998 "attribute '%s' must be float/vector/matrix",
5004 /* user-defined vertex attribute */
5005 const GLint attr
= -1; /* unknown */
5006 swizzle
= _slang_var_swizzle(size
, 0);
5007 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5008 size
, datatype
, attr
);
5010 index
= VERT_ATTRIB_GENERIC0
+ index
;
5013 /* pre-defined vertex attrib */
5014 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5017 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5018 if (dbg
) printf("ATTRIB ");
5020 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5021 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5022 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5024 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5025 if (dbg
) printf("INPUT ");
5027 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5028 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5029 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5030 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5033 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5034 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5035 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5036 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5038 if (dbg
) printf("OUTPUT ");
5040 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5041 /* pre-defined global constant, like gl_MaxLights */
5042 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5043 if (dbg
) printf("CONST ");
5046 /* ordinary variable (may be const) */
5049 /* IR node to declare the variable */
5050 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5052 /* emit GPU instructions */
5053 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5055 _slang_free_ir_tree(n
);
5058 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5059 store
? store
->Index
: -2);
5062 var
->store
= store
; /* save var's storage info */
5064 var
->declared
= GL_TRUE
;
5071 * Produce an IR tree from a function AST (fun->body).
5072 * Then call the code emitter to convert the IR tree into gl_program
5076 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5079 GLboolean success
= GL_TRUE
;
5081 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5082 /* we only really generate code for main, all other functions get
5083 * inlined or codegen'd upon an actual call.
5086 /* do some basic error checking though */
5087 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5088 /* check that non-void functions actually return something */
5090 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5092 slang_info_log_error(A
->log
,
5093 "function \"%s\" has no return statement",
5094 (char *) fun
->header
.a_name
);
5096 "function \"%s\" has no return statement\n",
5097 (char *) fun
->header
.a_name
);
5102 return GL_TRUE
; /* not an error */
5106 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5107 slang_print_function(fun
, 1);
5110 /* should have been allocated earlier: */
5111 assert(A
->program
->Parameters
);
5112 assert(A
->program
->Varying
);
5113 assert(A
->vartable
);
5115 A
->CurLoopOper
= NULL
;
5116 A
->CurFunction
= fun
;
5118 /* fold constant expressions, etc. */
5119 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5122 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5123 slang_print_function(fun
, 1);
5126 /* Create an end-of-function label */
5127 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5129 /* push new vartable scope */
5130 _slang_push_var_table(A
->vartable
);
5132 /* Generate IR tree for the function body code */
5133 n
= _slang_gen_operation(A
, fun
->body
);
5135 n
= new_node1(IR_SCOPE
, n
);
5137 /* pop vartable, restore previous */
5138 _slang_pop_var_table(A
->vartable
);
5141 /* XXX record error */
5145 /* append an end-of-function-label to IR tree */
5146 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5148 /*_slang_label_delete(A->curFuncEndLabel);*/
5149 A
->curFuncEndLabel
= NULL
;
5152 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5153 slang_print_function(fun
, 1);
5156 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5157 _slang_print_ir_tree(n
, 0);
5160 printf("************* End codegen function ************\n\n");
5163 if (A
->UnresolvedRefs
) {
5164 /* Can't codegen at this time.
5165 * At link time we'll concatenate all the vertex shaders and/or all
5166 * the fragment shaders and try recompiling.
5171 /* Emit program instructions */
5172 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5173 _slang_free_ir_tree(n
);
5175 /* free codegen context */
5177 _mesa_free(A->codegen);