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