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