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