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