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