home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / jas / main.c < prev    next >
C/C++ Source or Header  |  1991-02-22  |  4KB  |  224 lines

  1.  
  2. /*
  3.  * Copyright (c) 1988,1991 by Sozobon, Limited.  Author: Joseph M Treat
  4.  *
  5.  * Permission is granted to anyone to use this software for any purpose
  6.  * on any computer system, and to redistribute it freely, with the
  7.  * following restrictions:
  8.  * 1) No charge may be made other than reasonable charges for reproduction.
  9.  * 2) Modified versions must be clearly marked as such.
  10.  * 3) The authors are not responsible for any harmful consequences
  11.  *    of using this software, even if they result from defects in it.
  12.  */
  13.  
  14. /*
  15.  * Version 1.1 contains fixes for referencing the usc = calld ccr registers
  16.  * and not exiting if the -V switch is specified. format specifications for 
  17.  * move sr, ?? and usp moves were incorrect -- fixed. added aliasing for 
  18.  * immediate operations to sr and ccr.
  19.  *
  20.  * Version 1.2 contains changes for minix: input compatibility with ack, 
  21.  * including short absolute addressing mode and explicit pc-relative addressing
  22.  * mode involving text labels, and smarter use of memory.
  23.  */
  24.  
  25. #include "jas.h"
  26.  
  27. #define VERSION    2
  28. #define RELEASE 0
  29.  
  30. jmp_buf err_buf;
  31.  
  32. extern int sawerror; 
  33.  
  34. char *ofile = (char *) 0;
  35. char *ifile = (char *) 0;
  36. int Optimize = 1;
  37. int Lflag = 0;
  38. int flag8 = 0;
  39. extern FILE *yyin;
  40.  
  41. main(argc,argv)
  42.     int argc;
  43.     char *argv[];
  44. {
  45.     extern FILE *freopen();
  46.  
  47.     if ( setjmp( err_buf ) ) {
  48.         unlink( ofile );
  49.         exit( 1 );
  50.     }
  51.  
  52.     setflags( argc, argv );
  53.  
  54.     if ( freopen( ifile, "r", stdin ) == (FILE *) NULL )
  55.         error( 0, "can't open source file for reading" );
  56.  
  57. #if defined(MINIX) || defined(UNIXHOST)
  58.     if ( freopen( ofile, "w", stdout ) == (FILE *) NULL )
  59.         error( 0, "can't open object file for writing" );
  60. #else
  61.     if ( freopen( ofile, "bw", stdout ) == (FILE *) NULL )
  62.         error( 0, "can't open object file for writing" );
  63. #endif
  64.  
  65.     yyin = stdin;
  66.  
  67.     aspass1();
  68.  
  69.     if ( sawerror )
  70.         unlink( ofile );
  71.  
  72.     exit( sawerror );
  73. }
  74.  
  75. setflags(ac,av)
  76.     int ac;
  77.     char *av[];
  78. {
  79.     int errflag = 0, i;
  80.     int Vflag = 0;
  81.  
  82.     for ( i = 1; i < ac; i++ ) {
  83.         if ( *av[i] == '-' ) {
  84.             switch ( av[i][1] ) {
  85.             case 'o':
  86.                 ofile = av[++i];
  87.                 break;
  88.             case 'N':
  89.                 Optimize = 0;
  90.                 break;
  91.             case 'V':
  92.                 Vflag = 1;
  93.                 break;
  94.             case 'L':
  95.                 Lflag = 1;
  96.                 if ( av[i][2] )
  97.                     Lflag = av[i][2] - '0';
  98.                 break;
  99.             case 's':
  100.                 i++;
  101.             case 'l':
  102.             case 'u':
  103.                 break;
  104.             case '8':
  105.                 flag8 = 1;
  106.                 break;
  107.             default:
  108.                 errflag = 1;
  109.                 break;
  110.             }
  111.         } else if (! ifile ) {
  112.             ifile = av[i];
  113.         } else {
  114.             errflag = 1;
  115.         }
  116.     }
  117.  
  118.     if ( Vflag ) {
  119.         fprintf( stderr, "Sozobon Assembler, Version %d.%d\n",
  120.                             VERSION, RELEASE );
  121.         fprintf( stderr, "Copyright (c) 1988,1991 by Sozobon, Limited\n" );
  122.     }
  123.  
  124.     if (! ifile)
  125.         errflag = 1;
  126.  
  127.     if ( errflag ) {
  128.         fprintf( stderr, "usage: as [-N] source [-o object]\n" );
  129.         exit( 1 );
  130.     }
  131.  
  132.     if (! ofile ) {
  133.         char buf[32];
  134.         char *ip, *op;
  135.  
  136.         for ( op = buf, ip = ifile; *op++ = *ip; ip++ ) {
  137.             if ( *ip == '/' )
  138.                 op = buf;
  139.         }
  140.  
  141.         if ( op[-2] == 's' && op[-3] == '.' ) {
  142.             op[-2] = 'o';
  143.         } else {
  144.             fprintf(stderr, "usage: as [-N] source [-o object]\n" );
  145.             exit( 1 );
  146.         }
  147.         ofile = STRCPY(buf);
  148.     }
  149. }
  150.  
  151. error(n, s)
  152.     int n;
  153.     char *s;
  154. {
  155.     warn( n, s );
  156.     longjmp( err_buf, 1 );
  157. }
  158.  
  159. warn(n, s)
  160.     int n;
  161.     char *s;
  162. {
  163.     fprintf(stderr,"jas: ");
  164.     if ( n )
  165.         fprintf(stderr,"line %d: ", n);
  166.     fprintf(stderr,"( %s )\n", s);
  167.     sawerror = 1;
  168. }
  169.  
  170. char *
  171. allocate(size)
  172.     unsigned size;
  173. {
  174.     register char *loc;
  175.     extern char *calloc();
  176.  
  177.     loc = calloc( size, 1 );
  178. #ifdef MEM_DEBUG
  179.     fprintf( stderr, "alloc(%u bytes) => %lx\n", size, loc );
  180. #endif
  181.     if ( loc ) {
  182.         return loc;
  183.     }
  184.     error( 0, "out of memory" );
  185.     return (char *) NULL;
  186. }
  187.  
  188. char *
  189. myreallocate(ptr, size)
  190.     char *ptr;
  191.     unsigned size;
  192. {
  193.     register char *loc;
  194.     extern char *realloc();
  195.  
  196.     loc = realloc( ptr, size );
  197.     if ( loc ) {
  198.         return loc;
  199.     }
  200.     error( 0, "out of memory" );
  201.     return (char *) NULL;
  202. }
  203.  
  204. output( buffer, size, nitems )
  205.     char *buffer;
  206.     int size, nitems; 
  207. {
  208.     if ( fwrite( buffer, size, nitems, stdout ) != nitems )
  209.         error( 0, "trouble writing object file" );
  210. }
  211.  
  212. #ifdef free
  213. #    undef free
  214. #endif
  215.  
  216. my_free( x )
  217.     char *x;
  218. {
  219. #ifdef MEM_DEBUG
  220. fprintf( stderr, "free( %lx )\n", x );
  221. #endif
  222.     free( x );
  223. }
  224.