ref: 296d4035d72e6c58a6ec532468a73eaeda4e42b5
parent: 827b4cdaa90cabaa1ee149a79ceb4cf7c7813f47
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jan 21 05:45:54 EST 2016
Add stub of cc2 This is the begin of the back end version 2.
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,8 @@
SUBDIRS = \
lib \
- cc1
+ cc1 \
+ cc2
all clean:
@echo scc build options:
--- /dev/null
+++ b/cc2/Makefile
@@ -1,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
--- /dev/null
+++ b/cc2/cc2.h
@@ -1,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);
--- /dev/null
+++ b/cc2/cgen.c
@@ -1,0 +1,12 @@
+
+#include "cc2.h"
+
+void
+generate(void)
+{
+}
+
+void
+addable(void)
+{
+}
--- /dev/null
+++ b/cc2/code.c
@@ -1,0 +1,7 @@
+
+#include "cc2.h"
+
+void
+writeout(void)
+{
+}
--- /dev/null
+++ b/cc2/generror
@@ -1,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}
+
--- /dev/null
+++ b/cc2/main.c
@@ -1,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;
+}
--- /dev/null
+++ b/cc2/optm.c
@@ -1,0 +1,7 @@
+
+#include "cc2.h"
+
+void
+optimize(void)
+{
+}
--- /dev/null
+++ b/cc2/parser.c
@@ -1,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));
+}
--- /dev/null
+++ b/cc2/peep.c
@@ -1,0 +1,7 @@
+
+#include "cc2.h"
+
+void
+peephole(void)
+{
+}