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