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