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