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

Subversion Repositories or2k

[/] [or2k/] [trunk/] [analysis-bin/] [insnanalysis/] [insnanalysis.c] - Rev 25

Go to most recent revision | Compare with Previous | Blame | View Log

/*
  File for analysis of an architecture's instructions in binary format
 
  Julius Baxter, julius.baxter@orsoc.se
 
*/
 
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h> // for htonl()
 
#include "insnanalysis.h"
 
// Access to list management functions
#include "insn-lists.h"
 
int analyse_insn(instruction insn,
		 instruction_properties *insn_props)
{
  // For now no other instruction sets supported
  return or1k_32_analyse_insn(insn, insn_props);
}
 
 
void print_insn(instruction_properties *insn_props)
{
  if (insn_props->insn_string != NULL)
    printf("%s", insn_props->insn_string);
}
 
void collect_stats(instruction insn,
		 instruction_properties *insn_props)
{
  or1k_32_collect_stats(insn, insn_props);
}
 
 
void generate_stats(FILE * stream)
{
  or1k_32_generate_stats(stream);
}
 
int main(int argc, char *argv[])
{
  FILE *fp;
 
  char insn_buff[INSN_SIZE_BYTES]; // Buffer for instruction data
 
  int insns_seen_total = 0; // Keep track of total number of instructions read
 
  // Try to open the file passed as first parameter
  if((fp = fopen(argv[ 1 ], "rb"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }
 
  int filesize_bytes, filesize_insns;
  // Determine filesize
  if ( fseek(fp, 0, SEEK_END))
    {
      fclose(fp);
      fprintf(stderr, "Error detecting filesize\n");
      return -1;
    }
 
  filesize_bytes = ftell(fp);
  filesize_insns = filesize_bytes / INSN_SIZE_BYTES;
 
#ifdef DISPLAY_STRING  
  printf("\nAnalysing file:\t%s\tSize: %d MB\n",
	 argv[1],((filesize_bytes/1024)/1024));
#endif
 
  // Reset pointer
  rewind(fp);
 
  instruction * insn = (instruction *)insn_buff;
 
  instruction_properties insn_props;
 
  // Go through the file, collect stats about instructions
 
  // What is one-percent of instructions
  float file_one_percent = ((float)filesize_insns / 100.0f );
  float percent_of_percent=0; int percent=0;
 
  insn_lists_init();
 
  while(!feof(fp)) {
 
    if (fread(insn_buff, INSN_SIZE_BYTES, 1, fp) != 1)
      break;
 
    // Endianness is little when read in from binary file created with 
    // or32-elf-objcopy, so swap;
    *insn = htonl(*insn);
 
    if (*insn == 0) // most probably dead space in binary, skip
      continue;
 
    reset_instruction_properties(&insn_props);
 
    if (analyse_insn(*insn, &insn_props) == 0)
      {
 
	insns_seen_total++;
 
	collect_stats(*insn, &insn_props);
      }
    else
      {
	// Non-zero return from analyse_insn(): problem analysing instruction.
 
	// Is a NOP for now, but do something here if needed
 
	do{ } while(0);
      }
 
    // Progress indicator
    percent_of_percent += 1.0f;
    if (percent_of_percent >= file_one_percent)
      {
	percent++;
	fprintf(stderr, "\r%d%%", percent);
	percent_of_percent = 0;
      }
  }
 
  fclose(fp);
 
  fprintf(stderr, "\rDone\n", percent);
#ifdef DISPLAY_STRING
  fprintf(stdout, "Saw %d instructions\n", insns_seen_total);
#endif
#ifdef DISPLAY_CSV
  fprintf(stdout, "\"File:\",\"%s\",\"Num insns:\",%d,\n",
	  argv[1], insns_seen_total);
#endif
  generate_stats(stdout);
 
  insn_lists_free();
 
  return 0;
 
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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