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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [trace.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
 
3
static char rcsid[] = "$Id: trace.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
4
 
5
static char *fmt, *fp, *fmtend; /* format string, current & limit pointer */
6
static Tree args;               /* printf arguments */
7
static Symbol frameno;          /* local holding frame number */
8
 
9
/* appendstr - append str to the evolving format string, expanding it if necessary */
10
static void appendstr(char *str) {
11
        do
12
                if (fp == fmtend)
13
                        if (fp) {
14
                                char *s = allocate(2*(fmtend - fmt), FUNC);
15
                                strncpy(s, fmt, fmtend - fmt);
16
                                fp = s + (fmtend - fmt);
17
                                fmtend = s + 2*(fmtend - fmt);
18
                                fmt = s;
19
                        } else {
20
                                fp = fmt = allocate(80, FUNC);
21
                                fmtend = fmt + 80;
22
                        }
23
        while ((*fp++ = *str++) != 0);
24
        fp--;
25
}
26
 
27
/* tracevalue - append format and argument to print the value of e */
28
static void tracevalue(Tree e, int lev) {
29
        Type ty = unqual(e->type);
30
 
31
        switch (ty->op) {
32
        case INT:
33
                if (ty == chartype || ty == signedchar)
34
                        appendstr("'\\x%02x'");
35
                else if (ty == longtype)
36
                        appendstr("0x%ld");
37
                else
38
                        appendstr("0x%d");
39
                break;
40
        case UNSIGNED:
41
                if (ty == chartype || ty == unsignedchar)
42
                        appendstr("'\\x%02x'");
43
                else if (ty == unsignedlong)
44
                        appendstr("0x%lx");
45
                else
46
                        appendstr("0x%x");
47
                break;
48
        case FLOAT:
49
                if (ty == longdouble)
50
                        appendstr("%Lg");
51
                else
52
                        appendstr("%g");
53
                break;
54
        case POINTER:
55
                if (unqual(ty->type) == chartype
56
                ||  unqual(ty->type) == signedchar
57
                ||  unqual(ty->type) == unsignedchar) {
58
                        static Symbol null;
59
                        if (null == NULL)
60
                                null = mkstr("(null)");
61
                        tracevalue(cast(e, unsignedtype), lev + 1);
62
                        appendstr(" \"%.30s\"");
63
                        e = condtree(e, e, pointer(idtree(null->u.c.loc)));
64
                } else {
65
                        appendstr("("); appendstr(typestring(ty, "")); appendstr(")0x%x");
66
                }
67
                break;
68
        case STRUCT: {
69
                Field q;
70
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){");
71
                for (q = ty->u.sym->u.s.flist; q; q = q->link) {
72
                        appendstr(q->name); appendstr("=");
73
                        tracevalue(field(addrof(e), q->name), lev + 1);
74
                        if (q->link)
75
                                appendstr(",");
76
                }
77
                appendstr("}");
78
                return;
79
                }
80
        case UNION:
81
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){...}");
82
                return;
83
        case ARRAY:
84
                if (lev && ty->type->size > 0) {
85
                        int i;
86
                        e = pointer(e);
87
                        appendstr("{");
88
                        for (i = 0; i < ty->size/ty->type->size; i++) {
89
                                Tree p = (*optree['+'])(ADD, e, consttree(i, inttype));
90
                                if (isptr(p->type) && isarray(p->type->type))
91
                                        p = retype(p, p->type->type);
92
                                else
93
                                        p = rvalue(p);
94
                                if (i)
95
                                        appendstr(",");
96
                                tracevalue(p, lev + 1);
97
                        }
98
                        appendstr("}");
99
                } else
100
                        appendstr(typestring(ty, ""));
101
                return;
102
        default:
103
                assert(0);
104
        }
105
        e = cast(e, promote(ty));
106
        args = tree(mkop(ARG,e->type), e->type, e, args);
107
}
108
 
109
/* tracefinis - complete & generate the trace call to print */
110
static void tracefinis(Symbol printer) {
111
        Tree *ap;
112
        Symbol p;
113
 
114
        *fp = 0;
115
        p = mkstr(string(fmt));
116
        for (ap = &args; *ap; ap = &(*ap)->kids[1])
117
                ;
118
        *ap = tree(ARG+P, charptype, pointer(idtree(p->u.c.loc)), 0);
119
        walk(calltree(pointer(idtree(printer)), freturn(printer->type), args, NULL), 0, 0);
120
        args = 0;
121
        fp = fmtend = 0;
122
}
123
 
124
/* tracecall - generate code to trace entry to f */
125
static void tracecall(Symbol printer, Symbol f, void *ignore) {
126
        int i;
127
        Symbol counter = genident(STATIC, inttype, GLOBAL);
128
 
129
        defglobal(counter, BSS);
130
        (*IR->space)(counter->type->size);
131
        frameno = genident(AUTO, inttype, level);
132
        addlocal(frameno);
133
        appendstr(f->name); appendstr("#");
134
        tracevalue(asgn(frameno, incr(INCR, idtree(counter), consttree(1, inttype))), 0);
135
        appendstr("(");
136
        for (i = 0; f->u.f.callee[i]; i++) {
137
                if (i)
138
                        appendstr(",");
139
                appendstr(f->u.f.callee[i]->name); appendstr("=");
140
                tracevalue(idtree(f->u.f.callee[i]), 0);
141
        }
142
        if (variadic(f->type))
143
                appendstr(",...");
144
        appendstr(") called\n");
145
        tracefinis(printer);
146
}
147
 
148
/* tracereturn - generate code to trace return e */
149
static void tracereturn(Symbol printer, Symbol f, Tree e) {
150
        appendstr(f->name); appendstr("#");
151
        tracevalue(idtree(frameno), 0);
152
        appendstr(" returned");
153
        if (freturn(f->type) != voidtype && e) {
154
                appendstr(" ");
155
                tracevalue(e, 0);
156
        }
157
        appendstr("\n");
158
        tracefinis(printer);
159
}
160
 
161
/* traceInit - initialize for tracing */
162
void traceInit(char *arg) {
163
        if (strncmp(arg, "-t", 2) == 0 && strchr(arg, '=') == NULL) {
164
                Symbol printer = mksymbol(EXTERN, arg[2] ? &arg[2] : "printf",
165
                        ftype(inttype, ptr(qual(CONST, chartype)), voidtype, NULL));
166
                printer->defined = 0;
167
                attach((Apply)tracecall,   printer, &events.entry);
168
                attach((Apply)tracereturn, printer, &events.returns);
169
        }
170
}

powered by: WebSVN 2.1.0

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