1 /*
2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "radeon_compiler_util.h"
7 #include "radeon_program.h"
8
9 #include <stdio.h>
10
11 static const char *
textarget_to_string(rc_texture_target target)12 textarget_to_string(rc_texture_target target)
13 {
14 switch (target) {
15 case RC_TEXTURE_2D_ARRAY:
16 return "2D_ARRAY";
17 case RC_TEXTURE_1D_ARRAY:
18 return "1D_ARRAY";
19 case RC_TEXTURE_CUBE:
20 return "CUBE";
21 case RC_TEXTURE_3D:
22 return "3D";
23 case RC_TEXTURE_RECT:
24 return "RECT";
25 case RC_TEXTURE_2D:
26 return "2D";
27 case RC_TEXTURE_1D:
28 return "1D";
29 default:
30 return "BAD_TEXTURE_TARGET";
31 }
32 }
33
34 static const char *
presubtract_op_to_string(rc_presubtract_op op)35 presubtract_op_to_string(rc_presubtract_op op)
36 {
37 switch (op) {
38 case RC_PRESUB_NONE:
39 return "NONE";
40 case RC_PRESUB_BIAS:
41 return "(1 - 2 * src0)";
42 case RC_PRESUB_SUB:
43 return "(src1 - src0)";
44 case RC_PRESUB_ADD:
45 return "(src1 + src0)";
46 case RC_PRESUB_INV:
47 return "(1 - src0)";
48 default:
49 return "BAD_PRESUBTRACT_OP";
50 }
51 }
52
53 static void
print_omod_op(FILE * f,rc_omod_op op)54 print_omod_op(FILE *f, rc_omod_op op)
55 {
56 const char *omod_str;
57
58 switch (op) {
59 case RC_OMOD_MUL_1:
60 return;
61 case RC_OMOD_DISABLE:
62 omod_str = "(OMOD DISABLE)";
63 break;
64 case RC_OMOD_MUL_2:
65 omod_str = "* 2";
66 break;
67 case RC_OMOD_MUL_4:
68 omod_str = "* 4";
69 break;
70 case RC_OMOD_MUL_8:
71 omod_str = "* 8";
72 break;
73 case RC_OMOD_DIV_2:
74 omod_str = "/ 2";
75 break;
76 case RC_OMOD_DIV_4:
77 omod_str = "/ 4";
78 break;
79 case RC_OMOD_DIV_8:
80 omod_str = "/ 8";
81 break;
82 default:
83 return;
84 }
85 fprintf(f, " %s", omod_str);
86 }
87
88 static void
rc_print_comparefunc(FILE * f,const char * lhs,rc_compare_func func,const char * rhs)89 rc_print_comparefunc(FILE *f, const char *lhs, rc_compare_func func, const char *rhs)
90 {
91 if (func == RC_COMPARE_FUNC_NEVER) {
92 fprintf(f, "false");
93 } else if (func == RC_COMPARE_FUNC_ALWAYS) {
94 fprintf(f, "true");
95 } else {
96 const char *op;
97 switch (func) {
98 case RC_COMPARE_FUNC_LESS:
99 op = "<";
100 break;
101 case RC_COMPARE_FUNC_EQUAL:
102 op = "==";
103 break;
104 case RC_COMPARE_FUNC_LEQUAL:
105 op = "<=";
106 break;
107 case RC_COMPARE_FUNC_GREATER:
108 op = ">";
109 break;
110 case RC_COMPARE_FUNC_NOTEQUAL:
111 op = "!=";
112 break;
113 case RC_COMPARE_FUNC_GEQUAL:
114 op = ">=";
115 break;
116 default:
117 op = "???";
118 break;
119 }
120 fprintf(f, "%s %s %s", lhs, op, rhs);
121 }
122 }
123
124 static void
rc_print_inline_float(FILE * f,int index)125 rc_print_inline_float(FILE *f, int index)
126 {
127 fprintf(f, "%f (0x%x)", rc_inline_to_float(index), index);
128 }
129
130 static void
rc_print_register(FILE * f,rc_register_file file,int index,unsigned int reladdr)131 rc_print_register(FILE *f, rc_register_file file, int index, unsigned int reladdr)
132 {
133 if (file == RC_FILE_NONE) {
134 fprintf(f, "none");
135 } else if (file == RC_FILE_SPECIAL) {
136 switch (index) {
137 case RC_SPECIAL_ALU_RESULT:
138 fprintf(f, "aluresult");
139 break;
140 default:
141 fprintf(f, "special[%i]", index);
142 break;
143 }
144 } else if (file == RC_FILE_INLINE) {
145 rc_print_inline_float(f, index);
146 } else {
147 const char *filename;
148 switch (file) {
149 case RC_FILE_TEMPORARY:
150 filename = "temp";
151 break;
152 case RC_FILE_INPUT:
153 filename = "input";
154 break;
155 case RC_FILE_OUTPUT:
156 filename = "output";
157 break;
158 case RC_FILE_ADDRESS:
159 filename = "addr";
160 break;
161 case RC_FILE_CONSTANT:
162 filename = "const";
163 break;
164 default:
165 filename = "BAD FILE";
166 break;
167 }
168 fprintf(f, "%s[%i%s]", filename, index, reladdr ? " + addr[0]" : "");
169 }
170 }
171
172 static void
rc_print_mask(FILE * f,unsigned int mask)173 rc_print_mask(FILE *f, unsigned int mask)
174 {
175 if (mask & RC_MASK_X)
176 fprintf(f, "x");
177 if (mask & RC_MASK_Y)
178 fprintf(f, "y");
179 if (mask & RC_MASK_Z)
180 fprintf(f, "z");
181 if (mask & RC_MASK_W)
182 fprintf(f, "w");
183 }
184
185 static void
rc_print_dst_register(FILE * f,struct rc_dst_register dst)186 rc_print_dst_register(FILE *f, struct rc_dst_register dst)
187 {
188 rc_print_register(f, dst.File, dst.Index, 0);
189 if (dst.WriteMask != RC_MASK_XYZW) {
190 fprintf(f, ".");
191 rc_print_mask(f, dst.WriteMask);
192 }
193 }
194
195 static char
rc_swizzle_char(unsigned int swz)196 rc_swizzle_char(unsigned int swz)
197 {
198 switch (swz) {
199 case RC_SWIZZLE_X:
200 return 'x';
201 case RC_SWIZZLE_Y:
202 return 'y';
203 case RC_SWIZZLE_Z:
204 return 'z';
205 case RC_SWIZZLE_W:
206 return 'w';
207 case RC_SWIZZLE_ZERO:
208 return '0';
209 case RC_SWIZZLE_ONE:
210 return '1';
211 case RC_SWIZZLE_HALF:
212 return 'H';
213 case RC_SWIZZLE_UNUSED:
214 return '_';
215 }
216 fprintf(stderr, "bad swz: %u\n", swz);
217 return '?';
218 }
219
220 static void
rc_print_swizzle(FILE * f,unsigned int swizzle,unsigned int negate)221 rc_print_swizzle(FILE *f, unsigned int swizzle, unsigned int negate)
222 {
223 unsigned int comp;
224 for (comp = 0; comp < 4; ++comp) {
225 rc_swizzle swz = GET_SWZ(swizzle, comp);
226 if (GET_BIT(negate, comp))
227 fprintf(f, "-");
228 fprintf(f, "%c", rc_swizzle_char(swz));
229 }
230 }
231
232 static void
rc_print_presub_instruction(FILE * f,struct rc_presub_instruction inst)233 rc_print_presub_instruction(FILE *f, struct rc_presub_instruction inst)
234 {
235 fprintf(f, "(");
236 switch (inst.Opcode) {
237 case RC_PRESUB_BIAS:
238 fprintf(f, "1 - 2 * ");
239 rc_print_register(f, inst.SrcReg[0].File, inst.SrcReg[0].Index, inst.SrcReg[0].RelAddr);
240 break;
241 case RC_PRESUB_SUB:
242 rc_print_register(f, inst.SrcReg[1].File, inst.SrcReg[1].Index, inst.SrcReg[1].RelAddr);
243 fprintf(f, " - ");
244 rc_print_register(f, inst.SrcReg[0].File, inst.SrcReg[0].Index, inst.SrcReg[0].RelAddr);
245 break;
246 case RC_PRESUB_ADD:
247 rc_print_register(f, inst.SrcReg[1].File, inst.SrcReg[1].Index, inst.SrcReg[1].RelAddr);
248 fprintf(f, " + ");
249 rc_print_register(f, inst.SrcReg[0].File, inst.SrcReg[0].Index, inst.SrcReg[0].RelAddr);
250 break;
251 case RC_PRESUB_INV:
252 fprintf(f, "1 - ");
253 rc_print_register(f, inst.SrcReg[0].File, inst.SrcReg[0].Index, inst.SrcReg[0].RelAddr);
254 break;
255 default:
256 break;
257 }
258 fprintf(f, ")");
259 }
260
261 static void
rc_print_src_register(FILE * f,struct rc_instruction * inst,struct rc_src_register src)262 rc_print_src_register(FILE *f, struct rc_instruction *inst, struct rc_src_register src)
263 {
264 int trivial_negate = (src.Negate == RC_MASK_NONE || src.Negate == RC_MASK_XYZW);
265
266 if (src.Negate == RC_MASK_XYZW)
267 fprintf(f, "-");
268 if (src.Abs)
269 fprintf(f, "|");
270
271 if (src.File == RC_FILE_PRESUB)
272 rc_print_presub_instruction(f, inst->U.I.PreSub);
273 else
274 rc_print_register(f, src.File, src.Index, src.RelAddr);
275
276 if (src.Abs && !trivial_negate)
277 fprintf(f, "|");
278
279 if (src.Swizzle != RC_SWIZZLE_XYZW || !trivial_negate) {
280 fprintf(f, ".");
281 rc_print_swizzle(f, src.Swizzle, trivial_negate ? 0 : src.Negate);
282 }
283
284 if (src.Abs && trivial_negate)
285 fprintf(f, "|");
286 }
287
288 static unsigned
update_branch_depth(rc_opcode opcode,unsigned * branch_depth)289 update_branch_depth(rc_opcode opcode, unsigned *branch_depth)
290 {
291 switch (opcode) {
292 case RC_OPCODE_IF:
293 case RC_OPCODE_BGNLOOP:
294 return (*branch_depth)++ * 2;
295
296 case RC_OPCODE_ENDIF:
297 case RC_OPCODE_ENDLOOP:
298 assert(*branch_depth > 0);
299 return --(*branch_depth) * 2;
300
301 case RC_OPCODE_ELSE:
302 assert(*branch_depth > 0);
303 return (*branch_depth - 1) * 2;
304
305 default:
306 return *branch_depth * 2;
307 }
308 }
309
310 static void
rc_print_normal_instruction(FILE * f,struct rc_instruction * inst,unsigned * branch_depth)311 rc_print_normal_instruction(FILE *f, struct rc_instruction *inst, unsigned *branch_depth)
312 {
313 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
314 unsigned int reg;
315 unsigned spaces = update_branch_depth(inst->U.I.Opcode, branch_depth);
316
317 for (unsigned i = 0; i < spaces; i++)
318 fprintf(f, " ");
319
320 fprintf(f, "%s", opcode->Name);
321
322 switch (inst->U.I.SaturateMode) {
323 case RC_SATURATE_NONE:
324 break;
325 case RC_SATURATE_ZERO_ONE:
326 fprintf(f, "_SAT");
327 break;
328 case RC_SATURATE_MINUS_PLUS_ONE:
329 fprintf(f, "_SAT2");
330 break;
331 default:
332 fprintf(f, "_BAD_SAT");
333 break;
334 }
335
336 if (opcode->HasDstReg) {
337 fprintf(f, " ");
338 rc_print_dst_register(f, inst->U.I.DstReg);
339 print_omod_op(f, inst->U.I.Omod);
340 if (opcode->NumSrcRegs)
341 fprintf(f, ",");
342 }
343
344 for (reg = 0; reg < opcode->NumSrcRegs; ++reg) {
345 if (reg > 0)
346 fprintf(f, ",");
347 fprintf(f, " ");
348 rc_print_src_register(f, inst, inst->U.I.SrcReg[reg]);
349 }
350
351 if (opcode->HasTexture) {
352 fprintf(f, ", %s%s[%u]%s%s", textarget_to_string(inst->U.I.TexSrcTarget),
353 inst->U.I.TexShadow ? "SHADOW" : "", inst->U.I.TexSrcUnit,
354 inst->U.I.TexSemWait ? " SEM_WAIT" : "",
355 inst->U.I.TexSemAcquire ? " SEM_ACQUIRE" : "");
356 }
357
358 fprintf(f, ";");
359
360 if (inst->U.I.WriteALUResult) {
361 fprintf(f, " [aluresult = (");
362 rc_print_comparefunc(f, (inst->U.I.WriteALUResult == RC_ALURESULT_X) ? "x" : "w",
363 inst->U.I.ALUResultCompare, "0");
364 fprintf(f, ")]");
365 }
366
367 if (inst->U.I.DstReg.Pred == RC_PRED_SET) {
368 fprintf(f, " PRED_SET");
369 } else if (inst->U.I.DstReg.Pred == RC_PRED_INV) {
370 fprintf(f, " PRED_INV");
371 }
372
373 fprintf(f, "\n");
374 }
375
376 static void
rc_print_pair_instruction(FILE * f,struct rc_instruction * fullinst,unsigned * branch_depth)377 rc_print_pair_instruction(FILE *f, struct rc_instruction *fullinst, unsigned *branch_depth)
378 {
379 struct rc_pair_instruction *inst = &fullinst->U.P;
380 int printedsrc = 0;
381 unsigned spaces = update_branch_depth(
382 inst->RGB.Opcode != RC_OPCODE_NOP ? inst->RGB.Opcode : inst->Alpha.Opcode, branch_depth);
383
384 for (unsigned i = 0; i < spaces; i++)
385 fprintf(f, " ");
386
387 for (unsigned int src = 0; src < 3; ++src) {
388 if (inst->RGB.Src[src].Used) {
389 if (printedsrc)
390 fprintf(f, ", ");
391 fprintf(f, "src%i.xyz = ", src);
392 rc_print_register(f, inst->RGB.Src[src].File, inst->RGB.Src[src].Index, 0);
393 printedsrc = 1;
394 }
395 if (inst->Alpha.Src[src].Used) {
396 if (printedsrc)
397 fprintf(f, ", ");
398 fprintf(f, "src%i.w = ", src);
399 rc_print_register(f, inst->Alpha.Src[src].File, inst->Alpha.Src[src].Index, 0);
400 printedsrc = 1;
401 }
402 }
403 if (inst->RGB.Src[RC_PAIR_PRESUB_SRC].Used) {
404 fprintf(f, ", srcp.xyz = %s",
405 presubtract_op_to_string(inst->RGB.Src[RC_PAIR_PRESUB_SRC].Index));
406 }
407 if (inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Used) {
408 fprintf(f, ", srcp.w = %s",
409 presubtract_op_to_string(inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Index));
410 }
411 if (inst->SemWait) {
412 fprintf(f, " SEM_WAIT");
413 }
414 fprintf(f, "\n");
415
416 if (inst->RGB.Opcode != RC_OPCODE_NOP) {
417 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->RGB.Opcode);
418
419 for (unsigned i = 0; i < spaces; i++)
420 fprintf(f, " ");
421
422 fprintf(f, " %s%s", opcode->Name, inst->RGB.Saturate ? "_SAT" : "");
423 if (inst->RGB.WriteMask)
424 fprintf(f, " temp[%i].%s%s%s", inst->RGB.DestIndex, (inst->RGB.WriteMask & 1) ? "x" : "",
425 (inst->RGB.WriteMask & 2) ? "y" : "", (inst->RGB.WriteMask & 4) ? "z" : "");
426 if (inst->RGB.OutputWriteMask)
427 fprintf(
428 f, " color[%i].%s%s%s", inst->RGB.Target, (inst->RGB.OutputWriteMask & 1) ? "x" : "",
429 (inst->RGB.OutputWriteMask & 2) ? "y" : "", (inst->RGB.OutputWriteMask & 4) ? "z" : "");
430 if (inst->WriteALUResult == RC_ALURESULT_X)
431 fprintf(f, " aluresult");
432
433 print_omod_op(f, inst->RGB.Omod);
434
435 for (unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
436 const char *abs = inst->RGB.Arg[arg].Abs ? "|" : "";
437 const char *neg = inst->RGB.Arg[arg].Negate ? "-" : "";
438 fprintf(f, ", %s%ssrc", neg, abs);
439 if (inst->RGB.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
440 fprintf(f, "p");
441 else
442 fprintf(f, "%d", inst->RGB.Arg[arg].Source);
443 fprintf(f, ".%c%c%c%s", rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 0)),
444 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 1)),
445 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 2)), abs);
446 }
447 fprintf(f, "\n");
448 }
449
450 if (inst->Alpha.Opcode != RC_OPCODE_NOP) {
451 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->Alpha.Opcode);
452
453 for (unsigned i = 0; i < spaces; i++)
454 fprintf(f, " ");
455
456 fprintf(f, " %s%s", opcode->Name, inst->Alpha.Saturate ? "_SAT" : "");
457 if (inst->Alpha.WriteMask)
458 fprintf(f, " temp[%i].w", inst->Alpha.DestIndex);
459 if (inst->Alpha.OutputWriteMask)
460 fprintf(f, " color[%i].w", inst->Alpha.Target);
461 if (inst->Alpha.DepthWriteMask)
462 fprintf(f, " depth.w");
463 if (inst->WriteALUResult == RC_ALURESULT_W)
464 fprintf(f, " aluresult");
465
466 print_omod_op(f, inst->Alpha.Omod);
467
468 for (unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
469 const char *abs = inst->Alpha.Arg[arg].Abs ? "|" : "";
470 const char *neg = inst->Alpha.Arg[arg].Negate ? "-" : "";
471 fprintf(f, ", %s%ssrc", neg, abs);
472 if (inst->Alpha.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
473 fprintf(f, "p");
474 else
475 fprintf(f, "%d", inst->Alpha.Arg[arg].Source);
476 fprintf(f, ".%c%s", rc_swizzle_char(GET_SWZ(inst->Alpha.Arg[arg].Swizzle, 0)), abs);
477 }
478 fprintf(f, "\n");
479 }
480
481 if (inst->WriteALUResult) {
482 for (unsigned i = 0; i < spaces; i++)
483 fprintf(f, " ");
484
485 fprintf(f, " [aluresult = (");
486 rc_print_comparefunc(f, "result", inst->ALUResultCompare, "0");
487 fprintf(f, ")]\n");
488 }
489 }
490
491 /**
492 * Print program to stderr, default options.
493 */
494 void
rc_print_program(const struct rc_program * prog)495 rc_print_program(const struct rc_program *prog)
496 {
497 unsigned int linenum = 0;
498 unsigned branch_depth = 0;
499 struct rc_instruction *inst;
500
501 fprintf(stderr, "# Radeon Compiler Program\n");
502
503 for (inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
504 fprintf(stderr, "%3d: ", linenum);
505
506 if (inst->Type == RC_INSTRUCTION_PAIR)
507 rc_print_pair_instruction(stderr, inst, &branch_depth);
508 else
509 rc_print_normal_instruction(stderr, inst, &branch_depth);
510
511 linenum++;
512 }
513 }
514