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.1 (Berkeley) 06/06/85";
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 SYM *p;
58 	register int i, j, k;
59 	ADDRESS a;
60 
61 	i = 0;
62 	j = nfuncs - 1;
63 	if (addr < codeloc(functab[i])) {
64 		return program;
65 	} else if (addr == codeloc(functab[i])) {
66 		return functab[i];
67 	} else if (addr >= codeloc(functab[j])) {
68 		return functab[j];
69 	}
70 	while (i <= j) {
71 		k = (i + j) / 2;
72 		a = codeloc(functab[k]);
73 		if (a == addr) {
74 			return functab[k];
75 		} else if (addr > a) {
76 			i = k+1;
77 		} else {
78 			j = k-1;
79 		}
80 	}
81 	if (addr > codeloc(functab[i])) {
82 		return functab[i];
83 	} else {
84 		return functab[i-1];
85 	}
86 	/* NOTREACHED */
87 }
88 
89 /*
90  * Clear out the functab, used when re-reading the object information.
91  */
92 
93 clrfunctab()
94 {
95 	nfuncs = 0;
96 }
97