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