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