1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include "ppc-opcode.h"
5
6static int bits(
7    uint32_t width);
8
9static char *cond[] = { "lt", "gt", "eq", "un"};
10static char *pred[] = { "+", "-" };
11
12int
13main(
14int argc,
15char *argv[],
16char *envp[])
17{
18    int32_t i, j, x, d, p, doing_jbsr, add_pred;
19
20	add_pred = 0;
21	doing_jbsr = 0;
22	p = 0;
23	for(i = 0; *(ppc_opcodes[i].name) != '\0'; i++){
24	    if(strcmp("jbsr", ppc_opcodes[i].name) == 0)
25		continue;
26	    printf("\t%s", ppc_opcodes[i].name);
27	    if(IS_BRANCH_CONDITIONAL(ppc_opcodes[i].opcode)){
28		if(add_pred == 1){
29		    p = bits(5) & 1;
30		    printf("%s", pred[p]);
31		}
32	    }
33	    if(ppc_opcodes[i].ops[0].type == NONE)
34		printf("\n");
35	    else
36		printf("\t");
37	    d = 0;
38	    for(j = 0; j < 5 && ppc_opcodes[i].ops[j].type != NONE; j++){
39		switch(ppc_opcodes[i].ops[j].type){
40		case PCREL:
41		    if(doing_jbsr)
42			printf("L1");
43		    else
44			printf("_relitive");
45		    break;
46		case BADDR:
47		    printf("_absolute");
48		    break;
49		case D:
50		    printf("0x%04x(", bits(16));
51		    d = 1;
52		    break;
53		case DS:
54		    printf("0x%04x(", bits(14) << 2);
55		    d = 1;
56		    break;
57		case SI:
58		case UI:
59		case HI:
60		    printf("0x%04x", bits(16));
61		    break;
62		case GREG:
63		    if((strcmp("lmw", ppc_opcodes[i].name) == 0 && j == 0) ||
64		       (strcmp("lswi", ppc_opcodes[i].name) == 0 && j == 0))
65			printf("r31");
66		    else
67			printf("r%d", bits(5) );
68		    break;
69		case G0REG:
70		    printf("r%d", bits(5) | 0x1 );
71		    break;
72		case FREG:
73		    printf("f%d", bits(5) );
74		    break;
75		case SGREG:
76		    printf("sr%d", bits(4) );
77		    break;
78		case SPREG:
79		    printf("%d", bits(10) );
80		    break;
81		case BCND:
82		    printf("cr%d+%s", bits(3), cond[bits(2)] );
83		    break;
84		case CRF:
85		case CRFONLY:
86		    x = bits(3);
87		    printf("cr%ld", x == 0 ? 1 : x);
88		    break;
89		case sh:
90		    printf("%d", bits(6) );
91		    break;
92		case mb:
93		    printf("%d", bits(6) );
94		    break;
95		case NUM0:
96		case NUM:
97		    if(j == 0 &&
98		       IS_BRANCH_CONDITIONAL(ppc_opcodes[i].opcode)){
99		 	if(strcmp("bc", ppc_opcodes[i].name) == 0 ||
100		 	   strcmp("bca", ppc_opcodes[i].name) == 0 ||
101		 	   strcmp("bcl", ppc_opcodes[i].name) == 0 ||
102		 	   strcmp("bcla", ppc_opcodes[i].name) == 0 ||
103		 	   strcmp("bcctr", ppc_opcodes[i].name) == 0 ||
104		 	   strcmp("bcctrl", ppc_opcodes[i].name) == 0 ||
105		 	   strcmp("bclr", ppc_opcodes[i].name) == 0 ||
106		 	   strcmp("bclrl", ppc_opcodes[i].name) == 0 ||
107		 	   strcmp("bctr", ppc_opcodes[i].name) == 0 ||
108		 	   strcmp("bctrl", ppc_opcodes[i].name) == 0 ||
109		 	   strcmp("blr", ppc_opcodes[i].name) == 0 ||
110		 	   strcmp("blrl", ppc_opcodes[i].name) == 0){
111			   if(j == 0){
112				printf("20");
113				break;
114			   }
115			   if(j == 1){
116				printf("31");
117				break;
118			   }
119			}
120			x = bits(ppc_opcodes[i].ops[j].width);
121			if(ppc_opcodes[i].ops[2].type == PCREL)
122			    if(p == 0) /* + with negative disp */
123				x &= 0xfffffffe;
124			    else
125				x |= 1;
126			else
127			    if(p == 0) /* + with positive disp */
128				x |= 1;
129			    else
130				x &= 0xfffffffe;
131			if(x == 20)
132			    x = 0;
133			printf("%ld", x);
134		    }
135		    else
136			printf("%d", bits(ppc_opcodes[i].ops[j].width) );
137		    break;
138		case SNUM:
139		    printf("%d", bits(ppc_opcodes[i].ops[j].width - 1) );
140		    break;
141		case NONE:
142		    break;
143		case ZERO:
144		    printf("0");
145		    break;
146		case JBSR:
147		    printf("_mong_branch_stub");
148		    doing_jbsr = 1;
149		    break;
150		case FXM:
151		    printf("%d", 1 << bits(3));
152		    break;
153		case MBE:
154		    printf("%d", bits(ppc_opcodes[i].ops[j].width));
155		    break;
156		case VREG:
157		    printf("v%d", bits(5) );
158		    break;
159		default:
160		    fprintf(stderr, "Unknown parameter type\n");
161		    exit(1);
162		}
163		if(j == 4 || ppc_opcodes[i].ops[j+1].type == NONE){
164		    if(d == 1)
165			printf(")\n");
166		    else{
167			if(doing_jbsr){
168			    printf("\n");
169			    printf("L1:\n");
170			    doing_jbsr = 0;
171			}
172			else
173			    printf("\n");
174		    }
175		}
176		else{
177		    if(ppc_opcodes[i].ops[j].type != D &&
178		       ppc_opcodes[i].ops[j].type != DS)
179			printf(",");
180		}
181	    }
182	}
183	return(0);
184}
185
186static
187int
188bits(
189uint32_t width)
190{
191     static int x = 1;
192
193	x = (x + 1) & ((1 << width) - 1);
194	return(x);
195}
196