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