OpenCores
URL https://opencores.org/ocsvn/or2k/or2k/trunk

Subversion Repositories or2k

[/] [or2k/] [trunk/] [analysis-bin/] [insnanalysis/] [insnanalysis.c] - Blame information for rev 28

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 julius
/*
2
  File for analysis of an architecture's instructions in binary format
3
 
4
  Julius Baxter, julius.baxter@orsoc.se
5
 
6
*/
7
 
8 28 julius
#include <ctype.h>
9
#include <stdio.h>
10 16 julius
#include <stdlib.h>
11
#include <string.h>
12
#include <stdint.h>
13 28 julius
#include <unistd.h>
14 16 julius
#include <arpa/inet.h> // for htonl()
15
 
16
#include "insnanalysis.h"
17
 
18
// Access to list management functions
19
#include "insn-lists.h"
20
 
21
int analyse_insn(instruction insn,
22
                 instruction_properties *insn_props)
23
{
24
  // For now no other instruction sets supported
25
  return or1k_32_analyse_insn(insn, insn_props);
26
}
27
 
28
 
29
void print_insn(instruction_properties *insn_props)
30
{
31
  if (insn_props->insn_string != NULL)
32
    printf("%s", insn_props->insn_string);
33
}
34
 
35
void collect_stats(instruction insn,
36 28 julius
                   instruction_properties *insn_props, int record_unique)
37 16 julius
{
38 28 julius
  or1k_32_collect_stats(insn, insn_props, record_unique);
39 16 julius
}
40
 
41 17 julius
 
42
void generate_stats(FILE * stream)
43
{
44
  or1k_32_generate_stats(stream);
45
}
46
 
47 16 julius
int main(int argc, char *argv[])
48
{
49 28 julius
  FILE *ifp, *ofp;
50 16 julius
 
51
  char insn_buff[INSN_SIZE_BYTES]; // Buffer for instruction data
52
 
53
  int insns_seen_total = 0; // Keep track of total number of instructions read
54
 
55 28 julius
  char *infilename = NULL;
56
  char *outfilename = NULL;
57
 
58
  int outfile_flag = 0; // default is not to use an output file
59
 
60
  int opterr = 0;
61
 
62
  int uniquecount_flag = 0;
63
 
64
  int c;
65
 
66
  // Look for:
67
  // -f filename : Input file name (required)
68
  // [ -o filename] : Output file name (optional)
69
  // [ -u ] : Do unique instruction analysis (option)
70
  while ((c = getopt(argc, argv, "f:o:u::")) != -1)
71
    switch (c)
72
      {
73
      case 'f':
74
        infilename = optarg;
75
        break;
76
 
77
      case 'o':
78
        outfile_flag = 1;
79
        outfilename = optarg;
80
        break;
81
 
82
      case 'u':
83
        uniquecount_flag = 1;
84
        break;
85
 
86
      default:
87
        abort();
88
      }
89
 
90
 
91
  // Try to open the input file
92
  if((ifp = fopen(infilename, "rb"))==NULL) {
93
    printf("Cannot open input file, %s\n", infilename);
94 16 julius
    exit(1);
95
  }
96 28 julius
 
97 21 julius
  int filesize_bytes, filesize_insns;
98
  // Determine filesize
99 28 julius
  if ( fseek(ifp, 0, SEEK_END))
100 21 julius
    {
101 28 julius
      fclose(ifp);
102
      fprintf(stderr, "Error detecting input file size\n");
103 21 julius
      return -1;
104
    }
105
 
106 28 julius
  filesize_bytes = ftell(ifp);
107 21 julius
  filesize_insns = filesize_bytes / INSN_SIZE_BYTES;
108 22 julius
 
109
#ifdef DISPLAY_STRING  
110
  printf("\nAnalysing file:\t%s\tSize: %d MB\n",
111 28 julius
         infilename,((filesize_bytes/1024)/1024));
112 22 julius
#endif
113
 
114 21 julius
  // Reset pointer
115 28 julius
  rewind(ifp);
116 21 julius
 
117 16 julius
  instruction * insn = (instruction *)insn_buff;
118
 
119
  instruction_properties insn_props;
120
 
121 21 julius
  // Go through the file, collect stats about instructions
122 16 julius
 
123 21 julius
  // What is one-percent of instructions
124
  float file_one_percent = ((float)filesize_insns / 100.0f );
125 22 julius
  float percent_of_percent=0; int percent=0;
126 21 julius
 
127 16 julius
  insn_lists_init();
128
 
129 28 julius
  while(!feof(ifp)) {
130 16 julius
 
131 28 julius
    if (fread(insn_buff, INSN_SIZE_BYTES, 1, ifp) != 1)
132 16 julius
      break;
133
 
134
    // Endianness is little when read in from binary file created with 
135
    // or32-elf-objcopy, so swap;
136
    *insn = htonl(*insn);
137
 
138 17 julius
    if (*insn == 0) // most probably dead space in binary, skip
139
      continue;
140
 
141 16 julius
    reset_instruction_properties(&insn_props);
142
 
143 17 julius
    if (analyse_insn(*insn, &insn_props) == 0)
144
      {
145 21 julius
 
146 17 julius
        insns_seen_total++;
147
 
148 28 julius
        collect_stats(*insn, &insn_props, uniquecount_flag);
149
 
150 17 julius
      }
151
    else
152
      {
153 19 julius
        // Non-zero return from analyse_insn(): problem analysing instruction.
154
 
155
        // Is a NOP for now, but do something here if needed
156
 
157
        do{ } while(0);
158 17 julius
      }
159 21 julius
 
160
    // Progress indicator
161
    percent_of_percent += 1.0f;
162
    if (percent_of_percent >= file_one_percent)
163
      {
164
        percent++;
165
        fprintf(stderr, "\r%d%%", percent);
166
        percent_of_percent = 0;
167
      }
168 16 julius
  }
169
 
170 28 julius
  fclose(ifp);
171 17 julius
 
172 22 julius
  fprintf(stderr, "\rDone\n", percent);
173 28 julius
 
174
  // Try to open the output file
175
  if (outfile_flag)
176
    {
177
      if((ofp = fopen(outfilename, "wb+"))==NULL) {
178
        printf("Cannot open output file.\n");
179
        exit(1);
180
      }
181
    }
182
  else
183
    // Otherwise we'll use stdout
184
    ofp = (FILE *)stdout;
185
 
186 22 julius
#ifdef DISPLAY_STRING
187 28 julius
  fprintf(ofp, "Saw %d instructions\n", insns_seen_total);
188 22 julius
#endif
189 25 julius
#ifdef DISPLAY_CSV
190 28 julius
  fprintf(ofp, "\"File:\",\"%s\",\"Num insns:\",%d,\n",
191
          infilename, insns_seen_total);
192 25 julius
#endif
193 28 julius
 
194
  // Generate output
195
  generate_stats(ofp);
196 16 julius
 
197 17 julius
  insn_lists_free();
198
 
199 16 julius
  return 0;
200
 
201
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.