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