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