1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)functab.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * This file contains the implementation of a table for going
14  * from object addresses to the functions in which they belong.
15  */
16 
17 #include "defs.h"
18 #include "mappings.h"
19 #include "sym.h"
20 
21 #define MAXNFUNCS 1001		/* maximum number of functions allowed */
22 
23 LOCAL SYM *functab[MAXNFUNCS];
24 LOCAL int nfuncs;
25 
26 /*
27  * Insert a new function into the table.
28  * The table is ordered by object address.
29  */
30 
31 newfunc(f)
32 SYM *f;
33 {
34 	register int i, j;
35 	ADDRESS a;
36 
37 	if (nfuncs >= MAXNFUNCS) {
38 		panic("too many procedures/functions");
39 	}
40 	a = codeloc(f);
41 	i = 0;
42 	while (i < nfuncs && codeloc(functab[i]) < a) {
43 		i++;
44 	}
45 	for (j = nfuncs; j > i; j--) {
46 		functab[j] = functab[j - 1];
47 	}
48 	functab[i] = f;
49 	nfuncs++;
50 }
51 
52 /*
53  * Return the function that begins at the given address.
54  */
55 
56 SYM *whatblock(addr)
57 ADDRESS addr;
58 {
59 	register int i, j, k;
60 	ADDRESS a;
61 
62 	i = 0;
63 	j = nfuncs - 1;
64 	if (addr < codeloc(functab[i])) {
65 		return program;
66 	} else if (addr == codeloc(functab[i])) {
67 		return functab[i];
68 	} else if (addr >= codeloc(functab[j])) {
69 		return functab[j];
70 	}
71 	while (i <= j) {
72 		k = (i + j) / 2;
73 		a = codeloc(functab[k]);
74 		if (a == addr) {
75 			return functab[k];
76 		} else if (addr > a) {
77 			i = k+1;
78 		} else {
79 			j = k-1;
80 		}
81 	}
82 	if (addr > codeloc(functab[i])) {
83 		return functab[i];
84 	} else {
85 		return functab[i-1];
86 	}
87 	/* NOTREACHED */
88 }
89 
90 /*
91  * Clear out the functab, used when re-reading the object information.
92  */
93 
94 clrfunctab()
95 {
96 	nfuncs = 0;
97 }
98