home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / hcesource / top / source / main.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  250 lines

  1. /* Copyright (c) 1988,1989,1991 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  * Modified by Detlef Wuerkner for AMIGA
  12.  * Changes marked with TETISOFT
  13.  *
  14.  * 1994
  15.  * Modified by Jason Petty to work with HCE.
  16.  * Changes marked with VANSOFT.
  17.  */
  18.  
  19. #include "top.h"
  20.  
  21. FILE *ifp, *ofp;  /* input/output file pointers */
  22.  
  23. /* ADDED BY TETISOFT */
  24. FILE *rfp, *cfp, *dfp, *bfp; /* refs-code-data-bss-dest file pointers */
  25. FILE *destfp;   /* desired destination file pointer */
  26.  
  27. #ifndef MINIX
  28. long _STKSIZ = 32768L; /* need mucho stack for recursion */
  29. #endif
  30.  
  31. /*
  32.  * Options 
  33.  */
  34. bool debug   = FALSE;
  35. bool do_brev = TRUE;  /* branch reversals enabled */
  36. bool do_peep = TRUE;  /* peephole optimizations enabled */
  37. bool do_regs = TRUE;  /* do "registerizing" */
  38. bool do_lrot = TRUE;  /* do loop rotations */
  39. bool gflag = FALSE;  /* don't do stuff that confuses the debugger */
  40. bool verbose = FALSE;
  41.  
  42. /* ADDED BY TETISOFT */
  43. int dest_hunk = PUBLICMEM;
  44.  
  45. /*
  46.  * Optimization statistics (use -v to print)
  47.  */
  48. int s_bdel = 0;  /* branches deleted */
  49. int s_badd = 0;  /* branches added */
  50. int s_brev = 0;  /* branch reversals */
  51. int s_lrot = 0;  /* loop rotations */
  52. int s_peep1 = 0;  /* 1 instruction peephole changes */
  53. int s_peep2 = 0;  /* 2 instruction peephole changes */
  54. int s_peep3 = 0;  /* 3 instruction peephole changes */
  55. int s_idel = 0;  /* instructions deleted */
  56. int s_reg = 0;  /* variables "registerized" */
  57.  
  58. /* CHANGED BY TETISOFT */
  59. /* #define TMPFILE "top_tmp.$$$" */ /* temporary file name */
  60. /*int use_temp = FALSE; */ /* using temporary file */
  61. char *rfile = "T:TOP.refs";
  62. char *cfile = "T:TOP.code";
  63. char *dfile = "T:TOP.data";
  64. char *bfile = "T:TOP.bss";
  65.  
  66. char *Version =
  67. "top Version 2.00  Copyright (c) 1988-1991 by Sozobon, Limited.";
  68.  
  69. /* ADDED BY TETISOFT */
  70. char *Version2 =
  71. "    Amiga Version 1.1 by Detlef W\374rkner.";
  72.  
  73. usage()
  74. {
  75.  
  76. /* CHANGED BY TETISOFT */
  77.  /* fprintf(stderr, "usage: top [-gdvblpr] infile [outfile]\n"); */
  78.  fprintf(stderr, "\nUsage: top [flags] infile [outfile]\n");
  79.  fprintf(stderr, "Valid optimizer flags are:\n");
  80.  fprintf(stderr, "   -d: Debug\n");
  81.  fprintf(stderr, "   -v: Verbose\n");
  82.  fprintf(stderr, "   -b: Branch reversal OFF\n");
  83.  fprintf(stderr, "   -l: Loop rotation OFF\n");
  84.  fprintf(stderr, "   -p: Peephole optimization OFF\n");
  85.  fprintf(stderr, "   -r: Variable registerizing OFF\n");
  86.  fprintf(stderr, "   -g: No change of stack fix-ups (for debugging)\n");
  87.  fprintf(stderr, "   -c: Force DATA and BSS hunks to Chip Memory\n");
  88.  
  89. /* CHANGED BY TETISOFT */
  90. /* exit(1); */
  91.  exit(EXIT_FAILURE);
  92.  
  93. }
  94.  
  95. /* ADDED BY TETISOFT */
  96. FILE *saveopen(file, mode)
  97. char *file, *mode;
  98. {
  99.  FILE *fp;
  100.  void *buf;
  101.  
  102.  if ((fp = fopen(file, mode)) == NULL) {
  103.     fprintf(stderr, "TOP: Can't open file %s\n", file);
  104.     exit(EXIT_FAILURE);
  105.  }
  106.  buf = (void *)alloc(BUFSIZ);
  107.  setbuf (fp, buf);
  108.  return(fp);
  109. }
  110.  
  111. /* ADDED BY TETISOFT */
  112. void savecopy(fp, name)
  113. FILE *fp;
  114. char *name;
  115. {
  116.  short c;
  117.  
  118.  fclose(fp);   /* fflush/rewind doesn't work! */
  119.  fp = saveopen(name, "r");
  120.  while ((c = fgetc(fp)) != EOF) {
  121.      if (fputc(c, destfp) == EOF) {
  122.      fprintf(stderr, "TOP: Can't write to output file\n");
  123.   exit(EXIT_FAILURE);
  124.      }
  125.  }
  126.  fclose(fp);
  127.  remove(name);
  128. }
  129.  
  130. main(argc, argv)
  131. int argc;
  132. char *argv[];
  133. {
  134.  FILE *fopen();
  135.  register char *s;
  136.  
  137. /* ADDED BY TETISOFT */
  138.  if (argc == 0) {  /* We run from WorkBench */
  139.   exit(EXIT_FAILURE);
  140.  }
  141.  
  142.  while (argc > 1 && argv[1][0] == '-') {
  143.   for (s = &argv[1][1]; *s ;s++) {
  144.    switch (*s) {
  145.    case 'd': case 'D':
  146.     debug = TRUE;
  147.     break;
  148.    case 'b': case 'B':
  149.     do_brev = FALSE;
  150.     break;
  151.    case 'p': case 'P':
  152.     do_peep = FALSE;
  153.     break;
  154.    case 'r': case 'R':
  155.     do_regs = FALSE;
  156.     break;
  157.    case 'l': case 'L':
  158.     do_lrot = FALSE;
  159.     break;
  160.    case 'v': case 'V':
  161.  
  162. /* CHANGED BY TETISOFT */
  163. /* CHANGED AGIAN, VANSOFT */
  164.                          fprintf(stderr,"%s\n%s\n",Version,Version2);
  165.     verbose = TRUE;
  166.     break;
  167.    case 'g': case 'G':
  168.     gflag = TRUE;
  169.     break;
  170.    case 'O': case 'z': case 'Z':
  171.     /*
  172.      * When options are received from 'cc' they
  173.      * look like "-Oxxx", so just ignore the 'O'.
  174.      */
  175.     break;
  176.  
  177. /* ADDED BY TETISOFT */
  178.    case 'c': case 'C':
  179.     dest_hunk = CHIPMEM;
  180.     break;
  181.  
  182.    default:
  183.     usage();
  184.     break;
  185.    }
  186.   }
  187.   argv++;
  188.   argc--;
  189.  }
  190.  
  191. /* CHANGED BY TETISOFT */
  192.  if ((argc > 3) || (argc < 2))
  193.   usage();
  194.  
  195. /* COMMENTED OUT, VANSOFT.
  196.  fprintf(stderr, "%s:\n", argv[1]);
  197. */
  198.  
  199.  ifp = saveopen(argv[1], "r");
  200.  if (argc > 2)
  201.   destfp = saveopen(argv[2], "w");
  202.  rfp = saveopen(rfile, "w");
  203.  ofp = rfp;
  204.  cfp = saveopen(cfile, "w");
  205.  dfp = saveopen(dfile, "w");
  206.  bfp = saveopen(bfile, "w");
  207.  
  208.  dofile();
  209.  
  210.  if (verbose) {
  211.   if (do_peep) {
  212.    fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  213.    fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  214.    fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  215.    fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  216.   }
  217.   if (do_regs)
  218.    fprintf(stderr, "Variables registered: %4d\n", s_reg);
  219.   if (do_lrot)
  220.    fprintf(stderr, "Loop rotations      : %4d\n", s_lrot);
  221.   if (do_brev)
  222.    fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  223.   fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  224.  }
  225.  
  226. /* ADDED BY TETISOFT */
  227.  if (ifp != stdin)
  228.   fclose(ifp);
  229.  if (!destfp)
  230.     destfp = saveopen(argv[1], "w");
  231.  savecopy(rfp, rfile);
  232.  savecopy(cfp, cfile);
  233.  savecopy(dfp, dfile);
  234.  savecopy(bfp, bfile);
  235.  fprintf(destfp, "\n\tEND\n");
  236.  if (destfp != stdout)
  237.   fclose(destfp);
  238.  exit(EXIT_SUCCESS);
  239. }
  240.  
  241.  
  242. dofile()
  243. {
  244.  if (!readline())
  245.   return;
  246.  
  247.  while (dofunc())
  248.   ;
  249. }
  250.