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