[hackers] [scc] [cc2] Emit initializers as soon as posible || Roberto E. Vargas Caballero
commit b8ea542ae6db4934ceb3d3a69a7884830604df31
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sat Jan 23 21:28:09 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sat Jan 23 21:28:09 2016 +0100
[cc2] Emit initializers as soon as posible
Initializers are not important for the code generation,
so we don't waste our time with them and emit them as
soon as possible.
diff --git a/cc1/arch/i386-sysv/arch.h b/cc1/arch/i386-sysv/arch.h
new file mode 100644
index 0000000..82d6e9b
--- /dev/null
+++ b/cc1/arch/i386-sysv/arch.h
_AT_@ -0,0 +1,33 @@
+
+#define RANK_BOOL 0
+#define RANK_SCHAR 1
+#define RANK_UCHAR 1
+#define RANK_CHAR 1
+#define RANK_SHORT 2
+#define RANK_USHORT 2
+#define RANK_INT 3
+#define RANK_UINT 3
+#define RANK_LONG 4
+#define RANK_ULONG 4
+#define RANK_LLONG 5
+#define RANK_ULLONG 5
+#define RANK_FLOAT 6
+#define RANK_DOUBLE 7
+#define RANK_LDOUBLE 8
+
+#define TINT long long
+#define TUINT unsigned long long
+#define TFLOAT double
+
+#define L_SCHAR L_INT8
+#define L_UCHAR L_UINT8
+#define L_CHAR L_INT8
+#define L_SHORT L_INT16
+#define L_USHORT L_UINT16
+#define L_INT L_INT32
+#define L_UINT L_UINT32
+#define L_LONG L_INT32
+#define L_ULONG L_UINT32
+#define L_LLONG L_INT64
+#define L_ULLONG L_UINT64
+#define L_ENUM L_INT
diff --git a/cc1/arch/i386/arch.h b/cc1/arch/i386/arch.h
deleted file mode 100644
index 82d6e9b..0000000
--- a/cc1/arch/i386/arch.h
+++ /dev/null
_AT_@ -1,33 +0,0 @@
-
-#define RANK_BOOL 0
-#define RANK_SCHAR 1
-#define RANK_UCHAR 1
-#define RANK_CHAR 1
-#define RANK_SHORT 2
-#define RANK_USHORT 2
-#define RANK_INT 3
-#define RANK_UINT 3
-#define RANK_LONG 4
-#define RANK_ULONG 4
-#define RANK_LLONG 5
-#define RANK_ULLONG 5
-#define RANK_FLOAT 6
-#define RANK_DOUBLE 7
-#define RANK_LDOUBLE 8
-
-#define TINT long long
-#define TUINT unsigned long long
-#define TFLOAT double
-
-#define L_SCHAR L_INT8
-#define L_UCHAR L_UINT8
-#define L_CHAR L_INT8
-#define L_SHORT L_INT16
-#define L_USHORT L_UINT16
-#define L_INT L_INT32
-#define L_UINT L_UINT32
-#define L_LONG L_INT32
-#define L_ULONG L_UINT32
-#define L_LLONG L_INT64
-#define L_ULLONG L_UINT64
-#define L_ENUM L_INT
diff --git a/cc2/Makefile b/cc2/Makefile
index 0b30c73..630b294 100644
--- a/cc2/Makefile
+++ b/cc2/Makefile
_AT_@ -2,8 +2,8 @@
include ../config.mk
-OBJS = main.o parser.o code.o optm.o peep.o cgen.o \
- symbol.o node.o arch/$(ARCH)/types.o
+OBJS = main.o parser.o optm.o peep.o symbol.o node.o \
+ arch/$(ARCH)/code.o arch/$(ARCH)/cgen.o arch/$(ARCH)/types.o
all: cc2
diff --git a/cc2/arch/i386-sysv/code.c b/cc2/arch/i386-sysv/code.c
new file mode 100644
index 0000000..2159457
--- /dev/null
+++ b/cc2/arch/i386-sysv/code.c
_AT_@ -0,0 +1,13 @@
+
+#include "arch.h"
+#include "../../cc2.h"
+
+void
+emit(Node *np)
+{
+}
+
+void
+writeout(void)
+{
+}
diff --git a/cc2/arch/z80/cgen.c b/cc2/arch/z80/cgen.c
new file mode 100644
index 0000000..6b96df7
--- /dev/null
+++ b/cc2/arch/z80/cgen.c
_AT_@ -0,0 +1,13 @@
+
+#include "arch.h"
+#include "../../cc2.h"
+
+void
+generate(void)
+{
+}
+
+void
+addable(void)
+{
+}
diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c
new file mode 100644
index 0000000..b140b03
--- /dev/null
+++ b/cc2/arch/z80/code.c
_AT_@ -0,0 +1,106 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "arch.h"
+#include "../../cc2.h"
+
+enum segment {
+ CODESEG,
+ DATASEG,
+ BSSSEG,
+ NOSEG
+};
+
+static int curseg = NOSEG;
+
+static void
+segment(int seg)
+{
+ static char *txt[] = {
+ [CODESEG] = "\tCSEG\n",
+ [DATASEG] = "\tDSEG\n",
+ [BSSSEG] = "\tXSEG\n",
+ };
+
+ if (seg == curseg)
+ return;
+ fputs(txt[seg], stdout);
+ curseg = seg;
+}
+
+void
+code(int op, Node *to, Node *from)
+{
+}
+
+static void
+emitsym(Symbol *sym)
+{
+ /*In z80 we can ignore the aligment */
+ if (sym->type.flags & STRF) {
+ fputs(sym->u.s, stdout);
+ free(sym->u.s);
+ sym->u.s = NULL;
+ } else {
+ switch (sym->type.size) {
+ case 1:
+ printf("%02X", (int) (sym->u.i & 0xFF));
+ break;
+ case 2:
+ printf("%04X", (int) (sym->u.i & 0xFFFF));
+ break;
+ case 4:
+ printf("%08X", (long) (sym->u.i & 0xFFFFFFFF));
+ break;
+ default:
+ abort();
+ }
+ }
+}
+
+static void
+emittree(Node *np)
+{
+ if (!np)
+ return;
+ if (np->op == OSYM) {
+ emitsym(np->sym);
+ } else {
+ emit(np->left);
+ printf(" %c ", np->op);
+ emit(np->right);
+ }
+}
+
+void
+emit(Node *np)
+{
+ char *s;
+
+ /*
+ * In z80 we can ignore the alignment
+ */
+ segment(DATASEG);
+ switch (np->type.size) {
+ case 1:
+ s = "\tDB\t";
+ break;
+ case 2:
+ s = "\tDW\t";
+ break;
+ case 4:
+ s = "\tDD\t";
+ break;
+ default:
+ s = "\tDS\t";
+ break;
+ }
+ fputs(s, stdout);
+ emittree(np);
+}
+
+void
+writeout(void)
+{
+}
diff --git a/cc2/cc2.h b/cc2/cc2.h
index ae9f166..6af98df 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -1,12 +1,13 @@
enum tflags {
- SIGNF = 1,
- INTF = 2,
- DEFTYP = 4,
- STRUCTF = 8,
- UNIONF = 16,
- FUNCF = 32,
- ARYF = 64
+ SIGNF = 1,
+ INTF = 2,
+ DEFTYP = 4,
+ STRUCTF = 8,
+ UNIONF = 16,
+ FUNCF = 32,
+ ARYF = 64,
+ STRF = 128
};
enum op {
_AT_@ -150,11 +151,13 @@ extern void generate(void);
extern void peephole(void);
/* code.c */
+extern void emit(Node *np);
extern void writeout(void);
/* node.c */
extern void cleannodes(void);
extern void delnode(Node *np);
+extern void deltree(Node *np);
extern Node *newnode(void);
/* symbol.c */
diff --git a/cc2/cgen.c b/cc2/cgen.c
deleted file mode 100644
index 0e8d43b..0000000
--- a/cc2/cgen.c
+++ /dev/null
_AT_@ -1,13 +0,0 @@
-
-#include "arch.h"
-#include "cc2.h"
-
-void
-generate(void)
-{
-}
-
-void
-addable(void)
-{
-}
diff --git a/cc2/code.c b/cc2/code.c
deleted file mode 100644
index 26192c4..0000000
--- a/cc2/code.c
+++ /dev/null
_AT_@ -1,8 +0,0 @@
-
-#include "arch.h"
-#include "cc2.h"
-
-void
-writeout(void)
-{
-}
diff --git a/cc2/main.c b/cc2/main.c
index b71f468..1dcaf6a 100644
--- a/cc2/main.c
+++ b/cc2/main.c
_AT_@ -35,6 +35,7 @@ repeat:
int
main(void)
{
+
while (moreinput()) {
parse();
optimize();
diff --git a/cc2/node.c b/cc2/node.c
index 5e4f325..39ab4ca 100644
--- a/cc2/node.c
+++ b/cc2/node.c
_AT_@ -52,6 +52,16 @@ delnode(Node *np)
}
void
+deltree(Node *np)
+{
+ if (!np)
+ return;
+ deltree(np->left);
+ deltree(np->right);
+ delnode(np);
+}
+
+void
cleannodes(void)
{
struct arena *ap, *next;
diff --git a/cc2/parser.c b/cc2/parser.c
index 392ce1d..ebe4a93 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -212,8 +212,8 @@ constant(char *token, union tokenop u)
++token;
if (*token == OSTRING) {
np->op = OSYM;
- np->type = ptrtype;
sym->id = newid();
+ sym->type.flags = STRF;
sym->u.s = xstrdup(++token);
} else {
np->op = OCONST;
_AT_@ -224,6 +224,7 @@ constant(char *token, union tokenop u)
}
sym->u.i = v;
}
+ push(np);
}
static void
_AT_@ -363,6 +364,11 @@ stmt(void)
static Node *lastp;
Node *np = pop();
+ if (ininit) {
+ emit(np);
+ deltree(np);
+ return;
+ }
if (!stmtp)
stmtp = np;
else
Received on Sat Jan 23 2016 - 21:35:59 CET
This archive was generated by hypermail 2.3.0
: Sat Jan 23 2016 - 21:36:42 CET