home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / top / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-17  |  4.6 KB  |  222 lines

  1. /* Copyright (c) 1988 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. #include <fcntl.h>    /*Jal*/
  13. #include <errno.h>
  14.     
  15. /*
  16.  * Amiga changes by Jeff Lydiatt marked by Jal.
  17.  */
  18.  
  19. #ifdef AZTEC_C /*Jal*/
  20. #   define remove(x) unlink(x)
  21. #endif
  22.  
  23. FILE    *ifp, *ofp;        /* input/output file pointers */
  24.  
  25. long    _STKSIZ = 32768L;    /* need mucho stack for recursion */
  26.  
  27. /*
  28.  * Options 
  29.  */
  30. bool    debug   = FALSE;
  31. bool    do_brev = TRUE;        /* branch reversals enabled */
  32. bool    do_peep = TRUE;        /* peephole optimizations enabled */
  33. bool    do_dflow= TRUE;        /* enable data-flow analysis */
  34. bool    verbose = FALSE;
  35. bool    noopt    = FALSE;    /* Jal - nooptimize, just coalesce */
  36.  
  37. /*
  38.  * Optimization statistics (use -v to print)
  39.  */
  40. int    s_bdel = 0;        /* branches deleted */
  41. int    s_badd = 0;        /* branches added */
  42. int    s_brev = 0;        /* branch reversals */
  43. int    s_peep1 = 0;        /* 1 instruction peephole changes */
  44. int    s_peep2 = 0;        /* 2 instruction peephole changes */
  45. int    s_peep3 = 0;        /* 3 instruction peephole changes */
  46. int    s_idel = 0;        /* instructions deleted */
  47.  
  48. #ifdef AZTEC_C  /*Jal*/
  49. #define TMPFILE tmp
  50. #else
  51. #define    TMPFILE    "top_tmp.$$$"    /* temporary file name */
  52. #endif
  53.  
  54. int    use_temp = FALSE;    /* using temporary file */
  55.  
  56. char    *Version =
  57. "top Version 1.01  Copyright (c) 1988 by Sozobon, Limited.";
  58.  
  59. usage()
  60. {
  61.     fprintf(stderr, "usage: top [-dvfbp] infile [outfile]\n");
  62.     exit(1);
  63. }
  64.  
  65. main(argc, argv)
  66. int    argc;
  67. char    *argv[];
  68. {
  69.     FILE    *fopen();
  70.     register char    *s;
  71. #ifdef AZTEC_C
  72.     /*
  73.      *Jal - Used to create temporary file ok in multitasking env.
  74.      */
  75.      char tmp[L_tmpnam+4];
  76.     char *tmpnam();
  77.     int copy();
  78. #endif
  79.  
  80.     while (argc > 1 && argv[1][0] == '-') {
  81.         for (s = &argv[1][1]; *s ;s++) {
  82.             switch (*s) {
  83.             case 'a':
  84.                 noopt = TRUE;
  85.                 break;
  86.             case 'd':
  87.                 debug = TRUE;
  88.                 break;
  89.             case 'b':
  90.                 do_brev = FALSE;
  91.                 break;
  92.             case 'p':
  93.                 do_peep = FALSE;
  94.                 break;
  95.             case 'f':
  96.                 do_dflow = FALSE;
  97.                 break;
  98.             case 'v':
  99.                 fprintf(stderr, "%s\n", Version);
  100.                 verbose = TRUE;
  101.                 break;
  102.             default:
  103.                 usage();
  104.                 break;
  105.             }
  106.         }
  107.         argv++;
  108.         argc--;
  109.     }
  110.  
  111.     if (argc > 3)
  112.         usage();
  113.  
  114.     if (argc > 1) {
  115.         if (strcmp(argv[1], "-") == 0)
  116.             ifp = stdin;
  117.         else if ((ifp = fopen(argv[1], "r")) == NULL) {
  118.             fprintf(stderr, "top: can't open input file '%s'\n",
  119.                 argv[1]);
  120.             exit(1);
  121.         }
  122.         if (argc > 2) {
  123.             if (strcmp(argv[2], "-") == 0)
  124.                 ofp = stdout;
  125.             else if ((ofp = fopen(argv[2], "w")) == NULL) {
  126.                 fprintf(stderr, "top: can't open output file '%s'\n",
  127.                     argv[2]);
  128.                 exit(1);
  129.             }
  130.         } else {
  131. /*
  132.  *Jal-Create and use a unique temporary file.
  133.  */
  134. #ifdef AZTEC_C
  135.             (void)strcpy(tmp,"ram:");
  136.             (void)tmpnam(&tmp[4]);
  137. #endif
  138.             if ((ofp = fopen(TMPFILE, "w")) == NULL) {
  139.                 fprintf(stderr, "top: can't create temp file\n");
  140.                 exit(1);
  141.             }
  142.             use_temp = TRUE;
  143.         }
  144.     } else
  145.         usage();
  146.  
  147.     /*
  148.      * Jal - Coalesce CODE - output just one code section here.
  149.      */
  150.     fputs("\tCODE\tCODE\n", ofp);
  151.     dofile();
  152.     dumpall();
  153.  
  154.     if (verbose) {
  155.         if (do_peep) {
  156.             fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  157.             fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  158.             fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  159.             fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  160.         }
  161.         if (do_brev)
  162.             fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  163.         fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  164.     }
  165.  
  166.     /*
  167.      * If we're overwriting the original file, remove the old
  168.      * version, and rename the temp file to the old name.
  169.      */
  170.     (void)fclose(ifp);    /*Jal*/
  171.     (void)fclose(ofp);    /*Jal*/
  172.     
  173.     if (use_temp) { /*Jal-can't rename across devices. */
  174.         remove(argv[1]);
  175.         if ( rename(TMPFILE, argv[1]) ) { /*Jal*/
  176.             if ( copy(TMPFILE, argv[1]) )
  177.                 fprintf(stderr, "can't rename %s\n", TMPFILE);
  178.             else
  179.                 remove(TMPFILE);
  180.         }
  181.     }
  182.  
  183.     exit(0);
  184. }
  185.  
  186. static int
  187. copy(fname,tname) /*Jal*/
  188. char *fname, *tname;
  189. {
  190.     register int i, o, l;
  191.     char buf[1024];
  192.  
  193.     if ( (i = open(fname, O_RDONLY)) == -1 )
  194.         return -1;
  195.  
  196.     if ( (o = open(tname, O_CREAT | O_WRONLY | O_EXCL)) == -1 ){
  197.         close(i);
  198.         return -1;
  199.     }
  200.  
  201.     l = read(i, buf, sizeof(buf));
  202.     while (l > 0 ) {
  203.         l = write( o, buf, l);
  204.         if ( l > 0 )
  205.             l = read(i, buf, sizeof(buf));
  206.     }
  207.  
  208.     close(i);
  209.     close(o);
  210.  
  211.     return l;
  212. }
  213.  
  214. dofile()
  215. {
  216.     if (!readline())
  217.         return;
  218.  
  219.     while (dofunc())
  220.         ;
  221. }
  222.