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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [tree.c] - Blame information for rev 130

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
 
3
static char rcsid[] = "$Id: tree.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
4
 
5
int where = STMT;
6
static int warn;
7
static int nid = 1;             /* identifies trees & nodes in debugging output */
8
static struct nodeid {
9
        int printed;
10
        Tree node;
11
} ids[500];                     /* if ids[i].node == p, then p's id is i */
12
 
13
static void printtree1(Tree, int, int);
14
 
15
Tree tree(int op, Type type, Tree left, Tree right) {
16
        Tree p;
17
 
18
        NEW0(p, where);
19
        p->op = op;
20
        p->type = type;
21
        p->kids[0] = left;
22
        p->kids[1] = right;
23
        return p;
24
}
25
 
26
Tree texpr(Tree (*f)(int), int tok, int a) {
27
        int save = where;
28
        Tree p;
29
 
30
        where = a;
31
        p = (*f)(tok);
32
        where = save;
33
        return p;
34
}
35
static Tree root1(Tree p) {
36
        if (p == NULL)
37
                return p;
38
        if (p->type == voidtype)
39
                warn++;
40
        switch (generic(p->op)) {
41
        case COND: {
42
                Tree q = p->kids[1];
43
                assert(q && q->op == RIGHT);
44
                if (p->u.sym && q->kids[0] && generic(q->kids[0]->op) == ASGN)
45
                        q->kids[0] = root1(q->kids[0]->kids[1]);
46
                else
47
                        q->kids[0] = root1(q->kids[0]);
48
                if (p->u.sym && q->kids[1] && generic(q->kids[1]->op) == ASGN)
49
                        q->kids[1] = root1(q->kids[1]->kids[1]);
50
                else
51
                        q->kids[1] = root1(q->kids[1]);
52
                p->u.sym = 0;
53
                if (q->kids[0] == 0 && q->kids[1] == 0)
54
                        p = root1(p->kids[0]);
55
                }
56
                break;
57
        case AND: case OR:
58
                if ((p->kids[1] = root1(p->kids[1])) == 0)
59
                        p = root1(p->kids[0]);
60
                break;
61
        case NOT:
62
                if (warn++ == 0)
63
                        warning("expression with no effect elided\n");
64
                return root1(p->kids[0]);
65
        case RIGHT:
66
                if (p->kids[1] == 0)
67
                        return root1(p->kids[0]);
68
                if (p->kids[0] && p->kids[0]->op == CALL+B
69
                &&  p->kids[1] && p->kids[1]->op == INDIR+B)
70
                        /* avoid premature release of the CALL+B temporary */
71
                        return p->kids[0];
72
                if (p->kids[0] && p->kids[0]->op == RIGHT
73
                &&  p->kids[1] == p->kids[0]->kids[0])
74
                        /* de-construct e++ construction */
75
                        return p->kids[0]->kids[1];
76
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
77
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
78
        case EQ:  case NE:  case GT:   case GE:  case LE:  case LT:
79
        case ADD: case SUB: case MUL:  case DIV: case MOD:
80
        case LSH: case RSH: case BAND: case BOR: case BXOR:
81
                if (warn++ == 0)
82
                        warning("expression with no effect elided\n");
83
                p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
84
                return p->kids[0] || p->kids[1] ? p : (Tree)0;
85
        case INDIR:
86
                if (p->type->size == 0 && unqual(p->type) != voidtype)
87
                        warning("reference to `%t' elided\n", p->type);
88
                if (isptr(p->kids[0]->type) && isvolatile(p->kids[0]->type->type))
89
                        warning("reference to `volatile %t' elided\n", p->type);
90
                /* fall thru */
91
        case NEG: case BCOM: case FIELD:
92
                if (warn++ == 0)
93
                        warning("expression with no effect elided\n");
94
                return root1(p->kids[0]);
95
        case ADDRL: case ADDRG: case ADDRF: case CNST:
96
                if (needconst)
97
                        return p;
98
                if (warn++ == 0)
99
                        warning("expression with no effect elided\n");
100
                return NULL;
101
        case CVF:
102
                if (optype(p->op) == I
103
                || p->type->size < p->kids[0]->type->size)
104
                        if (warn++ == 0)
105
                                warning("expression with no effect elided\n");
106
                return root1(p->kids[0]);
107
        case CVI:
108
                if ((optype(p->op) == U || optype(p->op) == I)
109
                && p->type->size < p->kids[0]->type->size
110
                && specific(p->kids[0]->op) != CALL+I)
111
                        if (warn++ == 0)
112
                                warning("expression with no effect elided\n");
113
                return root1(p->kids[0]);
114
        case CVU: case CVP:
115
                if (optype(p->op) == U && p->type->size <  p->kids[0]->type->size
116
                ||  optype(p->op) == I && p->type->size <= p->kids[0]->type->size)
117
                        if (warn++ == 0)
118
                                warning("expression with no effect elided\n");
119
                return root1(p->kids[0]);
120
        case ARG: case ASGN: case CALL: case JUMP: case LABEL:
121
                break;
122
        default: assert(0);
123
        }
124
        return p;
125
}
126
 
