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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [types.c] - Blame information for rev 83

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

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
#include <float.h>
3
 
4
static char rcsid[] = "$Id: types.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
5
 
6
static Field isfield(const char *, Field);
7
static Type type(int, Type, int, int, void *);
8
 
9
static struct entry {
10
        struct type type;
11
        struct entry *link;
12
} *typetable[128];
13
static int maxlevel;
14
 
15
static Symbol pointersym;
16
 
17
Type chartype;                  /* char */
18
Type doubletype;                /* double */
19
Type floattype;                 /* float */
20
Type inttype;                   /* signed int */
21
Type longdouble;                /* long double */
22
Type longtype;                  /* long */
23
Type longlong;                  /* long long */
24
Type shorttype;                 /* signed short int */
25
Type signedchar;                /* signed char */
26
Type unsignedchar;              /* unsigned char */
27
Type unsignedlong;              /* unsigned long int */
28
Type unsignedlonglong;          /* unsigned long long int */
29
Type unsignedshort;             /* unsigned short int */
30
Type unsignedtype;              /* unsigned int */
31
Type funcptype;                 /* void (*)() */
32
Type charptype;                 /* char* */
33
Type voidptype;                 /* void* */
34
Type voidtype;                  /* basic types: void */
35
Type unsignedptr;               /* unsigned type to hold void* */
36
Type signedptr;                 /* signed type to hold void* */
37
Type widechar;                  /* unsigned type that represents wchar_t */
38
 
