glsl: checkpoint: replace 'return' with __returnFlag=true;
[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
79 /**
80 * Retrieves type information about an operation.
81 * Returns GL_TRUE on success.
82 * Returns GL_FALSE otherwise.
83 */
84 static GLboolean
85 typeof_operation(const struct slang_assemble_ctx_ *A,
86 slang_operation *op,
87 slang_typeinfo *ti)
88 {
89 return _slang_typeof_operation(op, &A->space, ti, A->atoms, A->log);
90 }
91
92
93 static GLboolean
94 is_sampler_type(const slang_fully_specified_type *t)
95 {
96 switch (t->specifier.type) {
97 case SLANG_SPEC_SAMPLER1D:
98 case SLANG_SPEC_SAMPLER2D:
99 case SLANG_SPEC_SAMPLER3D:
100 case SLANG_SPEC_SAMPLERCUBE:
101 case SLANG_SPEC_SAMPLER1DSHADOW:
102 case SLANG_SPEC_SAMPLER2DSHADOW:
103 case SLANG_SPEC_SAMPLER2DRECT:
104 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
105 return GL_TRUE;
106 default:
107 return GL_FALSE;
108 }
109 }
110
111
112 /**
113 * Return the offset (in floats or ints) of the named field within
114 * the given struct. Return -1 if field not found.
115 * If field is NULL, return the size of the struct instead.
116 */
117 static GLint
118 _slang_field_offset(const slang_type_specifier *spec, slang_atom field)
119 {
120 GLint offset = 0;
121 GLuint i;
122 for (i = 0; i < spec->_struct->fields->num_variables; i++) {
123 const slang_variable *v = spec->_struct->fields->variables[i];
124 const GLuint sz = _slang_sizeof_type_specifier(&v->type.specifier);
125 if (sz > 1) {
126 /* types larger than 1 float are register (4-float) aligned */
127 offset = (offset + 3) & ~3;
128 }
129 if (field && v->a_name == field) {
130 return offset;
131 }
132 offset += sz;
133 }
134 if (field)
135 return -1; /* field not found */
136 else
137 return offset; /* struct size */
138 }
139
140
141 /**
142 * Return the size (in floats) of the given type specifier.
143 * If the size is greater than 4, the size should be a multiple of 4
144 * so that the correct number of 4-float registers are allocated.
145 * For example, a mat3x2 is size 12 because we want to store the
146 * 3 columns in 3 float[4] registers.
147 */
148 GLuint
149 _slang_sizeof_type_specifier(const slang_type_specifier *spec)
150 {
151 GLuint sz;
152 switch (spec->type) {
153 case SLANG_SPEC_VOID:
154 sz = 0;
155 break;
156 case SLANG_SPEC_BOOL:
157 sz = 1;
158 break;
159 case SLANG_SPEC_BVEC2:
160 sz = 2;
161 break;
162 case SLANG_SPEC_BVEC3:
163 sz = 3;
164 break;
165 case SLANG_SPEC_BVEC4:
166 sz = 4;
167 break;
168 case SLANG_SPEC_INT:
169 sz = 1;
170 break;
171 case SLANG_SPEC_IVEC2:
172 sz = 2;
173 break;
174 case SLANG_SPEC_IVEC3:
175 sz = 3;
176 break;
177 case SLANG_SPEC_IVEC4:
178 sz = 4;
179 break;
180 case SLANG_SPEC_FLOAT:
181 sz = 1;
182 break;
183 case SLANG_SPEC_VEC2:
184 sz = 2;
185 break;
186 case SLANG_SPEC_VEC3:
187 sz = 3;
188 break;
189 case SLANG_SPEC_VEC4:
190 sz = 4;
191 break;
192 case SLANG_SPEC_MAT2:
193 sz = 2 * 4; /* 2 columns (regs) */
194 break;
195 case SLANG_SPEC_MAT3:
196 sz = 3 * 4;
197 break;
198 case SLANG_SPEC_MAT4:
199 sz = 4 * 4;
200 break;
201 case SLANG_SPEC_MAT23:
202 sz = 2 * 4; /* 2 columns (regs) */
203 break;
204 case SLANG_SPEC_MAT32:
205 sz = 3 * 4; /* 3 columns (regs) */
206 break;
207 case SLANG_SPEC_MAT24:
208 sz = 2 * 4;
209 break;
210 case SLANG_SPEC_MAT42:
211 sz = 4 * 4; /* 4 columns (regs) */
212 break;
213 case SLANG_SPEC_MAT34:
214 sz = 3 * 4;
215 break;
216 case SLANG_SPEC_MAT43:
217 sz = 4 * 4; /* 4 columns (regs) */
218 break;
219 case SLANG_SPEC_SAMPLER1D:
220 case SLANG_SPEC_SAMPLER2D:
221 case SLANG_SPEC_SAMPLER3D:
222 case SLANG_SPEC_SAMPLERCUBE:
223 case SLANG_SPEC_SAMPLER1DSHADOW:
224 case SLANG_SPEC_SAMPLER2DSHADOW:
225 case SLANG_SPEC_SAMPLER2DRECT:
226 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
227 sz = 1; /* a sampler is basically just an integer index */
228 break;
229 case SLANG_SPEC_STRUCT:
230 sz = _slang_field_offset(spec, 0); /* special use */
231 if (sz == 1) {
232 /* 1-float structs are actually troublesome to deal with since they
233 * might get placed at R.x, R.y, R.z or R.z. Return size=2 to
234 * ensure the object is placed at R.x
235 */
236 sz = 2;
237 }
238 else if (sz > 4) {
239 sz = (sz + 3) & ~0x3; /* round up to multiple of four */
240 }
241 break;
242 case SLANG_SPEC_ARRAY:
243 sz = _slang_sizeof_type_specifier(spec->_array);
244 break;
245 default:
246 _mesa_problem(NULL, "Unexpected type in _slang_sizeof_type_specifier()");
247 sz = 0;
248 }
249
250 if (sz > 4) {
251 /* if size is > 4, it should be a multiple of four */
252 assert((sz & 0x3) == 0);
253 }
254 return sz;
255 }
256
257
258 /**
259 * Query variable/array length (number of elements).
260 * This is slightly non-trivial because there are two ways to express
261 * arrays: "float x[3]" vs. "float[3] x".
262 * \return the length of the array for the given variable, or 0 if not an array
263 */
264 static GLint
265 _slang_array_length(const slang_variable *var)
266 {
267 if (var->type.array_len > 0) {
268 /* Ex: float[4] x; */
269 return var->type.array_len;
270 }
271 if (var->array_len > 0) {
272 /* Ex: float x[4]; */
273 return var->array_len;
274 }
275 return 0;
276 }
277
278
279 /**
280 * Compute total size of array give size of element, number of elements.
281 * \return size in floats
282 */
283 static GLint
284 _slang_array_size(GLint elemSize, GLint arrayLen)
285 {
286 GLint total;
287 assert(elemSize > 0);
288 if (arrayLen > 1) {
289 /* round up base type to multiple of 4 */
290 total = ((elemSize + 3) & ~0x3) * MAX2(arrayLen, 1);
291 }
292 else {
293 total = elemSize;
294 }
295 return total;
296 }
297
298
299 /**
300 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
301 * or -1 if the type is not a sampler.
302 */
303 static GLint
304 sampler_to_texture_index(const slang_type_specifier_type type)
305 {
306 switch (type) {
307 case SLANG_SPEC_SAMPLER1D:
308 return TEXTURE_1D_INDEX;
309 case SLANG_SPEC_SAMPLER2D:
310 return TEXTURE_2D_INDEX;
311 case SLANG_SPEC_SAMPLER3D:
312 return TEXTURE_3D_INDEX;
313 case SLANG_SPEC_SAMPLERCUBE:
314 return TEXTURE_CUBE_INDEX;
315 case SLANG_SPEC_SAMPLER1DSHADOW:
316 return TEXTURE_1D_INDEX; /* XXX fix */
317 case SLANG_SPEC_SAMPLER2DSHADOW:
318 return TEXTURE_2D_INDEX; /* XXX fix */
319 case SLANG_SPEC_SAMPLER2DRECT:
320 return TEXTURE_RECT_INDEX;
321 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
322 return TEXTURE_RECT_INDEX; /* XXX fix */
323 default:
324 return -1;
325 }
326 }
327
328
329 /** helper to build a SLANG_OPER_IDENTIFIER node */
330 static void
331 slang_operation_identifier(slang_operation *oper,
332 slang_assemble_ctx *A,
333 const char *name)
334 {
335 oper->type = SLANG_OPER_IDENTIFIER;
336 oper->a_id = slang_atom_pool_atom(A->atoms, name);
337 }
338
339
340 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
341
342 /**
343 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
344 * a vertex or fragment program input variable. Return -1 if the input
345 * name is invalid.
346 * XXX return size too
347 */
348 static GLint
349 _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
350 {
351 struct input_info {
352 const char *Name;
353 GLuint Attrib;
354 GLuint Swizzle;
355 };
356 static const struct input_info vertInputs[] = {
357 { "gl_Vertex", VERT_ATTRIB_POS, SWIZZLE_NOOP },
358 { "gl_Normal", VERT_ATTRIB_NORMAL, SWIZZLE_NOOP },
359 { "gl_Color", VERT_ATTRIB_COLOR0, SWIZZLE_NOOP },
360 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1, SWIZZLE_NOOP },
361 { "gl_FogCoord", VERT_ATTRIB_FOG, SWIZZLE_XXXX },
362 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0, SWIZZLE_NOOP },
363 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1, SWIZZLE_NOOP },
364 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2, SWIZZLE_NOOP },
365 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3, SWIZZLE_NOOP },
366 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4, SWIZZLE_NOOP },
367 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5, SWIZZLE_NOOP },
368 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6, SWIZZLE_NOOP },
369 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7, SWIZZLE_NOOP },
370 { NULL, 0, SWIZZLE_NOOP }
371 };
372 static const struct input_info fragInputs[] = {
373 { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
374 { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
375 { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
376 { "gl_TexCoord", FRAG_ATTRIB_TEX0, SWIZZLE_NOOP },
377 /* note: we're packing several quantities into the fogcoord vector */
378 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
379 { "gl_FrontFacing", FRAG_ATTRIB_FOGC, SWIZZLE_YYYY }, /*XXX*/
380 { "gl_PointCoord", FRAG_ATTRIB_FOGC, SWIZZLE_ZWWW },
381 { NULL, 0, SWIZZLE_NOOP }
382 };
383 GLuint i;
384 const struct input_info *inputs
385 = (target == GL_VERTEX_PROGRAM_ARB) ? vertInputs : fragInputs;
386
387 ASSERT(MAX_TEXTURE_COORD_UNITS == 8); /* if this fails, fix vertInputs above */
388
389 for (i = 0; inputs[i].Name; i++) {
390 if (strcmp(inputs[i].Name, name) == 0) {
391 /* found */
392 *swizzleOut = inputs[i].Swizzle;
393 return inputs[i].Attrib;
394 }
395 }
396 return -1;
397 }
398
399
400 /**
401 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
402 * a vertex or fragment program output variable. Return -1 for an invalid
403 * output name.
404 */
405 static GLint
406 _slang_output_index(const char *name, GLenum target)
407 {
408 struct output_info {
409 const char *Name;
410 GLuint Attrib;
411 };
412 static const struct output_info vertOutputs[] = {
413 { "gl_Position", VERT_RESULT_HPOS },
414 { "gl_FrontColor", VERT_RESULT_COL0 },
415 { "gl_BackColor", VERT_RESULT_BFC0 },
416 { "gl_FrontSecondaryColor", VERT_RESULT_COL1 },
417 { "gl_BackSecondaryColor", VERT_RESULT_BFC1 },
418 { "gl_TexCoord", VERT_RESULT_TEX0 },
419 { "gl_FogFragCoord", VERT_RESULT_FOGC },
420 { "gl_PointSize", VERT_RESULT_PSIZ },
421 { NULL, 0 }
422 };
423 static const struct output_info fragOutputs[] = {
424 { "gl_FragColor", FRAG_RESULT_COLOR },
425 { "gl_FragDepth", FRAG_RESULT_DEPTH },
426 { "gl_FragData", FRAG_RESULT_DATA0 },
427 { NULL, 0 }
428 };
429 GLuint i;
430 const struct output_info *outputs
431 = (target == GL_VERTEX_PROGRAM_ARB) ? vertOutputs : fragOutputs;
432
433 for (i = 0; outputs[i].Name; i++) {
434 if (strcmp(outputs[i].Name, name) == 0) {
435 /* found */
436 return outputs[i].Attrib;
437 }
438 }
439 return -1;
440 }
441
442
443
444 /**********************************************************************/
445
446
447 /**
448 * Map "_asm foo" to IR_FOO, etc.
449 */
450 typedef struct
451 {
452 const char *Name;
453 slang_ir_opcode Opcode;
454 GLuint HaveRetValue, NumParams;
455 } slang_asm_info;
456
457
458 static slang_asm_info AsmInfo[] = {
459 /* vec4 binary op */
460 { "vec4_add", IR_ADD, 1, 2 },
461 { "vec4_subtract", IR_SUB, 1, 2 },
462 { "vec4_multiply", IR_MUL, 1, 2 },
463 { "vec4_dot", IR_DOT4, 1, 2 },
464 { "vec3_dot", IR_DOT3, 1, 2 },
465 { "vec2_dot", IR_DOT2, 1, 2 },
466 { "vec3_nrm", IR_NRM3, 1, 1 },
467 { "vec4_nrm", IR_NRM4, 1, 1 },
468 { "vec3_cross", IR_CROSS, 1, 2 },
469 { "vec4_lrp", IR_LRP, 1, 3 },
470 { "vec4_min", IR_MIN, 1, 2 },
471 { "vec4_max", IR_MAX, 1, 2 },
472 { "vec4_clamp", IR_CLAMP, 1, 3 },
473 { "vec4_seq", IR_SEQUAL, 1, 2 },
474 { "vec4_sne", IR_SNEQUAL, 1, 2 },
475 { "vec4_sge", IR_SGE, 1, 2 },
476 { "vec4_sgt", IR_SGT, 1, 2 },
477 { "vec4_sle", IR_SLE, 1, 2 },
478 { "vec4_slt", IR_SLT, 1, 2 },
479 /* vec4 unary */
480 { "vec4_move", IR_MOVE, 1, 1 },
481 { "vec4_floor", IR_FLOOR, 1, 1 },
482 { "vec4_frac", IR_FRAC, 1, 1 },
483 { "vec4_abs", IR_ABS, 1, 1 },
484 { "vec4_negate", IR_NEG, 1, 1 },
485 { "vec4_ddx", IR_DDX, 1, 1 },
486 { "vec4_ddy", IR_DDY, 1, 1 },
487 /* float binary op */
488 { "float_power", IR_POW, 1, 2 },
489 /* texture / sampler */
490 { "vec4_tex_1d", IR_TEX, 1, 2 },
491 { "vec4_tex_1d_bias", IR_TEXB, 1, 2 }, /* 1d w/ bias */
492 { "vec4_tex_1d_proj", IR_TEXP, 1, 2 }, /* 1d w/ projection */
493 { "vec4_tex_2d", IR_TEX, 1, 2 },
494 { "vec4_tex_2d_bias", IR_TEXB, 1, 2 }, /* 2d w/ bias */
495 { "vec4_tex_2d_proj", IR_TEXP, 1, 2 }, /* 2d w/ projection */
496 { "vec4_tex_3d", IR_TEX, 1, 2 },
497 { "vec4_tex_3d_bias", IR_TEXB, 1, 2 }, /* 3d w/ bias */
498 { "vec4_tex_3d_proj", IR_TEXP, 1, 2 }, /* 3d w/ projection */
499 { "vec4_tex_cube", IR_TEX, 1, 2 }, /* cubemap */
500 { "vec4_tex_rect", IR_TEX, 1, 2 }, /* rectangle */
501 { "vec4_tex_rect_bias", IR_TEX, 1, 2 }, /* rectangle w/ projection */
502
503 /* texture / sampler but with shadow comparison */
504 { "vec4_tex_1d_shadow", IR_TEX_SH, 1, 2 },
505 { "vec4_tex_1d_bias_shadow", IR_TEXB_SH, 1, 2 },
506 { "vec4_tex_1d_proj_shadow", IR_TEXP_SH, 1, 2 },
507 { "vec4_tex_2d_shadow", IR_TEX_SH, 1, 2 },
508 { "vec4_tex_2d_bias_shadow", IR_TEXB_SH, 1, 2 },
509 { "vec4_tex_2d_proj_shadow", IR_TEXP_SH, 1, 2 },
510 { "vec4_tex_rect_shadow", IR_TEX_SH, 1, 2 },
511 { "vec4_tex_rect_proj_shadow", IR_TEXP_SH, 1, 2 },
512
513 /* unary op */
514 { "ivec4_to_vec4", IR_I_TO_F, 1, 1 }, /* int[4] to float[4] */
515 { "vec4_to_ivec4", IR_F_TO_I, 1, 1 }, /* float[4] to int[4] */
516 { "float_exp", IR_EXP, 1, 1 },
517 { "float_exp2", IR_EXP2, 1, 1 },
518 { "float_log2", IR_LOG2, 1, 1 },
519 { "float_rsq", IR_RSQ, 1, 1 },
520 { "float_rcp", IR_RCP, 1, 1 },
521 { "float_sine", IR_SIN, 1, 1 },
522 { "float_cosine", IR_COS, 1, 1 },
523 { "float_noise1", IR_NOISE1, 1, 1},
524 { "float_noise2", IR_NOISE2, 1, 1},
525 { "float_noise3", IR_NOISE3, 1, 1},
526 { "float_noise4", IR_NOISE4, 1, 1},
527
528 { NULL, IR_NOP, 0, 0 }
529 };
530
531
532 static slang_ir_node *
533 new_node3(slang_ir_opcode op,
534 slang_ir_node *c0, slang_ir_node *c1, slang_ir_node *c2)
535 {
536 slang_ir_node *n = (slang_ir_node *) _slang_alloc(sizeof(slang_ir_node));
537 if (n) {
538 n->Opcode = op;
539 n->Children[0] = c0;
540 n->Children[1] = c1;
541 n->Children[2] = c2;
542 n->InstLocation = -1;
543 }
544 return n;
545 }
546
547 static slang_ir_node *
548 new_node2(slang_ir_opcode op, slang_ir_node *c0, slang_ir_node *c1)
549 {
550 return new_node3(op, c0, c1, NULL);
551 }
552
553 static slang_ir_node *
554 new_node1(slang_ir_opcode op, slang_ir_node *c0)
555 {
556 return new_node3(op, c0, NULL, NULL);
557 }
558
559 static slang_ir_node *
560 new_node0(slang_ir_opcode op)
561 {
562 return new_node3(op, NULL, NULL, NULL);
563 }
564
565
566 /**
567 * Create sequence of two nodes.
568 */
569 static slang_ir_node *
570 new_seq(slang_ir_node *left, slang_ir_node *right)
571 {
572 if (!left)
573 return right;
574 if (!right)
575 return left;
576 return new_node2(IR_SEQ, left, right);
577 }
578
579 static slang_ir_node *
580 new_label(slang_label *label)
581 {
582 slang_ir_node *n = new_node0(IR_LABEL);
583 assert(label);
584 if (n)
585 n->Label = label;
586 return n;
587 }
588
589 static slang_ir_node *
590 new_float_literal(const float v[4], GLuint size)
591 {
592 slang_ir_node *n = new_node0(IR_FLOAT);
593 assert(size <= 4);
594 COPY_4V(n->Value, v);
595 /* allocate a storage object, but compute actual location (Index) later */
596 n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
597 return n;
598 }
599
600
601 static slang_ir_node *
602 new_not(slang_ir_node *n)
603 {
604 return new_node1(IR_NOT, n);
605 }
606
607
608 /**
609 * Non-inlined function call.
610 */
611 static slang_ir_node *
612 new_function_call(slang_ir_node *code, slang_label *name)
613 {
614 slang_ir_node *n = new_node1(IR_CALL, code);
615 assert(name);
616 if (n)
617 n->Label = name;
618 return n;
619 }
620
621
622 /**
623 * Unconditional jump.
624 */
625 static slang_ir_node *
626 new_return(slang_label *dest)
627 {
628 slang_ir_node *n = new_node0(IR_RETURN);
629 assert(dest);
630 if (n)
631 n->Label = dest;
632 return n;
633 }
634
635
636 static slang_ir_node *
637 new_loop(slang_ir_node *body)
638 {
639 return new_node1(IR_LOOP, body);
640 }
641
642
643 static slang_ir_node *
644 new_break(slang_ir_node *loopNode)
645 {
646 slang_ir_node *n = new_node0(IR_BREAK);
647 assert(loopNode);
648 assert(loopNode->Opcode == IR_LOOP);
649 if (n) {
650 /* insert this node at head of linked list of cont/break instructions */
651 n->List = loopNode->List;
652 loopNode->List = n;
653 }
654 return n;
655 }
656
657
658 /**
659 * Make new IR_BREAK_IF_TRUE.
660 */
661 static slang_ir_node *
662 new_break_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
663 {
664 slang_ir_node *loopNode = A->CurLoop;
665 slang_ir_node *n;
666 assert(loopNode);
667 assert(loopNode->Opcode == IR_LOOP);
668 n = new_node1(IR_BREAK_IF_TRUE, cond);
669 if (n) {
670 /* insert this node at head of linked list of cont/break instructions */
671 n->List = loopNode->List;
672 loopNode->List = n;
673 }
674 return n;
675 }
676
677
678 /**
679 * Make new IR_CONT_IF_TRUE node.
680 */
681 static slang_ir_node *
682 new_cont_if_true(slang_assemble_ctx *A, slang_ir_node *cond)
683 {
684 slang_ir_node *loopNode = A->CurLoop;
685 slang_ir_node *n;
686 assert(loopNode);
687 assert(loopNode->Opcode == IR_LOOP);
688 n = new_node1(IR_CONT_IF_TRUE, cond);
689 if (n) {
690 n->Parent = loopNode; /* pointer to containing loop */
691 /* insert this node at head of linked list of cont/break instructions */
692 n->List = loopNode->List;
693 loopNode->List = n;
694 }
695 return n;
696 }
697
698
699 static slang_ir_node *
700 new_cond(slang_ir_node *n)
701 {
702 slang_ir_node *c = new_node1(IR_COND, n);
703 return c;
704 }
705
706
707 static slang_ir_node *
708 new_if(slang_ir_node *cond, slang_ir_node *ifPart, slang_ir_node *elsePart)
709 {
710 return new_node3(IR_IF, cond, ifPart, elsePart);
711 }
712
713
714 /**
715 * New IR_VAR node - a reference to a previously declared variable.
716 */
717 static slang_ir_node *
718 new_var(slang_assemble_ctx *A, slang_variable *var)
719 {
720 slang_ir_node *n = new_node0(IR_VAR);
721 if (n) {
722 ASSERT(var);
723 ASSERT(var->store);
724 ASSERT(!n->Store);
725 ASSERT(!n->Var);
726
727 /* Set IR node's Var and Store pointers */
728 n->Var = var;
729 n->Store = var->store;
730 }
731 return n;
732 }
733
734
735 /**
736 * Check if the given function is really just a wrapper for a
737 * basic assembly instruction.
738 */
739 static GLboolean
740 slang_is_asm_function(const slang_function *fun)
741 {
742 if (fun->body->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE &&
743 fun->body->num_children == 1 &&
744 fun->body->children[0].type == SLANG_OPER_ASM) {
745 return GL_TRUE;
746 }
747 return GL_FALSE;
748 }
749
750
751 static GLboolean
752 _slang_is_noop(const slang_operation *oper)
753 {
754 if (!oper ||
755 oper->type == SLANG_OPER_VOID ||
756 (oper->num_children == 1 && oper->children[0].type == SLANG_OPER_VOID))
757 return GL_TRUE;
758 else
759 return GL_FALSE;
760 }
761
762
763 /**
764 * Recursively search tree for a node of the given type.
765 */
766 static slang_operation *
767 _slang_find_node_type(slang_operation *oper, slang_operation_type type)
768 {
769 GLuint i;
770 if (oper->type == type)
771 return oper;
772 for (i = 0; i < oper->num_children; i++) {
773 slang_operation *p = _slang_find_node_type(&oper->children[i], type);
774 if (p)
775 return p;
776 }
777 return NULL;
778 }
779
780
781 /**
782 * Count the number of operations of the given time rooted at 'oper'.
783 */
784 static GLuint
785 _slang_count_node_type(slang_operation *oper, slang_operation_type type)
786 {
787 GLuint i, count = 0;
788 if (oper->type == type) {
789 return 1;
790 }
791 for (i = 0; i < oper->num_children; i++) {
792 count += _slang_count_node_type(&oper->children[i], type);
793 }
794 return count;
795 }
796
797
798 /**
799 * Check if the 'return' statement found under 'oper' is a "tail return"
800 * that can be no-op'd. For example:
801 *
802 * void func(void)
803 * {
804 * .. do something ..
805 * return; // this is a no-op
806 * }
807 *
808 * This is used when determining if a function can be inlined. If the
809 * 'return' is not the last statement, we can't inline the function since
810 * we still need the semantic behaviour of the 'return' but we don't want
811 * to accidentally return from the _calling_ function. We'd need to use an
812 * unconditional branch, but we don't have such a GPU instruction (not
813 * always, at least).
814 */
815 static GLboolean
816 _slang_is_tail_return(const slang_operation *oper)
817 {
818 GLuint k = oper->num_children;
819
820 while (k > 0) {
821 const slang_operation *last = &oper->children[k - 1];
822 if (last->type == SLANG_OPER_RETURN)
823 return GL_TRUE;
824 else if (last->type == SLANG_OPER_IDENTIFIER ||
825 last->type == SLANG_OPER_LABEL)
826 k--; /* try prev child */
827 else if (last->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
828 last->type == SLANG_OPER_BLOCK_NEW_SCOPE)
829 /* try sub-children */
830 return _slang_is_tail_return(last);
831 else
832 break;
833 }
834
835 return GL_FALSE;
836 }
837
838
839 /**
840 * Generate a variable declaration opeartion.
841 * I.e.: generate AST code for "bool flag = false;"
842 */
843 static void
844 slang_generate_declaration(slang_assemble_ctx *A,
845 slang_variable_scope *scope,
846 slang_operation *decl,
847 slang_type_specifier_type type,
848 const char *name,
849 GLint initValue)
850 {
851 slang_variable *var;
852
853 assert(type == SLANG_SPEC_BOOL ||
854 type == SLANG_SPEC_INT);
855
856 decl->type = SLANG_OPER_VARIABLE_DECL;
857
858 var = slang_variable_scope_grow(scope);
859
860 slang_fully_specified_type_construct(&var->type);
861
862 var->type.specifier.type = type;
863 var->a_name = slang_atom_pool_atom(A->atoms, name);
864 decl->a_id = var->a_name;
865 var->initializer = slang_operation_new(1);
866 slang_operation_literal_bool(var->initializer, initValue);
867 }
868
869
870 static void
871 slang_resolve_variable(slang_operation *oper)
872 {
873 if (oper->type == SLANG_OPER_IDENTIFIER && !oper->var) {
874 oper->var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
875 }
876 }
877
878
879 /**
880 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
881 */
882 static void
883 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
884 GLuint substCount, slang_variable **substOld,
885 slang_operation **substNew, GLboolean isLHS)
886 {
887 switch (oper->type) {
888 case SLANG_OPER_VARIABLE_DECL:
889 {
890 slang_variable *v = _slang_variable_locate(oper->locals,
891 oper->a_id, GL_TRUE);
892 assert(v);
893 if (v->initializer && oper->num_children == 0) {
894 /* set child of oper to copy of initializer */
895 oper->num_children = 1;
896 oper->children = slang_operation_new(1);
897 slang_operation_copy(&oper->children[0], v->initializer);
898 }
899 if (oper->num_children == 1) {
900 /* the initializer */
901 slang_substitute(A, &oper->children[0], substCount,
902 substOld, substNew, GL_FALSE);
903 }
904 }
905 break;
906 case SLANG_OPER_IDENTIFIER:
907 assert(oper->num_children == 0);
908 if (1/**!isLHS XXX FIX */) {
909 slang_atom id = oper->a_id;
910 slang_variable *v;
911 GLuint i;
912 v = _slang_variable_locate(oper->locals, id, GL_TRUE);
913 if (!v) {
914 _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
915 return;
916 }
917
918 /* look for a substitution */
919 for (i = 0; i < substCount; i++) {
920 if (v == substOld[i]) {
921 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
922 #if 0 /* DEBUG only */
923 if (substNew[i]->type == SLANG_OPER_IDENTIFIER) {
924 assert(substNew[i]->var);
925 assert(substNew[i]->var->a_name);
926 printf("Substitute %s with %s in id node %p\n",
927 (char*)v->a_name, (char*) substNew[i]->var->a_name,
928 (void*) oper);
929 }
930 else {
931 printf("Substitute %s with %f in id node %p\n",
932 (char*)v->a_name, substNew[i]->literal[0],
933 (void*) oper);
934 }
935 #endif
936 slang_operation_copy(oper, substNew[i]);
937 break;
938 }
939 }
940 }
941 break;
942
943 case SLANG_OPER_RETURN:
944 /* do return replacement here too */
945 assert(oper->num_children == 0 || oper->num_children == 1);
946 if (oper->num_children == 1 && !_slang_is_noop(&oper->children[0])) {
947 /* replace:
948 * return expr;
949 * with:
950 * __retVal = expr;
951 * return;
952 * then do substitutions on the assignment.
953 */
954 slang_operation *blockOper, *assignOper, *returnOper;
955
956 /* check if function actually has a return type */
957 assert(A->CurFunction);
958 if (A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
959 slang_info_log_error(A->log, "illegal return expression");
960 return;
961 }
962
963 blockOper = slang_operation_new(1);
964 blockOper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
965 blockOper->num_children = 2;
966 blockOper->locals->outer_scope = oper->locals->outer_scope;
967 blockOper->children = slang_operation_new(2);
968 assignOper = blockOper->children + 0;
969 returnOper = blockOper->children + 1;
970
971 assignOper->type = SLANG_OPER_ASSIGN;
972 assignOper->num_children = 2;
973 assignOper->locals->outer_scope = blockOper->locals;
974 assignOper->children = slang_operation_new(2);
975 assignOper->children[0].type = SLANG_OPER_IDENTIFIER;
976 assignOper->children[0].a_id = slang_atom_pool_atom(A->atoms, "__retVal");
977 assignOper->children[0].locals->outer_scope = assignOper->locals;
978
979 slang_operation_copy(&assignOper->children[1],
980 &oper->children[0]);
981
982 returnOper->type = SLANG_OPER_RETURN; /* return w/ no value */
983 returnOper->locals->outer_scope = blockOper->locals;
984
985 assert(returnOper->num_children == 0);
986
987 /* do substitutions on the "__retVal = expr" sub-tree */
988 slang_substitute(A, assignOper,
989 substCount, substOld, substNew, GL_FALSE);
990
991 /* install new code */
992 slang_operation_copy(oper, blockOper);
993 slang_operation_destruct(blockOper);
994 }
995 else {
996 /* check if return value was expected */
997 assert(A->CurFunction);
998 if (A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
999 slang_info_log_error(A->log, "return statement requires an expression");
1000 return;
1001 }
1002 }
1003 break;
1004
1005 case SLANG_OPER_ASSIGN:
1006 case SLANG_OPER_SUBSCRIPT:
1007 /* special case:
1008 * child[0] can't have substitutions but child[1] can.
1009 */
1010 slang_substitute(A, &oper->children[0],
1011 substCount, substOld, substNew, GL_TRUE);
1012 slang_substitute(A, &oper->children[1],
1013 substCount, substOld, substNew, GL_FALSE);
1014 break;
1015 case SLANG_OPER_FIELD:
1016 /* XXX NEW - test */
1017 slang_substitute(A, &oper->children[0],
1018 substCount, substOld, substNew, GL_TRUE);
1019 break;
1020 default:
1021 {
1022 GLuint i;
1023 for (i = 0; i < oper->num_children; i++)
1024 slang_substitute(A, &oper->children[i],
1025 substCount, substOld, substNew, GL_FALSE);
1026 }
1027 }
1028 }
1029
1030
1031 /**
1032 * Produce inline code for a call to an assembly instruction.
1033 * This is typically used to compile a call to a built-in function like this:
1034 *
1035 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
1036 * {
1037 * __asm vec4_lrp __retVal, a, y, x;
1038 * }
1039 *
1040 *
1041 * A call to
1042 * r = mix(p1, p2, p3);
1043 *
1044 * Becomes:
1045 *
1046 * mov
1047 * / \
1048 * r vec4_lrp
1049 * / | \
1050 * p3 p2 p1
1051 *
1052 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
1053 */
1054 static slang_operation *
1055 slang_inline_asm_function(slang_assemble_ctx *A,
1056 slang_function *fun, slang_operation *oper)
1057 {
1058 const GLuint numArgs = oper->num_children;
1059 GLuint i;
1060 slang_operation *inlined;
1061 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1062 slang_variable **substOld;
1063 slang_operation **substNew;
1064
1065 ASSERT(slang_is_asm_function(fun));
1066 ASSERT(fun->param_count == numArgs + haveRetValue);
1067
1068 /*
1069 printf("Inline %s as %s\n",
1070 (char*) fun->header.a_name,
1071 (char*) fun->body->children[0].a_id);
1072 */
1073
1074 /*
1075 * We'll substitute formal params with actual args in the asm call.
1076 */
1077 substOld = (slang_variable **)
1078 _slang_alloc(numArgs * sizeof(slang_variable *));
1079 substNew = (slang_operation **)
1080 _slang_alloc(numArgs * sizeof(slang_operation *));
1081 for (i = 0; i < numArgs; i++) {
1082 substOld[i] = fun->parameters->variables[i];
1083 substNew[i] = oper->children + i;
1084 }
1085
1086 /* make a copy of the code to inline */
1087 inlined = slang_operation_new(1);
1088 slang_operation_copy(inlined, &fun->body->children[0]);
1089 if (haveRetValue) {
1090 /* get rid of the __retVal child */
1091 inlined->num_children--;
1092 for (i = 0; i < inlined->num_children; i++) {
1093 inlined->children[i] = inlined->children[i + 1];
1094 }
1095 }
1096
1097 /* now do formal->actual substitutions */
1098 slang_substitute(A, inlined, numArgs, substOld, substNew, GL_FALSE);
1099
1100 _slang_free(substOld);
1101 _slang_free(substNew);
1102
1103 #if 0
1104 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1105 (char *) fun->header.a_name);
1106 slang_print_tree(inlined, 3);
1107 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1108 #endif
1109
1110 return inlined;
1111 }
1112
1113
1114 /**
1115 * Inline the given function call operation.
1116 * Return a new slang_operation that corresponds to the inlined code.
1117 */
1118 static slang_operation *
1119 slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
1120 slang_operation *oper, slang_operation *returnOper)
1121 {
1122 typedef enum {
1123 SUBST = 1,
1124 COPY_IN,
1125 COPY_OUT
1126 } ParamMode;
1127 ParamMode *paramMode;
1128 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1129 const GLuint numArgs = oper->num_children;
1130 const GLuint totalArgs = numArgs + haveRetValue;
1131 slang_operation *args = oper->children;
1132 slang_operation *inlined, *top;
1133 slang_variable **substOld;
1134 slang_operation **substNew;
1135 GLuint substCount, numCopyIn, i;
1136 slang_function *prevFunction;
1137 slang_variable_scope *newScope = NULL;
1138
1139 /* save / push */
1140 prevFunction = A->CurFunction;
1141 A->CurFunction = fun;
1142
1143 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1144 assert(fun->param_count == totalArgs);
1145
1146 /* allocate temporary arrays */
1147 paramMode = (ParamMode *)
1148 _slang_alloc(totalArgs * sizeof(ParamMode));
1149 substOld = (slang_variable **)
1150 _slang_alloc(totalArgs * sizeof(slang_variable *));
1151 substNew = (slang_operation **)
1152 _slang_alloc(totalArgs * sizeof(slang_operation *));
1153
1154 #if 0
1155 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1156 (char *) fun->header.a_name,
1157 fun->parameters->num_variables, numArgs);
1158 #endif
1159
1160 if (haveRetValue && !returnOper) {
1161 /* Create 3-child comma sequence for inlined code:
1162 * child[0]: declare __resultTmp
1163 * child[1]: inlined function body
1164 * child[2]: __resultTmp
1165 */
1166 slang_operation *commaSeq;
1167 slang_operation *declOper = NULL;
1168 slang_variable *resultVar;
1169
1170 commaSeq = slang_operation_new(1);
1171 commaSeq->type = SLANG_OPER_SEQUENCE;
1172 assert(commaSeq->locals);
1173 commaSeq->locals->outer_scope = oper->locals->outer_scope;
1174 commaSeq->num_children = 3;
1175 commaSeq->children = slang_operation_new(3);
1176 /* allocate the return var */
1177 resultVar = slang_variable_scope_grow(commaSeq->locals);
1178 /*
1179 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1180 (void*)commaSeq->locals, (char *) fun->header.a_name);
1181 */
1182
1183 resultVar->a_name = slang_atom_pool_atom(A->atoms, "__resultTmp");
1184 resultVar->type = fun->header.type; /* XXX copy? */
1185 resultVar->isTemp = GL_TRUE;
1186
1187 /* child[0] = __resultTmp declaration */
1188 declOper = &commaSeq->children[0];
1189 declOper->type = SLANG_OPER_VARIABLE_DECL;
1190 declOper->a_id = resultVar->a_name;
1191 declOper->locals->outer_scope = commaSeq->locals;
1192
1193 /* child[1] = function body */
1194 inlined = &commaSeq->children[1];
1195 inlined->locals->outer_scope = commaSeq->locals;
1196
1197 /* child[2] = __resultTmp reference */
1198 returnOper = &commaSeq->children[2];
1199 returnOper->type = SLANG_OPER_IDENTIFIER;
1200 returnOper->a_id = resultVar->a_name;
1201 returnOper->locals->outer_scope = commaSeq->locals;
1202
1203 top = commaSeq;
1204 }
1205 else {
1206 top = inlined = slang_operation_new(1);
1207 /* XXXX this may be inappropriate!!!! */
1208 inlined->locals->outer_scope = oper->locals->outer_scope;
1209 }
1210
1211
1212 assert(inlined->locals);
1213
1214 /* Examine the parameters, look for inout/out params, look for possible
1215 * substitutions, etc:
1216 * param type behaviour
1217 * in copy actual to local
1218 * const in substitute param with actual
1219 * out copy out
1220 */
1221 substCount = 0;
1222 for (i = 0; i < totalArgs; i++) {
1223 slang_variable *p = fun->parameters->variables[i];
1224 /*
1225 printf("Param %d: %s %s \n", i,
1226 slang_type_qual_string(p->type.qualifier),
1227 (char *) p->a_name);
1228 */
1229 if (p->type.qualifier == SLANG_QUAL_INOUT ||
1230 p->type.qualifier == SLANG_QUAL_OUT) {
1231 /* an output param */
1232 slang_operation *arg;
1233 if (i < numArgs)
1234 arg = &args[i];
1235 else
1236 arg = returnOper;
1237 paramMode[i] = SUBST;
1238
1239 if (arg->type == SLANG_OPER_IDENTIFIER)
1240 slang_resolve_variable(arg);
1241
1242 /* replace parameter 'p' with argument 'arg' */
1243 substOld[substCount] = p;
1244 substNew[substCount] = arg; /* will get copied */
1245 substCount++;
1246 }
1247 else if (p->type.qualifier == SLANG_QUAL_CONST) {
1248 /* a constant input param */
1249 if (args[i].type == SLANG_OPER_IDENTIFIER ||
1250 args[i].type == SLANG_OPER_LITERAL_FLOAT) {
1251 /* replace all occurances of this parameter variable with the
1252 * actual argument variable or a literal.
1253 */
1254 paramMode[i] = SUBST;
1255 slang_resolve_variable(&args[i]);
1256 substOld[substCount] = p;
1257 substNew[substCount] = &args[i]; /* will get copied */
1258 substCount++;
1259 }
1260 else {
1261 paramMode[i] = COPY_IN;
1262 }
1263 }
1264 else {
1265 paramMode[i] = COPY_IN;
1266 }
1267 assert(paramMode[i]);
1268 }
1269
1270 /* actual code inlining: */
1271 slang_operation_copy(inlined, fun->body);
1272
1273 /*** XXX review this */
1274 assert(inlined->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
1275 inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE);
1276 inlined->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1277
1278 #if 0
1279 printf("======================= orig body code ======================\n");
1280 printf("=== params scope = %p\n", (void*) fun->parameters);
1281 slang_print_tree(fun->body, 8);
1282 printf("======================= copied code =========================\n");
1283 slang_print_tree(inlined, 8);
1284 #endif
1285
1286 /* do parameter substitution in inlined code: */
1287 slang_substitute(A, inlined, substCount, substOld, substNew, GL_FALSE);
1288
1289 #if 0
1290 printf("======================= subst code ==========================\n");
1291 slang_print_tree(inlined, 8);
1292 printf("=============================================================\n");
1293 #endif
1294
1295 /* New prolog statements: (inserted before the inlined code)
1296 * Copy the 'in' arguments.
1297 */
1298 numCopyIn = 0;
1299 for (i = 0; i < numArgs; i++) {
1300 if (paramMode[i] == COPY_IN) {
1301 slang_variable *p = fun->parameters->variables[i];
1302 /* declare parameter 'p' */
1303 slang_operation *decl = slang_operation_insert(&inlined->num_children,
1304 &inlined->children,
1305 numCopyIn);
1306
1307 decl->type = SLANG_OPER_VARIABLE_DECL;
1308 assert(decl->locals);
1309 decl->locals->outer_scope = inlined->locals;
1310 decl->a_id = p->a_name;
1311 decl->num_children = 1;
1312 decl->children = slang_operation_new(1);
1313
1314 /* child[0] is the var's initializer */
1315 slang_operation_copy(&decl->children[0], args + i);
1316
1317 /* add parameter 'p' to the local variable scope here */
1318 {
1319 slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
1320 pCopy->type = p->type;
1321 pCopy->a_name = p->a_name;
1322 pCopy->array_len = p->array_len;
1323 }
1324
1325 newScope = inlined->locals;
1326 numCopyIn++;
1327 }
1328 }
1329
1330 /* Now add copies of the function's local vars to the new variable scope */
1331 for (i = totalArgs; i < fun->parameters->num_variables; i++) {
1332 slang_variable *p = fun->parameters->variables[i];
1333 slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
1334 pCopy->type = p->type;
1335 pCopy->a_name = p->a_name;
1336 pCopy->array_len = p->array_len;
1337 }
1338
1339
1340 /* New epilog statements:
1341 * 1. Create end of function label to jump to from return statements.
1342 * 2. Copy the 'out' parameter vars
1343 */
1344 {
1345 slang_operation *lab = slang_operation_insert(&inlined->num_children,
1346 &inlined->children,
1347 inlined->num_children);
1348 lab->type = SLANG_OPER_LABEL;
1349 lab->label = A->curFuncEndLabel;
1350 }
1351
1352 for (i = 0; i < totalArgs; i++) {
1353 if (paramMode[i] == COPY_OUT) {
1354 const slang_variable *p = fun->parameters->variables[i];
1355 /* actualCallVar = outParam */
1356 /*if (i > 0 || !haveRetValue)*/
1357 slang_operation *ass = slang_operation_insert(&inlined->num_children,
1358 &inlined->children,
1359 inlined->num_children);
1360 ass->type = SLANG_OPER_ASSIGN;
1361 ass->num_children = 2;
1362 ass->locals->outer_scope = inlined->locals;
1363 ass->children = slang_operation_new(2);
1364 ass->children[0] = args[i]; /*XXX copy */
1365 ass->children[1].type = SLANG_OPER_IDENTIFIER;
1366 ass->children[1].a_id = p->a_name;
1367 ass->children[1].locals->outer_scope = ass->locals;
1368 }
1369 }
1370
1371 _slang_free(paramMode);
1372 _slang_free(substOld);
1373 _slang_free(substNew);
1374
1375 /* Update scoping to use the new local vars instead of the
1376 * original function's vars. This is especially important
1377 * for nested inlining.
1378 */
1379 if (newScope)
1380 slang_replace_scope(inlined, fun->parameters, newScope);
1381
1382 #if 0
1383 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1384 (char *) fun->header.a_name,
1385 fun->parameters->num_variables, numArgs);
1386 slang_print_tree(top, 0);
1387 #endif
1388
1389 /* pop */
1390 A->CurFunction = prevFunction;
1391
1392 return top;
1393 }
1394
1395
1396 /**
1397 * Insert declaration for "bool __returnFlag" in given block operation.
1398 * This is used when we can't emit "early" return statements in subroutines.
1399 */
1400 static void
1401 declare_return_flag(slang_assemble_ctx *A, slang_operation *oper)
1402 {
1403 slang_operation *decl;
1404
1405 assert(oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1406 oper->type == SLANG_OPER_SEQUENCE);
1407
1408 decl = slang_operation_insert_child(oper, 1);
1409
1410 slang_generate_declaration(A, oper->locals, decl,
1411 SLANG_SPEC_BOOL, "__returnFlag", GL_FALSE);
1412
1413 slang_print_tree(oper, 0);
1414 }
1415
1416
1417 /**
1418 * Replace 'return' with '__returnFlag = true'.
1419 * This is used to remove 'early returns' from functions.
1420 */
1421 static void
1422 replace_return_with_flag_set(slang_assemble_ctx *A, slang_operation *oper)
1423 {
1424 slang_atom id = slang_atom_pool_atom(A->atoms, "__returnFlag");
1425 assert(oper->type == SLANG_OPER_RETURN);
1426
1427 /* replace 'return' with __returnFlag = true' */
1428 slang_operation_free_children(oper);
1429 oper->type = SLANG_OPER_ASSIGN;
1430 slang_operation_add_children(oper, 2);
1431 {
1432 slang_operation *lhs = slang_oper_child(oper, 0);
1433 lhs->type = SLANG_OPER_IDENTIFIER;
1434 lhs->a_id = id;
1435 }
1436 {
1437 slang_operation *rhs = slang_oper_child(oper, 1);
1438 slang_operation_literal_bool(rhs, GL_TRUE);
1439 }
1440
1441 {
1442 slang_variable *var;
1443 var = _slang_variable_locate(oper->locals, id, GL_TRUE);
1444 assert(var);
1445 }
1446
1447 }
1448
1449
1450 static slang_ir_node *
1451 _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
1452 slang_operation *oper, slang_operation *dest)
1453 {
1454 slang_ir_node *n;
1455 slang_operation *inlined;
1456 slang_label *prevFuncEndLabel;
1457 char name[200];
1458
1459 prevFuncEndLabel = A->curFuncEndLabel;
1460 sprintf(name, "__endOfFunc_%s_", (char *) fun->header.a_name);
1461 A->curFuncEndLabel = _slang_label_new(name);
1462 assert(A->curFuncEndLabel);
1463
1464 if (slang_is_asm_function(fun) && !dest) {
1465 /* assemble assembly function - tree style */
1466 inlined = slang_inline_asm_function(A, fun, oper);
1467 }
1468 else {
1469 /* non-assembly function */
1470 /* We always generate an "inline-able" block of code here.
1471 * We may either:
1472 * 1. insert the inline code
1473 * 2. Generate a call to the "inline" code as a subroutine
1474 */
1475
1476
1477 slang_operation *ret = NULL;
1478
1479 inlined = slang_inline_function_call(A, fun, oper, dest);
1480 if (!inlined)
1481 return NULL;
1482
1483 ret = _slang_find_node_type(inlined, SLANG_OPER_RETURN);
1484 if (ret) {
1485 /* check if this is a "tail" return */
1486 if (_slang_count_node_type(inlined, SLANG_OPER_RETURN) == 1 &&
1487 _slang_is_tail_return(inlined)) {
1488 /* The only RETURN is the last stmt in the function, no-op it
1489 * and inline the function body.
1490 */
1491 ret->type = SLANG_OPER_NONE;
1492 }
1493 else {
1494 slang_operation *callOper;
1495 /* The function we're calling has one or more 'return' statements.
1496 * So, we can't truly inline this function because we need to
1497 * implement 'return' with RET (and CAL).
1498 * Nevertheless, we performed "inlining" to make a new instance
1499 * of the function body to deal with static register allocation.
1500 */
1501 assert(inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1502 inlined->type == SLANG_OPER_SEQUENCE);
1503
1504 if (_slang_function_has_return_value(fun) && !dest) {
1505 assert(inlined->children[0].type == SLANG_OPER_VARIABLE_DECL);
1506 assert(inlined->children[2].type == SLANG_OPER_IDENTIFIER);
1507 callOper = &inlined->children[1];
1508 }
1509 else {
1510 callOper = inlined;
1511 }
1512
1513 if (!A->EmitContReturn) {
1514 /* Early returns not supported. Create a _returnFlag variable
1515 * that's set upon 'return' and tested elsewhere to no-op any
1516 * remaining instructions in the subroutine.
1517 */
1518 assert(callOper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1519 callOper->type == SLANG_OPER_SEQUENCE);
1520 declare_return_flag(A, callOper);
1521 }
1522 callOper->type = SLANG_OPER_NON_INLINED_CALL;
1523 callOper->fun = fun;
1524 callOper->label = _slang_label_new_unique((char*) fun->header.a_name);
1525 }
1526 }
1527 }
1528
1529 if (!inlined)
1530 return NULL;
1531
1532 /* Replace the function call with the inlined block (or new CALL stmt) */
1533 slang_operation_destruct(oper);
1534 *oper = *inlined;
1535 _slang_free(inlined);
1536
1537 #if 0
1538 assert(inlined->locals);
1539 printf("*** Inlined code for call to %s:\n",
1540 (char*) fun->header.a_name);
1541 slang_print_tree(oper, 10);
1542 printf("\n");
1543 #endif
1544
1545 n = _slang_gen_operation(A, oper);
1546
1547 /*_slang_label_delete(A->curFuncEndLabel);*/
1548 A->curFuncEndLabel = prevFuncEndLabel;
1549
1550 if (A->pragmas->Debug) {
1551 char s[1000];
1552 _mesa_snprintf(s, sizeof(s), "Call/inline %s()", (char *) fun->header.a_name);
1553 n->Comment = _slang_strdup(s);
1554 }
1555
1556 return n;
1557 }
1558
1559
1560 static slang_asm_info *
1561 slang_find_asm_info(const char *name)
1562 {
1563 GLuint i;
1564 for (i = 0; AsmInfo[i].Name; i++) {
1565 if (_mesa_strcmp(AsmInfo[i].Name, name) == 0) {
1566 return AsmInfo + i;
1567 }
1568 }
1569 return NULL;
1570 }
1571
1572
1573 /**
1574 * Some write-masked assignments are simple, but others are hard.
1575 * Simple example:
1576 * vec3 v;
1577 * v.xy = vec2(a, b);
1578 * Hard example:
1579 * vec3 v;
1580 * v.zy = vec2(a, b);
1581 * this gets transformed/swizzled into:
1582 * v.zy = vec2(a, b).*yx* (* = don't care)
1583 * This function helps to determine simple vs. non-simple.
1584 */
1585 static GLboolean
1586 _slang_simple_writemask(GLuint writemask, GLuint swizzle)
1587 {
1588 switch (writemask) {
1589 case WRITEMASK_X:
1590 return GET_SWZ(swizzle, 0) == SWIZZLE_X;
1591 case WRITEMASK_Y:
1592 return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
1593 case WRITEMASK_Z:
1594 return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
1595 case WRITEMASK_W:
1596 return GET_SWZ(swizzle, 3) == SWIZZLE_W;
1597 case WRITEMASK_XY:
1598 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1599 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
1600 case WRITEMASK_XYZ:
1601 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1602 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y)
1603 && (GET_SWZ(swizzle, 2) == SWIZZLE_Z);
1604 case WRITEMASK_XYZW:
1605 return swizzle == SWIZZLE_NOOP;
1606 default:
1607 return GL_FALSE;
1608 }
1609 }
1610
1611
1612 /**
1613 * Convert the given swizzle into a writemask. In some cases this
1614 * is trivial, in other cases, we'll need to also swizzle the right
1615 * hand side to put components in the right places.
1616 * See comment above for more info.
1617 * XXX this function could be simplified and should probably be renamed.
1618 * \param swizzle the incoming swizzle
1619 * \param writemaskOut returns the writemask
1620 * \param swizzleOut swizzle to apply to the right-hand-side
1621 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1622 */
1623 static GLboolean
1624 swizzle_to_writemask(slang_assemble_ctx *A, GLuint swizzle,
1625 GLuint *writemaskOut, GLuint *swizzleOut)
1626 {
1627 GLuint mask = 0x0, newSwizzle[4];
1628 GLint i, size;
1629
1630 /* make new dst writemask, compute size */
1631 for (i = 0; i < 4; i++) {
1632 const GLuint swz = GET_SWZ(swizzle, i);
1633 if (swz == SWIZZLE_NIL) {
1634 /* end */
1635 break;
1636 }
1637 assert(swz >= 0 && swz <= 3);
1638
1639 if (swizzle != SWIZZLE_XXXX &&
1640 swizzle != SWIZZLE_YYYY &&
1641 swizzle != SWIZZLE_ZZZZ &&
1642 swizzle != SWIZZLE_WWWW &&
1643 (mask & (1 << swz))) {
1644 /* a channel can't be specified twice (ex: ".xyyz") */
1645 slang_info_log_error(A->log, "Invalid writemask '%s'",
1646 _mesa_swizzle_string(swizzle, 0, 0));
1647 return GL_FALSE;
1648 }
1649
1650 mask |= (1 << swz);
1651 }
1652 assert(mask <= 0xf);
1653 size = i; /* number of components in mask/swizzle */
1654
1655 *writemaskOut = mask;
1656
1657 /* make new src swizzle, by inversion */
1658 for (i = 0; i < 4; i++) {
1659 newSwizzle[i] = i; /*identity*/
1660 }
1661 for (i = 0; i < size; i++) {
1662 const GLuint swz = GET_SWZ(swizzle, i);
1663 newSwizzle[swz] = i;
1664 }
1665 *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
1666 newSwizzle[1],
1667 newSwizzle[2],
1668 newSwizzle[3]);
1669
1670 if (_slang_simple_writemask(mask, *swizzleOut)) {
1671 if (size >= 1)
1672 assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
1673 if (size >= 2)
1674 assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
1675 if (size >= 3)
1676 assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
1677 if (size >= 4)
1678 assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
1679 return GL_TRUE;
1680 }
1681 else
1682 return GL_FALSE;
1683 }
1684
1685
1686 #if 0 /* not used, but don't remove just yet */
1687 /**
1688 * Recursively traverse 'oper' to produce a swizzle mask in the event
1689 * of any vector subscripts and swizzle suffixes.
1690 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1691 */
1692 static GLuint
1693 resolve_swizzle(const slang_operation *oper)
1694 {
1695 if (oper->type == SLANG_OPER_FIELD) {
1696 /* writemask from .xyzw suffix */
1697 slang_swizzle swz;
1698 if (_slang_is_swizzle((char*) oper->a_id, 4, &swz)) {
1699 GLuint swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
1700 swz.swizzle[1],
1701 swz.swizzle[2],
1702 swz.swizzle[3]);
1703 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1704 GLuint s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1705 return s;
1706 }
1707 else
1708 return SWIZZLE_XYZW;
1709 }
1710 else if (oper->type == SLANG_OPER_SUBSCRIPT &&
1711 oper->children[1].type == SLANG_OPER_LITERAL_INT) {
1712 /* writemask from [index] */
1713 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1714 GLuint i = (GLuint) oper->children[1].literal[0];
1715 GLuint swizzle;
1716 GLuint s;
1717 switch (i) {
1718 case 0:
1719 swizzle = SWIZZLE_XXXX;
1720 break;
1721 case 1:
1722 swizzle = SWIZZLE_YYYY;
1723 break;
1724 case 2:
1725 swizzle = SWIZZLE_ZZZZ;
1726 break;
1727 case 3:
1728 swizzle = SWIZZLE_WWWW;
1729 break;
1730 default:
1731 swizzle = SWIZZLE_XYZW;
1732 }
1733 s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1734 return s;
1735 }
1736 else {
1737 return SWIZZLE_XYZW;
1738 }
1739 }
1740 #endif
1741
1742
1743 #if 0
1744 /**
1745 * Recursively descend through swizzle nodes to find the node's storage info.
1746 */
1747 static slang_ir_storage *
1748 get_store(const slang_ir_node *n)
1749 {
1750 if (n->Opcode == IR_SWIZZLE) {
1751 return get_store(n->Children[0]);
1752 }
1753 return n->Store;
1754 }
1755 #endif
1756
1757
1758 /**
1759 * Generate IR tree for an asm instruction/operation such as:
1760 * __asm vec4_dot __retVal.x, v1, v2;
1761 */
1762 static slang_ir_node *
1763 _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
1764 slang_operation *dest)
1765 {
1766 const slang_asm_info *info;
1767 slang_ir_node *kids[3], *n;
1768 GLuint j, firstOperand;
1769
1770 assert(oper->type == SLANG_OPER_ASM);
1771
1772 info = slang_find_asm_info((char *) oper->a_id);
1773 if (!info) {
1774 _mesa_problem(NULL, "undefined __asm function %s\n",
1775 (char *) oper->a_id);
1776 assert(info);
1777 }
1778 assert(info->NumParams <= 3);
1779
1780 if (info->NumParams == oper->num_children) {
1781 /* Storage for result is not specified.
1782 * Children[0], [1], [2] are the operands.
1783 */
1784 firstOperand = 0;
1785 }
1786 else {
1787 /* Storage for result (child[0]) is specified.
1788 * Children[1], [2], [3] are the operands.
1789 */
1790 firstOperand = 1;
1791 }
1792
1793 /* assemble child(ren) */
1794 kids[0] = kids[1] = kids[2] = NULL;
1795 for (j = 0; j < info->NumParams; j++) {
1796 kids[j] = _slang_gen_operation(A, &oper->children[firstOperand + j]);
1797 if (!kids[j])
1798 return NULL;
1799 }
1800
1801 n = new_node3(info->Opcode, kids[0], kids[1], kids[2]);
1802
1803 if (firstOperand) {
1804 /* Setup n->Store to be a particular location. Otherwise, storage
1805 * for the result (a temporary) will be allocated later.
1806 */
1807 slang_operation *dest_oper;
1808 slang_ir_node *n0;
1809
1810 dest_oper = &oper->children[0];
1811
1812 n0 = _slang_gen_operation(A, dest_oper);
1813 if (!n0)
1814 return NULL;
1815
1816 assert(!n->Store);
1817 n->Store = n0->Store;
1818
1819 assert(n->Store->File != PROGRAM_UNDEFINED || n->Store->Parent);
1820
1821 _slang_free(n0);
1822 }
1823
1824 return n;
1825 }
1826
1827
1828 #if 0
1829 static void
1830 print_funcs(struct slang_function_scope_ *scope, const char *name)
1831 {
1832 GLuint i;
1833 for (i = 0; i < scope->num_functions; i++) {
1834 slang_function *f = &scope->functions[i];
1835 if (!name || strcmp(name, (char*) f->header.a_name) == 0)
1836 printf(" %s (%d args)\n", name, f->param_count);
1837
1838 }
1839 if (scope->outer_scope)
1840 print_funcs(scope->outer_scope, name);
1841 }
1842 #endif
1843
1844
1845 /**
1846 * Find a function of the given name, taking 'numArgs' arguments.
1847 * This is the function we'll try to call when there is no exact match
1848 * between function parameters and call arguments.
1849 *
1850 * XXX we should really create a list of candidate functions and try
1851 * all of them...
1852 */
1853 static slang_function *
1854 _slang_find_function_by_argc(slang_function_scope *scope,
1855 const char *name, int numArgs)
1856 {
1857 while (scope) {
1858 GLuint i;
1859 for (i = 0; i < scope->num_functions; i++) {
1860 slang_function *f = &scope->functions[i];
1861 if (strcmp(name, (char*) f->header.a_name) == 0) {
1862 int haveRetValue = _slang_function_has_return_value(f);
1863 if (numArgs == f->param_count - haveRetValue)
1864 return f;
1865 }
1866 }
1867 scope = scope->outer_scope;
1868 }
1869
1870 return NULL;
1871 }
1872
1873
1874 static slang_function *
1875 _slang_find_function_by_max_argc(slang_function_scope *scope,
1876 const char *name)
1877 {
1878 slang_function *maxFunc = NULL;
1879 GLuint maxArgs = 0;
1880
1881 while (scope) {
1882 GLuint i;
1883 for (i = 0; i < scope->num_functions; i++) {
1884 slang_function *f = &scope->functions[i];
1885 if (strcmp(name, (char*) f->header.a_name) == 0) {
1886 if (f->param_count > maxArgs) {
1887 maxArgs = f->param_count;
1888 maxFunc = f;
1889 }
1890 }
1891 }
1892 scope = scope->outer_scope;
1893 }
1894
1895 return maxFunc;
1896 }
1897
1898
1899 /**
1900 * Generate a new slang_function which is a constructor for a user-defined
1901 * struct type.
1902 */
1903 static slang_function *
1904 _slang_make_struct_constructor(slang_assemble_ctx *A, slang_struct *str)
1905 {
1906 const GLint numFields = str->fields->num_variables;
1907 slang_function *fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
1908
1909 /* function header (name, return type) */
1910 fun->header.a_name = str->a_name;
1911 fun->header.type.qualifier = SLANG_QUAL_NONE;
1912 fun->header.type.specifier.type = SLANG_SPEC_STRUCT;
1913 fun->header.type.specifier._struct = str;
1914
1915 /* function parameters (= struct's fields) */
1916 {
1917 GLint i;
1918 for (i = 0; i < numFields; i++) {
1919 /*
1920 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1921 */
1922 slang_variable *p = slang_variable_scope_grow(fun->parameters);
1923 *p = *str->fields->variables[i]; /* copy the variable and type */
1924 p->type.qualifier = SLANG_QUAL_CONST;
1925 }
1926 fun->param_count = fun->parameters->num_variables;
1927 }
1928
1929 /* Add __retVal to params */
1930 {
1931 slang_variable *p = slang_variable_scope_grow(fun->parameters);
1932 slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
1933 assert(a_retVal);
1934 p->a_name = a_retVal;
1935 p->type = fun->header.type;
1936 p->type.qualifier = SLANG_QUAL_OUT;
1937 fun->param_count++;
1938 }
1939
1940 /* function body is:
1941 * block:
1942 * declare T;
1943 * T.f1 = p1;
1944 * T.f2 = p2;
1945 * ...
1946 * T.fn = pn;
1947 * return T;
1948 */
1949 {
1950 slang_variable_scope *scope;
1951 slang_variable *var;
1952 GLint i;
1953
1954 fun->body = slang_operation_new(1);
1955 fun->body->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1956 fun->body->num_children = numFields + 2;
1957 fun->body->children = slang_operation_new(numFields + 2);
1958
1959 scope = fun->body->locals;
1960 scope->outer_scope = fun->parameters;
1961
1962 /* create local var 't' */
1963 var = slang_variable_scope_grow(scope);
1964 var->a_name = slang_atom_pool_atom(A->atoms, "t");
1965 var->type = fun->header.type;
1966
1967 /* declare t */
1968 {
1969 slang_operation *decl;
1970
1971 decl = &fun->body->children[0];
1972 decl->type = SLANG_OPER_VARIABLE_DECL;
1973 decl->locals = _slang_variable_scope_new(scope);
1974 decl->a_id = var->a_name;
1975 }
1976
1977 /* assign params to fields of t */
1978 for (i = 0; i < numFields; i++) {
1979 slang_operation *assign = &fun->body->children[1 + i];
1980
1981 assign->type = SLANG_OPER_ASSIGN;
1982 assign->locals = _slang_variable_scope_new(scope);
1983 assign->num_children = 2;
1984 assign->children = slang_operation_new(2);
1985
1986 {
1987 slang_operation *lhs = &assign->children[0];
1988
1989 lhs->type = SLANG_OPER_FIELD;
1990 lhs->locals = _slang_variable_scope_new(scope);
1991 lhs->num_children = 1;
1992 lhs->children = slang_operation_new(1);
1993 lhs->a_id = str->fields->variables[i]->a_name;
1994
1995 lhs->children[0].type = SLANG_OPER_IDENTIFIER;
1996 lhs->children[0].a_id = var->a_name;
1997 lhs->children[0].locals = _slang_variable_scope_new(scope);
1998
1999 #if 0
2000 lhs->children[1].num_children = 1;
2001 lhs->children[1].children = slang_operation_new(1);
2002 lhs->children[1].children[0].type = SLANG_OPER_IDENTIFIER;
2003 lhs->children[1].children[0].a_id = str->fields->variables[i]->a_name;
2004 lhs->children[1].children->locals = _slang_variable_scope_new(scope);
2005 #endif
2006 }
2007
2008 {
2009 slang_operation *rhs = &assign->children[1];
2010
2011 rhs->type = SLANG_OPER_IDENTIFIER;
2012 rhs->locals = _slang_variable_scope_new(scope);
2013 rhs->a_id = str->fields->variables[i]->a_name;
2014 }
2015 }
2016
2017 /* return t; */
2018 {
2019 slang_operation *ret = &fun->body->children[numFields + 1];
2020
2021 ret->type = SLANG_OPER_RETURN;
2022 ret->locals = _slang_variable_scope_new(scope);
2023 ret->num_children = 1;
2024 ret->children = slang_operation_new(1);
2025 ret->children[0].type = SLANG_OPER_IDENTIFIER;
2026 ret->children[0].a_id = var->a_name;
2027 ret->children[0].locals = _slang_variable_scope_new(scope);
2028 }
2029 }
2030 /*
2031 slang_print_function(fun, 1);
2032 */
2033 return fun;
2034 }
2035
2036
2037 /**
2038 * Find/create a function (constructor) for the given structure name.
2039 */
2040 static slang_function *
2041 _slang_locate_struct_constructor(slang_assemble_ctx *A, const char *name)
2042 {
2043 unsigned int i;
2044 for (i = 0; i < A->space.structs->num_structs; i++) {
2045 slang_struct *str = &A->space.structs->structs[i];
2046 if (strcmp(name, (const char *) str->a_name) == 0) {
2047 /* found a structure type that matches the function name */
2048 if (!str->constructor) {
2049 /* create the constructor function now */
2050 str->constructor = _slang_make_struct_constructor(A, str);
2051 }
2052 return str->constructor;
2053 }
2054 }
2055 return NULL;
2056 }
2057
2058
2059 /**
2060 * Generate a new slang_function to satisfy a call to an array constructor.
2061 * Ex: float[3](1., 2., 3.)
2062 */
2063 static slang_function *
2064 _slang_make_array_constructor(slang_assemble_ctx *A, slang_operation *oper)
2065 {
2066 slang_type_specifier_type baseType;
2067 slang_function *fun;
2068 int num_elements;
2069
2070 fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
2071 if (!fun)
2072 return NULL;
2073
2074 baseType = slang_type_specifier_type_from_string((char *) oper->a_id);
2075
2076 num_elements = oper->num_children;
2077
2078 /* function header, return type */
2079 {
2080 fun->header.a_name = oper->a_id;
2081 fun->header.type.qualifier = SLANG_QUAL_NONE;
2082 fun->header.type.specifier.type = SLANG_SPEC_ARRAY;
2083 fun->header.type.specifier._array =
2084 slang_type_specifier_new(baseType, NULL, NULL);
2085 fun->header.type.array_len = num_elements;
2086 }
2087
2088 /* function parameters (= number of elements) */
2089 {
2090 GLint i;
2091 for (i = 0; i < num_elements; i++) {
2092 /*
2093 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
2094 */
2095 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2096 char name[10];
2097 _mesa_snprintf(name, sizeof(name), "p%d", i);
2098 p->a_name = slang_atom_pool_atom(A->atoms, name);
2099 p->type.qualifier = SLANG_QUAL_CONST;
2100 p->type.specifier.type = baseType;
2101 }
2102 fun->param_count = fun->parameters->num_variables;
2103 }
2104
2105 /* Add __retVal to params */
2106 {
2107 slang_variable *p = slang_variable_scope_grow(fun->parameters);
2108 slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2109 assert(a_retVal);
2110 p->a_name = a_retVal;
2111 p->type = fun->header.type;
2112 p->type.qualifier = SLANG_QUAL_OUT;
2113 p->type.specifier.type = baseType;
2114 fun->param_count++;
2115 }
2116
2117 /* function body is:
2118 * block:
2119 * declare T;
2120 * T[0] = p0;
2121 * T[1] = p1;
2122 * ...
2123 * T[n] = pn;
2124 * return T;
2125 */
2126 {
2127 slang_variable_scope *scope;
2128 slang_variable *var;
2129 GLint i;
2130
2131 fun->body = slang_operation_new(1);
2132 fun->body->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2133 fun->body->num_children = num_elements + 2;
2134 fun->body->children = slang_operation_new(num_elements + 2);
2135
2136 scope = fun->body->locals;
2137 scope->outer_scope = fun->parameters;
2138
2139 /* create local var 't' */
2140 var = slang_variable_scope_grow(scope);
2141 var->a_name = slang_atom_pool_atom(A->atoms, "ttt");
2142 var->type = fun->header.type;/*XXX copy*/
2143
2144 /* declare t */
2145 {
2146 slang_operation *decl;
2147
2148 decl = &fun->body->children[0];
2149 decl->type = SLANG_OPER_VARIABLE_DECL;
2150 decl->locals = _slang_variable_scope_new(scope);
2151 decl->a_id = var->a_name;
2152 }
2153
2154 /* assign params to elements of t */
2155 for (i = 0; i < num_elements; i++) {
2156 slang_operation *assign = &fun->body->children[1 + i];
2157
2158 assign->type = SLANG_OPER_ASSIGN;
2159 assign->locals = _slang_variable_scope_new(scope);
2160 assign->num_children = 2;
2161 assign->children = slang_operation_new(2);
2162
2163 {
2164 slang_operation *lhs = &assign->children[0];
2165
2166 lhs->type = SLANG_OPER_SUBSCRIPT;
2167 lhs->locals = _slang_variable_scope_new(scope);
2168 lhs->num_children = 2;
2169 lhs->children = slang_operation_new(2);
2170
2171 lhs->children[0].type = SLANG_OPER_IDENTIFIER;
2172 lhs->children[0].a_id = var->a_name;
2173 lhs->children[0].locals = _slang_variable_scope_new(scope);
2174
2175 lhs->children[1].type = SLANG_OPER_LITERAL_INT;
2176 lhs->children[1].literal[0] = (GLfloat) i;
2177 }
2178
2179 {
2180 slang_operation *rhs = &assign->children[1];
2181
2182 rhs->type = SLANG_OPER_IDENTIFIER;
2183 rhs->locals = _slang_variable_scope_new(scope);
2184 rhs->a_id = fun->parameters->variables[i]->a_name;
2185 }
2186 }
2187
2188 /* return t; */
2189 {
2190 slang_operation *ret = &fun->body->children[num_elements + 1];
2191
2192 ret->type = SLANG_OPER_RETURN;
2193 ret->locals = _slang_variable_scope_new(scope);
2194 ret->num_children = 1;
2195 ret->children = slang_operation_new(1);
2196 ret->children[0].type = SLANG_OPER_IDENTIFIER;
2197 ret->children[0].a_id = var->a_name;
2198 ret->children[0].locals = _slang_variable_scope_new(scope);
2199 }
2200 }
2201
2202 /*
2203 slang_print_function(fun, 1);
2204 */
2205
2206 return fun;
2207 }
2208
2209
2210 static GLboolean
2211 _slang_is_vec_mat_type(const char *name)
2212 {
2213 static const char *vecmat_types[] = {
2214 "float", "int", "bool",
2215 "vec2", "vec3", "vec4",
2216 "ivec2", "ivec3", "ivec4",
2217 "bvec2", "bvec3", "bvec4",
2218 "mat2", "mat3", "mat4",
2219 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2220 NULL
2221 };
2222 int i;
2223 for (i = 0; vecmat_types[i]; i++)
2224 if (_mesa_strcmp(name, vecmat_types[i]) == 0)
2225 return GL_TRUE;
2226 return GL_FALSE;
2227 }
2228
2229
2230 /**
2231 * Assemble a function call, given a particular function name.
2232 * \param name the function's name (operators like '*' are possible).
2233 */
2234 static slang_ir_node *
2235 _slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
2236 slang_operation *oper, slang_operation *dest)
2237 {
2238 slang_operation *params = oper->children;
2239 const GLuint param_count = oper->num_children;
2240 slang_atom atom;
2241 slang_function *fun;
2242 slang_ir_node *n;
2243
2244 atom = slang_atom_pool_atom(A->atoms, name);
2245 if (atom == SLANG_ATOM_NULL)
2246 return NULL;
2247
2248 if (oper->array_constructor) {
2249 /* this needs special handling */
2250 fun = _slang_make_array_constructor(A, oper);
2251 }
2252 else {
2253 /* Try to find function by name and exact argument type matching */
2254 GLboolean error = GL_FALSE;
2255 fun = _slang_function_locate(A->space.funcs, atom, params, param_count,
2256 &A->space, A->atoms, A->log, &error);
2257 if (error) {
2258 slang_info_log_error(A->log,
2259 "Function '%s' not found (check argument types)",
2260 name);
2261 return NULL;
2262 }
2263 }
2264
2265 if (!fun) {
2266 /* Next, try locating a constructor function for a user-defined type */
2267 fun = _slang_locate_struct_constructor(A, name);
2268 }
2269
2270 /*
2271 * At this point, some heuristics are used to try to find a function
2272 * that matches the calling signature by means of casting or "unrolling"
2273 * of constructors.
2274 */
2275
2276 if (!fun && _slang_is_vec_mat_type(name)) {
2277 /* Next, if this call looks like a vec() or mat() constructor call,
2278 * try "unwinding" the args to satisfy a constructor.
2279 */
2280 fun = _slang_find_function_by_max_argc(A->space.funcs, name);
2281 if (fun) {
2282 if (!_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
2283 slang_info_log_error(A->log,
2284 "Function '%s' not found (check argument types)",
2285 name);
2286 return NULL;
2287 }
2288 }
2289 }
2290
2291 if (!fun && _slang_is_vec_mat_type(name)) {
2292 /* Next, try casting args to the types of the formal parameters */
2293 int numArgs = oper->num_children;
2294 fun = _slang_find_function_by_argc(A->space.funcs, name, numArgs);
2295 if (!fun || !_slang_cast_func_params(oper, fun, &A->space, A->atoms, A->log)) {
2296 slang_info_log_error(A->log,
2297 "Function '%s' not found (check argument types)",
2298 name);
2299 return NULL;
2300 }
2301 assert(fun);
2302 }
2303
2304 if (!fun) {
2305 slang_info_log_error(A->log,
2306 "Function '%s' not found (check argument types)",
2307 name);
2308 return NULL;
2309 }
2310
2311 if (!fun->body) {
2312 /* The function body may be in another compilation unit.
2313 * We'll try concatenating the shaders and recompile at link time.
2314 */
2315 A->UnresolvedRefs = GL_TRUE;
2316 return new_node1(IR_NOP, NULL);
2317 }
2318
2319 /* type checking to be sure function's return type matches 'dest' type */
2320 if (dest) {
2321 slang_typeinfo t0;
2322
2323 slang_typeinfo_construct(&t0);
2324 typeof_operation(A, dest, &t0);
2325
2326 if (!slang_type_specifier_equal(&t0.spec, &fun->header.type.specifier)) {
2327 slang_info_log_error(A->log,
2328 "Incompatible type returned by call to '%s'",
2329 name);
2330 return NULL;
2331 }
2332 }
2333
2334 n = _slang_gen_function_call(A, fun, oper, dest);
2335
2336 if (n && !n->Store && !dest
2337 && fun->header.type.specifier.type != SLANG_SPEC_VOID) {
2338 /* setup n->Store for the result of the function call */
2339 GLint size = _slang_sizeof_type_specifier(&fun->header.type.specifier);
2340 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
2341 /*printf("Alloc storage for function result, size %d \n", size);*/
2342 }
2343
2344 if (oper->array_constructor) {
2345 /* free the temporary array constructor function now */
2346 slang_function_destruct(fun);
2347 }
2348
2349 return n;
2350 }
2351
2352
2353 static slang_ir_node *
2354 _slang_gen_method_call(slang_assemble_ctx *A, slang_operation *oper)
2355 {
2356 slang_atom *a_length = slang_atom_pool_atom(A->atoms, "length");
2357 slang_ir_node *n;
2358 slang_variable *var;
2359
2360 /* NOTE: In GLSL 1.20, there's only one kind of method
2361 * call: array.length(). Anything else is an error.
2362 */
2363 if (oper->a_id != a_length) {
2364 slang_info_log_error(A->log,
2365 "Undefined method call '%s'", (char *) oper->a_id);
2366 return NULL;
2367 }
2368
2369 /* length() takes no arguments */
2370 if (oper->num_children > 0) {
2371 slang_info_log_error(A->log, "Invalid arguments to length() method");
2372 return NULL;
2373 }
2374
2375 /* lookup the object/variable */
2376 var = _slang_variable_locate(oper->locals, oper->a_obj, GL_TRUE);
2377 if (!var || var->type.specifier.type != SLANG_SPEC_ARRAY) {
2378 slang_info_log_error(A->log,
2379 "Undefined object '%s'", (char *) oper->a_obj);
2380 return NULL;
2381 }
2382
2383 /* Create a float/literal IR node encoding the array length */
2384 n = new_node0(IR_FLOAT);
2385 if (n) {
2386 n->Value[0] = (float) _slang_array_length(var);
2387 n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, 1);
2388 }
2389 return n;
2390 }
2391
2392
2393 static GLboolean
2394 _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
2395 {
2396 if (oper->type == SLANG_OPER_LITERAL_FLOAT ||
2397 oper->type == SLANG_OPER_LITERAL_INT ||
2398 oper->type == SLANG_OPER_LITERAL_BOOL) {
2399 if (oper->literal[0])
2400 *value = GL_TRUE;
2401 else
2402 *value = GL_FALSE;
2403 return GL_TRUE;
2404 }
2405 else if (oper->type == SLANG_OPER_EXPRESSION &&
2406 oper->num_children == 1) {
2407 return _slang_is_constant_cond(&oper->children[0], value);
2408 }
2409 return GL_FALSE;
2410 }
2411
2412
2413 /**
2414 * Test if an operation is a scalar or boolean.
2415 */
2416 static GLboolean
2417 _slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
2418 {
2419 slang_typeinfo type;
2420 GLint size;
2421
2422 slang_typeinfo_construct(&type);
2423 typeof_operation(A, oper, &type);
2424 size = _slang_sizeof_type_specifier(&type.spec);
2425 slang_typeinfo_destruct(&type);
2426 return size == 1;
2427 }
2428
2429
2430 /**
2431 * Test if an operation is boolean.
2432 */
2433 static GLboolean
2434 _slang_is_boolean(slang_assemble_ctx *A, slang_operation *oper)
2435 {
2436 slang_typeinfo type;
2437 GLboolean isBool;
2438
2439 slang_typeinfo_construct(&type);
2440 typeof_operation(A, oper, &type);
2441 isBool = (type.spec.type == SLANG_SPEC_BOOL);
2442 slang_typeinfo_destruct(&type);
2443 return isBool;
2444 }
2445
2446
2447 /**
2448 * Check if a loop contains a 'continue' statement.
2449 * Stop looking if we find a nested loop.
2450 */
2451 static GLboolean
2452 _slang_loop_contains_continue(const slang_operation *oper)
2453 {
2454 switch (oper->type) {
2455 case SLANG_OPER_CONTINUE:
2456 return GL_TRUE;
2457 case SLANG_OPER_FOR:
2458 case SLANG_OPER_DO:
2459 case SLANG_OPER_WHILE:
2460 /* stop upon finding a nested loop */
2461 return GL_FALSE;
2462 default:
2463 /* recurse */
2464 {
2465 GLuint i;
2466 for (i = 0; i < oper->num_children; i++) {
2467 const slang_operation *child = slang_oper_child_const(oper, i);
2468 if (_slang_loop_contains_continue(child))
2469 return GL_TRUE;
2470 }
2471 }
2472 return GL_FALSE;
2473 }
2474 }
2475
2476
2477 /**
2478 * Check if a loop contains a 'continue' or 'break' statement.
2479 * Stop looking if we find a nested loop.
2480 */
2481 static GLboolean
2482 _slang_loop_contains_continue_or_break(const slang_operation *oper)
2483 {
2484 switch (oper->type) {
2485 case SLANG_OPER_CONTINUE:
2486 case SLANG_OPER_BREAK:
2487 return GL_TRUE;
2488 case SLANG_OPER_FOR:
2489 case SLANG_OPER_DO:
2490 case SLANG_OPER_WHILE:
2491 /* stop upon finding a nested loop */
2492 return GL_FALSE;
2493 default:
2494 /* recurse */
2495 {
2496 GLuint i;
2497 for (i = 0; i < oper->num_children; i++) {
2498 const slang_operation *child = slang_oper_child_const(oper, i);
2499 if (_slang_loop_contains_continue_or_break(child))
2500 return GL_TRUE;
2501 }
2502 }
2503 return GL_FALSE;
2504 }
2505 }
2506
2507
2508 /**
2509 * Replace 'break' and 'continue' statements inside a do and while loops.
2510 * This is a recursive helper function used by
2511 * _slang_gen_do/while_without_continue().
2512 */
2513 static void
2514 replace_break_and_cont(slang_assemble_ctx *A, slang_operation *oper)
2515 {
2516 switch (oper->type) {
2517 case SLANG_OPER_BREAK:
2518 /* replace 'break' with "_notBreakFlag = false; break" */
2519 {
2520 slang_operation *block = oper;
2521 block->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2522 slang_operation_add_children(block, 2);
2523 {
2524 slang_operation *assign = slang_oper_child(block, 0);
2525 assign->type = SLANG_OPER_ASSIGN;
2526 slang_operation_add_children(assign, 2);
2527 {
2528 slang_operation *lhs = slang_oper_child(assign, 0);
2529 slang_operation_identifier(lhs, A, "_notBreakFlag");
2530 }
2531 {
2532 slang_operation *rhs = slang_oper_child(assign, 1);
2533 slang_operation_literal_bool(rhs, GL_FALSE);
2534 }
2535 }
2536 {
2537 slang_operation *brk = slang_oper_child(block, 1);
2538 brk->type = SLANG_OPER_BREAK;
2539 assert(!brk->children);
2540 }
2541 }
2542 break;
2543 case SLANG_OPER_CONTINUE:
2544 /* convert continue into a break */
2545 oper->type = SLANG_OPER_BREAK;
2546 break;
2547 case SLANG_OPER_FOR:
2548 case SLANG_OPER_DO:
2549 case SLANG_OPER_WHILE:
2550 /* stop upon finding a nested loop */
2551 break;
2552 default:
2553 /* recurse */
2554 {
2555 GLuint i;
2556 for (i = 0; i < oper->num_children; i++) {
2557 replace_break_and_cont(A, slang_oper_child(oper, i));
2558 }
2559 }
2560 }
2561 }
2562
2563
2564 /**
2565 * Transform a while-loop so that continue statements are converted to breaks.
2566 * Then do normal IR code generation.
2567 *
2568 * Before:
2569 *
2570 * while (LOOPCOND) {
2571 * A;
2572 * if (IFCOND)
2573 * continue;
2574 * B;
2575 * break;
2576 * C;
2577 * }
2578 *
2579 * After:
2580 *
2581 * {
2582 * bool _notBreakFlag = 1;
2583 * while (_notBreakFlag && LOOPCOND) {
2584 * do {
2585 * A;
2586 * if (IFCOND) {
2587 * break; // was continue
2588 * }
2589 * B;
2590 * _notBreakFlag = 0; // was
2591 * break; // break
2592 * C;
2593 * } while (0)
2594 * }
2595 * }
2596 */
2597 static slang_ir_node *
2598 _slang_gen_while_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2599 {
2600 slang_operation *top;
2601 slang_operation *innerBody;
2602
2603 assert(oper->type == SLANG_OPER_WHILE);
2604
2605 top = slang_operation_new(1);
2606 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2607 top->locals->outer_scope = oper->locals->outer_scope;
2608 slang_operation_add_children(top, 2);
2609
2610 /* declare: bool _notBreakFlag = true */
2611 {
2612 slang_operation *condDecl = slang_oper_child(top, 0);
2613 slang_generate_declaration(A, top->locals, condDecl,
2614 SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2615 }
2616
2617 /* build outer while-loop: while (_notBreakFlag && LOOPCOND) { ... } */
2618 {
2619 slang_operation *outerWhile = slang_oper_child(top, 1);
2620 outerWhile->type = SLANG_OPER_WHILE;
2621 slang_operation_add_children(outerWhile, 2);
2622
2623 /* _notBreakFlag && LOOPCOND */
2624 {
2625 slang_operation *cond = slang_oper_child(outerWhile, 0);
2626 cond->type = SLANG_OPER_LOGICALAND;
2627 slang_operation_add_children(cond, 2);
2628 {
2629 slang_operation *notBreak = slang_oper_child(cond, 0);
2630 slang_operation_identifier(notBreak, A, "_notBreakFlag");
2631 }
2632 {
2633 slang_operation *origCond = slang_oper_child(cond, 1);
2634 slang_operation_copy(origCond, slang_oper_child(oper, 0));
2635 }
2636 }
2637
2638 /* inner loop */
2639 {
2640 slang_operation *innerDo = slang_oper_child(outerWhile, 1);
2641 innerDo->type = SLANG_OPER_DO;
2642 slang_operation_add_children(innerDo, 2);
2643
2644 /* copy original do-loop body into inner do-loop's body */
2645 innerBody = slang_oper_child(innerDo, 0);
2646 slang_operation_copy(innerBody, slang_oper_child(oper, 1));
2647 innerBody->locals->outer_scope = innerDo->locals;
2648
2649 /* inner do-loop's condition is constant/false */
2650 {
2651 slang_operation *constFalse = slang_oper_child(innerDo, 1);
2652 slang_operation_literal_bool(constFalse, GL_FALSE);
2653 }
2654 }
2655 }
2656
2657 /* Finally, in innerBody,
2658 * replace "break" with "_notBreakFlag = 0; break"
2659 * replace "continue" with "break"
2660 */
2661 replace_break_and_cont(A, innerBody);
2662
2663 /*slang_print_tree(top, 0);*/
2664
2665 return _slang_gen_operation(A, top);
2666
2667 return NULL;
2668 }
2669
2670
2671 /**
2672 * Generate loop code using high-level IR_LOOP instruction
2673 */
2674 static slang_ir_node *
2675 _slang_gen_while(slang_assemble_ctx * A, slang_operation *oper)
2676 {
2677 /*
2678 * LOOP:
2679 * BREAK if !expr (child[0])
2680 * body code (child[1])
2681 */
2682 const slang_operation *prevLoopOper;
2683 slang_ir_node *prevLoop, *loop, *breakIf, *body;
2684 GLboolean isConst, constTrue;
2685
2686 if (!A->EmitContReturn) {
2687 /* We don't want to emit CONT instructions. If this while-loop has
2688 * a continue, translate it away.
2689 */
2690 if (_slang_loop_contains_continue(slang_oper_child(oper, 1))) {
2691 return _slang_gen_while_without_continue(A, oper);
2692 }
2693 }
2694
2695 /* type-check expression */
2696 if (!_slang_is_boolean(A, &oper->children[0])) {
2697 slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
2698 return NULL;
2699 }
2700
2701 /* Check if loop condition is a constant */
2702 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
2703
2704 if (isConst && !constTrue) {
2705 /* loop is never executed! */
2706 return new_node0(IR_NOP);
2707 }
2708
2709 loop = new_loop(NULL);
2710
2711 /* save old, push new loop */
2712 prevLoop = A->CurLoop;
2713 A->CurLoop = loop;
2714 prevLoopOper = A->CurLoopOper;
2715 A->CurLoopOper = oper;
2716
2717 if (isConst && constTrue) {
2718 /* while(nonzero constant), no conditional break */
2719 breakIf = NULL;
2720 }
2721 else {
2722 slang_ir_node *cond
2723 = new_cond(new_not(_slang_gen_operation(A, &oper->children[0])));
2724 breakIf = new_break_if_true(A, cond);
2725 }
2726 body = _slang_gen_operation(A, &oper->children[1]);
2727 loop->Children[0] = new_seq(breakIf, body);
2728
2729 /* Do infinite loop detection */
2730 /* loop->List is head of linked list of break/continue nodes */
2731 if (!loop->List && isConst && constTrue) {
2732 /* infinite loop detected */
2733 A->CurLoop = prevLoop; /* clean-up */
2734 slang_info_log_error(A->log, "Infinite loop detected!");
2735 return NULL;
2736 }
2737
2738 /* pop loop, restore prev */
2739 A->CurLoop = prevLoop;
2740 A->CurLoopOper = prevLoopOper;
2741
2742 return loop;
2743 }
2744
2745
2746 /**
2747 * Transform a do-while-loop so that continue statements are converted to breaks.
2748 * Then do normal IR code generation.
2749 *
2750 * Before:
2751 *
2752 * do {
2753 * A;
2754 * if (IFCOND)
2755 * continue;
2756 * B;
2757 * break;
2758 * C;
2759 * } while (LOOPCOND);
2760 *
2761 * After:
2762 *
2763 * {
2764 * bool _notBreakFlag = 1;
2765 * do {
2766 * do {
2767 * A;
2768 * if (IFCOND) {
2769 * break; // was continue
2770 * }
2771 * B;
2772 * _notBreakFlag = 0; // was
2773 * break; // break
2774 * C;
2775 * } while (0)
2776 * } while (_notBreakFlag && LOOPCOND);
2777 * }
2778 */
2779 static slang_ir_node *
2780 _slang_gen_do_without_continue(slang_assemble_ctx *A, slang_operation *oper)
2781 {
2782 slang_operation *top;
2783 slang_operation *innerBody;
2784
2785 assert(oper->type == SLANG_OPER_DO);
2786
2787 top = slang_operation_new(1);
2788 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
2789 top->locals->outer_scope = oper->locals->outer_scope;
2790 slang_operation_add_children(top, 2);
2791
2792 /* declare: bool _notBreakFlag = true */
2793 {
2794 slang_operation *condDecl = slang_oper_child(top, 0);
2795 slang_generate_declaration(A, top->locals, condDecl,
2796 SLANG_SPEC_BOOL, "_notBreakFlag", GL_TRUE);
2797 }
2798
2799 /* build outer do-loop: do { ... } while (_notBreakFlag && LOOPCOND) */
2800 {
2801 slang_operation *outerDo = slang_oper_child(top, 1);
2802 outerDo->type = SLANG_OPER_DO;
2803 slang_operation_add_children(outerDo, 2);
2804
2805 /* inner do-loop */
2806 {
2807 slang_operation *innerDo = slang_oper_child(outerDo, 0);
2808 innerDo->type = SLANG_OPER_DO;
2809 slang_operation_add_children(innerDo, 2);
2810
2811 /* copy original do-loop body into inner do-loop's body */
2812 innerBody = slang_oper_child(innerDo, 0);
2813 slang_operation_copy(innerBody, slang_oper_child(oper, 0));
2814 innerBody->locals->outer_scope = innerDo->locals;
2815
2816 /* inner do-loop's condition is constant/false */
2817 {
2818 slang_operation *constFalse = slang_oper_child(innerDo, 1);
2819 slang_operation_literal_bool(constFalse, GL_FALSE);
2820 }
2821 }
2822
2823 /* _notBreakFlag && LOOPCOND */
2824 {
2825 slang_operation *cond = slang_oper_child(outerDo, 1);
2826 cond->type = SLANG_OPER_LOGICALAND;
2827 slang_operation_add_children(cond, 2);
2828 {
2829 slang_operation *notBreak = slang_oper_child(cond, 0);
2830 slang_operation_identifier(notBreak, A, "_notBreakFlag");
2831 }
2832 {
2833 slang_operation *origCond = slang_oper_child(cond, 1);
2834 slang_operation_copy(origCond, slang_oper_child(oper, 1));
2835 }
2836 }
2837 }
2838
2839 /* Finally, in innerBody,
2840 * replace "break" with "_notBreakFlag = 0; break"
2841 * replace "continue" with "break"
2842 */
2843 replace_break_and_cont(A, innerBody);
2844
2845 /*slang_print_tree(top, 0);*/
2846
2847 return _slang_gen_operation(A, top);
2848 }
2849
2850
2851 /**
2852 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2853 */
2854 static slang_ir_node *
2855 _slang_gen_do(slang_assemble_ctx * A, slang_operation *oper)
2856 {
2857 /*
2858 * LOOP:
2859 * body code (child[0])
2860 * tail code:
2861 * BREAK if !expr (child[1])
2862 */
2863 const slang_operation *prevLoopOper;
2864 slang_ir_node *prevLoop, *loop;
2865 GLboolean isConst, constTrue;
2866
2867 if (!A->EmitContReturn) {
2868 /* We don't want to emit CONT instructions. If this do-loop has
2869 * a continue, translate it away.
2870 */
2871 if (_slang_loop_contains_continue(slang_oper_child(oper, 0))) {
2872 return _slang_gen_do_without_continue(A, oper);
2873 }
2874 }
2875
2876 /* type-check expression */
2877 if (!_slang_is_boolean(A, &oper->children[1])) {
2878 slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
2879 return NULL;
2880 }
2881
2882 loop = new_loop(NULL);
2883
2884 /* save old, push new loop */
2885 prevLoop = A->CurLoop;
2886 A->CurLoop = loop;
2887 prevLoopOper = A->CurLoopOper;
2888 A->CurLoopOper = oper;
2889
2890 /* loop body: */
2891 loop->Children[0] = _slang_gen_operation(A, &oper->children[0]);
2892
2893 /* Check if loop condition is a constant */
2894 isConst = _slang_is_constant_cond(&oper->children[1], &constTrue);
2895 if (isConst && constTrue) {
2896 /* do { } while(1) ==> no conditional break */
2897 loop->Children[1] = NULL; /* no tail code */
2898 }
2899 else {
2900 slang_ir_node *cond
2901 = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
2902 loop->Children[1] = new_break_if_true(A, cond);
2903 }
2904
2905 /* XXX we should do infinite loop detection, as above */
2906
2907 /* pop loop, restore prev */
2908 A->CurLoop = prevLoop;
2909 A->CurLoopOper = prevLoopOper;
2910
2911 return loop;
2912 }
2913
2914
2915 /**
2916 * Recursively count the number of operations rooted at 'oper'.
2917 * This gives some kind of indication of the size/complexity of an operation.
2918 */
2919 static GLuint
2920 sizeof_operation(const slang_operation *oper)
2921 {
2922 if (oper) {
2923 GLuint count = 1; /* me */
2924 GLuint i;
2925 for (i = 0; i < oper->num_children; i++) {
2926 count += sizeof_operation(&oper->children[i]);
2927 }
2928 return count;
2929 }
2930 else {
2931 return 0;
2932 }
2933 }
2934
2935
2936 /**
2937 * Determine if a for-loop can be unrolled.
2938 * At this time, only a rather narrow class of for loops can be unrolled.
2939 * See code for details.
2940 * When a loop can't be unrolled because it's too large we'll emit a
2941 * message to the log.
2942 */
2943 static GLboolean
2944 _slang_can_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
2945 {
2946 GLuint bodySize;
2947 GLint start, end;
2948 const char *varName;
2949 slang_atom varId;
2950
2951 if (oper->type != SLANG_OPER_FOR)
2952 return GL_FALSE;
2953
2954 assert(oper->num_children == 4);
2955
2956 if (_slang_loop_contains_continue_or_break(slang_oper_child_const(oper, 3)))
2957 return GL_FALSE;
2958
2959 /* children[0] must be either "int i=constant" or "i=constant" */
2960 if (oper->children[0].type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) {
2961 slang_variable *var;
2962
2963 if (oper->children[0].children[0].type != SLANG_OPER_VARIABLE_DECL)
2964 return GL_FALSE;
2965
2966 varId = oper->children[0].children[0].a_id;
2967
2968 var = _slang_variable_locate(oper->children[0].children[0].locals,
2969 varId, GL_TRUE);
2970 if (!var)
2971 return GL_FALSE;
2972 if (!var->initializer)
2973 return GL_FALSE;
2974 if (var->initializer->type != SLANG_OPER_LITERAL_INT)
2975 return GL_FALSE;
2976 start = (GLint) var->initializer->literal[0];
2977 }
2978 else if (oper->children[0].type == SLANG_OPER_EXPRESSION) {
2979 if (oper->children[0].children[0].type != SLANG_OPER_ASSIGN)
2980 return GL_FALSE;
2981 if (oper->children[0].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
2982 return GL_FALSE;
2983 if (oper->children[0].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
2984 return GL_FALSE;
2985
2986 varId = oper->children[0].children[0].children[0].a_id;
2987
2988 start = (GLint) oper->children[0].children[0].children[1].literal[0];
2989 }
2990 else {
2991 return GL_FALSE;
2992 }
2993
2994 /* children[1] must be "i<constant" */
2995 if (oper->children[1].type != SLANG_OPER_EXPRESSION)
2996 return GL_FALSE;
2997 if (oper->children[1].children[0].type != SLANG_OPER_LESS)
2998 return GL_FALSE;
2999 if (oper->children[1].children[0].children[0].type != SLANG_OPER_IDENTIFIER)
3000 return GL_FALSE;
3001 if (oper->children[1].children[0].children[1].type != SLANG_OPER_LITERAL_INT)
3002 return GL_FALSE;
3003
3004 end = (GLint) oper->children[1].children[0].children[1].literal[0];
3005
3006 /* children[2] must be "i++" or "++i" */
3007 if (oper->children[2].type != SLANG_OPER_POSTINCREMENT &&
3008 oper->children[2].type != SLANG_OPER_PREINCREMENT)
3009 return GL_FALSE;
3010 if (oper->children[2].children[0].type != SLANG_OPER_IDENTIFIER)
3011 return GL_FALSE;
3012
3013 /* make sure the same variable name is used in all places */
3014 if ((oper->children[1].children[0].children[0].a_id != varId) ||
3015 (oper->children[2].children[0].a_id != varId))
3016 return GL_FALSE;
3017
3018 varName = (const char *) varId;
3019
3020 /* children[3], the loop body, can't be too large */
3021 bodySize = sizeof_operation(&oper->children[3]);
3022 if (bodySize > MAX_FOR_LOOP_UNROLL_BODY_SIZE) {
3023 slang_info_log_print(A->log,
3024 "Note: 'for (%s ... )' body is too large/complex"
3025 " to unroll",
3026 varName);
3027 return GL_FALSE;
3028 }
3029
3030 if (start >= end)
3031 return GL_FALSE; /* degenerate case */
3032
3033 if (end - start > MAX_FOR_LOOP_UNROLL_ITERATIONS) {
3034 slang_info_log_print(A->log,
3035 "Note: 'for (%s=%d; %s<%d; ++%s)' is too"
3036 " many iterations to unroll",
3037 varName, start, varName, end, varName);
3038 return GL_FALSE;
3039 }
3040
3041 if ((end - start) * bodySize > MAX_FOR_LOOP_UNROLL_COMPLEXITY) {
3042 slang_info_log_print(A->log,
3043 "Note: 'for (%s=%d; %s<%d; ++%s)' will generate"
3044 " too much code to unroll",
3045 varName, start, varName, end, varName);
3046 return GL_FALSE;
3047 }
3048
3049 return GL_TRUE; /* we can unroll the loop */
3050 }
3051
3052
3053 /**
3054 * Unroll a for-loop.
3055 * First we determine the number of iterations to unroll.
3056 * Then for each iteration:
3057 * make a copy of the loop body
3058 * replace instances of the loop variable with the current iteration value
3059 * generate IR code for the body
3060 * \return pointer to generated IR code or NULL if error, out of memory, etc.
3061 */
3062 static slang_ir_node *
3063 _slang_unroll_for_loop(slang_assemble_ctx * A, const slang_operation *oper)
3064 {
3065 GLint start, end, iter;
3066 slang_ir_node *n, *root = NULL;
3067 slang_atom varId;
3068
3069 if (oper->children[0].type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) {
3070 /* for (int i=0; ... */
3071 slang_variable *var;
3072
3073 varId = oper->children[0].children[0].a_id;
3074 var = _slang_variable_locate(oper->children[0].children[0].locals,
3075 varId, GL_TRUE);
3076 start = (GLint) var->initializer->literal[0];
3077 }
3078 else {
3079 /* for (i=0; ... */
3080 varId = oper->children[0].children[0].children[0].a_id;
3081 start = (GLint) oper->children[0].children[0].children[1].literal[0];
3082 }
3083
3084 end = (GLint) oper->children[1].children[0].children[1].literal[0];
3085
3086 for (iter = start; iter < end; iter++) {
3087 slang_operation *body;
3088
3089 /* make a copy of the loop body */
3090 body = slang_operation_new(1);
3091 if (!body)
3092 return NULL;
3093
3094 if (!slang_operation_copy(body, &oper->children[3]))
3095 return NULL;
3096
3097 /* in body, replace instances of 'varId' with literal 'iter' */
3098 {
3099 slang_variable *oldVar;
3100 slang_operation *newOper;
3101
3102 oldVar = _slang_variable_locate(oper->locals, varId, GL_TRUE);
3103 if (!oldVar) {
3104 /* undeclared loop variable */
3105 slang_operation_delete(body);
3106 return NULL;
3107 }
3108
3109 newOper = slang_operation_new(1);
3110 newOper->type = SLANG_OPER_LITERAL_INT;
3111 newOper->literal_size = 1;
3112 newOper->literal[0] = iter;
3113
3114 /* replace instances of the loop variable with newOper */
3115 slang_substitute(A, body, 1, &oldVar, &newOper, GL_FALSE);
3116 }
3117
3118 /* do IR codegen for body */
3119 n = _slang_gen_operation(A, body);
3120 if (!n)
3121 return NULL;
3122
3123 root = new_seq(root, n);
3124
3125 slang_operation_delete(body);
3126 }
3127
3128 return root;
3129 }
3130
3131
3132 /**
3133 * Replace 'continue' statement with 'break' inside a for-loop.
3134 * This is a recursive helper function used by _slang_gen_for_without_continue().
3135 */
3136 static void
3137 replace_continue_with_break(slang_assemble_ctx *A, slang_operation *oper)
3138 {
3139 switch (oper->type) {
3140 case SLANG_OPER_CONTINUE:
3141 oper->type = SLANG_OPER_BREAK;
3142 break;
3143 case SLANG_OPER_FOR:
3144 case SLANG_OPER_DO:
3145 case SLANG_OPER_WHILE:
3146 /* stop upon finding a nested loop */
3147 break;
3148 default:
3149 /* recurse */
3150 {
3151 GLuint i;
3152 for (i = 0; i < oper->num_children; i++) {
3153 replace_continue_with_break(A, slang_oper_child(oper, i));
3154 }
3155 }
3156 }
3157 }
3158
3159
3160 /**
3161 * Transform a for-loop so that continue statements are converted to breaks.
3162 * Then do normal IR code generation.
3163 *
3164 * Before:
3165 *
3166 * for (INIT; LOOPCOND; INCR) {
3167 * A;
3168 * if (IFCOND) {
3169 * continue;
3170 * }
3171 * B;
3172 * }
3173 *
3174 * After:
3175 *
3176 * {
3177 * bool _condFlag = 1;
3178 * for (INIT; _condFlag; ) {
3179 * for ( ; _condFlag = LOOPCOND; INCR) {
3180 * A;
3181 * if (IFCOND) {
3182 * break;
3183 * }
3184 * B;
3185 * }
3186 * if (_condFlag)
3187 * INCR;
3188 * }
3189 * }
3190 */
3191 static slang_ir_node *
3192 _slang_gen_for_without_continue(slang_assemble_ctx *A, slang_operation *oper)
3193 {
3194 slang_operation *top;
3195 slang_operation *outerFor, *innerFor, *init, *cond, *incr;
3196 slang_operation *lhs, *rhs;
3197
3198 assert(oper->type == SLANG_OPER_FOR);
3199
3200 top = slang_operation_new(1);
3201 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
3202 top->locals->outer_scope = oper->locals->outer_scope;
3203 slang_operation_add_children(top, 2);
3204
3205 /* declare: bool _condFlag = true */
3206 {
3207 slang_operation *condDecl = slang_oper_child(top, 0);
3208 slang_generate_declaration(A, top->locals, condDecl,
3209 SLANG_SPEC_BOOL, "_condFlag", GL_TRUE);
3210 }
3211
3212 /* build outer loop: for (INIT; _condFlag; ) { */
3213 outerFor = slang_oper_child(top, 1);
3214 outerFor->type = SLANG_OPER_FOR;
3215 slang_operation_add_children(outerFor, 4);
3216
3217 init = slang_oper_child(outerFor, 0);
3218 slang_operation_copy(init, slang_oper_child(oper, 0));
3219
3220 cond = slang_oper_child(outerFor, 1);
3221 cond->type = SLANG_OPER_IDENTIFIER;
3222 cond->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3223
3224 incr = slang_oper_child(outerFor, 2);
3225 incr->type = SLANG_OPER_VOID;
3226
3227 /* body of the outer loop */
3228 {
3229 slang_operation *block = slang_oper_child(outerFor, 3);
3230
3231 slang_operation_add_children(block, 2);
3232 block->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
3233
3234 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3235 {
3236 innerFor = slang_oper_child(block, 0);
3237
3238 /* make copy of orig loop */
3239 slang_operation_copy(innerFor, oper);
3240 assert(innerFor->type == SLANG_OPER_FOR);
3241 innerFor->locals->outer_scope = block->locals;
3242
3243 init = slang_oper_child(innerFor, 0);
3244 init->type = SLANG_OPER_VOID; /* leak? */
3245
3246 cond = slang_oper_child(innerFor, 1);
3247 slang_operation_destruct(cond);
3248 cond->type = SLANG_OPER_ASSIGN;
3249 cond->locals = _slang_variable_scope_new(innerFor->locals);
3250 slang_operation_add_children(cond, 2);
3251
3252 lhs = slang_oper_child(cond, 0);
3253 lhs->type = SLANG_OPER_IDENTIFIER;
3254 lhs->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3255
3256 rhs = slang_oper_child(cond, 1);
3257 slang_operation_copy(rhs, slang_oper_child(oper, 1));
3258 }
3259
3260 /* if (_condFlag) INCR; */
3261 {
3262 slang_operation *ifop = slang_oper_child(block, 1);
3263 ifop->type = SLANG_OPER_IF;
3264 slang_operation_add_children(ifop, 2);
3265
3266 /* re-use cond node build above */
3267 slang_operation_copy(slang_oper_child(ifop, 0), cond);
3268
3269 /* incr node from original for-loop operation */
3270 slang_operation_copy(slang_oper_child(ifop, 1),
3271 slang_oper_child(oper, 2));
3272 }
3273
3274 /* finally, replace "continue" with "break" in the inner for-loop */
3275 replace_continue_with_break(A, slang_oper_child(innerFor, 3));
3276 }
3277
3278 return _slang_gen_operation(A, top);
3279 }
3280
3281
3282
3283 /**
3284 * Generate IR for a for-loop. Unrolling will be done when possible.
3285 */
3286 static slang_ir_node *
3287 _slang_gen_for(slang_assemble_ctx * A, slang_operation *oper)
3288 {
3289 GLboolean unroll;
3290
3291 if (!A->EmitContReturn) {
3292 /* We don't want to emit CONT instructions. If this for-loop has
3293 * a continue, translate it away.
3294 */
3295 if (_slang_loop_contains_continue(slang_oper_child(oper, 3))) {
3296 return _slang_gen_for_without_continue(A, oper);
3297 }
3298 }
3299
3300 unroll = _slang_can_unroll_for_loop(A, oper);
3301 if (unroll) {
3302 slang_ir_node *code = _slang_unroll_for_loop(A, oper);
3303 if (code)
3304 return code;
3305 }
3306
3307 assert(oper->type == SLANG_OPER_FOR);
3308
3309 /* conventional for-loop code generation */
3310 {
3311 /*
3312 * init code (child[0])
3313 * LOOP:
3314 * BREAK if !expr (child[1])
3315 * body code (child[3])
3316 * tail code:
3317 * incr code (child[2]) // XXX continue here
3318 */
3319 const slang_operation *prevLoopOper;
3320 slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body, *init, *incr;
3321 init = _slang_gen_operation(A, &oper->children[0]);
3322 loop = new_loop(NULL);
3323
3324 /* save old, push new loop */
3325 prevLoop = A->CurLoop;
3326 A->CurLoop = loop;
3327 prevLoopOper = A->CurLoopOper;
3328 A->CurLoopOper = oper;
3329
3330 cond = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
3331 breakIf = new_break_if_true(A, cond);
3332 body = _slang_gen_operation(A, &oper->children[3]);
3333 incr = _slang_gen_operation(A, &oper->children[2]);
3334
3335 loop->Children[0] = new_seq(breakIf, body);
3336 loop->Children[1] = incr; /* tail code */
3337
3338 /* pop loop, restore prev */
3339 A->CurLoop = prevLoop;
3340 A->CurLoopOper = prevLoopOper;
3341
3342 return new_seq(init, loop);
3343 }
3344 }
3345
3346
3347 static slang_ir_node *
3348 _slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
3349 {
3350 slang_ir_node *n, *cont, *incr = NULL, *loopNode;
3351
3352 assert(oper->type == SLANG_OPER_CONTINUE);
3353 loopNode = A->CurLoop;
3354 assert(loopNode);
3355 assert(loopNode->Opcode == IR_LOOP);
3356
3357 cont = new_node0(IR_CONT);
3358 if (cont) {
3359 cont->Parent = loopNode;
3360 /* insert this node at head of linked list of cont/break instructions */
3361 cont->List = loopNode->List;
3362 loopNode->List = cont;
3363 }
3364
3365 n = new_seq(incr, cont);
3366 return n;
3367 }
3368
3369
3370 /**
3371 * Determine if the given operation is of a specific type.
3372 */
3373 static GLboolean
3374 is_operation_type(const slang_operation *oper, slang_operation_type type)
3375 {
3376 if (oper->type == type)
3377 return GL_TRUE;
3378 else if ((oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
3379 oper->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) &&
3380 oper->num_children == 1)
3381 return is_operation_type(&oper->children[0], type);
3382 else
3383 return GL_FALSE;
3384 }
3385
3386
3387 /**
3388 * Generate IR tree for an if/then/else conditional using high-level
3389 * IR_IF instruction.
3390 */
3391 static slang_ir_node *
3392 _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
3393 {
3394 /*
3395 * eval expr (child[0])
3396 * IF expr THEN
3397 * if-body code
3398 * ELSE
3399 * else-body code
3400 * ENDIF
3401 */
3402 const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
3403 slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
3404 GLboolean isConst, constTrue;
3405
3406 /* type-check expression */
3407 if (!_slang_is_boolean(A, &oper->children[0])) {
3408 slang_info_log_error(A->log, "boolean expression expected for 'if'");
3409 return NULL;
3410 }
3411
3412 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3413 slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
3414 return NULL;
3415 }
3416
3417 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
3418 if (isConst) {
3419 if (constTrue) {
3420 /* if (true) ... */
3421 return _slang_gen_operation(A, &oper->children[1]);
3422 }
3423 else {
3424 /* if (false) ... */
3425 return _slang_gen_operation(A, &oper->children[2]);
3426 }
3427 }
3428
3429 cond = _slang_gen_operation(A, &oper->children[0]);
3430 cond = new_cond(cond);
3431
3432 if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)
3433 && !haveElseClause) {
3434 /* Special case: generate a conditional break */
3435 if (!A->CurLoop) /* probably trying to unroll */
3436 return NULL;
3437 ifBody = new_break_if_true(A, cond);
3438 return ifBody;
3439 }
3440 else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)
3441 && !haveElseClause
3442 && A->CurLoopOper
3443 && A->CurLoopOper->type != SLANG_OPER_FOR) {
3444 /* Special case: generate a conditional continue */
3445 if (!A->CurLoop) /* probably trying to unroll */
3446 return NULL;
3447 ifBody = new_cont_if_true(A, cond);
3448 return ifBody;
3449 }
3450 else {
3451 /* general case */
3452 ifBody = _slang_gen_operation(A, &oper->children[1]);
3453 if (haveElseClause)
3454 elseBody = _slang_gen_operation(A, &oper->children[2]);
3455 else
3456 elseBody = NULL;
3457 ifNode = new_if(cond, ifBody, elseBody);
3458 return ifNode;
3459 }
3460 }
3461
3462
3463
3464 static slang_ir_node *
3465 _slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
3466 {
3467 slang_ir_node *n;
3468
3469 assert(oper->type == SLANG_OPER_NOT);
3470
3471 /* type-check expression */
3472 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3473 slang_info_log_error(A->log,
3474 "scalar/boolean expression expected for '!'");
3475 return NULL;
3476 }
3477
3478 n = _slang_gen_operation(A, &oper->children[0]);
3479 if (n)
3480 return new_not(n);
3481 else
3482 return NULL;
3483 }
3484
3485
3486 static slang_ir_node *
3487 _slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
3488 {
3489 slang_ir_node *n1, *n2;
3490
3491 assert(oper->type == SLANG_OPER_LOGICALXOR);
3492
3493 if (!_slang_is_scalar_or_boolean(A, &oper->children[0]) ||
3494 !_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3495 slang_info_log_error(A->log,
3496 "scalar/boolean expressions expected for '^^'");
3497 return NULL;
3498 }
3499
3500 n1 = _slang_gen_operation(A, &oper->children[0]);
3501 if (!n1)
3502 return NULL;
3503 n2 = _slang_gen_operation(A, &oper->children[1]);
3504 if (!n2)
3505 return NULL;
3506 return new_node2(IR_NOTEQUAL, n1, n2);
3507 }
3508
3509
3510 /**
3511 * Generate IR node for storage of a temporary of given size.
3512 */
3513 static slang_ir_node *
3514 _slang_gen_temporary(GLint size)
3515 {
3516 slang_ir_storage *store;
3517 slang_ir_node *n = NULL;
3518
3519 store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -2, size);
3520 if (store) {
3521 n = new_node0(IR_VAR_DECL);
3522 if (n) {
3523 n->Store = store;
3524 }
3525 else {
3526 _slang_free(store);
3527 }
3528 }
3529 return n;
3530 }
3531
3532
3533 /**
3534 * Generate program constants for an array.
3535 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3536 * This will allocate and initialize three vector constants, storing
3537 * the array in constant memory, not temporaries like a non-const array.
3538 * This can also be used for uniform array initializers.
3539 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3540 */
3541 static GLboolean
3542 make_constant_array(slang_assemble_ctx *A,
3543 slang_variable *var,
3544 slang_operation *initializer)
3545 {
3546 struct gl_program *prog = A->program;
3547 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3548 const char *varName = (char *) var->a_name;
3549 const GLuint numElements = initializer->num_children;
3550 GLint size;
3551 GLuint i, j;
3552 GLfloat *values;
3553
3554 if (!var->store) {
3555 var->store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -6, -6);
3556 }
3557 size = var->store->Size;
3558
3559 assert(var->type.qualifier == SLANG_QUAL_CONST ||
3560 var->type.qualifier == SLANG_QUAL_UNIFORM);
3561 assert(initializer->type == SLANG_OPER_CALL);
3562 assert(initializer->array_constructor);
3563
3564 values = (GLfloat *) _mesa_malloc(numElements * 4 * sizeof(GLfloat));
3565
3566 /* convert constructor params into ordinary floats */
3567 for (i = 0; i < numElements; i++) {
3568 const slang_operation *op = &initializer->children[i];
3569 if (op->type != SLANG_OPER_LITERAL_FLOAT) {
3570 /* unsupported type for this optimization */
3571 free(values);
3572 return GL_FALSE;
3573 }
3574 for (j = 0; j < op->literal_size; j++) {
3575 values[i * 4 + j] = op->literal[j];
3576 }
3577 for ( ; j < 4; j++) {
3578 values[i * 4 + j] = 0.0f;
3579 }
3580 }
3581
3582 /* slightly different paths for constants vs. uniforms */
3583 if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3584 var->store->File = PROGRAM_UNIFORM;
3585 var->store->Index = _mesa_add_uniform(prog->Parameters, varName,
3586 size, datatype, values);
3587 }
3588 else {
3589 var->store->File = PROGRAM_CONSTANT;
3590 var->store->Index = _mesa_add_named_constant(prog->Parameters, varName,
3591 values, size);
3592 }
3593 assert(var->store->Size == size);
3594
3595 _mesa_free(values);
3596
3597 return GL_TRUE;
3598 }
3599
3600
3601
3602 /**
3603 * Generate IR node for allocating/declaring a variable (either a local or
3604 * a global).
3605 * Generally, this involves allocating an slang_ir_storage instance for the
3606 * variable, choosing a register file (temporary, constant, etc).
3607 * For ordinary variables we do not yet allocate storage though. We do that
3608 * when we find the first actual use of the variable to avoid allocating temp
3609 * regs that will never get used.
3610 * At this time, uniforms are always allocated space in this function.
3611 *
3612 * \param initializer Optional initializer expression for the variable.
3613 */
3614 static slang_ir_node *
3615 _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var,
3616 slang_operation *initializer)
3617 {
3618 const char *varName = (const char *) var->a_name;
3619 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3620 slang_ir_node *varDecl, *n;
3621 slang_ir_storage *store;
3622 GLint arrayLen, size, totalSize; /* if array then totalSize > size */
3623 gl_register_file file;
3624
3625 /*assert(!var->declared);*/
3626 var->declared = GL_TRUE;
3627
3628 /* determine GPU register file for simple cases */
3629 if (is_sampler_type(&var->type)) {
3630 file = PROGRAM_SAMPLER;
3631 }
3632 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3633 file = PROGRAM_UNIFORM;
3634 }
3635 else {
3636 file = PROGRAM_TEMPORARY;
3637 }
3638
3639 size = _slang_sizeof_type_specifier(&var->type.specifier);
3640 if (size <= 0) {
3641 slang_info_log_error(A->log, "invalid declaration for '%s'", varName);
3642 return NULL;
3643 }
3644
3645 arrayLen = _slang_array_length(var);
3646 totalSize = _slang_array_size(size, arrayLen);
3647
3648 /* Allocate IR node for the declaration */
3649 varDecl = new_node0(IR_VAR_DECL);
3650 if (!varDecl)
3651 return NULL;
3652
3653 /* Allocate slang_ir_storage for this variable if needed.
3654 * Note that we may not actually allocate a constant or temporary register
3655 * until later.
3656 */
3657 if (!var->store) {
3658 GLint index = -7; /* TBD / unknown */
3659 var->store = _slang_new_ir_storage(file, index, totalSize);
3660 if (!var->store)
3661 return NULL; /* out of memory */
3662 }
3663
3664 /* set the IR node's Var and Store pointers */
3665 varDecl->Var = var;
3666 varDecl->Store = var->store;
3667
3668
3669 store = var->store;
3670
3671 /* if there's an initializer, generate IR for the expression */
3672 if (initializer) {
3673 slang_ir_node *varRef, *init;
3674
3675 if (var->type.qualifier == SLANG_QUAL_CONST) {
3676 /* if the variable is const, the initializer must be a const
3677 * expression as well.
3678 */
3679 #if 0
3680 if (!_slang_is_constant_expr(initializer)) {
3681 slang_info_log_error(A->log,
3682 "initializer for %s not constant", varName);
3683 return NULL;
3684 }
3685 #endif
3686 }
3687
3688 /* IR for the variable we're initializing */
3689 varRef = new_var(A, var);
3690 if (!varRef) {
3691 slang_info_log_error(A->log, "out of memory");
3692 return NULL;
3693 }
3694
3695 /* constant-folding, etc here */
3696 _slang_simplify(initializer, &A->space, A->atoms);
3697
3698 /* look for simple constant-valued variables and uniforms */
3699 if (var->type.qualifier == SLANG_QUAL_CONST ||
3700 var->type.qualifier == SLANG_QUAL_UNIFORM) {
3701
3702 if (initializer->type == SLANG_OPER_CALL &&
3703 initializer->array_constructor) {
3704 /* array initializer */
3705 if (make_constant_array(A, var, initializer))
3706 return varRef;
3707 }
3708 else if (initializer->type == SLANG_OPER_LITERAL_FLOAT ||
3709 initializer->type == SLANG_OPER_LITERAL_INT) {
3710 /* simple float/vector initializer */
3711 if (store->File == PROGRAM_UNIFORM) {
3712 store->Index = _mesa_add_uniform(A->program->Parameters,
3713 varName,
3714 totalSize, datatype,
3715 initializer->literal);
3716 store->Swizzle = _slang_var_swizzle(size, 0);
3717 return varRef;
3718 }
3719 #if 0
3720 else {
3721 store->File = PROGRAM_CONSTANT;
3722 store->Index = _mesa_add_named_constant(A->program->Parameters,
3723 varName,
3724 initializer->literal,
3725 totalSize);
3726 store->Swizzle = _slang_var_swizzle(size, 0);
3727 return varRef;
3728 }
3729 #endif
3730 }
3731 }
3732
3733 /* IR for initializer */
3734 init = _slang_gen_operation(A, initializer);
3735 if (!init)
3736 return NULL;
3737
3738 /* XXX remove this when type checking is added above */
3739 if (init->Store && init->Store->Size != totalSize) {
3740 slang_info_log_error(A->log, "invalid assignment (wrong types)");
3741 return NULL;
3742 }
3743
3744 /* assign RHS to LHS */
3745 n = new_node2(IR_COPY, varRef, init);
3746 n = new_seq(varDecl, n);
3747 }
3748 else {
3749 /* no initializer */
3750 n = varDecl;
3751 }
3752
3753 if (store->File == PROGRAM_UNIFORM && store->Index < 0) {
3754 /* always need to allocate storage for uniforms at this point */
3755 store->Index = _mesa_add_uniform(A->program->Parameters, varName,
3756 totalSize, datatype, NULL);
3757 store->Swizzle = _slang_var_swizzle(size, 0);
3758 }
3759
3760 #if 0
3761 printf("%s var %p %s store=%p index=%d size=%d\n",
3762 __FUNCTION__, (void *) var, (char *) varName,
3763 (void *) store, store->Index, store->Size);
3764 #endif
3765
3766 return n;
3767 }
3768
3769
3770 /**
3771 * Generate code for a selection expression: b ? x : y
3772 * XXX In some cases we could implement a selection expression
3773 * with an LRP instruction (use the boolean as the interpolant).
3774 * Otherwise, we use an IF/ELSE/ENDIF construct.
3775 */
3776 static slang_ir_node *
3777 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
3778 {
3779 slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
3780 slang_ir_node *tmpDecl, *tmpVar, *tree;
3781 slang_typeinfo type0, type1, type2;
3782 int size, isBool, isEqual;
3783
3784 assert(oper->type == SLANG_OPER_SELECT);
3785 assert(oper->num_children == 3);
3786
3787 /* type of children[0] must be boolean */
3788 slang_typeinfo_construct(&type0);
3789 typeof_operation(A, &oper->children[0], &type0);
3790 isBool = (type0.spec.type == SLANG_SPEC_BOOL);
3791 slang_typeinfo_destruct(&type0);
3792 if (!isBool) {
3793 slang_info_log_error(A->log, "selector type is not boolean");
3794 return NULL;
3795 }
3796
3797 slang_typeinfo_construct(&type1);
3798 slang_typeinfo_construct(&type2);
3799 typeof_operation(A, &oper->children[1], &type1);
3800 typeof_operation(A, &oper->children[2], &type2);
3801 isEqual = slang_type_specifier_equal(&type1.spec, &type2.spec);
3802 slang_typeinfo_destruct(&type1);
3803 slang_typeinfo_destruct(&type2);
3804 if (!isEqual) {
3805 slang_info_log_error(A->log, "incompatible types for ?: operator");
3806 return NULL;
3807 }
3808
3809 /* size of x or y's type */
3810 size = _slang_sizeof_type_specifier(&type1.spec);
3811 assert(size > 0);
3812
3813 /* temporary var */
3814 tmpDecl = _slang_gen_temporary(size);
3815
3816 /* the condition (child 0) */
3817 cond = _slang_gen_operation(A, &oper->children[0]);
3818 cond = new_cond(cond);
3819
3820 /* if-true body (child 1) */
3821 tmpVar = new_node0(IR_VAR);
3822 tmpVar->Store = tmpDecl->Store;
3823 trueExpr = _slang_gen_operation(A, &oper->children[1]);
3824 trueNode = new_node2(IR_COPY, tmpVar, trueExpr);
3825
3826 /* if-false body (child 2) */
3827 tmpVar = new_node0(IR_VAR);
3828 tmpVar->Store = tmpDecl->Store;
3829 falseExpr = _slang_gen_operation(A, &oper->children[2]);
3830 falseNode = new_node2(IR_COPY, tmpVar, falseExpr);
3831
3832 ifNode = new_if(cond, trueNode, falseNode);
3833
3834 /* tmp var value */
3835 tmpVar = new_node0(IR_VAR);
3836 tmpVar->Store = tmpDecl->Store;
3837
3838 tree = new_seq(ifNode, tmpVar);
3839 tree = new_seq(tmpDecl, tree);
3840
3841 /*_slang_print_ir_tree(tree, 10);*/
3842 return tree;
3843 }
3844
3845
3846 /**
3847 * Generate code for &&.
3848 */
3849 static slang_ir_node *
3850 _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
3851 {
3852 /* rewrite "a && b" as "a ? b : false" */
3853 slang_operation *select;
3854 slang_ir_node *n;
3855
3856 select = slang_operation_new(1);
3857 select->type = SLANG_OPER_SELECT;
3858 select->num_children = 3;
3859 select->children = slang_operation_new(3);
3860
3861 slang_operation_copy(&select->children[0], &oper->children[0]);
3862 slang_operation_copy(&select->children[1], &oper->children[1]);
3863 select->children[2].type = SLANG_OPER_LITERAL_BOOL;
3864 ASSIGN_4V(select->children[2].literal, 0, 0, 0, 0); /* false */
3865 select->children[2].literal_size = 1;
3866
3867 n = _slang_gen_select(A, select);
3868 return n;
3869 }
3870
3871
3872 /**
3873 * Generate code for ||.
3874 */
3875 static slang_ir_node *
3876 _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
3877 {
3878 /* rewrite "a || b" as "a ? true : b" */
3879 slang_operation *select;
3880 slang_ir_node *n;
3881
3882 select = slang_operation_new(1);
3883 select->type = SLANG_OPER_SELECT;
3884 select->num_children = 3;
3885 select->children = slang_operation_new(3);
3886
3887 slang_operation_copy(&select->children[0], &oper->children[0]);
3888 select->children[1].type = SLANG_OPER_LITERAL_BOOL;
3889 ASSIGN_4V(select->children[1].literal, 1, 1, 1, 1); /* true */
3890 select->children[1].literal_size = 1;
3891 slang_operation_copy(&select->children[2], &oper->children[1]);
3892
3893 n = _slang_gen_select(A, select);
3894 return n;
3895 }
3896
3897
3898 /**
3899 * Generate IR tree for a return statement.
3900 */
3901 static slang_ir_node *
3902 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
3903 {
3904 const GLboolean haveReturnValue
3905 = (oper->num_children == 1 && oper->children[0].type != SLANG_OPER_VOID);
3906
3907 assert(oper->type == SLANG_OPER_RETURN);
3908
3909 /* error checking */
3910 assert(A->CurFunction);
3911 if (haveReturnValue &&
3912 A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
3913 slang_info_log_error(A->log, "illegal return expression");
3914 return NULL;
3915 }
3916 else if (!haveReturnValue &&
3917 A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
3918 slang_info_log_error(A->log, "return statement requires an expression");
3919 return NULL;
3920 }
3921
3922 if (!haveReturnValue) {
3923 if (A->EmitContReturn) {
3924 return new_return(A->curFuncEndLabel);
3925 }
3926 else {
3927 replace_return_with_flag_set(A, oper);
3928 return _slang_gen_operation(A, oper);
3929 }
3930 }
3931 else {
3932 /*
3933 * Convert from:
3934 * return expr;
3935 * To:
3936 * __retVal = expr;
3937 * return; // goto __endOfFunction
3938 */
3939 slang_operation *assign;
3940 slang_atom a_retVal;
3941 slang_ir_node *n;
3942
3943 a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
3944 assert(a_retVal);
3945
3946 #if 1 /* DEBUG */
3947 {
3948 slang_variable *v =
3949 _slang_variable_locate(oper->locals, a_retVal, GL_TRUE);
3950 if (!v) {
3951 /* trying to return a value in a void-valued function */
3952 return NULL;
3953 }
3954 }
3955 #endif
3956
3957 assign = slang_operation_new(1);
3958 assign->type = SLANG_OPER_ASSIGN;
3959 assign->num_children = 2;
3960 assign->children = slang_operation_new(2);
3961 /* lhs (__retVal) */
3962 assign->children[0].type = SLANG_OPER_IDENTIFIER;
3963 assign->children[0].a_id = a_retVal;
3964 assign->children[0].locals->outer_scope = assign->locals;
3965 /* rhs (expr) */
3966 /* XXX we might be able to avoid this copy someday */
3967 slang_operation_copy(&assign->children[1], &oper->children[0]);
3968
3969 /* assemble the new code */
3970 if (A->EmitContReturn) {
3971 n = new_seq(_slang_gen_operation(A, assign),
3972 new_return(A->curFuncEndLabel));
3973 }
3974 else {
3975 slang_operation *setFlag = slang_operation_new(1);
3976 setFlag->type = SLANG_OPER_ASSIGN;
3977 setFlag->locals->outer_scope = oper->locals;
3978 slang_operation_add_children(setFlag, 2);
3979 {
3980 slang_operation *lhs = slang_oper_child(setFlag, 0);
3981 lhs->type = SLANG_OPER_IDENTIFIER;
3982 lhs->a_id = slang_atom_pool_atom(A->atoms, "__returnFlag");
3983 }
3984 {
3985 slang_operation *rhs = slang_oper_child(setFlag, 1);
3986 slang_operation_literal_bool(rhs, GL_TRUE);
3987 }
3988 n = new_seq(_slang_gen_operation(A, assign),
3989 _slang_gen_operation(A, setFlag));
3990 slang_operation_delete(setFlag);
3991 }
3992
3993 slang_operation_delete(assign);
3994 return n;
3995 }
3996 }
3997
3998
3999 #if 0
4000 /**
4001 * Determine if the given operation/expression is const-valued.
4002 */
4003 static GLboolean
4004 _slang_is_constant_expr(const slang_operation *oper)
4005 {
4006 slang_variable *var;
4007 GLuint i;
4008
4009 switch (oper->type) {
4010 case SLANG_OPER_IDENTIFIER:
4011 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4012 if (var && var->type.qualifier == SLANG_QUAL_CONST)
4013 return GL_TRUE;
4014 return GL_FALSE;
4015 default:
4016 for (i = 0; i < oper->num_children; i++) {
4017 if (!_slang_is_constant_expr(&oper->children[i]))
4018 return GL_FALSE;
4019 }
4020 return GL_TRUE;
4021 }
4022 }
4023 #endif
4024
4025
4026 /**
4027 * Check if an assignment of type t1 to t0 is legal.
4028 * XXX more cases needed.
4029 */
4030 static GLboolean
4031 _slang_assignment_compatible(slang_assemble_ctx *A,
4032 slang_operation *op0,
4033 slang_operation *op1)
4034 {
4035 slang_typeinfo t0, t1;
4036 GLuint sz0, sz1;
4037
4038 if (op0->type == SLANG_OPER_POSTINCREMENT ||
4039 op0->type == SLANG_OPER_POSTDECREMENT) {
4040 return GL_FALSE;
4041 }
4042
4043 slang_typeinfo_construct(&t0);
4044 typeof_operation(A, op0, &t0);
4045
4046 slang_typeinfo_construct(&t1);
4047 typeof_operation(A, op1, &t1);
4048
4049 sz0 = _slang_sizeof_type_specifier(&t0.spec);
4050 sz1 = _slang_sizeof_type_specifier(&t1.spec);
4051
4052 #if 1
4053 if (sz0 != sz1) {
4054 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4055 return GL_FALSE;
4056 }
4057 #endif
4058
4059 if (t0.spec.type == SLANG_SPEC_STRUCT &&
4060 t1.spec.type == SLANG_SPEC_STRUCT &&
4061 t0.spec._struct->a_name != t1.spec._struct->a_name)
4062 return GL_FALSE;
4063
4064 if (t0.spec.type == SLANG_SPEC_FLOAT &&
4065 t1.spec.type == SLANG_SPEC_BOOL)
4066 return GL_FALSE;
4067
4068 #if 0 /* not used just yet - causes problems elsewhere */
4069 if (t0.spec.type == SLANG_SPEC_INT &&
4070 t1.spec.type == SLANG_SPEC_FLOAT)
4071 return GL_FALSE;
4072 #endif
4073
4074 if (t0.spec.type == SLANG_SPEC_BOOL &&
4075 t1.spec.type == SLANG_SPEC_FLOAT)
4076 return GL_FALSE;
4077
4078 if (t0.spec.type == SLANG_SPEC_BOOL &&
4079 t1.spec.type == SLANG_SPEC_INT)
4080 return GL_FALSE;
4081
4082 return GL_TRUE;
4083 }
4084
4085
4086 /**
4087 * Generate IR tree for a local variable declaration.
4088 * Basically do some error checking and call _slang_gen_var_decl().
4089 */
4090 static slang_ir_node *
4091 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
4092 {
4093 const char *varName = (char *) oper->a_id;
4094 slang_variable *var;
4095 slang_ir_node *varDecl;
4096 slang_operation *initializer;
4097
4098 assert(oper->type == SLANG_OPER_VARIABLE_DECL);
4099 assert(oper->num_children <= 1);
4100
4101
4102 /* lookup the variable by name */
4103 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4104 if (!var)
4105 return NULL; /* "shouldn't happen" */
4106
4107 if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4108 var->type.qualifier == SLANG_QUAL_VARYING ||
4109 var->type.qualifier == SLANG_QUAL_UNIFORM) {
4110 /* can't declare attribute/uniform vars inside functions */
4111 slang_info_log_error(A->log,
4112 "local variable '%s' cannot be an attribute/uniform/varying",
4113 varName);
4114 return NULL;
4115 }
4116
4117 #if 0
4118 if (v->declared) {
4119 slang_info_log_error(A->log, "variable '%s' redeclared", varName);
4120 return NULL;
4121 }
4122 #endif
4123
4124 /* check if the var has an initializer */
4125 if (oper->num_children > 0) {
4126 assert(oper->num_children == 1);
4127 initializer = &oper->children[0];
4128 }
4129 else if (var->initializer) {
4130 initializer = var->initializer;
4131 }
4132 else {
4133 initializer = NULL;
4134 }
4135
4136 if (initializer) {
4137 /* check/compare var type and initializer type */
4138 if (!_slang_assignment_compatible(A, oper, initializer)) {
4139 slang_info_log_error(A->log, "incompatible types in assignment");
4140 return NULL;
4141 }
4142 }
4143 else {
4144 if (var->type.qualifier == SLANG_QUAL_CONST) {
4145 slang_info_log_error(A->log,
4146 "const-qualified variable '%s' requires initializer",
4147 varName);
4148 return NULL;
4149 }
4150 }
4151
4152 /* Generate IR node */
4153 varDecl = _slang_gen_var_decl(A, var, initializer);
4154 if (!varDecl)
4155 return NULL;
4156
4157 return varDecl;
4158 }
4159
4160
4161 /**
4162 * Generate IR tree for a reference to a variable (such as in an expression).
4163 * This is different from a variable declaration.
4164 */
4165 static slang_ir_node *
4166 _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
4167 {
4168 /* If there's a variable associated with this oper (from inlining)
4169 * use it. Otherwise, use the oper's var id.
4170 */
4171 slang_atom name = oper->var ? oper->var->a_name : oper->a_id;
4172 slang_variable *var = _slang_variable_locate(oper->locals, name, GL_TRUE);
4173 slang_ir_node *n;
4174 if (!var) {
4175 slang_info_log_error(A->log, "undefined variable '%s'", (char *) name);
4176 return NULL;
4177 }
4178 assert(var->declared);
4179 n = new_var(A, var);
4180 return n;
4181 }
4182
4183
4184
4185 /**
4186 * Return the number of components actually named by the swizzle.
4187 * Recall that swizzles may have undefined/don't-care values.
4188 */
4189 static GLuint
4190 swizzle_size(GLuint swizzle)
4191 {
4192 GLuint size = 0, i;
4193 for (i = 0; i < 4; i++) {
4194 GLuint swz = GET_SWZ(swizzle, i);
4195 size += (swz >= 0 && swz <= 3);
4196 }
4197 return size;
4198 }
4199
4200
4201 static slang_ir_node *
4202 _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
4203 {
4204 slang_ir_node *n = new_node1(IR_SWIZZLE, child);
4205 assert(child);
4206 if (n) {
4207 assert(!n->Store);
4208 n->Store = _slang_new_ir_storage_relative(0,
4209 swizzle_size(swizzle),
4210 child->Store);
4211 n->Store->Swizzle = swizzle;
4212 }
4213 return n;
4214 }
4215
4216
4217 static GLboolean
4218 is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
4219 {
4220 while (store->Parent)
4221 store = store->Parent;
4222
4223 if (!(store->File == PROGRAM_OUTPUT ||
4224 store->File == PROGRAM_TEMPORARY ||
4225 (store->File == PROGRAM_VARYING &&
4226 A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
4227 return GL_FALSE;
4228 }
4229 else {
4230 return GL_TRUE;
4231 }
4232 }
4233
4234
4235 /**
4236 * Walk up an IR storage path to compute the final swizzle.
4237 * This is used when we find an expression such as "foo.xz.yx".
4238 */
4239 static GLuint
4240 root_swizzle(const slang_ir_storage *st)
4241 {
4242 GLuint swizzle = st->Swizzle;
4243 while (st->Parent) {
4244 st = st->Parent;
4245 swizzle = _slang_swizzle_swizzle(st->Swizzle, swizzle);
4246 }
4247 return swizzle;
4248 }
4249
4250
4251 /**
4252 * Generate IR tree for an assignment (=).
4253 */
4254 static slang_ir_node *
4255 _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
4256 {
4257 if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
4258 /* Check that var is writeable */
4259 slang_variable *var
4260 = _slang_variable_locate(oper->children[0].locals,
4261 oper->children[0].a_id, GL_TRUE);
4262 if (!var) {
4263 slang_info_log_error(A->log, "undefined variable '%s'",
4264 (char *) oper->children[0].a_id);
4265 return NULL;
4266 }
4267 if (var->type.qualifier == SLANG_QUAL_CONST ||
4268 var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4269 var->type.qualifier == SLANG_QUAL_UNIFORM ||
4270 (var->type.qualifier == SLANG_QUAL_VARYING &&
4271 A->program->Target == GL_FRAGMENT_PROGRAM_ARB)) {
4272 slang_info_log_error(A->log,
4273 "illegal assignment to read-only variable '%s'",
4274 (char *) oper->children[0].a_id);
4275 return NULL;
4276 }
4277 }
4278
4279 if (oper->children[0].type == SLANG_OPER_IDENTIFIER &&
4280 oper->children[1].type == SLANG_OPER_CALL) {
4281 /* Special case of: x = f(a, b)
4282 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4283 *
4284 * XXX this could be even more effective if we could accomodate
4285 * cases such as "v.x = f();" - would help with typical vertex
4286 * transformation.
4287 */
4288 slang_ir_node *n;
4289 n = _slang_gen_function_call_name(A,
4290 (const char *) oper->children[1].a_id,
4291 &oper->children[1], &oper->children[0]);
4292 return n;
4293 }
4294 else {
4295 slang_ir_node *n, *lhs, *rhs;
4296
4297 /* lhs and rhs type checking */
4298 if (!_slang_assignment_compatible(A,
4299 &oper->children[0],
4300 &oper->children[1])) {
4301 slang_info_log_error(A->log, "incompatible types in assignment");
4302 return NULL;
4303 }
4304
4305 lhs = _slang_gen_operation(A, &oper->children[0]);
4306 if (!lhs) {
4307 return NULL;
4308 }
4309
4310 if (!lhs->Store) {
4311 slang_info_log_error(A->log,
4312 "invalid left hand side for assignment");
4313 return NULL;
4314 }
4315
4316 /* check that lhs is writable */
4317 if (!is_store_writable(A, lhs->Store)) {
4318 slang_info_log_error(A->log,
4319 "illegal assignment to read-only l-value");
4320 return NULL;
4321 }
4322
4323 rhs = _slang_gen_operation(A, &oper->children[1]);
4324 if (lhs && rhs) {
4325 /* convert lhs swizzle into writemask */
4326 const GLuint swizzle = root_swizzle(lhs->Store);
4327 GLuint writemask, newSwizzle = 0x0;
4328 if (!swizzle_to_writemask(A, swizzle, &writemask, &newSwizzle)) {
4329 /* Non-simple writemask, need to swizzle right hand side in
4330 * order to put components into the right place.
4331 */
4332 rhs = _slang_gen_swizzle(rhs, newSwizzle);
4333 }
4334 n = new_node2(IR_COPY, lhs, rhs);
4335 return n;
4336 }
4337 else {
4338 return NULL;
4339 }
4340 }
4341 }
4342
4343
4344 /**
4345 * Generate IR tree for referencing a field in a struct (or basic vector type)
4346 */
4347 static slang_ir_node *
4348 _slang_gen_struct_field(slang_assemble_ctx * A, slang_operation *oper)
4349 {
4350 slang_typeinfo ti;
4351
4352 /* type of struct */
4353 slang_typeinfo_construct(&ti);
4354 typeof_operation(A, &oper->children[0], &ti);
4355
4356 if (_slang_type_is_vector(ti.spec.type)) {
4357 /* the field should be a swizzle */
4358 const GLuint rows = _slang_type_dim(ti.spec.type);
4359 slang_swizzle swz;
4360 slang_ir_node *n;
4361 GLuint swizzle;
4362 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4363 slang_info_log_error(A->log, "Bad swizzle");
4364 return NULL;
4365 }
4366 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4367 swz.swizzle[1],
4368 swz.swizzle[2],
4369 swz.swizzle[3]);
4370
4371 n = _slang_gen_operation(A, &oper->children[0]);
4372 /* create new parent node with swizzle */
4373 if (n)
4374 n = _slang_gen_swizzle(n, swizzle);
4375 return n;
4376 }
4377 else if ( ti.spec.type == SLANG_SPEC_FLOAT
4378 || ti.spec.type == SLANG_SPEC_INT
4379 || ti.spec.type == SLANG_SPEC_BOOL) {
4380 const GLuint rows = 1;
4381 slang_swizzle swz;
4382 slang_ir_node *n;
4383 GLuint swizzle;
4384 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4385 slang_info_log_error(A->log, "Bad swizzle");
4386 }
4387 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4388 swz.swizzle[1],
4389 swz.swizzle[2],
4390 swz.swizzle[3]);
4391 n = _slang_gen_operation(A, &oper->children[0]);
4392 /* create new parent node with swizzle */
4393 n = _slang_gen_swizzle(n, swizzle);
4394 return n;
4395 }
4396 else {
4397 /* the field is a structure member (base.field) */
4398 /* oper->children[0] is the base */
4399 /* oper->a_id is the field name */
4400 slang_ir_node *base, *n;
4401 slang_typeinfo field_ti;
4402 GLint fieldSize, fieldOffset = -1;
4403
4404 /* type of field */
4405 slang_typeinfo_construct(&field_ti);
4406 typeof_operation(A, oper, &field_ti);
4407
4408 fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
4409 if (fieldSize > 0)
4410 fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
4411
4412 if (fieldSize == 0 || fieldOffset < 0) {
4413 const char *structName;
4414 if (ti.spec._struct)
4415 structName = (char *) ti.spec._struct->a_name;
4416 else
4417 structName = "unknown";
4418 slang_info_log_error(A->log,
4419 "\"%s\" is not a member of struct \"%s\"",
4420 (char *) oper->a_id, structName);
4421 return NULL;
4422 }
4423 assert(fieldSize >= 0);
4424
4425 base = _slang_gen_operation(A, &oper->children[0]);
4426 if (!base) {
4427 /* error msg should have already been logged */
4428 return NULL;
4429 }
4430
4431 n = new_node1(IR_FIELD, base);
4432 if (!n)
4433 return NULL;
4434
4435 n->Field = (char *) oper->a_id;
4436
4437 /* Store the field's offset in storage->Index */
4438 n->Store = _slang_new_ir_storage(base->Store->File,
4439 fieldOffset,
4440 fieldSize);
4441
4442 return n;
4443 }
4444 }
4445
4446
4447 /**
4448 * Gen code for array indexing.
4449 */
4450 static slang_ir_node *
4451 _slang_gen_array_element(slang_assemble_ctx * A, slang_operation *oper)
4452 {
4453 slang_typeinfo array_ti;
4454
4455 /* get array's type info */
4456 slang_typeinfo_construct(&array_ti);
4457 typeof_operation(A, &oper->children[0], &array_ti);
4458
4459 if (_slang_type_is_vector(array_ti.spec.type)) {
4460 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4461 /* translate the index into a swizzle/writemask: "v.x=p" */
4462 const GLuint max = _slang_type_dim(array_ti.spec.type);
4463 GLint index;
4464 slang_ir_node *n;
4465
4466 index = (GLint) oper->children[1].literal[0];
4467 if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
4468 index >= (GLint) max) {
4469 #if 0
4470 slang_info_log_error(A->log, "Invalid array index for vector type");
4471 printf("type = %d\n", oper->children[1].type);
4472 printf("index = %d, max = %d\n", index, max);
4473 printf("array = %s\n", (char*)oper->children[0].a_id);
4474 printf("index = %s\n", (char*)oper->children[1].a_id);
4475 return NULL;
4476 #else
4477 index = 0;
4478 #endif
4479 }
4480
4481 n = _slang_gen_operation(A, &oper->children[0]);
4482 if (n) {
4483 /* use swizzle to access the element */
4484 GLuint swizzle = MAKE_SWIZZLE4(SWIZZLE_X + index,
4485 SWIZZLE_NIL,
4486 SWIZZLE_NIL,
4487 SWIZZLE_NIL);
4488 n = _slang_gen_swizzle(n, swizzle);
4489 }
4490 assert(n->Store);
4491 return n;
4492 }
4493 else {
4494 /* conventional array */
4495 slang_typeinfo elem_ti;
4496 slang_ir_node *elem, *array, *index;
4497 GLint elemSize, arrayLen;
4498
4499 /* size of array element */
4500 slang_typeinfo_construct(&elem_ti);
4501 typeof_operation(A, oper, &elem_ti);
4502 elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
4503
4504 if (_slang_type_is_matrix(array_ti.spec.type))
4505 arrayLen = _slang_type_dim(array_ti.spec.type);
4506 else
4507 arrayLen = array_ti.array_len;
4508
4509 slang_typeinfo_destruct(&array_ti);
4510 slang_typeinfo_destruct(&elem_ti);
4511
4512 if (elemSize <= 0) {
4513 /* unknown var or type */
4514 slang_info_log_error(A->log, "Undefined variable or type");
4515 return NULL;
4516 }
4517
4518 array = _slang_gen_operation(A, &oper->children[0]);
4519 index = _slang_gen_operation(A, &oper->children[1]);
4520 if (array && index) {
4521 /* bounds check */
4522 GLint constIndex = -1;
4523 if (index->Opcode == IR_FLOAT) {
4524 constIndex = (int) index->Value[0];
4525 if (constIndex < 0 || constIndex >= arrayLen) {
4526 slang_info_log_error(A->log,
4527 "Array index out of bounds (index=%d size=%d)",
4528 constIndex, arrayLen);
4529 _slang_free_ir_tree(array);
4530 _slang_free_ir_tree(index);
4531 return NULL;
4532 }
4533 }
4534
4535 if (!array->Store) {
4536 slang_info_log_error(A->log, "Invalid array");
4537 return NULL;
4538 }
4539
4540 elem = new_node2(IR_ELEMENT, array, index);
4541
4542 /* The storage info here will be updated during code emit */
4543 elem->Store = _slang_new_ir_storage(array->Store->File,
4544 array->Store->Index,
4545 elemSize);
4546 elem->Store->Swizzle = _slang_var_swizzle(elemSize, 0);
4547 return elem;
4548 }
4549 else {
4550 _slang_free_ir_tree(array);
4551 _slang_free_ir_tree(index);
4552 return NULL;
4553 }
4554 }
4555 }
4556
4557
4558 static slang_ir_node *
4559 _slang_gen_compare(slang_assemble_ctx *A, slang_operation *oper,
4560 slang_ir_opcode opcode)
4561 {
4562 slang_typeinfo t0, t1;
4563 slang_ir_node *n;
4564
4565 slang_typeinfo_construct(&t0);
4566 typeof_operation(A, &oper->children[0], &t0);
4567
4568 slang_typeinfo_construct(&t1);
4569 typeof_operation(A, &oper->children[0], &t1);
4570
4571 if (t0.spec.type == SLANG_SPEC_ARRAY ||
4572 t1.spec.type == SLANG_SPEC_ARRAY) {
4573 slang_info_log_error(A->log, "Illegal array comparison");
4574 return NULL;
4575 }
4576
4577 if (oper->type != SLANG_OPER_EQUAL &&
4578 oper->type != SLANG_OPER_NOTEQUAL) {
4579 /* <, <=, >, >= can only be used with scalars */
4580 if ((t0.spec.type != SLANG_SPEC_INT &&
4581 t0.spec.type != SLANG_SPEC_FLOAT) ||
4582 (t1.spec.type != SLANG_SPEC_INT &&
4583 t1.spec.type != SLANG_SPEC_FLOAT)) {
4584 slang_info_log_error(A->log, "Incompatible type(s) for inequality operator");
4585 return NULL;
4586 }
4587 }
4588
4589 n = new_node2(opcode,
4590 _slang_gen_operation(A, &oper->children[0]),
4591 _slang_gen_operation(A, &oper->children[1]));
4592
4593 /* result is a bool (size 1) */
4594 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, 1);
4595
4596 return n;
4597 }
4598
4599
4600 #if 0
4601 static void
4602 print_vars(slang_variable_scope *s)
4603 {
4604 int i;
4605 printf("vars: ");
4606 for (i = 0; i < s->num_variables; i++) {
4607 printf("%s %d, \n",
4608 (char*) s->variables[i]->a_name,
4609 s->variables[i]->declared);
4610 }
4611
4612 printf("\n");
4613 }
4614 #endif
4615
4616
4617 #if 0
4618 static void
4619 _slang_undeclare_vars(slang_variable_scope *locals)
4620 {
4621 if (locals->num_variables > 0) {
4622 int i;
4623 for (i = 0; i < locals->num_variables; i++) {
4624 slang_variable *v = locals->variables[i];
4625 printf("undeclare %s at %p\n", (char*) v->a_name, v);
4626 v->declared = GL_FALSE;
4627 }
4628 }
4629 }
4630 #endif
4631
4632
4633 /**
4634 * Generate IR tree for a slang_operation (AST node)
4635 */
4636 static slang_ir_node *
4637 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
4638 {
4639 switch (oper->type) {
4640 case SLANG_OPER_BLOCK_NEW_SCOPE:
4641 {
4642 slang_ir_node *n;
4643
4644 _slang_push_var_table(A->vartable);
4645
4646 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE; /* temp change */
4647 n = _slang_gen_operation(A, oper);
4648 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE; /* restore */
4649
4650 _slang_pop_var_table(A->vartable);
4651
4652 /*_slang_undeclare_vars(oper->locals);*/
4653 /*print_vars(oper->locals);*/
4654
4655 if (n)
4656 n = new_node1(IR_SCOPE, n);
4657 return n;
4658 }
4659 break;
4660
4661 case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
4662 /* list of operations */
4663 if (oper->num_children > 0)
4664 {
4665 slang_ir_node *n, *tree = NULL;
4666 GLuint i;
4667
4668 for (i = 0; i < oper->num_children; i++) {
4669 n = _slang_gen_operation(A, &oper->children[i]);
4670 if (!n) {
4671 _slang_free_ir_tree(tree);
4672 return NULL; /* error must have occured */
4673 }
4674 tree = new_seq(tree, n);
4675 }
4676
4677 return tree;
4678 }
4679 else {
4680 return new_node0(IR_NOP);
4681 }
4682
4683 case SLANG_OPER_EXPRESSION:
4684 return _slang_gen_operation(A, &oper->children[0]);
4685
4686 case SLANG_OPER_FOR:
4687 return _slang_gen_for(A, oper);
4688 case SLANG_OPER_DO:
4689 return _slang_gen_do(A, oper);
4690 case SLANG_OPER_WHILE:
4691 return _slang_gen_while(A, oper);
4692 case SLANG_OPER_BREAK:
4693 if (!A->CurLoop) {
4694 slang_info_log_error(A->log, "'break' not in loop");
4695 return NULL;
4696 }
4697 return new_break(A->CurLoop);
4698 case SLANG_OPER_CONTINUE:
4699 if (!A->CurLoop) {
4700 slang_info_log_error(A->log, "'continue' not in loop");
4701 return NULL;
4702 }
4703 return _slang_gen_continue(A, oper);
4704 case SLANG_OPER_DISCARD:
4705 return new_node0(IR_KILL);
4706
4707 case SLANG_OPER_EQUAL:
4708 return _slang_gen_compare(A, oper, IR_EQUAL);
4709 case SLANG_OPER_NOTEQUAL:
4710 return _slang_gen_compare(A, oper, IR_NOTEQUAL);
4711 case SLANG_OPER_GREATER:
4712 return _slang_gen_compare(A, oper, IR_SGT);
4713 case SLANG_OPER_LESS:
4714 return _slang_gen_compare(A, oper, IR_SLT);
4715 case SLANG_OPER_GREATEREQUAL:
4716 return _slang_gen_compare(A, oper, IR_SGE);
4717 case SLANG_OPER_LESSEQUAL:
4718 return _slang_gen_compare(A, oper, IR_SLE);
4719 case SLANG_OPER_ADD:
4720 {
4721 slang_ir_node *n;
4722 assert(oper->num_children == 2);
4723 n = _slang_gen_function_call_name(A, "+", oper, NULL);
4724 return n;
4725 }
4726 case SLANG_OPER_SUBTRACT:
4727 {
4728 slang_ir_node *n;
4729 assert(oper->num_children == 2);
4730 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4731 return n;
4732 }
4733 case SLANG_OPER_MULTIPLY:
4734 {
4735 slang_ir_node *n;
4736 assert(oper->num_children == 2);
4737 n = _slang_gen_function_call_name(A, "*", oper, NULL);
4738 return n;
4739 }
4740 case SLANG_OPER_DIVIDE:
4741 {
4742 slang_ir_node *n;
4743 assert(oper->num_children == 2);
4744 n = _slang_gen_function_call_name(A, "/", oper, NULL);
4745 return n;
4746 }
4747 case SLANG_OPER_MINUS:
4748 {
4749 slang_ir_node *n;
4750 assert(oper->num_children == 1);
4751 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4752 return n;
4753 }
4754 case SLANG_OPER_PLUS:
4755 /* +expr --> do nothing */
4756 return _slang_gen_operation(A, &oper->children[0]);
4757 case SLANG_OPER_VARIABLE_DECL:
4758 return _slang_gen_declaration(A, oper);
4759 case SLANG_OPER_ASSIGN:
4760 return _slang_gen_assignment(A, oper);
4761 case SLANG_OPER_ADDASSIGN:
4762 {
4763 slang_ir_node *n;
4764 assert(oper->num_children == 2);
4765 n = _slang_gen_function_call_name(A, "+=", oper, NULL);
4766 return n;
4767 }
4768 case SLANG_OPER_SUBASSIGN:
4769 {
4770 slang_ir_node *n;
4771 assert(oper->num_children == 2);
4772 n = _slang_gen_function_call_name(A, "-=", oper, NULL);
4773 return n;
4774 }
4775 break;
4776 case SLANG_OPER_MULASSIGN:
4777 {
4778 slang_ir_node *n;
4779 assert(oper->num_children == 2);
4780 n = _slang_gen_function_call_name(A, "*=", oper, NULL);
4781 return n;
4782 }
4783 case SLANG_OPER_DIVASSIGN:
4784 {
4785 slang_ir_node *n;
4786 assert(oper->num_children == 2);
4787 n = _slang_gen_function_call_name(A, "/=", oper, NULL);
4788 return n;
4789 }
4790 case SLANG_OPER_LOGICALAND:
4791 {
4792 slang_ir_node *n;
4793 assert(oper->num_children == 2);
4794 n = _slang_gen_logical_and(A, oper);
4795 return n;
4796 }
4797 case SLANG_OPER_LOGICALOR:
4798 {
4799 slang_ir_node *n;
4800 assert(oper->num_children == 2);
4801 n = _slang_gen_logical_or(A, oper);
4802 return n;
4803 }
4804 case SLANG_OPER_LOGICALXOR:
4805 return _slang_gen_xor(A, oper);
4806 case SLANG_OPER_NOT:
4807 return _slang_gen_not(A, oper);
4808 case SLANG_OPER_SELECT: /* b ? x : y */
4809 {
4810 slang_ir_node *n;
4811 assert(oper->num_children == 3);
4812 n = _slang_gen_select(A, oper);
4813 return n;
4814 }
4815
4816 case SLANG_OPER_ASM:
4817 return _slang_gen_asm(A, oper, NULL);
4818 case SLANG_OPER_CALL:
4819 return _slang_gen_function_call_name(A, (const char *) oper->a_id,
4820 oper, NULL);
4821 case SLANG_OPER_METHOD:
4822 return _slang_gen_method_call(A, oper);
4823 case SLANG_OPER_RETURN:
4824 return _slang_gen_return(A, oper);
4825 case SLANG_OPER_LABEL:
4826 return new_label(oper->label);
4827 case SLANG_OPER_IDENTIFIER:
4828 return _slang_gen_variable(A, oper);
4829 case SLANG_OPER_IF:
4830 return _slang_gen_if(A, oper);
4831 case SLANG_OPER_FIELD:
4832 return _slang_gen_struct_field(A, oper);
4833 case SLANG_OPER_SUBSCRIPT:
4834 return _slang_gen_array_element(A, oper);
4835 case SLANG_OPER_LITERAL_FLOAT:
4836 /* fall-through */
4837 case SLANG_OPER_LITERAL_INT:
4838 /* fall-through */
4839 case SLANG_OPER_LITERAL_BOOL:
4840 return new_float_literal(oper->literal, oper->literal_size);
4841
4842 case SLANG_OPER_POSTINCREMENT: /* var++ */
4843 {
4844 slang_ir_node *n;
4845 assert(oper->num_children == 1);
4846 n = _slang_gen_function_call_name(A, "__postIncr", oper, NULL);
4847 return n;
4848 }
4849 case SLANG_OPER_POSTDECREMENT: /* var-- */
4850 {
4851 slang_ir_node *n;
4852 assert(oper->num_children == 1);
4853 n = _slang_gen_function_call_name(A, "__postDecr", oper, NULL);
4854 return n;
4855 }
4856 case SLANG_OPER_PREINCREMENT: /* ++var */
4857 {
4858 slang_ir_node *n;
4859 assert(oper->num_children == 1);
4860 n = _slang_gen_function_call_name(A, "++", oper, NULL);
4861 return n;
4862 }
4863 case SLANG_OPER_PREDECREMENT: /* --var */
4864 {
4865 slang_ir_node *n;
4866 assert(oper->num_children == 1);
4867 n = _slang_gen_function_call_name(A, "--", oper, NULL);
4868 return n;
4869 }
4870
4871 case SLANG_OPER_NON_INLINED_CALL:
4872 case SLANG_OPER_SEQUENCE:
4873 {
4874 slang_ir_node *tree = NULL;
4875 GLuint i;
4876 for (i = 0; i < oper->num_children; i++) {
4877 slang_ir_node *n = _slang_gen_operation(A, &oper->children[i]);
4878 tree = new_seq(tree, n);
4879 if (n)
4880 tree->Store = n->Store;
4881 }
4882 if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
4883 tree = new_function_call(tree, oper->label);
4884 }
4885 return tree;
4886 }
4887
4888 case SLANG_OPER_NONE:
4889 case SLANG_OPER_VOID:
4890 /* returning NULL here would generate an error */
4891 return new_node0(IR_NOP);
4892
4893 default:
4894 _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
4895 oper->type);
4896 return new_node0(IR_NOP);
4897 }
4898
4899 return NULL;
4900 }
4901
4902
4903 /**
4904 * Check if the given type specifier is a rectangular texture sampler.
4905 */
4906 static GLboolean
4907 is_rect_sampler_spec(const slang_type_specifier *spec)
4908 {
4909 while (spec->_array) {
4910 spec = spec->_array;
4911 }
4912 return spec->type == SLANG_SPEC_SAMPLER2DRECT ||
4913 spec->type == SLANG_SPEC_SAMPLER2DRECTSHADOW;
4914 }
4915
4916
4917
4918 /**
4919 * Called by compiler when a global variable has been parsed/compiled.
4920 * Here we examine the variable's type to determine what kind of register
4921 * storage will be used.
4922 *
4923 * A uniform such as "gl_Position" will become the register specification
4924 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4925 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4926 *
4927 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4928 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4929 * actual texture unit (as specified by the user calling glUniform1i()).
4930 */
4931 GLboolean
4932 _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
4933 slang_unit_type type)
4934 {
4935 struct gl_program *prog = A->program;
4936 const char *varName = (char *) var->a_name;
4937 GLboolean success = GL_TRUE;
4938 slang_ir_storage *store = NULL;
4939 int dbg = 0;
4940 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
4941 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
4942 const GLint arrayLen = _slang_array_length(var);
4943 const GLint totalSize = _slang_array_size(size, arrayLen);
4944 GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
4945
4946 /* check for sampler2D arrays */
4947 if (texIndex == -1 && var->type.specifier._array)
4948 texIndex = sampler_to_texture_index(var->type.specifier._array->type);
4949
4950 if (texIndex != -1) {
4951 /* This is a texture sampler variable...
4952 * store->File = PROGRAM_SAMPLER
4953 * store->Index = sampler number (0..7, typically)
4954 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4955 */
4956 if (var->initializer) {
4957 slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
4958 return GL_FALSE;
4959 }
4960 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4961 /* disallow rect samplers */
4962 if (is_rect_sampler_spec(&var->type.specifier)) {
4963 slang_info_log_error(A->log, "invalid sampler type for '%s'", varName);
4964 return GL_FALSE;
4965 }
4966 #else
4967 (void) is_rect_sampler_spec; /* silence warning */
4968 #endif
4969 {
4970 GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
4971 store = _slang_new_ir_storage_sampler(sampNum, texIndex, totalSize);
4972
4973 /* If we have a sampler array, then we need to allocate the
4974 * additional samplers to ensure we don't allocate them elsewhere.
4975 * We can't directly use _mesa_add_sampler() as that checks the
4976 * varName and gets a match, so we call _mesa_add_parameter()
4977 * directly and use the last sampler number from the call above.
4978 */
4979 if (arrayLen > 0) {
4980 GLint a = arrayLen - 1;
4981 GLint i;
4982 for (i = 0; i < a; i++) {
4983 GLfloat value = (GLfloat)(i + sampNum + 1);
4984 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_SAMPLER,
4985 varName, 1, datatype, &value, NULL, 0x0);
4986 }
4987 }
4988 }
4989 if (dbg) printf("SAMPLER ");
4990 }
4991 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
4992 /* Uniform variable */
4993 const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
4994
4995 if (prog) {
4996 /* user-defined uniform */
4997 if (datatype == GL_NONE) {
4998 if ((var->type.specifier.type == SLANG_SPEC_ARRAY &&
4999 var->type.specifier._array->type == SLANG_SPEC_STRUCT) ||
5000 (var->type.specifier.type == SLANG_SPEC_STRUCT)) {
5001 /* temporary work-around */
5002 GLenum datatype = GL_FLOAT;
5003 GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
5004 totalSize, datatype, NULL);
5005 store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
5006 totalSize, swizzle);
5007
5008 if (arrayLen > 0) {
5009 GLint a = arrayLen - 1;
5010 GLint i;
5011 for (i = 0; i < a; i++) {
5012 GLfloat value = (GLfloat)(i + uniformLoc + 1);
5013 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM,
5014 varName, 1, datatype, &value, NULL, 0x0);
5015 }
5016 }
5017
5018 /* XXX what we need to do is unroll the struct into its
5019 * basic types, creating a uniform variable for each.
5020 * For example:
5021 * struct foo {
5022 * vec3 a;
5023 * vec4 b;
5024 * };
5025 * uniform foo f;
5026 *
5027 * Should produce uniforms:
5028 * "f.a" (GL_FLOAT_VEC3)
5029 * "f.b" (GL_FLOAT_VEC4)
5030 */
5031
5032 if (var->initializer) {
5033 slang_info_log_error(A->log,
5034 "unsupported initializer for uniform '%s'", varName);
5035 return GL_FALSE;
5036 }
5037 }
5038 else {
5039 slang_info_log_error(A->log,
5040 "invalid datatype for uniform variable %s",
5041 varName);
5042 return GL_FALSE;
5043 }
5044 }
5045 else {
5046 /* non-struct uniform */
5047 if (!_slang_gen_var_decl(A, var, var->initializer))
5048 return GL_FALSE;
5049 store = var->store;
5050 }
5051 }
5052 else {
5053 /* pre-defined uniform, like gl_ModelviewMatrix */
5054 /* We know it's a uniform, but don't allocate storage unless
5055 * it's really used.
5056 */
5057 store = _slang_new_ir_storage_swz(PROGRAM_STATE_VAR, -1,
5058 totalSize, swizzle);
5059 }
5060 if (dbg) printf("UNIFORM (sz %d) ", totalSize);
5061 }
5062 else if (var->type.qualifier == SLANG_QUAL_VARYING) {
5063 /* varyings must be float, vec or mat */
5064 if (!_slang_type_is_float_vec_mat(var->type.specifier.type) &&
5065 var->type.specifier.type != SLANG_SPEC_ARRAY) {
5066 slang_info_log_error(A->log,
5067 "varying '%s' must be float/vector/matrix",
5068 varName);
5069 return GL_FALSE;
5070 }
5071
5072 if (var->initializer) {
5073 slang_info_log_error(A->log, "illegal initializer for varying '%s'",
5074 varName);
5075 return GL_FALSE;
5076 }
5077
5078 if (prog) {
5079 /* user-defined varying */
5080 GLbitfield flags;
5081 GLint varyingLoc;
5082 GLuint swizzle;
5083
5084 flags = 0x0;
5085 if (var->type.centroid == SLANG_CENTROID)
5086 flags |= PROG_PARAM_BIT_CENTROID;
5087 if (var->type.variant == SLANG_INVARIANT)
5088 flags |= PROG_PARAM_BIT_INVARIANT;
5089
5090 varyingLoc = _mesa_add_varying(prog->Varying, varName,
5091 totalSize, flags);
5092 swizzle = _slang_var_swizzle(size, 0);
5093 store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
5094 totalSize, swizzle);
5095 }
5096 else {
5097 /* pre-defined varying, like gl_Color or gl_TexCoord */
5098 if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
5099 /* fragment program input */
5100 GLuint swizzle;
5101 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5102 &swizzle);
5103 assert(index >= 0);
5104 assert(index < FRAG_ATTRIB_MAX);
5105 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index,
5106 size, swizzle);
5107 }
5108 else {
5109 /* vertex program output */
5110 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5111 GLuint swizzle = _slang_var_swizzle(size, 0);
5112 assert(index >= 0);
5113 assert(index < VERT_RESULT_MAX);
5114 assert(type == SLANG_UNIT_VERTEX_BUILTIN);
5115 store = _slang_new_ir_storage_swz(PROGRAM_OUTPUT, index,
5116 size, swizzle);
5117 }
5118 if (dbg) printf("V/F ");
5119 }
5120 if (dbg) printf("VARYING ");
5121 }
5122 else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
5123 GLuint swizzle;
5124 GLint index;
5125 /* attributes must be float, vec or mat */
5126 if (!_slang_type_is_float_vec_mat(var->type.specifier.type)) {
5127 slang_info_log_error(A->log,
5128 "attribute '%s' must be float/vector/matrix",
5129 varName);
5130 return GL_FALSE;
5131 }
5132
5133 if (prog) {
5134 /* user-defined vertex attribute */
5135 const GLint attr = -1; /* unknown */
5136 swizzle = _slang_var_swizzle(size, 0);
5137 index = _mesa_add_attribute(prog->Attributes, varName,
5138 size, datatype, attr);
5139 assert(index >= 0);
5140 index = VERT_ATTRIB_GENERIC0 + index;
5141 }
5142 else {
5143 /* pre-defined vertex attrib */
5144 index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
5145 assert(index >= 0);
5146 }
5147 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5148 if (dbg) printf("ATTRIB ");
5149 }
5150 else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
5151 GLuint swizzle = SWIZZLE_XYZW; /* silence compiler warning */
5152 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5153 &swizzle);
5154 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5155 if (dbg) printf("INPUT ");
5156 }
5157 else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
5158 if (type == SLANG_UNIT_VERTEX_BUILTIN) {
5159 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5160 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
5161 }
5162 else {
5163 GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
5164 GLint specialSize = 4; /* treat all fragment outputs as float[4] */
5165 assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
5166 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, specialSize);
5167 }
5168 if (dbg) printf("OUTPUT ");
5169 }
5170 else if (var->type.qualifier == SLANG_QUAL_CONST && !prog) {
5171 /* pre-defined global constant, like gl_MaxLights */
5172 store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
5173 if (dbg) printf("CONST ");
5174 }
5175 else {
5176 /* ordinary variable (may be const) */
5177 slang_ir_node *n;
5178
5179 /* IR node to declare the variable */
5180 n = _slang_gen_var_decl(A, var, var->initializer);
5181
5182 /* emit GPU instructions */
5183 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_FALSE, A->log);
5184
5185 _slang_free_ir_tree(n);
5186 }
5187
5188 if (dbg) printf("GLOBAL VAR %s idx %d\n", (char*) var->a_name,
5189 store ? store->Index : -2);
5190
5191 if (store)
5192 var->store = store; /* save var's storage info */
5193
5194 var->declared = GL_TRUE;
5195
5196 return success;
5197 }
5198
5199
5200 /**
5201 * Produce an IR tree from a function AST (fun->body).
5202 * Then call the code emitter to convert the IR tree into gl_program
5203 * instructions.
5204 */
5205 GLboolean
5206 _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
5207 {
5208 slang_ir_node *n;
5209 GLboolean success = GL_TRUE;
5210
5211 if (_mesa_strcmp((char *) fun->header.a_name, "main") != 0) {
5212 /* we only really generate code for main, all other functions get
5213 * inlined or codegen'd upon an actual call.
5214 */
5215 #if 0
5216 /* do some basic error checking though */
5217 if (fun->header.type.specifier.type != SLANG_SPEC_VOID) {
5218 /* check that non-void functions actually return something */
5219 slang_operation *op
5220 = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
5221 if (!op) {
5222 slang_info_log_error(A->log,
5223 "function \"%s\" has no return statement",
5224 (char *) fun->header.a_name);
5225 printf(
5226 "function \"%s\" has no return statement\n",
5227 (char *) fun->header.a_name);
5228 return GL_FALSE;
5229 }
5230 }
5231 #endif
5232 return GL_TRUE; /* not an error */
5233 }
5234
5235 #if 0
5236 printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
5237 slang_print_function(fun, 1);
5238 #endif
5239
5240 /* should have been allocated earlier: */
5241 assert(A->program->Parameters );
5242 assert(A->program->Varying);
5243 assert(A->vartable);
5244 A->CurLoop = NULL;
5245 A->CurLoopOper = NULL;
5246 A->CurFunction = fun;
5247
5248 /* fold constant expressions, etc. */
5249 _slang_simplify(fun->body, &A->space, A->atoms);
5250
5251 #if 0
5252 printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
5253 slang_print_function(fun, 1);
5254 #endif
5255
5256 /* Create an end-of-function label */
5257 A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
5258
5259 /* push new vartable scope */
5260 _slang_push_var_table(A->vartable);
5261
5262 /* Generate IR tree for the function body code */
5263 n = _slang_gen_operation(A, fun->body);
5264 if (n)
5265 n = new_node1(IR_SCOPE, n);
5266
5267 /* pop vartable, restore previous */
5268 _slang_pop_var_table(A->vartable);
5269
5270 if (!n) {
5271 /* XXX record error */
5272 return GL_FALSE;
5273 }
5274
5275 /* append an end-of-function-label to IR tree */
5276 n = new_seq(n, new_label(A->curFuncEndLabel));
5277
5278 /*_slang_label_delete(A->curFuncEndLabel);*/
5279 A->curFuncEndLabel = NULL;
5280
5281 #if 0
5282 printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
5283 slang_print_function(fun, 1);
5284 #endif
5285 #if 0
5286 printf("************* IR for %s *******\n", (char*)fun->header.a_name);
5287 _slang_print_ir_tree(n, 0);
5288 #endif
5289 #if 0
5290 printf("************* End codegen function ************\n\n");
5291 #endif
5292
5293 if (A->UnresolvedRefs) {
5294 /* Can't codegen at this time.
5295 * At link time we'll concatenate all the vertex shaders and/or all
5296 * the fragment shaders and try recompiling.
5297 */
5298 return GL_TRUE;
5299 }
5300
5301 /* Emit program instructions */
5302 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_TRUE, A->log);
5303 _slang_free_ir_tree(n);
5304
5305 /* free codegen context */
5306 /*
5307 _mesa_free(A->codegen);
5308 */
5309
5310 return success;
5311 }
5312