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);
79 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
80 GLuint substCount, slang_variable **substOld,
81 slang_operation **substNew, GLboolean isLHS);
85 * Retrieves type information about an operation.
86 * Returns GL_TRUE on success.
87 * Returns GL_FALSE otherwise.
90 typeof_operation(const struct slang_assemble_ctx_ *A,
94 return _slang_typeof_operation(op, &A->space, ti, A->atoms, A->log);
99 is_sampler_type(const slang_fully_specified_type *t)
101 switch (t->specifier.type) {
102 case SLANG_SPEC_SAMPLER1D:
103 case SLANG_SPEC_SAMPLER2D:
104 case SLANG_SPEC_SAMPLER3D:
105 case SLANG_SPEC_SAMPLERCUBE:
106 case SLANG_SPEC_SAMPLER1DSHADOW:
107 case SLANG_SPEC_SAMPLER2DSHADOW:
108 case SLANG_SPEC_SAMPLER2DRECT:
109 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
118 * Return the offset (in floats or ints) of the named field within
119 * the given struct. Return -1 if field not found.
120 * If field is NULL, return the size of the struct instead.
123 _slang_field_offset(const slang_type_specifier *spec, slang_atom field)
127 for (i = 0; i < spec->_struct->fields->num_variables; i++) {
128 const slang_variable *v = spec->_struct->fields->variables[i];
129 const GLuint sz = _slang_sizeof_type_specifier(&v->type.specifier);
131 /* types larger than 1 float are register (4-float) aligned */
132 offset = (offset + 3) & ~3;
134 if (field && v->a_name == field) {
140 return -1; /* field not found */
142 return offset; /* struct size */
147 * Return the size (in floats) of the given type specifier.
148 * If the size is greater than 4, the size should be a multiple of 4
149 * so that the correct number of 4-float registers are allocated.
150 * For example, a mat3x2 is size 12 because we want to store the
151 * 3 columns in 3 float[4] registers.
154 _slang_sizeof_type_specifier(const slang_type_specifier *spec)
157 switch (spec->type) {
158 case SLANG_SPEC_VOID:
161 case SLANG_SPEC_BOOL:
164 case SLANG_SPEC_BVEC2:
167 case SLANG_SPEC_BVEC3:
170 case SLANG_SPEC_BVEC4:
176 case SLANG_SPEC_IVEC2:
179 case SLANG_SPEC_IVEC3:
182 case SLANG_SPEC_IVEC4:
185 case SLANG_SPEC_FLOAT:
188 case SLANG_SPEC_VEC2:
191 case SLANG_SPEC_VEC3:
194 case SLANG_SPEC_VEC4:
197 case SLANG_SPEC_MAT2:
198 sz = 2 * 4; /* 2 columns (regs) */
200 case SLANG_SPEC_MAT3:
203 case SLANG_SPEC_MAT4:
206 case SLANG_SPEC_MAT23:
207 sz = 2 * 4; /* 2 columns (regs) */
209 case SLANG_SPEC_MAT32:
210 sz = 3 * 4; /* 3 columns (regs) */
212 case SLANG_SPEC_MAT24:
215 case SLANG_SPEC_MAT42:
216 sz = 4 * 4; /* 4 columns (regs) */
218 case SLANG_SPEC_MAT34:
221 case SLANG_SPEC_MAT43:
222 sz = 4 * 4; /* 4 columns (regs) */
224 case SLANG_SPEC_SAMPLER1D:
225 case SLANG_SPEC_SAMPLER2D:
226 case SLANG_SPEC_SAMPLER3D:
227 case SLANG_SPEC_SAMPLERCUBE:
228 case SLANG_SPEC_SAMPLER1DSHADOW:
229 case SLANG_SPEC_SAMPLER2DSHADOW:
230 case SLANG_SPEC_SAMPLER2DRECT:
231 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
232 sz = 1; /* a sampler is basically just an integer index */
234 case SLANG_SPEC_STRUCT:
235 sz = _slang_field_offset(spec, 0); /* special use */
237 /* 1-float structs are actually troublesome to deal with since they
238 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
239 * ensure the object is placed at R.x
244 sz = (sz + 3) & ~0x3; /* round up to multiple of four */
247 case SLANG_SPEC_ARRAY:
248 sz = _slang_sizeof_type_specifier(spec->_array);
251 _mesa_problem(NULL, "Unexpected type in _slang_sizeof_type_specifier()");
256 /* if size is > 4, it should be a multiple of four */
257 assert((sz & 0x3) == 0);
264 * Query variable/array length (number of elements).
265 * This is slightly non-trivial because there are two ways to express
266 * arrays: "float x[3]" vs. "float[3] x".
267 * \return the length of the array for the given variable, or 0 if not an array
270 _slang_array_length(const slang_variable *var)
272 if (var->type.array_len > 0) {
273 /* Ex: float[4] x; */
274 return var->type.array_len;
276 if (var->array_len > 0) {
277 /* Ex: float x[4]; */
278 return var->array_len;
285 * Compute total size of array give size of element, number of elements.
286 * \return size in floats
289 _slang_array_size(GLint elemSize, GLint arrayLen)
292 assert(elemSize > 0);
294 /* round up base type to multiple of 4 */
295 total = ((elemSize + 3) & ~0x3) * MAX2(arrayLen, 1);
305 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
306 * or -1 if the type is not a sampler.
309 sampler_to_texture_index(const slang_type_specifier_type type)
312 case SLANG_SPEC_SAMPLER1D:
313 return TEXTURE_1D_INDEX;
314 case SLANG_SPEC_SAMPLER2D:
315 return TEXTURE_2D_INDEX;
316 case SLANG_SPEC_SAMPLER3D:
317 return TEXTURE_3D_INDEX;
318 case SLANG_SPEC_SAMPLERCUBE:
319 return TEXTURE_CUBE_INDEX;
320 case SLANG_SPEC_SAMPLER1DSHADOW:
321 return TEXTURE_1D_INDEX; /* XXX fix */
322 case SLANG_SPEC_SAMPLER2DSHADOW:
323 return TEXTURE_2D_INDEX; /* XXX fix */
324 case SLANG_SPEC_SAMPLER2DRECT:
325 return TEXTURE_RECT_INDEX;
326 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
327 return TEXTURE_RECT_INDEX; /* XXX fix */
334 /** helper to build a SLANG_OPER_IDENTIFIER node */
336 slang_operation_identifier(slang_operation *oper,
337 slang_assemble_ctx *A,
340 oper->type = SLANG_OPER_IDENTIFIER;
341 oper->a_id = slang_atom_pool_atom(A->atoms, name);
345 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
348 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
349 * a vertex or fragment program input variable. Return -1 if the input
351 * XXX return size too
354 _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
361 static const struct input_info vertInputs[] = {
362 { "gl_Vertex", VERT_ATTRIB_POS, SWIZZLE_NOOP },
363 { "gl_Normal", VERT_ATTRIB_NORMAL, SWIZZLE_NOOP },
364 { "gl_Color", VERT_ATTRIB_COLOR0, SWIZZLE_NOOP },
365 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1, SWIZZLE_NOOP },
366 { "gl_FogCoord", VERT_ATTRIB_FOG, SWIZZLE_XXXX },
367 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0, SWIZZLE_NOOP },
368 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1, SWIZZLE_NOOP },
369 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2, SWIZZLE_NOOP },
370 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3, SWIZZLE_NOOP },
371 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4, SWIZZLE_NOOP },
372 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5, SWIZZLE_NOOP },
373 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6, SWIZZLE_NOOP },
374 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7, SWIZZLE_NOOP },
375 { NULL, 0, SWIZZLE_NOOP }
377 static const struct input_info fragInputs[] = {
378 { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
379 { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
380 { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
381 { "gl_TexCoord", FRAG_ATTRIB_TEX0, SWIZZLE_NOOP },
382 /* note: we're packing several quantities into the fogcoord vector */
383 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
384 { "gl_FrontFacing", FRAG_ATTRIB_FOGC, SWIZZLE_YYYY }, /*XXX*/
385 { "gl_PointCoord", FRAG_ATTRIB_FOGC, SWIZZLE_ZWWW },
386 { NULL, 0, SWIZZLE_NOOP }
389 const struct input_info *inputs
390 = (target == GL_VERTEX_PROGRAM_ARB) ? vertInputs : fragInputs;
392 ASSERT(MAX_TEXTURE_COORD_UNITS == 8); /* if this fails, fix vertInputs above */
394 for (i = 0; inputs[i].Name; i++) {
395 if (strcmp(inputs[i].Name, name) == 0) {
397 *swizzleOut = inputs[i].Swizzle;
398 return inputs[i].Attrib;
406 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
407 * a vertex or fragment program output variable. Return -1 for an invalid
411 _slang_output_index(const char *name, GLenum target)
417 static const struct output_info vertOutputs[] = {
418 { "gl_Position", VERT_RESULT_HPOS },
419 { "gl_FrontColor", VERT_RESULT_COL0 },
420 { "gl_BackColor", VERT_RESULT_BFC0 },
421 { "gl_FrontSecondaryColor", VERT_RESULT_COL1 },
422 { "gl_BackSecondaryColor", VERT_RESULT_BFC1 },
423 { "gl_TexCoord", VERT_RESULT_TEX0 },
424 { "gl_FogFragCoord", VERT_RESULT_FOGC },
425 { "gl_PointSize", VERT_RESULT_PSIZ },
428 static const struct output_info fragOutputs[] = {
429 { "gl_FragColor", FRAG_RESULT_COLOR },
430 { "gl_FragDepth", FRAG_RESULT_DEPTH },
431 { "gl_FragData", FRAG_RESULT_DATA0 },
435 const struct output_info *outputs
436 = (target == GL_VERTEX_PROGRAM_ARB) ? vertOutputs : fragOutputs;
438 for (i = 0; outputs[i].Name; i++) {
439 if (strcmp(outputs[i].Name, name) == 0) {
441 return outputs[i].Attrib;
449 * Called when we begin code/IR generation for a new while/do/for loop.
452 push_loop(slang_assemble_ctx *A, slang_operation *loopOper, slang_ir_node *loopIR)
454 A->LoopOperStack[A->LoopDepth] = loopOper;
455 A->LoopIRStack[A->LoopDepth] = loopIR;
461 * Called when we end code/IR generation for a new while/do/for loop.
464 pop_loop(slang_assemble_ctx *A)
466 assert(A->LoopDepth > 0);
472 * Return pointer to slang_operation for the loop we're currently inside,
473 * or NULL if not in a loop.
475 static const slang_operation *
476 current_loop_oper(const slang_assemble_ctx *A)
478 if (A->LoopDepth > 0)
479 return A->LoopOperStack[A->LoopDepth - 1];
486 * Return pointer to slang_ir_node for the loop we're currently inside,
487 * or NULL if not in a loop.
489 static slang_ir_node *
490 current_loop_ir(const slang_assemble_ctx *A)
492 if (A->LoopDepth > 0)
493 return A->LoopIRStack[A->LoopDepth - 1];
499 /**********************************************************************/
503 * Map "_asm foo" to IR_FOO, etc.
508 slang_ir_opcode Opcode;
509 GLuint HaveRetValue, NumParams;
513 static slang_asm_info AsmInfo[] = {
515 { "vec4_add", IR_ADD, 1, 2 },
516 { "vec4_subtract", IR_SUB, 1, 2 },
517 { "vec4_multiply", IR_MUL, 1, 2 },
518 { "vec4_dot", IR_DOT4, 1, 2 },
519 { "vec3_dot", IR_DOT3, 1, 2 },
520 { "vec2_dot", IR_DOT2, 1, 2 },
521 { "vec3_nrm", IR_NRM3, 1, 1 },
522 { "vec4_nrm", IR_NRM4, 1, 1 },
523 { "vec3_cross", IR_CROSS, 1, 2 },
524 { "vec4_lrp", IR_LRP, 1, 3 },
525 { "vec4_min", IR_MIN, 1, 2 },
526 { "vec4_max", IR_MAX, 1, 2 },
527 { "vec4_clamp", IR_CLAMP, 1, 3 },
528 { "vec4_seq", IR_SEQUAL, 1, 2 },
529 { "vec4_sne", IR_SNEQUAL, 1, 2 },
530 { "vec4_sge", IR_SGE, 1, 2 },
531 { "vec4_sgt", IR_SGT, 1, 2 },
532 { "vec4_sle", IR_SLE, 1, 2 },
533 { "vec4_slt", IR_SLT, 1, 2 },
535 { "vec4_move", IR_MOVE, 1, 1 },
536 { "vec4_floor", IR_FLOOR, 1, 1 },
537 { "vec4_frac", IR_FRAC, 1, 1 },
538 { "vec4_abs", IR_ABS, 1, 1 },
539 { "vec4_negate", IR_NEG, 1, 1 },
540 { "vec4_ddx", IR_DDX, 1, 1 },
541 { "vec4_ddy", IR_DDY, 1, 1 },
542 /* float binary op */
543 { "float_power", IR_POW, 1, 2 },
544 /* texture / sampler */
545 { "vec4_tex_1d", IR_TEX, 1, 2 },
546 { "vec4_tex_1d_bias", IR_TEXB, 1, 2 }, /* 1d w/ bias */
547 { "vec4_tex_1d_proj", IR_TEXP, 1, 2 }, /* 1d w/ projection */
548 { "vec4_tex_2d", IR_TEX, 1, 2 },
549 { "vec4_tex_2d_bias", IR_TEXB, 1, 2 }, /* 2d w/ bias */
550 { "vec4_tex_2d_proj", IR_TEXP, 1, 2 }, /* 2d w/ projection */
551 { "vec4_tex_3d", IR_TEX, 1, 2 },
552 { "vec4_tex_3d_bias", IR_TEXB, 1, 2 }, /* 3d w/ bias */
553 { "vec4_tex_3d_proj", IR_TEXP, 1, 2 }, /* 3d w/ projection */
554 { "vec4_tex_cube", IR_TEX, 1, 2 }, /* cubemap */
555 { "vec4_tex_rect", IR_TEX, 1, 2 }, /* rectangle */
556 { "vec4_tex_rect_bias", IR_TEX, 1, 2 }, /* rectangle w/ projection */
558 /* texture / sampler but with shadow comparison */
559 { "vec4_tex_1d_shadow", IR_TEX_SH, 1, 2 },
560 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH, 1, 2 },
561 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH, 1, 2 },
562 { "vec4_tex_2d_shadow", IR_TEX_SH, 1, 2 },
563 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH, 1, 2 },
564 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH, 1, 2 },
565 { "vec4_tex_rect_shadow", IR_TEX_SH, 1, 2 },
566 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH, 1, 2 },
569 { "ivec4_to_vec4", IR_I_TO_F, 1, 1 }, /* int[4] to float[4] */
570 { "vec4_to_ivec4", IR_F_TO_I, 1, 1 }, /* float[4] to int[4] */
571 { "float_exp", IR_EXP, 1, 1 },
572 { "float_exp2", IR_EXP2, 1, 1 },
573 { "float_log2", IR_LOG2, 1, 1 },
574 { "float_rsq", IR_RSQ, 1, 1 },
575 { "float_rcp", IR_RCP, 1, 1 },
576 { "float_sine", IR_SIN, 1, 1 },
577 { "float_cosine", IR_COS, 1, 1 },
578 { "float_noise1", IR_NOISE1, 1, 1},
579 { "float_noise2", IR_NOISE2, 1, 1},
580 { "float_noise3", IR_NOISE3, 1, 1},
581 { "float_noise4", IR_NOISE4, 1, 1},
583 { NULL, IR_NOP, 0, 0 }
587 static slang_ir_node *
588 new_node3(slang_ir_opcode op,
589 slang_ir_node *c0, slang_ir_node *c1, slang_ir_node *c2)
591 slang_ir_node *n = (slang_ir_node *) _slang_alloc(sizeof(slang_ir_node));
597 n->InstLocation = -1;
602 static slang_ir_node *
603 new_node2(slang_ir_opcode op, slang_ir_node *c0, slang_ir_node *c1)
605 return new_node3(op, c0, c1, NULL);
608 static slang_ir_node *
609 new_node1(slang_ir_opcode op, slang_ir_node *c0)
611 return new_node3(op, c0, NULL, NULL);
614 static slang_ir_node *
615 new_node0(slang_ir_opcode op)
617 return new_node3(op, NULL, NULL, NULL);
622 * Create sequence of two nodes.
624 static slang_ir_node *
625 new_seq(slang_ir_node *left, slang_ir_node *right)
631 return new_node2(IR_SEQ, left, right);
634 static slang_ir_node *
635 new_label(slang_label *label)
637 slang_ir_node *n = new_node0(IR_LABEL);
644 static slang_ir_node *
645 new_float_literal(const float v[4], GLuint size)
647 slang_ir_node *n = new_node0(IR_FLOAT);
649 COPY_4V(n->Value, v);
650 /* allocate a storage object, but compute actual location (Index) later */
651 n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
656 static slang_ir_node *
657 new_not(slang_ir_node *n)
659 return new_node1(IR_NOT, n);
664 * Non-inlined function call.
666 static slang_ir_node *
667 new_function_call(slang_ir_node *code, slang_label *name)
669 slang_ir_node *n = new_node1(IR_CALL, code);
678 * Unconditional jump.
680 static slang_ir_node *
681 new_return(slang_label *dest)
683 slang_ir_node *n = new_node0(IR_RETURN);
691 static slang_ir_node *
692 new_loop(slang_ir_node *body)
694 return new_node1(IR_LOOP, body);
698 static slang_ir_node *
699 new_break(slang_ir_node *loopNode)
701 slang_ir_node *n = new_node0(IR_BREAK);
703 assert(loopNode->Opcode == IR_LOOP);
705 /* insert this node at head of linked list of cont/break instructions */
706 n->List = loopNode->List;
714 * Make new IR_BREAK_IF_TRUE.
716 static slang_ir_node *
717 new_break_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
719 slang_ir_node *loopNode = current_loop_ir(A);
722 assert(loopNode->Opcode == IR_LOOP);
723 n = new_node1(IR_BREAK_IF_TRUE, cond);
725 /* insert this node at head of linked list of cont/break instructions */
726 n->List = loopNode->List;
734 * Make new IR_CONT_IF_TRUE node.
736 static slang_ir_node *
737 new_cont_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
739 slang_ir_node *loopNode = current_loop_ir(A);
742 assert(loopNode->Opcode == IR_LOOP);
743 n = new_node1(IR_CONT_IF_TRUE, cond);
745 n->Parent = loopNode; /* pointer to containing loop */
746 /* insert this node at head of linked list of cont/break instructions */
747 n->List = loopNode->List;
754 static slang_ir_node *
755 new_cond(slang_ir_node *n)
757 slang_ir_node *c = new_node1(IR_COND, n);
762 static slang_ir_node *
763 new_if(slang_ir_node *cond, slang_ir_node *ifPart, slang_ir_node *elsePart)
765 return new_node3(IR_IF, cond, ifPart, elsePart);
770 * New IR_VAR node - a reference to a previously declared variable.
772 static slang_ir_node *
773 new_var(slang_assemble_ctx *A, slang_variable *var)
775 slang_ir_node *n = new_node0(IR_VAR);
782 /* Set IR node's Var and Store pointers */
784 n->Store = var->store;
791 * Check if the given function is really just a wrapper for a
792 * basic assembly instruction.
795 slang_is_asm_function(const slang_function *fun)
797 if (fun->body->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE &&
798 fun->body->num_children == 1 &&
799 fun->body->children[0].type == SLANG_OPER_ASM) {
807 _slang_is_noop(const slang_operation *oper)
810 oper->type == SLANG_OPER_VOID ||
811 (oper->num_children == 1 && oper->children[0].type == SLANG_OPER_VOID))
819 * Recursively search tree for a node of the given type.
822 static slang_operation *
823 _slang_find_node_type(slang_operation *oper, slang_operation_type type)
826 if (oper->type == type)
828 for (i = 0; i < oper->num_children; i++) {
829 slang_operation *p = _slang_find_node_type(&oper->children[i], type);
839 * Count the number of operations of the given time rooted at 'oper'.
842 _slang_count_node_type(const slang_operation *oper, slang_operation_type type)
845 if (oper->type == type) {
848 for (i = 0; i < oper->num_children; i++) {
849 count += _slang_count_node_type(&oper->children[i], type);
856 * Check if the 'return' statement found under 'oper' is a "tail return"
857 * that can be no-op'd. For example:
862 * return; // this is a no-op
865 * This is used when determining if a function can be inlined. If the
866 * 'return' is not the last statement, we can't inline the function since
867 * we still need the semantic behaviour of the 'return' but we don't want
868 * to accidentally return from the _calling_ function. We'd need to use an
869 * unconditional branch, but we don't have such a GPU instruction (not
873 _slang_is_tail_return(const slang_operation *oper)
875 GLuint k = oper->num_children;
878 const slang_operation *last = &oper->children[k - 1];
879 if (last->type == SLANG_OPER_RETURN)
881 else if (last->type == SLANG_OPER_IDENTIFIER ||
882 last->type == SLANG_OPER_LABEL)
883 k--; /* try prev child */
884 else if (last->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
885 last->type == SLANG_OPER_BLOCK_NEW_SCOPE)
886 /* try sub-children */
887 return _slang_is_tail_return(last);
897 * Generate a variable declaration opeartion.
898 * I.e.: generate AST code for "bool flag = false;"
901 slang_generate_declaration(slang_assemble_ctx *A,
902 slang_variable_scope *scope,
903 slang_operation *decl,
904 slang_type_specifier_type type,
910 assert(type == SLANG_SPEC_BOOL ||
911 type == SLANG_SPEC_INT);
913 decl->type = SLANG_OPER_VARIABLE_DECL;
915 var = slang_variable_scope_grow(scope);
917 slang_fully_specified_type_construct(&var->type);
919 var->type.specifier.type = type;
920 var->a_name = slang_atom_pool_atom(A->atoms, name);
921 decl->a_id = var->a_name;
922 var->initializer = slang_operation_new(1);
923 slang_operation_literal_bool(var->initializer, initValue);
928 slang_resolve_variable(slang_operation *oper)
930 if (oper->type == SLANG_OPER_IDENTIFIER && !oper->var) {
931 oper->var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
937 * Rewrite AST code for "return expression;".
939 * We return values from functions by assinging the returned value to
940 * the hidden __retVal variable which is an extra 'out' parameter we add
941 * to the function signature.
942 * This code basically converts "return expr;" into "__retVal = expr; return;"
944 * \return the new AST code.
946 static slang_operation *
947 gen_return_with_expression(slang_assemble_ctx *A, slang_operation *oper)
949 slang_operation *blockOper, *assignOper;
951 assert(oper->type == SLANG_OPER_RETURN);
953 if (A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
954 slang_info_log_error(A->log, "illegal return expression");
958 blockOper = slang_operation_new(1);
959 blockOper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
960 blockOper->locals->outer_scope = oper->locals->outer_scope;
961 slang_operation_add_children(blockOper, 2);
963 if (A->UseReturnFlag) {
972 slang_operation *ifOper = slang_oper_child(blockOper, 0);
973 ifOper->type = SLANG_OPER_IF;
974 slang_operation_add_children(ifOper, 3);
976 slang_operation *cond = slang_oper_child(ifOper, 0);
977 cond->type = SLANG_OPER_IDENTIFIER;
978 cond->a_id = slang_atom_pool_atom(A->atoms, "__notRetFlag");
981 slang_operation *elseOper = slang_oper_child(ifOper, 2);
982 elseOper->type = SLANG_OPER_VOID;
984 assignOper = slang_oper_child(ifOper, 1);
987 slang_operation *setOper = slang_oper_child(blockOper, 1);
988 setOper->type = SLANG_OPER_ASSIGN;
989 slang_operation_add_children(setOper, 2);
991 slang_operation *lhs = slang_oper_child(setOper, 0);
992 lhs->type = SLANG_OPER_IDENTIFIER;
993 lhs->a_id = slang_atom_pool_atom(A->atoms, "__notRetFlag");
996 slang_operation *rhs = slang_oper_child(setOper, 1);
997 slang_operation_literal_bool(rhs, GL_FALSE);
1008 assignOper = slang_oper_child(blockOper, 0);
1010 slang_operation *returnOper = slang_oper_child(blockOper, 1);
1011 returnOper->type = SLANG_OPER_RETURN_INLINED;
1012 assert(returnOper->num_children == 0);
1016 /* __retVal = expression; */
1017 assignOper->type = SLANG_OPER_ASSIGN;
1018 slang_operation_add_children(assignOper, 2);
1020 slang_operation *lhs = slang_oper_child(assignOper, 0);
1021 lhs->type = SLANG_OPER_IDENTIFIER;
1022 lhs->a_id = slang_atom_pool_atom(A->atoms, "__retVal");
1025 slang_operation *rhs = slang_oper_child(assignOper, 1);
1026 slang_operation_copy(rhs, &oper->children[0]);
1029 ///blockOper->locals->outer_scope = oper->locals->outer_scope;
1031 /*slang_print_tree(blockOper, 0);*/
1038 * Rewrite AST code for "return;" (no expression).
1040 static slang_operation *
1041 gen_return_without_expression(slang_assemble_ctx *A, slang_operation *oper)
1043 slang_operation *newRet;
1045 assert(oper->type == SLANG_OPER_RETURN);
1047 if (A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
1048 slang_info_log_error(A->log, "return statement requires an expression");
1052 if (A->UseReturnFlag) {
1057 newRet = slang_operation_new(1);
1058 newRet->locals->outer_scope = oper->locals->outer_scope;
1059 newRet->type = SLANG_OPER_ASSIGN;
1060 slang_operation_add_children(newRet, 2);
1062 slang_operation *lhs = slang_oper_child(newRet, 0);
1063 lhs->type = SLANG_OPER_IDENTIFIER;
1064 lhs->a_id = slang_atom_pool_atom(A->atoms, "__notRetFlag");
1067 slang_operation *rhs = slang_oper_child(newRet, 1);
1068 slang_operation_literal_bool(rhs, GL_FALSE);
1076 newRet = slang_operation_new(1);
1077 newRet->locals->outer_scope = oper->locals->outer_scope;
1078 newRet->type = SLANG_OPER_RETURN_INLINED;
1081 /*slang_print_tree(newRet, 0);*/
1090 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1093 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
1094 GLuint substCount, slang_variable **substOld,
1095 slang_operation **substNew, GLboolean isLHS)
1097 switch (oper->type) {
1098 case SLANG_OPER_VARIABLE_DECL:
1100 slang_variable *v = _slang_variable_locate(oper->locals,
1101 oper->a_id, GL_TRUE);
1103 if (v->initializer && oper->num_children == 0) {
1104 /* set child of oper to copy of initializer */
1105 oper->num_children = 1;
1106 oper->children = slang_operation_new(1);
1107 slang_operation_copy(&oper->children[0], v->initializer);
1109 if (oper->num_children == 1) {
1110 /* the initializer */
1111 slang_substitute(A, &oper->children[0], substCount,
1112 substOld, substNew, GL_FALSE);
1116 case SLANG_OPER_IDENTIFIER:
1117 assert(oper->num_children == 0);
1118 if (1/**!isLHS XXX FIX */) {
1119 slang_atom id = oper->a_id;
1122 v = _slang_variable_locate(oper->locals, id, GL_TRUE);
1124 if (_mesa_strcmp((char *) oper->a_id, "__notRetFlag"))
1125 _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
1129 /* look for a substitution */
1130 for (i = 0; i < substCount; i++) {
1131 if (v == substOld[i]) {
1132 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
1133 #if 0 /* DEBUG only */
1134 if (substNew[i]->type == SLANG_OPER_IDENTIFIER) {
1135 assert(substNew[i]->var);
1136 assert(substNew[i]->var->a_name);
1137 printf("Substitute %s with %s in id node %p\n",
1138 (char*)v->a_name, (char*) substNew[i]->var->a_name,
1142 printf("Substitute %s with %f in id node %p\n",
1143 (char*)v->a_name, substNew[i]->literal[0],
1147 slang_operation_copy(oper, substNew[i]);
1154 case SLANG_OPER_RETURN:
1156 slang_operation *newReturn;
1157 /* generate new 'return' code' */
1158 if (slang_oper_child(oper, 0)->type == SLANG_OPER_VOID)
1159 newReturn = gen_return_without_expression(A, oper);
1161 newReturn = gen_return_with_expression(A, oper);
1166 /* do substitutions on the new 'return' code */
1167 slang_substitute(A, newReturn,
1168 substCount, substOld, substNew, GL_FALSE);
1170 /* install new 'return' code */
1171 slang_operation_copy(oper, newReturn);
1172 slang_operation_destruct(newReturn);
1176 case SLANG_OPER_ASSIGN:
1177 case SLANG_OPER_SUBSCRIPT:
1179 * child[0] can't have substitutions but child[1] can.
1181 slang_substitute(A, &oper->children[0],
1182 substCount, substOld, substNew, GL_TRUE);
1183 slang_substitute(A, &oper->children[1],
1184 substCount, substOld, substNew, GL_FALSE);
1186 case SLANG_OPER_FIELD:
1187 /* XXX NEW - test */
1188 slang_substitute(A, &oper->children[0],
1189 substCount, substOld, substNew, GL_TRUE);
1194 for (i = 0; i < oper->num_children; i++)
1195 slang_substitute(A, &oper->children[i],
1196 substCount, substOld, substNew, GL_FALSE);
1203 * Produce inline code for a call to an assembly instruction.
1204 * This is typically used to compile a call to a built-in function like this:
1206 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1208 * __asm vec4_lrp __retVal, a, y, x;
1213 * r = mix(p1, p2, p3);
1223 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1225 static slang_operation *
1226 slang_inline_asm_function(slang_assemble_ctx *A,
1227 slang_function *fun, slang_operation *oper)
1229 const GLuint numArgs = oper->num_children;
1231 slang_operation *inlined;
1232 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1233 slang_variable **substOld;
1234 slang_operation **substNew;
1236 ASSERT(slang_is_asm_function(fun));
1237 ASSERT(fun->param_count == numArgs + haveRetValue);
1240 printf("Inline %s as %s\n",
1241 (char*) fun->header.a_name,
1242 (char*) fun->body->children[0].a_id);
1246 * We'll substitute formal params with actual args in the asm call.
1248 substOld = (slang_variable **)
1249 _slang_alloc(numArgs * sizeof(slang_variable *));
1250 substNew = (slang_operation **)
1251 _slang_alloc(numArgs * sizeof(slang_operation *));
1252 for (i = 0; i < numArgs; i++) {
1253 substOld[i] = fun->parameters->variables[i];
1254 substNew[i] = oper->children + i;
1257 /* make a copy of the code to inline */
1258 inlined = slang_operation_new(1);
1259 slang_operation_copy(inlined, &fun->body->children[0]);
1261 /* get rid of the __retVal child */
1262 inlined->num_children--;
1263 for (i = 0; i < inlined->num_children; i++) {
1264 inlined->children[i] = inlined->children[i + 1];
1268 /* now do formal->actual substitutions */
1269 slang_substitute(A, inlined, numArgs, substOld, substNew, GL_FALSE);
1271 _slang_free(substOld);
1272 _slang_free(substNew);
1275 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1276 (char *) fun->header.a_name);
1277 slang_print_tree(inlined, 3);
1278 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1286 * Inline the given function call operation.
1287 * Return a new slang_operation that corresponds to the inlined code.
1289 static slang_operation *
1290 slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
1291 slang_operation *oper, slang_operation *returnOper)
1298 ParamMode *paramMode;
1299 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1300 const GLuint numArgs = oper->num_children;
1301 const GLuint totalArgs = numArgs + haveRetValue;
1302 slang_operation *args = oper->children;
1303 slang_operation *inlined, *top;
1304 slang_variable **substOld;
1305 slang_operation **substNew;
1306 GLuint substCount, numCopyIn, i;
1307 slang_function *prevFunction;
1308 slang_variable_scope *newScope = NULL;
1311 prevFunction = A->CurFunction;
1312 A->CurFunction = fun;
1314 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1315 assert(fun->param_count == totalArgs);
1317 /* allocate temporary arrays */
1318 paramMode = (ParamMode *)
1319 _slang_alloc(totalArgs * sizeof(ParamMode));
1320 substOld = (slang_variable **)
1321 _slang_alloc(totalArgs * sizeof(slang_variable *));
1322 substNew = (slang_operation **)
1323 _slang_alloc(totalArgs * sizeof(slang_operation *));
1326 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1327 (char *) fun->header.a_name,
1328 fun->parameters->num_variables, numArgs);
1331 if (haveRetValue && !returnOper) {
1332 /* Create 3-child comma sequence for inlined code:
1333 * child[0]: declare __resultTmp
1334 * child[1]: inlined function body
1335 * child[2]: __resultTmp
1337 slang_operation *commaSeq;
1338 slang_operation *declOper = NULL;
1339 slang_variable *resultVar;
1341 commaSeq = slang_operation_new(1);
1342 commaSeq->type = SLANG_OPER_SEQUENCE;
1343 assert(commaSeq->locals);
1344 commaSeq->locals->outer_scope = oper->locals->outer_scope;
1345 commaSeq->num_children = 3;
1346 commaSeq->children = slang_operation_new(3);
1347 /* allocate the return var */
1348 resultVar = slang_variable_scope_grow(commaSeq->locals);
1350 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1351 (void*)commaSeq->locals, (char *) fun->header.a_name);
1354 resultVar->a_name = slang_atom_pool_atom(A->atoms, "__resultTmp");
1355 resultVar->type = fun->header.type; /* XXX copy? */
1356 resultVar->isTemp = GL_TRUE;
1358 /* child[0] = __resultTmp declaration */
1359 declOper = &commaSeq->children[0];
1360 declOper->type = SLANG_OPER_VARIABLE_DECL;
1361 declOper->a_id = resultVar->a_name;
1362 declOper->locals->outer_scope = commaSeq->locals;
1364 /* child[1] = function body */
1365 inlined = &commaSeq->children[1];
1366 inlined->locals->outer_scope = commaSeq->locals;
1368 /* child[2] = __resultTmp reference */
1369 returnOper = &commaSeq->children[2];
1370 returnOper->type = SLANG_OPER_IDENTIFIER;
1371 returnOper->a_id = resultVar->a_name;
1372 returnOper->locals->outer_scope = commaSeq->locals;
1377 top = inlined = slang_operation_new(1);
1378 /* XXXX this may be inappropriate!!!! */
1379 inlined->locals->outer_scope = oper->locals->outer_scope;
1383 assert(inlined->locals);
1385 /* Examine the parameters, look for inout/out params, look for possible
1386 * substitutions, etc:
1387 * param type behaviour
1388 * in copy actual to local
1389 * const in substitute param with actual
1393 for (i = 0; i < totalArgs; i++) {
1394 slang_variable *p = fun->parameters->variables[i];
1396 printf("Param %d: %s %s \n", i,
1397 slang_type_qual_string(p->type.qualifier),
1398 (char *) p->a_name);
1400 if (p->type.qualifier == SLANG_QUAL_INOUT ||
1401 p->type.qualifier == SLANG_QUAL_OUT) {
1402 /* an output param */
1403 slang_operation *arg;
1408 paramMode[i] = SUBST;
1410 if (arg->type == SLANG_OPER_IDENTIFIER)
1411 slang_resolve_variable(arg);
1413 /* replace parameter 'p' with argument 'arg' */
1414 substOld[substCount] = p;
1415 substNew[substCount] = arg; /* will get copied */
1418 else if (p->type.qualifier == SLANG_QUAL_CONST) {
1419 /* a constant input param */
1420 if (args[i].type == SLANG_OPER_IDENTIFIER ||
1421 args[i].type == SLANG_OPER_LITERAL_FLOAT ||
1422 args[i].type == SLANG_OPER_SUBSCRIPT) {
1423 /* replace all occurances of this parameter variable with the
1424 * actual argument variable or a literal.
1426 paramMode[i] = SUBST;
1427 slang_resolve_variable(&args[i]);
1428 substOld[substCount] = p;
1429 substNew[substCount] = &args[i]; /* will get copied */
1433 paramMode[i] = COPY_IN;
1437 paramMode[i] = COPY_IN;
1439 assert(paramMode[i]);
1442 /* actual code inlining: */
1443 slang_operation_copy(inlined, fun->body);
1445 /*** XXX review this */
1446 assert(inlined->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
1447 inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE);
1448 inlined->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1451 printf("======================= orig body code ======================\n");
1452 printf("=== params scope = %p\n", (void*) fun->parameters);
1453 slang_print_tree(fun->body, 8);
1454 printf("======================= copied code =========================\n");
1455 slang_print_tree(inlined, 8);
1458 /* do parameter substitution in inlined code: */
1459 slang_substitute(A, inlined, substCount, substOld, substNew, GL_FALSE);
1462 printf("======================= subst code ==========================\n");
1463 slang_print_tree(inlined, 8);
1464 printf("=============================================================\n");
1467 /* New prolog statements: (inserted before the inlined code)
1468 * Copy the 'in' arguments.
1471 for (i = 0; i < numArgs; i++) {
1472 if (paramMode[i] == COPY_IN) {
1473 slang_variable *p = fun->parameters->variables[i];
1474 /* declare parameter 'p' */
1475 slang_operation *decl = slang_operation_insert(&inlined->num_children,
1479 decl->type = SLANG_OPER_VARIABLE_DECL;
1480 assert(decl->locals);
1481 decl->locals->outer_scope = inlined->locals;
1482 decl->a_id = p->a_name;
1483 decl->num_children = 1;
1484 decl->children = slang_operation_new(1);
1486 /* child[0] is the var's initializer */
1487 slang_operation_copy(&decl->children[0], args + i);
1489 /* add parameter 'p' to the local variable scope here */
1491 slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
1492 pCopy->type = p->type;
1493 pCopy->a_name = p->a_name;
1494 pCopy->array_len = p->array_len;
1497 newScope = inlined->locals;
1502 /* Now add copies of the function's local vars to the new variable scope */
1503 for (i = totalArgs; i < fun->parameters->num_variables; i++) {
1504 slang_variable *p = fun->parameters->variables[i];
1505 slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
1506 pCopy->type = p->type;
1507 pCopy->a_name = p->a_name;
1508 pCopy->array_len = p->array_len;
1512 /* New epilog statements:
1513 * 1. Create end of function label to jump to from return statements.
1514 * 2. Copy the 'out' parameter vars
1517 slang_operation *lab = slang_operation_insert(&inlined->num_children,
1519 inlined->num_children);
1520 lab->type = SLANG_OPER_LABEL;
1521 lab->label = A->curFuncEndLabel;
1524 for (i = 0; i < totalArgs; i++) {
1525 if (paramMode[i] == COPY_OUT) {
1526 const slang_variable *p = fun->parameters->variables[i];
1527 /* actualCallVar = outParam */
1528 /*if (i > 0 || !haveRetValue)*/
1529 slang_operation *ass = slang_operation_insert(&inlined->num_children,
1531 inlined->num_children);
1532 ass->type = SLANG_OPER_ASSIGN;
1533 ass->num_children = 2;
1534 ass->locals->outer_scope = inlined->locals;
1535 ass->children = slang_operation_new(2);
1536 ass->children[0] = args[i]; /*XXX copy */
1537 ass->children[1].type = SLANG_OPER_IDENTIFIER;
1538 ass->children[1].a_id = p->a_name;
1539 ass->children[1].locals->outer_scope = ass->locals;
1543 _slang_free(paramMode);
1544 _slang_free(substOld);
1545 _slang_free(substNew);
1547 /* Update scoping to use the new local vars instead of the
1548 * original function's vars. This is especially important
1549 * for nested inlining.
1552 slang_replace_scope(inlined, fun->parameters, newScope);
1555 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1556 (char *) fun->header.a_name,
1557 fun->parameters->num_variables, numArgs);
1558 slang_print_tree(top, 0);
1562 A->CurFunction = prevFunction;
1569 * Insert declaration for "bool __notRetFlag" in given block operation.
1570 * This is used when we can't emit "early" return statements in subroutines.
1573 declare_return_flag(slang_assemble_ctx *A, slang_operation *oper)
1575 slang_operation *decl;
1577 assert(oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1578 oper->type == SLANG_OPER_SEQUENCE);
1580 decl = slang_operation_insert_child(oper, 1);
1582 slang_generate_declaration(A, oper->locals, decl,
1583 SLANG_SPEC_BOOL, "__notRetFlag", GL_TRUE);
1585 /*slang_print_tree(oper, 0);*/
1590 * Recursively replace instances of the old node type with the new type.
1593 replace_node_type(slang_operation *oper, slang_operation_type oldType,
1594 slang_operation_type newType)
1598 if (oper->type == oldType)
1599 oper->type = newType;
1601 for (i = 0; i < slang_oper_num_children(oper); i++) {
1602 replace_node_type(slang_oper_child(oper, i), oldType, newType);
1609 * Test if the given function body has an "early return". That is, there's
1610 * a 'return' statement that's not the very last instruction in the body.
1613 has_early_return(const slang_operation *funcBody)
1615 GLuint retCount = _slang_count_node_type(funcBody, SLANG_OPER_RETURN);
1618 else if (retCount == 1 && _slang_is_tail_return(funcBody))
1626 * Emit IR code for a function call. This does one of two things:
1627 * 1. Inline the function's code
1628 * 2. Create an IR for the function's body and create a real call to it.
1630 static slang_ir_node *
1631 _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
1632 slang_operation *oper, slang_operation *dest)
1635 slang_operation *instance;
1636 slang_label *prevFuncEndLabel;
1639 prevFuncEndLabel = A->curFuncEndLabel;
1640 sprintf(name, "__endOfFunc_%s_", (char *) fun->header.a_name);
1641 A->curFuncEndLabel = _slang_label_new(name);
1642 assert(A->curFuncEndLabel);
1645 * 'instance' is basically a copy of the function's body with various
1649 if (slang_is_asm_function(fun) && !dest) {
1650 /* assemble assembly function - tree style */
1651 instance = slang_inline_asm_function(A, fun, oper);
1654 /* non-assembly function */
1655 /* We always generate an "inline-able" block of code here.
1657 * 1. insert the inline code
1658 * 2. Generate a call to the "inline" code as a subroutine
1660 const GLboolean earlyReturn = has_early_return(fun->body);
1662 if (earlyReturn && !A->EmitContReturn) {
1663 A->UseReturnFlag = GL_TRUE;
1666 instance = slang_inline_function_call(A, fun, oper, dest);
1671 /* The function we're calling has one or more 'return' statements
1672 * that prevent us from inlining the function's code.
1674 * In this case, change the function's body type from
1675 * SLANG_OPER_BLOCK_NEW_SCOPE to SLANG_OPER_NON_INLINED_CALL.
1676 * During code emit this will result in a true subroutine call.
1678 * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1680 slang_operation *callOper;
1682 assert(instance->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1683 instance->type == SLANG_OPER_SEQUENCE);
1685 if (_slang_function_has_return_value(fun) && !dest) {
1686 assert(instance->children[0].type == SLANG_OPER_VARIABLE_DECL);
1687 assert(instance->children[2].type == SLANG_OPER_IDENTIFIER);
1688 callOper = &instance->children[1];
1691 callOper = instance;
1694 if (A->UseReturnFlag) {
1695 /* Early returns not supported. Create a _returnFlag variable
1696 * that's set upon 'return' and tested elsewhere to no-op any
1697 * remaining instructions in the subroutine.
1699 assert(callOper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1700 callOper->type == SLANG_OPER_SEQUENCE);
1701 declare_return_flag(A, callOper);
1704 /* We can emit real 'return' statements. If we generated any
1705 * 'inline return' statements during function instantiation,
1706 * change them back to regular 'return' statements.
1708 replace_node_type(instance, SLANG_OPER_RETURN_INLINED,
1712 callOper->type = SLANG_OPER_NON_INLINED_CALL;
1713 callOper->fun = fun;
1714 callOper->label = _slang_label_new_unique((char*) fun->header.a_name);
1717 /* If there are any 'return' statements remaining, they're at the
1718 * very end of the function and can effectively become no-ops.
1720 replace_node_type(instance, SLANG_OPER_RETURN_INLINED,
1728 /* Replace the function call with the instance block (or new CALL stmt) */
1729 slang_operation_destruct(oper);
1731 _slang_free(instance);
1734 assert(instance->locals);
1735 printf("*** Inlined code for call to %s:\n", (char*) fun->header.a_name);
1736 slang_print_tree(oper, 10);
1740 n = _slang_gen_operation(A, oper);
1742 /*_slang_label_delete(A->curFuncEndLabel);*/
1743 A->curFuncEndLabel = prevFuncEndLabel;
1745 if (A->pragmas->Debug) {
1747 _mesa_snprintf(s, sizeof(s), "Call/inline %s()", (char *) fun->header.a_name);
1748 n->Comment = _slang_strdup(s);
1751 A->UseReturnFlag = GL_FALSE;
1757 static slang_asm_info *
1758 slang_find_asm_info(const char *name)
1761 for (i = 0; AsmInfo[i].Name; i++) {
1762 if (_mesa_strcmp(AsmInfo[i].Name, name) == 0) {
1771 * Some write-masked assignments are simple, but others are hard.
1774 * v.xy = vec2(a, b);
1777 * v.zy = vec2(a, b);
1778 * this gets transformed/swizzled into:
1779 * v.zy = vec2(a, b).*yx* (* = don't care)
1780 * This function helps to determine simple vs. non-simple.
1783 _slang_simple_writemask(GLuint writemask, GLuint swizzle)
1785 switch (writemask) {
1787 return GET_SWZ(swizzle, 0) == SWIZZLE_X;
1789 return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
1791 return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
1793 return GET_SWZ(swizzle, 3) == SWIZZLE_W;
1795 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1796 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
1798 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1799 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y)
1800 && (GET_SWZ(swizzle, 2) == SWIZZLE_Z);
1801 case WRITEMASK_XYZW:
1802 return swizzle == SWIZZLE_NOOP;
1810 * Convert the given swizzle into a writemask. In some cases this
1811 * is trivial, in other cases, we'll need to also swizzle the right
1812 * hand side to put components in the right places.
1813 * See comment above for more info.
1814 * XXX this function could be simplified and should probably be renamed.
1815 * \param swizzle the incoming swizzle
1816 * \param writemaskOut returns the writemask
1817 * \param swizzleOut swizzle to apply to the right-hand-side
1818 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1821 swizzle_to_writemask(slang_assemble_ctx *A, GLuint swizzle,
1822 GLuint *writemaskOut, GLuint *swizzleOut)
1824 GLuint mask = 0x0, newSwizzle[4];
1827 /* make new dst writemask, compute size */
1828 for (i = 0; i < 4; i++) {
1829 const GLuint swz = GET_SWZ(swizzle, i);
1830 if (swz == SWIZZLE_NIL) {
1834 assert(swz >= 0 && swz <= 3);
1836 if (swizzle != SWIZZLE_XXXX &&
1837 swizzle != SWIZZLE_YYYY &&
1838 swizzle != SWIZZLE_ZZZZ &&
1839 swizzle != SWIZZLE_WWWW &&
1840 (mask & (1 << swz))) {
1841 /* a channel can't be specified twice (ex: ".xyyz") */
1842 slang_info_log_error(A->log, "Invalid writemask '%s'",
1843 _mesa_swizzle_string(swizzle, 0, 0));
1849 assert(mask <= 0xf);
1850 size = i; /* number of components in mask/swizzle */
1852 *writemaskOut = mask;
1854 /* make new src swizzle, by inversion */
1855 for (i = 0; i < 4; i++) {
1856 newSwizzle[i] = i; /*identity*/
1858 for (i = 0; i < size; i++) {
1859 const GLuint swz = GET_SWZ(swizzle, i);
1860 newSwizzle[swz] = i;
1862 *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
1867 if (_slang_simple_writemask(mask, *swizzleOut)) {
1869 assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
1871 assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
1873 assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
1875 assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
1883 #if 0 /* not used, but don't remove just yet */
1885 * Recursively traverse 'oper' to produce a swizzle mask in the event
1886 * of any vector subscripts and swizzle suffixes.
1887 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1890 resolve_swizzle(const slang_operation *oper)
1892 if (oper->type == SLANG_OPER_FIELD) {
1893 /* writemask from .xyzw suffix */
1895 if (_slang_is_swizzle((char*) oper->a_id, 4, &swz)) {
1896 GLuint swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
1900 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1901 GLuint s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1905 return SWIZZLE_XYZW;
1907 else if (oper->type == SLANG_OPER_SUBSCRIPT &&
1908 oper->children[1].type == SLANG_OPER_LITERAL_INT) {
1909 /* writemask from [index] */
1910 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1911 GLuint i = (GLuint) oper->children[1].literal[0];
1916 swizzle = SWIZZLE_XXXX;
1919 swizzle = SWIZZLE_YYYY;
1922 swizzle = SWIZZLE_ZZZZ;
1925 swizzle = SWIZZLE_WWWW;
1928 swizzle = SWIZZLE_XYZW;
1930 s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1934 return SWIZZLE_XYZW;
1942 * Recursively descend through swizzle nodes to find the node's storage info.
1944 static slang_ir_storage *
1945 get_store(const slang_ir_node *n)
1947 if (n->Opcode == IR_SWIZZLE) {
1948 return get_store(n->Children[0]);
1956 * Generate IR tree for an asm instruction/operation such as:
1957 * __asm vec4_dot __retVal.x, v1, v2;
1959 static slang_ir_node *
1960 _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
1961 slang_operation *dest)
1963 const slang_asm_info *info;
1964 slang_ir_node *kids[3], *n;
1965 GLuint j, firstOperand;
1967 assert(oper->type == SLANG_OPER_ASM);
1969 info = slang_find_asm_info((char *) oper->a_id);
1971 _mesa_problem(NULL, "undefined __asm function %s\n",
1972 (char *) oper->a_id);
1975 assert(info->NumParams <= 3);
1977 if (info->NumParams == oper->num_children) {
1978 /* Storage for result is not specified.
1979 * Children[0], [1], [2] are the operands.
1984 /* Storage for result (child[0]) is specified.
1985 * Children[1], [2], [3] are the operands.
1990 /* assemble child(ren) */
1991 kids[0] = kids[1] = kids[2] = NULL;
1992 for (j = 0; j < info->NumParams; j++) {
1993 kids[j] = _slang_gen_operation(A, &oper->children[firstOperand + j]);
1998 n = new_node3(info->Opcode, kids[0], kids[1], kids[2]);
2001 /* Setup n->Store to be a particular location. Otherwise, storage
2002 * for the result (a temporary) will be allocated later.
2004 slang_operation *dest_oper;
2007 dest_oper = &oper->children[0];
2009 n0 = _slang_gen_operation(A, dest_oper);
2014 n->Store = n0->Store;
2016 assert(n->Store->File != PROGRAM_UNDEFINED || n->Store->Parent);
2027 print_funcs(struct slang_function_scope_ *scope, const char *name)
2030 for (i = 0; i < scope->num_functions; i++) {
2031 slang_function *f = &scope->functions[i];
2032 if (!name || strcmp(name, (char*) f->header.a_name) == 0)
2033 printf(" %s (%d args)\n", name, f->param_count);
2036 if (scope->outer_scope)
2037 print_funcs(scope->outer_scope, name);
2043 * Find a function of the given name, taking 'numArgs' arguments.
2044 * This is the function we'll try to call when there is no exact match
2045 * between function parameters and call arguments.
2047 * XXX we should really create a list of candidate functions and try
2050 static slang_function *
2051 _slang_find_function_by_argc(slang_function_scope *scope,
2052 const char *name, int numArgs)
2056 for (i = 0; i < scope->num_functions; i++) {
2057 slang_function *f = &scope->functions[i];
2058 if (strcmp(name, (char*) f->header.a_name) == 0) {
2059 int haveRetValue = _slang_function_has_return_value(f);
2060 if (numArgs == f->param_count - haveRetValue)
2064 scope = scope->outer_scope;
2071 static slang_function *
2072 _slang_find_function_by_max_argc(slang_function_scope *scope,
2075 slang_function *maxFunc = NULL;
2080 for (i = 0; i < scope->num_functions; i++) {
2081 slang_function *f = &scope->functions[i];
2082 if (strcmp(name, (char*) f->header.a_name) == 0) {
2083 if (f->param_count > maxArgs) {
2084 maxArgs = f->param_count;
2089 scope = scope->outer_scope;
2097 * Generate a new slang_function which is a constructor for a user-defined
2100 static slang_function *
2101 _slang_make_struct_constructor(slang_assemble_ctx *A, slang_struct *str)
2103 const GLint numFields = str->fields->num_variables;
2104 slang_function *fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
2106 /* function header (name, return type) */
2107 fun->header.a_name = str->a_name;
2108 fun->header.type.qualifier = SLANG_QUAL_NONE;
2109 fun->header.type.specifier.type = SLANG_SPEC_STRUCT;
2110 fun->header.type.specifier._struct = str;
2112 /* function parameters (= struct's fields) */
2115 for (i = 0; i < numFields; i++) {
2117 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2119 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2120 *p = *str->fields->variables[i]; /* copy the variable and type */
2121 p->type.qualifier = SLANG_QUAL_CONST;
2123 fun->param_count = fun->parameters->num_variables;
2126 /* Add __retVal to params */
2128 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2129 slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2131 p->a_name = a_retVal;
2132 p->type = fun->header.type;
2133 p->type.qualifier = SLANG_QUAL_OUT;
2137 /* function body is:
2147 slang_variable_scope *scope;
2148 slang_variable *var;
2151 fun->body = slang_operation_new(1);
2152 fun->body->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2153 fun->body->num_children = numFields + 2;
2154 fun->body->children = slang_operation_new(numFields + 2);
2156 scope = fun->body->locals;
2157 scope->outer_scope = fun->parameters;
2159 /* create local var 't' */
2160 var = slang_variable_scope_grow(scope);
2161 var->a_name = slang_atom_pool_atom(A->atoms, "t");
2162 var->type = fun->header.type;
2166 slang_operation *decl;
2168 decl = &fun->body->children[0];
2169 decl->type = SLANG_OPER_VARIABLE_DECL;
2170 decl->locals = _slang_variable_scope_new(scope);
2171 decl->a_id = var->a_name;
2174 /* assign params to fields of t */
2175 for (i = 0; i < numFields; i++) {
2176 slang_operation *assign = &fun->body->children[1 + i];
2178 assign->type = SLANG_OPER_ASSIGN;
2179 assign->locals = _slang_variable_scope_new(scope);
2180 assign->num_children = 2;
2181 assign->children = slang_operation_new(2);
2184 slang_operation *lhs = &assign->children[0];
2186 lhs->type = SLANG_OPER_FIELD;
2187 lhs->locals = _slang_variable_scope_new(scope);
2188 lhs->num_children = 1;
2189 lhs->children = slang_operation_new(1);
2190 lhs->a_id = str->fields->variables[i]->a_name;
2192 lhs->children[0].type = SLANG_OPER_IDENTIFIER;
2193 lhs->children[0].a_id = var->a_name;
2194 lhs->children[0].locals = _slang_variable_scope_new(scope);
2197 lhs->children[1].num_children = 1;
2198 lhs->children[1].children = slang_operation_new(1);
2199 lhs->children[1].children[0].type = SLANG_OPER_IDENTIFIER;
2200 lhs->children[1].children[0].a_id = str->fields->variables[i]->a_name;
2201 lhs->children[1].children->locals = _slang_variable_scope_new(scope);
2206 slang_operation *rhs = &assign->children[1];
2208 rhs->type = SLANG_OPER_IDENTIFIER;
2209 rhs->locals = _slang_variable_scope_new(scope);
2210 rhs->a_id = str->fields->variables[i]->a_name;
2216 slang_operation *ret = &fun->body->children[numFields + 1];
2218 ret->type = SLANG_OPER_RETURN;
2219 ret->locals = _slang_variable_scope_new(scope);
2220 ret->num_children = 1;
2221 ret->children = slang_operation_new(1);
2222 ret->children[0].type = SLANG_OPER_IDENTIFIER;
2223 ret->children[0].a_id = var->a_name;
2224 ret->children[0].locals = _slang_variable_scope_new(scope);
2228 slang_print_function(fun, 1);
2235 * Find/create a function (constructor) for the given structure name.
2237 static slang_function *
2238 _slang_locate_struct_constructor(slang_assemble_ctx *A, const char *name)
2241 for (i = 0; i < A->space.structs->num_structs; i++) {
2242 slang_struct *str = &A->space.structs->structs[i];
2243 if (strcmp(name, (const char *) str->a_name) == 0) {
2244 /* found a structure type that matches the function name */
2245 if (!str->constructor) {
2246 /* create the constructor function now */
2247 str->constructor = _slang_make_struct_constructor(A, str);
2249 return str->constructor;
2257 * Generate a new slang_function to satisfy a call to an array constructor.
2258 * Ex: float[3](1., 2., 3.)
2260 static slang_function *
2261 _slang_make_array_constructor(slang_assemble_ctx *A, slang_operation *oper)
2263 slang_type_specifier_type baseType;
2264 slang_function *fun;
2267 fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
2271 baseType = slang_type_specifier_type_from_string((char *) oper->a_id);
2273 num_elements = oper->num_children;
2275 /* function header, return type */
2277 fun->header.a_name = oper->a_id;
2278 fun->header.type.qualifier = SLANG_QUAL_NONE;
2279 fun->header.type.specifier.type = SLANG_SPEC_ARRAY;
2280 fun->header.type.specifier._array =
2281 slang_type_specifier_new(baseType, NULL, NULL);
2282 fun->header.type.array_len = num_elements;
2285 /* function parameters (= number of elements) */
2288 for (i = 0; i < num_elements; i++) {
2290 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2292 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2294 _mesa_snprintf(name, sizeof(name), "p%d", i);
2295 p->a_name = slang_atom_pool_atom(A->atoms, name);
2296 p->type.qualifier = SLANG_QUAL_CONST;
2297 p->type.specifier.type = baseType;
2299 fun->param_count = fun->parameters->num_variables;
2302 /* Add __retVal to params */
2304 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2305 slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2307 p->a_name = a_retVal;
2308 p->type = fun->header.type;
2309 p->type.qualifier = SLANG_QUAL_OUT;
2310 p->type.specifier.type = baseType;
2314 /* function body is:
2324 slang_variable_scope *scope;
2325 slang_variable *var;
2328 fun->body = slang_operation_new(1);
2329 fun->body->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2330 fun->body->num_children = num_elements + 2;
2331 fun->body->children = slang_operation_new(num_elements + 2);
2333 scope = fun->body->locals;
2334 scope->outer_scope = fun->parameters;
2336 /* create local var 't' */
2337 var = slang_variable_scope_grow(scope);
2338 var->a_name = slang_atom_pool_atom(A->atoms, "ttt");
2339 var->type = fun->header.type;/*XXX copy*/
2343 slang_operation *decl;
2345 decl = &fun->body->children[0];
2346 decl->type = SLANG_OPER_VARIABLE_DECL;
2347 decl->locals = _slang_variable_scope_new(scope);
2348 decl->a_id = var->a_name;
2351 /* assign params to elements of t */
2352 for (i = 0; i < num_elements; i++) {
2353 slang_operation *assign = &fun->body->children[1 + i];
2355 assign->type = SLANG_OPER_ASSIGN;
2356 assign->locals = _slang_variable_scope_new(scope);
2357 assign->num_children = 2;
2358 assign->children = slang_operation_new(2);
2361 slang_operation *lhs = &assign->children[0];
2363 lhs->type = SLANG_OPER_SUBSCRIPT;
2364 lhs->locals = _slang_variable_scope_new(scope);
2365 lhs->num_children = 2;
2366 lhs->children = slang_operation_new(2);
2368 lhs->children[0].type = SLANG_OPER_IDENTIFIER;
2369 lhs->children[0].a_id = var->a_name;
2370 lhs->children[0].locals = _slang_variable_scope_new(scope);
2372 lhs->children[1].type = SLANG_OPER_LITERAL_INT;
2373 lhs->children[1].literal[0] = (GLfloat) i;
2377 slang_operation *rhs = &assign->children[1];
2379 rhs->type = SLANG_OPER_IDENTIFIER;
2380 rhs->locals = _slang_variable_scope_new(scope);
2381 rhs->a_id = fun->parameters->variables[i]->a_name;
2387 slang_operation *ret = &fun->body->children[num_elements + 1];
2389 ret->type = SLANG_OPER_RETURN;
2390 ret->locals = _slang_variable_scope_new(scope);
2391 ret->num_children = 1;
2392 ret->children = slang_operation_new(1);
2393 ret->children[0].type = SLANG_OPER_IDENTIFIER;
2394 ret->children[0].a_id = var->a_name;
2395 ret->children[0].locals = _slang_variable_scope_new(scope);
2400 slang_print_function(fun, 1);
2408 _slang_is_vec_mat_type(const char *name)
2410 static const char *vecmat_types[] = {
2411 "float", "int", "bool",
2412 "vec2", "vec3", "vec4",
2413 "ivec2", "ivec3", "ivec4",
2414 "bvec2", "bvec3", "bvec4",
2415 "mat2", "mat3", "mat4",
2416 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2420 for (i = 0; vecmat_types[i]; i++)
2421 if (_mesa_strcmp(name, vecmat_types[i]) == 0)
2428 * Assemble a function call, given a particular function name.
2429 * \param name the function's name (operators like '*' are possible).
2431 static slang_ir_node *
2432 _slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
2433 slang_operation *oper, slang_operation *dest)
2435 slang_operation *params = oper->children;
2436 const GLuint param_count = oper->num_children;
2438 slang_function *fun;
2441 atom = slang_atom_pool_atom(A->atoms, name);
2442 if (atom == SLANG_ATOM_NULL)
2445 if (oper->array_constructor) {
2446 /* this needs special handling */
2447 fun = _slang_make_array_constructor(A, oper);
2450 /* Try to find function by name and exact argument type matching */
2451 GLboolean error = GL_FALSE;
2452 fun = _slang_function_locate(A->space.funcs, atom, params, param_count,
2453 &A->space, A->atoms, A->log, &error);
2455 slang_info_log_error(A->log,
2456 "Function '%s' not found (check argument types)",
2463 /* Next, try locating a constructor function for a user-defined type */
2464 fun = _slang_locate_struct_constructor(A, name);
2468 * At this point, some heuristics are used to try to find a function
2469 * that matches the calling signature by means of casting or "unrolling"
2473 if (!fun && _slang_is_vec_mat_type(name)) {
2474 /* Next, if this call looks like a vec() or mat() constructor call,
2475 * try "unwinding" the args to satisfy a constructor.
2477 fun = _slang_find_function_by_max_argc(A->space.funcs, name);
2479 if (!_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
2480 slang_info_log_error(A->log,
2481 "Function '%s' not found (check argument types)",
2488 if (!fun && _slang_is_vec_mat_type(name)) {
2489 /* Next, try casting args to the types of the formal parameters */
2490 int numArgs = oper->num_children;
2491 fun = _slang_find_function_by_argc(A->space.funcs, name, numArgs);
2492 if (!fun || !_slang_cast_func_params(oper, fun, &A->space, A->atoms, A->log)) {
2493 slang_info_log_error(A->log,
2494 "Function '%s' not found (check argument types)",
2502 slang_info_log_error(A->log,
2503 "Function '%s' not found (check argument types)",
2509 /* The function body may be in another compilation unit.
2510 * We'll try concatenating the shaders and recompile at link time.
2512 A->UnresolvedRefs = GL_TRUE;
2513 return new_node1(IR_NOP, NULL);
2516 /* type checking to be sure function's return type matches 'dest' type */
2520 slang_typeinfo_construct(&t0);
2521 typeof_operation(A, dest, &t0);
2523 if (!slang_type_specifier_equal(&t0.spec, &fun->header.type.specifier)) {
2524 slang_info_log_error(A->log,
2525 "Incompatible type returned by call to '%s'",
2531 n = _slang_gen_function_call(A, fun, oper, dest);
2533 if (n && !n->Store && !dest
2534 && fun->header.type.specifier.type != SLANG_SPEC_VOID) {
2535 /* setup n->Store for the result of the function call */
2536 GLint size = _slang_sizeof_type_specifier(&fun->header.type.specifier);
2537 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
2538 /*printf("Alloc storage for function result, size %d \n", size);*/
2541 if (oper->array_constructor) {
2542 /* free the temporary array constructor function now */
2543 slang_function_destruct(fun);
2550 static slang_ir_node *
2551 _slang_gen_method_call(slang_assemble_ctx *A, slang_operation *oper)
2553 slang_atom *a_length = slang_atom_pool_atom(A->atoms, "length");
2555 slang_variable *var;
2557 /* NOTE: In GLSL 1.20, there's only one kind of method
2558 * call: array.length(). Anything else is an error.
2560 if (oper->a_id != a_length) {
2561 slang_info_log_error(A->log,
2562 "Undefined method call '%s'", (char *) oper->a_id);
2566 /* length() takes no arguments */
2567 if (oper->num_children > 0) {
2568 slang_info_log_error(A->log, "Invalid arguments to length() method");
2572 /* lookup the object/variable */
2573 var = _slang_variable_locate(oper->locals, oper->a_obj, GL_TRUE);
2574 if (!var || var->type.specifier.type != SLANG_SPEC_ARRAY) {
2575 slang_info_log_error(A->log,
2576 "Undefined object '%s'", (char *) oper->a_obj);
2580 /* Create a float/literal IR node encoding the array length */
2581 n = new_node0(IR_FLOAT);
2583 n->Value[0] = (float) _slang_array_length(var);
2584 n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, 1);
2591 _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
2593 if (oper->type == SLANG_OPER_LITERAL_FLOAT ||
2594 oper->type == SLANG_OPER_LITERAL_INT ||
2595 oper->type == SLANG_OPER_LITERAL_BOOL) {
2596 if (oper->literal[0])
2602 else if (oper->type == SLANG_OPER_EXPRESSION &&
2603 oper->num_children == 1) {
2604 return _slang_is_constant_cond(&oper->children[0], value);
2611 * Test if an operation is a scalar or boolean.
2614 _slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
2616 slang_typeinfo type;
2619 slang_typeinfo_construct(&type);
2620 typeof_operation(A, oper, &type);
2621 size = _slang_sizeof_type_specifier(&type.spec);
2622 slang_typeinfo_destruct(&type);
2628 * Test if an operation is boolean.
2631 _slang_is_boolean(slang_assemble_ctx *A, slang_operation *oper)
2633 slang_typeinfo type;
2636 slang_typeinfo_construct(&type);
2637 typeof_operation(A, oper, &type);
2638 isBool = (type.spec.type == SLANG_SPEC_BOOL);
2639 slang_typeinfo_destruct(&type);
2645 * Check if a loop contains a 'continue' statement.
2646 * Stop looking if we find a nested loop.
2649 _slang_loop_contains_continue(const slang_operation *oper)
2651 switch (oper->type) {
2652 case SLANG_OPER_CONTINUE:
2654 case SLANG_OPER_FOR:
2656 case SLANG_OPER_WHILE:
2657 /* stop upon finding a nested loop */
2663 for (i = 0; i < oper->num_children; i++) {
2664 const slang_operation *child = slang_oper_child_const(oper, i);
2665 if (_slang_loop_contains_continue(child))
2675 * Check if a loop contains a 'continue' or 'break' statement.
2676 * Stop looking if we find a nested loop.
2679 _slang_loop_contains_continue_or_break(const slang_operation *oper)
2681 switch (oper->type) {
2682 case SLANG_OPER_CONTINUE:
2683 case SLANG_OPER_BREAK:
2685 case SLANG_OPER_FOR:
2687 case SLANG_OPER_WHILE:
2688 /* stop upon finding a nested loop */
2694 for (i = 0; i < oper->num_children; i++) {
2695 const slang_operation *child = slang_oper_child_const(oper, i);
2696 if (_slang_loop_contains_continue_or_break(child))
2706 * Replace 'break' and 'continue' statements inside a do and while loops.
2707 * This is a recursive helper function used by
2708 * _slang_gen_do/while_without_continue().
2711 replace_break_and_cont(slang_assemble_ctx *A, slang_operation *oper)
2713 switch (oper->type) {
2714 case SLANG_OPER_BREAK:
2715 /* replace 'break' with "_notBreakFlag = false; break" */
2717 slang_operation *block = oper;
2718 block->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2719 slang_operation_add_children(block, 2);
2721 slang_operation *assign = slang_oper_child(block, 0);
2722 assign->type = SLANG_OPER_ASSIGN;
2723 slang_operation_add_children(assign, 2);
2725 slang_operation *lhs = slang_oper_child(assign, 0);
2726 slang_operation_identifier(lhs, A, "_notBreakFlag");
2729 slang_operation *rhs = slang_oper_child(assign, 1);
2730 slang_operation_literal_bool(rhs, GL_FALSE);
2734 slang_operation *brk = slang_oper_child(block, 1);
2735 brk->type = SLANG_OPER_BREAK;
2736 assert(!brk->children);
2740 case SLANG_OPER_CONTINUE:
2741 /* convert continue into a break */
2742 oper->type = SLANG_OPER_BREAK;
2744 case SLANG_OPER_FOR:
2746 case SLANG_OPER_WHILE:
2747 /* stop upon finding a nested loop */
2753 for (i = 0; i < oper->num_children; i++) {
2754 replace_break_and_cont(A, slang_oper_child(oper, i));
2762 * Transform a while-loop so that continue statements are converted to breaks.
2763 * Then do normal IR code generation.
2767 * while (LOOPCOND) {
2779 * bool _notBreakFlag = 1;
2780 * while (_notBreakFlag && LOOPCOND) {
2784 * break; // was continue
2787 * _notBreakFlag = 0; // was
2794 static slang_ir_node *
2795 _slang_gen_while_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2797 slang_operation *top;
2798 slang_operation *innerBody;
2800 assert(oper->type == SLANG_OPER_WHILE);
2802 top = slang_operation_new(1);
2803 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2804 top->locals->outer_scope = oper->locals->outer_scope;
2805 slang_operation_add_children(top, 2);
2807 /* declare: bool _notBreakFlag = true */
2809 slang_operation *condDecl = slang_oper_child(top, 0);
2810 slang_generate_declaration(A, top->locals, condDecl,
2811 SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2814 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2816 slang_operation *outerWhile = slang_oper_child(top, 1);
2817 outerWhile->type = SLANG_OPER_WHILE;
2818 slang_operation_add_children(outerWhile, 2);
2820 /* _notBreakFlag && LOOPCOND */
2822 slang_operation *cond = slang_oper_child(outerWhile, 0);
2823 cond->type = SLANG_OPER_LOGICALAND;
2824 slang_operation_add_children(cond, 2);
2826 slang_operation *notBreak = slang_oper_child(cond, 0);
2827 slang_operation_identifier(notBreak, A, "_notBreakFlag");
2830 slang_operation *origCond = slang_oper_child(cond, 1);
2831 slang_operation_copy(origCond, slang_oper_child(oper, 0));
2837 slang_operation *innerDo = slang_oper_child(outerWhile, 1);
2838 innerDo->type = SLANG_OPER_DO;
2839 slang_operation_add_children(innerDo, 2);
2841 /* copy original do-loop body into inner do-loop's body */
2842 innerBody = slang_oper_child(innerDo, 0);
2843 slang_operation_copy(innerBody, slang_oper_child(oper, 1));
2844 innerBody->locals->outer_scope = innerDo->locals;
2846 /* inner do-loop's condition is constant/false */
2848 slang_operation *constFalse = slang_oper_child(innerDo, 1);
2849 slang_operation_literal_bool(constFalse, GL_FALSE);
2854 /* Finally, in innerBody,
2855 * replace "break" with "_notBreakFlag = 0; break"
2856 * replace "continue" with "break"
2858 replace_break_and_cont(A, innerBody);
2860 /*slang_print_tree(top, 0);*/
2862 return _slang_gen_operation(A, top);
2869 * Generate loop code using high-level IR_LOOP instruction
2871 static slang_ir_node *
2872 _slang_gen_while(slang_assemble_ctx * A, slang_operation *oper)
2876 * BREAK if !expr (child[0])
2877 * body code (child[1])
2879 slang_ir_node *loop, *breakIf, *body;
2880 GLboolean isConst, constTrue;
2882 if (!A->EmitContReturn) {
2883 /* We don't want to emit CONT instructions. If this while-loop has
2884 * a continue, translate it away.
2886 if (_slang_loop_contains_continue(slang_oper_child(oper, 1))) {
2887 return _slang_gen_while_without_continue(A, oper);
2891 /* type-check expression */
2892 if (!_slang_is_boolean(A, &oper->children[0])) {
2893 slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
2897 /* Check if loop condition is a constant */
2898 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
2900 if (isConst && !constTrue) {
2901 /* loop is never executed! */
2902 return new_node0(IR_NOP);
2905 /* Begin new loop */
2906 loop = new_loop(NULL);
2908 /* save loop state */
2909 push_loop(A, oper, loop);
2911 if (isConst && constTrue) {
2912 /* while(nonzero constant), no conditional break */
2917 = new_cond(new_not(_slang_gen_operation(A, &oper->children[0])));
2918 breakIf = new_break_if_true(A, cond);
2920 body = _slang_gen_operation(A, &oper->children[1]);
2921 loop->Children[0] = new_seq(breakIf, body);
2923 /* Do infinite loop detection */
2924 /* loop->List is head of linked list of break/continue nodes */
2925 if (!loop->List && isConst && constTrue) {
2926 /* infinite loop detected */
2928 slang_info_log_error(A->log, "Infinite loop detected!");
2932 /* restore loop state */
2940 * Transform a do-while-loop so that continue statements are converted to breaks.
2941 * Then do normal IR code generation.
2952 * } while (LOOPCOND);
2957 * bool _notBreakFlag = 1;
2962 * break; // was continue
2965 * _notBreakFlag = 0; // was
2969 * } while (_notBreakFlag && LOOPCOND);
2972 static slang_ir_node *
2973 _slang_gen_do_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2975 slang_operation *top;
2976 slang_operation *innerBody;
2978 assert(oper->type == SLANG_OPER_DO);
2980 top = slang_operation_new(1);
2981 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2982 top->locals->outer_scope = oper->locals->outer_scope;
2983 slang_operation_add_children(top, 2);
2985 /* declare: bool _notBreakFlag = true */
2987 slang_operation *condDecl = slang_oper_child(top, 0);
2988 slang_generate_declaration(A, top->locals, condDecl,
2989 SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2992 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2994 slang_operation *outerDo = slang_oper_child(top, 1);
2995 outerDo->type = SLANG_OPER_DO;
2996 slang_operation_add_children(outerDo, 2);
3000 slang_operation *innerDo = slang_oper_child(outerDo, 0);
3001 innerDo->type = SLANG_OPER_DO;
3002 slang_operation_add_children(innerDo, 2);
3004 /* copy original do-loop body into inner do-loop's body */
3005 innerBody = slang_oper_child(innerDo, 0);
3006 slang_operation_copy(innerBody, slang_oper_child(oper, 0));
3007 innerBody->locals->outer_scope = innerDo->locals;
3009 /* inner do-loop's condition is constant/false */
3011 slang_operation *constFalse = slang_oper_child(innerDo, 1);
3012 slang_operation_literal_bool(constFalse, GL_FALSE);
3016 /* _notBreakFlag && LOOPCOND */
3018 slang_operation *cond = slang_oper_child(outerDo, 1);
3019 cond->type = SLANG_OPER_LOGICALAND;
3020 slang_operation_add_children(cond, 2);
3022 slang_operation *notBreak = slang_oper_child(cond, 0);
3023 slang_operation_identifier(notBreak, A, "_notBreakFlag");
3026 slang_operation *origCond = slang_oper_child(cond, 1);
3027 slang_operation_copy(origCond, slang_oper_child(oper, 1));
3032 /* Finally, in innerBody,
3033 * replace "break" with "_notBreakFlag = 0; break"
3034 * replace "continue" with "break"
3036 replace_break_and_cont(A, innerBody);
3038 /*slang_print_tree(top, 0);*/
3040 return _slang_gen_operation(A, top);
3045 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
3047 static slang_ir_node *
3048 _slang_gen_do(slang_assemble_ctx * A, slang_operation *oper)
3052 * body code (child[0])
3054 * BREAK if !expr (child[1])
3056 slang_ir_node *loop;
3057 GLboolean isConst, constTrue;
3059 if (!A->EmitContReturn) {
3060 /* We don't want to emit CONT instructions. If this do-loop has
3061 * a continue, translate it away.
3063 if (_slang_loop_contains_continue(slang_oper_child(oper, 0))) {
3064 return _slang_gen_do_without_continue(A, oper);
3068 /* type-check expression */
3069 if (!_slang_is_boolean(A, &oper->children[1])) {
3070 slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
3074 loop = new_loop(NULL);
3076 /* save loop state */
3077 push_loop(A, oper, loop);
3080 loop->Children[0] = _slang_gen_operation(A, &oper->children[0]);
3082 /* Check if loop condition is a constant */
3083 isConst = _slang_is_constant_cond(&oper->children[1], &constTrue);
3084 if (isConst && constTrue) {
3085 /* do { } while(1) ==> no conditional break */
3086 loop->Children[1] = NULL; /* no tail code */
3090 = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
3091 loop->Children[1] = new_break_if_true(A, cond);
3094 /* XXX we should do infinite loop detection, as above */
3096 /* restore loop state */
3104 * Recursively count the number of operations rooted at 'oper'.
3105 * This gives some kind of indication of the size/complexity of an operation.
3108 sizeof_operation(const slang_operation *oper)
3111 GLuint count = 1; /* me */
3113 for (i = 0; i < oper->num_children; i++) {
3114 count += sizeof_operation(&oper->children[i]);
3125 * Determine if a for-loop can be unrolled.
3126 * At this time, only a rather narrow class of for loops can be unrolled.
3127 * See code for details.
3128 * When a loop can't be unrolled because it's too large we'll emit a
3129 * message to the log.
3132 _slang_can_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
3136 const char *varName;
3139 if (oper->type != SLANG_OPER_FOR)
3142 assert(oper->num_children == 4);
3144 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper, 3)))
3147 /* children[0] must be either "int i=constant" or "i=constant" */
3148 if (oper->children[0].type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) {
3149 slang_variable *var;
3151 if (oper->children[0].children[0].type != SLANG_OPER_VARIABLE_DECL)
3154 varId = oper->children[0].children[0].a_id;
3156 var = _slang_variable_locate(oper->children[0].children[0].locals,
3160 if (!var->initializer)
3162 if (var->initializer->type != SLANG_OPER_LITERAL_INT)
3164 start = (GLint) var->initializer->literal[0];
3166 else if (oper->children[0].type == SLANG_OPER_EXPRESSION) {
3167 if (oper->children[0].children[0].type != SLANG_OPER_ASSIGN)
3169 if (oper->children[0].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
3171 if (oper->children[0].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
3174 varId = oper->children[0].children[0].children[0].a_id;
3176 start = (GLint) oper->children[0].children[0].children[1].literal[0];
3182 /* children[1] must be "i<constant" */
3183 if (oper->children[1].type != SLANG_OPER_EXPRESSION)
3185 if (oper->children[1].children[0].type != SLANG_OPER_LESS)
3187 if (oper->children[1].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
3189 if (oper->children[1].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
3192 end = (GLint) oper->children[1].children[0].children[1].literal[0];
3194 /* children[2] must be "i++" or "++i" */
3195 if (oper->children[2].type != SLANG_OPER_POSTINCREMENT &&
3196 oper->children[2].type != SLANG_OPER_PREINCREMENT)
3198 if (oper->children[2].children[0].type != SLANG_OPER_IDENTIFIER)
3201 /* make sure the same variable name is used in all places */
3202 if ((oper->children[1].children[0].children[0].a_id != varId) ||
3203 (oper->children[2].children[0].a_id != varId))
3206 varName = (const char *) varId;
3208 /* children[3], the loop body, can't be too large */
3209 bodySize = sizeof_operation(&oper->children[3]);
3210 if (bodySize > MAX_FOR_LOOP_UNROLL_BODY_SIZE) {
3211 slang_info_log_print(A->log,
3212 "Note: 'for (%s ... )' body is too large/complex"
3219 return GL_FALSE; /* degenerate case */
3221 if (end - start > MAX_FOR_LOOP_UNROLL_ITERATIONS) {
3222 slang_info_log_print(A->log,
3223 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3224 " many iterations to unroll",
3225 varName, start, varName, end, varName);
3229 if ((end - start) * bodySize > MAX_FOR_LOOP_UNROLL_COMPLEXITY) {
3230 slang_info_log_print(A->log,
3231 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3232 " too much code to unroll",
3233 varName, start, varName, end, varName);
3237 return GL_TRUE; /* we can unroll the loop */
3242 * Unroll a for-loop.
3243 * First we determine the number of iterations to unroll.
3244 * Then for each iteration:
3245 * make a copy of the loop body
3246 * replace instances of the loop variable with the current iteration value
3247 * generate IR code for the body
3248 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3250 static slang_ir_node *
3251 _slang_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
3253 GLint start, end, iter;
3254 slang_ir_node *n, *root = NULL;
3257 if (oper->children[0].type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) {
3258 /* for (int i=0; ... */
3259 slang_variable *var;
3261 varId = oper->children[0].children[0].a_id;
3262 var = _slang_variable_locate(oper->children[0].children[0].locals,
3264 start = (GLint) var->initializer->literal[0];
3268 varId = oper->children[0].children[0].children[0].a_id;
3269 start = (GLint) oper->children[0].children[0].children[1].literal[0];
3272 end = (GLint) oper->children[1].children[0].children[1].literal[0];
3274 for (iter = start; iter < end; iter++) {
3275 slang_operation *body;
3277 /* make a copy of the loop body */
3278 body = slang_operation_new(1);
3282 if (!slang_operation_copy(body, &oper->children[3]))
3285 /* in body, replace instances of 'varId' with literal 'iter' */
3287 slang_variable *oldVar;
3288 slang_operation *newOper;
3290 oldVar = _slang_variable_locate(oper->locals, varId, GL_TRUE);
3292 /* undeclared loop variable */
3293 slang_operation_delete(body);
3297 newOper = slang_operation_new(1);
3298 newOper->type = SLANG_OPER_LITERAL_INT;
3299 newOper->literal_size = 1;
3300 newOper->literal[0] = iter;
3302 /* replace instances of the loop variable with newOper */
3303 slang_substitute(A, body, 1, &oldVar, &newOper, GL_FALSE);
3306 /* do IR codegen for body */
3307 n = _slang_gen_operation(A, body);
3311 root = new_seq(root, n);
3313 slang_operation_delete(body);
3321 * Replace 'continue' statement with 'break' inside a for-loop.
3322 * This is a recursive helper function used by _slang_gen_for_without_continue().
3325 replace_continue_with_break(slang_assemble_ctx *A, slang_operation *oper)
3327 switch (oper->type) {
3328 case SLANG_OPER_CONTINUE:
3329 oper->type = SLANG_OPER_BREAK;
3331 case SLANG_OPER_FOR:
3333 case SLANG_OPER_WHILE:
3334 /* stop upon finding a nested loop */
3340 for (i = 0; i < oper->num_children; i++) {
3341 replace_continue_with_break(A, slang_oper_child(oper, i));
3349 * Transform a for-loop so that continue statements are converted to breaks.
3350 * Then do normal IR code generation.
3354 * for (INIT; LOOPCOND; INCR) {
3365 * bool _condFlag = 1;
3366 * for (INIT; _condFlag; ) {
3367 * for ( ; _condFlag = LOOPCOND; INCR) {
3379 static slang_ir_node *
3380 _slang_gen_for_without_continue(slang_assemble_ctx *A, slang_operation *oper)
3382 slang_operation *top;
3383 slang_operation *outerFor, *innerFor, *init, *cond, *incr;
3384 slang_operation *lhs, *rhs;
3386 assert(oper->type == SLANG_OPER_FOR);
3388 top = slang_operation_new(1);
3389 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
3390 top->locals->outer_scope = oper->locals->outer_scope;
3391 slang_operation_add_children(top, 2);
3393 /* declare: bool _condFlag = true */
3395 slang_operation *condDecl = slang_oper_child(top, 0);
3396 slang_generate_declaration(A, top->locals, condDecl,
3397 SLANG_SPEC_BOOL, "_condFlag", GL_TRUE);
3400 /* build outer loop: for (INIT; _condFlag; ) { */
3401 outerFor = slang_oper_child(top, 1);
3402 outerFor->type = SLANG_OPER_FOR;
3403 slang_operation_add_children(outerFor, 4);
3405 init = slang_oper_child(outerFor, 0);
3406 slang_operation_copy(init, slang_oper_child(oper, 0));
3408 cond = slang_oper_child(outerFor, 1);
3409 cond->type = SLANG_OPER_IDENTIFIER;
3410 cond->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3412 incr = slang_oper_child(outerFor, 2);
3413 incr->type = SLANG_OPER_VOID;
3415 /* body of the outer loop */
3417 slang_operation *block = slang_oper_child(outerFor, 3);
3419 slang_operation_add_children(block, 2);
3420 block->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
3422 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3424 innerFor = slang_oper_child(block, 0);
3426 /* make copy of orig loop */
3427 slang_operation_copy(innerFor, oper);
3428 assert(innerFor->type == SLANG_OPER_FOR);
3429 innerFor->locals->outer_scope = block->locals;
3431 init = slang_oper_child(innerFor, 0);
3432 init->type = SLANG_OPER_VOID; /* leak? */
3434 cond = slang_oper_child(innerFor, 1);
3435 slang_operation_destruct(cond);
3436 cond->type = SLANG_OPER_ASSIGN;
3437 cond->locals = _slang_variable_scope_new(innerFor->locals);
3438 slang_operation_add_children(cond, 2);
3440 lhs = slang_oper_child(cond, 0);
3441 lhs->type = SLANG_OPER_IDENTIFIER;
3442 lhs->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3444 rhs = slang_oper_child(cond, 1);
3445 slang_operation_copy(rhs, slang_oper_child(oper, 1));
3448 /* if (_condFlag) INCR; */
3450 slang_operation *ifop = slang_oper_child(block, 1);
3451 ifop->type = SLANG_OPER_IF;
3452 slang_operation_add_children(ifop, 2);
3454 /* re-use cond node build above */
3455 slang_operation_copy(slang_oper_child(ifop, 0), cond);
3457 /* incr node from original for-loop operation */
3458 slang_operation_copy(slang_oper_child(ifop, 1),
3459 slang_oper_child(oper, 2));
3462 /* finally, replace "continue" with "break" in the inner for-loop */
3463 replace_continue_with_break(A, slang_oper_child(innerFor, 3));
3466 return _slang_gen_operation(A, top);
3472 * Generate IR for a for-loop. Unrolling will be done when possible.
3474 static slang_ir_node *
3475 _slang_gen_for(slang_assemble_ctx * A, slang_operation *oper)
3479 if (!A->EmitContReturn) {
3480 /* We don't want to emit CONT instructions. If this for-loop has
3481 * a continue, translate it away.
3483 if (_slang_loop_contains_continue(slang_oper_child(oper, 3))) {
3484 return _slang_gen_for_without_continue(A, oper);
3488 unroll = _slang_can_unroll_for_loop(A, oper);
3490 slang_ir_node *code = _slang_unroll_for_loop(A, oper);
3495 assert(oper->type == SLANG_OPER_FOR);
3497 /* conventional for-loop code generation */
3500 * init code (child[0])
3502 * BREAK if !expr (child[1])
3503 * body code (child[3])
3505 * incr code (child[2]) // XXX continue here
3507 slang_ir_node *loop, *cond, *breakIf, *body, *init, *incr;
3508 init = _slang_gen_operation(A, &oper->children[0]);
3509 loop = new_loop(NULL);
3511 /* save loop state */
3512 push_loop(A, oper, loop);
3514 cond = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
3515 breakIf = new_break_if_true(A, cond);
3516 body = _slang_gen_operation(A, &oper->children[3]);
3517 incr = _slang_gen_operation(A, &oper->children[2]);
3519 loop->Children[0] = new_seq(breakIf, body);
3520 loop->Children[1] = incr; /* tail code */
3522 /* restore loop state */
3525 return new_seq(init, loop);
3530 static slang_ir_node *
3531 _slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
3533 slang_ir_node *n, *cont, *incr = NULL, *loopNode;
3535 assert(oper->type == SLANG_OPER_CONTINUE);
3536 loopNode = current_loop_ir(A);
3538 assert(loopNode->Opcode == IR_LOOP);
3540 cont = new_node0(IR_CONT);
3542 cont->Parent = loopNode;
3543 /* insert this node at head of linked list of cont/break instructions */
3544 cont->List = loopNode->List;
3545 loopNode->List = cont;
3548 n = new_seq(incr, cont);
3554 * Determine if the given operation is of a specific type.
3557 is_operation_type(const slang_operation *oper, slang_operation_type type)
3559 if (oper->type == type)
3561 else if ((oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
3562 oper->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) &&
3563 oper->num_children == 1)
3564 return is_operation_type(&oper->children[0], type);
3571 * Generate IR tree for an if/then/else conditional using high-level
3572 * IR_IF instruction.
3574 static slang_ir_node *
3575 _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
3578 * eval expr (child[0])
3585 const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
3586 slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
3587 GLboolean isConst, constTrue;
3589 /* type-check expression */
3590 if (!_slang_is_boolean(A, &oper->children[0])) {
3591 slang_info_log_error(A->log, "boolean expression expected for 'if'");
3595 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3596 slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
3600 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
3604 return _slang_gen_operation(A, &oper->children[1]);
3607 /* if (false) ... */
3608 return _slang_gen_operation(A, &oper->children[2]);
3612 cond = _slang_gen_operation(A, &oper->children[0]);
3613 cond = new_cond(cond);
3615 if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)
3616 && !haveElseClause) {
3617 /* Special case: generate a conditional break */
3618 ifBody = new_break_if_true(A, cond);
3621 else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)
3623 && current_loop_oper(A)
3624 && current_loop_oper(A)->type != SLANG_OPER_FOR) {
3625 /* Special case: generate a conditional continue */
3626 ifBody = new_cont_if_true(A, cond);
3631 ifBody = _slang_gen_operation(A, &oper->children[1]);
3633 elseBody = _slang_gen_operation(A, &oper->children[2]);
3636 ifNode = new_if(cond, ifBody, elseBody);
3643 static slang_ir_node *
3644 _slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
3648 assert(oper->type == SLANG_OPER_NOT);
3650 /* type-check expression */
3651 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3652 slang_info_log_error(A->log,
3653 "scalar/boolean expression expected for '!'");
3657 n = _slang_gen_operation(A, &oper->children[0]);
3665 static slang_ir_node *
3666 _slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
3668 slang_ir_node *n1, *n2;
3670 assert(oper->type == SLANG_OPER_LOGICALXOR);
3672 if (!_slang_is_scalar_or_boolean(A, &oper->children[0]) ||
3673 !_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3674 slang_info_log_error(A->log,
3675 "scalar/boolean expressions expected for '^^'");
3679 n1 = _slang_gen_operation(A, &oper->children[0]);
3682 n2 = _slang_gen_operation(A, &oper->children[1]);
3685 return new_node2(IR_NOTEQUAL, n1, n2);
3690 * Generate IR node for storage of a temporary of given size.
3692 static slang_ir_node *
3693 _slang_gen_temporary(GLint size)
3695 slang_ir_storage *store;
3696 slang_ir_node *n = NULL;
3698 store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -2, size);
3700 n = new_node0(IR_VAR_DECL);
3713 * Generate program constants for an array.
3714 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3715 * This will allocate and initialize three vector constants, storing
3716 * the array in constant memory, not temporaries like a non-const array.
3717 * This can also be used for uniform array initializers.
3718 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3721 make_constant_array(slang_assemble_ctx *A,
3722 slang_variable *var,
3723 slang_operation *initializer)
3725 struct gl_program *prog = A->program;
3726 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3727 const char *varName = (char *) var->a_name;
3728 const GLuint numElements = initializer->num_children;
3734 var->store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -6, -6);
3736 size = var->store->Size;
3738 assert(var->type.qualifier == SLANG_QUAL_CONST ||
3739 var->type.qualifier == SLANG_QUAL_UNIFORM);
3740 assert(initializer->type == SLANG_OPER_CALL);
3741 assert(initializer->array_constructor);
3743 values = (GLfloat *) _mesa_malloc(numElements * 4 * sizeof(GLfloat));
3745 /* convert constructor params into ordinary floats */
3746 for (i = 0; i < numElements; i++) {
3747 const slang_operation *op = &initializer->children[i];
3748 if (op->type != SLANG_OPER_LITERAL_FLOAT) {
3749 /* unsupported type for this optimization */
3753 for (j = 0; j < op->literal_size; j++) {
3754 values[i * 4 + j] = op->literal[j];
3756 for ( ; j < 4; j++) {
3757 values[i * 4 + j] = 0.0f;
3761 /* slightly different paths for constants vs. uniforms */
3762 if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3763 var->store->File = PROGRAM_UNIFORM;
3764 var->store->Index = _mesa_add_uniform(prog->Parameters, varName,
3765 size, datatype, values);
3768 var->store->File = PROGRAM_CONSTANT;
3769 var->store->Index = _mesa_add_named_constant(prog->Parameters, varName,
3772 assert(var->store->Size == size);
3782 * Generate IR node for allocating/declaring a variable (either a local or
3784 * Generally, this involves allocating an slang_ir_storage instance for the
3785 * variable, choosing a register file (temporary, constant, etc).
3786 * For ordinary variables we do not yet allocate storage though. We do that
3787 * when we find the first actual use of the variable to avoid allocating temp
3788 * regs that will never get used.
3789 * At this time, uniforms are always allocated space in this function.
3791 * \param initializer Optional initializer expression for the variable.
3793 static slang_ir_node *
3794 _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var,
3795 slang_operation *initializer)
3797 const char *varName = (const char *) var->a_name;
3798 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3799 slang_ir_node *varDecl, *n;
3800 slang_ir_storage *store;
3801 GLint arrayLen, size, totalSize; /* if array then totalSize > size */
3802 gl_register_file file;
3804 /*assert(!var->declared);*/
3805 var->declared = GL_TRUE;
3807 /* determine GPU register file for simple cases */
3808 if (is_sampler_type(&var->type)) {
3809 file = PROGRAM_SAMPLER;
3811 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3812 file = PROGRAM_UNIFORM;
3815 file = PROGRAM_TEMPORARY;
3818 size = _slang_sizeof_type_specifier(&var->type.specifier);
3820 slang_info_log_error(A->log, "invalid declaration for '%s'", varName);
3824 arrayLen = _slang_array_length(var);
3825 totalSize = _slang_array_size(size, arrayLen);
3827 /* Allocate IR node for the declaration */
3828 varDecl = new_node0(IR_VAR_DECL);
3832 /* Allocate slang_ir_storage for this variable if needed.
3833 * Note that we may not actually allocate a constant or temporary register
3837 GLint index = -7; /* TBD / unknown */
3838 var->store = _slang_new_ir_storage(file, index, totalSize);
3840 return NULL; /* out of memory */
3843 /* set the IR node's Var and Store pointers */
3845 varDecl->Store = var->store;
3850 /* if there's an initializer, generate IR for the expression */
3852 slang_ir_node *varRef, *init;
3854 if (var->type.qualifier == SLANG_QUAL_CONST) {
3855 /* if the variable is const, the initializer must be a const
3856 * expression as well.
3859 if (!_slang_is_constant_expr(initializer)) {
3860 slang_info_log_error(A->log,
3861 "initializer for %s not constant", varName);
3867 /* IR for the variable we're initializing */
3868 varRef = new_var(A, var);
3870 slang_info_log_error(A->log, "out of memory");
3874 /* constant-folding, etc here */
3875 _slang_simplify(initializer, &A->space, A->atoms);
3877 /* look for simple constant-valued variables and uniforms */
3878 if (var->type.qualifier == SLANG_QUAL_CONST ||
3879 var->type.qualifier == SLANG_QUAL_UNIFORM) {
3881 if (initializer->type == SLANG_OPER_CALL &&
3882 initializer->array_constructor) {
3883 /* array initializer */
3884 if (make_constant_array(A, var, initializer))
3887 else if (initializer->type == SLANG_OPER_LITERAL_FLOAT ||
3888 initializer->type == SLANG_OPER_LITERAL_INT) {
3889 /* simple float/vector initializer */
3890 if (store->File == PROGRAM_UNIFORM) {
3891 store->Index = _mesa_add_uniform(A->program->Parameters,
3893 totalSize, datatype,
3894 initializer->literal);
3895 store->Swizzle = _slang_var_swizzle(size, 0);
3900 store->File = PROGRAM_CONSTANT;
3901 store->Index = _mesa_add_named_constant(A->program->Parameters,
3903 initializer->literal,
3905 store->Swizzle = _slang_var_swizzle(size, 0);
3912 /* IR for initializer */
3913 init = _slang_gen_operation(A, initializer);
3917 /* XXX remove this when type checking is added above */
3918 if (init->Store && init->Store->Size != totalSize) {
3919 slang_info_log_error(A->log, "invalid assignment (wrong types)");
3923 /* assign RHS to LHS */
3924 n = new_node2(IR_COPY, varRef, init);
3925 n = new_seq(varDecl, n);
3928 /* no initializer */
3932 if (store->File == PROGRAM_UNIFORM && store->Index < 0) {
3933 /* always need to allocate storage for uniforms at this point */
3934 store->Index = _mesa_add_uniform(A->program->Parameters, varName,
3935 totalSize, datatype, NULL);
3936 store->Swizzle = _slang_var_swizzle(size, 0);
3940 printf("%s var %p %s store=%p index=%d size=%d\n",
3941 __FUNCTION__, (void *) var, (char *) varName,
3942 (void *) store, store->Index, store->Size);
3950 * Generate code for a selection expression: b ? x : y
3951 * XXX In some cases we could implement a selection expression
3952 * with an LRP instruction (use the boolean as the interpolant).
3953 * Otherwise, we use an IF/ELSE/ENDIF construct.
3955 static slang_ir_node *
3956 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
3958 slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
3959 slang_ir_node *tmpDecl, *tmpVar, *tree;
3960 slang_typeinfo type0, type1, type2;
3961 int size, isBool, isEqual;
3963 assert(oper->type == SLANG_OPER_SELECT);
3964 assert(oper->num_children == 3);
3966 /* type of children[0] must be boolean */
3967 slang_typeinfo_construct(&type0);
3968 typeof_operation(A, &oper->children[0], &type0);
3969 isBool = (type0.spec.type == SLANG_SPEC_BOOL);
3970 slang_typeinfo_destruct(&type0);
3972 slang_info_log_error(A->log, "selector type is not boolean");
3976 slang_typeinfo_construct(&type1);
3977 slang_typeinfo_construct(&type2);
3978 typeof_operation(A, &oper->children[1], &type1);
3979 typeof_operation(A, &oper->children[2], &type2);
3980 isEqual = slang_type_specifier_equal(&type1.spec, &type2.spec);
3981 slang_typeinfo_destruct(&type1);
3982 slang_typeinfo_destruct(&type2);
3984 slang_info_log_error(A->log, "incompatible types for ?: operator");
3988 /* size of x or y's type */
3989 size = _slang_sizeof_type_specifier(&type1.spec);
3993 tmpDecl = _slang_gen_temporary(size);
3995 /* the condition (child 0) */
3996 cond = _slang_gen_operation(A, &oper->children[0]);
3997 cond = new_cond(cond);
3999 /* if-true body (child 1) */
4000 tmpVar = new_node0(IR_VAR);
4001 tmpVar->Store = tmpDecl->Store;
4002 trueExpr = _slang_gen_operation(A, &oper->children[1]);
4003 trueNode = new_node2(IR_COPY, tmpVar, trueExpr);
4005 /* if-false body (child 2) */
4006 tmpVar = new_node0(IR_VAR);
4007 tmpVar->Store = tmpDecl->Store;
4008 falseExpr = _slang_gen_operation(A, &oper->children[2]);
4009 falseNode = new_node2(IR_COPY, tmpVar, falseExpr);
4011 ifNode = new_if(cond, trueNode, falseNode);
4014 tmpVar = new_node0(IR_VAR);
4015 tmpVar->Store = tmpDecl->Store;
4017 tree = new_seq(ifNode, tmpVar);
4018 tree = new_seq(tmpDecl, tree);
4020 /*_slang_print_ir_tree(tree, 10);*/
4026 * Generate code for &&.
4028 static slang_ir_node *
4029 _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
4031 /* rewrite "a && b" as "a ? b : false" */
4032 slang_operation *select;
4035 select = slang_operation_new(1);
4036 select->type = SLANG_OPER_SELECT;
4037 slang_operation_add_children(select, 3);
4039 slang_operation_copy(slang_oper_child(select, 0), &oper->children[0]);
4040 slang_operation_copy(slang_oper_child(select, 1), &oper->children[1]);
4041 slang_operation_literal_bool(slang_oper_child(select, 2), GL_FALSE);
4043 n = _slang_gen_select(A, select);
4049 * Generate code for ||.
4051 static slang_ir_node *
4052 _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
4054 /* rewrite "a || b" as "a ? true : b" */
4055 slang_operation *select;
4058 select = slang_operation_new(1);
4059 select->type = SLANG_OPER_SELECT;
4060 slang_operation_add_children(select, 3);
4062 slang_operation_copy(slang_oper_child(select, 0), &oper->children[0]);
4063 slang_operation_literal_bool(slang_oper_child(select, 1), GL_TRUE);
4064 slang_operation_copy(slang_oper_child(select, 2), &oper->children[1]);
4066 n = _slang_gen_select(A, select);
4072 * Generate IR tree for a return statement.
4074 static slang_ir_node *
4075 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
4077 assert(oper->type == SLANG_OPER_RETURN);
4078 return new_return(A->curFuncEndLabel);
4084 * Determine if the given operation/expression is const-valued.
4087 _slang_is_constant_expr(const slang_operation *oper)
4089 slang_variable *var;
4092 switch (oper->type) {
4093 case SLANG_OPER_IDENTIFIER:
4094 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4095 if (var && var->type.qualifier == SLANG_QUAL_CONST)
4099 for (i = 0; i < oper->num_children; i++) {
4100 if (!_slang_is_constant_expr(&oper->children[i]))
4110 * Check if an assignment of type t1 to t0 is legal.
4111 * XXX more cases needed.
4114 _slang_assignment_compatible(slang_assemble_ctx *A,
4115 slang_operation *op0,
4116 slang_operation *op1)
4118 slang_typeinfo t0, t1;
4121 if (op0->type == SLANG_OPER_POSTINCREMENT ||
4122 op0->type == SLANG_OPER_POSTDECREMENT) {
4126 slang_typeinfo_construct(&t0);
4127 typeof_operation(A, op0, &t0);
4129 slang_typeinfo_construct(&t1);
4130 typeof_operation(A, op1, &t1);
4132 sz0 = _slang_sizeof_type_specifier(&t0.spec);
4133 sz1 = _slang_sizeof_type_specifier(&t1.spec);
4137 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4142 if (t0.spec.type == SLANG_SPEC_STRUCT &&
4143 t1.spec.type == SLANG_SPEC_STRUCT &&
4144 t0.spec._struct->a_name != t1.spec._struct->a_name)
4147 if (t0.spec.type == SLANG_SPEC_FLOAT &&
4148 t1.spec.type == SLANG_SPEC_BOOL)
4151 #if 0 /* not used just yet - causes problems elsewhere */
4152 if (t0.spec.type == SLANG_SPEC_INT &&
4153 t1.spec.type == SLANG_SPEC_FLOAT)
4157 if (t0.spec.type == SLANG_SPEC_BOOL &&
4158 t1.spec.type == SLANG_SPEC_FLOAT)
4161 if (t0.spec.type == SLANG_SPEC_BOOL &&
4162 t1.spec.type == SLANG_SPEC_INT)
4170 * Generate IR tree for a local variable declaration.
4171 * Basically do some error checking and call _slang_gen_var_decl().
4173 static slang_ir_node *
4174 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
4176 const char *varName = (char *) oper->a_id;
4177 slang_variable *var;
4178 slang_ir_node *varDecl;
4179 slang_operation *initializer;
4181 assert(oper->type == SLANG_OPER_VARIABLE_DECL);
4182 assert(oper->num_children <= 1);
4185 /* lookup the variable by name */
4186 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4188 return NULL; /* "shouldn't happen" */
4190 if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4191 var->type.qualifier == SLANG_QUAL_VARYING ||
4192 var->type.qualifier == SLANG_QUAL_UNIFORM) {
4193 /* can't declare attribute/uniform vars inside functions */
4194 slang_info_log_error(A->log,
4195 "local variable '%s' cannot be an attribute/uniform/varying",
4202 slang_info_log_error(A->log, "variable '%s' redeclared", varName);
4207 /* check if the var has an initializer */
4208 if (oper->num_children > 0) {
4209 assert(oper->num_children == 1);
4210 initializer = &oper->children[0];
4212 else if (var->initializer) {
4213 initializer = var->initializer;
4220 /* check/compare var type and initializer type */
4221 if (!_slang_assignment_compatible(A, oper, initializer)) {
4222 slang_info_log_error(A->log, "incompatible types in assignment");
4227 if (var->type.qualifier == SLANG_QUAL_CONST) {
4228 slang_info_log_error(A->log,
4229 "const-qualified variable '%s' requires initializer",
4235 /* Generate IR node */
4236 varDecl = _slang_gen_var_decl(A, var, initializer);
4245 * Generate IR tree for a reference to a variable (such as in an expression).
4246 * This is different from a variable declaration.
4248 static slang_ir_node *
4249 _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
4251 /* If there's a variable associated with this oper (from inlining)
4252 * use it. Otherwise, use the oper's var id.
4254 slang_atom name = oper->var ? oper->var->a_name : oper->a_id;
4255 slang_variable *var = _slang_variable_locate(oper->locals, name, GL_TRUE);
4258 slang_info_log_error(A->log, "undefined variable '%s'", (char *) name);
4261 assert(var->declared);
4262 n = new_var(A, var);
4269 * Return the number of components actually named by the swizzle.
4270 * Recall that swizzles may have undefined/don't-care values.
4273 swizzle_size(GLuint swizzle)
4276 for (i = 0; i < 4; i++) {
4277 GLuint swz = GET_SWZ(swizzle, i);
4278 size += (swz >= 0 && swz <= 3);
4284 static slang_ir_node *
4285 _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
4287 slang_ir_node *n = new_node1(IR_SWIZZLE, child);
4291 n->Store = _slang_new_ir_storage_relative(0,
4292 swizzle_size(swizzle),
4294 n->Store->Swizzle = swizzle;
4301 is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
4303 while (store->Parent)
4304 store = store->Parent;
4306 if (!(store->File == PROGRAM_OUTPUT ||
4307 store->File == PROGRAM_TEMPORARY ||
4308 (store->File == PROGRAM_VARYING &&
4309 A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
4319 * Walk up an IR storage path to compute the final swizzle.
4320 * This is used when we find an expression such as "foo.xz.yx".
4323 root_swizzle(const slang_ir_storage *st)
4325 GLuint swizzle = st->Swizzle;
4326 while (st->Parent) {
4328 swizzle = _slang_swizzle_swizzle(st->Swizzle, swizzle);
4335 * Generate IR tree for an assignment (=).
4337 static slang_ir_node *
4338 _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
4340 slang_operation *pred = NULL;
4341 slang_ir_node *n = NULL;
4343 if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
4344 /* Check that var is writeable */
4346 = _slang_variable_locate(oper->children[0].locals,
4347 oper->children[0].a_id, GL_TRUE);
4349 slang_info_log_error(A->log, "undefined variable '%s'",
4350 (char *) oper->children[0].a_id);
4353 if (var->type.qualifier == SLANG_QUAL_CONST ||
4354 var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4355 var->type.qualifier == SLANG_QUAL_UNIFORM ||
4356 (var->type.qualifier == SLANG_QUAL_VARYING &&
4357 A->program->Target == GL_FRAGMENT_PROGRAM_ARB)) {
4358 slang_info_log_error(A->log,
4359 "illegal assignment to read-only variable '%s'",
4360 (char *) oper->children[0].a_id);
4364 /* check if we need to predicate this assignment based on __notRetFlag */
4365 if ((var->is_global ||
4366 var->type.qualifier == SLANG_QUAL_OUT ||
4367 var->type.qualifier == SLANG_QUAL_INOUT) && A->UseReturnFlag) {
4368 /* create predicate, used below */
4369 pred = slang_operation_new(1);
4370 pred->type = SLANG_OPER_IDENTIFIER;
4371 pred->a_id = slang_atom_pool_atom(A->atoms, "__notRetFlag");
4372 pred->locals->outer_scope = oper->locals->outer_scope;
4376 if (oper->children[0].type == SLANG_OPER_IDENTIFIER &&
4377 oper->children[1].type == SLANG_OPER_CALL) {
4378 /* Special case of: x = f(a, b)
4379 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4381 * XXX this could be even more effective if we could accomodate
4382 * cases such as "v.x = f();" - would help with typical vertex
4385 n = _slang_gen_function_call_name(A,
4386 (const char *) oper->children[1].a_id,
4387 &oper->children[1], &oper->children[0]);
4390 slang_ir_node *lhs, *rhs;
4392 /* lhs and rhs type checking */
4393 if (!_slang_assignment_compatible(A,
4395 &oper->children[1])) {
4396 slang_info_log_error(A->log, "incompatible types in assignment");
4400 lhs = _slang_gen_operation(A, &oper->children[0]);
4406 slang_info_log_error(A->log,
4407 "invalid left hand side for assignment");
4411 /* check that lhs is writable */
4412 if (!is_store_writable(A, lhs->Store)) {
4413 slang_info_log_error(A->log,
4414 "illegal assignment to read-only l-value");
4418 rhs = _slang_gen_operation(A, &oper->children[1]);
4420 /* convert lhs swizzle into writemask */
4421 const GLuint swizzle = root_swizzle(lhs->Store);
4422 GLuint writemask, newSwizzle = 0x0;
4423 if (!swizzle_to_writemask(A, swizzle, &writemask, &newSwizzle)) {
4424 /* Non-simple writemask, need to swizzle right hand side in
4425 * order to put components into the right place.
4427 rhs = _slang_gen_swizzle(rhs, newSwizzle);
4429 n = new_node2(IR_COPY, lhs, rhs);
4437 /* predicate the assignment code on __notRetFlag */
4438 slang_ir_node *top, *cond;
4440 cond = _slang_gen_operation(A, pred);
4441 top = new_if(cond, n, NULL);
4449 * Generate IR tree for referencing a field in a struct (or basic vector type)
4451 static slang_ir_node *
4452 _slang_gen_struct_field(slang_assemble_ctx * A, slang_operation *oper)
4456 /* type of struct */
4457 slang_typeinfo_construct(&ti);
4458 typeof_operation(A, &oper->children[0], &ti);
4460 if (_slang_type_is_vector(ti.spec.type)) {
4461 /* the field should be a swizzle */
4462 const GLuint rows = _slang_type_dim(ti.spec.type);
4466 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4467 slang_info_log_error(A->log, "Bad swizzle");
4470 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4475 n = _slang_gen_operation(A, &oper->children[0]);
4476 /* create new parent node with swizzle */
4478 n = _slang_gen_swizzle(n, swizzle);
4481 else if ( ti.spec.type == SLANG_SPEC_FLOAT
4482 || ti.spec.type == SLANG_SPEC_INT
4483 || ti.spec.type == SLANG_SPEC_BOOL) {
4484 const GLuint rows = 1;
4488 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4489 slang_info_log_error(A->log, "Bad swizzle");
4491 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4495 n = _slang_gen_operation(A, &oper->children[0]);
4496 /* create new parent node with swizzle */
4497 n = _slang_gen_swizzle(n, swizzle);
4501 /* the field is a structure member (base.field) */
4502 /* oper->children[0] is the base */
4503 /* oper->a_id is the field name */
4504 slang_ir_node *base, *n;
4505 slang_typeinfo field_ti;
4506 GLint fieldSize, fieldOffset = -1;
4509 slang_typeinfo_construct(&field_ti);
4510 typeof_operation(A, oper, &field_ti);
4512 fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
4514 fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
4516 if (fieldSize == 0 || fieldOffset < 0) {
4517 const char *structName;
4518 if (ti.spec._struct)
4519 structName = (char *) ti.spec._struct->a_name;
4521 structName = "unknown";
4522 slang_info_log_error(A->log,
4523 "\"%s\" is not a member of struct \"%s\"",
4524 (char *) oper->a_id, structName);
4527 assert(fieldSize >= 0);
4529 base = _slang_gen_operation(A, &oper->children[0]);
4531 /* error msg should have already been logged */
4535 n = new_node1(IR_FIELD, base);
4539 n->Field = (char *) oper->a_id;
4541 /* Store the field's offset in storage->Index */
4542 n->Store = _slang_new_ir_storage(base->Store->File,
4552 * Gen code for array indexing.
4554 static slang_ir_node *
4555 _slang_gen_array_element(slang_assemble_ctx * A, slang_operation *oper)
4557 slang_typeinfo array_ti;
4559 /* get array's type info */
4560 slang_typeinfo_construct(&array_ti);
4561 typeof_operation(A, &oper->children[0], &array_ti);
4563 if (_slang_type_is_vector(array_ti.spec.type)) {
4564 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4565 /* translate the index into a swizzle/writemask: "v.x=p" */
4566 const GLuint max = _slang_type_dim(array_ti.spec.type);
4570 index = (GLint) oper->children[1].literal[0];
4571 if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
4572 index >= (GLint) max) {
4574 slang_info_log_error(A->log, "Invalid array index for vector type");
4575 printf("type = %d\n", oper->children[1].type);
4576 printf("index = %d, max = %d\n", index, max);
4577 printf("array = %s\n", (char*)oper->children[0].a_id);
4578 printf("index = %s\n", (char*)oper->children[1].a_id);
4585 n = _slang_gen_operation(A, &oper->children[0]);
4587 /* use swizzle to access the element */
4588 GLuint swizzle = MAKE_SWIZZLE4(SWIZZLE_X + index,
4592 n = _slang_gen_swizzle(n, swizzle);
4598 /* conventional array */
4599 slang_typeinfo elem_ti;
4600 slang_ir_node *elem, *array, *index;
4601 GLint elemSize, arrayLen;
4603 /* size of array element */
4604 slang_typeinfo_construct(&elem_ti);
4605 typeof_operation(A, oper, &elem_ti);
4606 elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
4608 if (_slang_type_is_matrix(array_ti.spec.type))
4609 arrayLen = _slang_type_dim(array_ti.spec.type);
4611 arrayLen = array_ti.array_len;
4613 slang_typeinfo_destruct(&array_ti);
4614 slang_typeinfo_destruct(&elem_ti);
4616 if (elemSize <= 0) {
4617 /* unknown var or type */
4618 slang_info_log_error(A->log, "Undefined variable or type");
4622 array = _slang_gen_operation(A, &oper->children[0]);
4623 index = _slang_gen_operation(A, &oper->children[1]);
4624 if (array && index) {
4626 GLint constIndex = -1;
4627 if (index->Opcode == IR_FLOAT) {
4628 constIndex = (int) index->Value[0];
4629 if (constIndex < 0 || constIndex >= arrayLen) {
4630 slang_info_log_error(A->log,
4631 "Array index out of bounds (index=%d size=%d)",
4632 constIndex, arrayLen);
4633 _slang_free_ir_tree(array);
4634 _slang_free_ir_tree(index);
4639 if (!array->Store) {
4640 slang_info_log_error(A->log, "Invalid array");
4644 elem = new_node2(IR_ELEMENT, array, index);
4646 /* The storage info here will be updated during code emit */
4647 elem->Store = _slang_new_ir_storage(array->Store->File,
4648 array->Store->Index,
4650 elem->Store->Swizzle = _slang_var_swizzle(elemSize, 0);
4654 _slang_free_ir_tree(array);
4655 _slang_free_ir_tree(index);
4662 static slang_ir_node *
4663 _slang_gen_compare(slang_assemble_ctx *A, slang_operation *oper,
4664 slang_ir_opcode opcode)
4666 slang_typeinfo t0, t1;
4669 slang_typeinfo_construct(&t0);
4670 typeof_operation(A, &oper->children[0], &t0);
4672 slang_typeinfo_construct(&t1);
4673 typeof_operation(A, &oper->children[0], &t1);
4675 if (t0.spec.type == SLANG_SPEC_ARRAY ||
4676 t1.spec.type == SLANG_SPEC_ARRAY) {
4677 slang_info_log_error(A->log, "Illegal array comparison");
4681 if (oper->type != SLANG_OPER_EQUAL &&
4682 oper->type != SLANG_OPER_NOTEQUAL) {
4683 /* <, <=, >, >= can only be used with scalars */
4684 if ((t0.spec.type != SLANG_SPEC_INT &&
4685 t0.spec.type != SLANG_SPEC_FLOAT) ||
4686 (t1.spec.type != SLANG_SPEC_INT &&
4687 t1.spec.type != SLANG_SPEC_FLOAT)) {
4688 slang_info_log_error(A->log, "Incompatible type(s) for inequality operator");
4693 n = new_node2(opcode,
4694 _slang_gen_operation(A, &oper->children[0]),
4695 _slang_gen_operation(A, &oper->children[1]));
4697 /* result is a bool (size 1) */
4698 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, 1);
4706 print_vars(slang_variable_scope *s)
4710 for (i = 0; i < s->num_variables; i++) {
4712 (char*) s->variables[i]->a_name,
4713 s->variables[i]->declared);
4723 _slang_undeclare_vars(slang_variable_scope *locals)
4725 if (locals->num_variables > 0) {
4727 for (i = 0; i < locals->num_variables; i++) {
4728 slang_variable *v = locals->variables[i];
4729 printf("undeclare %s at %p\n", (char*) v->a_name, v);
4730 v->declared = GL_FALSE;
4738 * Generate IR tree for a slang_operation (AST node)
4740 static slang_ir_node *
4741 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
4743 switch (oper->type) {
4744 case SLANG_OPER_BLOCK_NEW_SCOPE:
4748 _slang_push_var_table(A->vartable);
4750 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE; /* temp change */
4751 n = _slang_gen_operation(A, oper);
4752 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE; /* restore */
4754 _slang_pop_var_table(A->vartable);
4756 /*_slang_undeclare_vars(oper->locals);*/
4757 /*print_vars(oper->locals);*/
4760 n = new_node1(IR_SCOPE, n);
4765 case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
4766 /* list of operations */
4767 if (oper->num_children > 0)
4769 slang_ir_node *n, *tree = NULL;
4772 for (i = 0; i < oper->num_children; i++) {
4773 n = _slang_gen_operation(A, &oper->children[i]);
4775 _slang_free_ir_tree(tree);
4776 return NULL; /* error must have occured */
4778 tree = new_seq(tree, n);
4784 return new_node0(IR_NOP);
4787 case SLANG_OPER_EXPRESSION:
4788 return _slang_gen_operation(A, &oper->children[0]);
4790 case SLANG_OPER_FOR:
4791 return _slang_gen_for(A, oper);
4793 return _slang_gen_do(A, oper);
4794 case SLANG_OPER_WHILE:
4795 return _slang_gen_while(A, oper);
4796 case SLANG_OPER_BREAK:
4797 if (!current_loop_oper(A)) {
4798 slang_info_log_error(A->log, "'break' not in loop");
4801 return new_break(current_loop_ir(A));
4802 case SLANG_OPER_CONTINUE:
4803 if (!current_loop_oper(A)) {
4804 slang_info_log_error(A->log, "'continue' not in loop");
4807 return _slang_gen_continue(A, oper);
4808 case SLANG_OPER_DISCARD:
4809 return new_node0(IR_KILL);
4811 case SLANG_OPER_EQUAL:
4812 return _slang_gen_compare(A, oper, IR_EQUAL);
4813 case SLANG_OPER_NOTEQUAL:
4814 return _slang_gen_compare(A, oper, IR_NOTEQUAL);
4815 case SLANG_OPER_GREATER:
4816 return _slang_gen_compare(A, oper, IR_SGT);
4817 case SLANG_OPER_LESS:
4818 return _slang_gen_compare(A, oper, IR_SLT);
4819 case SLANG_OPER_GREATEREQUAL:
4820 return _slang_gen_compare(A, oper, IR_SGE);
4821 case SLANG_OPER_LESSEQUAL:
4822 return _slang_gen_compare(A, oper, IR_SLE);
4823 case SLANG_OPER_ADD:
4826 assert(oper->num_children == 2);
4827 n = _slang_gen_function_call_name(A, "+", oper, NULL);
4830 case SLANG_OPER_SUBTRACT:
4833 assert(oper->num_children == 2);
4834 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4837 case SLANG_OPER_MULTIPLY:
4840 assert(oper->num_children == 2);
4841 n = _slang_gen_function_call_name(A, "*", oper, NULL);
4844 case SLANG_OPER_DIVIDE:
4847 assert(oper->num_children == 2);
4848 n = _slang_gen_function_call_name(A, "/", oper, NULL);
4851 case SLANG_OPER_MINUS:
4854 assert(oper->num_children == 1);
4855 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4858 case SLANG_OPER_PLUS:
4859 /* +expr --> do nothing */
4860 return _slang_gen_operation(A, &oper->children[0]);
4861 case SLANG_OPER_VARIABLE_DECL:
4862 return _slang_gen_declaration(A, oper);
4863 case SLANG_OPER_ASSIGN:
4864 return _slang_gen_assignment(A, oper);
4865 case SLANG_OPER_ADDASSIGN:
4868 assert(oper->num_children == 2);
4869 n = _slang_gen_function_call_name(A, "+=", oper, NULL);
4872 case SLANG_OPER_SUBASSIGN:
4875 assert(oper->num_children == 2);
4876 n = _slang_gen_function_call_name(A, "-=", oper, NULL);
4880 case SLANG_OPER_MULASSIGN:
4883 assert(oper->num_children == 2);
4884 n = _slang_gen_function_call_name(A, "*=", oper, NULL);
4887 case SLANG_OPER_DIVASSIGN:
4890 assert(oper->num_children == 2);
4891 n = _slang_gen_function_call_name(A, "/=", oper, NULL);
4894 case SLANG_OPER_LOGICALAND:
4897 assert(oper->num_children == 2);
4898 n = _slang_gen_logical_and(A, oper);
4901 case SLANG_OPER_LOGICALOR:
4904 assert(oper->num_children == 2);
4905 n = _slang_gen_logical_or(A, oper);
4908 case SLANG_OPER_LOGICALXOR:
4909 return _slang_gen_xor(A, oper);
4910 case SLANG_OPER_NOT:
4911 return _slang_gen_not(A, oper);
4912 case SLANG_OPER_SELECT: /* b ? x : y */
4915 assert(oper->num_children == 3);
4916 n = _slang_gen_select(A, oper);
4920 case SLANG_OPER_ASM:
4921 return _slang_gen_asm(A, oper, NULL);
4922 case SLANG_OPER_CALL:
4923 return _slang_gen_function_call_name(A, (const char *) oper->a_id,
4925 case SLANG_OPER_METHOD:
4926 return _slang_gen_method_call(A, oper);
4927 case SLANG_OPER_RETURN:
4928 return _slang_gen_return(A, oper);
4929 case SLANG_OPER_RETURN_INLINED:
4930 return _slang_gen_return(A, oper);
4931 case SLANG_OPER_LABEL:
4932 return new_label(oper->label);
4933 case SLANG_OPER_IDENTIFIER:
4934 return _slang_gen_variable(A, oper);
4936 return _slang_gen_if(A, oper);
4937 case SLANG_OPER_FIELD:
4938 return _slang_gen_struct_field(A, oper);
4939 case SLANG_OPER_SUBSCRIPT:
4940 return _slang_gen_array_element(A, oper);
4941 case SLANG_OPER_LITERAL_FLOAT:
4943 case SLANG_OPER_LITERAL_INT:
4945 case SLANG_OPER_LITERAL_BOOL:
4946 return new_float_literal(oper->literal, oper->literal_size);
4948 case SLANG_OPER_POSTINCREMENT: /* var++ */
4951 assert(oper->num_children == 1);
4952 n = _slang_gen_function_call_name(A, "__postIncr", oper, NULL);
4955 case SLANG_OPER_POSTDECREMENT: /* var-- */
4958 assert(oper->num_children == 1);
4959 n = _slang_gen_function_call_name(A, "__postDecr", oper, NULL);
4962 case SLANG_OPER_PREINCREMENT: /* ++var */
4965 assert(oper->num_children == 1);
4966 n = _slang_gen_function_call_name(A, "++", oper, NULL);
4969 case SLANG_OPER_PREDECREMENT: /* --var */
4972 assert(oper->num_children == 1);
4973 n = _slang_gen_function_call_name(A, "--", oper, NULL);
4977 case SLANG_OPER_NON_INLINED_CALL:
4978 case SLANG_OPER_SEQUENCE:
4980 slang_ir_node *tree = NULL;
4982 for (i = 0; i < oper->num_children; i++) {
4983 slang_ir_node *n = _slang_gen_operation(A, &oper->children[i]);
4984 tree = new_seq(tree, n);
4986 tree->Store = n->Store;
4988 if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
4989 tree = new_function_call(tree, oper->label);
4994 case SLANG_OPER_NONE:
4995 case SLANG_OPER_VOID:
4996 /* returning NULL here would generate an error */
4997 return new_node0(IR_NOP);
5000 _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
5002 return new_node0(IR_NOP);
5010 * Check if the given type specifier is a rectangular texture sampler.
5013 is_rect_sampler_spec(const slang_type_specifier *spec)
5015 while (spec->_array) {
5016 spec = spec->_array;
5018 return spec->type == SLANG_SPEC_SAMPLER2DRECT ||
5019 spec->type == SLANG_SPEC_SAMPLER2DRECTSHADOW;
5025 * Called by compiler when a global variable has been parsed/compiled.
5026 * Here we examine the variable's type to determine what kind of register
5027 * storage will be used.
5029 * A uniform such as "gl_Position" will become the register specification
5030 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
5031 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
5033 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
5034 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
5035 * actual texture unit (as specified by the user calling glUniform1i()).
5038 _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
5039 slang_unit_type type)
5041 struct gl_program *prog = A->program;
5042 const char *varName = (char *) var->a_name;
5043 GLboolean success = GL_TRUE;
5044 slang_ir_storage *store = NULL;
5046 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
5047 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
5048 const GLint arrayLen = _slang_array_length(var);
5049 const GLint totalSize = _slang_array_size(size, arrayLen);
5050 GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
5052 var->is_global = GL_TRUE;
5054 /* check for sampler2D arrays */
5055 if (texIndex == -1 && var->type.specifier._array)
5056 texIndex = sampler_to_texture_index(var->type.specifier._array->type);
5058 if (texIndex != -1) {
5059 /* This is a texture sampler variable...
5060 * store->File = PROGRAM_SAMPLER
5061 * store->Index = sampler number (0..7, typically)
5062 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
5064 if (var->initializer) {
5065 slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
5068 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5069 /* disallow rect samplers */
5070 if (is_rect_sampler_spec(&var->type.specifier)) {
5071 slang_info_log_error(A->log, "invalid sampler type for '%s'", varName);
5075 (void) is_rect_sampler_spec; /* silence warning */
5078 GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
5079 store = _slang_new_ir_storage_sampler(sampNum, texIndex, totalSize);
5081 /* If we have a sampler array, then we need to allocate the
5082 * additional samplers to ensure we don't allocate them elsewhere.
5083 * We can't directly use _mesa_add_sampler() as that checks the
5084 * varName and gets a match, so we call _mesa_add_parameter()
5085 * directly and use the last sampler number from the call above.
5088 GLint a = arrayLen - 1;
5090 for (i = 0; i < a; i++) {
5091 GLfloat value = (GLfloat)(i + sampNum + 1);
5092 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_SAMPLER,
5093 varName, 1, datatype, &value, NULL, 0x0);
5097 if (dbg) printf("SAMPLER ");
5099 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
5100 /* Uniform variable */
5101 const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
5104 /* user-defined uniform */
5105 if (datatype == GL_NONE) {
5106 if ((var->type.specifier.type == SLANG_SPEC_ARRAY &&
5107 var->type.specifier._array->type == SLANG_SPEC_STRUCT) ||
5108 (var->type.specifier.type == SLANG_SPEC_STRUCT)) {
5109 /* temporary work-around */
5110 GLenum datatype = GL_FLOAT;
5111 GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
5112 totalSize, datatype, NULL);
5113 store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
5114 totalSize, swizzle);
5117 GLint a = arrayLen - 1;
5119 for (i = 0; i < a; i++) {
5120 GLfloat value = (GLfloat)(i + uniformLoc + 1);
5121 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM,
5122 varName, 1, datatype, &value, NULL, 0x0);
5126 /* XXX what we need to do is unroll the struct into its
5127 * basic types, creating a uniform variable for each.
5135 * Should produce uniforms:
5136 * "f.a" (GL_FLOAT_VEC3)
5137 * "f.b" (GL_FLOAT_VEC4)
5140 if (var->initializer) {
5141 slang_info_log_error(A->log,
5142 "unsupported initializer for uniform '%s'", varName);
5147 slang_info_log_error(A->log,
5148 "invalid datatype for uniform variable %s",
5154 /* non-struct uniform */
5155 if (!_slang_gen_var_decl(A, var, var->initializer))
5161 /* pre-defined uniform, like gl_ModelviewMatrix */
5162 /* We know it's a uniform, but don't allocate storage unless
5165 store = _slang_new_ir_storage_swz(PROGRAM_STATE_VAR, -1,
5166 totalSize, swizzle);
5168 if (dbg) printf("UNIFORM (sz %d) ", totalSize);
5170 else if (var->type.qualifier == SLANG_QUAL_VARYING) {
5171 /* varyings must be float, vec or mat */
5172 if (!_slang_type_is_float_vec_mat(var->type.specifier.type) &&
5173 var->type.specifier.type != SLANG_SPEC_ARRAY) {
5174 slang_info_log_error(A->log,
5175 "varying '%s' must be float/vector/matrix",
5180 if (var->initializer) {
5181 slang_info_log_error(A->log, "illegal initializer for varying '%s'",
5187 /* user-defined varying */
5193 if (var->type.centroid == SLANG_CENTROID)
5194 flags |= PROG_PARAM_BIT_CENTROID;
5195 if (var->type.variant == SLANG_INVARIANT)
5196 flags |= PROG_PARAM_BIT_INVARIANT;
5198 varyingLoc = _mesa_add_varying(prog->Varying, varName,
5200 swizzle = _slang_var_swizzle(size, 0);
5201 store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
5202 totalSize, swizzle);
5205 /* pre-defined varying, like gl_Color or gl_TexCoord */
5206 if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
5207 /* fragment program input */
5209 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5212 assert(index < FRAG_ATTRIB_MAX);
5213 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index,
5217 /* vertex program output */
5218 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5219 GLuint swizzle = _slang_var_swizzle(size, 0);
5221 assert(index < VERT_RESULT_MAX);
5222 assert(type == SLANG_UNIT_VERTEX_BUILTIN);
5223 store = _slang_new_ir_storage_swz(PROGRAM_OUTPUT, index,
5226 if (dbg) printf("V/F ");
5228 if (dbg) printf("VARYING ");
5230 else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
5233 /* attributes must be float, vec or mat */
5234 if (!_slang_type_is_float_vec_mat(var->type.specifier.type)) {
5235 slang_info_log_error(A->log,
5236 "attribute '%s' must be float/vector/matrix",
5242 /* user-defined vertex attribute */
5243 const GLint attr = -1; /* unknown */
5244 swizzle = _slang_var_swizzle(size, 0);
5245 index = _mesa_add_attribute(prog->Attributes, varName,
5246 size, datatype, attr);
5248 index = VERT_ATTRIB_GENERIC0 + index;
5251 /* pre-defined vertex attrib */
5252 index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
5255 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5256 if (dbg) printf("ATTRIB ");
5258 else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
5259 GLuint swizzle = SWIZZLE_XYZW; /* silence compiler warning */
5260 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5262 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5263 if (dbg) printf("INPUT ");
5265 else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
5266 if (type == SLANG_UNIT_VERTEX_BUILTIN) {
5267 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5268 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
5271 GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
5272 GLint specialSize = 4; /* treat all fragment outputs as float[4] */
5273 assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
5274 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, specialSize);
5276 if (dbg) printf("OUTPUT ");
5278 else if (var->type.qualifier == SLANG_QUAL_CONST && !prog) {
5279 /* pre-defined global constant, like gl_MaxLights */
5280 store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
5281 if (dbg) printf("CONST ");
5284 /* ordinary variable (may be const) */
5287 /* IR node to declare the variable */
5288 n = _slang_gen_var_decl(A, var, var->initializer);
5290 /* emit GPU instructions */
5291 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_FALSE, A->log);
5293 _slang_free_ir_tree(n);
5296 if (dbg) printf("GLOBAL VAR %s idx %d\n", (char*) var->a_name,
5297 store ? store->Index : -2);
5300 var->store = store; /* save var's storage info */
5302 var->declared = GL_TRUE;
5309 * Produce an IR tree from a function AST (fun->body).
5310 * Then call the code emitter to convert the IR tree into gl_program
5314 _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
5317 GLboolean success = GL_TRUE;
5319 if (_mesa_strcmp((char *) fun->header.a_name, "main") != 0) {
5320 /* we only really generate code for main, all other functions get
5321 * inlined or codegen'd upon an actual call.
5324 /* do some basic error checking though */
5325 if (fun->header.type.specifier.type != SLANG_SPEC_VOID) {
5326 /* check that non-void functions actually return something */
5328 = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
5330 slang_info_log_error(A->log,
5331 "function \"%s\" has no return statement",
5332 (char *) fun->header.a_name);
5334 "function \"%s\" has no return statement\n",
5335 (char *) fun->header.a_name);
5340 return GL_TRUE; /* not an error */
5344 printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
5345 slang_print_function(fun, 1);
5348 /* should have been allocated earlier: */
5349 assert(A->program->Parameters );
5350 assert(A->program->Varying);
5351 assert(A->vartable);
5354 A->UseReturnFlag = GL_FALSE;
5355 A->CurFunction = fun;
5357 /* fold constant expressions, etc. */
5358 _slang_simplify(fun->body, &A->space, A->atoms);
5361 printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
5362 slang_print_function(fun, 1);
5365 /* Create an end-of-function label */
5366 A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
5368 /* push new vartable scope */
5369 _slang_push_var_table(A->vartable);
5371 /* Generate IR tree for the function body code */
5372 n = _slang_gen_operation(A, fun->body);
5374 n = new_node1(IR_SCOPE, n);
5376 /* pop vartable, restore previous */
5377 _slang_pop_var_table(A->vartable);
5380 /* XXX record error */
5384 /* append an end-of-function-label to IR tree */
5385 n = new_seq(n, new_label(A->curFuncEndLabel));
5387 /*_slang_label_delete(A->curFuncEndLabel);*/
5388 A->curFuncEndLabel = NULL;
5391 printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
5392 slang_print_function(fun, 1);
5395 printf("************* IR for %s *******\n", (char*)fun->header.a_name);
5396 _slang_print_ir_tree(n, 0);
5399 printf("************* End codegen function ************\n\n");
5402 if (A->UnresolvedRefs) {
5403 /* Can't codegen at this time.
5404 * At link time we'll concatenate all the vertex shaders and/or all
5405 * the fragment shaders and try recompiling.
5410 /* Emit program instructions */
5411 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_TRUE, A->log);
5412 _slang_free_ir_tree(n);
5414 /* free codegen context */
5416 _mesa_free(A->codegen);