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