Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / libxref / xref_support.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: xref_support.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * @(#)xref_support.c 1.2 03/08/20
46 * Copyright 2001-2003 Sun Microsystems, Inc. All Rights Reserved
47 * Use is subject to license terms.
48 */
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <string.h>
53
54#include "xref_support.h"
55
56#define ASSERT(x, y) \
57if (x); else fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, y)
58
59/*
60 * The public statefull part of the cross referencer lives here
61 */
62static xref_state_t *xref_state;
63static int xref_inited = 0;
64static char *xref_file = NULL;
65static int xref_level = 0;
66
67void
68push_file_reference(xref_file_t *ref)
69{
70 ASSERT((ref != NULL), "Pushed a NULL file??\n");
71 ref->prev_file = xref_state->current_file;
72 xref_state->current_file = ref;
73}
74
75void
76pop_file_reference(xref_file_t *ref)
77{
78 ASSERT((ref != NULL), "Popped a NULL file??\n");
79 xref_state->current_file = ref->prev_file;
80 ref->prev_file = NULL;
81}
82
83/*
84 * We just touched a new file, so we need to create the reference structs for
85 * it
86 */
87static void
88add_file_reference(char *name)
89{
90 xref_file_t *newf;
91 char *nfile;
92
93 newf = xref_create_file_reference(xref_extract_pathname(name));
94 newf->next = xref_state->sources;
95 xref_state->sources = newf;
96 push_file_reference(newf);
97}
98
99void
100xref_init(char *filename, char *preload, int fwd)
101{
102 xref_file_t *newf;
103 if (!xref_inited) {
104 if (strcmp(filename, "-") == 0) {
105 xref_file = NULL;
106 } else {
107 xref_file = strdup(filename);
108 }
109 if (preload == NULL) {
110 xref_state = xref_create_state();
111 if (fwd) {
112 xref_state->flags |= XREF_STATE_FWD_DEF;
113 }
114 add_file_reference("Unknown-default");
115 } else {
116 xref_state = xref_load_file(preload, 0);
117 }
118 }
119 xref_inited = 1;
120}
121
122void
123xref_generate(int force)
124{
125 static int xref_done = 0;
126
127 if (!force && (xref_done++))
128 return;
129
130 if (!xref_inited)
131 return;
132
133 xref_save_file(xref_file, xref_state);
134}
135
136void
137xref_add_file_reference(char *name)
138{
139 if (!xref_inited)
140 return;
141
142 add_file_reference(name);
143}
144
145void
146xref_remove_file_reference(void)
147{
148 if (!xref_inited)
149 return;
150
151 pop_file_reference(xref_state->current_file);
152}
153
154void
155xref_add_symbol_reference(char *name, int line)
156{
157 xref_t *xref, *prev;
158 xref_t *fn = xref_state->current_def;
159
160 if (xref_state->current_def == NULL)
161 return;
162
163 xref = xref_find_symbol(xref_state->all_refs, name, &prev,
164 (XREF_HIDDEN | XREF_STRING));
165 if (xref == NULL) {
166 if (xref_state->flags & XREF_STATE_FWD_DEF) {
167 xref = xref_create_reference(name, line, xref_state);
168 xref->flags |= XREF_FORWARD;
169 xref->flags |= XREF_DEFINITION;
170 } else {
171 return;
172 }
173 }
174 xref_add_reference_to_buffer(&fn->calls, xref);
175 xref_add_reference_to_buffer(&xref->called_by, fn);
176}
177
178void
179xref_add_symbol_definition(char *name, int line)
180{
181 xref_t *xref, *prev;
182
183 xref = xref_find_symbol(xref_state->all_refs, name, &prev,
184 (XREF_HIDDEN | XREF_STRING));
185
186 if ((xref != NULL) &&
187 (xref->linenum == line) &&
188 (xref_state->current_file == xref->file)) {
189 /*
190 * the metacompiler causes multiple 'definition' hits
191 * so we ignore the repetitiions.
192 */
193#if 0
194 printf("%s:%d:Still required?\n",
195 xref_state->current_file->name, line);
196#endif
197#if 1
198 return;
199#endif
200 }
201 xref_state->current_def = xref_create_reference(name,
202 line, xref_state);
203 xref_state->current_def->flags |= XREF_DEFINITION;
204}
205
206void
207xref_modify_symbol_definition(char *name, int reveal)
208{
209 xref_t *xref, *prev;
210
211 /* this can happen because of bootstrapping!! */
212 if (name == NULL)
213 return;
214
215 xref = xref_find_symbol(xref_state->all_refs, name, &prev,
216 XREF_STRING);
217
218 if (xref == NULL) {
219 fprintf(stderr, "%s called for %s (that did not exist)!\n",
220 (reveal ? "reveal" : "hide"), name);
221 return;
222 }
223 if (reveal)
224 xref->flags &= ~XREF_HIDDEN;
225 else
226 xref->flags |= XREF_HIDDEN;
227}
228
229/*
230 * now the fun starts.. we need to break the string up into words
231 * throwing away extra spaces and control chars and add each word to the
232 * index, tagging each word with the XREF_STRING marker.
233 */
234void
235xref_add_string(char *string, int len, int line)
236{
237 int index;
238 char word[256];
239 int wordlen;
240 xref_t *xref;
241
242 index = 0;
243 wordlen = 0;
244 while (index < len) {
245 char c;
246
247 c = string[index];
248 if ((c <= ' ') || (c >= 127)) {
249 if (wordlen) {
250 word[wordlen] = 0;
251 wordlen = 0;
252 xref = xref_create_reference(word, line,
253 xref_state);
254 xref->flags |= XREF_STRING;
255 }
256 index++;
257 continue;
258 }
259 word[wordlen++] = c;
260 index++;
261 }
262 if (wordlen) {
263 word[wordlen] = 0;
264 xref = xref_create_reference(word, line, xref_state);
265 xref->flags |= XREF_STRING;
266 }
267}
268
269void
270xref_status(void)
271{
272 printf("Current File: '%s'\n",
273 (xref_state->current_file == NULL)?
274 "<NULL>" : xref_state->current_file->name);
275 printf("Last Definition: '%s' (%d)\n",
276 ((xref_state->current_def == NULL)?
277 "<NULL>" : xref_state->current_def->name),
278 ((xref_state->current_def == NULL)?
279 0 : xref_state->current_def->called_by.insert));
280}