Tizen 2.1 base
[sdk/emulator/qemu.git] / gl / mesa / src / gallium / drivers / r300 / compiler / radeon_program_print.c
1 /*
2  * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "radeon_program.h"
24
25 #include <stdio.h>
26
27 static const char * textarget_to_string(rc_texture_target target)
28 {
29         switch(target) {
30         case RC_TEXTURE_2D_ARRAY: return "2D_ARRAY";
31         case RC_TEXTURE_1D_ARRAY: return "1D_ARRAY";
32         case RC_TEXTURE_CUBE: return "CUBE";
33         case RC_TEXTURE_3D: return "3D";
34         case RC_TEXTURE_RECT: return "RECT";
35         case RC_TEXTURE_2D: return "2D";
36         case RC_TEXTURE_1D: return "1D";
37         default: return "BAD_TEXTURE_TARGET";
38         }
39 }
40
41 static const char * presubtract_op_to_string(rc_presubtract_op op)
42 {
43         switch(op) {
44         case RC_PRESUB_NONE:
45                 return "NONE";
46         case RC_PRESUB_BIAS:
47                 return "(1 - 2 * src0)";
48         case RC_PRESUB_SUB:
49                 return "(src1 - src0)";
50         case RC_PRESUB_ADD:
51                 return "(src1 + src0)";
52         case RC_PRESUB_INV:
53                 return "(1 - src0)";
54         default:
55                 return "BAD_PRESUBTRACT_OP";
56         }
57 }
58
59 static void print_omod_op(FILE * f, rc_omod_op op)
60 {
61         const char * omod_str;
62
63         switch(op) {
64         case RC_OMOD_MUL_1:
65         case RC_OMOD_DISABLE:
66                 return;
67         case RC_OMOD_MUL_2:
68                 omod_str = "* 2";
69                 break;
70         case RC_OMOD_MUL_4:
71                 omod_str = "* 4";
72                 break;
73         case RC_OMOD_MUL_8:
74                 omod_str = "* 8";
75                 break;
76         case RC_OMOD_DIV_2:
77                 omod_str = "/ 2";
78                 break;
79         case RC_OMOD_DIV_4:
80                 omod_str = "/ 4";
81                 break;
82         case RC_OMOD_DIV_8:
83                 omod_str = "/ 8";
84                 break;
85         default:
86                 return;
87         }
88         fprintf(f, " %s", omod_str);
89 }
90
91 static void rc_print_comparefunc(FILE * f, const char * lhs, rc_compare_func func, const char * rhs)
92 {
93         if (func == RC_COMPARE_FUNC_NEVER) {
94                 fprintf(f, "false");
95         } else if (func == RC_COMPARE_FUNC_ALWAYS) {
96                 fprintf(f, "true");
97         } else {
98                 const char * op;
99                 switch(func) {
100                 case RC_COMPARE_FUNC_LESS: op = "<"; break;
101                 case RC_COMPARE_FUNC_EQUAL: op = "=="; break;
102                 case RC_COMPARE_FUNC_LEQUAL: op = "<="; break;
103                 case RC_COMPARE_FUNC_GREATER: op = ">"; break;
104                 case RC_COMPARE_FUNC_NOTEQUAL: op = "!="; break;
105                 case RC_COMPARE_FUNC_GEQUAL: op = ">="; break;
106                 default: op = "???"; break;
107                 }
108                 fprintf(f, "%s %s %s", lhs, op, rhs);
109         }
110 }
111
112 static void rc_print_register(FILE * f, rc_register_file file, int index, unsigned int reladdr)
113 {
114         if (file == RC_FILE_NONE) {
115                 fprintf(f, "none");
116         } else if (file == RC_FILE_SPECIAL) {
117                 switch(index) {
118                 case RC_SPECIAL_ALU_RESULT: fprintf(f, "aluresult"); break;
119                 default: fprintf(f, "special[%i]", index); break;
120                 }
121         } else {
122                 const char * filename;
123                 switch(file) {
124                 case RC_FILE_TEMPORARY: filename = "temp"; break;
125                 case RC_FILE_INPUT: filename = "input"; break;
126                 case RC_FILE_OUTPUT: filename = "output"; break;
127                 case RC_FILE_ADDRESS: filename = "addr"; break;
128                 case RC_FILE_CONSTANT: filename = "const"; break;
129                 default: filename = "BAD FILE"; break;
130                 }
131                 fprintf(f, "%s[%i%s]", filename, index, reladdr ? " + addr[0]" : "");
132         }
133 }
134
135 static void rc_print_mask(FILE * f, unsigned int mask)
136 {
137         if (mask & RC_MASK_X) fprintf(f, "x");
138         if (mask & RC_MASK_Y) fprintf(f, "y");
139         if (mask & RC_MASK_Z) fprintf(f, "z");
140         if (mask & RC_MASK_W) fprintf(f, "w");
141 }
142
143 static void rc_print_dst_register(FILE * f, struct rc_dst_register dst)
144 {
145         rc_print_register(f, dst.File, dst.Index, 0);
146         if (dst.WriteMask != RC_MASK_XYZW) {
147                 fprintf(f, ".");
148                 rc_print_mask(f, dst.WriteMask);
149         }
150 }
151
152 static char rc_swizzle_char(unsigned int swz)
153 {
154         switch(swz) {
155         case RC_SWIZZLE_X: return 'x';
156         case RC_SWIZZLE_Y: return 'y';
157         case RC_SWIZZLE_Z: return 'z';
158         case RC_SWIZZLE_W: return 'w';
159         case RC_SWIZZLE_ZERO: return '0';
160         case RC_SWIZZLE_ONE: return '1';
161         case RC_SWIZZLE_HALF: return 'H';
162         case RC_SWIZZLE_UNUSED: return '_';
163         }
164         fprintf(stderr, "bad swz: %u\n", swz);
165         return '?';
166 }
167
168 static void rc_print_swizzle(FILE * f, unsigned int swizzle, unsigned int negate)
169 {
170         unsigned int comp;
171         for(comp = 0; comp < 4; ++comp) {
172                 rc_swizzle swz = GET_SWZ(swizzle, comp);
173                 if (GET_BIT(negate, comp))
174                         fprintf(f, "-");
175                 fprintf(f, "%c", rc_swizzle_char(swz));
176         }
177 }
178
179 static void rc_print_presub_instruction(FILE * f,
180                                         struct rc_presub_instruction inst)
181 {
182         fprintf(f,"(");
183         switch(inst.Opcode){
184         case RC_PRESUB_BIAS:
185                 fprintf(f, "1 - 2 * ");
186                 rc_print_register(f, inst.SrcReg[0].File,
187                                 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
188                 break;
189         case RC_PRESUB_SUB:
190                 rc_print_register(f, inst.SrcReg[1].File,
191                                 inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
192                 fprintf(f, " - ");
193                 rc_print_register(f, inst.SrcReg[0].File,
194                                 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
195                 break;
196         case RC_PRESUB_ADD:
197                 rc_print_register(f, inst.SrcReg[1].File,
198                                 inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
199                 fprintf(f, " + ");
200                 rc_print_register(f, inst.SrcReg[0].File,
201                                 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
202                 break;
203         case RC_PRESUB_INV:
204                 fprintf(f, "1 - ");
205                 rc_print_register(f, inst.SrcReg[0].File,
206                                 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
207                 break;
208         default:
209                 break;
210         }
211         fprintf(f, ")");
212 }
213
214 static void rc_print_src_register(FILE * f, struct rc_instruction * inst,
215                                                 struct rc_src_register src)
216 {
217         int trivial_negate = (src.Negate == RC_MASK_NONE || src.Negate == RC_MASK_XYZW);
218
219         if (src.Negate == RC_MASK_XYZW)
220                 fprintf(f, "-");
221         if (src.Abs)
222                 fprintf(f, "|");
223
224         if(src.File == RC_FILE_PRESUB)
225                 rc_print_presub_instruction(f, inst->U.I.PreSub);
226         else
227                 rc_print_register(f, src.File, src.Index, src.RelAddr);
228
229         if (src.Abs && !trivial_negate)
230                 fprintf(f, "|");
231
232         if (src.Swizzle != RC_SWIZZLE_XYZW || !trivial_negate) {
233                 fprintf(f, ".");
234                 rc_print_swizzle(f, src.Swizzle, trivial_negate ? 0 : src.Negate);
235         }
236
237         if (src.Abs && trivial_negate)
238                 fprintf(f, "|");
239 }
240
241 static unsigned update_branch_depth(rc_opcode opcode, unsigned *branch_depth)
242 {
243         switch (opcode) {
244         case RC_OPCODE_IF:
245         case RC_OPCODE_BGNLOOP:
246                 return (*branch_depth)++ * 2;
247
248         case RC_OPCODE_ENDIF:
249         case RC_OPCODE_ENDLOOP:
250                 assert(*branch_depth > 0);
251                 return --(*branch_depth) * 2;
252
253         case RC_OPCODE_ELSE:
254                 assert(*branch_depth > 0);
255                 return (*branch_depth - 1) * 2;
256
257         default:
258                 return *branch_depth * 2;
259         }
260 }
261
262 static void rc_print_normal_instruction(FILE * f, struct rc_instruction * inst, unsigned *branch_depth)
263 {
264         const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
265         unsigned int reg;
266         unsigned spaces = update_branch_depth(inst->U.I.Opcode, branch_depth);
267
268         for (unsigned i = 0; i < spaces; i++)
269                 fprintf(f, " ");
270
271         fprintf(f, "%s", opcode->Name);
272
273         switch(inst->U.I.SaturateMode) {
274         case RC_SATURATE_NONE: break;
275         case RC_SATURATE_ZERO_ONE: fprintf(f, "_SAT"); break;
276         case RC_SATURATE_MINUS_PLUS_ONE: fprintf(f, "_SAT2"); break;
277         default: fprintf(f, "_BAD_SAT"); break;
278         }
279
280         if (opcode->HasDstReg) {
281                 fprintf(f, " ");
282                 rc_print_dst_register(f, inst->U.I.DstReg);
283                 print_omod_op(f, inst->U.I.Omod);
284                 if (opcode->NumSrcRegs)
285                         fprintf(f, ",");
286         }
287
288         for(reg = 0; reg < opcode->NumSrcRegs; ++reg) {
289                 if (reg > 0)
290                         fprintf(f, ",");
291                 fprintf(f, " ");
292                 rc_print_src_register(f, inst, inst->U.I.SrcReg[reg]);
293         }
294
295         if (opcode->HasTexture) {
296                 fprintf(f, ", %s%s[%u]%s%s",
297                         textarget_to_string(inst->U.I.TexSrcTarget),
298                         inst->U.I.TexShadow ? "SHADOW" : "",
299                         inst->U.I.TexSrcUnit,
300                         inst->U.I.TexSemWait ? " SEM_WAIT" : "",
301                         inst->U.I.TexSemAcquire ? " SEM_ACQUIRE" : "");
302         }
303
304         fprintf(f, ";");
305
306         if (inst->U.I.WriteALUResult) {
307                 fprintf(f, " [aluresult = (");
308                 rc_print_comparefunc(f,
309                         (inst->U.I.WriteALUResult == RC_ALURESULT_X) ? "x" : "w",
310                         inst->U.I.ALUResultCompare, "0");
311                 fprintf(f, ")]");
312         }
313
314         fprintf(f, "\n");
315 }
316
317 static void rc_print_pair_instruction(FILE * f, struct rc_instruction * fullinst, unsigned *branch_depth)
318 {
319         struct rc_pair_instruction * inst = &fullinst->U.P;
320         int printedsrc = 0;
321         unsigned spaces = update_branch_depth(inst->RGB.Opcode != RC_OPCODE_NOP ?
322                                               inst->RGB.Opcode : inst->Alpha.Opcode, branch_depth);
323
324         for (unsigned i = 0; i < spaces; i++)
325                 fprintf(f, " ");
326
327         for(unsigned int src = 0; src < 3; ++src) {
328                 if (inst->RGB.Src[src].Used) {
329                         if (printedsrc)
330                                 fprintf(f, ", ");
331                         fprintf(f, "src%i.xyz = ", src);
332                         rc_print_register(f, inst->RGB.Src[src].File, inst->RGB.Src[src].Index, 0);
333                         printedsrc = 1;
334                 }
335                 if (inst->Alpha.Src[src].Used) {
336                         if (printedsrc)
337                                 fprintf(f, ", ");
338                         fprintf(f, "src%i.w = ", src);
339                         rc_print_register(f, inst->Alpha.Src[src].File, inst->Alpha.Src[src].Index, 0);
340                         printedsrc = 1;
341                 }
342         }
343         if(inst->RGB.Src[RC_PAIR_PRESUB_SRC].Used) {
344                 fprintf(f, ", srcp.xyz = %s",
345                         presubtract_op_to_string(
346                                         inst->RGB.Src[RC_PAIR_PRESUB_SRC].Index));
347         }
348         if(inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Used) {
349                 fprintf(f, ", srcp.w = %s",
350                         presubtract_op_to_string(
351                                         inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Index));
352         }
353         if (inst->SemWait) {
354                 fprintf(f, " SEM_WAIT");
355         }
356         fprintf(f, "\n");
357
358         if (inst->RGB.Opcode != RC_OPCODE_NOP) {
359                 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->RGB.Opcode);
360
361                 for (unsigned i = 0; i < spaces; i++)
362                         fprintf(f, " ");
363
364                 fprintf(f, "     %s%s", opcode->Name, inst->RGB.Saturate ? "_SAT" : "");
365                 if (inst->RGB.WriteMask)
366                         fprintf(f, " temp[%i].%s%s%s", inst->RGB.DestIndex,
367                                 (inst->RGB.WriteMask & 1) ? "x" : "",
368                                 (inst->RGB.WriteMask & 2) ? "y" : "",
369                                 (inst->RGB.WriteMask & 4) ? "z" : "");
370                 if (inst->RGB.OutputWriteMask)
371                         fprintf(f, " color[%i].%s%s%s", inst->RGB.Target,
372                                 (inst->RGB.OutputWriteMask & 1) ? "x" : "",
373                                 (inst->RGB.OutputWriteMask & 2) ? "y" : "",
374                                 (inst->RGB.OutputWriteMask & 4) ? "z" : "");
375                 if (inst->WriteALUResult == RC_ALURESULT_X)
376                         fprintf(f, " aluresult");
377
378                 print_omod_op(f, inst->RGB.Omod);
379
380                 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
381                         const char* abs = inst->RGB.Arg[arg].Abs ? "|" : "";
382                         const char* neg = inst->RGB.Arg[arg].Negate ? "-" : "";
383                         fprintf(f, ", %s%ssrc", neg, abs);
384                         if(inst->RGB.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
385                                 fprintf(f,"p");
386                         else
387                                 fprintf(f,"%d", inst->RGB.Arg[arg].Source);
388                         fprintf(f,".%c%c%c%s",
389                                 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 0)),
390                                 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 1)),
391                                 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 2)),
392                                 abs);
393                 }
394                 fprintf(f, "\n");
395         }
396
397         if (inst->Alpha.Opcode != RC_OPCODE_NOP) {
398                 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->Alpha.Opcode);
399
400                 for (unsigned i = 0; i < spaces; i++)
401                         fprintf(f, " ");
402
403                 fprintf(f, "     %s%s", opcode->Name, inst->Alpha.Saturate ? "_SAT" : "");
404                 if (inst->Alpha.WriteMask)
405                         fprintf(f, " temp[%i].w", inst->Alpha.DestIndex);
406                 if (inst->Alpha.OutputWriteMask)
407                         fprintf(f, " color[%i].w", inst->Alpha.Target);
408                 if (inst->Alpha.DepthWriteMask)
409                         fprintf(f, " depth.w");
410                 if (inst->WriteALUResult == RC_ALURESULT_W)
411                         fprintf(f, " aluresult");
412
413                 print_omod_op(f, inst->Alpha.Omod);
414
415                 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
416                         const char* abs = inst->Alpha.Arg[arg].Abs ? "|" : "";
417                         const char* neg = inst->Alpha.Arg[arg].Negate ? "-" : "";
418                         fprintf(f, ", %s%ssrc", neg, abs);
419                         if(inst->Alpha.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
420                                 fprintf(f,"p");
421                         else
422                                 fprintf(f,"%d", inst->Alpha.Arg[arg].Source);
423                         fprintf(f,".%c%s",
424                                 rc_swizzle_char(GET_SWZ(inst->Alpha.Arg[arg].Swizzle, 0)), abs);
425                 }
426                 fprintf(f, "\n");
427         }
428
429         if (inst->WriteALUResult) {
430                 for (unsigned i = 0; i < spaces; i++)
431                         fprintf(f, " ");
432
433                 fprintf(f, "      [aluresult = (");
434                 rc_print_comparefunc(f, "result", inst->ALUResultCompare, "0");
435                 fprintf(f, ")]\n");
436         }
437 }
438
439 /**
440  * Print program to stderr, default options.
441  */
442 void rc_print_program(const struct rc_program *prog)
443 {
444         unsigned int linenum = 0;
445         unsigned branch_depth = 0;
446         struct rc_instruction *inst;
447
448         fprintf(stderr, "# Radeon Compiler Program\n");
449
450         for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
451                 fprintf(stderr, "%3d: ", linenum);
452
453                 if (inst->Type == RC_INSTRUCTION_PAIR)
454                         rc_print_pair_instruction(stderr, inst, &branch_depth);
455                 else
456                         rc_print_normal_instruction(stderr, inst, &branch_depth);
457
458                 linenum++;
459         }
460 }