scc

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

commit d5f095d3afb21edf72fe746cf31839b8b11c9e7a
parent 5c4275a3d6c5c93b533dee468b2314eeb74c58a3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 12 Sep 2017 08:33:34 +0100

[cc1] Remove scalar use of char

Using scalar char is a bad idea because char are always promoted to
int in all the operations, they use the same space than an int in
the stack and since they may be signed, any operation over them
bigger than 127 produces an overflow with undefined behaviour.

Diffstat:
Mcc1/cc1.h | 2+-
Mcc1/code.c | 2+-
Mcc1/cpp.c | 6++++--
Mcc1/lex.c | 24+++++++++++++-----------
Mcc1/symbol.c | 4++--
5 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -412,7 +412,7 @@ extern Type *typename(void); extern void decl(void); /* lex.c */ -extern char ahead(void); +extern int ahead(void); extern unsigned next(void); extern void expect(unsigned tok); extern void discard(void); diff --git a/cc1/code.c b/cc1/code.c @@ -188,7 +188,7 @@ emit(unsigned op, void *arg) static void emitvar(Symbol *sym) { - char c; + int c; short flags = sym->flags; if (flags & SLOCAL) diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -184,7 +184,8 @@ parsepars(char *buffer, char **listp, int nargs) static size_t copymacro(char *buffer, char *s, size_t bufsiz, char *arglist[]) { - char delim, prevc, c, *p, *arg, *bp = buffer; + int delim, prevc, c; + char *p, *arg, *bp = buffer; size_t size; for (prevc = '\0'; c = *s; prevc = c, ++s) { @@ -797,7 +798,8 @@ ppragmaln(void) void outcpp(void) { - char c, *s, *t; + int c; + char *s, *t; for (next(); yytoken != EOFTOK; next()) { if (onlyheader) diff --git a/cc1/lex.c b/cc1/lex.c @@ -377,7 +377,7 @@ overflow: } static unsigned -integer(char *s, char base) +integer(char *s, int base) { Type *tp; Symbol *sym; @@ -414,7 +414,8 @@ convert: static char * digits(unsigned base) { - char c, *p; + char *p; + int c; for (p = input->p; c = *p; ++p) { switch (base) { @@ -441,7 +442,7 @@ end: static unsigned number(void) { - char base; + int base; if (*input->p != '0') { base = 10; @@ -457,7 +458,7 @@ number(void) return integer(digits(base), base); } -static char +static int escape(void) { int c, base; @@ -506,7 +507,7 @@ escape(void) static unsigned character(void) { - char c; + int c; Symbol *sym; if ((c = *++input->p) == '\\') @@ -530,7 +531,8 @@ character(void) static unsigned string(void) { - char *bp = yytext, c; + char *bp = yytext; + int c; *bp++ = '"'; for (++input->p; (c = *input->p) != '"'; ++input->p) { @@ -614,7 +616,7 @@ plus(void) static unsigned relational(int op, int equal, int shift, int assig) { - char c; + int c; if ((c = *input->p++) == '=') return equal; @@ -627,7 +629,7 @@ relational(int op, int equal, int shift, int assig) static unsigned logic(int op, int equal, int logic) { - char c; + int c; if ((c = *input->p++) == '=') return equal; @@ -640,7 +642,7 @@ logic(int op, int equal, int logic) static unsigned dot(void) { - char c; + int c; if ((c = *input->p) != '.') return '.'; @@ -753,7 +755,7 @@ expect(unsigned tok) } } -char +int ahead(void) { skipspaces(); @@ -770,7 +772,7 @@ void discard(void) { extern jmp_buf recover; - char c; + int c; input->begin = input->p; for (c = yytoken; ; c = *input->begin++) { diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -276,8 +276,8 @@ Symbol * lookup(int ns, char *name, int alloc) { Symbol *sym; - int sns; - char *t, c; + int sns, c; + char *t; c = *name; for (sym = *hash(name, ns); sym; sym = sym->hash) {