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