scc

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

commit 296d4035d72e6c58a6ec532468a73eaeda4e42b5
parent 827b4cdaa90cabaa1ee149a79ceb4cf7c7813f47
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 21 Jan 2016 10:45:54 +0100

Add stub of cc2

This is the begin of the back end version 2.

Diffstat:
MMakefile | 3++-
Acc2/Makefile | 22++++++++++++++++++++++
Acc2/cc2.h | 31+++++++++++++++++++++++++++++++
Acc2/cgen.c | 12++++++++++++
Acc2/code.c | 7+++++++
Acc2/generror | 12++++++++++++
Acc2/main.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Acc2/optm.c | 7+++++++
Acc2/parser.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acc2/peep.c | 7+++++++
10 files changed, 208 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -5,7 +5,8 @@ include config.mk SUBDIRS = \ lib \ - cc1 + cc1 \ + cc2 all clean: @echo scc build options: diff --git a/cc2/Makefile b/cc2/Makefile @@ -0,0 +1,22 @@ +.POSIX: + +include ../config.mk + +OBJS = main.o parser.o code.o optm.o peep.o cgen.o + +all: cc2 + + +$(OBJS): cc2.h +main.o: error.h + +error.h: cc2.h + rm -f $@; trap 'rm -f $$$$.h' EXIT INT QUIT + awk -f generror cc2.h > $$$$.h && mv $$$$.h $@ + +cc2: $(OBJS) ../lib/libcc.a + $(CC) $(LDFLAGS) $(OBJS) ../lib/libcc.a -o $@ + +clean: + rm -f $(OBJS) + rm -f cc2 error.h diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -0,0 +1,31 @@ + +enum nerrors { + ELNLINE, /* line too long */ + EFERROR, /* error reading from file:%s*/ + ENUMERR +}; + +typedef struct node Node; + +struct node { + unsigned char op; +}; + +/* main.c */ +extern void error(unsigned nerror, ...); + +/* parse.c */ +extern void parse(void); + +/* optm.c */ +extern void optimize(void); + +/* cgen.c */ +extern void addable(void); +extern void generate(void); + +/* peep.c */ +extern void peephole(void); + +/* code.c */ +extern void writeout(void); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -0,0 +1,12 @@ + +#include "cc2.h" + +void +generate(void) +{ +} + +void +addable(void) +{ +} diff --git a/cc2/code.c b/cc2/code.c @@ -0,0 +1,7 @@ + +#include "cc2.h" + +void +writeout(void) +{ +} diff --git a/cc2/generror b/cc2/generror @@ -0,0 +1,12 @@ + +BEGIN { + print "char *errlist[] = {" +} +/^enum nerrors \{/ {inhome = 1} +inhome && /E[A-Z]*, / {sub(/,/, "", $1) + printf("\t[%s] = \"", $1) + for (i = 3; i < NF-1; ++i) + printf("%s ", $i) + printf("%s\",\n", $(NF-1));} +inhome && /^}/ {print "};" ; inhome = 0} + diff --git a/cc2/main.c b/cc2/main.c @@ -0,0 +1,46 @@ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "cc2.h" +#include "error.h" + +void +error(unsigned nerror, ...) +{ + va_list va; + va_start(va, nerror); + vfprintf(stderr, errlist[nerror], va); + va_end(va); + putc('\n', stderr); + exit(1); +} + +static int +moreinput(void) +{ + int c; + +repeat: + if (feof(stdin)) + return 0; + if ((c = getchar()) == '\n' || c == EOF) + goto repeat; + ungetc(c, stdin); + return 1; +} + +int +main(void) +{ + while (moreinput()) { + parse(); + optimize(); + addable(); + generate(); + peephole(); + writeout(); + } + return 0; +} diff --git a/cc2/optm.c b/cc2/optm.c @@ -0,0 +1,7 @@ + +#include "cc2.h" + +void +optimize(void) +{ +} diff --git a/cc2/parser.c b/cc2/parser.c @@ -0,0 +1,62 @@ + +#include <errno.h> +#include <stdio.h> +#include <string.h> + +#include "cc2.h" + +#define MAXLINE 200 + +static void +push(Node * np) +{ +} + +static Node * +pop(void) +{ +} + +static void +expr(char *tok) +{ +} + +static void +stmt(Node *np) +{ +} + +static void +decl(char *tok) +{ +} + +void +parse(void) +{ + char line[MAXLINE]; + size_t len; + + for (;;) { + if (fgets(line, sizeof(line), stdin)) + break; + if ((len = strlen(line)) == 0 || line[0] == '\n') + continue; + if (line[len-1] != '\n') + error(ELNLINE); + line[len-1] = '\0'; + switch (*line) { + case '\t': + expr(strtok(line, "\t")); + stmt(pop()); + break; + default: + decl(strtok(line, "\t")); + break; + } + } + + if (ferror(stdin)) + error(EFERROR, strerror(errno)); +} diff --git a/cc2/peep.c b/cc2/peep.c @@ -0,0 +1,7 @@ + +#include "cc2.h" + +void +peephole(void) +{ +}