1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2018 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
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.
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.
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/>. */
27 #include "sim-assert.h"
29 struct hw_instance_data
31 hw_finish_instance_method *to_finish;
32 struct hw_instance *instances;
35 static hw_finish_instance_method abort_hw_finish_instance;
38 create_hw_instance_data (struct hw *me)
40 me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
41 set_hw_finish_instance (me, abort_hw_finish_instance);
45 delete_hw_instance_data (struct hw *me)
52 abort_hw_finish_instance (struct hw *hw,
53 struct hw_instance *instance)
55 hw_abort (hw, "no instance finish method");
59 set_hw_finish_instance (struct hw *me,
60 hw_finish_instance_method *finish)
62 me->instances_of_hw->to_finish = finish;
68 clean_hw_instances (struct hw *me)
70 struct hw_instance **instance = &me->instances;
71 while (*instance != NULL)
73 struct hw_instance *old_instance = *instance;
74 hw_instance_delete (old_instance);
75 instance = &me->instances;
82 hw_instance_delete (struct hw_instance *instance)
85 hw_abort (hw_instance_hw (instance), "not implemented");
87 struct hw *me = hw_instance_hw (instance);
88 if (instance->to_instance_delete == NULL)
89 hw_abort (me, "no delete method");
90 instance->method->delete (instance);
91 if (instance->args != NULL)
92 free (instance->args);
93 if (instance->path != NULL)
94 free (instance->path);
95 if (instance->child == NULL)
97 /* only remove leaf nodes */
98 struct hw_instance **curr = &me->instances;
99 while (*curr != instance)
101 ASSERT (*curr != NULL);
102 curr = &(*curr)->next;
104 *curr = instance->next;
108 /* check it isn't in the instance list */
109 struct hw_instance *curr = me->instances;
112 ASSERT (curr != instance);
115 /* unlink the child */
116 ASSERT (instance->child->parent == instance);
117 instance->child->parent = NULL;
119 cap_remove (me->ihandles, instance);
126 panic_hw_instance_read (struct hw_instance *instance,
130 hw_abort (hw_instance_hw (instance), "no read method");
137 panic_hw_instance_write (struct hw_instance *instance,
141 hw_abort (hw_instance_hw (instance), "no write method");
147 panic_hw_instance_seek (struct hw_instance *instance,
148 unsigned_word pos_hi,
149 unsigned_word pos_lo)
151 hw_abort (hw_instance_hw (instance), "no seek method");
157 hw_instance_call_method (struct hw_instance *instance,
158 const char *method_name,
160 unsigned_cell stack_args[/*n_stack_args*/],
162 unsigned_cell stack_returns[/*n_stack_args*/])
165 hw_abort (hw_instance_hw (instance), "not implemented");
168 struct hw *me = instance->owner;
169 const hw_instance_methods *method = instance->method->methods;
172 hw_abort (me, "no methods (want %s)", method_name);
174 while (method->name != NULL)
176 if (strcmp (method->name, method_name) == 0)
178 return method->method (instance,
179 n_stack_args, stack_args,
180 n_stack_returns, stack_returns);
184 hw_abort (me, "no %s method", method_name);
190 #define set_hw_instance_read(instance, method)\
191 ((instance)->to_instance_read = (method))
193 #define set_hw_instance_write(instance, method)\
194 ((instance)->to_instance_write = (method))
196 #define set_hw_instance_seek(instance, method)\
197 ((instance)->to_instance_seek = (method))
202 set_hw_instance_finish (struct hw *me,
203 hw_instance_finish_method *method)
205 if (me->instances_of_hw == NULL)
206 me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
207 me->instances_of_hw->to_finish = method;
213 hw_instance_create (struct hw *me,
214 struct hw_instance *parent,
218 struct hw_instance *instance = ZALLOC (struct hw_instance);
220 /* link this instance into the devices list */
221 instance->hw_of_instance = me;
222 instance->parent_of_instance = NULL;
223 /* link this instance into the front of the devices instance list */
224 instance->sibling_of_instance = me->instances_of_hw->instances;
225 me->instances_of_hw->instances = instance;
228 ASSERT (parent->child_of_instance == NULL);
229 parent->child_of_instance = instance;
230 instance->parent_of_instance = parent;
232 instance->args_of_instance = hw_strdup (me, args);
233 instance->path_of_instance = hw_strdup (me, path);
234 set_hw_instance_read (instance, panic_hw_instance_read);
235 set_hw_instance_write (instance, panic_hw_instance_write);
236 set_hw_instance_seek (instance, panic_hw_instance_seek);
237 hw_handle_add_ihandle (me, instance);
238 me->instances_of_hw->to_finish (me, instance);
244 hw_instance_interceed (struct hw_instance *parent,
251 struct hw_instance *instance = ZALLOC (struct hw_instance);
253 /* link this instance into the devices list */
256 ASSERT (parent == NULL);
257 instance->hw_of_instance = me;
258 instance->parent_of_instance = NULL;
259 /* link this instance into the front of the devices instance list */
260 instance->sibling_of_instance = me->instances_of_hw->instances;
261 me->instances_of_hw->instances = instance;
265 struct hw_instance **previous;
266 ASSERT (parent->child_of_instance == NULL);
267 parent->child_of_instance = instance;
268 instance->owner = parent->owner;
269 instance->parent_of_instance = parent;
270 /* in the devices instance list replace the parent instance with
272 instance->next = parent->next;
273 /* replace parent with this new node */
274 previous = &instance->owner->instances;
275 while (*previous != parent)
277 ASSERT (*previous != NULL);
278 previous = &(*previous)->next;
280 *previous = instance;
282 instance->data = data;
283 instance->args = (args == NULL ? NULL : (char *) strdup (args));
284 instance->path = (path == NULL ? NULL : (char *) strdup (path));
285 cap_add (instance->owner->ihandles, instance);