scc

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

commit b08bb3cddcba51334aca7b26a31172698116cf21
parent 32ec291ea9f9d16743fee4a53a0c100785643483
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  7 May 2015 07:29:07 +0200

Rewrite cc1.h

This header was a bit caotic, and a lot of stuff could be written
in anther way. It has several sections, so it is easier to find things.
The commit also removes some legacy code (several macros non used anymore
and some other similar things)

Diffstat:
Mcc1/cc1.h | 322++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mcc1/decl.c | 8++++----
Mcc1/lex.c | 1+
Mcc1/types.c | 10+++++-----
4 files changed, 222 insertions(+), 119 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -1,53 +1,27 @@ -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); - -enum { - END_DECL, - END_LDECL, - END_COMP, - END_COND -}; - -/* definitions of types */ - -#define CTX_OUTER 0 -#define CTX_FUNC 1 - -enum { - NS_IDEN = 0, - NS_TAG, - NS_LABEL, - NS_STRUCTS, - NR_NAMESPACES -}; - -typedef struct ctype Type; +/* + * Definition of structures + */ +typedef struct type Type; typedef struct symbol Symbol; typedef struct caselist Caselist; typedef struct node Node; -struct ctype { +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) */ - struct ctype *type; /* base type */ - struct ctype *next; /* next element in the hash */ - Type **pars; /* type parameters */ + bool defined; /* type defined (is not a forward reference) */ + 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 */ } n; }; - -/* definition of symbols */ - struct symbol { char *name; Type *type; @@ -71,15 +45,15 @@ struct symbol { struct symbol *hash; }; -extern bool eqtype(Type *tp1, Type *tp2); -extern Type *ctype(int8_t type, int8_t sign, int8_t size), - *mktype(Type *tp, uint8_t op, short nelem, void *data); - -extern Symbol - *lookup(char *s, unsigned char ns), - *install(char *s, unsigned char ns); - -extern void pushctx(void), popctx(void); +struct node { + uint8_t op; + Type *type; + Symbol *sym; + bool lvalue : 1; + bool symbol: 1; + bool constant : 1; + struct node *left, *right; +}; struct scase { Symbol *label; @@ -93,81 +67,162 @@ struct caselist { struct scase *head; }; -extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch); - -extern Type *typename(void); +struct yystype { + Symbol *sym; + uint8_t token; +}; -extern Type *voidtype, *pvoidtype, *booltype, - *uchartype, *chartype, - *uinttype, *inttype, - *ushortype, *shortype, - *longtype, *ulongtype, - *ullongtype, *llongtype, - *floattype, *doubletype, *ldoubletype; +/* + * Definition of enumerations + */ +/* recovery points */ enum { - FTN = 1, ENUM, TYPEIDEN, VOID, FLOAT, INT, BOOL, - STRUCT, UNION, PTR, ARY, CHAR, DOUBLE, SHORT, - LONG, COMPLEX, UNSIGNED, SIGNED + END_DECL, + END_LDECL, + END_COMP, + END_COND }; -#define CONST (1<<0) -#define VOLATILE (1<<1) -#define RESTRICT (1<<2) - -#define TYPEDEF 1 -#define EXTERN 2 -#define STATIC 3 -#define AUTO 4 -#define REGISTER 5 - -#define accept(t) ((yytoken == (t)) ? next() : 0) -extern uint8_t ahead(void); - -enum tokens { - TQUALIFIER = 128, TYPE, IDEN, SCLASS, - CONSTANT, SIZEOF, - INDIR, INC, DEC, SHL, SHR, - LE, GE, EQ, NE, AND, OR, - MUL_EQ, DIV_EQ, MOD_EQ, ADD_EQ, SUB_EQ, AND_EQ, - XOR_EQ, OR_EQ, SHL_EQ, SHR_EQ, - ELLIPSIS, STRING, - CASE, DEFAULT, IF, ELSE, SWITCH, WHILE, DO, FOR, GOTO, - CONTINUE, BREAK, RETURN, EOFTOK, NOTOK +/* type constructors */ +enum { + FTN = 1, + PTR, + ARY, }; -struct yystype { - Symbol *sym; - uint8_t token; +/* namespaces */ +enum { + NS_IDEN, + NS_TAG, + NS_LABEL, + NS_STRUCTS, + NR_NAMESPACES }; -extern struct yystype yylval; -extern char yytext[]; -extern uint8_t yytoken; - -extern uint8_t next(void); -extern void expect(uint8_t tok); - -struct node { - uint8_t op; - Type *type; - Symbol *sym; - bool lvalue : 1; - bool symbol: 1; - bool constant : 1; - struct node *left, *right; +/* input tokens */ +enum tokens { + TQUALIFIER = 128, + TYPE, + IDEN, + SCLASS, + CONSTANT, + SIZEOF, + INDIR, + INC, + DEC, + SHL, + SHR, + LE, + GE, + EQ, + NE, + AND, + OR, + MUL_EQ, + DIV_EQ, + MOD_EQ, + ADD_EQ, + SUB_EQ, + AND_EQ, + XOR_EQ, + OR_EQ, + SHL_EQ, + SHR_EQ, + ELLIPSIS, + STRING, /* TODO: remove this */ + CASE, + DEFAULT, + IF, + ELSE, + SWITCH, + WHILE, + DO, + FOR, + GOTO, + VOID, + FLOAT, + INT, + BOOL, + STRUCT, + UNION, + CHAR, + DOUBLE, + SHORT, + LONG, + LLONG, + COMPLEX, + CONST, + VOLATILE, + RESTRICT, + TYPEDEF, + EXTERN, + STATIC, + AUTO, + REGISTER, + ENUM, + TYPEIDEN, + UNSIGNED, + SIGNED, + CONTINUE, + BREAK, + RETURN, + EOFTOK, + NOTOK }; +/* operations */ enum { - OPTR = 1, OADD, OSIZE, OMUL, OSUB, - OINC, ODEC, ODIV, OMOD, OSHL, OSHR, - OBAND, OBXOR, OBOR, OASSIGN, OA_MUL, OA_DIV, - OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, - OA_AND, OA_XOR, OA_OR, OADDR,ONEG, OCPL, OEXC, - OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP, - OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH, - OEXPR, OEFUN, OESTRUCT, OELOOP, OBLOOP, OPRINT, - OFUN, ORET, ODECL, OSWITCH, + OPTR = 1, + OADD, + OSIZE, + OMUL, + OSUB, + OINC, + ODEC, + ODIV, + OMOD, + OSHL, + OSHR, + OBAND, + OBXOR, + OBOR, + OASSIGN, + OA_MUL, + OA_DIV, + OA_MOD, + OA_ADD, + OA_SUB, + OA_SHL, + OA_SHR, + OA_AND, + OA_XOR, + OA_OR, + OADDR,ONEG, + OCPL, + OEXC, + OCOMMA, + OCAST, + OSYM, + OASK, + OFIELD, + OTYP, + OLABEL, + ODEFAULT, + OCASE, + OSTRUCT, + OJUMP, + OBRANCH, + OEXPR, + OEFUN, + OESTRUCT, + OELOOP, + OBLOOP, + OPRINT, + OFUN, + ORET, + ODECL, + OSWITCH, /* TODO: This order is important, but must be changed */ OAND, OOR, /* @@ -177,14 +232,61 @@ enum { OEQ = 0x40, ONE, OLT, OGE, OLE, OGT }; +/* error.c */ +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 */ +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); + +/* symbol.c */ +extern Symbol *lookup(char *s, unsigned char ns); +extern Symbol *install(char *s, unsigned char ns); +extern void pushctx(void), popctx(void); + +/* stmt.c */ +extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch); + +/* decl.c */ +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); +#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 Node *symbol(Symbol *sym); extern void freetree(Node *np); +/* expr.c */ +extern Node *expr(void); + +/* + * Definition of global variables + */ +extern struct yystype yylval; +extern char yytext[]; +extern uint8_t yytoken; + +extern Type *voidtype, *pvoidtype, *booltype, + *uchartype, *chartype, + *uinttype, *inttype, + *ushortype, *shortype, + *longtype, *ulongtype, + *ullongtype, *llongtype, + *floattype, *doubletype, *ldoubletype; + +/* TODO: remove this ugly macros */ #define NEGATE(n, v) ((n)->op ^= (v)) #define ISNODECMP(n) ((n)->op >= OEQ) #define ISNODELOG(n) ((n)->op >= OAND) - -extern Node *expr(void); -extern void extdecl(void), decl(void); diff --git a/cc1/decl.c b/cc1/decl.c @@ -215,7 +215,7 @@ specifier(int8_t *sclass) p = &sign; break; case LONG: if (size == LONG) { - size = LONG+LONG; + size = LLONG; break; } case SHORT: @@ -397,7 +397,7 @@ static Symbol * parameter(void) { Symbol *sym; - int8_t sclass; + uint8_t sclass; Type *tp; if ((tp = specifier(&sclass)) == voidtype) @@ -427,7 +427,7 @@ decl(void) { Type *tp; Symbol *sym; - int8_t sclass, isfun; + uint8_t sclass, isfun; extern jmp_buf recover; setsafe(END_DECL); @@ -495,7 +495,7 @@ void extdecl(void) { Type *base, *tp; - int8_t sclass; + uint8_t sclass; Symbol *sym; extern Symbol *curfun; extern jmp_buf recover; diff --git a/cc1/lex.c b/cc1/lex.c @@ -370,6 +370,7 @@ next(void) return yytoken; } +/* TODO: Remove calls to expect without a character */ void expect(uint8_t tok) { diff --git a/cc1/types.c b/cc1/types.c @@ -135,7 +135,7 @@ Symbol *zero = &dummy0, *one = &dummy1; Type * -ctype(int8_t type, int8_t sign, int8_t size) +ctype(uint8_t type, uint8_t sign, uint8_t size) { switch (type) { case CHAR: @@ -171,17 +171,17 @@ ctype(int8_t type, int8_t sign, int8_t size) return (sign == UNSIGNED) ? ushortype : shortype; case LONG: return (sign == UNSIGNED) ? ulongtype : longtype; - case LONG+LONG: + case LLONG: return (sign == UNSIGNED) ? ullongtype : llongtype; } break; case DOUBLE: - if (size == LONG+LONG) + if (size == LLONG) goto invalid_type; size += LONG; goto floating; case FLOAT: - if (size == LONG+LONG) + if (size == LLONG) goto invalid_type; floating: if (sign) @@ -191,7 +191,7 @@ ctype(int8_t type, int8_t sign, int8_t size) return floattype; case LONG: return doubletype; - case LONG+LONG: + case LLONG: return ldoubletype; } break;