home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / language / sozobon2 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  3.9 KB  |  165 lines

  1. /* Copyright (c) 1988,1989 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    verbose = FALSE;
  28.  
  29. /*
  30.  * Optimization statistics (use -v to print)
  31.  */
  32. int    s_bdel = 0;        /* branches deleted */
  33. int    s_badd = 0;        /* branches added */
  34. int    s_brev = 0;        /* branch reversals */
  35. int    s_lrot = 0;        /* loop rotations */
  36. int    s_peep1 = 0;        /* 1 instruction peephole changes */
  37. int    s_peep2 = 0;        /* 2 instruction peephole changes */
  38. int    s_peep3 = 0;        /* 3 instruction peephole changes */
  39. int    s_idel = 0;        /* instructions deleted */
  40. int    s_reg = 0;        /* variables "registerized" */
  41.  
  42. #define    TMPFILE    "top_tmp.$$$"    /* temporary file name */
  43. int    use_temp = FALSE;    /* using temporary file */
  44.  
  45. char    *Version =
  46. "top Version 1.20  Copyright (c) 1988,1989 by Sozobon, Limited.";
  47.  
  48. usage()
  49. {
  50.     fprintf(stderr, "usage: top [-dvblpr] infile [outfile]\n");
  51.     exit(1);
  52. }
  53.  
  54. main(argc, argv)
  55. int    argc;
  56. char    *argv[];
  57. {
  58.     FILE    *fopen();
  59.     register char    *s;
  60.  
  61.     while (argc > 1 && argv[1][0] == '-') {
  62.         for (s = &argv[1][1]; *s ;s++) {
  63.             switch (*s) {
  64.             case 'd':
  65.                 debug = TRUE;
  66.                 break;
  67.             case 'b':
  68.                 do_brev = FALSE;
  69.                 break;
  70.             case 'p':
  71.                 do_peep = FALSE;
  72.                 break;
  73.             case 'r':
  74.                 do_regs = FALSE;
  75.                 break;
  76.             case 'l':
  77.                 do_lrot = FALSE;
  78.                 break;
  79.             case 'v':
  80.                 fprintf(stderr, "%s\n", Version);
  81.                 verbose = TRUE;
  82.                 break;
  83.             case 'O':
  84.                 /*
  85.                  * When options are received from 'cc' they
  86.                  * look like "-Oxxx", so just ignore the 'O'.
  87.                  */
  88.                 break;
  89.             default:
  90.                 usage();
  91.                 break;
  92.             }
  93.         }
  94.         argv++;
  95.         argc--;
  96.     }
  97.  
  98.     if (argc > 3)
  99.         usage();
  100.  
  101.     if (argc > 1) {
  102.         if (strcmp(argv[1], "-") == 0)
  103.             ifp = stdin;
  104.         else if ((ifp = fopen(argv[1], "r")) == NULL) {
  105.             fprintf(stderr, "top: can't open input file '%s'\n",
  106.                 argv[1]);
  107.             exit(1);
  108.         }
  109.         if (argc > 2) {
  110.             if (strcmp(argv[2], "-") == 0)
  111.                 ofp = stdout;
  112.             else if ((ofp = fopen(argv[2], "w")) == NULL) {
  113.                 fprintf(stderr, "top: can't open output file '%s'\n",
  114.                     argv[2]);
  115.                 exit(1);
  116.             }
  117.         } else {
  118.             if ((ofp = fopen(TMPFILE, "w")) == NULL) {
  119.                 fprintf(stderr, "top: can't create temp file\n");
  120.                 exit(1);
  121.             }
  122.             use_temp = TRUE;
  123.         }
  124.     } else
  125.         usage();
  126.  
  127.     dofile();
  128.  
  129.     if (verbose) {
  130.         if (do_peep) {
  131.             fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  132.             fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  133.             fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  134.             fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  135.         }
  136.         if (do_regs)
  137.             fprintf(stderr, "Variables registered: %4d\n", s_reg);
  138.         if (do_lrot)
  139.             fprintf(stderr, "Loop rotations      : %4d\n", s_lrot);
  140.         if (do_brev)
  141.             fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  142.         fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  143.     }
  144.  
  145.     /*
  146.      * If we're overwriting the original file, remove the old
  147.      * version, and rename the temp file to the old name.
  148.      */
  149.     if (use_temp) {
  150.         remove(argv[1]);
  151.         rename(TMPFILE, argv[1]);
  152.     }
  153.  
  154.     exit(0);
  155. }
  156.  
  157. dofile()
  158. {
  159.     if (!readline())
  160.         return;
  161.  
  162.     while (dofunc())
  163.         ;
  164. }
  165.