shithub: scc

Download patch

ref: b8ea542ae6db4934ceb3d3a69a7884830604df31
parent: af5912cb496c86beb67fdd17ef7038ffde27a04e
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Jan 23 16:28:09 EST 2016

[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: cannot open b/cc1/arch/i386-sysv//null: file does not exist: 'b/cc1/arch/i386-sysv//null' diff: cannot open a/cc1/arch/i386//null: file does not exist: 'a/cc1/arch/i386//null'
--- /dev/null
+++ b/cc1/arch/i386-sysv/arch.h
@@ -1,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
--- a/cc1/arch/i386/arch.h
+++ /dev/null
@@ -1,33 +1,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
--- a/cc2/Makefile
+++ b/cc2/Makefile
@@ -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
 
--- /dev/null
+++ b/cc2/arch/i386-sysv/code.c
@@ -1,0 +1,13 @@
+
+#include "arch.h"
+#include "../../cc2.h"
+
+void
+emit(Node *np)
+{
+}
+
+void
+writeout(void)
+{
+}
--- /dev/null
+++ b/cc2/arch/z80/cgen.c
@@ -1,0 +1,13 @@
+
+#include "arch.h"
+#include "../../cc2.h"
+
+void
+generate(void)
+{
+}
+
+void
+addable(void)
+{
+}
--- /dev/null
+++ b/cc2/arch/z80/code.c
@@ -1,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)
+{
+}
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -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 {
@@ -150,11 +151,13 @@
 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 */
--- a/cc2/cgen.c
+++ /dev/null
@@ -1,13 +1,0 @@
-
-#include "arch.h"
-#include "cc2.h"
-
-void
-generate(void)
-{
-}
-
-void
-addable(void)
-{
-}
--- a/cc2/code.c
+++ /dev/null
@@ -1,8 +1,0 @@
-
-#include "arch.h"
-#include "cc2.h"
-
-void
-writeout(void)
-{
-}
--- a/cc2/main.c
+++ b/cc2/main.c
@@ -35,6 +35,7 @@
 int
 main(void)
 {
+
 	while (moreinput()) {
 		parse();
 		optimize();
--- a/cc2/node.c
+++ b/cc2/node.c
@@ -52,6 +52,16 @@
 }
 
 void
+deltree(Node *np)
+{
+	if (!np)
+		return;
+	deltree(np->left);
+	deltree(np->right);
+	delnode(np);
+}
+
+void
 cleannodes(void)
 {
 	struct arena *ap, *next;
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -212,8 +212,8 @@
 	++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;
@@ -224,6 +224,7 @@
 		}
 		sym->u.i = v;
 	}
+	push(np);
 }
 
 static void
@@ -363,6 +364,11 @@
 	static Node *lastp;
 	Node *np = pop();
 
+	if (ininit) {
+		emit(np);
+		deltree(np);
+		return;
+	}
 	if (!stmtp)
 		stmtp = np;
 	else