checkpoint overhaul of pre-defined uniform code
[mesa.git] / src / mesa / shader / slang / slang_emit.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_emit.c
27 * Emit program instructions (PI code) from IR trees.
28 * \author Brian Paul
29 */
30
31 #include "imports.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "program.h"
35 #include "prog_instruction.h"
36 #include "prog_parameter.h"
37 #include "prog_print.h"
38 #include "slang_builtin.h"
39 #include "slang_emit.h"
40 #include "slang_error.h"
41
42
43 #define PEEPHOLE_OPTIMIZATIONS 1
44 #define ANNOTATE 0
45
46
47 /* XXX temporarily here */
48 static GLboolean EmitHighLevelInstructions = GL_TRUE;
49
50
51
52 /**
53 * Assembly and IR info
54 */
55 typedef struct
56 {
57 slang_ir_opcode IrOpcode;
58 const char *IrName;
59 gl_inst_opcode InstOpcode;
60 GLuint ResultSize, NumParams;
61 } slang_ir_info;
62
63
64
65 static const slang_ir_info IrInfo[] = {
66 /* binary ops */
67 { IR_ADD, "IR_ADD", OPCODE_ADD, 4, 2 },
68 { IR_SUB, "IR_SUB", OPCODE_SUB, 4, 2 },
69 { IR_MUL, "IR_MUL", OPCODE_MUL, 4, 2 },
70 { IR_DIV, "IR_DIV", OPCODE_NOP, 0, 2 }, /* XXX broke */
71 { IR_DOT4, "IR_DOT_4", OPCODE_DP4, 1, 2 },
72 { IR_DOT3, "IR_DOT_3", OPCODE_DP3, 1, 2 },
73 { IR_CROSS, "IR_CROSS", OPCODE_XPD, 3, 2 },
74 { IR_LRP, "IR_LRP", OPCODE_LRP, 4, 3 },
75 { IR_MIN, "IR_MIN", OPCODE_MIN, 4, 2 },
76 { IR_MAX, "IR_MAX", OPCODE_MAX, 4, 2 },
77 { IR_CLAMP, "IR_CLAMP", OPCODE_NOP, 4, 3 }, /* special case: emit_clamp() */
78 { IR_SEQUAL, "IR_SEQUAL", OPCODE_SEQ, 4, 2 },
79 { IR_SNEQUAL, "IR_SNEQUAL", OPCODE_SNE, 4, 2 },
80 { IR_SGE, "IR_SGE", OPCODE_SGE, 4, 2 },
81 { IR_SGT, "IR_SGT", OPCODE_SGT, 4, 2 },
82 { IR_POW, "IR_POW", OPCODE_POW, 1, 2 },
83 /* unary ops */
84 { IR_I_TO_F, "IR_I_TO_F", OPCODE_NOP, 1, 1 },
85 { IR_F_TO_I, "IR_F_TO_I", OPCODE_INT, 4, 1 }, /* 4 floats to 4 ints */
86 { IR_EXP, "IR_EXP", OPCODE_EXP, 1, 1 },
87 { IR_EXP2, "IR_EXP2", OPCODE_EX2, 1, 1 },
88 { IR_LOG2, "IR_LOG2", OPCODE_LG2, 1, 1 },
89 { IR_RSQ, "IR_RSQ", OPCODE_RSQ, 1, 1 },
90 { IR_RCP, "IR_RCP", OPCODE_RCP, 1, 1 },
91 { IR_FLOOR, "IR_FLOOR", OPCODE_FLR, 4, 1 },
92 { IR_FRAC, "IR_FRAC", OPCODE_FRC, 4, 1 },
93 { IR_ABS, "IR_ABS", OPCODE_ABS, 4, 1 },
94 { IR_NEG, "IR_NEG", OPCODE_NOP, 4, 1 }, /* special case: emit_negation() */
95 { IR_DDX, "IR_DDX", OPCODE_DDX, 4, 1 },
96 { IR_DDX, "IR_DDY", OPCODE_DDX, 4, 1 },
97 { IR_SIN, "IR_SIN", OPCODE_SIN, 1, 1 },
98 { IR_COS, "IR_COS", OPCODE_COS, 1, 1 },
99 { IR_NOISE1, "IR_NOISE1", OPCODE_NOISE1, 1, 1 },
100 { IR_NOISE2, "IR_NOISE2", OPCODE_NOISE2, 1, 1 },
101 { IR_NOISE3, "IR_NOISE3", OPCODE_NOISE3, 1, 1 },
102 { IR_NOISE4, "IR_NOISE4", OPCODE_NOISE4, 1, 1 },
103
104 /* other */
105 { IR_SEQ, "IR_SEQ", OPCODE_NOP, 0, 0 },
106 { IR_SCOPE, "IR_SCOPE", OPCODE_NOP, 0, 0 },
107 { IR_LABEL, "IR_LABEL", OPCODE_NOP, 0, 0 },
108 { IR_JUMP, "IR_JUMP", OPCODE_NOP, 0, 0 },
109 { IR_CJUMP0, "IR_CJUMP0", OPCODE_NOP, 0, 0 },
110 { IR_CJUMP1, "IR_CJUMP1", OPCODE_NOP, 0, 0 },
111 { IR_IF, "IR_IF", OPCODE_NOP, 0, 0 },
112 { IR_KILL, "IR_KILL", OPCODE_NOP, 0, 0 },
113 { IR_COND, "IR_COND", OPCODE_NOP, 0, 0 },
114 { IR_CALL, "IR_CALL", OPCODE_NOP, 0, 0 },
115 { IR_MOVE, "IR_MOVE", OPCODE_NOP, 0, 1 },
116 { IR_NOT, "IR_NOT", OPCODE_NOP, 1, 1 },
117 { IR_VAR, "IR_VAR", OPCODE_NOP, 0, 0 },
118 { IR_VAR_DECL, "IR_VAR_DECL", OPCODE_NOP, 0, 0 },
119 { IR_TEX, "IR_TEX", OPCODE_TEX, 4, 1 },
120 { IR_TEXB, "IR_TEXB", OPCODE_TXB, 4, 1 },
121 { IR_TEXP, "IR_TEXP", OPCODE_TXP, 4, 1 },
122 { IR_FLOAT, "IR_FLOAT", OPCODE_NOP, 0, 0 }, /* float literal */
123 { IR_FIELD, "IR_FIELD", OPCODE_NOP, 0, 0 },
124 { IR_ELEMENT, "IR_ELEMENT", OPCODE_NOP, 0, 0 },
125 { IR_SWIZZLE, "IR_SWIZZLE", OPCODE_NOP, 0, 0 },
126 { IR_NOP, NULL, OPCODE_NOP, 0, 0 }
127 };
128
129
130 static const slang_ir_info *
131 slang_find_ir_info(slang_ir_opcode opcode)
132 {
133 GLuint i;
134 for (i = 0; IrInfo[i].IrName; i++) {
135 if (IrInfo[i].IrOpcode == opcode) {
136 return IrInfo + i;
137 }
138 }
139 return NULL;
140 }
141
142 static const char *
143 slang_ir_name(slang_ir_opcode opcode)
144 {
145 return slang_find_ir_info(opcode)->IrName;
146 }
147
148
149 /**
150 * Swizzle a swizzle. That is, return swz2(swz1)
151 */
152 static GLuint
153 swizzle_swizzle(GLuint swz1, GLuint swz2)
154 {
155 GLuint i, swz, s[4];
156 for (i = 0; i < 4; i++) {
157 GLuint c = GET_SWZ(swz2, i);
158 s[i] = GET_SWZ(swz1, c);
159 }
160 swz = MAKE_SWIZZLE4(s[0], s[1], s[2], s[3]);
161 return swz;
162 }
163
164
165 slang_ir_storage *
166 _slang_new_ir_storage(enum register_file file, GLint index, GLint size)
167 {
168 slang_ir_storage *st;
169 st = (slang_ir_storage *) _mesa_calloc(sizeof(slang_ir_storage));
170 if (st) {
171 st->File = file;
172 st->Index = index;
173 st->Size = size;
174 st->Swizzle = SWIZZLE_NOOP;
175 }
176 return st;
177 }
178
179
180 static const char *
181 swizzle_string(GLuint swizzle)
182 {
183 static char s[6];
184 GLuint i;
185 s[0] = '.';
186 for (i = 1; i < 5; i++) {
187 s[i] = "xyzw"[GET_SWZ(swizzle, i-1)];
188 }
189 s[i] = 0;
190 return s;
191 }
192
193 static const char *
194 writemask_string(GLuint writemask)
195 {
196 static char s[6];
197 GLuint i, j = 0;
198 s[j++] = '.';
199 for (i = 0; i < 4; i++) {
200 if (writemask & (1 << i))
201 s[j++] = "xyzw"[i];
202 }
203 s[j] = 0;
204 return s;
205 }
206
207 static const char *
208 storage_string(const slang_ir_storage *st)
209 {
210 static const char *files[] = {
211 "TEMP",
212 "LOCAL_PARAM",
213 "ENV_PARAM",
214 "STATE",
215 "INPUT",
216 "OUTPUT",
217 "NAMED_PARAM",
218 "CONSTANT",
219 "UNIFORM",
220 "WRITE_ONLY",
221 "ADDRESS",
222 "SAMPLER",
223 "UNDEFINED"
224 };
225 static char s[100];
226 #if 0
227 if (st->Size == 1)
228 sprintf(s, "%s[%d]", files[st->File], st->Index);
229 else
230 sprintf(s, "%s[%d..%d]", files[st->File], st->Index,
231 st->Index + st->Size - 1);
232 #endif
233 assert(st->File < (GLint) (sizeof(files) / sizeof(files[0])));
234 sprintf(s, "%s[%d]", files[st->File], st->Index);
235 return s;
236 }
237
238
239 static void
240 spaces(int n)
241 {
242 while (n-- > 0) {
243 printf(" ");
244 }
245 }
246
247 #define IND 0
248 void
249 slang_print_ir(const slang_ir_node *n, int indent)
250 {
251 if (!n)
252 return;
253 #if !IND
254 if (n->Opcode != IR_SEQ)
255 #else
256 printf("%3d:", indent);
257 #endif
258 spaces(indent);
259
260 switch (n->Opcode) {
261 case IR_SEQ:
262 #if IND
263 printf("SEQ at %p\n", (void*) n);
264 #endif
265 assert(n->Children[0]);
266 assert(n->Children[1]);
267 slang_print_ir(n->Children[0], indent + IND);
268 slang_print_ir(n->Children[1], indent + IND);
269 break;
270 case IR_SCOPE:
271 printf("NEW SCOPE\n");
272 assert(!n->Children[1]);
273 slang_print_ir(n->Children[0], indent + 3);
274 break;
275 case IR_MOVE:
276 printf("MOVE (writemask = %s)\n", writemask_string(n->Writemask));
277 slang_print_ir(n->Children[0], indent+3);
278 slang_print_ir(n->Children[1], indent+3);
279 break;
280 case IR_LABEL:
281 printf("LABEL: %s\n", n->Target);
282 break;
283 case IR_COND:
284 printf("COND\n");
285 slang_print_ir(n->Children[0], indent + 3);
286 break;
287 case IR_JUMP:
288 printf("JUMP %s\n", n->Target);
289 break;
290 case IR_CJUMP0:
291 printf("CJUMP0 %s\n", n->Target);
292 slang_print_ir(n->Children[0], indent+3);
293 break;
294 case IR_CJUMP1:
295 printf("CJUMP1 %s\n", n->Target);
296 slang_print_ir(n->Children[0], indent+3);
297 break;
298
299 case IR_IF:
300 printf("IF \n");
301 slang_print_ir(n->Children[0], indent+3);
302 spaces(indent);
303 printf("THEN\n");
304 slang_print_ir(n->Children[1], indent+3);
305 if (n->Children[2]) {
306 spaces(indent);
307 printf("ELSE\n");
308 slang_print_ir(n->Children[2], indent+3);
309 }
310 printf("ENDIF\n");
311 break;
312
313 case IR_BEGIN_SUB:
314 printf("BEGIN_SUB\n");
315 break;
316 case IR_END_SUB:
317 printf("END_SUB\n");
318 break;
319 case IR_RETURN:
320 printf("RETURN\n");
321 break;
322 case IR_CALL:
323 printf("CALL\n");
324 break;
325
326 case IR_LOOP:
327 printf("LOOP\n");
328 slang_print_ir(n->Children[0], indent+3);
329 spaces(indent);
330 printf("ENDLOOP\n");
331 break;
332 case IR_CONT:
333 printf("CONT\n");
334 break;
335 case IR_BREAK:
336 printf("BREAK\n");
337 break;
338 case IR_BREAK_IF_FALSE:
339 printf("BREAK_IF_FALSE\n");
340 slang_print_ir(n->Children[0], indent+3);
341 break;
342 case IR_BREAK_IF_TRUE:
343 printf("BREAK_IF_TRUE\n");
344 slang_print_ir(n->Children[0], indent+3);
345 break;
346 case IR_CONT_IF_FALSE:
347 printf("CONT_IF_FALSE\n");
348 slang_print_ir(n->Children[0], indent+3);
349 break;
350 case IR_CONT_IF_TRUE:
351 printf("CONT_IF_TRUE\n");
352 slang_print_ir(n->Children[0], indent+3);
353 break;
354
355 case IR_VAR:
356 printf("VAR %s%s at %s store %p\n",
357 (n->Var ? (char *) n->Var->a_name : "TEMP"),
358 swizzle_string(n->Store->Swizzle),
359 storage_string(n->Store), (void*) n->Store);
360 break;
361 case IR_VAR_DECL:
362 printf("VAR_DECL %s (%p) at %s store %p\n",
363 (n->Var ? (char *) n->Var->a_name : "TEMP"),
364 (void*) n->Var, storage_string(n->Store),
365 (void*) n->Store);
366 break;
367 case IR_FIELD:
368 printf("FIELD %s of\n", n->Target);
369 slang_print_ir(n->Children[0], indent+3);
370 break;
371 case IR_FLOAT:
372 printf("FLOAT %g %g %g %g\n",
373 n->Value[0], n->Value[1], n->Value[2], n->Value[3]);
374 break;
375 case IR_I_TO_F:
376 printf("INT_TO_FLOAT\n");
377 slang_print_ir(n->Children[0], indent+3);
378 break;
379 case IR_F_TO_I:
380 printf("FLOAT_TO_INT\n");
381 slang_print_ir(n->Children[0], indent+3);
382 break;
383 case IR_SWIZZLE:
384 printf("SWIZZLE %s of (store %p) \n",
385 swizzle_string(n->Store->Swizzle), (void*) n->Store);
386 slang_print_ir(n->Children[0], indent + 3);
387 break;
388 default:
389 printf("%s (%p, %p) (store %p)\n", slang_ir_name(n->Opcode),
390 (void*) n->Children[0], (void*) n->Children[1], (void*) n->Store);
391 slang_print_ir(n->Children[0], indent+3);
392 slang_print_ir(n->Children[1], indent+3);
393 }
394 }
395
396
397 /**
398 * Allocate temporary storage for an intermediate result (such as for
399 * a multiply or add, etc.
400 */
401 static GLboolean
402 alloc_temp_storage(slang_var_table *vt, slang_ir_node *n, GLint size)
403 {
404 assert(!n->Var);
405 assert(!n->Store);
406 assert(size > 0);
407 n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
408 if (!_slang_alloc_temp(vt, n->Store)) {
409 RETURN_ERROR("Ran out of registers, too many temporaries", 0);
410 }
411 return GL_TRUE;
412 }
413
414
415 /**
416 * Free temporary storage, if n->Store is, in fact, temp storage.
417 * Otherwise, no-op.
418 */
419 static void
420 free_temp_storage(slang_var_table *vt, slang_ir_node *n)
421 {
422 if (n->Store->File == PROGRAM_TEMPORARY && n->Store->Index >= 0) {
423 if (_slang_is_temp(vt, n->Store)) {
424 _slang_free_temp(vt, n->Store);
425 n->Store->Index = -1;
426 n->Store->Size = -1;
427 }
428 }
429 }
430
431
432 /**
433 * Convert IR storage to an instruction dst register.
434 */
435 static void
436 storage_to_dst_reg(struct prog_dst_register *dst, const slang_ir_storage *st,
437 GLuint writemask)
438 {
439 static const GLuint defaultWritemask[4] = {
440 WRITEMASK_X,
441 WRITEMASK_X | WRITEMASK_Y,
442 WRITEMASK_X | WRITEMASK_Y | WRITEMASK_Z,
443 WRITEMASK_X | WRITEMASK_Y | WRITEMASK_Z | WRITEMASK_W
444 };
445 assert(st->Index >= 0 && st->Index <= 16);
446 dst->File = st->File;
447 dst->Index = st->Index;
448 assert(st->File != PROGRAM_UNDEFINED);
449 assert(st->Size >= 1);
450 assert(st->Size <= 4);
451 if (st->Size == 1) {
452 GLuint comp = GET_SWZ(st->Swizzle, 0);
453 assert(comp < 4);
454 assert(writemask & WRITEMASK_X);
455 dst->WriteMask = WRITEMASK_X << comp;
456 }
457 else {
458 dst->WriteMask = defaultWritemask[st->Size - 1] & writemask;
459 }
460 }
461
462
463 /**
464 * Convert IR storage to an instruction src register.
465 */
466 static void
467 storage_to_src_reg(struct prog_src_register *src, const slang_ir_storage *st)
468 {
469 static const GLuint defaultSwizzle[4] = {
470 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
471 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
472 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
473 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W)
474 };
475 assert(st->File >= 0 && st->File <= 16);
476 src->File = st->File;
477 src->Index = st->Index;
478 assert(st->File != PROGRAM_UNDEFINED);
479 assert(st->Size >= 1);
480 assert(st->Size <= 4);
481 if (st->Swizzle != SWIZZLE_NOOP)
482 src->Swizzle = st->Swizzle;
483 else
484 src->Swizzle = defaultSwizzle[st->Size - 1]; /*XXX really need this?*/
485
486 assert(GET_SWZ(src->Swizzle, 0) != SWIZZLE_NIL);
487 assert(GET_SWZ(src->Swizzle, 1) != SWIZZLE_NIL);
488 assert(GET_SWZ(src->Swizzle, 2) != SWIZZLE_NIL);
489 assert(GET_SWZ(src->Swizzle, 3) != SWIZZLE_NIL);
490 }
491
492
493
494 /**
495 * Add new instruction at end of given program.
496 * \param prog the program to append instruction onto
497 * \param opcode opcode for the new instruction
498 * \return pointer to the new instruction
499 */
500 static struct prog_instruction *
501 new_instruction(struct gl_program *prog, gl_inst_opcode opcode)
502 {
503 struct prog_instruction *inst;
504 prog->Instructions = _mesa_realloc_instructions(prog->Instructions,
505 prog->NumInstructions,
506 prog->NumInstructions + 1);
507 inst = prog->Instructions + prog->NumInstructions;
508 prog->NumInstructions++;
509 _mesa_init_instructions(inst, 1);
510 inst->Opcode = opcode;
511 inst->BranchTarget = -1; /* invalid */
512 return inst;
513 }
514
515
516 /**
517 * Return pointer to last instruction in program.
518 */
519 static struct prog_instruction *
520 prev_instruction(struct gl_program *prog)
521 {
522 if (prog->NumInstructions == 0)
523 return NULL;
524 else
525 return prog->Instructions + prog->NumInstructions - 1;
526 }
527
528
529 static struct prog_instruction *
530 emit(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog);
531
532
533 /**
534 * Return an annotation string for given node's storage.
535 */
536 static char *
537 storage_annotation(const slang_ir_node *n, const struct gl_program *prog)
538 {
539 #if ANNOTATE
540 const slang_ir_storage *st = n->Store;
541 static char s[100] = "";
542
543 if (!st)
544 return _mesa_strdup("");
545
546 switch (st->File) {
547 case PROGRAM_CONSTANT:
548 if (st->Index >= 0) {
549 const GLfloat *val = prog->Parameters->ParameterValues[st->Index];
550 if (st->Swizzle == SWIZZLE_NOOP)
551 sprintf(s, "{%g, %g, %g, %g}", val[0], val[1], val[2], val[3]);
552 else {
553 sprintf(s, "%g", val[GET_SWZ(st->Swizzle, 0)]);
554 }
555 }
556 break;
557 case PROGRAM_TEMPORARY:
558 if (n->Var)
559 sprintf(s, "%s", (char *) n->Var->a_name);
560 else
561 sprintf(s, "t[%d]", st->Index);
562 break;
563 case PROGRAM_STATE_VAR:
564 case PROGRAM_UNIFORM:
565 sprintf(s, "%s", prog->Parameters->Parameters[st->Index].Name);
566 break;
567 case PROGRAM_VARYING:
568 sprintf(s, "%s", prog->Varying->Parameters[st->Index].Name);
569 break;
570 case PROGRAM_INPUT:
571 sprintf(s, "input[%d]", st->Index);
572 break;
573 case PROGRAM_OUTPUT:
574 sprintf(s, "output[%d]", st->Index);
575 break;
576 default:
577 s[0] = 0;
578 }
579 return _mesa_strdup(s);
580 #else
581 return NULL;
582 #endif
583 }
584
585
586 /**
587 * Return an annotation string for an instruction.
588 */
589 static char *
590 instruction_annotation(gl_inst_opcode opcode, char *dstAnnot,
591 char *srcAnnot0, char *srcAnnot1, char *srcAnnot2)
592 {
593 #if ANNOTATE
594 const char *operator;
595 char *s;
596 int len = 50;
597
598 if (dstAnnot)
599 len += strlen(dstAnnot);
600 else
601 dstAnnot = _mesa_strdup("");
602
603 if (srcAnnot0)
604 len += strlen(srcAnnot0);
605 else
606 srcAnnot0 = _mesa_strdup("");
607
608 if (srcAnnot1)
609 len += strlen(srcAnnot1);
610 else
611 srcAnnot1 = _mesa_strdup("");
612
613 if (srcAnnot2)
614 len += strlen(srcAnnot2);
615 else
616 srcAnnot2 = _mesa_strdup("");
617
618 switch (opcode) {
619 case OPCODE_ADD:
620 operator = "+";
621 break;
622 case OPCODE_SUB:
623 operator = "-";
624 break;
625 case OPCODE_MUL:
626 operator = "*";
627 break;
628 case OPCODE_DP3:
629 operator = "DP3";
630 break;
631 case OPCODE_DP4:
632 operator = "DP4";
633 break;
634 case OPCODE_XPD:
635 operator = "XPD";
636 break;
637 case OPCODE_RSQ:
638 operator = "RSQ";
639 break;
640 case OPCODE_SGT:
641 operator = ">";
642 break;
643 default:
644 operator = ",";
645 }
646
647 s = (char *) malloc(len);
648 sprintf(s, "%s = %s %s %s %s", dstAnnot,
649 srcAnnot0, operator, srcAnnot1, srcAnnot2);
650 assert(_mesa_strlen(s) < len);
651
652 free(dstAnnot);
653 free(srcAnnot0);
654 free(srcAnnot1);
655 free(srcAnnot2);
656
657 return s;
658 #else
659 return NULL;
660 #endif
661 }
662
663
664
665 /**
666 * Generate code for a simple arithmetic instruction.
667 * Either 1, 2 or 3 operands.
668 */
669 static struct prog_instruction *
670 emit_arith(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
671 {
672 struct prog_instruction *inst;
673 const slang_ir_info *info = slang_find_ir_info(n->Opcode);
674 char *srcAnnot[3], *dstAnnot;
675 GLuint i;
676
677 assert(info);
678 assert(info->InstOpcode != OPCODE_NOP);
679
680 srcAnnot[0] = srcAnnot[1] = srcAnnot[2] = dstAnnot = NULL;
681
682 #if PEEPHOLE_OPTIMIZATIONS
683 /* Look for MAD opportunity */
684 if (info->NumParams == 2 &&
685 n->Opcode == IR_ADD && n->Children[0]->Opcode == IR_MUL) {
686 /* found pattern IR_ADD(IR_MUL(A, B), C) */
687 emit(vt, n->Children[0]->Children[0], prog); /* A */
688 emit(vt, n->Children[0]->Children[1], prog); /* B */
689 emit(vt, n->Children[1], prog); /* C */
690 /* generate MAD instruction */
691 inst = new_instruction(prog, OPCODE_MAD);
692 /* operands: A, B, C: */
693 storage_to_src_reg(&inst->SrcReg[0], n->Children[0]->Children[0]->Store);
694 storage_to_src_reg(&inst->SrcReg[1], n->Children[0]->Children[1]->Store);
695 storage_to_src_reg(&inst->SrcReg[2], n->Children[1]->Store);
696 free_temp_storage(vt, n->Children[0]->Children[0]);
697 free_temp_storage(vt, n->Children[0]->Children[1]);
698 free_temp_storage(vt, n->Children[1]);
699 }
700 else if (info->NumParams == 2 &&
701 n->Opcode == IR_ADD && n->Children[1]->Opcode == IR_MUL) {
702 /* found pattern IR_ADD(A, IR_MUL(B, C)) */
703 emit(vt, n->Children[0], prog); /* A */
704 emit(vt, n->Children[1]->Children[0], prog); /* B */
705 emit(vt, n->Children[1]->Children[1], prog); /* C */
706 /* generate MAD instruction */
707 inst = new_instruction(prog, OPCODE_MAD);
708 /* operands: B, C, A */
709 storage_to_src_reg(&inst->SrcReg[0], n->Children[1]->Children[0]->Store);
710 storage_to_src_reg(&inst->SrcReg[1], n->Children[1]->Children[1]->Store);
711 storage_to_src_reg(&inst->SrcReg[2], n->Children[0]->Store);
712 free_temp_storage(vt, n->Children[1]->Children[0]);
713 free_temp_storage(vt, n->Children[1]->Children[1]);
714 free_temp_storage(vt, n->Children[0]);
715 }
716 else
717 #endif
718 {
719 /* normal case */
720
721 /* gen code for children */
722 for (i = 0; i < info->NumParams; i++)
723 emit(vt, n->Children[i], prog);
724
725 /* gen this instruction and src registers */
726 inst = new_instruction(prog, info->InstOpcode);
727 for (i = 0; i < info->NumParams; i++)
728 storage_to_src_reg(&inst->SrcReg[i], n->Children[i]->Store);
729
730 /* annotation */
731 for (i = 0; i < info->NumParams; i++)
732 srcAnnot[i] = storage_annotation(n->Children[i], prog);
733
734 /* free temps */
735 for (i = 0; i < info->NumParams; i++)
736 free_temp_storage(vt, n->Children[i]);
737 }
738
739 /* result storage */
740 if (!n->Store) {
741 if (!alloc_temp_storage(vt, n, info->ResultSize))
742 return NULL;
743 }
744 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
745
746 dstAnnot = storage_annotation(n, prog);
747
748 inst->Comment = instruction_annotation(inst->Opcode, dstAnnot, srcAnnot[0],
749 srcAnnot[1], srcAnnot[2]);
750
751 /*_mesa_print_instruction(inst);*/
752 return inst;
753 }
754
755
756 /**
757 * Generate code for an IR_CLAMP instruction.
758 */
759 static struct prog_instruction *
760 emit_clamp(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
761 {
762 struct prog_instruction *inst;
763
764 assert(n->Opcode == IR_CLAMP);
765 /* ch[0] = value
766 * ch[1] = min limit
767 * ch[2] = max limit
768 */
769
770 inst = emit(vt, n->Children[0], prog);
771
772 /* If lower limit == 0.0 and upper limit == 1.0,
773 * set prev instruction's SaturateMode field to SATURATE_ZERO_ONE.
774 * Else,
775 * emit OPCODE_MIN, OPCODE_MAX sequence.
776 */
777 #if 0
778 /* XXX this isn't quite finished yet */
779 if (n->Children[1]->Opcode == IR_FLOAT &&
780 n->Children[1]->Value[0] == 0.0 &&
781 n->Children[1]->Value[1] == 0.0 &&
782 n->Children[1]->Value[2] == 0.0 &&
783 n->Children[1]->Value[3] == 0.0 &&
784 n->Children[2]->Opcode == IR_FLOAT &&
785 n->Children[2]->Value[0] == 1.0 &&
786 n->Children[2]->Value[1] == 1.0 &&
787 n->Children[2]->Value[2] == 1.0 &&
788 n->Children[2]->Value[3] == 1.0) {
789 if (!inst) {
790 inst = prev_instruction(prog);
791 }
792 if (inst && inst->Opcode != OPCODE_NOP) {
793 /* and prev instruction's DstReg matches n->Children[0]->Store */
794 inst->SaturateMode = SATURATE_ZERO_ONE;
795 n->Store = n->Children[0]->Store;
796 return inst;
797 }
798 }
799 #endif
800
801 if (!n->Store)
802 if (!alloc_temp_storage(vt, n, n->Children[0]->Store->Size))
803 return NULL;
804
805 emit(vt, n->Children[1], prog);
806 emit(vt, n->Children[2], prog);
807
808 /* tmp = max(ch[0], ch[1]) */
809 inst = new_instruction(prog, OPCODE_MAX);
810 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
811 storage_to_src_reg(&inst->SrcReg[0], n->Children[0]->Store);
812 storage_to_src_reg(&inst->SrcReg[1], n->Children[1]->Store);
813
814 /* tmp = min(tmp, ch[2]) */
815 inst = new_instruction(prog, OPCODE_MIN);
816 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
817 storage_to_src_reg(&inst->SrcReg[0], n->Store);
818 storage_to_src_reg(&inst->SrcReg[1], n->Children[2]->Store);
819
820 return inst;
821 }
822
823
824 static struct prog_instruction *
825 emit_negation(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
826 {
827 /* Implement as MOV dst, -src; */
828 /* XXX we could look at the previous instruction and in some circumstances
829 * modify it to accomplish the negation.
830 */
831 struct prog_instruction *inst;
832
833 emit(vt, n->Children[0], prog);
834
835 if (!n->Store)
836 if (!alloc_temp_storage(vt, n, n->Children[0]->Store->Size))
837 return NULL;
838
839 inst = new_instruction(prog, OPCODE_MOV);
840 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
841 storage_to_src_reg(&inst->SrcReg[0], n->Children[0]->Store);
842 inst->SrcReg[0].NegateBase = NEGATE_XYZW;
843 inst->Comment = n->Comment;
844 return inst;
845 }
846
847
848 static struct prog_instruction *
849 emit_label(const char *target, struct gl_program *prog)
850 {
851 struct prog_instruction *inst;
852 inst = new_instruction(prog, OPCODE_NOP);
853 inst->Comment = _mesa_strdup(target);
854 return inst;
855 }
856
857
858 static struct prog_instruction *
859 emit_cjump(const char *target, struct gl_program *prog, GLuint zeroOrOne)
860 {
861 struct prog_instruction *inst;
862 inst = new_instruction(prog, OPCODE_BRA);
863 if (zeroOrOne)
864 inst->DstReg.CondMask = COND_NE; /* branch if non-zero */
865 else
866 inst->DstReg.CondMask = COND_EQ; /* branch if equal to zero */
867 inst->DstReg.CondSwizzle = SWIZZLE_X;
868 inst->Comment = _mesa_strdup(target);
869 return inst;
870 }
871
872
873 static struct prog_instruction *
874 emit_jump(const char *target, struct gl_program *prog)
875 {
876 struct prog_instruction *inst;
877 inst = new_instruction(prog, OPCODE_BRA);
878 inst->DstReg.CondMask = COND_TR; /* always branch */
879 /*inst->DstReg.CondSwizzle = SWIZZLE_X;*/
880 inst->Comment = _mesa_strdup(target);
881 return inst;
882 }
883
884
885 static struct prog_instruction *
886 emit_kill(struct gl_program *prog)
887 {
888 struct prog_instruction *inst;
889 /* NV-KILL - discard fragment depending on condition code.
890 * Note that ARB-KILL depends on sign of vector operand.
891 */
892 inst = new_instruction(prog, OPCODE_KIL_NV);
893 inst->DstReg.CondMask = COND_TR; /* always branch */
894 return inst;
895 }
896
897
898 static struct prog_instruction *
899 emit_tex(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
900 {
901 struct prog_instruction *inst;
902 if (n->Opcode == IR_TEX) {
903 inst = new_instruction(prog, OPCODE_TEX);
904 }
905 else if (n->Opcode == IR_TEXB) {
906 inst = new_instruction(prog, OPCODE_TXB);
907 }
908 else {
909 assert(n->Opcode == IR_TEXP);
910 inst = new_instruction(prog, OPCODE_TXP);
911 }
912
913 if (!n->Store)
914 if (!alloc_temp_storage(vt, n, 4))
915 return NULL;
916
917 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
918
919 (void) emit(vt, n->Children[1], prog);
920
921 /* Child[1] is the coord */
922 storage_to_src_reg(&inst->SrcReg[0], n->Children[1]->Store);
923
924 /* Child[0] is the sampler (a uniform which'll indicate the texture unit) */
925 assert(n->Children[0]->Store);
926 assert(n->Children[0]->Store->Size >= TEXTURE_1D_INDEX);
927
928 inst->Sampler = n->Children[0]->Store->Index; /* i.e. uniform's index */
929 inst->TexSrcTarget = n->Children[0]->Store->Size;
930 inst->TexSrcUnit = 27; /* Dummy value; the TexSrcUnit will be computed at
931 * link time, using the sampler uniform's value.
932 */
933 return inst;
934 }
935
936
937 static struct prog_instruction *
938 emit_move(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
939 {
940 struct prog_instruction *inst;
941
942 /* rhs */
943 assert(n->Children[1]);
944 inst = emit(vt, n->Children[1], prog);
945
946 assert(n->Children[1]->Store->Index >= 0);
947
948 /* lhs */
949 emit(vt, n->Children[0], prog);
950
951 assert(!n->Store);
952 n->Store = n->Children[0]->Store;
953
954 #if PEEPHOLE_OPTIMIZATIONS
955 if (inst && _slang_is_temp(vt, n->Children[1]->Store)) {
956 /* Peephole optimization:
957 * Just modify the RHS to put its result into the dest of this
958 * MOVE operation. Then, this MOVE is a no-op.
959 */
960 _slang_free_temp(vt, n->Children[1]->Store);
961 *n->Children[1]->Store = *n->Children[0]->Store;
962 /* fixup the prev (RHS) instruction */
963 assert(n->Children[0]->Store->Index >= 0);
964 assert(n->Children[0]->Store->Index < 16);
965 storage_to_dst_reg(&inst->DstReg, n->Children[0]->Store, n->Writemask);
966 return inst;
967 }
968 else
969 #endif
970 {
971 if (n->Children[0]->Store->Size > 4) {
972 /* move matrix/struct etc (block of registers) */
973 slang_ir_storage dstStore = *n->Children[0]->Store;
974 slang_ir_storage srcStore = *n->Children[1]->Store;
975 GLint size = srcStore.Size;
976 ASSERT(n->Children[0]->Writemask == WRITEMASK_XYZW);
977 ASSERT(n->Children[1]->Store->Swizzle == SWIZZLE_NOOP);
978 dstStore.Size = 4;
979 srcStore.Size = 4;
980 while (size >= 4) {
981 inst = new_instruction(prog, OPCODE_MOV);
982 inst->Comment = _mesa_strdup("IR_MOVE block");
983 storage_to_dst_reg(&inst->DstReg, &dstStore, n->Writemask);
984 storage_to_src_reg(&inst->SrcReg[0], &srcStore);
985 srcStore.Index++;
986 dstStore.Index++;
987 size -= 4;
988 }
989 }
990 else {
991 /* single register move */
992 char *srcAnnot, *dstAnnot;
993 inst = new_instruction(prog, OPCODE_MOV);
994 assert(n->Children[0]->Store->Index >= 0);
995 assert(n->Children[0]->Store->Index < 16);
996 storage_to_dst_reg(&inst->DstReg, n->Children[0]->Store, n->Writemask);
997 storage_to_src_reg(&inst->SrcReg[0], n->Children[1]->Store);
998 dstAnnot = storage_annotation(n->Children[0], prog);
999 srcAnnot = storage_annotation(n->Children[1], prog);
1000 inst->Comment = instruction_annotation(inst->Opcode, dstAnnot,
1001 srcAnnot, NULL, NULL);
1002 }
1003 free_temp_storage(vt, n->Children[1]);
1004 return inst;
1005 }
1006 }
1007
1008
1009 static struct prog_instruction *
1010 emit_cond(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1011 {
1012 /* Conditional expression (in if/while/for stmts).
1013 * Need to update condition code register.
1014 * Next instruction is typically an IR_CJUMP0/1.
1015 */
1016 /* last child expr instruction: */
1017 struct prog_instruction *inst = emit(vt, n->Children[0], prog);
1018 if (inst) {
1019 /* set inst's CondUpdate flag */
1020 inst->CondUpdate = GL_TRUE;
1021 return inst; /* XXX or null? */
1022 }
1023 else {
1024 /* This'll happen for things like "if (i) ..." where no code
1025 * is normally generated for the expression "i".
1026 * Generate a move instruction just to set condition codes.
1027 * Note: must use full 4-component vector since all four
1028 * condition codes must be set identically.
1029 */
1030 if (!alloc_temp_storage(vt, n, 4))
1031 return NULL;
1032 inst = new_instruction(prog, OPCODE_MOV);
1033 inst->CondUpdate = GL_TRUE;
1034 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
1035 storage_to_src_reg(&inst->SrcReg[0], n->Children[0]->Store);
1036 _slang_free_temp(vt, n->Store);
1037 inst->Comment = _mesa_strdup("COND expr");
1038 return inst; /* XXX or null? */
1039 }
1040 }
1041
1042
1043 /**
1044 * Logical-NOT
1045 */
1046 static struct prog_instruction *
1047 emit_not(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1048 {
1049 GLfloat zero = 0.0;
1050 slang_ir_storage st;
1051 struct prog_instruction *inst;
1052
1053 /* need zero constant */
1054 st.File = PROGRAM_CONSTANT;
1055 st.Size = 1;
1056 st.Index = _mesa_add_unnamed_constant(prog->Parameters, &zero,
1057 1, &st.Swizzle);
1058
1059 /* child expr */
1060 (void) emit(vt, n->Children[0], prog);
1061 /* XXXX if child instr is SGT convert to SLE, if SEQ, SNE, etc */
1062
1063 if (!n->Store)
1064 if (!alloc_temp_storage(vt, n, n->Children[0]->Store->Size))
1065 return NULL;
1066
1067 inst = new_instruction(prog, OPCODE_SEQ);
1068 storage_to_dst_reg(&inst->DstReg, n->Store, n->Writemask);
1069 storage_to_src_reg(&inst->SrcReg[0], n->Children[0]->Store);
1070 storage_to_src_reg(&inst->SrcReg[1], &st);
1071
1072 free_temp_storage(vt, n->Children[0]);
1073
1074 inst->Comment = _mesa_strdup("NOT");
1075 return inst;
1076 }
1077
1078
1079 static struct prog_instruction *
1080 emit_if(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1081 {
1082 struct prog_instruction *ifInst;
1083 GLuint ifInstLoc, elseInstLoc;
1084
1085 emit(vt, n->Children[0], prog); /* the condition */
1086 ifInstLoc = prog->NumInstructions;
1087 if (EmitHighLevelInstructions) {
1088 ifInst = new_instruction(prog, OPCODE_IF);
1089 ifInst->DstReg.CondMask = COND_NE; /* if cond is non-zero */
1090 ifInst->DstReg.CondSwizzle = SWIZZLE_X;
1091 }
1092 else {
1093 /* conditional jump to else, or endif */
1094 ifInst = new_instruction(prog, OPCODE_BRA);
1095 ifInst->DstReg.CondMask = COND_EQ; /* BRA if cond is zero */
1096 ifInst->DstReg.CondSwizzle = SWIZZLE_X;
1097 ifInst->Comment = _mesa_strdup("if zero");
1098 }
1099
1100 /* if body */
1101 emit(vt, n->Children[1], prog);
1102
1103 if (n->Children[2]) {
1104 /* have else body */
1105 elseInstLoc = prog->NumInstructions;
1106 if (EmitHighLevelInstructions) {
1107 (void) new_instruction(prog, OPCODE_ELSE);
1108 }
1109 else {
1110 /* jump to endif instruction */
1111 struct prog_instruction *inst;
1112 inst = new_instruction(prog, OPCODE_BRA);
1113 inst->Comment = _mesa_strdup("else");
1114 inst->DstReg.CondMask = COND_TR; /* always branch */
1115 }
1116 ifInst = prog->Instructions + ifInstLoc;
1117 ifInst->BranchTarget = prog->NumInstructions;
1118
1119 emit(vt, n->Children[2], prog);
1120 }
1121 else {
1122 /* no else body */
1123 ifInst = prog->Instructions + ifInstLoc;
1124 ifInst->BranchTarget = prog->NumInstructions + 1;
1125 }
1126
1127 if (EmitHighLevelInstructions) {
1128 (void) new_instruction(prog, OPCODE_ENDIF);
1129 }
1130
1131 if (n->Children[2]) {
1132 struct prog_instruction *elseInst;
1133 elseInst = prog->Instructions + elseInstLoc;
1134 elseInst->BranchTarget = prog->NumInstructions;
1135 }
1136 return NULL;
1137 }
1138
1139
1140 static struct prog_instruction *
1141 emit_loop(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1142 {
1143 struct prog_instruction *beginInst, *endInst;
1144 GLuint beginInstLoc, endInstLoc;
1145 slang_ir_node *ir;
1146
1147 /* emit OPCODE_BGNLOOP */
1148 beginInstLoc = prog->NumInstructions;
1149 if (EmitHighLevelInstructions) {
1150 (void) new_instruction(prog, OPCODE_BGNLOOP);
1151 }
1152
1153 /* body */
1154 emit(vt, n->Children[0], prog);
1155
1156 endInstLoc = prog->NumInstructions;
1157 if (EmitHighLevelInstructions) {
1158 /* emit OPCODE_ENDLOOP */
1159 endInst = new_instruction(prog, OPCODE_ENDLOOP);
1160 }
1161 else {
1162 /* emit unconditional BRA-nch */
1163 endInst = new_instruction(prog, OPCODE_BRA);
1164 endInst->DstReg.CondMask = COND_TR; /* always true */
1165 }
1166 /* end instruction's BranchTarget points to top of loop */
1167 endInst->BranchTarget = beginInstLoc;
1168
1169 if (EmitHighLevelInstructions) {
1170 /* BGNLOOP's BranchTarget points to the ENDLOOP inst */
1171 beginInst = prog->Instructions + beginInstLoc;
1172 beginInst->BranchTarget = prog->NumInstructions - 1;
1173 }
1174
1175 /* Done emitting loop code. Now walk over the loop's linked list of
1176 * BREAK and CONT nodes, filling in their BranchTarget fields (which
1177 * will point to the ENDLOOP+1 or BGNLOOP instructions, respectively).
1178 */
1179 for (ir = n->BranchNode; ir; ir = ir->BranchNode) {
1180 struct prog_instruction *inst = prog->Instructions + ir->InstLocation;
1181 assert(inst->BranchTarget < 0);
1182 if (ir->Opcode == IR_BREAK ||
1183 ir->Opcode == IR_BREAK_IF_FALSE ||
1184 ir->Opcode == IR_BREAK_IF_TRUE) {
1185 assert(inst->Opcode == OPCODE_BRK ||
1186 inst->Opcode == OPCODE_BRA);
1187 /* go to instruction after end of loop */
1188 inst->BranchTarget = endInstLoc + 1;
1189 }
1190 else {
1191 assert(ir->Opcode == IR_CONT ||
1192 ir->Opcode == IR_CONT_IF_FALSE ||
1193 ir->Opcode == IR_CONT_IF_TRUE);
1194 assert(inst->Opcode == OPCODE_CONT ||
1195 inst->Opcode == OPCODE_BRA);
1196 /* to go instruction at top of loop */
1197 inst->BranchTarget = beginInstLoc;
1198 }
1199 }
1200 return NULL;
1201 }
1202
1203
1204 /**
1205 * "Continue" or "break" statement.
1206 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1207 */
1208 static struct prog_instruction *
1209 emit_cont_break(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1210 {
1211 gl_inst_opcode opcode;
1212 struct prog_instruction *inst;
1213 n->InstLocation = prog->NumInstructions;
1214 if (EmitHighLevelInstructions) {
1215 opcode = (n->Opcode == IR_CONT) ? OPCODE_CONT : OPCODE_BRK;
1216 }
1217 else {
1218 opcode = OPCODE_BRA;
1219 }
1220 inst = new_instruction(prog, opcode);
1221 inst->DstReg.CondMask = COND_TR; /* always true */
1222 return inst;
1223 }
1224
1225
1226 /**
1227 * Conditional "continue" or "break" statement.
1228 * Either OPCODE_CONT, OPCODE_BRK or OPCODE_BRA will be emitted.
1229 */
1230 static struct prog_instruction *
1231 emit_cont_break_if(slang_var_table *vt, slang_ir_node *n,
1232 struct gl_program *prog, GLboolean breakTrue)
1233 {
1234 gl_inst_opcode opcode;
1235 struct prog_instruction *inst;
1236
1237 /* evaluate condition expr, setting cond codes */
1238 inst = emit(vt, n->Children[0], prog);
1239 assert(inst);
1240 inst->CondUpdate = GL_TRUE;
1241
1242 n->InstLocation = prog->NumInstructions;
1243 if (EmitHighLevelInstructions) {
1244 if (n->Opcode == IR_CONT_IF_TRUE ||
1245 n->Opcode == IR_CONT_IF_FALSE)
1246 opcode = OPCODE_CONT;
1247 else
1248 opcode = OPCODE_BRK;
1249 }
1250 else {
1251 opcode = OPCODE_BRA;
1252 }
1253 inst = new_instruction(prog, opcode);
1254 inst->DstReg.CondMask = breakTrue ? COND_NE : COND_EQ;
1255 return inst;
1256 }
1257
1258
1259
1260 /**
1261 * Remove any SWIZZLE_NIL terms from given swizzle mask (smear prev term).
1262 * Ex: fix_swizzle("zyNN") -> "zyyy"
1263 */
1264 static GLuint
1265 fix_swizzle(GLuint swizzle)
1266 {
1267 GLuint swz[4], i;
1268 for (i = 0; i < 4; i++) {
1269 swz[i] = GET_SWZ(swizzle, i);
1270 if (swz[i] == SWIZZLE_NIL) {
1271 swz[i] = swz[i - 1];
1272 }
1273 }
1274 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
1275 }
1276
1277
1278 static struct prog_instruction *
1279 emit_swizzle(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1280 {
1281 GLuint swizzle;
1282
1283 /* swizzled storage access */
1284 (void) emit(vt, n->Children[0], prog);
1285
1286 /* "pull-up" the child's storage info, applying our swizzle info */
1287 n->Store->File = n->Children[0]->Store->File;
1288 n->Store->Index = n->Children[0]->Store->Index;
1289 n->Store->Size = n->Children[0]->Store->Size;
1290 /*n->Var = n->Children[0]->Var; XXX for debug */
1291 assert(n->Store->Index >= 0);
1292
1293 swizzle = fix_swizzle(n->Store->Swizzle);
1294 #ifdef DEBUG
1295 {
1296 GLuint s = n->Children[0]->Store->Swizzle;
1297 assert(GET_SWZ(s, 0) != SWIZZLE_NIL);
1298 assert(GET_SWZ(s, 1) != SWIZZLE_NIL);
1299 assert(GET_SWZ(s, 2) != SWIZZLE_NIL);
1300 assert(GET_SWZ(s, 3) != SWIZZLE_NIL);
1301 }
1302 #endif
1303
1304 /* apply this swizzle to child's swizzle to get composed swizzle */
1305 n->Store->Swizzle = swizzle_swizzle(n->Children[0]->Store->Swizzle,
1306 swizzle);
1307 return NULL;
1308 }
1309
1310
1311 /**
1312 * Dereference array element. Just resolve storage for the array
1313 * element represented by this node.
1314 */
1315 static struct prog_instruction *
1316 emit_array_element(slang_var_table *vt, slang_ir_node *n,
1317 struct gl_program *prog)
1318 {
1319 assert(n->Store);
1320 assert(n->Store->File != PROGRAM_UNDEFINED);
1321 assert(n->Store->Size > 0);
1322
1323 if (n->Store->File == PROGRAM_STATE_VAR) {
1324 n->Store->Index = _slang_alloc_statevar(n, prog->Parameters);
1325 return NULL;
1326 }
1327
1328
1329 if (n->Children[1]->Opcode == IR_FLOAT) {
1330 /* Constant index */
1331 const GLint arrayAddr = n->Children[0]->Store->Index;
1332 const GLint index = (GLint) n->Children[1]->Value[0];
1333 n->Store->Index = arrayAddr + index;
1334 }
1335 else {
1336 /* Variable index - PROBLEM */
1337 const GLint arrayAddr = n->Children[0]->Store->Index;
1338 const GLint index = 0;
1339 _mesa_problem(NULL, "variable array indexes not supported yet!");
1340 n->Store->Index = arrayAddr + index;
1341 }
1342 return NULL; /* no instruction */
1343 }
1344
1345
1346 /**
1347 * Resolve storage for accessing a structure field.
1348 */
1349 static struct prog_instruction *
1350 emit_struct_field(slang_var_table *vt, slang_ir_node *n,
1351 struct gl_program *prog)
1352 {
1353 if (n->Store->File == PROGRAM_STATE_VAR) {
1354 n->Store->Index = _slang_alloc_statevar(n, prog->Parameters);
1355 return NULL;
1356 }
1357
1358 if (n->Children[0]->Store->File == PROGRAM_STATE_VAR) {
1359 /* state variable sub-field */
1360 GLint pos;
1361 GLuint swizzle;
1362 assert(n->Children[0]->Opcode == IR_VAR);
1363 pos = _slang_lookup_statevar_field((char *) n->Children[0]->Var->a_name,
1364 n->Target,
1365 prog->Parameters, &swizzle);
1366 if (pos < 0) {
1367 RETURN_ERROR2("Undefined structure member", n->Target, 0);
1368 }
1369 assert(n->Store);
1370 assert(n->Store->File == PROGRAM_STATE_VAR);
1371 n->Store->Index = pos;
1372 n->Store->Swizzle = swizzle;
1373 }
1374 else {
1375 _mesa_problem(NULL, "structs/fields not supported yet");
1376 }
1377 return NULL; /* no instruction */
1378 }
1379
1380
1381 static struct prog_instruction *
1382 emit(slang_var_table *vt, slang_ir_node *n, struct gl_program *prog)
1383 {
1384 struct prog_instruction *inst;
1385 if (!n)
1386 return NULL;
1387
1388 switch (n->Opcode) {
1389 case IR_SEQ:
1390 /* sequence of two sub-trees */
1391 assert(n->Children[0]);
1392 assert(n->Children[1]);
1393 emit(vt, n->Children[0], prog);
1394 inst = emit(vt, n->Children[1], prog);
1395 assert(!n->Store);
1396 n->Store = n->Children[1]->Store;
1397 return inst;
1398
1399 case IR_SCOPE:
1400 /* new variable scope */
1401 _slang_push_var_table(vt);
1402 inst = emit(vt, n->Children[0], prog);
1403 _slang_pop_var_table(vt);
1404 return inst;
1405
1406 case IR_VAR_DECL:
1407 /* Variable declaration - allocate a register for it */
1408 assert(n->Store);
1409 assert(n->Store->File != PROGRAM_UNDEFINED);
1410 assert(n->Store->Size > 0);
1411 assert(n->Store->Index < 0);
1412 if (!n->Var || n->Var->isTemp) {
1413 /* a nameless/temporary variable, will be freed after first use */
1414 if (!_slang_alloc_temp(vt, n->Store))
1415 RETURN_ERROR("Ran out of registers, too many temporaries", 0);
1416 }
1417 else {
1418 /* a regular variable */
1419 _slang_add_variable(vt, n->Var);
1420 if (!_slang_alloc_var(vt, n->Store))
1421 RETURN_ERROR("Ran out of registers, too many variables", 0);
1422 /*
1423 printf("IR_VAR_DECL %s %d store %p\n",
1424 (char*) n->Var->a_name, n->Store->Index, (void*) n->Store);
1425 */
1426 assert(n->Var->aux == n->Store);
1427 }
1428 break;
1429
1430 case IR_VAR:
1431 /* Reference to a variable
1432 * Storage should have already been resolved/allocated.
1433 */
1434 assert(n->Store);
1435 assert(n->Store->File != PROGRAM_UNDEFINED);
1436
1437 if (n->Store->File == PROGRAM_STATE_VAR &&
1438 n->Store->Index < 0) {
1439 n->Store->Index = _slang_alloc_statevar(n, prog->Parameters);
1440 }
1441
1442 if (n->Store->Index < 0) {
1443 printf("#### VAR %s not allocated!\n", (char*)n->Var->a_name);
1444 }
1445 assert(n->Store->Index >= 0);
1446 assert(n->Store->Size > 0);
1447 break;
1448
1449 case IR_ELEMENT:
1450 return emit_array_element(vt, n, prog);
1451 case IR_FIELD:
1452 return emit_struct_field(vt, n, prog);
1453 case IR_SWIZZLE:
1454 return emit_swizzle(vt, n, prog);
1455
1456 case IR_I_TO_F:
1457 {
1458 n->Store = n->Children[0]->Store;
1459 }
1460 return NULL;
1461
1462 /* Simple arithmetic */
1463 /* unary */
1464 case IR_RSQ:
1465 case IR_RCP:
1466 case IR_FLOOR:
1467 case IR_FRAC:
1468 case IR_F_TO_I:
1469 case IR_ABS:
1470 case IR_SIN:
1471 case IR_COS:
1472 case IR_DDX:
1473 case IR_DDY:
1474 case IR_NOISE1:
1475 case IR_NOISE2:
1476 case IR_NOISE3:
1477 case IR_NOISE4:
1478 /* binary */
1479 case IR_ADD:
1480 case IR_SUB:
1481 case IR_MUL:
1482 case IR_DOT4:
1483 case IR_DOT3:
1484 case IR_CROSS:
1485 case IR_MIN:
1486 case IR_MAX:
1487 case IR_SEQUAL:
1488 case IR_SNEQUAL:
1489 case IR_SGE:
1490 case IR_SGT:
1491 case IR_POW:
1492 case IR_EXP:
1493 case IR_EXP2:
1494 /* trinary operators */
1495 case IR_LRP:
1496 return emit_arith(vt, n, prog);
1497 case IR_CLAMP:
1498 return emit_clamp(vt, n, prog);
1499 case IR_TEX:
1500 case IR_TEXB:
1501 case IR_TEXP:
1502 return emit_tex(vt, n, prog);
1503 case IR_NEG:
1504 return emit_negation(vt, n, prog);
1505 case IR_FLOAT:
1506 /* find storage location for this float constant */
1507 n->Store->Index = _mesa_add_unnamed_constant(prog->Parameters, n->Value,
1508 n->Store->Size,
1509 &n->Store->Swizzle);
1510 if (n->Store->Index < 0) {
1511 RETURN_ERROR("Ran out of space for constants.", 0);
1512 }
1513 return NULL;
1514
1515 case IR_MOVE:
1516 return emit_move(vt, n, prog);
1517
1518 case IR_COND:
1519 return emit_cond(vt, n, prog);
1520
1521 case IR_NOT:
1522 return emit_not(vt, n, prog);
1523
1524 case IR_LABEL:
1525 return emit_label(n->Target, prog);
1526 case IR_JUMP:
1527 return emit_jump(n->Target, prog);
1528 case IR_CJUMP0:
1529 return emit_cjump(n->Target, prog, 0);
1530 case IR_CJUMP1:
1531 return emit_cjump(n->Target, prog, 1);
1532 case IR_KILL:
1533 return emit_kill(prog);
1534
1535 case IR_IF:
1536 return emit_if(vt, n, prog);
1537
1538 case IR_LOOP:
1539 return emit_loop(vt, n, prog);
1540 case IR_BREAK_IF_FALSE:
1541 case IR_CONT_IF_FALSE:
1542 return emit_cont_break_if(vt, n, prog, GL_FALSE);
1543 case IR_BREAK_IF_TRUE:
1544 case IR_CONT_IF_TRUE:
1545 return emit_cont_break_if(vt, n, prog, GL_TRUE);
1546 case IR_BREAK:
1547 /* fall-through */
1548 case IR_CONT:
1549 return emit_cont_break(vt, n, prog);
1550
1551 case IR_BEGIN_SUB:
1552 return new_instruction(prog, OPCODE_BGNSUB);
1553 case IR_END_SUB:
1554 return new_instruction(prog, OPCODE_ENDSUB);
1555 case IR_RETURN:
1556 return new_instruction(prog, OPCODE_RET);
1557
1558 case IR_NOP:
1559 return NULL;
1560
1561 default:
1562 _mesa_problem(NULL, "Unexpected IR opcode in emit()\n");
1563 abort();
1564 }
1565 return NULL;
1566 }
1567
1568
1569 GLboolean
1570 _slang_emit_code(slang_ir_node *n, slang_var_table *vt,
1571 struct gl_program *prog, GLboolean withEnd)
1572 {
1573 GLboolean success;
1574
1575 if (emit(vt, n, prog)) {
1576 /* finish up by adding the END opcode to program */
1577 if (withEnd) {
1578 struct prog_instruction *inst;
1579 inst = new_instruction(prog, OPCODE_END);
1580 }
1581 success = GL_TRUE;
1582 }
1583 else {
1584 /* record an error? */
1585 success = GL_FALSE;
1586 }
1587
1588 printf("*********** End generate code (%u inst):\n", prog->NumInstructions);
1589 #if 0
1590 _mesa_print_program(prog);
1591 _mesa_print_program_parameters(ctx,prog);
1592 #endif
1593
1594 return success;
1595 }