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