home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / top / main.c < prev    next >
C/C++ Source or Header  |  1991-02-22  |  4KB  |  169 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. #include "top.h"
  12.  
  13. FILE    *ifp, *ofp;        /* input/output file pointers */
  14.  
  15. #ifndef    MINIX
  16. long    _STKSIZ = 32768L;    /* need mucho stack for recursion */
  17. #endif
  18.  
  19. /*
  20.  * Options 
  21.  */
  22. bool    debug   = FALSE;
  23. bool    do_brev = TRUE;        /* branch reversals enabled */
  24. bool    do_peep = TRUE;        /* peephole optimizations enabled */
  25. bool    do_regs = TRUE;        /* do "registerizing" */
  26. bool    do_lrot = TRUE;        /* do loop rotations */
  27. bool    gflag = FALSE;        /* don't do stuff that confuses the debugger */
  28. bool    verbose = FALSE;
  29.  
  30. /*
  31.  * Optimization statistics (use -v to print)
  32.  */
  33. int    s_bdel = 0;        /* branches deleted */
  34. int    s_badd = 0;        /* branches added */
  35. int    s_brev = 0;        /* branch reversals */
  36. int    s_lrot = 0;        /* loop rotations */
  37. int    s_peep1 = 0;        /* 1 instruction peephole changes */
  38. int    s_peep2 = 0;        /* 2 instruction peephole changes */
  39. int    s_peep3 = 0;        /* 3 instruction peephole changes */
  40. int    s_idel = 0;        /* instructions deleted */
  41. int    s_reg = 0;        /* variables "registerized" */
  42.  
  43. #define    TMPFILE    "top_tmp.$$$"    /* temporary file name */
  44. int    use_temp = FALSE;    /* using temporary file */
  45.  
  46. char    *Version =
  47. "top Version 2.00  Copyright (c) 1988-1991 by Sozobon, Limited.";
  48.  
  49. usage()
  50. {
  51.     fprintf(stderr, "usage: top [-gdvblpr] infile [outfile]\n");
  52.     exit(1);
  53. }
  54.  
  55. main(argc, argv)
  56. int    argc;
  57. char    *argv[];
  58. {
  59.     FILE    *fopen();
  60.     register char    *s;
  61.  
  62.     while (argc > 1 && argv[1][0] == '-') {
  63.         for (s = &argv[1][1]; *s ;s++) {
  64.             switch (*s) {
  65.             case 'd': case 'D':
  66.                 debug = TRUE;
  67.                 break;
  68.             case 'b': case 'B':
  69.                 do_brev = FALSE;
  70.                 break;
  71.             case 'p': case 'P':
  72.                 do_peep = FALSE;
  73.                 break;
  74.             case 'r': case 'R':
  75.                 do_regs = FALSE;
  76.                 break;
  77.             case 'l': case 'L':
  78.                 do_lrot = FALSE;
  79.                 break;
  80.             case 'v': case 'V':
  81.                 fprintf(stderr, "%s\n", Version);
  82.                 verbose = TRUE;
  83.                 break;
  84.             case 'g': case 'G':
  85.                 gflag = TRUE;
  86.                 break;
  87.             case 'O': case 'z': case 'Z':
  88.                 /*
  89.                  * When options are received from 'cc' they
  90.                  * look like "-Oxxx", so just ignore the 'O'.
  91.                  */
  92.                 break;
  93.             default:
  94.                 usage();
  95.                 break;
  96.             }
  97.         }
  98.         argv++;
  99.         argc--;
  100.     }
  101.  
  102.     if (argc > 3)
  103.         usage();
  104.  
  105.     if (argc > 1) {
  106.         if (strcmp(argv[1], "-") == 0)
  107.             ifp = stdin;
  108.         else if ((ifp = fopen(argv[1], "r")) == NULL) {
  109.             fprintf(stderr, "top: can't open input file '%s'\n",
  110.                 argv[1]);
  111.             exit(1);
  112.         }
  113.         if (argc > 2) {
  114.             if (strcmp(argv[2], "-") == 0)
  115.                 ofp = stdout;
  116.             else if ((ofp = fopen(argv[2], "w")) == NULL) {
  117.                 fprintf(stderr, "top: can't open output file '%s'\n",
  118.                     argv[2]);
  119.                 exit(1);
  120.             }
  121.         } else {
  122.             if ((ofp = fopen(TMPFILE, "w")) == NULL) {
  123.                 fprintf(stderr, "top: can't create temp file\n");
  124.                 exit(1);
  125.             }
  126.             use_temp = TRUE;
  127.         }
  128.     } else
  129.         usage();
  130.  
  131.     dofile();
  132.  
  133.     if (verbose) {
  134.         if (do_peep) {
  135.             fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  136.             fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  137.             fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  138.             fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  139.         }
  140.         if (do_regs)
  141.             fprintf(stderr, "Variables registered: %4d\n", s_reg);
  142.         if (do_lrot)
  143.             fprintf(stderr, "Loop rotations      : %4d\n", s_lrot);
  144.         if (do_brev)
  145.             fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  146.         fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  147.     }
  148.  
  149.     /*
  150.      * If we're overwriting the original file, remove the old
  151.      * version, and rename the temp file to the old name.
  152.      */
  153.     if (use_temp) {
  154.         remove(argv[1]);
  155.         rename(TMPFILE, argv[1]);
  156.     }
  157.  
  158.     exit(0);
  159. }
  160.  
  161. dofile()
  162. {
  163.     if (!readline())
  164.         return;
  165.  
  166.     while (dofunc())
  167.         ;
  168. }
  169.