127
Tree root(Tree p) {
128
        warn = 0;
129
        return root1(p);
130
}
131
 
132
char *opname(int op) {
133
        static char *opnames[] = {
134
        "",
135
        "CNST",
136
        "ARG",
137
        "ASGN",
138
        "INDIR",
139
        "CVC",
140
        "CVD",
141
        "CVF",
142
        "CVI",
143
        "CVP",
144
        "CVS",
145
        "CVU",
146
        "NEG",
147
        "CALL",
148
        "*LOAD*",
149
        "RET",
150
        "ADDRG",
151
        "ADDRF",
152
        "ADDRL",
153
        "ADD",
154
        "SUB",
155
        "LSH",
156
        "MOD",
157
        "RSH",
158
        "BAND",
159
        "BCOM",
160
        "BOR",
161
        "BXOR",
162
        "DIV",
163
        "MUL",
164
        "EQ",
165
        "GE",
166
        "GT",
167
        "LE",
168
        "LT",
169
        "NE",
170
        "JUMP",
171
        "LABEL",
172
        "AND",
173
        "NOT",
174
        "OR",
175
        "COND",
176
        "RIGHT",
177
        "FIELD"
178
        }, *suffixes[] = {
179
                "0", "F", "D", "C", "S", "I", "U", "P", "V", "B",
180
                "10","11","12","13","14","15"
181
        };
182
 
183
        if (generic(op) >= AND && generic(op) <= FIELD && opsize(op) == 0)
184
                return opnames[opindex(op)];
185
        return stringf("%s%s%s",
186
                opindex(op) > 0 && opindex(op) < NELEMS(opnames) ?
187
                        opnames[opindex(op)] : stringd(opindex(op)),
188
                suffixes[optype(op)], opsize(op) > 0 ? stringd(opsize(op)) : "");
189
}
190
 
191
int nodeid(Tree p) {
192
        int i = 1;
193
 
194
        ids[nid].node = p;
195
        while (ids[i].node != p)
196
                i++;
197
        if (i == nid)
198
                ids[nid++].printed = 0;
199
        return i;
200
}
201
 
202
/* printed - return pointer to ids[id].printed */
203
int *printed(int id) {
204
        if (id)
205
                return &ids[id].printed;
206
        nid = 1;
207
        return 0;
208
}
209
 
210
/* printtree - print tree p on fd */
211
void printtree(Tree p, int fd) {
212
        (void)printed(0);
213
        printtree1(p, fd, 1);
214
}
215
 
216
/* printtree1 - recursively print tree p */
217
static void printtree1(Tree p, int fd, int lev) {
218
        FILE *f = fd == 1 ? stdout : stderr;
219
        int i;
220
        static char blanks[] = "                                                   ";
221
 
222
        if (p == 0 || *printed(i = nodeid(p)))
223
                return;
224
        fprint(f, "#%d%S%S", i, blanks, i < 10 ? 2 : i < 100 ? 1 : 0, blanks, lev);
225
        fprint(f, "%s %t", opname(p->op), p->type);
226
        *printed(i) = 1;
227
        for (i = 0; i < NELEMS(p->kids); i++)
228
                if (p->kids[i])
229
                        fprint(f, " #%d", nodeid(p->kids[i]));
230
        if (p->op == FIELD && p->u.field)
231
                fprint(f, " %s %d..%d", p->u.field->name,
232
                        fieldsize(p->u.field) + fieldright(p->u.field), fieldright(p->u.field));
233
        else if (generic(p->op) == CNST)
234
                fprint(f, " %s", vtoa(p->type, p->u.v));
235
        else if (p->u.sym)
236
                fprint(f, " %s", p->u.sym->name);
237
        if (p->node)
238
                fprint(f, " node=%p", p->node);
239
        fprint(f, "\n");
240
        for (i = 0; i < NELEMS(p->kids); i++)
241
                printtree1(p->kids[i], fd, lev + 1);
242
}

powered by: WebSVN 2.1.0

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