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