148099Sbostic /*-
2*62138Sbostic  * Copyright (c) 1980, 1993
3*62138Sbostic  *	The Regents of the University of California.  All rights reserved.
448099Sbostic  *
548099Sbostic  * %sccs.include.redist.c%
622503Sdist  */
75492Slinton 
822503Sdist #ifndef lint
9*62138Sbostic static char sccsid[] = "@(#)functab.c	8.1 (Berkeley) 06/06/93";
1048099Sbostic #endif /* not lint */
1148099Sbostic 
125492Slinton /*
135492Slinton  * This file contains the implementation of a table for going
145492Slinton  * from object addresses to the functions in which they belong.
155492Slinton  */
165492Slinton 
175492Slinton #include "defs.h"
185492Slinton #include "mappings.h"
195492Slinton #include "sym.h"
205492Slinton 
215492Slinton #define MAXNFUNCS 1001		/* maximum number of functions allowed */
225492Slinton 
235492Slinton LOCAL SYM *functab[MAXNFUNCS];
245492Slinton LOCAL int nfuncs;
255492Slinton 
265492Slinton /*
275492Slinton  * Insert a new function into the table.
285492Slinton  * The table is ordered by object address.
295492Slinton  */
305492Slinton 
newfunc(f)315492Slinton newfunc(f)
325492Slinton SYM *f;
335492Slinton {
345492Slinton 	register int i, j;
355492Slinton 	ADDRESS a;
365492Slinton 
375492Slinton 	if (nfuncs >= MAXNFUNCS) {
385492Slinton 		panic("too many procedures/functions");
395492Slinton 	}
405492Slinton 	a = codeloc(f);
415492Slinton 	i = 0;
425492Slinton 	while (i < nfuncs && codeloc(functab[i]) < a) {
435492Slinton 		i++;
445492Slinton 	}
455492Slinton 	for (j = nfuncs; j > i; j--) {
465492Slinton 		functab[j] = functab[j - 1];
475492Slinton 	}
485492Slinton 	functab[i] = f;
495492Slinton 	nfuncs++;
505492Slinton }
515492Slinton 
525492Slinton /*
535492Slinton  * Return the function that begins at the given address.
545492Slinton  */
555492Slinton 
whatblock(addr)565492Slinton SYM *whatblock(addr)
575492Slinton ADDRESS addr;
585492Slinton {
595492Slinton 	register int i, j, k;
605492Slinton 	ADDRESS a;
615492Slinton 
625492Slinton 	i = 0;
635492Slinton 	j = nfuncs - 1;
645492Slinton 	if (addr < codeloc(functab[i])) {
655492Slinton 		return program;
665492Slinton 	} else if (addr == codeloc(functab[i])) {
675492Slinton 		return functab[i];
685492Slinton 	} else if (addr >= codeloc(functab[j])) {
695492Slinton 		return functab[j];
705492Slinton 	}
715492Slinton 	while (i <= j) {
725492Slinton 		k = (i + j) / 2;
735492Slinton 		a = codeloc(functab[k]);
745492Slinton 		if (a == addr) {
755492Slinton 			return functab[k];
765492Slinton 		} else if (addr > a) {
775492Slinton 			i = k+1;
785492Slinton 		} else {
795492Slinton 			j = k-1;
805492Slinton 		}
815492Slinton 	}
825492Slinton 	if (addr > codeloc(functab[i])) {
835492Slinton 		return functab[i];
845492Slinton 	} else {
855492Slinton 		return functab[i-1];
865492Slinton 	}
875492Slinton 	/* NOTREACHED */
885492Slinton }
895492Slinton 
905492Slinton /*
915492Slinton  * Clear out the functab, used when re-reading the object information.
925492Slinton  */
935492Slinton 
clrfunctab()945492Slinton clrfunctab()
955492Slinton {
965492Slinton 	nfuncs = 0;
975492Slinton }
98