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