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