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 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
332 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
333 * a vertex or fragment program input variable. Return -1 if the input
335 * XXX return size too
338 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
345 static const struct input_info vertInputs
[] = {
346 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
347 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
348 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
349 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
350 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
351 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
352 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
353 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
354 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
355 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
356 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
357 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
358 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
359 { NULL
, 0, SWIZZLE_NOOP
}
361 static const struct input_info fragInputs
[] = {
362 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
363 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
364 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
365 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
366 /* note: we're packing several quantities into the fogcoord vector */
367 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
368 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
369 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
370 { NULL
, 0, SWIZZLE_NOOP
}
373 const struct input_info
*inputs
374 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
376 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
378 for (i
= 0; inputs
[i
].Name
; i
++) {
379 if (strcmp(inputs
[i
].Name
, name
) == 0) {
381 *swizzleOut
= inputs
[i
].Swizzle
;
382 return inputs
[i
].Attrib
;
390 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
391 * a vertex or fragment program output variable. Return -1 for an invalid
395 _slang_output_index(const char *name
, GLenum target
)
401 static const struct output_info vertOutputs
[] = {
402 { "gl_Position", VERT_RESULT_HPOS
},
403 { "gl_FrontColor", VERT_RESULT_COL0
},
404 { "gl_BackColor", VERT_RESULT_BFC0
},
405 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
406 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
407 { "gl_TexCoord", VERT_RESULT_TEX0
},
408 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
409 { "gl_PointSize", VERT_RESULT_PSIZ
},
412 static const struct output_info fragOutputs
[] = {
413 { "gl_FragColor", FRAG_RESULT_COLOR
},
414 { "gl_FragDepth", FRAG_RESULT_DEPTH
},
415 { "gl_FragData", FRAG_RESULT_DATA0
},
419 const struct output_info
*outputs
420 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
422 for (i
= 0; outputs
[i
].Name
; i
++) {
423 if (strcmp(outputs
[i
].Name
, name
) == 0) {
425 return outputs
[i
].Attrib
;
433 /**********************************************************************/
437 * Map "_asm foo" to IR_FOO, etc.
442 slang_ir_opcode Opcode
;
443 GLuint HaveRetValue
, NumParams
;
447 static slang_asm_info AsmInfo
[] = {
449 { "vec4_add", IR_ADD
, 1, 2 },
450 { "vec4_subtract", IR_SUB
, 1, 2 },
451 { "vec4_multiply", IR_MUL
, 1, 2 },
452 { "vec4_dot", IR_DOT4
, 1, 2 },
453 { "vec3_dot", IR_DOT3
, 1, 2 },
454 { "vec2_dot", IR_DOT2
, 1, 2 },
455 { "vec3_nrm", IR_NRM3
, 1, 1 },
456 { "vec4_nrm", IR_NRM4
, 1, 1 },
457 { "vec3_cross", IR_CROSS
, 1, 2 },
458 { "vec4_lrp", IR_LRP
, 1, 3 },
459 { "vec4_min", IR_MIN
, 1, 2 },
460 { "vec4_max", IR_MAX
, 1, 2 },
461 { "vec4_clamp", IR_CLAMP
, 1, 3 },
462 { "vec4_seq", IR_SEQUAL
, 1, 2 },
463 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
464 { "vec4_sge", IR_SGE
, 1, 2 },
465 { "vec4_sgt", IR_SGT
, 1, 2 },
466 { "vec4_sle", IR_SLE
, 1, 2 },
467 { "vec4_slt", IR_SLT
, 1, 2 },
469 { "vec4_move", IR_MOVE
, 1, 1 },
470 { "vec4_floor", IR_FLOOR
, 1, 1 },
471 { "vec4_frac", IR_FRAC
, 1, 1 },
472 { "vec4_abs", IR_ABS
, 1, 1 },
473 { "vec4_negate", IR_NEG
, 1, 1 },
474 { "vec4_ddx", IR_DDX
, 1, 1 },
475 { "vec4_ddy", IR_DDY
, 1, 1 },
476 /* float binary op */
477 { "float_power", IR_POW
, 1, 2 },
478 /* texture / sampler */
479 { "vec4_tex_1d", IR_TEX
, 1, 2 },
480 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
481 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
482 { "vec4_tex_2d", IR_TEX
, 1, 2 },
483 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
484 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
485 { "vec4_tex_3d", IR_TEX
, 1, 2 },
486 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
487 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
488 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
489 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
490 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
492 /* texture / sampler but with shadow comparison */
493 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
494 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
495 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
496 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
497 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
498 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
499 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
500 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
503 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
504 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
505 { "float_exp", IR_EXP
, 1, 1 },
506 { "float_exp2", IR_EXP2
, 1, 1 },
507 { "float_log2", IR_LOG2
, 1, 1 },
508 { "float_rsq", IR_RSQ
, 1, 1 },
509 { "float_rcp", IR_RCP
, 1, 1 },
510 { "float_sine", IR_SIN
, 1, 1 },
511 { "float_cosine", IR_COS
, 1, 1 },
512 { "float_noise1", IR_NOISE1
, 1, 1},
513 { "float_noise2", IR_NOISE2
, 1, 1},
514 { "float_noise3", IR_NOISE3
, 1, 1},
515 { "float_noise4", IR_NOISE4
, 1, 1},
517 { NULL
, IR_NOP
, 0, 0 }
521 static slang_ir_node
*
522 new_node3(slang_ir_opcode op
,
523 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
525 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
531 n
->InstLocation
= -1;
536 static slang_ir_node
*
537 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
539 return new_node3(op
, c0
, c1
, NULL
);
542 static slang_ir_node
*
543 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
545 return new_node3(op
, c0
, NULL
, NULL
);
548 static slang_ir_node
*
549 new_node0(slang_ir_opcode op
)
551 return new_node3(op
, NULL
, NULL
, NULL
);
556 * Create sequence of two nodes.
558 static slang_ir_node
*
559 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
565 return new_node2(IR_SEQ
, left
, right
);
568 static slang_ir_node
*
569 new_label(slang_label
*label
)
571 slang_ir_node
*n
= new_node0(IR_LABEL
);
578 static slang_ir_node
*
579 new_float_literal(const float v
[4], GLuint size
)
581 slang_ir_node
*n
= new_node0(IR_FLOAT
);
583 COPY_4V(n
->Value
, v
);
584 /* allocate a storage object, but compute actual location (Index) later */
585 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
590 static slang_ir_node
*
591 new_not(slang_ir_node
*n
)
593 return new_node1(IR_NOT
, n
);
598 * Non-inlined function call.
600 static slang_ir_node
*
601 new_function_call(slang_ir_node
*code
, slang_label
*name
)
603 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
612 * Unconditional jump.
614 static slang_ir_node
*
615 new_return(slang_label
*dest
)
617 slang_ir_node
*n
= new_node0(IR_RETURN
);
625 static slang_ir_node
*
626 new_loop(slang_ir_node
*body
)
628 return new_node1(IR_LOOP
, body
);
632 static slang_ir_node
*
633 new_break(slang_ir_node
*loopNode
)
635 slang_ir_node
*n
= new_node0(IR_BREAK
);
637 assert(loopNode
->Opcode
== IR_LOOP
);
639 /* insert this node at head of linked list */
640 n
->List
= loopNode
->List
;
648 * Make new IR_BREAK_IF_TRUE.
650 static slang_ir_node
*
651 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
655 assert(loopNode
->Opcode
== IR_LOOP
);
656 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
658 /* insert this node at head of linked list */
659 n
->List
= loopNode
->List
;
667 * Make new IR_CONT_IF_TRUE node.
669 static slang_ir_node
*
670 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
674 assert(loopNode
->Opcode
== IR_LOOP
);
675 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
677 /* insert this node at head of linked list */
678 n
->List
= loopNode
->List
;
685 static slang_ir_node
*
686 new_cond(slang_ir_node
*n
)
688 slang_ir_node
*c
= new_node1(IR_COND
, n
);
693 static slang_ir_node
*
694 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
696 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
701 * New IR_VAR node - a reference to a previously declared variable.
703 static slang_ir_node
*
704 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
706 slang_ir_node
*n
= new_node0(IR_VAR
);
713 /* Set IR node's Var and Store pointers */
715 n
->Store
= var
->store
;
722 * Check if the given function is really just a wrapper for a
723 * basic assembly instruction.
726 slang_is_asm_function(const slang_function
*fun
)
728 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
729 fun
->body
->num_children
== 1 &&
730 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
738 _slang_is_noop(const slang_operation
*oper
)
741 oper
->type
== SLANG_OPER_VOID
||
742 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
750 * Recursively search tree for a node of the given type.
752 static slang_operation
*
753 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
756 if (oper
->type
== type
)
758 for (i
= 0; i
< oper
->num_children
; i
++) {
759 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
768 * Count the number of operations of the given time rooted at 'oper'.
771 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
774 if (oper
->type
== type
) {
777 for (i
= 0; i
< oper
->num_children
; i
++) {
778 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
785 * Check if the 'return' statement found under 'oper' is a "tail return"
786 * that can be no-op'd. For example:
791 * return; // this is a no-op
794 * This is used when determining if a function can be inlined. If the
795 * 'return' is not the last statement, we can't inline the function since
796 * we still need the semantic behaviour of the 'return' but we don't want
797 * to accidentally return from the _calling_ function. We'd need to use an
798 * unconditional branch, but we don't have such a GPU instruction (not
802 _slang_is_tail_return(const slang_operation
*oper
)
804 GLuint k
= oper
->num_children
;
807 const slang_operation
*last
= &oper
->children
[k
- 1];
808 if (last
->type
== SLANG_OPER_RETURN
)
810 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
811 last
->type
== SLANG_OPER_LABEL
)
812 k
--; /* try prev child */
813 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
814 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
815 /* try sub-children */
816 return _slang_is_tail_return(last
);
826 slang_resolve_variable(slang_operation
*oper
)
828 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
829 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
835 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
838 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
839 GLuint substCount
, slang_variable
**substOld
,
840 slang_operation
**substNew
, GLboolean isLHS
)
842 switch (oper
->type
) {
843 case SLANG_OPER_VARIABLE_DECL
:
845 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
846 oper
->a_id
, GL_TRUE
);
848 if (v
->initializer
&& oper
->num_children
== 0) {
849 /* set child of oper to copy of initializer */
850 oper
->num_children
= 1;
851 oper
->children
= slang_operation_new(1);
852 slang_operation_copy(&oper
->children
[0], v
->initializer
);
854 if (oper
->num_children
== 1) {
855 /* the initializer */
856 slang_substitute(A
, &oper
->children
[0], substCount
,
857 substOld
, substNew
, GL_FALSE
);
861 case SLANG_OPER_IDENTIFIER
:
862 assert(oper
->num_children
== 0);
863 if (1/**!isLHS XXX FIX */) {
864 slang_atom id
= oper
->a_id
;
867 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
869 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
873 /* look for a substitution */
874 for (i
= 0; i
< substCount
; i
++) {
875 if (v
== substOld
[i
]) {
876 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
877 #if 0 /* DEBUG only */
878 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
879 assert(substNew
[i
]->var
);
880 assert(substNew
[i
]->var
->a_name
);
881 printf("Substitute %s with %s in id node %p\n",
882 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
886 printf("Substitute %s with %f in id node %p\n",
887 (char*)v
->a_name
, substNew
[i
]->literal
[0],
891 slang_operation_copy(oper
, substNew
[i
]);
898 case SLANG_OPER_RETURN
:
899 /* do return replacement here too */
900 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
901 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
907 * then do substitutions on the assignment.
909 slang_operation
*blockOper
, *assignOper
, *returnOper
;
911 /* check if function actually has a return type */
912 assert(A
->CurFunction
);
913 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
914 slang_info_log_error(A
->log
, "illegal return expression");
918 blockOper
= slang_operation_new(1);
919 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
920 blockOper
->num_children
= 2;
921 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
922 blockOper
->children
= slang_operation_new(2);
923 assignOper
= blockOper
->children
+ 0;
924 returnOper
= blockOper
->children
+ 1;
926 assignOper
->type
= SLANG_OPER_ASSIGN
;
927 assignOper
->num_children
= 2;
928 assignOper
->locals
->outer_scope
= blockOper
->locals
;
929 assignOper
->children
= slang_operation_new(2);
930 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
931 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
932 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
934 slang_operation_copy(&assignOper
->children
[1],
937 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
938 assert(returnOper
->num_children
== 0);
940 /* do substitutions on the "__retVal = expr" sub-tree */
941 slang_substitute(A
, assignOper
,
942 substCount
, substOld
, substNew
, GL_FALSE
);
944 /* install new code */
945 slang_operation_copy(oper
, blockOper
);
946 slang_operation_destruct(blockOper
);
949 /* check if return value was expected */
950 assert(A
->CurFunction
);
951 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
952 slang_info_log_error(A
->log
, "return statement requires an expression");
958 case SLANG_OPER_ASSIGN
:
959 case SLANG_OPER_SUBSCRIPT
:
961 * child[0] can't have substitutions but child[1] can.
963 slang_substitute(A
, &oper
->children
[0],
964 substCount
, substOld
, substNew
, GL_TRUE
);
965 slang_substitute(A
, &oper
->children
[1],
966 substCount
, substOld
, substNew
, GL_FALSE
);
968 case SLANG_OPER_FIELD
:
970 slang_substitute(A
, &oper
->children
[0],
971 substCount
, substOld
, substNew
, GL_TRUE
);
976 for (i
= 0; i
< oper
->num_children
; i
++)
977 slang_substitute(A
, &oper
->children
[i
],
978 substCount
, substOld
, substNew
, GL_FALSE
);
985 * Produce inline code for a call to an assembly instruction.
986 * This is typically used to compile a call to a built-in function like this:
988 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
990 * __asm vec4_lrp __retVal, a, y, x;
995 * r = mix(p1, p2, p3);
1005 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1007 static slang_operation
*
1008 slang_inline_asm_function(slang_assemble_ctx
*A
,
1009 slang_function
*fun
, slang_operation
*oper
)
1011 const GLuint numArgs
= oper
->num_children
;
1013 slang_operation
*inlined
;
1014 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1015 slang_variable
**substOld
;
1016 slang_operation
**substNew
;
1018 ASSERT(slang_is_asm_function(fun
));
1019 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1022 printf("Inline %s as %s\n",
1023 (char*) fun->header.a_name,
1024 (char*) fun->body->children[0].a_id);
1028 * We'll substitute formal params with actual args in the asm call.
1030 substOld
= (slang_variable
**)
1031 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1032 substNew
= (slang_operation
**)
1033 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1034 for (i
= 0; i
< numArgs
; i
++) {
1035 substOld
[i
] = fun
->parameters
->variables
[i
];
1036 substNew
[i
] = oper
->children
+ i
;
1039 /* make a copy of the code to inline */
1040 inlined
= slang_operation_new(1);
1041 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1043 /* get rid of the __retVal child */
1044 inlined
->num_children
--;
1045 for (i
= 0; i
< inlined
->num_children
; i
++) {
1046 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1050 /* now do formal->actual substitutions */
1051 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1053 _slang_free(substOld
);
1054 _slang_free(substNew
);
1057 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1058 (char *) fun
->header
.a_name
);
1059 slang_print_tree(inlined
, 3);
1060 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1068 * Inline the given function call operation.
1069 * Return a new slang_operation that corresponds to the inlined code.
1071 static slang_operation
*
1072 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1073 slang_operation
*oper
, slang_operation
*returnOper
)
1080 ParamMode
*paramMode
;
1081 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1082 const GLuint numArgs
= oper
->num_children
;
1083 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1084 slang_operation
*args
= oper
->children
;
1085 slang_operation
*inlined
, *top
;
1086 slang_variable
**substOld
;
1087 slang_operation
**substNew
;
1088 GLuint substCount
, numCopyIn
, i
;
1089 slang_function
*prevFunction
;
1090 slang_variable_scope
*newScope
= NULL
;
1093 prevFunction
= A
->CurFunction
;
1094 A
->CurFunction
= fun
;
1096 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1097 assert(fun
->param_count
== totalArgs
);
1099 /* allocate temporary arrays */
1100 paramMode
= (ParamMode
*)
1101 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1102 substOld
= (slang_variable
**)
1103 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1104 substNew
= (slang_operation
**)
1105 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1108 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1109 (char *) fun
->header
.a_name
,
1110 fun
->parameters
->num_variables
, numArgs
);
1113 if (haveRetValue
&& !returnOper
) {
1114 /* Create 3-child comma sequence for inlined code:
1115 * child[0]: declare __resultTmp
1116 * child[1]: inlined function body
1117 * child[2]: __resultTmp
1119 slang_operation
*commaSeq
;
1120 slang_operation
*declOper
= NULL
;
1121 slang_variable
*resultVar
;
1123 commaSeq
= slang_operation_new(1);
1124 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1125 assert(commaSeq
->locals
);
1126 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1127 commaSeq
->num_children
= 3;
1128 commaSeq
->children
= slang_operation_new(3);
1129 /* allocate the return var */
1130 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1132 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1133 (void*)commaSeq->locals, (char *) fun->header.a_name);
1136 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1137 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1138 resultVar
->isTemp
= GL_TRUE
;
1140 /* child[0] = __resultTmp declaration */
1141 declOper
= &commaSeq
->children
[0];
1142 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1143 declOper
->a_id
= resultVar
->a_name
;
1144 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1146 /* child[1] = function body */
1147 inlined
= &commaSeq
->children
[1];
1148 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1150 /* child[2] = __resultTmp reference */
1151 returnOper
= &commaSeq
->children
[2];
1152 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1153 returnOper
->a_id
= resultVar
->a_name
;
1154 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1159 top
= inlined
= slang_operation_new(1);
1160 /* XXXX this may be inappropriate!!!! */
1161 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1165 assert(inlined
->locals
);
1167 /* Examine the parameters, look for inout/out params, look for possible
1168 * substitutions, etc:
1169 * param type behaviour
1170 * in copy actual to local
1171 * const in substitute param with actual
1175 for (i
= 0; i
< totalArgs
; i
++) {
1176 slang_variable
*p
= fun
->parameters
->variables
[i
];
1178 printf("Param %d: %s %s \n", i,
1179 slang_type_qual_string(p->type.qualifier),
1180 (char *) p->a_name);
1182 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1183 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1184 /* an output param */
1185 slang_operation
*arg
;
1190 paramMode
[i
] = SUBST
;
1192 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1193 slang_resolve_variable(arg
);
1195 /* replace parameter 'p' with argument 'arg' */
1196 substOld
[substCount
] = p
;
1197 substNew
[substCount
] = arg
; /* will get copied */
1200 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1201 /* a constant input param */
1202 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1203 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1204 /* replace all occurances of this parameter variable with the
1205 * actual argument variable or a literal.
1207 paramMode
[i
] = SUBST
;
1208 slang_resolve_variable(&args
[i
]);
1209 substOld
[substCount
] = p
;
1210 substNew
[substCount
] = &args
[i
]; /* will get copied */
1214 paramMode
[i
] = COPY_IN
;
1218 paramMode
[i
] = COPY_IN
;
1220 assert(paramMode
[i
]);
1223 /* actual code inlining: */
1224 slang_operation_copy(inlined
, fun
->body
);
1226 /*** XXX review this */
1227 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1228 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1229 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1232 printf("======================= orig body code ======================\n");
1233 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1234 slang_print_tree(fun
->body
, 8);
1235 printf("======================= copied code =========================\n");
1236 slang_print_tree(inlined
, 8);
1239 /* do parameter substitution in inlined code: */
1240 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1243 printf("======================= subst code ==========================\n");
1244 slang_print_tree(inlined
, 8);
1245 printf("=============================================================\n");
1248 /* New prolog statements: (inserted before the inlined code)
1249 * Copy the 'in' arguments.
1252 for (i
= 0; i
< numArgs
; i
++) {
1253 if (paramMode
[i
] == COPY_IN
) {
1254 slang_variable
*p
= fun
->parameters
->variables
[i
];
1255 /* declare parameter 'p' */
1256 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1260 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1261 assert(decl
->locals
);
1262 decl
->locals
->outer_scope
= inlined
->locals
;
1263 decl
->a_id
= p
->a_name
;
1264 decl
->num_children
= 1;
1265 decl
->children
= slang_operation_new(1);
1267 /* child[0] is the var's initializer */
1268 slang_operation_copy(&decl
->children
[0], args
+ i
);
1270 /* add parameter 'p' to the local variable scope here */
1272 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1273 pCopy
->type
= p
->type
;
1274 pCopy
->a_name
= p
->a_name
;
1275 pCopy
->array_len
= p
->array_len
;
1278 newScope
= inlined
->locals
;
1283 /* Now add copies of the function's local vars to the new variable scope */
1284 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1285 slang_variable
*p
= fun
->parameters
->variables
[i
];
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
;
1293 /* New epilog statements:
1294 * 1. Create end of function label to jump to from return statements.
1295 * 2. Copy the 'out' parameter vars
1298 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1300 inlined
->num_children
);
1301 lab
->type
= SLANG_OPER_LABEL
;
1302 lab
->label
= A
->curFuncEndLabel
;
1305 for (i
= 0; i
< totalArgs
; i
++) {
1306 if (paramMode
[i
] == COPY_OUT
) {
1307 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1308 /* actualCallVar = outParam */
1309 /*if (i > 0 || !haveRetValue)*/
1310 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1312 inlined
->num_children
);
1313 ass
->type
= SLANG_OPER_ASSIGN
;
1314 ass
->num_children
= 2;
1315 ass
->locals
->outer_scope
= inlined
->locals
;
1316 ass
->children
= slang_operation_new(2);
1317 ass
->children
[0] = args
[i
]; /*XXX copy */
1318 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1319 ass
->children
[1].a_id
= p
->a_name
;
1320 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1324 _slang_free(paramMode
);
1325 _slang_free(substOld
);
1326 _slang_free(substNew
);
1328 /* Update scoping to use the new local vars instead of the
1329 * original function's vars. This is especially important
1330 * for nested inlining.
1333 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1336 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1337 (char *) fun
->header
.a_name
,
1338 fun
->parameters
->num_variables
, numArgs
);
1339 slang_print_tree(top
, 0);
1343 A
->CurFunction
= prevFunction
;
1349 static slang_ir_node
*
1350 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1351 slang_operation
*oper
, slang_operation
*dest
)
1354 slang_operation
*inlined
;
1355 slang_label
*prevFuncEndLabel
;
1358 prevFuncEndLabel
= A
->curFuncEndLabel
;
1359 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1360 A
->curFuncEndLabel
= _slang_label_new(name
);
1361 assert(A
->curFuncEndLabel
);
1363 if (slang_is_asm_function(fun
) && !dest
) {
1364 /* assemble assembly function - tree style */
1365 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1368 /* non-assembly function */
1369 /* We always generate an "inline-able" block of code here.
1371 * 1. insert the inline code
1372 * 2. Generate a call to the "inline" code as a subroutine
1376 slang_operation
*ret
= NULL
;
1378 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1382 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1384 /* check if this is a "tail" return */
1385 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1386 _slang_is_tail_return(inlined
)) {
1387 /* The only RETURN is the last stmt in the function, no-op it
1388 * and inline the function body.
1390 ret
->type
= SLANG_OPER_NONE
;
1393 slang_operation
*callOper
;
1394 /* The function we're calling has one or more 'return' statements.
1395 * So, we can't truly inline this function because we need to
1396 * implement 'return' with RET (and CAL).
1397 * Nevertheless, we performed "inlining" to make a new instance
1398 * of the function body to deal with static register allocation.
1400 * XXX check if there's one 'return' and if it's the very last
1401 * statement in the function - we can optimize that case.
1403 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1404 inlined
->type
== SLANG_OPER_SEQUENCE
);
1406 if (_slang_function_has_return_value(fun
) && !dest
) {
1407 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1408 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1409 callOper
= &inlined
->children
[1];
1414 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1415 callOper
->fun
= fun
;
1416 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1424 /* Replace the function call with the inlined block (or new CALL stmt) */
1425 slang_operation_destruct(oper
);
1427 _slang_free(inlined
);
1430 assert(inlined
->locals
);
1431 printf("*** Inlined code for call to %s:\n",
1432 (char*) fun
->header
.a_name
);
1433 slang_print_tree(oper
, 10);
1437 n
= _slang_gen_operation(A
, oper
);
1439 /*_slang_label_delete(A->curFuncEndLabel);*/
1440 A
->curFuncEndLabel
= prevFuncEndLabel
;
1442 if (A
->pragmas
->Debug
) {
1444 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1445 n
->Comment
= _slang_strdup(s
);
1452 static slang_asm_info
*
1453 slang_find_asm_info(const char *name
)
1456 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1457 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1466 * Some write-masked assignments are simple, but others are hard.
1469 * v.xy = vec2(a, b);
1472 * v.zy = vec2(a, b);
1473 * this gets transformed/swizzled into:
1474 * v.zy = vec2(a, b).*yx* (* = don't care)
1475 * This function helps to determine simple vs. non-simple.
1478 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1480 switch (writemask
) {
1482 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1484 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1486 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1488 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1490 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1491 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1493 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1494 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1495 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1496 case WRITEMASK_XYZW
:
1497 return swizzle
== SWIZZLE_NOOP
;
1505 * Convert the given swizzle into a writemask. In some cases this
1506 * is trivial, in other cases, we'll need to also swizzle the right
1507 * hand side to put components in the right places.
1508 * See comment above for more info.
1509 * XXX this function could be simplified and should probably be renamed.
1510 * \param swizzle the incoming swizzle
1511 * \param writemaskOut returns the writemask
1512 * \param swizzleOut swizzle to apply to the right-hand-side
1513 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1516 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1517 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1519 GLuint mask
= 0x0, newSwizzle
[4];
1522 /* make new dst writemask, compute size */
1523 for (i
= 0; i
< 4; i
++) {
1524 const GLuint swz
= GET_SWZ(swizzle
, i
);
1525 if (swz
== SWIZZLE_NIL
) {
1529 assert(swz
>= 0 && swz
<= 3);
1531 if (swizzle
!= SWIZZLE_XXXX
&&
1532 swizzle
!= SWIZZLE_YYYY
&&
1533 swizzle
!= SWIZZLE_ZZZZ
&&
1534 swizzle
!= SWIZZLE_WWWW
&&
1535 (mask
& (1 << swz
))) {
1536 /* a channel can't be specified twice (ex: ".xyyz") */
1537 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1538 _mesa_swizzle_string(swizzle
, 0, 0));
1544 assert(mask
<= 0xf);
1545 size
= i
; /* number of components in mask/swizzle */
1547 *writemaskOut
= mask
;
1549 /* make new src swizzle, by inversion */
1550 for (i
= 0; i
< 4; i
++) {
1551 newSwizzle
[i
] = i
; /*identity*/
1553 for (i
= 0; i
< size
; i
++) {
1554 const GLuint swz
= GET_SWZ(swizzle
, i
);
1555 newSwizzle
[swz
] = i
;
1557 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1562 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1564 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1566 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1568 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1570 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1578 #if 0 /* not used, but don't remove just yet */
1580 * Recursively traverse 'oper' to produce a swizzle mask in the event
1581 * of any vector subscripts and swizzle suffixes.
1582 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1585 resolve_swizzle(const slang_operation
*oper
)
1587 if (oper
->type
== SLANG_OPER_FIELD
) {
1588 /* writemask from .xyzw suffix */
1590 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1591 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1595 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1596 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1600 return SWIZZLE_XYZW
;
1602 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1603 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1604 /* writemask from [index] */
1605 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1606 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1611 swizzle
= SWIZZLE_XXXX
;
1614 swizzle
= SWIZZLE_YYYY
;
1617 swizzle
= SWIZZLE_ZZZZ
;
1620 swizzle
= SWIZZLE_WWWW
;
1623 swizzle
= SWIZZLE_XYZW
;
1625 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1629 return SWIZZLE_XYZW
;
1637 * Recursively descend through swizzle nodes to find the node's storage info.
1639 static slang_ir_storage
*
1640 get_store(const slang_ir_node
*n
)
1642 if (n
->Opcode
== IR_SWIZZLE
) {
1643 return get_store(n
->Children
[0]);
1651 * Generate IR tree for an asm instruction/operation such as:
1652 * __asm vec4_dot __retVal.x, v1, v2;
1654 static slang_ir_node
*
1655 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1656 slang_operation
*dest
)
1658 const slang_asm_info
*info
;
1659 slang_ir_node
*kids
[3], *n
;
1660 GLuint j
, firstOperand
;
1662 assert(oper
->type
== SLANG_OPER_ASM
);
1664 info
= slang_find_asm_info((char *) oper
->a_id
);
1666 _mesa_problem(NULL
, "undefined __asm function %s\n",
1667 (char *) oper
->a_id
);
1670 assert(info
->NumParams
<= 3);
1672 if (info
->NumParams
== oper
->num_children
) {
1673 /* Storage for result is not specified.
1674 * Children[0], [1], [2] are the operands.
1679 /* Storage for result (child[0]) is specified.
1680 * Children[1], [2], [3] are the operands.
1685 /* assemble child(ren) */
1686 kids
[0] = kids
[1] = kids
[2] = NULL
;
1687 for (j
= 0; j
< info
->NumParams
; j
++) {
1688 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1693 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1696 /* Setup n->Store to be a particular location. Otherwise, storage
1697 * for the result (a temporary) will be allocated later.
1699 slang_operation
*dest_oper
;
1702 dest_oper
= &oper
->children
[0];
1704 n0
= _slang_gen_operation(A
, dest_oper
);
1709 n
->Store
= n0
->Store
;
1711 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1722 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1725 for (i
= 0; i
< scope
->num_functions
; i
++) {
1726 slang_function
*f
= &scope
->functions
[i
];
1727 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1728 printf(" %s (%d args)\n", name
, f
->param_count
);
1731 if (scope
->outer_scope
)
1732 print_funcs(scope
->outer_scope
, name
);
1738 * Find a function of the given name, taking 'numArgs' arguments.
1739 * This is the function we'll try to call when there is no exact match
1740 * between function parameters and call arguments.
1742 * XXX we should really create a list of candidate functions and try
1745 static slang_function
*
1746 _slang_find_function_by_argc(slang_function_scope
*scope
,
1747 const char *name
, int numArgs
)
1751 for (i
= 0; i
< scope
->num_functions
; i
++) {
1752 slang_function
*f
= &scope
->functions
[i
];
1753 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1754 int haveRetValue
= _slang_function_has_return_value(f
);
1755 if (numArgs
== f
->param_count
- haveRetValue
)
1759 scope
= scope
->outer_scope
;
1766 static slang_function
*
1767 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1770 slang_function
*maxFunc
= NULL
;
1775 for (i
= 0; i
< scope
->num_functions
; i
++) {
1776 slang_function
*f
= &scope
->functions
[i
];
1777 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1778 if (f
->param_count
> maxArgs
) {
1779 maxArgs
= f
->param_count
;
1784 scope
= scope
->outer_scope
;
1792 * Generate a new slang_function which is a constructor for a user-defined
1795 static slang_function
*
1796 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1798 const GLint numFields
= str
->fields
->num_variables
;
1799 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1801 /* function header (name, return type) */
1802 fun
->header
.a_name
= str
->a_name
;
1803 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1804 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1805 fun
->header
.type
.specifier
._struct
= str
;
1807 /* function parameters (= struct's fields) */
1810 for (i
= 0; i
< numFields
; i
++) {
1812 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1814 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1815 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1816 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1818 fun
->param_count
= fun
->parameters
->num_variables
;
1821 /* Add __retVal to params */
1823 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1824 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1826 p
->a_name
= a_retVal
;
1827 p
->type
= fun
->header
.type
;
1828 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1832 /* function body is:
1842 slang_variable_scope
*scope
;
1843 slang_variable
*var
;
1846 fun
->body
= slang_operation_new(1);
1847 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1848 fun
->body
->num_children
= numFields
+ 2;
1849 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1851 scope
= fun
->body
->locals
;
1852 scope
->outer_scope
= fun
->parameters
;
1854 /* create local var 't' */
1855 var
= slang_variable_scope_grow(scope
);
1856 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1857 var
->type
= fun
->header
.type
;
1861 slang_operation
*decl
;
1863 decl
= &fun
->body
->children
[0];
1864 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1865 decl
->locals
= _slang_variable_scope_new(scope
);
1866 decl
->a_id
= var
->a_name
;
1869 /* assign params to fields of t */
1870 for (i
= 0; i
< numFields
; i
++) {
1871 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1873 assign
->type
= SLANG_OPER_ASSIGN
;
1874 assign
->locals
= _slang_variable_scope_new(scope
);
1875 assign
->num_children
= 2;
1876 assign
->children
= slang_operation_new(2);
1879 slang_operation
*lhs
= &assign
->children
[0];
1881 lhs
->type
= SLANG_OPER_FIELD
;
1882 lhs
->locals
= _slang_variable_scope_new(scope
);
1883 lhs
->num_children
= 1;
1884 lhs
->children
= slang_operation_new(1);
1885 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1887 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1888 lhs
->children
[0].a_id
= var
->a_name
;
1889 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1892 lhs
->children
[1].num_children
= 1;
1893 lhs
->children
[1].children
= slang_operation_new(1);
1894 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1895 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1896 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1901 slang_operation
*rhs
= &assign
->children
[1];
1903 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1904 rhs
->locals
= _slang_variable_scope_new(scope
);
1905 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1911 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1913 ret
->type
= SLANG_OPER_RETURN
;
1914 ret
->locals
= _slang_variable_scope_new(scope
);
1915 ret
->num_children
= 1;
1916 ret
->children
= slang_operation_new(1);
1917 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1918 ret
->children
[0].a_id
= var
->a_name
;
1919 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1923 slang_print_function(fun, 1);
1930 * Find/create a function (constructor) for the given structure name.
1932 static slang_function
*
1933 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1936 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1937 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1938 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1939 /* found a structure type that matches the function name */
1940 if (!str
->constructor
) {
1941 /* create the constructor function now */
1942 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1944 return str
->constructor
;
1952 * Generate a new slang_function to satisfy a call to an array constructor.
1953 * Ex: float[3](1., 2., 3.)
1955 static slang_function
*
1956 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1958 slang_type_specifier_type baseType
;
1959 slang_function
*fun
;
1962 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1966 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1968 num_elements
= oper
->num_children
;
1970 /* function header, return type */
1972 fun
->header
.a_name
= oper
->a_id
;
1973 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1974 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1975 fun
->header
.type
.specifier
._array
=
1976 slang_type_specifier_new(baseType
, NULL
, NULL
);
1977 fun
->header
.type
.array_len
= num_elements
;
1980 /* function parameters (= number of elements) */
1983 for (i
= 0; i
< num_elements
; i
++) {
1985 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1987 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1989 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
1990 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1991 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1992 p
->type
.specifier
.type
= baseType
;
1994 fun
->param_count
= fun
->parameters
->num_variables
;
1997 /* Add __retVal to params */
1999 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2000 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2002 p
->a_name
= a_retVal
;
2003 p
->type
= fun
->header
.type
;
2004 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2005 p
->type
.specifier
.type
= baseType
;
2009 /* function body is:
2019 slang_variable_scope
*scope
;
2020 slang_variable
*var
;
2023 fun
->body
= slang_operation_new(1);
2024 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2025 fun
->body
->num_children
= num_elements
+ 2;
2026 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2028 scope
= fun
->body
->locals
;
2029 scope
->outer_scope
= fun
->parameters
;
2031 /* create local var 't' */
2032 var
= slang_variable_scope_grow(scope
);
2033 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2034 var
->type
= fun
->header
.type
;/*XXX copy*/
2038 slang_operation
*decl
;
2040 decl
= &fun
->body
->children
[0];
2041 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2042 decl
->locals
= _slang_variable_scope_new(scope
);
2043 decl
->a_id
= var
->a_name
;
2046 /* assign params to elements of t */
2047 for (i
= 0; i
< num_elements
; i
++) {
2048 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2050 assign
->type
= SLANG_OPER_ASSIGN
;
2051 assign
->locals
= _slang_variable_scope_new(scope
);
2052 assign
->num_children
= 2;
2053 assign
->children
= slang_operation_new(2);
2056 slang_operation
*lhs
= &assign
->children
[0];
2058 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2059 lhs
->locals
= _slang_variable_scope_new(scope
);
2060 lhs
->num_children
= 2;
2061 lhs
->children
= slang_operation_new(2);
2063 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2064 lhs
->children
[0].a_id
= var
->a_name
;
2065 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2067 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2068 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2072 slang_operation
*rhs
= &assign
->children
[1];
2074 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2075 rhs
->locals
= _slang_variable_scope_new(scope
);
2076 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2082 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2084 ret
->type
= SLANG_OPER_RETURN
;
2085 ret
->locals
= _slang_variable_scope_new(scope
);
2086 ret
->num_children
= 1;
2087 ret
->children
= slang_operation_new(1);
2088 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2089 ret
->children
[0].a_id
= var
->a_name
;
2090 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2095 slang_print_function(fun, 1);
2103 _slang_is_vec_mat_type(const char *name
)
2105 static const char *vecmat_types
[] = {
2106 "float", "int", "bool",
2107 "vec2", "vec3", "vec4",
2108 "ivec2", "ivec3", "ivec4",
2109 "bvec2", "bvec3", "bvec4",
2110 "mat2", "mat3", "mat4",
2111 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2115 for (i
= 0; vecmat_types
[i
]; i
++)
2116 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2123 * Assemble a function call, given a particular function name.
2124 * \param name the function's name (operators like '*' are possible).
2126 static slang_ir_node
*
2127 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2128 slang_operation
*oper
, slang_operation
*dest
)
2130 slang_operation
*params
= oper
->children
;
2131 const GLuint param_count
= oper
->num_children
;
2133 slang_function
*fun
;
2136 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2137 if (atom
== SLANG_ATOM_NULL
)
2140 if (oper
->array_constructor
) {
2141 /* this needs special handling */
2142 fun
= _slang_make_array_constructor(A
, oper
);
2145 /* Try to find function by name and exact argument type matching */
2146 GLboolean error
= GL_FALSE
;
2147 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2148 &A
->space
, A
->atoms
, A
->log
, &error
);
2150 slang_info_log_error(A
->log
,
2151 "Function '%s' not found (check argument types)",
2158 /* Next, try locating a constructor function for a user-defined type */
2159 fun
= _slang_locate_struct_constructor(A
, name
);
2163 * At this point, some heuristics are used to try to find a function
2164 * that matches the calling signature by means of casting or "unrolling"
2168 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2169 /* Next, if this call looks like a vec() or mat() constructor call,
2170 * try "unwinding" the args to satisfy a constructor.
2172 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2174 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2175 slang_info_log_error(A
->log
,
2176 "Function '%s' not found (check argument types)",
2183 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2184 /* Next, try casting args to the types of the formal parameters */
2185 int numArgs
= oper
->num_children
;
2186 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2187 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2188 slang_info_log_error(A
->log
,
2189 "Function '%s' not found (check argument types)",
2197 slang_info_log_error(A
->log
,
2198 "Function '%s' not found (check argument types)",
2204 /* The function body may be in another compilation unit.
2205 * We'll try concatenating the shaders and recompile at link time.
2207 A
->UnresolvedRefs
= GL_TRUE
;
2208 return new_node1(IR_NOP
, NULL
);
2211 /* type checking to be sure function's return type matches 'dest' type */
2215 slang_typeinfo_construct(&t0
);
2216 typeof_operation(A
, dest
, &t0
);
2218 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2219 slang_info_log_error(A
->log
,
2220 "Incompatible type returned by call to '%s'",
2226 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2228 if (n
&& !n
->Store
&& !dest
2229 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2230 /* setup n->Store for the result of the function call */
2231 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2232 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2233 /*printf("Alloc storage for function result, size %d \n", size);*/
2236 if (oper
->array_constructor
) {
2237 /* free the temporary array constructor function now */
2238 slang_function_destruct(fun
);
2245 static slang_ir_node
*
2246 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2248 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2250 slang_variable
*var
;
2252 /* NOTE: In GLSL 1.20, there's only one kind of method
2253 * call: array.length(). Anything else is an error.
2255 if (oper
->a_id
!= a_length
) {
2256 slang_info_log_error(A
->log
,
2257 "Undefined method call '%s'", (char *) oper
->a_id
);
2261 /* length() takes no arguments */
2262 if (oper
->num_children
> 0) {
2263 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2267 /* lookup the object/variable */
2268 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2269 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2270 slang_info_log_error(A
->log
,
2271 "Undefined object '%s'", (char *) oper
->a_obj
);
2275 /* Create a float/literal IR node encoding the array length */
2276 n
= new_node0(IR_FLOAT
);
2278 n
->Value
[0] = (float) _slang_array_length(var
);
2279 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2286 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2288 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2289 oper
->type
== SLANG_OPER_LITERAL_INT
||
2290 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2291 if (oper
->literal
[0])
2297 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2298 oper
->num_children
== 1) {
2299 return _slang_is_constant_cond(&oper
->children
[0], value
);
2306 * Test if an operation is a scalar or boolean.
2309 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2311 slang_typeinfo type
;
2314 slang_typeinfo_construct(&type
);
2315 typeof_operation(A
, oper
, &type
);
2316 size
= _slang_sizeof_type_specifier(&type
.spec
);
2317 slang_typeinfo_destruct(&type
);
2323 * Test if an operation is boolean.
2326 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2328 slang_typeinfo type
;
2331 slang_typeinfo_construct(&type
);
2332 typeof_operation(A
, oper
, &type
);
2333 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2334 slang_typeinfo_destruct(&type
);
2340 * Generate loop code using high-level IR_LOOP instruction
2342 static slang_ir_node
*
2343 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2347 * BREAK if !expr (child[0])
2348 * body code (child[1])
2350 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2351 GLboolean isConst
, constTrue
;
2353 /* type-check expression */
2354 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2355 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2359 /* Check if loop condition is a constant */
2360 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2362 if (isConst
&& !constTrue
) {
2363 /* loop is never executed! */
2364 return new_node0(IR_NOP
);
2367 loop
= new_loop(NULL
);
2369 /* save old, push new loop */
2370 prevLoop
= A
->CurLoop
;
2373 if (isConst
&& constTrue
) {
2374 /* while(nonzero constant), no conditional break */
2379 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2380 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2382 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2383 loop
->Children
[0] = new_seq(breakIf
, body
);
2385 /* Do infinite loop detection */
2386 /* loop->List is head of linked list of break/continue nodes */
2387 if (!loop
->List
&& isConst
&& constTrue
) {
2388 /* infinite loop detected */
2389 A
->CurLoop
= prevLoop
; /* clean-up */
2390 slang_info_log_error(A
->log
, "Infinite loop detected!");
2394 /* pop loop, restore prev */
2395 A
->CurLoop
= prevLoop
;
2402 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2404 static slang_ir_node
*
2405 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2409 * body code (child[0])
2411 * BREAK if !expr (child[1])
2413 slang_ir_node
*prevLoop
, *loop
;
2414 GLboolean isConst
, constTrue
;
2416 /* type-check expression */
2417 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2418 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2422 loop
= new_loop(NULL
);
2424 /* save old, push new loop */
2425 prevLoop
= A
->CurLoop
;
2429 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2431 /* Check if loop condition is a constant */
2432 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2433 if (isConst
&& constTrue
) {
2434 /* do { } while(1) ==> no conditional break */
2435 loop
->Children
[1] = NULL
; /* no tail code */
2439 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2440 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2443 /* XXX we should do infinite loop detection, as above */
2445 /* pop loop, restore prev */
2446 A
->CurLoop
= prevLoop
;
2453 * Recursively count the number of operations rooted at 'oper'.
2454 * This gives some kind of indication of the size/complexity of an operation.
2457 sizeof_operation(const slang_operation
*oper
)
2460 GLuint count
= 1; /* me */
2462 for (i
= 0; i
< oper
->num_children
; i
++) {
2463 count
+= sizeof_operation(&oper
->children
[i
]);
2474 * Determine if a for-loop can be unrolled.
2475 * At this time, only a rather narrow class of for loops can be unrolled.
2476 * See code for details.
2477 * When a loop can't be unrolled because it's too large we'll emit a
2478 * message to the log.
2481 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2485 const char *varName
;
2488 assert(oper
->type
== SLANG_OPER_FOR
);
2489 assert(oper
->num_children
== 4);
2491 /* children[0] must be either "int i=constant" or "i=constant" */
2492 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2493 slang_variable
*var
;
2495 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2498 varId
= oper
->children
[0].children
[0].a_id
;
2500 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2504 if (!var
->initializer
)
2506 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2508 start
= (GLint
) var
->initializer
->literal
[0];
2510 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2511 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2513 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2515 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2518 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2520 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2526 /* children[1] must be "i<constant" */
2527 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2529 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2531 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2533 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2536 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2538 /* children[2] must be "i++" or "++i" */
2539 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2540 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2542 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2545 /* make sure the same variable name is used in all places */
2546 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2547 (oper
->children
[2].children
[0].a_id
!= varId
))
2550 varName
= (const char *) varId
;
2552 /* children[3], the loop body, can't be too large */
2553 bodySize
= sizeof_operation(&oper
->children
[3]);
2554 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2555 slang_info_log_print(A
->log
,
2556 "Note: 'for (%s ... )' body is too large/complex"
2563 return GL_FALSE
; /* degenerate case */
2565 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2566 slang_info_log_print(A
->log
,
2567 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2568 " many iterations to unroll",
2569 varName
, start
, varName
, end
, varName
);
2573 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2574 slang_info_log_print(A
->log
,
2575 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2576 " too much code to unroll",
2577 varName
, start
, varName
, end
, varName
);
2581 return GL_TRUE
; /* we can unroll the loop */
2586 _unroll_loop_inc(slang_assemble_ctx
* A
)
2593 _unroll_loop_dec(slang_assemble_ctx
* A
)
2600 * Unroll a for-loop.
2601 * First we determine the number of iterations to unroll.
2602 * Then for each iteration:
2603 * make a copy of the loop body
2604 * replace instances of the loop variable with the current iteration value
2605 * generate IR code for the body
2606 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2608 static slang_ir_node
*
2609 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2611 GLint start
, end
, iter
;
2612 slang_ir_node
*n
, *root
= NULL
;
2615 /* Set flag so code generator knows we're unrolling loops */
2616 _unroll_loop_inc( A
);
2618 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2619 /* for (int i=0; ... */
2620 slang_variable
*var
;
2622 varId
= oper
->children
[0].children
[0].a_id
;
2623 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2625 start
= (GLint
) var
->initializer
->literal
[0];
2629 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2630 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2633 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2635 for (iter
= start
; iter
< end
; iter
++) {
2636 slang_operation
*body
;
2638 /* make a copy of the loop body */
2639 body
= slang_operation_new(1);
2641 _unroll_loop_dec( A
);
2645 if (!slang_operation_copy(body
, &oper
->children
[3])) {
2646 _unroll_loop_dec( A
);
2650 /* in body, replace instances of 'varId' with literal 'iter' */
2652 slang_variable
*oldVar
;
2653 slang_operation
*newOper
;
2655 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2657 /* undeclared loop variable */
2658 slang_operation_delete(body
);
2659 _unroll_loop_dec( A
);
2663 newOper
= slang_operation_new(1);
2664 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2665 newOper
->literal_size
= 1;
2666 newOper
->literal
[0] = iter
;
2668 /* replace instances of the loop variable with newOper */
2669 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2672 /* do IR codegen for body */
2673 n
= _slang_gen_operation(A
, body
);
2675 _unroll_loop_dec( A
);
2679 root
= new_seq(root
, n
);
2681 slang_operation_delete(body
);
2684 _unroll_loop_dec( A
);
2691 * Generate IR for a for-loop. Unrolling will be done when possible.
2693 static slang_ir_node
*
2694 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2696 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2699 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2704 /* conventional for-loop code generation */
2707 * init code (child[0])
2709 * BREAK if !expr (child[1])
2710 * body code (child[3])
2712 * incr code (child[2]) // XXX continue here
2714 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2715 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2716 loop
= new_loop(NULL
);
2718 /* save old, push new loop */
2719 prevLoop
= A
->CurLoop
;
2722 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2723 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2724 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2725 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2727 loop
->Children
[0] = new_seq(breakIf
, body
);
2728 loop
->Children
[1] = incr
; /* tail code */
2730 /* pop loop, restore prev */
2731 A
->CurLoop
= prevLoop
;
2733 return new_seq(init
, loop
);
2738 static slang_ir_node
*
2739 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2741 slang_ir_node
*n
, *loopNode
;
2742 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2743 loopNode
= A
->CurLoop
;
2745 assert(loopNode
->Opcode
== IR_LOOP
);
2746 n
= new_node0(IR_CONT
);
2748 n
->Parent
= loopNode
;
2749 /* insert this node at head of linked list */
2750 n
->List
= loopNode
->List
;
2758 * Determine if the given operation is of a specific type.
2761 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2763 if (oper
->type
== type
)
2765 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2766 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2767 oper
->num_children
== 1)
2768 return is_operation_type(&oper
->children
[0], type
);
2775 * Generate IR tree for an if/then/else conditional using high-level
2776 * IR_IF instruction.
2778 static slang_ir_node
*
2779 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2782 * eval expr (child[0])
2789 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2790 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2791 GLboolean isConst
, constTrue
;
2793 /* type-check expression */
2794 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2795 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2799 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2800 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2804 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2808 return _slang_gen_operation(A
, &oper
->children
[1]);
2811 /* if (false) ... */
2812 return _slang_gen_operation(A
, &oper
->children
[2]);
2816 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2817 cond
= new_cond(cond
);
2819 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2820 && !haveElseClause
) {
2821 /* Special case: generate a conditional break */
2822 if (!A
->CurLoop
&& A
->UnrollLoop
) /* trying to unroll */
2824 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2827 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2828 && !haveElseClause
) {
2829 /* Special case: generate a conditional continue */
2830 if (!A
->CurLoop
&& A
->UnrollLoop
) /* trying to unroll */
2832 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2837 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2841 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2844 ifNode
= new_if(cond
, ifBody
, elseBody
);
2851 static slang_ir_node
*
2852 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2856 assert(oper
->type
== SLANG_OPER_NOT
);
2858 /* type-check expression */
2859 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2860 slang_info_log_error(A
->log
,
2861 "scalar/boolean expression expected for '!'");
2865 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2873 static slang_ir_node
*
2874 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2876 slang_ir_node
*n1
, *n2
;
2878 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2880 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2881 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2882 slang_info_log_error(A
->log
,
2883 "scalar/boolean expressions expected for '^^'");
2887 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2890 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2893 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2898 * Generate IR node for storage of a temporary of given size.
2900 static slang_ir_node
*
2901 _slang_gen_temporary(GLint size
)
2903 slang_ir_storage
*store
;
2904 slang_ir_node
*n
= NULL
;
2906 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2908 n
= new_node0(IR_VAR_DECL
);
2921 * Generate program constants for an array.
2922 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2923 * This will allocate and initialize three vector constants, storing
2924 * the array in constant memory, not temporaries like a non-const array.
2925 * This can also be used for uniform array initializers.
2926 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2929 make_constant_array(slang_assemble_ctx
*A
,
2930 slang_variable
*var
,
2931 slang_operation
*initializer
)
2933 struct gl_program
*prog
= A
->program
;
2934 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2935 const char *varName
= (char *) var
->a_name
;
2936 const GLuint numElements
= initializer
->num_children
;
2942 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2944 size
= var
->store
->Size
;
2946 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2947 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2948 assert(initializer
->type
== SLANG_OPER_CALL
);
2949 assert(initializer
->array_constructor
);
2951 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2953 /* convert constructor params into ordinary floats */
2954 for (i
= 0; i
< numElements
; i
++) {
2955 const slang_operation
*op
= &initializer
->children
[i
];
2956 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2957 /* unsupported type for this optimization */
2961 for (j
= 0; j
< op
->literal_size
; j
++) {
2962 values
[i
* 4 + j
] = op
->literal
[j
];
2964 for ( ; j
< 4; j
++) {
2965 values
[i
* 4 + j
] = 0.0f
;
2969 /* slightly different paths for constants vs. uniforms */
2970 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2971 var
->store
->File
= PROGRAM_UNIFORM
;
2972 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2973 size
, datatype
, values
);
2976 var
->store
->File
= PROGRAM_CONSTANT
;
2977 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2980 assert(var
->store
->Size
== size
);
2990 * Generate IR node for allocating/declaring a variable (either a local or
2992 * Generally, this involves allocating an slang_ir_storage instance for the
2993 * variable, choosing a register file (temporary, constant, etc).
2994 * For ordinary variables we do not yet allocate storage though. We do that
2995 * when we find the first actual use of the variable to avoid allocating temp
2996 * regs that will never get used.
2997 * At this time, uniforms are always allocated space in this function.
2999 * \param initializer Optional initializer expression for the variable.
3001 static slang_ir_node
*
3002 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3003 slang_operation
*initializer
)
3005 const char *varName
= (const char *) var
->a_name
;
3006 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3007 slang_ir_node
*varDecl
, *n
;
3008 slang_ir_storage
*store
;
3009 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3010 gl_register_file file
;
3012 /*assert(!var->declared);*/
3013 var
->declared
= GL_TRUE
;
3015 /* determine GPU register file for simple cases */
3016 if (is_sampler_type(&var
->type
)) {
3017 file
= PROGRAM_SAMPLER
;
3019 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3020 file
= PROGRAM_UNIFORM
;
3023 file
= PROGRAM_TEMPORARY
;
3026 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3028 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3032 arrayLen
= _slang_array_length(var
);
3033 totalSize
= _slang_array_size(size
, arrayLen
);
3035 /* Allocate IR node for the declaration */
3036 varDecl
= new_node0(IR_VAR_DECL
);
3040 /* Allocate slang_ir_storage for this variable if needed.
3041 * Note that we may not actually allocate a constant or temporary register
3045 GLint index
= -7; /* TBD / unknown */
3046 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3048 return NULL
; /* out of memory */
3051 /* set the IR node's Var and Store pointers */
3053 varDecl
->Store
= var
->store
;
3058 /* if there's an initializer, generate IR for the expression */
3060 slang_ir_node
*varRef
, *init
;
3062 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3063 /* if the variable is const, the initializer must be a const
3064 * expression as well.
3067 if (!_slang_is_constant_expr(initializer
)) {
3068 slang_info_log_error(A
->log
,
3069 "initializer for %s not constant", varName
);
3075 /* IR for the variable we're initializing */
3076 varRef
= new_var(A
, var
);
3078 slang_info_log_error(A
->log
, "out of memory");
3082 /* constant-folding, etc here */
3083 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3085 /* look for simple constant-valued variables and uniforms */
3086 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3087 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3089 if (initializer
->type
== SLANG_OPER_CALL
&&
3090 initializer
->array_constructor
) {
3091 /* array initializer */
3092 if (make_constant_array(A
, var
, initializer
))
3095 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3096 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3097 /* simple float/vector initializer */
3098 if (store
->File
== PROGRAM_UNIFORM
) {
3099 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3101 totalSize
, datatype
,
3102 initializer
->literal
);
3103 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3108 store
->File
= PROGRAM_CONSTANT
;
3109 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3111 initializer
->literal
,
3113 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3120 /* IR for initializer */
3121 init
= _slang_gen_operation(A
, initializer
);
3125 /* XXX remove this when type checking is added above */
3126 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3127 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3131 /* assign RHS to LHS */
3132 n
= new_node2(IR_COPY
, varRef
, init
);
3133 n
= new_seq(varDecl
, n
);
3136 /* no initializer */
3140 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3141 /* always need to allocate storage for uniforms at this point */
3142 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3143 totalSize
, datatype
, NULL
);
3144 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3148 printf("%s var %p %s store=%p index=%d size=%d\n",
3149 __FUNCTION__
, (void *) var
, (char *) varName
,
3150 (void *) store
, store
->Index
, store
->Size
);
3158 * Generate code for a selection expression: b ? x : y
3159 * XXX In some cases we could implement a selection expression
3160 * with an LRP instruction (use the boolean as the interpolant).
3161 * Otherwise, we use an IF/ELSE/ENDIF construct.
3163 static slang_ir_node
*
3164 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3166 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3167 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3168 slang_typeinfo type0
, type1
, type2
;
3169 int size
, isBool
, isEqual
;
3171 assert(oper
->type
== SLANG_OPER_SELECT
);
3172 assert(oper
->num_children
== 3);
3174 /* type of children[0] must be boolean */
3175 slang_typeinfo_construct(&type0
);
3176 typeof_operation(A
, &oper
->children
[0], &type0
);
3177 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3178 slang_typeinfo_destruct(&type0
);
3180 slang_info_log_error(A
->log
, "selector type is not boolean");
3184 slang_typeinfo_construct(&type1
);
3185 slang_typeinfo_construct(&type2
);
3186 typeof_operation(A
, &oper
->children
[1], &type1
);
3187 typeof_operation(A
, &oper
->children
[2], &type2
);
3188 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3189 slang_typeinfo_destruct(&type1
);
3190 slang_typeinfo_destruct(&type2
);
3192 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3196 /* size of x or y's type */
3197 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3201 tmpDecl
= _slang_gen_temporary(size
);
3203 /* the condition (child 0) */
3204 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3205 cond
= new_cond(cond
);
3207 /* if-true body (child 1) */
3208 tmpVar
= new_node0(IR_VAR
);
3209 tmpVar
->Store
= tmpDecl
->Store
;
3210 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3211 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3213 /* if-false body (child 2) */
3214 tmpVar
= new_node0(IR_VAR
);
3215 tmpVar
->Store
= tmpDecl
->Store
;
3216 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3217 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3219 ifNode
= new_if(cond
, trueNode
, falseNode
);
3222 tmpVar
= new_node0(IR_VAR
);
3223 tmpVar
->Store
= tmpDecl
->Store
;
3225 tree
= new_seq(ifNode
, tmpVar
);
3226 tree
= new_seq(tmpDecl
, tree
);
3228 /*_slang_print_ir_tree(tree, 10);*/
3234 * Generate code for &&.
3236 static slang_ir_node
*
3237 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3239 /* rewrite "a && b" as "a ? b : false" */
3240 slang_operation
*select
;
3243 select
= slang_operation_new(1);
3244 select
->type
= SLANG_OPER_SELECT
;
3245 select
->num_children
= 3;
3246 select
->children
= slang_operation_new(3);
3248 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3249 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3250 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3251 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3252 select
->children
[2].literal_size
= 1;
3254 n
= _slang_gen_select(A
, select
);
3260 * Generate code for ||.
3262 static slang_ir_node
*
3263 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3265 /* rewrite "a || b" as "a ? true : b" */
3266 slang_operation
*select
;
3269 select
= slang_operation_new(1);
3270 select
->type
= SLANG_OPER_SELECT
;
3271 select
->num_children
= 3;
3272 select
->children
= slang_operation_new(3);
3274 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3275 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3276 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3277 select
->children
[1].literal_size
= 1;
3278 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3280 n
= _slang_gen_select(A
, select
);
3286 * Generate IR tree for a return statement.
3288 static slang_ir_node
*
3289 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3291 const GLboolean haveReturnValue
3292 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3294 /* error checking */
3295 assert(A
->CurFunction
);
3296 if (haveReturnValue
&&
3297 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3298 slang_info_log_error(A
->log
, "illegal return expression");
3301 else if (!haveReturnValue
&&
3302 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3303 slang_info_log_error(A
->log
, "return statement requires an expression");
3307 if (!haveReturnValue
) {
3308 return new_return(A
->curFuncEndLabel
);
3316 * return; // goto __endOfFunction
3318 slang_operation
*assign
;
3319 slang_atom a_retVal
;
3322 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3328 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3330 /* trying to return a value in a void-valued function */
3336 assign
= slang_operation_new(1);
3337 assign
->type
= SLANG_OPER_ASSIGN
;
3338 assign
->num_children
= 2;
3339 assign
->children
= slang_operation_new(2);
3340 /* lhs (__retVal) */
3341 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3342 assign
->children
[0].a_id
= a_retVal
;
3343 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3345 /* XXX we might be able to avoid this copy someday */
3346 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3348 /* assemble the new code */
3349 n
= new_seq(_slang_gen_operation(A
, assign
),
3350 new_return(A
->curFuncEndLabel
));
3352 slang_operation_delete(assign
);
3360 * Determine if the given operation/expression is const-valued.
3363 _slang_is_constant_expr(const slang_operation
*oper
)
3365 slang_variable
*var
;
3368 switch (oper
->type
) {
3369 case SLANG_OPER_IDENTIFIER
:
3370 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3371 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3375 for (i
= 0; i
< oper
->num_children
; i
++) {
3376 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3386 * Check if an assignment of type t1 to t0 is legal.
3387 * XXX more cases needed.
3390 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3391 slang_operation
*op0
,
3392 slang_operation
*op1
)
3394 slang_typeinfo t0
, t1
;
3397 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3398 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3402 slang_typeinfo_construct(&t0
);
3403 typeof_operation(A
, op0
, &t0
);
3405 slang_typeinfo_construct(&t1
);
3406 typeof_operation(A
, op1
, &t1
);
3408 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3409 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3413 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3418 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3419 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3420 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3423 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3424 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3427 #if 0 /* not used just yet - causes problems elsewhere */
3428 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3429 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3433 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3434 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3437 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3438 t1
.spec
.type
== SLANG_SPEC_INT
)
3446 * Generate IR tree for a local variable declaration.
3447 * Basically do some error checking and call _slang_gen_var_decl().
3449 static slang_ir_node
*
3450 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3452 const char *varName
= (char *) oper
->a_id
;
3453 slang_variable
*var
;
3454 slang_ir_node
*varDecl
;
3455 slang_operation
*initializer
;
3457 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3458 assert(oper
->num_children
<= 1);
3460 /* lookup the variable by name */
3461 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3463 return NULL
; /* "shouldn't happen" */
3465 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3466 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3467 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3468 /* can't declare attribute/uniform vars inside functions */
3469 slang_info_log_error(A
->log
,
3470 "local variable '%s' cannot be an attribute/uniform/varying",
3477 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3482 /* check if the var has an initializer */
3483 if (oper
->num_children
> 0) {
3484 assert(oper
->num_children
== 1);
3485 initializer
= &oper
->children
[0];
3487 else if (var
->initializer
) {
3488 initializer
= var
->initializer
;
3495 /* check/compare var type and initializer type */
3496 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3497 slang_info_log_error(A
->log
, "incompatible types in assignment");
3502 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3503 slang_info_log_error(A
->log
,
3504 "const-qualified variable '%s' requires initializer",
3510 /* Generate IR node */
3511 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3520 * Generate IR tree for a reference to a variable (such as in an expression).
3521 * This is different from a variable declaration.
3523 static slang_ir_node
*
3524 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3526 /* If there's a variable associated with this oper (from inlining)
3527 * use it. Otherwise, use the oper's var id.
3529 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3530 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3533 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3536 assert(var
->declared
);
3537 n
= new_var(A
, var
);
3544 * Return the number of components actually named by the swizzle.
3545 * Recall that swizzles may have undefined/don't-care values.
3548 swizzle_size(GLuint swizzle
)
3551 for (i
= 0; i
< 4; i
++) {
3552 GLuint swz
= GET_SWZ(swizzle
, i
);
3553 size
+= (swz
>= 0 && swz
<= 3);
3559 static slang_ir_node
*
3560 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3562 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3566 n
->Store
= _slang_new_ir_storage_relative(0,
3567 swizzle_size(swizzle
),
3569 n
->Store
->Swizzle
= swizzle
;
3576 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3578 while (store
->Parent
)
3579 store
= store
->Parent
;
3581 if (!(store
->File
== PROGRAM_OUTPUT
||
3582 store
->File
== PROGRAM_TEMPORARY
||
3583 (store
->File
== PROGRAM_VARYING
&&
3584 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3594 * Walk up an IR storage path to compute the final swizzle.
3595 * This is used when we find an expression such as "foo.xz.yx".
3598 root_swizzle(const slang_ir_storage
*st
)
3600 GLuint swizzle
= st
->Swizzle
;
3601 while (st
->Parent
) {
3603 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3610 * Generate IR tree for an assignment (=).
3612 static slang_ir_node
*
3613 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3615 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3616 /* Check that var is writeable */
3618 = _slang_variable_locate(oper
->children
[0].locals
,
3619 oper
->children
[0].a_id
, GL_TRUE
);
3621 slang_info_log_error(A
->log
, "undefined variable '%s'",
3622 (char *) oper
->children
[0].a_id
);
3625 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3626 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3627 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3628 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3629 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3630 slang_info_log_error(A
->log
,
3631 "illegal assignment to read-only variable '%s'",
3632 (char *) oper
->children
[0].a_id
);
3637 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3638 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3639 /* Special case of: x = f(a, b)
3640 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3642 * XXX this could be even more effective if we could accomodate
3643 * cases such as "v.x = f();" - would help with typical vertex
3647 n
= _slang_gen_function_call_name(A
,
3648 (const char *) oper
->children
[1].a_id
,
3649 &oper
->children
[1], &oper
->children
[0]);
3653 slang_ir_node
*n
, *lhs
, *rhs
;
3655 /* lhs and rhs type checking */
3656 if (!_slang_assignment_compatible(A
,
3658 &oper
->children
[1])) {
3659 slang_info_log_error(A
->log
, "incompatible types in assignment");
3663 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3669 slang_info_log_error(A
->log
,
3670 "invalid left hand side for assignment");
3674 /* check that lhs is writable */
3675 if (!is_store_writable(A
, lhs
->Store
)) {
3676 slang_info_log_error(A
->log
,
3677 "illegal assignment to read-only l-value");
3681 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3683 /* convert lhs swizzle into writemask */
3684 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3685 GLuint writemask
, newSwizzle
= 0x0;
3686 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3687 /* Non-simple writemask, need to swizzle right hand side in
3688 * order to put components into the right place.
3690 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3692 n
= new_node2(IR_COPY
, lhs
, rhs
);
3703 * Generate IR tree for referencing a field in a struct (or basic vector type)
3705 static slang_ir_node
*
3706 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3710 /* type of struct */
3711 slang_typeinfo_construct(&ti
);
3712 typeof_operation(A
, &oper
->children
[0], &ti
);
3714 if (_slang_type_is_vector(ti
.spec
.type
)) {
3715 /* the field should be a swizzle */
3716 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3720 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3721 slang_info_log_error(A
->log
, "Bad swizzle");
3724 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3729 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3730 /* create new parent node with swizzle */
3732 n
= _slang_gen_swizzle(n
, swizzle
);
3735 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3736 || ti
.spec
.type
== SLANG_SPEC_INT
3737 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3738 const GLuint rows
= 1;
3742 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3743 slang_info_log_error(A
->log
, "Bad swizzle");
3745 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3749 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3750 /* create new parent node with swizzle */
3751 n
= _slang_gen_swizzle(n
, swizzle
);
3755 /* the field is a structure member (base.field) */
3756 /* oper->children[0] is the base */
3757 /* oper->a_id is the field name */
3758 slang_ir_node
*base
, *n
;
3759 slang_typeinfo field_ti
;
3760 GLint fieldSize
, fieldOffset
= -1;
3763 slang_typeinfo_construct(&field_ti
);
3764 typeof_operation(A
, oper
, &field_ti
);
3766 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3768 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3770 if (fieldSize
== 0 || fieldOffset
< 0) {
3771 const char *structName
;
3772 if (ti
.spec
._struct
)
3773 structName
= (char *) ti
.spec
._struct
->a_name
;
3775 structName
= "unknown";
3776 slang_info_log_error(A
->log
,
3777 "\"%s\" is not a member of struct \"%s\"",
3778 (char *) oper
->a_id
, structName
);
3781 assert(fieldSize
>= 0);
3783 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3785 /* error msg should have already been logged */
3789 n
= new_node1(IR_FIELD
, base
);
3793 n
->Field
= (char *) oper
->a_id
;
3795 /* Store the field's offset in storage->Index */
3796 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3806 * Gen code for array indexing.
3808 static slang_ir_node
*
3809 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3811 slang_typeinfo array_ti
;
3813 /* get array's type info */
3814 slang_typeinfo_construct(&array_ti
);
3815 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3817 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3818 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3819 /* translate the index into a swizzle/writemask: "v.x=p" */
3820 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3824 index
= (GLint
) oper
->children
[1].literal
[0];
3825 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3826 index
>= (GLint
) max
) {
3828 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3829 printf("type = %d\n", oper
->children
[1].type
);
3830 printf("index = %d, max = %d\n", index
, max
);
3831 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3832 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3839 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3841 /* use swizzle to access the element */
3842 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3846 n
= _slang_gen_swizzle(n
, swizzle
);
3852 /* conventional array */
3853 slang_typeinfo elem_ti
;
3854 slang_ir_node
*elem
, *array
, *index
;
3855 GLint elemSize
, arrayLen
;
3857 /* size of array element */
3858 slang_typeinfo_construct(&elem_ti
);
3859 typeof_operation(A
, oper
, &elem_ti
);
3860 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3862 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3863 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3865 arrayLen
= array_ti
.array_len
;
3867 slang_typeinfo_destruct(&array_ti
);
3868 slang_typeinfo_destruct(&elem_ti
);
3870 if (elemSize
<= 0) {
3871 /* unknown var or type */
3872 slang_info_log_error(A
->log
, "Undefined variable or type");
3876 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3877 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3878 if (array
&& index
) {
3880 GLint constIndex
= -1;
3881 if (index
->Opcode
== IR_FLOAT
) {
3882 constIndex
= (int) index
->Value
[0];
3883 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3884 slang_info_log_error(A
->log
,
3885 "Array index out of bounds (index=%d size=%d)",
3886 constIndex
, arrayLen
);
3887 _slang_free_ir_tree(array
);
3888 _slang_free_ir_tree(index
);
3893 if (!array
->Store
) {
3894 slang_info_log_error(A
->log
, "Invalid array");
3898 elem
= new_node2(IR_ELEMENT
, array
, index
);
3900 /* The storage info here will be updated during code emit */
3901 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3902 array
->Store
->Index
,
3904 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3908 _slang_free_ir_tree(array
);
3909 _slang_free_ir_tree(index
);
3916 static slang_ir_node
*
3917 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3918 slang_ir_opcode opcode
)
3920 slang_typeinfo t0
, t1
;
3923 slang_typeinfo_construct(&t0
);
3924 typeof_operation(A
, &oper
->children
[0], &t0
);
3926 slang_typeinfo_construct(&t1
);
3927 typeof_operation(A
, &oper
->children
[0], &t1
);
3929 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3930 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3931 slang_info_log_error(A
->log
, "Illegal array comparison");
3935 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3936 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3937 /* <, <=, >, >= can only be used with scalars */
3938 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3939 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3940 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3941 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3942 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3947 n
= new_node2(opcode
,
3948 _slang_gen_operation(A
, &oper
->children
[0]),
3949 _slang_gen_operation(A
, &oper
->children
[1]));
3951 /* result is a bool (size 1) */
3952 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3960 print_vars(slang_variable_scope
*s
)
3964 for (i
= 0; i
< s
->num_variables
; i
++) {
3966 (char*) s
->variables
[i
]->a_name
,
3967 s
->variables
[i
]->declared
);
3977 _slang_undeclare_vars(slang_variable_scope
*locals
)
3979 if (locals
->num_variables
> 0) {
3981 for (i
= 0; i
< locals
->num_variables
; i
++) {
3982 slang_variable
*v
= locals
->variables
[i
];
3983 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3984 v
->declared
= GL_FALSE
;
3992 * Generate IR tree for a slang_operation (AST node)
3994 static slang_ir_node
*
3995 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3997 switch (oper
->type
) {
3998 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4002 _slang_push_var_table(A
->vartable
);
4004 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4005 n
= _slang_gen_operation(A
, oper
);
4006 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4008 _slang_pop_var_table(A
->vartable
);
4010 /*_slang_undeclare_vars(oper->locals);*/
4011 /*print_vars(oper->locals);*/
4014 n
= new_node1(IR_SCOPE
, n
);
4019 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4020 /* list of operations */
4021 if (oper
->num_children
> 0)
4023 slang_ir_node
*n
, *tree
= NULL
;
4026 for (i
= 0; i
< oper
->num_children
; i
++) {
4027 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4029 _slang_free_ir_tree(tree
);
4030 return NULL
; /* error must have occured */
4032 tree
= new_seq(tree
, n
);
4038 return new_node0(IR_NOP
);
4041 case SLANG_OPER_EXPRESSION
:
4042 return _slang_gen_operation(A
, &oper
->children
[0]);
4044 case SLANG_OPER_FOR
:
4045 return _slang_gen_for(A
, oper
);
4047 return _slang_gen_do(A
, oper
);
4048 case SLANG_OPER_WHILE
:
4049 return _slang_gen_while(A
, oper
);
4050 case SLANG_OPER_BREAK
:
4053 slang_info_log_error(A
->log
, "'break' not in loop");
4056 return new_break(A
->CurLoop
);
4057 case SLANG_OPER_CONTINUE
:
4060 slang_info_log_error(A
->log
, "'continue' not in loop");
4063 return _slang_gen_continue(A
, oper
);
4064 case SLANG_OPER_DISCARD
:
4065 return new_node0(IR_KILL
);
4067 case SLANG_OPER_EQUAL
:
4068 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4069 case SLANG_OPER_NOTEQUAL
:
4070 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4071 case SLANG_OPER_GREATER
:
4072 return _slang_gen_compare(A
, oper
, IR_SGT
);
4073 case SLANG_OPER_LESS
:
4074 return _slang_gen_compare(A
, oper
, IR_SLT
);
4075 case SLANG_OPER_GREATEREQUAL
:
4076 return _slang_gen_compare(A
, oper
, IR_SGE
);
4077 case SLANG_OPER_LESSEQUAL
:
4078 return _slang_gen_compare(A
, oper
, IR_SLE
);
4079 case SLANG_OPER_ADD
:
4082 assert(oper
->num_children
== 2);
4083 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4086 case SLANG_OPER_SUBTRACT
:
4089 assert(oper
->num_children
== 2);
4090 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4093 case SLANG_OPER_MULTIPLY
:
4096 assert(oper
->num_children
== 2);
4097 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4100 case SLANG_OPER_DIVIDE
:
4103 assert(oper
->num_children
== 2);
4104 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4107 case SLANG_OPER_MINUS
:
4110 assert(oper
->num_children
== 1);
4111 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4114 case SLANG_OPER_PLUS
:
4115 /* +expr --> do nothing */
4116 return _slang_gen_operation(A
, &oper
->children
[0]);
4117 case SLANG_OPER_VARIABLE_DECL
:
4118 return _slang_gen_declaration(A
, oper
);
4119 case SLANG_OPER_ASSIGN
:
4120 return _slang_gen_assignment(A
, oper
);
4121 case SLANG_OPER_ADDASSIGN
:
4124 assert(oper
->num_children
== 2);
4125 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4128 case SLANG_OPER_SUBASSIGN
:
4131 assert(oper
->num_children
== 2);
4132 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4136 case SLANG_OPER_MULASSIGN
:
4139 assert(oper
->num_children
== 2);
4140 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4143 case SLANG_OPER_DIVASSIGN
:
4146 assert(oper
->num_children
== 2);
4147 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4150 case SLANG_OPER_LOGICALAND
:
4153 assert(oper
->num_children
== 2);
4154 n
= _slang_gen_logical_and(A
, oper
);
4157 case SLANG_OPER_LOGICALOR
:
4160 assert(oper
->num_children
== 2);
4161 n
= _slang_gen_logical_or(A
, oper
);
4164 case SLANG_OPER_LOGICALXOR
:
4165 return _slang_gen_xor(A
, oper
);
4166 case SLANG_OPER_NOT
:
4167 return _slang_gen_not(A
, oper
);
4168 case SLANG_OPER_SELECT
: /* b ? x : y */
4171 assert(oper
->num_children
== 3);
4172 n
= _slang_gen_select(A
, oper
);
4176 case SLANG_OPER_ASM
:
4177 return _slang_gen_asm(A
, oper
, NULL
);
4178 case SLANG_OPER_CALL
:
4179 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4181 case SLANG_OPER_METHOD
:
4182 return _slang_gen_method_call(A
, oper
);
4183 case SLANG_OPER_RETURN
:
4184 return _slang_gen_return(A
, oper
);
4185 case SLANG_OPER_LABEL
:
4186 return new_label(oper
->label
);
4187 case SLANG_OPER_IDENTIFIER
:
4188 return _slang_gen_variable(A
, oper
);
4190 return _slang_gen_if(A
, oper
);
4191 case SLANG_OPER_FIELD
:
4192 return _slang_gen_struct_field(A
, oper
);
4193 case SLANG_OPER_SUBSCRIPT
:
4194 return _slang_gen_array_element(A
, oper
);
4195 case SLANG_OPER_LITERAL_FLOAT
:
4197 case SLANG_OPER_LITERAL_INT
:
4199 case SLANG_OPER_LITERAL_BOOL
:
4200 return new_float_literal(oper
->literal
, oper
->literal_size
);
4202 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4205 assert(oper
->num_children
== 1);
4206 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4209 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4212 assert(oper
->num_children
== 1);
4213 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4216 case SLANG_OPER_PREINCREMENT
: /* ++var */
4219 assert(oper
->num_children
== 1);
4220 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4223 case SLANG_OPER_PREDECREMENT
: /* --var */
4226 assert(oper
->num_children
== 1);
4227 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4231 case SLANG_OPER_NON_INLINED_CALL
:
4232 case SLANG_OPER_SEQUENCE
:
4234 slang_ir_node
*tree
= NULL
;
4236 for (i
= 0; i
< oper
->num_children
; i
++) {
4237 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4238 tree
= new_seq(tree
, n
);
4240 tree
->Store
= n
->Store
;
4242 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4243 tree
= new_function_call(tree
, oper
->label
);
4248 case SLANG_OPER_NONE
:
4249 case SLANG_OPER_VOID
:
4250 /* returning NULL here would generate an error */
4251 return new_node0(IR_NOP
);
4254 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4256 return new_node0(IR_NOP
);
4264 * Check if the given type specifier is a rectangular texture sampler.
4267 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4269 while (spec
->_array
) {
4270 spec
= spec
->_array
;
4272 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4273 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4279 * Called by compiler when a global variable has been parsed/compiled.
4280 * Here we examine the variable's type to determine what kind of register
4281 * storage will be used.
4283 * A uniform such as "gl_Position" will become the register specification
4284 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4285 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4287 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4288 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4289 * actual texture unit (as specified by the user calling glUniform1i()).
4292 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4293 slang_unit_type type
)
4295 struct gl_program
*prog
= A
->program
;
4296 const char *varName
= (char *) var
->a_name
;
4297 GLboolean success
= GL_TRUE
;
4298 slang_ir_storage
*store
= NULL
;
4300 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4301 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4302 const GLint arrayLen
= _slang_array_length(var
);
4303 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4304 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4306 /* check for sampler2D arrays */
4307 if (texIndex
== -1 && var
->type
.specifier
._array
)
4308 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4310 if (texIndex
!= -1) {
4311 /* This is a texture sampler variable...
4312 * store->File = PROGRAM_SAMPLER
4313 * store->Index = sampler number (0..7, typically)
4314 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4316 if (var
->initializer
) {
4317 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4320 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4321 /* disallow rect samplers */
4322 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4323 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4327 (void) is_rect_sampler_spec
; /* silence warning */
4330 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4331 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4333 /* If we have a sampler array, then we need to allocate the
4334 * additional samplers to ensure we don't allocate them elsewhere.
4335 * We can't directly use _mesa_add_sampler() as that checks the
4336 * varName and gets a match, so we call _mesa_add_parameter()
4337 * directly and use the last sampler number from the call above.
4340 GLint a
= arrayLen
- 1;
4342 for (i
= 0; i
< a
; i
++) {
4343 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4344 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4345 varName
, 1, datatype
, &value
, NULL
, 0x0);
4349 if (dbg
) printf("SAMPLER ");
4351 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4352 /* Uniform variable */
4353 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4356 /* user-defined uniform */
4357 if (datatype
== GL_NONE
) {
4358 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
4359 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
4360 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
4361 /* temporary work-around */
4362 GLenum datatype
= GL_FLOAT
;
4363 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4364 totalSize
, datatype
, NULL
);
4365 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4366 totalSize
, swizzle
);
4369 GLint a
= arrayLen
- 1;
4371 for (i
= 0; i
< a
; i
++) {
4372 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
4373 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
4374 varName
, 1, datatype
, &value
, NULL
, 0x0);
4378 /* XXX what we need to do is unroll the struct into its
4379 * basic types, creating a uniform variable for each.
4387 * Should produce uniforms:
4388 * "f.a" (GL_FLOAT_VEC3)
4389 * "f.b" (GL_FLOAT_VEC4)
4392 if (var
->initializer
) {
4393 slang_info_log_error(A
->log
,
4394 "unsupported initializer for uniform '%s'", varName
);
4399 slang_info_log_error(A
->log
,
4400 "invalid datatype for uniform variable %s",
4406 /* non-struct uniform */
4407 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4413 /* pre-defined uniform, like gl_ModelviewMatrix */
4414 /* We know it's a uniform, but don't allocate storage unless
4417 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4418 totalSize
, swizzle
);
4420 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4422 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4423 /* varyings must be float, vec or mat */
4424 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4425 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4426 slang_info_log_error(A
->log
,
4427 "varying '%s' must be float/vector/matrix",
4432 if (var
->initializer
) {
4433 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4439 /* user-defined varying */
4445 if (var
->type
.centroid
== SLANG_CENTROID
)
4446 flags
|= PROG_PARAM_BIT_CENTROID
;
4447 if (var
->type
.variant
== SLANG_INVARIANT
)
4448 flags
|= PROG_PARAM_BIT_INVARIANT
;
4450 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4452 swizzle
= _slang_var_swizzle(size
, 0);
4453 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4454 totalSize
, swizzle
);
4457 /* pre-defined varying, like gl_Color or gl_TexCoord */
4458 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4459 /* fragment program input */
4461 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4464 assert(index
< FRAG_ATTRIB_MAX
);
4465 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4469 /* vertex program output */
4470 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4471 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4473 assert(index
< VERT_RESULT_MAX
);
4474 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4475 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4478 if (dbg
) printf("V/F ");
4480 if (dbg
) printf("VARYING ");
4482 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4485 /* attributes must be float, vec or mat */
4486 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4487 slang_info_log_error(A
->log
,
4488 "attribute '%s' must be float/vector/matrix",
4494 /* user-defined vertex attribute */
4495 const GLint attr
= -1; /* unknown */
4496 swizzle
= _slang_var_swizzle(size
, 0);
4497 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4498 size
, datatype
, attr
);
4500 index
= VERT_ATTRIB_GENERIC0
+ index
;
4503 /* pre-defined vertex attrib */
4504 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4507 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4508 if (dbg
) printf("ATTRIB ");
4510 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4511 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4512 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4514 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4515 if (dbg
) printf("INPUT ");
4517 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4518 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4519 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4520 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4523 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4524 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4525 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4526 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4528 if (dbg
) printf("OUTPUT ");
4530 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4531 /* pre-defined global constant, like gl_MaxLights */
4532 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4533 if (dbg
) printf("CONST ");
4536 /* ordinary variable (may be const) */
4539 /* IR node to declare the variable */
4540 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4542 /* emit GPU instructions */
4543 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
4545 _slang_free_ir_tree(n
);
4548 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4549 store
? store
->Index
: -2);
4552 var
->store
= store
; /* save var's storage info */
4554 var
->declared
= GL_TRUE
;
4561 * Produce an IR tree from a function AST (fun->body).
4562 * Then call the code emitter to convert the IR tree into gl_program
4566 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4569 GLboolean success
= GL_TRUE
;
4571 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4572 /* we only really generate code for main, all other functions get
4573 * inlined or codegen'd upon an actual call.
4576 /* do some basic error checking though */
4577 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4578 /* check that non-void functions actually return something */
4580 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4582 slang_info_log_error(A
->log
,
4583 "function \"%s\" has no return statement",
4584 (char *) fun
->header
.a_name
);
4586 "function \"%s\" has no return statement\n",
4587 (char *) fun
->header
.a_name
);
4592 return GL_TRUE
; /* not an error */
4596 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4597 slang_print_function(fun
, 1);
4600 /* should have been allocated earlier: */
4601 assert(A
->program
->Parameters
);
4602 assert(A
->program
->Varying
);
4603 assert(A
->vartable
);
4605 A
->CurFunction
= fun
;
4607 /* fold constant expressions, etc. */
4608 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4611 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4612 slang_print_function(fun
, 1);
4615 /* Create an end-of-function label */
4616 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4618 /* push new vartable scope */
4619 _slang_push_var_table(A
->vartable
);
4621 /* Generate IR tree for the function body code */
4622 n
= _slang_gen_operation(A
, fun
->body
);
4624 n
= new_node1(IR_SCOPE
, n
);
4626 /* pop vartable, restore previous */
4627 _slang_pop_var_table(A
->vartable
);
4630 /* XXX record error */
4634 /* append an end-of-function-label to IR tree */
4635 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4637 /*_slang_label_delete(A->curFuncEndLabel);*/
4638 A
->curFuncEndLabel
= NULL
;
4641 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4642 slang_print_function(fun
, 1);
4645 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4646 _slang_print_ir_tree(n
, 0);
4649 printf("************* End codegen function ************\n\n");
4652 if (A
->UnresolvedRefs
) {
4653 /* Can't codegen at this time.
4654 * At link time we'll concatenate all the vertex shaders and/or all
4655 * the fragment shaders and try recompiling.
4660 /* Emit program instructions */
4661 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
4662 _slang_free_ir_tree(n
);
4664 /* free codegen context */
4666 _mesa_free(A->codegen);