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