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