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