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

Subversion Repositories or2k

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

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
 
71
  // Reset pointer
72
  rewind(fp);
73
 
74
 
75
 
76 16 julius
  instruction * insn = (instruction *)insn_buff;
77
 
78
  instruction_properties insn_props;
79
 
80 21 julius
  // Go through the file, collect stats about instructions
81 16 julius
 
82 21 julius
  // What is one-percent of instructions
83
  float file_one_percent = ((float)filesize_insns / 100.0f );
84
  float percent_of_percent=0; int percent;
85
 
86 16 julius
  insn_lists_init();
87
 
88
  while(!feof(fp)) {
89
 
90
    if (fread(insn_buff, INSN_SIZE_BYTES, 1, fp) != 1)
91
      break;
92
 
93
    // Endianness is little when read in from binary file created with 
94
    // or32-elf-objcopy, so swap;
95
    *insn = htonl(*insn);
96
 
97 17 julius
    if (*insn == 0) // most probably dead space in binary, skip
98
      continue;
99
 
100 16 julius
    reset_instruction_properties(&insn_props);
101
 
102 17 julius
    if (analyse_insn(*insn, &insn_props) == 0)
103
      {
104 21 julius
 
105 17 julius
        insns_seen_total++;
106
 
107
        collect_stats(*insn, &insn_props);
108
      }
109
    else
110
      {
111 19 julius
        // Non-zero return from analyse_insn(): problem analysing instruction.
112
 
113
        // Is a NOP for now, but do something here if needed
114
 
115
        do{ } while(0);
116 17 julius
      }
117 21 julius
 
118
    // Progress indicator
119
    percent_of_percent += 1.0f;
120
    if (percent_of_percent >= file_one_percent)
121
      {
122
        percent++;
123
        fprintf(stderr, "\r%d%%", percent);
124
        percent_of_percent = 0;
125
      }
126 16 julius
 
127 21 julius
 
128 16 julius
  }
129
 
130
  fclose(fp);
131 17 julius
 
132 21 julius
  printf("\rSaw %d instructions\n", insns_seen_total);
133 16 julius
 
134 17 julius
  generate_stats(stdout);
135 16 julius
 
136 17 julius
  insn_lists_free();
137
 
138 16 julius
  return 0;
139
 
140
}

powered by: WebSVN 2.1.0

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