scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

main.c (1019B)


      1 static char sccsid[] = "@(#) ./cc2/main.c";
      2 
      3 #include <errno.h>
      4 #include <stdarg.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 #include <scc/arg.h>
     10 #include <scc/scc.h>
     11 #include "cc2.h"
     12 #include "error.h"
     13 
     14 char *argv0;
     15 
     16 void
     17 error(unsigned nerror, ...)
     18 {
     19 	va_list va;
     20 	va_start(va, nerror);
     21 	vfprintf(stderr, errlist[nerror], va);
     22 	va_end(va);
     23 	putc('\n', stderr);
     24 	exit(1);
     25 }
     26 
     27 static int
     28 moreinput(void)
     29 {
     30 	int c;
     31 
     32 repeat:
     33 	if (feof(stdin))
     34 		return 0;
     35 	if ((c = getchar()) == '\n' || c == EOF)
     36 		goto repeat;
     37 	ungetc(c, stdin);
     38 	return 1;
     39 }
     40 
     41 static void
     42 usage(void)
     43 {
     44 	fputs("usage: cc2 [irfile]\n", stderr);
     45 	exit(1);
     46 }
     47 
     48 int
     49 main(int argc, char *argv[])
     50 {
     51 	ARGBEGIN {
     52 	default:
     53 		usage();
     54 	} ARGEND
     55 
     56 	if (argv[0] && !freopen(argv[0], "r", stdin))
     57 		die("cc2: %s: %s", argv[0], strerror(errno));
     58 
     59 	while (moreinput()) {
     60 		parse();
     61 		apply(optm_ind);
     62 		apply(optm_dep);
     63 		apply(sethi);
     64 		apply(cgen);
     65 		getbblocks();  /* TODO: run apply over asm ins too */
     66 		peephole();
     67 		writeout();
     68 	}
     69 	return 0;
     70 }