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