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