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