xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/hw-handles.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002-2024 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 /* This must come before any other includes.  */
23 #include "defs.h"
24 
25 #include <stdlib.h>
26 
27 #include "hw-main.h"
28 #include "hw-base.h"
29 
30 struct hw_handle_mapping
31 {
32   cell_word external;
33   struct hw *phandle;
34   struct hw_instance *ihandle;
35   struct hw_handle_mapping *next;
36 };
37 
38 
39 struct hw_handle_data
40 {
41   int nr_mappings;
42   struct hw_handle_mapping *mappings;
43 };
44 
45 void
46 create_hw_handle_data (struct hw *hw)
47 {
48   if (hw_parent (hw) == NULL)
49     {
50       hw->handles_of_hw = HW_ZALLOC (hw, struct hw_handle_data);
51     }
52   else
53     {
54       hw->handles_of_hw = hw_root (hw)->handles_of_hw;
55     }
56 }
57 
58 void
59 delete_hw_handle_data (struct hw *hw)
60 {
61   /* NULL */
62 }
63 
64 
65 
66 #if 0
67 void
68 hw_handle_init (struct hw *hw)
69 {
70   struct hw_handle_mapping *current_map = db->mappings;
71   if (current_map != NULL)
72     {
73       db->nr_mappings = db->mappings->external;
74       /* verify that the mappings that were not removed are in
75 	 sequence down to nr 1 */
76       while (current_map->next != NULL)
77 	{
78 	  if (current_map->external != current_map->next->external + 1)
79 	    error ("hw_handle: hw_handle database possibly corrupt");
80 	  current_map = current_map->next;
81 	}
82       ASSERT (current_map->next == NULL);
83       if (current_map->external != 1)
84 	error ("hw_handle: hw_handle database possibly corrupt");
85     }
86   else
87     {
88       db->nr_mappings = 0;
89     }
90 }
91 #endif
92 
93 
94 struct hw_instance *
95 hw_handle_ihandle2 (struct hw *hw,
96 		    cell_word external)
97 {
98   struct hw_handle_data *db = hw->handles_of_hw;
99   struct hw_handle_mapping *current_map = db->mappings;
100   while (current_map != NULL)
101     {
102       if (current_map->external == external)
103 	return current_map->ihandle;
104       current_map = current_map->next;
105     }
106   return (void*)0;
107 }
108 
109 
110 struct hw *
111 hw_handle_phandle2 (struct hw *hw,
112 		    cell_word external)
113 {
114   struct hw_handle_data *db = hw->handles_of_hw;
115   struct hw_handle_mapping *current_map = db->mappings;
116   while (current_map != NULL)
117     {
118       if (current_map->external == external)
119 	return current_map->phandle;
120       current_map = current_map->next;
121     }
122   return (void*)0;
123 }
124 
125 
126 cell_word
127 hw_handle_2ihandle (struct hw *hw,
128 		    struct hw_instance *internal)
129 {
130   struct hw_handle_data *db = hw->handles_of_hw;
131   struct hw_handle_mapping *current_map = db->mappings;
132   while (current_map != NULL)
133     {
134       if (current_map->ihandle == internal)
135 	return current_map->external;
136       current_map = current_map->next;
137     }
138   return 0;
139 }
140 
141 
142 cell_word
143 hw_handle_2phandle (struct hw *hw,
144 		    struct hw *internal)
145 {
146   struct hw_handle_data *db = hw->handles_of_hw;
147   struct hw_handle_mapping *current_map = db->mappings;
148   while (current_map != NULL)
149     {
150       if (current_map->phandle == internal)
151 	return current_map->external;
152       current_map = current_map->next;
153     }
154   return 0;
155 }
156 
157 
158 void
159 hw_handle_add_ihandle (struct hw *hw,
160 		       struct hw_instance *internal)
161 {
162   struct hw_handle_data *db = hw->handles_of_hw;
163   if (hw_handle_2ihandle (hw, internal) != 0)
164     {
165       hw_abort (hw, "attempting to add an ihandle already in the data base");
166     }
167   else
168     {
169       /* insert at the front making things in decending order */
170       struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
171       new_map->next = db->mappings;
172       new_map->ihandle = internal;
173       db->nr_mappings += 1;
174       new_map->external = db->nr_mappings;
175       db->mappings = new_map;
176     }
177 }
178 
179 
180 void
181 hw_handle_add_phandle (struct hw *hw,
182 		       struct hw *internal)
183 {
184   struct hw_handle_data *db = hw->handles_of_hw;
185   if (hw_handle_2phandle (hw, internal) != 0)
186     {
187       hw_abort (hw, "attempting to add a phandle already in the data base");
188     }
189   else
190     {
191       /* insert at the front making things in decending order */
192       struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
193       new_map->next = db->mappings;
194       new_map->phandle = internal;
195       db->nr_mappings += 1;
196       new_map->external = db->nr_mappings;
197       db->mappings = new_map;
198     }
199 }
200 
201 
202 void
203 hw_handle_remove_ihandle (struct hw *hw,
204 			  struct hw_instance *internal)
205 {
206   struct hw_handle_data *db = hw->handles_of_hw;
207   struct hw_handle_mapping **current_map = &db->mappings;
208   while (*current_map != NULL)
209     {
210       if ((*current_map)->ihandle == internal)
211 	{
212 	  struct hw_handle_mapping *delete = *current_map;
213 	  *current_map = delete->next;
214 	  free (delete);
215 	  return;
216 	}
217       current_map = &(*current_map)->next;
218     }
219   hw_abort (hw, "attempt to remove nonexistant ihandle");
220 }
221 
222 
223 void
224 hw_handle_remove_phandle (struct hw *hw,
225 			  struct hw *internal)
226 {
227   struct hw_handle_data *db = hw->handles_of_hw;
228   struct hw_handle_mapping **current_map = &db->mappings;
229   while (*current_map != NULL)
230     {
231       if ((*current_map)->phandle == internal)
232 	{
233 	  struct hw_handle_mapping *delete = *current_map;
234 	  *current_map = delete->next;
235 	  free (delete);
236 	  return;
237 	}
238       current_map = &(*current_map)->next;
239     }
240   hw_abort (hw, "attempt to remove nonexistant phandle");
241 }
242