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