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

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [lcc/] [src/] [tree.c] - Diff between revs 4 and 21

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

Rev 4 Rev 21
#include "c.h"
#include "c.h"
 
 
static char rcsid[] = "$Id: tree.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
static char rcsid[] = "$Id: tree.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
 
 
int where = STMT;
int where = STMT;
static int warn;
static int warn;
static int nid = 1;             /* identifies trees & nodes in debugging output */
static int nid = 1;             /* identifies trees & nodes in debugging output */
static struct nodeid {
static struct nodeid {
        int printed;
        int printed;
        Tree node;
        Tree node;
} ids[500];                     /* if ids[i].node == p, then p's id is i */
} ids[500];                     /* if ids[i].node == p, then p's id is i */
 
 
static void printtree1(Tree, int, int);
static void printtree1(Tree, int, int);
 
 
Tree tree(int op, Type type, Tree left, Tree right) {
Tree tree(int op, Type type, Tree left, Tree right) {
        Tree p;
        Tree p;
 
 
        NEW0(p, where);
        NEW0(p, where);
        p->op = op;
        p->op = op;
        p->type = type;
        p->type = type;
        p->kids[0] = left;
        p->kids[0] = left;
        p->kids[1] = right;
        p->kids[1] = right;
        return p;
        return p;
}
}
 
 
Tree texpr(Tree (*f)(int), int tok, int a) {
Tree texpr(Tree (*f)(int), int tok, int a) {
        int save = where;
        int save = where;
        Tree p;
        Tree p;
 
 
        where = a;
        where = a;
        p = (*f)(tok);
        p = (*f)(tok);
        where = save;
        where = save;
        return p;
        return p;
}
}
static Tree root1(Tree p) {
static Tree root1(Tree p) {
        if (p == NULL)
        if (p == NULL)
                return p;
                return p;
        if (p->type == voidtype)
        if (p->type == voidtype)
                warn++;
                warn++;
        switch (generic(p->op)) {
        switch (generic(p->op)) {
        case COND: {
        case COND: {
                Tree q = p->kids[1];
                Tree q = p->kids[1];
                assert(q && q->op == RIGHT);
                assert(q && q->op == RIGHT);
                if (p->u.sym && q->kids[0] && generic(q->kids[0]->op) == ASGN)
                if (p->u.sym && q->kids[0] && generic(q->kids[0]->op) == ASGN)
                        q->kids[0] = root1(q->kids[0]->kids[1]);
                        q->kids[0] = root1(q->kids[0]->kids[1]);
                else
                else
                        q->kids[0] = root1(q->kids[0]);
                        q->kids[0] = root1(q->kids[0]);
                if (p->u.sym && q->kids[1] && generic(q->kids[1]->op) == ASGN)
                if (p->u.sym && q->kids[1] && generic(q->kids[1]->op) == ASGN)
                        q->kids[1] = root1(q->kids[1]->kids[1]);
                        q->kids[1] = root1(q->kids[1]->kids[1]);
                else
                else
                        q->kids[1] = root1(q->kids[1]);
                        q->kids[1] = root1(q->kids[1]);
                p->u.sym = 0;
                p->u.sym = 0;
                if (q->kids[0] == 0 && q->kids[1] == 0)
                if (q->kids[0] == 0 && q->kids[1] == 0)
                        p = root1(p->kids[0]);
                        p = root1(p->kids[0]);
                }
                }
                break;
                break;
        case AND: case OR:
        case AND: case OR:
                if ((p->kids[1] = root1(p->kids[1])) == 0)
                if ((p->kids[1] = root1(p->kids[1])) == 0)
                        p = root1(p->kids[0]);
                        p = root1(p->kids[0]);
                break;
                break;
        case NOT:
        case NOT:
                if (warn++ == 0)
                if (warn++ == 0)
                        warning("expression with no effect elided\n");
                        warning("expression with no effect elided\n");
                return root1(p->kids[0]);
                return root1(p->kids[0]);
        case RIGHT:
        case RIGHT:
                if (p->kids[1] == 0)
                if (p->kids[1] == 0)
                        return root1(p->kids[0]);
                        return root1(p->kids[0]);
                if (p->kids[0] && p->kids[0]->op == CALL+B
                if (p->kids[0] && p->kids[0]->op == CALL+B
                &&  p->kids[1] && p->kids[1]->op == INDIR+B)
                &&  p->kids[1] && p->kids[1]->op == INDIR+B)
                        /* avoid premature release of the CALL+B temporary */
                        /* avoid premature release of the CALL+B temporary */
                        return p->kids[0];
                        return p->kids[0];
                if (p->kids[0] && p->kids[0]->op == RIGHT
                if (p->kids[0] && p->kids[0]->op == RIGHT
                &&  p->kids[1] == p->kids[0]->kids[0])
                &&  p->kids[1] == p->kids[0]->kids[0])
                        /* de-construct e++ construction */
                        /* de-construct e++ construction */
                        return p->kids[0]->kids[1];
                        return p->kids[0]->kids[1];
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
        case EQ:  case NE:  case GT:   case GE:  case LE:  case LT:
        case EQ:  case NE:  case GT:   case GE:  case LE:  case LT:
        case ADD: case SUB: case MUL:  case DIV: case MOD:
        case ADD: case SUB: case MUL:  case DIV: case MOD:
        case LSH: case RSH: case BAND: case BOR: case BXOR:
        case LSH: case RSH: case BAND: case BOR: case BXOR:
                if (warn++ == 0)
                if (warn++ == 0)
                        warning("expression with no effect elided\n");
                        warning("expression with no effect elided\n");
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
        case INDIR:
        case INDIR:
                if (p->type->size == 0 && unqual(p->type) != voidtype)
                if (p->type->size == 0 && unqual(p->type) != voidtype)
                        warning("reference to `%t' elided\n", p->type);
                        warning("reference to `%t' elided\n", p->type);
                if (isptr(p->kids[0]->type) && isvolatile(p->kids[0]->type->type))
                if (isptr(p->kids[0]->type) && isvolatile(p->kids[0]->type->type))
                        warning("reference to `volatile %t' elided\n", p->type);
                        warning("reference to `volatile %t' elided\n", p->type);
                /* fall thru */
                /* fall thru */
        case NEG: case BCOM: case FIELD:
        case NEG: case BCOM: case FIELD:
                if (warn++ == 0)
                if (warn++ == 0)
                        warning("expression with no effect elided\n");
                        warning("expression with no effect elided\n");
                return root1(p->kids[0]);
                return root1(p->kids[0]);
        case ADDRL: case ADDRG: case ADDRF: case CNST:
        case ADDRL: case ADDRG: case ADDRF: case CNST:
                if (needconst)
                if (needconst)
                        return p;
                        return p;
                if (warn++ == 0)
                if (warn++ == 0)
                        warning("expression with no effect elided\n");
                        warning("expression with no effect elided\n");
                return NULL;
                return NULL;
        case CVF:
        case CVF:
                if (optype(p->op) == I
                if (optype(p->op) == I
                || p->type->size < p->kids[0]->type->size)
                || p->type->size < p->kids[0]->type->size)
                        if (warn++ == 0)
                        if (warn++ == 0)
                                warning("expression with no effect elided\n");
                                warning("expression with no effect elided\n");
                return root1(p->kids[0]);
                return root1(p->kids[0]);
        case CVI:
        case CVI:
                if ((optype(p->op) == U || optype(p->op) == I)
                if ((optype(p->op) == U || optype(p->op) == I)
                && p->type->size < p->kids[0]->type->size
                && p->type->size < p->kids[0]->type->size
                && specific(p->kids[0]->op) != CALL+I)
                && specific(p->kids[0]->op) != CALL+I)
                        if (warn++ == 0)
                        if (warn++ == 0)
                                warning("expression with no effect elided\n");
                                warning("expression with no effect elided\n");
                return root1(p->kids[0]);
                return root1(p->kids[0]);
        case CVU: case CVP:
        case CVU: case CVP:
                if (optype(p->op) == U && p->type->size <  p->kids[0]->type->size
                if (optype(p->op) == U && p->type->size <  p->kids[0]->type->size
                ||  optype(p->op) == I && p->type->size <= p->kids[0]->type->size)
                ||  optype(p->op) == I && p->type->size <= p->kids[0]->type->size)
                        if (warn++ == 0)
                        if (warn++ == 0)
                                warning("expression with no effect elided\n");
                                warning("expression with no effect elided\n");
                return root1(p->kids[0]);
                return root1(p->kids[0]);
        case ARG: case ASGN: case CALL: case JUMP: case LABEL:
        case ARG: case ASGN: case CALL: case JUMP: case LABEL:
                break;
                break;
        default: assert(0);
        default: assert(0);
        }
        }
        return p;
        return p;
}
}
 
 
Tree root(Tree p) {
Tree root(Tree p) {
        warn = 0;
        warn = 0;
        return root1(p);
        return root1(p);
}
}
 
 
char *opname(int op) {
char *opname(int op) {
        static char *opnames[] = {
        static char *opnames[] = {
        "",
        "",
        "CNST",
        "CNST",
        "ARG",
        "ARG",
        "ASGN",
        "ASGN",
        "INDIR",
        "INDIR",
        "CVC",
        "CVC",
        "CVD",
        "CVD",
        "CVF",
        "CVF",
        "CVI",
        "CVI",
        "CVP",
        "CVP",
        "CVS",
        "CVS",
        "CVU",
        "CVU",
        "NEG",
        "NEG",
        "CALL",
        "CALL",
        "*LOAD*",
        "*LOAD*",
        "RET",
        "RET",
        "ADDRG",
        "ADDRG",
        "ADDRF",
        "ADDRF",
        "ADDRL",
        "ADDRL",
        "ADD",
        "ADD",
        "SUB",
        "SUB",
        "LSH",
        "LSH",
        "MOD",
        "MOD",
        "RSH",
        "RSH",
        "BAND",
        "BAND",
        "BCOM",
        "BCOM",
        "BOR",
        "BOR",
        "BXOR",
        "BXOR",
        "DIV",
        "DIV",
        "MUL",
        "MUL",
        "EQ",
        "EQ",
        "GE",
        "GE",
        "GT",
        "GT",
        "LE",
        "LE",
        "LT",
        "LT",
        "NE",
        "NE",
        "JUMP",
        "JUMP",
        "LABEL",
        "LABEL",
        "AND",
        "AND",
        "NOT",
        "NOT",
        "OR",
        "OR",
        "COND",
        "COND",
        "RIGHT",
        "RIGHT",
        "FIELD"
        "FIELD"
        }, *suffixes[] = {
        }, *suffixes[] = {
                "0", "F", "D", "C", "S", "I", "U", "P", "V", "B",
                "0", "F", "D", "C", "S", "I", "U", "P", "V", "B",
                "10","11","12","13","14","15"
                "10","11","12","13","14","15"
        };
        };
 
 
        if (generic(op) >= AND && generic(op) <= FIELD && opsize(op) == 0)
        if (generic(op) >= AND && generic(op) <= FIELD && opsize(op) == 0)
                return opnames[opindex(op)];
                return opnames[opindex(op)];
        return stringf("%s%s%s",
        return stringf("%s%s%s",
                opindex(op) > 0 && opindex(op) < NELEMS(opnames) ?
                opindex(op) > 0 && opindex(op) < NELEMS(opnames) ?
                        opnames[opindex(op)] : stringd(opindex(op)),
                        opnames[opindex(op)] : stringd(opindex(op)),
                suffixes[optype(op)], opsize(op) > 0 ? stringd(opsize(op)) : "");
                suffixes[optype(op)], opsize(op) > 0 ? stringd(opsize(op)) : "");
}
}
 
 
int nodeid(Tree p) {
int nodeid(Tree p) {
        int i = 1;
        int i = 1;
 
 
        ids[nid].node = p;
        ids[nid].node = p;
        while (ids[i].node != p)
        while (ids[i].node != p)
                i++;
                i++;
        if (i == nid)
        if (i == nid)
                ids[nid++].printed = 0;
                ids[nid++].printed = 0;
        return i;
        return i;
}
}
 
 
/* printed - return pointer to ids[id].printed */
/* printed - return pointer to ids[id].printed */
int *printed(int id) {
int *printed(int id) {
        if (id)
        if (id)
                return &ids[id].printed;
                return &ids[id].printed;
        nid = 1;
        nid = 1;
        return 0;
        return 0;
}
}
 
 
/* printtree - print tree p on fd */
/* printtree - print tree p on fd */
void printtree(Tree p, int fd) {
void printtree(Tree p, int fd) {
        (void)printed(0);
        (void)printed(0);
        printtree1(p, fd, 1);
        printtree1(p, fd, 1);
}
}
 
 
/* printtree1 - recursively print tree p */
/* printtree1 - recursively print tree p */
static void printtree1(Tree p, int fd, int lev) {
static void printtree1(Tree p, int fd, int lev) {
        FILE *f = fd == 1 ? stdout : stderr;
        FILE *f = fd == 1 ? stdout : stderr;
        int i;
        int i;
        static char blanks[] = "                                                   ";
        static char blanks[] = "                                                   ";
 
 
        if (p == 0 || *printed(i = nodeid(p)))
        if (p == 0 || *printed(i = nodeid(p)))
                return;
                return;
        fprint(f, "#%d%S%S", i, blanks, i < 10 ? 2 : i < 100 ? 1 : 0, blanks, lev);
        fprint(f, "#%d%S%S", i, blanks, i < 10 ? 2 : i < 100 ? 1 : 0, blanks, lev);
        fprint(f, "%s %t", opname(p->op), p->type);
        fprint(f, "%s %t", opname(p->op), p->type);
        *printed(i) = 1;
        *printed(i) = 1;
        for (i = 0; i < NELEMS(p->kids); i++)
        for (i = 0; i < NELEMS(p->kids); i++)
                if (p->kids[i])
                if (p->kids[i])
                        fprint(f, " #%d", nodeid(p->kids[i]));
                        fprint(f, " #%d", nodeid(p->kids[i]));
        if (p->op == FIELD && p->u.field)
        if (p->op == FIELD && p->u.field)
                fprint(f, " %s %d..%d", p->u.field->name,
                fprint(f, " %s %d..%d", p->u.field->name,
                        fieldsize(p->u.field) + fieldright(p->u.field), fieldright(p->u.field));
                        fieldsize(p->u.field) + fieldright(p->u.field), fieldright(p->u.field));
        else if (generic(p->op) == CNST)
        else if (generic(p->op) == CNST)
                fprint(f, " %s", vtoa(p->type, p->u.v));
                fprint(f, " %s", vtoa(p->type, p->u.v));
        else if (p->u.sym)
        else if (p->u.sym)
                fprint(f, " %s", p->u.sym->name);
                fprint(f, " %s", p->u.sym->name);
        if (p->node)
        if (p->node)
                fprint(f, " node=%p", p->node);
                fprint(f, " node=%p", p->node);
        fprint(f, "\n");
        fprint(f, "\n");
        for (i = 0; i < NELEMS(p->kids); i++)
        for (i = 0; i < NELEMS(p->kids); i++)
                printtree1(p->kids[i], fd, lev + 1);
                printtree1(p->kids[i], fd, lev + 1);
}
}
 
 

powered by: WebSVN 2.1.0

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