• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013 Rob Clark <robdclark@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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <assert.h>
29 
30 #include <util/u_debug.h>
31 
32 #include "disasm.h"
33 #include "instr-a3xx.h"
34 
35 static enum debug_t debug;
36 
37 #define printf debug_printf
38 
39 static const char *levels[] = {
40 		"",
41 		"\t",
42 		"\t\t",
43 		"\t\t\t",
44 		"\t\t\t\t",
45 		"\t\t\t\t\t",
46 		"\t\t\t\t\t\t",
47 		"\t\t\t\t\t\t\t",
48 		"\t\t\t\t\t\t\t\t",
49 		"\t\t\t\t\t\t\t\t\t",
50 		"x",
51 		"x",
52 		"x",
53 		"x",
54 		"x",
55 		"x",
56 };
57 
58 static const char *component = "xyzw";
59 
60 static const char *type[] = {
61 		[TYPE_F16] = "f16",
62 		[TYPE_F32] = "f32",
63 		[TYPE_U16] = "u16",
64 		[TYPE_U32] = "u32",
65 		[TYPE_S16] = "s16",
66 		[TYPE_S32] = "s32",
67 		[TYPE_U8]  = "u8",
68 		[TYPE_S8]  = "s8",
69 };
70 
print_reg(reg_t reg,bool full,bool r,bool c,bool im,bool neg,bool abs,bool addr_rel)71 static void print_reg(reg_t reg, bool full, bool r, bool c, bool im,
72 		bool neg, bool abs, bool addr_rel)
73 {
74 	const char type = c ? 'c' : 'r';
75 
76 	// XXX I prefer - and || for neg/abs, but preserving format used
77 	// by libllvm-a3xx for easy diffing..
78 
79 	if (abs && neg)
80 		printf("(absneg)");
81 	else if (neg)
82 		printf("(neg)");
83 	else if (abs)
84 		printf("(abs)");
85 
86 	if (r)
87 		printf("(r)");
88 
89 	if (im) {
90 		printf("%d", reg.iim_val);
91 	} else if (addr_rel) {
92 		/* I would just use %+d but trying to make it diff'able with
93 		 * libllvm-a3xx...
94 		 */
95 		if (reg.iim_val < 0)
96 			printf("%s%c<a0.x - %d>", full ? "" : "h", type, -reg.iim_val);
97 		else if (reg.iim_val > 0)
98 			printf("%s%c<a0.x + %d>", full ? "" : "h", type, reg.iim_val);
99 		else
100 			printf("%s%c<a0.x>", full ? "" : "h", type);
101 	} else if ((reg.num == REG_A0) && !c) {
102 		printf("a0.%c", component[reg.comp]);
103 	} else if ((reg.num == REG_P0) && !c) {
104 		printf("p0.%c", component[reg.comp]);
105 	} else {
106 		printf("%s%c%d.%c", full ? "" : "h", type, reg.num & 0x3f, component[reg.comp]);
107 	}
108 }
109 
110 
111 /* current instruction repeat flag: */
112 static unsigned repeat;
113 
print_reg_dst(reg_t reg,bool full,bool addr_rel)114 static void print_reg_dst(reg_t reg, bool full, bool addr_rel)
115 {
116 	print_reg(reg, full, false, false, false, false, false, addr_rel);
117 }
118 
print_reg_src(reg_t reg,bool full,bool r,bool c,bool im,bool neg,bool abs,bool addr_rel)119 static void print_reg_src(reg_t reg, bool full, bool r, bool c, bool im,
120 		bool neg, bool abs, bool addr_rel)
121 {
122 	print_reg(reg, full, r, c, im, neg, abs, addr_rel);
123 }
124 
125 /* TODO switch to using reginfo struct everywhere, since more readable
126  * than passing a bunch of bools to print_reg_src
127  */
128 
129 struct reginfo {
130 	reg_t reg;
131 	bool full;
132 	bool r;
133 	bool c;
134 	bool im;
135 	bool neg;
136 	bool abs;
137 	bool addr_rel;
138 };
139 
print_src(struct reginfo * info)140 static void print_src(struct reginfo *info)
141 {
142 	print_reg_src(info->reg, info->full, info->r, info->c, info->im,
143 			info->neg, info->abs, info->addr_rel);
144 }
145 
146 //static void print_dst(struct reginfo *info)
147 //{
148 //	print_reg_dst(info->reg, info->full, info->addr_rel);
149 //}
150 
print_instr_cat0(instr_t * instr)151 static void print_instr_cat0(instr_t *instr)
152 {
153 	instr_cat0_t *cat0 = &instr->cat0;
154 
155 	switch (cat0->opc) {
156 	case OPC_KILL:
157 		printf(" %sp0.%c", cat0->inv ? "!" : "",
158 				component[cat0->comp]);
159 		break;
160 	case OPC_BR:
161 		printf(" %sp0.%c, #%d", cat0->inv ? "!" : "",
162 				component[cat0->comp], cat0->a5xx.immed);
163 		break;
164 	case OPC_JUMP:
165 	case OPC_CALL:
166 		printf(" #%d", cat0->a5xx.immed);
167 		break;
168 	}
169 
170 	if ((debug & PRINT_VERBOSE) && (cat0->dummy2|cat0->dummy3|cat0->dummy4))
171 		printf("\t{0: %x,%x,%x}", cat0->dummy2, cat0->dummy3, cat0->dummy4);
172 }
173 
print_instr_cat1(instr_t * instr)174 static void print_instr_cat1(instr_t *instr)
175 {
176 	instr_cat1_t *cat1 = &instr->cat1;
177 
178 	if (cat1->ul)
179 		printf("(ul)");
180 
181 	if (cat1->src_type == cat1->dst_type) {
182 		if ((cat1->src_type == TYPE_S16) && (((reg_t)cat1->dst).num == REG_A0)) {
183 			/* special case (nmemonic?): */
184 			printf("mova");
185 		} else {
186 			printf("mov.%s%s", type[cat1->src_type], type[cat1->dst_type]);
187 		}
188 	} else {
189 		printf("cov.%s%s", type[cat1->src_type], type[cat1->dst_type]);
190 	}
191 
192 	printf(" ");
193 
194 	if (cat1->even)
195 		printf("(even)");
196 
197 	if (cat1->pos_inf)
198 		printf("(pos_infinity)");
199 
200 	print_reg_dst((reg_t)(cat1->dst), type_size(cat1->dst_type) == 32,
201 			cat1->dst_rel);
202 
203 	printf(", ");
204 
205 	/* ugg, have to special case this.. vs print_reg().. */
206 	if (cat1->src_im) {
207 		if (type_float(cat1->src_type))
208 			printf("(%f)", cat1->fim_val);
209 		else if (type_uint(cat1->src_type))
210 			printf("0x%08x", cat1->uim_val);
211 		else
212 			printf("%d", cat1->iim_val);
213 	} else if (cat1->src_rel && !cat1->src_c) {
214 		/* I would just use %+d but trying to make it diff'able with
215 		 * libllvm-a3xx...
216 		 */
217 		char type = cat1->src_rel_c ? 'c' : 'r';
218 		if (cat1->off < 0)
219 			printf("%c<a0.x - %d>", type, -cat1->off);
220 		else if (cat1->off > 0)
221 			printf("%c<a0.x + %d>", type, cat1->off);
222 		else
223 			printf("%c<a0.x>", type);
224 	} else {
225 		print_reg_src((reg_t)(cat1->src), type_size(cat1->src_type) == 32,
226 				cat1->src_r, cat1->src_c, cat1->src_im, false, false, false);
227 	}
228 
229 	if ((debug & PRINT_VERBOSE) && (cat1->must_be_0))
230 		printf("\t{1: %x}", cat1->must_be_0);
231 }
232 
print_instr_cat2(instr_t * instr)233 static void print_instr_cat2(instr_t *instr)
234 {
235 	instr_cat2_t *cat2 = &instr->cat2;
236 	static const char *cond[] = {
237 			"lt",
238 			"le",
239 			"gt",
240 			"ge",
241 			"eq",
242 			"ne",
243 			"?6?",
244 	};
245 
246 	switch (_OPC(2, cat2->opc)) {
247 	case OPC_CMPS_F:
248 	case OPC_CMPS_U:
249 	case OPC_CMPS_S:
250 	case OPC_CMPV_F:
251 	case OPC_CMPV_U:
252 	case OPC_CMPV_S:
253 		printf(".%s", cond[cat2->cond]);
254 		break;
255 	}
256 
257 	printf(" ");
258 	if (cat2->ei)
259 		printf("(ei)");
260 	print_reg_dst((reg_t)(cat2->dst), cat2->full ^ cat2->dst_half, false);
261 	printf(", ");
262 
263 	if (cat2->c1.src1_c) {
264 		print_reg_src((reg_t)(cat2->c1.src1), cat2->full, cat2->src1_r,
265 				cat2->c1.src1_c, cat2->src1_im, cat2->src1_neg,
266 				cat2->src1_abs, false);
267 	} else if (cat2->rel1.src1_rel) {
268 		print_reg_src((reg_t)(cat2->rel1.src1), cat2->full, cat2->src1_r,
269 				cat2->rel1.src1_c, cat2->src1_im, cat2->src1_neg,
270 				cat2->src1_abs, cat2->rel1.src1_rel);
271 	} else {
272 		print_reg_src((reg_t)(cat2->src1), cat2->full, cat2->src1_r,
273 				false, cat2->src1_im, cat2->src1_neg,
274 				cat2->src1_abs, false);
275 	}
276 
277 	switch (_OPC(2, cat2->opc)) {
278 	case OPC_ABSNEG_F:
279 	case OPC_ABSNEG_S:
280 	case OPC_CLZ_B:
281 	case OPC_CLZ_S:
282 	case OPC_SIGN_F:
283 	case OPC_FLOOR_F:
284 	case OPC_CEIL_F:
285 	case OPC_RNDNE_F:
286 	case OPC_RNDAZ_F:
287 	case OPC_TRUNC_F:
288 	case OPC_NOT_B:
289 	case OPC_BFREV_B:
290 	case OPC_SETRM:
291 	case OPC_CBITS_B:
292 		/* these only have one src reg */
293 		break;
294 	default:
295 		printf(", ");
296 		if (cat2->c2.src2_c) {
297 			print_reg_src((reg_t)(cat2->c2.src2), cat2->full, cat2->src2_r,
298 					cat2->c2.src2_c, cat2->src2_im, cat2->src2_neg,
299 					cat2->src2_abs, false);
300 		} else if (cat2->rel2.src2_rel) {
301 			print_reg_src((reg_t)(cat2->rel2.src2), cat2->full, cat2->src2_r,
302 					cat2->rel2.src2_c, cat2->src2_im, cat2->src2_neg,
303 					cat2->src2_abs, cat2->rel2.src2_rel);
304 		} else {
305 			print_reg_src((reg_t)(cat2->src2), cat2->full, cat2->src2_r,
306 					false, cat2->src2_im, cat2->src2_neg,
307 					cat2->src2_abs, false);
308 		}
309 		break;
310 	}
311 }
312 
print_instr_cat3(instr_t * instr)313 static void print_instr_cat3(instr_t *instr)
314 {
315 	instr_cat3_t *cat3 = &instr->cat3;
316 	bool full = instr_cat3_full(cat3);
317 
318 	printf(" ");
319 	print_reg_dst((reg_t)(cat3->dst), full ^ cat3->dst_half, false);
320 	printf(", ");
321 	if (cat3->c1.src1_c) {
322 		print_reg_src((reg_t)(cat3->c1.src1), full,
323 				cat3->src1_r, cat3->c1.src1_c, false, cat3->src1_neg,
324 				false, false);
325 	} else if (cat3->rel1.src1_rel) {
326 		print_reg_src((reg_t)(cat3->rel1.src1), full,
327 				cat3->src1_r, cat3->rel1.src1_c, false, cat3->src1_neg,
328 				false, cat3->rel1.src1_rel);
329 	} else {
330 		print_reg_src((reg_t)(cat3->src1), full,
331 				cat3->src1_r, false, false, cat3->src1_neg,
332 				false, false);
333 	}
334 	printf(", ");
335 	print_reg_src((reg_t)cat3->src2, full,
336 			cat3->src2_r, cat3->src2_c, false, cat3->src2_neg,
337 			false, false);
338 	printf(", ");
339 	if (cat3->c2.src3_c) {
340 		print_reg_src((reg_t)(cat3->c2.src3), full,
341 				cat3->src3_r, cat3->c2.src3_c, false, cat3->src3_neg,
342 				false, false);
343 	} else if (cat3->rel2.src3_rel) {
344 		print_reg_src((reg_t)(cat3->rel2.src3), full,
345 				cat3->src3_r, cat3->rel2.src3_c, false, cat3->src3_neg,
346 				false, cat3->rel2.src3_rel);
347 	} else {
348 		print_reg_src((reg_t)(cat3->src3), full,
349 				cat3->src3_r, false, false, cat3->src3_neg,
350 				false, false);
351 	}
352 }
353 
print_instr_cat4(instr_t * instr)354 static void print_instr_cat4(instr_t *instr)
355 {
356 	instr_cat4_t *cat4 = &instr->cat4;
357 
358 	printf(" ");
359 	print_reg_dst((reg_t)(cat4->dst), cat4->full ^ cat4->dst_half, false);
360 	printf(", ");
361 
362 	if (cat4->c.src_c) {
363 		print_reg_src((reg_t)(cat4->c.src), cat4->full,
364 				cat4->src_r, cat4->c.src_c, cat4->src_im,
365 				cat4->src_neg, cat4->src_abs, false);
366 	} else if (cat4->rel.src_rel) {
367 		print_reg_src((reg_t)(cat4->rel.src), cat4->full,
368 				cat4->src_r, cat4->rel.src_c, cat4->src_im,
369 				cat4->src_neg, cat4->src_abs, cat4->rel.src_rel);
370 	} else {
371 		print_reg_src((reg_t)(cat4->src), cat4->full,
372 				cat4->src_r, false, cat4->src_im,
373 				cat4->src_neg, cat4->src_abs, false);
374 	}
375 
376 	if ((debug & PRINT_VERBOSE) && (cat4->dummy1|cat4->dummy2))
377 		printf("\t{4: %x,%x}", cat4->dummy1, cat4->dummy2);
378 }
379 
print_instr_cat5(instr_t * instr)380 static void print_instr_cat5(instr_t *instr)
381 {
382 	static const struct {
383 		bool src1, src2, samp, tex;
384 	} info[0x1f] = {
385 			[opc_op(OPC_ISAM)]     = { true,  false, true,  true,  },
386 			[opc_op(OPC_ISAML)]    = { true,  true,  true,  true,  },
387 			[opc_op(OPC_ISAMM)]    = { true,  false, true,  true,  },
388 			[opc_op(OPC_SAM)]      = { true,  false, true,  true,  },
389 			[opc_op(OPC_SAMB)]     = { true,  true,  true,  true,  },
390 			[opc_op(OPC_SAML)]     = { true,  true,  true,  true,  },
391 			[opc_op(OPC_SAMGQ)]    = { true,  false, true,  true,  },
392 			[opc_op(OPC_GETLOD)]   = { true,  false, true,  true,  },
393 			[opc_op(OPC_CONV)]     = { true,  true,  true,  true,  },
394 			[opc_op(OPC_CONVM)]    = { true,  true,  true,  true,  },
395 			[opc_op(OPC_GETSIZE)]  = { true,  false, false, true,  },
396 			[opc_op(OPC_GETBUF)]   = { false, false, false, true,  },
397 			[opc_op(OPC_GETPOS)]   = { true,  false, false, true,  },
398 			[opc_op(OPC_GETINFO)]  = { false, false, false, true,  },
399 			[opc_op(OPC_DSX)]      = { true,  false, false, false, },
400 			[opc_op(OPC_DSY)]      = { true,  false, false, false, },
401 			[opc_op(OPC_GATHER4R)] = { true,  false, true,  true,  },
402 			[opc_op(OPC_GATHER4G)] = { true,  false, true,  true,  },
403 			[opc_op(OPC_GATHER4B)] = { true,  false, true,  true,  },
404 			[opc_op(OPC_GATHER4A)] = { true,  false, true,  true,  },
405 			[opc_op(OPC_SAMGP0)]   = { true,  false, true,  true,  },
406 			[opc_op(OPC_SAMGP1)]   = { true,  false, true,  true,  },
407 			[opc_op(OPC_SAMGP2)]   = { true,  false, true,  true,  },
408 			[opc_op(OPC_SAMGP3)]   = { true,  false, true,  true,  },
409 			[opc_op(OPC_DSXPP_1)]  = { true,  false, false, false, },
410 			[opc_op(OPC_DSYPP_1)]  = { true,  false, false, false, },
411 			[opc_op(OPC_RGETPOS)]  = { false, false, false, false, },
412 			[opc_op(OPC_RGETINFO)] = { false, false, false, false, },
413 	};
414 	instr_cat5_t *cat5 = &instr->cat5;
415 	int i;
416 
417 	if (cat5->is_3d)   printf(".3d");
418 	if (cat5->is_a)    printf(".a");
419 	if (cat5->is_o)    printf(".o");
420 	if (cat5->is_p)    printf(".p");
421 	if (cat5->is_s)    printf(".s");
422 	if (cat5->is_s2en) printf(".s2en");
423 
424 	printf(" ");
425 
426 	switch (_OPC(5, cat5->opc)) {
427 	case OPC_DSXPP_1:
428 	case OPC_DSYPP_1:
429 		break;
430 	default:
431 		printf("(%s)", type[cat5->type]);
432 		break;
433 	}
434 
435 	printf("(");
436 	for (i = 0; i < 4; i++)
437 		if (cat5->wrmask & (1 << i))
438 			printf("%c", "xyzw"[i]);
439 	printf(")");
440 
441 	print_reg_dst((reg_t)(cat5->dst), type_size(cat5->type) == 32, false);
442 
443 	if (info[cat5->opc].src1) {
444 		printf(", ");
445 		print_reg_src((reg_t)(cat5->src1), cat5->full, false, false, false,
446 				false, false, false);
447 	}
448 
449 	if (cat5->is_s2en) {
450 		printf(", ");
451 		print_reg_src((reg_t)(cat5->s2en.src2), cat5->full, false, false, false,
452 				false, false, false);
453 		printf(", ");
454 		print_reg_src((reg_t)(cat5->s2en.src3), false, false, false, false,
455 				false, false, false);
456 	} else {
457 		if (cat5->is_o || info[cat5->opc].src2) {
458 			printf(", ");
459 			print_reg_src((reg_t)(cat5->norm.src2), cat5->full,
460 					false, false, false, false, false, false);
461 		}
462 		if (info[cat5->opc].samp)
463 			printf(", s#%d", cat5->norm.samp);
464 		if (info[cat5->opc].tex)
465 			printf(", t#%d", cat5->norm.tex);
466 	}
467 
468 	if (debug & PRINT_VERBOSE) {
469 		if (cat5->is_s2en) {
470 			if ((debug & PRINT_VERBOSE) && (cat5->s2en.dummy1|cat5->s2en.dummy2|cat5->dummy2))
471 				printf("\t{5: %x,%x,%x}", cat5->s2en.dummy1, cat5->s2en.dummy2, cat5->dummy2);
472 		} else {
473 			if ((debug & PRINT_VERBOSE) && (cat5->norm.dummy1|cat5->dummy2))
474 				printf("\t{5: %x,%x}", cat5->norm.dummy1, cat5->dummy2);
475 		}
476 	}
477 }
478 
print_instr_cat6(instr_t * instr)479 static void print_instr_cat6(instr_t *instr)
480 {
481 	instr_cat6_t *cat6 = &instr->cat6;
482 	char sd = 0, ss = 0;  /* dst/src address space */
483 	bool nodst = false;
484 	struct reginfo dst, src1, src2;
485 	int src1off = 0, dstoff = 0;
486 
487 	memset(&dst, 0, sizeof(dst));
488 	memset(&src1, 0, sizeof(src1));
489 	memset(&src2, 0, sizeof(src2));
490 
491 	switch (_OPC(6, cat6->opc)) {
492 	case OPC_RESINFO:
493 	case OPC_RESFMT:
494 		dst.full  = type_size(cat6->type) == 32;
495 		src1.full = type_size(cat6->type) == 32;
496 		src2.full = type_size(cat6->type) == 32;
497 		break;
498 	case OPC_L2G:
499 	case OPC_G2L:
500 		dst.full = true;
501 		src1.full = true;
502 		src2.full = true;
503 		break;
504 	case OPC_STG:
505 	case OPC_STL:
506 	case OPC_STP:
507 	case OPC_STI:
508 	case OPC_STLW:
509 	case OPC_STIB:
510 		dst.full  = true;
511 		src1.full = type_size(cat6->type) == 32;
512 		src2.full = type_size(cat6->type) == 32;
513 		break;
514 	default:
515 		dst.full  = type_size(cat6->type) == 32;
516 		src1.full = true;
517 		src2.full = true;
518 		break;
519 	}
520 
521 	switch (_OPC(6, cat6->opc)) {
522 	case OPC_PREFETCH:
523 		break;
524 	case OPC_RESINFO:
525 		printf(".%dd", cat6->ldgb.d + 1);
526 		break;
527 	case OPC_LDGB:
528 		printf(".%s", cat6->ldgb.typed ? "typed" : "untyped");
529 		printf(".%dd", cat6->ldgb.d + 1);
530 		printf(".%s", type[cat6->type]);
531 		printf(".%d", cat6->ldgb.type_size + 1);
532 		break;
533 	case OPC_STGB:
534 	case OPC_STIB:
535 		printf(".%s", cat6->stgb.typed ? "typed" : "untyped");
536 		printf(".%dd", cat6->stgb.d + 1);
537 		printf(".%s", type[cat6->type]);
538 		printf(".%d", cat6->stgb.type_size + 1);
539 		break;
540 	case OPC_ATOMIC_ADD:
541 	case OPC_ATOMIC_SUB:
542 	case OPC_ATOMIC_XCHG:
543 	case OPC_ATOMIC_INC:
544 	case OPC_ATOMIC_DEC:
545 	case OPC_ATOMIC_CMPXCHG:
546 	case OPC_ATOMIC_MIN:
547 	case OPC_ATOMIC_MAX:
548 	case OPC_ATOMIC_AND:
549 	case OPC_ATOMIC_OR:
550 	case OPC_ATOMIC_XOR:
551 		ss = cat6->g ? 'g' : 'l';
552 		printf(".%s", cat6->ldgb.typed ? "typed" : "untyped");
553 		printf(".%dd", cat6->ldgb.d + 1);
554 		printf(".%s", type[cat6->type]);
555 		printf(".%d", cat6->ldgb.type_size + 1);
556 		printf(".%c", ss);
557 		break;
558 	default:
559 		dst.im = cat6->g && !cat6->dst_off;
560 		printf(".%s", type[cat6->type]);
561 		break;
562 	}
563 	printf(" ");
564 
565 	switch (_OPC(6, cat6->opc)) {
566 	case OPC_STG:
567 		sd = 'g';
568 		break;
569 	case OPC_STP:
570 		sd = 'p';
571 		break;
572 	case OPC_STL:
573 	case OPC_STLW:
574 		sd = 'l';
575 		break;
576 
577 	case OPC_LDG:
578 	case OPC_LDC:
579 		ss = 'g';
580 		break;
581 	case OPC_LDP:
582 		ss = 'p';
583 		break;
584 	case OPC_LDL:
585 	case OPC_LDLW:
586 	case OPC_LDLV:
587 		ss = 'l';
588 		break;
589 
590 	case OPC_L2G:
591 		ss = 'l';
592 		sd = 'g';
593 		break;
594 
595 	case OPC_G2L:
596 		ss = 'g';
597 		sd = 'l';
598 		break;
599 
600 	case OPC_PREFETCH:
601 		ss = 'g';
602 		nodst = true;
603 		break;
604 
605 	case OPC_STI:
606 		dst.full = false;  // XXX or inverts??
607 		break;
608 	}
609 
610 	if ((_OPC(6, cat6->opc) == OPC_STGB) || (_OPC(6, cat6->opc) == OPC_STIB)) {
611 		struct reginfo src3;
612 
613 		memset(&src3, 0, sizeof(src3));
614 
615 		src1.reg = (reg_t)(cat6->stgb.src1);
616 		src2.reg = (reg_t)(cat6->stgb.src2);
617 		src2.im  = cat6->stgb.src2_im;
618 		src3.reg = (reg_t)(cat6->stgb.src3);
619 		src3.im  = cat6->stgb.src3_im;
620 		src3.full = true;
621 
622 		printf("g[%u], ", cat6->stgb.dst_ssbo);
623 		print_src(&src1);
624 		printf(", ");
625 		print_src(&src2);
626 		printf(", ");
627 		print_src(&src3);
628 
629 		if (debug & PRINT_VERBOSE)
630 			printf(" (pad0=%x, pad3=%x)", cat6->stgb.pad0, cat6->stgb.pad3);
631 
632 		return;
633 	}
634 
635 	if (is_atomic(_OPC(6, cat6->opc))) {
636 
637 		src1.reg = (reg_t)(cat6->ldgb.src1);
638 		src1.im  = cat6->ldgb.src1_im;
639 		src2.reg = (reg_t)(cat6->ldgb.src2);
640 		src2.im  = cat6->ldgb.src2_im;
641 		dst.reg  = (reg_t)(cat6->ldgb.dst);
642 
643 		print_src(&dst);
644 		printf(", ");
645 		if (ss == 'g') {
646 			struct reginfo src3;
647 			memset(&src3, 0, sizeof(src3));
648 
649 			src3.reg = (reg_t)(cat6->ldgb.src3);
650 			src3.full = true;
651 
652 			/* For images, the ".typed" variant is used and src2 is
653 			 * the ivecN coordinates, ie ivec2 for 2d.
654 			 *
655 			 * For SSBOs, the ".untyped" variant is used and src2 is
656 			 * a simple dword offset..  src3 appears to be
657 			 * uvec2(offset * 4, 0).  Not sure the point of that.
658 			 */
659 
660 			printf("g[%u], ", cat6->ldgb.src_ssbo);
661 			print_src(&src1);  /* value */
662 			printf(", ");
663 			print_src(&src2);  /* offset/coords */
664 			printf(", ");
665 			print_src(&src3);  /* 64b byte offset.. */
666 
667 			if (debug & PRINT_VERBOSE) {
668 				printf(" (pad0=%x, pad3=%x, mustbe0=%x)", cat6->ldgb.pad0,
669 						cat6->ldgb.pad3, cat6->ldgb.mustbe0);
670 			}
671 		} else { /* ss == 'l' */
672 			printf("l[");
673 			print_src(&src1);  /* simple byte offset */
674 			printf("], ");
675 			print_src(&src2);  /* value */
676 
677 			if (debug & PRINT_VERBOSE) {
678 				printf(" (src3=%x, pad0=%x, pad3=%x, mustbe0=%x)",
679 						cat6->ldgb.src3, cat6->ldgb.pad0,
680 						cat6->ldgb.pad3, cat6->ldgb.mustbe0);
681 			}
682 		}
683 
684 		return;
685 	} else if (_OPC(6, cat6->opc) == OPC_RESINFO) {
686 		dst.reg  = (reg_t)(cat6->ldgb.dst);
687 
688 		print_src(&dst);
689 		printf(", ");
690 		printf("g[%u]", cat6->ldgb.src_ssbo);
691 
692 		return;
693 	} else if (_OPC(6, cat6->opc) == OPC_LDGB) {
694 
695 		src1.reg = (reg_t)(cat6->ldgb.src1);
696 		src1.im  = cat6->ldgb.src1_im;
697 		src2.reg = (reg_t)(cat6->ldgb.src2);
698 		src2.im  = cat6->ldgb.src2_im;
699 		dst.reg  = (reg_t)(cat6->ldgb.dst);
700 
701 		print_src(&dst);
702 		printf(", ");
703 		printf("g[%u], ", cat6->ldgb.src_ssbo);
704 		print_src(&src1);
705 		printf(", ");
706 		print_src(&src2);
707 
708 		if (debug & PRINT_VERBOSE)
709 			printf(" (pad0=%x, pad3=%x, mustbe0=%x)", cat6->ldgb.pad0, cat6->ldgb.pad3, cat6->ldgb.mustbe0);
710 
711 		return;
712 	}
713 	if (cat6->dst_off) {
714 		dst.reg = (reg_t)(cat6->c.dst);
715 		dstoff  = cat6->c.off;
716 	} else {
717 		dst.reg = (reg_t)(cat6->d.dst);
718 	}
719 
720 	if (cat6->src_off) {
721 		src1.reg = (reg_t)(cat6->a.src1);
722 		src1.im  = cat6->a.src1_im;
723 		src2.reg = (reg_t)(cat6->a.src2);
724 		src2.im  = cat6->a.src2_im;
725 		src1off  = cat6->a.off;
726 	} else {
727 		src1.reg = (reg_t)(cat6->b.src1);
728 		src1.im  = cat6->b.src1_im;
729 		src2.reg = (reg_t)(cat6->b.src2);
730 		src2.im  = cat6->b.src2_im;
731 	}
732 
733 	if (!nodst) {
734 		if (sd)
735 			printf("%c[", sd);
736 		/* note: dst might actually be a src (ie. address to store to) */
737 		print_src(&dst);
738 		if (dstoff)
739 			printf("%+d", dstoff);
740 		if (sd)
741 			printf("]");
742 		printf(", ");
743 	}
744 
745 	if (ss)
746 		printf("%c[", ss);
747 
748 	/* can have a larger than normal immed, so hack: */
749 	if (src1.im) {
750 		printf("%u", src1.reg.dummy13);
751 	} else {
752 		print_src(&src1);
753 	}
754 
755 	if (src1off)
756 		printf("%+d", src1off);
757 	if (ss)
758 		printf("]");
759 
760 	switch (_OPC(6, cat6->opc)) {
761 	case OPC_RESINFO:
762 	case OPC_RESFMT:
763 		break;
764 	default:
765 		printf(", ");
766 		print_src(&src2);
767 		break;
768 	}
769 }
770 
print_instr_cat7(instr_t * instr)771 static void print_instr_cat7(instr_t *instr)
772 {
773 	instr_cat7_t *cat7 = &instr->cat7;
774 
775 	if (cat7->g)
776 		printf(".g");
777 	if (cat7->l)
778 		printf(".l");
779 
780 	if (_OPC(7, cat7->opc) == OPC_FENCE) {
781 		if (cat7->r)
782 			printf(".r");
783 		if (cat7->w)
784 			printf(".w");
785 	}
786 }
787 
788 /* size of largest OPC field of all the instruction categories: */
789 #define NOPC_BITS 6
790 
791 static const struct opc_info {
792 	uint16_t cat;
793 	uint16_t opc;
794 	const char *name;
795 	void (*print)(instr_t *instr);
796 } opcs[1 << (3+NOPC_BITS)] = {
797 #define OPC(cat, opc, name) [(opc)] = { (cat), (opc), #name, print_instr_cat##cat }
798 	/* category 0: */
799 	OPC(0, OPC_NOP,          nop),
800 	OPC(0, OPC_BR,           br),
801 	OPC(0, OPC_JUMP,         jump),
802 	OPC(0, OPC_CALL,         call),
803 	OPC(0, OPC_RET,          ret),
804 	OPC(0, OPC_KILL,         kill),
805 	OPC(0, OPC_END,          end),
806 	OPC(0, OPC_EMIT,         emit),
807 	OPC(0, OPC_CUT,          cut),
808 	OPC(0, OPC_CHMASK,       chmask),
809 	OPC(0, OPC_CHSH,         chsh),
810 	OPC(0, OPC_FLOW_REV,     flow_rev),
811 
812 	/* category 1: */
813 	OPC(1, OPC_MOV, ),
814 
815 	/* category 2: */
816 	OPC(2, OPC_ADD_F,        add.f),
817 	OPC(2, OPC_MIN_F,        min.f),
818 	OPC(2, OPC_MAX_F,        max.f),
819 	OPC(2, OPC_MUL_F,        mul.f),
820 	OPC(2, OPC_SIGN_F,       sign.f),
821 	OPC(2, OPC_CMPS_F,       cmps.f),
822 	OPC(2, OPC_ABSNEG_F,     absneg.f),
823 	OPC(2, OPC_CMPV_F,       cmpv.f),
824 	OPC(2, OPC_FLOOR_F,      floor.f),
825 	OPC(2, OPC_CEIL_F,       ceil.f),
826 	OPC(2, OPC_RNDNE_F,      rndne.f),
827 	OPC(2, OPC_RNDAZ_F,      rndaz.f),
828 	OPC(2, OPC_TRUNC_F,      trunc.f),
829 	OPC(2, OPC_ADD_U,        add.u),
830 	OPC(2, OPC_ADD_S,        add.s),
831 	OPC(2, OPC_SUB_U,        sub.u),
832 	OPC(2, OPC_SUB_S,        sub.s),
833 	OPC(2, OPC_CMPS_U,       cmps.u),
834 	OPC(2, OPC_CMPS_S,       cmps.s),
835 	OPC(2, OPC_MIN_U,        min.u),
836 	OPC(2, OPC_MIN_S,        min.s),
837 	OPC(2, OPC_MAX_U,        max.u),
838 	OPC(2, OPC_MAX_S,        max.s),
839 	OPC(2, OPC_ABSNEG_S,     absneg.s),
840 	OPC(2, OPC_AND_B,        and.b),
841 	OPC(2, OPC_OR_B,         or.b),
842 	OPC(2, OPC_NOT_B,        not.b),
843 	OPC(2, OPC_XOR_B,        xor.b),
844 	OPC(2, OPC_CMPV_U,       cmpv.u),
845 	OPC(2, OPC_CMPV_S,       cmpv.s),
846 	OPC(2, OPC_MUL_U,        mul.u),
847 	OPC(2, OPC_MUL_S,        mul.s),
848 	OPC(2, OPC_MULL_U,       mull.u),
849 	OPC(2, OPC_BFREV_B,      bfrev.b),
850 	OPC(2, OPC_CLZ_S,        clz.s),
851 	OPC(2, OPC_CLZ_B,        clz.b),
852 	OPC(2, OPC_SHL_B,        shl.b),
853 	OPC(2, OPC_SHR_B,        shr.b),
854 	OPC(2, OPC_ASHR_B,       ashr.b),
855 	OPC(2, OPC_BARY_F,       bary.f),
856 	OPC(2, OPC_MGEN_B,       mgen.b),
857 	OPC(2, OPC_GETBIT_B,     getbit.b),
858 	OPC(2, OPC_SETRM,        setrm),
859 	OPC(2, OPC_CBITS_B,      cbits.b),
860 	OPC(2, OPC_SHB,          shb),
861 	OPC(2, OPC_MSAD,         msad),
862 
863 	/* category 3: */
864 	OPC(3, OPC_MAD_U16,      mad.u16),
865 	OPC(3, OPC_MADSH_U16,    madsh.u16),
866 	OPC(3, OPC_MAD_S16,      mad.s16),
867 	OPC(3, OPC_MADSH_M16,    madsh.m16),
868 	OPC(3, OPC_MAD_U24,      mad.u24),
869 	OPC(3, OPC_MAD_S24,      mad.s24),
870 	OPC(3, OPC_MAD_F16,      mad.f16),
871 	OPC(3, OPC_MAD_F32,      mad.f32),
872 	OPC(3, OPC_SEL_B16,      sel.b16),
873 	OPC(3, OPC_SEL_B32,      sel.b32),
874 	OPC(3, OPC_SEL_S16,      sel.s16),
875 	OPC(3, OPC_SEL_S32,      sel.s32),
876 	OPC(3, OPC_SEL_F16,      sel.f16),
877 	OPC(3, OPC_SEL_F32,      sel.f32),
878 	OPC(3, OPC_SAD_S16,      sad.s16),
879 	OPC(3, OPC_SAD_S32,      sad.s32),
880 
881 	/* category 4: */
882 	OPC(4, OPC_RCP,          rcp),
883 	OPC(4, OPC_RSQ,          rsq),
884 	OPC(4, OPC_LOG2,         log2),
885 	OPC(4, OPC_EXP2,         exp2),
886 	OPC(4, OPC_SIN,          sin),
887 	OPC(4, OPC_COS,          cos),
888 	OPC(4, OPC_SQRT,         sqrt),
889 
890 	/* category 5: */
891 	OPC(5, OPC_ISAM,         isam),
892 	OPC(5, OPC_ISAML,        isaml),
893 	OPC(5, OPC_ISAMM,        isamm),
894 	OPC(5, OPC_SAM,          sam),
895 	OPC(5, OPC_SAMB,         samb),
896 	OPC(5, OPC_SAML,         saml),
897 	OPC(5, OPC_SAMGQ,        samgq),
898 	OPC(5, OPC_GETLOD,       getlod),
899 	OPC(5, OPC_CONV,         conv),
900 	OPC(5, OPC_CONVM,        convm),
901 	OPC(5, OPC_GETSIZE,      getsize),
902 	OPC(5, OPC_GETBUF,       getbuf),
903 	OPC(5, OPC_GETPOS,       getpos),
904 	OPC(5, OPC_GETINFO,      getinfo),
905 	OPC(5, OPC_DSX,          dsx),
906 	OPC(5, OPC_DSY,          dsy),
907 	OPC(5, OPC_GATHER4R,     gather4r),
908 	OPC(5, OPC_GATHER4G,     gather4g),
909 	OPC(5, OPC_GATHER4B,     gather4b),
910 	OPC(5, OPC_GATHER4A,     gather4a),
911 	OPC(5, OPC_SAMGP0,       samgp0),
912 	OPC(5, OPC_SAMGP1,       samgp1),
913 	OPC(5, OPC_SAMGP2,       samgp2),
914 	OPC(5, OPC_SAMGP3,       samgp3),
915 	OPC(5, OPC_DSXPP_1,      dsxpp.1),
916 	OPC(5, OPC_DSYPP_1,      dsypp.1),
917 	OPC(5, OPC_RGETPOS,      rgetpos),
918 	OPC(5, OPC_RGETINFO,     rgetinfo),
919 
920 
921 	/* category 6: */
922 	OPC(6, OPC_LDG,          ldg),
923 	OPC(6, OPC_LDL,          ldl),
924 	OPC(6, OPC_LDP,          ldp),
925 	OPC(6, OPC_STG,          stg),
926 	OPC(6, OPC_STL,          stl),
927 	OPC(6, OPC_STP,          stp),
928 	OPC(6, OPC_STI,          sti),
929 	OPC(6, OPC_G2L,          g2l),
930 	OPC(6, OPC_L2G,          l2g),
931 	OPC(6, OPC_PREFETCH,     prefetch),
932 	OPC(6, OPC_LDLW,         ldlw),
933 	OPC(6, OPC_STLW,         stlw),
934 	OPC(6, OPC_RESFMT,       resfmt),
935 	OPC(6, OPC_RESINFO,      resinfo),
936 	OPC(6, OPC_ATOMIC_ADD,     atomic.add),
937 	OPC(6, OPC_ATOMIC_SUB,     atomic.sub),
938 	OPC(6, OPC_ATOMIC_XCHG,    atomic.xchg),
939 	OPC(6, OPC_ATOMIC_INC,     atomic.inc),
940 	OPC(6, OPC_ATOMIC_DEC,     atomic.dec),
941 	OPC(6, OPC_ATOMIC_CMPXCHG, atomic.cmpxchg),
942 	OPC(6, OPC_ATOMIC_MIN,     atomic.min),
943 	OPC(6, OPC_ATOMIC_MAX,     atomic.max),
944 	OPC(6, OPC_ATOMIC_AND,     atomic.and),
945 	OPC(6, OPC_ATOMIC_OR,      atomic.or),
946 	OPC(6, OPC_ATOMIC_XOR,     atomic.xor),
947 	OPC(6, OPC_LDGB,         ldgb),
948 	OPC(6, OPC_STGB,         stgb),
949 	OPC(6, OPC_STIB,         stib),
950 	OPC(6, OPC_LDC,          ldc),
951 	OPC(6, OPC_LDLV,         ldlv),
952 
953 	OPC(7, OPC_BAR,          bar),
954 	OPC(7, OPC_FENCE,        fence),
955 
956 #undef OPC
957 };
958 
959 #define GETINFO(instr) (&(opcs[((instr)->opc_cat << NOPC_BITS) | instr_opc(instr)]))
960 
961 // XXX hack.. probably should move this table somewhere common:
962 #include "ir3.h"
ir3_instr_name(struct ir3_instruction * instr)963 const char *ir3_instr_name(struct ir3_instruction *instr)
964 {
965 	if (opc_cat(instr->opc) == -1) return "??meta??";
966 	return opcs[instr->opc].name;
967 }
968 
print_instr(uint32_t * dwords,int level,int n)969 static void print_instr(uint32_t *dwords, int level, int n)
970 {
971 	instr_t *instr = (instr_t *)dwords;
972 	uint32_t opc = instr_opc(instr);
973 	const char *name;
974 
975 	if (debug & PRINT_VERBOSE)
976 		printf("%s%04d[%08xx_%08xx] ", levels[level], n, dwords[1], dwords[0]);
977 
978 	/* NOTE: order flags are printed is a bit fugly.. but for now I
979 	 * try to match the order in llvm-a3xx disassembler for easy
980 	 * diff'ing..
981 	 */
982 
983 	if (instr->sync)
984 		printf("(sy)");
985 	if (instr->ss && ((instr->opc_cat <= 4) || (instr->opc_cat == 7)))
986 		printf("(ss)");
987 	if (instr->jmp_tgt)
988 		printf("(jp)");
989 	if (instr->repeat && (instr->opc_cat <= 4)) {
990 		printf("(rpt%d)", instr->repeat);
991 		repeat = instr->repeat;
992 	} else {
993 		repeat = 0;
994 	}
995 	if (instr->ul && ((2 <= instr->opc_cat) && (instr->opc_cat <= 4)))
996 		printf("(ul)");
997 
998 	name = GETINFO(instr)->name;
999 
1000 	if (name) {
1001 		printf("%s", name);
1002 		GETINFO(instr)->print(instr);
1003 	} else {
1004 		printf("unknown(%d,%d)", instr->opc_cat, opc);
1005 	}
1006 
1007 	printf("\n");
1008 }
1009 
disasm_a3xx(uint32_t * dwords,int sizedwords,int level,enum shader_t type)1010 int disasm_a3xx(uint32_t *dwords, int sizedwords, int level, enum shader_t type)
1011 {
1012 	int i;
1013 
1014 	assert((sizedwords % 2) == 0);
1015 
1016 	for (i = 0; i < sizedwords; i += 2)
1017 		print_instr(&dwords[i], level, i/2);
1018 
1019 	return 0;
1020 }
1021