scc

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

commit 4e94d4039945fec694b6a74b2bcf03c5ffc71186
parent 242c473f7221643384498dd67ee2717f31482ffd
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 31 May 2012 23:11:42 +0200

Moved type stack to types.c

All this stuff is related to types, and this file it is a better place for
it. Also added control of number of modifiers.

Diffstat:
Mdecl.c | 30+++++++++---------------------
Mtypes.c | 22+++++++++++++++++++++-
Mtypes.h | 5+++++
3 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/decl.c b/decl.c @@ -9,19 +9,10 @@ #include "types.h" -/* ANSI C says minimum maximum for indirection level is 12 */ -#define PTRLEVEL_MAX 12 - char parser_out_home; static unsigned char symhash; static char symname[TOKSIZ_MAX + 1]; -static unsigned char stack[30]; -static unsigned char *stackp = stack; - -#define push(x) (*stackp++ = (x)) -#define pop() (*--stackp) -#define empty() (stackp == stack) #include <stdio.h> /* TODO: remove this */ @@ -49,14 +40,14 @@ void dirdcl(void) for (;;) { switch (yytoken) { case '(': - push(FTN); + pushtype(FTN); if (gettok() == ')') gettok(); else /* TODO: prototyped function */; continue; case '[': - push(ARY); + pushtype(ARY); if (gettok() == ']') gettok(); else @@ -229,10 +220,10 @@ void decl(void) dirdcl(); while (bp-- != qlf) { - if (*bp & 1) push(CONST); - if (*bp & 2) push(RESTRICTED); - if (*bp & 4) push(VOLATILE); - push(PTR); + if (*bp & 1) pushtype(CONST); + if (*bp & 2) pushtype(RESTRICTED); + if (*bp & 4) pushtype(VOLATILE); + pushtype(PTR); } printf("leaving dcl %c\n", yytoken); @@ -246,22 +237,19 @@ duplicated: void declaration(void) { - struct type *t; + struct type *t, *spec; struct symbol *sym; repeat: - t = specifier(); + spec = specifier(); for (; ; gettok()) { /* TODO: put here savepoint for error recovering */ decl(); if (yytoken != ',' && yytoken != ';') error("unexpected", yytext); - while (!empty()) - t = mktype(t, pop()); + t = decl_type(spec); ptype(t); - sym = alloca(sizeof(*sym)); - addsym(siden, sym, symhash); if (yytoken == ',') continue; diff --git a/types.c b/types.c @@ -5,7 +5,7 @@ #include "tokens.h" #include "types.h" - +/* TODO: create wrapper file */ #define xcalloc calloc struct type tschar = {.btype = CHAR, .sign = 1}; @@ -23,6 +23,26 @@ struct type tllong = {.btype = LLONG, .sign = 1}; struct type tullong = {.btype = LLONG, .sign = 0}; struct type tvoid = {.btype = VOID, .sign = 0}; +#define TYPEOP_MAX PTRLEVEL_MAX /* TODO: take a look of the ANSI standard */ + +static unsigned char stack[TYPEOP_MAX]; +static unsigned char *stackp = stack; + + +void pushtype(char op) +{ + if (stackp == stack + TYPEOP_MAX) + error("Too much type modifiers"); + *stackp++ = op; +} + +struct type *decl_type(struct type *t) +{ + while (stackp != stack) + t = mktype(t, *--stackp); + return t; +} + struct type *mktype(register struct type *base, unsigned char op) { register struct type **ptr, *nt; diff --git a/types.h b/types.h @@ -49,6 +49,11 @@ extern struct type tulong, tllong, tullong, tvoid; struct type *mktype(register struct type *base, unsigned char op); +/* ANSI C says minimum maximum for indirection level is 12 */ +#define PTRLEVEL_MAX 12 + +extern struct type *decl_type(struct type *t); +extern void pushtype(char op); #ifndef NDEBUG extern void ptype(register struct type *t);