mesa: fix array storage allocation bug
[mesa.git] / src / mesa / shader / slang / slang_codegen.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
28 * \author Brian Paul
29 */
30
31
32 /***
33 *** NOTES:
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
36 ***/
37
38
39
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_statevars.h"
47 #include "slang_typeinfo.h"
48 #include "slang_codegen.h"
49 #include "slang_compile.h"
50 #include "slang_label.h"
51 #include "slang_mem.h"
52 #include "slang_simplify.h"
53 #include "slang_emit.h"
54 #include "slang_vartable.h"
55 #include "slang_ir.h"
56 #include "slang_print.h"
57
58
59 static slang_ir_node *
60 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper);
61
62
63 static GLboolean
64 is_sampler_type(const slang_fully_specified_type *t)
65 {
66 switch (t->specifier.type) {
67 case SLANG_SPEC_SAMPLER1D:
68 case SLANG_SPEC_SAMPLER2D:
69 case SLANG_SPEC_SAMPLER3D:
70 case SLANG_SPEC_SAMPLERCUBE:
71 case SLANG_SPEC_SAMPLER1DSHADOW:
72 case SLANG_SPEC_SAMPLER2DSHADOW:
73 case SLANG_SPEC_SAMPLER2DRECT:
74 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
75 return GL_TRUE;
76 default:
77 return GL_FALSE;
78 }
79 }
80
81
82 /**
83 * Return the offset (in floats or ints) of the named field within
84 * the given struct. Return -1 if field not found.
85 * If field is NULL, return the size of the struct instead.
86 */
87 static GLint
88 _slang_field_offset(const slang_type_specifier *spec, slang_atom field)
89 {
90 GLint offset = 0;
91 GLuint i;
92 for (i = 0; i < spec->_struct->fields->num_variables; i++) {
93 const slang_variable *v = spec->_struct->fields->variables[i];
94 const GLuint sz = _slang_sizeof_type_specifier(&v->type.specifier);
95 if (sz > 1) {
96 /* types larger than 1 float are register (4-float) aligned */
97 offset = (offset + 3) & ~3;
98 }
99 if (field && v->a_name == field) {
100 return offset;
101 }
102 offset += sz;
103 }
104 if (field)
105 return -1; /* field not found */
106 else
107 return offset; /* struct size */
108 }
109
110
111 /**
112 * Return the size (in floats) of the given type specifier.
113 * If the size is greater than 4, the size should be a multiple of 4
114 * so that the correct number of 4-float registers are allocated.
115 * For example, a mat3x2 is size 12 because we want to store the
116 * 3 columns in 3 float[4] registers.
117 */
118 GLuint
119 _slang_sizeof_type_specifier(const slang_type_specifier *spec)
120 {
121 GLuint sz;
122 switch (spec->type) {
123 case SLANG_SPEC_VOID:
124 sz = 0;
125 break;
126 case SLANG_SPEC_BOOL:
127 sz = 1;
128 break;
129 case SLANG_SPEC_BVEC2:
130 sz = 2;
131 break;
132 case SLANG_SPEC_BVEC3:
133 sz = 3;
134 break;
135 case SLANG_SPEC_BVEC4:
136 sz = 4;
137 break;
138 case SLANG_SPEC_INT:
139 sz = 1;
140 break;
141 case SLANG_SPEC_IVEC2:
142 sz = 2;
143 break;
144 case SLANG_SPEC_IVEC3:
145 sz = 3;
146 break;
147 case SLANG_SPEC_IVEC4:
148 sz = 4;
149 break;
150 case SLANG_SPEC_FLOAT:
151 sz = 1;
152 break;
153 case SLANG_SPEC_VEC2:
154 sz = 2;
155 break;
156 case SLANG_SPEC_VEC3:
157 sz = 3;
158 break;
159 case SLANG_SPEC_VEC4:
160 sz = 4;
161 break;
162 case SLANG_SPEC_MAT2:
163 sz = 2 * 4; /* 2 columns (regs) */
164 break;
165 case SLANG_SPEC_MAT3:
166 sz = 3 * 4;
167 break;
168 case SLANG_SPEC_MAT4:
169 sz = 4 * 4;
170 break;
171 case SLANG_SPEC_MAT23:
172 sz = 2 * 4; /* 2 columns (regs) */
173 break;
174 case SLANG_SPEC_MAT32:
175 sz = 3 * 4; /* 3 columns (regs) */
176 break;
177 case SLANG_SPEC_MAT24:
178 sz = 2 * 4;
179 break;
180 case SLANG_SPEC_MAT42:
181 sz = 4 * 4; /* 4 columns (regs) */
182 break;
183 case SLANG_SPEC_MAT34:
184 sz = 3 * 4;
185 break;
186 case SLANG_SPEC_MAT43:
187 sz = 4 * 4; /* 4 columns (regs) */
188 break;
189 case SLANG_SPEC_SAMPLER1D:
190 case SLANG_SPEC_SAMPLER2D:
191 case SLANG_SPEC_SAMPLER3D:
192 case SLANG_SPEC_SAMPLERCUBE:
193 case SLANG_SPEC_SAMPLER1DSHADOW:
194 case SLANG_SPEC_SAMPLER2DSHADOW:
195 case SLANG_SPEC_SAMPLER2DRECT:
196 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
197 sz = 1; /* a sampler is basically just an integer index */
198 break;
199 case SLANG_SPEC_STRUCT:
200 sz = _slang_field_offset(spec, 0); /* special use */
201 if (sz > 4) {
202 sz = (sz + 3) & ~0x3; /* round up to multiple of four */
203 }
204 break;
205 case SLANG_SPEC_ARRAY:
206 sz = _slang_sizeof_type_specifier(spec->_array);
207 break;
208 default:
209 _mesa_problem(NULL, "Unexpected type in _slang_sizeof_type_specifier()");
210 sz = 0;
211 }
212
213 if (sz > 4) {
214 /* if size is > 4, it should be a multiple of four */
215 assert((sz & 0x3) == 0);
216 }
217 return sz;
218 }
219
220
221 /**
222 * Establish the binding between a slang_ir_node and a slang_variable.
223 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
224 * The IR node must be a IR_VAR or IR_VAR_DECL node.
225 * \param n the IR node
226 * \param var the variable to associate with the IR node
227 */
228 static void
229 _slang_attach_storage(slang_ir_node *n, slang_variable *var)
230 {
231 assert(n);
232 assert(var);
233 assert(n->Opcode == IR_VAR || n->Opcode == IR_VAR_DECL);
234 assert(!n->Var || n->Var == var);
235
236 n->Var = var;
237
238 if (!n->Store) {
239 /* need to setup storage */
240 if (n->Var && n->Var->aux) {
241 /* node storage info = var storage info */
242 n->Store = (slang_ir_storage *) n->Var->aux;
243 }
244 else {
245 /* alloc new storage info */
246 n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -1, -5);
247 if (n->Var)
248 n->Var->aux = n->Store;
249 assert(n->Var->aux);
250 }
251 }
252 }
253
254
255 /**
256 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
257 * or -1 if the type is not a sampler.
258 */
259 static GLint
260 sampler_to_texture_index(const slang_type_specifier_type type)
261 {
262 switch (type) {
263 case SLANG_SPEC_SAMPLER1D:
264 return TEXTURE_1D_INDEX;
265 case SLANG_SPEC_SAMPLER2D:
266 return TEXTURE_2D_INDEX;
267 case SLANG_SPEC_SAMPLER3D:
268 return TEXTURE_3D_INDEX;
269 case SLANG_SPEC_SAMPLERCUBE:
270 return TEXTURE_CUBE_INDEX;
271 case SLANG_SPEC_SAMPLER1DSHADOW:
272 return TEXTURE_1D_INDEX; /* XXX fix */
273 case SLANG_SPEC_SAMPLER2DSHADOW:
274 return TEXTURE_2D_INDEX; /* XXX fix */
275 case SLANG_SPEC_SAMPLER2DRECT:
276 return TEXTURE_RECT_INDEX;
277 case SLANG_SPEC_SAMPLER2DRECTSHADOW:
278 return TEXTURE_RECT_INDEX; /* XXX fix */
279 default:
280 return -1;
281 }
282 }
283
284
285 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
286
287 /**
288 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
289 * a vertex or fragment program input variable. Return -1 if the input
290 * name is invalid.
291 * XXX return size too
292 */
293 static GLint
294 _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
295 {
296 struct input_info {
297 const char *Name;
298 GLuint Attrib;
299 GLuint Swizzle;
300 };
301 static const struct input_info vertInputs[] = {
302 { "gl_Vertex", VERT_ATTRIB_POS, SWIZZLE_NOOP },
303 { "gl_Normal", VERT_ATTRIB_NORMAL, SWIZZLE_NOOP },
304 { "gl_Color", VERT_ATTRIB_COLOR0, SWIZZLE_NOOP },
305 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1, SWIZZLE_NOOP },
306 { "gl_FogCoord", VERT_ATTRIB_FOG, SWIZZLE_XXXX },
307 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0, SWIZZLE_NOOP },
308 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1, SWIZZLE_NOOP },
309 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2, SWIZZLE_NOOP },
310 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3, SWIZZLE_NOOP },
311 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4, SWIZZLE_NOOP },
312 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5, SWIZZLE_NOOP },
313 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6, SWIZZLE_NOOP },
314 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7, SWIZZLE_NOOP },
315 { NULL, 0, SWIZZLE_NOOP }
316 };
317 static const struct input_info fragInputs[] = {
318 { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
319 { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
320 { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
321 { "gl_TexCoord", FRAG_ATTRIB_TEX0, SWIZZLE_NOOP },
322 /* note: we're packing several quantities into the fogcoord vector */
323 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
324 { "gl_FrontFacing", FRAG_ATTRIB_FOGC, SWIZZLE_YYYY }, /*XXX*/
325 { "gl_PointCoord", FRAG_ATTRIB_FOGC, SWIZZLE_ZWWW },
326 { NULL, 0, SWIZZLE_NOOP }
327 };
328 GLuint i;
329 const struct input_info *inputs
330 = (target == GL_VERTEX_PROGRAM_ARB) ? vertInputs : fragInputs;
331
332 ASSERT(MAX_TEXTURE_UNITS == 8); /* if this fails, fix vertInputs above */
333
334 for (i = 0; inputs[i].Name; i++) {
335 if (strcmp(inputs[i].Name, name) == 0) {
336 /* found */
337 *swizzleOut = inputs[i].Swizzle;
338 return inputs[i].Attrib;
339 }
340 }
341 return -1;
342 }
343
344
345 /**
346 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
347 * a vertex or fragment program output variable. Return -1 for an invalid
348 * output name.
349 */
350 static GLint
351 _slang_output_index(const char *name, GLenum target)
352 {
353 struct output_info {
354 const char *Name;
355 GLuint Attrib;
356 };
357 static const struct output_info vertOutputs[] = {
358 { "gl_Position", VERT_RESULT_HPOS },
359 { "gl_FrontColor", VERT_RESULT_COL0 },
360 { "gl_BackColor", VERT_RESULT_BFC0 },
361 { "gl_FrontSecondaryColor", VERT_RESULT_COL1 },
362 { "gl_BackSecondaryColor", VERT_RESULT_BFC1 },
363 { "gl_TexCoord", VERT_RESULT_TEX0 },
364 { "gl_FogFragCoord", VERT_RESULT_FOGC },
365 { "gl_PointSize", VERT_RESULT_PSIZ },
366 { NULL, 0 }
367 };
368 static const struct output_info fragOutputs[] = {
369 { "gl_FragColor", FRAG_RESULT_COLR },
370 { "gl_FragDepth", FRAG_RESULT_DEPR },
371 { "gl_FragData", FRAG_RESULT_DATA0 },
372 { NULL, 0 }
373 };
374 GLuint i;
375 const struct output_info *outputs
376 = (target == GL_VERTEX_PROGRAM_ARB) ? vertOutputs : fragOutputs;
377
378 for (i = 0; outputs[i].Name; i++) {
379 if (strcmp(outputs[i].Name, name) == 0) {
380 /* found */
381 return outputs[i].Attrib;
382 }
383 }
384 return -1;
385 }
386
387
388
389 /**********************************************************************/
390
391
392 /**
393 * Map "_asm foo" to IR_FOO, etc.
394 */
395 typedef struct
396 {
397 const char *Name;
398 slang_ir_opcode Opcode;
399 GLuint HaveRetValue, NumParams;
400 } slang_asm_info;
401
402
403 static slang_asm_info AsmInfo[] = {
404 /* vec4 binary op */
405 { "vec4_add", IR_ADD, 1, 2 },
406 { "vec4_subtract", IR_SUB, 1, 2 },
407 { "vec4_multiply", IR_MUL, 1, 2 },
408 { "vec4_dot", IR_DOT4, 1, 2 },
409 { "vec3_dot", IR_DOT3, 1, 2 },
410 { "vec3_cross", IR_CROSS, 1, 2 },
411 { "vec4_lrp", IR_LRP, 1, 3 },
412 { "vec4_min", IR_MIN, 1, 2 },
413 { "vec4_max", IR_MAX, 1, 2 },
414 { "vec4_clamp", IR_CLAMP, 1, 3 },
415 { "vec4_seq", IR_SEQUAL, 1, 2 },
416 { "vec4_sne", IR_SNEQUAL, 1, 2 },
417 { "vec4_sge", IR_SGE, 1, 2 },
418 { "vec4_sgt", IR_SGT, 1, 2 },
419 { "vec4_sle", IR_SLE, 1, 2 },
420 { "vec4_slt", IR_SLT, 1, 2 },
421 /* vec4 unary */
422 { "vec4_floor", IR_FLOOR, 1, 1 },
423 { "vec4_frac", IR_FRAC, 1, 1 },
424 { "vec4_abs", IR_ABS, 1, 1 },
425 { "vec4_negate", IR_NEG, 1, 1 },
426 { "vec4_ddx", IR_DDX, 1, 1 },
427 { "vec4_ddy", IR_DDY, 1, 1 },
428 /* float binary op */
429 { "float_power", IR_POW, 1, 2 },
430 /* texture / sampler */
431 { "vec4_tex1d", IR_TEX, 1, 2 },
432 { "vec4_texb1d", IR_TEXB, 1, 2 }, /* 1d w/ bias */
433 { "vec4_texp1d", IR_TEXP, 1, 2 }, /* 1d w/ projection */
434 { "vec4_tex2d", IR_TEX, 1, 2 },
435 { "vec4_texb2d", IR_TEXB, 1, 2 }, /* 2d w/ bias */
436 { "vec4_texp2d", IR_TEXP, 1, 2 }, /* 2d w/ projection */
437 { "vec4_tex3d", IR_TEX, 1, 2 },
438 { "vec4_texb3d", IR_TEXB, 1, 2 }, /* 3d w/ bias */
439 { "vec4_texp3d", IR_TEXP, 1, 2 }, /* 3d w/ projection */
440 { "vec4_texcube", IR_TEX, 1, 2 }, /* cubemap */
441 { "vec4_tex_rect", IR_TEX, 1, 2 }, /* rectangle */
442 { "vec4_texp_rect", IR_TEX, 1, 2 },/* rectangle w/ projection */
443
444 /* unary op */
445 { "int_to_float", IR_I_TO_F, 1, 1 },
446 { "float_to_int", IR_F_TO_I, 1, 1 },
447 { "float_exp", IR_EXP, 1, 1 },
448 { "float_exp2", IR_EXP2, 1, 1 },
449 { "float_log2", IR_LOG2, 1, 1 },
450 { "float_rsq", IR_RSQ, 1, 1 },
451 { "float_rcp", IR_RCP, 1, 1 },
452 { "float_sine", IR_SIN, 1, 1 },
453 { "float_cosine", IR_COS, 1, 1 },
454 { "float_noise1", IR_NOISE1, 1, 1},
455 { "float_noise2", IR_NOISE2, 1, 1},
456 { "float_noise3", IR_NOISE3, 1, 1},
457 { "float_noise4", IR_NOISE4, 1, 1},
458
459 { NULL, IR_NOP, 0, 0 }
460 };
461
462
463 static slang_ir_node *
464 new_node3(slang_ir_opcode op,
465 slang_ir_node *c0, slang_ir_node *c1, slang_ir_node *c2)
466 {
467 slang_ir_node *n = (slang_ir_node *) _slang_alloc(sizeof(slang_ir_node));
468 if (n) {
469 n->Opcode = op;
470 n->Children[0] = c0;
471 n->Children[1] = c1;
472 n->Children[2] = c2;
473 n->Writemask = WRITEMASK_XYZW;
474 n->InstLocation = -1;
475 }
476 return n;
477 }
478
479 static slang_ir_node *
480 new_node2(slang_ir_opcode op, slang_ir_node *c0, slang_ir_node *c1)
481 {
482 return new_node3(op, c0, c1, NULL);
483 }
484
485 static slang_ir_node *
486 new_node1(slang_ir_opcode op, slang_ir_node *c0)
487 {
488 return new_node3(op, c0, NULL, NULL);
489 }
490
491 static slang_ir_node *
492 new_node0(slang_ir_opcode op)
493 {
494 return new_node3(op, NULL, NULL, NULL);
495 }
496
497
498 /**
499 * Create sequence of two nodes.
500 */
501 static slang_ir_node *
502 new_seq(slang_ir_node *left, slang_ir_node *right)
503 {
504 if (!left)
505 return right;
506 if (!right)
507 return left;
508 return new_node2(IR_SEQ, left, right);
509 }
510
511 static slang_ir_node *
512 new_label(slang_label *label)
513 {
514 slang_ir_node *n = new_node0(IR_LABEL);
515 assert(label);
516 if (n)
517 n->Label = label;
518 return n;
519 }
520
521 static slang_ir_node *
522 new_float_literal(const float v[4], GLuint size)
523 {
524 slang_ir_node *n = new_node0(IR_FLOAT);
525 assert(size <= 4);
526 COPY_4V(n->Value, v);
527 /* allocate a storage object, but compute actual location (Index) later */
528 n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
529 return n;
530 }
531
532
533 static slang_ir_node *
534 new_not(slang_ir_node *n)
535 {
536 return new_node1(IR_NOT, n);
537 }
538
539
540 /**
541 * Non-inlined function call.
542 */
543 static slang_ir_node *
544 new_function_call(slang_ir_node *code, slang_label *name)
545 {
546 slang_ir_node *n = new_node1(IR_CALL, code);
547 assert(name);
548 if (n)
549 n->Label = name;
550 return n;
551 }
552
553
554 /**
555 * Unconditional jump.
556 */
557 static slang_ir_node *
558 new_return(slang_label *dest)
559 {
560 slang_ir_node *n = new_node0(IR_RETURN);
561 assert(dest);
562 if (n)
563 n->Label = dest;
564 return n;
565 }
566
567
568 static slang_ir_node *
569 new_loop(slang_ir_node *body)
570 {
571 return new_node1(IR_LOOP, body);
572 }
573
574
575 static slang_ir_node *
576 new_break(slang_ir_node *loopNode)
577 {
578 slang_ir_node *n = new_node0(IR_BREAK);
579 assert(loopNode);
580 assert(loopNode->Opcode == IR_LOOP);
581 if (n) {
582 /* insert this node at head of linked list */
583 n->List = loopNode->List;
584 loopNode->List = n;
585 }
586 return n;
587 }
588
589
590 /**
591 * Make new IR_BREAK_IF_TRUE.
592 */
593 static slang_ir_node *
594 new_break_if_true(slang_ir_node *loopNode, slang_ir_node *cond)
595 {
596 slang_ir_node *n;
597 assert(loopNode);
598 assert(loopNode->Opcode == IR_LOOP);
599 n = new_node1(IR_BREAK_IF_TRUE, cond);
600 if (n) {
601 /* insert this node at head of linked list */
602 n->List = loopNode->List;
603 loopNode->List = n;
604 }
605 return n;
606 }
607
608
609 /**
610 * Make new IR_CONT_IF_TRUE node.
611 */
612 static slang_ir_node *
613 new_cont_if_true(slang_ir_node *loopNode, slang_ir_node *cond)
614 {
615 slang_ir_node *n;
616 assert(loopNode);
617 assert(loopNode->Opcode == IR_LOOP);
618 n = new_node1(IR_CONT_IF_TRUE, cond);
619 if (n) {
620 /* insert this node at head of linked list */
621 n->List = loopNode->List;
622 loopNode->List = n;
623 }
624 return n;
625 }
626
627
628 static slang_ir_node *
629 new_cond(slang_ir_node *n)
630 {
631 slang_ir_node *c = new_node1(IR_COND, n);
632 return c;
633 }
634
635
636 static slang_ir_node *
637 new_if(slang_ir_node *cond, slang_ir_node *ifPart, slang_ir_node *elsePart)
638 {
639 return new_node3(IR_IF, cond, ifPart, elsePart);
640 }
641
642
643 /**
644 * New IR_VAR node - a reference to a previously declared variable.
645 */
646 static slang_ir_node *
647 new_var(slang_assemble_ctx *A, slang_operation *oper, slang_atom name)
648 {
649 slang_ir_node *n;
650 slang_variable *var = _slang_locate_variable(oper->locals, name, GL_TRUE);
651 if (!var)
652 return NULL;
653
654 assert(!oper->var || oper->var == var);
655
656 n = new_node0(IR_VAR);
657 if (n) {
658 _slang_attach_storage(n, var);
659 }
660 return n;
661 }
662
663
664 /**
665 * Check if the given function is really just a wrapper for a
666 * basic assembly instruction.
667 */
668 static GLboolean
669 slang_is_asm_function(const slang_function *fun)
670 {
671 if (fun->body->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE &&
672 fun->body->num_children == 1 &&
673 fun->body->children[0].type == SLANG_OPER_ASM) {
674 return GL_TRUE;
675 }
676 return GL_FALSE;
677 }
678
679
680 static GLboolean
681 _slang_is_noop(const slang_operation *oper)
682 {
683 if (!oper ||
684 oper->type == SLANG_OPER_VOID ||
685 (oper->num_children == 1 && oper->children[0].type == SLANG_OPER_VOID))
686 return GL_TRUE;
687 else
688 return GL_FALSE;
689 }
690
691
692 /**
693 * Recursively search tree for a node of the given type.
694 */
695 static slang_operation *
696 _slang_find_node_type(slang_operation *oper, slang_operation_type type)
697 {
698 GLuint i;
699 if (oper->type == type)
700 return oper;
701 for (i = 0; i < oper->num_children; i++) {
702 slang_operation *p = _slang_find_node_type(&oper->children[i], type);
703 if (p)
704 return p;
705 }
706 return NULL;
707 }
708
709
710 /**
711 * Count the number of operations of the given time rooted at 'oper'.
712 */
713 static GLuint
714 _slang_count_node_type(slang_operation *oper, slang_operation_type type)
715 {
716 GLuint i, count = 0;
717 if (oper->type == type) {
718 return 1;
719 }
720 for (i = 0; i < oper->num_children; i++) {
721 count += _slang_count_node_type(&oper->children[i], type);
722 }
723 return count;
724 }
725
726
727 /**
728 * Check if the 'return' statement found under 'oper' is a "tail return"
729 * that can be no-op'd. For example:
730 *
731 * void func(void)
732 * {
733 * .. do something ..
734 * return; // this is a no-op
735 * }
736 *
737 * This is used when determining if a function can be inlined. If the
738 * 'return' is not the last statement, we can't inline the function since
739 * we still need the semantic behaviour of the 'return' but we don't want
740 * to accidentally return from the _calling_ function. We'd need to use an
741 * unconditional branch, but we don't have such a GPU instruction (not
742 * always, at least).
743 */
744 static GLboolean
745 _slang_is_tail_return(const slang_operation *oper)
746 {
747 GLuint k = oper->num_children;
748
749 while (k > 0) {
750 const slang_operation *last = &oper->children[k - 1];
751 if (last->type == SLANG_OPER_RETURN)
752 return GL_TRUE;
753 else if (last->type == SLANG_OPER_IDENTIFIER ||
754 last->type == SLANG_OPER_LABEL)
755 k--; /* try prev child */
756 else if (last->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
757 last->type == SLANG_OPER_BLOCK_NEW_SCOPE)
758 /* try sub-children */
759 return _slang_is_tail_return(last);
760 else
761 break;
762 }
763
764 return GL_FALSE;
765 }
766
767
768 static void
769 slang_resolve_variable(slang_operation *oper)
770 {
771 if (oper->type == SLANG_OPER_IDENTIFIER && !oper->var) {
772 oper->var = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
773 }
774 }
775
776
777 /**
778 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
779 */
780 static void
781 slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
782 GLuint substCount, slang_variable **substOld,
783 slang_operation **substNew, GLboolean isLHS)
784 {
785 switch (oper->type) {
786 case SLANG_OPER_VARIABLE_DECL:
787 {
788 slang_variable *v = _slang_locate_variable(oper->locals,
789 oper->a_id, GL_TRUE);
790 assert(v);
791 if (v->initializer && oper->num_children == 0) {
792 /* set child of oper to copy of initializer */
793 oper->num_children = 1;
794 oper->children = slang_operation_new(1);
795 slang_operation_copy(&oper->children[0], v->initializer);
796 }
797 if (oper->num_children == 1) {
798 /* the initializer */
799 slang_substitute(A, &oper->children[0], substCount,
800 substOld, substNew, GL_FALSE);
801 }
802 }
803 break;
804 case SLANG_OPER_IDENTIFIER:
805 assert(oper->num_children == 0);
806 if (1/**!isLHS XXX FIX */) {
807 slang_atom id = oper->a_id;
808 slang_variable *v;
809 GLuint i;
810 v = _slang_locate_variable(oper->locals, id, GL_TRUE);
811 if (!v) {
812 _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
813 return;
814 }
815
816 /* look for a substitution */
817 for (i = 0; i < substCount; i++) {
818 if (v == substOld[i]) {
819 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
820 #if 0 /* DEBUG only */
821 if (substNew[i]->type == SLANG_OPER_IDENTIFIER) {
822 assert(substNew[i]->var);
823 assert(substNew[i]->var->a_name);
824 printf("Substitute %s with %s in id node %p\n",
825 (char*)v->a_name, (char*) substNew[i]->var->a_name,
826 (void*) oper);
827 }
828 else {
829 printf("Substitute %s with %f in id node %p\n",
830 (char*)v->a_name, substNew[i]->literal[0],
831 (void*) oper);
832 }
833 #endif
834 slang_operation_copy(oper, substNew[i]);
835 break;
836 }
837 }
838 }
839 break;
840
841 case SLANG_OPER_RETURN:
842 /* do return replacement here too */
843 assert(oper->num_children == 0 || oper->num_children == 1);
844 if (oper->num_children == 1 && !_slang_is_noop(&oper->children[0])) {
845 /* replace:
846 * return expr;
847 * with:
848 * __retVal = expr;
849 * return;
850 * then do substitutions on the assignment.
851 */
852 slang_operation *blockOper, *assignOper, *returnOper;
853
854 /* check if function actually has a return type */
855 assert(A->CurFunction);
856 if (A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
857 slang_info_log_error(A->log, "illegal return expression");
858 return;
859 }
860
861 blockOper = slang_operation_new(1);
862 blockOper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
863 blockOper->num_children = 2;
864 blockOper->locals->outer_scope = oper->locals->outer_scope;
865 blockOper->children = slang_operation_new(2);
866 assignOper = blockOper->children + 0;
867 returnOper = blockOper->children + 1;
868
869 assignOper->type = SLANG_OPER_ASSIGN;
870 assignOper->num_children = 2;
871 assignOper->locals->outer_scope = blockOper->locals;
872 assignOper->children = slang_operation_new(2);
873 assignOper->children[0].type = SLANG_OPER_IDENTIFIER;
874 assignOper->children[0].a_id = slang_atom_pool_atom(A->atoms, "__retVal");
875 assignOper->children[0].locals->outer_scope = assignOper->locals;
876
877 slang_operation_copy(&assignOper->children[1],
878 &oper->children[0]);
879
880 returnOper->type = SLANG_OPER_RETURN; /* return w/ no value */
881 assert(returnOper->num_children == 0);
882
883 /* do substitutions on the "__retVal = expr" sub-tree */
884 slang_substitute(A, assignOper,
885 substCount, substOld, substNew, GL_FALSE);
886
887 /* install new code */
888 slang_operation_copy(oper, blockOper);
889 slang_operation_destruct(blockOper);
890 }
891 else {
892 /* check if return value was expected */
893 assert(A->CurFunction);
894 if (A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
895 slang_info_log_error(A->log, "return statement requires an expression");
896 return;
897 }
898 }
899 break;
900
901 case SLANG_OPER_ASSIGN:
902 case SLANG_OPER_SUBSCRIPT:
903 /* special case:
904 * child[0] can't have substitutions but child[1] can.
905 */
906 slang_substitute(A, &oper->children[0],
907 substCount, substOld, substNew, GL_TRUE);
908 slang_substitute(A, &oper->children[1],
909 substCount, substOld, substNew, GL_FALSE);
910 break;
911 case SLANG_OPER_FIELD:
912 /* XXX NEW - test */
913 slang_substitute(A, &oper->children[0],
914 substCount, substOld, substNew, GL_TRUE);
915 break;
916 default:
917 {
918 GLuint i;
919 for (i = 0; i < oper->num_children; i++)
920 slang_substitute(A, &oper->children[i],
921 substCount, substOld, substNew, GL_FALSE);
922 }
923 }
924 }
925
926
927
928 /**
929 * Produce inline code for a call to an assembly instruction.
930 * This is typically used to compile a call to a built-in function like this:
931 *
932 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
933 * {
934 * __asm vec4_lrp __retVal, a, y, x;
935 * }
936 *
937 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
938 */
939 static slang_operation *
940 slang_inline_asm_function(slang_assemble_ctx *A,
941 slang_function *fun, slang_operation *oper)
942 {
943 const GLuint numArgs = oper->num_children;
944 GLuint i;
945 slang_operation *inlined;
946 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
947 slang_variable **substOld;
948 slang_operation **substNew;
949
950 ASSERT(slang_is_asm_function(fun));
951 ASSERT(fun->param_count == numArgs + haveRetValue);
952
953 /*
954 printf("Inline %s as %s\n",
955 (char*) fun->header.a_name,
956 (char*) fun->body->children[0].a_id);
957 */
958
959 /*
960 * We'll substitute formal params with actual args in the asm call.
961 */
962 substOld = (slang_variable **)
963 _slang_alloc(numArgs * sizeof(slang_variable *));
964 substNew = (slang_operation **)
965 _slang_alloc(numArgs * sizeof(slang_operation *));
966 for (i = 0; i < numArgs; i++) {
967 substOld[i] = fun->parameters->variables[i];
968 substNew[i] = oper->children + i;
969 }
970
971 /* make a copy of the code to inline */
972 inlined = slang_operation_new(1);
973 slang_operation_copy(inlined, &fun->body->children[0]);
974 if (haveRetValue) {
975 /* get rid of the __retVal child */
976 for (i = 0; i < numArgs; i++) {
977 inlined->children[i] = inlined->children[i + 1];
978 }
979 inlined->num_children--;
980 }
981
982 /* now do formal->actual substitutions */
983 slang_substitute(A, inlined, numArgs, substOld, substNew, GL_FALSE);
984
985 _slang_free(substOld);
986 _slang_free(substNew);
987
988 return inlined;
989 }
990
991
992 /**
993 * Inline the given function call operation.
994 * Return a new slang_operation that corresponds to the inlined code.
995 */
996 static slang_operation *
997 slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
998 slang_operation *oper, slang_operation *returnOper)
999 {
1000 typedef enum {
1001 SUBST = 1,
1002 COPY_IN,
1003 COPY_OUT
1004 } ParamMode;
1005 ParamMode *paramMode;
1006 const GLboolean haveRetValue = _slang_function_has_return_value(fun);
1007 const GLuint numArgs = oper->num_children;
1008 const GLuint totalArgs = numArgs + haveRetValue;
1009 slang_operation *args = oper->children;
1010 slang_operation *inlined, *top;
1011 slang_variable **substOld;
1012 slang_operation **substNew;
1013 GLuint substCount, numCopyIn, i;
1014 slang_function *prevFunction;
1015
1016 /* save / push */
1017 prevFunction = A->CurFunction;
1018 A->CurFunction = fun;
1019
1020 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1021 assert(fun->param_count == totalArgs);
1022
1023 /* allocate temporary arrays */
1024 paramMode = (ParamMode *)
1025 _slang_alloc(totalArgs * sizeof(ParamMode));
1026 substOld = (slang_variable **)
1027 _slang_alloc(totalArgs * sizeof(slang_variable *));
1028 substNew = (slang_operation **)
1029 _slang_alloc(totalArgs * sizeof(slang_operation *));
1030
1031 #if 0
1032 printf("Inline call to %s (total vars=%d nparams=%d)\n",
1033 (char *) fun->header.a_name,
1034 fun->parameters->num_variables, numArgs);
1035 #endif
1036
1037 if (haveRetValue && !returnOper) {
1038 /* Create 3-child comma sequence for inlined code:
1039 * child[0]: declare __resultTmp
1040 * child[1]: inlined function body
1041 * child[2]: __resultTmp
1042 */
1043 slang_operation *commaSeq;
1044 slang_operation *declOper = NULL;
1045 slang_variable *resultVar;
1046
1047 commaSeq = slang_operation_new(1);
1048 commaSeq->type = SLANG_OPER_SEQUENCE;
1049 assert(commaSeq->locals);
1050 commaSeq->locals->outer_scope = oper->locals->outer_scope;
1051 commaSeq->num_children = 3;
1052 commaSeq->children = slang_operation_new(3);
1053 /* allocate the return var */
1054 resultVar = slang_variable_scope_grow(commaSeq->locals);
1055 /*
1056 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1057 (void*)commaSeq->locals, (char *) fun->header.a_name);
1058 */
1059
1060 resultVar->a_name = slang_atom_pool_atom(A->atoms, "__resultTmp");
1061 resultVar->type = fun->header.type; /* XXX copy? */
1062 resultVar->isTemp = GL_TRUE;
1063
1064 /* child[0] = __resultTmp declaration */
1065 declOper = &commaSeq->children[0];
1066 declOper->type = SLANG_OPER_VARIABLE_DECL;
1067 declOper->a_id = resultVar->a_name;
1068 declOper->locals->outer_scope = commaSeq->locals;
1069
1070 /* child[1] = function body */
1071 inlined = &commaSeq->children[1];
1072 inlined->locals->outer_scope = commaSeq->locals;
1073
1074 /* child[2] = __resultTmp reference */
1075 returnOper = &commaSeq->children[2];
1076 returnOper->type = SLANG_OPER_IDENTIFIER;
1077 returnOper->a_id = resultVar->a_name;
1078 returnOper->locals->outer_scope = commaSeq->locals;
1079
1080 top = commaSeq;
1081 }
1082 else {
1083 top = inlined = slang_operation_new(1);
1084 /* XXXX this may be inappropriate!!!! */
1085 inlined->locals->outer_scope = oper->locals->outer_scope;
1086 }
1087
1088
1089 assert(inlined->locals);
1090
1091 /* Examine the parameters, look for inout/out params, look for possible
1092 * substitutions, etc:
1093 * param type behaviour
1094 * in copy actual to local
1095 * const in substitute param with actual
1096 * out copy out
1097 */
1098 substCount = 0;
1099 for (i = 0; i < totalArgs; i++) {
1100 slang_variable *p = fun->parameters->variables[i];
1101 /*
1102 printf("Param %d: %s %s \n", i,
1103 slang_type_qual_string(p->type.qualifier),
1104 (char *) p->a_name);
1105 */
1106 if (p->type.qualifier == SLANG_QUAL_INOUT ||
1107 p->type.qualifier == SLANG_QUAL_OUT) {
1108 /* an output param */
1109 slang_operation *arg;
1110 if (i < numArgs)
1111 arg = &args[i];
1112 else
1113 arg = returnOper;
1114 paramMode[i] = SUBST;
1115
1116 if (arg->type == SLANG_OPER_IDENTIFIER)
1117 slang_resolve_variable(arg);
1118
1119 /* replace parameter 'p' with argument 'arg' */
1120 substOld[substCount] = p;
1121 substNew[substCount] = arg; /* will get copied */
1122 substCount++;
1123 }
1124 else if (p->type.qualifier == SLANG_QUAL_CONST) {
1125 /* a constant input param */
1126 if (args[i].type == SLANG_OPER_IDENTIFIER ||
1127 args[i].type == SLANG_OPER_LITERAL_FLOAT) {
1128 /* replace all occurances of this parameter variable with the
1129 * actual argument variable or a literal.
1130 */
1131 paramMode[i] = SUBST;
1132 slang_resolve_variable(&args[i]);
1133 substOld[substCount] = p;
1134 substNew[substCount] = &args[i]; /* will get copied */
1135 substCount++;
1136 }
1137 else {
1138 paramMode[i] = COPY_IN;
1139 }
1140 }
1141 else {
1142 paramMode[i] = COPY_IN;
1143 }
1144 assert(paramMode[i]);
1145 }
1146
1147 /* actual code inlining: */
1148 slang_operation_copy(inlined, fun->body);
1149
1150 /*** XXX review this */
1151 assert(inlined->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE);
1152 inlined->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1153
1154 #if 0
1155 printf("======================= orig body code ======================\n");
1156 printf("=== params scope = %p\n", (void*) fun->parameters);
1157 slang_print_tree(fun->body, 8);
1158 printf("======================= copied code =========================\n");
1159 slang_print_tree(inlined, 8);
1160 #endif
1161
1162 /* do parameter substitution in inlined code: */
1163 slang_substitute(A, inlined, substCount, substOld, substNew, GL_FALSE);
1164
1165 #if 0
1166 printf("======================= subst code ==========================\n");
1167 slang_print_tree(inlined, 8);
1168 printf("=============================================================\n");
1169 #endif
1170
1171 /* New prolog statements: (inserted before the inlined code)
1172 * Copy the 'in' arguments.
1173 */
1174 numCopyIn = 0;
1175 for (i = 0; i < numArgs; i++) {
1176 if (paramMode[i] == COPY_IN) {
1177 slang_variable *p = fun->parameters->variables[i];
1178 /* declare parameter 'p' */
1179 slang_operation *decl = slang_operation_insert(&inlined->num_children,
1180 &inlined->children,
1181 numCopyIn);
1182 /*
1183 printf("COPY_IN %s from expr\n", (char*)p->a_name);
1184 */
1185 decl->type = SLANG_OPER_VARIABLE_DECL;
1186 assert(decl->locals);
1187 decl->locals->outer_scope = inlined->locals;
1188 decl->a_id = p->a_name;
1189 decl->num_children = 1;
1190 decl->children = slang_operation_new(1);
1191
1192 /* child[0] is the var's initializer */
1193 slang_operation_copy(&decl->children[0], args + i);
1194
1195 numCopyIn++;
1196 }
1197 }
1198
1199 /* New epilog statements:
1200 * 1. Create end of function label to jump to from return statements.
1201 * 2. Copy the 'out' parameter vars
1202 */
1203 {
1204 slang_operation *lab = slang_operation_insert(&inlined->num_children,
1205 &inlined->children,
1206 inlined->num_children);
1207 lab->type = SLANG_OPER_LABEL;
1208 lab->label = A->curFuncEndLabel;
1209 }
1210
1211 for (i = 0; i < totalArgs; i++) {
1212 if (paramMode[i] == COPY_OUT) {
1213 const slang_variable *p = fun->parameters->variables[i];
1214 /* actualCallVar = outParam */
1215 /*if (i > 0 || !haveRetValue)*/
1216 slang_operation *ass = slang_operation_insert(&inlined->num_children,
1217 &inlined->children,
1218 inlined->num_children);
1219 ass->type = SLANG_OPER_ASSIGN;
1220 ass->num_children = 2;
1221 ass->locals->outer_scope = inlined->locals;
1222 ass->children = slang_operation_new(2);
1223 ass->children[0] = args[i]; /*XXX copy */
1224 ass->children[1].type = SLANG_OPER_IDENTIFIER;
1225 ass->children[1].a_id = p->a_name;
1226 ass->children[1].locals->outer_scope = ass->locals;
1227 }
1228 }
1229
1230 _slang_free(paramMode);
1231 _slang_free(substOld);
1232 _slang_free(substNew);
1233
1234 #if 0
1235 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1236 (char *) fun->header.a_name,
1237 fun->parameters->num_variables, numArgs);
1238 slang_print_tree(top, 0);
1239 #endif
1240
1241 /* pop */
1242 A->CurFunction = prevFunction;
1243
1244 return top;
1245 }
1246
1247
1248 static slang_ir_node *
1249 _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
1250 slang_operation *oper, slang_operation *dest)
1251 {
1252 slang_ir_node *n;
1253 slang_operation *inlined;
1254 slang_label *prevFuncEndLabel;
1255 char name[200];
1256
1257 prevFuncEndLabel = A->curFuncEndLabel;
1258 sprintf(name, "__endOfFunc_%s_", (char *) fun->header.a_name);
1259 A->curFuncEndLabel = _slang_label_new(name);
1260 assert(A->curFuncEndLabel);
1261
1262 if (slang_is_asm_function(fun) && !dest) {
1263 /* assemble assembly function - tree style */
1264 inlined = slang_inline_asm_function(A, fun, oper);
1265 }
1266 else {
1267 /* non-assembly function */
1268 /* We always generate an "inline-able" block of code here.
1269 * We may either:
1270 * 1. insert the inline code
1271 * 2. Generate a call to the "inline" code as a subroutine
1272 */
1273
1274
1275 slang_operation *ret = NULL;
1276
1277 inlined = slang_inline_function_call(A, fun, oper, dest);
1278 if (!inlined)
1279 return NULL;
1280
1281 ret = _slang_find_node_type(inlined, SLANG_OPER_RETURN);
1282 if (ret) {
1283 /* check if this is a "tail" return */
1284 if (_slang_count_node_type(inlined, SLANG_OPER_RETURN) == 1 &&
1285 _slang_is_tail_return(inlined)) {
1286 /* The only RETURN is the last stmt in the function, no-op it
1287 * and inline the function body.
1288 */
1289 ret->type = SLANG_OPER_NONE;
1290 }
1291 else {
1292 slang_operation *callOper;
1293 /* The function we're calling has one or more 'return' statements.
1294 * So, we can't truly inline this function because we need to
1295 * implement 'return' with RET (and CAL).
1296 * Nevertheless, we performed "inlining" to make a new instance
1297 * of the function body to deal with static register allocation.
1298 *
1299 * XXX check if there's one 'return' and if it's the very last
1300 * statement in the function - we can optimize that case.
1301 */
1302 assert(inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1303 inlined->type == SLANG_OPER_SEQUENCE);
1304
1305 if (_slang_function_has_return_value(fun) && !dest) {
1306 assert(inlined->children[0].type == SLANG_OPER_VARIABLE_DECL);
1307 assert(inlined->children[2].type == SLANG_OPER_IDENTIFIER);
1308 callOper = &inlined->children[1];
1309 }
1310 else {
1311 callOper = inlined;
1312 }
1313 callOper->type = SLANG_OPER_NON_INLINED_CALL;
1314 callOper->fun = fun;
1315 callOper->label = _slang_label_new_unique((char*) fun->header.a_name);
1316 }
1317 }
1318 }
1319
1320 if (!inlined)
1321 return NULL;
1322
1323 /* Replace the function call with the inlined block (or new CALL stmt) */
1324 slang_operation_destruct(oper);
1325 *oper = *inlined;
1326 _slang_free(inlined);
1327
1328 #if 0
1329 assert(inlined->locals);
1330 printf("*** Inlined code for call to %s:\n",
1331 (char*) fun->header.a_name);
1332 slang_print_tree(oper, 10);
1333 printf("\n");
1334 #endif
1335
1336 n = _slang_gen_operation(A, oper);
1337
1338 /*_slang_label_delete(A->curFuncEndLabel);*/
1339 A->curFuncEndLabel = prevFuncEndLabel;
1340
1341 return n;
1342 }
1343
1344
1345 static slang_asm_info *
1346 slang_find_asm_info(const char *name)
1347 {
1348 GLuint i;
1349 for (i = 0; AsmInfo[i].Name; i++) {
1350 if (_mesa_strcmp(AsmInfo[i].Name, name) == 0) {
1351 return AsmInfo + i;
1352 }
1353 }
1354 return NULL;
1355 }
1356
1357
1358 /**
1359 * Some write-masked assignments are simple, but others are hard.
1360 * Simple example:
1361 * vec3 v;
1362 * v.xy = vec2(a, b);
1363 * Hard example:
1364 * vec3 v;
1365 * v.zy = vec2(a, b);
1366 * this gets transformed/swizzled into:
1367 * v.zy = vec2(a, b).*yx* (* = don't care)
1368 * This function helps to determine simple vs. non-simple.
1369 */
1370 static GLboolean
1371 _slang_simple_writemask(GLuint writemask, GLuint swizzle)
1372 {
1373 switch (writemask) {
1374 case WRITEMASK_X:
1375 return GET_SWZ(swizzle, 0) == SWIZZLE_X;
1376 case WRITEMASK_Y:
1377 return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
1378 case WRITEMASK_Z:
1379 return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
1380 case WRITEMASK_W:
1381 return GET_SWZ(swizzle, 3) == SWIZZLE_W;
1382 case WRITEMASK_XY:
1383 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1384 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
1385 case WRITEMASK_XYZ:
1386 return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
1387 && (GET_SWZ(swizzle, 1) == SWIZZLE_Y)
1388 && (GET_SWZ(swizzle, 2) == SWIZZLE_Z);
1389 case WRITEMASK_XYZW:
1390 return swizzle == SWIZZLE_NOOP;
1391 default:
1392 return GL_FALSE;
1393 }
1394 }
1395
1396
1397 /**
1398 * Convert the given swizzle into a writemask. In some cases this
1399 * is trivial, in other cases, we'll need to also swizzle the right
1400 * hand side to put components in the right places.
1401 * \param swizzle the incoming swizzle
1402 * \param writemaskOut returns the writemask
1403 * \param swizzleOut swizzle to apply to the right-hand-side
1404 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1405 */
1406 static GLboolean
1407 swizzle_to_writemask(GLuint swizzle,
1408 GLuint *writemaskOut, GLuint *swizzleOut)
1409 {
1410 GLuint mask = 0x0, newSwizzle[4];
1411 GLint i, size;
1412
1413 /* make new dst writemask, compute size */
1414 for (i = 0; i < 4; i++) {
1415 const GLuint swz = GET_SWZ(swizzle, i);
1416 if (swz == SWIZZLE_NIL) {
1417 /* end */
1418 break;
1419 }
1420 assert(swz >= 0 && swz <= 3);
1421 mask |= (1 << swz);
1422 }
1423 assert(mask <= 0xf);
1424 size = i; /* number of components in mask/swizzle */
1425
1426 *writemaskOut = mask;
1427
1428 /* make new src swizzle, by inversion */
1429 for (i = 0; i < 4; i++) {
1430 newSwizzle[i] = i; /*identity*/
1431 }
1432 for (i = 0; i < size; i++) {
1433 const GLuint swz = GET_SWZ(swizzle, i);
1434 newSwizzle[swz] = i;
1435 }
1436 *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
1437 newSwizzle[1],
1438 newSwizzle[2],
1439 newSwizzle[3]);
1440
1441 if (_slang_simple_writemask(mask, *swizzleOut)) {
1442 if (size >= 1)
1443 assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
1444 if (size >= 2)
1445 assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
1446 if (size >= 3)
1447 assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
1448 if (size >= 4)
1449 assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
1450 return GL_TRUE;
1451 }
1452 else
1453 return GL_FALSE;
1454 }
1455
1456
1457 /**
1458 * Recursively traverse 'oper' to produce a swizzle mask in the event
1459 * of any vector subscripts and swizzle suffixes.
1460 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1461 */
1462 static GLuint
1463 resolve_swizzle(const slang_operation *oper)
1464 {
1465 if (oper->type == SLANG_OPER_FIELD) {
1466 /* writemask from .xyzw suffix */
1467 slang_swizzle swz;
1468 if (_slang_is_swizzle((char*) oper->a_id, 4, &swz)) {
1469 GLuint swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
1470 swz.swizzle[1],
1471 swz.swizzle[2],
1472 swz.swizzle[3]);
1473 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1474 GLuint s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1475 return s;
1476 }
1477 else
1478 return SWIZZLE_XYZW;
1479 }
1480 else if (oper->type == SLANG_OPER_SUBSCRIPT &&
1481 oper->children[1].type == SLANG_OPER_LITERAL_INT) {
1482 /* writemask from [index] */
1483 GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
1484 GLuint i = (GLuint) oper->children[1].literal[0];
1485 GLuint swizzle;
1486 GLuint s;
1487 switch (i) {
1488 case 0:
1489 swizzle = SWIZZLE_XXXX;
1490 break;
1491 case 1:
1492 swizzle = SWIZZLE_YYYY;
1493 break;
1494 case 2:
1495 swizzle = SWIZZLE_ZZZZ;
1496 break;
1497 case 3:
1498 swizzle = SWIZZLE_WWWW;
1499 break;
1500 default:
1501 swizzle = SWIZZLE_XYZW;
1502 }
1503 s = _slang_swizzle_swizzle(child_swizzle, swizzle);
1504 return s;
1505 }
1506 else {
1507 return SWIZZLE_XYZW;
1508 }
1509 }
1510
1511
1512 /**
1513 * As above, but produce a writemask.
1514 */
1515 static GLuint
1516 resolve_writemask(const slang_operation *oper)
1517 {
1518 GLuint swizzle = resolve_swizzle(oper);
1519 GLuint writemask, swizzleOut;
1520 swizzle_to_writemask(swizzle, &writemask, &swizzleOut);
1521 return writemask;
1522 }
1523
1524
1525 /**
1526 * Recursively descend through swizzle nodes to find the node's storage info.
1527 */
1528 static slang_ir_storage *
1529 get_store(const slang_ir_node *n)
1530 {
1531 if (n->Opcode == IR_SWIZZLE) {
1532 return get_store(n->Children[0]);
1533 }
1534 return n->Store;
1535 }
1536
1537
1538
1539 /**
1540 * Generate IR tree for an asm instruction/operation such as:
1541 * __asm vec4_dot __retVal.x, v1, v2;
1542 */
1543 static slang_ir_node *
1544 _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
1545 slang_operation *dest)
1546 {
1547 const slang_asm_info *info;
1548 slang_ir_node *kids[3], *n;
1549 GLuint j, firstOperand;
1550
1551 assert(oper->type == SLANG_OPER_ASM);
1552
1553 info = slang_find_asm_info((char *) oper->a_id);
1554 if (!info) {
1555 _mesa_problem(NULL, "undefined __asm function %s\n",
1556 (char *) oper->a_id);
1557 assert(info);
1558 }
1559 assert(info->NumParams <= 3);
1560
1561 if (info->NumParams == oper->num_children) {
1562 /* Storage for result is not specified.
1563 * Children[0], [1] are the operands.
1564 */
1565 firstOperand = 0;
1566 }
1567 else {
1568 /* Storage for result (child[0]) is specified.
1569 * Children[1], [2] are the operands.
1570 */
1571 firstOperand = 1;
1572 }
1573
1574 /* assemble child(ren) */
1575 kids[0] = kids[1] = kids[2] = NULL;
1576 for (j = 0; j < info->NumParams; j++) {
1577 kids[j] = _slang_gen_operation(A, &oper->children[firstOperand + j]);
1578 if (!kids[j])
1579 return NULL;
1580 }
1581
1582 n = new_node3(info->Opcode, kids[0], kids[1], kids[2]);
1583
1584 if (firstOperand) {
1585 /* Setup n->Store to be a particular location. Otherwise, storage
1586 * for the result (a temporary) will be allocated later.
1587 */
1588 GLuint writemask = WRITEMASK_XYZW;
1589 slang_operation *dest_oper;
1590 slang_ir_node *n0;
1591
1592 dest_oper = &oper->children[0];
1593
1594 writemask = resolve_writemask(dest_oper);
1595
1596 n0 = _slang_gen_operation(A, dest_oper);
1597 if (!n0)
1598 return NULL;
1599
1600 assert(!n->Store);
1601 n->Store = get_store(n0);
1602 n->Writemask = writemask;
1603
1604 assert(n->Store->File != PROGRAM_UNDEFINED);
1605
1606 _slang_free(n0);
1607 }
1608
1609 return n;
1610 }
1611
1612
1613 static void
1614 print_funcs(struct slang_function_scope_ *scope, const char *name)
1615 {
1616 GLuint i;
1617 for (i = 0; i < scope->num_functions; i++) {
1618 slang_function *f = &scope->functions[i];
1619 if (!name || strcmp(name, (char*) f->header.a_name) == 0)
1620 printf(" %s (%d args)\n", name, f->param_count);
1621
1622 }
1623 if (scope->outer_scope)
1624 print_funcs(scope->outer_scope, name);
1625 }
1626
1627
1628 /**
1629 * Return first function in the scope that has the given name.
1630 * This is the function we'll try to call when there is no exact match
1631 * between function parameters and call arguments.
1632 *
1633 * XXX we should really create a list of candidate functions and try
1634 * all of them...
1635 */
1636 static slang_function *
1637 _slang_first_function(struct slang_function_scope_ *scope, const char *name)
1638 {
1639 GLuint i;
1640 for (i = 0; i < scope->num_functions; i++) {
1641 slang_function *f = &scope->functions[i];
1642 if (strcmp(name, (char*) f->header.a_name) == 0)
1643 return f;
1644 }
1645 if (scope->outer_scope)
1646 return _slang_first_function(scope->outer_scope, name);
1647 return NULL;
1648 }
1649
1650
1651
1652 /**
1653 * Assemble a function call, given a particular function name.
1654 * \param name the function's name (operators like '*' are possible).
1655 */
1656 static slang_ir_node *
1657 _slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
1658 slang_operation *oper, slang_operation *dest)
1659 {
1660 slang_operation *params = oper->children;
1661 const GLuint param_count = oper->num_children;
1662 slang_atom atom;
1663 slang_function *fun;
1664
1665 atom = slang_atom_pool_atom(A->atoms, name);
1666 if (atom == SLANG_ATOM_NULL)
1667 return NULL;
1668
1669 /*
1670 * Use 'name' to find the function to call
1671 */
1672 fun = _slang_locate_function(A->space.funcs, atom, params, param_count,
1673 &A->space, A->atoms, A->log);
1674 if (!fun) {
1675 /* A function with exactly the right parameters/types was not found.
1676 * Try adapting the parameters.
1677 */
1678 fun = _slang_first_function(A->space.funcs, name);
1679 if (!fun || !_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
1680 slang_info_log_error(A->log, "Function '%s' not found (check argument types)", name);
1681 return NULL;
1682 }
1683 assert(fun);
1684 }
1685
1686 return _slang_gen_function_call(A, fun, oper, dest);
1687 }
1688
1689
1690 static GLboolean
1691 _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
1692 {
1693 if (oper->type == SLANG_OPER_LITERAL_FLOAT ||
1694 oper->type == SLANG_OPER_LITERAL_INT ||
1695 oper->type == SLANG_OPER_LITERAL_BOOL) {
1696 if (oper->literal[0])
1697 *value = GL_TRUE;
1698 else
1699 *value = GL_FALSE;
1700 return GL_TRUE;
1701 }
1702 else if (oper->type == SLANG_OPER_EXPRESSION &&
1703 oper->num_children == 1) {
1704 return _slang_is_constant_cond(&oper->children[0], value);
1705 }
1706 return GL_FALSE;
1707 }
1708
1709
1710 /**
1711 * Test if an operation is a scalar or boolean.
1712 */
1713 static GLboolean
1714 _slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
1715 {
1716 slang_typeinfo type;
1717 GLint size;
1718
1719 slang_typeinfo_construct(&type);
1720 _slang_typeof_operation(A, oper, &type);
1721 size = _slang_sizeof_type_specifier(&type.spec);
1722 slang_typeinfo_destruct(&type);
1723 return size == 1;
1724 }
1725
1726
1727 /**
1728 * Generate loop code using high-level IR_LOOP instruction
1729 */
1730 static slang_ir_node *
1731 _slang_gen_while(slang_assemble_ctx * A, const slang_operation *oper)
1732 {
1733 /*
1734 * LOOP:
1735 * BREAK if !expr (child[0])
1736 * body code (child[1])
1737 */
1738 slang_ir_node *prevLoop, *loop, *breakIf, *body;
1739 GLboolean isConst, constTrue;
1740
1741 /* type-check expression */
1742 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
1743 slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
1744 return NULL;
1745 }
1746
1747 /* Check if loop condition is a constant */
1748 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
1749
1750 if (isConst && !constTrue) {
1751 /* loop is never executed! */
1752 return new_node0(IR_NOP);
1753 }
1754
1755 loop = new_loop(NULL);
1756
1757 /* save old, push new loop */
1758 prevLoop = A->CurLoop;
1759 A->CurLoop = loop;
1760
1761 if (isConst && constTrue) {
1762 /* while(nonzero constant), no conditional break */
1763 breakIf = NULL;
1764 }
1765 else {
1766 slang_ir_node *cond
1767 = new_cond(new_not(_slang_gen_operation(A, &oper->children[0])));
1768 breakIf = new_break_if_true(A->CurLoop, cond);
1769 }
1770 body = _slang_gen_operation(A, &oper->children[1]);
1771 loop->Children[0] = new_seq(breakIf, body);
1772
1773 /* Do infinite loop detection */
1774 /* loop->List is head of linked list of break/continue nodes */
1775 if (!loop->List && isConst && constTrue) {
1776 /* infinite loop detected */
1777 A->CurLoop = prevLoop; /* clean-up */
1778 slang_info_log_error(A->log, "Infinite loop detected!");
1779 return NULL;
1780 }
1781
1782 /* pop loop, restore prev */
1783 A->CurLoop = prevLoop;
1784
1785 return loop;
1786 }
1787
1788
1789 /**
1790 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1791 */
1792 static slang_ir_node *
1793 _slang_gen_do(slang_assemble_ctx * A, const slang_operation *oper)
1794 {
1795 /*
1796 * LOOP:
1797 * body code (child[0])
1798 * tail code:
1799 * BREAK if !expr (child[1])
1800 */
1801 slang_ir_node *prevLoop, *loop;
1802 GLboolean isConst, constTrue;
1803
1804 /* type-check expression */
1805 if (!_slang_is_scalar_or_boolean(A, &oper->children[1])) {
1806 slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
1807 return NULL;
1808 }
1809
1810 loop = new_loop(NULL);
1811
1812 /* save old, push new loop */
1813 prevLoop = A->CurLoop;
1814 A->CurLoop = loop;
1815
1816 /* loop body: */
1817 loop->Children[0] = _slang_gen_operation(A, &oper->children[0]);
1818
1819 /* Check if loop condition is a constant */
1820 isConst = _slang_is_constant_cond(&oper->children[1], &constTrue);
1821 if (isConst && constTrue) {
1822 /* do { } while(1) ==> no conditional break */
1823 loop->Children[1] = NULL; /* no tail code */
1824 }
1825 else {
1826 slang_ir_node *cond
1827 = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
1828 loop->Children[1] = new_break_if_true(A->CurLoop, cond);
1829 }
1830
1831 /* XXX we should do infinite loop detection, as above */
1832
1833 /* pop loop, restore prev */
1834 A->CurLoop = prevLoop;
1835
1836 return loop;
1837 }
1838
1839
1840 /**
1841 * Generate for-loop using high-level IR_LOOP instruction.
1842 */
1843 static slang_ir_node *
1844 _slang_gen_for(slang_assemble_ctx * A, const slang_operation *oper)
1845 {
1846 /*
1847 * init code (child[0])
1848 * LOOP:
1849 * BREAK if !expr (child[1])
1850 * body code (child[3])
1851 * tail code:
1852 * incr code (child[2]) // XXX continue here
1853 */
1854 slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body, *init, *incr;
1855
1856 init = _slang_gen_operation(A, &oper->children[0]);
1857 loop = new_loop(NULL);
1858
1859 /* save old, push new loop */
1860 prevLoop = A->CurLoop;
1861 A->CurLoop = loop;
1862
1863 cond = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
1864 breakIf = new_break_if_true(A->CurLoop, cond);
1865 body = _slang_gen_operation(A, &oper->children[3]);
1866 incr = _slang_gen_operation(A, &oper->children[2]);
1867
1868 loop->Children[0] = new_seq(breakIf, body);
1869 loop->Children[1] = incr; /* tail code */
1870
1871 /* pop loop, restore prev */
1872 A->CurLoop = prevLoop;
1873
1874 return new_seq(init, loop);
1875 }
1876
1877
1878 static slang_ir_node *
1879 _slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
1880 {
1881 slang_ir_node *n, *loopNode;
1882 assert(oper->type == SLANG_OPER_CONTINUE);
1883 loopNode = A->CurLoop;
1884 assert(loopNode);
1885 assert(loopNode->Opcode == IR_LOOP);
1886 n = new_node0(IR_CONT);
1887 if (n) {
1888 n->Parent = loopNode;
1889 /* insert this node at head of linked list */
1890 n->List = loopNode->List;
1891 loopNode->List = n;
1892 }
1893 return n;
1894 }
1895
1896
1897 /**
1898 * Determine if the given operation is of a specific type.
1899 */
1900 static GLboolean
1901 is_operation_type(const slang_operation *oper, slang_operation_type type)
1902 {
1903 if (oper->type == type)
1904 return GL_TRUE;
1905 else if ((oper->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
1906 oper->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE) &&
1907 oper->num_children == 1)
1908 return is_operation_type(&oper->children[0], type);
1909 else
1910 return GL_FALSE;
1911 }
1912
1913
1914 /**
1915 * Generate IR tree for an if/then/else conditional using high-level
1916 * IR_IF instruction.
1917 */
1918 static slang_ir_node *
1919 _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
1920 {
1921 /*
1922 * eval expr (child[0])
1923 * IF expr THEN
1924 * if-body code
1925 * ELSE
1926 * else-body code
1927 * ENDIF
1928 */
1929 const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
1930 slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
1931 GLboolean isConst, constTrue;
1932
1933 /* type-check expression */
1934 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
1935 slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
1936 return NULL;
1937 }
1938
1939 isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
1940 if (isConst) {
1941 if (constTrue) {
1942 /* if (true) ... */
1943 return _slang_gen_operation(A, &oper->children[1]);
1944 }
1945 else {
1946 /* if (false) ... */
1947 return _slang_gen_operation(A, &oper->children[2]);
1948 }
1949 }
1950
1951 cond = _slang_gen_operation(A, &oper->children[0]);
1952 cond = new_cond(cond);
1953
1954 if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)) {
1955 /* Special case: generate a conditional break */
1956 ifBody = new_break_if_true(A->CurLoop, cond);
1957 if (haveElseClause) {
1958 elseBody = _slang_gen_operation(A, &oper->children[2]);
1959 return new_seq(ifBody, elseBody);
1960 }
1961 return ifBody;
1962 }
1963 else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)) {
1964 /* Special case: generate a conditional break */
1965 ifBody = new_cont_if_true(A->CurLoop, cond);
1966 if (haveElseClause) {
1967 elseBody = _slang_gen_operation(A, &oper->children[2]);
1968 return new_seq(ifBody, elseBody);
1969 }
1970 return ifBody;
1971 }
1972 else {
1973 /* general case */
1974 ifBody = _slang_gen_operation(A, &oper->children[1]);
1975 if (haveElseClause)
1976 elseBody = _slang_gen_operation(A, &oper->children[2]);
1977 else
1978 elseBody = NULL;
1979 ifNode = new_if(cond, ifBody, elseBody);
1980 return ifNode;
1981 }
1982 }
1983
1984
1985
1986 static slang_ir_node *
1987 _slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
1988 {
1989 slang_ir_node *n;
1990
1991 assert(oper->type == SLANG_OPER_NOT);
1992
1993 /* type-check expression */
1994 if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
1995 slang_info_log_error(A->log,
1996 "scalar/boolean expression expected for '!'");
1997 return NULL;
1998 }
1999
2000 n = _slang_gen_operation(A, &oper->children[0]);
2001 if (n)
2002 return new_not(n);
2003 else
2004 return NULL;
2005 }
2006
2007
2008 static slang_ir_node *
2009 _slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
2010 {
2011 slang_ir_node *n1, *n2;
2012
2013 assert(oper->type == SLANG_OPER_LOGICALXOR);
2014
2015 if (!_slang_is_scalar_or_boolean(A, &oper->children[0]) ||
2016 !_slang_is_scalar_or_boolean(A, &oper->children[0])) {
2017 slang_info_log_error(A->log,
2018 "scalar/boolean expressions expected for '^^'");
2019 return NULL;
2020 }
2021
2022 n1 = _slang_gen_operation(A, &oper->children[0]);
2023 if (!n1)
2024 return NULL;
2025 n2 = _slang_gen_operation(A, &oper->children[1]);
2026 if (!n2)
2027 return NULL;
2028 return new_node2(IR_NOTEQUAL, n1, n2);
2029 }
2030
2031
2032 /**
2033 * Generate IR node for storage of a temporary of given size.
2034 */
2035 static slang_ir_node *
2036 _slang_gen_temporary(GLint size)
2037 {
2038 slang_ir_storage *store;
2039 slang_ir_node *n = NULL;
2040
2041 store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
2042 if (store) {
2043 n = new_node0(IR_VAR_DECL);
2044 if (n) {
2045 n->Store = store;
2046 }
2047 else {
2048 _slang_free(store);
2049 }
2050 }
2051 return n;
2052 }
2053
2054
2055 /**
2056 * Generate IR node for allocating/declaring a variable.
2057 */
2058 static slang_ir_node *
2059 _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var)
2060 {
2061 slang_ir_node *n;
2062 assert(!is_sampler_type(&var->type));
2063 n = new_node0(IR_VAR_DECL);
2064 if (n) {
2065 _slang_attach_storage(n, var);
2066
2067 assert(var->aux);
2068 assert(n->Store == var->aux);
2069 assert(n->Store);
2070 assert(n->Store->Index < 0);
2071
2072 n->Store->File = PROGRAM_TEMPORARY;
2073 n->Store->Size = _slang_sizeof_type_specifier(&n->Var->type.specifier);
2074 if (var->array_len > 0) {
2075 /* this is an array */
2076 /* round up element size to mult of 4 */
2077 GLint sz = (n->Store->Size + 3) & ~3;
2078 /* mult by array size */
2079 sz *= var->array_len;
2080 n->Store->Size = sz;
2081 }
2082 A->program->NumTemporaries++;
2083 assert(n->Store->Size > 0);
2084 }
2085 return n;
2086 }
2087
2088
2089 /**
2090 * Generate code for a selection expression: b ? x : y
2091 * XXX In some cases we could implement a selection expression
2092 * with an LRP instruction (use the boolean as the interpolant).
2093 * Otherwise, we use an IF/ELSE/ENDIF construct.
2094 */
2095 static slang_ir_node *
2096 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
2097 {
2098 slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
2099 slang_ir_node *tmpDecl, *tmpVar, *tree;
2100 slang_typeinfo type;
2101 int size;
2102
2103 assert(oper->type == SLANG_OPER_SELECT);
2104 assert(oper->num_children == 3);
2105
2106 /* size of x or y's type */
2107 slang_typeinfo_construct(&type);
2108 _slang_typeof_operation(A, &oper->children[1], &type);
2109 size = _slang_sizeof_type_specifier(&type.spec);
2110 assert(size > 0);
2111
2112 /* temporary var */
2113 tmpDecl = _slang_gen_temporary(size);
2114
2115 /* the condition (child 0) */
2116 cond = _slang_gen_operation(A, &oper->children[0]);
2117 cond = new_cond(cond);
2118
2119 /* if-true body (child 1) */
2120 tmpVar = new_node0(IR_VAR);
2121 tmpVar->Store = tmpDecl->Store;
2122 trueExpr = _slang_gen_operation(A, &oper->children[1]);
2123 trueNode = new_node2(IR_MOVE, tmpVar, trueExpr);
2124
2125 /* if-false body (child 2) */
2126 tmpVar = new_node0(IR_VAR);
2127 tmpVar->Store = tmpDecl->Store;
2128 falseExpr = _slang_gen_operation(A, &oper->children[2]);
2129 falseNode = new_node2(IR_MOVE, tmpVar, falseExpr);
2130
2131 ifNode = new_if(cond, trueNode, falseNode);
2132
2133 /* tmp var value */
2134 tmpVar = new_node0(IR_VAR);
2135 tmpVar->Store = tmpDecl->Store;
2136
2137 tree = new_seq(ifNode, tmpVar);
2138 tree = new_seq(tmpDecl, tree);
2139
2140 /*_slang_print_ir_tree(tree, 10);*/
2141 return tree;
2142 }
2143
2144
2145 /**
2146 * Generate code for &&.
2147 */
2148 static slang_ir_node *
2149 _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
2150 {
2151 /* rewrite "a && b" as "a ? b : false" */
2152 slang_operation *select;
2153 slang_ir_node *n;
2154
2155 select = slang_operation_new(1);
2156 select->type = SLANG_OPER_SELECT;
2157 select->num_children = 3;
2158 select->children = slang_operation_new(3);
2159
2160 slang_operation_copy(&select->children[0], &oper->children[0]);
2161 slang_operation_copy(&select->children[1], &oper->children[1]);
2162 select->children[2].type = SLANG_OPER_LITERAL_BOOL;
2163 ASSIGN_4V(select->children[2].literal, 0, 0, 0, 0); /* false */
2164 select->children[2].literal_size = 1;
2165
2166 n = _slang_gen_select(A, select);
2167 return n;
2168 }
2169
2170
2171 /**
2172 * Generate code for ||.
2173 */
2174 static slang_ir_node *
2175 _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
2176 {
2177 /* rewrite "a || b" as "a ? true : b" */
2178 slang_operation *select;
2179 slang_ir_node *n;
2180
2181 select = slang_operation_new(1);
2182 select->type = SLANG_OPER_SELECT;
2183 select->num_children = 3;
2184 select->children = slang_operation_new(3);
2185
2186 slang_operation_copy(&select->children[0], &oper->children[0]);
2187 select->children[1].type = SLANG_OPER_LITERAL_BOOL;
2188 ASSIGN_4V(select->children[1].literal, 1, 1, 1, 1); /* true */
2189 select->children[1].literal_size = 1;
2190 slang_operation_copy(&select->children[2], &oper->children[1]);
2191
2192 n = _slang_gen_select(A, select);
2193 return n;
2194 }
2195
2196
2197 /**
2198 * Generate IR tree for a return statement.
2199 */
2200 static slang_ir_node *
2201 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
2202 {
2203 const GLboolean haveReturnValue
2204 = (oper->num_children == 1 && oper->children[0].type != SLANG_OPER_VOID);
2205
2206 /* error checking */
2207 assert(A->CurFunction);
2208 if (haveReturnValue &&
2209 A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
2210 slang_info_log_error(A->log, "illegal return expression");
2211 return NULL;
2212 }
2213 else if (!haveReturnValue &&
2214 A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
2215 slang_info_log_error(A->log, "return statement requires an expression");
2216 return NULL;
2217 }
2218
2219 if (!haveReturnValue) {
2220 return new_return(A->curFuncEndLabel);
2221 }
2222 else {
2223 /*
2224 * Convert from:
2225 * return expr;
2226 * To:
2227 * __retVal = expr;
2228 * return; // goto __endOfFunction
2229 */
2230 slang_operation *assign;
2231 slang_atom a_retVal;
2232 slang_ir_node *n;
2233
2234 a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
2235 assert(a_retVal);
2236
2237 #if 1 /* DEBUG */
2238 {
2239 slang_variable *v
2240 = _slang_locate_variable(oper->locals, a_retVal, GL_TRUE);
2241 if (!v) {
2242 /* trying to return a value in a void-valued function */
2243 return NULL;
2244 }
2245 }
2246 #endif
2247
2248 assign = slang_operation_new(1);
2249 assign->type = SLANG_OPER_ASSIGN;
2250 assign->num_children = 2;
2251 assign->children = slang_operation_new(2);
2252 /* lhs (__retVal) */
2253 assign->children[0].type = SLANG_OPER_IDENTIFIER;
2254 assign->children[0].a_id = a_retVal;
2255 assign->children[0].locals->outer_scope = assign->locals;
2256 /* rhs (expr) */
2257 /* XXX we might be able to avoid this copy someday */
2258 slang_operation_copy(&assign->children[1], &oper->children[0]);
2259
2260 /* assemble the new code */
2261 n = new_seq(_slang_gen_operation(A, assign),
2262 new_return(A->curFuncEndLabel));
2263
2264 slang_operation_delete(assign);
2265 return n;
2266 }
2267 }
2268
2269
2270 /**
2271 * Generate IR tree for a variable declaration.
2272 */
2273 static slang_ir_node *
2274 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
2275 {
2276 slang_ir_node *n;
2277 slang_ir_node *varDecl;
2278 slang_variable *v;
2279 const char *varName = (char *) oper->a_id;
2280
2281 assert(oper->num_children == 0 || oper->num_children == 1);
2282
2283 v = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
2284 assert(v);
2285
2286 varDecl = _slang_gen_var_decl(A, v);
2287
2288 if (oper->num_children > 0) {
2289 /* child is initializer */
2290 slang_ir_node *var, *init, *rhs;
2291 assert(oper->num_children == 1);
2292 var = new_var(A, oper, oper->a_id);
2293 if (!var) {
2294 slang_info_log_error(A->log, "undefined variable '%s'", varName);
2295 return NULL;
2296 }
2297 /* XXX make copy of this initializer? */
2298 rhs = _slang_gen_operation(A, &oper->children[0]);
2299 if (!rhs)
2300 return NULL; /* must have found an error */
2301 init = new_node2(IR_MOVE, var, rhs);
2302 /*assert(rhs->Opcode != IR_SEQ);*/
2303 n = new_seq(varDecl, init);
2304 }
2305 else if (v->initializer) {
2306 slang_ir_node *var, *init, *rhs;
2307 var = new_var(A, oper, oper->a_id);
2308 if (!var) {
2309 slang_info_log_error(A->log, "undefined variable '%s'", varName);
2310 return NULL;
2311 }
2312 #if 0
2313 /* XXX make copy of this initializer? */
2314 {
2315 slang_operation dup;
2316 slang_operation_construct(&dup);
2317 slang_operation_copy(&dup, v->initializer);
2318 _slang_simplify(&dup, &A->space, A->atoms);
2319 rhs = _slang_gen_operation(A, &dup);
2320 }
2321 #else
2322 _slang_simplify(v->initializer, &A->space, A->atoms);
2323 rhs = _slang_gen_operation(A, v->initializer);
2324 #endif
2325 if (!rhs)
2326 return NULL;
2327
2328 assert(rhs);
2329 init = new_node2(IR_MOVE, var, rhs);
2330 /*
2331 assert(rhs->Opcode != IR_SEQ);
2332 */
2333 n = new_seq(varDecl, init);
2334 }
2335 else {
2336 n = varDecl;
2337 }
2338 return n;
2339 }
2340
2341
2342 /**
2343 * Generate IR tree for a variable (such as in an expression).
2344 */
2345 static slang_ir_node *
2346 _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
2347 {
2348 /* If there's a variable associated with this oper (from inlining)
2349 * use it. Otherwise, use the oper's var id.
2350 */
2351 slang_atom aVar = oper->var ? oper->var->a_name : oper->a_id;
2352 slang_ir_node *n = new_var(A, oper, aVar);
2353 if (!n) {
2354 slang_info_log_error(A->log, "undefined variable '%s'", (char *) aVar);
2355 return NULL;
2356 }
2357 return n;
2358 }
2359
2360
2361 static slang_ir_node *
2362 _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
2363 {
2364 slang_ir_node *n = new_node1(IR_SWIZZLE, child);
2365 assert(child);
2366 if (n) {
2367 n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -1, -1);
2368 n->Store->Swizzle = swizzle;
2369 }
2370 return n;
2371 }
2372
2373
2374 /**
2375 * Generate IR tree for an assignment (=).
2376 */
2377 static slang_ir_node *
2378 _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
2379 {
2380 if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
2381 /* Check that var is writeable */
2382 slang_variable *var
2383 = _slang_locate_variable(oper->children[0].locals,
2384 oper->children[0].a_id, GL_TRUE);
2385 if (!var) {
2386 slang_info_log_error(A->log, "undefined variable '%s'",
2387 (char *) oper->children[0].a_id);
2388 return NULL;
2389 }
2390 if (var->type.qualifier == SLANG_QUAL_CONST ||
2391 var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
2392 var->type.qualifier == SLANG_QUAL_UNIFORM ||
2393 (var->type.qualifier == SLANG_QUAL_VARYING &&
2394 A->program->Target == GL_FRAGMENT_PROGRAM_ARB)) {
2395 slang_info_log_error(A->log,
2396 "illegal assignment to read-only variable '%s'",
2397 (char *) oper->children[0].a_id);
2398 return NULL;
2399 }
2400 }
2401
2402 if (oper->children[0].type == SLANG_OPER_IDENTIFIER &&
2403 oper->children[1].type == SLANG_OPER_CALL) {
2404 /* Special case of: x = f(a, b)
2405 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2406 *
2407 * XXX this could be even more effective if we could accomodate
2408 * cases such as "v.x = f();" - would help with typical vertex
2409 * transformation.
2410 */
2411 slang_ir_node *n;
2412 n = _slang_gen_function_call_name(A,
2413 (const char *) oper->children[1].a_id,
2414 &oper->children[1], &oper->children[0]);
2415 return n;
2416 }
2417 else {
2418 slang_ir_node *n, *lhs, *rhs;
2419 lhs = _slang_gen_operation(A, &oper->children[0]);
2420
2421 if (lhs) {
2422 if (!(lhs->Store->File == PROGRAM_OUTPUT ||
2423 lhs->Store->File == PROGRAM_TEMPORARY ||
2424 (lhs->Store->File == PROGRAM_VARYING &&
2425 A->program->Target == GL_VERTEX_PROGRAM_ARB) ||
2426 lhs->Store->File == PROGRAM_UNDEFINED)) {
2427 slang_info_log_error(A->log,
2428 "illegal assignment to read-only l-value");
2429 return NULL;
2430 }
2431 }
2432
2433 rhs = _slang_gen_operation(A, &oper->children[1]);
2434 if (lhs && rhs) {
2435 /* convert lhs swizzle into writemask */
2436 GLuint writemask, newSwizzle;
2437 if (!swizzle_to_writemask(lhs->Store->Swizzle,
2438 &writemask, &newSwizzle)) {
2439 /* Non-simple writemask, need to swizzle right hand side in
2440 * order to put components into the right place.
2441 */
2442 rhs = _slang_gen_swizzle(rhs, newSwizzle);
2443 }
2444 n = new_node2(IR_MOVE, lhs, rhs);
2445 n->Writemask = writemask;
2446 return n;
2447 }
2448 else {
2449 return NULL;
2450 }
2451 }
2452 }
2453
2454
2455 /**
2456 * Generate IR tree for referencing a field in a struct (or basic vector type)
2457 */
2458 static slang_ir_node *
2459 _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
2460 {
2461 slang_typeinfo ti;
2462
2463 /* type of struct */
2464 slang_typeinfo_construct(&ti);
2465 _slang_typeof_operation(A, &oper->children[0], &ti);
2466
2467 if (_slang_type_is_vector(ti.spec.type)) {
2468 /* the field should be a swizzle */
2469 const GLuint rows = _slang_type_dim(ti.spec.type);
2470 slang_swizzle swz;
2471 slang_ir_node *n;
2472 GLuint swizzle;
2473 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
2474 slang_info_log_error(A->log, "Bad swizzle");
2475 }
2476 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
2477 swz.swizzle[1],
2478 swz.swizzle[2],
2479 swz.swizzle[3]);
2480
2481 n = _slang_gen_operation(A, &oper->children[0]);
2482 /* create new parent node with swizzle */
2483 if (n)
2484 n = _slang_gen_swizzle(n, swizzle);
2485 return n;
2486 }
2487 else if ( ti.spec.type == SLANG_SPEC_FLOAT
2488 || ti.spec.type == SLANG_SPEC_INT
2489 || ti.spec.type == SLANG_SPEC_BOOL) {
2490 const GLuint rows = 1;
2491 slang_swizzle swz;
2492 slang_ir_node *n;
2493 GLuint swizzle;
2494 if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
2495 slang_info_log_error(A->log, "Bad swizzle");
2496 }
2497 swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
2498 swz.swizzle[1],
2499 swz.swizzle[2],
2500 swz.swizzle[3]);
2501 n = _slang_gen_operation(A, &oper->children[0]);
2502 /* create new parent node with swizzle */
2503 n = _slang_gen_swizzle(n, swizzle);
2504 return n;
2505 }
2506 else {
2507 /* the field is a structure member (base.field) */
2508 /* oper->children[0] is the base */
2509 /* oper->a_id is the field name */
2510 slang_ir_node *base, *n;
2511 slang_typeinfo field_ti;
2512 GLint fieldSize, fieldOffset = -1;
2513 /* type of field */
2514 slang_typeinfo_construct(&field_ti);
2515 _slang_typeof_operation(A, oper, &field_ti);
2516
2517 fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
2518 if (fieldSize > 0)
2519 fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
2520
2521 if (fieldSize == 0 || fieldOffset < 0) {
2522 slang_info_log_error(A->log,
2523 "\"%s\" is not a member of struct \"%s\"",
2524 (char *) oper->a_id,
2525 (char *) ti.spec._struct->a_name);
2526 return NULL;
2527 }
2528 assert(fieldSize >= 0);
2529
2530 base = _slang_gen_operation(A, &oper->children[0]);
2531 if (!base) {
2532 /* error msg should have already been logged */
2533 return NULL;
2534 }
2535
2536 n = new_node1(IR_FIELD, base);
2537 if (n) {
2538 n->Field = (char *) oper->a_id;
2539 n->FieldOffset = fieldOffset;
2540 assert(n->FieldOffset >= 0);
2541 n->Store = _slang_new_ir_storage(base->Store->File,
2542 base->Store->Index,
2543 fieldSize);
2544 }
2545 return n;
2546
2547 #if 0
2548 _mesa_problem(NULL, "glsl structs/fields not supported yet");
2549 return NULL;
2550 #endif
2551 }
2552 }
2553
2554
2555 /**
2556 * Gen code for array indexing.
2557 */
2558 static slang_ir_node *
2559 _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
2560 {
2561 slang_typeinfo array_ti;
2562
2563 /* get array's type info */
2564 slang_typeinfo_construct(&array_ti);
2565 _slang_typeof_operation(A, &oper->children[0], &array_ti);
2566
2567 if (_slang_type_is_vector(array_ti.spec.type)) {
2568 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2569 /* translate the index into a swizzle/writemask: "v.x=p" */
2570 const GLuint max = _slang_type_dim(array_ti.spec.type);
2571 GLint index;
2572 slang_ir_node *n;
2573
2574 index = (GLint) oper->children[1].literal[0];
2575 if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
2576 index >= max) {
2577 slang_info_log_error(A->log, "Invalid array index for vector type");
2578 return NULL;
2579 }
2580
2581 n = _slang_gen_operation(A, &oper->children[0]);
2582 if (n) {
2583 /* use swizzle to access the element */
2584 GLuint swizzle = MAKE_SWIZZLE4(SWIZZLE_X + index,
2585 SWIZZLE_NIL,
2586 SWIZZLE_NIL,
2587 SWIZZLE_NIL);
2588 n = _slang_gen_swizzle(n, swizzle);
2589 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2590 n->Writemask = WRITEMASK_X << index;
2591 }
2592 return n;
2593 }
2594 else {
2595 /* conventional array */
2596 slang_typeinfo elem_ti;
2597 slang_ir_node *elem, *array, *index;
2598 GLint elemSize, arrayLen;
2599
2600 /* size of array element */
2601 slang_typeinfo_construct(&elem_ti);
2602 _slang_typeof_operation(A, oper, &elem_ti);
2603 elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
2604
2605 if (_slang_type_is_matrix(array_ti.spec.type))
2606 arrayLen = _slang_type_dim(array_ti.spec.type);
2607 else
2608 arrayLen = array_ti.array_len;
2609
2610 slang_typeinfo_destruct(&array_ti);
2611 slang_typeinfo_destruct(&elem_ti);
2612
2613 if (elemSize <= 0) {
2614 /* unknown var or type */
2615 slang_info_log_error(A->log, "Undefined variable or type");
2616 return NULL;
2617 }
2618
2619 array = _slang_gen_operation(A, &oper->children[0]);
2620 index = _slang_gen_operation(A, &oper->children[1]);
2621 if (array && index) {
2622 /* bounds check */
2623 if (index->Opcode == IR_FLOAT &&
2624 ((int) index->Value[0] < 0 ||
2625 (int) index->Value[0] >= arrayLen)) {
2626 slang_info_log_error(A->log,
2627 "Array index out of bounds (index=%d size=%d)",
2628 (int) index->Value[0], arrayLen);
2629 _slang_free_ir_tree(array);
2630 _slang_free_ir_tree(index);
2631 return NULL;
2632 }
2633
2634 elem = new_node2(IR_ELEMENT, array, index);
2635 elem->Store = _slang_new_ir_storage(array->Store->File,
2636 array->Store->Index,
2637 elemSize);
2638 /* XXX try to do some array bounds checking here */
2639 return elem;
2640 }
2641 else {
2642 _slang_free_ir_tree(array);
2643 _slang_free_ir_tree(index);
2644 return NULL;
2645 }
2646 }
2647 }
2648
2649
2650 /**
2651 * Generate IR tree for a slang_operation (AST node)
2652 */
2653 static slang_ir_node *
2654 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
2655 {
2656 switch (oper->type) {
2657 case SLANG_OPER_BLOCK_NEW_SCOPE:
2658 {
2659 slang_ir_node *n;
2660
2661 _slang_push_var_table(A->vartable);
2662
2663 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE; /* temp change */
2664 n = _slang_gen_operation(A, oper);
2665 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE; /* restore */
2666
2667 _slang_pop_var_table(A->vartable);
2668
2669 if (n)
2670 n = new_node1(IR_SCOPE, n);
2671 return n;
2672 }
2673 break;
2674
2675 case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
2676 /* list of operations */
2677 if (oper->num_children > 0)
2678 {
2679 slang_ir_node *n, *tree = NULL;
2680 GLuint i;
2681
2682 for (i = 0; i < oper->num_children; i++) {
2683 n = _slang_gen_operation(A, &oper->children[i]);
2684 if (!n) {
2685 _slang_free_ir_tree(tree);
2686 return NULL; /* error must have occured */
2687 }
2688 tree = new_seq(tree, n);
2689 }
2690
2691 #if 00
2692 if (oper->locals->num_variables > 0) {
2693 int i;
2694 /*
2695 printf("\n****** Deallocate vars in scope!\n");
2696 */
2697 for (i = 0; i < oper->locals->num_variables; i++) {
2698 slang_variable *v = oper->locals->variables + i;
2699 if (v->aux) {
2700 slang_ir_storage *store = (slang_ir_storage *) v->aux;
2701 /*
2702 printf(" Deallocate var %s\n", (char*) v->a_name);
2703 */
2704 assert(store->File == PROGRAM_TEMPORARY);
2705 assert(store->Index >= 0);
2706 _slang_free_temp(A->vartable, store->Index, store->Size);
2707 }
2708 }
2709 }
2710 #endif
2711 return tree;
2712 }
2713 else {
2714 return new_node0(IR_NOP);
2715 }
2716
2717 case SLANG_OPER_EXPRESSION:
2718 return _slang_gen_operation(A, &oper->children[0]);
2719
2720 case SLANG_OPER_FOR:
2721 return _slang_gen_for(A, oper);
2722 case SLANG_OPER_DO:
2723 return _slang_gen_do(A, oper);
2724 case SLANG_OPER_WHILE:
2725 return _slang_gen_while(A, oper);
2726 case SLANG_OPER_BREAK:
2727 if (!A->CurLoop) {
2728 slang_info_log_error(A->log, "'break' not in loop");
2729 return NULL;
2730 }
2731 return new_break(A->CurLoop);
2732 case SLANG_OPER_CONTINUE:
2733 if (!A->CurLoop) {
2734 slang_info_log_error(A->log, "'continue' not in loop");
2735 return NULL;
2736 }
2737 return _slang_gen_continue(A, oper);
2738 case SLANG_OPER_DISCARD:
2739 return new_node0(IR_KILL);
2740
2741 case SLANG_OPER_EQUAL:
2742 return new_node2(IR_EQUAL,
2743 _slang_gen_operation(A, &oper->children[0]),
2744 _slang_gen_operation(A, &oper->children[1]));
2745 case SLANG_OPER_NOTEQUAL:
2746 return new_node2(IR_NOTEQUAL,
2747 _slang_gen_operation(A, &oper->children[0]),
2748 _slang_gen_operation(A, &oper->children[1]));
2749 case SLANG_OPER_GREATER:
2750 return new_node2(IR_SGT,
2751 _slang_gen_operation(A, &oper->children[0]),
2752 _slang_gen_operation(A, &oper->children[1]));
2753 case SLANG_OPER_LESS:
2754 return new_node2(IR_SLT,
2755 _slang_gen_operation(A, &oper->children[0]),
2756 _slang_gen_operation(A, &oper->children[1]));
2757 case SLANG_OPER_GREATEREQUAL:
2758 return new_node2(IR_SGE,
2759 _slang_gen_operation(A, &oper->children[0]),
2760 _slang_gen_operation(A, &oper->children[1]));
2761 case SLANG_OPER_LESSEQUAL:
2762 return new_node2(IR_SLE,
2763 _slang_gen_operation(A, &oper->children[0]),
2764 _slang_gen_operation(A, &oper->children[1]));
2765 case SLANG_OPER_ADD:
2766 {
2767 slang_ir_node *n;
2768 assert(oper->num_children == 2);
2769 n = _slang_gen_function_call_name(A, "+", oper, NULL);
2770 return n;
2771 }
2772 case SLANG_OPER_SUBTRACT:
2773 {
2774 slang_ir_node *n;
2775 assert(oper->num_children == 2);
2776 n = _slang_gen_function_call_name(A, "-", oper, NULL);
2777 return n;
2778 }
2779 case SLANG_OPER_MULTIPLY:
2780 {
2781 slang_ir_node *n;
2782 assert(oper->num_children == 2);
2783 n = _slang_gen_function_call_name(A, "*", oper, NULL);
2784 return n;
2785 }
2786 case SLANG_OPER_DIVIDE:
2787 {
2788 slang_ir_node *n;
2789 assert(oper->num_children == 2);
2790 n = _slang_gen_function_call_name(A, "/", oper, NULL);
2791 return n;
2792 }
2793 case SLANG_OPER_MINUS:
2794 {
2795 slang_ir_node *n;
2796 assert(oper->num_children == 1);
2797 n = _slang_gen_function_call_name(A, "-", oper, NULL);
2798 return n;
2799 }
2800 case SLANG_OPER_PLUS:
2801 /* +expr --> do nothing */
2802 return _slang_gen_operation(A, &oper->children[0]);
2803 case SLANG_OPER_VARIABLE_DECL:
2804 return _slang_gen_declaration(A, oper);
2805 case SLANG_OPER_ASSIGN:
2806 return _slang_gen_assignment(A, oper);
2807 case SLANG_OPER_ADDASSIGN:
2808 {
2809 slang_ir_node *n;
2810 assert(oper->num_children == 2);
2811 n = _slang_gen_function_call_name(A, "+=", oper, &oper->children[0]);
2812 return n;
2813 }
2814 case SLANG_OPER_SUBASSIGN:
2815 {
2816 slang_ir_node *n;
2817 assert(oper->num_children == 2);
2818 n = _slang_gen_function_call_name(A, "-=", oper, &oper->children[0]);
2819 return n;
2820 }
2821 break;
2822 case SLANG_OPER_MULASSIGN:
2823 {
2824 slang_ir_node *n;
2825 assert(oper->num_children == 2);
2826 n = _slang_gen_function_call_name(A, "*=", oper, &oper->children[0]);
2827 return n;
2828 }
2829 case SLANG_OPER_DIVASSIGN:
2830 {
2831 slang_ir_node *n;
2832 assert(oper->num_children == 2);
2833 n = _slang_gen_function_call_name(A, "/=", oper, &oper->children[0]);
2834 return n;
2835 }
2836 case SLANG_OPER_LOGICALAND:
2837 {
2838 slang_ir_node *n;
2839 assert(oper->num_children == 2);
2840 n = _slang_gen_logical_and(A, oper);
2841 return n;
2842 }
2843 case SLANG_OPER_LOGICALOR:
2844 {
2845 slang_ir_node *n;
2846 assert(oper->num_children == 2);
2847 n = _slang_gen_logical_or(A, oper);
2848 return n;
2849 }
2850 case SLANG_OPER_LOGICALXOR:
2851 return _slang_gen_xor(A, oper);
2852 case SLANG_OPER_NOT:
2853 return _slang_gen_not(A, oper);
2854 case SLANG_OPER_SELECT: /* b ? x : y */
2855 {
2856 slang_ir_node *n;
2857 assert(oper->num_children == 3);
2858 n = _slang_gen_select(A, oper);
2859 return n;
2860 }
2861
2862 case SLANG_OPER_ASM:
2863 return _slang_gen_asm(A, oper, NULL);
2864 case SLANG_OPER_CALL:
2865 return _slang_gen_function_call_name(A, (const char *) oper->a_id,
2866 oper, NULL);
2867 case SLANG_OPER_RETURN:
2868 return _slang_gen_return(A, oper);
2869 case SLANG_OPER_LABEL:
2870 return new_label(oper->label);
2871 case SLANG_OPER_IDENTIFIER:
2872 return _slang_gen_variable(A, oper);
2873 case SLANG_OPER_IF:
2874 return _slang_gen_if(A, oper);
2875 case SLANG_OPER_FIELD:
2876 return _slang_gen_field(A, oper);
2877 case SLANG_OPER_SUBSCRIPT:
2878 return _slang_gen_subscript(A, oper);
2879 case SLANG_OPER_LITERAL_FLOAT:
2880 /* fall-through */
2881 case SLANG_OPER_LITERAL_INT:
2882 /* fall-through */
2883 case SLANG_OPER_LITERAL_BOOL:
2884 return new_float_literal(oper->literal, oper->literal_size);
2885
2886 case SLANG_OPER_POSTINCREMENT: /* var++ */
2887 {
2888 slang_ir_node *n;
2889 assert(oper->num_children == 1);
2890 n = _slang_gen_function_call_name(A, "__postIncr", oper, NULL);
2891 return n;
2892 }
2893 case SLANG_OPER_POSTDECREMENT: /* var-- */
2894 {
2895 slang_ir_node *n;
2896 assert(oper->num_children == 1);
2897 n = _slang_gen_function_call_name(A, "__postDecr", oper, NULL);
2898 return n;
2899 }
2900 case SLANG_OPER_PREINCREMENT: /* ++var */
2901 {
2902 slang_ir_node *n;
2903 assert(oper->num_children == 1);
2904 n = _slang_gen_function_call_name(A, "++", oper, NULL);
2905 return n;
2906 }
2907 case SLANG_OPER_PREDECREMENT: /* --var */
2908 {
2909 slang_ir_node *n;
2910 assert(oper->num_children == 1);
2911 n = _slang_gen_function_call_name(A, "--", oper, NULL);
2912 return n;
2913 }
2914
2915 case SLANG_OPER_NON_INLINED_CALL:
2916 case SLANG_OPER_SEQUENCE:
2917 {
2918 slang_ir_node *tree = NULL;
2919 GLuint i;
2920 for (i = 0; i < oper->num_children; i++) {
2921 slang_ir_node *n = _slang_gen_operation(A, &oper->children[i]);
2922 tree = new_seq(tree, n);
2923 }
2924 if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
2925 tree = new_function_call(tree, oper->label);
2926 }
2927 return tree;
2928 }
2929
2930 case SLANG_OPER_NONE:
2931 case SLANG_OPER_VOID:
2932 /* returning NULL here would generate an error */
2933 return new_node0(IR_NOP);
2934
2935 default:
2936 _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
2937 oper->type);
2938 return new_node0(IR_NOP);
2939 }
2940
2941 return NULL;
2942 }
2943
2944
2945
2946 /**
2947 * Called by compiler when a global variable has been parsed/compiled.
2948 * Here we examine the variable's type to determine what kind of register
2949 * storage will be used.
2950 *
2951 * A uniform such as "gl_Position" will become the register specification
2952 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2953 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2954 *
2955 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2956 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2957 * actual texture unit (as specified by the user calling glUniform1i()).
2958 */
2959 GLboolean
2960 _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
2961 slang_unit_type type)
2962 {
2963 struct gl_program *prog = A->program;
2964 const char *varName = (char *) var->a_name;
2965 GLboolean success = GL_TRUE;
2966 slang_ir_storage *store = NULL;
2967 int dbg = 0;
2968 const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
2969 const GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
2970
2971 if (texIndex != -1) {
2972 /* This is a texture sampler variable...
2973 * store->File = PROGRAM_SAMPLER
2974 * store->Index = sampler number (0..7, typically)
2975 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2976 */
2977 GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
2978 store = _slang_new_ir_storage(PROGRAM_SAMPLER, sampNum, texIndex);
2979 if (dbg) printf("SAMPLER ");
2980 }
2981 else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
2982 /* Uniform variable */
2983 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier)
2984 * MAX2(var->array_len, 1);
2985 if (prog) {
2986 /* user-defined uniform */
2987 if (datatype == GL_NONE) {
2988 if (var->type.specifier.type == SLANG_SPEC_STRUCT) {
2989 _mesa_problem(NULL, "user-declared uniform structs not supported yet");
2990 /* XXX what we need to do is unroll the struct into its
2991 * basic types, creating a uniform variable for each.
2992 * For example:
2993 * struct foo {
2994 * vec3 a;
2995 * vec4 b;
2996 * };
2997 * uniform foo f;
2998 *
2999 * Should produce uniforms:
3000 * "f.a" (GL_FLOAT_VEC3)
3001 * "f.b" (GL_FLOAT_VEC4)
3002 */
3003 }
3004 else {
3005 slang_info_log_error(A->log,
3006 "invalid datatype for uniform variable %s",
3007 (char *) var->a_name);
3008 }
3009 return GL_FALSE;
3010 }
3011 else {
3012 GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
3013 size, datatype);
3014 store = _slang_new_ir_storage(PROGRAM_UNIFORM, uniformLoc, size);
3015 }
3016 }
3017 else {
3018 /* pre-defined uniform, like gl_ModelviewMatrix */
3019 /* We know it's a uniform, but don't allocate storage unless
3020 * it's really used.
3021 */
3022 store = _slang_new_ir_storage(PROGRAM_STATE_VAR, -1, size);
3023 }
3024 if (dbg) printf("UNIFORM (sz %d) ", size);
3025 }
3026 else if (var->type.qualifier == SLANG_QUAL_VARYING) {
3027 const GLint size = 4; /* XXX fix */
3028 if (prog) {
3029 /* user-defined varying */
3030 GLint varyingLoc = _mesa_add_varying(prog->Varying, varName, size);
3031 store = _slang_new_ir_storage(PROGRAM_VARYING, varyingLoc, size);
3032 }
3033 else {
3034 /* pre-defined varying, like gl_Color or gl_TexCoord */
3035 if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
3036 GLuint swizzle;
3037 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
3038 &swizzle);
3039 assert(index >= 0);
3040 store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
3041 store->Swizzle = swizzle;
3042 assert(index < FRAG_ATTRIB_MAX);
3043 }
3044 else {
3045 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
3046 assert(index >= 0);
3047 assert(type == SLANG_UNIT_VERTEX_BUILTIN);
3048 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
3049 assert(index < VERT_RESULT_MAX);
3050 }
3051 if (dbg) printf("V/F ");
3052 }
3053 if (dbg) printf("VARYING ");
3054 }
3055 else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
3056 if (prog) {
3057 /* user-defined vertex attribute */
3058 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
3059 const GLint attr = -1; /* unknown */
3060 GLint index = _mesa_add_attribute(prog->Attributes, varName,
3061 size, attr);
3062 assert(index >= 0);
3063 store = _slang_new_ir_storage(PROGRAM_INPUT,
3064 VERT_ATTRIB_GENERIC0 + index, size);
3065 }
3066 else {
3067 /* pre-defined vertex attrib */
3068 GLuint swizzle;
3069 GLint index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB,
3070 &swizzle);
3071 GLint size = 4; /* XXX? */
3072 assert(index >= 0);
3073 store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
3074 store->Swizzle = swizzle;
3075 }
3076 if (dbg) printf("ATTRIB ");
3077 }
3078 else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
3079 GLuint swizzle = SWIZZLE_XYZW; /* silence compiler warning */
3080 GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
3081 &swizzle);
3082 GLint size = 4; /* XXX? */
3083 store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
3084 store->Swizzle = swizzle;
3085 if (dbg) printf("INPUT ");
3086 }
3087 else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
3088 if (type == SLANG_UNIT_VERTEX_BUILTIN) {
3089 GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
3090 GLint size = 4; /* XXX? */
3091 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
3092 }
3093 else {
3094 GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
3095 GLint size = 4; /* XXX? */
3096 assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
3097 store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
3098 }
3099 if (dbg) printf("OUTPUT ");
3100 }
3101 else if (var->type.qualifier == SLANG_QUAL_CONST && !prog) {
3102 /* pre-defined global constant, like gl_MaxLights */
3103 const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
3104 store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
3105 if (dbg) printf("CONST ");
3106 }
3107 else {
3108 /* ordinary variable (may be const) */
3109 slang_ir_node *n;
3110
3111 /* IR node to declare the variable */
3112 n = _slang_gen_var_decl(A, var);
3113
3114 /* IR code for the var's initializer, if present */
3115 if (var->initializer) {
3116 slang_ir_node *lhs, *rhs, *init;
3117
3118 /* Generate IR_MOVE instruction to initialize the variable */
3119 lhs = new_node0(IR_VAR);
3120 lhs->Var = var;
3121 lhs->Store = n->Store;
3122
3123 /* constant folding, etc */
3124 _slang_simplify(var->initializer, &A->space, A->atoms);
3125
3126 rhs = _slang_gen_operation(A, var->initializer);
3127 assert(rhs);
3128 init = new_node2(IR_MOVE, lhs, rhs);
3129 n = new_seq(n, init);
3130 }
3131
3132 success = _slang_emit_code(n, A->vartable, A->program, GL_FALSE, A->log);
3133
3134 _slang_free_ir_tree(n);
3135 }
3136
3137 if (dbg) printf("GLOBAL VAR %s idx %d\n", (char*) var->a_name,
3138 store ? store->Index : -2);
3139
3140 if (store)
3141 var->aux = store; /* save var's storage info */
3142
3143 return success;
3144 }
3145
3146
3147 /**
3148 * Produce an IR tree from a function AST (fun->body).
3149 * Then call the code emitter to convert the IR tree into gl_program
3150 * instructions.
3151 */
3152 GLboolean
3153 _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
3154 {
3155 slang_ir_node *n;
3156 GLboolean success = GL_TRUE;
3157
3158 if (_mesa_strcmp((char *) fun->header.a_name, "main") != 0) {
3159 /* we only really generate code for main, all other functions get
3160 * inlined or codegen'd upon an actual call.
3161 */
3162 #if 0
3163 /* do some basic error checking though */
3164 if (fun->header.type.specifier.type != SLANG_SPEC_VOID) {
3165 /* check that non-void functions actually return something */
3166 slang_operation *op
3167 = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
3168 if (!op) {
3169 slang_info_log_error(A->log,
3170 "function \"%s\" has no return statement",
3171 (char *) fun->header.a_name);
3172 printf(
3173 "function \"%s\" has no return statement\n",
3174 (char *) fun->header.a_name);
3175 return GL_FALSE;
3176 }
3177 }
3178 #endif
3179 return GL_TRUE; /* not an error */
3180 }
3181
3182 #if 0
3183 printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
3184 slang_print_function(fun, 1);
3185 #endif
3186
3187 /* should have been allocated earlier: */
3188 assert(A->program->Parameters );
3189 assert(A->program->Varying);
3190 assert(A->vartable);
3191 A->CurLoop = NULL;
3192 A->CurFunction = fun;
3193
3194 /* fold constant expressions, etc. */
3195 _slang_simplify(fun->body, &A->space, A->atoms);
3196
3197 #if 0
3198 printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
3199 slang_print_function(fun, 1);
3200 #endif
3201
3202 /* Create an end-of-function label */
3203 A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
3204
3205 /* push new vartable scope */
3206 _slang_push_var_table(A->vartable);
3207
3208 /* Generate IR tree for the function body code */
3209 n = _slang_gen_operation(A, fun->body);
3210 if (n)
3211 n = new_node1(IR_SCOPE, n);
3212
3213 /* pop vartable, restore previous */
3214 _slang_pop_var_table(A->vartable);
3215
3216 if (!n) {
3217 /* XXX record error */
3218 return GL_FALSE;
3219 }
3220
3221 /* append an end-of-function-label to IR tree */
3222 n = new_seq(n, new_label(A->curFuncEndLabel));
3223
3224 /*_slang_label_delete(A->curFuncEndLabel);*/
3225 A->curFuncEndLabel = NULL;
3226
3227 #if 0
3228 printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
3229 slang_print_function(fun, 1);
3230 #endif
3231 #if 0
3232 printf("************* IR for %s *******\n", (char*)fun->header.a_name);
3233 _slang_print_ir_tree(n, 0);
3234 #endif
3235 #if 0
3236 printf("************* End codegen function ************\n\n");
3237 #endif
3238
3239 /* Emit program instructions */
3240 success = _slang_emit_code(n, A->vartable, A->program, GL_TRUE, A->log);
3241 _slang_free_ir_tree(n);
3242
3243 /* free codegen context */
3244 /*
3245 _mesa_free(A->codegen);
3246 */
3247
3248 return success;
3249 }
3250