Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / libxref / xref.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: xref.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.c 1.1 02/05/02
46 * Copyright 2001-2002 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.h"
55
56xref_state_t *
57xref_create_state(void)
58{
59 xref_state_t *new;
60 new = malloc(sizeof (xref_state_t));
61 memset(new, 0, sizeof (xref_state_t));
62 return (new);
63}
64
65xref_file_t *
66xref_create_file_reference(char *name)
67{
68 size_t bytes = 128 * sizeof (xref_t *);
69 xref_file_t *newf;
70
71 newf = malloc(sizeof (xref_file_t));
72 newf->name = strdup(name);
73 newf->defs = malloc(bytes);
74 memset(newf->defs, 0, bytes);
75 return (newf);
76}
77
78void
79xref_add_definition(xref_t **where, xref_t *new)
80{
81 unsigned int index;
82 index = new->name[0];
83
84 new->next_def = where[index];
85 where[index] = new;
86}
87
88/*
89 * when a new routine is defined call this..
90 */
91xref_t *
92xref_create_reference(char *name, int line, xref_state_t *state)
93{
94 xref_t *new;
95
96 new = malloc(sizeof (xref_t));
97 new->name = strdup(name);
98 new->file = state->current_file;
99 new->linenum = line;
100 new->flags = 0;
101 new->calls.size = MIN_REF;
102 new->calls.insert = 0;
103 new->calls.ptr = malloc(sizeof (xref_t *) * new->calls.size);
104 new->called_by.size = MIN_REF;
105 new->called_by.insert = 0;
106 new->called_by.ptr = malloc(sizeof (xref_t *) * new->called_by.size);
107 new->next_def = NULL;
108
109 if (state->current_file == NULL) {
110 fprintf(stderr, "%s:%d: Current file == NULL!!\n",
111 __FILE__, __LINE__);
112 }
113 xref_add_definition(state->current_file->defs, new);
114 xref_add_definition(state->all_refs, new);
115 return (new);
116}
117
118void
119xref_add_reference_to_buffer(ref_t *where, xref_t *fn)
120{
121 xref_t **newbuf, **oldbuf;
122
123 if (where->insert < where->size) {
124 where->ptr[where->insert++] = fn;
125 } else {
126 int bytes;
127
128 /*
129 * we need to grow the reference buffer.
130 */
131 bytes = (where->size + MIN_REF) * sizeof (xref_t *);
132 newbuf = malloc(bytes);
133 memset(newbuf, 0, bytes);
134 oldbuf = where->ptr;
135 memcpy(newbuf, oldbuf, (where->size*sizeof (xref_t *)));
136 free(oldbuf);
137 where->ptr = newbuf;
138 where->size += MIN_REF;
139 where->ptr[where->insert++] = fn;
140 }
141}
142
143void
144xref_free_state(xref_state_t *state)
145{
146}