ARB prog parser: Delete the old parser
[profile/ivi/mesa.git] / src / mesa / shader / slang / slang_codegen.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
5  * Copyright (C) 2008 VMware, Inc.  All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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.
23  */
24
25 /**
26  * \file slang_codegen.c
27  * Generate IR tree from AST.
28  * \author Brian Paul
29  */
30
31
32 /***
33  *** NOTES:
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.
36  ***/
37
38
39
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"
56 #include "slang_ir.h"
57 #include "slang_print.h"
58
59
60 /** Max iterations to unroll */
61 const GLuint MAX_FOR_LOOP_UNROLL_ITERATIONS = 32;
62
63 /** Max for-loop body size (in slang operations) to unroll */
64 const GLuint MAX_FOR_LOOP_UNROLL_BODY_SIZE = 50;
65
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.
70  */
71 const GLuint MAX_FOR_LOOP_UNROLL_COMPLEXITY = 256;
72
73
74
75 static slang_ir_node *
76 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper);
77
78 static void
79 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
80                  GLuint substCount, slang_variable **substOld,
81                  slang_operation **substNew, GLboolean isLHS);
82
83
84 /**
85  * Retrieves type information about an operation.
86  * Returns GL_TRUE on success.
87  * Returns GL_FALSE otherwise.
88  */
89 static GLboolean
90 typeof_operation(const struct slang_assemble_ctx_ *A,
91                  slang_operation *op,
92                  slang_typeinfo *ti)
93 {
94    return _slang_typeof_operation(op, &A->space, ti, A->atoms, A->log);
95 }
96
97
98 static GLboolean
99 is_sampler_type(const slang_fully_specified_type *t)
100 {
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:
110       return GL_TRUE;
111    default:
112       return GL_FALSE;
113    }
114 }
115
116
117 /**
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.
121  */
122 static GLint
123 _slang_field_offset(const slang_type_specifier *spec, slang_atom field)
124 {
125    GLint offset = 0;
126    GLuint i;
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);
130       if (sz > 1) {
131          /* types larger than 1 float are register (4-float) aligned */
132          offset = (offset + 3) & ~3;
133       }
134       if (field && v->a_name == field) {
135          return offset;
136       }
137       offset += sz;
138    }
139    if (field)
140       return -1; /* field not found */
141    else
142       return offset;  /* struct size */
143 }
144
145
146 /**
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.
152  */
153 GLuint
154 _slang_sizeof_type_specifier(const slang_type_specifier *spec)
155 {
156    GLuint sz;
157    switch (spec->type) {
158    case SLANG_SPEC_VOID:
159       sz = 0;
160       break;
161    case SLANG_SPEC_BOOL:
162       sz = 1;
163       break;
164    case SLANG_SPEC_BVEC2:
165       sz = 2;
166       break;
167    case SLANG_SPEC_BVEC3:
168       sz = 3;
169       break;
170    case SLANG_SPEC_BVEC4:
171       sz = 4;
172       break;
173    case SLANG_SPEC_INT:
174       sz = 1;
175       break;
176    case SLANG_SPEC_IVEC2:
177       sz = 2;
178       break;
179    case SLANG_SPEC_IVEC3:
180       sz = 3;
181       break;
182    case SLANG_SPEC_IVEC4:
183       sz = 4;
184       break;
185    case SLANG_SPEC_FLOAT:
186       sz = 1;
187       break;
188    case SLANG_SPEC_VEC2:
189       sz = 2;
190       break;
191    case SLANG_SPEC_VEC3:
192       sz = 3;
193       break;
194    case SLANG_SPEC_VEC4:
195       sz = 4;
196       break;
197    case SLANG_SPEC_MAT2:
198       sz = 2 * 4; /* 2 columns (regs) */
199       break;
200    case SLANG_SPEC_MAT3:
201       sz = 3 * 4;
202       break;
203    case SLANG_SPEC_MAT4:
204       sz = 4 * 4;
205       break;
206    case SLANG_SPEC_MAT23:
207       sz = 2 * 4; /* 2 columns (regs) */
208       break;
209    case SLANG_SPEC_MAT32:
210       sz = 3 * 4; /* 3 columns (regs) */
211       break;
212    case SLANG_SPEC_MAT24:
213       sz = 2 * 4;
214       break;
215    case SLANG_SPEC_MAT42:
216       sz = 4 * 4; /* 4 columns (regs) */
217       break;
218    case SLANG_SPEC_MAT34:
219       sz = 3 * 4;
220       break;
221    case SLANG_SPEC_MAT43:
222       sz = 4 * 4; /* 4 columns (regs) */
223       break;
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 */
233       break;
234    case SLANG_SPEC_STRUCT:
235       sz = _slang_field_offset(spec, 0); /* special use */
236       if (sz == 1) {
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
240           */
241          sz = 2;
242       }
243       else if (sz > 4) {
244          sz = (sz + 3) & ~0x3; /* round up to multiple of four */
245       }
246       break;
247    case SLANG_SPEC_ARRAY:
248       sz = _slang_sizeof_type_specifier(spec->_array);
249       break;
250    default:
251       _mesa_problem(NULL, "Unexpected type in _slang_sizeof_type_specifier()");
252       sz = 0;
253    }
254
255    if (sz > 4) {
256       /* if size is > 4, it should be a multiple of four */
257       assert((sz & 0x3) == 0);
258    }
259    return sz;
260 }
261
262
263 /**
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
268  */
269 static GLint
270 _slang_array_length(const slang_variable *var)
271 {
272    if (var->type.array_len > 0) {
273       /* Ex: float[4] x; */
274       return var->type.array_len;
275    }
276    if (var->array_len > 0) {
277       /* Ex: float x[4]; */
278       return var->array_len;
279    }
280    return 0;
281 }
282
283
284 /**
285  * Compute total size of array give size of element, number of elements.
286  * \return size in floats
287  */
288 static GLint
289 _slang_array_size(GLint elemSize, GLint arrayLen)
290 {
291    GLint total;
292    assert(elemSize > 0);
293    if (arrayLen > 1) {
294       /* round up base type to multiple of 4 */
295       total = ((elemSize + 3) & ~0x3) * MAX2(arrayLen, 1);
296    }
297    else {
298       total = elemSize;
299    }
300    return total;
301 }
302
303
304 /**
305  * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
306  * or -1 if the type is not a sampler.
307  */
308 static GLint
309 sampler_to_texture_index(const slang_type_specifier_type type)
310 {
311    switch (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 */
328    default:
329       return -1;
330    }
331 }
332
333
334 /** helper to build a SLANG_OPER_IDENTIFIER node */
335 static void
336 slang_operation_identifier(slang_operation *oper,
337                            slang_assemble_ctx *A,
338                            const char *name)
339 {
340    oper->type = SLANG_OPER_IDENTIFIER;
341    oper->a_id = slang_atom_pool_atom(A->atoms, name);
342 }
343
344
345 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
346
347 /**
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
350  * name is invalid.
351  * XXX return size too
352  */
353 static GLint
354 _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
355 {
356    struct input_info {
357       const char *Name;
358       GLuint Attrib;
359       GLuint Swizzle;
360    };
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 }
376    };
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 }
387    };
388    GLuint i;
389    const struct input_info *inputs
390       = (target == GL_VERTEX_PROGRAM_ARB) ? vertInputs : fragInputs;
391
392    ASSERT(MAX_TEXTURE_COORD_UNITS == 8); /* if this fails, fix vertInputs above */
393
394    for (i = 0; inputs[i].Name; i++) {
395       if (strcmp(inputs[i].Name, name) == 0) {
396          /* found */
397          *swizzleOut = inputs[i].Swizzle;
398          return inputs[i].Attrib;
399       }
400    }
401    return -1;
402 }
403
404
405 /**
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
408  * output name.
409  */
410 static GLint
411 _slang_output_index(const char *name, GLenum target)
412 {
413    struct output_info {
414       const char *Name;
415       GLuint Attrib;
416    };
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 },
426       { NULL, 0 }
427    };
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 },
432       { NULL, 0 }
433    };
434    GLuint i;
435    const struct output_info *outputs
436       = (target == GL_VERTEX_PROGRAM_ARB) ? vertOutputs : fragOutputs;
437
438    for (i = 0; outputs[i].Name; i++) {
439       if (strcmp(outputs[i].Name, name) == 0) {
440          /* found */
441          return outputs[i].Attrib;
442       }
443    }
444    return -1;
445 }
446
447
448 /**
449  * Called when we begin code/IR generation for a new while/do/for loop.
450  */
451 static void
452 push_loop(slang_assemble_ctx *A, slang_operation *loopOper, slang_ir_node *loopIR)
453 {
454    A->LoopOperStack[A->LoopDepth] = loopOper;
455    A->LoopIRStack[A->LoopDepth] = loopIR;
456    A->LoopDepth++;
457 }
458
459
460 /**
461  * Called when we end code/IR generation for a new while/do/for loop.
462  */
463 static void
464 pop_loop(slang_assemble_ctx *A)
465 {
466    assert(A->LoopDepth > 0);
467    A->LoopDepth--;
468 }
469
470
471 /**
472  * Return pointer to slang_operation for the loop we're currently inside,
473  * or NULL if not in a loop.
474  */
475 static const slang_operation *
476 current_loop_oper(const slang_assemble_ctx *A)
477 {
478    if (A->LoopDepth > 0)
479       return A->LoopOperStack[A->LoopDepth - 1];
480    else
481       return NULL;
482 }
483
484
485 /**
486  * Return pointer to slang_ir_node for the loop we're currently inside,
487  * or NULL if not in a loop.
488  */
489 static slang_ir_node *
490 current_loop_ir(const slang_assemble_ctx *A)
491 {
492    if (A->LoopDepth > 0)
493       return A->LoopIRStack[A->LoopDepth - 1];
494    else
495       return NULL;
496 }
497
498
499 /**********************************************************************/
500
501
502 /**
503  * Map "_asm foo" to IR_FOO, etc.
504  */
505 typedef struct
506 {
507    const char *Name;
508    slang_ir_opcode Opcode;
509    GLuint HaveRetValue, NumParams;
510 } slang_asm_info;
511
512
513 static slang_asm_info AsmInfo[] = {
514    /* vec4 binary op */
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 },
534    /* vec4 unary */
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 */
557
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 },
567
568    /* unary op */
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},
582
583    { NULL, IR_NOP, 0, 0 }
584 };
585
586
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)
590 {
591    slang_ir_node *n = (slang_ir_node *) _slang_alloc(sizeof(slang_ir_node));
592    if (n) {
593       n->Opcode = op;
594       n->Children[0] = c0;
595       n->Children[1] = c1;
596       n->Children[2] = c2;
597       n->InstLocation = -1;
598    }
599    return n;
600 }
601
602 static slang_ir_node *
603 new_node2(slang_ir_opcode op, slang_ir_node *c0, slang_ir_node *c1)
604 {
605    return new_node3(op, c0, c1, NULL);
606 }
607
608 static slang_ir_node *
609 new_node1(slang_ir_opcode op, slang_ir_node *c0)
610 {
611    return new_node3(op, c0, NULL, NULL);
612 }
613
614 static slang_ir_node *
615 new_node0(slang_ir_opcode op)
616 {
617    return new_node3(op, NULL, NULL, NULL);
618 }
619
620
621 /**
622  * Create sequence of two nodes.
623  */
624 static slang_ir_node *
625 new_seq(slang_ir_node *left, slang_ir_node *right)
626 {
627    if (!left)
628       return right;
629    if (!right)
630       return left;
631    return new_node2(IR_SEQ, left, right);
632 }
633
634 static slang_ir_node *
635 new_label(slang_label *label)
636 {
637    slang_ir_node *n = new_node0(IR_LABEL);
638    assert(label);
639    if (n)
640       n->Label = label;
641    return n;
642 }
643
644 static slang_ir_node *
645 new_float_literal(const float v[4], GLuint size)
646 {
647    slang_ir_node *n = new_node0(IR_FLOAT);
648    assert(size <= 4);
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);
652    return n;
653 }
654
655
656 static slang_ir_node *
657 new_not(slang_ir_node *n)
658 {
659    return new_node1(IR_NOT, n);
660 }
661
662
663 /**
664  * Non-inlined function call.
665  */
666 static slang_ir_node *
667 new_function_call(slang_ir_node *code, slang_label *name)
668 {
669    slang_ir_node *n = new_node1(IR_CALL, code);
670    assert(name);
671    if (n)
672       n->Label = name;
673    return n;
674 }
675
676
677 /**
678  * Unconditional jump.
679  */
680 static slang_ir_node *
681 new_return(slang_label *dest)
682 {
683    slang_ir_node *n = new_node0(IR_RETURN);
684    assert(dest);
685    if (n)
686       n->Label = dest;
687    return n;
688 }
689
690
691 static slang_ir_node *
692 new_loop(slang_ir_node *body)
693 {
694    return new_node1(IR_LOOP, body);
695 }
696
697
698 static slang_ir_node *
699 new_break(slang_ir_node *loopNode)
700 {
701    slang_ir_node *n = new_node0(IR_BREAK);
702    assert(loopNode);
703    assert(loopNode->Opcode == IR_LOOP);
704    if (n) {
705       /* insert this node at head of linked list of cont/break instructions */
706       n->List = loopNode->List;
707       loopNode->List = n;
708    }
709    return n;
710 }
711
712
713 /**
714  * Make new IR_BREAK_IF_TRUE.
715  */
716 static slang_ir_node *
717 new_break_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
718 {
719    slang_ir_node *loopNode = current_loop_ir(A);
720    slang_ir_node *n;
721    assert(loopNode);
722    assert(loopNode->Opcode == IR_LOOP);
723    n = new_node1(IR_BREAK_IF_TRUE, cond);
724    if (n) {
725       /* insert this node at head of linked list of cont/break instructions */
726       n->List = loopNode->List;
727       loopNode->List = n;
728    }
729    return n;
730 }
731
732
733 /**
734  * Make new IR_CONT_IF_TRUE node.
735  */
736 static slang_ir_node *
737 new_cont_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
738 {
739    slang_ir_node *loopNode = current_loop_ir(A);
740    slang_ir_node *n;
741    assert(loopNode);
742    assert(loopNode->Opcode == IR_LOOP);
743    n = new_node1(IR_CONT_IF_TRUE, cond);
744    if (n) {
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;
748       loopNode->List = n;
749    }
750    return n;
751 }
752
753
754 static slang_ir_node *
755 new_cond(slang_ir_node *n)
756 {
757    slang_ir_node *c = new_node1(IR_COND, n);
758    return c;
759 }
760
761
762 static slang_ir_node *
763 new_if(slang_ir_node *cond, slang_ir_node *ifPart, slang_ir_node *elsePart)
764 {
765    return new_node3(IR_IF, cond, ifPart, elsePart);
766 }
767
768
769 /**
770  * New IR_VAR node - a reference to a previously declared variable.
771  */
772 static slang_ir_node *
773 new_var(slang_assemble_ctx *A, slang_variable *var)
774 {
775    slang_ir_node *n = new_node0(IR_VAR);
776    if (n) {
777       ASSERT(var);
778       ASSERT(var->store);
779       ASSERT(!n->Store);
780       ASSERT(!n->Var);
781
782       /* Set IR node's Var and Store pointers */
783       n->Var = var;
784       n->Store = var->store;
785    }
786    return n;
787 }
788
789
790 /**
791  * Check if the given function is really just a wrapper for a
792  * basic assembly instruction.
793  */
794 static GLboolean
795 slang_is_asm_function(const slang_function *fun)
796 {
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) {
800       return GL_TRUE;
801    }
802    return GL_FALSE;
803 }
804
805
806 static GLboolean
807 _slang_is_noop(const slang_operation *oper)
808 {
809    if (!oper ||
810        oper->type == SLANG_OPER_VOID ||
811        (oper->num_children == 1 && oper->children[0].type == SLANG_OPER_VOID))
812       return GL_TRUE;
813    else
814       return GL_FALSE;
815 }
816
817
818 /**
819  * Recursively search tree for a node of the given type.
820  */
821 #if 0
822 static slang_operation *
823 _slang_find_node_type(slang_operation *oper, slang_operation_type type)
824 {
825    GLuint i;
826    if (oper->type == type)
827       return oper;
828    for (i = 0; i < oper->num_children; i++) {
829       slang_operation *p = _slang_find_node_type(&oper->children[i], type);
830       if (p)
831          return p;
832    }
833    return NULL;
834 }
835 #endif
836
837
838 /**
839  * Count the number of operations of the given time rooted at 'oper'.
840  */
841 static GLuint
842 _slang_count_node_type(const slang_operation *oper, slang_operation_type type)
843 {
844    GLuint i, count = 0;
845    if (oper->type == type) {
846       return 1;
847    }
848    for (i = 0; i < oper->num_children; i++) {
849       count += _slang_count_node_type(&oper->children[i], type);
850    }
851    return count;
852 }
853
854
855 /**
856  * Check if the 'return' statement found under 'oper' is a "tail return"
857  * that can be no-op'd.  For example:
858  *
859  * void func(void)
860  * {
861  *    .. do something ..
862  *    return;   // this is a no-op
863  * }
864  *
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
870  * always, at least).
871  */
872 static GLboolean
873 _slang_is_tail_return(const slang_operation *oper)
874 {
875    GLuint k = oper->num_children;
876
877    while (k > 0) {
878       const slang_operation *last = &oper->children[k - 1];
879       if (last->type == SLANG_OPER_RETURN)
880          return GL_TRUE;
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);
888       else
889          break;
890    }
891
892    return GL_FALSE;
893 }
894
895
896 /**
897  * Generate a variable declaration opeartion.
898  * I.e.: generate AST code for "bool flag = false;"
899  */
900 static void
901 slang_generate_declaration(slang_assemble_ctx *A,
902                            slang_variable_scope *scope,
903                            slang_operation *decl,
904                            slang_type_specifier_type type,
905                            const char *name,
906                            GLint initValue)
907 {
908    slang_variable *var;
909
910    assert(type == SLANG_SPEC_BOOL ||
911           type == SLANG_SPEC_INT);
912
913    decl->type = SLANG_OPER_VARIABLE_DECL;
914
915    var = slang_variable_scope_grow(scope);
916
917    slang_fully_specified_type_construct(&var->type);
918
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);
924 }
925
926
927 static void
928 slang_resolve_variable(slang_operation *oper)
929 {
930    if (oper->type == SLANG_OPER_IDENTIFIER && !oper->var) {
931       oper->var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
932    }
933 }
934
935
936 /**
937  * Rewrite AST code for "return expression;".
938  *
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;"
943  *
944  * \return the new AST code.
945  */
946 static slang_operation *
947 gen_return_with_expression(slang_assemble_ctx *A, slang_operation *oper)
948 {
949    slang_operation *blockOper, *assignOper;
950
951    assert(oper->type == SLANG_OPER_RETURN);
952
953    if (A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
954       slang_info_log_error(A->log, "illegal return expression");
955       return NULL;
956    }
957
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);
962
963    if (A->UseReturnFlag) {
964       /* Emit:
965        *    {
966        *       if (__notRetFlag)
967        *          __retVal = expr;
968        *       __notRetFlag = 0;
969        *    }
970        */
971       {
972          slang_operation *ifOper = slang_oper_child(blockOper, 0);
973          ifOper->type = SLANG_OPER_IF;
974          slang_operation_add_children(ifOper, 3);
975          {
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");
979          }
980          {
981             slang_operation *elseOper = slang_oper_child(ifOper, 2);
982             elseOper->type = SLANG_OPER_VOID;
983          }
984          assignOper = slang_oper_child(ifOper, 1);
985       }
986       {
987          slang_operation *setOper = slang_oper_child(blockOper, 1);
988          setOper->type = SLANG_OPER_ASSIGN;
989          slang_operation_add_children(setOper, 2);
990          {
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");
994          }
995          {
996             slang_operation *rhs = slang_oper_child(setOper, 1);
997             slang_operation_literal_bool(rhs, GL_FALSE);
998          }
999       }
1000    }
1001    else {
1002       /* Emit:
1003        *    {
1004        *       __retVal = expr;
1005        *       return_inlined;
1006        *    }
1007        */
1008       assignOper = slang_oper_child(blockOper, 0);
1009       {
1010          slang_operation *returnOper = slang_oper_child(blockOper, 1);
1011          returnOper->type = SLANG_OPER_RETURN_INLINED;
1012          assert(returnOper->num_children == 0);
1013       }
1014    }
1015
1016    /* __retVal = expression; */
1017    assignOper->type = SLANG_OPER_ASSIGN;
1018    slang_operation_add_children(assignOper, 2);
1019    {
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");
1023    }
1024    {
1025       slang_operation *rhs = slang_oper_child(assignOper, 1);
1026       slang_operation_copy(rhs, &oper->children[0]);
1027    }
1028
1029    ///blockOper->locals->outer_scope = oper->locals->outer_scope;
1030
1031    /*slang_print_tree(blockOper, 0);*/
1032
1033    return blockOper;
1034 }
1035
1036
1037 /**
1038  * Rewrite AST code for "return;" (no expression).
1039  */
1040 static slang_operation *
1041 gen_return_without_expression(slang_assemble_ctx *A, slang_operation *oper)
1042 {
1043    slang_operation *newRet;
1044
1045    assert(oper->type == SLANG_OPER_RETURN);
1046
1047    if (A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
1048       slang_info_log_error(A->log, "return statement requires an expression");
1049       return NULL;
1050    }
1051
1052    if (A->UseReturnFlag) {
1053       /* Emit:
1054        *    __notRetFlag = 0;
1055        */
1056       {
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);
1061          {
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");
1065          }
1066          {
1067             slang_operation *rhs = slang_oper_child(newRet, 1);
1068             slang_operation_literal_bool(rhs, GL_FALSE);
1069          }
1070       }
1071    }
1072    else {
1073       /* Emit:
1074        *    return_inlined;
1075        */
1076       newRet = slang_operation_new(1);
1077       newRet->locals->outer_scope = oper->locals->outer_scope;
1078       newRet->type = SLANG_OPER_RETURN_INLINED;
1079    }
1080
1081    /*slang_print_tree(newRet, 0);*/
1082
1083    return newRet;
1084 }
1085
1086
1087
1088
1089 /**
1090  * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
1091  */
1092 static void
1093 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
1094                  GLuint substCount, slang_variable **substOld,
1095                  slang_operation **substNew, GLboolean isLHS)
1096 {
1097    switch (oper->type) {
1098    case SLANG_OPER_VARIABLE_DECL:
1099       {
1100          slang_variable *v = _slang_variable_locate(oper->locals,
1101                                                     oper->a_id, GL_TRUE);
1102          assert(v);
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);
1108          }
1109          if (oper->num_children == 1) {
1110             /* the initializer */
1111             slang_substitute(A, &oper->children[0], substCount,
1112                              substOld, substNew, GL_FALSE);
1113          }
1114       }
1115       break;
1116    case SLANG_OPER_IDENTIFIER:
1117       assert(oper->num_children == 0);
1118       if (1/**!isLHS XXX FIX */) {
1119          slang_atom id = oper->a_id;
1120          slang_variable *v;
1121          GLuint i;
1122          v = _slang_variable_locate(oper->locals, id, GL_TRUE);
1123          if (!v) {
1124             if (_mesa_strcmp((char *) oper->a_id, "__notRetFlag"))
1125                _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
1126             return;
1127          }
1128
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,
1139                          (void*) oper);
1140                }
1141                else {
1142                   printf("Substitute %s with %f in id node %p\n",
1143                          (char*)v->a_name, substNew[i]->literal[0],
1144                          (void*) oper);
1145                }
1146 #endif
1147                slang_operation_copy(oper, substNew[i]);
1148                break;
1149             }
1150          }
1151       }
1152       break;
1153
1154    case SLANG_OPER_RETURN:
1155       {
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);
1160          else
1161             newReturn = gen_return_with_expression(A, oper);
1162
1163          if (!newReturn)
1164             return;
1165
1166          /* do substitutions on the new 'return' code */
1167          slang_substitute(A, newReturn,
1168                           substCount, substOld, substNew, GL_FALSE);
1169
1170          /* install new 'return' code */
1171          slang_operation_copy(oper, newReturn);
1172          slang_operation_destruct(newReturn);
1173       }
1174       break;
1175
1176    case SLANG_OPER_ASSIGN:
1177    case SLANG_OPER_SUBSCRIPT:
1178       /* special case:
1179        * child[0] can't have substitutions but child[1] can.
1180        */
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);
1185       break;
1186    case SLANG_OPER_FIELD:
1187       /* XXX NEW - test */
1188       slang_substitute(A, &oper->children[0],
1189                        substCount, substOld, substNew, GL_TRUE);
1190       break;
1191    default:
1192       {
1193          GLuint i;
1194          for (i = 0; i < oper->num_children; i++) 
1195             slang_substitute(A, &oper->children[i],
1196                              substCount, substOld, substNew, GL_FALSE);
1197       }
1198    }
1199 }
1200
1201
1202 /**
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:
1205  *
1206  * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1207  * {
1208  *    __asm vec4_lrp __retVal, a, y, x;
1209  * }
1210  *
1211  *
1212  * A call to
1213  *     r = mix(p1, p2, p3);
1214  *
1215  * Becomes:
1216  *
1217  *              mov
1218  *             /   \
1219  *            r   vec4_lrp
1220  *                 /  |  \
1221  *                p3  p2  p1
1222  *
1223  * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1224  */
1225 static slang_operation *
1226 slang_inline_asm_function(slang_assemble_ctx *A,
1227                           slang_function *fun, slang_operation *oper)
1228 {
1229    const GLuint numArgs = oper->num_children;
1230    GLuint i;
1231    slang_operation *inlined;
1232    const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1233    slang_variable **substOld;
1234    slang_operation **substNew;
1235
1236    ASSERT(slang_is_asm_function(fun));
1237    ASSERT(fun->param_count == numArgs + haveRetValue);
1238
1239    /*
1240    printf("Inline %s as %s\n",
1241           (char*) fun->header.a_name,
1242           (char*) fun->body->children[0].a_id);
1243    */
1244
1245    /*
1246     * We'll substitute formal params with actual args in the asm call.
1247     */
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;
1255    }
1256
1257    /* make a copy of the code to inline */
1258    inlined = slang_operation_new(1);
1259    slang_operation_copy(inlined, &fun->body->children[0]);
1260    if (haveRetValue) {
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];
1265       }
1266    }
1267
1268    /* now do formal->actual substitutions */
1269    slang_substitute(A, inlined, numArgs, substOld, substNew, GL_FALSE);
1270
1271    _slang_free(substOld);
1272    _slang_free(substNew);
1273
1274 #if 0
1275    printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1276           (char *) fun->header.a_name);
1277    slang_print_tree(inlined, 3);
1278    printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1279 #endif
1280
1281    return inlined;
1282 }
1283
1284
1285 /**
1286  * Inline the given function call operation.
1287  * Return a new slang_operation that corresponds to the inlined code.
1288  */
1289 static slang_operation *
1290 slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
1291                            slang_operation *oper, slang_operation *returnOper)
1292 {
1293    typedef enum {
1294       SUBST = 1,
1295       COPY_IN,
1296       COPY_OUT
1297    } ParamMode;
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;
1309
1310    /* save / push */
1311    prevFunction = A->CurFunction;
1312    A->CurFunction = fun;
1313
1314    /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1315    assert(fun->param_count == totalArgs);
1316
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 *));
1324
1325 #if 0
1326    printf("\nInline call to %s  (total vars=%d  nparams=%d)\n",
1327           (char *) fun->header.a_name,
1328           fun->parameters->num_variables, numArgs);
1329 #endif
1330
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
1336        */
1337       slang_operation *commaSeq;
1338       slang_operation *declOper = NULL;
1339       slang_variable *resultVar;
1340
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);
1349       /*
1350       printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1351              (void*)commaSeq->locals, (char *) fun->header.a_name);
1352       */
1353
1354       resultVar->a_name = slang_atom_pool_atom(A->atoms, "__resultTmp");
1355       resultVar->type = fun->header.type; /* XXX copy? */
1356       resultVar->isTemp = GL_TRUE;
1357
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;
1363
1364       /* child[1] = function body */
1365       inlined = &commaSeq->children[1];
1366       inlined->locals->outer_scope = commaSeq->locals;
1367
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;
1373
1374       top = commaSeq;
1375    }
1376    else {
1377       top = inlined = slang_operation_new(1);
1378       /* XXXX this may be inappropriate!!!! */
1379       inlined->locals->outer_scope = oper->locals->outer_scope;
1380    }
1381
1382
1383    assert(inlined->locals);
1384
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
1390     *     out            copy out
1391     */
1392    substCount = 0;
1393    for (i = 0; i < totalArgs; i++) {
1394       slang_variable *p = fun->parameters->variables[i];
1395       /*
1396       printf("Param %d: %s %s \n", i,
1397              slang_type_qual_string(p->type.qualifier),
1398              (char *) p->a_name);
1399       */
1400       if (p->type.qualifier == SLANG_QUAL_INOUT ||
1401           p->type.qualifier == SLANG_QUAL_OUT) {
1402          /* an output param */
1403          slang_operation *arg;
1404          if (i < numArgs)
1405             arg = &args[i];
1406          else
1407             arg = returnOper;
1408          paramMode[i] = SUBST;
1409
1410          if (arg->type == SLANG_OPER_IDENTIFIER)
1411             slang_resolve_variable(arg);
1412
1413          /* replace parameter 'p' with argument 'arg' */
1414          substOld[substCount] = p;
1415          substNew[substCount] = arg; /* will get copied */
1416          substCount++;
1417       }
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.
1425              */
1426             paramMode[i] = SUBST;
1427             slang_resolve_variable(&args[i]);
1428             substOld[substCount] = p;
1429             substNew[substCount] = &args[i]; /* will get copied */
1430             substCount++;
1431          }
1432          else {
1433             paramMode[i] = COPY_IN;
1434          }
1435       }
1436       else {
1437          paramMode[i] = COPY_IN;
1438       }
1439       assert(paramMode[i]);
1440    }
1441
1442    /* actual code inlining: */
1443    slang_operation_copy(inlined, fun->body);
1444
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;
1449
1450 #if 0
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);
1456 #endif
1457
1458    /* do parameter substitution in inlined code: */
1459    slang_substitute(A, inlined, substCount, substOld, substNew, GL_FALSE);
1460
1461 #if 0
1462    printf("======================= subst code ==========================\n");
1463    slang_print_tree(inlined, 8);
1464    printf("=============================================================\n");
1465 #endif
1466
1467    /* New prolog statements: (inserted before the inlined code)
1468     * Copy the 'in' arguments.
1469     */
1470    numCopyIn = 0;
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,
1476                                                         &inlined->children,
1477                                                         numCopyIn);
1478
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);
1485
1486          /* child[0] is the var's initializer */
1487          slang_operation_copy(&decl->children[0], args + i);
1488
1489          /* add parameter 'p' to the local variable scope here */
1490          {
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;
1495          }
1496
1497          newScope = inlined->locals;
1498          numCopyIn++;
1499       }
1500    }
1501
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;
1509    }
1510
1511
1512    /* New epilog statements:
1513     * 1. Create end of function label to jump to from return statements.
1514     * 2. Copy the 'out' parameter vars
1515     */
1516    {
1517       slang_operation *lab = slang_operation_insert(&inlined->num_children,
1518                                                     &inlined->children,
1519                                                     inlined->num_children);
1520       lab->type = SLANG_OPER_LABEL;
1521       lab->label = A->curFuncEndLabel;
1522    }
1523
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,
1530                                                        &inlined->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;
1540       }
1541    }
1542
1543    _slang_free(paramMode);
1544    _slang_free(substOld);
1545    _slang_free(substNew);
1546
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.
1550     */
1551    if (newScope)
1552       slang_replace_scope(inlined, fun->parameters, newScope);
1553
1554 #if 0
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);
1559 #endif
1560
1561    /* pop */
1562    A->CurFunction = prevFunction;
1563
1564    return top;
1565 }
1566
1567
1568 /**
1569  * Insert declaration for "bool __notRetFlag" in given block operation.
1570  * This is used when we can't emit "early" return statements in subroutines.
1571  */
1572 static void
1573 declare_return_flag(slang_assemble_ctx *A, slang_operation *oper)
1574 {
1575    slang_operation *decl;
1576
1577    assert(oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1578           oper->type == SLANG_OPER_SEQUENCE);
1579
1580    decl = slang_operation_insert_child(oper, 1);
1581
1582    slang_generate_declaration(A, oper->locals, decl,
1583                               SLANG_SPEC_BOOL, "__notRetFlag", GL_TRUE);
1584
1585    /*slang_print_tree(oper, 0);*/
1586 }
1587
1588
1589 /**
1590  * Recursively replace instances of the old node type with the new type.
1591  */
1592 static void
1593 replace_node_type(slang_operation *oper, slang_operation_type oldType,
1594                   slang_operation_type newType)
1595 {
1596    GLuint i;
1597
1598    if (oper->type == oldType)
1599       oper->type = newType;
1600
1601    for (i = 0; i < slang_oper_num_children(oper); i++) {
1602       replace_node_type(slang_oper_child(oper, i), oldType, newType);
1603    }
1604 }
1605
1606
1607
1608 /**
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.
1611  */
1612 static GLboolean
1613 has_early_return(const slang_operation *funcBody)
1614 {
1615    GLuint retCount = _slang_count_node_type(funcBody, SLANG_OPER_RETURN);
1616    if (retCount == 0)
1617       return GL_FALSE;
1618    else if (retCount == 1 && _slang_is_tail_return(funcBody))
1619       return GL_FALSE;
1620    else
1621       return GL_TRUE;
1622 }
1623
1624
1625 /**
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.
1629  */
1630 static slang_ir_node *
1631 _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
1632                          slang_operation *oper, slang_operation *dest)
1633 {
1634    slang_ir_node *n;
1635    slang_operation *instance;
1636    slang_label *prevFuncEndLabel;
1637    char name[200];
1638
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);
1643
1644    /*
1645     * 'instance' is basically a copy of the function's body with various
1646     * transformations.
1647     */
1648
1649    if (slang_is_asm_function(fun) && !dest) {
1650       /* assemble assembly function - tree style */
1651       instance = slang_inline_asm_function(A, fun, oper);
1652    }
1653    else {
1654       /* non-assembly function */
1655       /* We always generate an "inline-able" block of code here.
1656        * We may either:
1657        *  1. insert the inline code
1658        *  2. Generate a call to the "inline" code as a subroutine
1659        */
1660       const GLboolean earlyReturn = has_early_return(fun->body);
1661
1662       if (earlyReturn && !A->EmitContReturn) {
1663          A->UseReturnFlag = GL_TRUE;
1664       }
1665
1666       instance = slang_inline_function_call(A, fun, oper, dest);
1667       if (!instance)
1668          return NULL;
1669
1670       if (earlyReturn) {
1671          /* The function we're calling has one or more 'return' statements
1672           * that prevent us from inlining the function's code.
1673           *
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.
1677           *
1678           * Also, convert SLANG_OPER_RETURN_INLINED nodes to SLANG_OPER_RETURN.
1679           */
1680          slang_operation *callOper;
1681
1682          assert(instance->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1683                 instance->type == SLANG_OPER_SEQUENCE);
1684
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];
1689          }
1690          else {
1691             callOper = instance;
1692          }
1693
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.
1698              */
1699             assert(callOper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1700                    callOper->type == SLANG_OPER_SEQUENCE);
1701             declare_return_flag(A, callOper);
1702          }
1703          else {
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.
1707              */
1708             replace_node_type(instance, SLANG_OPER_RETURN_INLINED,
1709                               SLANG_OPER_RETURN);
1710          }
1711
1712          callOper->type = SLANG_OPER_NON_INLINED_CALL;
1713          callOper->fun = fun;
1714          callOper->label = _slang_label_new_unique((char*) fun->header.a_name);
1715       }
1716       else {
1717          /* If there are any 'return' statements remaining, they're at the
1718           * very end of the function and can effectively become no-ops.
1719           */
1720          replace_node_type(instance, SLANG_OPER_RETURN_INLINED,
1721                            SLANG_OPER_VOID);
1722       }
1723    }
1724
1725    if (!instance)
1726       return NULL;
1727
1728    /* Replace the function call with the instance block (or new CALL stmt) */
1729    slang_operation_destruct(oper);
1730    *oper = *instance;
1731    _slang_free(instance);
1732
1733 #if 0
1734    assert(instance->locals);
1735    printf("*** Inlined code for call to %s:\n", (char*) fun->header.a_name);
1736    slang_print_tree(oper, 10);
1737    printf("\n");
1738 #endif
1739
1740    n = _slang_gen_operation(A, oper);
1741
1742    /*_slang_label_delete(A->curFuncEndLabel);*/
1743    A->curFuncEndLabel = prevFuncEndLabel;
1744
1745    if (A->pragmas->Debug) {
1746       char s[1000];
1747       _mesa_snprintf(s, sizeof(s), "Call/inline %s()", (char *) fun->header.a_name);
1748       n->Comment = _slang_strdup(s);
1749    }
1750
1751    A->UseReturnFlag = GL_FALSE;
1752
1753    return n;
1754 }
1755
1756
1757 static slang_asm_info *
1758 slang_find_asm_info(const char *name)
1759 {
1760    GLuint i;
1761    for (i = 0; AsmInfo[i].Name; i++) {
1762       if (_mesa_strcmp(AsmInfo[i].Name, name) == 0) {
1763          return AsmInfo + i;
1764       }
1765    }
1766    return NULL;
1767 }
1768
1769
1770 /**
1771  * Some write-masked assignments are simple, but others are hard.
1772  * Simple example:
1773  *    vec3 v;
1774  *    v.xy = vec2(a, b);
1775  * Hard example:
1776  *    vec3 v;
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.
1781  */
1782 static GLboolean
1783 _slang_simple_writemask(GLuint writemask, GLuint swizzle)
1784 {
1785    switch (writemask) {
1786    case WRITEMASK_X:
1787       return GET_SWZ(swizzle, 0) == SWIZZLE_X;
1788    case WRITEMASK_Y:
1789       return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
1790    case WRITEMASK_Z:
1791       return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
1792    case WRITEMASK_W:
1793       return GET_SWZ(swizzle, 3) == SWIZZLE_W;
1794    case WRITEMASK_XY:
1795       return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1796          && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
1797    case WRITEMASK_XYZ:
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;
1803    default:
1804       return GL_FALSE;
1805    }
1806 }
1807
1808
1809 /**
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
1819  */
1820 static GLboolean
1821 swizzle_to_writemask(slang_assemble_ctx *A, GLuint swizzle,
1822                      GLuint *writemaskOut, GLuint *swizzleOut)
1823 {
1824    GLuint mask = 0x0, newSwizzle[4];
1825    GLint i, size;
1826
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) {
1831          /* end */
1832          break;
1833       }
1834       assert(swz >= 0 && swz <= 3);
1835
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));
1844          return GL_FALSE;
1845       }
1846
1847       mask |= (1 << swz);
1848    }
1849    assert(mask <= 0xf);
1850    size = i;  /* number of components in mask/swizzle */
1851
1852    *writemaskOut = mask;
1853
1854    /* make new src swizzle, by inversion */
1855    for (i = 0; i < 4; i++) {
1856       newSwizzle[i] = i; /*identity*/
1857    }
1858    for (i = 0; i < size; i++) {
1859       const GLuint swz = GET_SWZ(swizzle, i);
1860       newSwizzle[swz] = i;
1861    }
1862    *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
1863                                newSwizzle[1],
1864                                newSwizzle[2],
1865                                newSwizzle[3]);
1866
1867    if (_slang_simple_writemask(mask, *swizzleOut)) {
1868       if (size >= 1)
1869          assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
1870       if (size >= 2)
1871          assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
1872       if (size >= 3)
1873          assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
1874       if (size >= 4)
1875          assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
1876       return GL_TRUE;
1877    }
1878    else
1879       return GL_FALSE;
1880 }
1881
1882
1883 #if 0 /* not used, but don't remove just yet */
1884 /**
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
1888  */
1889 static GLuint
1890 resolve_swizzle(const slang_operation *oper)
1891 {
1892    if (oper->type == SLANG_OPER_FIELD) {
1893       /* writemask from .xyzw suffix */
1894       slang_swizzle swz;
1895       if (_slang_is_swizzle((char*) oper->a_id, 4, &swz)) {
1896          GLuint swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
1897                                         swz.swizzle[1],
1898                                         swz.swizzle[2],
1899                                         swz.swizzle[3]);
1900          GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1901          GLuint s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1902          return s;
1903       }
1904       else
1905          return SWIZZLE_XYZW;
1906    }
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];
1912       GLuint swizzle;
1913       GLuint s;
1914       switch (i) {
1915       case 0:
1916          swizzle = SWIZZLE_XXXX;
1917          break;
1918       case 1:
1919          swizzle = SWIZZLE_YYYY;
1920          break;
1921       case 2:
1922          swizzle = SWIZZLE_ZZZZ;
1923          break;
1924       case 3:
1925          swizzle = SWIZZLE_WWWW;
1926          break;
1927       default:
1928          swizzle = SWIZZLE_XYZW;
1929       }
1930       s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1931       return s;
1932    }
1933    else {
1934       return SWIZZLE_XYZW;
1935    }
1936 }
1937 #endif
1938
1939
1940 #if 0
1941 /**
1942  * Recursively descend through swizzle nodes to find the node's storage info.
1943  */
1944 static slang_ir_storage *
1945 get_store(const slang_ir_node *n)
1946 {
1947    if (n->Opcode == IR_SWIZZLE) {
1948       return get_store(n->Children[0]);
1949    }
1950    return n->Store;
1951 }
1952 #endif
1953
1954
1955 /**
1956  * Generate IR tree for an asm instruction/operation such as:
1957  *    __asm vec4_dot __retVal.x, v1, v2;
1958  */
1959 static slang_ir_node *
1960 _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
1961                slang_operation *dest)
1962 {
1963    const slang_asm_info *info;
1964    slang_ir_node *kids[3], *n;
1965    GLuint j, firstOperand;
1966
1967    assert(oper->type == SLANG_OPER_ASM);
1968
1969    info = slang_find_asm_info((char *) oper->a_id);
1970    if (!info) {
1971       _mesa_problem(NULL, "undefined __asm function %s\n",
1972                     (char *) oper->a_id);
1973       assert(info);
1974    }
1975    assert(info->NumParams <= 3);
1976
1977    if (info->NumParams == oper->num_children) {
1978       /* Storage for result is not specified.
1979        * Children[0], [1], [2] are the operands.
1980        */
1981       firstOperand = 0;
1982    }
1983    else {
1984       /* Storage for result (child[0]) is specified.
1985        * Children[1], [2], [3] are the operands.
1986        */
1987       firstOperand = 1;
1988    }
1989
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]);
1994       if (!kids[j])
1995          return NULL;
1996    }
1997
1998    n = new_node3(info->Opcode, kids[0], kids[1], kids[2]);
1999
2000    if (firstOperand) {
2001       /* Setup n->Store to be a particular location.  Otherwise, storage
2002        * for the result (a temporary) will be allocated later.
2003        */
2004       slang_operation *dest_oper;
2005       slang_ir_node *n0;
2006
2007       dest_oper = &oper->children[0];
2008
2009       n0 = _slang_gen_operation(A, dest_oper);
2010       if (!n0)
2011          return NULL;
2012
2013       assert(!n->Store);
2014       n->Store = n0->Store;
2015
2016       assert(n->Store->File != PROGRAM_UNDEFINED || n->Store->Parent);
2017
2018       _slang_free(n0);
2019    }
2020
2021    return n;
2022 }
2023
2024
2025 #if 0
2026 static void
2027 print_funcs(struct slang_function_scope_ *scope, const char *name)
2028 {
2029    GLuint i;
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);
2034
2035    }
2036    if (scope->outer_scope)
2037       print_funcs(scope->outer_scope, name);
2038 }
2039 #endif
2040
2041
2042 /**
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.
2046  *
2047  * XXX we should really create a list of candidate functions and try
2048  * all of them...
2049  */
2050 static slang_function *
2051 _slang_find_function_by_argc(slang_function_scope *scope,
2052                              const char *name, int numArgs)
2053 {
2054    while (scope) {
2055       GLuint i;
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)
2061                return f;
2062          }
2063       }
2064       scope = scope->outer_scope;
2065    }
2066
2067    return NULL;
2068 }
2069
2070
2071 static slang_function *
2072 _slang_find_function_by_max_argc(slang_function_scope *scope,
2073                                  const char *name)
2074 {
2075    slang_function *maxFunc = NULL;
2076    GLuint maxArgs = 0;
2077
2078    while (scope) {
2079       GLuint i;
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;
2085                maxFunc = f;
2086             }
2087          }
2088       }
2089       scope = scope->outer_scope;
2090    }
2091
2092    return maxFunc;
2093 }
2094
2095
2096 /**
2097  * Generate a new slang_function which is a constructor for a user-defined
2098  * struct type.
2099  */
2100 static slang_function *
2101 _slang_make_struct_constructor(slang_assemble_ctx *A, slang_struct *str)
2102 {
2103    const GLint numFields = str->fields->num_variables;
2104    slang_function *fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
2105
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;
2111
2112    /* function parameters (= struct's fields) */
2113    {
2114       GLint i;
2115       for (i = 0; i < numFields; i++) {
2116          /*
2117          printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2118          */
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;
2122       }
2123       fun->param_count = fun->parameters->num_variables;
2124    }
2125
2126    /* Add __retVal to params */
2127    {
2128       slang_variable *p = slang_variable_scope_grow(fun->parameters);
2129       slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2130       assert(a_retVal);
2131       p->a_name = a_retVal;
2132       p->type = fun->header.type;
2133       p->type.qualifier = SLANG_QUAL_OUT;
2134       fun->param_count++;
2135    }
2136
2137    /* function body is:
2138     *    block:
2139     *       declare T;
2140     *       T.f1 = p1;
2141     *       T.f2 = p2;
2142     *       ...
2143     *       T.fn = pn;
2144     *       return T;
2145     */
2146    {
2147       slang_variable_scope *scope;
2148       slang_variable *var;
2149       GLint i;
2150
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);
2155
2156       scope = fun->body->locals;
2157       scope->outer_scope = fun->parameters;
2158
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;
2163
2164       /* declare t */
2165       {
2166          slang_operation *decl;
2167
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;
2172       }
2173
2174       /* assign params to fields of t */
2175       for (i = 0; i < numFields; i++) {
2176          slang_operation *assign = &fun->body->children[1 + i];
2177
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);
2182          
2183          {
2184             slang_operation *lhs = &assign->children[0];
2185
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;
2191
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);
2195
2196 #if 0
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);
2202 #endif
2203          }
2204
2205          {
2206             slang_operation *rhs = &assign->children[1];
2207
2208             rhs->type = SLANG_OPER_IDENTIFIER;
2209             rhs->locals = _slang_variable_scope_new(scope);
2210             rhs->a_id = str->fields->variables[i]->a_name;
2211          }         
2212       }
2213
2214       /* return t; */
2215       {
2216          slang_operation *ret = &fun->body->children[numFields + 1];
2217
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);
2225       }
2226    }
2227    /*
2228    slang_print_function(fun, 1);
2229    */
2230    return fun;
2231 }
2232
2233
2234 /**
2235  * Find/create a function (constructor) for the given structure name.
2236  */
2237 static slang_function *
2238 _slang_locate_struct_constructor(slang_assemble_ctx *A, const char *name)
2239 {
2240    unsigned int i;
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);
2248          }
2249          return str->constructor;
2250       }
2251    }
2252    return NULL;
2253 }
2254
2255
2256 /**
2257  * Generate a new slang_function to satisfy a call to an array constructor.
2258  * Ex:  float[3](1., 2., 3.)
2259  */
2260 static slang_function *
2261 _slang_make_array_constructor(slang_assemble_ctx *A, slang_operation *oper)
2262 {
2263    slang_type_specifier_type baseType;
2264    slang_function *fun;
2265    int num_elements;
2266
2267    fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
2268    if (!fun)
2269       return NULL;
2270
2271    baseType = slang_type_specifier_type_from_string((char *) oper->a_id);
2272
2273    num_elements = oper->num_children;
2274
2275    /* function header, return type */
2276    {
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;
2283    }
2284
2285    /* function parameters (= number of elements) */
2286    {
2287       GLint i;
2288       for (i = 0; i < num_elements; i++) {
2289          /*
2290          printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2291          */
2292          slang_variable *p = slang_variable_scope_grow(fun->parameters);
2293          char name[10];
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;
2298       }
2299       fun->param_count = fun->parameters->num_variables;
2300    }
2301
2302    /* Add __retVal to params */
2303    {
2304       slang_variable *p = slang_variable_scope_grow(fun->parameters);
2305       slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2306       assert(a_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;
2311       fun->param_count++;
2312    }
2313
2314    /* function body is:
2315     *    block:
2316     *       declare T;
2317     *       T[0] = p0;
2318     *       T[1] = p1;
2319     *       ...
2320     *       T[n] = pn;
2321     *       return T;
2322     */
2323    {
2324       slang_variable_scope *scope;
2325       slang_variable *var;
2326       GLint i;
2327
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);
2332
2333       scope = fun->body->locals;
2334       scope->outer_scope = fun->parameters;
2335
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*/
2340
2341       /* declare t */
2342       {
2343          slang_operation *decl;
2344
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;
2349       }
2350
2351       /* assign params to elements of t */
2352       for (i = 0; i < num_elements; i++) {
2353          slang_operation *assign = &fun->body->children[1 + i];
2354
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);
2359          
2360          {
2361             slang_operation *lhs = &assign->children[0];
2362
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);
2367  
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);
2371
2372             lhs->children[1].type = SLANG_OPER_LITERAL_INT;
2373             lhs->children[1].literal[0] = (GLfloat) i;
2374          }
2375
2376          {
2377             slang_operation *rhs = &assign->children[1];
2378
2379             rhs->type = SLANG_OPER_IDENTIFIER;
2380             rhs->locals = _slang_variable_scope_new(scope);
2381             rhs->a_id = fun->parameters->variables[i]->a_name;
2382          }         
2383       }
2384
2385       /* return t; */
2386       {
2387          slang_operation *ret = &fun->body->children[num_elements + 1];
2388
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);
2396       }
2397    }
2398
2399    /*
2400    slang_print_function(fun, 1);
2401    */
2402
2403    return fun;
2404 }
2405
2406
2407 static GLboolean
2408 _slang_is_vec_mat_type(const char *name)
2409 {
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",
2417       NULL
2418    };
2419    int i;
2420    for (i = 0; vecmat_types[i]; i++)
2421       if (_mesa_strcmp(name, vecmat_types[i]) == 0)
2422          return GL_TRUE;
2423    return GL_FALSE;
2424 }
2425
2426
2427 /**
2428  * Assemble a function call, given a particular function name.
2429  * \param name  the function's name (operators like '*' are possible).
2430  */
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)
2434 {
2435    slang_operation *params = oper->children;
2436    const GLuint param_count = oper->num_children;
2437    slang_atom atom;
2438    slang_function *fun;
2439    slang_ir_node *n;
2440
2441    atom = slang_atom_pool_atom(A->atoms, name);
2442    if (atom == SLANG_ATOM_NULL)
2443       return NULL;
2444
2445    if (oper->array_constructor) {
2446       /* this needs special handling */
2447       fun = _slang_make_array_constructor(A, oper);
2448    }
2449    else {
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);
2454       if (error) {
2455          slang_info_log_error(A->log,
2456                               "Function '%s' not found (check argument types)",
2457                               name);
2458          return NULL;
2459       }
2460    }
2461
2462    if (!fun) {
2463       /* Next, try locating a constructor function for a user-defined type */
2464       fun = _slang_locate_struct_constructor(A, name);
2465    }
2466
2467    /*
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"
2470     * of constructors.
2471     */
2472
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.
2476        */
2477       fun = _slang_find_function_by_max_argc(A->space.funcs, name);
2478       if (fun) {
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)",
2482                                  name);
2483             return NULL;
2484          }
2485       }
2486    }
2487
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)",
2495                               name);
2496          return NULL;
2497       }
2498       assert(fun);
2499    }
2500
2501    if (!fun) {
2502       slang_info_log_error(A->log,
2503                            "Function '%s' not found (check argument types)",
2504                            name);
2505       return NULL;
2506    }
2507
2508    if (!fun->body) {
2509       /* The function body may be in another compilation unit.
2510        * We'll try concatenating the shaders and recompile at link time.
2511        */
2512       A->UnresolvedRefs = GL_TRUE;
2513       return new_node1(IR_NOP, NULL);
2514    }
2515
2516    /* type checking to be sure function's return type matches 'dest' type */
2517    if (dest) {
2518       slang_typeinfo t0;
2519
2520       slang_typeinfo_construct(&t0);
2521       typeof_operation(A, dest, &t0);
2522
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'",
2526                               name);
2527          return NULL;
2528       }
2529    }
2530
2531    n = _slang_gen_function_call(A, fun, oper, dest);
2532
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);*/
2539    }
2540
2541    if (oper->array_constructor) {
2542       /* free the temporary array constructor function now */
2543       slang_function_destruct(fun);
2544    }
2545
2546    return n;
2547 }
2548
2549
2550 static slang_ir_node *
2551 _slang_gen_method_call(slang_assemble_ctx *A, slang_operation *oper)
2552 {
2553    slang_atom *a_length = slang_atom_pool_atom(A->atoms, "length");
2554    slang_ir_node *n;
2555    slang_variable *var;
2556
2557    /* NOTE: In GLSL 1.20, there's only one kind of method
2558     * call: array.length().  Anything else is an error.
2559     */
2560    if (oper->a_id != a_length) {
2561       slang_info_log_error(A->log,
2562                            "Undefined method call '%s'", (char *) oper->a_id);
2563       return NULL;
2564    }
2565
2566    /* length() takes no arguments */
2567    if (oper->num_children > 0) {
2568       slang_info_log_error(A->log, "Invalid arguments to length() method");
2569       return NULL;
2570    }
2571
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);
2577       return NULL;
2578    }
2579
2580    /* Create a float/literal IR node encoding the array length */
2581    n = new_node0(IR_FLOAT);
2582    if (n) {
2583       n->Value[0] = (float) _slang_array_length(var);
2584       n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, 1);
2585    }
2586    return n;
2587 }
2588
2589
2590 static GLboolean
2591 _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
2592 {
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])
2597          *value = GL_TRUE;
2598       else
2599          *value = GL_FALSE;
2600       return GL_TRUE;
2601    }
2602    else if (oper->type == SLANG_OPER_EXPRESSION &&
2603             oper->num_children == 1) {
2604       return _slang_is_constant_cond(&oper->children[0], value);
2605    }
2606    return GL_FALSE;
2607 }
2608
2609
2610 /**
2611  * Test if an operation is a scalar or boolean.
2612  */
2613 static GLboolean
2614 _slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
2615 {
2616    slang_typeinfo type;
2617    GLint size;
2618
2619    slang_typeinfo_construct(&type);
2620    typeof_operation(A, oper, &type);
2621    size = _slang_sizeof_type_specifier(&type.spec);
2622    slang_typeinfo_destruct(&type);
2623    return size == 1;
2624 }
2625
2626
2627 /**
2628  * Test if an operation is boolean.
2629  */
2630 static GLboolean
2631 _slang_is_boolean(slang_assemble_ctx *A, slang_operation *oper)
2632 {
2633    slang_typeinfo type;
2634    GLboolean isBool;
2635
2636    slang_typeinfo_construct(&type);
2637    typeof_operation(A, oper, &type);
2638    isBool = (type.spec.type == SLANG_SPEC_BOOL);
2639    slang_typeinfo_destruct(&type);
2640    return isBool;
2641 }
2642
2643
2644 /**
2645  * Check if a loop contains a 'continue' statement.
2646  * Stop looking if we find a nested loop.
2647  */
2648 static GLboolean
2649 _slang_loop_contains_continue(const slang_operation *oper)
2650 {
2651    switch (oper->type) {
2652    case SLANG_OPER_CONTINUE:
2653       return GL_TRUE;
2654    case SLANG_OPER_FOR:
2655    case SLANG_OPER_DO:
2656    case SLANG_OPER_WHILE:
2657       /* stop upon finding a nested loop */
2658       return GL_FALSE;
2659    default:
2660        /* recurse */
2661       {
2662          GLuint i;
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))
2666                return GL_TRUE;
2667          }
2668       }
2669       return GL_FALSE;
2670    }
2671 }
2672
2673
2674 /**
2675  * Check if a loop contains a 'continue' or 'break' statement.
2676  * Stop looking if we find a nested loop.
2677  */
2678 static GLboolean
2679 _slang_loop_contains_continue_or_break(const slang_operation *oper)
2680 {
2681    switch (oper->type) {
2682    case SLANG_OPER_CONTINUE:
2683    case SLANG_OPER_BREAK:
2684       return GL_TRUE;
2685    case SLANG_OPER_FOR:
2686    case SLANG_OPER_DO:
2687    case SLANG_OPER_WHILE:
2688       /* stop upon finding a nested loop */
2689       return GL_FALSE;
2690    default:
2691        /* recurse */
2692       {
2693          GLuint i;
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))
2697                return GL_TRUE;
2698          }
2699       }
2700       return GL_FALSE;
2701    }
2702 }
2703
2704
2705 /**
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().
2709  */
2710 static void
2711 replace_break_and_cont(slang_assemble_ctx *A, slang_operation *oper)
2712 {
2713    switch (oper->type) {
2714    case SLANG_OPER_BREAK:
2715       /* replace 'break' with "_notBreakFlag = false; break" */
2716       {
2717          slang_operation *block = oper;
2718          block->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2719          slang_operation_add_children(block, 2);
2720          {
2721             slang_operation *assign = slang_oper_child(block, 0);
2722             assign->type = SLANG_OPER_ASSIGN;
2723             slang_operation_add_children(assign, 2);
2724             {
2725                slang_operation *lhs = slang_oper_child(assign, 0);
2726                slang_operation_identifier(lhs, A, "_notBreakFlag");
2727             }
2728             {
2729                slang_operation *rhs = slang_oper_child(assign, 1);
2730                slang_operation_literal_bool(rhs, GL_FALSE);
2731             }
2732          }
2733          {
2734             slang_operation *brk = slang_oper_child(block, 1);
2735             brk->type = SLANG_OPER_BREAK;
2736             assert(!brk->children);
2737          }
2738       }
2739       break;
2740    case SLANG_OPER_CONTINUE:
2741       /* convert continue into a break */
2742       oper->type = SLANG_OPER_BREAK;
2743       break;
2744    case SLANG_OPER_FOR:
2745    case SLANG_OPER_DO:
2746    case SLANG_OPER_WHILE:
2747       /* stop upon finding a nested loop */
2748       break;
2749    default:
2750       /* recurse */
2751       {
2752          GLuint i;
2753          for (i = 0; i < oper->num_children; i++) {
2754             replace_break_and_cont(A, slang_oper_child(oper, i));
2755          }
2756       }
2757    }
2758 }
2759
2760
2761 /**
2762  * Transform a while-loop so that continue statements are converted to breaks.
2763  * Then do normal IR code generation.
2764  *
2765  * Before:
2766  * 
2767  * while (LOOPCOND) {
2768  *    A;
2769  *    if (IFCOND)
2770  *       continue;
2771  *    B;
2772  *    break;
2773  *    C;
2774  * }
2775  * 
2776  * After:
2777  * 
2778  * {
2779  *    bool _notBreakFlag = 1;
2780  *    while (_notBreakFlag && LOOPCOND) {
2781  *       do {
2782  *          A;
2783  *          if (IFCOND) {
2784  *             break;  // was continue
2785  *          }
2786  *          B;
2787  *          _notBreakFlag = 0; // was
2788  *          break;             // break
2789  *          C;
2790  *       } while (0)
2791  *    }
2792  * }
2793  */
2794 static slang_ir_node *
2795 _slang_gen_while_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2796 {
2797    slang_operation *top;
2798    slang_operation *innerBody;
2799
2800    assert(oper->type == SLANG_OPER_WHILE);
2801
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);
2806
2807    /* declare: bool _notBreakFlag = true */
2808    {
2809       slang_operation *condDecl = slang_oper_child(top, 0);
2810       slang_generate_declaration(A, top->locals, condDecl,
2811                                  SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2812    }
2813
2814    /* build outer while-loop:  while (_notBreakFlag && LOOPCOND) { ... } */
2815    {
2816       slang_operation *outerWhile = slang_oper_child(top, 1);
2817       outerWhile->type = SLANG_OPER_WHILE;
2818       slang_operation_add_children(outerWhile, 2);
2819
2820       /* _notBreakFlag && LOOPCOND */
2821       {
2822          slang_operation *cond = slang_oper_child(outerWhile, 0);
2823          cond->type = SLANG_OPER_LOGICALAND;
2824          slang_operation_add_children(cond, 2);
2825          {
2826             slang_operation *notBreak = slang_oper_child(cond, 0);
2827             slang_operation_identifier(notBreak, A, "_notBreakFlag");
2828          }
2829          {
2830             slang_operation *origCond = slang_oper_child(cond, 1);
2831             slang_operation_copy(origCond, slang_oper_child(oper, 0));
2832          }
2833       }
2834
2835       /* inner loop */
2836       {
2837          slang_operation *innerDo = slang_oper_child(outerWhile, 1);
2838          innerDo->type = SLANG_OPER_DO;
2839          slang_operation_add_children(innerDo, 2);
2840
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;
2845
2846          /* inner do-loop's condition is constant/false */
2847          {
2848             slang_operation *constFalse = slang_oper_child(innerDo, 1);
2849             slang_operation_literal_bool(constFalse, GL_FALSE);
2850          }
2851       }
2852    }
2853
2854    /* Finally, in innerBody,
2855     *   replace "break" with "_notBreakFlag = 0; break"
2856     *   replace "continue" with "break"
2857     */
2858    replace_break_and_cont(A, innerBody);
2859
2860    /*slang_print_tree(top, 0);*/
2861
2862    return _slang_gen_operation(A, top);
2863
2864    return NULL;
2865 }
2866
2867
2868 /**
2869  * Generate loop code using high-level IR_LOOP instruction
2870  */
2871 static slang_ir_node *
2872 _slang_gen_while(slang_assemble_ctx * A, slang_operation *oper)
2873 {
2874    /*
2875     * LOOP:
2876     *    BREAK if !expr (child[0])
2877     *    body code (child[1])
2878     */
2879    slang_ir_node *loop, *breakIf, *body;
2880    GLboolean isConst, constTrue;
2881
2882    if (!A->EmitContReturn) {
2883       /* We don't want to emit CONT instructions.  If this while-loop has
2884        * a continue, translate it away.
2885        */
2886       if (_slang_loop_contains_continue(slang_oper_child(oper, 1))) {
2887          return _slang_gen_while_without_continue(A, oper);
2888       }
2889    }
2890
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'");
2894       return NULL;
2895    }
2896
2897    /* Check if loop condition is a constant */
2898    isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
2899
2900    if (isConst && !constTrue) {
2901       /* loop is never executed! */
2902       return new_node0(IR_NOP);
2903    }
2904
2905    /* Begin new loop */
2906    loop = new_loop(NULL);
2907
2908    /* save loop state */
2909    push_loop(A, oper, loop);
2910
2911    if (isConst && constTrue) {
2912       /* while(nonzero constant), no conditional break */
2913       breakIf = NULL;
2914    }
2915    else {
2916       slang_ir_node *cond
2917          = new_cond(new_not(_slang_gen_operation(A, &oper->children[0])));
2918       breakIf = new_break_if_true(A, cond);
2919    }
2920    body = _slang_gen_operation(A, &oper->children[1]);
2921    loop->Children[0] = new_seq(breakIf, body);
2922
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 */
2927       pop_loop(A);
2928       slang_info_log_error(A->log, "Infinite loop detected!");
2929       return NULL;
2930    }
2931
2932    /* restore loop state */
2933    pop_loop(A);
2934
2935    return loop;
2936 }
2937
2938
2939 /**
2940  * Transform a do-while-loop so that continue statements are converted to breaks.
2941  * Then do normal IR code generation.
2942  *
2943  * Before:
2944  * 
2945  * do {
2946  *    A;
2947  *    if (IFCOND)
2948  *       continue;
2949  *    B;
2950  *    break;
2951  *    C;
2952  * } while (LOOPCOND);
2953  * 
2954  * After:
2955  * 
2956  * {
2957  *    bool _notBreakFlag = 1;
2958  *    do {
2959  *       do {
2960  *          A;
2961  *          if (IFCOND) {
2962  *             break;  // was continue
2963  *          }
2964  *          B;
2965  *          _notBreakFlag = 0; // was
2966  *          break;             // break
2967  *          C;
2968  *       } while (0)
2969  *    } while (_notBreakFlag && LOOPCOND);
2970  * }
2971  */
2972 static slang_ir_node *
2973 _slang_gen_do_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2974 {
2975    slang_operation *top;
2976    slang_operation *innerBody;
2977
2978    assert(oper->type == SLANG_OPER_DO);
2979
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);
2984
2985    /* declare: bool _notBreakFlag = true */
2986    {
2987       slang_operation *condDecl = slang_oper_child(top, 0);
2988       slang_generate_declaration(A, top->locals, condDecl,
2989                                  SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2990    }
2991
2992    /* build outer do-loop:  do { ... } while (_notBreakFlag && LOOPCOND) */
2993    {
2994       slang_operation *outerDo = slang_oper_child(top, 1);
2995       outerDo->type = SLANG_OPER_DO;
2996       slang_operation_add_children(outerDo, 2);
2997
2998       /* inner do-loop */
2999       {
3000          slang_operation *innerDo = slang_oper_child(outerDo, 0);
3001          innerDo->type = SLANG_OPER_DO;
3002          slang_operation_add_children(innerDo, 2);
3003
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;
3008
3009          /* inner do-loop's condition is constant/false */
3010          {
3011             slang_operation *constFalse = slang_oper_child(innerDo, 1);
3012             slang_operation_literal_bool(constFalse, GL_FALSE);
3013          }
3014       }
3015
3016       /* _notBreakFlag && LOOPCOND */
3017       {
3018          slang_operation *cond = slang_oper_child(outerDo, 1);
3019          cond->type = SLANG_OPER_LOGICALAND;
3020          slang_operation_add_children(cond, 2);
3021          {
3022             slang_operation *notBreak = slang_oper_child(cond, 0);
3023             slang_operation_identifier(notBreak, A, "_notBreakFlag");
3024          }
3025          {
3026             slang_operation *origCond = slang_oper_child(cond, 1);
3027             slang_operation_copy(origCond, slang_oper_child(oper, 1));
3028          }
3029       }
3030    }
3031
3032    /* Finally, in innerBody,
3033     *   replace "break" with "_notBreakFlag = 0; break"
3034     *   replace "continue" with "break"
3035     */
3036    replace_break_and_cont(A, innerBody);
3037
3038    /*slang_print_tree(top, 0);*/
3039
3040    return _slang_gen_operation(A, top);
3041 }
3042
3043
3044 /**
3045  * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
3046  */
3047 static slang_ir_node *
3048 _slang_gen_do(slang_assemble_ctx * A, slang_operation *oper)
3049 {
3050    /*
3051     * LOOP:
3052     *    body code (child[0])
3053     *    tail code:
3054     *       BREAK if !expr (child[1])
3055     */
3056    slang_ir_node *loop;
3057    GLboolean isConst, constTrue;
3058
3059    if (!A->EmitContReturn) {
3060       /* We don't want to emit CONT instructions.  If this do-loop has
3061        * a continue, translate it away.
3062        */
3063       if (_slang_loop_contains_continue(slang_oper_child(oper, 0))) {
3064          return _slang_gen_do_without_continue(A, oper);
3065       }
3066    }
3067
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'");
3071       return NULL;
3072    }
3073
3074    loop = new_loop(NULL);
3075
3076    /* save loop state */
3077    push_loop(A, oper, loop);
3078
3079    /* loop body: */
3080    loop->Children[0] = _slang_gen_operation(A, &oper->children[0]);
3081
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 */
3087    }
3088    else {
3089       slang_ir_node *cond
3090          = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
3091       loop->Children[1] = new_break_if_true(A, cond);
3092    }
3093
3094    /* XXX we should do infinite loop detection, as above */
3095
3096    /* restore loop state */
3097    pop_loop(A);
3098
3099    return loop;
3100 }
3101
3102
3103 /**
3104  * Recursively count the number of operations rooted at 'oper'.
3105  * This gives some kind of indication of the size/complexity of an operation.
3106  */
3107 static GLuint
3108 sizeof_operation(const slang_operation *oper)
3109 {
3110    if (oper) {
3111       GLuint count = 1; /* me */
3112       GLuint i;
3113       for (i = 0; i < oper->num_children; i++) {
3114          count += sizeof_operation(&oper->children[i]);
3115       }
3116       return count;
3117    }
3118    else {
3119       return 0;
3120    }
3121 }
3122
3123
3124 /**
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.
3130  */
3131 static GLboolean
3132 _slang_can_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
3133 {
3134    GLuint bodySize;
3135    GLint start, end;
3136    const char *varName;
3137    slang_atom varId;
3138
3139    if (oper->type != SLANG_OPER_FOR)
3140       return GL_FALSE;
3141
3142    assert(oper->num_children == 4);
3143
3144    if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper, 3)))
3145       return GL_FALSE;
3146
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;
3150
3151       if (oper->children[0].children[0].type != SLANG_OPER_VARIABLE_DECL)
3152          return GL_FALSE;
3153
3154       varId = oper->children[0].children[0].a_id;
3155
3156       var = _slang_variable_locate(oper->children[0].children[0].locals,
3157                                    varId, GL_TRUE);
3158       if (!var)
3159          return GL_FALSE;
3160       if (!var->initializer)
3161          return GL_FALSE;
3162       if (var->initializer->type != SLANG_OPER_LITERAL_INT)
3163          return GL_FALSE;
3164       start = (GLint) var->initializer->literal[0];
3165    }
3166    else if (oper->children[0].type == SLANG_OPER_EXPRESSION) {
3167       if (oper->children[0].children[0].type != SLANG_OPER_ASSIGN)
3168          return GL_FALSE;
3169       if (oper->children[0].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
3170          return GL_FALSE;
3171       if (oper->children[0].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
3172          return GL_FALSE;
3173
3174       varId = oper->children[0].children[0].children[0].a_id;
3175
3176       start = (GLint) oper->children[0].children[0].children[1].literal[0];
3177    }
3178    else {
3179       return GL_FALSE;
3180    }
3181
3182    /* children[1] must be "i<constant" */
3183    if (oper->children[1].type != SLANG_OPER_EXPRESSION)
3184       return GL_FALSE;
3185    if (oper->children[1].children[0].type != SLANG_OPER_LESS)
3186       return GL_FALSE;
3187    if (oper->children[1].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
3188       return GL_FALSE;
3189    if (oper->children[1].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
3190       return GL_FALSE;
3191
3192    end = (GLint) oper->children[1].children[0].children[1].literal[0];
3193
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)
3197       return GL_FALSE;
3198    if (oper->children[2].children[0].type != SLANG_OPER_IDENTIFIER)
3199       return GL_FALSE;
3200
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))
3204       return GL_FALSE;
3205
3206    varName = (const char *) varId;
3207
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"
3213                            " to unroll",
3214                            varName);
3215       return GL_FALSE;
3216    }
3217
3218    if (start >= end)
3219       return GL_FALSE; /* degenerate case */
3220
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);
3226       return GL_FALSE;
3227    }
3228
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);
3234       return GL_FALSE;
3235    }
3236
3237    return GL_TRUE; /* we can unroll the loop */
3238 }
3239
3240
3241 /**
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.
3249  */
3250 static slang_ir_node *
3251 _slang_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
3252 {
3253    GLint start, end, iter;
3254    slang_ir_node *n, *root = NULL;
3255    slang_atom varId;
3256
3257    if (oper->children[0].type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) {
3258       /* for (int i=0; ... */
3259       slang_variable *var;
3260
3261       varId = oper->children[0].children[0].a_id;
3262       var = _slang_variable_locate(oper->children[0].children[0].locals,
3263                                    varId, GL_TRUE);
3264       start = (GLint) var->initializer->literal[0];
3265    }
3266    else {
3267       /* for (i=0; ... */
3268       varId = oper->children[0].children[0].children[0].a_id;
3269       start = (GLint) oper->children[0].children[0].children[1].literal[0];
3270    }
3271
3272    end = (GLint) oper->children[1].children[0].children[1].literal[0];
3273
3274    for (iter = start; iter < end; iter++) {
3275       slang_operation *body;
3276
3277       /* make a copy of the loop body */
3278       body = slang_operation_new(1);
3279       if (!body)
3280          return NULL;
3281
3282       if (!slang_operation_copy(body, &oper->children[3]))
3283          return NULL;
3284
3285       /* in body, replace instances of 'varId' with literal 'iter' */
3286       {
3287          slang_variable *oldVar;
3288          slang_operation *newOper;
3289
3290          oldVar = _slang_variable_locate(oper->locals, varId, GL_TRUE);
3291          if (!oldVar) {
3292             /* undeclared loop variable */
3293             slang_operation_delete(body);
3294             return NULL;
3295          }
3296
3297          newOper = slang_operation_new(1);
3298          newOper->type = SLANG_OPER_LITERAL_INT;
3299          newOper->literal_size = 1;
3300          newOper->literal[0] = iter;
3301
3302          /* replace instances of the loop variable with newOper */
3303          slang_substitute(A, body, 1, &oldVar, &newOper, GL_FALSE);
3304       }
3305
3306       /* do IR codegen for body */
3307       n = _slang_gen_operation(A, body);
3308       if (!n)
3309          return NULL;
3310
3311       root = new_seq(root, n);
3312
3313       slang_operation_delete(body);
3314    }
3315
3316    return root;
3317 }
3318
3319
3320 /**
3321  * Replace 'continue' statement with 'break' inside a for-loop.
3322  * This is a recursive helper function used by _slang_gen_for_without_continue().
3323  */
3324 static void
3325 replace_continue_with_break(slang_assemble_ctx *A, slang_operation *oper)
3326 {
3327    switch (oper->type) {
3328    case SLANG_OPER_CONTINUE:
3329       oper->type = SLANG_OPER_BREAK;
3330       break;
3331    case SLANG_OPER_FOR:
3332    case SLANG_OPER_DO:
3333    case SLANG_OPER_WHILE:
3334       /* stop upon finding a nested loop */
3335       break;
3336    default:
3337       /* recurse */
3338       {
3339          GLuint i;
3340          for (i = 0; i < oper->num_children; i++) {
3341             replace_continue_with_break(A, slang_oper_child(oper, i));
3342          }
3343       }
3344    }
3345 }
3346
3347
3348 /**
3349  * Transform a for-loop so that continue statements are converted to breaks.
3350  * Then do normal IR code generation.
3351  *
3352  * Before:
3353  * 
3354  * for (INIT; LOOPCOND; INCR) {
3355  *    A;
3356  *    if (IFCOND) {
3357  *       continue;
3358  *    }
3359  *    B;
3360  * }
3361  * 
3362  * After:
3363  * 
3364  * {
3365  *    bool _condFlag = 1;
3366  *    for (INIT; _condFlag; ) {
3367  *       for ( ; _condFlag = LOOPCOND; INCR) {
3368  *          A;
3369  *          if (IFCOND) {
3370  *             break;
3371  *          }
3372  *          B;
3373  *       }
3374  *       if (_condFlag)
3375  *          INCR;
3376  *    }
3377  * }
3378  */
3379 static slang_ir_node *
3380 _slang_gen_for_without_continue(slang_assemble_ctx *A, slang_operation *oper)
3381 {
3382    slang_operation *top;
3383    slang_operation *outerFor, *innerFor, *init, *cond, *incr;
3384    slang_operation *lhs, *rhs;
3385
3386    assert(oper->type == SLANG_OPER_FOR);
3387
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);
3392
3393    /* declare: bool _condFlag = true */
3394    {
3395       slang_operation *condDecl = slang_oper_child(top, 0);
3396       slang_generate_declaration(A, top->locals, condDecl,
3397                                  SLANG_SPEC_BOOL, "_condFlag", GL_TRUE);
3398    }
3399
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);
3404
3405    init = slang_oper_child(outerFor, 0);
3406    slang_operation_copy(init, slang_oper_child(oper, 0));
3407
3408    cond = slang_oper_child(outerFor, 1);
3409    cond->type = SLANG_OPER_IDENTIFIER;
3410    cond->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3411
3412    incr = slang_oper_child(outerFor, 2);
3413    incr->type = SLANG_OPER_VOID;
3414
3415    /* body of the outer loop */
3416    {
3417       slang_operation *block = slang_oper_child(outerFor, 3);
3418
3419       slang_operation_add_children(block, 2);
3420       block->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
3421
3422       /* build inner loop:  for ( ; _condFlag = LOOPCOND; INCR) { */
3423       {
3424          innerFor = slang_oper_child(block, 0);
3425
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;
3430
3431          init = slang_oper_child(innerFor, 0);
3432          init->type = SLANG_OPER_VOID; /* leak? */
3433
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);
3439
3440          lhs = slang_oper_child(cond, 0);
3441          lhs->type = SLANG_OPER_IDENTIFIER;
3442          lhs->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3443
3444          rhs = slang_oper_child(cond, 1);
3445          slang_operation_copy(rhs, slang_oper_child(oper, 1));
3446       }
3447
3448       /* if (_condFlag) INCR; */
3449       {
3450          slang_operation *ifop = slang_oper_child(block, 1);
3451          ifop->type = SLANG_OPER_IF;
3452          slang_operation_add_children(ifop, 2);
3453
3454          /* re-use cond node build above */
3455          slang_operation_copy(slang_oper_child(ifop, 0), cond);
3456
3457          /* incr node from original for-loop operation */
3458          slang_operation_copy(slang_oper_child(ifop, 1),
3459                               slang_oper_child(oper, 2));
3460       }
3461
3462       /* finally, replace "continue" with "break" in the inner for-loop */
3463       replace_continue_with_break(A, slang_oper_child(innerFor, 3));
3464    }
3465
3466    return _slang_gen_operation(A, top);
3467 }
3468
3469
3470
3471 /**
3472  * Generate IR for a for-loop.  Unrolling will be done when possible.
3473  */
3474 static slang_ir_node *
3475 _slang_gen_for(slang_assemble_ctx * A, slang_operation *oper)
3476 {
3477    GLboolean unroll;
3478
3479    if (!A->EmitContReturn) {
3480       /* We don't want to emit CONT instructions.  If this for-loop has
3481        * a continue, translate it away.
3482        */
3483       if (_slang_loop_contains_continue(slang_oper_child(oper, 3))) {
3484          return _slang_gen_for_without_continue(A, oper);
3485       }
3486    }
3487
3488    unroll = _slang_can_unroll_for_loop(A, oper);
3489    if (unroll) {
3490       slang_ir_node *code = _slang_unroll_for_loop(A, oper);
3491       if (code)
3492          return code;
3493    }
3494
3495    assert(oper->type == SLANG_OPER_FOR);
3496
3497    /* conventional for-loop code generation */
3498    {
3499       /*
3500        * init code (child[0])
3501        * LOOP:
3502        *    BREAK if !expr (child[1])
3503        *    body code (child[3])
3504        *    tail code:
3505        *       incr code (child[2])   // XXX continue here
3506        */
3507       slang_ir_node *loop, *cond, *breakIf, *body, *init, *incr;
3508       init = _slang_gen_operation(A, &oper->children[0]);
3509       loop = new_loop(NULL);
3510
3511       /* save loop state */
3512       push_loop(A, oper, loop);
3513
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]);
3518
3519       loop->Children[0] = new_seq(breakIf, body);
3520       loop->Children[1] = incr;  /* tail code */
3521
3522       /* restore loop state */
3523       pop_loop(A);
3524
3525       return new_seq(init, loop);
3526    }
3527 }
3528
3529
3530 static slang_ir_node *
3531 _slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
3532 {
3533    slang_ir_node *n, *cont, *incr = NULL, *loopNode;
3534
3535    assert(oper->type == SLANG_OPER_CONTINUE);
3536    loopNode = current_loop_ir(A);
3537    assert(loopNode);
3538    assert(loopNode->Opcode == IR_LOOP);
3539
3540    cont = new_node0(IR_CONT);
3541    if (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;
3546    }
3547
3548    n = new_seq(incr, cont);
3549    return n;
3550 }
3551
3552
3553 /**
3554  * Determine if the given operation is of a specific type.
3555  */
3556 static GLboolean
3557 is_operation_type(const slang_operation *oper, slang_operation_type type)
3558 {
3559    if (oper->type == type)
3560       return GL_TRUE;
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);
3565    else
3566       return GL_FALSE;
3567 }
3568
3569
3570 /**
3571  * Generate IR tree for an if/then/else conditional using high-level
3572  * IR_IF instruction.
3573  */
3574 static slang_ir_node *
3575 _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
3576 {
3577    /*
3578     * eval expr (child[0])
3579     * IF expr THEN
3580     *    if-body code
3581     * ELSE
3582     *    else-body code
3583     * ENDIF
3584     */
3585    const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
3586    slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
3587    GLboolean isConst, constTrue;
3588
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'");
3592       return NULL;
3593    }
3594
3595    if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3596       slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
3597       return NULL;
3598    }
3599
3600    isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
3601    if (isConst) {
3602       if (constTrue) {
3603          /* if (true) ... */
3604          return _slang_gen_operation(A, &oper->children[1]);
3605       }
3606       else {
3607          /* if (false) ... */
3608          return _slang_gen_operation(A, &oper->children[2]);
3609       }
3610    }
3611
3612    cond = _slang_gen_operation(A, &oper->children[0]);
3613    cond = new_cond(cond);
3614
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);
3619       return ifBody;
3620    }
3621    else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)
3622             && !haveElseClause
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);
3627       return ifBody;
3628    }
3629    else {
3630       /* general case */
3631       ifBody = _slang_gen_operation(A, &oper->children[1]);
3632       if (haveElseClause)
3633          elseBody = _slang_gen_operation(A, &oper->children[2]);
3634       else
3635          elseBody = NULL;
3636       ifNode = new_if(cond, ifBody, elseBody);
3637       return ifNode;
3638    }
3639 }
3640
3641
3642
3643 static slang_ir_node *
3644 _slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
3645 {
3646    slang_ir_node *n;
3647
3648    assert(oper->type == SLANG_OPER_NOT);
3649
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 '!'");
3654       return NULL;
3655    }
3656
3657    n = _slang_gen_operation(A, &oper->children[0]);
3658    if (n)
3659       return new_not(n);
3660    else
3661       return NULL;
3662 }
3663
3664
3665 static slang_ir_node *
3666 _slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
3667 {
3668    slang_ir_node *n1, *n2;
3669
3670    assert(oper->type == SLANG_OPER_LOGICALXOR);
3671
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 '^^'");
3676       return NULL;
3677    }
3678
3679    n1 = _slang_gen_operation(A, &oper->children[0]);
3680    if (!n1)
3681       return NULL;
3682    n2 = _slang_gen_operation(A, &oper->children[1]);
3683    if (!n2)
3684       return NULL;
3685    return new_node2(IR_NOTEQUAL, n1, n2);
3686 }
3687
3688
3689 /**
3690  * Generate IR node for storage of a temporary of given size.
3691  */
3692 static slang_ir_node *
3693 _slang_gen_temporary(GLint size)
3694 {
3695    slang_ir_storage *store;
3696    slang_ir_node *n = NULL;
3697
3698    store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -2, size);
3699    if (store) {
3700       n = new_node0(IR_VAR_DECL);
3701       if (n) {
3702          n->Store = store;
3703       }
3704       else {
3705          _slang_free(store);
3706       }
3707    }
3708    return n;
3709 }
3710
3711
3712 /**
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).
3719  */
3720 static GLboolean
3721 make_constant_array(slang_assemble_ctx *A,
3722                     slang_variable *var,
3723                     slang_operation *initializer)
3724 {
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;
3729    GLint size;
3730    GLuint i, j;
3731    GLfloat *values;
3732
3733    if (!var->store) {
3734       var->store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -6, -6);
3735    }
3736    size = var->store->Size;
3737
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);
3742
3743    values = (GLfloat *) _mesa_malloc(numElements * 4 * sizeof(GLfloat));
3744
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 */
3750          free(values);
3751          return GL_FALSE;
3752       }
3753       for (j = 0; j < op->literal_size; j++) {
3754          values[i * 4 + j] = op->literal[j];
3755       }
3756       for ( ; j < 4; j++) {
3757          values[i * 4 + j] = 0.0f;
3758       }
3759    }
3760
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);
3766    }
3767    else {
3768       var->store->File = PROGRAM_CONSTANT;
3769       var->store->Index = _mesa_add_named_constant(prog->Parameters, varName,
3770                                                    values, size);
3771    }
3772    assert(var->store->Size == size);
3773
3774    _mesa_free(values);
3775
3776    return GL_TRUE;
3777 }
3778
3779
3780
3781 /**
3782  * Generate IR node for allocating/declaring a variable (either a local or
3783  * a global).
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.
3790  *
3791  * \param initializer  Optional initializer expression for the variable.
3792  */
3793 static slang_ir_node *
3794 _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var,
3795                     slang_operation *initializer)
3796 {
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;
3803
3804    /*assert(!var->declared);*/
3805    var->declared = GL_TRUE;
3806
3807    /* determine GPU register file for simple cases */
3808    if (is_sampler_type(&var->type)) {
3809       file = PROGRAM_SAMPLER;
3810    }
3811    else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3812       file = PROGRAM_UNIFORM;
3813    }
3814    else {
3815       file = PROGRAM_TEMPORARY;
3816    }
3817
3818    size = _slang_sizeof_type_specifier(&var->type.specifier);
3819    if (size <= 0) {
3820       slang_info_log_error(A->log, "invalid declaration for '%s'", varName);
3821       return NULL;
3822    }
3823
3824    arrayLen = _slang_array_length(var);
3825    totalSize = _slang_array_size(size, arrayLen);
3826
3827    /* Allocate IR node for the declaration */
3828    varDecl = new_node0(IR_VAR_DECL);
3829    if (!varDecl)
3830       return NULL;
3831
3832    /* Allocate slang_ir_storage for this variable if needed.
3833     * Note that we may not actually allocate a constant or temporary register
3834     * until later.
3835     */
3836    if (!var->store) {
3837       GLint index = -7;  /* TBD / unknown */
3838       var->store = _slang_new_ir_storage(file, index, totalSize);
3839       if (!var->store)
3840          return NULL; /* out of memory */
3841    }
3842
3843    /* set the IR node's Var and Store pointers */
3844    varDecl->Var = var;
3845    varDecl->Store = var->store;
3846
3847
3848    store = var->store;
3849
3850    /* if there's an initializer, generate IR for the expression */
3851    if (initializer) {
3852       slang_ir_node *varRef, *init;
3853
3854       if (var->type.qualifier == SLANG_QUAL_CONST) {
3855          /* if the variable is const, the initializer must be a const
3856           * expression as well.
3857           */
3858 #if 0
3859          if (!_slang_is_constant_expr(initializer)) {
3860             slang_info_log_error(A->log,
3861                                  "initializer for %s not constant", varName);
3862             return NULL;
3863          }
3864 #endif
3865       }
3866
3867       /* IR for the variable we're initializing */
3868       varRef = new_var(A, var);
3869       if (!varRef) {
3870          slang_info_log_error(A->log, "out of memory");
3871          return NULL;
3872       }
3873
3874       /* constant-folding, etc here */
3875       _slang_simplify(initializer, &A->space, A->atoms); 
3876
3877       /* look for simple constant-valued variables and uniforms */
3878       if (var->type.qualifier == SLANG_QUAL_CONST ||
3879           var->type.qualifier == SLANG_QUAL_UNIFORM) {
3880
3881          if (initializer->type == SLANG_OPER_CALL &&
3882              initializer->array_constructor) {
3883             /* array initializer */
3884             if (make_constant_array(A, var, initializer))
3885                return varRef;
3886          }
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,
3892                                                 varName,
3893                                                 totalSize, datatype,
3894                                                 initializer->literal);
3895                store->Swizzle = _slang_var_swizzle(size, 0);
3896                return varRef;
3897             }
3898 #if 0
3899             else {
3900                store->File = PROGRAM_CONSTANT;
3901                store->Index = _mesa_add_named_constant(A->program->Parameters,
3902                                                        varName,
3903                                                        initializer->literal,
3904                                                        totalSize);
3905                store->Swizzle = _slang_var_swizzle(size, 0);
3906                return varRef;
3907             }
3908 #endif
3909          }
3910       }
3911
3912       /* IR for initializer */
3913       init = _slang_gen_operation(A, initializer);
3914       if (!init)
3915          return NULL;
3916
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)");
3920          return NULL;
3921       }
3922
3923       /* assign RHS to LHS */
3924       n = new_node2(IR_COPY, varRef, init);
3925       n = new_seq(varDecl, n);
3926    }
3927    else {
3928       /* no initializer */
3929       n = varDecl;
3930    }
3931
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);
3937    }
3938
3939 #if 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);
3943 #endif
3944
3945    return n;
3946 }
3947
3948
3949 /**
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.
3954  */
3955 static slang_ir_node *
3956 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
3957 {
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;
3962
3963    assert(oper->type == SLANG_OPER_SELECT);
3964    assert(oper->num_children == 3);
3965
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);
3971    if (!isBool) {
3972       slang_info_log_error(A->log, "selector type is not boolean");
3973       return NULL;
3974    }
3975
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);
3983    if (!isEqual) {
3984       slang_info_log_error(A->log, "incompatible types for ?: operator");
3985       return NULL;
3986    }
3987
3988    /* size of x or y's type */
3989    size = _slang_sizeof_type_specifier(&type1.spec);
3990    assert(size > 0);
3991
3992    /* temporary var */
3993    tmpDecl = _slang_gen_temporary(size);
3994
3995    /* the condition (child 0) */
3996    cond = _slang_gen_operation(A, &oper->children[0]);
3997    cond = new_cond(cond);
3998
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);
4004
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);
4010
4011    ifNode = new_if(cond, trueNode, falseNode);
4012
4013    /* tmp var value */
4014    tmpVar = new_node0(IR_VAR);
4015    tmpVar->Store = tmpDecl->Store;
4016
4017    tree = new_seq(ifNode, tmpVar);
4018    tree = new_seq(tmpDecl, tree);
4019
4020    /*_slang_print_ir_tree(tree, 10);*/
4021    return tree;
4022 }
4023
4024
4025 /**
4026  * Generate code for &&.
4027  */
4028 static slang_ir_node *
4029 _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
4030 {
4031    /* rewrite "a && b" as  "a ? b : false" */
4032    slang_operation *select;
4033    slang_ir_node *n;
4034
4035    select = slang_operation_new(1);
4036    select->type = SLANG_OPER_SELECT;
4037    slang_operation_add_children(select, 3);
4038
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);
4042
4043    n = _slang_gen_select(A, select);
4044    return n;
4045 }
4046
4047
4048 /**
4049  * Generate code for ||.
4050  */
4051 static slang_ir_node *
4052 _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
4053 {
4054    /* rewrite "a || b" as  "a ? true : b" */
4055    slang_operation *select;
4056    slang_ir_node *n;
4057
4058    select = slang_operation_new(1);
4059    select->type = SLANG_OPER_SELECT;
4060    slang_operation_add_children(select, 3);
4061
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]);
4065
4066    n = _slang_gen_select(A, select);
4067    return n;
4068 }
4069
4070
4071 /**
4072  * Generate IR tree for a return statement.
4073  */
4074 static slang_ir_node *
4075 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
4076 {
4077    assert(oper->type == SLANG_OPER_RETURN);
4078    return new_return(A->curFuncEndLabel);
4079 }
4080
4081
4082 #if 0
4083 /**
4084  * Determine if the given operation/expression is const-valued.
4085  */
4086 static GLboolean
4087 _slang_is_constant_expr(const slang_operation *oper)
4088 {
4089    slang_variable *var;
4090    GLuint i;
4091
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)
4096          return GL_TRUE;
4097       return GL_FALSE;
4098    default:
4099       for (i = 0; i < oper->num_children; i++) {
4100          if (!_slang_is_constant_expr(&oper->children[i]))
4101             return GL_FALSE;
4102       }
4103       return GL_TRUE;
4104    }
4105 }
4106 #endif
4107
4108
4109 /**
4110  * Check if an assignment of type t1 to t0 is legal.
4111  * XXX more cases needed.
4112  */
4113 static GLboolean
4114 _slang_assignment_compatible(slang_assemble_ctx *A,
4115                              slang_operation *op0,
4116                              slang_operation *op1)
4117 {
4118    slang_typeinfo t0, t1;
4119    GLuint sz0, sz1;
4120
4121    if (op0->type == SLANG_OPER_POSTINCREMENT ||
4122        op0->type == SLANG_OPER_POSTDECREMENT) {
4123       return GL_FALSE;
4124    }
4125
4126    slang_typeinfo_construct(&t0);
4127    typeof_operation(A, op0, &t0);
4128
4129    slang_typeinfo_construct(&t1);
4130    typeof_operation(A, op1, &t1);
4131
4132    sz0 = _slang_sizeof_type_specifier(&t0.spec);
4133    sz1 = _slang_sizeof_type_specifier(&t1.spec);
4134
4135 #if 1
4136    if (sz0 != sz1) {
4137       /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4138       return GL_FALSE;
4139    }
4140 #endif
4141
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)
4145       return GL_FALSE;
4146
4147    if (t0.spec.type == SLANG_SPEC_FLOAT &&
4148        t1.spec.type == SLANG_SPEC_BOOL)
4149       return GL_FALSE;
4150
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)
4154       return GL_FALSE;
4155 #endif
4156
4157    if (t0.spec.type == SLANG_SPEC_BOOL &&
4158        t1.spec.type == SLANG_SPEC_FLOAT)
4159       return GL_FALSE;
4160
4161    if (t0.spec.type == SLANG_SPEC_BOOL &&
4162        t1.spec.type == SLANG_SPEC_INT)
4163       return GL_FALSE;
4164
4165    return GL_TRUE;
4166 }
4167
4168
4169 /**
4170  * Generate IR tree for a local variable declaration.
4171  * Basically do some error checking and call _slang_gen_var_decl().
4172  */
4173 static slang_ir_node *
4174 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
4175 {
4176    const char *varName = (char *) oper->a_id;
4177    slang_variable *var;
4178    slang_ir_node *varDecl;
4179    slang_operation *initializer;
4180
4181    assert(oper->type == SLANG_OPER_VARIABLE_DECL);
4182    assert(oper->num_children <= 1);
4183
4184
4185    /* lookup the variable by name */
4186    var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4187    if (!var)
4188       return NULL;  /* "shouldn't happen" */
4189
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",
4196                 varName);
4197       return NULL;
4198    }
4199
4200 #if 0
4201    if (v->declared) {
4202       slang_info_log_error(A->log, "variable '%s' redeclared", varName);
4203       return NULL;
4204    }
4205 #endif
4206
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];
4211    }
4212    else if (var->initializer) {
4213       initializer = var->initializer;
4214    }
4215    else {
4216       initializer = NULL;
4217    }
4218
4219    if (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");
4223          return NULL;
4224       }         
4225    }
4226    else {
4227       if (var->type.qualifier == SLANG_QUAL_CONST) {
4228          slang_info_log_error(A->log,
4229                        "const-qualified variable '%s' requires initializer",
4230                        varName);
4231          return NULL;
4232       }
4233    }
4234
4235    /* Generate IR node */
4236    varDecl = _slang_gen_var_decl(A, var, initializer);
4237    if (!varDecl)
4238       return NULL;
4239
4240    return varDecl;
4241 }
4242
4243
4244 /**
4245  * Generate IR tree for a reference to a variable (such as in an expression).
4246  * This is different from a variable declaration.
4247  */
4248 static slang_ir_node *
4249 _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
4250 {
4251    /* If there's a variable associated with this oper (from inlining)
4252     * use it.  Otherwise, use the oper's var id.
4253     */
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);
4256    slang_ir_node *n;
4257    if (!var) {
4258       slang_info_log_error(A->log, "undefined variable '%s'", (char *) name);
4259       return NULL;
4260    }
4261    assert(var->declared);
4262    n = new_var(A, var);
4263    return n;
4264 }
4265
4266
4267
4268 /**
4269  * Return the number of components actually named by the swizzle.
4270  * Recall that swizzles may have undefined/don't-care values.
4271  */
4272 static GLuint
4273 swizzle_size(GLuint swizzle)
4274 {
4275    GLuint size = 0, i;
4276    for (i = 0; i < 4; i++) {
4277       GLuint swz = GET_SWZ(swizzle, i);
4278       size += (swz >= 0 && swz <= 3);
4279    }
4280    return size;
4281 }
4282
4283
4284 static slang_ir_node *
4285 _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
4286 {
4287    slang_ir_node *n = new_node1(IR_SWIZZLE, child);
4288    assert(child);
4289    if (n) {
4290       assert(!n->Store);
4291       n->Store = _slang_new_ir_storage_relative(0,
4292                                                 swizzle_size(swizzle),
4293                                                 child->Store);
4294       n->Store->Swizzle = swizzle;
4295    }
4296    return n;
4297 }
4298
4299
4300 static GLboolean
4301 is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
4302 {
4303    while (store->Parent)
4304       store = store->Parent;
4305
4306    if (!(store->File == PROGRAM_OUTPUT ||
4307          store->File == PROGRAM_TEMPORARY ||
4308          (store->File == PROGRAM_VARYING &&
4309           A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
4310       return GL_FALSE;
4311    }
4312    else {
4313       return GL_TRUE;
4314    }
4315 }
4316
4317
4318 /**
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".
4321  */
4322 static GLuint
4323 root_swizzle(const slang_ir_storage *st)
4324 {
4325    GLuint swizzle = st->Swizzle;
4326    while (st->Parent) {
4327       st = st->Parent;
4328       swizzle = _slang_swizzle_swizzle(st->Swizzle, swizzle);
4329    }
4330    return swizzle;
4331 }
4332
4333
4334 /**
4335  * Generate IR tree for an assignment (=).
4336  */
4337 static slang_ir_node *
4338 _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
4339 {
4340    slang_operation *pred = NULL;
4341    slang_ir_node *n = NULL;
4342
4343    if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
4344       /* Check that var is writeable */
4345       slang_variable *var
4346          = _slang_variable_locate(oper->children[0].locals,
4347                                   oper->children[0].a_id, GL_TRUE);
4348       if (!var) {
4349          slang_info_log_error(A->log, "undefined variable '%s'",
4350                               (char *) oper->children[0].a_id);
4351          return NULL;
4352       }
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);
4361          return NULL;
4362       }
4363
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;
4373       }
4374    }
4375
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)
4380        *
4381        * XXX this could be even more effective if we could accomodate
4382        * cases such as "v.x = f();"  - would help with typical vertex
4383        * transformation.
4384        */
4385       n = _slang_gen_function_call_name(A,
4386                                       (const char *) oper->children[1].a_id,
4387                                       &oper->children[1], &oper->children[0]);
4388    }
4389    else {
4390       slang_ir_node *lhs, *rhs;
4391
4392       /* lhs and rhs type checking */
4393       if (!_slang_assignment_compatible(A,
4394                                         &oper->children[0],
4395                                         &oper->children[1])) {
4396          slang_info_log_error(A->log, "incompatible types in assignment");
4397          return NULL;
4398       }
4399
4400       lhs = _slang_gen_operation(A, &oper->children[0]);
4401       if (!lhs) {
4402          return NULL;
4403       }
4404
4405       if (!lhs->Store) {
4406          slang_info_log_error(A->log,
4407                               "invalid left hand side for assignment");
4408          return NULL;
4409       }
4410
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");
4415          return NULL;
4416       }
4417
4418       rhs = _slang_gen_operation(A, &oper->children[1]);
4419       if (lhs && rhs) {
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.
4426              */
4427             rhs = _slang_gen_swizzle(rhs, newSwizzle);
4428          }
4429          n = new_node2(IR_COPY, lhs, rhs);
4430       }
4431       else {
4432          return NULL;
4433       }
4434    }
4435
4436    if (n && pred) {
4437       /* predicate the assignment code on __notRetFlag */
4438       slang_ir_node *top, *cond;
4439
4440       cond = _slang_gen_operation(A, pred);
4441       top = new_if(cond, n, NULL);
4442       return top;
4443    }
4444    return n;
4445 }
4446
4447
4448 /**
4449  * Generate IR tree for referencing a field in a struct (or basic vector type)
4450  */
4451 static slang_ir_node *
4452 _slang_gen_struct_field(slang_assemble_ctx * A, slang_operation *oper)
4453 {
4454    slang_typeinfo ti;
4455
4456    /* type of struct */
4457    slang_typeinfo_construct(&ti);
4458    typeof_operation(A, &oper->children[0], &ti);
4459
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);
4463       slang_swizzle swz;
4464       slang_ir_node *n;
4465       GLuint swizzle;
4466       if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4467          slang_info_log_error(A->log, "Bad swizzle");
4468          return NULL;
4469       }
4470       swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4471                               swz.swizzle[1],
4472                               swz.swizzle[2],
4473                               swz.swizzle[3]);
4474
4475       n = _slang_gen_operation(A, &oper->children[0]);
4476       /* create new parent node with swizzle */
4477       if (n)
4478          n = _slang_gen_swizzle(n, swizzle);
4479       return n;
4480    }
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;
4485       slang_swizzle swz;
4486       slang_ir_node *n;
4487       GLuint swizzle;
4488       if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4489          slang_info_log_error(A->log, "Bad swizzle");
4490       }
4491       swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4492                               swz.swizzle[1],
4493                               swz.swizzle[2],
4494                               swz.swizzle[3]);
4495       n = _slang_gen_operation(A, &oper->children[0]);
4496       /* create new parent node with swizzle */
4497       n = _slang_gen_swizzle(n, swizzle);
4498       return n;
4499    }
4500    else {
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;
4507
4508       /* type of field */
4509       slang_typeinfo_construct(&field_ti);
4510       typeof_operation(A, oper, &field_ti);
4511
4512       fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
4513       if (fieldSize > 0)
4514          fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
4515
4516       if (fieldSize == 0 || fieldOffset < 0) {
4517          const char *structName;
4518          if (ti.spec._struct)
4519             structName = (char *) ti.spec._struct->a_name;
4520          else
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);
4525          return NULL;
4526       }
4527       assert(fieldSize >= 0);
4528
4529       base = _slang_gen_operation(A, &oper->children[0]);
4530       if (!base) {
4531          /* error msg should have already been logged */
4532          return NULL;
4533       }
4534
4535       n = new_node1(IR_FIELD, base);
4536       if (!n)
4537          return NULL;
4538
4539       n->Field = (char *) oper->a_id;
4540
4541       /* Store the field's offset in storage->Index */
4542       n->Store = _slang_new_ir_storage(base->Store->File,
4543                                        fieldOffset,
4544                                        fieldSize);
4545
4546       return n;
4547    }
4548 }
4549
4550
4551 /**
4552  * Gen code for array indexing.
4553  */
4554 static slang_ir_node *
4555 _slang_gen_array_element(slang_assemble_ctx * A, slang_operation *oper)
4556 {
4557    slang_typeinfo array_ti;
4558
4559    /* get array's type info */
4560    slang_typeinfo_construct(&array_ti);
4561    typeof_operation(A, &oper->children[0], &array_ti);
4562
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);
4567       GLint index;
4568       slang_ir_node *n;
4569
4570       index = (GLint) oper->children[1].literal[0];
4571       if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
4572           index >= (GLint) max) {
4573 #if 0
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);
4579          return NULL;
4580 #else
4581          index = 0;
4582 #endif
4583       }
4584
4585       n = _slang_gen_operation(A, &oper->children[0]);
4586       if (n) {
4587          /* use swizzle to access the element */
4588          GLuint swizzle = MAKE_SWIZZLE4(SWIZZLE_X + index,
4589                                         SWIZZLE_NIL,
4590                                         SWIZZLE_NIL,
4591                                         SWIZZLE_NIL);
4592          n = _slang_gen_swizzle(n, swizzle);
4593       }
4594       assert(n->Store);
4595       return n;
4596    }
4597    else {
4598       /* conventional array */
4599       slang_typeinfo elem_ti;
4600       slang_ir_node *elem, *array, *index;
4601       GLint elemSize, arrayLen;
4602
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);
4607
4608       if (_slang_type_is_matrix(array_ti.spec.type))
4609          arrayLen = _slang_type_dim(array_ti.spec.type);
4610       else
4611          arrayLen = array_ti.array_len;
4612
4613       slang_typeinfo_destruct(&array_ti);
4614       slang_typeinfo_destruct(&elem_ti);
4615
4616       if (elemSize <= 0) {
4617          /* unknown var or type */
4618          slang_info_log_error(A->log, "Undefined variable or type");
4619          return NULL;
4620       }
4621
4622       array = _slang_gen_operation(A, &oper->children[0]);
4623       index = _slang_gen_operation(A, &oper->children[1]);
4624       if (array && index) {
4625          /* bounds check */
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);
4635                return NULL;
4636             }
4637          }
4638
4639          if (!array->Store) {
4640             slang_info_log_error(A->log, "Invalid array");
4641             return NULL;
4642          }
4643
4644          elem = new_node2(IR_ELEMENT, array, index);
4645
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,
4649                                              elemSize);
4650          elem->Store->Swizzle = _slang_var_swizzle(elemSize, 0);
4651          return elem;
4652       }
4653       else {
4654          _slang_free_ir_tree(array);
4655          _slang_free_ir_tree(index);
4656          return NULL;
4657       }
4658    }
4659 }
4660
4661
4662 static slang_ir_node *
4663 _slang_gen_compare(slang_assemble_ctx *A, slang_operation *oper,
4664                    slang_ir_opcode opcode)
4665 {
4666    slang_typeinfo t0, t1;
4667    slang_ir_node *n;
4668    
4669    slang_typeinfo_construct(&t0);
4670    typeof_operation(A, &oper->children[0], &t0);
4671
4672    slang_typeinfo_construct(&t1);
4673    typeof_operation(A, &oper->children[0], &t1);
4674
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");
4678       return NULL;
4679    }
4680
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");
4689          return NULL;
4690       }
4691    }
4692
4693    n =  new_node2(opcode,
4694                   _slang_gen_operation(A, &oper->children[0]),
4695                   _slang_gen_operation(A, &oper->children[1]));
4696
4697    /* result is a bool (size 1) */
4698    n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, 1);
4699
4700    return n;
4701 }
4702
4703
4704 #if 0
4705 static void
4706 print_vars(slang_variable_scope *s)
4707 {
4708    int i;
4709    printf("vars: ");
4710    for (i = 0; i < s->num_variables; i++) {
4711       printf("%s %d, \n",
4712              (char*) s->variables[i]->a_name,
4713              s->variables[i]->declared);
4714    }
4715
4716    printf("\n");
4717 }
4718 #endif
4719
4720
4721 #if 0
4722 static void
4723 _slang_undeclare_vars(slang_variable_scope *locals)
4724 {
4725    if (locals->num_variables > 0) {
4726       int i;
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;
4731       }
4732    }
4733 }
4734 #endif
4735
4736
4737 /**
4738  * Generate IR tree for a slang_operation (AST node)
4739  */
4740 static slang_ir_node *
4741 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
4742 {
4743    switch (oper->type) {
4744    case SLANG_OPER_BLOCK_NEW_SCOPE:
4745       {
4746          slang_ir_node *n;
4747
4748          _slang_push_var_table(A->vartable);
4749
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 */
4753
4754          _slang_pop_var_table(A->vartable);
4755
4756          /*_slang_undeclare_vars(oper->locals);*/
4757          /*print_vars(oper->locals);*/
4758
4759          if (n)
4760             n = new_node1(IR_SCOPE, n);
4761          return n;
4762       }
4763       break;
4764
4765    case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
4766       /* list of operations */
4767       if (oper->num_children > 0)
4768       {
4769          slang_ir_node *n, *tree = NULL;
4770          GLuint i;
4771
4772          for (i = 0; i < oper->num_children; i++) {
4773             n = _slang_gen_operation(A, &oper->children[i]);
4774             if (!n) {
4775                _slang_free_ir_tree(tree);
4776                return NULL; /* error must have occured */
4777             }
4778             tree = new_seq(tree, n);
4779          }
4780
4781          return tree;
4782       }
4783       else {
4784          return new_node0(IR_NOP);
4785       }
4786
4787    case SLANG_OPER_EXPRESSION:
4788       return _slang_gen_operation(A, &oper->children[0]);
4789
4790    case SLANG_OPER_FOR:
4791       return _slang_gen_for(A, oper);
4792    case SLANG_OPER_DO:
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");
4799          return NULL;
4800       }
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");
4805          return NULL;
4806       }
4807       return _slang_gen_continue(A, oper);
4808    case SLANG_OPER_DISCARD:
4809       return new_node0(IR_KILL);
4810
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:
4824       {
4825          slang_ir_node *n;
4826          assert(oper->num_children == 2);
4827          n = _slang_gen_function_call_name(A, "+", oper, NULL);
4828          return n;
4829       }
4830    case SLANG_OPER_SUBTRACT:
4831       {
4832          slang_ir_node *n;
4833          assert(oper->num_children == 2);
4834          n = _slang_gen_function_call_name(A, "-", oper, NULL);
4835          return n;
4836       }
4837    case SLANG_OPER_MULTIPLY:
4838       {
4839          slang_ir_node *n;
4840          assert(oper->num_children == 2);
4841          n = _slang_gen_function_call_name(A, "*", oper, NULL);
4842          return n;
4843       }
4844    case SLANG_OPER_DIVIDE:
4845       {
4846          slang_ir_node *n;
4847          assert(oper->num_children == 2);
4848          n = _slang_gen_function_call_name(A, "/", oper, NULL);
4849          return n;
4850       }
4851    case SLANG_OPER_MINUS:
4852       {
4853          slang_ir_node *n;
4854          assert(oper->num_children == 1);
4855          n = _slang_gen_function_call_name(A, "-", oper, NULL);
4856          return n;
4857       }
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:
4866       {
4867          slang_ir_node *n;
4868          assert(oper->num_children == 2);
4869          n = _slang_gen_function_call_name(A, "+=", oper, NULL);
4870          return n;
4871       }
4872    case SLANG_OPER_SUBASSIGN:
4873       {
4874          slang_ir_node *n;
4875          assert(oper->num_children == 2);
4876          n = _slang_gen_function_call_name(A, "-=", oper, NULL);
4877          return n;
4878       }
4879       break;
4880    case SLANG_OPER_MULASSIGN:
4881       {
4882          slang_ir_node *n;
4883          assert(oper->num_children == 2);
4884          n = _slang_gen_function_call_name(A, "*=", oper, NULL);
4885          return n;
4886       }
4887    case SLANG_OPER_DIVASSIGN:
4888       {
4889          slang_ir_node *n;
4890          assert(oper->num_children == 2);
4891          n = _slang_gen_function_call_name(A, "/=", oper, NULL);
4892          return n;
4893       }
4894    case SLANG_OPER_LOGICALAND:
4895       {
4896          slang_ir_node *n;
4897          assert(oper->num_children == 2);
4898          n = _slang_gen_logical_and(A, oper);
4899          return n;
4900       }
4901    case SLANG_OPER_LOGICALOR:
4902       {
4903          slang_ir_node *n;
4904          assert(oper->num_children == 2);
4905          n = _slang_gen_logical_or(A, oper);
4906          return n;
4907       }
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 */
4913       {
4914          slang_ir_node *n;
4915          assert(oper->num_children == 3);
4916          n = _slang_gen_select(A, oper);
4917          return n;
4918       }
4919
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,
4924                                            oper, NULL);
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);
4935    case SLANG_OPER_IF:
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:
4942       /* fall-through */
4943    case SLANG_OPER_LITERAL_INT:
4944       /* fall-through */
4945    case SLANG_OPER_LITERAL_BOOL:
4946       return new_float_literal(oper->literal, oper->literal_size);
4947
4948    case SLANG_OPER_POSTINCREMENT:   /* var++ */
4949       {
4950          slang_ir_node *n;
4951          assert(oper->num_children == 1);
4952          n = _slang_gen_function_call_name(A, "__postIncr", oper, NULL);
4953          return n;
4954       }
4955    case SLANG_OPER_POSTDECREMENT:   /* var-- */
4956       {
4957          slang_ir_node *n;
4958          assert(oper->num_children == 1);
4959          n = _slang_gen_function_call_name(A, "__postDecr", oper, NULL);
4960          return n;
4961       }
4962    case SLANG_OPER_PREINCREMENT:   /* ++var */
4963       {
4964          slang_ir_node *n;
4965          assert(oper->num_children == 1);
4966          n = _slang_gen_function_call_name(A, "++", oper, NULL);
4967          return n;
4968       }
4969    case SLANG_OPER_PREDECREMENT:   /* --var */
4970       {
4971          slang_ir_node *n;
4972          assert(oper->num_children == 1);
4973          n = _slang_gen_function_call_name(A, "--", oper, NULL);
4974          return n;
4975       }
4976
4977    case SLANG_OPER_NON_INLINED_CALL:
4978    case SLANG_OPER_SEQUENCE:
4979       {
4980          slang_ir_node *tree = NULL;
4981          GLuint i;
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);
4985             if (n)
4986                tree->Store = n->Store;
4987          }
4988          if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
4989             tree = new_function_call(tree, oper->label);
4990          }
4991          return tree;
4992       }
4993
4994    case SLANG_OPER_NONE:
4995    case SLANG_OPER_VOID:
4996       /* returning NULL here would generate an error */
4997       return new_node0(IR_NOP);
4998
4999    default:
5000       _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
5001                     oper->type);
5002       return new_node0(IR_NOP);
5003    }
5004
5005    return NULL;
5006 }
5007
5008
5009 /**
5010  * Check if the given type specifier is a rectangular texture sampler.
5011  */
5012 static GLboolean
5013 is_rect_sampler_spec(const slang_type_specifier *spec)
5014 {
5015    while (spec->_array) {
5016       spec = spec->_array;
5017    }
5018    return spec->type == SLANG_SPEC_SAMPLER2DRECT ||
5019           spec->type == SLANG_SPEC_SAMPLER2DRECTSHADOW;
5020 }
5021
5022
5023
5024 /**
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.
5028  *
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).
5032  *
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()).
5036  */
5037 GLboolean
5038 _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
5039                                slang_unit_type type)
5040 {
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;
5045    int dbg = 0;
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);
5051
5052    var->is_global = GL_TRUE;
5053
5054    /* check for sampler2D arrays */
5055    if (texIndex == -1 && var->type.specifier._array)
5056       texIndex = sampler_to_texture_index(var->type.specifier._array->type);
5057
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)
5063        */
5064       if (var->initializer) {
5065          slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
5066          return GL_FALSE;
5067       }
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);
5072          return GL_FALSE;
5073       }
5074 #else
5075       (void) is_rect_sampler_spec; /* silence warning */
5076 #endif
5077       {
5078          GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
5079          store = _slang_new_ir_storage_sampler(sampNum, texIndex, totalSize);
5080
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.
5086           */
5087          if (arrayLen > 0) {
5088             GLint a = arrayLen - 1;
5089             GLint i;
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);
5094             }
5095          }
5096       }
5097       if (dbg) printf("SAMPLER ");
5098    }
5099    else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
5100       /* Uniform variable */
5101       const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
5102
5103       if (prog) {
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);
5115          
5116                if (arrayLen > 0) {
5117                   GLint a = arrayLen - 1;
5118                   GLint i;
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);
5123                   }
5124                }
5125
5126                /* XXX what we need to do is unroll the struct into its
5127                 * basic types, creating a uniform variable for each.
5128                 * For example:
5129                 * struct foo {
5130                 *   vec3 a;
5131                 *   vec4 b;
5132                 * };
5133                 * uniform foo f;
5134                 *
5135                 * Should produce uniforms:
5136                 * "f.a"  (GL_FLOAT_VEC3)
5137                 * "f.b"  (GL_FLOAT_VEC4)
5138                 */
5139
5140                if (var->initializer) {
5141                   slang_info_log_error(A->log,
5142                      "unsupported initializer for uniform '%s'", varName);
5143                   return GL_FALSE;
5144                }
5145             }
5146             else {
5147                slang_info_log_error(A->log,
5148                                     "invalid datatype for uniform variable %s",
5149                                     varName);
5150                return GL_FALSE;
5151             }
5152          }
5153          else {
5154             /* non-struct uniform */
5155             if (!_slang_gen_var_decl(A, var, var->initializer))
5156                return GL_FALSE;
5157             store = var->store;
5158          }
5159       }
5160       else {
5161          /* pre-defined uniform, like gl_ModelviewMatrix */
5162          /* We know it's a uniform, but don't allocate storage unless
5163           * it's really used.
5164           */
5165          store = _slang_new_ir_storage_swz(PROGRAM_STATE_VAR, -1,
5166                                            totalSize, swizzle);
5167       }
5168       if (dbg) printf("UNIFORM (sz %d) ", totalSize);
5169    }
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",
5176                               varName);
5177          return GL_FALSE;
5178       }
5179
5180       if (var->initializer) {
5181          slang_info_log_error(A->log, "illegal initializer for varying '%s'",
5182                               varName);
5183          return GL_FALSE;
5184       }
5185
5186       if (prog) {
5187          /* user-defined varying */
5188          GLbitfield flags;
5189          GLint varyingLoc;
5190          GLuint swizzle;
5191
5192          flags = 0x0;
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;
5197
5198          varyingLoc = _mesa_add_varying(prog->Varying, varName,
5199                                         totalSize, flags);
5200          swizzle = _slang_var_swizzle(size, 0);
5201          store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
5202                                            totalSize, swizzle);
5203       }
5204       else {
5205          /* pre-defined varying, like gl_Color or gl_TexCoord */
5206          if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
5207             /* fragment program input */
5208             GLuint swizzle;
5209             GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5210                                              &swizzle);
5211             assert(index >= 0);
5212             assert(index < FRAG_ATTRIB_MAX);
5213             store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index,
5214                                               size, swizzle);
5215          }
5216          else {
5217             /* vertex program output */
5218             GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5219             GLuint swizzle = _slang_var_swizzle(size, 0);
5220             assert(index >= 0);
5221             assert(index < VERT_RESULT_MAX);
5222             assert(type == SLANG_UNIT_VERTEX_BUILTIN);
5223             store = _slang_new_ir_storage_swz(PROGRAM_OUTPUT, index,
5224                                               size, swizzle);
5225          }
5226          if (dbg) printf("V/F ");
5227       }
5228       if (dbg) printf("VARYING ");
5229    }
5230    else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
5231       GLuint swizzle;
5232       GLint index;
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",
5237                               varName);
5238          return GL_FALSE;
5239       }
5240
5241       if (prog) {
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);
5247          assert(index >= 0);
5248          index = VERT_ATTRIB_GENERIC0 + index;
5249       }
5250       else {
5251          /* pre-defined vertex attrib */
5252          index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
5253          assert(index >= 0);
5254       }
5255       store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5256       if (dbg) printf("ATTRIB ");
5257    }
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,
5261                                        &swizzle);
5262       store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5263       if (dbg) printf("INPUT ");
5264    }
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);
5269       }
5270       else {
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);
5275       }
5276       if (dbg) printf("OUTPUT ");
5277    }
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 ");
5282    }
5283    else {
5284       /* ordinary variable (may be const) */
5285       slang_ir_node *n;
5286
5287       /* IR node to declare the variable */
5288       n = _slang_gen_var_decl(A, var, var->initializer);
5289
5290       /* emit GPU instructions */
5291       success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_FALSE, A->log);
5292
5293       _slang_free_ir_tree(n);
5294    }
5295
5296    if (dbg) printf("GLOBAL VAR %s  idx %d\n", (char*) var->a_name,
5297                    store ? store->Index : -2);
5298
5299    if (store)
5300       var->store = store;  /* save var's storage info */
5301
5302    var->declared = GL_TRUE;
5303
5304    return success;
5305 }
5306
5307
5308 /**
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
5311  * instructions.
5312  */
5313 GLboolean
5314 _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
5315 {
5316    slang_ir_node *n;
5317    GLboolean success = GL_TRUE;
5318
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.
5322        */
5323 #if 0
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 */
5327          slang_operation *op
5328             = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
5329          if (!op) {
5330             slang_info_log_error(A->log,
5331                                  "function \"%s\" has no return statement",
5332                                  (char *) fun->header.a_name);
5333             printf(
5334                    "function \"%s\" has no return statement\n",
5335                    (char *) fun->header.a_name);
5336             return GL_FALSE;
5337          }
5338       }
5339 #endif
5340       return GL_TRUE;  /* not an error */
5341    }
5342
5343 #if 0
5344    printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
5345    slang_print_function(fun, 1);
5346 #endif
5347
5348    /* should have been allocated earlier: */
5349    assert(A->program->Parameters );
5350    assert(A->program->Varying);
5351    assert(A->vartable);
5352
5353    A->LoopDepth = 0;
5354    A->UseReturnFlag = GL_FALSE;
5355    A->CurFunction = fun;
5356
5357    /* fold constant expressions, etc. */
5358    _slang_simplify(fun->body, &A->space, A->atoms);
5359
5360 #if 0
5361    printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
5362    slang_print_function(fun, 1);
5363 #endif
5364
5365    /* Create an end-of-function label */
5366    A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
5367
5368    /* push new vartable scope */
5369    _slang_push_var_table(A->vartable);
5370
5371    /* Generate IR tree for the function body code */
5372    n = _slang_gen_operation(A, fun->body);
5373    if (n)
5374       n = new_node1(IR_SCOPE, n);
5375
5376    /* pop vartable, restore previous */
5377    _slang_pop_var_table(A->vartable);
5378
5379    if (!n) {
5380       /* XXX record error */
5381       return GL_FALSE;
5382    }
5383
5384    /* append an end-of-function-label to IR tree */
5385    n = new_seq(n, new_label(A->curFuncEndLabel));
5386
5387    /*_slang_label_delete(A->curFuncEndLabel);*/
5388    A->curFuncEndLabel = NULL;
5389
5390 #if 0
5391    printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
5392    slang_print_function(fun, 1);
5393 #endif
5394 #if 0
5395    printf("************* IR for %s *******\n", (char*)fun->header.a_name);
5396    _slang_print_ir_tree(n, 0);
5397 #endif
5398 #if 0
5399    printf("************* End codegen function ************\n\n");
5400 #endif
5401
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.
5406        */
5407       return GL_TRUE;
5408    }
5409
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);
5413
5414    /* free codegen context */
5415    /*
5416    _mesa_free(A->codegen);
5417    */
5418
5419    return success;
5420 }
5421