github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/8l/prof.c (about)

     1  // Inferno utils/8l/obj.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/8l/obj.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  // Profiling.
    32  
    33  #include	"l.h"
    34  #include	"../ld/lib.h"
    35  
    36  void
    37  doprof1(void)
    38  {
    39  #ifdef	NOTDEF  // TODO(rsc)
    40  	Sym *s;
    41  	int32 n;
    42  	Prog *p, *q;
    43  
    44  	if(debug['v'])
    45  		Bprint(&bso, "%5.2f profile 1\n", cputime());
    46  	Bflush(&bso);
    47  	s = lookup("__mcount", 0);
    48  	n = 1;
    49  	for(p = firstp->link; p != P; p = p->link) {
    50  		if(p->as == ATEXT) {
    51  			q = prg();
    52  			q->line = p->line;
    53  			q->link = datap;
    54  			datap = q;
    55  			q->as = ADATA;
    56  			q->from.type = D_EXTERN;
    57  			q->from.offset = n*4;
    58  			q->from.sym = s;
    59  			q->from.scale = 4;
    60  			q->to = p->from;
    61  			q->to.type = D_CONST;
    62  
    63  			q = prg();
    64  			q->line = p->line;
    65  			q->pc = p->pc;
    66  			q->link = p->link;
    67  			p->link = q;
    68  			p = q;
    69  			p->as = AADDL;
    70  			p->from.type = D_CONST;
    71  			p->from.offset = 1;
    72  			p->to.type = D_EXTERN;
    73  			p->to.sym = s;
    74  			p->to.offset = n*4 + 4;
    75  
    76  			n += 2;
    77  			continue;
    78  		}
    79  	}
    80  	q = prg();
    81  	q->line = 0;
    82  	q->link = datap;
    83  	datap = q;
    84  
    85  	q->as = ADATA;
    86  	q->from.type = D_EXTERN;
    87  	q->from.sym = s;
    88  	q->from.scale = 4;
    89  	q->to.type = D_CONST;
    90  	q->to.offset = n;
    91  
    92  	s->type = SBSS;
    93  	s->size = n*4;
    94  #endif
    95  }
    96  
    97  void
    98  doprof2(void)
    99  {
   100  	Sym *s2, *s4;
   101  	Prog *p, *q, *ps2, *ps4;
   102  
   103  	if(debug['v'])
   104  		Bprint(&bso, "%5.2f profile 2\n", cputime());
   105  	Bflush(&bso);
   106  
   107  	s2 = lookup("_profin", 0);
   108  	s4 = lookup("_profout", 0);
   109  	if(s2->type != STEXT || s4->type != STEXT) {
   110  		diag("_profin/_profout not defined");
   111  		return;
   112  	}
   113  
   114  	ps2 = P;
   115  	ps4 = P;
   116  	for(cursym = textp; cursym != nil; cursym = cursym->next) {
   117  		p = cursym->text;
   118  		if(p->from.sym == s2) {
   119  			p->from.scale = 1;
   120  			ps2 = p;
   121  		}
   122  		if(p->from.sym == s4) {
   123  			p->from.scale = 1;
   124  			ps4 = p;
   125  		}
   126  	}
   127  	for(cursym = textp; cursym != nil; cursym = cursym->next) {
   128  		p = cursym->text;
   129  
   130  		if(p->from.scale & NOPROF)	/* dont profile */
   131  			continue;
   132  
   133  		/*
   134  		 * JMPL	profin
   135  		 */
   136  		q = prg();
   137  		q->line = p->line;
   138  		q->pc = p->pc;
   139  		q->link = p->link;
   140  		p->link = q;
   141  		p = q;
   142  		p->as = ACALL;
   143  		p->to.type = D_BRANCH;
   144  		p->pcond = ps2;
   145  		p->to.sym = s2;
   146  
   147  		for(; p; p=p->link) {
   148  			if(p->as == ARET) {
   149  				/*
   150  				 * RET
   151  				 */
   152  				q = prg();
   153  				q->as = ARET;
   154  				q->from = p->from;
   155  				q->to = p->to;
   156  				q->link = p->link;
   157  				p->link = q;
   158  	
   159  				/*
   160  				 * JAL	profout
   161  				 */
   162  				p->as = ACALL;
   163  				p->from = zprg.from;
   164  				p->to = zprg.to;
   165  				p->to.type = D_BRANCH;
   166  				p->pcond = ps4;
   167  				p->to.sym = s4;
   168  	
   169  				p = q;
   170  			}
   171  		}
   172  	}
   173  }