Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / usr.bin / pascal / pdx / mappings / functab.c
CommitLineData
505bf312
KB
1/*-
2 * Copyright (c) 1980 The Regents of the University of California.
3 * All rights reserved.
4 *
c0567266
KB
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
f644bb55 32 */
ff1b71cf 33
f644bb55 34#ifndef lint
af359dea 35static char sccsid[] = "@(#)functab.c 5.3 (Berkeley) 4/16/91";
505bf312
KB
36#endif /* not lint */
37
ff1b71cf
ML
38/*
39 * This file contains the implementation of a table for going
40 * from object addresses to the functions in which they belong.
41 */
42
43#include "defs.h"
44#include "mappings.h"
45#include "sym.h"
46
47#define MAXNFUNCS 1001 /* maximum number of functions allowed */
48
49LOCAL SYM *functab[MAXNFUNCS];
50LOCAL int nfuncs;
51
52/*
53 * Insert a new function into the table.
54 * The table is ordered by object address.
55 */
56
57newfunc(f)
58SYM *f;
59{
60 register int i, j;
61 ADDRESS a;
62
63 if (nfuncs >= MAXNFUNCS) {
64 panic("too many procedures/functions");
65 }
66 a = codeloc(f);
67 i = 0;
68 while (i < nfuncs && codeloc(functab[i]) < a) {
69 i++;
70 }
71 for (j = nfuncs; j > i; j--) {
72 functab[j] = functab[j - 1];
73 }
74 functab[i] = f;
75 nfuncs++;
76}
77
78/*
79 * Return the function that begins at the given address.
80 */
81
82SYM *whatblock(addr)
83ADDRESS addr;
84{
ff1b71cf
ML
85 register int i, j, k;
86 ADDRESS a;
87
88 i = 0;
89 j = nfuncs - 1;
90 if (addr < codeloc(functab[i])) {
91 return program;
92 } else if (addr == codeloc(functab[i])) {
93 return functab[i];
94 } else if (addr >= codeloc(functab[j])) {
95 return functab[j];
96 }
97 while (i <= j) {
98 k = (i + j) / 2;
99 a = codeloc(functab[k]);
100 if (a == addr) {
101 return functab[k];
102 } else if (addr > a) {
103 i = k+1;
104 } else {
105 j = k-1;
106 }
107 }
108 if (addr > codeloc(functab[i])) {
109 return functab[i];
110 } else {
111 return functab[i-1];
112 }
113 /* NOTREACHED */
114}
115
116/*
117 * Clear out the functab, used when re-reading the object information.
118 */
119
120clrfunctab()
121{
122 nfuncs = 0;
123}