adfea890b3c329dac9b5175875fb5edc8dfbdb16
[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 start = (GLint) var->initializer->literal[0];
3188 }
3189 else {
3190 /* for (i=0; ... */
3191 varId = oper->children[0].children[0].children[0].a_id;
3192 start = (GLint) oper->children[0].children[0].children[1].literal[0];
3193 }
3194
3195 end = (GLint) oper->children[1].children[0].children[1].literal[0];
3196
3197 for (iter = start; iter < end; iter++) {
3198 slang_operation *body;
3199
3200 /* make a copy of the loop body */
3201 body = slang_operation_new(1);
3202 if (!body)
3203 return NULL;
3204
3205 if (!slang_operation_copy(body, &oper->children[3]))
3206 return NULL;
3207
3208 /* in body, replace instances of 'varId' with literal 'iter' */
3209 {
3210 slang_variable *oldVar;
3211 slang_operation *newOper;
3212
3213 oldVar = _slang_variable_locate(oper->locals, varId, GL_TRUE);
3214 if (!oldVar) {
3215 /* undeclared loop variable */
3216 slang_operation_delete(body);
3217 return NULL;
3218 }
3219
3220 newOper = slang_operation_new(1);
3221 newOper->type = SLANG_OPER_LITERAL_INT;
3222 newOper->literal_size = 1;
3223 newOper->literal[0] = (GLfloat) iter;
3224
3225 /* replace instances of the loop variable with newOper */
3226 slang_substitute(A, body, 1, &oldVar, &newOper, GL_FALSE);
3227 }
3228
3229 /* do IR codegen for body */
3230 n = _slang_gen_operation(A, body);
3231 if (!n)
3232 return NULL;
3233
3234 root = new_seq(root, n);
3235
3236 slang_operation_delete(body);
3237 }
3238
3239 return root;
3240 }
3241
3242
3243 /**
3244 * Replace 'continue' statement with 'break' inside a for-loop.
3245 * This is a recursive helper function used by _slang_gen_for_without_continue().
3246 */
3247 static void
3248 replace_continue_with_break(slang_assemble_ctx *A, slang_operation *oper)
3249 {
3250 switch (oper->type) {
3251 case SLANG_OPER_CONTINUE:
3252 oper->type = SLANG_OPER_BREAK;
3253 break;
3254 case SLANG_OPER_FOR:
3255 case SLANG_OPER_DO:
3256 case SLANG_OPER_WHILE:
3257 /* stop upon finding a nested loop */
3258 break;
3259 default:
3260 /* recurse */
3261 {
3262 GLuint i;
3263 for (i = 0; i < oper->num_children; i++) {
3264 replace_continue_with_break(A, slang_oper_child(oper, i));
3265 }
3266 }
3267 }
3268 }
3269
3270
3271 /**
3272 * Transform a for-loop so that continue statements are converted to breaks.
3273 * Then do normal IR code generation.
3274 *
3275 * Before:
3276 *
3277 * for (INIT; LOOPCOND; INCR) {
3278 * A;
3279 * if (IFCOND) {
3280 * continue;
3281 * }
3282 * B;
3283 * }
3284 *
3285 * After:
3286 *
3287 * {
3288 * bool _condFlag = 1;
3289 * for (INIT; _condFlag; ) {
3290 * for ( ; _condFlag = LOOPCOND; INCR) {
3291 * A;
3292 * if (IFCOND) {
3293 * break;
3294 * }
3295 * B;
3296 * }
3297 * if (_condFlag)
3298 * INCR;
3299 * }
3300 * }
3301 */
3302 static slang_ir_node *
3303 _slang_gen_for_without_continue(slang_assemble_ctx *A, slang_operation *oper)
3304 {
3305 slang_operation *top;
3306 slang_operation *outerFor, *innerFor, *init, *cond, *incr;
3307 slang_operation *lhs, *rhs;
3308
3309 assert(oper->type == SLANG_OPER_FOR);
3310
3311 top = slang_operation_new(1);
3312 top->type = SLANG_OPER_BLOCK_NEW_SCOPE;
3313 top->locals->outer_scope = oper->locals->outer_scope;
3314 slang_operation_add_children(top, 2);
3315
3316 /* declare: bool _condFlag = true */
3317 {
3318 slang_operation *condDecl = slang_oper_child(top, 0);
3319 slang_generate_declaration(A, top->locals, condDecl,
3320 SLANG_SPEC_BOOL, "_condFlag", GL_TRUE);
3321 }
3322
3323 /* build outer loop: for (INIT; _condFlag; ) { */
3324 outerFor = slang_oper_child(top, 1);
3325 outerFor->type = SLANG_OPER_FOR;
3326 slang_operation_add_children(outerFor, 4);
3327
3328 init = slang_oper_child(outerFor, 0);
3329 slang_operation_copy(init, slang_oper_child(oper, 0));
3330
3331 cond = slang_oper_child(outerFor, 1);
3332 cond->type = SLANG_OPER_IDENTIFIER;
3333 cond->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3334
3335 incr = slang_oper_child(outerFor, 2);
3336 incr->type = SLANG_OPER_VOID;
3337
3338 /* body of the outer loop */
3339 {
3340 slang_operation *block = slang_oper_child(outerFor, 3);
3341
3342 slang_operation_add_children(block, 2);
3343 block->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
3344
3345 /* build inner loop: for ( ; _condFlag = LOOPCOND; INCR) { */
3346 {
3347 innerFor = slang_oper_child(block, 0);
3348
3349 /* make copy of orig loop */
3350 slang_operation_copy(innerFor, oper);
3351 assert(innerFor->type == SLANG_OPER_FOR);
3352 innerFor->locals->outer_scope = block->locals;
3353
3354 init = slang_oper_child(innerFor, 0);
3355 init->type = SLANG_OPER_VOID; /* leak? */
3356
3357 cond = slang_oper_child(innerFor, 1);
3358 slang_operation_destruct(cond);
3359 cond->type = SLANG_OPER_ASSIGN;
3360 cond->locals = _slang_variable_scope_new(innerFor->locals);
3361 slang_operation_add_children(cond, 2);
3362
3363 lhs = slang_oper_child(cond, 0);
3364 lhs->type = SLANG_OPER_IDENTIFIER;
3365 lhs->a_id = slang_atom_pool_atom(A->atoms, "_condFlag");
3366
3367 rhs = slang_oper_child(cond, 1);
3368 slang_operation_copy(rhs, slang_oper_child(oper, 1));
3369 }
3370
3371 /* if (_condFlag) INCR; */
3372 {
3373 slang_operation *ifop = slang_oper_child(block, 1);
3374 ifop->type = SLANG_OPER_IF;
3375 slang_operation_add_children(ifop, 2);
3376
3377 /* re-use cond node build above */
3378 slang_operation_copy(slang_oper_child(ifop, 0), cond);
3379
3380 /* incr node from original for-loop operation */
3381 slang_operation_copy(slang_oper_child(ifop, 1),
3382 slang_oper_child(oper, 2));
3383 }
3384
3385 /* finally, replace "continue" with "break" in the inner for-loop */
3386 replace_continue_with_break(A, slang_oper_child(innerFor, 3));
3387 }
3388
3389 return _slang_gen_operation(A, top);
3390 }
3391
3392
3393
3394 /**
3395 * Generate IR for a for-loop. Unrolling will be done when possible.
3396 */
3397 static slang_ir_node *
3398 _slang_gen_for(slang_assemble_ctx * A, slang_operation *oper)
3399 {
3400 GLboolean unroll;
3401
3402 if (!A->EmitContReturn) {
3403 /* We don't want to emit CONT instructions. If this for-loop has
3404 * a continue, translate it away.
3405 */
3406 if (_slang_loop_contains_continue(slang_oper_child(oper, 3))) {
3407 return _slang_gen_for_without_continue(A, oper);
3408 }
3409 }
3410
3411 unroll = _slang_can_unroll_for_loop(A, oper);
3412 if (unroll) {
3413 slang_ir_node *code = _slang_unroll_for_loop(A, oper);
3414 if (code)
3415 return code;
3416 }
3417
3418 assert(oper->type == SLANG_OPER_FOR);
3419
3420 /* conventional for-loop code generation */
3421 {
3422 /*
3423 * init code (child[0])
3424 * LOOP:
3425 * BREAK if !expr (child[1])
3426 * body code (child[3])
3427 * tail code:
3428 * incr code (child[2]) // XXX continue here
3429 */
3430 slang_ir_node *loop, *cond, *breakIf, *body, *init, *incr;
3431 init = _slang_gen_operation(A, &oper->children[0]);
3432 loop = new_loop(NULL);
3433
3434 /* save loop state */
3435 push_loop(A, oper, loop);
3436
3437 cond = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
3438 breakIf = new_break_if_true(A, cond);
3439 body = _slang_gen_operation(A, &oper->children[3]);
3440 incr = _slang_gen_operation(A, &oper->children[2]);
3441
3442 loop->Children[0] = new_seq(breakIf, body);
3443 loop->Children[1] = incr; /* tail code */
3444
3445 /* restore loop state */
3446 pop_loop(A);
3447
3448 return new_seq(init, loop);
3449 }
3450 }
3451
3452
3453 static slang_ir_node *
3454 _slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
3455 {
3456 slang_ir_node *n, *cont, *incr = NULL, *loopNode;
3457
3458 assert(oper->type == SLANG_OPER_CONTINUE);
3459 loopNode = current_loop_ir(A);
3460 assert(loopNode);
3461 assert(loopNode->Opcode == IR_LOOP);
3462
3463 cont = new_node0(IR_CONT);
3464 if (cont) {
3465 cont->Parent = loopNode;
3466 /* insert this node at head of linked list of cont/break instructions */
3467 cont->List = loopNode->List;
3468 loopNode->List = cont;
3469 }
3470
3471 n = new_seq(incr, cont);
3472 return n;
3473 }
3474
3475
3476 /**
3477 * Determine if the given operation is of a specific type.
3478 */
3479 static GLboolean
3480 is_operation_type(const slang_operation *oper, slang_operation_type type)
3481 {
3482 if (oper->type == type)
3483 return GL_TRUE;
3484 else if ((oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
3485 oper->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) &&
3486 oper->num_children == 1)
3487 return is_operation_type(&oper->children[0], type);
3488 else
3489 return GL_FALSE;
3490 }
3491
3492
3493 /**
3494 * Generate IR tree for an if/then/else conditional using high-level
3495 * IR_IF instruction.
3496 */
3497 static slang_ir_node *
3498 _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
3499 {
3500 /*
3501 * eval expr (child[0])
3502 * IF expr THEN
3503 * if-body code
3504 * ELSE
3505 * else-body code
3506 * ENDIF
3507 */
3508 const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
3509 slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
3510 GLboolean isConst, constTrue;
3511
3512 /* type-check expression */
3513 if (!_slang_is_boolean(A, &oper->children[0])) {
3514 slang_info_log_error(A->log, "boolean expression expected for 'if'");
3515 return NULL;
3516 }
3517
3518 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3519 slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
3520 return NULL;
3521 }
3522
3523 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
3524 if (isConst) {
3525 if (constTrue) {
3526 /* if (true) ... */
3527 return _slang_gen_operation(A, &oper->children[1]);
3528 }
3529 else {
3530 /* if (false) ... */
3531 return _slang_gen_operation(A, &oper->children[2]);
3532 }
3533 }
3534
3535 cond = _slang_gen_operation(A, &oper->children[0]);
3536 cond = new_cond(cond);
3537
3538 if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)
3539 && !haveElseClause) {
3540 /* Special case: generate a conditional break */
3541 ifBody = new_break_if_true(A, cond);
3542 return ifBody;
3543 }
3544 else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)
3545 && !haveElseClause
3546 && current_loop_oper(A)
3547 && current_loop_oper(A)->type != SLANG_OPER_FOR) {
3548 /* Special case: generate a conditional continue */
3549 ifBody = new_cont_if_true(A, cond);
3550 return ifBody;
3551 }
3552 else {
3553 /* general case */
3554 ifBody = _slang_gen_operation(A, &oper->children[1]);
3555 if (haveElseClause)
3556 elseBody = _slang_gen_operation(A, &oper->children[2]);
3557 else
3558 elseBody = NULL;
3559 ifNode = new_if(cond, ifBody, elseBody);
3560 return ifNode;
3561 }
3562 }
3563
3564
3565
3566 static slang_ir_node *
3567 _slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
3568 {
3569 slang_ir_node *n;
3570
3571 assert(oper->type == SLANG_OPER_NOT);
3572
3573 /* type-check expression */
3574 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3575 slang_info_log_error(A->log,
3576 "scalar/boolean expression expected for '!'");
3577 return NULL;
3578 }
3579
3580 n = _slang_gen_operation(A, &oper->children[0]);
3581 if (n)
3582 return new_not(n);
3583 else
3584 return NULL;
3585 }
3586
3587
3588 static slang_ir_node *
3589 _slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
3590 {
3591 slang_ir_node *n1, *n2;
3592
3593 assert(oper->type == SLANG_OPER_LOGICALXOR);
3594
3595 if (!_slang_is_scalar_or_boolean(A, &oper->children[0]) ||
3596 !_slang_is_scalar_or_boolean(A, &oper->children[0])) {
3597 slang_info_log_error(A->log,
3598 "scalar/boolean expressions expected for '^^'");
3599 return NULL;
3600 }
3601
3602 n1 = _slang_gen_operation(A, &oper->children[0]);
3603 if (!n1)
3604 return NULL;
3605 n2 = _slang_gen_operation(A, &oper->children[1]);
3606 if (!n2)
3607 return NULL;
3608 return new_node2(IR_NOTEQUAL, n1, n2);
3609 }
3610
3611
3612 /**
3613 * Generate IR node for storage of a temporary of given size.
3614 */
3615 static slang_ir_node *
3616 _slang_gen_temporary(GLint size)
3617 {
3618 slang_ir_storage *store;
3619 slang_ir_node *n = NULL;
3620
3621 store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -2, size);
3622 if (store) {
3623 n = new_node0(IR_VAR_DECL);
3624 if (n) {
3625 n->Store = store;
3626 }
3627 else {
3628 _slang_free(store);
3629 }
3630 }
3631 return n;
3632 }
3633
3634
3635 /**
3636 * Generate program constants for an array.
3637 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
3638 * This will allocate and initialize three vector constants, storing
3639 * the array in constant memory, not temporaries like a non-const array.
3640 * This can also be used for uniform array initializers.
3641 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
3642 */
3643 static GLboolean
3644 make_constant_array(slang_assemble_ctx *A,
3645 slang_variable *var,
3646 slang_operation *initializer)
3647 {
3648 struct gl_program *prog = A->program;
3649 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3650 const char *varName = (char *) var->a_name;
3651 const GLuint numElements = initializer->num_children;
3652 GLint size;
3653 GLuint i, j;
3654 GLfloat *values;
3655
3656 if (!var->store) {
3657 var->store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -6, -6);
3658 }
3659 size = var->store->Size;
3660
3661 assert(var->type.qualifier == SLANG_QUAL_CONST ||
3662 var->type.qualifier == SLANG_QUAL_UNIFORM);
3663 assert(initializer->type == SLANG_OPER_CALL);
3664 assert(initializer->array_constructor);
3665
3666 values = (GLfloat *) malloc(numElements * 4 * sizeof(GLfloat));
3667
3668 /* convert constructor params into ordinary floats */
3669 for (i = 0; i < numElements; i++) {
3670 const slang_operation *op = &initializer->children[i];
3671 if (op->type != SLANG_OPER_LITERAL_FLOAT) {
3672 /* unsupported type for this optimization */
3673 free(values);
3674 return GL_FALSE;
3675 }
3676 for (j = 0; j < op->literal_size; j++) {
3677 values[i * 4 + j] = op->literal[j];
3678 }
3679 for ( ; j < 4; j++) {
3680 values[i * 4 + j] = 0.0f;
3681 }
3682 }
3683
3684 /* slightly different paths for constants vs. uniforms */
3685 if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3686 var->store->File = PROGRAM_UNIFORM;
3687 var->store->Index = _mesa_add_uniform(prog->Parameters, varName,
3688 size, datatype, values);
3689 }
3690 else {
3691 var->store->File = PROGRAM_CONSTANT;
3692 var->store->Index = _mesa_add_named_constant(prog->Parameters, varName,
3693 values, size);
3694 }
3695 assert(var->store->Size == size);
3696
3697 free(values);
3698
3699 return GL_TRUE;
3700 }
3701
3702
3703
3704 /**
3705 * Generate IR node for allocating/declaring a variable (either a local or
3706 * a global).
3707 * Generally, this involves allocating an slang_ir_storage instance for the
3708 * variable, choosing a register file (temporary, constant, etc).
3709 * For ordinary variables we do not yet allocate storage though. We do that
3710 * when we find the first actual use of the variable to avoid allocating temp
3711 * regs that will never get used.
3712 * At this time, uniforms are always allocated space in this function.
3713 *
3714 * \param initializer Optional initializer expression for the variable.
3715 */
3716 static slang_ir_node *
3717 _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var,
3718 slang_operation *initializer)
3719 {
3720 const char *varName = (const char *) var->a_name;
3721 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
3722 slang_ir_node *varDecl, *n;
3723 slang_ir_storage *store;
3724 GLint arrayLen, size, totalSize; /* if array then totalSize > size */
3725 gl_register_file file;
3726
3727 /*assert(!var->declared);*/
3728 var->declared = GL_TRUE;
3729
3730 /* determine GPU register file for simple cases */
3731 if (is_sampler_type(&var->type)) {
3732 file = PROGRAM_SAMPLER;
3733 }
3734 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
3735 file = PROGRAM_UNIFORM;
3736 }
3737 else {
3738 file = PROGRAM_TEMPORARY;
3739 }
3740
3741 size = _slang_sizeof_type_specifier(&var->type.specifier);
3742 if (size <= 0) {
3743 slang_info_log_error(A->log, "invalid declaration for '%s'", varName);
3744 return NULL;
3745 }
3746
3747 arrayLen = _slang_array_length(var);
3748 totalSize = _slang_array_size(size, arrayLen);
3749
3750 /* Allocate IR node for the declaration */
3751 varDecl = new_node0(IR_VAR_DECL);
3752 if (!varDecl)
3753 return NULL;
3754
3755 /* Allocate slang_ir_storage for this variable if needed.
3756 * Note that we may not actually allocate a constant or temporary register
3757 * until later.
3758 */
3759 if (!var->store) {
3760 GLint index = -7; /* TBD / unknown */
3761 var->store = _slang_new_ir_storage(file, index, totalSize);
3762 if (!var->store)
3763 return NULL; /* out of memory */
3764 }
3765
3766 /* set the IR node's Var and Store pointers */
3767 varDecl->Var = var;
3768 varDecl->Store = var->store;
3769
3770
3771 store = var->store;
3772
3773 /* if there's an initializer, generate IR for the expression */
3774 if (initializer) {
3775 slang_ir_node *varRef, *init;
3776
3777 if (var->type.qualifier == SLANG_QUAL_CONST) {
3778 /* if the variable is const, the initializer must be a const
3779 * expression as well.
3780 */
3781 #if 0
3782 if (!_slang_is_constant_expr(initializer)) {
3783 slang_info_log_error(A->log,
3784 "initializer for %s not constant", varName);
3785 return NULL;
3786 }
3787 #endif
3788 }
3789
3790 if (var->type.qualifier == SLANG_QUAL_UNIFORM &&
3791 !A->allow_uniform_initializers) {
3792 slang_info_log_error(A->log,
3793 "initializer for uniform %s not allowed",
3794 varName);
3795 return NULL;
3796 }
3797
3798 /* IR for the variable we're initializing */
3799 varRef = new_var(A, var);
3800 if (!varRef) {
3801 slang_info_log_error(A->log, "out of memory");
3802 return NULL;
3803 }
3804
3805 /* constant-folding, etc here */
3806 _slang_simplify(initializer, &A->space, A->atoms);
3807
3808 /* look for simple constant-valued variables and uniforms */
3809 if (var->type.qualifier == SLANG_QUAL_CONST ||
3810 var->type.qualifier == SLANG_QUAL_UNIFORM) {
3811
3812 if (initializer->type == SLANG_OPER_CALL &&
3813 initializer->array_constructor) {
3814 /* array initializer */
3815 if (make_constant_array(A, var, initializer))
3816 return varRef;
3817 }
3818 else if (initializer->type == SLANG_OPER_LITERAL_FLOAT ||
3819 initializer->type == SLANG_OPER_LITERAL_INT) {
3820 /* simple float/vector initializer */
3821 if (store->File == PROGRAM_UNIFORM) {
3822 store->Index = _mesa_add_uniform(A->program->Parameters,
3823 varName,
3824 totalSize, datatype,
3825 initializer->literal);
3826 store->Swizzle = _slang_var_swizzle(size, 0);
3827 return varRef;
3828 }
3829 #if 0
3830 else {
3831 store->File = PROGRAM_CONSTANT;
3832 store->Index = _mesa_add_named_constant(A->program->Parameters,
3833 varName,
3834 initializer->literal,
3835 totalSize);
3836 store->Swizzle = _slang_var_swizzle(size, 0);
3837 return varRef;
3838 }
3839 #endif
3840 }
3841 }
3842
3843 /* IR for initializer */
3844 init = _slang_gen_operation(A, initializer);
3845 if (!init)
3846 return NULL;
3847
3848 /* XXX remove this when type checking is added above */
3849 if (init->Store && init->Store->Size != totalSize) {
3850 slang_info_log_error(A->log, "invalid assignment (wrong types)");
3851 return NULL;
3852 }
3853
3854 /* assign RHS to LHS */
3855 n = new_node2(IR_COPY, varRef, init);
3856 n = new_seq(varDecl, n);
3857 }
3858 else {
3859 /* no initializer */
3860 n = varDecl;
3861 }
3862
3863 if (store->File == PROGRAM_UNIFORM && store->Index < 0) {
3864 /* always need to allocate storage for uniforms at this point */
3865 store->Index = _mesa_add_uniform(A->program->Parameters, varName,
3866 totalSize, datatype, NULL);
3867 store->Swizzle = _slang_var_swizzle(size, 0);
3868 }
3869
3870 #if 0
3871 printf("%s var %p %s store=%p index=%d size=%d\n",
3872 __FUNCTION__, (void *) var, (char *) varName,
3873 (void *) store, store->Index, store->Size);
3874 #endif
3875
3876 return n;
3877 }
3878
3879
3880 /**
3881 * Generate code for a selection expression: b ? x : y
3882 * XXX In some cases we could implement a selection expression
3883 * with an LRP instruction (use the boolean as the interpolant).
3884 * Otherwise, we use an IF/ELSE/ENDIF construct.
3885 */
3886 static slang_ir_node *
3887 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
3888 {
3889 slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
3890 slang_ir_node *tmpDecl, *tmpVar, *tree;
3891 slang_typeinfo type0, type1, type2;
3892 int size, isBool, isEqual;
3893
3894 assert(oper->type == SLANG_OPER_SELECT);
3895 assert(oper->num_children == 3);
3896
3897 /* type of children[0] must be boolean */
3898 slang_typeinfo_construct(&type0);
3899 typeof_operation(A, &oper->children[0], &type0);
3900 isBool = (type0.spec.type == SLANG_SPEC_BOOL);
3901 slang_typeinfo_destruct(&type0);
3902 if (!isBool) {
3903 slang_info_log_error(A->log, "selector type is not boolean");
3904 return NULL;
3905 }
3906
3907 slang_typeinfo_construct(&type1);
3908 slang_typeinfo_construct(&type2);
3909 typeof_operation(A, &oper->children[1], &type1);
3910 typeof_operation(A, &oper->children[2], &type2);
3911 isEqual = slang_type_specifier_equal(&type1.spec, &type2.spec);
3912 slang_typeinfo_destruct(&type1);
3913 slang_typeinfo_destruct(&type2);
3914 if (!isEqual) {
3915 slang_info_log_error(A->log, "incompatible types for ?: operator");
3916 return NULL;
3917 }
3918
3919 /* size of x or y's type */
3920 size = _slang_sizeof_type_specifier(&type1.spec);
3921 assert(size > 0);
3922
3923 /* temporary var */
3924 tmpDecl = _slang_gen_temporary(size);
3925
3926 /* the condition (child 0) */
3927 cond = _slang_gen_operation(A, &oper->children[0]);
3928 cond = new_cond(cond);
3929
3930 /* if-true body (child 1) */
3931 tmpVar = new_node0(IR_VAR);
3932 tmpVar->Store = tmpDecl->Store;
3933 trueExpr = _slang_gen_operation(A, &oper->children[1]);
3934 trueNode = new_node2(IR_COPY, tmpVar, trueExpr);
3935
3936 /* if-false body (child 2) */
3937 tmpVar = new_node0(IR_VAR);
3938 tmpVar->Store = tmpDecl->Store;
3939 falseExpr = _slang_gen_operation(A, &oper->children[2]);
3940 falseNode = new_node2(IR_COPY, tmpVar, falseExpr);
3941
3942 ifNode = new_if(cond, trueNode, falseNode);
3943
3944 /* tmp var value */
3945 tmpVar = new_node0(IR_VAR);
3946 tmpVar->Store = tmpDecl->Store;
3947
3948 tree = new_seq(ifNode, tmpVar);
3949 tree = new_seq(tmpDecl, tree);
3950
3951 /*_slang_print_ir_tree(tree, 10);*/
3952 return tree;
3953 }
3954
3955
3956 /**
3957 * Generate code for &&.
3958 */
3959 static slang_ir_node *
3960 _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
3961 {
3962 /* rewrite "a && b" as "a ? b : false" */
3963 slang_operation *select;
3964 slang_ir_node *n;
3965
3966 select = slang_operation_new(1);
3967 select->type = SLANG_OPER_SELECT;
3968 slang_operation_add_children(select, 3);
3969
3970 slang_operation_copy(slang_oper_child(select, 0), &oper->children[0]);
3971 slang_operation_copy(slang_oper_child(select, 1), &oper->children[1]);
3972 slang_operation_literal_bool(slang_oper_child(select, 2), GL_FALSE);
3973
3974 n = _slang_gen_select(A, select);
3975 return n;
3976 }
3977
3978
3979 /**
3980 * Generate code for ||.
3981 */
3982 static slang_ir_node *
3983 _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
3984 {
3985 /* rewrite "a || b" as "a ? true : b" */
3986 slang_operation *select;
3987 slang_ir_node *n;
3988
3989 select = slang_operation_new(1);
3990 select->type = SLANG_OPER_SELECT;
3991 slang_operation_add_children(select, 3);
3992
3993 slang_operation_copy(slang_oper_child(select, 0), &oper->children[0]);
3994 slang_operation_literal_bool(slang_oper_child(select, 1), GL_TRUE);
3995 slang_operation_copy(slang_oper_child(select, 2), &oper->children[1]);
3996
3997 n = _slang_gen_select(A, select);
3998 return n;
3999 }
4000
4001
4002 /**
4003 * Generate IR tree for a return statement.
4004 */
4005 static slang_ir_node *
4006 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
4007 {
4008 assert(oper->type == SLANG_OPER_RETURN);
4009 return new_return(A->curFuncEndLabel);
4010 }
4011
4012
4013 #if 0
4014 /**
4015 * Determine if the given operation/expression is const-valued.
4016 */
4017 static GLboolean
4018 _slang_is_constant_expr(const slang_operation *oper)
4019 {
4020 slang_variable *var;
4021 GLuint i;
4022
4023 switch (oper->type) {
4024 case SLANG_OPER_IDENTIFIER:
4025 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4026 if (var && var->type.qualifier == SLANG_QUAL_CONST)
4027 return GL_TRUE;
4028 return GL_FALSE;
4029 default:
4030 for (i = 0; i < oper->num_children; i++) {
4031 if (!_slang_is_constant_expr(&oper->children[i]))
4032 return GL_FALSE;
4033 }
4034 return GL_TRUE;
4035 }
4036 }
4037 #endif
4038
4039
4040 /**
4041 * Check if an assignment of type t1 to t0 is legal.
4042 * XXX more cases needed.
4043 */
4044 static GLboolean
4045 _slang_assignment_compatible(slang_assemble_ctx *A,
4046 slang_operation *op0,
4047 slang_operation *op1)
4048 {
4049 slang_typeinfo t0, t1;
4050 GLuint sz0, sz1;
4051
4052 if (op0->type == SLANG_OPER_POSTINCREMENT ||
4053 op0->type == SLANG_OPER_POSTDECREMENT) {
4054 return GL_FALSE;
4055 }
4056
4057 slang_typeinfo_construct(&t0);
4058 typeof_operation(A, op0, &t0);
4059
4060 slang_typeinfo_construct(&t1);
4061 typeof_operation(A, op1, &t1);
4062
4063 sz0 = _slang_sizeof_type_specifier(&t0.spec);
4064 sz1 = _slang_sizeof_type_specifier(&t1.spec);
4065
4066 #if 1
4067 if (sz0 != sz1) {
4068 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
4069 return GL_FALSE;
4070 }
4071 #endif
4072
4073 if (t0.spec.type == SLANG_SPEC_STRUCT &&
4074 t1.spec.type == SLANG_SPEC_STRUCT &&
4075 t0.spec._struct->a_name != t1.spec._struct->a_name)
4076 return GL_FALSE;
4077
4078 if (t0.spec.type == SLANG_SPEC_FLOAT &&
4079 t1.spec.type == SLANG_SPEC_BOOL)
4080 return GL_FALSE;
4081
4082 #if 0 /* not used just yet - causes problems elsewhere */
4083 if (t0.spec.type == SLANG_SPEC_INT &&
4084 t1.spec.type == SLANG_SPEC_FLOAT)
4085 return GL_FALSE;
4086 #endif
4087
4088 if (t0.spec.type == SLANG_SPEC_BOOL &&
4089 t1.spec.type == SLANG_SPEC_FLOAT)
4090 return GL_FALSE;
4091
4092 if (t0.spec.type == SLANG_SPEC_BOOL &&
4093 t1.spec.type == SLANG_SPEC_INT)
4094 return GL_FALSE;
4095
4096 return GL_TRUE;
4097 }
4098
4099
4100 /**
4101 * Generate IR tree for a local variable declaration.
4102 * Basically do some error checking and call _slang_gen_var_decl().
4103 */
4104 static slang_ir_node *
4105 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
4106 {
4107 const char *varName = (char *) oper->a_id;
4108 slang_variable *var;
4109 slang_ir_node *varDecl;
4110 slang_operation *initializer;
4111
4112 assert(oper->type == SLANG_OPER_VARIABLE_DECL);
4113 assert(oper->num_children <= 1);
4114
4115
4116 /* lookup the variable by name */
4117 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
4118 if (!var)
4119 return NULL; /* "shouldn't happen" */
4120
4121 if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4122 var->type.qualifier == SLANG_QUAL_VARYING ||
4123 var->type.qualifier == SLANG_QUAL_UNIFORM) {
4124 /* can't declare attribute/uniform vars inside functions */
4125 slang_info_log_error(A->log,
4126 "local variable '%s' cannot be an attribute/uniform/varying",
4127 varName);
4128 return NULL;
4129 }
4130
4131 #if 0
4132 if (v->declared) {
4133 slang_info_log_error(A->log, "variable '%s' redeclared", varName);
4134 return NULL;
4135 }
4136 #endif
4137
4138 /* check if the var has an initializer */
4139 if (oper->num_children > 0) {
4140 assert(oper->num_children == 1);
4141 initializer = &oper->children[0];
4142 }
4143 else if (var->initializer) {
4144 initializer = var->initializer;
4145 }
4146 else {
4147 initializer = NULL;
4148 }
4149
4150 if (initializer) {
4151 /* check/compare var type and initializer type */
4152 if (!_slang_assignment_compatible(A, oper, initializer)) {
4153 slang_info_log_error(A->log, "incompatible types in assignment");
4154 return NULL;
4155 }
4156 }
4157 else {
4158 if (var->type.qualifier == SLANG_QUAL_CONST) {
4159 slang_info_log_error(A->log,
4160 "const-qualified variable '%s' requires initializer",
4161 varName);
4162 return NULL;
4163 }
4164 }
4165
4166 /* Generate IR node */
4167 varDecl = _slang_gen_var_decl(A, var, initializer);
4168 if (!varDecl)
4169 return NULL;
4170
4171 return varDecl;
4172 }
4173
4174
4175 /**
4176 * Generate IR tree for a reference to a variable (such as in an expression).
4177 * This is different from a variable declaration.
4178 */
4179 static slang_ir_node *
4180 _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
4181 {
4182 /* If there's a variable associated with this oper (from inlining)
4183 * use it. Otherwise, use the oper's var id.
4184 */
4185 slang_atom name = oper->var ? oper->var->a_name : oper->a_id;
4186 slang_variable *var = _slang_variable_locate(oper->locals, name, GL_TRUE);
4187 slang_ir_node *n;
4188 if (!var) {
4189 slang_info_log_error(A->log, "undefined variable '%s'", (char *) name);
4190 return NULL;
4191 }
4192 assert(var->declared);
4193 n = new_var(A, var);
4194 return n;
4195 }
4196
4197
4198
4199 /**
4200 * Return the number of components actually named by the swizzle.
4201 * Recall that swizzles may have undefined/don't-care values.
4202 */
4203 static GLuint
4204 swizzle_size(GLuint swizzle)
4205 {
4206 GLuint size = 0, i;
4207 for (i = 0; i < 4; i++) {
4208 GLuint swz = GET_SWZ(swizzle, i);
4209 size += (swz >= 0 && swz <= 3);
4210 }
4211 return size;
4212 }
4213
4214
4215 static slang_ir_node *
4216 _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
4217 {
4218 slang_ir_node *n = new_node1(IR_SWIZZLE, child);
4219 assert(child);
4220 if (n) {
4221 assert(!n->Store);
4222 n->Store = _slang_new_ir_storage_relative(0,
4223 swizzle_size(swizzle),
4224 child->Store);
4225 n->Store->Swizzle = swizzle;
4226 }
4227 return n;
4228 }
4229
4230
4231 static GLboolean
4232 is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
4233 {
4234 while (store->Parent)
4235 store = store->Parent;
4236
4237 if (!(store->File == PROGRAM_OUTPUT ||
4238 store->File == PROGRAM_TEMPORARY ||
4239 (store->File == PROGRAM_VARYING &&
4240 A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
4241 return GL_FALSE;
4242 }
4243 else {
4244 return GL_TRUE;
4245 }
4246 }
4247
4248
4249 /**
4250 * Walk up an IR storage path to compute the final swizzle.
4251 * This is used when we find an expression such as "foo.xz.yx".
4252 */
4253 static GLuint
4254 root_swizzle(const slang_ir_storage *st)
4255 {
4256 GLuint swizzle = st->Swizzle;
4257 while (st->Parent) {
4258 st = st->Parent;
4259 swizzle = _slang_swizzle_swizzle(st->Swizzle, swizzle);
4260 }
4261 return swizzle;
4262 }
4263
4264
4265 /**
4266 * Generate IR tree for an assignment (=).
4267 */
4268 static slang_ir_node *
4269 _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
4270 {
4271 slang_operation *pred = NULL;
4272 slang_ir_node *n = NULL;
4273
4274 if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
4275 /* Check that var is writeable */
4276 const char *varName = (char *) oper->children[0].a_id;
4277 slang_variable *var
4278 = _slang_variable_locate(oper->children[0].locals,
4279 oper->children[0].a_id, GL_TRUE);
4280 if (!var) {
4281 slang_info_log_error(A->log, "undefined variable '%s'", varName);
4282 return NULL;
4283 }
4284
4285 if (var->type.qualifier == SLANG_QUAL_CONST ||
4286 var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
4287 var->type.qualifier == SLANG_QUAL_UNIFORM ||
4288 (var->type.qualifier == SLANG_QUAL_VARYING &&
4289 A->program->Target == GL_FRAGMENT_PROGRAM_ARB)) {
4290 slang_info_log_error(A->log,
4291 "illegal assignment to read-only variable '%s'",
4292 varName);
4293 return NULL;
4294 }
4295
4296 /* check if we need to predicate this assignment based on __notRetFlag */
4297 if ((var->is_global ||
4298 var->type.qualifier == SLANG_QUAL_OUT ||
4299 var->type.qualifier == SLANG_QUAL_INOUT) && A->UseReturnFlag) {
4300 /* create predicate, used below */
4301 pred = slang_operation_new(1);
4302 pred->type = SLANG_OPER_IDENTIFIER;
4303 pred->a_id = slang_atom_pool_atom(A->atoms, "__notRetFlag");
4304 pred->locals->outer_scope = oper->locals->outer_scope;
4305 }
4306 }
4307
4308 if (oper->children[0].type == SLANG_OPER_IDENTIFIER &&
4309 oper->children[1].type == SLANG_OPER_CALL) {
4310 /* Special case of: x = f(a, b)
4311 * Replace with f(a, b, x) (where x == hidden __retVal out param)
4312 *
4313 * XXX this could be even more effective if we could accomodate
4314 * cases such as "v.x = f();" - would help with typical vertex
4315 * transformation.
4316 */
4317 n = _slang_gen_function_call_name(A,
4318 (const char *) oper->children[1].a_id,
4319 &oper->children[1], &oper->children[0]);
4320 }
4321 else {
4322 slang_ir_node *lhs, *rhs;
4323
4324 /* lhs and rhs type checking */
4325 if (!_slang_assignment_compatible(A,
4326 &oper->children[0],
4327 &oper->children[1])) {
4328 slang_info_log_error(A->log, "incompatible types in assignment");
4329 return NULL;
4330 }
4331
4332 lhs = _slang_gen_operation(A, &oper->children[0]);
4333 if (!lhs) {
4334 return NULL;
4335 }
4336
4337 if (!lhs->Store) {
4338 slang_info_log_error(A->log,
4339 "invalid left hand side for assignment");
4340 return NULL;
4341 }
4342
4343 /* check that lhs is writable */
4344 if (!is_store_writable(A, lhs->Store)) {
4345 slang_info_log_error(A->log,
4346 "illegal assignment to read-only l-value");
4347 return NULL;
4348 }
4349
4350 rhs = _slang_gen_operation(A, &oper->children[1]);
4351 if (lhs && rhs) {
4352 /* convert lhs swizzle into writemask */
4353 const GLuint swizzle = root_swizzle(lhs->Store);
4354 GLuint writemask, newSwizzle = 0x0;
4355 if (!swizzle_to_writemask(A, swizzle, &writemask, &newSwizzle)) {
4356 /* Non-simple writemask, need to swizzle right hand side in
4357 * order to put components into the right place.
4358 */
4359 rhs = _slang_gen_swizzle(rhs, newSwizzle);
4360 }
4361 n = new_node2(IR_COPY, lhs, rhs);
4362 }
4363 else {
4364 return NULL;
4365 }
4366 }
4367
4368 if (n && pred) {
4369 /* predicate the assignment code on __notRetFlag */
4370 slang_ir_node *top, *cond;
4371
4372 cond = _slang_gen_operation(A, pred);
4373 top = new_if(cond, n, NULL);
4374 return top;
4375 }
4376 return n;
4377 }
4378
4379
4380 /**
4381 * Generate IR tree for referencing a field in a struct (or basic vector type)
4382 */
4383 static slang_ir_node *
4384 _slang_gen_struct_field(slang_assemble_ctx * A, slang_operation *oper)
4385 {
4386 slang_typeinfo ti;
4387
4388 /* type of struct */
4389 slang_typeinfo_construct(&ti);
4390 typeof_operation(A, &oper->children[0], &ti);
4391
4392 if (_slang_type_is_vector(ti.spec.type)) {
4393 /* the field should be a swizzle */
4394 const GLuint rows = _slang_type_dim(ti.spec.type);
4395 slang_swizzle swz;
4396 slang_ir_node *n;
4397 GLuint swizzle;
4398 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4399 slang_info_log_error(A->log, "Bad swizzle");
4400 return NULL;
4401 }
4402 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4403 swz.swizzle[1],
4404 swz.swizzle[2],
4405 swz.swizzle[3]);
4406
4407 n = _slang_gen_operation(A, &oper->children[0]);
4408 /* create new parent node with swizzle */
4409 if (n)
4410 n = _slang_gen_swizzle(n, swizzle);
4411 return n;
4412 }
4413 else if ( ti.spec.type == SLANG_SPEC_FLOAT
4414 || ti.spec.type == SLANG_SPEC_INT
4415 || ti.spec.type == SLANG_SPEC_BOOL) {
4416 const GLuint rows = 1;
4417 slang_swizzle swz;
4418 slang_ir_node *n;
4419 GLuint swizzle;
4420 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
4421 slang_info_log_error(A->log, "Bad swizzle");
4422 }
4423 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
4424 swz.swizzle[1],
4425 swz.swizzle[2],
4426 swz.swizzle[3]);
4427 n = _slang_gen_operation(A, &oper->children[0]);
4428 /* create new parent node with swizzle */
4429 n = _slang_gen_swizzle(n, swizzle);
4430 return n;
4431 }
4432 else {
4433 /* the field is a structure member (base.field) */
4434 /* oper->children[0] is the base */
4435 /* oper->a_id is the field name */
4436 slang_ir_node *base, *n;
4437 slang_typeinfo field_ti;
4438 GLint fieldSize, fieldOffset = -1;
4439
4440 /* type of field */
4441 slang_typeinfo_construct(&field_ti);
4442 typeof_operation(A, oper, &field_ti);
4443
4444 fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
4445 if (fieldSize > 0)
4446 fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
4447
4448 if (fieldSize == 0 || fieldOffset < 0) {
4449 const char *structName;
4450 if (ti.spec._struct)
4451 structName = (char *) ti.spec._struct->a_name;
4452 else
4453 structName = "unknown";
4454 slang_info_log_error(A->log,
4455 "\"%s\" is not a member of struct \"%s\"",
4456 (char *) oper->a_id, structName);
4457 return NULL;
4458 }
4459 assert(fieldSize >= 0);
4460
4461 base = _slang_gen_operation(A, &oper->children[0]);
4462 if (!base) {
4463 /* error msg should have already been logged */
4464 return NULL;
4465 }
4466
4467 n = new_node1(IR_FIELD, base);
4468 if (!n)
4469 return NULL;
4470
4471 n->Field = (char *) oper->a_id;
4472
4473 /* Store the field's offset in storage->Index */
4474 n->Store = _slang_new_ir_storage(base->Store->File,
4475 fieldOffset,
4476 fieldSize);
4477
4478 return n;
4479 }
4480 }
4481
4482
4483 /**
4484 * Gen code for array indexing.
4485 */
4486 static slang_ir_node *
4487 _slang_gen_array_element(slang_assemble_ctx * A, slang_operation *oper)
4488 {
4489 slang_typeinfo array_ti;
4490
4491 /* get array's type info */
4492 slang_typeinfo_construct(&array_ti);
4493 typeof_operation(A, &oper->children[0], &array_ti);
4494
4495 if (_slang_type_is_vector(array_ti.spec.type)) {
4496 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
4497 /* translate the index into a swizzle/writemask: "v.x=p" */
4498 const GLuint max = _slang_type_dim(array_ti.spec.type);
4499 GLint index;
4500 slang_ir_node *n;
4501
4502 index = (GLint) oper->children[1].literal[0];
4503 if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
4504 index >= (GLint) max) {
4505 #if 0
4506 slang_info_log_error(A->log, "Invalid array index for vector type");
4507 printf("type = %d\n", oper->children[1].type);
4508 printf("index = %d, max = %d\n", index, max);
4509 printf("array = %s\n", (char*)oper->children[0].a_id);
4510 printf("index = %s\n", (char*)oper->children[1].a_id);
4511 return NULL;
4512 #else
4513 index = 0;
4514 #endif
4515 }
4516
4517 n = _slang_gen_operation(A, &oper->children[0]);
4518 if (n) {
4519 /* use swizzle to access the element */
4520 GLuint swizzle = MAKE_SWIZZLE4(SWIZZLE_X + index,
4521 SWIZZLE_NIL,
4522 SWIZZLE_NIL,
4523 SWIZZLE_NIL);
4524 n = _slang_gen_swizzle(n, swizzle);
4525 }
4526 assert(n->Store);
4527 return n;
4528 }
4529 else {
4530 /* conventional array */
4531 slang_typeinfo elem_ti;
4532 slang_ir_node *elem, *array, *index;
4533 GLint elemSize, arrayLen;
4534
4535 /* size of array element */
4536 slang_typeinfo_construct(&elem_ti);
4537 typeof_operation(A, oper, &elem_ti);
4538 elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
4539
4540 if (_slang_type_is_matrix(array_ti.spec.type))
4541 arrayLen = _slang_type_dim(array_ti.spec.type);
4542 else
4543 arrayLen = array_ti.array_len;
4544
4545 slang_typeinfo_destruct(&array_ti);
4546 slang_typeinfo_destruct(&elem_ti);
4547
4548 if (elemSize <= 0) {
4549 /* unknown var or type */
4550 slang_info_log_error(A->log, "Undefined variable or type");
4551 return NULL;
4552 }
4553
4554 array = _slang_gen_operation(A, &oper->children[0]);
4555 index = _slang_gen_operation(A, &oper->children[1]);
4556 if (array && index) {
4557 /* bounds check */
4558 GLint constIndex = -1;
4559 if (index->Opcode == IR_FLOAT) {
4560 constIndex = (int) index->Value[0];
4561 if (constIndex < 0 || constIndex >= arrayLen) {
4562 slang_info_log_error(A->log,
4563 "Array index out of bounds (index=%d size=%d)",
4564 constIndex, arrayLen);
4565 _slang_free_ir_tree(array);
4566 _slang_free_ir_tree(index);
4567 return NULL;
4568 }
4569 }
4570
4571 if (!array->Store) {
4572 slang_info_log_error(A->log, "Invalid array");
4573 return NULL;
4574 }
4575
4576 elem = new_node2(IR_ELEMENT, array, index);
4577
4578 /* The storage info here will be updated during code emit */
4579 elem->Store = _slang_new_ir_storage(array->Store->File,
4580 array->Store->Index,
4581 elemSize);
4582 elem->Store->Swizzle = _slang_var_swizzle(elemSize, 0);
4583 return elem;
4584 }
4585 else {
4586 _slang_free_ir_tree(array);
4587 _slang_free_ir_tree(index);
4588 return NULL;
4589 }
4590 }
4591 }
4592
4593
4594 static slang_ir_node *
4595 _slang_gen_compare(slang_assemble_ctx *A, slang_operation *oper,
4596 slang_ir_opcode opcode)
4597 {
4598 slang_typeinfo t0, t1;
4599 slang_ir_node *n;
4600
4601 slang_typeinfo_construct(&t0);
4602 typeof_operation(A, &oper->children[0], &t0);
4603
4604 slang_typeinfo_construct(&t1);
4605 typeof_operation(A, &oper->children[0], &t1);
4606
4607 if (t0.spec.type == SLANG_SPEC_ARRAY ||
4608 t1.spec.type == SLANG_SPEC_ARRAY) {
4609 slang_info_log_error(A->log, "Illegal array comparison");
4610 return NULL;
4611 }
4612
4613 if (oper->type != SLANG_OPER_EQUAL &&
4614 oper->type != SLANG_OPER_NOTEQUAL) {
4615 /* <, <=, >, >= can only be used with scalars */
4616 if ((t0.spec.type != SLANG_SPEC_INT &&
4617 t0.spec.type != SLANG_SPEC_FLOAT) ||
4618 (t1.spec.type != SLANG_SPEC_INT &&
4619 t1.spec.type != SLANG_SPEC_FLOAT)) {
4620 slang_info_log_error(A->log, "Incompatible type(s) for inequality operator");
4621 return NULL;
4622 }
4623 }
4624
4625 n = new_node2(opcode,
4626 _slang_gen_operation(A, &oper->children[0]),
4627 _slang_gen_operation(A, &oper->children[1]));
4628
4629 /* result is a bool (size 1) */
4630 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, 1);
4631
4632 return n;
4633 }
4634
4635
4636 #if 0
4637 static void
4638 print_vars(slang_variable_scope *s)
4639 {
4640 int i;
4641 printf("vars: ");
4642 for (i = 0; i < s->num_variables; i++) {
4643 printf("%s %d, \n",
4644 (char*) s->variables[i]->a_name,
4645 s->variables[i]->declared);
4646 }
4647
4648 printf("\n");
4649 }
4650 #endif
4651
4652
4653 #if 0
4654 static void
4655 _slang_undeclare_vars(slang_variable_scope *locals)
4656 {
4657 if (locals->num_variables > 0) {
4658 int i;
4659 for (i = 0; i < locals->num_variables; i++) {
4660 slang_variable *v = locals->variables[i];
4661 printf("undeclare %s at %p\n", (char*) v->a_name, v);
4662 v->declared = GL_FALSE;
4663 }
4664 }
4665 }
4666 #endif
4667
4668
4669 /**
4670 * Generate IR tree for a slang_operation (AST node)
4671 */
4672 static slang_ir_node *
4673 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
4674 {
4675 switch (oper->type) {
4676 case SLANG_OPER_BLOCK_NEW_SCOPE:
4677 {
4678 slang_ir_node *n;
4679
4680 _slang_push_var_table(A->vartable);
4681
4682 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE; /* temp change */
4683 n = _slang_gen_operation(A, oper);
4684 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE; /* restore */
4685
4686 _slang_pop_var_table(A->vartable);
4687
4688 /*_slang_undeclare_vars(oper->locals);*/
4689 /*print_vars(oper->locals);*/
4690
4691 if (n)
4692 n = new_node1(IR_SCOPE, n);
4693 return n;
4694 }
4695 break;
4696
4697 case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
4698 /* list of operations */
4699 if (oper->num_children > 0)
4700 {
4701 slang_ir_node *n, *tree = NULL;
4702 GLuint i;
4703
4704 for (i = 0; i < oper->num_children; i++) {
4705 n = _slang_gen_operation(A, &oper->children[i]);
4706 if (!n) {
4707 _slang_free_ir_tree(tree);
4708 return NULL; /* error must have occured */
4709 }
4710 tree = new_seq(tree, n);
4711 }
4712
4713 return tree;
4714 }
4715 else {
4716 return new_node0(IR_NOP);
4717 }
4718
4719 case SLANG_OPER_EXPRESSION:
4720 return _slang_gen_operation(A, &oper->children[0]);
4721
4722 case SLANG_OPER_FOR:
4723 return _slang_gen_for(A, oper);
4724 case SLANG_OPER_DO:
4725 return _slang_gen_do(A, oper);
4726 case SLANG_OPER_WHILE:
4727 return _slang_gen_while(A, oper);
4728 case SLANG_OPER_BREAK:
4729 if (!current_loop_oper(A)) {
4730 slang_info_log_error(A->log, "'break' not in loop");
4731 return NULL;
4732 }
4733 return new_break(current_loop_ir(A));
4734 case SLANG_OPER_CONTINUE:
4735 if (!current_loop_oper(A)) {
4736 slang_info_log_error(A->log, "'continue' not in loop");
4737 return NULL;
4738 }
4739 return _slang_gen_continue(A, oper);
4740 case SLANG_OPER_DISCARD:
4741 return new_node0(IR_KILL);
4742
4743 case SLANG_OPER_EQUAL:
4744 return _slang_gen_compare(A, oper, IR_EQUAL);
4745 case SLANG_OPER_NOTEQUAL:
4746 return _slang_gen_compare(A, oper, IR_NOTEQUAL);
4747 case SLANG_OPER_GREATER:
4748 return _slang_gen_compare(A, oper, IR_SGT);
4749 case SLANG_OPER_LESS:
4750 return _slang_gen_compare(A, oper, IR_SLT);
4751 case SLANG_OPER_GREATEREQUAL:
4752 return _slang_gen_compare(A, oper, IR_SGE);
4753 case SLANG_OPER_LESSEQUAL:
4754 return _slang_gen_compare(A, oper, IR_SLE);
4755 case SLANG_OPER_ADD:
4756 {
4757 slang_ir_node *n;
4758 assert(oper->num_children == 2);
4759 n = _slang_gen_function_call_name(A, "+", oper, NULL);
4760 return n;
4761 }
4762 case SLANG_OPER_SUBTRACT:
4763 {
4764 slang_ir_node *n;
4765 assert(oper->num_children == 2);
4766 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4767 return n;
4768 }
4769 case SLANG_OPER_MULTIPLY:
4770 {
4771 slang_ir_node *n;
4772 assert(oper->num_children == 2);
4773 n = _slang_gen_function_call_name(A, "*", oper, NULL);
4774 return n;
4775 }
4776 case SLANG_OPER_DIVIDE:
4777 {
4778 slang_ir_node *n;
4779 assert(oper->num_children == 2);
4780 n = _slang_gen_function_call_name(A, "/", oper, NULL);
4781 return n;
4782 }
4783 case SLANG_OPER_MINUS:
4784 {
4785 slang_ir_node *n;
4786 assert(oper->num_children == 1);
4787 n = _slang_gen_function_call_name(A, "-", oper, NULL);
4788 return n;
4789 }
4790 case SLANG_OPER_PLUS:
4791 /* +expr --> do nothing */
4792 return _slang_gen_operation(A, &oper->children[0]);
4793 case SLANG_OPER_VARIABLE_DECL:
4794 return _slang_gen_declaration(A, oper);
4795 case SLANG_OPER_ASSIGN:
4796 return _slang_gen_assignment(A, oper);
4797 case SLANG_OPER_ADDASSIGN:
4798 {
4799 slang_ir_node *n;
4800 assert(oper->num_children == 2);
4801 n = _slang_gen_function_call_name(A, "+=", oper, NULL);
4802 return n;
4803 }
4804 case SLANG_OPER_SUBASSIGN:
4805 {
4806 slang_ir_node *n;
4807 assert(oper->num_children == 2);
4808 n = _slang_gen_function_call_name(A, "-=", oper, NULL);
4809 return n;
4810 }
4811 break;
4812 case SLANG_OPER_MULASSIGN:
4813 {
4814 slang_ir_node *n;
4815 assert(oper->num_children == 2);
4816 n = _slang_gen_function_call_name(A, "*=", oper, NULL);
4817 return n;
4818 }
4819 case SLANG_OPER_DIVASSIGN:
4820 {
4821 slang_ir_node *n;
4822 assert(oper->num_children == 2);
4823 n = _slang_gen_function_call_name(A, "/=", oper, NULL);
4824 return n;
4825 }
4826 case SLANG_OPER_LOGICALAND:
4827 {
4828 slang_ir_node *n;
4829 assert(oper->num_children == 2);
4830 n = _slang_gen_logical_and(A, oper);
4831 return n;
4832 }
4833 case SLANG_OPER_LOGICALOR:
4834 {
4835 slang_ir_node *n;
4836 assert(oper->num_children == 2);
4837 n = _slang_gen_logical_or(A, oper);
4838 return n;
4839 }
4840 case SLANG_OPER_LOGICALXOR:
4841 return _slang_gen_xor(A, oper);
4842 case SLANG_OPER_NOT:
4843 return _slang_gen_not(A, oper);
4844 case SLANG_OPER_SELECT: /* b ? x : y */
4845 {
4846 slang_ir_node *n;
4847 assert(oper->num_children == 3);
4848 n = _slang_gen_select(A, oper);
4849 return n;
4850 }
4851
4852 case SLANG_OPER_ASM:
4853 return _slang_gen_asm(A, oper, NULL);
4854 case SLANG_OPER_CALL:
4855 return _slang_gen_function_call_name(A, (const char *) oper->a_id,
4856 oper, NULL);
4857 case SLANG_OPER_METHOD:
4858 return _slang_gen_method_call(A, oper);
4859 case SLANG_OPER_RETURN:
4860 return _slang_gen_return(A, oper);
4861 case SLANG_OPER_RETURN_INLINED:
4862 return _slang_gen_return(A, oper);
4863 case SLANG_OPER_LABEL:
4864 return new_label(oper->label);
4865 case SLANG_OPER_IDENTIFIER:
4866 return _slang_gen_variable(A, oper);
4867 case SLANG_OPER_IF:
4868 return _slang_gen_if(A, oper);
4869 case SLANG_OPER_FIELD:
4870 return _slang_gen_struct_field(A, oper);
4871 case SLANG_OPER_SUBSCRIPT:
4872 return _slang_gen_array_element(A, oper);
4873 case SLANG_OPER_LITERAL_FLOAT:
4874 /* fall-through */
4875 case SLANG_OPER_LITERAL_INT:
4876 /* fall-through */
4877 case SLANG_OPER_LITERAL_BOOL:
4878 return new_float_literal(oper->literal, oper->literal_size);
4879
4880 case SLANG_OPER_POSTINCREMENT: /* var++ */
4881 {
4882 slang_ir_node *n;
4883 assert(oper->num_children == 1);
4884 n = _slang_gen_function_call_name(A, "__postIncr", oper, NULL);
4885 return n;
4886 }
4887 case SLANG_OPER_POSTDECREMENT: /* var-- */
4888 {
4889 slang_ir_node *n;
4890 assert(oper->num_children == 1);
4891 n = _slang_gen_function_call_name(A, "__postDecr", oper, NULL);
4892 return n;
4893 }
4894 case SLANG_OPER_PREINCREMENT: /* ++var */
4895 {
4896 slang_ir_node *n;
4897 assert(oper->num_children == 1);
4898 n = _slang_gen_function_call_name(A, "++", oper, NULL);
4899 return n;
4900 }
4901 case SLANG_OPER_PREDECREMENT: /* --var */
4902 {
4903 slang_ir_node *n;
4904 assert(oper->num_children == 1);
4905 n = _slang_gen_function_call_name(A, "--", oper, NULL);
4906 return n;
4907 }
4908
4909 case SLANG_OPER_NON_INLINED_CALL:
4910 case SLANG_OPER_SEQUENCE:
4911 {
4912 slang_ir_node *tree = NULL;
4913 GLuint i;
4914 for (i = 0; i < oper->num_children; i++) {
4915 slang_ir_node *n = _slang_gen_operation(A, &oper->children[i]);
4916 tree = new_seq(tree, n);
4917 if (n)
4918 tree->Store = n->Store;
4919 }
4920 if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
4921 tree = new_function_call(tree, oper->label);
4922 }
4923 return tree;
4924 }
4925
4926 case SLANG_OPER_NONE:
4927 case SLANG_OPER_VOID:
4928 /* returning NULL here would generate an error */
4929 return new_node0(IR_NOP);
4930
4931 default:
4932 _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
4933 oper->type);
4934 return new_node0(IR_NOP);
4935 }
4936
4937 return NULL;
4938 }
4939
4940
4941 /**
4942 * Check if the given type specifier is a rectangular texture sampler.
4943 */
4944 static GLboolean
4945 is_rect_sampler_spec(const slang_type_specifier *spec)
4946 {
4947 while (spec->_array) {
4948 spec = spec->_array;
4949 }
4950 return spec->type == SLANG_SPEC_SAMPLER_RECT ||
4951 spec->type == SLANG_SPEC_SAMPLER_RECT_SHADOW;
4952 }
4953
4954
4955
4956 /**
4957 * Called by compiler when a global variable has been parsed/compiled.
4958 * Here we examine the variable's type to determine what kind of register
4959 * storage will be used.
4960 *
4961 * A uniform such as "gl_Position" will become the register specification
4962 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
4963 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
4964 *
4965 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
4966 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
4967 * actual texture unit (as specified by the user calling glUniform1i()).
4968 */
4969 GLboolean
4970 _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
4971 slang_unit_type type)
4972 {
4973 struct gl_program *prog = A->program;
4974 const char *varName = (char *) var->a_name;
4975 GLboolean success = GL_TRUE;
4976 slang_ir_storage *store = NULL;
4977 int dbg = 0;
4978 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
4979 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
4980 const GLint arrayLen = _slang_array_length(var);
4981 const GLint totalSize = _slang_array_size(size, arrayLen);
4982 GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
4983
4984 var->is_global = GL_TRUE;
4985
4986 /* check for sampler2D arrays */
4987 if (texIndex == -1 && var->type.specifier._array)
4988 texIndex = sampler_to_texture_index(var->type.specifier._array->type);
4989
4990 if (texIndex != -1) {
4991 /* This is a texture sampler variable...
4992 * store->File = PROGRAM_SAMPLER
4993 * store->Index = sampler number (0..7, typically)
4994 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4995 */
4996 if (var->initializer) {
4997 slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
4998 return GL_FALSE;
4999 }
5000 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
5001 /* disallow rect samplers */
5002 if (is_rect_sampler_spec(&var->type.specifier)) {
5003 slang_info_log_error(A->log, "invalid sampler type for '%s'", varName);
5004 return GL_FALSE;
5005 }
5006 #else
5007 (void) is_rect_sampler_spec; /* silence warning */
5008 #endif
5009 {
5010 GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
5011 store = _slang_new_ir_storage_sampler(sampNum, texIndex, totalSize);
5012
5013 /* If we have a sampler array, then we need to allocate the
5014 * additional samplers to ensure we don't allocate them elsewhere.
5015 * We can't directly use _mesa_add_sampler() as that checks the
5016 * varName and gets a match, so we call _mesa_add_parameter()
5017 * directly and use the last sampler number from the call above.
5018 */
5019 if (arrayLen > 0) {
5020 GLint a = arrayLen - 1;
5021 GLint i;
5022 for (i = 0; i < a; i++) {
5023 GLfloat value = (GLfloat)(i + sampNum + 1);
5024 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_SAMPLER,
5025 varName, 1, datatype, &value, NULL, 0x0);
5026 }
5027 }
5028 }
5029 if (dbg) printf("SAMPLER ");
5030 }
5031 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
5032 /* Uniform variable */
5033 const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
5034
5035 if (prog) {
5036 /* user-defined uniform */
5037 if (datatype == GL_NONE) {
5038 if ((var->type.specifier.type == SLANG_SPEC_ARRAY &&
5039 var->type.specifier._array->type == SLANG_SPEC_STRUCT) ||
5040 (var->type.specifier.type == SLANG_SPEC_STRUCT)) {
5041 /* temporary work-around */
5042 GLenum datatype = GL_FLOAT;
5043 GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
5044 totalSize, datatype, NULL);
5045 store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
5046 totalSize, swizzle);
5047
5048 if (arrayLen > 0) {
5049 GLint a = arrayLen - 1;
5050 GLint i;
5051 for (i = 0; i < a; i++) {
5052 GLfloat value = (GLfloat)(i + uniformLoc + 1);
5053 (void) _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM,
5054 varName, 1, datatype, &value, NULL, 0x0);
5055 }
5056 }
5057
5058 /* XXX what we need to do is unroll the struct into its
5059 * basic types, creating a uniform variable for each.
5060 * For example:
5061 * struct foo {
5062 * vec3 a;
5063 * vec4 b;
5064 * };
5065 * uniform foo f;
5066 *
5067 * Should produce uniforms:
5068 * "f.a" (GL_FLOAT_VEC3)
5069 * "f.b" (GL_FLOAT_VEC4)
5070 */
5071
5072 if (var->initializer) {
5073 slang_info_log_error(A->log,
5074 "unsupported initializer for uniform '%s'", varName);
5075 return GL_FALSE;
5076 }
5077 }
5078 else {
5079 slang_info_log_error(A->log,
5080 "invalid datatype for uniform variable %s",
5081 varName);
5082 return GL_FALSE;
5083 }
5084 }
5085 else {
5086 /* non-struct uniform */
5087 if (!_slang_gen_var_decl(A, var, var->initializer))
5088 return GL_FALSE;
5089 store = var->store;
5090 }
5091 }
5092 else {
5093 /* pre-defined uniform, like gl_ModelviewMatrix */
5094 /* We know it's a uniform, but don't allocate storage unless
5095 * it's really used.
5096 */
5097 store = _slang_new_ir_storage_swz(PROGRAM_STATE_VAR, -1,
5098 totalSize, swizzle);
5099 }
5100 if (dbg) printf("UNIFORM (sz %d) ", totalSize);
5101 }
5102 else if (var->type.qualifier == SLANG_QUAL_VARYING) {
5103 /* varyings must be float, vec or mat */
5104 if (!_slang_type_is_float_vec_mat(var->type.specifier.type) &&
5105 var->type.specifier.type != SLANG_SPEC_ARRAY) {
5106 slang_info_log_error(A->log,
5107 "varying '%s' must be float/vector/matrix",
5108 varName);
5109 return GL_FALSE;
5110 }
5111
5112 if (var->initializer) {
5113 slang_info_log_error(A->log, "illegal initializer for varying '%s'",
5114 varName);
5115 return GL_FALSE;
5116 }
5117
5118 if (prog) {
5119 /* user-defined varying */
5120 GLbitfield flags;
5121 GLint varyingLoc;
5122 GLuint swizzle;
5123
5124 flags = 0x0;
5125 if (var->type.centroid == SLANG_CENTROID)
5126 flags |= PROG_PARAM_BIT_CENTROID;
5127 if (var->type.variant == SLANG_INVARIANT)
5128 flags |= PROG_PARAM_BIT_INVARIANT;
5129
5130 varyingLoc = _mesa_add_varying(prog->Varying, varName,
5131 totalSize, flags);
5132 swizzle = _slang_var_swizzle(size, 0);
5133 store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
5134 totalSize, swizzle);
5135 }
5136 else {
5137 /* pre-defined varying, like gl_Color or gl_TexCoord */
5138 if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
5139 /* fragment program input */
5140 GLuint swizzle;
5141 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5142 &swizzle);
5143 assert(index >= 0);
5144 assert(index < FRAG_ATTRIB_MAX);
5145 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index,
5146 size, swizzle);
5147 }
5148 else {
5149 /* vertex program output */
5150 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5151 GLuint swizzle = _slang_var_swizzle(size, 0);
5152 assert(index >= 0);
5153 assert(index < VERT_RESULT_MAX);
5154 assert(type == SLANG_UNIT_VERTEX_BUILTIN);
5155 store = _slang_new_ir_storage_swz(PROGRAM_OUTPUT, index,
5156 size, swizzle);
5157 }
5158 if (dbg) printf("V/F ");
5159 }
5160 if (dbg) printf("VARYING ");
5161 }
5162 else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
5163 GLuint swizzle;
5164 GLint index;
5165 /* attributes must be float, vec or mat */
5166 if (!_slang_type_is_float_vec_mat(var->type.specifier.type)) {
5167 slang_info_log_error(A->log,
5168 "attribute '%s' must be float/vector/matrix",
5169 varName);
5170 return GL_FALSE;
5171 }
5172
5173 if (prog) {
5174 /* user-defined vertex attribute */
5175 const GLint attr = -1; /* unknown */
5176 swizzle = _slang_var_swizzle(size, 0);
5177 index = _mesa_add_attribute(prog->Attributes, varName,
5178 size, datatype, attr);
5179 assert(index >= 0);
5180 index = VERT_ATTRIB_GENERIC0 + index;
5181 }
5182 else {
5183 /* pre-defined vertex attrib */
5184 index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
5185 assert(index >= 0);
5186 }
5187 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5188 if (dbg) printf("ATTRIB ");
5189 }
5190 else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
5191 GLuint swizzle = SWIZZLE_XYZW; /* silence compiler warning */
5192 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
5193 &swizzle);
5194 store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
5195 if (dbg) printf("INPUT ");
5196 }
5197 else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
5198 if (type == SLANG_UNIT_VERTEX_BUILTIN) {
5199 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
5200 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
5201 }
5202 else {
5203 GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
5204 GLint specialSize = 4; /* treat all fragment outputs as float[4] */
5205 assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
5206 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, specialSize);
5207 }
5208 if (dbg) printf("OUTPUT ");
5209 }
5210 else if (var->type.qualifier == SLANG_QUAL_CONST && !prog) {
5211 /* pre-defined global constant, like gl_MaxLights */
5212 store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
5213 if (dbg) printf("CONST ");
5214 }
5215 else {
5216 /* ordinary variable (may be const) */
5217 slang_ir_node *n;
5218
5219 /* IR node to declare the variable */
5220 n = _slang_gen_var_decl(A, var, var->initializer);
5221
5222 /* emit GPU instructions */
5223 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_FALSE, A->log);
5224
5225 _slang_free_ir_tree(n);
5226 }
5227
5228 if (dbg) printf("GLOBAL VAR %s idx %d\n", (char*) var->a_name,
5229 store ? store->Index : -2);
5230
5231 if (store)
5232 var->store = store; /* save var's storage info */
5233
5234 var->declared = GL_TRUE;
5235
5236 return success;
5237 }
5238
5239
5240 /**
5241 * Produce an IR tree from a function AST (fun->body).
5242 * Then call the code emitter to convert the IR tree into gl_program
5243 * instructions.
5244 */
5245 GLboolean
5246 _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
5247 {
5248 slang_ir_node *n;
5249 GLboolean success = GL_TRUE;
5250
5251 if (strcmp((char *) fun->header.a_name, "main") != 0) {
5252 /* we only really generate code for main, all other functions get
5253 * inlined or codegen'd upon an actual call.
5254 */
5255 #if 0
5256 /* do some basic error checking though */
5257 if (fun->header.type.specifier.type != SLANG_SPEC_VOID) {
5258 /* check that non-void functions actually return something */
5259 slang_operation *op
5260 = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
5261 if (!op) {
5262 slang_info_log_error(A->log,
5263 "function \"%s\" has no return statement",
5264 (char *) fun->header.a_name);
5265 printf(
5266 "function \"%s\" has no return statement\n",
5267 (char *) fun->header.a_name);
5268 return GL_FALSE;
5269 }
5270 }
5271 #endif
5272 return GL_TRUE; /* not an error */
5273 }
5274
5275 #if 0
5276 printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
5277 slang_print_function(fun, 1);
5278 #endif
5279
5280 /* should have been allocated earlier: */
5281 assert(A->program->Parameters );
5282 assert(A->program->Varying);
5283 assert(A->vartable);
5284
5285 A->LoopDepth = 0;
5286 A->UseReturnFlag = GL_FALSE;
5287 A->CurFunction = fun;
5288
5289 /* fold constant expressions, etc. */
5290 _slang_simplify(fun->body, &A->space, A->atoms);
5291
5292 #if 0
5293 printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
5294 slang_print_function(fun, 1);
5295 #endif
5296
5297 /* Create an end-of-function label */
5298 A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
5299
5300 /* push new vartable scope */
5301 _slang_push_var_table(A->vartable);
5302
5303 /* Generate IR tree for the function body code */
5304 n = _slang_gen_operation(A, fun->body);
5305 if (n)
5306 n = new_node1(IR_SCOPE, n);
5307
5308 /* pop vartable, restore previous */
5309 _slang_pop_var_table(A->vartable);
5310
5311 if (!n) {
5312 /* XXX record error */
5313 return GL_FALSE;
5314 }
5315
5316 /* append an end-of-function-label to IR tree */
5317 n = new_seq(n, new_label(A->curFuncEndLabel));
5318
5319 /*_slang_label_delete(A->curFuncEndLabel);*/
5320 A->curFuncEndLabel = NULL;
5321
5322 #if 0
5323 printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
5324 slang_print_function(fun, 1);
5325 #endif
5326 #if 0
5327 printf("************* IR for %s *******\n", (char*)fun->header.a_name);
5328 _slang_print_ir_tree(n, 0);
5329 #endif
5330 #if 0
5331 printf("************* End codegen function ************\n\n");
5332 #endif
5333
5334 if (A->UnresolvedRefs) {
5335 /* Can't codegen at this time.
5336 * At link time we'll concatenate all the vertex shaders and/or all
5337 * the fragment shaders and try recompiling.
5338 */
5339 return GL_TRUE;
5340 }
5341
5342 /* Emit program instructions */
5343 success = _slang_emit_code(n, A->vartable, A->program, A->pragmas, GL_TRUE, A->log);
5344 _slang_free_ir_tree(n);
5345
5346 /* free codegen context */
5347 /*
5348 free(A->codegen);
5349 */
5350
5351 return success;
5352 }
5353