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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [bytecode.c] - Blame information for rev 323

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

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
#define I(f) b_##f
3
 
4
static char rcsid[] = "$Id: bytecode.c,v 1.1 2002/08/28 23:12:41 drh Exp $";
5
 
6
static void I(segment)(int n) {
7
        static int cseg;
8
 
9
        if (cseg != n)
10
                switch (cseg = n) {
11
                case CODE: print("code\n"); return;
12
                case DATA: print("data\n"); return;
13
                case BSS:  print("bss\n");  return;
14
                case LIT:  print("lit\n");  return;
15
                default: assert(0);
16
                }
17
}
18
 
19
static void I(address)(Symbol q, Symbol p, long n) {
20
        q->x.name = stringf("%s%s%D", p->x.name, n > 0 ? "+" : "", n);
21
}
22
 
23
static void I(defaddress)(Symbol p) {
24
        print("address %s\n", p->x.name);
25
}
26
 
27
static void I(defconst)(int suffix, int size, Value v) {
28
        switch (suffix) {
29
        case I:
30
                if (size > sizeof (int))
31
                        print("byte %d %D\n", size, v.i);
32
                else
33
                        print("byte %d %d\n", size, v.i);
34
                return;
35
        case U:
36
                if (size > sizeof (unsigned))
37
                        print("byte %d %U\n", size, v.u);
38
                else
39
                        print("byte %d %u\n", size, v.u);
40
                return;
41
        case P: print("byte %d %U\n", size, (unsigned long)v.p); return;
42
        case F:
43
                if (size == 4) {
44
                        float f = v.d;
45
                        print("byte 4 %u\n", *(unsigned *)&f);
46
                } else {
47
                        double d = v.d;
48
                        unsigned *p = (unsigned *)&d;
49
                        print("byte 4 %u\n", p[swap]);
50
                        print("byte 4 %u\n", p[1 - swap]);
51
                }
52
                return;
53
        }
54
        assert(0);
55
}
56
 
57
static void I(defstring)(int len, char *str) {
58
        char *s;
59
 
60
        for (s = str; s < str + len; s++)
61
                print("byte 1 %d\n", (*s)&0377);
62
}
63
 
64
static void I(defsymbol)(Symbol p) {
65
        if (p->scope == CONSTANTS)
66
                switch (optype(ttob(p->type))) {
67
                case I: p->x.name = stringf("%D", p->u.c.v.i); break;
68
                case U: p->x.name = stringf("%U", p->u.c.v.u); break;
69
                case P: p->x.name = stringf("%U", p->u.c.v.p); break;
70
                default: assert(0);
71
                }
72
        else if (p->scope >= LOCAL && p->sclass == STATIC)
73
                p->x.name = stringf("$%d", genlabel(1));
74
        else if (p->scope == LABELS || p->generated)
75
                p->x.name = stringf("$%s", p->name);
76
        else
77
                p->x.name = p->name;
78
}
79
 
80
static void dumptree(Node p) {
81
        switch (specific(p->op)) {
82
        case ASGN+B:
83
                assert(p->kids[0]);
84
                assert(p->kids[1]);
85
                assert(p->syms[0]);
86
                dumptree(p->kids[0]);
87
                dumptree(p->kids[1]);
88
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.u);
89
                return;
90
        case RET+V:
91
                assert(!p->kids[0]);
92
                assert(!p->kids[1]);
93
                print("%s\n", opname(p->op));
94
                return;
95
        }
96
        switch (generic(p->op)) {
97
        case CNST: case ADDRG: case ADDRF: case ADDRL: case LABEL:
98
                assert(!p->kids[0]);
99
                assert(!p->kids[1]);
100
                assert(p->syms[0] && p->syms[0]->x.name);
101
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
102
                return;
103
        case CVF: case CVI: case CVP: case CVU:
104
                assert(p->kids[0]);
105
                assert(!p->kids[1]);
106
                assert(p->syms[0]);
107
                dumptree(p->kids[0]);
108
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.i);
109
                return;
110
        case ARG: case BCOM: case NEG: case INDIR: case JUMP: case RET:
111
                assert(p->kids[0]);
112
                assert(!p->kids[1]);
113
                dumptree(p->kids[0]);
114
                print("%s\n", opname(p->op));
115
                return;
116
        case CALL:
117
                assert(p->kids[0]);
118
                assert(!p->kids[1]);
119
                assert(optype(p->op) != B);
120
                dumptree(p->kids[0]);
121
                print("%s\n", opname(p->op));
122
                return;
123
        case ASGN: case BOR: case BAND: case BXOR: case RSH: case LSH:
124
        case ADD: case SUB: case DIV: case MUL: case MOD:
125
                assert(p->kids[0]);
126
                assert(p->kids[1]);
127
                dumptree(p->kids[0]);
128
                dumptree(p->kids[1]);
129
                print("%s\n", opname(p->op));
130
                return;
131
        case EQ: case NE: case GT: case GE: case LE: case LT:
