scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit abff0851258d61b3257f50f876daa2a4828b96e1
parent ea5689c219a25ed3c026764ef41894727393ddbc
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 12 Dec 2017 07:56:17 +0000

Merge branch 'master' of ssh://simple-cc.org:/var/gitrepos/scc

Diffstat:
Mas/as.h | 34++++++++++++++--------------------
Mas/expr.c | 4++--
Mas/main.c | 32++++++++++++++++++++++++++++----
Mas/myro.c | 10++++++++--
Mas/parser.c | 3+--
Mas/symbol.c | 19++++++++++---------
Mas/target/i386/proc.c | 2+-
Mas/target/z80/proc.c | 2+-
8 files changed, 65 insertions(+), 41 deletions(-)

diff --git a/as/as.h b/as/as.h @@ -2,28 +2,21 @@ * First 3 bits of flags in segments and symbols are for the * type of segment */ -enum symtype { - TUNDEF = 0, - TABS = 1, - TTEXT = 2, - TBSS = 3, - TDATA = 4, - TREG = 5, - TMASK = 7, +enum symflags { + FREG = 1 << 0, + FCOMMON = 1 << 1, + FLOCAL = 1 << 2, + FEXTERN = 1 << 3, + FUNDEF = 1 << 4, }; enum secflags { - SRELOC = 1 << 4, - SREAD = 1 << 5, - SWRITE = 1 << 6, - SFILE = 1 << 7, -}; - -enum symflags { - FCOMMON = 1 << 4, - FLOCAL = 1 << 5, - FEXTERN = 1 << 6, - FUNDEF = 1 << 7, + SREAD = 1 << 0, + SWRITE = 1 << 1, + SEXEC = 1 << 2, + SLOAD = 1 << 3, + SFILE = 1 << 4, + SABS = 1 << 5, }; enum endianess { @@ -110,6 +103,7 @@ struct symbol { unsigned char pass; unsigned char argtype; TUINT value; + Section *section; struct symbol *next; struct symbol *hash; }; @@ -175,4 +169,4 @@ extern int pass; extern TUINT maxaddr; extern int endian; extern Symbol *linesym, *symlist; -extern char *filename; +extern char *infile; diff --git a/as/expr.c b/as/expr.c @@ -200,7 +200,7 @@ iden(void) while (isalnum(c = *endp) || c == '_' || c == '.') ++endp; tok2str(); - yylval.sym = lookup(yytext, TUNDEF); + yylval.sym = lookup(yytext, FUNDEF); return IDEN; } @@ -267,7 +267,7 @@ reg(void) while (isalnum(c = *endp)) ++endp; tok2str(); - yylval.sym = lookup(yytext, TREG); + yylval.sym = lookup(yytext, FREG); if (!yylval.sym->argtype) error("incorrect register name"); return REG; diff --git a/as/main.c b/as/main.c @@ -7,8 +7,20 @@ static char sccsid[] = "@(#) ./as/main.c"; #include <string.h> #include "../inc/scc.h" +#include "../inc/arg.h" #include "as.h" +char *argv0; +char *outfile, *infile; + + +static void +cleanup(void) +{ + if (outfile) + remove(outfile); +} + static int cmp(const void *f1, const void *f2) { @@ -81,7 +93,7 @@ dopass(char *fname) if (fclose(fp)) die("as: error reading from input file '%s'", fname); if (pass == 2) - writeout("a.out"); + writeout(outfile); /* * kill tmp symbols because they are not needed anymore */ @@ -100,15 +112,27 @@ usage(void) int main(int argc, char *argv[]) { - if (argc != 2) + outfile = "a.out"; + + ARGBEGIN { + case 'o': + outfile = EARGF(usage()); + break; + default: + usage(); + } ARGEND + + if (argc != 1) usage(); + infile = *argv; + atexit(cleanup); iarch(); - filename = argv[1]; for (pass = 1; pass <= 2; pass++) { - if (!dopass(filename)) + if (!dopass(infile)) return 1; } + outfile = NULL; return 0; } diff --git a/as/myro.c b/as/myro.c @@ -26,7 +26,7 @@ writestrings(FILE *fp) off = sizeof(FORMAT); for (sym = symlist; sym; sym = sym->next) { - if ((sym->flags & TMASK) == TREG) + if (sym->flags & FREG) continue; str = &sym->name; len = strlen(str->buf) + 1; @@ -57,6 +57,12 @@ getsecflags(Section *sp) flags |= MYROSEC_WRITE; if (sp->flags & SFILE) flags |= MYROSEC_FILE; + if (sp->flags & SEXEC) + flags |= MYROSEC_EXEC; + if (sp->flags & SLOAD) + flags |= MYROSEC_LOAD; + if (sp->flags & SABS) + flags |= MYROSEC_ABS; return flags; } @@ -88,7 +94,7 @@ writesymbols(FILE *fp) struct myrosym symbol; for (sym = symlist; sym; sym = sym->next) { - if ((sym->flags & TMASK) == TREG) + if (sym->flags & FREG) continue; symbol.name = sym->name.offset; symbol.type = -1; diff --git a/as/parser.c b/as/parser.c @@ -12,7 +12,6 @@ static char sccsid[] = "@(#) ./as/parser.c"; #define NARGS 20 #define MAXLINE 100 -char *filename; int nerrors; jmp_buf recover; @@ -24,7 +23,7 @@ error(char *msg, ...) va_list va; va_start(va, msg); - fprintf(stderr, "as:%s:%u: ", filename, lineno); + fprintf(stderr, "as:%s:%u: ", infile, lineno); vfprintf(stderr, msg, va); putc('\n', stderr); nerrors++; diff --git a/as/symbol.c b/as/symbol.c @@ -13,24 +13,24 @@ static char sccsid[] = "@(#) ./as/symbol.c"; static Section abss = { .name = (String) {"abs"}, - .flags = TABS|SREAD|SWRITE, + .flags = SABS|SREAD|SWRITE|SFILE|SLOAD, }; static Section bss = { .name = (String) {"bss"}, - .flags = TBSS|SRELOC|SREAD|SWRITE, + .flags = SREAD|SWRITE|SLOAD, .next = &abss, }; static Section data = { .name = (String) {"data"}, - .flags = TDATA|SRELOC|SREAD|SWRITE|SFILE, + .flags = SREAD|SWRITE|SFILE|SLOAD, .next = &bss, }; static Section text = { .name = (String) {"text"}, - .flags = TTEXT|SRELOC|SFILE, + .flags = SREAD|SEXEC|SLOAD|SFILE, .next = &data, }; @@ -83,8 +83,8 @@ lookup(char *name, int type) t = sym->name.buf; if (c != toupper(*t) || casecmp(t, name)) continue; - symtype = sym->flags & TMASK; - if (symtype != TUNDEF && symtype != type) + symtype = sym->flags; + if (((symtype | type) & FUNDEF) == 0 && symtype != type) continue; return sym; } @@ -93,6 +93,7 @@ lookup(char *name, int type) sym->name = newstring(name); sym->flags = FLOCAL | FUNDEF | type; sym->value = 0; + sym->section = cursec; sym->hash = *list; sym->next = symlist; @@ -124,7 +125,7 @@ deflabel(char *name) name = label; } - sym = lookup(name, TUNDEF); + sym = lookup(name, FUNDEF); if (pass == 1 && (sym->flags & FUNDEF) == 0) error("redefinition of label '%s'", name); sym->flags &= ~FUNDEF; @@ -197,7 +198,7 @@ section(char *name) sec->name = newstring(name); sec->base = sec->max = sec->pc = sec->curpc = 0; sec->next = seclist; - sec->flags = SRELOC|SREAD|SWRITE|SFILE; + sec->flags = 0; sec->fill = 0; sec->aligment = 0; } @@ -230,7 +231,7 @@ tmpsym(TUINT val) tmpalloc = alloc(sizeof(*sym), NALLOC); sym = new(tmpalloc); sym->value = val; - sym->flags = TABS; + sym->section = NULL; return sym; } diff --git a/as/target/i386/proc.c b/as/target/i386/proc.c @@ -92,7 +92,7 @@ iarch(void) }, *bp; for (bp = regs; bp->name; ++bp) { - Symbol *sym = lookup(bp->name, TREG); + Symbol *sym = lookup(bp->name, FREG); sym->argtype = bp->type; } } diff --git a/as/target/z80/proc.c b/as/target/z80/proc.c @@ -45,7 +45,7 @@ iarch(void) }, *bp; for (bp = regs; bp->name; ++bp) { - Symbol *sym = lookup(bp->name, TREG); + Symbol *sym = lookup(bp->name, FREG); sym->argtype = bp->type; } }