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