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_tex1d", IR_TEX
, 1, 2 },
520 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
521 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
522 { "vec4_tex2d", IR_TEX
, 1, 2 },
523 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
524 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
525 { "vec4_tex3d", IR_TEX
, 1, 2 },
526 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
527 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
528 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
529 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
530 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
533 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
534 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
535 { "float_exp", IR_EXP
, 1, 1 },
536 { "float_exp2", IR_EXP2
, 1, 1 },
537 { "float_log2", IR_LOG2
, 1, 1 },
538 { "float_rsq", IR_RSQ
, 1, 1 },
539 { "float_rcp", IR_RCP
, 1, 1 },
540 { "float_sine", IR_SIN
, 1, 1 },
541 { "float_cosine", IR_COS
, 1, 1 },
542 { "float_noise1", IR_NOISE1
, 1, 1},
543 { "float_noise2", IR_NOISE2
, 1, 1},
544 { "float_noise3", IR_NOISE3
, 1, 1},
545 { "float_noise4", IR_NOISE4
, 1, 1},
547 { NULL
, IR_NOP
, 0, 0 }
551 static slang_ir_node
*
552 new_node3(slang_ir_opcode op
,
553 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
555 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
561 n
->InstLocation
= -1;
566 static slang_ir_node
*
567 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
569 return new_node3(op
, c0
, c1
, NULL
);
572 static slang_ir_node
*
573 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
575 return new_node3(op
, c0
, NULL
, NULL
);
578 static slang_ir_node
*
579 new_node0(slang_ir_opcode op
)
581 return new_node3(op
, NULL
, NULL
, NULL
);
586 * Create sequence of two nodes.
588 static slang_ir_node
*
589 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
595 return new_node2(IR_SEQ
, left
, right
);
598 static slang_ir_node
*
599 new_label(slang_label
*label
)
601 slang_ir_node
*n
= new_node0(IR_LABEL
);
608 static slang_ir_node
*
609 new_float_literal(const float v
[4], GLuint size
)
611 slang_ir_node
*n
= new_node0(IR_FLOAT
);
613 COPY_4V(n
->Value
, v
);
614 /* allocate a storage object, but compute actual location (Index) later */
615 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
620 static slang_ir_node
*
621 new_not(slang_ir_node
*n
)
623 return new_node1(IR_NOT
, n
);
628 * Non-inlined function call.
630 static slang_ir_node
*
631 new_function_call(slang_ir_node
*code
, slang_label
*name
)
633 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
642 * Unconditional jump.
644 static slang_ir_node
*
645 new_return(slang_label
*dest
)
647 slang_ir_node
*n
= new_node0(IR_RETURN
);
655 static slang_ir_node
*
656 new_loop(slang_ir_node
*body
)
658 return new_node1(IR_LOOP
, body
);
662 static slang_ir_node
*
663 new_break(slang_ir_node
*loopNode
)
665 slang_ir_node
*n
= new_node0(IR_BREAK
);
667 assert(loopNode
->Opcode
== IR_LOOP
);
669 /* insert this node at head of linked list */
670 n
->List
= loopNode
->List
;
678 * Make new IR_BREAK_IF_TRUE.
680 static slang_ir_node
*
681 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
685 assert(loopNode
->Opcode
== IR_LOOP
);
686 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
688 /* insert this node at head of linked list */
689 n
->List
= loopNode
->List
;
697 * Make new IR_CONT_IF_TRUE node.
699 static slang_ir_node
*
700 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
704 assert(loopNode
->Opcode
== IR_LOOP
);
705 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
707 /* insert this node at head of linked list */
708 n
->List
= loopNode
->List
;
715 static slang_ir_node
*
716 new_cond(slang_ir_node
*n
)
718 slang_ir_node
*c
= new_node1(IR_COND
, n
);
723 static slang_ir_node
*
724 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
726 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
731 * New IR_VAR node - a reference to a previously declared variable.
733 static slang_ir_node
*
734 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
736 slang_ir_node
*n
= new_node0(IR_VAR
);
738 _slang_attach_storage(n
, var
);
745 * Check if the given function is really just a wrapper for a
746 * basic assembly instruction.
749 slang_is_asm_function(const slang_function
*fun
)
751 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
752 fun
->body
->num_children
== 1 &&
753 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
761 _slang_is_noop(const slang_operation
*oper
)
764 oper
->type
== SLANG_OPER_VOID
||
765 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
773 * Recursively search tree for a node of the given type.
775 static slang_operation
*
776 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
779 if (oper
->type
== type
)
781 for (i
= 0; i
< oper
->num_children
; i
++) {
782 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
791 * Count the number of operations of the given time rooted at 'oper'.
794 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
797 if (oper
->type
== type
) {
800 for (i
= 0; i
< oper
->num_children
; i
++) {
801 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
808 * Check if the 'return' statement found under 'oper' is a "tail return"
809 * that can be no-op'd. For example:
814 * return; // this is a no-op
817 * This is used when determining if a function can be inlined. If the
818 * 'return' is not the last statement, we can't inline the function since
819 * we still need the semantic behaviour of the 'return' but we don't want
820 * to accidentally return from the _calling_ function. We'd need to use an
821 * unconditional branch, but we don't have such a GPU instruction (not
825 _slang_is_tail_return(const slang_operation
*oper
)
827 GLuint k
= oper
->num_children
;
830 const slang_operation
*last
= &oper
->children
[k
- 1];
831 if (last
->type
== SLANG_OPER_RETURN
)
833 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
834 last
->type
== SLANG_OPER_LABEL
)
835 k
--; /* try prev child */
836 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
837 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
838 /* try sub-children */
839 return _slang_is_tail_return(last
);
849 slang_resolve_variable(slang_operation
*oper
)
851 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
852 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
858 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
861 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
862 GLuint substCount
, slang_variable
**substOld
,
863 slang_operation
**substNew
, GLboolean isLHS
)
865 switch (oper
->type
) {
866 case SLANG_OPER_VARIABLE_DECL
:
868 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
869 oper
->a_id
, GL_TRUE
);
871 if (v
->initializer
&& oper
->num_children
== 0) {
872 /* set child of oper to copy of initializer */
873 oper
->num_children
= 1;
874 oper
->children
= slang_operation_new(1);
875 slang_operation_copy(&oper
->children
[0], v
->initializer
);
877 if (oper
->num_children
== 1) {
878 /* the initializer */
879 slang_substitute(A
, &oper
->children
[0], substCount
,
880 substOld
, substNew
, GL_FALSE
);
884 case SLANG_OPER_IDENTIFIER
:
885 assert(oper
->num_children
== 0);
886 if (1/**!isLHS XXX FIX */) {
887 slang_atom id
= oper
->a_id
;
890 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
892 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
896 /* look for a substitution */
897 for (i
= 0; i
< substCount
; i
++) {
898 if (v
== substOld
[i
]) {
899 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
900 #if 0 /* DEBUG only */
901 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
902 assert(substNew
[i
]->var
);
903 assert(substNew
[i
]->var
->a_name
);
904 printf("Substitute %s with %s in id node %p\n",
905 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
909 printf("Substitute %s with %f in id node %p\n",
910 (char*)v
->a_name
, substNew
[i
]->literal
[0],
914 slang_operation_copy(oper
, substNew
[i
]);
921 case SLANG_OPER_RETURN
:
922 /* do return replacement here too */
923 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
924 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
930 * then do substitutions on the assignment.
932 slang_operation
*blockOper
, *assignOper
, *returnOper
;
934 /* check if function actually has a return type */
935 assert(A
->CurFunction
);
936 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
937 slang_info_log_error(A
->log
, "illegal return expression");
941 blockOper
= slang_operation_new(1);
942 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
943 blockOper
->num_children
= 2;
944 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
945 blockOper
->children
= slang_operation_new(2);
946 assignOper
= blockOper
->children
+ 0;
947 returnOper
= blockOper
->children
+ 1;
949 assignOper
->type
= SLANG_OPER_ASSIGN
;
950 assignOper
->num_children
= 2;
951 assignOper
->locals
->outer_scope
= blockOper
->locals
;
952 assignOper
->children
= slang_operation_new(2);
953 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
954 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
955 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
957 slang_operation_copy(&assignOper
->children
[1],
960 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
961 assert(returnOper
->num_children
== 0);
963 /* do substitutions on the "__retVal = expr" sub-tree */
964 slang_substitute(A
, assignOper
,
965 substCount
, substOld
, substNew
, GL_FALSE
);
967 /* install new code */
968 slang_operation_copy(oper
, blockOper
);
969 slang_operation_destruct(blockOper
);
972 /* check if return value was expected */
973 assert(A
->CurFunction
);
974 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
975 slang_info_log_error(A
->log
, "return statement requires an expression");
981 case SLANG_OPER_ASSIGN
:
982 case SLANG_OPER_SUBSCRIPT
:
984 * child[0] can't have substitutions but child[1] can.
986 slang_substitute(A
, &oper
->children
[0],
987 substCount
, substOld
, substNew
, GL_TRUE
);
988 slang_substitute(A
, &oper
->children
[1],
989 substCount
, substOld
, substNew
, GL_FALSE
);
991 case SLANG_OPER_FIELD
:
993 slang_substitute(A
, &oper
->children
[0],
994 substCount
, substOld
, substNew
, GL_TRUE
);
999 for (i
= 0; i
< oper
->num_children
; i
++)
1000 slang_substitute(A
, &oper
->children
[i
],
1001 substCount
, substOld
, substNew
, GL_FALSE
);
1008 * Produce inline code for a call to an assembly instruction.
1009 * This is typically used to compile a call to a built-in function like this:
1011 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1013 * __asm vec4_lrp __retVal, a, y, x;
1018 * r = mix(p1, p2, p3);
1028 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1030 static slang_operation
*
1031 slang_inline_asm_function(slang_assemble_ctx
*A
,
1032 slang_function
*fun
, slang_operation
*oper
)
1034 const GLuint numArgs
= oper
->num_children
;
1036 slang_operation
*inlined
;
1037 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1038 slang_variable
**substOld
;
1039 slang_operation
**substNew
;
1041 ASSERT(slang_is_asm_function(fun
));
1042 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1045 printf("Inline %s as %s\n",
1046 (char*) fun->header.a_name,
1047 (char*) fun->body->children[0].a_id);
1051 * We'll substitute formal params with actual args in the asm call.
1053 substOld
= (slang_variable
**)
1054 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1055 substNew
= (slang_operation
**)
1056 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1057 for (i
= 0; i
< numArgs
; i
++) {
1058 substOld
[i
] = fun
->parameters
->variables
[i
];
1059 substNew
[i
] = oper
->children
+ i
;
1062 /* make a copy of the code to inline */
1063 inlined
= slang_operation_new(1);
1064 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1066 /* get rid of the __retVal child */
1067 inlined
->num_children
--;
1068 for (i
= 0; i
< inlined
->num_children
; i
++) {
1069 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1073 /* now do formal->actual substitutions */
1074 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1076 _slang_free(substOld
);
1077 _slang_free(substNew
);
1080 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1081 (char *) fun
->header
.a_name
);
1082 slang_print_tree(inlined
, 3);
1083 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1091 * Inline the given function call operation.
1092 * Return a new slang_operation that corresponds to the inlined code.
1094 static slang_operation
*
1095 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1096 slang_operation
*oper
, slang_operation
*returnOper
)
1103 ParamMode
*paramMode
;
1104 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1105 const GLuint numArgs
= oper
->num_children
;
1106 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1107 slang_operation
*args
= oper
->children
;
1108 slang_operation
*inlined
, *top
;
1109 slang_variable
**substOld
;
1110 slang_operation
**substNew
;
1111 GLuint substCount
, numCopyIn
, i
;
1112 slang_function
*prevFunction
;
1113 slang_variable_scope
*newScope
= NULL
;
1116 prevFunction
= A
->CurFunction
;
1117 A
->CurFunction
= fun
;
1119 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1120 assert(fun
->param_count
== totalArgs
);
1122 /* allocate temporary arrays */
1123 paramMode
= (ParamMode
*)
1124 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1125 substOld
= (slang_variable
**)
1126 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1127 substNew
= (slang_operation
**)
1128 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1131 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1132 (char *) fun
->header
.a_name
,
1133 fun
->parameters
->num_variables
, numArgs
);
1136 if (haveRetValue
&& !returnOper
) {
1137 /* Create 3-child comma sequence for inlined code:
1138 * child[0]: declare __resultTmp
1139 * child[1]: inlined function body
1140 * child[2]: __resultTmp
1142 slang_operation
*commaSeq
;
1143 slang_operation
*declOper
= NULL
;
1144 slang_variable
*resultVar
;
1146 commaSeq
= slang_operation_new(1);
1147 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1148 assert(commaSeq
->locals
);
1149 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1150 commaSeq
->num_children
= 3;
1151 commaSeq
->children
= slang_operation_new(3);
1152 /* allocate the return var */
1153 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1155 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1156 (void*)commaSeq->locals, (char *) fun->header.a_name);
1159 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1160 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1161 resultVar
->isTemp
= GL_TRUE
;
1163 /* child[0] = __resultTmp declaration */
1164 declOper
= &commaSeq
->children
[0];
1165 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1166 declOper
->a_id
= resultVar
->a_name
;
1167 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1169 /* child[1] = function body */
1170 inlined
= &commaSeq
->children
[1];
1171 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1173 /* child[2] = __resultTmp reference */
1174 returnOper
= &commaSeq
->children
[2];
1175 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1176 returnOper
->a_id
= resultVar
->a_name
;
1177 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1182 top
= inlined
= slang_operation_new(1);
1183 /* XXXX this may be inappropriate!!!! */
1184 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1188 assert(inlined
->locals
);
1190 /* Examine the parameters, look for inout/out params, look for possible
1191 * substitutions, etc:
1192 * param type behaviour
1193 * in copy actual to local
1194 * const in substitute param with actual
1198 for (i
= 0; i
< totalArgs
; i
++) {
1199 slang_variable
*p
= fun
->parameters
->variables
[i
];
1201 printf("Param %d: %s %s \n", i,
1202 slang_type_qual_string(p->type.qualifier),
1203 (char *) p->a_name);
1205 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1206 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1207 /* an output param */
1208 slang_operation
*arg
;
1213 paramMode
[i
] = SUBST
;
1215 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1216 slang_resolve_variable(arg
);
1218 /* replace parameter 'p' with argument 'arg' */
1219 substOld
[substCount
] = p
;
1220 substNew
[substCount
] = arg
; /* will get copied */
1223 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1224 /* a constant input param */
1225 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1226 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1227 /* replace all occurances of this parameter variable with the
1228 * actual argument variable or a literal.
1230 paramMode
[i
] = SUBST
;
1231 slang_resolve_variable(&args
[i
]);
1232 substOld
[substCount
] = p
;
1233 substNew
[substCount
] = &args
[i
]; /* will get copied */
1237 paramMode
[i
] = COPY_IN
;
1241 paramMode
[i
] = COPY_IN
;
1243 assert(paramMode
[i
]);
1246 /* actual code inlining: */
1247 slang_operation_copy(inlined
, fun
->body
);
1249 /*** XXX review this */
1250 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1251 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1252 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1255 printf("======================= orig body code ======================\n");
1256 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1257 slang_print_tree(fun
->body
, 8);
1258 printf("======================= copied code =========================\n");
1259 slang_print_tree(inlined
, 8);
1262 /* do parameter substitution in inlined code: */
1263 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1266 printf("======================= subst code ==========================\n");
1267 slang_print_tree(inlined
, 8);
1268 printf("=============================================================\n");
1271 /* New prolog statements: (inserted before the inlined code)
1272 * Copy the 'in' arguments.
1275 for (i
= 0; i
< numArgs
; i
++) {
1276 if (paramMode
[i
] == COPY_IN
) {
1277 slang_variable
*p
= fun
->parameters
->variables
[i
];
1278 /* declare parameter 'p' */
1279 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1283 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1284 assert(decl
->locals
);
1285 decl
->locals
->outer_scope
= inlined
->locals
;
1286 decl
->a_id
= p
->a_name
;
1287 decl
->num_children
= 1;
1288 decl
->children
= slang_operation_new(1);
1290 /* child[0] is the var's initializer */
1291 slang_operation_copy(&decl
->children
[0], args
+ i
);
1293 /* add parameter 'p' to the local variable scope here */
1295 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1296 pCopy
->type
= p
->type
;
1297 pCopy
->a_name
= p
->a_name
;
1298 pCopy
->array_len
= p
->array_len
;
1301 newScope
= inlined
->locals
;
1306 /* Now add copies of the function's local vars to the new variable scope */
1307 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1308 slang_variable
*p
= fun
->parameters
->variables
[i
];
1309 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1310 pCopy
->type
= p
->type
;
1311 pCopy
->a_name
= p
->a_name
;
1312 pCopy
->array_len
= p
->array_len
;
1316 /* New epilog statements:
1317 * 1. Create end of function label to jump to from return statements.
1318 * 2. Copy the 'out' parameter vars
1321 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1323 inlined
->num_children
);
1324 lab
->type
= SLANG_OPER_LABEL
;
1325 lab
->label
= A
->curFuncEndLabel
;
1328 for (i
= 0; i
< totalArgs
; i
++) {
1329 if (paramMode
[i
] == COPY_OUT
) {
1330 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1331 /* actualCallVar = outParam */
1332 /*if (i > 0 || !haveRetValue)*/
1333 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1335 inlined
->num_children
);
1336 ass
->type
= SLANG_OPER_ASSIGN
;
1337 ass
->num_children
= 2;
1338 ass
->locals
->outer_scope
= inlined
->locals
;
1339 ass
->children
= slang_operation_new(2);
1340 ass
->children
[0] = args
[i
]; /*XXX copy */
1341 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1342 ass
->children
[1].a_id
= p
->a_name
;
1343 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1347 _slang_free(paramMode
);
1348 _slang_free(substOld
);
1349 _slang_free(substNew
);
1351 /* Update scoping to use the new local vars instead of the
1352 * original function's vars. This is especially important
1353 * for nested inlining.
1356 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1359 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1360 (char *) fun
->header
.a_name
,
1361 fun
->parameters
->num_variables
, numArgs
);
1362 slang_print_tree(top
, 0);
1366 A
->CurFunction
= prevFunction
;
1372 static slang_ir_node
*
1373 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1374 slang_operation
*oper
, slang_operation
*dest
)
1377 slang_operation
*inlined
;
1378 slang_label
*prevFuncEndLabel
;
1381 prevFuncEndLabel
= A
->curFuncEndLabel
;
1382 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1383 A
->curFuncEndLabel
= _slang_label_new(name
);
1384 assert(A
->curFuncEndLabel
);
1386 if (slang_is_asm_function(fun
) && !dest
) {
1387 /* assemble assembly function - tree style */
1388 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1391 /* non-assembly function */
1392 /* We always generate an "inline-able" block of code here.
1394 * 1. insert the inline code
1395 * 2. Generate a call to the "inline" code as a subroutine
1399 slang_operation
*ret
= NULL
;
1401 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1405 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1407 /* check if this is a "tail" return */
1408 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1409 _slang_is_tail_return(inlined
)) {
1410 /* The only RETURN is the last stmt in the function, no-op it
1411 * and inline the function body.
1413 ret
->type
= SLANG_OPER_NONE
;
1416 slang_operation
*callOper
;
1417 /* The function we're calling has one or more 'return' statements.
1418 * So, we can't truly inline this function because we need to
1419 * implement 'return' with RET (and CAL).
1420 * Nevertheless, we performed "inlining" to make a new instance
1421 * of the function body to deal with static register allocation.
1423 * XXX check if there's one 'return' and if it's the very last
1424 * statement in the function - we can optimize that case.
1426 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1427 inlined
->type
== SLANG_OPER_SEQUENCE
);
1429 if (_slang_function_has_return_value(fun
) && !dest
) {
1430 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1431 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1432 callOper
= &inlined
->children
[1];
1437 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1438 callOper
->fun
= fun
;
1439 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1447 /* Replace the function call with the inlined block (or new CALL stmt) */
1448 slang_operation_destruct(oper
);
1450 _slang_free(inlined
);
1453 assert(inlined
->locals
);
1454 printf("*** Inlined code for call to %s:\n",
1455 (char*) fun
->header
.a_name
);
1456 slang_print_tree(oper
, 10);
1460 n
= _slang_gen_operation(A
, oper
);
1462 /*_slang_label_delete(A->curFuncEndLabel);*/
1463 A
->curFuncEndLabel
= prevFuncEndLabel
;
1469 static slang_asm_info
*
1470 slang_find_asm_info(const char *name
)
1473 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1474 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1483 * Some write-masked assignments are simple, but others are hard.
1486 * v.xy = vec2(a, b);
1489 * v.zy = vec2(a, b);
1490 * this gets transformed/swizzled into:
1491 * v.zy = vec2(a, b).*yx* (* = don't care)
1492 * This function helps to determine simple vs. non-simple.
1495 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1497 switch (writemask
) {
1499 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1501 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1503 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1505 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1507 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1508 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1510 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1511 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1512 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1513 case WRITEMASK_XYZW
:
1514 return swizzle
== SWIZZLE_NOOP
;
1522 * Convert the given swizzle into a writemask. In some cases this
1523 * is trivial, in other cases, we'll need to also swizzle the right
1524 * hand side to put components in the right places.
1525 * See comment above for more info.
1526 * XXX this function could be simplified and should probably be renamed.
1527 * \param swizzle the incoming swizzle
1528 * \param writemaskOut returns the writemask
1529 * \param swizzleOut swizzle to apply to the right-hand-side
1530 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1533 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1534 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1536 GLuint mask
= 0x0, newSwizzle
[4];
1539 /* make new dst writemask, compute size */
1540 for (i
= 0; i
< 4; i
++) {
1541 const GLuint swz
= GET_SWZ(swizzle
, i
);
1542 if (swz
== SWIZZLE_NIL
) {
1546 assert(swz
>= 0 && swz
<= 3);
1548 if (swizzle
!= SWIZZLE_XXXX
&&
1549 swizzle
!= SWIZZLE_YYYY
&&
1550 swizzle
!= SWIZZLE_ZZZZ
&&
1551 swizzle
!= SWIZZLE_WWWW
&&
1552 (mask
& (1 << swz
))) {
1553 /* a channel can't be specified twice (ex: ".xyyz") */
1554 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1555 _mesa_swizzle_string(swizzle
, 0, 0));
1561 assert(mask
<= 0xf);
1562 size
= i
; /* number of components in mask/swizzle */
1564 *writemaskOut
= mask
;
1566 /* make new src swizzle, by inversion */
1567 for (i
= 0; i
< 4; i
++) {
1568 newSwizzle
[i
] = i
; /*identity*/
1570 for (i
= 0; i
< size
; i
++) {
1571 const GLuint swz
= GET_SWZ(swizzle
, i
);
1572 newSwizzle
[swz
] = i
;
1574 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1579 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1581 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1583 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1585 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1587 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1595 #if 0 /* not used, but don't remove just yet */
1597 * Recursively traverse 'oper' to produce a swizzle mask in the event
1598 * of any vector subscripts and swizzle suffixes.
1599 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1602 resolve_swizzle(const slang_operation
*oper
)
1604 if (oper
->type
== SLANG_OPER_FIELD
) {
1605 /* writemask from .xyzw suffix */
1607 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1608 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1612 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1613 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1617 return SWIZZLE_XYZW
;
1619 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1620 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1621 /* writemask from [index] */
1622 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1623 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1628 swizzle
= SWIZZLE_XXXX
;
1631 swizzle
= SWIZZLE_YYYY
;
1634 swizzle
= SWIZZLE_ZZZZ
;
1637 swizzle
= SWIZZLE_WWWW
;
1640 swizzle
= SWIZZLE_XYZW
;
1642 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1646 return SWIZZLE_XYZW
;
1654 * Recursively descend through swizzle nodes to find the node's storage info.
1656 static slang_ir_storage
*
1657 get_store(const slang_ir_node
*n
)
1659 if (n
->Opcode
== IR_SWIZZLE
) {
1660 return get_store(n
->Children
[0]);
1668 * Generate IR tree for an asm instruction/operation such as:
1669 * __asm vec4_dot __retVal.x, v1, v2;
1671 static slang_ir_node
*
1672 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1673 slang_operation
*dest
)
1675 const slang_asm_info
*info
;
1676 slang_ir_node
*kids
[3], *n
;
1677 GLuint j
, firstOperand
;
1679 assert(oper
->type
== SLANG_OPER_ASM
);
1681 info
= slang_find_asm_info((char *) oper
->a_id
);
1683 _mesa_problem(NULL
, "undefined __asm function %s\n",
1684 (char *) oper
->a_id
);
1687 assert(info
->NumParams
<= 3);
1689 if (info
->NumParams
== oper
->num_children
) {
1690 /* Storage for result is not specified.
1691 * Children[0], [1], [2] are the operands.
1696 /* Storage for result (child[0]) is specified.
1697 * Children[1], [2], [3] are the operands.
1702 /* assemble child(ren) */
1703 kids
[0] = kids
[1] = kids
[2] = NULL
;
1704 for (j
= 0; j
< info
->NumParams
; j
++) {
1705 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1710 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1713 /* Setup n->Store to be a particular location. Otherwise, storage
1714 * for the result (a temporary) will be allocated later.
1716 slang_operation
*dest_oper
;
1719 dest_oper
= &oper
->children
[0];
1721 n0
= _slang_gen_operation(A
, dest_oper
);
1726 n
->Store
= n0
->Store
;
1728 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1739 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1742 for (i
= 0; i
< scope
->num_functions
; i
++) {
1743 slang_function
*f
= &scope
->functions
[i
];
1744 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1745 printf(" %s (%d args)\n", name
, f
->param_count
);
1748 if (scope
->outer_scope
)
1749 print_funcs(scope
->outer_scope
, name
);
1755 * Find a function of the given name, taking 'numArgs' arguments.
1756 * This is the function we'll try to call when there is no exact match
1757 * between function parameters and call arguments.
1759 * XXX we should really create a list of candidate functions and try
1762 static slang_function
*
1763 _slang_find_function_by_argc(slang_function_scope
*scope
,
1764 const char *name
, int numArgs
)
1768 for (i
= 0; i
< scope
->num_functions
; i
++) {
1769 slang_function
*f
= &scope
->functions
[i
];
1770 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1771 int haveRetValue
= _slang_function_has_return_value(f
);
1772 if (numArgs
== f
->param_count
- haveRetValue
)
1776 scope
= scope
->outer_scope
;
1783 static slang_function
*
1784 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1787 slang_function
*maxFunc
= NULL
;
1792 for (i
= 0; i
< scope
->num_functions
; i
++) {
1793 slang_function
*f
= &scope
->functions
[i
];
1794 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1795 if (f
->param_count
> maxArgs
) {
1796 maxArgs
= f
->param_count
;
1801 scope
= scope
->outer_scope
;
1809 * Generate a new slang_function which is a constructor for a user-defined
1812 static slang_function
*
1813 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1815 const GLint numFields
= str
->fields
->num_variables
;
1816 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1818 /* function header (name, return type) */
1819 fun
->header
.a_name
= str
->a_name
;
1820 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1821 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1822 fun
->header
.type
.specifier
._struct
= str
;
1824 /* function parameters (= struct's fields) */
1827 for (i
= 0; i
< numFields
; i
++) {
1829 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1831 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1832 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1833 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1835 fun
->param_count
= fun
->parameters
->num_variables
;
1838 /* Add __retVal to params */
1840 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1841 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1843 p
->a_name
= a_retVal
;
1844 p
->type
= fun
->header
.type
;
1845 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1849 /* function body is:
1859 slang_variable_scope
*scope
;
1860 slang_variable
*var
;
1863 fun
->body
= slang_operation_new(1);
1864 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1865 fun
->body
->num_children
= numFields
+ 2;
1866 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1868 scope
= fun
->body
->locals
;
1869 scope
->outer_scope
= fun
->parameters
;
1871 /* create local var 't' */
1872 var
= slang_variable_scope_grow(scope
);
1873 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1874 var
->type
= fun
->header
.type
;
1878 slang_operation
*decl
;
1880 decl
= &fun
->body
->children
[0];
1881 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1882 decl
->locals
= _slang_variable_scope_new(scope
);
1883 decl
->a_id
= var
->a_name
;
1886 /* assign params to fields of t */
1887 for (i
= 0; i
< numFields
; i
++) {
1888 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1890 assign
->type
= SLANG_OPER_ASSIGN
;
1891 assign
->locals
= _slang_variable_scope_new(scope
);
1892 assign
->num_children
= 2;
1893 assign
->children
= slang_operation_new(2);
1896 slang_operation
*lhs
= &assign
->children
[0];
1898 lhs
->type
= SLANG_OPER_FIELD
;
1899 lhs
->locals
= _slang_variable_scope_new(scope
);
1900 lhs
->num_children
= 1;
1901 lhs
->children
= slang_operation_new(1);
1902 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1904 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1905 lhs
->children
[0].a_id
= var
->a_name
;
1906 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1909 lhs
->children
[1].num_children
= 1;
1910 lhs
->children
[1].children
= slang_operation_new(1);
1911 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1912 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1913 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1918 slang_operation
*rhs
= &assign
->children
[1];
1920 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1921 rhs
->locals
= _slang_variable_scope_new(scope
);
1922 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1928 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1930 ret
->type
= SLANG_OPER_RETURN
;
1931 ret
->locals
= _slang_variable_scope_new(scope
);
1932 ret
->num_children
= 1;
1933 ret
->children
= slang_operation_new(1);
1934 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1935 ret
->children
[0].a_id
= var
->a_name
;
1936 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1940 slang_print_function(fun, 1);
1947 * Find/create a function (constructor) for the given structure name.
1949 static slang_function
*
1950 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1953 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1954 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1955 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1956 /* found a structure type that matches the function name */
1957 if (!str
->constructor
) {
1958 /* create the constructor function now */
1959 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1961 return str
->constructor
;
1969 * Generate a new slang_function to satisfy a call to an array constructor.
1970 * Ex: float[3](1., 2., 3.)
1972 static slang_function
*
1973 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1975 slang_type_specifier_type baseType
;
1976 slang_function
*fun
;
1979 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1983 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1985 num_elements
= oper
->num_children
;
1987 /* function header, return type */
1989 fun
->header
.a_name
= oper
->a_id
;
1990 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1991 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1992 fun
->header
.type
.specifier
._array
=
1993 slang_type_specifier_new(baseType
, NULL
, NULL
);
1994 fun
->header
.type
.array_len
= num_elements
;
1997 /* function parameters (= number of elements) */
2000 for (i
= 0; i
< num_elements
; i
++) {
2002 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2004 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2006 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2007 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2008 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2009 p
->type
.specifier
.type
= baseType
;
2011 fun
->param_count
= fun
->parameters
->num_variables
;
2014 /* Add __retVal to params */
2016 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2017 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2019 p
->a_name
= a_retVal
;
2020 p
->type
= fun
->header
.type
;
2021 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2022 p
->type
.specifier
.type
= baseType
;
2026 /* function body is:
2036 slang_variable_scope
*scope
;
2037 slang_variable
*var
;
2040 fun
->body
= slang_operation_new(1);
2041 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2042 fun
->body
->num_children
= num_elements
+ 2;
2043 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2045 scope
= fun
->body
->locals
;
2046 scope
->outer_scope
= fun
->parameters
;
2048 /* create local var 't' */
2049 var
= slang_variable_scope_grow(scope
);
2050 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2051 var
->type
= fun
->header
.type
;/*XXX copy*/
2055 slang_operation
*decl
;
2057 decl
= &fun
->body
->children
[0];
2058 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2059 decl
->locals
= _slang_variable_scope_new(scope
);
2060 decl
->a_id
= var
->a_name
;
2063 /* assign params to elements of t */
2064 for (i
= 0; i
< num_elements
; i
++) {
2065 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2067 assign
->type
= SLANG_OPER_ASSIGN
;
2068 assign
->locals
= _slang_variable_scope_new(scope
);
2069 assign
->num_children
= 2;
2070 assign
->children
= slang_operation_new(2);
2073 slang_operation
*lhs
= &assign
->children
[0];
2075 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2076 lhs
->locals
= _slang_variable_scope_new(scope
);
2077 lhs
->num_children
= 2;
2078 lhs
->children
= slang_operation_new(2);
2080 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2081 lhs
->children
[0].a_id
= var
->a_name
;
2082 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2084 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2085 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2089 slang_operation
*rhs
= &assign
->children
[1];
2091 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2092 rhs
->locals
= _slang_variable_scope_new(scope
);
2093 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2099 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2101 ret
->type
= SLANG_OPER_RETURN
;
2102 ret
->locals
= _slang_variable_scope_new(scope
);
2103 ret
->num_children
= 1;
2104 ret
->children
= slang_operation_new(1);
2105 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2106 ret
->children
[0].a_id
= var
->a_name
;
2107 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2112 slang_print_function(fun, 1);
2120 _slang_is_vec_mat_type(const char *name
)
2122 static const char *vecmat_types
[] = {
2123 "float", "int", "bool",
2124 "vec2", "vec3", "vec4",
2125 "ivec2", "ivec3", "ivec4",
2126 "bvec2", "bvec3", "bvec4",
2127 "mat2", "mat3", "mat4",
2128 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2132 for (i
= 0; vecmat_types
[i
]; i
++)
2133 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2140 * Assemble a function call, given a particular function name.
2141 * \param name the function's name (operators like '*' are possible).
2143 static slang_ir_node
*
2144 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2145 slang_operation
*oper
, slang_operation
*dest
)
2147 slang_operation
*params
= oper
->children
;
2148 const GLuint param_count
= oper
->num_children
;
2150 slang_function
*fun
;
2153 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2154 if (atom
== SLANG_ATOM_NULL
)
2157 if (oper
->array_constructor
) {
2158 /* this needs special handling */
2159 fun
= _slang_make_array_constructor(A
, oper
);
2162 /* Try to find function by name and exact argument type matching */
2163 GLboolean error
= GL_FALSE
;
2164 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2165 &A
->space
, A
->atoms
, A
->log
, &error
);
2167 slang_info_log_error(A
->log
,
2168 "Function '%s' not found (check argument types)",
2175 /* Next, try locating a constructor function for a user-defined type */
2176 fun
= _slang_locate_struct_constructor(A
, name
);
2180 * At this point, some heuristics are used to try to find a function
2181 * that matches the calling signature by means of casting or "unrolling"
2185 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2186 /* Next, if this call looks like a vec() or mat() constructor call,
2187 * try "unwinding" the args to satisfy a constructor.
2189 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2191 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2192 slang_info_log_error(A
->log
,
2193 "Function '%s' not found (check argument types)",
2200 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2201 /* Next, try casting args to the types of the formal parameters */
2202 int numArgs
= oper
->num_children
;
2203 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2204 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2205 slang_info_log_error(A
->log
,
2206 "Function '%s' not found (check argument types)",
2214 slang_info_log_error(A
->log
,
2215 "Function '%s' not found (check argument types)",
2220 slang_info_log_error(A
->log
,
2221 "Function '%s' prototyped but not defined. "
2222 "Separate compilation units not supported.",
2227 /* type checking to be sure function's return type matches 'dest' type */
2231 slang_typeinfo_construct(&t0
);
2232 typeof_operation(A
, dest
, &t0
);
2234 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2235 slang_info_log_error(A
->log
,
2236 "Incompatible type returned by call to '%s'",
2242 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2244 if (n
&& !n
->Store
&& !dest
2245 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2246 /* setup n->Store for the result of the function call */
2247 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2248 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2249 /*printf("Alloc storage for function result, size %d \n", size);*/
2252 if (oper
->array_constructor
) {
2253 /* free the temporary array constructor function now */
2254 slang_function_destruct(fun
);
2261 static slang_ir_node
*
2262 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2264 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2266 slang_variable
*var
;
2268 /* NOTE: In GLSL 1.20, there's only one kind of method
2269 * call: array.length(). Anything else is an error.
2271 if (oper
->a_id
!= a_length
) {
2272 slang_info_log_error(A
->log
,
2273 "Undefined method call '%s'", (char *) oper
->a_id
);
2277 /* length() takes no arguments */
2278 if (oper
->num_children
> 0) {
2279 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2283 /* lookup the object/variable */
2284 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2285 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2286 slang_info_log_error(A
->log
,
2287 "Undefined object '%s'", (char *) oper
->a_obj
);
2291 /* Create a float/literal IR node encoding the array length */
2292 n
= new_node0(IR_FLOAT
);
2294 n
->Value
[0] = (float) _slang_array_length(var
);
2295 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2302 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2304 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2305 oper
->type
== SLANG_OPER_LITERAL_INT
||
2306 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2307 if (oper
->literal
[0])
2313 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2314 oper
->num_children
== 1) {
2315 return _slang_is_constant_cond(&oper
->children
[0], value
);
2322 * Test if an operation is a scalar or boolean.
2325 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2327 slang_typeinfo type
;
2330 slang_typeinfo_construct(&type
);
2331 typeof_operation(A
, oper
, &type
);
2332 size
= _slang_sizeof_type_specifier(&type
.spec
);
2333 slang_typeinfo_destruct(&type
);
2339 * Test if an operation is boolean.
2342 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2344 slang_typeinfo type
;
2347 slang_typeinfo_construct(&type
);
2348 typeof_operation(A
, oper
, &type
);
2349 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2350 slang_typeinfo_destruct(&type
);
2356 * Generate loop code using high-level IR_LOOP instruction
2358 static slang_ir_node
*
2359 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2363 * BREAK if !expr (child[0])
2364 * body code (child[1])
2366 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2367 GLboolean isConst
, constTrue
;
2369 /* type-check expression */
2370 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2371 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2375 /* Check if loop condition is a constant */
2376 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2378 if (isConst
&& !constTrue
) {
2379 /* loop is never executed! */
2380 return new_node0(IR_NOP
);
2383 loop
= new_loop(NULL
);
2385 /* save old, push new loop */
2386 prevLoop
= A
->CurLoop
;
2389 if (isConst
&& constTrue
) {
2390 /* while(nonzero constant), no conditional break */
2395 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2396 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2398 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2399 loop
->Children
[0] = new_seq(breakIf
, body
);
2401 /* Do infinite loop detection */
2402 /* loop->List is head of linked list of break/continue nodes */
2403 if (!loop
->List
&& isConst
&& constTrue
) {
2404 /* infinite loop detected */
2405 A
->CurLoop
= prevLoop
; /* clean-up */
2406 slang_info_log_error(A
->log
, "Infinite loop detected!");
2410 /* pop loop, restore prev */
2411 A
->CurLoop
= prevLoop
;
2418 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2420 static slang_ir_node
*
2421 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2425 * body code (child[0])
2427 * BREAK if !expr (child[1])
2429 slang_ir_node
*prevLoop
, *loop
;
2430 GLboolean isConst
, constTrue
;
2432 /* type-check expression */
2433 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2434 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2438 loop
= new_loop(NULL
);
2440 /* save old, push new loop */
2441 prevLoop
= A
->CurLoop
;
2445 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2447 /* Check if loop condition is a constant */
2448 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2449 if (isConst
&& constTrue
) {
2450 /* do { } while(1) ==> no conditional break */
2451 loop
->Children
[1] = NULL
; /* no tail code */
2455 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2456 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2459 /* XXX we should do infinite loop detection, as above */
2461 /* pop loop, restore prev */
2462 A
->CurLoop
= prevLoop
;
2469 * Recursively count the number of operations rooted at 'oper'.
2470 * This gives some kind of indication of the size/complexity of an operation.
2473 sizeof_operation(const slang_operation
*oper
)
2476 GLuint count
= 1; /* me */
2478 for (i
= 0; i
< oper
->num_children
; i
++) {
2479 count
+= sizeof_operation(&oper
->children
[i
]);
2490 * Determine if a for-loop can be unrolled.
2491 * At this time, only a rather narrow class of for loops can be unrolled.
2492 * See code for details.
2493 * When a loop can't be unrolled because it's too large we'll emit a
2494 * message to the log.
2497 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2501 const char *varName
;
2504 assert(oper
->type
== SLANG_OPER_FOR
);
2505 assert(oper
->num_children
== 4);
2507 /* children[0] must be either "int i=constant" or "i=constant" */
2508 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2509 slang_variable
*var
;
2511 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2514 varId
= oper
->children
[0].children
[0].a_id
;
2516 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2520 if (!var
->initializer
)
2522 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2524 start
= (GLint
) var
->initializer
->literal
[0];
2526 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2527 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2529 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2531 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2534 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2536 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2542 /* children[1] must be "i<constant" */
2543 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2545 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2547 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2549 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2552 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2554 /* children[2] must be "i++" or "++i" */
2555 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2556 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2558 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2561 /* make sure the same variable name is used in all places */
2562 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2563 (oper
->children
[2].children
[0].a_id
!= varId
))
2566 varName
= (const char *) varId
;
2568 /* children[3], the loop body, can't be too large */
2569 bodySize
= sizeof_operation(&oper
->children
[3]);
2570 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2571 slang_info_log_print(A
->log
,
2572 "Note: 'for (%s ... )' body is too large/complex"
2579 return GL_FALSE
; /* degenerate case */
2581 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
2582 slang_info_log_print(A
->log
,
2583 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
2584 " many iterations to unroll",
2585 varName
, start
, varName
, end
, varName
);
2589 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
2590 slang_info_log_print(A
->log
,
2591 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
2592 " too much code to unroll",
2593 varName
, start
, varName
, end
, varName
);
2597 return GL_TRUE
; /* we can unroll the loop */
2602 * Unroll a for-loop.
2603 * First we determine the number of iterations to unroll.
2604 * Then for each iteration:
2605 * make a copy of the loop body
2606 * replace instances of the loop variable with the current iteration value
2607 * generate IR code for the body
2608 * \return pointer to generated IR code or NULL if error, out of memory, etc.
2610 static slang_ir_node
*
2611 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2613 GLint start
, end
, iter
;
2614 slang_ir_node
*n
, *root
= NULL
;
2617 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2618 /* for (int i=0; ... */
2619 slang_variable
*var
;
2621 varId
= oper
->children
[0].children
[0].a_id
;
2622 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2624 start
= (GLint
) var
->initializer
->literal
[0];
2628 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2629 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2632 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2634 for (iter
= start
; iter
< end
; iter
++) {
2635 slang_operation
*body
;
2637 /* make a copy of the loop body */
2638 body
= slang_operation_new(1);
2642 if (!slang_operation_copy(body
, &oper
->children
[3]))
2645 /* in body, replace instances of 'varId' with literal 'iter' */
2647 slang_variable
*oldVar
;
2648 slang_operation
*newOper
;
2650 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
2652 /* undeclared loop variable */
2653 slang_operation_delete(body
);
2657 newOper
= slang_operation_new(1);
2658 newOper
->type
= SLANG_OPER_LITERAL_INT
;
2659 newOper
->literal_size
= 1;
2660 newOper
->literal
[0] = iter
;
2662 /* replace instances of the loop variable with newOper */
2663 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
2666 /* do IR codegen for body */
2667 n
= _slang_gen_operation(A
, body
);
2668 root
= new_seq(root
, n
);
2670 slang_operation_delete(body
);
2678 * Generate IR for a for-loop. Unrolling will be done when possible.
2680 static slang_ir_node
*
2681 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2683 GLboolean unroll
= _slang_can_unroll_for_loop(A
, oper
);
2686 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
2691 /* conventional for-loop code generation */
2694 * init code (child[0])
2696 * BREAK if !expr (child[1])
2697 * body code (child[3])
2699 * incr code (child[2]) // XXX continue here
2701 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2702 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2703 loop
= new_loop(NULL
);
2705 /* save old, push new loop */
2706 prevLoop
= A
->CurLoop
;
2709 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2710 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2711 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2712 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2714 loop
->Children
[0] = new_seq(breakIf
, body
);
2715 loop
->Children
[1] = incr
; /* tail code */
2717 /* pop loop, restore prev */
2718 A
->CurLoop
= prevLoop
;
2720 return new_seq(init
, loop
);
2725 static slang_ir_node
*
2726 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2728 slang_ir_node
*n
, *loopNode
;
2729 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2730 loopNode
= A
->CurLoop
;
2732 assert(loopNode
->Opcode
== IR_LOOP
);
2733 n
= new_node0(IR_CONT
);
2735 n
->Parent
= loopNode
;
2736 /* insert this node at head of linked list */
2737 n
->List
= loopNode
->List
;
2745 * Determine if the given operation is of a specific type.
2748 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2750 if (oper
->type
== type
)
2752 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2753 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2754 oper
->num_children
== 1)
2755 return is_operation_type(&oper
->children
[0], type
);
2762 * Generate IR tree for an if/then/else conditional using high-level
2763 * IR_IF instruction.
2765 static slang_ir_node
*
2766 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2769 * eval expr (child[0])
2776 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2777 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2778 GLboolean isConst
, constTrue
;
2780 /* type-check expression */
2781 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2782 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2786 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2787 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2791 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2795 return _slang_gen_operation(A
, &oper
->children
[1]);
2798 /* if (false) ... */
2799 return _slang_gen_operation(A
, &oper
->children
[2]);
2803 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2804 cond
= new_cond(cond
);
2806 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2807 && !haveElseClause
) {
2808 /* Special case: generate a conditional break */
2809 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2812 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2813 && !haveElseClause
) {
2814 /* Special case: generate a conditional break */
2815 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2820 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2822 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2825 ifNode
= new_if(cond
, ifBody
, elseBody
);
2832 static slang_ir_node
*
2833 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2837 assert(oper
->type
== SLANG_OPER_NOT
);
2839 /* type-check expression */
2840 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2841 slang_info_log_error(A
->log
,
2842 "scalar/boolean expression expected for '!'");
2846 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2854 static slang_ir_node
*
2855 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2857 slang_ir_node
*n1
, *n2
;
2859 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2861 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2862 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2863 slang_info_log_error(A
->log
,
2864 "scalar/boolean expressions expected for '^^'");
2868 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2871 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2874 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2879 * Generate IR node for storage of a temporary of given size.
2881 static slang_ir_node
*
2882 _slang_gen_temporary(GLint size
)
2884 slang_ir_storage
*store
;
2885 slang_ir_node
*n
= NULL
;
2887 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2889 n
= new_node0(IR_VAR_DECL
);
2902 * Generate program constants for an array.
2903 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2904 * This will allocate and initialize three vector constants, storing
2905 * the array in constant memory, not temporaries like a non-const array.
2906 * This can also be used for uniform array initializers.
2907 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2910 make_constant_array(slang_assemble_ctx
*A
,
2911 slang_variable
*var
,
2912 slang_operation
*initializer
)
2914 struct gl_program
*prog
= A
->program
;
2915 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2916 const char *varName
= (char *) var
->a_name
;
2917 const GLuint numElements
= initializer
->num_children
;
2923 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2925 size
= var
->store
->Size
;
2927 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2928 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2929 assert(initializer
->type
== SLANG_OPER_CALL
);
2930 assert(initializer
->array_constructor
);
2932 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2934 /* convert constructor params into ordinary floats */
2935 for (i
= 0; i
< numElements
; i
++) {
2936 const slang_operation
*op
= &initializer
->children
[i
];
2937 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2938 /* unsupported type for this optimization */
2942 for (j
= 0; j
< op
->literal_size
; j
++) {
2943 values
[i
* 4 + j
] = op
->literal
[j
];
2945 for ( ; j
< 4; j
++) {
2946 values
[i
* 4 + j
] = 0.0f
;
2950 /* slightly different paths for constants vs. uniforms */
2951 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2952 var
->store
->File
= PROGRAM_UNIFORM
;
2953 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2954 size
, datatype
, values
);
2957 var
->store
->File
= PROGRAM_CONSTANT
;
2958 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2961 assert(var
->store
->Size
== size
);
2971 * Generate IR node for allocating/declaring a variable (either a local or
2973 * Generally, this involves allocating an slang_ir_storage instance for the
2974 * variable, choosing a register file (temporary, constant, etc).
2975 * For ordinary variables we do not yet allocate storage though. We do that
2976 * when we find the first actual use of the variable to avoid allocating temp
2977 * regs that will never get used.
2978 * At this time, uniforms are always allocated space in this function.
2980 * \param initializer Optional initializer expression for the variable.
2982 static slang_ir_node
*
2983 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2984 slang_operation
*initializer
)
2986 const char *varName
= (const char *) var
->a_name
;
2987 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2988 slang_ir_node
*varDecl
, *n
;
2989 slang_ir_storage
*store
;
2990 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
2991 enum register_file file
;
2993 /*assert(!var->declared);*/
2994 var
->declared
= GL_TRUE
;
2996 /* determine GPU register file for simple cases */
2997 if (is_sampler_type(&var
->type
)) {
2998 file
= PROGRAM_SAMPLER
;
3000 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3001 file
= PROGRAM_UNIFORM
;
3004 file
= PROGRAM_TEMPORARY
;
3007 totalSize
= size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3009 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3013 arrayLen
= _slang_array_length(var
);
3014 totalSize
= _slang_array_size(size
, arrayLen
);
3016 /* Allocate IR node for the declaration */
3017 varDecl
= new_node0(IR_VAR_DECL
);
3021 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
3023 assert(varDecl
->Store
== var
->store
);
3024 assert(varDecl
->Store
);
3025 assert(varDecl
->Store
->Index
< 0);
3028 assert(store
== varDecl
->Store
);
3031 /* Fill in storage fields which we now know. store->Index/Swizzle may be
3032 * set for some cases below. Otherwise, store->Index/Swizzle will be set
3036 store
->Size
= totalSize
;
3038 /* if there's an initializer, generate IR for the expression */
3040 slang_ir_node
*varRef
, *init
;
3042 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3043 /* if the variable is const, the initializer must be a const
3044 * expression as well.
3047 if (!_slang_is_constant_expr(initializer
)) {
3048 slang_info_log_error(A
->log
,
3049 "initializer for %s not constant", varName
);
3055 /* IR for the variable we're initializing */
3056 varRef
= new_var(A
, var
);
3058 slang_info_log_error(A
->log
, "out of memory");
3062 /* constant-folding, etc here */
3063 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3065 /* look for simple constant-valued variables and uniforms */
3066 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3067 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3069 if (initializer
->type
== SLANG_OPER_CALL
&&
3070 initializer
->array_constructor
) {
3071 /* array initializer */
3072 if (make_constant_array(A
, var
, initializer
))
3075 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3076 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3077 /* simple float/vector initializer */
3078 if (store
->File
== PROGRAM_UNIFORM
) {
3079 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3081 totalSize
, datatype
,
3082 initializer
->literal
);
3083 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3088 store
->File
= PROGRAM_CONSTANT
;
3089 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3091 initializer
->literal
,
3093 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3100 /* IR for initializer */
3101 init
= _slang_gen_operation(A
, initializer
);
3105 /* XXX remove this when type checking is added above */
3106 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3107 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3111 /* assign RHS to LHS */
3112 n
= new_node2(IR_COPY
, varRef
, init
);
3113 n
= new_seq(varDecl
, n
);
3116 /* no initializer */
3120 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3121 /* always need to allocate storage for uniforms at this point */
3122 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3123 totalSize
, datatype
, NULL
);
3124 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3128 printf("%s var %p %s store=%p index=%d size=%d\n",
3129 __FUNCTION__
, (void *) var
, (char *) varName
,
3130 (void *) store
, store
->Index
, store
->Size
);
3138 * Generate code for a selection expression: b ? x : y
3139 * XXX In some cases we could implement a selection expression
3140 * with an LRP instruction (use the boolean as the interpolant).
3141 * Otherwise, we use an IF/ELSE/ENDIF construct.
3143 static slang_ir_node
*
3144 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3146 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3147 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3148 slang_typeinfo type0
, type1
, type2
;
3149 int size
, isBool
, isEqual
;
3151 assert(oper
->type
== SLANG_OPER_SELECT
);
3152 assert(oper
->num_children
== 3);
3154 /* type of children[0] must be boolean */
3155 slang_typeinfo_construct(&type0
);
3156 typeof_operation(A
, &oper
->children
[0], &type0
);
3157 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3158 slang_typeinfo_destruct(&type0
);
3160 slang_info_log_error(A
->log
, "selector type is not boolean");
3164 slang_typeinfo_construct(&type1
);
3165 slang_typeinfo_construct(&type2
);
3166 typeof_operation(A
, &oper
->children
[1], &type1
);
3167 typeof_operation(A
, &oper
->children
[2], &type2
);
3168 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3169 slang_typeinfo_destruct(&type1
);
3170 slang_typeinfo_destruct(&type2
);
3172 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3176 /* size of x or y's type */
3177 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3181 tmpDecl
= _slang_gen_temporary(size
);
3183 /* the condition (child 0) */
3184 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3185 cond
= new_cond(cond
);
3187 /* if-true body (child 1) */
3188 tmpVar
= new_node0(IR_VAR
);
3189 tmpVar
->Store
= tmpDecl
->Store
;
3190 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3191 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3193 /* if-false body (child 2) */
3194 tmpVar
= new_node0(IR_VAR
);
3195 tmpVar
->Store
= tmpDecl
->Store
;
3196 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3197 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3199 ifNode
= new_if(cond
, trueNode
, falseNode
);
3202 tmpVar
= new_node0(IR_VAR
);
3203 tmpVar
->Store
= tmpDecl
->Store
;
3205 tree
= new_seq(ifNode
, tmpVar
);
3206 tree
= new_seq(tmpDecl
, tree
);
3208 /*_slang_print_ir_tree(tree, 10);*/
3214 * Generate code for &&.
3216 static slang_ir_node
*
3217 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3219 /* rewrite "a && b" as "a ? b : false" */
3220 slang_operation
*select
;
3223 select
= slang_operation_new(1);
3224 select
->type
= SLANG_OPER_SELECT
;
3225 select
->num_children
= 3;
3226 select
->children
= slang_operation_new(3);
3228 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3229 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3230 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3231 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3232 select
->children
[2].literal_size
= 1;
3234 n
= _slang_gen_select(A
, select
);
3240 * Generate code for ||.
3242 static slang_ir_node
*
3243 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3245 /* rewrite "a || b" as "a ? true : b" */
3246 slang_operation
*select
;
3249 select
= slang_operation_new(1);
3250 select
->type
= SLANG_OPER_SELECT
;
3251 select
->num_children
= 3;
3252 select
->children
= slang_operation_new(3);
3254 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3255 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3256 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3257 select
->children
[1].literal_size
= 1;
3258 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3260 n
= _slang_gen_select(A
, select
);
3266 * Generate IR tree for a return statement.
3268 static slang_ir_node
*
3269 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3271 const GLboolean haveReturnValue
3272 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3274 /* error checking */
3275 assert(A
->CurFunction
);
3276 if (haveReturnValue
&&
3277 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3278 slang_info_log_error(A
->log
, "illegal return expression");
3281 else if (!haveReturnValue
&&
3282 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3283 slang_info_log_error(A
->log
, "return statement requires an expression");
3287 if (!haveReturnValue
) {
3288 return new_return(A
->curFuncEndLabel
);
3296 * return; // goto __endOfFunction
3298 slang_operation
*assign
;
3299 slang_atom a_retVal
;
3302 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3308 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3310 /* trying to return a value in a void-valued function */
3316 assign
= slang_operation_new(1);
3317 assign
->type
= SLANG_OPER_ASSIGN
;
3318 assign
->num_children
= 2;
3319 assign
->children
= slang_operation_new(2);
3320 /* lhs (__retVal) */
3321 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3322 assign
->children
[0].a_id
= a_retVal
;
3323 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3325 /* XXX we might be able to avoid this copy someday */
3326 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3328 /* assemble the new code */
3329 n
= new_seq(_slang_gen_operation(A
, assign
),
3330 new_return(A
->curFuncEndLabel
));
3332 slang_operation_delete(assign
);
3340 * Determine if the given operation/expression is const-valued.
3343 _slang_is_constant_expr(const slang_operation
*oper
)
3345 slang_variable
*var
;
3348 switch (oper
->type
) {
3349 case SLANG_OPER_IDENTIFIER
:
3350 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3351 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3355 for (i
= 0; i
< oper
->num_children
; i
++) {
3356 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3366 * Check if an assignment of type t1 to t0 is legal.
3367 * XXX more cases needed.
3370 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3371 slang_operation
*op0
,
3372 slang_operation
*op1
)
3374 slang_typeinfo t0
, t1
;
3377 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3378 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3382 slang_typeinfo_construct(&t0
);
3383 typeof_operation(A
, op0
, &t0
);
3385 slang_typeinfo_construct(&t1
);
3386 typeof_operation(A
, op1
, &t1
);
3388 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3389 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3393 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3398 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3399 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3400 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3403 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3404 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3407 #if 0 /* not used just yet - causes problems elsewhere */
3408 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3409 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3413 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3414 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3417 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3418 t1
.spec
.type
== SLANG_SPEC_INT
)
3426 * Generate IR tree for a local variable declaration.
3427 * Basically do some error checking and call _slang_gen_var_decl().
3429 static slang_ir_node
*
3430 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3432 const char *varName
= (char *) oper
->a_id
;
3433 slang_variable
*var
;
3434 slang_ir_node
*varDecl
;
3435 slang_operation
*initializer
;
3437 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3438 assert(oper
->num_children
<= 1);
3440 /* lookup the variable by name */
3441 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3443 return NULL
; /* "shouldn't happen" */
3445 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3446 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3447 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3448 /* can't declare attribute/uniform vars inside functions */
3449 slang_info_log_error(A
->log
,
3450 "local variable '%s' cannot be an attribute/uniform/varying",
3457 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3462 /* check if the var has an initializer */
3463 if (oper
->num_children
> 0) {
3464 assert(oper
->num_children
== 1);
3465 initializer
= &oper
->children
[0];
3467 else if (var
->initializer
) {
3468 initializer
= var
->initializer
;
3475 /* check/compare var type and initializer type */
3476 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3477 slang_info_log_error(A
->log
, "incompatible types in assignment");
3482 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3483 slang_info_log_error(A
->log
,
3484 "const-qualified variable '%s' requires initializer",
3490 /* Generate IR node */
3491 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3500 * Generate IR tree for a reference to a variable (such as in an expression).
3501 * This is different from a variable declaration.
3503 static slang_ir_node
*
3504 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3506 /* If there's a variable associated with this oper (from inlining)
3507 * use it. Otherwise, use the oper's var id.
3509 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3510 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3513 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3516 assert(var
->declared
);
3517 n
= new_var(A
, var
);
3524 * Return the number of components actually named by the swizzle.
3525 * Recall that swizzles may have undefined/don't-care values.
3528 swizzle_size(GLuint swizzle
)
3531 for (i
= 0; i
< 4; i
++) {
3532 GLuint swz
= GET_SWZ(swizzle
, i
);
3533 size
+= (swz
>= 0 && swz
<= 3);
3539 static slang_ir_node
*
3540 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3542 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3546 n
->Store
= _slang_new_ir_storage_relative(0,
3547 swizzle_size(swizzle
),
3549 n
->Store
->Swizzle
= swizzle
;
3556 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3558 while (store
->Parent
)
3559 store
= store
->Parent
;
3561 if (!(store
->File
== PROGRAM_OUTPUT
||
3562 store
->File
== PROGRAM_TEMPORARY
||
3563 (store
->File
== PROGRAM_VARYING
&&
3564 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3574 * Walk up an IR storage path to compute the final swizzle.
3575 * This is used when we find an expression such as "foo.xz.yx".
3578 root_swizzle(const slang_ir_storage
*st
)
3580 GLuint swizzle
= st
->Swizzle
;
3581 while (st
->Parent
) {
3583 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
3590 * Generate IR tree for an assignment (=).
3592 static slang_ir_node
*
3593 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3595 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3596 /* Check that var is writeable */
3598 = _slang_variable_locate(oper
->children
[0].locals
,
3599 oper
->children
[0].a_id
, GL_TRUE
);
3601 slang_info_log_error(A
->log
, "undefined variable '%s'",
3602 (char *) oper
->children
[0].a_id
);
3605 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3606 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3607 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3608 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3609 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3610 slang_info_log_error(A
->log
,
3611 "illegal assignment to read-only variable '%s'",
3612 (char *) oper
->children
[0].a_id
);
3617 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3618 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3619 /* Special case of: x = f(a, b)
3620 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3622 * XXX this could be even more effective if we could accomodate
3623 * cases such as "v.x = f();" - would help with typical vertex
3627 n
= _slang_gen_function_call_name(A
,
3628 (const char *) oper
->children
[1].a_id
,
3629 &oper
->children
[1], &oper
->children
[0]);
3633 slang_ir_node
*n
, *lhs
, *rhs
;
3635 /* lhs and rhs type checking */
3636 if (!_slang_assignment_compatible(A
,
3638 &oper
->children
[1])) {
3639 slang_info_log_error(A
->log
, "incompatible types in assignment");
3643 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3649 slang_info_log_error(A
->log
,
3650 "invalid left hand side for assignment");
3654 /* check that lhs is writable */
3655 if (!is_store_writable(A
, lhs
->Store
)) {
3656 slang_info_log_error(A
->log
,
3657 "illegal assignment to read-only l-value");
3661 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3663 /* convert lhs swizzle into writemask */
3664 const GLuint swizzle
= root_swizzle(lhs
->Store
);
3665 GLuint writemask
, newSwizzle
;
3666 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
3667 /* Non-simple writemask, need to swizzle right hand side in
3668 * order to put components into the right place.
3670 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3672 n
= new_node2(IR_COPY
, lhs
, rhs
);
3683 * Generate IR tree for referencing a field in a struct (or basic vector type)
3685 static slang_ir_node
*
3686 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3690 /* type of struct */
3691 slang_typeinfo_construct(&ti
);
3692 typeof_operation(A
, &oper
->children
[0], &ti
);
3694 if (_slang_type_is_vector(ti
.spec
.type
)) {
3695 /* the field should be a swizzle */
3696 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3700 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3701 slang_info_log_error(A
->log
, "Bad swizzle");
3704 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3709 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3710 /* create new parent node with swizzle */
3712 n
= _slang_gen_swizzle(n
, swizzle
);
3715 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3716 || ti
.spec
.type
== SLANG_SPEC_INT
3717 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3718 const GLuint rows
= 1;
3722 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3723 slang_info_log_error(A
->log
, "Bad swizzle");
3725 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3729 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3730 /* create new parent node with swizzle */
3731 n
= _slang_gen_swizzle(n
, swizzle
);
3735 /* the field is a structure member (base.field) */
3736 /* oper->children[0] is the base */
3737 /* oper->a_id is the field name */
3738 slang_ir_node
*base
, *n
;
3739 slang_typeinfo field_ti
;
3740 GLint fieldSize
, fieldOffset
= -1;
3743 slang_typeinfo_construct(&field_ti
);
3744 typeof_operation(A
, oper
, &field_ti
);
3746 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3748 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3750 if (fieldSize
== 0 || fieldOffset
< 0) {
3751 const char *structName
;
3752 if (ti
.spec
._struct
)
3753 structName
= (char *) ti
.spec
._struct
->a_name
;
3755 structName
= "unknown";
3756 slang_info_log_error(A
->log
,
3757 "\"%s\" is not a member of struct \"%s\"",
3758 (char *) oper
->a_id
, structName
);
3761 assert(fieldSize
>= 0);
3763 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3765 /* error msg should have already been logged */
3769 n
= new_node1(IR_FIELD
, base
);
3773 n
->Field
= (char *) oper
->a_id
;
3775 /* Store the field's offset in storage->Index */
3776 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3786 * Gen code for array indexing.
3788 static slang_ir_node
*
3789 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3791 slang_typeinfo array_ti
;
3793 /* get array's type info */
3794 slang_typeinfo_construct(&array_ti
);
3795 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3797 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3798 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3799 /* translate the index into a swizzle/writemask: "v.x=p" */
3800 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3804 index
= (GLint
) oper
->children
[1].literal
[0];
3805 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3806 index
>= (GLint
) max
) {
3808 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3809 printf("type = %d\n", oper
->children
[1].type
);
3810 printf("index = %d, max = %d\n", index
, max
);
3811 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
3812 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
3819 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3821 /* use swizzle to access the element */
3822 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3826 n
= _slang_gen_swizzle(n
, swizzle
);
3832 /* conventional array */
3833 slang_typeinfo elem_ti
;
3834 slang_ir_node
*elem
, *array
, *index
;
3835 GLint elemSize
, arrayLen
;
3837 /* size of array element */
3838 slang_typeinfo_construct(&elem_ti
);
3839 typeof_operation(A
, oper
, &elem_ti
);
3840 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3842 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3843 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3845 arrayLen
= array_ti
.array_len
;
3847 slang_typeinfo_destruct(&array_ti
);
3848 slang_typeinfo_destruct(&elem_ti
);
3850 if (elemSize
<= 0) {
3851 /* unknown var or type */
3852 slang_info_log_error(A
->log
, "Undefined variable or type");
3856 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3857 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3858 if (array
&& index
) {
3860 GLint constIndex
= -1;
3861 if (index
->Opcode
== IR_FLOAT
) {
3862 constIndex
= (int) index
->Value
[0];
3863 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3864 slang_info_log_error(A
->log
,
3865 "Array index out of bounds (index=%d size=%d)",
3866 constIndex
, arrayLen
);
3867 _slang_free_ir_tree(array
);
3868 _slang_free_ir_tree(index
);
3873 if (!array
->Store
) {
3874 slang_info_log_error(A
->log
, "Invalid array");
3878 elem
= new_node2(IR_ELEMENT
, array
, index
);
3880 /* The storage info here will be updated during code emit */
3881 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3882 array
->Store
->Index
,
3884 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
3888 _slang_free_ir_tree(array
);
3889 _slang_free_ir_tree(index
);
3896 static slang_ir_node
*
3897 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3898 slang_ir_opcode opcode
)
3900 slang_typeinfo t0
, t1
;
3903 slang_typeinfo_construct(&t0
);
3904 typeof_operation(A
, &oper
->children
[0], &t0
);
3906 slang_typeinfo_construct(&t1
);
3907 typeof_operation(A
, &oper
->children
[0], &t1
);
3909 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3910 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3911 slang_info_log_error(A
->log
, "Illegal array comparison");
3915 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3916 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3917 /* <, <=, >, >= can only be used with scalars */
3918 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3919 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3920 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3921 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3922 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3927 n
= new_node2(opcode
,
3928 _slang_gen_operation(A
, &oper
->children
[0]),
3929 _slang_gen_operation(A
, &oper
->children
[1]));
3931 /* result is a bool (size 1) */
3932 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3940 print_vars(slang_variable_scope
*s
)
3944 for (i
= 0; i
< s
->num_variables
; i
++) {
3946 (char*) s
->variables
[i
]->a_name
,
3947 s
->variables
[i
]->declared
);
3957 _slang_undeclare_vars(slang_variable_scope
*locals
)
3959 if (locals
->num_variables
> 0) {
3961 for (i
= 0; i
< locals
->num_variables
; i
++) {
3962 slang_variable
*v
= locals
->variables
[i
];
3963 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3964 v
->declared
= GL_FALSE
;
3972 * Generate IR tree for a slang_operation (AST node)
3974 static slang_ir_node
*
3975 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3977 switch (oper
->type
) {
3978 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3982 _slang_push_var_table(A
->vartable
);
3984 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3985 n
= _slang_gen_operation(A
, oper
);
3986 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3988 _slang_pop_var_table(A
->vartable
);
3990 /*_slang_undeclare_vars(oper->locals);*/
3991 /*print_vars(oper->locals);*/
3994 n
= new_node1(IR_SCOPE
, n
);
3999 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4000 /* list of operations */
4001 if (oper
->num_children
> 0)
4003 slang_ir_node
*n
, *tree
= NULL
;
4006 for (i
= 0; i
< oper
->num_children
; i
++) {
4007 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4009 _slang_free_ir_tree(tree
);
4010 return NULL
; /* error must have occured */
4012 tree
= new_seq(tree
, n
);
4018 return new_node0(IR_NOP
);
4021 case SLANG_OPER_EXPRESSION
:
4022 return _slang_gen_operation(A
, &oper
->children
[0]);
4024 case SLANG_OPER_FOR
:
4025 return _slang_gen_for(A
, oper
);
4027 return _slang_gen_do(A
, oper
);
4028 case SLANG_OPER_WHILE
:
4029 return _slang_gen_while(A
, oper
);
4030 case SLANG_OPER_BREAK
:
4032 slang_info_log_error(A
->log
, "'break' not in loop");
4035 return new_break(A
->CurLoop
);
4036 case SLANG_OPER_CONTINUE
:
4038 slang_info_log_error(A
->log
, "'continue' not in loop");
4041 return _slang_gen_continue(A
, oper
);
4042 case SLANG_OPER_DISCARD
:
4043 return new_node0(IR_KILL
);
4045 case SLANG_OPER_EQUAL
:
4046 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4047 case SLANG_OPER_NOTEQUAL
:
4048 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4049 case SLANG_OPER_GREATER
:
4050 return _slang_gen_compare(A
, oper
, IR_SGT
);
4051 case SLANG_OPER_LESS
:
4052 return _slang_gen_compare(A
, oper
, IR_SLT
);
4053 case SLANG_OPER_GREATEREQUAL
:
4054 return _slang_gen_compare(A
, oper
, IR_SGE
);
4055 case SLANG_OPER_LESSEQUAL
:
4056 return _slang_gen_compare(A
, oper
, IR_SLE
);
4057 case SLANG_OPER_ADD
:
4060 assert(oper
->num_children
== 2);
4061 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4064 case SLANG_OPER_SUBTRACT
:
4067 assert(oper
->num_children
== 2);
4068 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4071 case SLANG_OPER_MULTIPLY
:
4074 assert(oper
->num_children
== 2);
4075 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4078 case SLANG_OPER_DIVIDE
:
4081 assert(oper
->num_children
== 2);
4082 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4085 case SLANG_OPER_MINUS
:
4088 assert(oper
->num_children
== 1);
4089 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4092 case SLANG_OPER_PLUS
:
4093 /* +expr --> do nothing */
4094 return _slang_gen_operation(A
, &oper
->children
[0]);
4095 case SLANG_OPER_VARIABLE_DECL
:
4096 return _slang_gen_declaration(A
, oper
);
4097 case SLANG_OPER_ASSIGN
:
4098 return _slang_gen_assignment(A
, oper
);
4099 case SLANG_OPER_ADDASSIGN
:
4102 assert(oper
->num_children
== 2);
4103 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4106 case SLANG_OPER_SUBASSIGN
:
4109 assert(oper
->num_children
== 2);
4110 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4114 case SLANG_OPER_MULASSIGN
:
4117 assert(oper
->num_children
== 2);
4118 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4121 case SLANG_OPER_DIVASSIGN
:
4124 assert(oper
->num_children
== 2);
4125 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4128 case SLANG_OPER_LOGICALAND
:
4131 assert(oper
->num_children
== 2);
4132 n
= _slang_gen_logical_and(A
, oper
);
4135 case SLANG_OPER_LOGICALOR
:
4138 assert(oper
->num_children
== 2);
4139 n
= _slang_gen_logical_or(A
, oper
);
4142 case SLANG_OPER_LOGICALXOR
:
4143 return _slang_gen_xor(A
, oper
);
4144 case SLANG_OPER_NOT
:
4145 return _slang_gen_not(A
, oper
);
4146 case SLANG_OPER_SELECT
: /* b ? x : y */
4149 assert(oper
->num_children
== 3);
4150 n
= _slang_gen_select(A
, oper
);
4154 case SLANG_OPER_ASM
:
4155 return _slang_gen_asm(A
, oper
, NULL
);
4156 case SLANG_OPER_CALL
:
4157 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4159 case SLANG_OPER_METHOD
:
4160 return _slang_gen_method_call(A
, oper
);
4161 case SLANG_OPER_RETURN
:
4162 return _slang_gen_return(A
, oper
);
4163 case SLANG_OPER_LABEL
:
4164 return new_label(oper
->label
);
4165 case SLANG_OPER_IDENTIFIER
:
4166 return _slang_gen_variable(A
, oper
);
4168 return _slang_gen_if(A
, oper
);
4169 case SLANG_OPER_FIELD
:
4170 return _slang_gen_struct_field(A
, oper
);
4171 case SLANG_OPER_SUBSCRIPT
:
4172 return _slang_gen_array_element(A
, oper
);
4173 case SLANG_OPER_LITERAL_FLOAT
:
4175 case SLANG_OPER_LITERAL_INT
:
4177 case SLANG_OPER_LITERAL_BOOL
:
4178 return new_float_literal(oper
->literal
, oper
->literal_size
);
4180 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4183 assert(oper
->num_children
== 1);
4184 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4187 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4190 assert(oper
->num_children
== 1);
4191 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4194 case SLANG_OPER_PREINCREMENT
: /* ++var */
4197 assert(oper
->num_children
== 1);
4198 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4201 case SLANG_OPER_PREDECREMENT
: /* --var */
4204 assert(oper
->num_children
== 1);
4205 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4209 case SLANG_OPER_NON_INLINED_CALL
:
4210 case SLANG_OPER_SEQUENCE
:
4212 slang_ir_node
*tree
= NULL
;
4214 for (i
= 0; i
< oper
->num_children
; i
++) {
4215 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4216 tree
= new_seq(tree
, n
);
4218 tree
->Store
= n
->Store
;
4220 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4221 tree
= new_function_call(tree
, oper
->label
);
4226 case SLANG_OPER_NONE
:
4227 case SLANG_OPER_VOID
:
4228 /* returning NULL here would generate an error */
4229 return new_node0(IR_NOP
);
4232 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4234 return new_node0(IR_NOP
);
4242 * Called by compiler when a global variable has been parsed/compiled.
4243 * Here we examine the variable's type to determine what kind of register
4244 * storage will be used.
4246 * A uniform such as "gl_Position" will become the register specification
4247 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4248 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4250 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4251 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4252 * actual texture unit (as specified by the user calling glUniform1i()).
4255 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4256 slang_unit_type type
)
4258 struct gl_program
*prog
= A
->program
;
4259 const char *varName
= (char *) var
->a_name
;
4260 GLboolean success
= GL_TRUE
;
4261 slang_ir_storage
*store
= NULL
;
4263 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4264 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4265 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4266 const GLint arrayLen
= _slang_array_length(var
);
4267 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4269 if (texIndex
!= -1) {
4270 /* This is a texture sampler variable...
4271 * store->File = PROGRAM_SAMPLER
4272 * store->Index = sampler number (0..7, typically)
4273 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4275 if (var
->initializer
) {
4276 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4279 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4280 /* disallow rect samplers */
4281 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4282 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
4283 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4288 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4289 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4291 if (dbg
) printf("SAMPLER ");
4293 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4294 /* Uniform variable */
4295 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4298 /* user-defined uniform */
4299 if (datatype
== GL_NONE
) {
4300 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4301 /* temporary work-around */
4302 GLenum datatype
= GL_FLOAT
;
4303 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4304 totalSize
, datatype
, NULL
);
4305 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4306 totalSize
, swizzle
);
4308 /* XXX what we need to do is unroll the struct into its
4309 * basic types, creating a uniform variable for each.
4317 * Should produce uniforms:
4318 * "f.a" (GL_FLOAT_VEC3)
4319 * "f.b" (GL_FLOAT_VEC4)
4322 if (var
->initializer
) {
4323 slang_info_log_error(A
->log
,
4324 "unsupported initializer for uniform '%s'", varName
);
4329 slang_info_log_error(A
->log
,
4330 "invalid datatype for uniform variable %s",
4336 /* non-struct uniform */
4337 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4343 /* pre-defined uniform, like gl_ModelviewMatrix */
4344 /* We know it's a uniform, but don't allocate storage unless
4347 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4348 totalSize
, swizzle
);
4350 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4352 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4353 /* varyings must be float, vec or mat */
4354 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4355 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4356 slang_info_log_error(A
->log
,
4357 "varying '%s' must be float/vector/matrix",
4362 if (var
->initializer
) {
4363 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4369 /* user-defined varying */
4375 if (var
->type
.centroid
== SLANG_CENTROID
)
4376 flags
|= PROG_PARAM_BIT_CENTROID
;
4377 if (var
->type
.variant
== SLANG_INVARIANT
)
4378 flags
|= PROG_PARAM_BIT_INVARIANT
;
4380 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4382 swizzle
= _slang_var_swizzle(size
, 0);
4383 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4384 totalSize
, swizzle
);
4387 /* pre-defined varying, like gl_Color or gl_TexCoord */
4388 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4389 /* fragment program input */
4391 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4394 assert(index
< FRAG_ATTRIB_MAX
);
4395 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4399 /* vertex program output */
4400 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4401 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4403 assert(index
< VERT_RESULT_MAX
);
4404 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4405 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4408 if (dbg
) printf("V/F ");
4410 if (dbg
) printf("VARYING ");
4412 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4415 /* attributes must be float, vec or mat */
4416 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4417 slang_info_log_error(A
->log
,
4418 "attribute '%s' must be float/vector/matrix",
4424 /* user-defined vertex attribute */
4425 const GLint attr
= -1; /* unknown */
4426 swizzle
= _slang_var_swizzle(size
, 0);
4427 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4428 size
, datatype
, attr
);
4430 index
= VERT_ATTRIB_GENERIC0
+ index
;
4433 /* pre-defined vertex attrib */
4434 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4437 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4438 if (dbg
) printf("ATTRIB ");
4440 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4441 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4442 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4444 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4445 if (dbg
) printf("INPUT ");
4447 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4448 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4449 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4450 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4453 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4454 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4455 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4456 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4458 if (dbg
) printf("OUTPUT ");
4460 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4461 /* pre-defined global constant, like gl_MaxLights */
4462 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4463 if (dbg
) printf("CONST ");
4466 /* ordinary variable (may be const) */
4469 /* IR node to declare the variable */
4470 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4472 /* emit GPU instructions */
4473 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4475 _slang_free_ir_tree(n
);
4478 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4479 store
? store
->Index
: -2);
4482 var
->store
= store
; /* save var's storage info */
4484 var
->declared
= GL_TRUE
;
4491 * Produce an IR tree from a function AST (fun->body).
4492 * Then call the code emitter to convert the IR tree into gl_program
4496 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4499 GLboolean success
= GL_TRUE
;
4501 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4502 /* we only really generate code for main, all other functions get
4503 * inlined or codegen'd upon an actual call.
4506 /* do some basic error checking though */
4507 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4508 /* check that non-void functions actually return something */
4510 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4512 slang_info_log_error(A
->log
,
4513 "function \"%s\" has no return statement",
4514 (char *) fun
->header
.a_name
);
4516 "function \"%s\" has no return statement\n",
4517 (char *) fun
->header
.a_name
);
4522 return GL_TRUE
; /* not an error */
4526 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4527 slang_print_function(fun
, 1);
4530 /* should have been allocated earlier: */
4531 assert(A
->program
->Parameters
);
4532 assert(A
->program
->Varying
);
4533 assert(A
->vartable
);
4535 A
->CurFunction
= fun
;
4537 /* fold constant expressions, etc. */
4538 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4541 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4542 slang_print_function(fun
, 1);
4545 /* Create an end-of-function label */
4546 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4548 /* push new vartable scope */
4549 _slang_push_var_table(A
->vartable
);
4551 /* Generate IR tree for the function body code */
4552 n
= _slang_gen_operation(A
, fun
->body
);
4554 n
= new_node1(IR_SCOPE
, n
);
4556 /* pop vartable, restore previous */
4557 _slang_pop_var_table(A
->vartable
);
4560 /* XXX record error */
4564 /* append an end-of-function-label to IR tree */
4565 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4567 /*_slang_label_delete(A->curFuncEndLabel);*/
4568 A
->curFuncEndLabel
= NULL
;
4571 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4572 slang_print_function(fun
, 1);
4575 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4576 _slang_print_ir_tree(n
, 0);
4579 printf("************* End codegen function ************\n\n");
4582 /* Emit program instructions */
4583 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4584 _slang_free_ir_tree(n
);
4586 /* free codegen context */
4588 _mesa_free(A->codegen);