scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

main.c (891B)


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