home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / src / trace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-11  |  4.7 KB  |  178 lines

  1. #include "c.h"
  2.  
  3.  
  4.  
  5. static void appendstr ARGS((char *));
  6.  
  7. static void tracecall ARGS((Symbol, Symbol));
  8.  
  9. static void tracefinis ARGS((Symbol));
  10.  
  11. static void tracereturn ARGS((Symbol, Symbol, Tree));
  12.  
  13. static void tracevalue ARGS((Tree, int));
  14.  
  15.  
  16.  
  17. static char *fmt, *fp, *fmtend;    /* format string, current & limit pointer */
  18.  
  19. static Tree args;        /* printf arguments */
  20.  
  21. static Symbol frameno;        /* local holding frame number */
  22.  
  23.  
  24.  
  25. /* appendstr - append str to the evolving format string, expanding it if necessary */
  26.  
  27. static void appendstr(char *str)
  28.  
  29. {
  30.  
  31.     do
  32.  
  33.         if (fp == fmtend)
  34.  
  35.             if (fp) {
  36.  
  37.                 char *s = allocate(2*(fmtend - fmt), FUNC);
  38.  
  39.                 strncpy(s, fmt, fmtend - fmt);
  40.  
  41.                 fp = s + (fmtend - fmt);
  42.  
  43.                 fmtend = s + 2*(fmtend - fmt);
  44.  
  45.                 fmt = s;
  46.  
  47.             } else {
  48.  
  49.                 fp = fmt = allocate(80, FUNC);
  50.  
  51.                 fmtend = fmt + 80;
  52.  
  53.             }
  54.  
  55.     while ((*fp++ = *str++) != 0);
  56.  
  57.     fp--;
  58.  
  59. }
  60.  
  61.  
  62.  
  63. /* tracecall - generate code to trace entry to f */
  64.  
  65. static void tracecall(Symbol printer,Symbol f)
  66.  
  67. {
  68.  
  69.     int i;
  70.  
  71.     Symbol counter = genident(STATIC, inttype, GLOBAL);
  72.  
  73.  
  74.  
  75.     defglobal(counter, BSS);
  76.  
  77.     (*IR->space)(counter->type->size);
  78.  
  79.     frameno = genident(AUTO, inttype, level);
  80.  
  81.     addlocal(frameno);
  82.  
  83.     appendstr(f->name); appendstr("#");
  84.  
  85.     tracevalue(asgn(frameno, incr(INCR, idtree(counter), consttree(1, inttype))), 0);
  86.  
  87.     appendstr("(");
  88.  
  89.     for (i = 0; f->u.f.callee[i]; i++) {
  90.  
  91.         if (i)
  92.  
  93.             appendstr(",");
  94.  
  95.         appendstr(f->u.f.callee[i]->name); appendstr("=");
  96.  
  97.         tracevalue(idtree(f->u.f.callee[i]), 0);
  98.  
  99.     }
  100.  
  101.     if (variadic(f->type))
  102.  
  103.         appendstr(",...");
  104.  
  105.     appendstr(") called\n");
  106.  
  107.     tracefinis(printer);
  108.  
  109. }
  110.  
  111.  
  112.  
  113. /* tracefinis - complete & generate the trace call to print */
  114.  
  115. static void tracefinis(Symbol printer)
  116.  
  117. {
  118.  
  119.     Tree *ap;
  120.  
  121.     Symbol p;
  122.  
  123.  
  124.  
  125.     *fp = 0;
  126.  
  127.     p = mkstr(string(fmt));
  128.  
  129.     for (ap = &args; *ap; ap = &(*ap)->kids[1])
  130.  
  131.         ;
  132.  
  133.     *ap = tree(ARG+P, ptr(chartype), pointer(idtree(p->u.c.loc)), 0);
  134.  
  135.     walk(calltree(pointer(idtree(printer)), freturn(printer->type), args, NULL), 0, 0);
  136.  
  137.     args = 0;
  138.  
  139.     fp = fmtend = 0;
  140.  
  141. }
  142.  
  143.  
  144.  
  145. /* traceinit - initialize for tracing */
  146.  
  147. void traceInit(char *print)
  148.  
  149. {
  150.  
  151.     static Symbol printer;
  152.  
  153.  
  154.  
  155.     if (!printer) {
  156.  
  157.         printer = mksymbol(EXTERN, print && *print ? print : "printf",
  158.  
  159.             ftype(inttype, ptr(qual(CONST, chartype))));
  160.  
  161.         printer->defined = 0;
  162.  
  163.         attach((Apply)tracecall,   printer, &events.entry);
  164.  
  165.         attach((Apply)tracereturn, printer, &events.returns);
  166.  
  167.     }
  168.  
  169. }
  170.  
  171.  
  172.  
  173. /* tracereturn - generate code to trace return e */
  174.  
  175. static void tracereturn(Symbol printer,Symbol f,Tree e)
  176.  
  177. {
  178.  
  179.     appendstr(f->name); appendstr("#");
  180.  
  181.     tracevalue(idtree(frameno), 0);
  182.  
  183.     appendstr(" returned");
  184.  
  185.     if (freturn(f->type) != voidtype && e) {
  186.  
  187.         appendstr(" ");
  188.  
  189.         tracevalue(e, 0);
  190.  
  191.     }
  192.  
  193.     appendstr("\n");
  194.  
  195.     tracefinis(printer);
  196.  
  197. }
  198.  
  199.  
  200.  
  201. /* tracevalue - append format and argument to print the value of e */
  202.  
  203. static void tracevalue(Tree e,int lev)
  204.  
  205. {
  206.  
  207.     Type ty = unqual(e->type);
  208.  
  209.  
  210.  
  211.     switch (ty->op) {
  212.  
  213.     case CHAR:
  214.  
  215.         appendstr("'\\x%02x'");
  216.  
  217.         break;
  218.  
  219.     case SHORT:
  220.  
  221.         if (ty == unsignedshort)
  222.  
  223.             appendstr("0x%x");
  224.  
  225.         else /* fall thru */
  226.  
  227.     case INT:
  228.  
  229.             appendstr("%d");
  230.  
  231.         break;
  232.  
  233.     case UNSIGNED:
  234.  
  235.         appendstr("0x%x");
  236.  
  237.         break;
  238.  
  239.     case FLOAT: case DOUBLE:
  240.  
  241.         appendstr("%g");
  242.  
  243.         break;
  244.  
  245.     case POINTER:
  246.  
  247.         if (unqual(ty->type) == chartype) {
  248.  
  249.             static Symbol null;
  250.  
  251.             if (null == NULL)
  252.  
  253.                 null = mkstr("(null)");
  254.  
  255.             tracevalue(cast(e, unsignedtype), lev + 1);
  256.  
  257.             appendstr(" \"%.30s\"");
  258.  
  259.             e = condtree(e, e, pointer(idtree(null->u.c.loc)));
  260.  
  261.         } else {
  262.  
  263.             appendstr("("); appendstr(typestring(ty, "")); appendstr(")0x%x");
  264.  
  265.         }
  266.  
  267.         break;
  268.  
  269.     case STRUCT: {
  270.  
  271.         Field q;
  272.  
  273.         appendstr("("); appendstr(typestring(ty, "")); appendstr("){");
  274.  
  275.         for (q = ty->u.sym->u.s.flist; q; q = q->link) {
  276.  
  277.             appendstr(q->name); appendstr("=");
  278.  
  279.             tracevalue(field(addrof(e), q->name), lev + 1);
  280.  
  281.             if (q->link)
  282.  
  283.                 appendstr(",");
  284.  
  285.         }
  286.  
  287.         appendstr("}");
  288.  
  289.         return;
  290.  
  291.         }
  292.  
  293.     case UNION:
  294.  
  295.         appendstr("("); appendstr(typestring(ty, "")); appendstr("){...}");
  296.  
  297.         return;
  298.  
  299.     case ARRAY:
  300.  
  301.         if (lev && ty->type->size > 0) {
  302.  
  303.             int i;
  304.  
  305.             e = pointer(e);
  306.  
  307.             appendstr("{");
  308.  
  309.             for (i = 0; i < ty->size/ty->type->size; i++) {
  310.  
  311.                 Tree p = (*optree['+'])(ADD, e, consttree(i, inttype));
  312.  
  313.                 if (isptr(p->type) && isarray(p->type->type))
  314.  
  315.                     p = retype(p, p->type->type);
  316.  
  317.                 else
  318.  
  319.                     p = rvalue(p);
  320.  
  321.                 if (i)
  322.  
  323.                     appendstr(",");
  324.  
  325.                 tracevalue(p, lev + 1);
  326.  
  327.             }
  328.  
  329.             appendstr("}");
  330.  
  331.         } else
  332.  
  333.             appendstr(typestring(ty, ""));
  334.  
  335.         return;
  336.  
  337.     default:
  338.  
  339.         assert(0);
  340.  
  341.     }
  342.  
  343.     if (ty == floattype)
  344.  
  345.         e = cast(e, doubletype);
  346.  
  347.     else if ((isint(ty) || isenum(ty)) && ty->size != inttype->size)
  348.  
  349.         e = cast(e, promote(ty));
  350.  
  351.     args = tree(ARG + widen(e->type), e->type, e, args);
  352.  
  353. }
  354.  
  355.