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

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.23/] [lcc/] [src/] [trace.c] - Diff between revs 4 and 157

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

Rev 4 Rev 157
#include "c.h"
#include "c.h"
 
 
static char rcsid[] = "$Id: trace.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
static char rcsid[] = "$Id: trace.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
 
 
static char *fmt, *fp, *fmtend; /* format string, current & limit pointer */
static char *fmt, *fp, *fmtend; /* format string, current & limit pointer */
static Tree args;               /* printf arguments */
static Tree args;               /* printf arguments */
static Symbol frameno;          /* local holding frame number */
static Symbol frameno;          /* local holding frame number */
 
 
/* appendstr - append str to the evolving format string, expanding it if necessary */
/* appendstr - append str to the evolving format string, expanding it if necessary */
static void appendstr(char *str) {
static void appendstr(char *str) {
        do
        do
                if (fp == fmtend)
                if (fp == fmtend)
                        if (fp) {
                        if (fp) {
                                char *s = allocate(2*(fmtend - fmt), FUNC);
                                char *s = allocate(2*(fmtend - fmt), FUNC);
                                strncpy(s, fmt, fmtend - fmt);
                                strncpy(s, fmt, fmtend - fmt);
                                fp = s + (fmtend - fmt);
                                fp = s + (fmtend - fmt);
                                fmtend = s + 2*(fmtend - fmt);
                                fmtend = s + 2*(fmtend - fmt);
                                fmt = s;
                                fmt = s;
                        } else {
                        } else {
                                fp = fmt = allocate(80, FUNC);
                                fp = fmt = allocate(80, FUNC);
                                fmtend = fmt + 80;
                                fmtend = fmt + 80;
                        }
                        }
        while ((*fp++ = *str++) != 0);
        while ((*fp++ = *str++) != 0);
        fp--;
        fp--;
}
}
 
 
/* tracevalue - append format and argument to print the value of e */
/* tracevalue - append format and argument to print the value of e */
static void tracevalue(Tree e, int lev) {
static void tracevalue(Tree e, int lev) {
        Type ty = unqual(e->type);
        Type ty = unqual(e->type);
 
 
        switch (ty->op) {
        switch (ty->op) {
        case INT:
        case INT:
                if (ty == chartype || ty == signedchar)
                if (ty == chartype || ty == signedchar)
                        appendstr("'\\x%02x'");
                        appendstr("'\\x%02x'");
                else if (ty == longtype)
                else if (ty == longtype)
                        appendstr("0x%ld");
                        appendstr("0x%ld");
                else
                else
                        appendstr("0x%d");
                        appendstr("0x%d");
                break;
                break;
        case UNSIGNED:
        case UNSIGNED:
                if (ty == chartype || ty == unsignedchar)
                if (ty == chartype || ty == unsignedchar)
                        appendstr("'\\x%02x'");
                        appendstr("'\\x%02x'");
                else if (ty == unsignedlong)
                else if (ty == unsignedlong)
                        appendstr("0x%lx");
                        appendstr("0x%lx");
                else
                else
                        appendstr("0x%x");
                        appendstr("0x%x");
                break;
                break;
        case FLOAT:
        case FLOAT:
                if (ty == longdouble)
                if (ty == longdouble)
                        appendstr("%Lg");
                        appendstr("%Lg");
                else
                else
                        appendstr("%g");
                        appendstr("%g");
                break;
                break;
        case POINTER:
        case POINTER:
                if (unqual(ty->type) == chartype
                if (unqual(ty->type) == chartype
                ||  unqual(ty->type) == signedchar
                ||  unqual(ty->type) == signedchar
                ||  unqual(ty->type) == unsignedchar) {
                ||  unqual(ty->type) == unsignedchar) {
                        static Symbol null;
                        static Symbol null;
                        if (null == NULL)
                        if (null == NULL)
                                null = mkstr("(null)");
                                null = mkstr("(null)");
                        tracevalue(cast(e, unsignedtype), lev + 1);
                        tracevalue(cast(e, unsignedtype), lev + 1);
                        appendstr(" \"%.30s\"");
                        appendstr(" \"%.30s\"");
                        e = condtree(e, e, pointer(idtree(null->u.c.loc)));
                        e = condtree(e, e, pointer(idtree(null->u.c.loc)));
                } else {
                } else {
                        appendstr("("); appendstr(typestring(ty, "")); appendstr(")0x%x");
                        appendstr("("); appendstr(typestring(ty, "")); appendstr(")0x%x");
                }
                }
                break;
                break;
        case STRUCT: {
        case STRUCT: {
                Field q;
                Field q;
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){");
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){");
                for (q = ty->u.sym->u.s.flist; q; q = q->link) {
                for (q = ty->u.sym->u.s.flist; q; q = q->link) {
                        appendstr(q->name); appendstr("=");
                        appendstr(q->name); appendstr("=");
                        tracevalue(field(addrof(e), q->name), lev + 1);
                        tracevalue(field(addrof(e), q->name), lev + 1);
                        if (q->link)
                        if (q->link)
                                appendstr(",");
                                appendstr(",");
                }
                }
                appendstr("}");
                appendstr("}");
                return;
                return;
                }
                }
        case UNION:
        case UNION:
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){...}");
                appendstr("("); appendstr(typestring(ty, "")); appendstr("){...}");
                return;
                return;
        case ARRAY:
        case ARRAY:
                if (lev && ty->type->size > 0) {
                if (lev && ty->type->size > 0) {
                        int i;
                        int i;
                        e = pointer(e);
                        e = pointer(e);
                        appendstr("{");
                        appendstr("{");
                        for (i = 0; i < ty->size/ty->type->size; i++) {
                        for (i = 0; i < ty->size/ty->type->size; i++) {
                                Tree p = (*optree['+'])(ADD, e, consttree(i, inttype));
                                Tree p = (*optree['+'])(ADD, e, consttree(i, inttype));
                                if (isptr(p->type) && isarray(p->type->type))
                                if (isptr(p->type) && isarray(p->type->type))
                                        p = retype(p, p->type->type);
                                        p = retype(p, p->type->type);
                                else
                                else
                                        p = rvalue(p);
                                        p = rvalue(p);
                                if (i)
                                if (i)
                                        appendstr(",");
                                        appendstr(",");
                                tracevalue(p, lev + 1);
                                tracevalue(p, lev + 1);
                        }
                        }
                        appendstr("}");
                        appendstr("}");
                } else
                } else
                        appendstr(typestring(ty, ""));
                        appendstr(typestring(ty, ""));
                return;
                return;
        default:
        default:
                assert(0);
                assert(0);
        }
        }
        e = cast(e, promote(ty));
        e = cast(e, promote(ty));
        args = tree(mkop(ARG,e->type), e->type, e, args);
        args = tree(mkop(ARG,e->type), e->type, e, args);
}
}
 
 
/* tracefinis - complete & generate the trace call to print */
/* tracefinis - complete & generate the trace call to print */
static void tracefinis(Symbol printer) {
static void tracefinis(Symbol printer) {
        Tree *ap;
        Tree *ap;
        Symbol p;
        Symbol p;
 
 
        *fp = 0;
        *fp = 0;
        p = mkstr(string(fmt));
        p = mkstr(string(fmt));
        for (ap = &args; *ap; ap = &(*ap)->kids[1])
        for (ap = &args; *ap; ap = &(*ap)->kids[1])
                ;
                ;
        *ap = tree(ARG+P, charptype, pointer(idtree(p->u.c.loc)), 0);
        *ap = tree(ARG+P, charptype, pointer(idtree(p->u.c.loc)), 0);
        walk(calltree(pointer(idtree(printer)), freturn(printer->type), args, NULL), 0, 0);
        walk(calltree(pointer(idtree(printer)), freturn(printer->type), args, NULL), 0, 0);
        args = 0;
        args = 0;
        fp = fmtend = 0;
        fp = fmtend = 0;
}
}
 
 
/* tracecall - generate code to trace entry to f */
/* tracecall - generate code to trace entry to f */
static void tracecall(Symbol printer, Symbol f, void *ignore) {
static void tracecall(Symbol printer, Symbol f, void *ignore) {
        int i;
        int i;
        Symbol counter = genident(STATIC, inttype, GLOBAL);
        Symbol counter = genident(STATIC, inttype, GLOBAL);
 
 
        defglobal(counter, BSS);
        defglobal(counter, BSS);
        (*IR->space)(counter->type->size);
        (*IR->space)(counter->type->size);
        frameno = genident(AUTO, inttype, level);
        frameno = genident(AUTO, inttype, level);
        addlocal(frameno);
        addlocal(frameno);
        appendstr(f->name); appendstr("#");
        appendstr(f->name); appendstr("#");
        tracevalue(asgn(frameno, incr(INCR, idtree(counter), consttree(1, inttype))), 0);
        tracevalue(asgn(frameno, incr(INCR, idtree(counter), consttree(1, inttype))), 0);
        appendstr("(");
        appendstr("(");
        for (i = 0; f->u.f.callee[i]; i++) {
        for (i = 0; f->u.f.callee[i]; i++) {
                if (i)
                if (i)
                        appendstr(",");
                        appendstr(",");
                appendstr(f->u.f.callee[i]->name); appendstr("=");
                appendstr(f->u.f.callee[i]->name); appendstr("=");
                tracevalue(idtree(f->u.f.callee[i]), 0);
                tracevalue(idtree(f->u.f.callee[i]), 0);
        }
        }
        if (variadic(f->type))
        if (variadic(f->type))
                appendstr(",...");
                appendstr(",...");
        appendstr(") called\n");
        appendstr(") called\n");
        tracefinis(printer);
        tracefinis(printer);
}
}
 
 
/* tracereturn - generate code to trace return e */
/* tracereturn - generate code to trace return e */
static void tracereturn(Symbol printer, Symbol f, Tree e) {
static void tracereturn(Symbol printer, Symbol f, Tree e) {
        appendstr(f->name); appendstr("#");
        appendstr(f->name); appendstr("#");
        tracevalue(idtree(frameno), 0);
        tracevalue(idtree(frameno), 0);
        appendstr(" returned");
        appendstr(" returned");
        if (freturn(f->type) != voidtype && e) {
        if (freturn(f->type) != voidtype && e) {
                appendstr(" ");
                appendstr(" ");
                tracevalue(e, 0);
                tracevalue(e, 0);
        }
        }
        appendstr("\n");
        appendstr("\n");
        tracefinis(printer);
        tracefinis(printer);
}
}
 
 
/* traceInit - initialize for tracing */
/* traceInit - initialize for tracing */
void traceInit(char *arg) {
void traceInit(char *arg) {
        if (strncmp(arg, "-t", 2) == 0 && strchr(arg, '=') == NULL) {
        if (strncmp(arg, "-t", 2) == 0 && strchr(arg, '=') == NULL) {
                Symbol printer = mksymbol(EXTERN, arg[2] ? &arg[2] : "printf",
                Symbol printer = mksymbol(EXTERN, arg[2] ? &arg[2] : "printf",
                        ftype(inttype, ptr(qual(CONST, chartype)), voidtype, NULL));
                        ftype(inttype, ptr(qual(CONST, chartype)), voidtype, NULL));
                printer->defined = 0;
                printer->defined = 0;
                attach((Apply)tracecall,   printer, &events.entry);
                attach((Apply)tracecall,   printer, &events.entry);
                attach((Apply)tracereturn, printer, &events.returns);
                attach((Apply)tracereturn, printer, &events.returns);
        }
        }
}
}
 
 

powered by: WebSVN 2.1.0

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