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);
301 * Establish the binding between a slang_ir_node and a slang_variable.
302 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
303 * The IR node must be a IR_VAR or IR_VAR_DECL node.
304 * \param n the IR node
305 * \param var the variable to associate with the IR node
308 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
312 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
313 assert(!n
->Var
|| n
->Var
== var
);
318 /* need to setup storage */
319 if (n
->Var
&& n
->Var
->store
) {
320 /* node storage info = var storage info */
321 n
->Store
= n
->Var
->store
;
324 /* alloc new storage info */
325 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
327 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
329 (void*) n
->Store
, n
->Store
->Size
);
332 n
->Var
->store
= n
->Store
;
333 assert(n
->Var
->store
);
340 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
341 * or -1 if the type is not a sampler.
344 sampler_to_texture_index(const slang_type_specifier_type type
)
347 case SLANG_SPEC_SAMPLER1D
:
348 return TEXTURE_1D_INDEX
;
349 case SLANG_SPEC_SAMPLER2D
:
350 return TEXTURE_2D_INDEX
;
351 case SLANG_SPEC_SAMPLER3D
:
352 return TEXTURE_3D_INDEX
;
353 case SLANG_SPEC_SAMPLERCUBE
:
354 return TEXTURE_CUBE_INDEX
;
355 case SLANG_SPEC_SAMPLER1DSHADOW
:
356 return TEXTURE_1D_INDEX
; /* XXX fix */
357 case SLANG_SPEC_SAMPLER2DSHADOW
:
358 return TEXTURE_2D_INDEX
; /* XXX fix */
359 case SLANG_SPEC_SAMPLER2DRECT
:
360 return TEXTURE_RECT_INDEX
;
361 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
362 return TEXTURE_RECT_INDEX
; /* XXX fix */
369 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
372 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
373 * a vertex or fragment program input variable. Return -1 if the input
375 * XXX return size too
378 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
385 static const struct input_info vertInputs
[] = {
386 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
387 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
388 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
389 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
390 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
391 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
392 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
393 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
394 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
395 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
396 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
397 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
398 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
399 { NULL
, 0, SWIZZLE_NOOP
}
401 static const struct input_info fragInputs
[] = {
402 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
403 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
404 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
405 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
406 /* note: we're packing several quantities into the fogcoord vector */
407 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
408 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
409 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
410 { NULL
, 0, SWIZZLE_NOOP
}
413 const struct input_info
*inputs
414 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
416 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
418 for (i
= 0; inputs
[i
].Name
; i
++) {
419 if (strcmp(inputs
[i
].Name
, name
) == 0) {
421 *swizzleOut
= inputs
[i
].Swizzle
;
422 return inputs
[i
].Attrib
;
430 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
431 * a vertex or fragment program output variable. Return -1 for an invalid
435 _slang_output_index(const char *name
, GLenum target
)
441 static const struct output_info vertOutputs
[] = {
442 { "gl_Position", VERT_RESULT_HPOS
},
443 { "gl_FrontColor", VERT_RESULT_COL0
},
444 { "gl_BackColor", VERT_RESULT_BFC0
},
445 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
446 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
447 { "gl_TexCoord", VERT_RESULT_TEX0
},
448 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
449 { "gl_PointSize", VERT_RESULT_PSIZ
},
452 static const struct output_info fragOutputs
[] = {
453 { "gl_FragColor", FRAG_RESULT_COLR
},
454 { "gl_FragDepth", FRAG_RESULT_DEPR
},
455 { "gl_FragData", FRAG_RESULT_DATA0
},
459 const struct output_info
*outputs
460 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
462 for (i
= 0; outputs
[i
].Name
; i
++) {
463 if (strcmp(outputs
[i
].Name
, name
) == 0) {
465 return outputs
[i
].Attrib
;
473 /**********************************************************************/
477 * Map "_asm foo" to IR_FOO, etc.
482 slang_ir_opcode Opcode
;
483 GLuint HaveRetValue
, NumParams
;
487 static slang_asm_info AsmInfo
[] = {
489 { "vec4_add", IR_ADD
, 1, 2 },
490 { "vec4_subtract", IR_SUB
, 1, 2 },
491 { "vec4_multiply", IR_MUL
, 1, 2 },
492 { "vec4_dot", IR_DOT4
, 1, 2 },
493 { "vec3_dot", IR_DOT3
, 1, 2 },
494 { "vec2_dot", IR_DOT2
, 1, 2 },
495 { "vec3_nrm", IR_NRM3
, 1, 1 },
496 { "vec4_nrm", IR_NRM4
, 1, 1 },
497 { "vec3_cross", IR_CROSS
, 1, 2 },
498 { "vec4_lrp", IR_LRP
, 1, 3 },
499 { "vec4_min", IR_MIN
, 1, 2 },
500 { "vec4_max", IR_MAX
, 1, 2 },
501 { "vec4_clamp", IR_CLAMP
, 1, 3 },
502 { "vec4_seq", IR_SEQUAL
, 1, 2 },
503 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
504 { "vec4_sge", IR_SGE
, 1, 2 },
505 { "vec4_sgt", IR_SGT
, 1, 2 },
506 { "vec4_sle", IR_SLE
, 1, 2 },
507 { "vec4_slt", IR_SLT
, 1, 2 },
509 { "vec4_move", IR_MOVE
, 1, 1 },
510 { "vec4_floor", IR_FLOOR
, 1, 1 },
511 { "vec4_frac", IR_FRAC
, 1, 1 },
512 { "vec4_abs", IR_ABS
, 1, 1 },
513 { "vec4_negate", IR_NEG
, 1, 1 },
514 { "vec4_ddx", IR_DDX
, 1, 1 },
515 { "vec4_ddy", IR_DDY
, 1, 1 },
516 /* float binary op */
517 { "float_power", IR_POW
, 1, 2 },
518 /* texture / sampler */
519 { "vec4_tex_1d", IR_TEX
, 1, 2 },
520 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
521 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
522 { "vec4_tex_2d", IR_TEX
, 1, 2 },
523 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
524 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
525 { "vec4_tex_3d", IR_TEX
, 1, 2 },
526 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
527 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
528 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
529 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
530 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
532 /* texture / sampler but with shadow comparison */
533 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
534 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
535 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
536 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
537 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
538 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
539 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
540 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
543 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
544 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
545 { "float_exp", IR_EXP
, 1, 1 },
546 { "float_exp2", IR_EXP2
, 1, 1 },
547 { "float_log2", IR_LOG2
, 1, 1 },
548 { "float_rsq", IR_RSQ
, 1, 1 },
549 { "float_rcp", IR_RCP
, 1, 1 },
550 { "float_sine", IR_SIN
, 1, 1 },
551 { "float_cosine", IR_COS
, 1, 1 },
552 { "float_noise1", IR_NOISE1
, 1, 1},
553 { "float_noise2", IR_NOISE2
, 1, 1},
554 { "float_noise3", IR_NOISE3
, 1, 1},
555 { "float_noise4", IR_NOISE4
, 1, 1},
557 { NULL
, IR_NOP
, 0, 0 }
561 static slang_ir_node
*
562 new_node3(slang_ir_opcode op
,
563 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
565 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
571 n
->InstLocation
= -1;
576 static slang_ir_node
*
577 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
579 return new_node3(op
, c0
, c1
, NULL
);
582 static slang_ir_node
*
583 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
585 return new_node3(op
, c0
, NULL
, NULL
);
588 static slang_ir_node
*
589 new_node0(slang_ir_opcode op
)
591 return new_node3(op
, NULL
, NULL
, NULL
);
596 * Create sequence of two nodes.
598 static slang_ir_node
*
599 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
605 return new_node2(IR_SEQ
, left
, right
);
608 static slang_ir_node
*
609 new_label(slang_label
*label
)
611 slang_ir_node
*n
= new_node0(IR_LABEL
);
618 static slang_ir_node
*
619 new_float_literal(const float v
[4], GLuint size
)
621 slang_ir_node
*n
= new_node0(IR_FLOAT
);
623 COPY_4V(n
->Value
, v
);
624 /* allocate a storage object, but compute actual location (Index) later */
625 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
630 static slang_ir_node
*
631 new_not(slang_ir_node
*n
)
633 return new_node1(IR_NOT
, n
);
638 * Non-inlined function call.
640 static slang_ir_node
*
641 new_function_call(slang_ir_node
*code
, slang_label
*name
)
643 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
652 * Unconditional jump.
654 static slang_ir_node
*
655 new_return(slang_label
*dest
)
657 slang_ir_node
*n
= new_node0(IR_RETURN
);
665 static slang_ir_node
*
666 new_loop(slang_ir_node
*body
)
668 return new_node1(IR_LOOP
, body
);
672 static slang_ir_node
*
673 new_break(slang_ir_node
*loopNode
)
675 slang_ir_node
*n
= new_node0(IR_BREAK
);
677 assert(loopNode
->Opcode
== IR_LOOP
);
679 /* insert this node at head of linked list */
680 n
->List
= loopNode
->List
;
688 * Make new IR_BREAK_IF_TRUE.
690 static slang_ir_node
*
691 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
695 assert(loopNode
->Opcode
== IR_LOOP
);
696 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
698 /* insert this node at head of linked list */
699 n
->List
= loopNode
->List
;
707 * Make new IR_CONT_IF_TRUE node.
709 static slang_ir_node
*
710 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
714 assert(loopNode
->Opcode
== IR_LOOP
);
715 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
717 /* insert this node at head of linked list */
718 n
->List
= loopNode
->List
;
725 static slang_ir_node
*
726 new_cond(slang_ir_node
*n
)
728 slang_ir_node
*c
= new_node1(IR_COND
, n
);
733 static slang_ir_node
*
734 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
736 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
741 * New IR_VAR node - a reference to a previously declared variable.
743 static slang_ir_node
*
744 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
746 slang_ir_node
*n
= new_node0(IR_VAR
);
748 _slang_attach_storage(n
, var
);
755 * Check if the given function is really just a wrapper for a
756 * basic assembly instruction.
759 slang_is_asm_function(const slang_function
*fun
)
761 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
762 fun
->body
->num_children
== 1 &&
763 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
771 _slang_is_noop(const slang_operation
*oper
)
774 oper
->type
== SLANG_OPER_VOID
||
775 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
783 * Recursively search tree for a node of the given type.
785 static slang_operation
*
786 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
789 if (oper
->type
== type
)
791 for (i
= 0; i
< oper
->num_children
; i
++) {
792 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
801 * Count the number of operations of the given time rooted at 'oper'.
804 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
807 if (oper
->type
== type
) {
810 for (i
= 0; i
< oper
->num_children
; i
++) {
811 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
818 * Check if the 'return' statement found under 'oper' is a "tail return"
819 * that can be no-op'd. For example:
824 * return; // this is a no-op
827 * This is used when determining if a function can be inlined. If the
828 * 'return' is not the last statement, we can't inline the function since
829 * we still need the semantic behaviour of the 'return' but we don't want
830 * to accidentally return from the _calling_ function. We'd need to use an
831 * unconditional branch, but we don't have such a GPU instruction (not
835 _slang_is_tail_return(const slang_operation
*oper
)
837 GLuint k
= oper
->num_children
;
840 const slang_operation
*last
= &oper
->children
[k
- 1];
841 if (last
->type
== SLANG_OPER_RETURN
)
843 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
844 last
->type
== SLANG_OPER_LABEL
)
845 k
--; /* try prev child */
846 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
847 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
848 /* try sub-children */
849 return _slang_is_tail_return(last
);
859 slang_resolve_variable(slang_operation
*oper
)
861 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
862 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
868 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
871 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
872 GLuint substCount
, slang_variable
**substOld
,
873 slang_operation
**substNew
, GLboolean isLHS
)
875 switch (oper
->type
) {
876 case SLANG_OPER_VARIABLE_DECL
:
878 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
879 oper
->a_id
, GL_TRUE
);
881 if (v
->initializer
&& oper
->num_children
== 0) {
882 /* set child of oper to copy of initializer */
883 oper
->num_children
= 1;
884 oper
->children
= slang_operation_new(1);
885 slang_operation_copy(&oper
->children
[0], v
->initializer
);
887 if (oper
->num_children
== 1) {
888 /* the initializer */
889 slang_substitute(A
, &oper
->children
[0], substCount
,
890 substOld
, substNew
, GL_FALSE
);
894 case SLANG_OPER_IDENTIFIER
:
895 assert(oper
->num_children
== 0);
896 if (1/**!isLHS XXX FIX */) {
897 slang_atom id
= oper
->a_id
;
900 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
902 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
906 /* look for a substitution */
907 for (i
= 0; i
< substCount
; i
++) {
908 if (v
== substOld
[i
]) {
909 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
910 #if 0 /* DEBUG only */
911 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
912 assert(substNew
[i
]->var
);
913 assert(substNew
[i
]->var
->a_name
);
914 printf("Substitute %s with %s in id node %p\n",
915 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
919 printf("Substitute %s with %f in id node %p\n",
920 (char*)v
->a_name
, substNew
[i
]->literal
[0],
924 slang_operation_copy(oper
, substNew
[i
]);
931 case SLANG_OPER_RETURN
:
932 /* do return replacement here too */
933 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
934 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
940 * then do substitutions on the assignment.
942 slang_operation
*blockOper
, *assignOper
, *returnOper
;
944 /* check if function actually has a return type */
945 assert(A
->CurFunction
);
946 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
947 slang_info_log_error(A
->log
, "illegal return expression");
951 blockOper
= slang_operation_new(1);
952 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
953 blockOper
->num_children
= 2;
954 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
955 blockOper
->children
= slang_operation_new(2);
956 assignOper
= blockOper
->children
+ 0;
957 returnOper
= blockOper
->children
+ 1;
959 assignOper
->type
= SLANG_OPER_ASSIGN
;
960 assignOper
->num_children
= 2;
961 assignOper
->locals
->outer_scope
= blockOper
->locals
;
962 assignOper
->children
= slang_operation_new(2);
963 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
964 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
965 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
967 slang_operation_copy(&assignOper
->children
[1],
970 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
971 assert(returnOper
->num_children
== 0);
973 /* do substitutions on the "__retVal = expr" sub-tree */
974 slang_substitute(A
, assignOper
,
975 substCount
, substOld
, substNew
, GL_FALSE
);
977 /* install new code */
978 slang_operation_copy(oper
, blockOper
);
979 slang_operation_destruct(blockOper
);
982 /* check if return value was expected */
983 assert(A
->CurFunction
);
984 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
985 slang_info_log_error(A
->log
, "return statement requires an expression");
991 case SLANG_OPER_ASSIGN
:
992 case SLANG_OPER_SUBSCRIPT
:
994 * child[0] can't have substitutions but child[1] can.
996 slang_substitute(A
, &oper
->children
[0],
997 substCount
, substOld
, substNew
, GL_TRUE
);
998 slang_substitute(A
, &oper
->children
[1],
999 substCount
, substOld
, substNew
, GL_FALSE
);
1001 case SLANG_OPER_FIELD
:
1002 /* XXX NEW - test */
1003 slang_substitute(A
, &oper
->children
[0],
1004 substCount
, substOld
, substNew
, GL_TRUE
);
1009 for (i
= 0; i
< oper
->num_children
; i
++)
1010 slang_substitute(A
, &oper
->children
[i
],
1011 substCount
, substOld
, substNew
, GL_FALSE
);
1018 * Produce inline code for a call to an assembly instruction.
1019 * This is typically used to compile a call to a built-in function like this:
1021 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1023 * __asm vec4_lrp __retVal, a, y, x;
1028 * r = mix(p1, p2, p3);
1038 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1040 static slang_operation
*
1041 slang_inline_asm_function(slang_assemble_ctx
*A
,
1042 slang_function
*fun
, slang_operation
*oper
)
1044 const GLuint numArgs
= oper
->num_children
;
1046 slang_operation
*inlined
;
1047 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1048 slang_variable
**substOld
;
1049 slang_operation
**substNew
;
1051 ASSERT(slang_is_asm_function(fun
));
1052 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1055 printf("Inline %s as %s\n",
1056 (char*) fun->header.a_name,
1057 (char*) fun->body->children[0].a_id);
1061 * We'll substitute formal params with actual args in the asm call.
1063 substOld
= (slang_variable
**)
1064 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1065 substNew
= (slang_operation
**)
1066 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1067 for (i
= 0; i
< numArgs
; i
++) {
1068 substOld
[i
] = fun
->parameters
->variables
[i
];
1069 substNew
[i
] = oper
->children
+ i
;
1072 /* make a copy of the code to inline */
1073 inlined
= slang_operation_new(1);
1074 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1076 /* get rid of the __retVal child */
1077 inlined
->num_children
--;
1078 for (i
= 0; i
< inlined
->num_children
; i
++) {
1079 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1083 /* now do formal->actual substitutions */
1084 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1086 _slang_free(substOld
);
1087 _slang_free(substNew
);
1090 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1091 (char *) fun
->header
.a_name
);
1092 slang_print_tree(inlined
, 3);
1093 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1101 * Inline the given function call operation.
1102 * Return a new slang_operation that corresponds to the inlined code.
1104 static slang_operation
*
1105 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1106 slang_operation
*oper
, slang_operation
*returnOper
)
1113 ParamMode
*paramMode
;
1114 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1115 const GLuint numArgs
= oper
->num_children
;
1116 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1117 slang_operation
*args
= oper
->children
;
1118 slang_operation
*inlined
, *top
;
1119 slang_variable
**substOld
;
1120 slang_operation
**substNew
;
1121 GLuint substCount
, numCopyIn
, i
;
1122 slang_function
*prevFunction
;
1123 slang_variable_scope
*newScope
= NULL
;
1126 prevFunction
= A
->CurFunction
;
1127 A
->CurFunction
= fun
;
1129 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1130 assert(fun
->param_count
== totalArgs
);
1132 /* allocate temporary arrays */
1133 paramMode
= (ParamMode
*)
1134 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1135 substOld
= (slang_variable
**)
1136 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1137 substNew
= (slang_operation
**)
1138 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1141 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1142 (char *) fun
->header
.a_name
,
1143 fun
->parameters
->num_variables
, numArgs
);
1146 if (haveRetValue
&& !returnOper
) {
1147 /* Create 3-child comma sequence for inlined code:
1148 * child[0]: declare __resultTmp
1149 * child[1]: inlined function body
1150 * child[2]: __resultTmp
1152 slang_operation
*commaSeq
;
1153 slang_operation
*declOper
= NULL
;
1154 slang_variable
*resultVar
;
1156 commaSeq
= slang_operation_new(1);
1157 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1158 assert(commaSeq
->locals
);
1159 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1160 commaSeq
->num_children
= 3;
1161 commaSeq
->children
= slang_operation_new(3);
1162 /* allocate the return var */
1163 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1165 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1166 (void*)commaSeq->locals, (char *) fun->header.a_name);
1169 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1170 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1171 resultVar
->isTemp
= GL_TRUE
;
1173 /* child[0] = __resultTmp declaration */
1174 declOper
= &commaSeq
->children
[0];
1175 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1176 declOper
->a_id
= resultVar
->a_name
;
1177 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1179 /* child[1] = function body */
1180 inlined
= &commaSeq
->children
[1];
1181 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1183 /* child[2] = __resultTmp reference */
1184 returnOper
= &commaSeq
->children
[2];
1185 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1186 returnOper
->a_id
= resultVar
->a_name
;
1187 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1192 top
= inlined
= slang_operation_new(1);
1193 /* XXXX this may be inappropriate!!!! */
1194 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1198 assert(inlined
->locals
);
1200 /* Examine the parameters, look for inout/out params, look for possible
1201 * substitutions, etc:
1202 * param type behaviour
1203 * in copy actual to local
1204 * const in substitute param with actual
1208 for (i
= 0; i
< totalArgs
; i
++) {
1209 slang_variable
*p
= fun
->parameters
->variables
[i
];
1211 printf("Param %d: %s %s \n", i,
1212 slang_type_qual_string(p->type.qualifier),
1213 (char *) p->a_name);
1215 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1216 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1217 /* an output param */
1218 slang_operation
*arg
;
1223 paramMode
[i
] = SUBST
;
1225 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1226 slang_resolve_variable(arg
);
1228 /* replace parameter 'p' with argument 'arg' */
1229 substOld
[substCount
] = p
;
1230 substNew
[substCount
] = arg
; /* will get copied */
1233 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1234 /* a constant input param */
1235 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1236 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1237 /* replace all occurances of this parameter variable with the
1238 * actual argument variable or a literal.
1240 paramMode
[i
] = SUBST
;
1241 slang_resolve_variable(&args
[i
]);
1242 substOld
[substCount
] = p
;
1243 substNew
[substCount
] = &args
[i
]; /* will get copied */
1247 paramMode
[i
] = COPY_IN
;
1251 paramMode
[i
] = COPY_IN
;
1253 assert(paramMode
[i
]);
1256 /* actual code inlining: */
1257 slang_operation_copy(inlined
, fun
->body
);
1259 /*** XXX review this */
1260 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1261 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1262 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1265 printf("======================= orig body code ======================\n");
1266 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1267 slang_print_tree(fun
->body
, 8);
1268 printf("======================= copied code =========================\n");
1269 slang_print_tree(inlined
, 8);
1272 /* do parameter substitution in inlined code: */
1273 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1276 printf("======================= subst code ==========================\n");
1277 slang_print_tree(inlined
, 8);
1278 printf("=============================================================\n");
1281 /* New prolog statements: (inserted before the inlined code)
1282 * Copy the 'in' arguments.
1285 for (i
= 0; i
< numArgs
; i
++) {
1286 if (paramMode
[i
] == COPY_IN
) {
1287 slang_variable
*p
= fun
->parameters
->variables
[i
];
1288 /* declare parameter 'p' */
1289 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1293 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1294 assert(decl
->locals
);
1295 decl
->locals
->outer_scope
= inlined
->locals
;
1296 decl
->a_id
= p
->a_name
;
1297 decl
->num_children
= 1;
1298 decl
->children
= slang_operation_new(1);
1300 /* child[0] is the var's initializer */
1301 slang_operation_copy(&decl
->children
[0], args
+ i
);
1303 /* add parameter 'p' to the local variable scope here */
1305 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1306 pCopy
->type
= p
->type
;
1307 pCopy
->a_name
= p
->a_name
;
1308 pCopy
->array_len
= p
->array_len
;
1311 newScope
= inlined
->locals
;
1316 /* Now add copies of the function's local vars to the new variable scope */
1317 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1318 slang_variable
*p
= fun
->parameters
->variables
[i
];
1319 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1320 pCopy
->type
= p
->type
;
1321 pCopy
->a_name
= p
->a_name
;
1322 pCopy
->array_len
= p
->array_len
;
1326 /* New epilog statements:
1327 * 1. Create end of function label to jump to from return statements.
1328 * 2. Copy the 'out' parameter vars
1331 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1333 inlined
->num_children
);
1334 lab
->type
= SLANG_OPER_LABEL
;
1335 lab
->label
= A
->curFuncEndLabel
;
1338 for (i
= 0; i
< totalArgs
; i
++) {
1339 if (paramMode
[i
] == COPY_OUT
) {
1340 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1341 /* actualCallVar = outParam */
1342 /*if (i > 0 || !haveRetValue)*/
1343 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1345 inlined
->num_children
);
1346 ass
->type
= SLANG_OPER_ASSIGN
;
1347 ass
->num_children
= 2;
1348 ass
->locals
->outer_scope
= inlined
->locals
;
1349 ass
->children
= slang_operation_new(2);
1350 ass
->children
[0] = args
[i
]; /*XXX copy */
1351 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1352 ass
->children
[1].a_id
= p
->a_name
;
1353 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1357 _slang_free(paramMode
);
1358 _slang_free(substOld
);
1359 _slang_free(substNew
);
1361 /* Update scoping to use the new local vars instead of the
1362 * original function's vars. This is especially important
1363 * for nested inlining.
1366 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1369 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1370 (char *) fun
->header
.a_name
,
1371 fun
->parameters
->num_variables
, numArgs
);
1372 slang_print_tree(top
, 0);
1376 A
->CurFunction
= prevFunction
;
1382 static slang_ir_node
*
1383 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1384 slang_operation
*oper
, slang_operation
*dest
)
1387 slang_operation
*inlined
;
1388 slang_label
*prevFuncEndLabel
;
1391 prevFuncEndLabel
= A
->curFuncEndLabel
;
1392 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1393 A
->curFuncEndLabel
= _slang_label_new(name
);
1394 assert(A
->curFuncEndLabel
);
1396 if (slang_is_asm_function(fun
) && !dest
) {
1397 /* assemble assembly function - tree style */
1398 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1401 /* non-assembly function */
1402 /* We always generate an "inline-able" block of code here.
1404 * 1. insert the inline code
1405 * 2. Generate a call to the "inline" code as a subroutine
1409 slang_operation
*ret
= NULL
;
1411 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1415 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1417 /* check if this is a "tail" return */
1418 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1419 _slang_is_tail_return(inlined
)) {
1420 /* The only RETURN is the last stmt in the function, no-op it
1421 * and inline the function body.
1423 ret
->type
= SLANG_OPER_NONE
;
1426 slang_operation
*callOper
;
1427 /* The function we're calling has one or more 'return' statements.
1428 * So, we can't truly inline this function because we need to
1429 * implement 'return' with RET (and CAL).
1430 * Nevertheless, we performed "inlining" to make a new instance
1431 * of the function body to deal with static register allocation.
1433 * XXX check if there's one 'return' and if it's the very last
1434 * statement in the function - we can optimize that case.
1436 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1437 inlined
->type
== SLANG_OPER_SEQUENCE
);
1439 if (_slang_function_has_return_value(fun
) && !dest
) {
1440 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1441 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1442 callOper
= &inlined
->children
[1];
1447 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1448 callOper
->fun
= fun
;
1449 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1457 /* Replace the function call with the inlined block (or new CALL stmt) */
1458 slang_operation_destruct(oper
);
1460 _slang_free(inlined
);
1463 assert(inlined
->locals
);
1464 printf("*** Inlined code for call to %s:\n",
1465 (char*) fun
->header
.a_name
);
1466 slang_print_tree(oper
, 10);
1470 n
= _slang_gen_operation(A
, oper
);
1472 /*_slang_label_delete(A->curFuncEndLabel);*/
1473 A
->curFuncEndLabel
= prevFuncEndLabel
;
1479 static slang_asm_info
*
1480 slang_find_asm_info(const char *name
)
1483 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1484 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1493 * Some write-masked assignments are simple, but others are hard.
1496 * v.xy = vec2(a, b);
1499 * v.zy = vec2(a, b);
1500 * this gets transformed/swizzled into:
1501 * v.zy = vec2(a, b).*yx* (* = don't care)
1502 * This function helps to determine simple vs. non-simple.
1505 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1507 switch (writemask
) {
1509 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1511 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1513 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1515 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1517 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1518 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1520 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1521 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1522 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1523 case WRITEMASK_XYZW
:
1524 return swizzle
== SWIZZLE_NOOP
;
1532 * Convert the given swizzle into a writemask. In some cases this
1533 * is trivial, in other cases, we'll need to also swizzle the right
1534 * hand side to put components in the right places.
1535 * See comment above for more info.
1536 * XXX this function could be simplified and should probably be renamed.
1537 * \param swizzle the incoming swizzle
1538 * \param writemaskOut returns the writemask
1539 * \param swizzleOut swizzle to apply to the right-hand-side
1540 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1543 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1544 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1546 GLuint mask
= 0x0, newSwizzle
[4];
1549 /* make new dst writemask, compute size */
1550 for (i
= 0; i
< 4; i
++) {
1551 const GLuint swz
= GET_SWZ(swizzle
, i
);
1552 if (swz
== SWIZZLE_NIL
) {
1556 assert(swz
>= 0 && swz
<= 3);
1558 if (swizzle
!= SWIZZLE_XXXX
&&
1559 swizzle
!= SWIZZLE_YYYY
&&
1560 swizzle
!= SWIZZLE_ZZZZ
&&
1561 swizzle
!= SWIZZLE_WWWW
&&
1562 (mask
& (1 << swz
))) {
1563 /* a channel can't be specified twice (ex: ".xyyz") */
1564 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1565 _mesa_swizzle_string(swizzle
, 0, 0));
1571 assert(mask
<= 0xf);
1572 size
= i
; /* number of components in mask/swizzle */
1574 *writemaskOut
= mask
;
1576 /* make new src swizzle, by inversion */
1577 for (i
= 0; i
< 4; i
++) {
1578 newSwizzle
[i
] = i
; /*identity*/
1580 for (i
= 0; i
< size
; i
++) {
1581 const GLuint swz
= GET_SWZ(swizzle
, i
);
1582 newSwizzle
[swz
] = i
;
1584 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1589 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1591 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1593 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1595 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1597 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1605 #if 0 /* not used, but don't remove just yet */
1607 * Recursively traverse 'oper' to produce a swizzle mask in the event
1608 * of any vector subscripts and swizzle suffixes.
1609 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1612 resolve_swizzle(const slang_operation
*oper
)
1614 if (oper
->type
== SLANG_OPER_FIELD
) {
1615 /* writemask from .xyzw suffix */
1617 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1618 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1622 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1623 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1627 return SWIZZLE_XYZW
;
1629 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1630 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1631 /* writemask from [index] */
1632 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1633 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1638 swizzle
= SWIZZLE_XXXX
;
1641 swizzle
= SWIZZLE_YYYY
;
1644 swizzle
= SWIZZLE_ZZZZ
;
1647 swizzle
= SWIZZLE_WWWW
;
1650 swizzle
= SWIZZLE_XYZW
;
1652 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1656 return SWIZZLE_XYZW
;
1664 * Recursively descend through swizzle nodes to find the node's storage info.
1666 static slang_ir_storage
*
1667 get_store(const slang_ir_node
*n
)
1669 if (n
->Opcode
== IR_SWIZZLE
) {
1670 return get_store(n
->Children
[0]);
1678 * Generate IR tree for an asm instruction/operation such as:
1679 * __asm vec4_dot __retVal.x, v1, v2;
1681 static slang_ir_node
*
1682 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1683 slang_operation
*dest
)
1685 const slang_asm_info
*info
;
1686 slang_ir_node
*kids
[3], *n
;
1687 GLuint j
, firstOperand
;
1689 assert(oper
->type
== SLANG_OPER_ASM
);
1691 info
= slang_find_asm_info((char *) oper
->a_id
);
1693 _mesa_problem(NULL
, "undefined __asm function %s\n",
1694 (char *) oper
->a_id
);
1697 assert(info
->NumParams
<= 3);
1699 if (info
->NumParams
== oper
->num_children
) {
1700 /* Storage for result is not specified.
1701 * Children[0], [1], [2] are the operands.
1706 /* Storage for result (child[0]) is specified.
1707 * Children[1], [2], [3] are the operands.
1712 /* assemble child(ren) */
1713 kids
[0] = kids
[1] = kids
[2] = NULL
;
1714 for (j
= 0; j
< info
->NumParams
; j
++) {
1715 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1720 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1723 /* Setup n->Store to be a particular location. Otherwise, storage
1724 * for the result (a temporary) will be allocated later.
1726 slang_operation
*dest_oper
;
1729 dest_oper
= &oper
->children
[0];
1731 n0
= _slang_gen_operation(A
, dest_oper
);
1736 n
->Store
= n0
->Store
;
1738 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1749 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1752 for (i
= 0; i
< scope
->num_functions
; i
++) {
1753 slang_function
*f
= &scope
->functions
[i
];
1754 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1755 printf(" %s (%d args)\n", name
, f
->param_count
);
1758 if (scope
->outer_scope
)
1759 print_funcs(scope
->outer_scope
, name
);
1765 * Find a function of the given name, taking 'numArgs' arguments.
1766 * This is the function we'll try to call when there is no exact match
1767 * between function parameters and call arguments.
1769 * XXX we should really create a list of candidate functions and try
1772 static slang_function
*
1773 _slang_find_function_by_argc(slang_function_scope
*scope
,
1774 const char *name
, int numArgs
)
1778 for (i
= 0; i
< scope
->num_functions
; i
++) {
1779 slang_function
*f
= &scope
->functions
[i
];
1780 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1781 int haveRetValue
= _slang_function_has_return_value(f
);
1782 if (numArgs
== f
->param_count
- haveRetValue
)
1786 scope
= scope
->outer_scope
;
1793 static slang_function
*
1794 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1797 slang_function
*maxFunc
= NULL
;
1802 for (i
= 0; i
< scope
->num_functions
; i
++) {
1803 slang_function
*f
= &scope
->functions
[i
];
1804 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1805 if (f
->param_count
> maxArgs
) {
1806 maxArgs
= f
->param_count
;
1811 scope
= scope
->outer_scope
;
1819 * Generate a new slang_function which is a constructor for a user-defined
1822 static slang_function
*
1823 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1825 const GLint numFields
= str
->fields
->num_variables
;
1826 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1828 /* function header (name, return type) */
1829 fun
->header
.a_name
= str
->a_name
;
1830 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1831 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1832 fun
->header
.type
.specifier
._struct
= str
;
1834 /* function parameters (= struct's fields) */
1837 for (i
= 0; i
< numFields
; i
++) {
1839 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1841 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1842 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1843 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1845 fun
->param_count
= fun
->parameters
->num_variables
;
1848 /* Add __retVal to params */
1850 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1851 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1853 p
->a_name
= a_retVal
;
1854 p
->type
= fun
->header
.type
;
1855 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1859 /* function body is:
1869 slang_variable_scope
*scope
;
1870 slang_variable
*var
;
1873 fun
->body
= slang_operation_new(1);
1874 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1875 fun
->body
->num_children
= numFields
+ 2;
1876 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1878 scope
= fun
->body
->locals
;
1879 scope
->outer_scope
= fun
->parameters
;
1881 /* create local var 't' */
1882 var
= slang_variable_scope_grow(scope
);
1883 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1884 var
->type
= fun
->header
.type
;
1888 slang_operation
*decl
;
1890 decl
= &fun
->body
->children
[0];
1891 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1892 decl
->locals
= _slang_variable_scope_new(scope
);
1893 decl
->a_id
= var
->a_name
;
1896 /* assign params to fields of t */
1897 for (i
= 0; i
< numFields
; i
++) {
1898 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1900 assign
->type
= SLANG_OPER_ASSIGN
;
1901 assign
->locals
= _slang_variable_scope_new(scope
);
1902 assign
->num_children
= 2;
1903 assign
->children
= slang_operation_new(2);
1906 slang_operation
*lhs
= &assign
->children
[0];
1908 lhs
->type
= SLANG_OPER_FIELD
;
1909 lhs
->locals
= _slang_variable_scope_new(scope
);
1910 lhs
->num_children
= 1;
1911 lhs
->children
= slang_operation_new(1);
1912 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1914 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1915 lhs
->children
[0].a_id
= var
->a_name
;
1916 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1919 lhs
->children
[1].num_children
= 1;
1920 lhs
->children
[1].children
= slang_operation_new(1);
1921 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1922 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1923 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1928 slang_operation
*rhs
= &assign
->children
[1];
1930 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1931 rhs
->locals
= _slang_variable_scope_new(scope
);
1932 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1938 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1940 ret
->type
= SLANG_OPER_RETURN
;
1941 ret
->locals
= _slang_variable_scope_new(scope
);
1942 ret
->num_children
= 1;
1943 ret
->children
= slang_operation_new(1);
1944 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1945 ret
->children
[0].a_id
= var
->a_name
;
1946 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1950 slang_print_function(fun, 1);
1957 * Find/create a function (constructor) for the given structure name.
1959 static slang_function
*
1960 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1963 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1964 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1965 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1966 /* found a structure type that matches the function name */
1967 if (!str
->constructor
) {
1968 /* create the constructor function now */
1969 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1971 return str
->constructor
;
1979 * Generate a new slang_function to satisfy a call to an array constructor.
1980 * Ex: float[3](1., 2., 3.)
1982 static slang_function
*
1983 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1985 slang_type_specifier_type baseType
;
1986 slang_function
*fun
;
1989 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1993 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1995 num_elements
= oper
->num_children
;
1997 /* function header, return type */
1999 fun
->header
.a_name
= oper
->a_id
;
2000 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2001 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2002 fun
->header
.type
.specifier
._array
=
2003 slang_type_specifier_new(baseType
, NULL
, NULL
);
2004 fun
->header
.type
.array_len
= num_elements
;
2007 /* function parameters (= number of elements) */
2010 for (i
= 0; i
< num_elements
; i
++) {
2012 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2014 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2016 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2017 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2018 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2019 p
->type
.specifier
.type
= baseType
;
2021 fun
->param_count
= fun
->parameters
->num_variables
;
2024 /* Add __retVal to params */
2026 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2027 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2029 p
->a_name
= a_retVal
;
2030 p
->type
= fun
->header
.type
;
2031 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2032 p
->type
.specifier
.type
= baseType
;
2036 /* function body is:
2046 slang_variable_scope
*scope
;
2047 slang_variable
*var
;
2050 fun
->body
= slang_operation_new(1);
2051 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2052 fun
->body
->num_children
= num_elements
+ 2;
2053 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2055 scope
= fun
->body
->locals
;
2056 scope
->outer_scope
= fun
->parameters
;
2058 /* create local var 't' */
2059 var
= slang_variable_scope_grow(scope
);
2060 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2061 var
->type
= fun
->header
.type
;/*XXX copy*/
2065 slang_operation
*decl
;
2067 decl
= &fun
->body
->children
[0];
2068 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2069 decl
->locals
= _slang_variable_scope_new(scope
);
2070 decl
->a_id
= var
->a_name
;
2073 /* assign params to elements of t */
2074 for (i
= 0; i
< num_elements
; i
++) {
2075 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2077 assign
->type
= SLANG_OPER_ASSIGN
;
2078 assign
->locals
= _slang_variable_scope_new(scope
);
2079 assign
->num_children
= 2;
2080 assign
->children
= slang_operation_new(2);
2083 slang_operation
*lhs
= &assign
->children
[0];
2085 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2086 lhs
->locals
= _slang_variable_scope_new(scope
);
2087 lhs
->num_children
= 2;
2088 lhs
->children
= slang_operation_new(2);
2090 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2091 lhs
->children
[0].a_id
= var
->a_name
;
2092 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2094 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2095 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2099 slang_operation
*rhs
= &assign
->children
[1];
2101 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2102 rhs
->locals
= _slang_variable_scope_new(scope
);
2103 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2109 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2111 ret
->type
= SLANG_OPER_RETURN
;
2112 ret
->locals
= _slang_variable_scope_new(scope
);
2113 ret
->num_children
= 1;
2114 ret
->children
= slang_operation_new(1);
2115 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2116 ret
->children
[0].a_id
= var
->a_name
;
2117 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2122 slang_print_function(fun, 1);
2130 _slang_is_vec_mat_type(const char *name
)
2132 static const char *vecmat_types
[] = {
2133 "float", "int", "bool",
2134 "vec2", "vec3", "vec4",
2135 "ivec2", "ivec3", "ivec4",
2136 "bvec2", "bvec3", "bvec4",
2137 "mat2", "mat3", "mat4",
2138 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2142 for (i
= 0; vecmat_types
[i
]; i
++)
2143 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2150 * Assemble a function call, given a particular function name.
2151 * \param name the function's name (operators like '*' are possible).
2153 static slang_ir_node
*
2154 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2155 slang_operation
*oper
, slang_operation
*dest
)
2157 slang_operation
*params
= oper
->children
;
2158 const GLuint param_count
= oper
->num_children
;
2160 slang_function
*fun
;
2163 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2164 if (atom
== SLANG_ATOM_NULL
)
2167 if (oper
->array_constructor
) {
2168 /* this needs special handling */
2169 fun
= _slang_make_array_constructor(A
, oper
);
2172 /* Try to find function by name and exact argument type matching */
2173 GLboolean error
= GL_FALSE
;
2174 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2175 &A
->space
, A
->atoms
, A
->log
, &error
);
2177 slang_info_log_error(A
->log
,
2178 "Function '%s' not found (check argument types)",
2185 /* Next, try locating a constructor function for a user-defined type */
2186 fun
= _slang_locate_struct_constructor(A
, name
);
2190 * At this point, some heuristics are used to try to find a function
2191 * that matches the calling signature by means of casting or "unrolling"
2195 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2196 /* Next, if this call looks like a vec() or mat() constructor call,
2197 * try "unwinding" the args to satisfy a constructor.
2199 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2201 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2202 slang_info_log_error(A
->log
,
2203 "Function '%s' not found (check argument types)",
2210 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2211 /* Next, try casting args to the types of the formal parameters */
2212 int numArgs
= oper
->num_children
;
2213 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2214 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2215 slang_info_log_error(A
->log
,
2216 "Function '%s' not found (check argument types)",
2224 slang_info_log_error(A
->log
,
2225 "Function '%s' not found (check argument types)",
2230 slang_info_log_error(A
->log
,
2231 "Function '%s' prototyped but not defined. "
2232 "Separate compilation units not supported.",
2237 /* type checking to be sure function's return type matches 'dest' type */
2241 slang_typeinfo_construct(&t0
);
2242 typeof_operation(A
, dest
, &t0
);
2244 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2245 slang_info_log_error(A
->log
,
2246 "Incompatible type returned by call to '%s'",
2252 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2254 if (n
&& !n
->Store
&& !dest
2255 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2256 /* setup n->Store for the result of the function call */
2257 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2258 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2259 /*printf("Alloc storage for function result, size %d \n", size);*/
2262 if (oper
->array_constructor
) {
2263 /* free the temporary array constructor function now */
2264 slang_function_destruct(fun
);
2271 static slang_ir_node
*
2272 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2274 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2276 slang_variable
*var
;
2278 /* NOTE: In GLSL 1.20, there's only one kind of method
2279 * call: array.length(). Anything else is an error.
2281 if (oper
->a_id
!= a_length
) {
2282 slang_info_log_error(A
->log
,
2283 "Undefined method call '%s'", (char *) oper
->a_id
);
2287 /* length() takes no arguments */
2288 if (oper
->num_children
> 0) {
2289 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2293 /* lookup the object/variable */
2294 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2295 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2296 slang_info_log_error(A
->log
,
2297 "Undefined object '%s'", (char *) oper
->a_obj
);
2301 /* Create a float/literal IR node encoding the array length */
2302 n
= new_node0(IR_FLOAT
);
2304 n
->Value
[0] = (float) _slang_array_length(var
);
2305 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2312 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2314 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2315 oper
->type
== SLANG_OPER_LITERAL_INT
||
2316 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2317 if (oper
->literal
[0])
2323 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2324 oper
->num_children
== 1) {
2325 return _slang_is_constant_cond(&oper
->children
[0], value
);
2332 * Test if an operation is a scalar or boolean.
2335 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2337 slang_typeinfo type
;
2340 slang_typeinfo_construct(&type
);
2341 typeof_operation(A
, oper
, &type
);
2342 size
= _slang_sizeof_type_specifier(&type
.spec
);
2343 slang_typeinfo_destruct(&type
);
2349 * Test if an operation is boolean.
2352 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2354 slang_typeinfo type
;
2357 slang_typeinfo_construct(&type
);
2358 typeof_operation(A
, oper
, &type
);
2359 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2360 slang_typeinfo_destruct(&type
);
2366 * Generate loop code using high-level IR_LOOP instruction
2368 static slang_ir_node
*
2369 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2373 * BREAK if !expr (child[0])
2374 * body code (child[1])
2376 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2377 GLboolean isConst
, constTrue
;
2379 /* type-check expression */
2380 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2381 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2385 /* Check if loop condition is a constant */
2386 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2388 if (isConst
&& !constTrue
) {
2389 /* loop is never executed! */
2390 return new_node0(IR_NOP
);
2393 loop
= new_loop(NULL
);
2395 /* save old, push new loop */
2396 prevLoop
= A
->CurLoop
;
2399 if (isConst
&& constTrue
) {
2400 /* while(nonzero constant), no conditional break */
2405 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2406 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2408 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2409 loop
->Children
[0] = new_seq(breakIf
, body
);
2411 /* Do infinite loop detection */
2412 /* loop->List is head of linked list of break/continue nodes */
2413 if (!loop
->List
&& isConst
&& constTrue
) {
2414 /* infinite loop detected */
2415 A
->CurLoop
= prevLoop
; /* clean-up */
2416 slang_info_log_error(A
->log
, "Infinite loop detected!");
2420 /* pop loop, restore prev */
2421 A
->CurLoop
= prevLoop
;
2428 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2430 static slang_ir_node
*
2431 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2435 * body code (child[0])
2437 * BREAK if !expr (child[1])
2439 slang_ir_node
*prevLoop
, *loop
;
2440 GLboolean isConst
, constTrue
;
2442 /* type-check expression */
2443 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2444 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2448 loop
= new_loop(NULL
);
2450 /* save old, push new loop */
2451 prevLoop
= A
->CurLoop
;
2455 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2457 /* Check if loop condition is a constant */
2458 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2459 if (isConst
&& constTrue
) {
2460 /* do { } while(1) ==> no conditional break */
2461 loop
->Children
[1] = NULL
; /* no tail code */
2465 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2466 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2469 /* XXX we should do infinite loop detection, as above */
2471 /* pop loop, restore prev */
2472 A
->CurLoop
= prevLoop
;
2479 * Recursively count the number of operations rooted at 'oper'.
2480 * This gives some kind of indication of the size/complexity of an operation.
2483 sizeof_operation(const slang_operation
*oper
)
2486 GLuint count
= 1; /* me */
2488 for (i
= 0; i
< oper
->num_children
; i
++) {
2489 count
+= sizeof_operation(&oper
->children
[i
]);
2500 * Determine if a for-loop can be unrolled.
2501 * At this time, only a rather narrow class of for loops can be unrolled.
2502 * See code for details.
2503 * When a loop can't be unrolled because it's too large we'll emit a
2504 * message to the log.
2507 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2511 const char *varName
;
2514 assert(oper
->type
== SLANG_OPER_FOR
);
2515 assert(oper
->num_children
== 4);
2517 /* children[0] must be either "int i=constant" or "i=constant" */
2518 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2519 slang_variable
*var
;
2521 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2524 varId
= oper
->children
[0].children
[0].a_id
;
2526 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2530 if (!var
->initializer
)
2532 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2534 start
= (GLint
) var
->initializer
->literal
[0];
2536 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2537 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2539 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2541 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2544 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2546 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2552 /* children[1] must be "i<constant" */
2553 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2555 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2557 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2559 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2562 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2564 /* children[2] must be "i++" or "++i" */
2565 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2566 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2568 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2571 /* make sure the same variable name is used in all places */
2572 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2573 (oper
->children
[2].children
[0].a_id
!= varId
))
2576 varName
= (const char *) varId
;
2578 /* children[3], the loop body, can't be too large */
2579 bodySize
= sizeof_operation(&oper
->children
[3]);
2580 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2581 slang_info_log_print(A
->log
,
2582 "Note: 'for (%s ... )' body is too large/complex"
2589 return GL_FALSE
; /* degenerate case */
2591 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2592 slang_info_log_print(A
->log
,
2593 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2594 " many iterations to unroll",
2595 varName
, start
, varName
, end
, varName
);
2599 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2600 slang_info_log_print(A
->log
,
2601 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2602 " too much code to unroll",
2603 varName
, start
, varName
, end
, varName
);
2607 return GL_TRUE
; /* we can unroll the loop */
2612 * Unroll a for-loop.
2613 * First we determine the number of iterations to unroll.
2614 * Then for each iteration:
2615 * make a copy of the loop body
2616 * replace instances of the loop variable with the current iteration value
2617 * generate IR code for the body
2618 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2620 static slang_ir_node
*
2621 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2623 GLint start
, end
, iter
;
2624 slang_ir_node
*n
, *root
= NULL
;
2627 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2628 /* for (int i=0; ... */
2629 slang_variable
*var
;
2631 varId
= oper
->children
[0].children
[0].a_id
;
2632 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2634 start
= (GLint
) var
->initializer
->literal
[0];
2638 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2639 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2642 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2644 for (iter
= start
; iter
< end
; iter
++) {
2645 slang_operation
*body
;
2647 /* make a copy of the loop body */
2648 body
= slang_operation_new(1);
2652 if (!slang_operation_copy(body
, &oper
->children
[3]))
2655 /* in body, replace instances of 'varId' with literal 'iter' */
2657 slang_variable
*oldVar
;
2658 slang_operation
*newOper
;
2660 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2662 /* undeclared loop variable */
2663 slang_operation_delete(body
);
2667 newOper
= slang_operation_new(1);
2668 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2669 newOper
->literal_size
= 1;
2670 newOper
->literal
[0] = iter
;
2672 /* replace instances of the loop variable with newOper */
2673 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2676 /* do IR codegen for body */
2677 n
= _slang_gen_operation(A
, body
);
2678 root
= new_seq(root
, n
);
2680 slang_operation_delete(body
);
2688 * Generate IR for a for-loop. Unrolling will be done when possible.
2690 static slang_ir_node
*
2691 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2693 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2696 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2701 /* conventional for-loop code generation */
2704 * init code (child[0])
2706 * BREAK if !expr (child[1])
2707 * body code (child[3])
2709 * incr code (child[2]) // XXX continue here
2711 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2712 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2713 loop
= new_loop(NULL
);
2715 /* save old, push new loop */
2716 prevLoop
= A
->CurLoop
;
2719 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2720 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2721 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2722 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2724 loop
->Children
[0] = new_seq(breakIf
, body
);
2725 loop
->Children
[1] = incr
; /* tail code */
2727 /* pop loop, restore prev */
2728 A
->CurLoop
= prevLoop
;
2730 return new_seq(init
, loop
);
2735 static slang_ir_node
*
2736 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2738 slang_ir_node
*n
, *loopNode
;
2739 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2740 loopNode
= A
->CurLoop
;
2742 assert(loopNode
->Opcode
== IR_LOOP
);
2743 n
= new_node0(IR_CONT
);
2745 n
->Parent
= loopNode
;
2746 /* insert this node at head of linked list */
2747 n
->List
= loopNode
->List
;
2755 * Determine if the given operation is of a specific type.
2758 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2760 if (oper
->type
== type
)
2762 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2763 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2764 oper
->num_children
== 1)
2765 return is_operation_type(&oper
->children
[0], type
);
2772 * Generate IR tree for an if/then/else conditional using high-level
2773 * IR_IF instruction.
2775 static slang_ir_node
*
2776 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2779 * eval expr (child[0])
2786 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2787 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2788 GLboolean isConst
, constTrue
;
2790 /* type-check expression */
2791 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2792 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2796 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2797 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2801 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2805 return _slang_gen_operation(A
, &oper
->children
[1]);
2808 /* if (false) ... */
2809 return _slang_gen_operation(A
, &oper
->children
[2]);
2813 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2814 cond
= new_cond(cond
);
2816 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2817 && !haveElseClause
) {
2818 /* Special case: generate a conditional break */
2819 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2822 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2823 && !haveElseClause
) {
2824 /* Special case: generate a conditional break */
2825 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2830 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2832 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2835 ifNode
= new_if(cond
, ifBody
, elseBody
);
2842 static slang_ir_node
*
2843 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2847 assert(oper
->type
== SLANG_OPER_NOT
);
2849 /* type-check expression */
2850 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2851 slang_info_log_error(A
->log
,
2852 "scalar/boolean expression expected for '!'");
2856 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2864 static slang_ir_node
*
2865 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2867 slang_ir_node
*n1
, *n2
;
2869 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2871 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2872 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2873 slang_info_log_error(A
->log
,
2874 "scalar/boolean expressions expected for '^^'");
2878 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2881 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2884 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2889 * Generate IR node for storage of a temporary of given size.
2891 static slang_ir_node
*
2892 _slang_gen_temporary(GLint size
)
2894 slang_ir_storage
*store
;
2895 slang_ir_node
*n
= NULL
;
2897 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2899 n
= new_node0(IR_VAR_DECL
);
2912 * Generate program constants for an array.
2913 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2914 * This will allocate and initialize three vector constants, storing
2915 * the array in constant memory, not temporaries like a non-const array.
2916 * This can also be used for uniform array initializers.
2917 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2920 make_constant_array(slang_assemble_ctx
*A
,
2921 slang_variable
*var
,
2922 slang_operation
*initializer
)
2924 struct gl_program
*prog
= A
->program
;
2925 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2926 const char *varName
= (char *) var
->a_name
;
2927 const GLuint numElements
= initializer
->num_children
;
2933 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2935 size
= var
->store
->Size
;
2937 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2938 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2939 assert(initializer
->type
== SLANG_OPER_CALL
);
2940 assert(initializer
->array_constructor
);
2942 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2944 /* convert constructor params into ordinary floats */
2945 for (i
= 0; i
< numElements
; i
++) {
2946 const slang_operation
*op
= &initializer
->children
[i
];
2947 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2948 /* unsupported type for this optimization */
2952 for (j
= 0; j
< op
->literal_size
; j
++) {
2953 values
[i
* 4 + j
] = op
->literal
[j
];
2955 for ( ; j
< 4; j
++) {
2956 values
[i
* 4 + j
] = 0.0f
;
2960 /* slightly different paths for constants vs. uniforms */
2961 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2962 var
->store
->File
= PROGRAM_UNIFORM
;
2963 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2964 size
, datatype
, values
);
2967 var
->store
->File
= PROGRAM_CONSTANT
;
2968 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2971 assert(var
->store
->Size
== size
);
2981 * Generate IR node for allocating/declaring a variable (either a local or
2983 * Generally, this involves allocating an slang_ir_storage instance for the
2984 * variable, choosing a register file (temporary, constant, etc).
2985 * For ordinary variables we do not yet allocate storage though. We do that
2986 * when we find the first actual use of the variable to avoid allocating temp
2987 * regs that will never get used.
2988 * At this time, uniforms are always allocated space in this function.
2990 * \param initializer Optional initializer expression for the variable.
2992 static slang_ir_node
*
2993 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2994 slang_operation
*initializer
)
2996 const char *varName
= (const char *) var
->a_name
;
2997 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2998 slang_ir_node
*varDecl
, *n
;
2999 slang_ir_storage
*store
;
3000 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3001 enum register_file file
;
3003 /*assert(!var->declared);*/
3004 var
->declared
= GL_TRUE
;
3006 /* determine GPU register file for simple cases */
3007 if (is_sampler_type(&var
->type
)) {
3008 file
= PROGRAM_SAMPLER
;
3010 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3011 file
= PROGRAM_UNIFORM
;
3014 file
= PROGRAM_TEMPORARY
;
3017 totalSize
= size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3019 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3023 arrayLen
= _slang_array_length(var
);
3024 totalSize
= _slang_array_size(size
, arrayLen
);
3026 /* Allocate IR node for the declaration */
3027 varDecl
= new_node0(IR_VAR_DECL
);
3031 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
3033 assert(varDecl
->Store
== var
->store
);
3034 assert(varDecl
->Store
);
3035 assert(varDecl
->Store
->Index
< 0);
3038 assert(store
== varDecl
->Store
);
3041 /* Fill in storage fields which we now know. store->Index/Swizzle may be
3042 * set for some cases below. Otherwise, store->Index/Swizzle will be set
3046 store
->Size
= totalSize
;
3048 /* if there's an initializer, generate IR for the expression */
3050 slang_ir_node
*varRef
, *init
;
3052 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3053 /* if the variable is const, the initializer must be a const
3054 * expression as well.
3057 if (!_slang_is_constant_expr(initializer
)) {
3058 slang_info_log_error(A
->log
,
3059 "initializer for %s not constant", varName
);
3065 /* IR for the variable we're initializing */
3066 varRef
= new_var(A
, var
);
3068 slang_info_log_error(A
->log
, "out of memory");
3072 /* constant-folding, etc here */
3073 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3075 /* look for simple constant-valued variables and uniforms */
3076 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3077 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3079 if (initializer
->type
== SLANG_OPER_CALL
&&
3080 initializer
->array_constructor
) {
3081 /* array initializer */
3082 if (make_constant_array(A
, var
, initializer
))
3085 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3086 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3087 /* simple float/vector initializer */
3088 if (store
->File
== PROGRAM_UNIFORM
) {
3089 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3091 totalSize
, datatype
,
3092 initializer
->literal
);
3093 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3098 store
->File
= PROGRAM_CONSTANT
;
3099 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3101 initializer
->literal
,
3103 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3110 /* IR for initializer */
3111 init
= _slang_gen_operation(A
, initializer
);
3115 /* XXX remove this when type checking is added above */
3116 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3117 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3121 /* assign RHS to LHS */
3122 n
= new_node2(IR_COPY
, varRef
, init
);
3123 n
= new_seq(varDecl
, n
);
3126 /* no initializer */
3130 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3131 /* always need to allocate storage for uniforms at this point */
3132 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3133 totalSize
, datatype
, NULL
);
3134 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3138 printf("%s var %p %s store=%p index=%d size=%d\n",
3139 __FUNCTION__
, (void *) var
, (char *) varName
,
3140 (void *) store
, store
->Index
, store
->Size
);
3148 * Generate code for a selection expression: b ? x : y
3149 * XXX In some cases we could implement a selection expression
3150 * with an LRP instruction (use the boolean as the interpolant).
3151 * Otherwise, we use an IF/ELSE/ENDIF construct.
3153 static slang_ir_node
*
3154 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3156 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3157 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3158 slang_typeinfo type0
, type1
, type2
;
3159 int size
, isBool
, isEqual
;
3161 assert(oper
->type
== SLANG_OPER_SELECT
);
3162 assert(oper
->num_children
== 3);
3164 /* type of children[0] must be boolean */
3165 slang_typeinfo_construct(&type0
);
3166 typeof_operation(A
, &oper
->children
[0], &type0
);
3167 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3168 slang_typeinfo_destruct(&type0
);
3170 slang_info_log_error(A
->log
, "selector type is not boolean");
3174 slang_typeinfo_construct(&type1
);
3175 slang_typeinfo_construct(&type2
);
3176 typeof_operation(A
, &oper
->children
[1], &type1
);
3177 typeof_operation(A
, &oper
->children
[2], &type2
);
3178 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3179 slang_typeinfo_destruct(&type1
);
3180 slang_typeinfo_destruct(&type2
);
3182 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3186 /* size of x or y's type */
3187 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3191 tmpDecl
= _slang_gen_temporary(size
);
3193 /* the condition (child 0) */
3194 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3195 cond
= new_cond(cond
);
3197 /* if-true body (child 1) */
3198 tmpVar
= new_node0(IR_VAR
);
3199 tmpVar
->Store
= tmpDecl
->Store
;
3200 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3201 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3203 /* if-false body (child 2) */
3204 tmpVar
= new_node0(IR_VAR
);
3205 tmpVar
->Store
= tmpDecl
->Store
;
3206 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3207 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3209 ifNode
= new_if(cond
, trueNode
, falseNode
);
3212 tmpVar
= new_node0(IR_VAR
);
3213 tmpVar
->Store
= tmpDecl
->Store
;
3215 tree
= new_seq(ifNode
, tmpVar
);
3216 tree
= new_seq(tmpDecl
, tree
);
3218 /*_slang_print_ir_tree(tree, 10);*/
3224 * Generate code for &&.
3226 static slang_ir_node
*
3227 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3229 /* rewrite "a && b" as "a ? b : false" */
3230 slang_operation
*select
;
3233 select
= slang_operation_new(1);
3234 select
->type
= SLANG_OPER_SELECT
;
3235 select
->num_children
= 3;
3236 select
->children
= slang_operation_new(3);
3238 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3239 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3240 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3241 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3242 select
->children
[2].literal_size
= 1;
3244 n
= _slang_gen_select(A
, select
);
3250 * Generate code for ||.
3252 static slang_ir_node
*
3253 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3255 /* rewrite "a || b" as "a ? true : b" */
3256 slang_operation
*select
;
3259 select
= slang_operation_new(1);
3260 select
->type
= SLANG_OPER_SELECT
;
3261 select
->num_children
= 3;
3262 select
->children
= slang_operation_new(3);
3264 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3265 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3266 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3267 select
->children
[1].literal_size
= 1;
3268 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3270 n
= _slang_gen_select(A
, select
);
3276 * Generate IR tree for a return statement.
3278 static slang_ir_node
*
3279 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3281 const GLboolean haveReturnValue
3282 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3284 /* error checking */
3285 assert(A
->CurFunction
);
3286 if (haveReturnValue
&&
3287 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3288 slang_info_log_error(A
->log
, "illegal return expression");
3291 else if (!haveReturnValue
&&
3292 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3293 slang_info_log_error(A
->log
, "return statement requires an expression");
3297 if (!haveReturnValue
) {
3298 return new_return(A
->curFuncEndLabel
);
3306 * return; // goto __endOfFunction
3308 slang_operation
*assign
;
3309 slang_atom a_retVal
;
3312 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3318 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3320 /* trying to return a value in a void-valued function */
3326 assign
= slang_operation_new(1);
3327 assign
->type
= SLANG_OPER_ASSIGN
;
3328 assign
->num_children
= 2;
3329 assign
->children
= slang_operation_new(2);
3330 /* lhs (__retVal) */
3331 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3332 assign
->children
[0].a_id
= a_retVal
;
3333 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3335 /* XXX we might be able to avoid this copy someday */
3336 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3338 /* assemble the new code */
3339 n
= new_seq(_slang_gen_operation(A
, assign
),
3340 new_return(A
->curFuncEndLabel
));
3342 slang_operation_delete(assign
);
3350 * Determine if the given operation/expression is const-valued.
3353 _slang_is_constant_expr(const slang_operation
*oper
)
3355 slang_variable
*var
;
3358 switch (oper
->type
) {
3359 case SLANG_OPER_IDENTIFIER
:
3360 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3361 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3365 for (i
= 0; i
< oper
->num_children
; i
++) {
3366 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3376 * Check if an assignment of type t1 to t0 is legal.
3377 * XXX more cases needed.
3380 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3381 slang_operation
*op0
,
3382 slang_operation
*op1
)
3384 slang_typeinfo t0
, t1
;
3387 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3388 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3392 slang_typeinfo_construct(&t0
);
3393 typeof_operation(A
, op0
, &t0
);
3395 slang_typeinfo_construct(&t1
);
3396 typeof_operation(A
, op1
, &t1
);
3398 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3399 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3403 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3408 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3409 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3410 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3413 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3414 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3417 #if 0 /* not used just yet - causes problems elsewhere */
3418 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3419 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3423 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3424 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3427 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3428 t1
.spec
.type
== SLANG_SPEC_INT
)
3436 * Generate IR tree for a local variable declaration.
3437 * Basically do some error checking and call _slang_gen_var_decl().
3439 static slang_ir_node
*
3440 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3442 const char *varName
= (char *) oper
->a_id
;
3443 slang_variable
*var
;
3444 slang_ir_node
*varDecl
;
3445 slang_operation
*initializer
;
3447 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3448 assert(oper
->num_children
<= 1);
3450 /* lookup the variable by name */
3451 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3453 return NULL
; /* "shouldn't happen" */
3455 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3456 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3457 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3458 /* can't declare attribute/uniform vars inside functions */
3459 slang_info_log_error(A
->log
,
3460 "local variable '%s' cannot be an attribute/uniform/varying",
3467 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3472 /* check if the var has an initializer */
3473 if (oper
->num_children
> 0) {
3474 assert(oper
->num_children
== 1);
3475 initializer
= &oper
->children
[0];
3477 else if (var
->initializer
) {
3478 initializer
= var
->initializer
;
3485 /* check/compare var type and initializer type */
3486 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3487 slang_info_log_error(A
->log
, "incompatible types in assignment");
3492 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3493 slang_info_log_error(A
->log
,
3494 "const-qualified variable '%s' requires initializer",
3500 /* Generate IR node */
3501 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3510 * Generate IR tree for a reference to a variable (such as in an expression).
3511 * This is different from a variable declaration.
3513 static slang_ir_node
*
3514 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3516 /* If there's a variable associated with this oper (from inlining)
3517 * use it. Otherwise, use the oper's var id.
3519 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3520 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3523 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3526 assert(var
->declared
);
3527 n
= new_var(A
, var
);
3534 * Return the number of components actually named by the swizzle.
3535 * Recall that swizzles may have undefined/don't-care values.
3538 swizzle_size(GLuint swizzle
)
3541 for (i
= 0; i
< 4; i
++) {
3542 GLuint swz
= GET_SWZ(swizzle
, i
);
3543 size
+= (swz
>= 0 && swz
<= 3);
3549 static slang_ir_node
*
3550 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3552 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3556 n
->Store
= _slang_new_ir_storage_relative(0,
3557 swizzle_size(swizzle
),
3559 n
->Store
->Swizzle
= swizzle
;
3566 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3568 while (store
->Parent
)
3569 store
= store
->Parent
;
3571 if (!(store
->File
== PROGRAM_OUTPUT
||
3572 store
->File
== PROGRAM_TEMPORARY
||
3573 (store
->File
== PROGRAM_VARYING
&&
3574 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3584 * Walk up an IR storage path to compute the final swizzle.
3585 * This is used when we find an expression such as "foo.xz.yx".
3588 root_swizzle(const slang_ir_storage
*st
)
3590 GLuint swizzle
= st
->Swizzle
;
3591 while (st
->Parent
) {
3593 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3600 * Generate IR tree for an assignment (=).
3602 static slang_ir_node
*
3603 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3605 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3606 /* Check that var is writeable */
3608 = _slang_variable_locate(oper
->children
[0].locals
,
3609 oper
->children
[0].a_id
, GL_TRUE
);
3611 slang_info_log_error(A
->log
, "undefined variable '%s'",
3612 (char *) oper
->children
[0].a_id
);
3615 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3616 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3617 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3618 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3619 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3620 slang_info_log_error(A
->log
,
3621 "illegal assignment to read-only variable '%s'",
3622 (char *) oper
->children
[0].a_id
);
3627 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3628 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3629 /* Special case of: x = f(a, b)
3630 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3632 * XXX this could be even more effective if we could accomodate
3633 * cases such as "v.x = f();" - would help with typical vertex
3637 n
= _slang_gen_function_call_name(A
,
3638 (const char *) oper
->children
[1].a_id
,
3639 &oper
->children
[1], &oper
->children
[0]);
3643 slang_ir_node
*n
, *lhs
, *rhs
;
3645 /* lhs and rhs type checking */
3646 if (!_slang_assignment_compatible(A
,
3648 &oper
->children
[1])) {
3649 slang_info_log_error(A
->log
, "incompatible types in assignment");
3653 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3659 slang_info_log_error(A
->log
,
3660 "invalid left hand side for assignment");
3664 /* check that lhs is writable */
3665 if (!is_store_writable(A
, lhs
->Store
)) {
3666 slang_info_log_error(A
->log
,
3667 "illegal assignment to read-only l-value");
3671 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3673 /* convert lhs swizzle into writemask */
3674 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3675 GLuint writemask
, newSwizzle
= 0x0;
3676 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3677 /* Non-simple writemask, need to swizzle right hand side in
3678 * order to put components into the right place.
3680 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3682 n
= new_node2(IR_COPY
, lhs
, rhs
);
3693 * Generate IR tree for referencing a field in a struct (or basic vector type)
3695 static slang_ir_node
*
3696 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3700 /* type of struct */
3701 slang_typeinfo_construct(&ti
);
3702 typeof_operation(A
, &oper
->children
[0], &ti
);
3704 if (_slang_type_is_vector(ti
.spec
.type
)) {
3705 /* the field should be a swizzle */
3706 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3710 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3711 slang_info_log_error(A
->log
, "Bad swizzle");
3714 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3719 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3720 /* create new parent node with swizzle */
3722 n
= _slang_gen_swizzle(n
, swizzle
);
3725 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3726 || ti
.spec
.type
== SLANG_SPEC_INT
3727 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3728 const GLuint rows
= 1;
3732 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3733 slang_info_log_error(A
->log
, "Bad swizzle");
3735 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3739 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3740 /* create new parent node with swizzle */
3741 n
= _slang_gen_swizzle(n
, swizzle
);
3745 /* the field is a structure member (base.field) */
3746 /* oper->children[0] is the base */
3747 /* oper->a_id is the field name */
3748 slang_ir_node
*base
, *n
;
3749 slang_typeinfo field_ti
;
3750 GLint fieldSize
, fieldOffset
= -1;
3753 slang_typeinfo_construct(&field_ti
);
3754 typeof_operation(A
, oper
, &field_ti
);
3756 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3758 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3760 if (fieldSize
== 0 || fieldOffset
< 0) {
3761 const char *structName
;
3762 if (ti
.spec
._struct
)
3763 structName
= (char *) ti
.spec
._struct
->a_name
;
3765 structName
= "unknown";
3766 slang_info_log_error(A
->log
,
3767 "\"%s\" is not a member of struct \"%s\"",
3768 (char *) oper
->a_id
, structName
);
3771 assert(fieldSize
>= 0);
3773 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3775 /* error msg should have already been logged */
3779 n
= new_node1(IR_FIELD
, base
);
3783 n
->Field
= (char *) oper
->a_id
;
3785 /* Store the field's offset in storage->Index */
3786 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3796 * Gen code for array indexing.
3798 static slang_ir_node
*
3799 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3801 slang_typeinfo array_ti
;
3803 /* get array's type info */
3804 slang_typeinfo_construct(&array_ti
);
3805 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3807 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3808 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3809 /* translate the index into a swizzle/writemask: "v.x=p" */
3810 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3814 index
= (GLint
) oper
->children
[1].literal
[0];
3815 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3816 index
>= (GLint
) max
) {
3818 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3819 printf("type = %d\n", oper
->children
[1].type
);
3820 printf("index = %d, max = %d\n", index
, max
);
3821 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3822 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3829 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3831 /* use swizzle to access the element */
3832 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3836 n
= _slang_gen_swizzle(n
, swizzle
);
3842 /* conventional array */
3843 slang_typeinfo elem_ti
;
3844 slang_ir_node
*elem
, *array
, *index
;
3845 GLint elemSize
, arrayLen
;
3847 /* size of array element */
3848 slang_typeinfo_construct(&elem_ti
);
3849 typeof_operation(A
, oper
, &elem_ti
);
3850 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3852 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3853 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3855 arrayLen
= array_ti
.array_len
;
3857 slang_typeinfo_destruct(&array_ti
);
3858 slang_typeinfo_destruct(&elem_ti
);
3860 if (elemSize
<= 0) {
3861 /* unknown var or type */
3862 slang_info_log_error(A
->log
, "Undefined variable or type");
3866 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3867 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3868 if (array
&& index
) {
3870 GLint constIndex
= -1;
3871 if (index
->Opcode
== IR_FLOAT
) {
3872 constIndex
= (int) index
->Value
[0];
3873 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3874 slang_info_log_error(A
->log
,
3875 "Array index out of bounds (index=%d size=%d)",
3876 constIndex
, arrayLen
);
3877 _slang_free_ir_tree(array
);
3878 _slang_free_ir_tree(index
);
3883 if (!array
->Store
) {
3884 slang_info_log_error(A
->log
, "Invalid array");
3888 elem
= new_node2(IR_ELEMENT
, array
, index
);
3890 /* The storage info here will be updated during code emit */
3891 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3892 array
->Store
->Index
,
3894 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3898 _slang_free_ir_tree(array
);
3899 _slang_free_ir_tree(index
);
3906 static slang_ir_node
*
3907 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3908 slang_ir_opcode opcode
)
3910 slang_typeinfo t0
, t1
;
3913 slang_typeinfo_construct(&t0
);
3914 typeof_operation(A
, &oper
->children
[0], &t0
);
3916 slang_typeinfo_construct(&t1
);
3917 typeof_operation(A
, &oper
->children
[0], &t1
);
3919 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3920 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3921 slang_info_log_error(A
->log
, "Illegal array comparison");
3925 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3926 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3927 /* <, <=, >, >= can only be used with scalars */
3928 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3929 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3930 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3931 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3932 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3937 n
= new_node2(opcode
,
3938 _slang_gen_operation(A
, &oper
->children
[0]),
3939 _slang_gen_operation(A
, &oper
->children
[1]));
3941 /* result is a bool (size 1) */
3942 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3950 print_vars(slang_variable_scope
*s
)
3954 for (i
= 0; i
< s
->num_variables
; i
++) {
3956 (char*) s
->variables
[i
]->a_name
,
3957 s
->variables
[i
]->declared
);
3967 _slang_undeclare_vars(slang_variable_scope
*locals
)
3969 if (locals
->num_variables
> 0) {
3971 for (i
= 0; i
< locals
->num_variables
; i
++) {
3972 slang_variable
*v
= locals
->variables
[i
];
3973 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3974 v
->declared
= GL_FALSE
;
3982 * Generate IR tree for a slang_operation (AST node)
3984 static slang_ir_node
*
3985 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3987 switch (oper
->type
) {
3988 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3992 _slang_push_var_table(A
->vartable
);
3994 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3995 n
= _slang_gen_operation(A
, oper
);
3996 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3998 _slang_pop_var_table(A
->vartable
);
4000 /*_slang_undeclare_vars(oper->locals);*/
4001 /*print_vars(oper->locals);*/
4004 n
= new_node1(IR_SCOPE
, n
);
4009 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4010 /* list of operations */
4011 if (oper
->num_children
> 0)
4013 slang_ir_node
*n
, *tree
= NULL
;
4016 for (i
= 0; i
< oper
->num_children
; i
++) {
4017 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4019 _slang_free_ir_tree(tree
);
4020 return NULL
; /* error must have occured */
4022 tree
= new_seq(tree
, n
);
4028 return new_node0(IR_NOP
);
4031 case SLANG_OPER_EXPRESSION
:
4032 return _slang_gen_operation(A
, &oper
->children
[0]);
4034 case SLANG_OPER_FOR
:
4035 return _slang_gen_for(A
, oper
);
4037 return _slang_gen_do(A
, oper
);
4038 case SLANG_OPER_WHILE
:
4039 return _slang_gen_while(A
, oper
);
4040 case SLANG_OPER_BREAK
:
4042 slang_info_log_error(A
->log
, "'break' not in loop");
4045 return new_break(A
->CurLoop
);
4046 case SLANG_OPER_CONTINUE
:
4048 slang_info_log_error(A
->log
, "'continue' not in loop");
4051 return _slang_gen_continue(A
, oper
);
4052 case SLANG_OPER_DISCARD
:
4053 return new_node0(IR_KILL
);
4055 case SLANG_OPER_EQUAL
:
4056 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4057 case SLANG_OPER_NOTEQUAL
:
4058 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4059 case SLANG_OPER_GREATER
:
4060 return _slang_gen_compare(A
, oper
, IR_SGT
);
4061 case SLANG_OPER_LESS
:
4062 return _slang_gen_compare(A
, oper
, IR_SLT
);
4063 case SLANG_OPER_GREATEREQUAL
:
4064 return _slang_gen_compare(A
, oper
, IR_SGE
);
4065 case SLANG_OPER_LESSEQUAL
:
4066 return _slang_gen_compare(A
, oper
, IR_SLE
);
4067 case SLANG_OPER_ADD
:
4070 assert(oper
->num_children
== 2);
4071 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4074 case SLANG_OPER_SUBTRACT
:
4077 assert(oper
->num_children
== 2);
4078 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4081 case SLANG_OPER_MULTIPLY
:
4084 assert(oper
->num_children
== 2);
4085 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4088 case SLANG_OPER_DIVIDE
:
4091 assert(oper
->num_children
== 2);
4092 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4095 case SLANG_OPER_MINUS
:
4098 assert(oper
->num_children
== 1);
4099 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4102 case SLANG_OPER_PLUS
:
4103 /* +expr --> do nothing */
4104 return _slang_gen_operation(A
, &oper
->children
[0]);
4105 case SLANG_OPER_VARIABLE_DECL
:
4106 return _slang_gen_declaration(A
, oper
);
4107 case SLANG_OPER_ASSIGN
:
4108 return _slang_gen_assignment(A
, oper
);
4109 case SLANG_OPER_ADDASSIGN
:
4112 assert(oper
->num_children
== 2);
4113 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4116 case SLANG_OPER_SUBASSIGN
:
4119 assert(oper
->num_children
== 2);
4120 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4124 case SLANG_OPER_MULASSIGN
:
4127 assert(oper
->num_children
== 2);
4128 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4131 case SLANG_OPER_DIVASSIGN
:
4134 assert(oper
->num_children
== 2);
4135 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4138 case SLANG_OPER_LOGICALAND
:
4141 assert(oper
->num_children
== 2);
4142 n
= _slang_gen_logical_and(A
, oper
);
4145 case SLANG_OPER_LOGICALOR
:
4148 assert(oper
->num_children
== 2);
4149 n
= _slang_gen_logical_or(A
, oper
);
4152 case SLANG_OPER_LOGICALXOR
:
4153 return _slang_gen_xor(A
, oper
);
4154 case SLANG_OPER_NOT
:
4155 return _slang_gen_not(A
, oper
);
4156 case SLANG_OPER_SELECT
: /* b ? x : y */
4159 assert(oper
->num_children
== 3);
4160 n
= _slang_gen_select(A
, oper
);
4164 case SLANG_OPER_ASM
:
4165 return _slang_gen_asm(A
, oper
, NULL
);
4166 case SLANG_OPER_CALL
:
4167 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4169 case SLANG_OPER_METHOD
:
4170 return _slang_gen_method_call(A
, oper
);
4171 case SLANG_OPER_RETURN
:
4172 return _slang_gen_return(A
, oper
);
4173 case SLANG_OPER_LABEL
:
4174 return new_label(oper
->label
);
4175 case SLANG_OPER_IDENTIFIER
:
4176 return _slang_gen_variable(A
, oper
);
4178 return _slang_gen_if(A
, oper
);
4179 case SLANG_OPER_FIELD
:
4180 return _slang_gen_struct_field(A
, oper
);
4181 case SLANG_OPER_SUBSCRIPT
:
4182 return _slang_gen_array_element(A
, oper
);
4183 case SLANG_OPER_LITERAL_FLOAT
:
4185 case SLANG_OPER_LITERAL_INT
:
4187 case SLANG_OPER_LITERAL_BOOL
:
4188 return new_float_literal(oper
->literal
, oper
->literal_size
);
4190 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4193 assert(oper
->num_children
== 1);
4194 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4197 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4200 assert(oper
->num_children
== 1);
4201 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4204 case SLANG_OPER_PREINCREMENT
: /* ++var */
4207 assert(oper
->num_children
== 1);
4208 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4211 case SLANG_OPER_PREDECREMENT
: /* --var */
4214 assert(oper
->num_children
== 1);
4215 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4219 case SLANG_OPER_NON_INLINED_CALL
:
4220 case SLANG_OPER_SEQUENCE
:
4222 slang_ir_node
*tree
= NULL
;
4224 for (i
= 0; i
< oper
->num_children
; i
++) {
4225 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4226 tree
= new_seq(tree
, n
);
4228 tree
->Store
= n
->Store
;
4230 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4231 tree
= new_function_call(tree
, oper
->label
);
4236 case SLANG_OPER_NONE
:
4237 case SLANG_OPER_VOID
:
4238 /* returning NULL here would generate an error */
4239 return new_node0(IR_NOP
);
4242 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4244 return new_node0(IR_NOP
);
4252 * Check if the given type specifier is a rectangular texture sampler.
4255 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4257 while (spec
->_array
) {
4258 spec
= spec
->_array
;
4260 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4261 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4267 * Called by compiler when a global variable has been parsed/compiled.
4268 * Here we examine the variable's type to determine what kind of register
4269 * storage will be used.
4271 * A uniform such as "gl_Position" will become the register specification
4272 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4273 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4275 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4276 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4277 * actual texture unit (as specified by the user calling glUniform1i()).
4280 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4281 slang_unit_type type
)
4283 struct gl_program
*prog
= A
->program
;
4284 const char *varName
= (char *) var
->a_name
;
4285 GLboolean success
= GL_TRUE
;
4286 slang_ir_storage
*store
= NULL
;
4288 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4289 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4290 const GLint arrayLen
= _slang_array_length(var
);
4291 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4292 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4294 /* check for sampler2D arrays */
4295 if (texIndex
== -1 && var
->type
.specifier
._array
)
4296 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4298 if (texIndex
!= -1) {
4299 /* This is a texture sampler variable...
4300 * store->File = PROGRAM_SAMPLER
4301 * store->Index = sampler number (0..7, typically)
4302 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4304 if (var
->initializer
) {
4305 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4308 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4309 /* disallow rect samplers */
4310 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4311 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4315 (void) is_rect_sampler_spec
; /* silence warning */
4318 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4319 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4321 /* If we have a sampler array, then we need to allocate the
4322 * additional samplers to ensure we don't allocate them elsewhere.
4323 * We can't directly use _mesa_add_sampler() as that checks the
4324 * varName and gets a match, so we call _mesa_add_parameter()
4325 * directly and use the last sampler number from the call above.
4328 GLint a
= arrayLen
- 1;
4330 for (i
= 0; i
< a
; i
++) {
4331 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4332 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4333 varName
, 1, datatype
, &value
, NULL
, 0x0);
4337 if (dbg
) printf("SAMPLER ");
4339 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4340 /* Uniform variable */
4341 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4344 /* user-defined uniform */
4345 if (datatype
== GL_NONE
) {
4346 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4347 /* temporary work-around */
4348 GLenum datatype
= GL_FLOAT
;
4349 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4350 totalSize
, datatype
, NULL
);
4351 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4352 totalSize
, swizzle
);
4354 /* XXX what we need to do is unroll the struct into its
4355 * basic types, creating a uniform variable for each.
4363 * Should produce uniforms:
4364 * "f.a" (GL_FLOAT_VEC3)
4365 * "f.b" (GL_FLOAT_VEC4)
4368 if (var
->initializer
) {
4369 slang_info_log_error(A
->log
,
4370 "unsupported initializer for uniform '%s'", varName
);
4375 slang_info_log_error(A
->log
,
4376 "invalid datatype for uniform variable %s",
4382 /* non-struct uniform */
4383 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4389 /* pre-defined uniform, like gl_ModelviewMatrix */
4390 /* We know it's a uniform, but don't allocate storage unless
4393 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4394 totalSize
, swizzle
);
4396 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4398 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4399 /* varyings must be float, vec or mat */
4400 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4401 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4402 slang_info_log_error(A
->log
,
4403 "varying '%s' must be float/vector/matrix",
4408 if (var
->initializer
) {
4409 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4415 /* user-defined varying */
4421 if (var
->type
.centroid
== SLANG_CENTROID
)
4422 flags
|= PROG_PARAM_BIT_CENTROID
;
4423 if (var
->type
.variant
== SLANG_INVARIANT
)
4424 flags
|= PROG_PARAM_BIT_INVARIANT
;
4426 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4428 swizzle
= _slang_var_swizzle(size
, 0);
4429 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4430 totalSize
, swizzle
);
4433 /* pre-defined varying, like gl_Color or gl_TexCoord */
4434 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4435 /* fragment program input */
4437 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4440 assert(index
< FRAG_ATTRIB_MAX
);
4441 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4445 /* vertex program output */
4446 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4447 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4449 assert(index
< VERT_RESULT_MAX
);
4450 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4451 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4454 if (dbg
) printf("V/F ");
4456 if (dbg
) printf("VARYING ");
4458 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4461 /* attributes must be float, vec or mat */
4462 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4463 slang_info_log_error(A
->log
,
4464 "attribute '%s' must be float/vector/matrix",
4470 /* user-defined vertex attribute */
4471 const GLint attr
= -1; /* unknown */
4472 swizzle
= _slang_var_swizzle(size
, 0);
4473 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4474 size
, datatype
, attr
);
4476 index
= VERT_ATTRIB_GENERIC0
+ index
;
4479 /* pre-defined vertex attrib */
4480 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4483 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4484 if (dbg
) printf("ATTRIB ");
4486 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4487 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4488 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4490 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4491 if (dbg
) printf("INPUT ");
4493 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4494 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4495 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4496 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4499 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4500 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4501 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4502 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4504 if (dbg
) printf("OUTPUT ");
4506 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4507 /* pre-defined global constant, like gl_MaxLights */
4508 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4509 if (dbg
) printf("CONST ");
4512 /* ordinary variable (may be const) */
4515 /* IR node to declare the variable */
4516 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4518 /* emit GPU instructions */
4519 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
4521 _slang_free_ir_tree(n
);
4524 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4525 store
? store
->Index
: -2);
4528 var
->store
= store
; /* save var's storage info */
4530 var
->declared
= GL_TRUE
;
4537 * Produce an IR tree from a function AST (fun->body).
4538 * Then call the code emitter to convert the IR tree into gl_program
4542 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4545 GLboolean success
= GL_TRUE
;
4547 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4548 /* we only really generate code for main, all other functions get
4549 * inlined or codegen'd upon an actual call.
4552 /* do some basic error checking though */
4553 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4554 /* check that non-void functions actually return something */
4556 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4558 slang_info_log_error(A
->log
,
4559 "function \"%s\" has no return statement",
4560 (char *) fun
->header
.a_name
);
4562 "function \"%s\" has no return statement\n",
4563 (char *) fun
->header
.a_name
);
4568 return GL_TRUE
; /* not an error */
4572 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4573 slang_print_function(fun
, 1);
4576 /* should have been allocated earlier: */
4577 assert(A
->program
->Parameters
);
4578 assert(A
->program
->Varying
);
4579 assert(A
->vartable
);
4581 A
->CurFunction
= fun
;
4583 /* fold constant expressions, etc. */
4584 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4587 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4588 slang_print_function(fun
, 1);
4591 /* Create an end-of-function label */
4592 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4594 /* push new vartable scope */
4595 _slang_push_var_table(A
->vartable
);
4597 /* Generate IR tree for the function body code */
4598 n
= _slang_gen_operation(A
, fun
->body
);
4600 n
= new_node1(IR_SCOPE
, n
);
4602 /* pop vartable, restore previous */
4603 _slang_pop_var_table(A
->vartable
);
4606 /* XXX record error */
4610 /* append an end-of-function-label to IR tree */
4611 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4613 /*_slang_label_delete(A->curFuncEndLabel);*/
4614 A
->curFuncEndLabel
= NULL
;
4617 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4618 slang_print_function(fun
, 1);
4621 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4622 _slang_print_ir_tree(n
, 0);
4625 printf("************* End codegen function ************\n\n");
4628 /* Emit program instructions */
4629 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
4630 _slang_free_ir_tree(n
);
4632 /* free codegen context */
4634 _mesa_free(A->codegen);