scc

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

commit 000d4104bf2f31750b2699b35a59484179b462ba
parent fc8190e4863a2d300391578be850614b7b3f4b9d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 21 May 2015 11:46:37 +0200

Remove abuse use of int8_t types in cc1

It is desirable this compiler can be executed in real z80 machines
and it is the reason why there were a lot of variables with int8_t
types, but it caused the code to be non idiomatic at all. This
patch changes all these types to int/unsigned, with the convention
they must hold values of int8_t/uint8_t types, so an option of the
compiler can generate code with 8 bit integers and have the same
result. This same option is present in the AVR gcc port.

Diffstat:
Mcc1/cc1.h | 61++++++++++++++++++++++++++++++-------------------------------
Mcc1/code.c | 48++++++++++++++++++++++++------------------------
Mcc1/cpp.c | 2+-
Mcc1/decl.c | 64++++++++++++++++++++++++++++++++++++++--------------------------
Mcc1/error.c | 8++++----
Mcc1/expr.c | 6+++---
Mcc1/lex.c | 61+++++++++++++++++++++++++++++--------------------------------
Mcc1/main.c | 4++--
Mcc1/symbol.c | 20+++++++++++---------
Mcc1/types.c | 13+++++++------
Mlib/die.c | 2+-
11 files changed, 150 insertions(+), 139 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -9,16 +9,16 @@ typedef struct caselist Caselist; typedef struct node Node; struct type { - uint8_t op; /* type builder operator */ - uint8_t ns; - char letter; /* letter of the type */ - bool defined; /* type defined (is not a forward reference) */ - Type *type; /* base type */ - Type *next; /* next element in the hash */ - Type **pars; /* type parameters */ + unsigned char op; /* type builder operator */ + unsigned char ns; + char letter; /* letter of the type */ + bool defined; /* type defined */ + Type *type; /* base type */ + Type *next; /* next element in the hash */ + Type **pars; /* type parameters */ union { - unsigned char rank; /* convertion rank */ - short elem; /* number of type parameters */ + unsigned char rank; /* convertion rank */ + short elem; /* number of type parameters */ } n; }; @@ -26,21 +26,21 @@ struct symbol { char *name; Type *type; short id; - uint8_t ctx; - uint8_t ns; - uint8_t token; + unsigned char ctx; + unsigned char ns; + unsigned char token; char flags; union { int i; char *s; - uint8_t token; + unsigned char token; } u; struct symbol *next; struct symbol *hash; }; struct node { - uint8_t op; + unsigned char op; Type *type; Symbol *sym; bool lvalue : 1; @@ -63,7 +63,7 @@ struct caselist { struct yystype { Symbol *sym; - uint8_t token; + unsigned char token; }; /* @@ -173,8 +173,7 @@ enum tokens { CONTINUE, BREAK, RETURN, - EOFTOK, - NOTOK + EOFTOK }; /* operations */ @@ -244,17 +243,16 @@ extern void error(char *fmt, ...); extern void warn(char *fmt, ...); extern void unexpected(void); extern void softerror(char *fmt, ...); -extern void setsafe(uint8_t type); -/* type.c */ +/* types.c */ extern bool eqtype(Type *tp1, Type *tp2); -extern Type *ctype(uint8_t type, uint8_t sign, uint8_t size); -extern Type *mktype(Type *tp, uint8_t op, short nelem, void *data); +extern Type *ctype(unsigned type, unsigned sign, unsigned size); +extern Type *mktype(Type *tp, unsigned op, short nelem, void *data); /* symbol.c */ -extern Symbol *lookup(uint8_t ns); -extern Symbol *install(uint8_t ns); -extern Symbol *newsym(uint8_t ns); +extern Symbol *lookup(unsigned ns); +extern Symbol *install(unsigned ns); +extern Symbol *newsym(unsigned ns); extern void pushctx(void), popctx(void); /* stmt.c */ @@ -265,9 +263,9 @@ extern Type *typename(void); extern void extdecl(void), decl(void); /* lex.c */ -extern uint8_t ahead(void); -extern uint8_t next(void); -extern void expect(uint8_t tok); +extern char ahead(void); +extern unsigned next(void); +extern void expect(unsigned tok); extern void discard(void); extern char *getfname(void); extern unsigned short getfline(void); @@ -275,12 +273,13 @@ extern void setfname(char *name); extern void setfline(unsigned short line); extern bool addinput(char *fname); extern void delinput(void); -extern void setnamespace(uint8_t ns); +extern void setnamespace(int ns); +extern void setsafe(int type); #define accept(t) ((yytoken == (t)) ? next() : 0) /* code.c */ -extern void emit(uint8_t, void *); -extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth); +extern void emit(unsigned, void *); +extern Node *node(unsigned op, Type *tp, Node *left, Node *rigth); extern Node *varnode(Symbol *sym); extern Node *constnode(Symbol *sym); extern Node *sizeofnode(Type *tp); @@ -297,7 +296,7 @@ extern char *preprocessor(char *s); */ extern struct yystype yylval; extern char yytext[]; -extern uint8_t yytoken; +extern unsigned yytoken; extern Type *voidtype, *pvoidtype, *booltype, *uchartype, *chartype, diff --git a/cc1/code.c b/cc1/code.c @@ -7,13 +7,13 @@ #include "../inc/cc.h" #include "cc1.h" -static void emitbin(uint8_t, void *), emitunary(uint8_t, void *), - emitcast(uint8_t, void *), emitswitch(uint8_t, void *), - emitsym(uint8_t, void *), emitfield(uint8_t, void *), - emitsizeof(uint8_t, void *), emitexp(uint8_t, void *), - emitsymid(uint8_t, void *), emittext(uint8_t, void *), - emitprint(uint8_t, void *), emitfun(uint8_t, void *), - emitret(uint8_t, void *), emitdcl(uint8_t, void *); +static void emitbin(unsigned, void *), + emitcast(unsigned, void *), emitswitch(unsigned, void *), + emitsym(unsigned, void *), emitfield(unsigned, void *), + emitsizeof(unsigned, void *), emitexp(unsigned, void *), + emitsymid(unsigned, void *), emittext(unsigned, void *), + emitprint(unsigned, void *), emitfun(unsigned, void *), + emitret(unsigned, void *), emitdcl(unsigned, void *); char *optxt[] = { [OADD] = "+", @@ -66,7 +66,7 @@ char *optxt[] = { [OBLOOP] = "\td" }; -void (*opcode[])(uint8_t, void *) = { +void (*opcode[])(unsigned, void *) = { [OADD] = emitbin, [OSUB] = emitbin, [OMUL] = emitbin, @@ -145,9 +145,9 @@ emitnode(Node *np) } void -emit(uint8_t op, void *arg) +emit(unsigned op, void *arg) { - extern uint8_t failure; + extern int failure; if (failure) return; @@ -190,7 +190,7 @@ emitconst(Node *np) } static void -emitsym(uint8_t op, void *arg) +emitsym(unsigned op, void *arg) { Node *np = arg; putchar('\t'); @@ -204,7 +204,7 @@ emittype(Type *tp) } static void -emitdcl(uint8_t op, void *arg) +emitdcl(unsigned op, void *arg) { Symbol *sym = arg; @@ -215,7 +215,7 @@ emitdcl(uint8_t op, void *arg) } static void -emitcast(uint8_t op, void *arg) +emitcast(unsigned op, void *arg) { Node *np = arg, *lp = np->left; @@ -224,7 +224,7 @@ emitcast(uint8_t op, void *arg) } static void -emitbin(uint8_t op, void *arg) +emitbin(unsigned op, void *arg) { Node *np = arg; char *s; @@ -236,14 +236,14 @@ emitbin(uint8_t op, void *arg) } static void -emitsizeof(uint8_t op, void *arg) +emitsizeof(unsigned op, void *arg) { Node *np = arg; printf("\t#%c", np->left->type->letter); } static void -emitexp(uint8_t op, void *arg) +emitexp(unsigned op, void *arg) { Node *np = arg; @@ -253,7 +253,7 @@ emitexp(uint8_t op, void *arg) } static void -emitprint(uint8_t op, void *arg) +emitprint(unsigned op, void *arg) { Node *np = arg; @@ -264,7 +264,7 @@ emitprint(uint8_t op, void *arg) } static void -emitfun(uint8_t op, void *arg) +emitfun(unsigned op, void *arg) { Symbol *sym = arg; @@ -273,7 +273,7 @@ emitfun(uint8_t op, void *arg) } static void -emitret(uint8_t op, void *arg) +emitret(unsigned op, void *arg) { Type *tp = arg; @@ -282,20 +282,20 @@ emitret(uint8_t op, void *arg) } static void -emittext(uint8_t op, void *arg) +emittext(unsigned op, void *arg) { puts(optxt[op]); } static void -emitsymid(uint8_t op, void *arg) +emitsymid(unsigned op, void *arg) { Symbol *sym = arg; printf(optxt[op], sym->id); } static void -emitswitch(uint8_t op, void *arg) +emitswitch(unsigned op, void *arg) { Caselist *lcase = arg; @@ -303,7 +303,7 @@ emitswitch(uint8_t op, void *arg) } void -emitfield(uint8_t op, void *arg) +emitfield(unsigned op, void *arg) { Node *np = arg; @@ -313,7 +313,7 @@ emitfield(uint8_t op, void *arg) } Node * -node(uint8_t op, Type *tp, Node *left, Node *right) +node(unsigned op, Type *tp, Node *left, Node *right) { Node *np; diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -210,7 +210,7 @@ line(char *s) { char *p, *q; - if (!isdigit(*p)) + if (!isdigit(*s)) goto bad_line; for (p = s; isdigit(*p); ++p) /* nothing */; diff --git a/cc1/decl.c b/cc1/decl.c @@ -14,13 +14,13 @@ /* TODO: check identifiers in enum declaration */ struct dcldata { - uint8_t op; + unsigned char op; unsigned short nelem; void *data; }; static struct dcldata * -queue(struct dcldata *dp, uint8_t op, short nelem, void *data) +queue(struct dcldata *dp, unsigned op, short nelem, void *data) { if (dp->op == 255) error("too much declarators"); @@ -44,7 +44,7 @@ static struct dcldata * fundcl(struct dcldata *dp) { size_t siz; - uint8_t n, i, noname; + unsigned n, i, noname; Type *pars[NR_FUNPARAM], **tp = pars; Symbol *syms[NR_FUNPARAM], **sp = syms, *sym; @@ -88,10 +88,10 @@ fundcl(struct dcldata *dp) return queue(dp, FTN, n, tp); } -static struct dcldata *declarator0(struct dcldata *dp, uint8_t ns); +static struct dcldata *declarator0(struct dcldata *dp, unsigned ns); static struct dcldata * -directdcl(struct dcldata *dp, uint8_t ns) +directdcl(struct dcldata *dp, unsigned ns) { Symbol *sym; @@ -119,9 +119,9 @@ directdcl(struct dcldata *dp, uint8_t ns) } static struct dcldata* -declarator0(struct dcldata *dp, uint8_t ns) +declarator0(struct dcldata *dp, unsigned ns) { - uint8_t n; + unsigned n; for (n = 0; accept('*'); ++n) { while (accept(TQUALIFIER)) @@ -137,7 +137,7 @@ declarator0(struct dcldata *dp, uint8_t ns) } static Symbol * -declarator(Type *tp, int8_t flags, uint8_t ns) +declarator(Type *tp, int flags, unsigned ns) { struct dcldata data[NR_DECLARATORS+2]; struct dcldata *bp; @@ -169,19 +169,21 @@ declarator(Type *tp, int8_t flags, uint8_t ns) static Type *structdcl(void), *enumdcl(void); static Type * -specifier(int8_t *sclass) +specifier(unsigned *sclass) { Type *tp = NULL; - int8_t qlf, sign, type, cls, size, t; + unsigned qlf, sign, type, cls, size; qlf = sign = type = cls = size = 0; for (;;) { - int8_t *p; + unsigned *p; Type *(*dcl)(void) = NULL; switch (yytoken) { - case SCLASS: p = &cls; break; + case SCLASS: + p = &cls; + break; case TQUALIFIER: if ((qlf |= yylval.token) & RESTRICT) goto invalid_type; @@ -196,13 +198,22 @@ specifier(int8_t *sclass) case TYPE: switch (yylval.token) { case ENUM: - dcl = enumdcl; p = &type; break; - case STRUCT: case UNION: - dcl = structdcl; p = &type; break; - case VOID: case BOOL: case CHAR: - case INT: case FLOAT: case DOUBLE: + dcl = enumdcl; + p = &type; break; + case STRUCT: + case UNION: + dcl = structdcl; p = &type; break; - case SIGNED: case UNSIGNED: + case VOID: + case BOOL: + case CHAR: + case INT: + case FLOAT: + case DOUBLE: + p = &type; + break; + case SIGNED: + case UNSIGNED: p = &sign; break; case LONG: if (size == LONG) { @@ -210,7 +221,8 @@ specifier(int8_t *sclass) break; } case SHORT: - p = &size; break; + p = &size; + break; } break; default: @@ -262,8 +274,8 @@ static Symbol * newtag(void) { Symbol *sym; - uint8_t tag = yylval.token; - static uint8_t ns = NS_STRUCTS; + unsigned tag = yylval.token; + static unsigned ns = NS_STRUCTS; setnamespace(NS_TAG); next(); @@ -297,7 +309,7 @@ structdcl(void) { Type *tagtype, *buff[NR_MAXSTRUCTS], **bp = &buff[0]; Symbol *tagsym, *sym; - uint8_t n; + unsigned n; size_t siz; tagsym = newtag(); @@ -391,7 +403,7 @@ static Symbol * parameter(void) { Symbol *sym; - uint8_t sclass; + unsigned sclass; Type *tp; if ((tp = specifier(&sclass)) == voidtype) @@ -421,7 +433,7 @@ decl(void) { Type *tp; Symbol *sym; - uint8_t sclass, isfun; + unsigned sclass, isfun; extern jmp_buf recover; setsafe(END_DECL); @@ -475,7 +487,7 @@ bad_function: Type * typename(void) { - int8_t sclass; + unsigned sclass; Type *tp; Symbol *sym; @@ -490,7 +502,7 @@ void extdecl(void) { Type *base, *tp; - uint8_t sclass; + unsigned sclass; Symbol *sym; extern Symbol *curfun; extern jmp_buf recover; diff --git a/cc1/error.c b/cc1/error.c @@ -9,11 +9,11 @@ #define MAXERRNUM 10 -extern uint8_t failure; -static uint8_t nerrors; +extern int failure; +static unsigned nerrors; static void -warn_helper(int8_t flag, char *fmt, va_list va) +warn_helper(int flag, char *fmt, va_list va) { if (!flag) return; @@ -30,7 +30,7 @@ warn_helper(int8_t flag, char *fmt, va_list va) void warn(char *fmt, ...) { - extern uint8_t warnings; + extern int warnings; va_list va; va_start(va, fmt); diff --git a/cc1/expr.c b/cc1/expr.c @@ -16,7 +16,7 @@ static Node * promote(Node *np) { Type *tp; - uint8_t r; + unsigned r; tp = np->type; r = tp->n.rank; @@ -31,7 +31,7 @@ typeconv(Node **p1, Node **p2) { Type *tp1, *tp2; Node *np1, *np2; - int8_t n; + int n; np1 = promote(*p1); np2 = promote(*p2); @@ -280,7 +280,7 @@ compare(char op, Node *lp, Node *rp) Node * negate(Node *np) { - uint8_t op; + unsigned op; switch (np->op) { case OAND: op = OOR; break; diff --git a/cc1/lex.c b/cc1/lex.c @@ -23,13 +23,13 @@ struct input { struct input *next; }; -static uint8_t lex_ns = NS_IDEN; +static unsigned lex_ns = NS_IDEN; -uint8_t yytoken; +unsigned yytoken; struct yystype yylval; char yytext[IDENTSIZ + 1]; unsigned short yylen; -static uint8_t safe; +static int safe; static Input *input; bool @@ -217,12 +217,12 @@ tok2str(void) input->begin = input->p; } -static uint8_t +static unsigned integer(char *s, char base) { Type *tp; Symbol *sym; - uint8_t size, sign; + unsigned size, sign; long v; for (size = sign = 0; ; ++input->p) { @@ -256,7 +256,7 @@ convert: } static char * -digits(uint8_t base) +digits(unsigned base) { char c, *p; @@ -282,10 +282,9 @@ end: return yytext; } -static uint8_t +static unsigned number(void) { - int ch; char base; if (*input->p != '0') { @@ -333,7 +332,7 @@ escape(void) return c; } -static uint8_t +static unsigned character(void) { static char c; @@ -352,7 +351,7 @@ character(void) return CONSTANT; } -static uint8_t +static unsigned string(void) { char buf[STRINGSIZ+1]; @@ -393,24 +392,22 @@ repeat: return CONSTANT; } -static uint8_t +static unsigned iden(void) { char *p; - Symbol *sym; for (p = input->p; isalnum(*p) || *p == '_'; ++p) /* nothing */; input->p = p; tok2str(); - sym = yylval.sym = lookup(lex_ns); - if (sym->token == IDEN) - return IDEN; - yylval.token = sym->u.token; - return sym->token; + yylval.sym = lookup(lex_ns); + if (yylval.sym->token != IDEN) + yylval.token = yylval.sym->u.token; + return yylval.sym->token; } -static uint8_t +static unsigned follow(int expect, int ifyes, int ifno) { if (*input->p++ == expect) @@ -419,7 +416,7 @@ follow(int expect, int ifyes, int ifno) return ifno; } -static uint8_t +static unsigned minus(void) { switch (*input->p++) { @@ -430,7 +427,7 @@ minus(void) } } -static uint8_t +static unsigned plus(void) { switch (*input->p++) { @@ -440,8 +437,8 @@ plus(void) } } -static uint8_t -relational(uint8_t op, uint8_t equal, uint8_t shift, uint8_t assig) +static unsigned +relational(int op, int equal, int shift, int assig) { char c; @@ -453,8 +450,8 @@ relational(uint8_t op, uint8_t equal, uint8_t shift, uint8_t assig) return op; } -static uint8_t -logic(uint8_t op, uint8_t equal, uint8_t logic) +static unsigned +logic(int op, int equal, int logic) { char c; @@ -466,7 +463,7 @@ logic(uint8_t op, uint8_t equal, uint8_t logic) return op; } -static uint8_t +static unsigned dot(void) { char c; @@ -479,10 +476,10 @@ dot(void) return ELLIPSIS; } -static uint8_t +static unsigned operator(void) { - uint8_t t; + unsigned t; switch (t = *input->p++) { case '<': t = relational('<', LE, SHL, SHL_EQ); break; @@ -504,12 +501,12 @@ operator(void) /* TODO: Ensure that lex_ns is NS_IDEN after a recovery */ void -setnamespace(uint8_t ns) +setnamespace(int ns) { lex_ns = ns; } -uint8_t +unsigned next(void) { char c; @@ -537,7 +534,7 @@ next(void) } void -expect(uint8_t tok) +expect(unsigned tok) { if (yytoken != tok) { if (isgraph(tok)) @@ -549,7 +546,7 @@ expect(uint8_t tok) } } -uint8_t +char ahead(void) { int c; @@ -568,7 +565,7 @@ repeat: } void -setsafe(uint8_t type) +setsafe(int type) { safe = type; } diff --git a/cc1/main.c b/cc1/main.c @@ -11,7 +11,7 @@ extern void ikeywords(void), lexfile(char *file); -uint8_t warnings; +int warnings; jmp_buf recover; static char *output; @@ -19,7 +19,7 @@ static char *output; static void clean(void) { - extern uint8_t failure; + extern int failure; if (failure && output) remove(output); diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -4,21 +4,22 @@ #include <string.h> #include "../inc/cc.h" +#include "../inc/sizes.h" #include "cc1.h" #define NR_SYM_HASH 32 -uint8_t curctx; +static unsigned curctx; static short localcnt; static short globalcnt; static Symbol *head; static Symbol *htab[NR_SYM_HASH]; -static inline uint8_t +static inline unsigned hash(const char *s) { - uint8_t h, ch; + unsigned h, ch; for (h = 0; ch = *s++; h += ch) /* nothing */; @@ -28,7 +29,8 @@ hash(const char *s) void pushctx(void) { - ++curctx; + if (++curctx == NR_BLOCK) + error("too much nested blocks"); } void @@ -60,7 +62,7 @@ popctx(void) } Symbol * -newsym(uint8_t ns) +newsym(unsigned ns) { Symbol *sym; @@ -79,10 +81,10 @@ newsym(uint8_t ns) } Symbol * -lookup(uint8_t ns) +lookup(unsigned ns) { Symbol *sym, **h; - uint8_t sns; + unsigned sns; char *t, c; h = &htab[hash(yytext)]; @@ -108,7 +110,7 @@ lookup(uint8_t ns) } Symbol * -install(uint8_t ns) +install(unsigned ns) { Symbol *sym, **h; /* @@ -139,7 +141,7 @@ ikeywords(void) { static struct { char *str; - uint8_t token, value; + unsigned char token, value; } *bp, buff[] = { {"auto", SCLASS, AUTO}, {"break", BREAK, BREAK}, diff --git a/cc1/types.c b/cc1/types.c @@ -135,7 +135,7 @@ Symbol *zero = &dummy0, *one = &dummy1; Type * -ctype(uint8_t type, uint8_t sign, uint8_t size) +ctype(unsigned type, unsigned sign, unsigned size) { switch (type) { case CHAR: @@ -204,10 +204,11 @@ invalid_type: } Type * -mktype(Type *tp, uint8_t op, short nelem, void *data) +mktype(Type *tp, unsigned op, short nelem, void *data) { - static Type *typetab[NR_TYPE_HASH], **tbl, type; - static uint8_t t; + static Type *typetab[NR_TYPE_HASH]; + Type **tbl, type; + unsigned t; Type *bp; static char letters[] = { [PTR] = L_POINTER, [ARY] = L_ARRAY, @@ -230,7 +231,7 @@ mktype(Type *tp, uint8_t op, short nelem, void *data) else type.defined = 1; - t = (op ^ (uint8_t) ((unsigned short) tp >> 3)) & NR_TYPE_HASH-1; + t = (op ^ (char) ((unsigned short) tp >> 3)) & NR_TYPE_HASH-1; tbl = &typetab[t]; for (bp = *tbl; bp; bp = bp->next) { if (eqtype(bp, &type)) { @@ -249,7 +250,7 @@ mktype(Type *tp, uint8_t op, short nelem, void *data) bool eqtype(Type *tp1, Type *tp2) { - uint8_t n; + unsigned n; Type **p1, **p2; if (tp1 == tp2) diff --git a/lib/die.c b/lib/die.c @@ -6,7 +6,7 @@ #include "../inc/cc.h" -uint8_t failure; +int failure; void die(const char *fmt, ...)