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