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