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

Subversion Repositories or2k

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

Go to most recent revision | 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
#include<stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <stdint.h>
12
#include <arpa/inet.h> // for htonl()
13
 
14
#include "insnanalysis.h"
15
 
16
// Access to list management functions
17
#include "insn-lists.h"
18
 
19
int analyse_insn(instruction insn,
20
                 instruction_properties *insn_props)
21
{
22
  // For now no other instruction sets supported
23
  return or1k_32_analyse_insn(insn, insn_props);
24
}
25
 
26
 
27
void print_insn(instruction_properties *insn_props)
28
{
29
  if (insn_props->insn_string != NULL)
30
    printf("%s", insn_props->insn_string);
31
}
32
 
33
void collect_stats(instruction insn,
34
                 instruction_properties *insn_props)
35
{
36
  or1k_32_collect_stats(insn, insn_props);
37
}
38
 
39 17 julius
 
40
void generate_stats(FILE * stream)
41
{
42
  or1k_32_generate_stats(stream);
43
}
44
 
45 16 julius
int main(int argc, char *argv[])
46
{
47
  FILE *fp;
48
 
49
  char insn_buff[INSN_SIZE_BYTES]; // Buffer for instruction data
50
 
51
  int insns_seen_total = 0; // Keep track of total number of instructions read
52
 
53
  // Try to open the file passed as first parameter
54
  if((fp = fopen(argv[ 1 ], "rb"))==NULL) {
55
    printf("Cannot open file.\n");
56
    exit(1);
57
  }
58
 
59 21 julius
  int filesize_bytes, filesize_insns;
60
  // Determine filesize
61
  if ( fseek(fp, 0, SEEK_END))
62
    {
63
      fclose(fp);
64
      fprintf(stderr, "Error detecting filesize\n");
65
      return -1;
66
    }
67
 
68
  filesize_bytes = ftell(fp);
69
  filesize_insns = filesize_bytes / INSN_SIZE_BYTES;
70 22 julius
 
71
#ifdef DISPLAY_STRING  
72
  printf("\nAnalysing file:\t%s\tSize: %d MB\n",
73
         argv[1],((filesize_bytes/1024)/1024));
74
#endif
75
 
76 21 julius
  // Reset pointer
77
  rewind(fp);
78
 
79 16 julius
  instruction * insn = (instruction *)insn_buff;
80
 
81
  instruction_properties insn_props;
82
 
83 21 julius
  // Go through the file, collect stats about instructions
84 16 julius
 
85 21 julius
  // What is one-percent of instructions
86
  float file_one_percent = ((float)filesize_insns / 100.0f );
87 22 julius
  float percent_of_percent=0; int percent=0;
88 21 julius
 
89 16 julius
  insn_lists_init();
90
 
91
  while(!feof(fp)) {
92
 
93
    if (fread(insn_buff, INSN_SIZE_BYTES, 1, fp) != 1)
94
      break;
95
 
96
    // Endianness is little when read in from binary file created with 
97
    // or32-elf-objcopy, so swap;
98
    *insn = htonl(*insn);
99
 
100 17 julius
    if (*insn == 0) // most probably dead space in binary, skip
101
      continue;
102
 
103 16 julius
    reset_instruction_properties(&insn_props);
104
 
105 17 julius
    if (analyse_insn(*insn, &insn_props) == 0)
106
      {
107 21 julius
 
108 17 julius
        insns_seen_total++;
109
 
110
        collect_stats(*insn, &insn_props);
111
      }
112
    else
113
      {
114 19 julius
        // Non-zero return from analyse_insn(): problem analysing instruction.
115
 
116
        // Is a NOP for now, but do something here if needed
117
 
118
        do{ } while(0);
119 17 julius
      }
120 21 julius
 
121
    // Progress indicator
122
    percent_of_percent += 1.0f;
123
    if (percent_of_percent >= file_one_percent)
124
      {
125
        percent++;
126
        fprintf(stderr, "\r%d%%", percent);
127
        percent_of_percent = 0;
128
      }
129 16 julius
  }
130
 
131
  fclose(fp);
132 17 julius
 
133 22 julius
  fprintf(stderr, "\rDone\n", percent);
134
#ifdef DISPLAY_STRING
135 25 julius
  fprintf(stdout, "Saw %d instructions\n", insns_seen_total);
136 22 julius
#endif
137 25 julius
#ifdef DISPLAY_CSV
138
  fprintf(stdout, "\"File:\",\"%s\",\"Num insns:\",%d,\n",
139
          argv[1], insns_seen_total);
140
#endif
141 17 julius
  generate_stats(stdout);
142 16 julius
 
143 17 julius
  insn_lists_free();
144
 
145 16 julius
  return 0;
146
 
147
}

powered by: WebSVN 2.1.0

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