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