scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

main.c (860B)


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