commit da493b024ec13e759e5f5efd2e165120ca301e37
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Tue Apr 12 16:41:09 2016 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Tue Apr 12 16:41:09 2016 +0200

    [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.

diff --git a/cc2/Makefile b/cc2/Makefile
index 9ac6879..3e1b26c 100644
--- 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
diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c
index 2954ecf..8533f02 100644
--- a/cc2/arch/z80/code.c
+++ b/cc2/arch/z80/code.c
@@ -55,11 +55,6 @@ symname(Symbol *sym)
        return name;
 }
 
-void
-code(int op, Node *to, Node *from)
-{
-}
-
 static void
 label(Symbol *sym)
 {
diff --git a/cc2/cc2.h b/cc2/cc2.h
index 442d7ce..d176514 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -38,6 +38,7 @@ enum op {
        CONST    = '#',
        STRING   = '"',
        LABEL    = 'L',
+       INDEX    = 'I',
        /* storage class */
        GLOB     = 'G',
        EXTRN    = 'X',
@@ -116,6 +117,8 @@ enum nerrors {
 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 @@ struct node {
        char address;
        union {
                TUINT i;
+               char reg;
                char *s;
                Symbol *sym;
                char subop;
@@ -153,6 +157,21 @@ struct node {
        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 */
 extern void error(unsigned nerror, ...);
 
diff --git a/cc2/code.c b/cc2/code.c
new file mode 100644
index 0000000..f19abb1
--- /dev/null
+++ b/cc2/code.c
@@ -0,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;
+        }
+}

Reply via email to