scc

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

main.c (974B)


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