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)",
2203 slang_info_log_error(A
->log
,
2204 "Function '%s' prototyped but not defined. "
2205 "Separate compilation units not supported.",
2210 /* type checking to be sure function's return type matches 'dest' type */
2214 slang_typeinfo_construct(&t0
);
2215 typeof_operation(A
, dest
, &t0
);
2217 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2218 slang_info_log_error(A
->log
,
2219 "Incompatible type returned by call to '%s'",
2225 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2227 if (n
&& !n
->Store
&& !dest
2228 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2229 /* setup n->Store for the result of the function call */
2230 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2231 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2232 /*printf("Alloc storage for function result, size %d \n", size);*/
2235 if (oper
->array_constructor
) {
2236 /* free the temporary array constructor function now */
2237 slang_function_destruct(fun
);
2244 static slang_ir_node
*
2245 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2247 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2249 slang_variable
*var
;
2251 /* NOTE: In GLSL 1.20, there's only one kind of method
2252 * call: array.length(). Anything else is an error.
2254 if (oper
->a_id
!= a_length
) {
2255 slang_info_log_error(A
->log
,
2256 "Undefined method call '%s'", (char *) oper
->a_id
);
2260 /* length() takes no arguments */
2261 if (oper
->num_children
> 0) {
2262 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2266 /* lookup the object/variable */
2267 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2268 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2269 slang_info_log_error(A
->log
,
2270 "Undefined object '%s'", (char *) oper
->a_obj
);
2274 /* Create a float/literal IR node encoding the array length */
2275 n
= new_node0(IR_FLOAT
);
2277 n
->Value
[0] = (float) _slang_array_length(var
);
2278 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2285 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2287 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2288 oper
->type
== SLANG_OPER_LITERAL_INT
||
2289 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2290 if (oper
->literal
[0])
2296 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2297 oper
->num_children
== 1) {
2298 return _slang_is_constant_cond(&oper
->children
[0], value
);
2305 * Test if an operation is a scalar or boolean.
2308 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2310 slang_typeinfo type
;
2313 slang_typeinfo_construct(&type
);
2314 typeof_operation(A
, oper
, &type
);
2315 size
= _slang_sizeof_type_specifier(&type
.spec
);
2316 slang_typeinfo_destruct(&type
);
2322 * Test if an operation is boolean.
2325 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2327 slang_typeinfo type
;
2330 slang_typeinfo_construct(&type
);
2331 typeof_operation(A
, oper
, &type
);
2332 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2333 slang_typeinfo_destruct(&type
);
2339 * Generate loop code using high-level IR_LOOP instruction
2341 static slang_ir_node
*
2342 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2346 * BREAK if !expr (child[0])
2347 * body code (child[1])
2349 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2350 GLboolean isConst
, constTrue
;
2352 /* type-check expression */
2353 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2354 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2358 /* Check if loop condition is a constant */
2359 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2361 if (isConst
&& !constTrue
) {
2362 /* loop is never executed! */
2363 return new_node0(IR_NOP
);
2366 loop
= new_loop(NULL
);
2368 /* save old, push new loop */
2369 prevLoop
= A
->CurLoop
;
2372 if (isConst
&& constTrue
) {
2373 /* while(nonzero constant), no conditional break */
2378 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2379 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2381 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2382 loop
->Children
[0] = new_seq(breakIf
, body
);
2384 /* Do infinite loop detection */
2385 /* loop->List is head of linked list of break/continue nodes */
2386 if (!loop
->List
&& isConst
&& constTrue
) {
2387 /* infinite loop detected */
2388 A
->CurLoop
= prevLoop
; /* clean-up */
2389 slang_info_log_error(A
->log
, "Infinite loop detected!");
2393 /* pop loop, restore prev */
2394 A
->CurLoop
= prevLoop
;
2401 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2403 static slang_ir_node
*
2404 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2408 * body code (child[0])
2410 * BREAK if !expr (child[1])
2412 slang_ir_node
*prevLoop
, *loop
;
2413 GLboolean isConst
, constTrue
;
2415 /* type-check expression */
2416 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2417 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2421 loop
= new_loop(NULL
);
2423 /* save old, push new loop */
2424 prevLoop
= A
->CurLoop
;
2428 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2430 /* Check if loop condition is a constant */
2431 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2432 if (isConst
&& constTrue
) {
2433 /* do { } while(1) ==> no conditional break */
2434 loop
->Children
[1] = NULL
; /* no tail code */
2438 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2439 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2442 /* XXX we should do infinite loop detection, as above */
2444 /* pop loop, restore prev */
2445 A
->CurLoop
= prevLoop
;
2452 * Recursively count the number of operations rooted at 'oper'.
2453 * This gives some kind of indication of the size/complexity of an operation.
2456 sizeof_operation(const slang_operation
*oper
)
2459 GLuint count
= 1; /* me */
2461 for (i
= 0; i
< oper
->num_children
; i
++) {
2462 count
+= sizeof_operation(&oper
->children
[i
]);
2473 * Determine if a for-loop can be unrolled.
2474 * At this time, only a rather narrow class of for loops can be unrolled.
2475 * See code for details.
2476 * When a loop can't be unrolled because it's too large we'll emit a
2477 * message to the log.
2480 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2484 const char *varName
;
2487 assert(oper
->type
== SLANG_OPER_FOR
);
2488 assert(oper
->num_children
== 4);
2490 /* children[0] must be either "int i=constant" or "i=constant" */
2491 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2492 slang_variable
*var
;
2494 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2497 varId
= oper
->children
[0].children
[0].a_id
;
2499 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2503 if (!var
->initializer
)
2505 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2507 start
= (GLint
) var
->initializer
->literal
[0];
2509 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2510 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2512 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2514 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2517 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2519 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2525 /* children[1] must be "i<constant" */
2526 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2528 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2530 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2532 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2535 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2537 /* children[2] must be "i++" or "++i" */
2538 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2539 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2541 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2544 /* make sure the same variable name is used in all places */
2545 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2546 (oper
->children
[2].children
[0].a_id
!= varId
))
2549 varName
= (const char *) varId
;
2551 /* children[3], the loop body, can't be too large */
2552 bodySize
= sizeof_operation(&oper
->children
[3]);
2553 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2554 slang_info_log_print(A
->log
,
2555 "Note: 'for (%s ... )' body is too large/complex"
2562 return GL_FALSE
; /* degenerate case */
2564 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2565 slang_info_log_print(A
->log
,
2566 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2567 " many iterations to unroll",
2568 varName
, start
, varName
, end
, varName
);
2572 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2573 slang_info_log_print(A
->log
,
2574 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2575 " too much code to unroll",
2576 varName
, start
, varName
, end
, varName
);
2580 return GL_TRUE
; /* we can unroll the loop */
2585 _unroll_loop_inc(slang_assemble_ctx
* A
)
2592 _unroll_loop_dec(slang_assemble_ctx
* A
)
2599 * Unroll a for-loop.
2600 * First we determine the number of iterations to unroll.
2601 * Then for each iteration:
2602 * make a copy of the loop body
2603 * replace instances of the loop variable with the current iteration value
2604 * generate IR code for the body
2605 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2607 static slang_ir_node
*
2608 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2610 GLint start
, end
, iter
;
2611 slang_ir_node
*n
, *root
= NULL
;
2614 /* Set flag so code generator knows we're unrolling loops */
2615 _unroll_loop_inc( A
);
2617 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2618 /* for (int i=0; ... */
2619 slang_variable
*var
;
2621 varId
= oper
->children
[0].children
[0].a_id
;
2622 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2624 start
= (GLint
) var
->initializer
->literal
[0];
2628 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2629 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2632 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2634 for (iter
= start
; iter
< end
; iter
++) {
2635 slang_operation
*body
;
2637 /* make a copy of the loop body */
2638 body
= slang_operation_new(1);
2640 _unroll_loop_dec( A
);
2644 if (!slang_operation_copy(body
, &oper
->children
[3])) {
2645 _unroll_loop_dec( A
);
2649 /* in body, replace instances of 'varId' with literal 'iter' */
2651 slang_variable
*oldVar
;
2652 slang_operation
*newOper
;
2654 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2656 /* undeclared loop variable */
2657 slang_operation_delete(body
);
2658 _unroll_loop_dec( A
);
2662 newOper
= slang_operation_new(1);
2663 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2664 newOper
->literal_size
= 1;
2665 newOper
->literal
[0] = iter
;
2667 /* replace instances of the loop variable with newOper */
2668 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2671 /* do IR codegen for body */
2672 n
= _slang_gen_operation(A
, body
);
2674 _unroll_loop_dec( A
);
2678 root
= new_seq(root
, n
);
2680 slang_operation_delete(body
);
2683 _unroll_loop_dec( A
);
2690 * Generate IR for a for-loop. Unrolling will be done when possible.
2692 static slang_ir_node
*
2693 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2695 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2698 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2703 /* conventional for-loop code generation */
2706 * init code (child[0])
2708 * BREAK if !expr (child[1])
2709 * body code (child[3])
2711 * incr code (child[2]) // XXX continue here
2713 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2714 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2715 loop
= new_loop(NULL
);
2717 /* save old, push new loop */
2718 prevLoop
= A
->CurLoop
;
2721 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2722 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2723 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2724 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2726 loop
->Children
[0] = new_seq(breakIf
, body
);
2727 loop
->Children
[1] = incr
; /* tail code */
2729 /* pop loop, restore prev */
2730 A
->CurLoop
= prevLoop
;
2732 return new_seq(init
, loop
);
2737 static slang_ir_node
*
2738 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2740 slang_ir_node
*n
, *loopNode
;
2741 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2742 loopNode
= A
->CurLoop
;
2744 assert(loopNode
->Opcode
== IR_LOOP
);
2745 n
= new_node0(IR_CONT
);
2747 n
->Parent
= loopNode
;
2748 /* insert this node at head of linked list */
2749 n
->List
= loopNode
->List
;
2757 * Determine if the given operation is of a specific type.
2760 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2762 if (oper
->type
== type
)
2764 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2765 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2766 oper
->num_children
== 1)
2767 return is_operation_type(&oper
->children
[0], type
);
2774 * Generate IR tree for an if/then/else conditional using high-level
2775 * IR_IF instruction.
2777 static slang_ir_node
*
2778 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2781 * eval expr (child[0])
2788 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2789 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2790 GLboolean isConst
, constTrue
;
2792 /* type-check expression */
2793 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2794 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2798 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2799 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2803 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2807 return _slang_gen_operation(A
, &oper
->children
[1]);
2810 /* if (false) ... */
2811 return _slang_gen_operation(A
, &oper
->children
[2]);
2815 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2816 cond
= new_cond(cond
);
2818 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2819 && !haveElseClause
) {
2820 /* Special case: generate a conditional break */
2821 if (!A
->CurLoop
&& A
->UnrollLoop
) /* trying to unroll */
2823 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2826 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2827 && !haveElseClause
) {
2828 /* Special case: generate a conditional continue */
2829 if (!A
->CurLoop
&& A
->UnrollLoop
) /* trying to unroll */
2831 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2836 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2840 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2843 ifNode
= new_if(cond
, ifBody
, elseBody
);
2850 static slang_ir_node
*
2851 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2855 assert(oper
->type
== SLANG_OPER_NOT
);
2857 /* type-check expression */
2858 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2859 slang_info_log_error(A
->log
,
2860 "scalar/boolean expression expected for '!'");
2864 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2872 static slang_ir_node
*
2873 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2875 slang_ir_node
*n1
, *n2
;
2877 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2879 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2880 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2881 slang_info_log_error(A
->log
,
2882 "scalar/boolean expressions expected for '^^'");
2886 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2889 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2892 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2897 * Generate IR node for storage of a temporary of given size.
2899 static slang_ir_node
*
2900 _slang_gen_temporary(GLint size
)
2902 slang_ir_storage
*store
;
2903 slang_ir_node
*n
= NULL
;
2905 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2907 n
= new_node0(IR_VAR_DECL
);
2920 * Generate program constants for an array.
2921 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2922 * This will allocate and initialize three vector constants, storing
2923 * the array in constant memory, not temporaries like a non-const array.
2924 * This can also be used for uniform array initializers.
2925 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2928 make_constant_array(slang_assemble_ctx
*A
,
2929 slang_variable
*var
,
2930 slang_operation
*initializer
)
2932 struct gl_program
*prog
= A
->program
;
2933 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2934 const char *varName
= (char *) var
->a_name
;
2935 const GLuint numElements
= initializer
->num_children
;
2941 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2943 size
= var
->store
->Size
;
2945 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2946 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2947 assert(initializer
->type
== SLANG_OPER_CALL
);
2948 assert(initializer
->array_constructor
);
2950 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2952 /* convert constructor params into ordinary floats */
2953 for (i
= 0; i
< numElements
; i
++) {
2954 const slang_operation
*op
= &initializer
->children
[i
];
2955 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2956 /* unsupported type for this optimization */
2960 for (j
= 0; j
< op
->literal_size
; j
++) {
2961 values
[i
* 4 + j
] = op
->literal
[j
];
2963 for ( ; j
< 4; j
++) {
2964 values
[i
* 4 + j
] = 0.0f
;
2968 /* slightly different paths for constants vs. uniforms */
2969 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2970 var
->store
->File
= PROGRAM_UNIFORM
;
2971 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2972 size
, datatype
, values
);
2975 var
->store
->File
= PROGRAM_CONSTANT
;
2976 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2979 assert(var
->store
->Size
== size
);
2989 * Generate IR node for allocating/declaring a variable (either a local or
2991 * Generally, this involves allocating an slang_ir_storage instance for the
2992 * variable, choosing a register file (temporary, constant, etc).
2993 * For ordinary variables we do not yet allocate storage though. We do that
2994 * when we find the first actual use of the variable to avoid allocating temp
2995 * regs that will never get used.
2996 * At this time, uniforms are always allocated space in this function.
2998 * \param initializer Optional initializer expression for the variable.
3000 static slang_ir_node
*
3001 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3002 slang_operation
*initializer
)
3004 const char *varName
= (const char *) var
->a_name
;
3005 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3006 slang_ir_node
*varDecl
, *n
;
3007 slang_ir_storage
*store
;
3008 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3009 gl_register_file file
;
3011 /*assert(!var->declared);*/
3012 var
->declared
= GL_TRUE
;
3014 /* determine GPU register file for simple cases */
3015 if (is_sampler_type(&var
->type
)) {
3016 file
= PROGRAM_SAMPLER
;
3018 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3019 file
= PROGRAM_UNIFORM
;
3022 file
= PROGRAM_TEMPORARY
;
3025 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3027 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3031 arrayLen
= _slang_array_length(var
);
3032 totalSize
= _slang_array_size(size
, arrayLen
);
3034 /* Allocate IR node for the declaration */
3035 varDecl
= new_node0(IR_VAR_DECL
);
3039 /* Allocate slang_ir_storage for this variable if needed.
3040 * Note that we may not actually allocate a constant or temporary register
3044 GLint index
= -7; /* TBD / unknown */
3045 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3047 return NULL
; /* out of memory */
3050 /* set the IR node's Var and Store pointers */
3052 varDecl
->Store
= var
->store
;
3057 /* if there's an initializer, generate IR for the expression */
3059 slang_ir_node
*varRef
, *init
;
3061 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3062 /* if the variable is const, the initializer must be a const
3063 * expression as well.
3066 if (!_slang_is_constant_expr(initializer
)) {
3067 slang_info_log_error(A
->log
,
3068 "initializer for %s not constant", varName
);
3074 /* IR for the variable we're initializing */
3075 varRef
= new_var(A
, var
);
3077 slang_info_log_error(A
->log
, "out of memory");
3081 /* constant-folding, etc here */
3082 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3084 /* look for simple constant-valued variables and uniforms */
3085 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3086 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3088 if (initializer
->type
== SLANG_OPER_CALL
&&
3089 initializer
->array_constructor
) {
3090 /* array initializer */
3091 if (make_constant_array(A
, var
, initializer
))
3094 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3095 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3096 /* simple float/vector initializer */
3097 if (store
->File
== PROGRAM_UNIFORM
) {
3098 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3100 totalSize
, datatype
,
3101 initializer
->literal
);
3102 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3107 store
->File
= PROGRAM_CONSTANT
;
3108 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3110 initializer
->literal
,
3112 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3119 /* IR for initializer */
3120 init
= _slang_gen_operation(A
, initializer
);
3124 /* XXX remove this when type checking is added above */
3125 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3126 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3130 /* assign RHS to LHS */
3131 n
= new_node2(IR_COPY
, varRef
, init
);
3132 n
= new_seq(varDecl
, n
);
3135 /* no initializer */
3139 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3140 /* always need to allocate storage for uniforms at this point */
3141 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3142 totalSize
, datatype
, NULL
);
3143 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3147 printf("%s var %p %s store=%p index=%d size=%d\n",
3148 __FUNCTION__
, (void *) var
, (char *) varName
,
3149 (void *) store
, store
->Index
, store
->Size
);
3157 * Generate code for a selection expression: b ? x : y
3158 * XXX In some cases we could implement a selection expression
3159 * with an LRP instruction (use the boolean as the interpolant).
3160 * Otherwise, we use an IF/ELSE/ENDIF construct.
3162 static slang_ir_node
*
3163 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3165 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3166 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3167 slang_typeinfo type0
, type1
, type2
;
3168 int size
, isBool
, isEqual
;
3170 assert(oper
->type
== SLANG_OPER_SELECT
);
3171 assert(oper
->num_children
== 3);
3173 /* type of children[0] must be boolean */
3174 slang_typeinfo_construct(&type0
);
3175 typeof_operation(A
, &oper
->children
[0], &type0
);
3176 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3177 slang_typeinfo_destruct(&type0
);
3179 slang_info_log_error(A
->log
, "selector type is not boolean");
3183 slang_typeinfo_construct(&type1
);
3184 slang_typeinfo_construct(&type2
);
3185 typeof_operation(A
, &oper
->children
[1], &type1
);
3186 typeof_operation(A
, &oper
->children
[2], &type2
);
3187 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3188 slang_typeinfo_destruct(&type1
);
3189 slang_typeinfo_destruct(&type2
);
3191 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3195 /* size of x or y's type */
3196 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3200 tmpDecl
= _slang_gen_temporary(size
);
3202 /* the condition (child 0) */
3203 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3204 cond
= new_cond(cond
);
3206 /* if-true body (child 1) */
3207 tmpVar
= new_node0(IR_VAR
);
3208 tmpVar
->Store
= tmpDecl
->Store
;
3209 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3210 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3212 /* if-false body (child 2) */
3213 tmpVar
= new_node0(IR_VAR
);
3214 tmpVar
->Store
= tmpDecl
->Store
;
3215 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3216 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3218 ifNode
= new_if(cond
, trueNode
, falseNode
);
3221 tmpVar
= new_node0(IR_VAR
);
3222 tmpVar
->Store
= tmpDecl
->Store
;
3224 tree
= new_seq(ifNode
, tmpVar
);
3225 tree
= new_seq(tmpDecl
, tree
);
3227 /*_slang_print_ir_tree(tree, 10);*/
3233 * Generate code for &&.
3235 static slang_ir_node
*
3236 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3238 /* rewrite "a && b" as "a ? b : false" */
3239 slang_operation
*select
;
3242 select
= slang_operation_new(1);
3243 select
->type
= SLANG_OPER_SELECT
;
3244 select
->num_children
= 3;
3245 select
->children
= slang_operation_new(3);
3247 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3248 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3249 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3250 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3251 select
->children
[2].literal_size
= 1;
3253 n
= _slang_gen_select(A
, select
);
3259 * Generate code for ||.
3261 static slang_ir_node
*
3262 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3264 /* rewrite "a || b" as "a ? true : b" */
3265 slang_operation
*select
;
3268 select
= slang_operation_new(1);
3269 select
->type
= SLANG_OPER_SELECT
;
3270 select
->num_children
= 3;
3271 select
->children
= slang_operation_new(3);
3273 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3274 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3275 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3276 select
->children
[1].literal_size
= 1;
3277 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3279 n
= _slang_gen_select(A
, select
);
3285 * Generate IR tree for a return statement.
3287 static slang_ir_node
*
3288 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3290 const GLboolean haveReturnValue
3291 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3293 /* error checking */
3294 assert(A
->CurFunction
);
3295 if (haveReturnValue
&&
3296 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3297 slang_info_log_error(A
->log
, "illegal return expression");
3300 else if (!haveReturnValue
&&
3301 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3302 slang_info_log_error(A
->log
, "return statement requires an expression");
3306 if (!haveReturnValue
) {
3307 return new_return(A
->curFuncEndLabel
);
3315 * return; // goto __endOfFunction
3317 slang_operation
*assign
;
3318 slang_atom a_retVal
;
3321 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3327 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3329 /* trying to return a value in a void-valued function */
3335 assign
= slang_operation_new(1);
3336 assign
->type
= SLANG_OPER_ASSIGN
;
3337 assign
->num_children
= 2;
3338 assign
->children
= slang_operation_new(2);
3339 /* lhs (__retVal) */
3340 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3341 assign
->children
[0].a_id
= a_retVal
;
3342 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3344 /* XXX we might be able to avoid this copy someday */
3345 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3347 /* assemble the new code */
3348 n
= new_seq(_slang_gen_operation(A
, assign
),
3349 new_return(A
->curFuncEndLabel
));
3351 slang_operation_delete(assign
);
3359 * Determine if the given operation/expression is const-valued.
3362 _slang_is_constant_expr(const slang_operation
*oper
)
3364 slang_variable
*var
;
3367 switch (oper
->type
) {
3368 case SLANG_OPER_IDENTIFIER
:
3369 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3370 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3374 for (i
= 0; i
< oper
->num_children
; i
++) {
3375 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3385 * Check if an assignment of type t1 to t0 is legal.
3386 * XXX more cases needed.
3389 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3390 slang_operation
*op0
,
3391 slang_operation
*op1
)
3393 slang_typeinfo t0
, t1
;
3396 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3397 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3401 slang_typeinfo_construct(&t0
);
3402 typeof_operation(A
, op0
, &t0
);
3404 slang_typeinfo_construct(&t1
);
3405 typeof_operation(A
, op1
, &t1
);
3407 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3408 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3412 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3417 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3418 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3419 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3422 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3423 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3426 #if 0 /* not used just yet - causes problems elsewhere */
3427 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3428 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3432 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3433 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3436 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3437 t1
.spec
.type
== SLANG_SPEC_INT
)
3445 * Generate IR tree for a local variable declaration.
3446 * Basically do some error checking and call _slang_gen_var_decl().
3448 static slang_ir_node
*
3449 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3451 const char *varName
= (char *) oper
->a_id
;
3452 slang_variable
*var
;
3453 slang_ir_node
*varDecl
;
3454 slang_operation
*initializer
;
3456 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3457 assert(oper
->num_children
<= 1);
3459 /* lookup the variable by name */
3460 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3462 return NULL
; /* "shouldn't happen" */
3464 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3465 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3466 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3467 /* can't declare attribute/uniform vars inside functions */
3468 slang_info_log_error(A
->log
,
3469 "local variable '%s' cannot be an attribute/uniform/varying",
3476 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3481 /* check if the var has an initializer */
3482 if (oper
->num_children
> 0) {
3483 assert(oper
->num_children
== 1);
3484 initializer
= &oper
->children
[0];
3486 else if (var
->initializer
) {
3487 initializer
= var
->initializer
;
3494 /* check/compare var type and initializer type */
3495 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3496 slang_info_log_error(A
->log
, "incompatible types in assignment");
3501 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3502 slang_info_log_error(A
->log
,
3503 "const-qualified variable '%s' requires initializer",
3509 /* Generate IR node */
3510 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3519 * Generate IR tree for a reference to a variable (such as in an expression).
3520 * This is different from a variable declaration.
3522 static slang_ir_node
*
3523 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3525 /* If there's a variable associated with this oper (from inlining)
3526 * use it. Otherwise, use the oper's var id.
3528 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3529 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3532 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3535 assert(var
->declared
);
3536 n
= new_var(A
, var
);
3543 * Return the number of components actually named by the swizzle.
3544 * Recall that swizzles may have undefined/don't-care values.
3547 swizzle_size(GLuint swizzle
)
3550 for (i
= 0; i
< 4; i
++) {
3551 GLuint swz
= GET_SWZ(swizzle
, i
);
3552 size
+= (swz
>= 0 && swz
<= 3);
3558 static slang_ir_node
*
3559 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3561 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3565 n
->Store
= _slang_new_ir_storage_relative(0,
3566 swizzle_size(swizzle
),
3568 n
->Store
->Swizzle
= swizzle
;
3575 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3577 while (store
->Parent
)
3578 store
= store
->Parent
;
3580 if (!(store
->File
== PROGRAM_OUTPUT
||
3581 store
->File
== PROGRAM_TEMPORARY
||
3582 (store
->File
== PROGRAM_VARYING
&&
3583 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3593 * Walk up an IR storage path to compute the final swizzle.
3594 * This is used when we find an expression such as "foo.xz.yx".
3597 root_swizzle(const slang_ir_storage
*st
)
3599 GLuint swizzle
= st
->Swizzle
;
3600 while (st
->Parent
) {
3602 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3609 * Generate IR tree for an assignment (=).
3611 static slang_ir_node
*
3612 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3614 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3615 /* Check that var is writeable */
3617 = _slang_variable_locate(oper
->children
[0].locals
,
3618 oper
->children
[0].a_id
, GL_TRUE
);
3620 slang_info_log_error(A
->log
, "undefined variable '%s'",
3621 (char *) oper
->children
[0].a_id
);
3624 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3625 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3626 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3627 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3628 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3629 slang_info_log_error(A
->log
,
3630 "illegal assignment to read-only variable '%s'",
3631 (char *) oper
->children
[0].a_id
);
3636 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3637 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3638 /* Special case of: x = f(a, b)
3639 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3641 * XXX this could be even more effective if we could accomodate
3642 * cases such as "v.x = f();" - would help with typical vertex
3646 n
= _slang_gen_function_call_name(A
,
3647 (const char *) oper
->children
[1].a_id
,
3648 &oper
->children
[1], &oper
->children
[0]);
3652 slang_ir_node
*n
, *lhs
, *rhs
;
3654 /* lhs and rhs type checking */
3655 if (!_slang_assignment_compatible(A
,
3657 &oper
->children
[1])) {
3658 slang_info_log_error(A
->log
, "incompatible types in assignment");
3662 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3668 slang_info_log_error(A
->log
,
3669 "invalid left hand side for assignment");
3673 /* check that lhs is writable */
3674 if (!is_store_writable(A
, lhs
->Store
)) {
3675 slang_info_log_error(A
->log
,
3676 "illegal assignment to read-only l-value");
3680 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3682 /* convert lhs swizzle into writemask */
3683 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3684 GLuint writemask
, newSwizzle
= 0x0;
3685 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3686 /* Non-simple writemask, need to swizzle right hand side in
3687 * order to put components into the right place.
3689 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3691 n
= new_node2(IR_COPY
, lhs
, rhs
);
3702 * Generate IR tree for referencing a field in a struct (or basic vector type)
3704 static slang_ir_node
*
3705 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3709 /* type of struct */
3710 slang_typeinfo_construct(&ti
);
3711 typeof_operation(A
, &oper
->children
[0], &ti
);
3713 if (_slang_type_is_vector(ti
.spec
.type
)) {
3714 /* the field should be a swizzle */
3715 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3719 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3720 slang_info_log_error(A
->log
, "Bad swizzle");
3723 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3728 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3729 /* create new parent node with swizzle */
3731 n
= _slang_gen_swizzle(n
, swizzle
);
3734 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3735 || ti
.spec
.type
== SLANG_SPEC_INT
3736 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3737 const GLuint rows
= 1;
3741 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3742 slang_info_log_error(A
->log
, "Bad swizzle");
3744 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3748 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3749 /* create new parent node with swizzle */
3750 n
= _slang_gen_swizzle(n
, swizzle
);
3754 /* the field is a structure member (base.field) */
3755 /* oper->children[0] is the base */
3756 /* oper->a_id is the field name */
3757 slang_ir_node
*base
, *n
;
3758 slang_typeinfo field_ti
;
3759 GLint fieldSize
, fieldOffset
= -1;
3762 slang_typeinfo_construct(&field_ti
);
3763 typeof_operation(A
, oper
, &field_ti
);
3765 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3767 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3769 if (fieldSize
== 0 || fieldOffset
< 0) {
3770 const char *structName
;
3771 if (ti
.spec
._struct
)
3772 structName
= (char *) ti
.spec
._struct
->a_name
;
3774 structName
= "unknown";
3775 slang_info_log_error(A
->log
,
3776 "\"%s\" is not a member of struct \"%s\"",
3777 (char *) oper
->a_id
, structName
);
3780 assert(fieldSize
>= 0);
3782 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3784 /* error msg should have already been logged */
3788 n
= new_node1(IR_FIELD
, base
);
3792 n
->Field
= (char *) oper
->a_id
;
3794 /* Store the field's offset in storage->Index */
3795 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3805 * Gen code for array indexing.
3807 static slang_ir_node
*
3808 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3810 slang_typeinfo array_ti
;
3812 /* get array's type info */
3813 slang_typeinfo_construct(&array_ti
);
3814 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3816 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3817 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3818 /* translate the index into a swizzle/writemask: "v.x=p" */
3819 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3823 index
= (GLint
) oper
->children
[1].literal
[0];
3824 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3825 index
>= (GLint
) max
) {
3827 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3828 printf("type = %d\n", oper
->children
[1].type
);
3829 printf("index = %d, max = %d\n", index
, max
);
3830 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3831 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3838 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3840 /* use swizzle to access the element */
3841 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3845 n
= _slang_gen_swizzle(n
, swizzle
);
3851 /* conventional array */
3852 slang_typeinfo elem_ti
;
3853 slang_ir_node
*elem
, *array
, *index
;
3854 GLint elemSize
, arrayLen
;
3856 /* size of array element */
3857 slang_typeinfo_construct(&elem_ti
);
3858 typeof_operation(A
, oper
, &elem_ti
);
3859 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3861 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3862 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3864 arrayLen
= array_ti
.array_len
;
3866 slang_typeinfo_destruct(&array_ti
);
3867 slang_typeinfo_destruct(&elem_ti
);
3869 if (elemSize
<= 0) {
3870 /* unknown var or type */
3871 slang_info_log_error(A
->log
, "Undefined variable or type");
3875 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3876 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3877 if (array
&& index
) {
3879 GLint constIndex
= -1;
3880 if (index
->Opcode
== IR_FLOAT
) {
3881 constIndex
= (int) index
->Value
[0];
3882 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3883 slang_info_log_error(A
->log
,
3884 "Array index out of bounds (index=%d size=%d)",
3885 constIndex
, arrayLen
);
3886 _slang_free_ir_tree(array
);
3887 _slang_free_ir_tree(index
);
3892 if (!array
->Store
) {
3893 slang_info_log_error(A
->log
, "Invalid array");
3897 elem
= new_node2(IR_ELEMENT
, array
, index
);
3899 /* The storage info here will be updated during code emit */
3900 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3901 array
->Store
->Index
,
3903 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3907 _slang_free_ir_tree(array
);
3908 _slang_free_ir_tree(index
);
3915 static slang_ir_node
*
3916 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3917 slang_ir_opcode opcode
)
3919 slang_typeinfo t0
, t1
;
3922 slang_typeinfo_construct(&t0
);
3923 typeof_operation(A
, &oper
->children
[0], &t0
);
3925 slang_typeinfo_construct(&t1
);
3926 typeof_operation(A
, &oper
->children
[0], &t1
);
3928 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3929 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3930 slang_info_log_error(A
->log
, "Illegal array comparison");
3934 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3935 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3936 /* <, <=, >, >= can only be used with scalars */
3937 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3938 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3939 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3940 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3941 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3946 n
= new_node2(opcode
,
3947 _slang_gen_operation(A
, &oper
->children
[0]),
3948 _slang_gen_operation(A
, &oper
->children
[1]));
3950 /* result is a bool (size 1) */
3951 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3959 print_vars(slang_variable_scope
*s
)
3963 for (i
= 0; i
< s
->num_variables
; i
++) {
3965 (char*) s
->variables
[i
]->a_name
,
3966 s
->variables
[i
]->declared
);
3976 _slang_undeclare_vars(slang_variable_scope
*locals
)
3978 if (locals
->num_variables
> 0) {
3980 for (i
= 0; i
< locals
->num_variables
; i
++) {
3981 slang_variable
*v
= locals
->variables
[i
];
3982 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3983 v
->declared
= GL_FALSE
;
3991 * Generate IR tree for a slang_operation (AST node)
3993 static slang_ir_node
*
3994 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3996 switch (oper
->type
) {
3997 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4001 _slang_push_var_table(A
->vartable
);
4003 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4004 n
= _slang_gen_operation(A
, oper
);
4005 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4007 _slang_pop_var_table(A
->vartable
);
4009 /*_slang_undeclare_vars(oper->locals);*/
4010 /*print_vars(oper->locals);*/
4013 n
= new_node1(IR_SCOPE
, n
);
4018 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4019 /* list of operations */
4020 if (oper
->num_children
> 0)
4022 slang_ir_node
*n
, *tree
= NULL
;
4025 for (i
= 0; i
< oper
->num_children
; i
++) {
4026 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4028 _slang_free_ir_tree(tree
);
4029 return NULL
; /* error must have occured */
4031 tree
= new_seq(tree
, n
);
4037 return new_node0(IR_NOP
);
4040 case SLANG_OPER_EXPRESSION
:
4041 return _slang_gen_operation(A
, &oper
->children
[0]);
4043 case SLANG_OPER_FOR
:
4044 return _slang_gen_for(A
, oper
);
4046 return _slang_gen_do(A
, oper
);
4047 case SLANG_OPER_WHILE
:
4048 return _slang_gen_while(A
, oper
);
4049 case SLANG_OPER_BREAK
:
4052 slang_info_log_error(A
->log
, "'break' not in loop");
4055 return new_break(A
->CurLoop
);
4056 case SLANG_OPER_CONTINUE
:
4059 slang_info_log_error(A
->log
, "'continue' not in loop");
4062 return _slang_gen_continue(A
, oper
);
4063 case SLANG_OPER_DISCARD
:
4064 return new_node0(IR_KILL
);
4066 case SLANG_OPER_EQUAL
:
4067 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4068 case SLANG_OPER_NOTEQUAL
:
4069 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4070 case SLANG_OPER_GREATER
:
4071 return _slang_gen_compare(A
, oper
, IR_SGT
);
4072 case SLANG_OPER_LESS
:
4073 return _slang_gen_compare(A
, oper
, IR_SLT
);
4074 case SLANG_OPER_GREATEREQUAL
:
4075 return _slang_gen_compare(A
, oper
, IR_SGE
);
4076 case SLANG_OPER_LESSEQUAL
:
4077 return _slang_gen_compare(A
, oper
, IR_SLE
);
4078 case SLANG_OPER_ADD
:
4081 assert(oper
->num_children
== 2);
4082 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4085 case SLANG_OPER_SUBTRACT
:
4088 assert(oper
->num_children
== 2);
4089 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4092 case SLANG_OPER_MULTIPLY
:
4095 assert(oper
->num_children
== 2);
4096 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4099 case SLANG_OPER_DIVIDE
:
4102 assert(oper
->num_children
== 2);
4103 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4106 case SLANG_OPER_MINUS
:
4109 assert(oper
->num_children
== 1);
4110 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4113 case SLANG_OPER_PLUS
:
4114 /* +expr --> do nothing */
4115 return _slang_gen_operation(A
, &oper
->children
[0]);
4116 case SLANG_OPER_VARIABLE_DECL
:
4117 return _slang_gen_declaration(A
, oper
);
4118 case SLANG_OPER_ASSIGN
:
4119 return _slang_gen_assignment(A
, oper
);
4120 case SLANG_OPER_ADDASSIGN
:
4123 assert(oper
->num_children
== 2);
4124 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4127 case SLANG_OPER_SUBASSIGN
:
4130 assert(oper
->num_children
== 2);
4131 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4135 case SLANG_OPER_MULASSIGN
:
4138 assert(oper
->num_children
== 2);
4139 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4142 case SLANG_OPER_DIVASSIGN
:
4145 assert(oper
->num_children
== 2);
4146 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4149 case SLANG_OPER_LOGICALAND
:
4152 assert(oper
->num_children
== 2);
4153 n
= _slang_gen_logical_and(A
, oper
);
4156 case SLANG_OPER_LOGICALOR
:
4159 assert(oper
->num_children
== 2);
4160 n
= _slang_gen_logical_or(A
, oper
);
4163 case SLANG_OPER_LOGICALXOR
:
4164 return _slang_gen_xor(A
, oper
);
4165 case SLANG_OPER_NOT
:
4166 return _slang_gen_not(A
, oper
);
4167 case SLANG_OPER_SELECT
: /* b ? x : y */
4170 assert(oper
->num_children
== 3);
4171 n
= _slang_gen_select(A
, oper
);
4175 case SLANG_OPER_ASM
:
4176 return _slang_gen_asm(A
, oper
, NULL
);
4177 case SLANG_OPER_CALL
:
4178 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4180 case SLANG_OPER_METHOD
:
4181 return _slang_gen_method_call(A
, oper
);
4182 case SLANG_OPER_RETURN
:
4183 return _slang_gen_return(A
, oper
);
4184 case SLANG_OPER_LABEL
:
4185 return new_label(oper
->label
);
4186 case SLANG_OPER_IDENTIFIER
:
4187 return _slang_gen_variable(A
, oper
);
4189 return _slang_gen_if(A
, oper
);
4190 case SLANG_OPER_FIELD
:
4191 return _slang_gen_struct_field(A
, oper
);
4192 case SLANG_OPER_SUBSCRIPT
:
4193 return _slang_gen_array_element(A
, oper
);
4194 case SLANG_OPER_LITERAL_FLOAT
:
4196 case SLANG_OPER_LITERAL_INT
:
4198 case SLANG_OPER_LITERAL_BOOL
:
4199 return new_float_literal(oper
->literal
, oper
->literal_size
);
4201 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4204 assert(oper
->num_children
== 1);
4205 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4208 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4211 assert(oper
->num_children
== 1);
4212 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4215 case SLANG_OPER_PREINCREMENT
: /* ++var */
4218 assert(oper
->num_children
== 1);
4219 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4222 case SLANG_OPER_PREDECREMENT
: /* --var */
4225 assert(oper
->num_children
== 1);
4226 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4230 case SLANG_OPER_NON_INLINED_CALL
:
4231 case SLANG_OPER_SEQUENCE
:
4233 slang_ir_node
*tree
= NULL
;
4235 for (i
= 0; i
< oper
->num_children
; i
++) {
4236 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4237 tree
= new_seq(tree
, n
);
4239 tree
->Store
= n
->Store
;
4241 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4242 tree
= new_function_call(tree
, oper
->label
);
4247 case SLANG_OPER_NONE
:
4248 case SLANG_OPER_VOID
:
4249 /* returning NULL here would generate an error */
4250 return new_node0(IR_NOP
);
4253 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4255 return new_node0(IR_NOP
);
4263 * Check if the given type specifier is a rectangular texture sampler.
4266 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4268 while (spec
->_array
) {
4269 spec
= spec
->_array
;
4271 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4272 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4278 * Called by compiler when a global variable has been parsed/compiled.
4279 * Here we examine the variable's type to determine what kind of register
4280 * storage will be used.
4282 * A uniform such as "gl_Position" will become the register specification
4283 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4284 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4286 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4287 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4288 * actual texture unit (as specified by the user calling glUniform1i()).
4291 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4292 slang_unit_type type
)
4294 struct gl_program
*prog
= A
->program
;
4295 const char *varName
= (char *) var
->a_name
;
4296 GLboolean success
= GL_TRUE
;
4297 slang_ir_storage
*store
= NULL
;
4299 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4300 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4301 const GLint arrayLen
= _slang_array_length(var
);
4302 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4303 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4305 /* check for sampler2D arrays */
4306 if (texIndex
== -1 && var
->type
.specifier
._array
)
4307 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4309 if (texIndex
!= -1) {
4310 /* This is a texture sampler variable...
4311 * store->File = PROGRAM_SAMPLER
4312 * store->Index = sampler number (0..7, typically)
4313 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4315 if (var
->initializer
) {
4316 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4319 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4320 /* disallow rect samplers */
4321 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4322 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4326 (void) is_rect_sampler_spec
; /* silence warning */
4329 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4330 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4332 /* If we have a sampler array, then we need to allocate the
4333 * additional samplers to ensure we don't allocate them elsewhere.
4334 * We can't directly use _mesa_add_sampler() as that checks the
4335 * varName and gets a match, so we call _mesa_add_parameter()
4336 * directly and use the last sampler number from the call above.
4339 GLint a
= arrayLen
- 1;
4341 for (i
= 0; i
< a
; i
++) {
4342 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4343 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4344 varName
, 1, datatype
, &value
, NULL
, 0x0);
4348 if (dbg
) printf("SAMPLER ");
4350 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4351 /* Uniform variable */
4352 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4355 /* user-defined uniform */
4356 if (datatype
== GL_NONE
) {
4357 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
4358 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
4359 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
4360 /* temporary work-around */
4361 GLenum datatype
= GL_FLOAT
;
4362 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4363 totalSize
, datatype
, NULL
);
4364 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4365 totalSize
, swizzle
);
4368 GLint a
= arrayLen
- 1;
4370 for (i
= 0; i
< a
; i
++) {
4371 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
4372 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
4373 varName
, 1, datatype
, &value
, NULL
, 0x0);
4377 /* XXX what we need to do is unroll the struct into its
4378 * basic types, creating a uniform variable for each.
4386 * Should produce uniforms:
4387 * "f.a" (GL_FLOAT_VEC3)
4388 * "f.b" (GL_FLOAT_VEC4)
4391 if (var
->initializer
) {
4392 slang_info_log_error(A
->log
,
4393 "unsupported initializer for uniform '%s'", varName
);
4398 slang_info_log_error(A
->log
,
4399 "invalid datatype for uniform variable %s",
4405 /* non-struct uniform */
4406 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4412 /* pre-defined uniform, like gl_ModelviewMatrix */
4413 /* We know it's a uniform, but don't allocate storage unless
4416 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4417 totalSize
, swizzle
);
4419 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4421 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4422 /* varyings must be float, vec or mat */
4423 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4424 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4425 slang_info_log_error(A
->log
,
4426 "varying '%s' must be float/vector/matrix",
4431 if (var
->initializer
) {
4432 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4438 /* user-defined varying */
4444 if (var
->type
.centroid
== SLANG_CENTROID
)
4445 flags
|= PROG_PARAM_BIT_CENTROID
;
4446 if (var
->type
.variant
== SLANG_INVARIANT
)
4447 flags
|= PROG_PARAM_BIT_INVARIANT
;
4449 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4451 swizzle
= _slang_var_swizzle(size
, 0);
4452 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4453 totalSize
, swizzle
);
4456 /* pre-defined varying, like gl_Color or gl_TexCoord */
4457 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4458 /* fragment program input */
4460 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4463 assert(index
< FRAG_ATTRIB_MAX
);
4464 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4468 /* vertex program output */
4469 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4470 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4472 assert(index
< VERT_RESULT_MAX
);
4473 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4474 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4477 if (dbg
) printf("V/F ");
4479 if (dbg
) printf("VARYING ");
4481 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4484 /* attributes must be float, vec or mat */
4485 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4486 slang_info_log_error(A
->log
,
4487 "attribute '%s' must be float/vector/matrix",
4493 /* user-defined vertex attribute */
4494 const GLint attr
= -1; /* unknown */
4495 swizzle
= _slang_var_swizzle(size
, 0);
4496 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4497 size
, datatype
, attr
);
4499 index
= VERT_ATTRIB_GENERIC0
+ index
;
4502 /* pre-defined vertex attrib */
4503 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4506 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4507 if (dbg
) printf("ATTRIB ");
4509 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4510 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4511 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4513 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4514 if (dbg
) printf("INPUT ");
4516 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4517 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4518 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4519 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4522 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4523 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4524 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4525 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4527 if (dbg
) printf("OUTPUT ");
4529 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4530 /* pre-defined global constant, like gl_MaxLights */
4531 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4532 if (dbg
) printf("CONST ");
4535 /* ordinary variable (may be const) */
4538 /* IR node to declare the variable */
4539 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4541 /* emit GPU instructions */
4542 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
4544 _slang_free_ir_tree(n
);
4547 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4548 store
? store
->Index
: -2);
4551 var
->store
= store
; /* save var's storage info */
4553 var
->declared
= GL_TRUE
;
4560 * Produce an IR tree from a function AST (fun->body).
4561 * Then call the code emitter to convert the IR tree into gl_program
4565 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4568 GLboolean success
= GL_TRUE
;
4570 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4571 /* we only really generate code for main, all other functions get
4572 * inlined or codegen'd upon an actual call.
4575 /* do some basic error checking though */
4576 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4577 /* check that non-void functions actually return something */
4579 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4581 slang_info_log_error(A
->log
,
4582 "function \"%s\" has no return statement",
4583 (char *) fun
->header
.a_name
);
4585 "function \"%s\" has no return statement\n",
4586 (char *) fun
->header
.a_name
);
4591 return GL_TRUE
; /* not an error */
4595 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4596 slang_print_function(fun
, 1);
4599 /* should have been allocated earlier: */
4600 assert(A
->program
->Parameters
);
4601 assert(A
->program
->Varying
);
4602 assert(A
->vartable
);
4604 A
->CurFunction
= fun
;
4606 /* fold constant expressions, etc. */
4607 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4610 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4611 slang_print_function(fun
, 1);
4614 /* Create an end-of-function label */
4615 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4617 /* push new vartable scope */
4618 _slang_push_var_table(A
->vartable
);
4620 /* Generate IR tree for the function body code */
4621 n
= _slang_gen_operation(A
, fun
->body
);
4623 n
= new_node1(IR_SCOPE
, n
);
4625 /* pop vartable, restore previous */
4626 _slang_pop_var_table(A
->vartable
);
4629 /* XXX record error */
4633 /* append an end-of-function-label to IR tree */
4634 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4636 /*_slang_label_delete(A->curFuncEndLabel);*/
4637 A
->curFuncEndLabel
= NULL
;
4640 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4641 slang_print_function(fun
, 1);
4644 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4645 _slang_print_ir_tree(n
, 0);
4648 printf("************* End codegen function ************\n\n");
4651 /* Emit program instructions */
4652 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
4653 _slang_free_ir_tree(n
);
4655 /* free codegen context */
4657 _mesa_free(A->codegen);