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