shithub: scc

Download patch

ref: d50069c79f745e969bbfacd15798947778a18552
parent: a91cab7bce37b82534ee3f59c8fed4e270dd919a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Apr 12 12:41:09 EDT 2016

[cc2] Add skeleton for code.c

This file is going to have the functions that implement the common
functions about the 3 address instructions. This code is basically
imported from cc2.old, but this time the basic structure of the
instructions is a 3 address, instead of a 2 address. The original
cc2 had 2 address instructions because it was only z80 targered,
but this new version wants to be multi targered, so it is better
a more general approach, although it will waste memory in accumulator
based architectures.

--- a/cc2/Makefile
+++ b/cc2/Makefile
@@ -2,7 +2,7 @@
 
 include ../config.mk
 
-OBJS = main.o parser.o optm.o peep.o symbol.o node.o \
+OBJS = main.o parser.o optm.o peep.o symbol.o node.o code.o\
        arch/$(ARCH)/code.o arch/$(ARCH)/cgen.o arch/$(ARCH)/types.o
 
 all: cc2
--- a/cc2/arch/z80/code.c
+++ b/cc2/arch/z80/code.c
@@ -55,11 +55,6 @@
 	return name;
 }
 
-void
-code(int op, Node *to, Node *from)
-{
-}
-
 static void
 label(Symbol *sym)
 {
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -38,6 +38,7 @@
 	CONST    = '#',
 	STRING   = '"',
 	LABEL    = 'L',
+	INDEX    = 'I',
 	/* storage class */
 	GLOB     = 'G',
 	EXTRN    = 'X',
@@ -116,6 +117,8 @@
 typedef struct node Node;
 typedef struct type Type;
 typedef struct symbol Symbol;
+typedef struct addr Addr;
+typedef struct inst Inst;
 
 struct type {
 	TSIZE size;
@@ -144,6 +147,7 @@
 	char address;
 	union {
 		TUINT i;
+		char reg;
 		char *s;
 		Symbol *sym;
 		char subop;
@@ -151,6 +155,21 @@
 	Symbol *label;
 	Node *left, *right;
 	Node *stmt;
+};
+
+struct addr {
+        char kind;
+        union {
+                char reg;
+                TUINT i;
+                Symbol *sym;
+        } u;
+};
+
+struct inst {
+        char op;
+        Addr from1, from2, to;
+        Inst *next, *prev;
 };
 
 /* main.c */
--- /dev/null
+++ b/cc2/code.c
@@ -1,0 +1,81 @@
+
+#include <stdlib.h>
+
+#include "arch.h"
+#include "cc2.h"
+
+static Inst *pc, *prog;
+
+static void
+nextpc(void)
+{
+        Inst *new;
+
+        new = malloc(sizeof(*new)); /* TODO: create an arena */
+
+        if (!pc) {
+                new->next = NULL;
+                prog = new;
+        } else {
+                new->next = pc->next;
+                pc->next = new;
+        }
+
+        new->prev = pc;
+        new->to.kind = new->from2.kind = new->from1.kind = NONE;
+        pc = new;
+}
+
+void
+addr(int op, Node *np, Addr *addr)
+{
+	switch (addr->kind = np->op) {
+	case REG:
+		addr->u.reg = np->u.reg;
+		break;
+	case CONST:
+		/* TODO: different type of constants*/
+		np->u.i = np->u.i;
+		break;
+	case LABEL:
+	case MEM:
+		addr->u.sym = np->u.sym;
+		break;
+	case AUTO:
+	case INDEX:
+		break;
+	default:
+		abort();
+	}
+
+}
+
+void
+code(int op, Node *to, Node *from1, Node *from2)
+{
+	nextpc();
+	if (from1)
+		addr(op, from1, &pc->from1);
+	if (from2)
+		addr(op, from2, &pc->from2);
+	if (to)
+		addr(op, to, &pc->to);
+}
+
+
+void
+delcode(void)
+{
+        Inst *prev = pc->prev, *next = pc->next;
+
+        free(pc);
+        if (!prev) {
+                pc = next;
+                prog = NULL;
+        } else {
+                pc = prev;
+                prev->next = next;
+                if (next)
+                        next->prev = prev;
+        }
+}
--