OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.23/] [lcc/] [src/] [decl.c] - Diff between revs 4 and 157

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 157
#include "c.h"
#include "c.h"
 
 
static char rcsid[] = "$Id: decl.c,v 1.1 2002/08/28 23:12:42 drh Exp $";
static char rcsid[] = "$Id: decl.c,v 1.1 2002/08/28 23:12:42 drh Exp $";
 
 
#define add(x,n) (x > inttype->u.sym->u.limits.max.i-(n) ? (overflow=1,x) : x+(n))
#define add(x,n) (x > inttype->u.sym->u.limits.max.i-(n) ? (overflow=1,x) : x+(n))
#define chkoverflow(x,n) ((void)add(x,n))
#define chkoverflow(x,n) ((void)add(x,n))
#define bits2bytes(n) (((n) + 7)/8)
#define bits2bytes(n) (((n) + 7)/8)
static int regcount;
static int regcount;
 
 
static List autos, registers;
static List autos, registers;
Symbol cfunc;           /* current function */
Symbol cfunc;           /* current function */
Symbol retv;            /* return value location for structs */
Symbol retv;            /* return value location for structs */
 
 
static void checkref(Symbol, void *);
static void checkref(Symbol, void *);
static Symbol dclglobal(int, char *, Type, Coordinate *);
static Symbol dclglobal(int, char *, Type, Coordinate *);
static Symbol dcllocal(int, char *, Type, Coordinate *);
static Symbol dcllocal(int, char *, Type, Coordinate *);
static Symbol dclparam(int, char *, Type, Coordinate *);
static Symbol dclparam(int, char *, Type, Coordinate *);
static Type dclr(Type, char **, Symbol **, int);
static Type dclr(Type, char **, Symbol **, int);
static Type dclr1(char **, Symbol **, int);
static Type dclr1(char **, Symbol **, int);
static void decl(Symbol (*)(int, char *, Type, Coordinate *));
static void decl(Symbol (*)(int, char *, Type, Coordinate *));
extern void doconst(Symbol, void *);
extern void doconst(Symbol, void *);
static void doglobal(Symbol, void *);
static void doglobal(Symbol, void *);
static void doextern(Symbol, void *);
static void doextern(Symbol, void *);
static void exitparams(Symbol []);
static void exitparams(Symbol []);
static void fields(Type);
static void fields(Type);
static void funcdefn(int, char *, Type, Symbol [], Coordinate);
static void funcdefn(int, char *, Type, Symbol [], Coordinate);
static void initglobal(Symbol, int);
static void initglobal(Symbol, int);
static void oldparam(Symbol, void *);
static void oldparam(Symbol, void *);
static Symbol *parameters(Type);
static Symbol *parameters(Type);
static Type specifier(int *);
static Type specifier(int *);
static Type structdcl(int);
static Type structdcl(int);
static Type tnode(int, Type);
static Type tnode(int, Type);
void program(void) {
void program(void) {
        int n;
        int n;
 
 
        level = GLOBAL;
        level = GLOBAL;
        for (n = 0; t != EOI; n++)
        for (n = 0; t != EOI; n++)
                if (kind[t] == CHAR || kind[t] == STATIC
                if (kind[t] == CHAR || kind[t] == STATIC
                || t == ID || t == '*' || t == '(') {
                || t == ID || t == '*' || t == '(') {
                        decl(dclglobal);
                        decl(dclglobal);
                        deallocate(STMT);
                        deallocate(STMT);
                        if (!(glevel >= 3 || xref))
                        if (!(glevel >= 3 || xref))
                        deallocate(FUNC);
                        deallocate(FUNC);
                } else if (t == ';') {
                } else if (t == ';') {
                        warning("empty declaration\n");
                        warning("empty declaration\n");
                        t = gettok();
                        t = gettok();
                } else {
                } else {
                        error("unrecognized declaration\n");
                        error("unrecognized declaration\n");
                        t = gettok();
                        t = gettok();
                }
                }
        if (n == 0)
        if (n == 0)
                warning("empty input file\n");
                warning("empty input file\n");
}
}
static Type specifier(int *sclass) {
static Type specifier(int *sclass) {
        int cls, cons, sign, size, type, vol;
        int cls, cons, sign, size, type, vol;
        Type ty = NULL;
        Type ty = NULL;
 
 
        cls = vol = cons = sign = size = type = 0;
        cls = vol = cons = sign = size = type = 0;
        if (sclass == NULL)
        if (sclass == NULL)
                cls = AUTO;
                cls = AUTO;
        for (;;) {
        for (;;) {
                int *p, tt = t;
                int *p, tt = t;
                switch (t) {
                switch (t) {
                case AUTO:
                case AUTO:
                case REGISTER: if (level <= GLOBAL && cls == 0)
                case REGISTER: if (level <= GLOBAL && cls == 0)
                                error("invalid use of `%k'\n", t);
                                error("invalid use of `%k'\n", t);
                               p = &cls;  t = gettok();      break;
                               p = &cls;  t = gettok();      break;
                case STATIC: case EXTERN:
                case STATIC: case EXTERN:
                case TYPEDEF:  p = &cls;  t = gettok();      break;
                case TYPEDEF:  p = &cls;  t = gettok();      break;
                case CONST:    p = &cons; t = gettok();      break;
                case CONST:    p = &cons; t = gettok();      break;
                case VOLATILE: p = &vol;  t = gettok();      break;
                case VOLATILE: p = &vol;  t = gettok();      break;
                case SIGNED:
                case SIGNED:
                case UNSIGNED: p = &sign; t = gettok();      break;
                case UNSIGNED: p = &sign; t = gettok();      break;
                case LONG:     if (size == LONG) {
                case LONG:     if (size == LONG) {
                                       size = 0;
                                       size = 0;
                                       tt = LONG+LONG;
                                       tt = LONG+LONG;
                               }
                               }
                               p = &size; t = gettok();      break;
                               p = &size; t = gettok();      break;
                case SHORT:    p = &size; t = gettok();      break;
                case SHORT:    p = &size; t = gettok();      break;
                case VOID: case CHAR: case INT: case FLOAT:
                case VOID: case CHAR: case INT: case FLOAT:
                case DOUBLE:   p = &type; ty = tsym->type;
                case DOUBLE:   p = &type; ty = tsym->type;
                                          t = gettok();      break;
                                          t = gettok();      break;
                case ENUM:     p = &type; ty = enumdcl();    break;
                case ENUM:     p = &type; ty = enumdcl();    break;
                case STRUCT:
                case STRUCT:
                case UNION:    p = &type; ty = structdcl(t); break;
                case UNION:    p = &type; ty = structdcl(t); break;
                case ID:
                case ID:
                        if (istypename(t, tsym) && type == 0
                        if (istypename(t, tsym) && type == 0
                        && sign == 0 && size == 0) {
                        && sign == 0 && size == 0) {
                                use(tsym, src);
                                use(tsym, src);
                                ty = tsym->type;
                                ty = tsym->type;
                                if (isqual(ty)
                                if (isqual(ty)
                                && ty->size != ty->type->size) {
                                && ty->size != ty->type->size) {
                                        ty = unqual(ty);
                                        ty = unqual(ty);
                                        if (isconst(tsym->type))
                                        if (isconst(tsym->type))
                                                ty = qual(CONST, ty);
                                                ty = qual(CONST, ty);
                                        if (isvolatile(tsym->type))
                                        if (isvolatile(tsym->type))
                                                ty = qual(VOLATILE, ty);
                                                ty = qual(VOLATILE, ty);
                                        tsym->type = ty;
                                        tsym->type = ty;
                                }
                                }
                                p = &type;
                                p = &type;
                                t = gettok();
                                t = gettok();
                        } else
                        } else
                                p = NULL;
                                p = NULL;
                        break;
                        break;
                default: p = NULL;
                default: p = NULL;
                }
                }
                if (p == NULL)
                if (p == NULL)
                        break;
                        break;
                if (*p)
                if (*p)
                        error("invalid use of `%k'\n", tt);
                        error("invalid use of `%k'\n", tt);
                *p = tt;
                *p = tt;
        }
        }
        if (sclass)
        if (sclass)
                *sclass = cls;
                *sclass = cls;
        if (type == 0) {
        if (type == 0) {
                type = INT;
                type = INT;
                ty = inttype;
                ty = inttype;
        }
        }
        if (size == SHORT     && type != INT
        if (size == SHORT     && type != INT
        ||  size == LONG+LONG && type != INT
        ||  size == LONG+LONG && type != INT
        ||  size == LONG      && type != INT && type != DOUBLE
        ||  size == LONG      && type != INT && type != DOUBLE
        ||  sign && type != INT && type != CHAR)
        ||  sign && type != INT && type != CHAR)
                error("invalid type specification\n");
                error("invalid type specification\n");
        if (type == CHAR && sign)
        if (type == CHAR && sign)
                ty = sign == UNSIGNED ? unsignedchar : signedchar;
                ty = sign == UNSIGNED ? unsignedchar : signedchar;
        else if (size == SHORT)
        else if (size == SHORT)
                ty = sign == UNSIGNED ? unsignedshort : shorttype;
                ty = sign == UNSIGNED ? unsignedshort : shorttype;
        else if (size == LONG && type == DOUBLE)
        else if (size == LONG && type == DOUBLE)
                ty = longdouble;
                ty = longdouble;
        else if (size == LONG+LONG) {
        else if (size == LONG+LONG) {
                ty = sign == UNSIGNED ? unsignedlonglong : longlong;
                ty = sign == UNSIGNED ? unsignedlonglong : longlong;
                if (Aflag >= 1)
                if (Aflag >= 1)
                        warning("`%t' is a non-ANSI type\n", ty);
                        warning("`%t' is a non-ANSI type\n", ty);
        } else if (size == LONG)
        } else if (size == LONG)
                ty = sign == UNSIGNED ? unsignedlong : longtype;
                ty = sign == UNSIGNED ? unsignedlong : longtype;
        else if (sign == UNSIGNED && type == INT)
        else if (sign == UNSIGNED && type == INT)
                ty = unsignedtype;
                ty = unsignedtype;
        if (cons == CONST)
        if (cons == CONST)
                ty = qual(CONST, ty);
                ty = qual(CONST, ty);
        if (vol  == VOLATILE)
        if (vol  == VOLATILE)
                ty = qual(VOLATILE, ty);
                ty = qual(VOLATILE, ty);
        return ty;
        return ty;
}
}
static void decl(Symbol (*dcl)(int, char *, Type, Coordinate *)) {
static void decl(Symbol (*dcl)(int, char *, Type, Coordinate *)) {
        int sclass;
        int sclass;
        Type ty, ty1;
        Type ty, ty1;
        static char stop[] = { CHAR, STATIC, ID, 0 };
        static char stop[] = { CHAR, STATIC, ID, 0 };
 
 
        ty = specifier(&sclass);
        ty = specifier(&sclass);
        if (t == ID || t == '*' || t == '(' || t == '[') {
        if (t == ID || t == '*' || t == '(' || t == '[') {
                char *id;
                char *id;
                Coordinate pos;
                Coordinate pos;
                id = NULL;
                id = NULL;
                pos = src;
                pos = src;
                if (level == GLOBAL) {
                if (level == GLOBAL) {
                        Symbol *params = NULL;
                        Symbol *params = NULL;
                        ty1 = dclr(ty, &id, &params, 0);
                        ty1 = dclr(ty, &id, &params, 0);
                        if (params && id && isfunc(ty1)
                        if (params && id && isfunc(ty1)
                            && (t == '{' || istypename(t, tsym)
                            && (t == '{' || istypename(t, tsym)
                            || (kind[t] == STATIC && t != TYPEDEF))) {
                            || (kind[t] == STATIC && t != TYPEDEF))) {
                                if (sclass == TYPEDEF) {
                                if (sclass == TYPEDEF) {
                                        error("invalid use of `typedef'\n");
                                        error("invalid use of `typedef'\n");
                                        sclass = EXTERN;
                                        sclass = EXTERN;
                                }
                                }
                                if (ty1->u.f.oldstyle)
                                if (ty1->u.f.oldstyle)
                                        exitscope();
                                        exitscope();
                                funcdefn(sclass, id, ty1, params, pos);
                                funcdefn(sclass, id, ty1, params, pos);
                                return;
                                return;
                        } else if (params)
                        } else if (params)
                                exitparams(params);
                                exitparams(params);
                } else
                } else
                        ty1 = dclr(ty, &id, NULL, 0);
                        ty1 = dclr(ty, &id, NULL, 0);
                for (;;) {
                for (;;) {
                        if (Aflag >= 1 && !hasproto(ty1))
                        if (Aflag >= 1 && !hasproto(ty1))
                                warning("missing prototype\n");
                                warning("missing prototype\n");
                        if (id == NULL)
                        if (id == NULL)
                                error("missing identifier\n");
                                error("missing identifier\n");
                        else if (sclass == TYPEDEF)
                        else if (sclass == TYPEDEF)
                                {
                                {
                                        Symbol p = lookup(id, identifiers);
                                        Symbol p = lookup(id, identifiers);
                                        if (p && p->scope == level)
                                        if (p && p->scope == level)
                                                error("redeclaration of `%s'\n", id);
                                                error("redeclaration of `%s'\n", id);
                                        p = install(id, &identifiers, level,
                                        p = install(id, &identifiers, level,
                                                level < LOCAL ? PERM : FUNC);
                                                level < LOCAL ? PERM : FUNC);
                                        p->type = ty1;
                                        p->type = ty1;
                                        p->sclass = TYPEDEF;
                                        p->sclass = TYPEDEF;
                                        p->src = pos;
                                        p->src = pos;
                                }
                                }
                        else
                        else
                                (void)(*dcl)(sclass, id, ty1, &pos);
                                (void)(*dcl)(sclass, id, ty1, &pos);
                        if (t != ',')
                        if (t != ',')
                                break;
                                break;
                        t = gettok();
                        t = gettok();
                        id = NULL;
                        id = NULL;
                        pos = src;
                        pos = src;
                        ty1 = dclr(ty, &id, NULL, 0);
                        ty1 = dclr(ty, &id, NULL, 0);
                }
                }
        } else if (ty == NULL
        } else if (ty == NULL
        || !(isenum(ty) ||
        || !(isenum(ty) ||
             isstruct(ty) && (*unqual(ty)->u.sym->name < '1' || *unqual(ty)->u.sym->name > '9')))
             isstruct(ty) && (*unqual(ty)->u.sym->name < '1' || *unqual(ty)->u.sym->name > '9')))
                error("empty declaration\n");
                error("empty declaration\n");
        test(';', stop);
        test(';', stop);
}
}
static Symbol dclglobal(int sclass, char *id, Type ty, Coordinate *pos) {
static Symbol dclglobal(int sclass, char *id, Type ty, Coordinate *pos) {
        Symbol p;
        Symbol p;
 
 
        if (sclass == 0)
        if (sclass == 0)
                sclass = AUTO;
                sclass = AUTO;
        else if (sclass != EXTERN && sclass != STATIC) {
        else if (sclass != EXTERN && sclass != STATIC) {
                error("invalid storage class `%k' for `%t %s'\n",
                error("invalid storage class `%k' for `%t %s'\n",
                        sclass, ty, id);
                        sclass, ty, id);
                sclass = AUTO;
                sclass = AUTO;
        }
        }
        p = lookup(id, identifiers);
        p = lookup(id, identifiers);
        if (p && p->scope == GLOBAL) {
        if (p && p->scope == GLOBAL) {
                if (p->sclass != TYPEDEF && eqtype(ty, p->type, 1))
                if (p->sclass != TYPEDEF && eqtype(ty, p->type, 1))
                        ty = compose(ty, p->type);
                        ty = compose(ty, p->type);
                else
                else
                        error("redeclaration of `%s' previously declared at %w\n", p->name, &p->src);
                        error("redeclaration of `%s' previously declared at %w\n", p->name, &p->src);
 
 
                if (!isfunc(ty) && p->defined && t == '=')
                if (!isfunc(ty) && p->defined && t == '=')
                        error("redefinition of `%s' previously defined at %w\n", p->name, &p->src);
                        error("redefinition of `%s' previously defined at %w\n", p->name, &p->src);
 
 
                if (p->sclass == EXTERN && sclass == STATIC
                if (p->sclass == EXTERN && sclass == STATIC
                ||  p->sclass == STATIC && sclass == AUTO
                ||  p->sclass == STATIC && sclass == AUTO
                ||  p->sclass == AUTO   && sclass == STATIC)
                ||  p->sclass == AUTO   && sclass == STATIC)
                        warning("inconsistent linkage for `%s' previously declared at %w\n", p->name, &p->src);
                        warning("inconsistent linkage for `%s' previously declared at %w\n", p->name, &p->src);
 
 
        }
        }
        if (p == NULL || p->scope != GLOBAL) {
        if (p == NULL || p->scope != GLOBAL) {
                Symbol q = lookup(id, externals);
                Symbol q = lookup(id, externals);
                if (q) {
                if (q) {
                        if (sclass == STATIC || !eqtype(ty, q->type, 1))
                        if (sclass == STATIC || !eqtype(ty, q->type, 1))
                                warning("declaration of `%s' does not match previous declaration at %w\n", id, &q->src);
                                warning("declaration of `%s' does not match previous declaration at %w\n", id, &q->src);
 
 
                        p = relocate(id, externals, globals);
                        p = relocate(id, externals, globals);
                        p->sclass = sclass;
                        p->sclass = sclass;
                } else {
                } else {
                        p = install(id, &globals, GLOBAL, PERM);
                        p = install(id, &globals, GLOBAL, PERM);
                        p->sclass = sclass;
                        p->sclass = sclass;
                        (*IR->defsymbol)(p);
                        (*IR->defsymbol)(p);
                }
                }
                if (p->sclass != STATIC) {
                if (p->sclass != STATIC) {
                        static int nglobals;
                        static int nglobals;
                        nglobals++;
                        nglobals++;
                        if (Aflag >= 2 && nglobals == 512)
                        if (Aflag >= 2 && nglobals == 512)
                                warning("more than 511 external identifiers\n");
                                warning("more than 511 external identifiers\n");
                }
                }
        } else if (p->sclass == EXTERN)
        } else if (p->sclass == EXTERN)
                p->sclass = sclass;
                p->sclass = sclass;
        p->type = ty;
        p->type = ty;
        p->src = *pos;
        p->src = *pos;
        if (t == '=' && isfunc(p->type)) {
        if (t == '=' && isfunc(p->type)) {
                error("illegal initialization for `%s'\n", p->name);
                error("illegal initialization for `%s'\n", p->name);
                t = gettok();
                t = gettok();
                initializer(p->type, 0);
                initializer(p->type, 0);
        } else if (t == '=') {
        } else if (t == '=') {
                initglobal(p, 0);
                initglobal(p, 0);
                if (glevel > 0 && IR->stabsym) {
                if (glevel > 0 && IR->stabsym) {
                        (*IR->stabsym)(p); swtoseg(p->u.seg); }
                        (*IR->stabsym)(p); swtoseg(p->u.seg); }
        } else if (p->sclass == STATIC && !isfunc(p->type)
        } else if (p->sclass == STATIC && !isfunc(p->type)
        && p->type->size == 0)
        && p->type->size == 0)
                error("undefined size for `%t %s'\n", p->type, p->name);
                error("undefined size for `%t %s'\n", p->type, p->name);
        return p;
        return p;
}
}
static void initglobal(Symbol p, int flag) {
static void initglobal(Symbol p, int flag) {
        Type ty;
        Type ty;
 
 
        if (t == '=' || flag) {
        if (t == '=' || flag) {
                if (p->sclass == STATIC) {
                if (p->sclass == STATIC) {
                        for (ty = p->type; isarray(ty); ty = ty->type)
                        for (ty = p->type; isarray(ty); ty = ty->type)
                                ;
                                ;
                        defglobal(p, isconst(ty) ? LIT : DATA);
                        defglobal(p, isconst(ty) ? LIT : DATA);
                } else
                } else
                        defglobal(p, DATA);
                        defglobal(p, DATA);
                if (t == '=')
                if (t == '=')
                        t = gettok();
                        t = gettok();
                ty = initializer(p->type, 0);
                ty = initializer(p->type, 0);
                if (isarray(p->type) && p->type->size == 0)
                if (isarray(p->type) && p->type->size == 0)
                        p->type = ty;
                        p->type = ty;
                if (p->sclass == EXTERN)
                if (p->sclass == EXTERN)
                        p->sclass = AUTO;
                        p->sclass = AUTO;
        }
        }
}
}
void defglobal(Symbol p, int seg) {
void defglobal(Symbol p, int seg) {
        p->u.seg = seg;
        p->u.seg = seg;
        swtoseg(p->u.seg);
        swtoseg(p->u.seg);
        if (p->sclass != STATIC)
        if (p->sclass != STATIC)
                (*IR->export)(p);
                (*IR->export)(p);
        (*IR->global)(p);
        (*IR->global)(p);
        p->defined = 1;
        p->defined = 1;
}
}
 
 
static Type dclr(Type basety, char **id, Symbol **params, int abstract) {
static Type dclr(Type basety, char **id, Symbol **params, int abstract) {
        Type ty = dclr1(id, params, abstract);
        Type ty = dclr1(id, params, abstract);
 
 
        for ( ; ty; ty = ty->type)
        for ( ; ty; ty = ty->type)
                switch (ty->op) {
                switch (ty->op) {
                case POINTER:
                case POINTER:
                        basety = ptr(basety);
                        basety = ptr(basety);
                        break;
                        break;
                case FUNCTION:
                case FUNCTION:
                        basety = func(basety, ty->u.f.proto,
                        basety = func(basety, ty->u.f.proto,
                                ty->u.f.oldstyle);
                                ty->u.f.oldstyle);
                        break;
                        break;
                case ARRAY:
                case ARRAY:
                        basety = array(basety, ty->size, 0);
                        basety = array(basety, ty->size, 0);
                        break;
                        break;
                case CONST: case VOLATILE:
                case CONST: case VOLATILE:
                        basety = qual(ty->op, basety);
                        basety = qual(ty->op, basety);
                        break;
                        break;
                default: assert(0);
                default: assert(0);
                }
                }
        if (Aflag >= 2 && basety->size > 32767)
        if (Aflag >= 2 && basety->size > 32767)
                warning("more than 32767 bytes in `%t'\n", basety);
                warning("more than 32767 bytes in `%t'\n", basety);
        return basety;
        return basety;
}
}
static Type tnode(int op, Type type) {
static Type tnode(int op, Type type) {
        Type ty;
        Type ty;
 
 
        NEW0(ty, STMT);
        NEW0(ty, STMT);
        ty->op = op;
        ty->op = op;
        ty->type = type;
        ty->type = type;
        return ty;
        return ty;
}
}
static Type dclr1(char **id, Symbol **params, int abstract) {
static Type dclr1(char **id, Symbol **params, int abstract) {
        Type ty = NULL;
        Type ty = NULL;
 
 
        switch (t) {
        switch (t) {
        case ID:                if (id)
        case ID:                if (id)
                                        *id = token;
                                        *id = token;
                                else
                                else
                                        error("extraneous identifier `%s'\n", token);
                                        error("extraneous identifier `%s'\n", token);
                                t = gettok(); break;
                                t = gettok(); break;
        case '*': t = gettok(); if (t == CONST || t == VOLATILE) {
        case '*': t = gettok(); if (t == CONST || t == VOLATILE) {
                                        Type ty1;
                                        Type ty1;
                                        ty1 = ty = tnode(t, NULL);
                                        ty1 = ty = tnode(t, NULL);
                                        while ((t = gettok()) == CONST || t == VOLATILE)
                                        while ((t = gettok()) == CONST || t == VOLATILE)
                                                ty1 = tnode(t, ty1);
                                                ty1 = tnode(t, ty1);
                                        ty->type = dclr1(id, params, abstract);
                                        ty->type = dclr1(id, params, abstract);
                                        ty = ty1;
                                        ty = ty1;
                                } else
                                } else
                                        ty = dclr1(id, params, abstract);
                                        ty = dclr1(id, params, abstract);
                                ty = tnode(POINTER, ty); break;
                                ty = tnode(POINTER, ty); break;
        case '(': t = gettok(); if (abstract
        case '(': t = gettok(); if (abstract
                                && (t == REGISTER || istypename(t, tsym) || t == ')')) {
                                && (t == REGISTER || istypename(t, tsym) || t == ')')) {
                                        Symbol *args;
                                        Symbol *args;
                                        ty = tnode(FUNCTION, ty);
                                        ty = tnode(FUNCTION, ty);
                                        enterscope();
                                        enterscope();
                                        if (level > PARAM)
                                        if (level > PARAM)
                                                enterscope();
                                                enterscope();
                                        args = parameters(ty);
                                        args = parameters(ty);
                                        exitparams(args);
                                        exitparams(args);
                                } else {
                                } else {
                                        ty = dclr1(id, params, abstract);
                                        ty = dclr1(id, params, abstract);
                                        expect(')');
                                        expect(')');
                                        if (abstract && ty == NULL
                                        if (abstract && ty == NULL
                                        && (id == NULL || *id == NULL))
                                        && (id == NULL || *id == NULL))
                                                return tnode(FUNCTION, NULL);
                                                return tnode(FUNCTION, NULL);
                                } break;
                                } break;
        case '[': break;
        case '[': break;
        default:  return ty;
        default:  return ty;
        }
        }
        while (t == '(' || t == '[')
        while (t == '(' || t == '[')
                switch (t) {
                switch (t) {
                case '(': t = gettok(); { Symbol *args;
                case '(': t = gettok(); { Symbol *args;
                                          ty = tnode(FUNCTION, ty);
                                          ty = tnode(FUNCTION, ty);
                                          enterscope();
                                          enterscope();
                                          if (level > PARAM)
                                          if (level > PARAM)
                                                enterscope();
                                                enterscope();
                                          args = parameters(ty);
                                          args = parameters(ty);
                                          if (params && *params == NULL)
                                          if (params && *params == NULL)
                                                *params = args;
                                                *params = args;
                                          else
                                          else
                                                exitparams(args);
                                                exitparams(args);
 }
 }
                          break;
                          break;
                case '[': t = gettok(); { int n = 0;
                case '[': t = gettok(); { int n = 0;
                                          if (kind[t] == ID) {
                                          if (kind[t] == ID) {
                                                n = intexpr(']', 1);
                                                n = intexpr(']', 1);
                                                if (n <= 0) {
                                                if (n <= 0) {
                                                        error("`%d' is an illegal array size\n", n);
                                                        error("`%d' is an illegal array size\n", n);
                                                        n = 1;
                                                        n = 1;
                                                }
                                                }
                                          } else
                                          } else
                                                expect(']');
                                                expect(']');
                                          ty = tnode(ARRAY, ty);
                                          ty = tnode(ARRAY, ty);
                                          ty->size = n; } break;
                                          ty->size = n; } break;
                default: assert(0);
                default: assert(0);
                }
                }
        return ty;
        return ty;
}
}
static Symbol *parameters(Type fty) {
static Symbol *parameters(Type fty) {
        List list = NULL;
        List list = NULL;
        Symbol *params;
        Symbol *params;
 
 
        if (kind[t] == STATIC || istypename(t, tsym)) {
        if (kind[t] == STATIC || istypename(t, tsym)) {
                int n = 0;
                int n = 0;
                Type ty1 = NULL;
                Type ty1 = NULL;
                for (;;) {
                for (;;) {
                        Type ty;
                        Type ty;
                        int sclass = 0;
                        int sclass = 0;
                        char *id = NULL;
                        char *id = NULL;
                        if (ty1 && t == ELLIPSIS) {
                        if (ty1 && t == ELLIPSIS) {
                                static struct symbol sentinel;
                                static struct symbol sentinel;
                                if (sentinel.type == NULL) {
                                if (sentinel.type == NULL) {
                                        sentinel.type = voidtype;
                                        sentinel.type = voidtype;
                                        sentinel.defined = 1;
                                        sentinel.defined = 1;
                                }
                                }
                                if (ty1 == voidtype)
                                if (ty1 == voidtype)
                                        error("illegal formal parameter types\n");
                                        error("illegal formal parameter types\n");
                                list = append(&sentinel, list);
                                list = append(&sentinel, list);
                                t = gettok();
                                t = gettok();
                                break;
                                break;
                        }
                        }
                        if (!istypename(t, tsym) && t != REGISTER)
                        if (!istypename(t, tsym) && t != REGISTER)
                                error("missing parameter type\n");
                                error("missing parameter type\n");
                        n++;
                        n++;
                        ty = dclr(specifier(&sclass), &id, NULL, 1);
                        ty = dclr(specifier(&sclass), &id, NULL, 1);
                        if ( ty == voidtype && (ty1 || id)
                        if ( ty == voidtype && (ty1 || id)
                        ||  ty1 == voidtype)
                        ||  ty1 == voidtype)
                                error("illegal formal parameter types\n");
                                error("illegal formal parameter types\n");
                        if (id == NULL)
                        if (id == NULL)
                                id = stringd(n);
                                id = stringd(n);
                        if (ty != voidtype)
                        if (ty != voidtype)
                                list = append(dclparam(sclass, id, ty, &src), list);
                                list = append(dclparam(sclass, id, ty, &src), list);
                        if (Aflag >= 1 && !hasproto(ty))
                        if (Aflag >= 1 && !hasproto(ty))
                                warning("missing prototype\n");
                                warning("missing prototype\n");
                        if (ty1 == NULL)
                        if (ty1 == NULL)
                                ty1 = ty;
                                ty1 = ty;
                        if (t != ',')
                        if (t != ',')
                                break;
                                break;
                        t = gettok();
                        t = gettok();
                }
                }
                fty->u.f.proto = newarray(length(list) + 1,
                fty->u.f.proto = newarray(length(list) + 1,
                        sizeof (Type *), PERM);
                        sizeof (Type *), PERM);
                params = ltov(&list, FUNC);
                params = ltov(&list, FUNC);
                for (n = 0; params[n]; n++)
                for (n = 0; params[n]; n++)
                        fty->u.f.proto[n] = params[n]->type;
                        fty->u.f.proto[n] = params[n]->type;
                fty->u.f.proto[n] = NULL;
                fty->u.f.proto[n] = NULL;
                fty->u.f.oldstyle = 0;
                fty->u.f.oldstyle = 0;
        } else {
        } else {
                if (t == ID)
                if (t == ID)
                        for (;;) {
                        for (;;) {
                                Symbol p;
                                Symbol p;
                                if (t != ID) {
                                if (t != ID) {
                                        error("expecting an identifier\n");
                                        error("expecting an identifier\n");
                                        break;
                                        break;
                                }
                                }
                                p = dclparam(0, token, inttype, &src);
                                p = dclparam(0, token, inttype, &src);
                                p->defined = 0;
                                p->defined = 0;
                                list = append(p, list);
                                list = append(p, list);
                                t = gettok();
                                t = gettok();
                                if (t != ',')
                                if (t != ',')
                                        break;
                                        break;
                                t = gettok();
                                t = gettok();
                        }
                        }
                params = ltov(&list, FUNC);
                params = ltov(&list, FUNC);
                fty->u.f.proto = NULL;
                fty->u.f.proto = NULL;
                fty->u.f.oldstyle = 1;
                fty->u.f.oldstyle = 1;
        }
        }
        if (t != ')') {
        if (t != ')') {
                static char stop[] = { CHAR, STATIC, IF, ')', 0 };
                static char stop[] = { CHAR, STATIC, IF, ')', 0 };
                expect(')');
                expect(')');
                skipto('{', stop);
                skipto('{', stop);
        }
        }
        if (t == ')')
        if (t == ')')
                t = gettok();
                t = gettok();
        return params;
        return params;
}
}
static void exitparams(Symbol params[]) {
static void exitparams(Symbol params[]) {
        assert(params);
        assert(params);
        if (params[0] && !params[0]->defined)
        if (params[0] && !params[0]->defined)
                error("extraneous old-style parameter list\n");
                error("extraneous old-style parameter list\n");
        if (level > PARAM)
        if (level > PARAM)
                exitscope();
                exitscope();
        exitscope();
        exitscope();
}
}
 
 
static Symbol dclparam(int sclass, char *id, Type ty, Coordinate *pos) {
static Symbol dclparam(int sclass, char *id, Type ty, Coordinate *pos) {
        Symbol p;
        Symbol p;
 
 
        if (isfunc(ty))
        if (isfunc(ty))
                ty = ptr(ty);
                ty = ptr(ty);
        else if (isarray(ty))
        else if (isarray(ty))
                ty = atop(ty);
                ty = atop(ty);
        if (sclass == 0)
        if (sclass == 0)
                sclass = AUTO;
                sclass = AUTO;
        else if (sclass != REGISTER) {
        else if (sclass != REGISTER) {
                error("invalid storage class `%k' for `%t%s\n",
                error("invalid storage class `%k' for `%t%s\n",
                        sclass, ty, stringf(id ? " %s'" : "' parameter", id));
                        sclass, ty, stringf(id ? " %s'" : "' parameter", id));
                sclass = AUTO;
                sclass = AUTO;
        } else if (isvolatile(ty) || isstruct(ty)) {
        } else if (isvolatile(ty) || isstruct(ty)) {
                warning("register declaration ignored for `%t%s\n",
                warning("register declaration ignored for `%t%s\n",
                        ty, stringf(id ? " %s'" : "' parameter", id));
                        ty, stringf(id ? " %s'" : "' parameter", id));
                sclass = AUTO;
                sclass = AUTO;
        }
        }
 
 
        p = lookup(id, identifiers);
        p = lookup(id, identifiers);
        if (p && p->scope == level)
        if (p && p->scope == level)
                error("duplicate declaration for `%s' previously declared at %w\n", id, &p->src);
                error("duplicate declaration for `%s' previously declared at %w\n", id, &p->src);
 
 
        else
        else
                p = install(id, &identifiers, level, FUNC);
                p = install(id, &identifiers, level, FUNC);
        p->sclass = sclass;
        p->sclass = sclass;
        p->src = *pos;
        p->src = *pos;
        p->type = ty;
        p->type = ty;
        p->defined = 1;
        p->defined = 1;
        if (t == '=') {
        if (t == '=') {
                error("illegal initialization for parameter `%s'\n", id);
                error("illegal initialization for parameter `%s'\n", id);
                t = gettok();
                t = gettok();
                (void)expr1(0);
                (void)expr1(0);
        }
        }
        return p;
        return p;
}
}
static Type structdcl(int op) {
static Type structdcl(int op) {
        char *tag;
        char *tag;
        Type ty;
        Type ty;
        Symbol p;
        Symbol p;
        Coordinate pos;
        Coordinate pos;
 
 
        t = gettok();
        t = gettok();
        pos = src;
        pos = src;
        if (t == ID) {
        if (t == ID) {
                tag = token;
                tag = token;
                t = gettok();
                t = gettok();
        } else
        } else
                tag = "";
                tag = "";
        if (t == '{') {
        if (t == '{') {
                static char stop[] = { IF, ',', 0 };
                static char stop[] = { IF, ',', 0 };
                ty = newstruct(op, tag);
                ty = newstruct(op, tag);
                ty->u.sym->src = pos;
                ty->u.sym->src = pos;
                ty->u.sym->defined = 1;
                ty->u.sym->defined = 1;
                t = gettok();
                t = gettok();
                if (istypename(t, tsym))
                if (istypename(t, tsym))
                        fields(ty);
                        fields(ty);
                else
                else
                        error("invalid %k field declarations\n", op);
                        error("invalid %k field declarations\n", op);
                test('}', stop);
                test('}', stop);
        }
        }
        else if (*tag && (p = lookup(tag, types)) != NULL
        else if (*tag && (p = lookup(tag, types)) != NULL
        && p->type->op == op) {
        && p->type->op == op) {
                ty = p->type;
                ty = p->type;
                if (t == ';' && p->scope < level)
                if (t == ';' && p->scope < level)
                        ty = newstruct(op, tag);
                        ty = newstruct(op, tag);
        }
        }
        else {
        else {
                if (*tag == 0)
                if (*tag == 0)
                        error("missing %k tag\n", op);
                        error("missing %k tag\n", op);
                ty = newstruct(op, tag);
                ty = newstruct(op, tag);
        }
        }
        if (*tag && xref)
        if (*tag && xref)
                use(ty->u.sym, pos);
                use(ty->u.sym, pos);
        return ty;
        return ty;
}
}
static void fields(Type ty) {
static void fields(Type ty) {
        { int n = 0;
        { int n = 0;
          while (istypename(t, tsym)) {
          while (istypename(t, tsym)) {
                static char stop[] = { IF, CHAR, '}', 0 };
                static char stop[] = { IF, CHAR, '}', 0 };
                Type ty1 = specifier(NULL);
                Type ty1 = specifier(NULL);
                for (;;) {
                for (;;) {
                        Field p;
                        Field p;
                        char *id = NULL;
                        char *id = NULL;
                        Type fty = dclr(ty1, &id, NULL, 0);
                        Type fty = dclr(ty1, &id, NULL, 0);
                        p = newfield(id, ty, fty);
                        p = newfield(id, ty, fty);
                        if (Aflag >= 1 && !hasproto(p->type))
                        if (Aflag >= 1 && !hasproto(p->type))
                                warning("missing prototype\n");
                                warning("missing prototype\n");
                        if (t == ':') {
                        if (t == ':') {
                                if (unqual(p->type) != inttype
                                if (unqual(p->type) != inttype
                                &&  unqual(p->type) != unsignedtype) {
                                &&  unqual(p->type) != unsignedtype) {
                                        error("`%t' is an illegal bit-field type\n",
                                        error("`%t' is an illegal bit-field type\n",
                                                p->type);
                                                p->type);
                                        p->type = inttype;
                                        p->type = inttype;
                                }
                                }
                                t = gettok();
                                t = gettok();
                                p->bitsize = intexpr(0, 0);
                                p->bitsize = intexpr(0, 0);
                                if (p->bitsize > 8*inttype->size || p->bitsize < 0) {
                                if (p->bitsize > 8*inttype->size || p->bitsize < 0) {
                                        error("`%d' is an illegal bit-field size\n",
                                        error("`%d' is an illegal bit-field size\n",
                                                p->bitsize);
                                                p->bitsize);
                                        p->bitsize = 8*inttype->size;
                                        p->bitsize = 8*inttype->size;
                                } else if (p->bitsize == 0 && id) {
                                } else if (p->bitsize == 0 && id) {
                                        warning("extraneous 0-width bit field `%t %s' ignored\n", p->type, id);
                                        warning("extraneous 0-width bit field `%t %s' ignored\n", p->type, id);
 
 
                                        p->name = stringd(genlabel(1));
                                        p->name = stringd(genlabel(1));
                                }
                                }
                                p->lsb = 1;
                                p->lsb = 1;
                        }
                        }
                        else {
                        else {
                                if (id == NULL)
                                if (id == NULL)
                                        error("field name missing\n");
                                        error("field name missing\n");
                                else if (isfunc(p->type))
                                else if (isfunc(p->type))
                                        error("`%t' is an illegal field type\n", p->type);
                                        error("`%t' is an illegal field type\n", p->type);
                                else if (p->type->size == 0)
                                else if (p->type->size == 0)
                                        error("undefined size for field `%t %s'\n",
                                        error("undefined size for field `%t %s'\n",
                                                p->type, id);
                                                p->type, id);
                        }
                        }
                        if (isconst(p->type))
                        if (isconst(p->type))
                                ty->u.sym->u.s.cfields = 1;
                                ty->u.sym->u.s.cfields = 1;
                        if (isvolatile(p->type))
                        if (isvolatile(p->type))
                                ty->u.sym->u.s.vfields = 1;
                                ty->u.sym->u.s.vfields = 1;
                        n++;
                        n++;
                        if (Aflag >= 2 && n == 128)
                        if (Aflag >= 2 && n == 128)
                                warning("more than 127 fields in `%t'\n", ty);
                                warning("more than 127 fields in `%t'\n", ty);
                        if (t != ',')
                        if (t != ',')
                                break;
                                break;
                        t = gettok();
                        t = gettok();
                }
                }
                test(';', stop);
                test(';', stop);
          } }
          } }
        { int bits = 0, off = 0, overflow = 0;
        { int bits = 0, off = 0, overflow = 0;
          Field p, *q = &ty->u.sym->u.s.flist;
          Field p, *q = &ty->u.sym->u.s.flist;
          ty->align = IR->structmetric.align;
          ty->align = IR->structmetric.align;
          for (p = *q; p; p = p->link) {
          for (p = *q; p; p = p->link) {
                int a = p->type->align ? p->type->align : 1;
                int a = p->type->align ? p->type->align : 1;
                if (p->lsb)
                if (p->lsb)
                        a = unsignedtype->align;
                        a = unsignedtype->align;
                if (ty->op == UNION)
                if (ty->op == UNION)
                        off = bits = 0;
                        off = bits = 0;
                else if (p->bitsize == 0 || bits == 0
                else if (p->bitsize == 0 || bits == 0
                || bits - 1 + p->bitsize > 8*unsignedtype->size) {
                || bits - 1 + p->bitsize > 8*unsignedtype->size) {
                        off = add(off, bits2bytes(bits-1));
                        off = add(off, bits2bytes(bits-1));
                        bits = 0;
                        bits = 0;
                        chkoverflow(off, a - 1);
                        chkoverflow(off, a - 1);
                        off = roundup(off, a);
                        off = roundup(off, a);
                }
                }
                if (a > ty->align)
                if (a > ty->align)
                        ty->align = a;
                        ty->align = a;
                p->offset = off;
                p->offset = off;
 
 
                if (p->lsb) {
                if (p->lsb) {
                        if (bits == 0)
                        if (bits == 0)
                                bits = 1;
                                bits = 1;
                        if (IR->little_endian)
                        if (IR->little_endian)
                                p->lsb = bits;
                                p->lsb = bits;
                        else
                        else
                                p->lsb = 8*unsignedtype->size - bits + 1
                                p->lsb = 8*unsignedtype->size - bits + 1
                                        - p->bitsize + 1;
                                        - p->bitsize + 1;
                        bits += p->bitsize;
                        bits += p->bitsize;
                } else
                } else
                        off = add(off, p->type->size);
                        off = add(off, p->type->size);
                if (off + bits2bytes(bits-1) > ty->size)
                if (off + bits2bytes(bits-1) > ty->size)
                        ty->size = off + bits2bytes(bits-1);
                        ty->size = off + bits2bytes(bits-1);
                if (p->name == NULL
                if (p->name == NULL
                || !('1' <= *p->name && *p->name <= '9')) {
                || !('1' <= *p->name && *p->name <= '9')) {
                        *q = p;
                        *q = p;
                        q = &p->link;
                        q = &p->link;
                }
                }
          }
          }
          *q = NULL;
          *q = NULL;
          chkoverflow(ty->size, ty->align - 1);
          chkoverflow(ty->size, ty->align - 1);
          ty->size = roundup(ty->size, ty->align);
          ty->size = roundup(ty->size, ty->align);
          if (overflow) {
          if (overflow) {
                error("size of `%t' exceeds %d bytes\n", ty, inttype->u.sym->u.limits.max.i);
                error("size of `%t' exceeds %d bytes\n", ty, inttype->u.sym->u.limits.max.i);
                ty->size = inttype->u.sym->u.limits.max.i&(~(ty->align - 1));
                ty->size = inttype->u.sym->u.limits.max.i&(~(ty->align - 1));
          } }
          } }
}
}
static void funcdefn(int sclass, char *id, Type ty, Symbol params[], Coordinate pt) {
static void funcdefn(int sclass, char *id, Type ty, Symbol params[], Coordinate pt) {
        int i, n;
        int i, n;
        Symbol *callee, *caller, p;
        Symbol *callee, *caller, p;
        Type rty = freturn(ty);
        Type rty = freturn(ty);
 
 
        if (isstruct(rty) && rty->size == 0)
        if (isstruct(rty) && rty->size == 0)
                error("illegal use of incomplete type `%t'\n", rty);
                error("illegal use of incomplete type `%t'\n", rty);
        for (n = 0; params[n]; n++)
        for (n = 0; params[n]; n++)
                ;
                ;
        if (n > 0 && params[n-1]->name == NULL)
        if (n > 0 && params[n-1]->name == NULL)
                params[--n] = NULL;
                params[--n] = NULL;
        if (Aflag >= 2 && n > 31)
        if (Aflag >= 2 && n > 31)
                warning("more than 31 parameters in function `%s'\n", id);
                warning("more than 31 parameters in function `%s'\n", id);
        if (ty->u.f.oldstyle) {
        if (ty->u.f.oldstyle) {
                if (Aflag >= 1)
                if (Aflag >= 1)
                        warning("old-style function definition for `%s'\n", id);
                        warning("old-style function definition for `%s'\n", id);
                caller = params;
                caller = params;
                callee = newarray(n + 1, sizeof *callee, FUNC);
                callee = newarray(n + 1, sizeof *callee, FUNC);
                memcpy(callee, caller, (n+1)*sizeof *callee);
                memcpy(callee, caller, (n+1)*sizeof *callee);
                enterscope();
                enterscope();
                assert(level == PARAM);
                assert(level == PARAM);
                while (kind[t] == STATIC || istypename(t, tsym))
                while (kind[t] == STATIC || istypename(t, tsym))
                        decl(dclparam);
                        decl(dclparam);
                foreach(identifiers, PARAM, oldparam, callee);
                foreach(identifiers, PARAM, oldparam, callee);
 
 
                for (i = 0; (p = callee[i]) != NULL; i++) {
                for (i = 0; (p = callee[i]) != NULL; i++) {
                        if (!p->defined)
                        if (!p->defined)
                                callee[i] = dclparam(0, p->name, inttype, &p->src);
                                callee[i] = dclparam(0, p->name, inttype, &p->src);
                        *caller[i] = *p;
                        *caller[i] = *p;
                        caller[i]->sclass = AUTO;
                        caller[i]->sclass = AUTO;
                        caller[i]->type = promote(p->type);
                        caller[i]->type = promote(p->type);
                }
                }
                p = lookup(id, identifiers);
                p = lookup(id, identifiers);
                if (p && p->scope == GLOBAL && isfunc(p->type)
                if (p && p->scope == GLOBAL && isfunc(p->type)
                && p->type->u.f.proto) {
                && p->type->u.f.proto) {
                        Type *proto = p->type->u.f.proto;
                        Type *proto = p->type->u.f.proto;
                        for (i = 0; caller[i] && proto[i]; i++) {
                        for (i = 0; caller[i] && proto[i]; i++) {
                                Type ty = unqual(proto[i]);
                                Type ty = unqual(proto[i]);
                                if (eqtype(isenum(ty) ? ty->type : ty,
                                if (eqtype(isenum(ty) ? ty->type : ty,
                                        unqual(caller[i]->type), 1) == 0)
                                        unqual(caller[i]->type), 1) == 0)
                                        break;
                                        break;
                                else if (isenum(ty) && !isenum(unqual(caller[i]->type)))
                                else if (isenum(ty) && !isenum(unqual(caller[i]->type)))
                                        warning("compatibility of `%t' and `%t' is compiler dependent\n",
                                        warning("compatibility of `%t' and `%t' is compiler dependent\n",
                                                proto[i], caller[i]->type);
                                                proto[i], caller[i]->type);
                        }
                        }
                        if (proto[i] || caller[i])
                        if (proto[i] || caller[i])
                                error("conflicting argument declarations for function `%s'\n", id);
                                error("conflicting argument declarations for function `%s'\n", id);
 
 
                }
                }
                else {
                else {
                        Type *proto = newarray(n + 1, sizeof *proto, PERM);
                        Type *proto = newarray(n + 1, sizeof *proto, PERM);
                        if (Aflag >= 1)
                        if (Aflag >= 1)
                                warning("missing prototype for `%s'\n", id);
                                warning("missing prototype for `%s'\n", id);
                        for (i = 0; i < n; i++)
                        for (i = 0; i < n; i++)
                                proto[i] = caller[i]->type;
                                proto[i] = caller[i]->type;
                        proto[i] = NULL;
                        proto[i] = NULL;
                        ty = func(rty, proto, 1);
                        ty = func(rty, proto, 1);
                }
                }
        } else {
        } else {
                callee = params;
                callee = params;
                caller = newarray(n + 1, sizeof *caller, FUNC);
                caller = newarray(n + 1, sizeof *caller, FUNC);
                for (i = 0; (p = callee[i]) != NULL && p->name; i++) {
                for (i = 0; (p = callee[i]) != NULL && p->name; i++) {
                        NEW(caller[i], FUNC);
                        NEW(caller[i], FUNC);
                        *caller[i] = *p;
                        *caller[i] = *p;
                        if (isint(p->type))
                        if (isint(p->type))
                                caller[i]->type = promote(p->type);
                                caller[i]->type = promote(p->type);
                        caller[i]->sclass = AUTO;
                        caller[i]->sclass = AUTO;
                        if ('1' <= *p->name && *p->name <= '9')
                        if ('1' <= *p->name && *p->name <= '9')
                                error("missing name for parameter %d to function `%s'\n", i + 1, id);
                                error("missing name for parameter %d to function `%s'\n", i + 1, id);
 
 
                }
                }
                caller[i] = NULL;
                caller[i] = NULL;
        }
        }
        for (i = 0; (p = callee[i]) != NULL; i++)
        for (i = 0; (p = callee[i]) != NULL; i++)
                if (p->type->size == 0) {
                if (p->type->size == 0) {
                        error("undefined size for parameter `%t %s'\n",
                        error("undefined size for parameter `%t %s'\n",
                                p->type, p->name);
                                p->type, p->name);
                        caller[i]->type = p->type = inttype;
                        caller[i]->type = p->type = inttype;
                }
                }
        if (Aflag >= 2 && sclass != STATIC && strcmp(id, "main") == 0) {
        if (Aflag >= 2 && sclass != STATIC && strcmp(id, "main") == 0) {
                if (ty->u.f.oldstyle)
                if (ty->u.f.oldstyle)
                        warning("`%t %s()' is a non-ANSI definition\n", rty, id);
                        warning("`%t %s()' is a non-ANSI definition\n", rty, id);
                else if (!(rty == inttype
                else if (!(rty == inttype
                        && (n == 0 && callee[0] == NULL
                        && (n == 0 && callee[0] == NULL
                        ||  n == 2 && callee[0]->type == inttype
                        ||  n == 2 && callee[0]->type == inttype
                        && isptr(callee[1]->type) && callee[1]->type->type == charptype
                        && isptr(callee[1]->type) && callee[1]->type->type == charptype
                        && !variadic(ty))))
                        && !variadic(ty))))
                        warning("`%s' is a non-ANSI definition\n", typestring(ty, id));
                        warning("`%s' is a non-ANSI definition\n", typestring(ty, id));
        }
        }
        p = lookup(id, identifiers);
        p = lookup(id, identifiers);
        if (p && isfunc(p->type) && p->defined)
        if (p && isfunc(p->type) && p->defined)
                error("redefinition of `%s' previously defined at %w\n",
                error("redefinition of `%s' previously defined at %w\n",
                        p->name, &p->src);
                        p->name, &p->src);
        cfunc = dclglobal(sclass, id, ty, &pt);
        cfunc = dclglobal(sclass, id, ty, &pt);
        cfunc->u.f.label = genlabel(1);
        cfunc->u.f.label = genlabel(1);
        cfunc->u.f.callee = callee;
        cfunc->u.f.callee = callee;
        cfunc->u.f.pt = src;
        cfunc->u.f.pt = src;
        cfunc->defined = 1;
        cfunc->defined = 1;
        if (xref)
        if (xref)
                use(cfunc, cfunc->src);
                use(cfunc, cfunc->src);
        if (Pflag)
        if (Pflag)
                printproto(cfunc, cfunc->u.f.callee);
                printproto(cfunc, cfunc->u.f.callee);
        if (ncalled >= 0)
        if (ncalled >= 0)
                ncalled = findfunc(cfunc->name, pt.file);
                ncalled = findfunc(cfunc->name, pt.file);
        labels   = table(NULL, LABELS);
        labels   = table(NULL, LABELS);
        stmtlabs = table(NULL, LABELS);
        stmtlabs = table(NULL, LABELS);
        refinc = 1.0;
        refinc = 1.0;
        regcount = 0;
        regcount = 0;
        codelist = &codehead;
        codelist = &codehead;
        codelist->next = NULL;
        codelist->next = NULL;
        if (!IR->wants_callb && isstruct(rty))
        if (!IR->wants_callb && isstruct(rty))
                retv = genident(AUTO, ptr(unqual(rty)), PARAM);
                retv = genident(AUTO, ptr(unqual(rty)), PARAM);
        compound(0, NULL, 0);
        compound(0, NULL, 0);
 
 
        definelab(cfunc->u.f.label);
        definelab(cfunc->u.f.label);
        if (events.exit)
        if (events.exit)
                apply(events.exit, cfunc, NULL);
                apply(events.exit, cfunc, NULL);
        walk(NULL, 0, 0);
        walk(NULL, 0, 0);
        exitscope();
        exitscope();
        assert(level == PARAM);
        assert(level == PARAM);
        foreach(identifiers, level, checkref, NULL);
        foreach(identifiers, level, checkref, NULL);
        if (!IR->wants_callb && isstruct(rty)) {
        if (!IR->wants_callb && isstruct(rty)) {
                Symbol *a;
                Symbol *a;
                a = newarray(n + 2, sizeof *a, FUNC);
                a = newarray(n + 2, sizeof *a, FUNC);
                a[0] = retv;
                a[0] = retv;
                memcpy(&a[1], callee, (n+1)*sizeof *callee);
                memcpy(&a[1], callee, (n+1)*sizeof *callee);
                callee = a;
                callee = a;
                a = newarray(n + 2, sizeof *a, FUNC);
                a = newarray(n + 2, sizeof *a, FUNC);
                NEW(a[0], FUNC);
                NEW(a[0], FUNC);
                *a[0] = *retv;
                *a[0] = *retv;
                memcpy(&a[1], caller, (n+1)*sizeof *callee);
                memcpy(&a[1], caller, (n+1)*sizeof *callee);
                caller = a;
                caller = a;
        }
        }
        if (!IR->wants_argb)
        if (!IR->wants_argb)
                for (i = 0; caller[i]; i++)
                for (i = 0; caller[i]; i++)
                        if (isstruct(caller[i]->type)) {
                        if (isstruct(caller[i]->type)) {
                                caller[i]->type = ptr(caller[i]->type);
                                caller[i]->type = ptr(caller[i]->type);
                                callee[i]->type = ptr(callee[i]->type);
                                callee[i]->type = ptr(callee[i]->type);
                                caller[i]->structarg = callee[i]->structarg = 1;
                                caller[i]->structarg = callee[i]->structarg = 1;
                        }
                        }
        if (glevel > 1) for (i = 0; callee[i]; i++) callee[i]->sclass = AUTO;
        if (glevel > 1) for (i = 0; callee[i]; i++) callee[i]->sclass = AUTO;
        if (cfunc->sclass != STATIC)
        if (cfunc->sclass != STATIC)
                (*IR->export)(cfunc);
                (*IR->export)(cfunc);
        if (glevel && IR->stabsym) {
        if (glevel && IR->stabsym) {
                swtoseg(CODE); (*IR->stabsym)(cfunc); }
                swtoseg(CODE); (*IR->stabsym)(cfunc); }
        swtoseg(CODE);
        swtoseg(CODE);
        (*IR->function)(cfunc, caller, callee, cfunc->u.f.ncalls);
        (*IR->function)(cfunc, caller, callee, cfunc->u.f.ncalls);
        if (glevel && IR->stabfend)
        if (glevel && IR->stabfend)
                (*IR->stabfend)(cfunc, lineno);
                (*IR->stabfend)(cfunc, lineno);
        foreach(stmtlabs, LABELS, checklab, NULL);
        foreach(stmtlabs, LABELS, checklab, NULL);
        exitscope();
        exitscope();
        expect('}');
        expect('}');
        labels = stmtlabs = NULL;
        labels = stmtlabs = NULL;
        retv  = NULL;
        retv  = NULL;
        cfunc = NULL;
        cfunc = NULL;
}
}
static void oldparam(Symbol p, void *cl) {
static void oldparam(Symbol p, void *cl) {
        int i;
        int i;
        Symbol *callee = cl;
        Symbol *callee = cl;
 
 
        for (i = 0; callee[i]; i++)
        for (i = 0; callee[i]; i++)
                if (p->name == callee[i]->name) {
                if (p->name == callee[i]->name) {
                        callee[i] = p;
                        callee[i] = p;
                        return;
                        return;
                }
                }
        error("declared parameter `%s' is missing\n", p->name);
        error("declared parameter `%s' is missing\n", p->name);
}
}
void compound(int loop, struct swtch *swp, int lev) {
void compound(int loop, struct swtch *swp, int lev) {
        Code cp;
        Code cp;
        int nregs;
        int nregs;
 
 
        walk(NULL, 0, 0);
        walk(NULL, 0, 0);
        cp = code(Blockbeg);
        cp = code(Blockbeg);
        enterscope();
        enterscope();
        assert(level >= LOCAL);
        assert(level >= LOCAL);
        if (level == LOCAL && events.entry)
        if (level == LOCAL && events.entry)
                apply(events.entry, cfunc, NULL);
                apply(events.entry, cfunc, NULL);
        definept(NULL);
        definept(NULL);
        expect('{');
        expect('{');
        autos = registers = NULL;
        autos = registers = NULL;
        if (level == LOCAL && IR->wants_callb
        if (level == LOCAL && IR->wants_callb
        && isstruct(freturn(cfunc->type))) {
        && isstruct(freturn(cfunc->type))) {
                retv = genident(AUTO, ptr(unqual(freturn(cfunc->type))), level);
                retv = genident(AUTO, ptr(unqual(freturn(cfunc->type))), level);
                retv->defined = 1;
                retv->defined = 1;
                retv->ref = 1;
                retv->ref = 1;
                registers = append(retv, registers);
                registers = append(retv, registers);
        }
        }
        while (kind[t] == CHAR || kind[t] == STATIC
        while (kind[t] == CHAR || kind[t] == STATIC
        || istypename(t, tsym) && getchr() != ':')
        || istypename(t, tsym) && getchr() != ':')
                decl(dcllocal);
                decl(dcllocal);
        {
        {
                int i;
                int i;
                Symbol *a = ltov(&autos, STMT);
                Symbol *a = ltov(&autos, STMT);
                nregs = length(registers);
                nregs = length(registers);
                for (i = 0; a[i]; i++)
                for (i = 0; a[i]; i++)
                        registers = append(a[i], registers);
                        registers = append(a[i], registers);
                cp->u.block.locals = ltov(&registers, FUNC);
                cp->u.block.locals = ltov(&registers, FUNC);
        }
        }
        if (events.blockentry)
        if (events.blockentry)
                apply(events.blockentry, cp->u.block.locals, NULL);
                apply(events.blockentry, cp->u.block.locals, NULL);
        while (kind[t] == IF || kind[t] == ID)
        while (kind[t] == IF || kind[t] == ID)
                statement(loop, swp, lev);
                statement(loop, swp, lev);
        walk(NULL, 0, 0);
        walk(NULL, 0, 0);
        foreach(identifiers, level, checkref, NULL);
        foreach(identifiers, level, checkref, NULL);
        {
        {
                int i = nregs, j;
                int i = nregs, j;
                Symbol p;
                Symbol p;
                for ( ; (p = cp->u.block.locals[i]) != NULL; i++) {
                for ( ; (p = cp->u.block.locals[i]) != NULL; i++) {
                        for (j = i; j > nregs
                        for (j = i; j > nregs
                                && cp->u.block.locals[j-1]->ref < p->ref; j--)
                                && cp->u.block.locals[j-1]->ref < p->ref; j--)
                                cp->u.block.locals[j] = cp->u.block.locals[j-1];
                                cp->u.block.locals[j] = cp->u.block.locals[j-1];
                        cp->u.block.locals[j] = p;
                        cp->u.block.locals[j] = p;
                }
                }
        }
        }
        if (level == LOCAL) {
        if (level == LOCAL) {
                Code cp;
                Code cp;
                for (cp = codelist; cp->kind < Label; cp = cp->prev)
                for (cp = codelist; cp->kind < Label; cp = cp->prev)
                        ;
                        ;
                if (cp->kind != Jump) {
                if (cp->kind != Jump) {
                        if (freturn(cfunc->type) != voidtype) {
                        if (freturn(cfunc->type) != voidtype) {
                                warning("missing return value\n");
                                warning("missing return value\n");
                                retcode(cnsttree(inttype, 0L));
                                retcode(cnsttree(inttype, 0L));
                        } else
                        } else
                                retcode(NULL);
                                retcode(NULL);
                }
                }
        }
        }
        if (events.blockexit)
        if (events.blockexit)
                apply(events.blockexit, cp->u.block.locals, NULL);
                apply(events.blockexit, cp->u.block.locals, NULL);
        cp->u.block.level = level;
        cp->u.block.level = level;
        cp->u.block.identifiers = identifiers;
        cp->u.block.identifiers = identifiers;
        cp->u.block.types = types;
        cp->u.block.types = types;
        code(Blockend)->u.begin = cp;
        code(Blockend)->u.begin = cp;
        if (reachable(Gen))
        if (reachable(Gen))
                definept(NULL);
                definept(NULL);
        if (level > LOCAL) {
        if (level > LOCAL) {
                exitscope();
                exitscope();
                expect('}');
                expect('}');
        }
        }
}
}
static void checkref(Symbol p, void *cl) {
static void checkref(Symbol p, void *cl) {
        if (p->scope >= PARAM
        if (p->scope >= PARAM
        && (isvolatile(p->type) || isfunc(p->type)))
        && (isvolatile(p->type) || isfunc(p->type)))
                p->addressed = 1;
                p->addressed = 1;
        if (Aflag >= 2 && p->defined && p->ref == 0) {
        if (Aflag >= 2 && p->defined && p->ref == 0) {
                if (p->sclass == STATIC)
                if (p->sclass == STATIC)
                        warning("static `%t %s' is not referenced\n",
                        warning("static `%t %s' is not referenced\n",
                                p->type, p->name);
                                p->type, p->name);
                else if (p->scope == PARAM)
                else if (p->scope == PARAM)
                        warning("parameter `%t %s' is not referenced\n",
                        warning("parameter `%t %s' is not referenced\n",
                                p->type, p->name);
                                p->type, p->name);
                else if (p->scope >= LOCAL && p->sclass != EXTERN)
                else if (p->scope >= LOCAL && p->sclass != EXTERN)
                        warning("local `%t %s' is not referenced\n",
                        warning("local `%t %s' is not referenced\n",
                                p->type, p->name);
                                p->type, p->name);
        }
        }
        if (p->sclass == AUTO
        if (p->sclass == AUTO
        && (p->scope  == PARAM && regcount == 0
        && (p->scope  == PARAM && regcount == 0
         || p->scope  >= LOCAL)
         || p->scope  >= LOCAL)
        && !p->addressed && isscalar(p->type) && p->ref >= 3.0)
        && !p->addressed && isscalar(p->type) && p->ref >= 3.0)
                p->sclass = REGISTER;
                p->sclass = REGISTER;
        if (level == GLOBAL && p->sclass == STATIC && !p->defined
        if (level == GLOBAL && p->sclass == STATIC && !p->defined
        && isfunc(p->type) && p->ref)
        && isfunc(p->type) && p->ref)
                error("undefined static `%t %s'\n", p->type, p->name);
                error("undefined static `%t %s'\n", p->type, p->name);
        assert(!(level == GLOBAL && p->sclass == STATIC && !p->defined && !isfunc(p->type)));
        assert(!(level == GLOBAL && p->sclass == STATIC && !p->defined && !isfunc(p->type)));
}
}
static Symbol dcllocal(int sclass, char *id, Type ty, Coordinate *pos) {
static Symbol dcllocal(int sclass, char *id, Type ty, Coordinate *pos) {
        Symbol p, q;
        Symbol p, q;
 
 
        if (sclass == 0)
        if (sclass == 0)
                sclass = isfunc(ty) ? EXTERN : AUTO;
                sclass = isfunc(ty) ? EXTERN : AUTO;
        else if (isfunc(ty) && sclass != EXTERN) {
        else if (isfunc(ty) && sclass != EXTERN) {
                error("invalid storage class `%k' for `%t %s'\n",
                error("invalid storage class `%k' for `%t %s'\n",
                        sclass, ty, id);
                        sclass, ty, id);
                sclass = EXTERN;
                sclass = EXTERN;
        } else if (sclass == REGISTER
        } else if (sclass == REGISTER
        && (isvolatile(ty) || isstruct(ty) || isarray(ty))) {
        && (isvolatile(ty) || isstruct(ty) || isarray(ty))) {
                warning("register declaration ignored for `%t %s'\n",
                warning("register declaration ignored for `%t %s'\n",
                        ty, id);
                        ty, id);
                sclass = AUTO;
                sclass = AUTO;
        }
        }
        q = lookup(id, identifiers);
        q = lookup(id, identifiers);
        if (q && q->scope >= level
        if (q && q->scope >= level
        ||  q && q->scope == PARAM && level == LOCAL)
        ||  q && q->scope == PARAM && level == LOCAL)
                if (sclass == EXTERN && q->sclass == EXTERN
                if (sclass == EXTERN && q->sclass == EXTERN
                && eqtype(q->type, ty, 1))
                && eqtype(q->type, ty, 1))
                        ty = compose(ty, q->type);
                        ty = compose(ty, q->type);
                else
                else
                        error("redeclaration of `%s' previously declared at %w\n", q->name, &q->src);
                        error("redeclaration of `%s' previously declared at %w\n", q->name, &q->src);
 
 
        assert(level >= LOCAL);
        assert(level >= LOCAL);
        p = install(id, &identifiers, level, sclass == STATIC || sclass == EXTERN ? PERM : FUNC);
        p = install(id, &identifiers, level, sclass == STATIC || sclass == EXTERN ? PERM : FUNC);
        p->type = ty;
        p->type = ty;
        p->sclass = sclass;
        p->sclass = sclass;
        p->src = *pos;
        p->src = *pos;
        switch (sclass) {
        switch (sclass) {
        case EXTERN:   q = lookup(id, globals);
        case EXTERN:   q = lookup(id, globals);
                       if (q == NULL || q->sclass == TYPEDEF || q->sclass == ENUM) {
                       if (q == NULL || q->sclass == TYPEDEF || q->sclass == ENUM) {
                        q = lookup(id, externals);
                        q = lookup(id, externals);
                        if (q == NULL) {
                        if (q == NULL) {
                                q = install(p->name, &externals, GLOBAL, PERM);
                                q = install(p->name, &externals, GLOBAL, PERM);
                                q->type = p->type;
                                q->type = p->type;
                                q->sclass = EXTERN;
                                q->sclass = EXTERN;
                                q->src = src;
                                q->src = src;
                                (*IR->defsymbol)(q);
                                (*IR->defsymbol)(q);
                        }
                        }
                       }
                       }
                       if (!eqtype(p->type, q->type, 1))
                       if (!eqtype(p->type, q->type, 1))
                        warning("declaration of `%s' does not match previous declaration at %w\n", q->name, &q->src);
                        warning("declaration of `%s' does not match previous declaration at %w\n", q->name, &q->src);
 
 
                       p->u.alias = q; break;
                       p->u.alias = q; break;
        case STATIC:   (*IR->defsymbol)(p);
        case STATIC:   (*IR->defsymbol)(p);
                       initglobal(p, 0);
                       initglobal(p, 0);
                       if (!p->defined)
                       if (!p->defined)
                        if (p->type->size > 0) {
                        if (p->type->size > 0) {
                                defglobal(p, BSS);
                                defglobal(p, BSS);
                                (*IR->space)(p->type->size);
                                (*IR->space)(p->type->size);
                        } else
                        } else
                                error("undefined size for `%t %s'\n",
                                error("undefined size for `%t %s'\n",
                                        p->type, p->name);
                                        p->type, p->name);
                       p->defined = 1; break;
                       p->defined = 1; break;
        case REGISTER: registers = append(p, registers);
        case REGISTER: registers = append(p, registers);
                       regcount++;
                       regcount++;
                       p->defined = 1;
                       p->defined = 1;
 break;
 break;
        case AUTO:     autos = append(p, autos);
        case AUTO:     autos = append(p, autos);
                       p->defined = 1;
                       p->defined = 1;
                       if (isarray(ty))
                       if (isarray(ty))
                        p->addressed = 1; break;
                        p->addressed = 1; break;
        default: assert(0);
        default: assert(0);
        }
        }
        if (t == '=') {
        if (t == '=') {
                Tree e;
                Tree e;
                if (sclass == EXTERN)
                if (sclass == EXTERN)
                        error("illegal initialization of `extern %s'\n", id);
                        error("illegal initialization of `extern %s'\n", id);
                t = gettok();
                t = gettok();
                definept(NULL);
                definept(NULL);
                if (isscalar(p->type)
                if (isscalar(p->type)
                ||  isstruct(p->type) && t != '{') {
                ||  isstruct(p->type) && t != '{') {
                        if (t == '{') {
                        if (t == '{') {
                                t = gettok();
                                t = gettok();
                                e = expr1(0);
                                e = expr1(0);
                                expect('}');
                                expect('}');
                        } else
                        } else
                                e = expr1(0);
                                e = expr1(0);
                } else {
                } else {
                        Symbol t1;
                        Symbol t1;
                        Type ty = p->type, ty1 = ty;
                        Type ty = p->type, ty1 = ty;
                        while (isarray(ty1))
                        while (isarray(ty1))
                                ty1 = ty1->type;
                                ty1 = ty1->type;
                        if (!isconst(ty) && (!isarray(ty) || !isconst(ty1)))
                        if (!isconst(ty) && (!isarray(ty) || !isconst(ty1)))
                                ty = qual(CONST, ty);
                                ty = qual(CONST, ty);
                        t1 = genident(STATIC, ty, GLOBAL);
                        t1 = genident(STATIC, ty, GLOBAL);
                        initglobal(t1, 1);
                        initglobal(t1, 1);
                        if (isarray(p->type) && p->type->size == 0
                        if (isarray(p->type) && p->type->size == 0
                        && t1->type->size > 0)
                        && t1->type->size > 0)
                                p->type = array(p->type->type,
                                p->type = array(p->type->type,
                                        t1->type->size/t1->type->type->size, 0);
                                        t1->type->size/t1->type->type->size, 0);
                        e = idtree(t1);
                        e = idtree(t1);
                }
                }
                walk(root(asgn(p, e)), 0, 0);
                walk(root(asgn(p, e)), 0, 0);
                p->ref = 1;
                p->ref = 1;
        }
        }
        if (!isfunc(p->type) && p->defined && p->type->size <= 0)
        if (!isfunc(p->type) && p->defined && p->type->size <= 0)
                error("undefined size for `%t %s'\n", p->type, id);
                error("undefined size for `%t %s'\n", p->type, id);
        return p;
        return p;
}
}
void finalize(void) {
void finalize(void) {
        foreach(externals,   GLOBAL,    doextern, NULL);
        foreach(externals,   GLOBAL,    doextern, NULL);
        foreach(identifiers, GLOBAL,    doglobal, NULL);
        foreach(identifiers, GLOBAL,    doglobal, NULL);
        foreach(identifiers, GLOBAL,    checkref, NULL);
        foreach(identifiers, GLOBAL,    checkref, NULL);
        foreach(constants,   CONSTANTS, doconst,  NULL);
        foreach(constants,   CONSTANTS, doconst,  NULL);
}
}
static void doextern(Symbol p, void *cl) {
static void doextern(Symbol p, void *cl) {
        (*IR->import)(p);
        (*IR->import)(p);
}
}
static void doglobal(Symbol p, void *cl) {
static void doglobal(Symbol p, void *cl) {
        if (!p->defined && (p->sclass == EXTERN
        if (!p->defined && (p->sclass == EXTERN
        || isfunc(p->type) && p->sclass == AUTO))
        || isfunc(p->type) && p->sclass == AUTO))
                (*IR->import)(p);
                (*IR->import)(p);
        else if (!p->defined && !isfunc(p->type)
        else if (!p->defined && !isfunc(p->type)
        && (p->sclass == AUTO || p->sclass == STATIC)) {
        && (p->sclass == AUTO || p->sclass == STATIC)) {
                if (isarray(p->type)
                if (isarray(p->type)
                && p->type->size == 0 && p->type->type->size > 0)
                && p->type->size == 0 && p->type->type->size > 0)
                        p->type = array(p->type->type, 1, 0);
                        p->type = array(p->type->type, 1, 0);
                if (p->type->size > 0) {
                if (p->type->size > 0) {
                        defglobal(p, BSS);
                        defglobal(p, BSS);
                        (*IR->space)(p->type->size);
                        (*IR->space)(p->type->size);
                        if (glevel > 0 && IR->stabsym)
                        if (glevel > 0 && IR->stabsym)
                                (*IR->stabsym)(p);
                                (*IR->stabsym)(p);
                } else
                } else
                        error("undefined size for `%t %s'\n",
                        error("undefined size for `%t %s'\n",
                                p->type, p->name);
                                p->type, p->name);
                p->defined = 1;
                p->defined = 1;
        }
        }
        if (Pflag
        if (Pflag
        && !isfunc(p->type)
        && !isfunc(p->type)
        && !p->generated && p->sclass != EXTERN)
        && !p->generated && p->sclass != EXTERN)
                printdecl(p, p->type);
                printdecl(p, p->type);
}
}
void doconst(Symbol p, void *cl) {
void doconst(Symbol p, void *cl) {
        if (p->u.c.loc) {
        if (p->u.c.loc) {
                assert(p->u.c.loc->u.seg == 0);
                assert(p->u.c.loc->u.seg == 0);
                defglobal(p->u.c.loc, LIT);
                defglobal(p->u.c.loc, LIT);
                if (isarray(p->type) && p->type->type == widechar) {
                if (isarray(p->type) && p->type->type == widechar) {
                        unsigned int *s = p->u.c.v.p;
                        unsigned int *s = p->u.c.v.p;
                        int n = p->type->size/widechar->size;
                        int n = p->type->size/widechar->size;
                        while (n-- > 0) {
                        while (n-- > 0) {
                                Value v;
                                Value v;
                                v.u = *s++;
                                v.u = *s++;
                                (*IR->defconst)(widechar->op, widechar->size, v);
                                (*IR->defconst)(widechar->op, widechar->size, v);
                        }
                        }
                } else if (isarray(p->type))
                } else if (isarray(p->type))
                        (*IR->defstring)(p->type->size, p->u.c.v.p);
                        (*IR->defstring)(p->type->size, p->u.c.v.p);
                else
                else
                        (*IR->defconst)(p->type->op, p->type->size, p->u.c.v);
                        (*IR->defconst)(p->type->op, p->type->size, p->u.c.v);
                p->u.c.loc = NULL;
                p->u.c.loc = NULL;
        }
        }
}
}
void checklab(Symbol p, void *cl) {
void checklab(Symbol p, void *cl) {
        if (!p->defined)
        if (!p->defined)
                error("undefined label `%s'\n", p->name);
                error("undefined label `%s'\n", p->name);
        p->defined = 1;
        p->defined = 1;
}
}
 
 
Type enumdcl(void) {
Type enumdcl(void) {
        char *tag;
        char *tag;
        Type ty;
        Type ty;
        Symbol p;
        Symbol p;
        Coordinate pos;
        Coordinate pos;
 
 
        t = gettok();
        t = gettok();
        pos = src;
        pos = src;
        if (t == ID) {
        if (t == ID) {
                tag = token;
                tag = token;
                t = gettok();
                t = gettok();
        } else
        } else
                tag = "";
                tag = "";
        if (t == '{') {
        if (t == '{') {
                static char follow[] = { IF, 0 };
                static char follow[] = { IF, 0 };
                int n = 0;
                int n = 0;
                long k = -1;
                long k = -1;
                List idlist = 0;
                List idlist = 0;
                ty = newstruct(ENUM, tag);
                ty = newstruct(ENUM, tag);
                t = gettok();
                t = gettok();
                if (t != ID)
                if (t != ID)
                        error("expecting an enumerator identifier\n");
                        error("expecting an enumerator identifier\n");
                while (t == ID) {
                while (t == ID) {
                        char *id = token;
                        char *id = token;
                        Coordinate s;
                        Coordinate s;
                        if (tsym && tsym->scope == level)
                        if (tsym && tsym->scope == level)
                                error("redeclaration of `%s' previously declared at %w\n",
                                error("redeclaration of `%s' previously declared at %w\n",
                                        token, &tsym->src);
                                        token, &tsym->src);
                        s = src;
                        s = src;
                        t = gettok();
                        t = gettok();
                        if (t == '=') {
                        if (t == '=') {
                                t = gettok();
                                t = gettok();
                                k = intexpr(0, 0);
                                k = intexpr(0, 0);
                        } else {
                        } else {
                                if (k == inttype->u.sym->u.limits.max.i)
                                if (k == inttype->u.sym->u.limits.max.i)
                                        error("overflow in value for enumeration constant `%s'\n", id);
                                        error("overflow in value for enumeration constant `%s'\n", id);
                                k++;
                                k++;
                        }
                        }
                        p = install(id, &identifiers, level,  level < LOCAL ? PERM : FUNC);
                        p = install(id, &identifiers, level,  level < LOCAL ? PERM : FUNC);
                        p->src = s;
                        p->src = s;
                        p->type = ty;
                        p->type = ty;
                        p->sclass = ENUM;
                        p->sclass = ENUM;
                        p->u.value = k;
                        p->u.value = k;
                        idlist = append(p, idlist);
                        idlist = append(p, idlist);
                        n++;
                        n++;
                        if (Aflag >= 2 && n == 128)
                        if (Aflag >= 2 && n == 128)
                                warning("more than 127 enumeration constants in `%t'\n", ty);
                                warning("more than 127 enumeration constants in `%t'\n", ty);
                        if (t != ',')
                        if (t != ',')
                                break;
                                break;
                        t = gettok();
                        t = gettok();
                        if (Aflag >= 2 && t == '}')
                        if (Aflag >= 2 && t == '}')
                                warning("non-ANSI trailing comma in enumerator list\n");
                                warning("non-ANSI trailing comma in enumerator list\n");
                }
                }
                test('}', follow);
                test('}', follow);
                ty->type = inttype;
                ty->type = inttype;
                ty->size = ty->type->size;
                ty->size = ty->type->size;
                ty->align = ty->type->align;
                ty->align = ty->type->align;
                ty->u.sym->u.idlist = ltov(&idlist, PERM);
                ty->u.sym->u.idlist = ltov(&idlist, PERM);
                ty->u.sym->defined = 1;
                ty->u.sym->defined = 1;
        } else if ((p = lookup(tag, types)) != NULL && p->type->op == ENUM) {
        } else if ((p = lookup(tag, types)) != NULL && p->type->op == ENUM) {
                ty = p->type;
                ty = p->type;
                if (t == ';')
                if (t == ';')
                        error("empty declaration\n");
                        error("empty declaration\n");
        } else {
        } else {
                error("unknown enumeration `%s'\n",  tag);
                error("unknown enumeration `%s'\n",  tag);
                ty = newstruct(ENUM, tag);
                ty = newstruct(ENUM, tag);
                ty->type = inttype;
                ty->type = inttype;
        }
        }
        if (*tag && xref)
        if (*tag && xref)
                use(p, pos);
                use(p, pos);
        return ty;
        return ty;
}
}
 
 
Type typename(void) {
Type typename(void) {
        Type ty = specifier(NULL);
        Type ty = specifier(NULL);
 
 
        if (t == '*' || t == '(' || t == '[') {
        if (t == '*' || t == '(' || t == '[') {
                ty = dclr(ty, NULL, NULL, 1);
                ty = dclr(ty, NULL, NULL, 1);
                if (Aflag >= 1 && !hasproto(ty))
                if (Aflag >= 1 && !hasproto(ty))
                        warning("missing prototype\n");
                        warning("missing prototype\n");
        }
        }
        return ty;
        return ty;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.