forked from riscv-non-isa/tg-nexus-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NexRv.c
297 lines (244 loc) · 9.58 KB
/
NexRv.c
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
/*
* Copyright (c) 2020 IAR Systems AB.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//****************************************************************************
// File nexrv.c - Nexus Trace dump/encode/decode reference implementation
// Code below is written in plain C-code.
// It was compiled using VisualC, GNU and IAR C/C++ compiler.
// 1. Only standard C-types are used.
// 2. Only few standard C functions used - see notes with "#include <...>"
// 3. Only non K&R C is 'for (int x' and 'int x;' between instructions.
#include <stdio.h> // For NULL, 'printf', 'fopen, ...'
#include <stdlib.h> // For 'exit'
#include <string.h> // For 'strcmp', 'strchr'
#include "NexRvInfo.h" // For 'InfoInit/InfoTerm'
FILE *fNex = NULL; // Used by NexusDump/NexusDeco/NexusEnco
extern int NexusDump(FILE *f, int disp);
extern int NexusDeco(FILE *f, int disp);
extern int NexusEnco(FILE *f, int level, int disp);
extern int ConvGnuObjdump(FILE *fObjd, FILE *fPcInfo);
extern int ConvAddInfo(FILE *fIn, FILE *fOut, FILE *fComp);
static int error(const char *err)
{
printf("\n");
printf("NexRv ERROR: %s\n", err);
return 9;
}
static int usage(const char *err)
{
if (err != NULL)
{
error(err);
}
printf("\n");
printf("Usage:\n");
printf(" NexRv -dump <nex> [<dump>] [-msg|-none] - dump Nexus file\n");
printf(" NexRv -deco <nex> -pcinfo <info> -pcout <pco> [-stat|-full|-all|-msg|-none] - decode trace\n");
printf(" NexRv -enco <pcseq> -nex <nex> [-nobhm|-norbm] [-stat|-full|-all|-msg|-none] - encode trace \n");
printf(" NexRv -conv -objd <objd> -pcinfo <pci> - create <pci> from objdump -d output <objd>\n");
printf(" NexRv -conv -pcinfo <pci> -pconly <pco> -pcseq <pcs> - convert <pco> to <pcs> using <pci>\n");
printf(" NexRv -diff -pcseq <pcs> -pconly <pco> - compare <pcs> with <pco>\n");
printf("where:\n");
printf(" -nobhm|-norbm - do not generate Branch History/Repeat Branch Messages\n");
printf(" -stat|-full|-all|-msg|-none - verbose level\n");
return 1;
}
int main(int argc, char *argv[])
{
if (argc < 2) return usage(NULL);
if (strcmp(argv[1], "-dump") == 0) // Dump?
{
if (argc < 3) return usage("Nexus bin file is expected");
fNex = fopen(argv[2], "rb");
if (fNex == NULL) return error("Cannot open NEX file");
int opt = 3;
FILE *fDump = stdout;
if (argc > 3 && argv[3][0] != '-')
{
fDump = fopen(argv[3], "wt");
if (fDump == NULL) return error("Cannot create DUMP file");
opt = 4;
}
int disp = 4 | 2 | 1; // Default (all)
if (argc > opt && strcmp(argv[opt], "-msg") == 0) disp = 4 | 2; // TCODE and stat.
if (argc > opt && strcmp(argv[opt], "-none") == 0) disp = 4; // Only statistics
int ret = NexusDump(fDump, disp);
fclose(fNex); fNex = NULL;
if (fDump != stdout) fclose(fDump);
if (ret <= 0) return error("Nexus Trace dump failed");
return 0; // All OK
}
if (strcmp(argv[1], "-conv") == 0) // Convert?
{
// -conv -objd <objd> -pcinfo <pci>
// -conv -pcinfo <pci> -pconly <pc> -pcseq <ps>
const char *err = "Incorrect -conv calling";
int ret = 0;
if (argc == 6 && strcmp(argv[2], "-objd") == 0)
{
// -conv -objd <objd> -pcinfo <pci>
if (strcmp(argv[4], "-pcinfo") == 0)
{
// Syntax correct - open all files
err = NULL;
FILE *objdFile = fopen(argv[3], "rt");
if (objdFile == NULL) return error("Cannot open OBJD file");
FILE *pciFile = fopen(argv[5], "wt");
if (pciFile == NULL) return error("Cannot create PCINFO file");
// Run conversion
ret = ConvGnuObjdump(objdFile, pciFile);
fclose(pciFile);
fclose(objdFile);
}
}
else
if (argc == 8 && strcmp(argv[2], "-pcinfo") == 0)
{
// -conv -pcinfo <pci> -pconly <pc> -pcseq <ps>
if (strcmp(argv[4], "-pconly") == 0 && strcmp(argv[6], "-pcseq") == 0)
{
// Syntax correct - open all files
err = NULL;
if (InfoInit(argv[3]) < 0) return error("Cannot open PCINFO file");
FILE *pcFile = fopen(argv[5], "rt");
if (pcFile == NULL) return error("Cannot open PCONLY file");
FILE *psFile = fopen(argv[7], "wt");
if (psFile == NULL) return error("Cannot create PCSEQ file");
// Run conversion
ret = ConvAddInfo(pcFile, psFile, NULL);
fclose(pcFile);
fclose(psFile);
InfoTerm();
}
}
if (err != NULL) return usage(err);
if (ret > 0)
{
printf("Converted OK (%d instructions)\n\n", ret);
ret = 0;
}
else
{
printf("ERROR: Conversion failed with error code #%d\n\n", -ret);
ret = 9;
}
return ret;
}
if (strcmp(argv[1], "-diff") == 0) // Diff?
{
// -comp -pcseq <pcs> -pcout <pco>
const char *err = "Incorrect -diff calling";
int ret = 0;
if (argc == 6 && strcmp(argv[2], "-pcseq") == 0)
{
// -conv -objd <objd> -pcinfo <pci>
if (strcmp(argv[4], "-pcout") == 0)
{
// Syntax correct - open all files
err = NULL;
FILE *fPcseq = fopen(argv[3], "rt");
if (fPcseq == NULL) return error("Cannot open PCSEQ file");
FILE *fPcout = fopen(argv[5], "rt");
if (fPcout == NULL) return error("Cannot open PCOUT file");
// Run comparison
ret = ConvAddInfo(fPcseq, NULL, fPcout);
fclose(fPcseq);
fclose(fPcout);
}
}
if (err != NULL) return usage(err);
if (ret > 0)
{
printf("Compared OK (%d instructions)\n\n", ret);
ret = 0;
}
else
{
printf("ERROR: Conversion failed with error code #%d\n\n", -ret);
ret = 9;
}
return ret;
}
if (strcmp(argv[1], "-enco") == 0) // Encode?
{
if (argc < 5) return error("Incorrect number of parameters");
if (strcmp(argv[3], "-nex") != 0) return error("-nex must be provided");
FILE *fPcseq = fopen(argv[2], "rt");
if (fPcseq == NULL) return error("Cannot open PCSEQ file");
fNex = fopen(argv[4], "wb");
if (fNex == NULL) return error("Cannot create NEX file");
int level = -1; // Default level
if (argc > 5 && strcmp(argv[5], "-nobhm") == 0) level = 10; // Level 1.0
if (argc > 5 && strcmp(argv[5], "-norbm") == 0) level = 20; // Level 2.0
int disp = 4; // Default (stat)
int dispPos = 5;
if (level >= 0) dispPos++; // Skip over level parameter
if (argc > dispPos && strcmp(argv[dispPos], "-all") == 0) disp = 4 | 2 | 1; // All
if (argc > dispPos && strcmp(argv[dispPos], "-msg") == 0) disp = 4 | 2; // TCODE and stat.
if (argc > dispPos && strcmp(argv[dispPos], "-stat") == 0) disp = 4; // Only statistics
if (argc > dispPos && strcmp(argv[dispPos], "-none") == 0) disp = 0; // Nothing
if (argc > dispPos && strcmp(argv[dispPos], "-full") == 0) disp = 0xFF; // Everything
if (level < 0) level = 21; // Level 2.1 is default
int ret = NexusEnco(fPcseq, level, disp);
fclose(fNex); fNex = NULL;
fclose(fPcseq); fPcseq = NULL;
if (ret > 0)
{
printf("Encoded OK (%d messages)\n\n", ret);
ret = 0;
}
else
{
printf("ERROR: Encoding failed with error code #%d\n\n", -ret);
ret = 9;
}
return ret;
}
if (strcmp(argv[1], "-deco") == 0) // Decode?
{
if (argc < 7) return error("Incorrect number of parameters");
if (strcmp(argv[3], "-pcinfo") != 0) return error("-pcinfo must be provided");
if (strcmp(argv[5], "-pcout") != 0) return error("-pcout must be provided");
fNex = fopen(argv[2], "rb");
if (fNex == NULL) return error("Cannot open NEX file");
if (InfoInit(argv[4]) < 0) return error("Cannot open PCINFO file");
FILE *fOut = fopen(argv[6], "wt");
if (fOut == NULL) return error("Cannot create PCOUT file");
int disp = 4; // Default (-stat)
if (argc > 7 && strcmp(argv[7], "-all") == 0) disp = 4 | 2 | 1; // All
if (argc > 7 && strcmp(argv[7], "-msg") == 0) disp = 4 | 2; // TCODE and stat.
if (argc > 7 && strcmp(argv[7], "-stat") == 0) disp = 4; // Only statistics
if (argc > 7 && strcmp(argv[7], "-none") == 0) disp = 0; // Nothing
if (argc > 7 && strcmp(argv[7], "-full") == 0) disp = 0xFF; // Everything
int ret = NexusDeco(fOut, disp);
fclose(fOut);
fclose(fNex); fNex = NULL;
InfoTerm();
if (ret > 0)
{
printf("Decoded OK (%d instructions)\n\n", ret);
ret = 0;
}
else
{
printf("ERROR: Decoding failed with error code #%d\n\n", -ret);
ret = 9;
}
return ret;
}
return usage("Unkown option");
}
//****************************************************************************
// End of nexrv.c file