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