scc

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

commit 44127ce7cb456a8ed9885416745a158db8aaddeb
parent 01389bee81489f45f6e57e7649032ef4b75e1b19
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 23 Nov 2017 08:17:37 +0000

[as] Add String type

This type is needed because we will need offsets
in the strings stored in the string table of the
object file.

Diffstat:
as/as.h | 11+++++++++--
as/symbol.c | 34+++++++++++++++++++++++-----------
2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/as/as.h b/as/as.h @@ -54,8 +54,14 @@ typedef struct op Op; typedef struct section Section; typedef struct symbol Symbol; typedef struct node Node; +typedef struct string String; typedef void Format(Op *, Node **); +struct string { + char *buf; + size_t offset; +}; + struct line { char *label; char *op; @@ -76,7 +82,7 @@ struct op { }; struct section { - char *name; + String name; char *mem; unsigned char flags; TUINT base; @@ -87,7 +93,7 @@ struct section { }; struct symbol { - char *name; + String name; unsigned char flags; char pass; char argtype; @@ -113,6 +119,7 @@ extern Symbol *tmpsym(TUINT val); extern void killtmp(void); extern int toobig(Node *np, int type); extern void dumpstab(char *msg); +extern String newstring(char *s); /* main.c */ extern Symbol *lookup(char *name, int type); diff --git a/as/symbol.c b/as/symbol.c @@ -12,24 +12,24 @@ static char sccsid[] = "@(#) ./as/symbol.c"; #define NALLOC 10 static Section abss = { - .name = "abs", + .name = (String) {"abs"}, .flags = TABS|SREAD|SWRITE, }; static Section bss = { - .name = "bss", + .name = (String) {"bss"}, .flags = TBSS|SRELOC|SREAD|SWRITE, .next = &abss, }; static Section data = { - .name = "data", + .name = (String) {"data"}, .flags = TDATA|SRELOC|SREAD|SWRITE|SFILE, .next = &bss, }; static Section text = { - .name = "text", + .name = (String) {"text"}, .flags = TTEXT|SRELOC|SFILE, .next = &data, }; @@ -57,7 +57,7 @@ dumpstab(char *msg) fprintf(stderr, "[%d]", (int) (bp - hashtbl)); for (sym = *bp; sym; sym = sym->next) { fprintf(stderr, " -> %s:%0X:%0X", - sym->name, sym->flags, sym->argtype); + sym->name.buf, sym->flags, sym->argtype); } putc('\n', stderr); } @@ -80,7 +80,7 @@ lookup(char *name, int type) c = toupper(*name); list = &hashtbl[h]; for (sym = *list; sym; sym = sym->next) { - t = sym->name; + t = sym->name.buf; if (c != toupper(*t) || casecmp(t, name)) continue; symtype = sym->flags & TMASK; @@ -90,7 +90,7 @@ lookup(char *name, int type) } sym = xmalloc(sizeof(*sym)); - sym->name = xstrdup(name); + sym->name = newstring(name); sym->flags = FLOCAL | FUNDEF | type; sym->desc = 0; sym->value = 0; @@ -116,10 +116,10 @@ deflabel(char *name) } r = snprintf(label, sizeof(label), "%s%s", - cursym->name, name); + cursym->name.buf, name); if (r == sizeof(label)) { error("local label '%s' in '%s' produces too long symbol", - name, cursym->name); + name, cursym->name.buf); return NULL; } name = label; @@ -190,12 +190,12 @@ section(char *name) Section *sec; for (sec = seclist; sec; sec = sec->next) { - if (!strcmp(sec->name, name)) + if (!strcmp(sec->name.buf, name)) break; } if (!sec) { sec = xmalloc(sizeof(*sec)); - sec->name = xstrdup(name); + sec->name = newstring(name); sec->base = sec->max = sec->pc = sec->curpc = 0; sec->next = seclist; sec->flags = SRELOC|SREAD|SWRITE|SFILE; @@ -242,3 +242,15 @@ killtmp(void) dealloc(tmpalloc); tmpalloc = NULL; } + +String +newstring(char *s) +{ + size_t len = strlen(s) + 1; + String str; + + str.offset = 0; + str.buf = xmalloc(len); + memcpy(str.buf, s, len); + return str; +}