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

     1  // Inferno utils/6l/obj.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/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
    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(cursym = textp; cursym != nil; cursym = cursym->next) {
    50  		p = cursym->text;
    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  	}
    78  	q = prg();
    79  	q->line = 0;
    80  	q->link = datap;
    81  	datap = q;
    82  
    83  	q->as = ADATA;
    84  	q->from.type = D_EXTERN;
    85  	q->from.sym = s;
    86  	q->from.scale = 4;
    87  	q->to.type = D_CONST;
    88  	q->to.offset = n;
    89  
    90  	s->type = SBSS;
    91  	s->size = n*4;
    92  #endif
    93  }
    94  
    95  void
    96  doprof2(void)
    97  {
    98  	Sym *s2, *s4;
    99  	Prog *p, *q, *ps2, *ps4;
   100  
   101  	if(debug['v'])
   102  		Bprint(&bso, "%5.2f profile 2\n", cputime());
   103  	Bflush(&bso);
   104  
   105  	s2 = lookup("_profin", 0);
   106  	s4 = lookup("_profout", 0);
   107  	if(s2->type != STEXT || s4->type != STEXT) {
   108  		diag("_profin/_profout not defined");
   109  		return;
   110  	}
   111  
   112  	ps2 = P;
   113  	ps4 = P;
   114  	for(cursym = textp; cursym != nil; cursym = cursym->next) {
   115  		p = cursym->text;
   116  		if(p->from.sym == s2) {
   117  			p->from.scale = 1;
   118  			ps2 = p;
   119  		}
   120  		if(p->from.sym == s4) {
   121  			p->from.scale = 1;
   122  			ps4 = p;
   123  		}
   124  	}
   125  	for(cursym = textp; cursym != nil; cursym = cursym->next) {
   126  		p = cursym->text;
   127  
   128  		if(p->from.scale & NOPROF)	/* dont profile */
   129  			continue;
   130  
   131  		/*
   132  		 * JMPL	profin
   133  		 */
   134  		q = prg();
   135  		q->line = p->line;
   136  		q->pc = p->pc;
   137  		q->link = p->link;
   138  		p->link = q;
   139  		p = q;
   140  		p->as = ACALL;
   141  		p->to.type = D_BRANCH;
   142  		p->pcond = ps2;
   143  		p->to.sym = s2;
   144  
   145  		for(; p; p=p->link) {
   146  			if(p->as == ARET) {
   147  				/*
   148  				 * RET
   149  				 */
   150  				q = prg();
   151  				q->as = ARET;
   152  				q->from = p->from;
   153  				q->to = p->to;
   154  				q->link = p->link;
   155  				p->link = q;
   156  	
   157  				/*
   158  				 * JAL	profout
   159  				 */
   160  				p->as = ACALL;
   161  				p->from = zprg.from;
   162  				p->to = zprg.to;
   163  				p->to.type = D_BRANCH;
   164  				p->pcond = ps4;
   165  				p->to.sym = s4;
   166  	
   167  				p = q;
   168  			}
   169  		}
   170  	}
   171  }