1/* The common simulator framework for GDB, the GNU Debugger.
2
3   Copyright 2002, 2007 Free Software Foundation, Inc.
4
5   Contributed by Andrew Cagney and Red Hat.
6
7   This file is part of GDB.
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 3 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22
23#include "hw-main.h"
24#include "hw-base.h"
25
26
27struct hw_handle_mapping {
28  cell_word external;
29  struct hw *phandle;
30  struct hw_instance *ihandle;
31  struct hw_handle_mapping *next;
32};
33
34
35struct hw_handle_data {
36  int nr_mappings;
37  struct hw_handle_mapping *mappings;
38};
39
40void
41create_hw_handle_data (struct hw *hw)
42{
43  if (hw_parent (hw) == NULL)
44    {
45      hw->handles_of_hw = HW_ZALLOC (hw, struct hw_handle_data);
46    }
47  else
48    {
49      hw->handles_of_hw = hw_root (hw)->handles_of_hw;
50    }
51}
52
53void
54delete_hw_handle_data (struct hw *hw)
55{
56  /* NULL */
57}
58
59
60
61#if 0
62void
63hw_handle_init (struct hw *hw)
64{
65  struct hw_handle_mapping *current_map = db->mappings;
66  if (current_map != NULL)
67    {
68      db->nr_mappings = db->mappings->external;
69      /* verify that the mappings that were not removed are in
70	 sequence down to nr 1 */
71      while (current_map->next != NULL)
72	{
73	  if (current_map->external != current_map->next->external + 1)
74	    error ("hw_handle: hw_handle database possibly corrupt");
75	  current_map = current_map->next;
76	}
77      ASSERT (current_map->next == NULL);
78      if (current_map->external != 1)
79	error ("hw_handle: hw_handle database possibly corrupt");
80    }
81  else
82    {
83      db->nr_mappings = 0;
84    }
85}
86#endif
87
88
89struct hw_instance *
90hw_handle_ihandle2 (struct hw *hw,
91		    cell_word external)
92{
93  struct hw_handle_data *db = hw->handles_of_hw;
94  struct hw_handle_mapping *current_map = db->mappings;
95  while (current_map != NULL)
96    {
97      if (current_map->external == external)
98	return current_map->ihandle;
99      current_map = current_map->next;
100    }
101  return (void*)0;
102}
103
104
105struct hw *
106hw_handle_phandle2 (struct hw *hw,
107		    cell_word external)
108{
109  struct hw_handle_data *db = hw->handles_of_hw;
110  struct hw_handle_mapping *current_map = db->mappings;
111  while (current_map != NULL)
112    {
113      if (current_map->external == external)
114	return current_map->phandle;
115      current_map = current_map->next;
116    }
117  return (void*)0;
118}
119
120
121cell_word
122hw_handle_2ihandle (struct hw *hw,
123		    struct hw_instance *internal)
124{
125  struct hw_handle_data *db = hw->handles_of_hw;
126  struct hw_handle_mapping *current_map = db->mappings;
127  while (current_map != NULL)
128    {
129      if (current_map->ihandle == internal)
130	return current_map->external;
131      current_map = current_map->next;
132    }
133  return 0;
134}
135
136
137cell_word
138hw_handle_2phandle (struct hw *hw,
139		    struct hw *internal)
140{
141  struct hw_handle_data *db = hw->handles_of_hw;
142  struct hw_handle_mapping *current_map = db->mappings;
143  while (current_map != NULL)
144    {
145      if (current_map->phandle == internal)
146	return current_map->external;
147      current_map = current_map->next;
148    }
149  return 0;
150}
151
152
153void
154hw_handle_add_ihandle (struct hw *hw,
155		       struct hw_instance *internal)
156{
157  struct hw_handle_data *db = hw->handles_of_hw;
158  if (hw_handle_2ihandle (hw, internal) != 0)
159    {
160      hw_abort (hw, "attempting to add an ihandle already in the data base");
161    }
162  else
163    {
164      /* insert at the front making things in decending order */
165      struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
166      new_map->next = db->mappings;
167      new_map->ihandle = internal;
168      db->nr_mappings += 1;
169      new_map->external = db->nr_mappings;
170      db->mappings = new_map;
171    }
172}
173
174
175void
176hw_handle_add_phandle (struct hw *hw,
177		       struct hw *internal)
178{
179  struct hw_handle_data *db = hw->handles_of_hw;
180  if (hw_handle_2phandle (hw, internal) != 0)
181    {
182      hw_abort (hw, "attempting to add a phandle already in the data base");
183    }
184  else
185    {
186      /* insert at the front making things in decending order */
187      struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
188      new_map->next = db->mappings;
189      new_map->phandle = internal;
190      db->nr_mappings += 1;
191      new_map->external = db->nr_mappings;
192      db->mappings = new_map;
193    }
194}
195
196
197void
198hw_handle_remove_ihandle (struct hw *hw,
199			  struct hw_instance *internal)
200{
201  struct hw_handle_data *db = hw->handles_of_hw;
202  struct hw_handle_mapping **current_map = &db->mappings;
203  while (*current_map != NULL)
204    {
205      if ((*current_map)->ihandle == internal)
206	{
207	  struct hw_handle_mapping *delete = *current_map;
208	  *current_map = delete->next;
209	  zfree (delete);
210	  return;
211	}
212      current_map = &(*current_map)->next;
213    }
214  hw_abort (hw, "attempt to remove nonexistant ihandle");
215}
216
217
218void
219hw_handle_remove_phandle (struct hw *hw,
220			  struct hw *internal)
221{
222  struct hw_handle_data *db = hw->handles_of_hw;
223  struct hw_handle_mapping **current_map = &db->mappings;
224  while (*current_map != NULL)
225    {
226      if ((*current_map)->phandle == internal)
227	{
228	  struct hw_handle_mapping *delete = *current_map;
229	  *current_map = delete->next;
230	  zfree (delete);
231	  return;
232	}
233      current_map = &(*current_map)->next;
234    }
235  hw_abort (hw, "attempt to remove nonexistant phandle");
236}
237
238
239