scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 3cfe5e81491c46ec77110bd2a05280cd1e10d237
parent ff136f0ec39799a447752a1736173c4fa86917ab
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  9 Sep 2017 21:09:37 +0200

[as] Add symbol type

Diffstat:
Mas/as.h | 18++++++++++++++++++
Mas/emit.c | 35+++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/as/as.h b/as/as.h @@ -5,6 +5,14 @@ enum secflags { SFILE, }; +enum symflags { + FUNDEF = 'U', + FABS = 'A', + FCOMMON = 'C', + FBSS = 'B', + FDATA = 'D', +}; + enum args { AIMM = 1, AMAX, @@ -21,6 +29,7 @@ typedef struct op Op; typedef struct arg Arg; typedef void Format(Op *, Arg *); typedef struct section Section; +typedef struct symbol Symbol; struct line { char *label; @@ -57,6 +66,14 @@ struct section { struct section *next; }; +struct symbol { + char *name; + char type; + short desc; + TUINT value; + struct symbol *next; +}; + extern void isections(void); extern void writeout(char *name); extern void emit(Section *sec, char *bytes, int nbytes); @@ -65,6 +82,7 @@ extern void incpc(int siz); extern char *pack(TUINT v, int n, int inc); extern void error(char *msg, ...); extern Arg *getargs(char *s); +extern Symbol *lookup(char *name); /* Avoid errors in files where stdio is not included */ #ifdef stdin diff --git a/as/emit.c b/as/emit.c @@ -6,6 +6,8 @@ #include "../inc/scc.h" #include "as.h" +#define HASHSIZ 64 + static Section abss = { .name = "abs", .flags = SREAD|SWRITE @@ -33,6 +35,39 @@ Section *cursec = &text, *headp = &text; int pass; +static Symbol *hashtbl[HASHSIZ]; + +Symbol * +lookup(char *name) +{ + unsigned h; + Symbol *sym; + int c; + char *t, *s; + + s = name; + for (h = 0; c = *s; ++s) + h = h*33 ^ c; + h &= HASHSIZ-1; + + c = *name; + for (sym = hashtbl[h]; sym; sym = sym->next) { + t = sym->name; + if (c == *t && !strcmp(t, name)) + return sym; + } + + sym = xmalloc(sizeof(sym)); + sym->name = xstrdup(name); + sym->type = FUNDEF; + sym->desc = 0; + sym->value = 0; + sym->next = hashtbl[h]; + hashtbl[h] = sym; + + return sym; +} + char * pack(TUINT v, int n, int inc) {