aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2014-03-26 19:22:20 +0800
committerTeddy <[email protected]>2014-03-26 19:22:20 +0800
commit5f2965bcaca608f0d9af84373565539ae384918d (patch)
treecae25013c6456dc28fc93440b2528622b730f628
parentd2f316adaeae270e6c4e9cba040e07de3d11e737 (diff)
semantics: symbol table now works
-rw-r--r--Makefile7
-rw-r--r--semantics.c87
-rw-r--r--semantics.h42
-rw-r--r--test.c26
4 files changed, 150 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index 35fe25f..88bf597 100644
--- a/Makefile
+++ b/Makefile
@@ -23,3 +23,10 @@ cibic.tab.c: cibic.y
clean:
rm -f cibic lex.yy.c cibic.tab.c *.o
+
+sem: semantics.o test.o
+ gcc -o sem semantics.o test.o
+semantics.o: semantics.c
+ gcc -c semantics.c -Wall -Wextra
+test.o: test.c
+ gcc -c test.c -Wall -Wextra
diff --git a/semantics.c b/semantics.c
index 9178316..f491b2c 100644
--- a/semantics.c
+++ b/semantics.c
@@ -1,14 +1,26 @@
#include <stdlib.h>
+#include <assert.h>
#include <string.h>
+#include <stdio.h>
#include "semantics.h"
#define NEW(type) ((type *)malloc(sizeof(type)))
+#ifdef CIBIC_DEBUG
+CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc) {
+ CTable_t ct = NEW(CTable);
+ memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
+ ct->hfunc = hfunc;
+ ct->pfunc = pfunc;
+ return ct;
+}
+#else
CTable_t ctable_create(Hashfunc_t hfunc) {
CTable_t ct = NEW(CTable);
memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
ct->hfunc = hfunc;
return ct;
}
+#endif
void *ctable_lookup(CTable_t ct, const char *key) {
unsigned int hv = (ct->hfunc(key)) % MAX_TABLE_SIZE;
@@ -41,10 +53,16 @@ void ctable_clip(CTable_t ct, unsigned int hv, int max_lvl) {
CScope_t cscope_create() {
CScope_t p = NEW(CScope);
- p->lvl = 0;
+ p->lvl = -1;
p->top = NULL;
+#ifdef CIBIC_DEBUG
+ p->tvar = ctable_create(bkdr_hash, cvar_print);
+ p->ttype = ctable_create(bkdr_hash, ctype_print);
+#else
p->tvar = ctable_create(bkdr_hash);
p->ttype = ctable_create(bkdr_hash);
+#endif
+ cscope_enter(p);
return p;
}
@@ -88,6 +106,45 @@ void cscope_exit(CScope_t cs) {
cs->lvl--;
}
+void ctable_debug_print(CTable_t ct) {
+ int i;
+ fprintf(stderr, "*** CTable ***\n");
+ for (i = 0; i < MAX_TABLE_SIZE; i++)
+ if (ct->head[i])
+ {
+ CTNode *p;
+ fprintf(stderr, "[%04d]", i);
+ for (p = ct->head[i]; p; p = p->next)
+ fprintf(stderr, "->[%s:%d]", ct->pfunc(p), p->lvl);
+ fprintf(stderr, "\n");
+ }
+ fprintf(stderr, "*** CTable ***\n");
+}
+
+void cscope_debug_print(CScope_t cs) {
+ int lvl = cs->lvl;
+ CSNode *p;
+ fprintf(stderr, "*** CScope ***\n");
+ for (p = cs->top; p; p = p->next)
+ {
+ CVar *vp;
+ CType *tp;
+ fprintf(stderr, "Level %d:\n", lvl--);
+ fprintf(stderr, "Vars: ");
+ for (vp = p->vhead; vp; vp = vp->next)
+ fprintf(stderr, "%s ", vp->name);
+ fprintf(stderr, "\nTypes: ");
+ for (tp = p->thead; tp; tp = tp->next)
+ fprintf(stderr, "%s ", tp->name);
+ fprintf(stderr, "\n\n");
+ }
+ fprintf(stderr, "Var Table:\n");
+ ctable_debug_print(cs->tvar);
+ fprintf(stderr, "Type Table:\n");
+ ctable_debug_print(cs->ttype);
+ fprintf(stderr, "*** CScope ***\n");
+}
+
CVar *cscope_lookup_var(CScope_t cs, const char *name) {
return ctable_lookup(cs->tvar, name);
}
@@ -101,5 +158,31 @@ unsigned int bkdr_hash(const char *str) {
unsigned int hv = 0;
while (*str)
hv = hv * seed + (unsigned)(*str++);
- return hv;
+ return hv % 5;
+}
+
+const char *cvar_print(void *var) {
+ static char buff[MAX_DEBUG_PRINT_BUFF];
+ sprintf(buff, "%s", ((CVar *)var)->name);
+ return buff;
+}
+
+const char *ctype_print(void *type) {
+ static char buff[MAX_DEBUG_PRINT_BUFF];
+ sprintf(buff, "%s", ((CType *)type)->name);
+ return buff;
+}
+
+CVar_t cvar_create(const char *name, CType *type) {
+ CVar *cv = NEW(CVar);
+ cv->name = name;
+ cv->type = type;
+ return cv;
+}
+
+CType_t ctype_create(const char *name, int type) {
+ CType *ct = NEW(CType);
+ ct->name = name;
+ ct->type = type;
+ return ct;
}
diff --git a/semantics.h b/semantics.h
index b3d2942..1ec4c21 100644
--- a/semantics.h
+++ b/semantics.h
@@ -1,28 +1,33 @@
#ifndef SEMANTICS_H
#define SEMANTICS_H
-
+#include "ast.h"
#define MAX_TABLE_SIZE 1021
+#define CIBIC_DEBUG
struct CTable;
+struct CType;
typedef struct CVar{
- char *name;
+ const char *name;
struct CVar *next; /* next in the linked list */
struct CType *type;
int offset;
} CVar;
+typedef CVar *CVar_t;
+CVar_t cvar_create(const char *name, struct CType *type);
+
typedef struct CType {
enum {
- INT,
- CHAR,
- VOID,
- STRUCT,
- UNION,
- ARR,
- PTR
+ CINT,
+ CCHAR,
+ CVOID,
+ CSTRUCT,
+ CUNION,
+ CARR,
+ CPTR
} type;
- char *name;
+ const char *name;
struct CType *next;
union {
struct CType *fields; /* for a struct or union */
@@ -35,7 +40,13 @@ typedef struct CType {
int size; /* memory footprint */
} CType;
+typedef CType *CType_t;
+CType_t ctype_create(const char *name, int type);
+
typedef unsigned int (*Hashfunc_t) (const char *);
+#ifdef CIBIC_DEBUG
+typedef const char *(*Printfunc_t) (void *);
+#endif
typedef struct CTable *CTable_t;
typedef struct CTNode {
@@ -48,13 +59,21 @@ typedef struct CTNode {
typedef struct CTable {
struct CTNode *head[MAX_TABLE_SIZE];
Hashfunc_t hfunc;
+#ifdef CIBIC_DEBUG
+ Printfunc_t pfunc;
+#endif
} CTable;
+#ifdef CIBIC_DEBUG
+CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc);
+#else
CTable_t ctable_create(Hashfunc_t hfunc);
+#endif
void *ctable_lookup(CTable_t ct, const char *key);
void ctable_insert(CTable_t ct, const char *key, void *val, int lvl);
void ctable_clip(CTable_t ct, unsigned int hv, int max_lvl);
+void ctable_debug_print(CTable_t ct);
typedef struct CSNode {
struct CVar *vhead;
@@ -77,6 +96,9 @@ void cscope_push_var(CScope_t cs, CVar *var);
void cscope_push_type(CScope_t cs, CType *type);
void cscope_enter(CScope_t cs);
void cscope_exit(CScope_t cs);
+void cscope_debug_print(CScope_t cs);
unsigned int bkdr_hash(const char *str);
+const char *cvar_print(void *var);
+const char *ctype_print(void *type);
#endif
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..20b2968
--- /dev/null
+++ b/test.c
@@ -0,0 +1,26 @@
+#include <stdlib.h>
+#include "semantics.h"
+#define PV(str) cscope_push_var(scope, newvar(str))
+
+CVar_t newvar(const char *name) {
+ return cvar_create(name, NULL);
+}
+
+CType_t newtype(const char *name) {
+ return ctype_create(name, 0);
+}
+
+int main() {
+ CScope_t scope = cscope_create();
+ PV("a");
+ PV("b");
+ PV("asdf");
+ PV("fdsa");
+ PV("hello");
+ cscope_debug_print(scope);
+ cscope_enter(scope);
+ PV("a");
+ PV("hello");
+ cscope_debug_print(scope);
+ return 0;
+}