home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / northc_384.lzh / NorthC / Example2.LZH / top / main.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  4KB  |  170 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.  
  13. FILE    *ifp, *ofp;        /* input/output file pointers */
  14.  
  15. long    _STKSIZ = 32768L;    /* need mucho stack for recursion */
  16.  
  17. /*
  18.  * Options 
  19.  */
  20. bool    debug   = FALSE;
  21. bool    verbose = FALSE;
  22. bool    do_brev = TRUE;        /* branch reversals enabled */
  23. bool    do_peep = TRUE;        /* peephole optimizations enabled */
  24. bool    do_dflow= TRUE;        /* enable data-flow analysis */
  25.  
  26. /*
  27.  * Optimization statistics (use -v to print)
  28.  */
  29. int    s_bdel = 0;        /* branches deleted */
  30. int    s_badd = 0;        /* branches added */
  31. int    s_brev = 0;        /* branch reversals */
  32. int    s_peep1 = 0;        /* 1 instruction peephole changes */
  33. int    s_peep2 = 0;        /* 2 instruction peephole changes */
  34. int    s_peep3 = 0;        /* 3 instruction peephole changes */
  35. int    s_idel = 0;        /* instructions deleted */
  36.  
  37. #ifdef NORTHC
  38. char tempfile[L_tmpnam];
  39. #else
  40. #define    TMPFILE    "top_tmp.$$$"    /* temporary file name */
  41. #endif
  42.  
  43. int    use_temp = FALSE;    /* using temporary file */
  44.  
  45. char    *Version =
  46. "top Version 1.01  Copyright (c) 1988 by Sozobon, Limited.";
  47.  
  48. usage()
  49. {
  50.     fprintf(stderr, "usage: top [-dvfbp] infile [outfile]\n");
  51.     exit(EXIT_FAILURE);
  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 'f':
  74.                 do_dflow = FALSE;
  75.                 break;
  76.             case 'v':
  77.                 fprintf(stderr, "%s\n", Version);
  78.                 verbose = TRUE;
  79.                 break;
  80.             default:
  81.                 usage();
  82.                 break;
  83.             }
  84.         }
  85.         argv++;
  86.         argc--;
  87.     }
  88.  
  89.     if (argc > 3)
  90.         usage();
  91.  
  92.     if (argc > 1) {
  93.         if (strcmp(argv[1], "-") == 0)
  94.             ifp = stdin;
  95.         else if ((ifp = fopen(argv[1], "r")) == NULL) {
  96.             fprintf(stderr, "top: can't open input file '%s'\n",
  97.                 argv[1]);
  98.             exit(EXIT_FAILURE);
  99.         }
  100.         if (argc > 2) {
  101.             if (strcmp(argv[2], "-") == 0)
  102.                 ofp = stdout;
  103.             else if ((ofp = fopen(argv[2], "w")) == NULL) {
  104.                 fprintf(stderr, "top: can't open output file '%s'\n",
  105.                     argv[2]);
  106.                 exit(EXIT_FAILURE);
  107.             }
  108.         } else {
  109. #ifdef NORTHC
  110.             tmpnam(tempfile);
  111.             if ((ofp = fopen(tempfile, "w")) == NULL) {
  112. #else
  113.             if ((ofp = fopen(TMPFILE, "w")) == NULL) {
  114. #endif
  115.                 fprintf(stderr, "top: can't create temp file\n");
  116.                 exit(EXIT_FAILURE);
  117.             }
  118.             use_temp = TRUE;
  119.         }
  120.     } else
  121.         usage();
  122.  
  123.     dofile();
  124.  
  125.     if (verbose) {
  126.         if (do_peep) {
  127.             fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  128.             fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  129.             fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  130.             fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  131.         }
  132.         if (do_brev)
  133.             fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  134.         fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  135.     }
  136.  
  137. #ifdef NORTHC
  138.     dumpall();
  139. #endif
  140.     /*
  141.      * If we're overwriting the original file, remove the old
  142.      * version, and rename the temp file to the old name.
  143.      */
  144.     if (use_temp) {
  145. #ifdef NORTHC
  146.         fclose(ifp);
  147.         remove(argv[1]);
  148.         fclose(ofp);
  149.         if(copy(tempfile,argv[1])<0)
  150.                     printf("copy from \"%s\" to \"%s\" failed\n",
  151.                            tempfile,argv[1]);
  152.         remove(tempfile);
  153. #else
  154.         remove(argv[1]);
  155.         rename(TMPFILE, argv[1]);
  156. #endif
  157.     }
  158.  
  159.     exit(0);
  160. }
  161.  
  162. dofile()
  163. {
  164.     if (!readline())
  165.         return;
  166.  
  167.     while (dofunc())
  168.         ;
  169. }
  170.