randinst.c
5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* File: randinst.c
* Creator: rww@sgi.com
* Create Date: Wed Sep 28 13:52:37 PDT 1994
*
* This file has some functions to generate random instructions.
*
*/
#include <stdio.h>
#include <string.h>
typedef unsigned long int u32;
void rsp_DisasmInst( u32, char *, int );
u32 R32(void);
void init_random(void);
u32 rand_inst( char *buf, int pc );
#define NUM_INSTS (1024)
#define LOADS "lw", "lh", "lb"
#define STORES "sw", "sh", "sb"
#define MULTS "mult", "div"
#define BRNCHES "b", "j"
#define COPROCS "mt", "mf", "ctc", "cfc", "bc0"
#define VLOADS "lpv", "lrv", "lqv", "ltv", "luv", "lsv", "lav", "llv", "ldv", "lfv"
#define VSAVES "spv", "srv", "sqv", "stv", "suv", "ssv", "sav", "slv", "sdv", "sfv"
#define VECTOPS "v"
char *special_list[100] = { NULL };
int special_and[100];
int special_or[100];
char *skip_list[100] = { NULL };
void skip_list_read( char * );
char *branch_list[10] = { BRNCHES };
char *vect_list[10] = { VECTOPS };
int count;
main( int argc, char **argv )
{
int i, c;
u32 inst;
char buf[80];
FILE *insts;
int first_branch;
int branch;
if( argc <= 1 ) {
fprintf(stderr,"Usage: %s bad_insts_file\n", argv[0] );
exit(1);
};
skip_list_read( argv[1] );
insts = fopen("insts","w");
init_random();
/* Remember if first inst is a branch */
i = 0;
inst = rand_inst( buf, i );
first_branch = inst_check( buf, branch_list );
printf("pc%03x:\t%-30s\t# 0x%08x (%d)\n", 4*i, buf, inst, count );
fwrite( & inst, sizeof( inst ), 1, insts );
for(i=1; i<NUM_INSTS-1; i++) {
inst = rand_inst( buf, i*4 );
printf("pc%03x:\t%-30s\t# 0x%08x (%d)\n", 4*i, buf, inst, count );
fwrite( & inst, sizeof( inst ), 1, insts );
};
/* Make sure first and last aren't BOTH branches */
/* Doesn't handle case of :
0000: b
.
.
.
1022: b
1023: v
or
0000: v
0001: b
.
.
.
1023: b
*/
do {
inst = rand_inst( buf, i );
branch = inst_check( buf, branch_list );
} while( branch && first_branch );
printf("pc%03x:\t%-30s\t# 0x%08x (%d)\n", 4*i, buf, inst, count );
fwrite( & inst, sizeof( inst ), 1, insts );
fclose( insts );
}
#define SPECIAL_MASK (~(0x3f<<26))
#define VECTOR_MASK (0x12<<26)
#define VLOAD_MASK (0x32<<26)
#define VSTOR_MASK (0x3a<<26)
u32
rand_inst( char *buf, int pc )
{
u32 inst;
int branch;
int vect;
int indx;
static int prev_branch = 0;
static int prev_vect = 0;
count = 0;
do {
inst = R32();
if( random() & 1 )
inst = VECTOR_MASK | (inst & SPECIAL_MASK);
else if( random() & 1 ) {
if( random() & 1 )
inst = VLOAD_MASK | (inst & SPECIAL_MASK);
else
inst = VSTOR_MASK | (inst & SPECIAL_MASK);
} else if( random() & 0x3 )
inst &= SPECIAL_MASK;
rsp_DisasmInst( inst, buf, pc );
branch = inst_check( buf, branch_list );
count++;
} while( inst_check( buf, skip_list ) ||
(branch && prev_branch) );
if( indx = inst_check( buf, special_list ) ) {
#ifdef SPECIAL_TRACE
fprintf(stderr,"Special_inst(%d) <%s> & 0x%08x | 0x%08x => ", indx-1,
buf, special_and[indx-1], special_or[indx-1] );
#endif
inst = (inst & special_and[indx-1]) | special_or[indx-1];
rsp_DisasmInst( inst, buf, pc );
#ifdef SPECIAL_TRACE
fprintf(stderr,"<%s>\n", buf );
#endif
};
vect = inst_check( buf, vect_list );
if( prev_branch && vect && !prev_vect ) /* b+v => b */
prev_branch = 1;
else
prev_branch = branch;
prev_vect = vect;
return( inst );
}
int
inst_check( char *buf, char *skips[] )
{
int i, len;
for(i=0; skips[i] != NULL; i++) {
len = strlen( skips[i] );
if( len == 0 )
break;
if( strncmp( buf, skips[i], len ) == 0 )
return( i+1 );
};
return( 0 );
}
void
init_random( void )
{
int rseed;
rseed = time( 0 );
srandom(rseed);
}
u32
R32( void )
{
return( (random()<<1) | (random() & 1) );
}
#include <ctype.h>
#define BUFLEN (1024)
void
skip_list_read( char *fname )
{
FILE *skipfile;
char buf[BUFLEN];
char *b, *word;
int sknum;
int spnum;
int special;
skipfile = fopen( fname, "r" );
if( skipfile == NULL ) {
fprintf(stderr,"Couldn't open <%s>", fname);
perror(":");
exit(2);
};
for( sknum=0; skip_list[sknum] != NULL; sknum++ )
;
for( spnum=0; special_list[spnum] != NULL; spnum++ )
;
while( fgets( buf, BUFLEN, skipfile ) != NULL ) {
if( buf[0] == '#' ) /* Skip comment lines */
continue;
b=buf;
special = 0;
/*
Format for Special lines:
$ Inst_name And_mask Or_mask
Ex.
$ lpv 0xfffff87f 0x00000400
*/
if( buf[0] == '$' ) { /* Special */
b++;
special = 1;
};
while( *b ) {
while( *b && isspace( *b ) ) /* Skip Spaces */
b++;
if( *b == 0 )
continue;
word = b;
while( *b && !isspace( *b ) ) /* Skip non-Spaces */
b++;
if( *b ) { /* Terminate word */
*b = '\0';
b++; /* Skip to next char if not End Of String */
};
if( special == 0 ) {
skip_list[sknum++] = strdup( word );
} else {
sscanf( b, "%i %i", &special_and[spnum], &special_or[spnum] );
special_list[spnum++] = strdup( word );
*b = '\0';
};
};
};
skip_list[sknum] = NULL;
special_list[spnum] = NULL;
}