2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_print.h"
47 #include "shader/prog_statevars.h"
48 #include "slang_typeinfo.h"
49 #include "slang_codegen.h"
50 #include "slang_compile.h"
51 #include "slang_label.h"
52 #include "slang_mem.h"
53 #include "slang_simplify.h"
54 #include "slang_emit.h"
55 #include "slang_vartable.h"
57 #include "slang_print.h"
60 /** Max iterations to unroll */
61 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS
= 32;
63 /** Max for-loop body size (in slang operations) to unroll */
64 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE
= 50;
66 /** Max for-loop body complexity to unroll.
67 * We'll compute complexity as the product of the number of iterations
68 * and the size of the body. So long-ish loops with very simple bodies
69 * can be unrolled, as well as short loops with larger bodies.
71 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY
= 256;
75 static slang_ir_node
*
76 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
80 * Retrieves type information about an operation.
81 * Returns GL_TRUE on success.
82 * Returns GL_FALSE otherwise.
85 typeof_operation(const struct slang_assemble_ctx_
*A
,
89 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
94 is_sampler_type(const slang_fully_specified_type
*t
)
96 switch (t
->specifier
.type
) {
97 case SLANG_SPEC_SAMPLER1D
:
98 case SLANG_SPEC_SAMPLER2D
:
99 case SLANG_SPEC_SAMPLER3D
:
100 case SLANG_SPEC_SAMPLERCUBE
:
101 case SLANG_SPEC_SAMPLER1DSHADOW
:
102 case SLANG_SPEC_SAMPLER2DSHADOW
:
103 case SLANG_SPEC_SAMPLER2DRECT
:
104 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
113 * Return the offset (in floats or ints) of the named field within
114 * the given struct. Return -1 if field not found.
115 * If field is NULL, return the size of the struct instead.
118 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
122 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
123 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
124 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
126 /* types larger than 1 float are register (4-float) aligned */
127 offset
= (offset
+ 3) & ~3;
129 if (field
&& v
->a_name
== field
) {
135 return -1; /* field not found */
137 return offset
; /* struct size */
142 * Return the size (in floats) of the given type specifier.
143 * If the size is greater than 4, the size should be a multiple of 4
144 * so that the correct number of 4-float registers are allocated.
145 * For example, a mat3x2 is size 12 because we want to store the
146 * 3 columns in 3 float[4] registers.
149 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
152 switch (spec
->type
) {
153 case SLANG_SPEC_VOID
:
156 case SLANG_SPEC_BOOL
:
159 case SLANG_SPEC_BVEC2
:
162 case SLANG_SPEC_BVEC3
:
165 case SLANG_SPEC_BVEC4
:
171 case SLANG_SPEC_IVEC2
:
174 case SLANG_SPEC_IVEC3
:
177 case SLANG_SPEC_IVEC4
:
180 case SLANG_SPEC_FLOAT
:
183 case SLANG_SPEC_VEC2
:
186 case SLANG_SPEC_VEC3
:
189 case SLANG_SPEC_VEC4
:
192 case SLANG_SPEC_MAT2
:
193 sz
= 2 * 4; /* 2 columns (regs) */
195 case SLANG_SPEC_MAT3
:
198 case SLANG_SPEC_MAT4
:
201 case SLANG_SPEC_MAT23
:
202 sz
= 2 * 4; /* 2 columns (regs) */
204 case SLANG_SPEC_MAT32
:
205 sz
= 3 * 4; /* 3 columns (regs) */
207 case SLANG_SPEC_MAT24
:
210 case SLANG_SPEC_MAT42
:
211 sz
= 4 * 4; /* 4 columns (regs) */
213 case SLANG_SPEC_MAT34
:
216 case SLANG_SPEC_MAT43
:
217 sz
= 4 * 4; /* 4 columns (regs) */
219 case SLANG_SPEC_SAMPLER1D
:
220 case SLANG_SPEC_SAMPLER2D
:
221 case SLANG_SPEC_SAMPLER3D
:
222 case SLANG_SPEC_SAMPLERCUBE
:
223 case SLANG_SPEC_SAMPLER1DSHADOW
:
224 case SLANG_SPEC_SAMPLER2DSHADOW
:
225 case SLANG_SPEC_SAMPLER2DRECT
:
226 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
227 sz
= 1; /* a sampler is basically just an integer index */
229 case SLANG_SPEC_STRUCT
:
230 sz
= _slang_field_offset(spec
, 0); /* special use */
232 /* 1-float structs are actually troublesome to deal with since they
233 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
234 * ensure the object is placed at R.x
239 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
242 case SLANG_SPEC_ARRAY
:
243 sz
= _slang_sizeof_type_specifier(spec
->_array
);
246 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
251 /* if size is > 4, it should be a multiple of four */
252 assert((sz
& 0x3) == 0);
259 * Query variable/array length (number of elements).
260 * This is slightly non-trivial because there are two ways to express
261 * arrays: "float x[3]" vs. "float[3] x".
262 * \return the length of the array for the given variable, or 0 if not an array
265 _slang_array_length(const slang_variable
*var
)
267 if (var
->type
.array_len
> 0) {
268 /* Ex: float[4] x; */
269 return var
->type
.array_len
;
271 if (var
->array_len
> 0) {
272 /* Ex: float x[4]; */
273 return var
->array_len
;
280 * Compute total size of array give size of element, number of elements.
281 * \return size in floats
284 _slang_array_size(GLint elemSize
, GLint arrayLen
)
287 assert(elemSize
> 0);
289 /* round up base type to multiple of 4 */
290 total
= ((elemSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
300 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
301 * or -1 if the type is not a sampler.
304 sampler_to_texture_index(const slang_type_specifier_type type
)
307 case SLANG_SPEC_SAMPLER1D
:
308 return TEXTURE_1D_INDEX
;
309 case SLANG_SPEC_SAMPLER2D
:
310 return TEXTURE_2D_INDEX
;
311 case SLANG_SPEC_SAMPLER3D
:
312 return TEXTURE_3D_INDEX
;
313 case SLANG_SPEC_SAMPLERCUBE
:
314 return TEXTURE_CUBE_INDEX
;
315 case SLANG_SPEC_SAMPLER1DSHADOW
:
316 return TEXTURE_1D_INDEX
; /* XXX fix */
317 case SLANG_SPEC_SAMPLER2DSHADOW
:
318 return TEXTURE_2D_INDEX
; /* XXX fix */
319 case SLANG_SPEC_SAMPLER2DRECT
:
320 return TEXTURE_RECT_INDEX
;
321 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
322 return TEXTURE_RECT_INDEX
; /* XXX fix */
329 /** helper to build a SLANG_OPER_IDENTIFIER node */
331 slang_operation_identifier(slang_operation
*oper
,
332 slang_assemble_ctx
*A
,
335 oper
->type
= SLANG_OPER_IDENTIFIER
;
336 oper
->a_id
= slang_atom_pool_atom(A
->atoms
, name
);
340 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
343 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
344 * a vertex or fragment program input variable. Return -1 if the input
346 * XXX return size too
349 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
356 static const struct input_info vertInputs
[] = {
357 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
358 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
359 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
360 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
361 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
362 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
363 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
364 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
365 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
366 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
367 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
368 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
369 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
370 { NULL
, 0, SWIZZLE_NOOP
}
372 static const struct input_info fragInputs
[] = {
373 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
374 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
375 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
376 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
377 /* note: we're packing several quantities into the fogcoord vector */
378 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
379 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
380 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
381 { NULL
, 0, SWIZZLE_NOOP
}
384 const struct input_info
*inputs
385 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
387 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
389 for (i
= 0; inputs
[i
].Name
; i
++) {
390 if (strcmp(inputs
[i
].Name
, name
) == 0) {
392 *swizzleOut
= inputs
[i
].Swizzle
;
393 return inputs
[i
].Attrib
;
401 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
402 * a vertex or fragment program output variable. Return -1 for an invalid
406 _slang_output_index(const char *name
, GLenum target
)
412 static const struct output_info vertOutputs
[] = {
413 { "gl_Position", VERT_RESULT_HPOS
},
414 { "gl_FrontColor", VERT_RESULT_COL0
},
415 { "gl_BackColor", VERT_RESULT_BFC0
},
416 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
417 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
418 { "gl_TexCoord", VERT_RESULT_TEX0
},
419 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
420 { "gl_PointSize", VERT_RESULT_PSIZ
},
423 static const struct output_info fragOutputs
[] = {
424 { "gl_FragColor", FRAG_RESULT_COLOR
},
425 { "gl_FragDepth", FRAG_RESULT_DEPTH
},
426 { "gl_FragData", FRAG_RESULT_DATA0
},
430 const struct output_info
*outputs
431 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
433 for (i
= 0; outputs
[i
].Name
; i
++) {
434 if (strcmp(outputs
[i
].Name
, name
) == 0) {
436 return outputs
[i
].Attrib
;
444 /**********************************************************************/
448 * Map "_asm foo" to IR_FOO, etc.
453 slang_ir_opcode Opcode
;
454 GLuint HaveRetValue
, NumParams
;
458 static slang_asm_info AsmInfo
[] = {
460 { "vec4_add", IR_ADD
, 1, 2 },
461 { "vec4_subtract", IR_SUB
, 1, 2 },
462 { "vec4_multiply", IR_MUL
, 1, 2 },
463 { "vec4_dot", IR_DOT4
, 1, 2 },
464 { "vec3_dot", IR_DOT3
, 1, 2 },
465 { "vec2_dot", IR_DOT2
, 1, 2 },
466 { "vec3_nrm", IR_NRM3
, 1, 1 },
467 { "vec4_nrm", IR_NRM4
, 1, 1 },
468 { "vec3_cross", IR_CROSS
, 1, 2 },
469 { "vec4_lrp", IR_LRP
, 1, 3 },
470 { "vec4_min", IR_MIN
, 1, 2 },
471 { "vec4_max", IR_MAX
, 1, 2 },
472 { "vec4_clamp", IR_CLAMP
, 1, 3 },
473 { "vec4_seq", IR_SEQUAL
, 1, 2 },
474 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
475 { "vec4_sge", IR_SGE
, 1, 2 },
476 { "vec4_sgt", IR_SGT
, 1, 2 },
477 { "vec4_sle", IR_SLE
, 1, 2 },
478 { "vec4_slt", IR_SLT
, 1, 2 },
480 { "vec4_move", IR_MOVE
, 1, 1 },
481 { "vec4_floor", IR_FLOOR
, 1, 1 },
482 { "vec4_frac", IR_FRAC
, 1, 1 },
483 { "vec4_abs", IR_ABS
, 1, 1 },
484 { "vec4_negate", IR_NEG
, 1, 1 },
485 { "vec4_ddx", IR_DDX
, 1, 1 },
486 { "vec4_ddy", IR_DDY
, 1, 1 },
487 /* float binary op */
488 { "float_power", IR_POW
, 1, 2 },
489 /* texture / sampler */
490 { "vec4_tex_1d", IR_TEX
, 1, 2 },
491 { "vec4_tex_1d_bias", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
492 { "vec4_tex_1d_proj", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
493 { "vec4_tex_2d", IR_TEX
, 1, 2 },
494 { "vec4_tex_2d_bias", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
495 { "vec4_tex_2d_proj", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
496 { "vec4_tex_3d", IR_TEX
, 1, 2 },
497 { "vec4_tex_3d_bias", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
498 { "vec4_tex_3d_proj", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
499 { "vec4_tex_cube", IR_TEX
, 1, 2 }, /* cubemap */
500 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
501 { "vec4_tex_rect_bias", IR_TEX
, 1, 2 }, /* rectangle w/ projection */
503 /* texture / sampler but with shadow comparison */
504 { "vec4_tex_1d_shadow", IR_TEX_SH
, 1, 2 },
505 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH
, 1, 2 },
506 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH
, 1, 2 },
507 { "vec4_tex_2d_shadow", IR_TEX_SH
, 1, 2 },
508 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH
, 1, 2 },
509 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH
, 1, 2 },
510 { "vec4_tex_rect_shadow", IR_TEX_SH
, 1, 2 },
511 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH
, 1, 2 },
514 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
515 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
516 { "float_exp", IR_EXP
, 1, 1 },
517 { "float_exp2", IR_EXP2
, 1, 1 },
518 { "float_log2", IR_LOG2
, 1, 1 },
519 { "float_rsq", IR_RSQ
, 1, 1 },
520 { "float_rcp", IR_RCP
, 1, 1 },
521 { "float_sine", IR_SIN
, 1, 1 },
522 { "float_cosine", IR_COS
, 1, 1 },
523 { "float_noise1", IR_NOISE1
, 1, 1},
524 { "float_noise2", IR_NOISE2
, 1, 1},
525 { "float_noise3", IR_NOISE3
, 1, 1},
526 { "float_noise4", IR_NOISE4
, 1, 1},
528 { NULL
, IR_NOP
, 0, 0 }
532 static slang_ir_node
*
533 new_node3(slang_ir_opcode op
,
534 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
536 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
542 n
->InstLocation
= -1;
547 static slang_ir_node
*
548 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
550 return new_node3(op
, c0
, c1
, NULL
);
553 static slang_ir_node
*
554 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
556 return new_node3(op
, c0
, NULL
, NULL
);
559 static slang_ir_node
*
560 new_node0(slang_ir_opcode op
)
562 return new_node3(op
, NULL
, NULL
, NULL
);
567 * Create sequence of two nodes.
569 static slang_ir_node
*
570 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
576 return new_node2(IR_SEQ
, left
, right
);
579 static slang_ir_node
*
580 new_label(slang_label
*label
)
582 slang_ir_node
*n
= new_node0(IR_LABEL
);
589 static slang_ir_node
*
590 new_float_literal(const float v
[4], GLuint size
)
592 slang_ir_node
*n
= new_node0(IR_FLOAT
);
594 COPY_4V(n
->Value
, v
);
595 /* allocate a storage object, but compute actual location (Index) later */
596 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
601 static slang_ir_node
*
602 new_not(slang_ir_node
*n
)
604 return new_node1(IR_NOT
, n
);
609 * Non-inlined function call.
611 static slang_ir_node
*
612 new_function_call(slang_ir_node
*code
, slang_label
*name
)
614 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
623 * Unconditional jump.
625 static slang_ir_node
*
626 new_return(slang_label
*dest
)
628 slang_ir_node
*n
= new_node0(IR_RETURN
);
636 static slang_ir_node
*
637 new_loop(slang_ir_node
*body
)
639 return new_node1(IR_LOOP
, body
);
643 static slang_ir_node
*
644 new_break(slang_ir_node
*loopNode
)
646 slang_ir_node
*n
= new_node0(IR_BREAK
);
648 assert(loopNode
->Opcode
== IR_LOOP
);
650 /* insert this node at head of linked list of cont/break instructions */
651 n
->List
= loopNode
->List
;
659 * Make new IR_BREAK_IF_TRUE.
661 static slang_ir_node
*
662 new_break_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
664 slang_ir_node
*loopNode
= A
->CurLoop
;
667 assert(loopNode
->Opcode
== IR_LOOP
);
668 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
670 /* insert this node at head of linked list of cont/break instructions */
671 n
->List
= loopNode
->List
;
679 * Make new IR_CONT_IF_TRUE node.
681 static slang_ir_node
*
682 new_cont_if_true(slang_assemble_ctx
*A
, slang_ir_node
*cond
)
684 slang_ir_node
*loopNode
= A
->CurLoop
;
687 assert(loopNode
->Opcode
== IR_LOOP
);
688 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
690 n
->Parent
= loopNode
; /* pointer to containing loop */
691 /* insert this node at head of linked list of cont/break instructions */
692 n
->List
= loopNode
->List
;
699 static slang_ir_node
*
700 new_cond(slang_ir_node
*n
)
702 slang_ir_node
*c
= new_node1(IR_COND
, n
);
707 static slang_ir_node
*
708 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
710 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
715 * New IR_VAR node - a reference to a previously declared variable.
717 static slang_ir_node
*
718 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
720 slang_ir_node
*n
= new_node0(IR_VAR
);
727 /* Set IR node's Var and Store pointers */
729 n
->Store
= var
->store
;
736 * Check if the given function is really just a wrapper for a
737 * basic assembly instruction.
740 slang_is_asm_function(const slang_function
*fun
)
742 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
743 fun
->body
->num_children
== 1 &&
744 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
752 _slang_is_noop(const slang_operation
*oper
)
755 oper
->type
== SLANG_OPER_VOID
||
756 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
764 * Recursively search tree for a node of the given type.
766 static slang_operation
*
767 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
770 if (oper
->type
== type
)
772 for (i
= 0; i
< oper
->num_children
; i
++) {
773 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
782 * Count the number of operations of the given time rooted at 'oper'.
785 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
788 if (oper
->type
== type
) {
791 for (i
= 0; i
< oper
->num_children
; i
++) {
792 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
799 * Check if the 'return' statement found under 'oper' is a "tail return"
800 * that can be no-op'd. For example:
805 * return; // this is a no-op
808 * This is used when determining if a function can be inlined. If the
809 * 'return' is not the last statement, we can't inline the function since
810 * we still need the semantic behaviour of the 'return' but we don't want
811 * to accidentally return from the _calling_ function. We'd need to use an
812 * unconditional branch, but we don't have such a GPU instruction (not
816 _slang_is_tail_return(const slang_operation
*oper
)
818 GLuint k
= oper
->num_children
;
821 const slang_operation
*last
= &oper
->children
[k
- 1];
822 if (last
->type
== SLANG_OPER_RETURN
)
824 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
825 last
->type
== SLANG_OPER_LABEL
)
826 k
--; /* try prev child */
827 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
828 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
829 /* try sub-children */
830 return _slang_is_tail_return(last
);
840 * Generate a variable declaration opeartion.
841 * I.e.: generate AST code for "bool flag = false;"
844 slang_generate_declaration(slang_assemble_ctx
*A
,
845 slang_variable_scope
*scope
,
846 slang_operation
*decl
,
847 slang_type_specifier_type type
,
853 assert(type
== SLANG_SPEC_BOOL
||
854 type
== SLANG_SPEC_INT
);
856 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
858 var
= slang_variable_scope_grow(scope
);
860 slang_fully_specified_type_construct(&var
->type
);
862 var
->type
.specifier
.type
= type
;
863 var
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
864 decl
->a_id
= var
->a_name
;
865 var
->initializer
= slang_operation_new(1);
866 slang_operation_literal_bool(var
->initializer
, initValue
);
871 slang_resolve_variable(slang_operation
*oper
)
873 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
874 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
880 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
883 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
884 GLuint substCount
, slang_variable
**substOld
,
885 slang_operation
**substNew
, GLboolean isLHS
)
887 switch (oper
->type
) {
888 case SLANG_OPER_VARIABLE_DECL
:
890 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
891 oper
->a_id
, GL_TRUE
);
893 if (v
->initializer
&& oper
->num_children
== 0) {
894 /* set child of oper to copy of initializer */
895 oper
->num_children
= 1;
896 oper
->children
= slang_operation_new(1);
897 slang_operation_copy(&oper
->children
[0], v
->initializer
);
899 if (oper
->num_children
== 1) {
900 /* the initializer */
901 slang_substitute(A
, &oper
->children
[0], substCount
,
902 substOld
, substNew
, GL_FALSE
);
906 case SLANG_OPER_IDENTIFIER
:
907 assert(oper
->num_children
== 0);
908 if (1/**!isLHS XXX FIX */) {
909 slang_atom id
= oper
->a_id
;
912 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
914 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
918 /* look for a substitution */
919 for (i
= 0; i
< substCount
; i
++) {
920 if (v
== substOld
[i
]) {
921 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
922 #if 0 /* DEBUG only */
923 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
924 assert(substNew
[i
]->var
);
925 assert(substNew
[i
]->var
->a_name
);
926 printf("Substitute %s with %s in id node %p\n",
927 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
931 printf("Substitute %s with %f in id node %p\n",
932 (char*)v
->a_name
, substNew
[i
]->literal
[0],
936 slang_operation_copy(oper
, substNew
[i
]);
943 case SLANG_OPER_RETURN
:
944 /* do return replacement here too */
945 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
946 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
952 * then do substitutions on the assignment.
954 slang_operation
*blockOper
, *assignOper
, *returnOper
;
956 /* check if function actually has a return type */
957 assert(A
->CurFunction
);
958 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
959 slang_info_log_error(A
->log
, "illegal return expression");
963 blockOper
= slang_operation_new(1);
964 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
965 blockOper
->num_children
= 2;
966 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
967 blockOper
->children
= slang_operation_new(2);
968 assignOper
= blockOper
->children
+ 0;
969 returnOper
= blockOper
->children
+ 1;
971 assignOper
->type
= SLANG_OPER_ASSIGN
;
972 assignOper
->num_children
= 2;
973 assignOper
->locals
->outer_scope
= blockOper
->locals
;
974 assignOper
->children
= slang_operation_new(2);
975 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
976 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
977 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
979 slang_operation_copy(&assignOper
->children
[1],
982 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
983 assert(returnOper
->num_children
== 0);
985 /* do substitutions on the "__retVal = expr" sub-tree */
986 slang_substitute(A
, assignOper
,
987 substCount
, substOld
, substNew
, GL_FALSE
);
989 /* install new code */
990 slang_operation_copy(oper
, blockOper
);
991 slang_operation_destruct(blockOper
);
994 /* check if return value was expected */
995 assert(A
->CurFunction
);
996 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
997 slang_info_log_error(A
->log
, "return statement requires an expression");
1003 case SLANG_OPER_ASSIGN
:
1004 case SLANG_OPER_SUBSCRIPT
:
1006 * child[0] can't have substitutions but child[1] can.
1008 slang_substitute(A
, &oper
->children
[0],
1009 substCount
, substOld
, substNew
, GL_TRUE
);
1010 slang_substitute(A
, &oper
->children
[1],
1011 substCount
, substOld
, substNew
, GL_FALSE
);
1013 case SLANG_OPER_FIELD
:
1014 /* XXX NEW - test */
1015 slang_substitute(A
, &oper
->children
[0],
1016 substCount
, substOld
, substNew
, GL_TRUE
);
1021 for (i
= 0; i
< oper
->num_children
; i
++)
1022 slang_substitute(A
, &oper
->children
[i
],
1023 substCount
, substOld
, substNew
, GL_FALSE
);
1030 * Produce inline code for a call to an assembly instruction.
1031 * This is typically used to compile a call to a built-in function like this:
1033 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1035 * __asm vec4_lrp __retVal, a, y, x;
1040 * r = mix(p1, p2, p3);
1050 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1052 static slang_operation
*
1053 slang_inline_asm_function(slang_assemble_ctx
*A
,
1054 slang_function
*fun
, slang_operation
*oper
)
1056 const GLuint numArgs
= oper
->num_children
;
1058 slang_operation
*inlined
;
1059 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1060 slang_variable
**substOld
;
1061 slang_operation
**substNew
;
1063 ASSERT(slang_is_asm_function(fun
));
1064 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
1067 printf("Inline %s as %s\n",
1068 (char*) fun->header.a_name,
1069 (char*) fun->body->children[0].a_id);
1073 * We'll substitute formal params with actual args in the asm call.
1075 substOld
= (slang_variable
**)
1076 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1077 substNew
= (slang_operation
**)
1078 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1079 for (i
= 0; i
< numArgs
; i
++) {
1080 substOld
[i
] = fun
->parameters
->variables
[i
];
1081 substNew
[i
] = oper
->children
+ i
;
1084 /* make a copy of the code to inline */
1085 inlined
= slang_operation_new(1);
1086 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1088 /* get rid of the __retVal child */
1089 inlined
->num_children
--;
1090 for (i
= 0; i
< inlined
->num_children
; i
++) {
1091 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1095 /* now do formal->actual substitutions */
1096 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1098 _slang_free(substOld
);
1099 _slang_free(substNew
);
1102 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1103 (char *) fun
->header
.a_name
);
1104 slang_print_tree(inlined
, 3);
1105 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1113 * Inline the given function call operation.
1114 * Return a new slang_operation that corresponds to the inlined code.
1116 static slang_operation
*
1117 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1118 slang_operation
*oper
, slang_operation
*returnOper
)
1125 ParamMode
*paramMode
;
1126 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1127 const GLuint numArgs
= oper
->num_children
;
1128 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1129 slang_operation
*args
= oper
->children
;
1130 slang_operation
*inlined
, *top
;
1131 slang_variable
**substOld
;
1132 slang_operation
**substNew
;
1133 GLuint substCount
, numCopyIn
, i
;
1134 slang_function
*prevFunction
;
1135 slang_variable_scope
*newScope
= NULL
;
1138 prevFunction
= A
->CurFunction
;
1139 A
->CurFunction
= fun
;
1141 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1142 assert(fun
->param_count
== totalArgs
);
1144 /* allocate temporary arrays */
1145 paramMode
= (ParamMode
*)
1146 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1147 substOld
= (slang_variable
**)
1148 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1149 substNew
= (slang_operation
**)
1150 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1153 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1154 (char *) fun
->header
.a_name
,
1155 fun
->parameters
->num_variables
, numArgs
);
1158 if (haveRetValue
&& !returnOper
) {
1159 /* Create 3-child comma sequence for inlined code:
1160 * child[0]: declare __resultTmp
1161 * child[1]: inlined function body
1162 * child[2]: __resultTmp
1164 slang_operation
*commaSeq
;
1165 slang_operation
*declOper
= NULL
;
1166 slang_variable
*resultVar
;
1168 commaSeq
= slang_operation_new(1);
1169 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1170 assert(commaSeq
->locals
);
1171 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1172 commaSeq
->num_children
= 3;
1173 commaSeq
->children
= slang_operation_new(3);
1174 /* allocate the return var */
1175 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1177 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1178 (void*)commaSeq->locals, (char *) fun->header.a_name);
1181 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1182 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1183 resultVar
->isTemp
= GL_TRUE
;
1185 /* child[0] = __resultTmp declaration */
1186 declOper
= &commaSeq
->children
[0];
1187 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1188 declOper
->a_id
= resultVar
->a_name
;
1189 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1191 /* child[1] = function body */
1192 inlined
= &commaSeq
->children
[1];
1193 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1195 /* child[2] = __resultTmp reference */
1196 returnOper
= &commaSeq
->children
[2];
1197 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1198 returnOper
->a_id
= resultVar
->a_name
;
1199 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1204 top
= inlined
= slang_operation_new(1);
1205 /* XXXX this may be inappropriate!!!! */
1206 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1210 assert(inlined
->locals
);
1212 /* Examine the parameters, look for inout/out params, look for possible
1213 * substitutions, etc:
1214 * param type behaviour
1215 * in copy actual to local
1216 * const in substitute param with actual
1220 for (i
= 0; i
< totalArgs
; i
++) {
1221 slang_variable
*p
= fun
->parameters
->variables
[i
];
1223 printf("Param %d: %s %s \n", i,
1224 slang_type_qual_string(p->type.qualifier),
1225 (char *) p->a_name);
1227 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1228 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1229 /* an output param */
1230 slang_operation
*arg
;
1235 paramMode
[i
] = SUBST
;
1237 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1238 slang_resolve_variable(arg
);
1240 /* replace parameter 'p' with argument 'arg' */
1241 substOld
[substCount
] = p
;
1242 substNew
[substCount
] = arg
; /* will get copied */
1245 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1246 /* a constant input param */
1247 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1248 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1249 /* replace all occurances of this parameter variable with the
1250 * actual argument variable or a literal.
1252 paramMode
[i
] = SUBST
;
1253 slang_resolve_variable(&args
[i
]);
1254 substOld
[substCount
] = p
;
1255 substNew
[substCount
] = &args
[i
]; /* will get copied */
1259 paramMode
[i
] = COPY_IN
;
1263 paramMode
[i
] = COPY_IN
;
1265 assert(paramMode
[i
]);
1268 /* actual code inlining: */
1269 slang_operation_copy(inlined
, fun
->body
);
1271 /*** XXX review this */
1272 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1273 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1274 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1277 printf("======================= orig body code ======================\n");
1278 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1279 slang_print_tree(fun
->body
, 8);
1280 printf("======================= copied code =========================\n");
1281 slang_print_tree(inlined
, 8);
1284 /* do parameter substitution in inlined code: */
1285 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1288 printf("======================= subst code ==========================\n");
1289 slang_print_tree(inlined
, 8);
1290 printf("=============================================================\n");
1293 /* New prolog statements: (inserted before the inlined code)
1294 * Copy the 'in' arguments.
1297 for (i
= 0; i
< numArgs
; i
++) {
1298 if (paramMode
[i
] == COPY_IN
) {
1299 slang_variable
*p
= fun
->parameters
->variables
[i
];
1300 /* declare parameter 'p' */
1301 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1305 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1306 assert(decl
->locals
);
1307 decl
->locals
->outer_scope
= inlined
->locals
;
1308 decl
->a_id
= p
->a_name
;
1309 decl
->num_children
= 1;
1310 decl
->children
= slang_operation_new(1);
1312 /* child[0] is the var's initializer */
1313 slang_operation_copy(&decl
->children
[0], args
+ i
);
1315 /* add parameter 'p' to the local variable scope here */
1317 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1318 pCopy
->type
= p
->type
;
1319 pCopy
->a_name
= p
->a_name
;
1320 pCopy
->array_len
= p
->array_len
;
1323 newScope
= inlined
->locals
;
1328 /* Now add copies of the function's local vars to the new variable scope */
1329 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1330 slang_variable
*p
= fun
->parameters
->variables
[i
];
1331 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1332 pCopy
->type
= p
->type
;
1333 pCopy
->a_name
= p
->a_name
;
1334 pCopy
->array_len
= p
->array_len
;
1338 /* New epilog statements:
1339 * 1. Create end of function label to jump to from return statements.
1340 * 2. Copy the 'out' parameter vars
1343 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1345 inlined
->num_children
);
1346 lab
->type
= SLANG_OPER_LABEL
;
1347 lab
->label
= A
->curFuncEndLabel
;
1350 for (i
= 0; i
< totalArgs
; i
++) {
1351 if (paramMode
[i
] == COPY_OUT
) {
1352 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1353 /* actualCallVar = outParam */
1354 /*if (i > 0 || !haveRetValue)*/
1355 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1357 inlined
->num_children
);
1358 ass
->type
= SLANG_OPER_ASSIGN
;
1359 ass
->num_children
= 2;
1360 ass
->locals
->outer_scope
= inlined
->locals
;
1361 ass
->children
= slang_operation_new(2);
1362 ass
->children
[0] = args
[i
]; /*XXX copy */
1363 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1364 ass
->children
[1].a_id
= p
->a_name
;
1365 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1369 _slang_free(paramMode
);
1370 _slang_free(substOld
);
1371 _slang_free(substNew
);
1373 /* Update scoping to use the new local vars instead of the
1374 * original function's vars. This is especially important
1375 * for nested inlining.
1378 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1381 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1382 (char *) fun
->header
.a_name
,
1383 fun
->parameters
->num_variables
, numArgs
);
1384 slang_print_tree(top
, 0);
1388 A
->CurFunction
= prevFunction
;
1395 * Insert declaration for "bool _returnFlag" in given block operation.
1396 * This is used when we can't emit "early" return statements in subroutines.
1399 declare_return_flag(slang_assemble_ctx
*A
, slang_operation
*oper
)
1401 slang_operation
*decl
;
1403 assert(oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1404 oper
->type
== SLANG_OPER_SEQUENCE
);
1406 decl
= slang_operation_insert_child(oper
, 1);
1408 slang_generate_declaration(A
, oper
->locals
, decl
,
1409 SLANG_SPEC_BOOL
, "_returnFlag", GL_FALSE
);
1411 slang_print_tree(oper
, 0);
1415 static slang_ir_node
*
1416 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1417 slang_operation
*oper
, slang_operation
*dest
)
1420 slang_operation
*inlined
;
1421 slang_label
*prevFuncEndLabel
;
1424 prevFuncEndLabel
= A
->curFuncEndLabel
;
1425 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1426 A
->curFuncEndLabel
= _slang_label_new(name
);
1427 assert(A
->curFuncEndLabel
);
1429 if (slang_is_asm_function(fun
) && !dest
) {
1430 /* assemble assembly function - tree style */
1431 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1434 /* non-assembly function */
1435 /* We always generate an "inline-able" block of code here.
1437 * 1. insert the inline code
1438 * 2. Generate a call to the "inline" code as a subroutine
1442 slang_operation
*ret
= NULL
;
1444 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1448 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1450 /* check if this is a "tail" return */
1451 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1452 _slang_is_tail_return(inlined
)) {
1453 /* The only RETURN is the last stmt in the function, no-op it
1454 * and inline the function body.
1456 ret
->type
= SLANG_OPER_NONE
;
1459 slang_operation
*callOper
;
1460 /* The function we're calling has one or more 'return' statements.
1461 * So, we can't truly inline this function because we need to
1462 * implement 'return' with RET (and CAL).
1463 * Nevertheless, we performed "inlining" to make a new instance
1464 * of the function body to deal with static register allocation.
1466 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1467 inlined
->type
== SLANG_OPER_SEQUENCE
);
1469 if (_slang_function_has_return_value(fun
) && !dest
) {
1470 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1471 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1472 callOper
= &inlined
->children
[1];
1478 if (!A
->EmitContReturn
) {
1479 /* Early returns not supported. Create a _returnFlag variable
1480 * that's set upon 'return' and tested elsewhere to no-op any
1481 * remaining instructions in the subroutine.
1483 assert(callOper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1484 callOper
->type
== SLANG_OPER_SEQUENCE
);
1485 declare_return_flag(A
, callOper
);
1486 printf("DECLARE _returnFlag\n");
1489 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1490 callOper
->fun
= fun
;
1491 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1499 /* Replace the function call with the inlined block (or new CALL stmt) */
1500 slang_operation_destruct(oper
);
1502 _slang_free(inlined
);
1505 assert(inlined
->locals
);
1506 printf("*** Inlined code for call to %s:\n",
1507 (char*) fun
->header
.a_name
);
1508 slang_print_tree(oper
, 10);
1512 n
= _slang_gen_operation(A
, oper
);
1514 /*_slang_label_delete(A->curFuncEndLabel);*/
1515 A
->curFuncEndLabel
= prevFuncEndLabel
;
1517 if (A
->pragmas
->Debug
) {
1519 _mesa_snprintf(s
, sizeof(s
), "Call/inline %s()", (char *) fun
->header
.a_name
);
1520 n
->Comment
= _slang_strdup(s
);
1527 static slang_asm_info
*
1528 slang_find_asm_info(const char *name
)
1531 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1532 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1541 * Some write-masked assignments are simple, but others are hard.
1544 * v.xy = vec2(a, b);
1547 * v.zy = vec2(a, b);
1548 * this gets transformed/swizzled into:
1549 * v.zy = vec2(a, b).*yx* (* = don't care)
1550 * This function helps to determine simple vs. non-simple.
1553 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1555 switch (writemask
) {
1557 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1559 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1561 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1563 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1565 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1566 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1568 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1569 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1570 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1571 case WRITEMASK_XYZW
:
1572 return swizzle
== SWIZZLE_NOOP
;
1580 * Convert the given swizzle into a writemask. In some cases this
1581 * is trivial, in other cases, we'll need to also swizzle the right
1582 * hand side to put components in the right places.
1583 * See comment above for more info.
1584 * XXX this function could be simplified and should probably be renamed.
1585 * \param swizzle the incoming swizzle
1586 * \param writemaskOut returns the writemask
1587 * \param swizzleOut swizzle to apply to the right-hand-side
1588 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1591 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1592 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1594 GLuint mask
= 0x0, newSwizzle
[4];
1597 /* make new dst writemask, compute size */
1598 for (i
= 0; i
< 4; i
++) {
1599 const GLuint swz
= GET_SWZ(swizzle
, i
);
1600 if (swz
== SWIZZLE_NIL
) {
1604 assert(swz
>= 0 && swz
<= 3);
1606 if (swizzle
!= SWIZZLE_XXXX
&&
1607 swizzle
!= SWIZZLE_YYYY
&&
1608 swizzle
!= SWIZZLE_ZZZZ
&&
1609 swizzle
!= SWIZZLE_WWWW
&&
1610 (mask
& (1 << swz
))) {
1611 /* a channel can't be specified twice (ex: ".xyyz") */
1612 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1613 _mesa_swizzle_string(swizzle
, 0, 0));
1619 assert(mask
<= 0xf);
1620 size
= i
; /* number of components in mask/swizzle */
1622 *writemaskOut
= mask
;
1624 /* make new src swizzle, by inversion */
1625 for (i
= 0; i
< 4; i
++) {
1626 newSwizzle
[i
] = i
; /*identity*/
1628 for (i
= 0; i
< size
; i
++) {
1629 const GLuint swz
= GET_SWZ(swizzle
, i
);
1630 newSwizzle
[swz
] = i
;
1632 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1637 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1639 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1641 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1643 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1645 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1653 #if 0 /* not used, but don't remove just yet */
1655 * Recursively traverse 'oper' to produce a swizzle mask in the event
1656 * of any vector subscripts and swizzle suffixes.
1657 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1660 resolve_swizzle(const slang_operation
*oper
)
1662 if (oper
->type
== SLANG_OPER_FIELD
) {
1663 /* writemask from .xyzw suffix */
1665 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1666 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1670 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1671 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1675 return SWIZZLE_XYZW
;
1677 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1678 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1679 /* writemask from [index] */
1680 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1681 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1686 swizzle
= SWIZZLE_XXXX
;
1689 swizzle
= SWIZZLE_YYYY
;
1692 swizzle
= SWIZZLE_ZZZZ
;
1695 swizzle
= SWIZZLE_WWWW
;
1698 swizzle
= SWIZZLE_XYZW
;
1700 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1704 return SWIZZLE_XYZW
;
1712 * Recursively descend through swizzle nodes to find the node's storage info.
1714 static slang_ir_storage
*
1715 get_store(const slang_ir_node
*n
)
1717 if (n
->Opcode
== IR_SWIZZLE
) {
1718 return get_store(n
->Children
[0]);
1726 * Generate IR tree for an asm instruction/operation such as:
1727 * __asm vec4_dot __retVal.x, v1, v2;
1729 static slang_ir_node
*
1730 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1731 slang_operation
*dest
)
1733 const slang_asm_info
*info
;
1734 slang_ir_node
*kids
[3], *n
;
1735 GLuint j
, firstOperand
;
1737 assert(oper
->type
== SLANG_OPER_ASM
);
1739 info
= slang_find_asm_info((char *) oper
->a_id
);
1741 _mesa_problem(NULL
, "undefined __asm function %s\n",
1742 (char *) oper
->a_id
);
1745 assert(info
->NumParams
<= 3);
1747 if (info
->NumParams
== oper
->num_children
) {
1748 /* Storage for result is not specified.
1749 * Children[0], [1], [2] are the operands.
1754 /* Storage for result (child[0]) is specified.
1755 * Children[1], [2], [3] are the operands.
1760 /* assemble child(ren) */
1761 kids
[0] = kids
[1] = kids
[2] = NULL
;
1762 for (j
= 0; j
< info
->NumParams
; j
++) {
1763 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1768 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1771 /* Setup n->Store to be a particular location. Otherwise, storage
1772 * for the result (a temporary) will be allocated later.
1774 slang_operation
*dest_oper
;
1777 dest_oper
= &oper
->children
[0];
1779 n0
= _slang_gen_operation(A
, dest_oper
);
1784 n
->Store
= n0
->Store
;
1786 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1797 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1800 for (i
= 0; i
< scope
->num_functions
; i
++) {
1801 slang_function
*f
= &scope
->functions
[i
];
1802 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1803 printf(" %s (%d args)\n", name
, f
->param_count
);
1806 if (scope
->outer_scope
)
1807 print_funcs(scope
->outer_scope
, name
);
1813 * Find a function of the given name, taking 'numArgs' arguments.
1814 * This is the function we'll try to call when there is no exact match
1815 * between function parameters and call arguments.
1817 * XXX we should really create a list of candidate functions and try
1820 static slang_function
*
1821 _slang_find_function_by_argc(slang_function_scope
*scope
,
1822 const char *name
, int numArgs
)
1826 for (i
= 0; i
< scope
->num_functions
; i
++) {
1827 slang_function
*f
= &scope
->functions
[i
];
1828 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1829 int haveRetValue
= _slang_function_has_return_value(f
);
1830 if (numArgs
== f
->param_count
- haveRetValue
)
1834 scope
= scope
->outer_scope
;
1841 static slang_function
*
1842 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1845 slang_function
*maxFunc
= NULL
;
1850 for (i
= 0; i
< scope
->num_functions
; i
++) {
1851 slang_function
*f
= &scope
->functions
[i
];
1852 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1853 if (f
->param_count
> maxArgs
) {
1854 maxArgs
= f
->param_count
;
1859 scope
= scope
->outer_scope
;
1867 * Generate a new slang_function which is a constructor for a user-defined
1870 static slang_function
*
1871 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1873 const GLint numFields
= str
->fields
->num_variables
;
1874 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1876 /* function header (name, return type) */
1877 fun
->header
.a_name
= str
->a_name
;
1878 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1879 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1880 fun
->header
.type
.specifier
._struct
= str
;
1882 /* function parameters (= struct's fields) */
1885 for (i
= 0; i
< numFields
; i
++) {
1887 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1889 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1890 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1891 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1893 fun
->param_count
= fun
->parameters
->num_variables
;
1896 /* Add __retVal to params */
1898 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1899 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1901 p
->a_name
= a_retVal
;
1902 p
->type
= fun
->header
.type
;
1903 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1907 /* function body is:
1917 slang_variable_scope
*scope
;
1918 slang_variable
*var
;
1921 fun
->body
= slang_operation_new(1);
1922 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1923 fun
->body
->num_children
= numFields
+ 2;
1924 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1926 scope
= fun
->body
->locals
;
1927 scope
->outer_scope
= fun
->parameters
;
1929 /* create local var 't' */
1930 var
= slang_variable_scope_grow(scope
);
1931 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1932 var
->type
= fun
->header
.type
;
1936 slang_operation
*decl
;
1938 decl
= &fun
->body
->children
[0];
1939 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1940 decl
->locals
= _slang_variable_scope_new(scope
);
1941 decl
->a_id
= var
->a_name
;
1944 /* assign params to fields of t */
1945 for (i
= 0; i
< numFields
; i
++) {
1946 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1948 assign
->type
= SLANG_OPER_ASSIGN
;
1949 assign
->locals
= _slang_variable_scope_new(scope
);
1950 assign
->num_children
= 2;
1951 assign
->children
= slang_operation_new(2);
1954 slang_operation
*lhs
= &assign
->children
[0];
1956 lhs
->type
= SLANG_OPER_FIELD
;
1957 lhs
->locals
= _slang_variable_scope_new(scope
);
1958 lhs
->num_children
= 1;
1959 lhs
->children
= slang_operation_new(1);
1960 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1962 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1963 lhs
->children
[0].a_id
= var
->a_name
;
1964 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1967 lhs
->children
[1].num_children
= 1;
1968 lhs
->children
[1].children
= slang_operation_new(1);
1969 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1970 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1971 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1976 slang_operation
*rhs
= &assign
->children
[1];
1978 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1979 rhs
->locals
= _slang_variable_scope_new(scope
);
1980 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1986 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1988 ret
->type
= SLANG_OPER_RETURN
;
1989 ret
->locals
= _slang_variable_scope_new(scope
);
1990 ret
->num_children
= 1;
1991 ret
->children
= slang_operation_new(1);
1992 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1993 ret
->children
[0].a_id
= var
->a_name
;
1994 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1998 slang_print_function(fun, 1);
2005 * Find/create a function (constructor) for the given structure name.
2007 static slang_function
*
2008 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
2011 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
2012 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
2013 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
2014 /* found a structure type that matches the function name */
2015 if (!str
->constructor
) {
2016 /* create the constructor function now */
2017 str
->constructor
= _slang_make_struct_constructor(A
, str
);
2019 return str
->constructor
;
2027 * Generate a new slang_function to satisfy a call to an array constructor.
2028 * Ex: float[3](1., 2., 3.)
2030 static slang_function
*
2031 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
2033 slang_type_specifier_type baseType
;
2034 slang_function
*fun
;
2037 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
2041 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
2043 num_elements
= oper
->num_children
;
2045 /* function header, return type */
2047 fun
->header
.a_name
= oper
->a_id
;
2048 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
2049 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
2050 fun
->header
.type
.specifier
._array
=
2051 slang_type_specifier_new(baseType
, NULL
, NULL
);
2052 fun
->header
.type
.array_len
= num_elements
;
2055 /* function parameters (= number of elements) */
2058 for (i
= 0; i
< num_elements
; i
++) {
2060 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2062 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2064 _mesa_snprintf(name
, sizeof(name
), "p%d", i
);
2065 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
2066 p
->type
.qualifier
= SLANG_QUAL_CONST
;
2067 p
->type
.specifier
.type
= baseType
;
2069 fun
->param_count
= fun
->parameters
->num_variables
;
2072 /* Add __retVal to params */
2074 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
2075 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2077 p
->a_name
= a_retVal
;
2078 p
->type
= fun
->header
.type
;
2079 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2080 p
->type
.specifier
.type
= baseType
;
2084 /* function body is:
2094 slang_variable_scope
*scope
;
2095 slang_variable
*var
;
2098 fun
->body
= slang_operation_new(1);
2099 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2100 fun
->body
->num_children
= num_elements
+ 2;
2101 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2103 scope
= fun
->body
->locals
;
2104 scope
->outer_scope
= fun
->parameters
;
2106 /* create local var 't' */
2107 var
= slang_variable_scope_grow(scope
);
2108 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2109 var
->type
= fun
->header
.type
;/*XXX copy*/
2113 slang_operation
*decl
;
2115 decl
= &fun
->body
->children
[0];
2116 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2117 decl
->locals
= _slang_variable_scope_new(scope
);
2118 decl
->a_id
= var
->a_name
;
2121 /* assign params to elements of t */
2122 for (i
= 0; i
< num_elements
; i
++) {
2123 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2125 assign
->type
= SLANG_OPER_ASSIGN
;
2126 assign
->locals
= _slang_variable_scope_new(scope
);
2127 assign
->num_children
= 2;
2128 assign
->children
= slang_operation_new(2);
2131 slang_operation
*lhs
= &assign
->children
[0];
2133 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2134 lhs
->locals
= _slang_variable_scope_new(scope
);
2135 lhs
->num_children
= 2;
2136 lhs
->children
= slang_operation_new(2);
2138 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2139 lhs
->children
[0].a_id
= var
->a_name
;
2140 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2142 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2143 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2147 slang_operation
*rhs
= &assign
->children
[1];
2149 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2150 rhs
->locals
= _slang_variable_scope_new(scope
);
2151 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2157 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2159 ret
->type
= SLANG_OPER_RETURN
;
2160 ret
->locals
= _slang_variable_scope_new(scope
);
2161 ret
->num_children
= 1;
2162 ret
->children
= slang_operation_new(1);
2163 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2164 ret
->children
[0].a_id
= var
->a_name
;
2165 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2170 slang_print_function(fun, 1);
2178 _slang_is_vec_mat_type(const char *name
)
2180 static const char *vecmat_types
[] = {
2181 "float", "int", "bool",
2182 "vec2", "vec3", "vec4",
2183 "ivec2", "ivec3", "ivec4",
2184 "bvec2", "bvec3", "bvec4",
2185 "mat2", "mat3", "mat4",
2186 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2190 for (i
= 0; vecmat_types
[i
]; i
++)
2191 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2198 * Assemble a function call, given a particular function name.
2199 * \param name the function's name (operators like '*' are possible).
2201 static slang_ir_node
*
2202 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2203 slang_operation
*oper
, slang_operation
*dest
)
2205 slang_operation
*params
= oper
->children
;
2206 const GLuint param_count
= oper
->num_children
;
2208 slang_function
*fun
;
2211 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2212 if (atom
== SLANG_ATOM_NULL
)
2215 if (oper
->array_constructor
) {
2216 /* this needs special handling */
2217 fun
= _slang_make_array_constructor(A
, oper
);
2220 /* Try to find function by name and exact argument type matching */
2221 GLboolean error
= GL_FALSE
;
2222 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2223 &A
->space
, A
->atoms
, A
->log
, &error
);
2225 slang_info_log_error(A
->log
,
2226 "Function '%s' not found (check argument types)",
2233 /* Next, try locating a constructor function for a user-defined type */
2234 fun
= _slang_locate_struct_constructor(A
, name
);
2238 * At this point, some heuristics are used to try to find a function
2239 * that matches the calling signature by means of casting or "unrolling"
2243 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2244 /* Next, if this call looks like a vec() or mat() constructor call,
2245 * try "unwinding" the args to satisfy a constructor.
2247 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2249 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2250 slang_info_log_error(A
->log
,
2251 "Function '%s' not found (check argument types)",
2258 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2259 /* Next, try casting args to the types of the formal parameters */
2260 int numArgs
= oper
->num_children
;
2261 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2262 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2263 slang_info_log_error(A
->log
,
2264 "Function '%s' not found (check argument types)",
2272 slang_info_log_error(A
->log
,
2273 "Function '%s' not found (check argument types)",
2279 /* The function body may be in another compilation unit.
2280 * We'll try concatenating the shaders and recompile at link time.
2282 A
->UnresolvedRefs
= GL_TRUE
;
2283 return new_node1(IR_NOP
, NULL
);
2286 /* type checking to be sure function's return type matches 'dest' type */
2290 slang_typeinfo_construct(&t0
);
2291 typeof_operation(A
, dest
, &t0
);
2293 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2294 slang_info_log_error(A
->log
,
2295 "Incompatible type returned by call to '%s'",
2301 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2303 if (n
&& !n
->Store
&& !dest
2304 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2305 /* setup n->Store for the result of the function call */
2306 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2307 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2308 /*printf("Alloc storage for function result, size %d \n", size);*/
2311 if (oper
->array_constructor
) {
2312 /* free the temporary array constructor function now */
2313 slang_function_destruct(fun
);
2320 static slang_ir_node
*
2321 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2323 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2325 slang_variable
*var
;
2327 /* NOTE: In GLSL 1.20, there's only one kind of method
2328 * call: array.length(). Anything else is an error.
2330 if (oper
->a_id
!= a_length
) {
2331 slang_info_log_error(A
->log
,
2332 "Undefined method call '%s'", (char *) oper
->a_id
);
2336 /* length() takes no arguments */
2337 if (oper
->num_children
> 0) {
2338 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2342 /* lookup the object/variable */
2343 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2344 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2345 slang_info_log_error(A
->log
,
2346 "Undefined object '%s'", (char *) oper
->a_obj
);
2350 /* Create a float/literal IR node encoding the array length */
2351 n
= new_node0(IR_FLOAT
);
2353 n
->Value
[0] = (float) _slang_array_length(var
);
2354 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2361 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2363 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2364 oper
->type
== SLANG_OPER_LITERAL_INT
||
2365 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2366 if (oper
->literal
[0])
2372 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2373 oper
->num_children
== 1) {
2374 return _slang_is_constant_cond(&oper
->children
[0], value
);
2381 * Test if an operation is a scalar or boolean.
2384 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2386 slang_typeinfo type
;
2389 slang_typeinfo_construct(&type
);
2390 typeof_operation(A
, oper
, &type
);
2391 size
= _slang_sizeof_type_specifier(&type
.spec
);
2392 slang_typeinfo_destruct(&type
);
2398 * Test if an operation is boolean.
2401 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2403 slang_typeinfo type
;
2406 slang_typeinfo_construct(&type
);
2407 typeof_operation(A
, oper
, &type
);
2408 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2409 slang_typeinfo_destruct(&type
);
2415 * Check if a loop contains a 'continue' statement.
2416 * Stop looking if we find a nested loop.
2419 _slang_loop_contains_continue(const slang_operation
*oper
)
2421 switch (oper
->type
) {
2422 case SLANG_OPER_CONTINUE
:
2424 case SLANG_OPER_FOR
:
2426 case SLANG_OPER_WHILE
:
2427 /* stop upon finding a nested loop */
2433 for (i
= 0; i
< oper
->num_children
; i
++) {
2434 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2435 if (_slang_loop_contains_continue(child
))
2445 * Check if a loop contains a 'continue' or 'break' statement.
2446 * Stop looking if we find a nested loop.
2449 _slang_loop_contains_continue_or_break(const slang_operation
*oper
)
2451 switch (oper
->type
) {
2452 case SLANG_OPER_CONTINUE
:
2453 case SLANG_OPER_BREAK
:
2455 case SLANG_OPER_FOR
:
2457 case SLANG_OPER_WHILE
:
2458 /* stop upon finding a nested loop */
2464 for (i
= 0; i
< oper
->num_children
; i
++) {
2465 const slang_operation
*child
= slang_oper_child_const(oper
, i
);
2466 if (_slang_loop_contains_continue_or_break(child
))
2476 * Replace 'break' and 'continue' statements inside a do and while loops.
2477 * This is a recursive helper function used by
2478 * _slang_gen_do/while_without_continue().
2481 replace_break_and_cont(slang_assemble_ctx
*A
, slang_operation
*oper
)
2483 switch (oper
->type
) {
2484 case SLANG_OPER_BREAK
:
2485 /* replace 'break' with "_notBreakFlag = false; break" */
2487 slang_operation
*block
= oper
;
2488 block
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2489 slang_operation_add_children(block
, 2);
2491 slang_operation
*assign
= slang_oper_child(block
, 0);
2492 assign
->type
= SLANG_OPER_ASSIGN
;
2493 slang_operation_add_children(assign
, 2);
2495 slang_operation
*lhs
= slang_oper_child(assign
, 0);
2496 slang_operation_identifier(lhs
, A
, "_notBreakFlag");
2499 slang_operation
*rhs
= slang_oper_child(assign
, 1);
2500 slang_operation_literal_bool(rhs
, GL_FALSE
);
2504 slang_operation
*brk
= slang_oper_child(block
, 1);
2505 brk
->type
= SLANG_OPER_BREAK
;
2506 assert(!brk
->children
);
2510 case SLANG_OPER_CONTINUE
:
2511 /* convert continue into a break */
2512 oper
->type
= SLANG_OPER_BREAK
;
2514 case SLANG_OPER_FOR
:
2516 case SLANG_OPER_WHILE
:
2517 /* stop upon finding a nested loop */
2523 for (i
= 0; i
< oper
->num_children
; i
++) {
2524 replace_break_and_cont(A
, slang_oper_child(oper
, i
));
2532 * Transform a while-loop so that continue statements are converted to breaks.
2533 * Then do normal IR code generation.
2537 * while (LOOPCOND) {
2549 * bool _notBreakFlag = 1;
2550 * while (_notBreakFlag && LOOPCOND) {
2554 * break; // was continue
2557 * _notBreakFlag = 0; // was
2564 static slang_ir_node
*
2565 _slang_gen_while_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2567 slang_operation
*top
;
2568 slang_operation
*innerBody
;
2570 assert(oper
->type
== SLANG_OPER_WHILE
);
2572 top
= slang_operation_new(1);
2573 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2574 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2575 slang_operation_add_children(top
, 2);
2577 /* declare: bool _notBreakFlag = true */
2579 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2580 slang_generate_declaration(A
, top
->locals
, condDecl
,
2581 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2584 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2586 slang_operation
*outerWhile
= slang_oper_child(top
, 1);
2587 outerWhile
->type
= SLANG_OPER_WHILE
;
2588 slang_operation_add_children(outerWhile
, 2);
2590 /* _notBreakFlag && LOOPCOND */
2592 slang_operation
*cond
= slang_oper_child(outerWhile
, 0);
2593 cond
->type
= SLANG_OPER_LOGICALAND
;
2594 slang_operation_add_children(cond
, 2);
2596 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2597 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2600 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2601 slang_operation_copy(origCond
, slang_oper_child(oper
, 0));
2607 slang_operation
*innerDo
= slang_oper_child(outerWhile
, 1);
2608 innerDo
->type
= SLANG_OPER_DO
;
2609 slang_operation_add_children(innerDo
, 2);
2611 /* copy original do-loop body into inner do-loop's body */
2612 innerBody
= slang_oper_child(innerDo
, 0);
2613 slang_operation_copy(innerBody
, slang_oper_child(oper
, 1));
2614 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2616 /* inner do-loop's condition is constant/false */
2618 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2619 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2624 /* Finally, in innerBody,
2625 * replace "break" with "_notBreakFlag = 0; break"
2626 * replace "continue" with "break"
2628 replace_break_and_cont(A
, innerBody
);
2630 /*slang_print_tree(top, 0);*/
2632 return _slang_gen_operation(A
, top
);
2639 * Generate loop code using high-level IR_LOOP instruction
2641 static slang_ir_node
*
2642 _slang_gen_while(slang_assemble_ctx
* A
, slang_operation
*oper
)
2646 * BREAK if !expr (child[0])
2647 * body code (child[1])
2649 const slang_operation
*prevLoopOper
;
2650 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2651 GLboolean isConst
, constTrue
;
2653 if (!A
->EmitContReturn
) {
2654 /* We don't want to emit CONT instructions. If this while-loop has
2655 * a continue, translate it away.
2657 if (_slang_loop_contains_continue(slang_oper_child(oper
, 1))) {
2658 return _slang_gen_while_without_continue(A
, oper
);
2662 /* type-check expression */
2663 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2664 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2668 /* Check if loop condition is a constant */
2669 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2671 if (isConst
&& !constTrue
) {
2672 /* loop is never executed! */
2673 return new_node0(IR_NOP
);
2676 loop
= new_loop(NULL
);
2678 /* save old, push new loop */
2679 prevLoop
= A
->CurLoop
;
2681 prevLoopOper
= A
->CurLoopOper
;
2682 A
->CurLoopOper
= oper
;
2684 if (isConst
&& constTrue
) {
2685 /* while(nonzero constant), no conditional break */
2690 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2691 breakIf
= new_break_if_true(A
, cond
);
2693 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2694 loop
->Children
[0] = new_seq(breakIf
, body
);
2696 /* Do infinite loop detection */
2697 /* loop->List is head of linked list of break/continue nodes */
2698 if (!loop
->List
&& isConst
&& constTrue
) {
2699 /* infinite loop detected */
2700 A
->CurLoop
= prevLoop
; /* clean-up */
2701 slang_info_log_error(A
->log
, "Infinite loop detected!");
2705 /* pop loop, restore prev */
2706 A
->CurLoop
= prevLoop
;
2707 A
->CurLoopOper
= prevLoopOper
;
2714 * Transform a do-while-loop so that continue statements are converted to breaks.
2715 * Then do normal IR code generation.
2726 * } while (LOOPCOND);
2731 * bool _notBreakFlag = 1;
2736 * break; // was continue
2739 * _notBreakFlag = 0; // was
2743 * } while (_notBreakFlag && LOOPCOND);
2746 static slang_ir_node
*
2747 _slang_gen_do_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
2749 slang_operation
*top
;
2750 slang_operation
*innerBody
;
2752 assert(oper
->type
== SLANG_OPER_DO
);
2754 top
= slang_operation_new(1);
2755 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2756 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
2757 slang_operation_add_children(top
, 2);
2759 /* declare: bool _notBreakFlag = true */
2761 slang_operation
*condDecl
= slang_oper_child(top
, 0);
2762 slang_generate_declaration(A
, top
->locals
, condDecl
,
2763 SLANG_SPEC_BOOL
, "_notBreakFlag", GL_TRUE
);
2766 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2768 slang_operation
*outerDo
= slang_oper_child(top
, 1);
2769 outerDo
->type
= SLANG_OPER_DO
;
2770 slang_operation_add_children(outerDo
, 2);
2774 slang_operation
*innerDo
= slang_oper_child(outerDo
, 0);
2775 innerDo
->type
= SLANG_OPER_DO
;
2776 slang_operation_add_children(innerDo
, 2);
2778 /* copy original do-loop body into inner do-loop's body */
2779 innerBody
= slang_oper_child(innerDo
, 0);
2780 slang_operation_copy(innerBody
, slang_oper_child(oper
, 0));
2781 innerBody
->locals
->outer_scope
= innerDo
->locals
;
2783 /* inner do-loop's condition is constant/false */
2785 slang_operation
*constFalse
= slang_oper_child(innerDo
, 1);
2786 slang_operation_literal_bool(constFalse
, GL_FALSE
);
2790 /* _notBreakFlag && LOOPCOND */
2792 slang_operation
*cond
= slang_oper_child(outerDo
, 1);
2793 cond
->type
= SLANG_OPER_LOGICALAND
;
2794 slang_operation_add_children(cond
, 2);
2796 slang_operation
*notBreak
= slang_oper_child(cond
, 0);
2797 slang_operation_identifier(notBreak
, A
, "_notBreakFlag");
2800 slang_operation
*origCond
= slang_oper_child(cond
, 1);
2801 slang_operation_copy(origCond
, slang_oper_child(oper
, 1));
2806 /* Finally, in innerBody,
2807 * replace "break" with "_notBreakFlag = 0; break"
2808 * replace "continue" with "break"
2810 replace_break_and_cont(A
, innerBody
);
2812 /*slang_print_tree(top, 0);*/
2814 return _slang_gen_operation(A
, top
);
2819 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2821 static slang_ir_node
*
2822 _slang_gen_do(slang_assemble_ctx
* A
, slang_operation
*oper
)
2826 * body code (child[0])
2828 * BREAK if !expr (child[1])
2830 const slang_operation
*prevLoopOper
;
2831 slang_ir_node
*prevLoop
, *loop
;
2832 GLboolean isConst
, constTrue
;
2834 if (!A
->EmitContReturn
) {
2835 /* We don't want to emit CONT instructions. If this do-loop has
2836 * a continue, translate it away.
2838 if (_slang_loop_contains_continue(slang_oper_child(oper
, 0))) {
2839 return _slang_gen_do_without_continue(A
, oper
);
2843 /* type-check expression */
2844 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2845 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2849 loop
= new_loop(NULL
);
2851 /* save old, push new loop */
2852 prevLoop
= A
->CurLoop
;
2854 prevLoopOper
= A
->CurLoopOper
;
2855 A
->CurLoopOper
= oper
;
2858 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2860 /* Check if loop condition is a constant */
2861 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2862 if (isConst
&& constTrue
) {
2863 /* do { } while(1) ==> no conditional break */
2864 loop
->Children
[1] = NULL
; /* no tail code */
2868 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2869 loop
->Children
[1] = new_break_if_true(A
, cond
);
2872 /* XXX we should do infinite loop detection, as above */
2874 /* pop loop, restore prev */
2875 A
->CurLoop
= prevLoop
;
2876 A
->CurLoopOper
= prevLoopOper
;
2883 * Recursively count the number of operations rooted at 'oper'.
2884 * This gives some kind of indication of the size/complexity of an operation.
2887 sizeof_operation(const slang_operation
*oper
)
2890 GLuint count
= 1; /* me */
2892 for (i
= 0; i
< oper
->num_children
; i
++) {
2893 count
+= sizeof_operation(&oper
->children
[i
]);
2904 * Determine if a for-loop can be unrolled.
2905 * At this time, only a rather narrow class of for loops can be unrolled.
2906 * See code for details.
2907 * When a loop can't be unrolled because it's too large we'll emit a
2908 * message to the log.
2911 _slang_can_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2915 const char *varName
;
2918 if (oper
->type
!= SLANG_OPER_FOR
)
2921 assert(oper
->num_children
== 4);
2923 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper
, 3)))
2926 /* children[0] must be either "int i=constant" or "i=constant" */
2927 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
2928 slang_variable
*var
;
2930 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_VARIABLE_DECL
)
2933 varId
= oper
->children
[0].children
[0].a_id
;
2935 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
2939 if (!var
->initializer
)
2941 if (var
->initializer
->type
!= SLANG_OPER_LITERAL_INT
)
2943 start
= (GLint
) var
->initializer
->literal
[0];
2945 else if (oper
->children
[0].type
== SLANG_OPER_EXPRESSION
) {
2946 if (oper
->children
[0].children
[0].type
!= SLANG_OPER_ASSIGN
)
2948 if (oper
->children
[0].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2950 if (oper
->children
[0].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2953 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
2955 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
2961 /* children[1] must be "i<constant" */
2962 if (oper
->children
[1].type
!= SLANG_OPER_EXPRESSION
)
2964 if (oper
->children
[1].children
[0].type
!= SLANG_OPER_LESS
)
2966 if (oper
->children
[1].children
[0].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2968 if (oper
->children
[1].children
[0].children
[1].type
!= SLANG_OPER_LITERAL_INT
)
2971 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
2973 /* children[2] must be "i++" or "++i" */
2974 if (oper
->children
[2].type
!= SLANG_OPER_POSTINCREMENT
&&
2975 oper
->children
[2].type
!= SLANG_OPER_PREINCREMENT
)
2977 if (oper
->children
[2].children
[0].type
!= SLANG_OPER_IDENTIFIER
)
2980 /* make sure the same variable name is used in all places */
2981 if ((oper
->children
[1].children
[0].children
[0].a_id
!= varId
) ||
2982 (oper
->children
[2].children
[0].a_id
!= varId
))
2985 varName
= (const char *) varId
;
2987 /* children[3], the loop body, can't be too large */
2988 bodySize
= sizeof_operation(&oper
->children
[3]);
2989 if (bodySize
> MAX_FOR_LOOP_UNROLL_BODY_SIZE
) {
2990 slang_info_log_print(A
->log
,
2991 "Note: 'for (%s ... )' body is too large/complex"
2998 return GL_FALSE
; /* degenerate case */
3000 if (end
- start
> MAX_FOR_LOOP_UNROLL_ITERATIONS
) {
3001 slang_info_log_print(A
->log
,
3002 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3003 " many iterations to unroll",
3004 varName
, start
, varName
, end
, varName
);
3008 if ((end
- start
) * bodySize
> MAX_FOR_LOOP_UNROLL_COMPLEXITY
) {
3009 slang_info_log_print(A
->log
,
3010 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3011 " too much code to unroll",
3012 varName
, start
, varName
, end
, varName
);
3016 return GL_TRUE
; /* we can unroll the loop */
3021 * Unroll a for-loop.
3022 * First we determine the number of iterations to unroll.
3023 * Then for each iteration:
3024 * make a copy of the loop body
3025 * replace instances of the loop variable with the current iteration value
3026 * generate IR code for the body
3027 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3029 static slang_ir_node
*
3030 _slang_unroll_for_loop(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3032 GLint start
, end
, iter
;
3033 slang_ir_node
*n
, *root
= NULL
;
3036 if (oper
->children
[0].type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) {
3037 /* for (int i=0; ... */
3038 slang_variable
*var
;
3040 varId
= oper
->children
[0].children
[0].a_id
;
3041 var
= _slang_variable_locate(oper
->children
[0].children
[0].locals
,
3043 start
= (GLint
) var
->initializer
->literal
[0];
3047 varId
= oper
->children
[0].children
[0].children
[0].a_id
;
3048 start
= (GLint
) oper
->children
[0].children
[0].children
[1].literal
[0];
3051 end
= (GLint
) oper
->children
[1].children
[0].children
[1].literal
[0];
3053 for (iter
= start
; iter
< end
; iter
++) {
3054 slang_operation
*body
;
3056 /* make a copy of the loop body */
3057 body
= slang_operation_new(1);
3061 if (!slang_operation_copy(body
, &oper
->children
[3]))
3064 /* in body, replace instances of 'varId' with literal 'iter' */
3066 slang_variable
*oldVar
;
3067 slang_operation
*newOper
;
3069 oldVar
= _slang_variable_locate(oper
->locals
, varId
, GL_TRUE
);
3071 /* undeclared loop variable */
3072 slang_operation_delete(body
);
3076 newOper
= slang_operation_new(1);
3077 newOper
->type
= SLANG_OPER_LITERAL_INT
;
3078 newOper
->literal_size
= 1;
3079 newOper
->literal
[0] = iter
;
3081 /* replace instances of the loop variable with newOper */
3082 slang_substitute(A
, body
, 1, &oldVar
, &newOper
, GL_FALSE
);
3085 /* do IR codegen for body */
3086 n
= _slang_gen_operation(A
, body
);
3090 root
= new_seq(root
, n
);
3092 slang_operation_delete(body
);
3100 * Replace 'continue' statement with 'break' inside a for-loop.
3101 * This is a recursive helper function used by _slang_gen_for_without_continue().
3104 replace_continue_with_break(slang_assemble_ctx
*A
, slang_operation
*oper
)
3106 switch (oper
->type
) {
3107 case SLANG_OPER_CONTINUE
:
3108 oper
->type
= SLANG_OPER_BREAK
;
3110 case SLANG_OPER_FOR
:
3112 case SLANG_OPER_WHILE
:
3113 /* stop upon finding a nested loop */
3119 for (i
= 0; i
< oper
->num_children
; i
++) {
3120 replace_continue_with_break(A
, slang_oper_child(oper
, i
));
3128 * Transform a for-loop so that continue statements are converted to breaks.
3129 * Then do normal IR code generation.
3133 * for (INIT; LOOPCOND; INCR) {
3144 * bool _condFlag = 1;
3145 * for (INIT; _condFlag; ) {
3146 * for ( ; _condFlag = LOOPCOND; INCR) {
3158 static slang_ir_node
*
3159 _slang_gen_for_without_continue(slang_assemble_ctx
*A
, slang_operation
*oper
)
3161 slang_operation
*top
;
3162 slang_operation
*outerFor
, *innerFor
, *init
, *cond
, *incr
;
3163 slang_operation
*lhs
, *rhs
;
3165 assert(oper
->type
== SLANG_OPER_FOR
);
3167 top
= slang_operation_new(1);
3168 top
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
3169 top
->locals
->outer_scope
= oper
->locals
->outer_scope
;
3170 slang_operation_add_children(top
, 2);
3172 /* declare: bool _condFlag = true */
3174 slang_operation
*condDecl
;
3175 slang_generate_declaration(A
, top
->locals
, condDecl
,
3176 SLANG_SPEC_BOOL
, "_condFlag", GL_TRUE
);
3179 /* build outer loop: for (INIT; _condFlag; ) { */
3180 outerFor
= slang_oper_child(top
, 1);
3181 outerFor
->type
= SLANG_OPER_FOR
;
3182 slang_operation_add_children(outerFor
, 4);
3184 init
= slang_oper_child(outerFor
, 0);
3185 slang_operation_copy(init
, slang_oper_child(oper
, 0));
3187 cond
= slang_oper_child(outerFor
, 1);
3188 cond
->type
= SLANG_OPER_IDENTIFIER
;
3189 cond
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3191 incr
= slang_oper_child(outerFor
, 2);
3192 incr
->type
= SLANG_OPER_VOID
;
3194 /* body of the outer loop */
3196 slang_operation
*block
= slang_oper_child(outerFor
, 3);
3198 slang_operation_add_children(block
, 2);
3199 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
3201 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3203 innerFor
= slang_oper_child(block
, 0);
3205 /* make copy of orig loop */
3206 slang_operation_copy(innerFor
, oper
);
3207 assert(innerFor
->type
== SLANG_OPER_FOR
);
3208 innerFor
->locals
->outer_scope
= block
->locals
;
3210 init
= slang_oper_child(innerFor
, 0);
3211 init
->type
= SLANG_OPER_VOID
; /* leak? */
3213 cond
= slang_oper_child(innerFor
, 1);
3214 slang_operation_destruct(cond
);
3215 cond
->type
= SLANG_OPER_ASSIGN
;
3216 cond
->locals
= _slang_variable_scope_new(innerFor
->locals
);
3217 slang_operation_add_children(cond
, 2);
3219 lhs
= slang_oper_child(cond
, 0);
3220 lhs
->type
= SLANG_OPER_IDENTIFIER
;
3221 lhs
->a_id
= slang_atom_pool_atom(A
->atoms
, "_condFlag");
3223 rhs
= slang_oper_child(cond
, 1);
3224 slang_operation_copy(rhs
, slang_oper_child(oper
, 1));
3227 /* if (_condFlag) INCR; */
3229 slang_operation
*ifop
= slang_oper_child(block
, 1);
3230 ifop
->type
= SLANG_OPER_IF
;
3231 slang_operation_add_children(ifop
, 2);
3233 /* re-use cond node build above */
3234 slang_operation_copy(slang_oper_child(ifop
, 0), cond
);
3236 /* incr node from original for-loop operation */
3237 slang_operation_copy(slang_oper_child(ifop
, 1),
3238 slang_oper_child(oper
, 2));
3241 /* finally, replace "continue" with "break" in the inner for-loop */
3242 replace_continue_with_break(A
, slang_oper_child(innerFor
, 3));
3245 return _slang_gen_operation(A
, top
);
3251 * Generate IR for a for-loop. Unrolling will be done when possible.
3253 static slang_ir_node
*
3254 _slang_gen_for(slang_assemble_ctx
* A
, slang_operation
*oper
)
3258 if (!A
->EmitContReturn
) {
3259 /* We don't want to emit CONT instructions. If this for-loop has
3260 * a continue, translate it away.
3262 if (_slang_loop_contains_continue(slang_oper_child(oper
, 3))) {
3263 return _slang_gen_for_without_continue(A
, oper
);
3267 unroll
= _slang_can_unroll_for_loop(A
, oper
);
3269 slang_ir_node
*code
= _slang_unroll_for_loop(A
, oper
);
3274 assert(oper
->type
== SLANG_OPER_FOR
);
3276 /* conventional for-loop code generation */
3279 * init code (child[0])
3281 * BREAK if !expr (child[1])
3282 * body code (child[3])
3284 * incr code (child[2]) // XXX continue here
3286 const slang_operation
*prevLoopOper
;
3287 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
3288 init
= _slang_gen_operation(A
, &oper
->children
[0]);
3289 loop
= new_loop(NULL
);
3291 /* save old, push new loop */
3292 prevLoop
= A
->CurLoop
;
3294 prevLoopOper
= A
->CurLoopOper
;
3295 A
->CurLoopOper
= oper
;
3297 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
3298 breakIf
= new_break_if_true(A
, cond
);
3299 body
= _slang_gen_operation(A
, &oper
->children
[3]);
3300 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
3302 loop
->Children
[0] = new_seq(breakIf
, body
);
3303 loop
->Children
[1] = incr
; /* tail code */
3305 /* pop loop, restore prev */
3306 A
->CurLoop
= prevLoop
;
3307 A
->CurLoopOper
= prevLoopOper
;
3309 return new_seq(init
, loop
);
3314 static slang_ir_node
*
3315 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3317 slang_ir_node
*n
, *cont
, *incr
= NULL
, *loopNode
;
3319 assert(oper
->type
== SLANG_OPER_CONTINUE
);
3320 loopNode
= A
->CurLoop
;
3322 assert(loopNode
->Opcode
== IR_LOOP
);
3324 cont
= new_node0(IR_CONT
);
3326 cont
->Parent
= loopNode
;
3327 /* insert this node at head of linked list of cont/break instructions */
3328 cont
->List
= loopNode
->List
;
3329 loopNode
->List
= cont
;
3332 n
= new_seq(incr
, cont
);
3338 * Determine if the given operation is of a specific type.
3341 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
3343 if (oper
->type
== type
)
3345 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
3346 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
3347 oper
->num_children
== 1)
3348 return is_operation_type(&oper
->children
[0], type
);
3355 * Generate IR tree for an if/then/else conditional using high-level
3356 * IR_IF instruction.
3358 static slang_ir_node
*
3359 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3362 * eval expr (child[0])
3369 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
3370 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
3371 GLboolean isConst
, constTrue
;
3373 /* type-check expression */
3374 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
3375 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
3379 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3380 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
3384 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
3388 return _slang_gen_operation(A
, &oper
->children
[1]);
3391 /* if (false) ... */
3392 return _slang_gen_operation(A
, &oper
->children
[2]);
3396 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3397 cond
= new_cond(cond
);
3399 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
3400 && !haveElseClause
) {
3401 /* Special case: generate a conditional break */
3402 if (!A
->CurLoop
) /* probably trying to unroll */
3404 ifBody
= new_break_if_true(A
, cond
);
3407 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
3410 && A
->CurLoopOper
->type
!= SLANG_OPER_FOR
) {
3411 /* Special case: generate a conditional continue */
3412 if (!A
->CurLoop
) /* probably trying to unroll */
3414 ifBody
= new_cont_if_true(A
, cond
);
3419 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
3421 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
3424 ifNode
= new_if(cond
, ifBody
, elseBody
);
3431 static slang_ir_node
*
3432 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3436 assert(oper
->type
== SLANG_OPER_NOT
);
3438 /* type-check expression */
3439 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3440 slang_info_log_error(A
->log
,
3441 "scalar/boolean expression expected for '!'");
3445 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3453 static slang_ir_node
*
3454 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
3456 slang_ir_node
*n1
, *n2
;
3458 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
3460 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
3461 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
3462 slang_info_log_error(A
->log
,
3463 "scalar/boolean expressions expected for '^^'");
3467 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
3470 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
3473 return new_node2(IR_NOTEQUAL
, n1
, n2
);
3478 * Generate IR node for storage of a temporary of given size.
3480 static slang_ir_node
*
3481 _slang_gen_temporary(GLint size
)
3483 slang_ir_storage
*store
;
3484 slang_ir_node
*n
= NULL
;
3486 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
3488 n
= new_node0(IR_VAR_DECL
);
3501 * Generate program constants for an array.
3502 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3503 * This will allocate and initialize three vector constants, storing
3504 * the array in constant memory, not temporaries like a non-const array.
3505 * This can also be used for uniform array initializers.
3506 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3509 make_constant_array(slang_assemble_ctx
*A
,
3510 slang_variable
*var
,
3511 slang_operation
*initializer
)
3513 struct gl_program
*prog
= A
->program
;
3514 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3515 const char *varName
= (char *) var
->a_name
;
3516 const GLuint numElements
= initializer
->num_children
;
3522 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
3524 size
= var
->store
->Size
;
3526 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
3527 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
3528 assert(initializer
->type
== SLANG_OPER_CALL
);
3529 assert(initializer
->array_constructor
);
3531 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
3533 /* convert constructor params into ordinary floats */
3534 for (i
= 0; i
< numElements
; i
++) {
3535 const slang_operation
*op
= &initializer
->children
[i
];
3536 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
3537 /* unsupported type for this optimization */
3541 for (j
= 0; j
< op
->literal_size
; j
++) {
3542 values
[i
* 4 + j
] = op
->literal
[j
];
3544 for ( ; j
< 4; j
++) {
3545 values
[i
* 4 + j
] = 0.0f
;
3549 /* slightly different paths for constants vs. uniforms */
3550 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3551 var
->store
->File
= PROGRAM_UNIFORM
;
3552 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
3553 size
, datatype
, values
);
3556 var
->store
->File
= PROGRAM_CONSTANT
;
3557 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
3560 assert(var
->store
->Size
== size
);
3570 * Generate IR node for allocating/declaring a variable (either a local or
3572 * Generally, this involves allocating an slang_ir_storage instance for the
3573 * variable, choosing a register file (temporary, constant, etc).
3574 * For ordinary variables we do not yet allocate storage though. We do that
3575 * when we find the first actual use of the variable to avoid allocating temp
3576 * regs that will never get used.
3577 * At this time, uniforms are always allocated space in this function.
3579 * \param initializer Optional initializer expression for the variable.
3581 static slang_ir_node
*
3582 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
3583 slang_operation
*initializer
)
3585 const char *varName
= (const char *) var
->a_name
;
3586 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3587 slang_ir_node
*varDecl
, *n
;
3588 slang_ir_storage
*store
;
3589 GLint arrayLen
, size
, totalSize
; /* if array then totalSize > size */
3590 gl_register_file file
;
3592 /*assert(!var->declared);*/
3593 var
->declared
= GL_TRUE
;
3595 /* determine GPU register file for simple cases */
3596 if (is_sampler_type(&var
->type
)) {
3597 file
= PROGRAM_SAMPLER
;
3599 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3600 file
= PROGRAM_UNIFORM
;
3603 file
= PROGRAM_TEMPORARY
;
3606 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3608 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
3612 arrayLen
= _slang_array_length(var
);
3613 totalSize
= _slang_array_size(size
, arrayLen
);
3615 /* Allocate IR node for the declaration */
3616 varDecl
= new_node0(IR_VAR_DECL
);
3620 /* Allocate slang_ir_storage for this variable if needed.
3621 * Note that we may not actually allocate a constant or temporary register
3625 GLint index
= -7; /* TBD / unknown */
3626 var
->store
= _slang_new_ir_storage(file
, index
, totalSize
);
3628 return NULL
; /* out of memory */
3631 /* set the IR node's Var and Store pointers */
3633 varDecl
->Store
= var
->store
;
3638 /* if there's an initializer, generate IR for the expression */
3640 slang_ir_node
*varRef
, *init
;
3642 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3643 /* if the variable is const, the initializer must be a const
3644 * expression as well.
3647 if (!_slang_is_constant_expr(initializer
)) {
3648 slang_info_log_error(A
->log
,
3649 "initializer for %s not constant", varName
);
3655 /* IR for the variable we're initializing */
3656 varRef
= new_var(A
, var
);
3658 slang_info_log_error(A
->log
, "out of memory");
3662 /* constant-folding, etc here */
3663 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
3665 /* look for simple constant-valued variables and uniforms */
3666 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3667 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3669 if (initializer
->type
== SLANG_OPER_CALL
&&
3670 initializer
->array_constructor
) {
3671 /* array initializer */
3672 if (make_constant_array(A
, var
, initializer
))
3675 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3676 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3677 /* simple float/vector initializer */
3678 if (store
->File
== PROGRAM_UNIFORM
) {
3679 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
3681 totalSize
, datatype
,
3682 initializer
->literal
);
3683 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3688 store
->File
= PROGRAM_CONSTANT
;
3689 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
3691 initializer
->literal
,
3693 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3700 /* IR for initializer */
3701 init
= _slang_gen_operation(A
, initializer
);
3705 /* XXX remove this when type checking is added above */
3706 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
3707 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
3711 /* assign RHS to LHS */
3712 n
= new_node2(IR_COPY
, varRef
, init
);
3713 n
= new_seq(varDecl
, n
);
3716 /* no initializer */
3720 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
3721 /* always need to allocate storage for uniforms at this point */
3722 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
3723 totalSize
, datatype
, NULL
);
3724 store
->Swizzle
= _slang_var_swizzle(size
, 0);
3728 printf("%s var %p %s store=%p index=%d size=%d\n",
3729 __FUNCTION__
, (void *) var
, (char *) varName
,
3730 (void *) store
, store
->Index
, store
->Size
);
3738 * Generate code for a selection expression: b ? x : y
3739 * XXX In some cases we could implement a selection expression
3740 * with an LRP instruction (use the boolean as the interpolant).
3741 * Otherwise, we use an IF/ELSE/ENDIF construct.
3743 static slang_ir_node
*
3744 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
3746 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
3747 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
3748 slang_typeinfo type0
, type1
, type2
;
3749 int size
, isBool
, isEqual
;
3751 assert(oper
->type
== SLANG_OPER_SELECT
);
3752 assert(oper
->num_children
== 3);
3754 /* type of children[0] must be boolean */
3755 slang_typeinfo_construct(&type0
);
3756 typeof_operation(A
, &oper
->children
[0], &type0
);
3757 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
3758 slang_typeinfo_destruct(&type0
);
3760 slang_info_log_error(A
->log
, "selector type is not boolean");
3764 slang_typeinfo_construct(&type1
);
3765 slang_typeinfo_construct(&type2
);
3766 typeof_operation(A
, &oper
->children
[1], &type1
);
3767 typeof_operation(A
, &oper
->children
[2], &type2
);
3768 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
3769 slang_typeinfo_destruct(&type1
);
3770 slang_typeinfo_destruct(&type2
);
3772 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
3776 /* size of x or y's type */
3777 size
= _slang_sizeof_type_specifier(&type1
.spec
);
3781 tmpDecl
= _slang_gen_temporary(size
);
3783 /* the condition (child 0) */
3784 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
3785 cond
= new_cond(cond
);
3787 /* if-true body (child 1) */
3788 tmpVar
= new_node0(IR_VAR
);
3789 tmpVar
->Store
= tmpDecl
->Store
;
3790 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
3791 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
3793 /* if-false body (child 2) */
3794 tmpVar
= new_node0(IR_VAR
);
3795 tmpVar
->Store
= tmpDecl
->Store
;
3796 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
3797 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
3799 ifNode
= new_if(cond
, trueNode
, falseNode
);
3802 tmpVar
= new_node0(IR_VAR
);
3803 tmpVar
->Store
= tmpDecl
->Store
;
3805 tree
= new_seq(ifNode
, tmpVar
);
3806 tree
= new_seq(tmpDecl
, tree
);
3808 /*_slang_print_ir_tree(tree, 10);*/
3814 * Generate code for &&.
3816 static slang_ir_node
*
3817 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
3819 /* rewrite "a && b" as "a ? b : false" */
3820 slang_operation
*select
;
3823 select
= slang_operation_new(1);
3824 select
->type
= SLANG_OPER_SELECT
;
3825 select
->num_children
= 3;
3826 select
->children
= slang_operation_new(3);
3828 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3829 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
3830 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
3831 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
3832 select
->children
[2].literal_size
= 1;
3834 n
= _slang_gen_select(A
, select
);
3840 * Generate code for ||.
3842 static slang_ir_node
*
3843 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
3845 /* rewrite "a || b" as "a ? true : b" */
3846 slang_operation
*select
;
3849 select
= slang_operation_new(1);
3850 select
->type
= SLANG_OPER_SELECT
;
3851 select
->num_children
= 3;
3852 select
->children
= slang_operation_new(3);
3854 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3855 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3856 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3857 select
->children
[1].literal_size
= 1;
3858 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3860 n
= _slang_gen_select(A
, select
);
3866 * Generate IR tree for a return statement.
3868 static slang_ir_node
*
3869 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3871 const GLboolean haveReturnValue
3872 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3874 /* error checking */
3875 assert(A
->CurFunction
);
3876 if (haveReturnValue
&&
3877 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3878 slang_info_log_error(A
->log
, "illegal return expression");
3881 else if (!haveReturnValue
&&
3882 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3883 slang_info_log_error(A
->log
, "return statement requires an expression");
3887 if (!haveReturnValue
) {
3888 return new_return(A
->curFuncEndLabel
);
3896 * return; // goto __endOfFunction
3898 slang_operation
*assign
;
3899 slang_atom a_retVal
;
3902 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3908 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3910 /* trying to return a value in a void-valued function */
3916 assign
= slang_operation_new(1);
3917 assign
->type
= SLANG_OPER_ASSIGN
;
3918 assign
->num_children
= 2;
3919 assign
->children
= slang_operation_new(2);
3920 /* lhs (__retVal) */
3921 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3922 assign
->children
[0].a_id
= a_retVal
;
3923 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3925 /* XXX we might be able to avoid this copy someday */
3926 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3928 /* assemble the new code */
3929 n
= new_seq(_slang_gen_operation(A
, assign
),
3930 new_return(A
->curFuncEndLabel
));
3932 slang_operation_delete(assign
);
3940 * Determine if the given operation/expression is const-valued.
3943 _slang_is_constant_expr(const slang_operation
*oper
)
3945 slang_variable
*var
;
3948 switch (oper
->type
) {
3949 case SLANG_OPER_IDENTIFIER
:
3950 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3951 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3955 for (i
= 0; i
< oper
->num_children
; i
++) {
3956 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3966 * Check if an assignment of type t1 to t0 is legal.
3967 * XXX more cases needed.
3970 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3971 slang_operation
*op0
,
3972 slang_operation
*op1
)
3974 slang_typeinfo t0
, t1
;
3977 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3978 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3982 slang_typeinfo_construct(&t0
);
3983 typeof_operation(A
, op0
, &t0
);
3985 slang_typeinfo_construct(&t1
);
3986 typeof_operation(A
, op1
, &t1
);
3988 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3989 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3993 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3998 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3999 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
4000 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
4003 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
4004 t1
.spec
.type
== SLANG_SPEC_BOOL
)
4007 #if 0 /* not used just yet - causes problems elsewhere */
4008 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
4009 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4013 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4014 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
4017 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
4018 t1
.spec
.type
== SLANG_SPEC_INT
)
4026 * Generate IR tree for a local variable declaration.
4027 * Basically do some error checking and call _slang_gen_var_decl().
4029 static slang_ir_node
*
4030 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
4032 const char *varName
= (char *) oper
->a_id
;
4033 slang_variable
*var
;
4034 slang_ir_node
*varDecl
;
4035 slang_operation
*initializer
;
4037 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
4038 assert(oper
->num_children
<= 1);
4041 /* lookup the variable by name */
4042 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
4044 return NULL
; /* "shouldn't happen" */
4046 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4047 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
4048 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4049 /* can't declare attribute/uniform vars inside functions */
4050 slang_info_log_error(A
->log
,
4051 "local variable '%s' cannot be an attribute/uniform/varying",
4058 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
4063 /* check if the var has an initializer */
4064 if (oper
->num_children
> 0) {
4065 assert(oper
->num_children
== 1);
4066 initializer
= &oper
->children
[0];
4068 else if (var
->initializer
) {
4069 initializer
= var
->initializer
;
4076 /* check/compare var type and initializer type */
4077 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
4078 slang_info_log_error(A
->log
, "incompatible types in assignment");
4083 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
4084 slang_info_log_error(A
->log
,
4085 "const-qualified variable '%s' requires initializer",
4091 /* Generate IR node */
4092 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
4101 * Generate IR tree for a reference to a variable (such as in an expression).
4102 * This is different from a variable declaration.
4104 static slang_ir_node
*
4105 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
4107 /* If there's a variable associated with this oper (from inlining)
4108 * use it. Otherwise, use the oper's var id.
4110 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
4111 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
4114 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
4117 assert(var
->declared
);
4118 n
= new_var(A
, var
);
4125 * Return the number of components actually named by the swizzle.
4126 * Recall that swizzles may have undefined/don't-care values.
4129 swizzle_size(GLuint swizzle
)
4132 for (i
= 0; i
< 4; i
++) {
4133 GLuint swz
= GET_SWZ(swizzle
, i
);
4134 size
+= (swz
>= 0 && swz
<= 3);
4140 static slang_ir_node
*
4141 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
4143 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
4147 n
->Store
= _slang_new_ir_storage_relative(0,
4148 swizzle_size(swizzle
),
4150 n
->Store
->Swizzle
= swizzle
;
4157 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
4159 while (store
->Parent
)
4160 store
= store
->Parent
;
4162 if (!(store
->File
== PROGRAM_OUTPUT
||
4163 store
->File
== PROGRAM_TEMPORARY
||
4164 (store
->File
== PROGRAM_VARYING
&&
4165 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
4175 * Walk up an IR storage path to compute the final swizzle.
4176 * This is used when we find an expression such as "foo.xz.yx".
4179 root_swizzle(const slang_ir_storage
*st
)
4181 GLuint swizzle
= st
->Swizzle
;
4182 while (st
->Parent
) {
4184 swizzle
= _slang_swizzle_swizzle(st
->Swizzle
, swizzle
);
4191 * Generate IR tree for an assignment (=).
4193 static slang_ir_node
*
4194 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
4196 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
4197 /* Check that var is writeable */
4199 = _slang_variable_locate(oper
->children
[0].locals
,
4200 oper
->children
[0].a_id
, GL_TRUE
);
4202 slang_info_log_error(A
->log
, "undefined variable '%s'",
4203 (char *) oper
->children
[0].a_id
);
4206 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
4207 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
4208 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
4209 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
4210 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
4211 slang_info_log_error(A
->log
,
4212 "illegal assignment to read-only variable '%s'",
4213 (char *) oper
->children
[0].a_id
);
4218 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
4219 oper
->children
[1].type
== SLANG_OPER_CALL
) {
4220 /* Special case of: x = f(a, b)
4221 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4223 * XXX this could be even more effective if we could accomodate
4224 * cases such as "v.x = f();" - would help with typical vertex
4228 n
= _slang_gen_function_call_name(A
,
4229 (const char *) oper
->children
[1].a_id
,
4230 &oper
->children
[1], &oper
->children
[0]);
4234 slang_ir_node
*n
, *lhs
, *rhs
;
4236 /* lhs and rhs type checking */
4237 if (!_slang_assignment_compatible(A
,
4239 &oper
->children
[1])) {
4240 slang_info_log_error(A
->log
, "incompatible types in assignment");
4244 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
4250 slang_info_log_error(A
->log
,
4251 "invalid left hand side for assignment");
4255 /* check that lhs is writable */
4256 if (!is_store_writable(A
, lhs
->Store
)) {
4257 slang_info_log_error(A
->log
,
4258 "illegal assignment to read-only l-value");
4262 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
4264 /* convert lhs swizzle into writemask */
4265 const GLuint swizzle
= root_swizzle(lhs
->Store
);
4266 GLuint writemask
, newSwizzle
= 0x0;
4267 if (!swizzle_to_writemask(A
, swizzle
, &writemask
, &newSwizzle
)) {
4268 /* Non-simple writemask, need to swizzle right hand side in
4269 * order to put components into the right place.
4271 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
4273 n
= new_node2(IR_COPY
, lhs
, rhs
);
4284 * Generate IR tree for referencing a field in a struct (or basic vector type)
4286 static slang_ir_node
*
4287 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
4291 /* type of struct */
4292 slang_typeinfo_construct(&ti
);
4293 typeof_operation(A
, &oper
->children
[0], &ti
);
4295 if (_slang_type_is_vector(ti
.spec
.type
)) {
4296 /* the field should be a swizzle */
4297 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
4301 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4302 slang_info_log_error(A
->log
, "Bad swizzle");
4305 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4310 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4311 /* create new parent node with swizzle */
4313 n
= _slang_gen_swizzle(n
, swizzle
);
4316 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
4317 || ti
.spec
.type
== SLANG_SPEC_INT
4318 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
4319 const GLuint rows
= 1;
4323 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
4324 slang_info_log_error(A
->log
, "Bad swizzle");
4326 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
4330 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4331 /* create new parent node with swizzle */
4332 n
= _slang_gen_swizzle(n
, swizzle
);
4336 /* the field is a structure member (base.field) */
4337 /* oper->children[0] is the base */
4338 /* oper->a_id is the field name */
4339 slang_ir_node
*base
, *n
;
4340 slang_typeinfo field_ti
;
4341 GLint fieldSize
, fieldOffset
= -1;
4344 slang_typeinfo_construct(&field_ti
);
4345 typeof_operation(A
, oper
, &field_ti
);
4347 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
4349 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
4351 if (fieldSize
== 0 || fieldOffset
< 0) {
4352 const char *structName
;
4353 if (ti
.spec
._struct
)
4354 structName
= (char *) ti
.spec
._struct
->a_name
;
4356 structName
= "unknown";
4357 slang_info_log_error(A
->log
,
4358 "\"%s\" is not a member of struct \"%s\"",
4359 (char *) oper
->a_id
, structName
);
4362 assert(fieldSize
>= 0);
4364 base
= _slang_gen_operation(A
, &oper
->children
[0]);
4366 /* error msg should have already been logged */
4370 n
= new_node1(IR_FIELD
, base
);
4374 n
->Field
= (char *) oper
->a_id
;
4376 /* Store the field's offset in storage->Index */
4377 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
4387 * Gen code for array indexing.
4389 static slang_ir_node
*
4390 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
4392 slang_typeinfo array_ti
;
4394 /* get array's type info */
4395 slang_typeinfo_construct(&array_ti
);
4396 typeof_operation(A
, &oper
->children
[0], &array_ti
);
4398 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
4399 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4400 /* translate the index into a swizzle/writemask: "v.x=p" */
4401 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
4405 index
= (GLint
) oper
->children
[1].literal
[0];
4406 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
4407 index
>= (GLint
) max
) {
4409 slang_info_log_error(A
->log
, "Invalid array index for vector type");
4410 printf("type = %d\n", oper
->children
[1].type
);
4411 printf("index = %d, max = %d\n", index
, max
);
4412 printf("array = %s\n", (char*)oper
->children
[0].a_id
);
4413 printf("index = %s\n", (char*)oper
->children
[1].a_id
);
4420 n
= _slang_gen_operation(A
, &oper
->children
[0]);
4422 /* use swizzle to access the element */
4423 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
4427 n
= _slang_gen_swizzle(n
, swizzle
);
4433 /* conventional array */
4434 slang_typeinfo elem_ti
;
4435 slang_ir_node
*elem
, *array
, *index
;
4436 GLint elemSize
, arrayLen
;
4438 /* size of array element */
4439 slang_typeinfo_construct(&elem_ti
);
4440 typeof_operation(A
, oper
, &elem_ti
);
4441 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
4443 if (_slang_type_is_matrix(array_ti
.spec
.type
))
4444 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
4446 arrayLen
= array_ti
.array_len
;
4448 slang_typeinfo_destruct(&array_ti
);
4449 slang_typeinfo_destruct(&elem_ti
);
4451 if (elemSize
<= 0) {
4452 /* unknown var or type */
4453 slang_info_log_error(A
->log
, "Undefined variable or type");
4457 array
= _slang_gen_operation(A
, &oper
->children
[0]);
4458 index
= _slang_gen_operation(A
, &oper
->children
[1]);
4459 if (array
&& index
) {
4461 GLint constIndex
= -1;
4462 if (index
->Opcode
== IR_FLOAT
) {
4463 constIndex
= (int) index
->Value
[0];
4464 if (constIndex
< 0 || constIndex
>= arrayLen
) {
4465 slang_info_log_error(A
->log
,
4466 "Array index out of bounds (index=%d size=%d)",
4467 constIndex
, arrayLen
);
4468 _slang_free_ir_tree(array
);
4469 _slang_free_ir_tree(index
);
4474 if (!array
->Store
) {
4475 slang_info_log_error(A
->log
, "Invalid array");
4479 elem
= new_node2(IR_ELEMENT
, array
, index
);
4481 /* The storage info here will be updated during code emit */
4482 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
4483 array
->Store
->Index
,
4485 elem
->Store
->Swizzle
= _slang_var_swizzle(elemSize
, 0);
4489 _slang_free_ir_tree(array
);
4490 _slang_free_ir_tree(index
);
4497 static slang_ir_node
*
4498 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
4499 slang_ir_opcode opcode
)
4501 slang_typeinfo t0
, t1
;
4504 slang_typeinfo_construct(&t0
);
4505 typeof_operation(A
, &oper
->children
[0], &t0
);
4507 slang_typeinfo_construct(&t1
);
4508 typeof_operation(A
, &oper
->children
[0], &t1
);
4510 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
4511 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
4512 slang_info_log_error(A
->log
, "Illegal array comparison");
4516 if (oper
->type
!= SLANG_OPER_EQUAL
&&
4517 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
4518 /* <, <=, >, >= can only be used with scalars */
4519 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
4520 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
4521 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
4522 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
4523 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
4528 n
= new_node2(opcode
,
4529 _slang_gen_operation(A
, &oper
->children
[0]),
4530 _slang_gen_operation(A
, &oper
->children
[1]));
4532 /* result is a bool (size 1) */
4533 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
4541 print_vars(slang_variable_scope
*s
)
4545 for (i
= 0; i
< s
->num_variables
; i
++) {
4547 (char*) s
->variables
[i
]->a_name
,
4548 s
->variables
[i
]->declared
);
4558 _slang_undeclare_vars(slang_variable_scope
*locals
)
4560 if (locals
->num_variables
> 0) {
4562 for (i
= 0; i
< locals
->num_variables
; i
++) {
4563 slang_variable
*v
= locals
->variables
[i
];
4564 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
4565 v
->declared
= GL_FALSE
;
4573 * Generate IR tree for a slang_operation (AST node)
4575 static slang_ir_node
*
4576 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
4578 switch (oper
->type
) {
4579 case SLANG_OPER_BLOCK_NEW_SCOPE
:
4583 _slang_push_var_table(A
->vartable
);
4585 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
4586 n
= _slang_gen_operation(A
, oper
);
4587 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
4589 _slang_pop_var_table(A
->vartable
);
4591 /*_slang_undeclare_vars(oper->locals);*/
4592 /*print_vars(oper->locals);*/
4595 n
= new_node1(IR_SCOPE
, n
);
4600 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
4601 /* list of operations */
4602 if (oper
->num_children
> 0)
4604 slang_ir_node
*n
, *tree
= NULL
;
4607 for (i
= 0; i
< oper
->num_children
; i
++) {
4608 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4610 _slang_free_ir_tree(tree
);
4611 return NULL
; /* error must have occured */
4613 tree
= new_seq(tree
, n
);
4619 return new_node0(IR_NOP
);
4622 case SLANG_OPER_EXPRESSION
:
4623 return _slang_gen_operation(A
, &oper
->children
[0]);
4625 case SLANG_OPER_FOR
:
4626 return _slang_gen_for(A
, oper
);
4628 return _slang_gen_do(A
, oper
);
4629 case SLANG_OPER_WHILE
:
4630 return _slang_gen_while(A
, oper
);
4631 case SLANG_OPER_BREAK
:
4633 slang_info_log_error(A
->log
, "'break' not in loop");
4636 return new_break(A
->CurLoop
);
4637 case SLANG_OPER_CONTINUE
:
4639 slang_info_log_error(A
->log
, "'continue' not in loop");
4642 return _slang_gen_continue(A
, oper
);
4643 case SLANG_OPER_DISCARD
:
4644 return new_node0(IR_KILL
);
4646 case SLANG_OPER_EQUAL
:
4647 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
4648 case SLANG_OPER_NOTEQUAL
:
4649 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
4650 case SLANG_OPER_GREATER
:
4651 return _slang_gen_compare(A
, oper
, IR_SGT
);
4652 case SLANG_OPER_LESS
:
4653 return _slang_gen_compare(A
, oper
, IR_SLT
);
4654 case SLANG_OPER_GREATEREQUAL
:
4655 return _slang_gen_compare(A
, oper
, IR_SGE
);
4656 case SLANG_OPER_LESSEQUAL
:
4657 return _slang_gen_compare(A
, oper
, IR_SLE
);
4658 case SLANG_OPER_ADD
:
4661 assert(oper
->num_children
== 2);
4662 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
4665 case SLANG_OPER_SUBTRACT
:
4668 assert(oper
->num_children
== 2);
4669 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4672 case SLANG_OPER_MULTIPLY
:
4675 assert(oper
->num_children
== 2);
4676 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
4679 case SLANG_OPER_DIVIDE
:
4682 assert(oper
->num_children
== 2);
4683 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
4686 case SLANG_OPER_MINUS
:
4689 assert(oper
->num_children
== 1);
4690 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
4693 case SLANG_OPER_PLUS
:
4694 /* +expr --> do nothing */
4695 return _slang_gen_operation(A
, &oper
->children
[0]);
4696 case SLANG_OPER_VARIABLE_DECL
:
4697 return _slang_gen_declaration(A
, oper
);
4698 case SLANG_OPER_ASSIGN
:
4699 return _slang_gen_assignment(A
, oper
);
4700 case SLANG_OPER_ADDASSIGN
:
4703 assert(oper
->num_children
== 2);
4704 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
4707 case SLANG_OPER_SUBASSIGN
:
4710 assert(oper
->num_children
== 2);
4711 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
4715 case SLANG_OPER_MULASSIGN
:
4718 assert(oper
->num_children
== 2);
4719 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
4722 case SLANG_OPER_DIVASSIGN
:
4725 assert(oper
->num_children
== 2);
4726 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
4729 case SLANG_OPER_LOGICALAND
:
4732 assert(oper
->num_children
== 2);
4733 n
= _slang_gen_logical_and(A
, oper
);
4736 case SLANG_OPER_LOGICALOR
:
4739 assert(oper
->num_children
== 2);
4740 n
= _slang_gen_logical_or(A
, oper
);
4743 case SLANG_OPER_LOGICALXOR
:
4744 return _slang_gen_xor(A
, oper
);
4745 case SLANG_OPER_NOT
:
4746 return _slang_gen_not(A
, oper
);
4747 case SLANG_OPER_SELECT
: /* b ? x : y */
4750 assert(oper
->num_children
== 3);
4751 n
= _slang_gen_select(A
, oper
);
4755 case SLANG_OPER_ASM
:
4756 return _slang_gen_asm(A
, oper
, NULL
);
4757 case SLANG_OPER_CALL
:
4758 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
4760 case SLANG_OPER_METHOD
:
4761 return _slang_gen_method_call(A
, oper
);
4762 case SLANG_OPER_RETURN
:
4763 return _slang_gen_return(A
, oper
);
4764 case SLANG_OPER_LABEL
:
4765 return new_label(oper
->label
);
4766 case SLANG_OPER_IDENTIFIER
:
4767 return _slang_gen_variable(A
, oper
);
4769 return _slang_gen_if(A
, oper
);
4770 case SLANG_OPER_FIELD
:
4771 return _slang_gen_struct_field(A
, oper
);
4772 case SLANG_OPER_SUBSCRIPT
:
4773 return _slang_gen_array_element(A
, oper
);
4774 case SLANG_OPER_LITERAL_FLOAT
:
4776 case SLANG_OPER_LITERAL_INT
:
4778 case SLANG_OPER_LITERAL_BOOL
:
4779 return new_float_literal(oper
->literal
, oper
->literal_size
);
4781 case SLANG_OPER_POSTINCREMENT
: /* var++ */
4784 assert(oper
->num_children
== 1);
4785 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
4788 case SLANG_OPER_POSTDECREMENT
: /* var-- */
4791 assert(oper
->num_children
== 1);
4792 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
4795 case SLANG_OPER_PREINCREMENT
: /* ++var */
4798 assert(oper
->num_children
== 1);
4799 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
4802 case SLANG_OPER_PREDECREMENT
: /* --var */
4805 assert(oper
->num_children
== 1);
4806 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
4810 case SLANG_OPER_NON_INLINED_CALL
:
4811 case SLANG_OPER_SEQUENCE
:
4813 slang_ir_node
*tree
= NULL
;
4815 for (i
= 0; i
< oper
->num_children
; i
++) {
4816 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
4817 tree
= new_seq(tree
, n
);
4819 tree
->Store
= n
->Store
;
4821 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
4822 tree
= new_function_call(tree
, oper
->label
);
4827 case SLANG_OPER_NONE
:
4828 case SLANG_OPER_VOID
:
4829 /* returning NULL here would generate an error */
4830 return new_node0(IR_NOP
);
4833 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
4835 return new_node0(IR_NOP
);
4843 * Check if the given type specifier is a rectangular texture sampler.
4846 is_rect_sampler_spec(const slang_type_specifier
*spec
)
4848 while (spec
->_array
) {
4849 spec
= spec
->_array
;
4851 return spec
->type
== SLANG_SPEC_SAMPLER2DRECT
||
4852 spec
->type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
;
4858 * Called by compiler when a global variable has been parsed/compiled.
4859 * Here we examine the variable's type to determine what kind of register
4860 * storage will be used.
4862 * A uniform such as "gl_Position" will become the register specification
4863 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4864 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4866 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4867 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4868 * actual texture unit (as specified by the user calling glUniform1i()).
4871 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
4872 slang_unit_type type
)
4874 struct gl_program
*prog
= A
->program
;
4875 const char *varName
= (char *) var
->a_name
;
4876 GLboolean success
= GL_TRUE
;
4877 slang_ir_storage
*store
= NULL
;
4879 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4880 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4881 const GLint arrayLen
= _slang_array_length(var
);
4882 const GLint totalSize
= _slang_array_size(size
, arrayLen
);
4883 GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4885 /* check for sampler2D arrays */
4886 if (texIndex
== -1 && var
->type
.specifier
._array
)
4887 texIndex
= sampler_to_texture_index(var
->type
.specifier
._array
->type
);
4889 if (texIndex
!= -1) {
4890 /* This is a texture sampler variable...
4891 * store->File = PROGRAM_SAMPLER
4892 * store->Index = sampler number (0..7, typically)
4893 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4895 if (var
->initializer
) {
4896 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4899 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4900 /* disallow rect samplers */
4901 if (is_rect_sampler_spec(&var
->type
.specifier
)) {
4902 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4906 (void) is_rect_sampler_spec
; /* silence warning */
4909 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4910 store
= _slang_new_ir_storage_sampler(sampNum
, texIndex
, totalSize
);
4912 /* If we have a sampler array, then we need to allocate the
4913 * additional samplers to ensure we don't allocate them elsewhere.
4914 * We can't directly use _mesa_add_sampler() as that checks the
4915 * varName and gets a match, so we call _mesa_add_parameter()
4916 * directly and use the last sampler number from the call above.
4919 GLint a
= arrayLen
- 1;
4921 for (i
= 0; i
< a
; i
++) {
4922 GLfloat value
= (GLfloat
)(i
+ sampNum
+ 1);
4923 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_SAMPLER
,
4924 varName
, 1, datatype
, &value
, NULL
, 0x0);
4928 if (dbg
) printf("SAMPLER ");
4930 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4931 /* Uniform variable */
4932 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4935 /* user-defined uniform */
4936 if (datatype
== GL_NONE
) {
4937 if ((var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
&&
4938 var
->type
.specifier
._array
->type
== SLANG_SPEC_STRUCT
) ||
4939 (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
)) {
4940 /* temporary work-around */
4941 GLenum datatype
= GL_FLOAT
;
4942 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4943 totalSize
, datatype
, NULL
);
4944 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4945 totalSize
, swizzle
);
4948 GLint a
= arrayLen
- 1;
4950 for (i
= 0; i
< a
; i
++) {
4951 GLfloat value
= (GLfloat
)(i
+ uniformLoc
+ 1);
4952 (void) _mesa_add_parameter(prog
->Parameters
, PROGRAM_UNIFORM
,
4953 varName
, 1, datatype
, &value
, NULL
, 0x0);
4957 /* XXX what we need to do is unroll the struct into its
4958 * basic types, creating a uniform variable for each.
4966 * Should produce uniforms:
4967 * "f.a" (GL_FLOAT_VEC3)
4968 * "f.b" (GL_FLOAT_VEC4)
4971 if (var
->initializer
) {
4972 slang_info_log_error(A
->log
,
4973 "unsupported initializer for uniform '%s'", varName
);
4978 slang_info_log_error(A
->log
,
4979 "invalid datatype for uniform variable %s",
4985 /* non-struct uniform */
4986 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4992 /* pre-defined uniform, like gl_ModelviewMatrix */
4993 /* We know it's a uniform, but don't allocate storage unless
4996 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4997 totalSize
, swizzle
);
4999 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
5001 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
5002 /* varyings must be float, vec or mat */
5003 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
5004 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
5005 slang_info_log_error(A
->log
,
5006 "varying '%s' must be float/vector/matrix",
5011 if (var
->initializer
) {
5012 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
5018 /* user-defined varying */
5024 if (var
->type
.centroid
== SLANG_CENTROID
)
5025 flags
|= PROG_PARAM_BIT_CENTROID
;
5026 if (var
->type
.variant
== SLANG_INVARIANT
)
5027 flags
|= PROG_PARAM_BIT_INVARIANT
;
5029 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
5031 swizzle
= _slang_var_swizzle(size
, 0);
5032 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
5033 totalSize
, swizzle
);
5036 /* pre-defined varying, like gl_Color or gl_TexCoord */
5037 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
5038 /* fragment program input */
5040 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5043 assert(index
< FRAG_ATTRIB_MAX
);
5044 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
5048 /* vertex program output */
5049 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5050 GLuint swizzle
= _slang_var_swizzle(size
, 0);
5052 assert(index
< VERT_RESULT_MAX
);
5053 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
5054 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
5057 if (dbg
) printf("V/F ");
5059 if (dbg
) printf("VARYING ");
5061 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
5064 /* attributes must be float, vec or mat */
5065 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
5066 slang_info_log_error(A
->log
,
5067 "attribute '%s' must be float/vector/matrix",
5073 /* user-defined vertex attribute */
5074 const GLint attr
= -1; /* unknown */
5075 swizzle
= _slang_var_swizzle(size
, 0);
5076 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
5077 size
, datatype
, attr
);
5079 index
= VERT_ATTRIB_GENERIC0
+ index
;
5082 /* pre-defined vertex attrib */
5083 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
5086 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5087 if (dbg
) printf("ATTRIB ");
5089 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
5090 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
5091 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
5093 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
5094 if (dbg
) printf("INPUT ");
5096 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
5097 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
5098 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
5099 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
5102 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
5103 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
5104 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
5105 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
5107 if (dbg
) printf("OUTPUT ");
5109 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
5110 /* pre-defined global constant, like gl_MaxLights */
5111 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
5112 if (dbg
) printf("CONST ");
5115 /* ordinary variable (may be const) */
5118 /* IR node to declare the variable */
5119 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
5121 /* emit GPU instructions */
5122 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_FALSE
, A
->log
);
5124 _slang_free_ir_tree(n
);
5127 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
5128 store
? store
->Index
: -2);
5131 var
->store
= store
; /* save var's storage info */
5133 var
->declared
= GL_TRUE
;
5140 * Produce an IR tree from a function AST (fun->body).
5141 * Then call the code emitter to convert the IR tree into gl_program
5145 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
5148 GLboolean success
= GL_TRUE
;
5150 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
5151 /* we only really generate code for main, all other functions get
5152 * inlined or codegen'd upon an actual call.
5155 /* do some basic error checking though */
5156 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
5157 /* check that non-void functions actually return something */
5159 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
5161 slang_info_log_error(A
->log
,
5162 "function \"%s\" has no return statement",
5163 (char *) fun
->header
.a_name
);
5165 "function \"%s\" has no return statement\n",
5166 (char *) fun
->header
.a_name
);
5171 return GL_TRUE
; /* not an error */
5175 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
5176 slang_print_function(fun
, 1);
5179 /* should have been allocated earlier: */
5180 assert(A
->program
->Parameters
);
5181 assert(A
->program
->Varying
);
5182 assert(A
->vartable
);
5184 A
->CurLoopOper
= NULL
;
5185 A
->CurFunction
= fun
;
5187 /* fold constant expressions, etc. */
5188 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
5191 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
5192 slang_print_function(fun
, 1);
5195 /* Create an end-of-function label */
5196 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
5198 /* push new vartable scope */
5199 _slang_push_var_table(A
->vartable
);
5201 /* Generate IR tree for the function body code */
5202 n
= _slang_gen_operation(A
, fun
->body
);
5204 n
= new_node1(IR_SCOPE
, n
);
5206 /* pop vartable, restore previous */
5207 _slang_pop_var_table(A
->vartable
);
5210 /* XXX record error */
5214 /* append an end-of-function-label to IR tree */
5215 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
5217 /*_slang_label_delete(A->curFuncEndLabel);*/
5218 A
->curFuncEndLabel
= NULL
;
5221 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
5222 slang_print_function(fun
, 1);
5225 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
5226 _slang_print_ir_tree(n
, 0);
5229 printf("************* End codegen function ************\n\n");
5232 if (A
->UnresolvedRefs
) {
5233 /* Can't codegen at this time.
5234 * At link time we'll concatenate all the vertex shaders and/or all
5235 * the fragment shaders and try recompiling.
5240 /* Emit program instructions */
5241 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, A
->pragmas
, GL_TRUE
, A
->log
);
5242 _slang_free_ir_tree(n
);
5244 /* free codegen context */
5246 _mesa_free(A->codegen);