132
                assert(p->kids[0]);
133
                assert(p->kids[1]);
134
                assert(p->syms[0]);
135
                assert(p->syms[0]->x.name);
136
                dumptree(p->kids[0]);
137
                dumptree(p->kids[1]);
138
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
139
                return;
140
        }
141
        assert(0);
142
}
143
 
144
static void I(emit)(Node p) {
145
        for (; p; p = p->link)
146
                dumptree(p);
147
}
148
 
149
static void I(export)(Symbol p) {
150
        print("export %s\n", p->x.name);
151
}
152
 
153
static void I(function)(Symbol f, Symbol caller[], Symbol callee[], int ncalls) {
154
        int i;
155
 
156
        (*IR->segment)(CODE);
157
        offset = 0;
158
        for (i = 0; caller[i] && callee[i]; i++) {
159
                offset = roundup(offset, caller[i]->type->align);
160
                caller[i]->x.name = callee[i]->x.name = stringf("%d", offset);
161
                caller[i]->x.offset = callee[i]->x.offset = offset;
162
                offset += caller[i]->type->size;
163
        }
164
        maxargoffset = maxoffset = argoffset = offset = 0;
165
        gencode(caller, callee);
166
        print("proc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
167
        emitcode();
168
        print("endproc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
169
 
170
}
171
 
172
static void gen02(Node p) {
173
        assert(p);
174
        if (generic(p->op) == ARG) {
175
                assert(p->syms[0]);
176
                argoffset += (p->syms[0]->u.c.v.i < 4 ? 4 : p->syms[0]->u.c.v.i);
177
        } else if (generic(p->op) == CALL) {
178
                maxargoffset = (argoffset > maxargoffset ? argoffset : maxargoffset);
179
                argoffset = 0;
180
        }
181
}
182
 
183
static void gen01(Node p) {
184
        if (p) {
185
                gen01(p->kids[0]);
186
                gen01(p->kids[1]);
187
                gen02(p);
188
        }
189
}
190
 
191
static Node I(gen)(Node p) {
192
        Node q;
193
 
194
        assert(p);
195
        for (q = p; q; q = q->link)
196
                gen01(q);
197
        return p;
198
}
199
 
200
static void I(global)(Symbol p) {
201
        print("align %d\n", p->type->align > 4 ? 4 : p->type->align);
202
        print("LABELV %s\n", p->x.name);
203
}
204
 
205
static void I(import)(Symbol p) {
206
        print("import %s\n", p->x.name);
207
}
208
 
209
static void I(local)(Symbol p) {
210
        offset = roundup(offset, p->type->align);
211
        p->x.name = stringf("%d", offset);
212
        p->x.offset = offset;
213
        offset += p->type->size;
214
}
215
 
216
static void I(progbeg)(int argc, char *argv[]) {}
217
 
218
static void I(progend)(void) {}
219
 
220
static void I(space)(int n) {
221
        print("skip %d\n", n);
222
}
223
 
224
static void I(stabline)(Coordinate *cp) {
225
        static char *prevfile;
226
        static int prevline;
227
 
228
        if (cp->file && (prevfile == NULL || strcmp(prevfile, cp->file) != 0)) {
229
                print("file \"%s\"\n", prevfile = cp->file);
230
                prevline = 0;
231
        }
232
        if (cp->y != prevline)
233
                print("line %d\n", prevline = cp->y);
234
}
235
 
236
#define b_blockbeg blockbeg
237
#define b_blockend blockend
238
 
239
Interface bytecodeIR = {
240
        1, 1, 0, /* char */
241
        2, 2, 0, /* short */
242
        4, 4, 0, /* int */
243
        4, 4, 0, /* long */
244
        4, 4, 0, /* long long */
245
        4, 4, 1,        /* float */
246
        8, 8, 1,        /* double */
247
        8, 8, 1,        /* long double */
248
        4, 4, 0, /* T* */
249
        0, 4, 0,  /* struct */
250
        0,               /* little_endian */
251
        0,               /* mulops_calls */
252
        0,               /* wants_callb */
253
        0,               /* wants_argb */
254
        1,              /* left_to_right */
255
        0,               /* wants_dag */
256
        0,               /* unsigned_char */
257
        I(address),
258
        I(blockbeg),
259
        I(blockend),
260
        I(defaddress),
261
        I(defconst),
262
        I(defstring),
263
        I(defsymbol),
264
        I(emit),
265
        I(export),
266
        I(function),
267
        I(gen),
268
        I(global),
269
        I(import),
270
        I(local),
271
        I(progbeg),
272
        I(progend),
273
        I(segment),
274
        I(space),
275
        0,               /* I(stabblock) */
276
        0,               /* I(stabend) */
277
        0,               /* I(stabfend) */
278
        0,               /* I(stabinit) */
279
        I(stabline),
280
        0,               /* I(stabsym) */
281
        0,               /* I(stabtype) */
282
};

powered by: WebSVN 2.1.0

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