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