39
static Type xxinit(int op, char *name, Metrics m) {
40
        Symbol p = install(string(name), &types, GLOBAL, PERM);
41
        Type ty = type(op, 0, m.size, m.align, p);
42
 
43
        assert(ty->align == 0 || ty->size%ty->align == 0);
44
        p->type = ty;
45
        p->addressed = m.outofline;
46
        switch (ty->op) {
47
        case INT:
48
                p->u.limits.max.i = ones(8*ty->size)>>1;
49
                p->u.limits.min.i = -p->u.limits.max.i - 1;
50
                break;
51
        case UNSIGNED:
52
                p->u.limits.max.u = ones(8*ty->size);
53
                p->u.limits.min.u = 0;
54
                break;
55
        case FLOAT:
56
                if (ty->size == sizeof (float))
57
                        p->u.limits.max.d =  FLT_MAX;
58
                else if (ty->size == sizeof (double))
59
                        p->u.limits.max.d =  DBL_MAX;
60
                else
61
                        p->u.limits.max.d = LDBL_MAX;
62
                p->u.limits.min.d = -p->u.limits.max.d;
63
                break;
64
        default: assert(0);
65
        }
66
        return ty;
67
}
68
static Type type(int op, Type ty, int size, int align, void *sym) {
69
        unsigned h = (op^((unsigned long)ty>>3))
70
&(NELEMS(typetable)-1);
71
        struct entry *tn;
72
 
73
        if (op != FUNCTION && (op != ARRAY || size > 0))
74
                for (tn = typetable[h]; tn; tn = tn->link)
75
                        if (tn->type.op    == op   && tn->type.type  == ty
76
                        &&  tn->type.size  == size && tn->type.align == align
77
                        &&  tn->type.u.sym == sym)
78
                                return &tn->type;
79
        NEW0(tn, PERM);
80
        tn->type.op = op;
81
        tn->type.type = ty;
82
        tn->type.size = size;
83
        tn->type.align = align;
84
        tn->type.u.sym = sym;
85
        tn->link = typetable[h];
86
        typetable[h] = tn;
87
        return &tn->type;
88
}
89
void type_init(int argc, char *argv[]) {
90
        static int inited;
91
        int i;
92
 
93
        if (inited)
94
                return;
95
        inited = 1;
96
        if (!IR)
97
                return;
98
        for (i = 1; i < argc; i++) {
99
                int size, align, outofline;
100
                if (strncmp(argv[i], "-unsigned_char=", 15) == 0)
101
                        IR->unsigned_char = argv[i][15] - '0';
102
#define xx(name) \
103
                else if (sscanf(argv[i], "-" #name "=%d,%d,%d", &size, &align, &outofline) == 3) { \
104
                        IR->name.size = size; IR->name.align = align; \
105
                        IR->name.outofline = outofline; }
106
        xx(charmetric)
107
        xx(shortmetric)
108
        xx(intmetric)
109
        xx(longmetric)
110
        xx(longlongmetric)
111
        xx(floatmetric)
112
        xx(doublemetric)
113
        xx(longdoublemetric)
114
        xx(ptrmetric)
115
        xx(structmetric)
116
#undef xx
117
        }
118
#define xx(v,name,op,metrics) v=xxinit(op,name,IR->metrics)
119
        xx(chartype,        "char",              IR->unsigned_char ? UNSIGNED : INT,charmetric);
120
        xx(doubletype,      "double",            FLOAT,   doublemetric);
121
        xx(floattype,       "float",             FLOAT,   floatmetric);
122
        xx(inttype,         "int",               INT,     intmetric);
123
        xx(longdouble,      "long double",       FLOAT,   longdoublemetric);
124
        xx(longtype,        "long int",          INT,     longmetric);
125
        xx(longlong,        "long long int",     INT,     longlongmetric);
126
        xx(shorttype,       "short",             INT,     shortmetric);
127
        xx(signedchar,      "signed char",       INT,     charmetric);
128
        xx(unsignedchar,    "unsigned char",     UNSIGNED,charmetric);
129
        xx(unsignedlong,    "unsigned long",     UNSIGNED,longmetric);
130
        xx(unsignedshort,   "unsigned short",    UNSIGNED,shortmetric);
131
        xx(unsignedtype,    "unsigned int",      UNSIGNED,intmetric);
132
        xx(unsignedlonglong,"unsigned long long",UNSIGNED,longlongmetric);
133
#undef xx
134
        {
135
                Symbol p;
136
                p = install(string("void"), &types, GLOBAL, PERM);
137
                voidtype = type(VOID, NULL, 0, 0, p);
138
                p->type = voidtype;
139
        }
140
        pointersym = install(string("T*"), &types, GLOBAL, PERM);
141
        pointersym->addressed = IR->ptrmetric.outofline;
142
        pointersym->u.limits.max.p = (void*)ones(8*IR->ptrmetric.size);
143
        pointersym->u.limits.min.p = 0;
144
        voidptype = ptr(voidtype);
145
        funcptype = ptr(func(voidtype, NULL, 1));
146
        charptype = ptr(chartype);
147
#define xx(v,t) if (v==NULL && t->size==voidptype->size && t->align==voidptype->align) v=t
148
        xx(unsignedptr,unsignedshort);
149
        xx(unsignedptr,unsignedtype);
150
        xx(unsignedptr,unsignedlong);
151
        xx(unsignedptr,unsignedlonglong);
152
        if (unsignedptr == NULL)
153
                unsignedptr = type(UNSIGNED, NULL, voidptype->size, voidptype->align, voidptype->u.sym);
154
        xx(signedptr,shorttype);
155
        xx(signedptr,inttype);
156
        xx(signedptr,longtype);
157
        xx(signedptr,longlong);
158
        if (signedptr == NULL)
159
                signedptr = type(INT, NULL, voidptype->size, voidptype->align, voidptype->u.sym);
160
#undef xx
161
        widechar = unsignedshort;
162
        for (i = 0; i < argc; i++) {
163
#define xx(name,type) \
164
                if (strcmp(argv[i], "-wchar_t=" #name) == 0) \
165
                        widechar = type;
166
                xx(unsigned_char,unsignedchar)
167
                xx(unsigned_int,unsignedtype)
168
                xx(unsigned_short,unsignedshort)
169
        }
170
#undef xx
171
}
172
void rmtypes(int lev) {
173
        if (maxlevel >= lev) {
174
                int i;
175
                maxlevel = 0;
176
                for (i = 0; i < NELEMS(typetable); i++) {
177
                        struct entry *tn, **tq = &typetable[i];
178
                        while ((tn = *tq) != NULL)
179
                                if (tn->type.op == FUNCTION)
180
                                        tq = &tn->link;
181
                                else if (tn->type.u.sym && tn->type.u.sym->scope >= lev)
182
                                        *tq = tn->link;
183
                                else {
184
                                        if (tn->type.u.sym && tn->type.u.sym->scope > maxlevel)
185
                                                maxlevel = tn->type.u.sym->scope;
186
                                        tq = &tn->link;
187
                                }
188
 
189
                }
190
        }
191
}
192
Type ptr(Type ty) {
193
        return type(POINTER, ty, IR->ptrmetric.size,
194
                IR->ptrmetric.align, pointersym);
195
}
196
Type deref(Type ty) {
197
        if (isptr(ty))
198
                ty = ty->type;
199
        else
200
                error("type error: %s\n", "pointer expected");
201
        return isenum(ty) ? unqual(ty)->type : ty;
202
}
203
Type array(Type ty, int n, int a) {
204
        assert(ty);
205
        if (isfunc(ty)) {
206
                error("illegal type `array of %t'\n", ty);
207
                return array(inttype, n, 0);
208
        }
209
        if (isarray(ty) && ty->size == 0)
210
                error("missing array size\n");
211
        if (ty->size == 0) {
212
                if (unqual(ty) == voidtype)
213
                        error("illegal type `array of %t'\n", ty);
214
                else if (Aflag >= 2)
215
                        warning("declaring type array of %t' is undefined\n", ty);
216
 
217
        } else if (n > INT_MAX/ty->size) {
218
                error("size of `array of %t' exceeds %d bytes\n",
219
                        ty, INT_MAX);
220
                n = 1;
221
        }
222
        return type(ARRAY, ty, n*ty->size,
223
                a ? a : ty->align, NULL);
224
}
225
Type atop(Type ty) {
226
        if (isarray(ty))
227
                return ptr(ty->type);
228
        error("type error: %s\n", "array expected");
229
        return ptr(ty);
230
}
231
Type qual(int op, Type ty) {
232
        if (isarray(ty))
233
                ty = type(ARRAY, qual(op, ty->type), ty->size,
234
                        ty->align, NULL);
235
        else if (isfunc(ty))
236
                warning("qualified function type ignored\n");
237
        else if (isconst(ty)    && op == CONST
238
        ||       isvolatile(ty) && op == VOLATILE)
239
                error("illegal type `%k %t'\n", op, ty);
240
        else {
241
                if (isqual(ty)) {
242
                        op += ty->op;
243
                        ty = ty->type;
244
                }
245
                ty = type(op, ty, ty->size, ty->align, NULL);
246
        }
247
        return ty;
248
}
249
Type func(Type ty, Type *proto, int style) {
250
        if (ty && (isarray(ty) || isfunc(ty)))
251
                error("illegal return type `%t'\n", ty);
252
        ty = type(FUNCTION, ty, 0, 0, NULL);
253
        ty->u.f.proto = proto;
254
        ty->u.f.oldstyle = style;
255
        return ty;
256
}
257
Type freturn(Type ty) {
258
        if (isfunc(ty))
259
                return ty->type;
260
        error("type error: %s\n", "function expected");
261
        return inttype;
262
}
263
int variadic(Type ty) {
264
        if (isfunc(ty) && ty->u.f.proto) {
265
                int i;
266
                for (i = 0; ty->u.f.proto[i]; i++)
267
                        ;
268
                return i > 1 && ty->u.f.proto[i-1] == voidtype;
269
        }
270
        return 0;
271
}
272
Type newstruct(int op, char *tag) {
273
        Symbol p;
274
 
275
        assert(tag);
276
        if (*tag == 0)
277
                tag = stringd(genlabel(1));
278
        else
279
                if ((p = lookup(tag, types)) != NULL && (p->scope == level
280
                || p->scope == PARAM && level == PARAM+1)) {
281
                        if (p->type->op == op && !p->defined)
282
                                return p->type;
283
                        error("redefinition of `%s' previously defined at %w\n",
284
                                p->name, &p->src);
285
                }
286
        p = install(tag, &types, level, PERM);
287
        p->type = type(op, NULL, 0, 0, p);
288
        if (p->scope > maxlevel)
289
                maxlevel = p->scope;
290
        p->src = src;
291
        return p->type;
292
}
293
Field newfield(char *name, Type ty, Type fty) {
294
        Field p, *q = &ty->u.sym->u.s.flist;
295
 
296
        if (name == NULL)
297
                name = stringd(genlabel(1));
298
        for (p = *q; p; q = &p->link, p = *q)
299
                if (p->name == name)
300
                        error("duplicate field name `%s' in `%t'\n",
301
                                name, ty);
302
        NEW0(p, PERM);
303
        *q = p;
304
        p->name = name;
305
        p->type = fty;
306
        if (xref) {                                                     /* omit */
307
                if (ty->u.sym->u.s.ftab == NULL)                        /* omit */
308
                        ty->u.sym->u.s.ftab = table(NULL, level);       /* omit */
309
                install(name, &ty->u.sym->u.s.ftab, 0, PERM)->src = src;/* omit */
310
        }                                                               /* omit */
311
        return p;
312
}
313
int eqtype(Type ty1, Type ty2, int ret) {
314
        if (ty1 == ty2)
315
                return 1;
316
        if (ty1->op != ty2->op)
317
                return 0;
318
        switch (ty1->op) {
319
        case ENUM: case UNION: case STRUCT:
320
        case UNSIGNED: case INT: case FLOAT:
321
                return 0;
322
        case POINTER:  return eqtype(ty1->type, ty2->type, 1);
323
        case VOLATILE: case CONST+VOLATILE:
324
        case CONST:    return eqtype(ty1->type, ty2->type, 1);
325
        case ARRAY:    if (eqtype(ty1->type, ty2->type, 1)) {
326
                        if (ty1->size == ty2->size)
327
                                return 1;
328
                        if (ty1->size == 0 || ty2->size == 0)
329
                                return ret;
330
                       }
331
                       return 0;
332
        case FUNCTION: if (eqtype(ty1->type, ty2->type, 1)) {
333
                        Type *p1 = ty1->u.f.proto, *p2 = ty2->u.f.proto;
334
                        if (p1 == p2)
335
                                return 1;
336
                        if (p1 && p2) {
337
                                for ( ; *p1 && *p2; p1++, p2++)
338
                                        if (eqtype(unqual(*p1), unqual(*p2), 1) == 0)
339
                                                return 0;
340
                                if (*p1 == NULL && *p2 == NULL)
341
                                        return 1;
342
                        } else {
343
                                if (variadic(p1 ? ty1 : ty2))
344
                                        return 0;
345
                                if (p1 == NULL)
346
                                        p1 = p2;
347
                                for ( ; *p1; p1++) {
348
                                        Type ty = unqual(*p1);
349
                                        if (promote(ty) != (isenum(ty) ? ty->type : ty))
350
                                                return 0;
351
                                }
352
                                return 1;
353
                        }
354
                       }
355
                       return 0;
356
        }
357
        assert(0); return 0;
358
}
359
Type promote(Type ty) {
360
        ty = unqual(ty);
361
        switch (ty->op) {
362
        case ENUM:
363
                return inttype;
364
        case INT:
365
                if (ty->size < inttype->size)
366
                        return inttype;
367
                break;
368
        case UNSIGNED:
369
                if (ty->size < inttype->size)
370
                        return inttype;
371
                if (ty->size < unsignedtype->size)
372
                        return unsignedtype;
373
                break;
374
        case FLOAT:
375
                if (ty->size < doubletype->size)
376
                        return doubletype;
377
        }
378
        return ty;
379
}
380
Type signedint(Type ty) {
381
        if (ty->op == INT)
382
                return ty;
383
        assert(ty->op == UNSIGNED);
384
#define xx(t) if (ty->size == t->size) return t
385
        xx(inttype);
386
        xx(longtype);
387
        xx(longlong);
388
#undef xx
389
        assert(0); return NULL;
390
}
391
Type compose(Type ty1, Type ty2) {
392
        if (ty1 == ty2)
393
                return ty1;
394
        assert(ty1->op == ty2->op);
395
        switch (ty1->op) {
396
        case POINTER:
397
                return ptr(compose(ty1->type, ty2->type));
398
        case CONST+VOLATILE:
399
                return qual(CONST, qual(VOLATILE,
400
                        compose(ty1->type, ty2->type)));
401
        case CONST: case VOLATILE:
402
                return qual(ty1->op, compose(ty1->type, ty2->type));
403
        case ARRAY:    { Type ty = compose(ty1->type, ty2->type);
404
                         if (ty1->size && (ty1->type->size && ty2->size == 0 || ty1->size == ty2->size))
405
                                return array(ty, ty1->size/ty1->type->size, ty1->align);
406
                         if (ty2->size && ty2->type->size && ty1->size == 0)
407
                                return array(ty, ty2->size/ty2->type->size, ty2->align);
408
                         return array(ty, 0, 0);    }
409
        case FUNCTION: { Type *p1  = ty1->u.f.proto, *p2 = ty2->u.f.proto;
410
                         Type ty   = compose(ty1->type, ty2->type);
411
                         List tlist = NULL;
412
                         if (p1 == NULL && p2 == NULL)
413
                                return func(ty, NULL, 1);
414
                         if (p1 && p2 == NULL)
415
                                return func(ty, p1, ty1->u.f.oldstyle);
416
                         if (p2 && p1 == NULL)
417
                                return func(ty, p2, ty2->u.f.oldstyle);
418
                         for ( ; *p1 && *p2; p1++, p2++) {
419
                                Type ty = compose(unqual(*p1), unqual(*p2));
420
                                if (isconst(*p1)    || isconst(*p2))
421
                                        ty = qual(CONST, ty);
422
                                if (isvolatile(*p1) || isvolatile(*p2))
423
                                        ty = qual(VOLATILE, ty);
424
                                tlist = append(ty, tlist);
425
                         }
426
                         assert(*p1 == NULL && *p2 == NULL);
427
                         return func(ty, ltov(&tlist, PERM), 0); }
428
        }
429
        assert(0); return NULL;
430
}
431
int ttob(Type ty) {
432
        switch (ty->op) {
433
        case CONST: case VOLATILE: case CONST+VOLATILE:
434
                return ttob(ty->type);
435
        case VOID: case INT: case UNSIGNED: case FLOAT:
436
                return ty->op + sizeop(ty->size);
437
        case POINTER:
438
                return POINTER + sizeop(voidptype->size);
439
        case FUNCTION:
440
                return POINTER + sizeop(funcptype->size);
441
        case ARRAY: case STRUCT: case UNION:
442
                return STRUCT;
443
        case ENUM:
444
                return INT + sizeop(inttype->size);
445
        }
446
        assert(0); return INT;
447
}
448
Type btot(int op, int size) {
449
#define xx(ty) if (size == (ty)->size) return ty;
450
        switch (optype(op)) {
451
        case F:
452
                xx(floattype);
453
                xx(doubletype);
454
                xx(longdouble);
455
                assert(0); return 0;
456
        case I:
457
                if (chartype->op == INT)
458
                        xx(chartype);
459
                xx(signedchar);
460
                xx(shorttype);
461
                xx(inttype);
462
                xx(longtype);
463
                xx(longlong);
464
                assert(0); return 0;
465
        case U:
466
                if (chartype->op == UNSIGNED)
467
                        xx(chartype);
468
                xx(unsignedchar);
469
                xx(unsignedshort);
470
                xx(unsignedtype);
471
                xx(unsignedlong);
472
                xx(unsignedlonglong);
473
                assert(0); return 0;
474
        case P:
475
                xx(voidptype);
476
                xx(funcptype);
477
                assert(0); return 0;
478
        }
479
#undef xx
480
        assert(0); return 0;
481
}
482
int hasproto(Type ty) {
483
        if (ty == 0)
484
                return 1;
485
        switch (ty->op) {
486
        case CONST: case VOLATILE: case CONST+VOLATILE: case POINTER:
487
        case ARRAY:
488
                return hasproto(ty->type);
489
        case FUNCTION:
490
                return hasproto(ty->type) && ty->u.f.proto;
491
        case STRUCT: case UNION:
492
        case VOID:   case FLOAT: case ENUM:  case INT: case UNSIGNED:
493
                return 1;
494
        }
495
        assert(0); return 0;
496
}
497
/* fieldlist - construct a flat list of fields in type ty */
498
Field fieldlist(Type ty) {
499
        return ty->u.sym->u.s.flist;
500
}
501
 
502
/* fieldref - find field name of type ty, return entry */
503
Field fieldref(const char *name, Type ty) {
504
        Field p = isfield(name, unqual(ty)->u.sym->u.s.flist);
505
 
506
        if (p && xref) {
507
                Symbol q;
508
                assert(unqual(ty)->u.sym->u.s.ftab);
509
                q = lookup(name, unqual(ty)->u.sym->u.s.ftab);
510
                assert(q);
511
                use(q, src);
512
        }
513
        return p;
514
}
515
 
516
/* ftype - return a function type for rty function (ty,...)' */
517
Type ftype(Type rty, ...) {
518
        va_list ap;
519
        Type ty = NULL;
520
        List list = NULL;
521
 
522
        va_start(ap, rty);
523
        ty = va_arg(ap, Type);
524
        for ( ; ty != NULL; ty = va_arg(ap, Type))
525
                list = append(ty, list);
526
        va_end(ap);
527
        return func(rty, ltov(&list, PERM), 0);
528
}
529
 
530
/* isfield - if name is a field in flist, return pointer to the field structure */
531
static Field isfield(const char *name, Field flist) {
532
        for ( ; flist; flist = flist->link)
533
                if (flist->name == name)
534
                        break;
535
        return flist;
536
}
537
 
538
/* outtype - output type ty */
539
void outtype(Type ty, FILE *f) {
540
        switch (ty->op) {
541
        case CONST+VOLATILE: case CONST: case VOLATILE:
542
                fprint(f, "%k %t", ty->op, ty->type);
543
                break;
544
        case STRUCT: case UNION: case ENUM:
545
                assert(ty->u.sym);
546
                if (ty->size == 0)
547
                        fprint(f, "incomplete ");
548
                assert(ty->u.sym->name);
549
                if (*ty->u.sym->name >= '1' && *ty->u.sym->name <= '9') {
550
                        Symbol p = findtype(ty);
551
                        if (p == 0)
552
                                fprint(f, "%k defined at %w", ty->op, &ty->u.sym->src);
553
                        else
554
                                fprint(f, p->name);
555
                } else {
556
                        fprint(f, "%k %s", ty->op, ty->u.sym->name);
557
                        if (ty->size == 0)
558
                                fprint(f, " defined at %w", &ty->u.sym->src);
559
                }
560
                break;
561
        case VOID: case FLOAT: case INT: case UNSIGNED:
562
                fprint(f, ty->u.sym->name);
563
                break;
564
        case POINTER:
565
                fprint(f, "pointer to %t", ty->type);
566
                break;
567
        case FUNCTION:
568
                fprint(f, "%t function", ty->type);
569
                if (ty->u.f.proto && ty->u.f.proto[0]) {
570
                        int i;
571
                        fprint(f, "(%t", ty->u.f.proto[0]);
572
                        for (i = 1; ty->u.f.proto[i]; i++)
573
                                if (ty->u.f.proto[i] == voidtype)
574
                                        fprint(f, ",...");
575
                                else
576
                                        fprint(f, ",%t", ty->u.f.proto[i]);
577
                        fprint(f, ")");
578
                } else if (ty->u.f.proto && ty->u.f.proto[0] == 0)
579
                        fprint(f, "(void)");
580
 
581
                break;
582
        case ARRAY:
583
                if (ty->size > 0 && ty->type && ty->type->size > 0) {
584
                        fprint(f, "array %d", ty->size/ty->type->size);
585
                        while (ty->type && isarray(ty->type) && ty->type->type->size > 0) {
586
                                ty = ty->type;
587
                                fprint(f, ",%d", ty->size/ty->type->size);
588
                        }
589
                } else
590
                        fprint(f, "incomplete array");
591
                if (ty->type)
592
                        fprint(f, " of %t", ty->type);
593
                break;
594
        default: assert(0);
595
        }
596
}
597
 
598
/* printdecl - output a C declaration for symbol p of type ty */
599
void printdecl(Symbol p, Type ty) {
600
        switch (p->sclass) {
601
        case AUTO:
602
                fprint(stderr, "%s;\n", typestring(ty, p->name));
603
                break;
604
        case STATIC: case EXTERN:
605
                fprint(stderr, "%k %s;\n", p->sclass, typestring(ty, p->name));
606
                break;
607
        case TYPEDEF: case ENUM:
608
                break;
609
        default: assert(0);
610
        }
611
}
612
 
613
/* printproto - output a prototype declaration for function p */
614
void printproto(Symbol p, Symbol callee[]) {
615
        if (p->type->u.f.proto)
616
                printdecl(p, p->type);
617
        else {
618
                int i;
619
                List list = 0;
620
                if (callee[0] == 0)
621
                        list = append(voidtype, list);
622
                else
623
                        for (i = 0; callee[i]; i++)
624
                                list = append(callee[i]->type, list);
625
                printdecl(p, func(freturn(p->type), ltov(&list, PERM), 0));
626
        }
627
}
628
 
629
/* prtype - print details of type ty on f with given indent */
630
static void prtype(Type ty, FILE *f, int indent, unsigned mark) {
631
        switch (ty->op) {
632
        default:
633
                fprint(f, "(%d %d %d [%p])", ty->op, ty->size, ty->align, ty->u.sym);
634
                break;
635
        case FLOAT: case INT: case UNSIGNED: case VOID:
636
                fprint(f, "(%k %d %d [\"%s\"])", ty->op, ty->size, ty->align, ty->u.sym->name);
637
                break;
638
        case CONST+VOLATILE: case CONST: case VOLATILE: case POINTER: case ARRAY:
639
                fprint(f, "(%k %d %d ", ty->op, ty->size, ty->align);
640
                prtype(ty->type, f, indent+1, mark);
641
                fprint(f, ")");
642
                break;
643
        case STRUCT: case UNION:
644
                fprint(f, "(%k %d %d [\"%s\"]", ty->op, ty->size, ty->align, ty->u.sym->name);
645
                if (ty->x.marked != mark) {
646
                        Field p;
647
                        ty->x.marked = mark;
648
                        for (p = ty->u.sym->u.s.flist; p; p = p->link) {
649
                                fprint(f, "\n%I", indent+1);
650
                                prtype(p->type, f, indent+1, mark);
651
                                fprint(f, " %s@%d", p->name, p->offset);
652
                                if (p->lsb)
653
                                        fprint(f, ":%d..%d",
654
                                                fieldsize(p) + fieldright(p), fieldright(p));
655
                        }
656
                        fprint(f, "\n%I", indent);
657
                }
658
                fprint(f, ")");
659
                break;
660
        case ENUM:
661
                fprint(f, "(%k %d %d [\"%s\"]", ty->op, ty->size, ty->align, ty->u.sym->name);
662
                if (ty->x.marked != mark) {
663
                        int i;
664
                        Symbol *p = ty->u.sym->u.idlist;
665
                        ty->x.marked = mark;
666
                        for (i = 0; p[i] != NULL; i++)
667
                                fprint(f, "%I%s=%d\n", indent+1, p[i]->name, p[i]->u.value);
668
                }
669
                fprint(f, ")");
670
                break;
671
        case FUNCTION:
672
                fprint(f, "(%k %d %d ", ty->op, ty->size, ty->align);
673
                prtype(ty->type, f, indent+1, mark);
674
                if (ty->u.f.proto) {
675
                        int i;
676
                        fprint(f, "\n%I{", indent+1);
677
                        for (i = 0; ty->u.f.proto[i]; i++) {
678
                                if (i > 0)
679
                                        fprint(f, "%I", indent+2);
680
                                prtype(ty->u.f.proto[i], f, indent+2, mark);
681
                                fprint(f, "\n");
682
                        }
683
                        fprint(f, "%I}", indent+1);
684
                }
685
                fprint(f, ")");
686
                break;
687
        }
688
}
689
 
690
/* printtype - print details of type ty on fd */
691
void printtype(Type ty, int fd) {
692
        static unsigned mark;
693
        prtype(ty, fd == 1 ? stdout : stderr, 0, ++mark);
694
        fprint(fd == 1 ? stdout : stderr, "\n");
695
}
696
 
697
/* typestring - return ty as C declaration for str, which may be "" */
698
char *typestring(Type ty, char *str) {
699
        for ( ; ty; ty = ty->type) {
700
                Symbol p;
701
                switch (ty->op) {
702
                case CONST+VOLATILE: case CONST: case VOLATILE:
703
                        if (isptr(ty->type))
704
                                str = stringf("%k %s", ty->op, str);
705
                        else
706
                                return stringf("%k %s", ty->op, typestring(ty->type, str));
707
                        break;
708
                case STRUCT: case UNION: case ENUM:
709
                        assert(ty->u.sym);
710
                        if ((p = findtype(ty)) != NULL)
711
                                return *str ? stringf("%s %s", p->name, str) : p->name;
712
                        if (*ty->u.sym->name >= '1' && *ty->u.sym->name <= '9')
713
                                warning("unnamed %k in prototype\n", ty->op);
714
                        if (*str)
715
                                return stringf("%k %s %s", ty->op, ty->u.sym->name, str);
716
                        else
717
                                return stringf("%k %s", ty->op, ty->u.sym->name);
718
                case VOID: case FLOAT: case INT: case UNSIGNED:
719
                        return *str ? stringf("%s %s", ty->u.sym->name, str) : ty->u.sym->name;
720
                case POINTER:
721
                        if (!ischar(ty->type) && (p = findtype(ty)) != NULL)
722
                                return *str ? stringf("%s %s", p->name, str) : p->name;
723
                        str = stringf(isarray(ty->type) || isfunc(ty->type) ? "(*%s)" : "*%s", str);
724
                        break;
725
                case FUNCTION:
726
                        if ((p = findtype(ty)) != NULL)
727
                                return *str ? stringf("%s %s", p->name, str) : p->name;
728
                        if (ty->u.f.proto == 0)
729
                                str = stringf("%s()", str);
730
                        else if (ty->u.f.proto[0]) {
731
                                int i;
732
                                str = stringf("%s(%s", str, typestring(ty->u.f.proto[0], ""));
733
                                for (i = 1; ty->u.f.proto[i]; i++)
734
                                        if (ty->u.f.proto[i] == voidtype)
735
                                                str = stringf("%s, ...", str);
736
                                        else
737
                                                str = stringf("%s, %s", str, typestring(ty->u.f.proto[i], ""));
738
                                str = stringf("%s)", str);
739
                        } else
740
                                str = stringf("%s(void)", str);
741
                        break;
742
                case ARRAY:
743
                        if ((p = findtype(ty)) != NULL)
744
                                return *str ? stringf("%s %s", p->name, str) : p->name;
745
                        if (ty->type && ty->type->size > 0)
746
                                str = stringf("%s[%d]", str, ty->size/ty->type->size);
747
                        else
748
                                str = stringf("%s[]", str);
749
                        break;
750
                default: assert(0);
751
                }
752
        }
753
        assert(0); return 0;
754
}
755
 

powered by: WebSVN 2.1.0

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