1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007 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
{
30 hw_finish_instance_method
*to_finish
;
31 struct hw_instance
*instances
;
34 static hw_finish_instance_method abort_hw_finish_instance
;
37 create_hw_instance_data (struct hw
*me
)
39 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
40 set_hw_finish_instance (me
, abort_hw_finish_instance
);
44 delete_hw_instance_data (struct hw
*me
)
51 abort_hw_finish_instance (struct hw
*hw
,
52 struct hw_instance
*instance
)
54 hw_abort (hw
, "no instance finish method");
58 set_hw_finish_instance (struct hw
*me
,
59 hw_finish_instance_method
*finish
)
61 me
->instances_of_hw
->to_finish
= finish
;
67 clean_hw_instances (struct hw
*me
)
69 struct hw_instance
**instance
= &me
->instances
;
70 while (*instance
!= NULL
)
72 struct hw_instance
*old_instance
= *instance
;
73 hw_instance_delete (old_instance
);
74 instance
= &me
->instances
;
81 hw_instance_delete (struct hw_instance
*instance
)
84 hw_abort (hw_instance_hw (instance
), "not implemented");
86 struct hw
*me
= hw_instance_hw (instance
);
87 if (instance
->to_instance_delete
== NULL
)
88 hw_abort (me
, "no delete method");
89 instance
->method
->delete(instance
);
90 if (instance
->args
!= NULL
)
91 zfree (instance
->args
);
92 if (instance
->path
!= NULL
)
93 zfree (instance
->path
);
94 if (instance
->child
== NULL
)
96 /* only remove leaf nodes */
97 struct hw_instance
**curr
= &me
->instances
;
98 while (*curr
!= instance
)
100 ASSERT (*curr
!= NULL
);
101 curr
= &(*curr
)->next
;
103 *curr
= instance
->next
;
107 /* check it isn't in the instance list */
108 struct hw_instance
*curr
= me
->instances
;
111 ASSERT(curr
!= instance
);
114 /* unlink the child */
115 ASSERT (instance
->child
->parent
== instance
);
116 instance
->child
->parent
= NULL
;
118 cap_remove (me
->ihandles
, instance
);
125 panic_hw_instance_read (struct hw_instance
*instance
,
129 hw_abort (hw_instance_hw (instance
), "no read method");
136 panic_hw_instance_write (struct hw_instance
*instance
,
140 hw_abort (hw_instance_hw (instance
), "no write method");
146 panic_hw_instance_seek (struct hw_instance
*instance
,
147 unsigned_word pos_hi
,
148 unsigned_word pos_lo
)
150 hw_abort (hw_instance_hw (instance
), "no seek method");
156 hw_instance_call_method (struct hw_instance
*instance
,
157 const char *method_name
,
159 unsigned_cell stack_args
[/*n_stack_args*/],
161 unsigned_cell stack_returns
[/*n_stack_args*/])
164 hw_abort (hw_instance_hw (instance
), "not implemented");
167 struct hw
*me
= instance
->owner
;
168 const hw_instance_methods
*method
= instance
->method
->methods
;
171 hw_abort (me
, "no methods (want %s)", method_name
);
173 while (method
->name
!= NULL
)
175 if (strcmp(method
->name
, method_name
) == 0)
177 return method
->method (instance
,
178 n_stack_args
, stack_args
,
179 n_stack_returns
, stack_returns
);
183 hw_abort (me
, "no %s method", method_name
);
189 #define set_hw_instance_read(instance, method)\
190 ((instance)->to_instance_read = (method))
192 #define set_hw_instance_write(instance, method)\
193 ((instance)->to_instance_write = (method))
195 #define set_hw_instance_seek(instance, method)\
196 ((instance)->to_instance_seek = (method))
201 set_hw_instance_finish (struct hw
*me
,
202 hw_instance_finish_method
*method
)
204 if (me
->instances_of_hw
== NULL
)
205 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
206 me
->instances_of_hw
->to_finish
= method
;
212 hw_instance_create (struct hw
*me
,
213 struct hw_instance
*parent
,
217 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
219 /* link this instance into the devices list */
220 instance
->hw_of_instance
= me
;
221 instance
->parent_of_instance
= NULL
;
222 /* link this instance into the front of the devices instance list */
223 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
224 me
->instances_of_hw
->instances
= instance
;
227 ASSERT (parent
->child_of_instance
== NULL
);
228 parent
->child_of_instance
= instance
;
229 instance
->parent_of_instance
= parent
;
231 instance
->args_of_instance
= hw_strdup (me
, args
);
232 instance
->path_of_instance
= hw_strdup (me
, path
);
233 set_hw_instance_read (instance
, panic_hw_instance_read
);
234 set_hw_instance_write (instance
, panic_hw_instance_write
);
235 set_hw_instance_seek (instance
, panic_hw_instance_seek
);
236 hw_handle_add_ihandle (me
, instance
);
237 me
->instances_of_hw
->to_finish (me
, instance
);
243 hw_instance_interceed (struct hw_instance
*parent
,
250 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
252 /* link this instance into the devices list */
255 ASSERT (parent
== NULL
);
256 instance
->hw_of_instance
= me
;
257 instance
->parent_of_instance
= NULL
;
258 /* link this instance into the front of the devices instance list */
259 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
260 me
->instances_of_hw
->instances
= instance
;
264 struct hw_instance
**previous
;
265 ASSERT (parent
->child_of_instance
== NULL
);
266 parent
->child_of_instance
= instance
;
267 instance
->owner
= parent
->owner
;
268 instance
->parent_of_instance
= parent
;
269 /* in the devices instance list replace the parent instance with
271 instance
->next
= parent
->next
;
272 /* replace parent with this new node */
273 previous
= &instance
->owner
->instances
;
274 while (*previous
!= parent
)
276 ASSERT (*previous
!= NULL
);
277 previous
= &(*previous
)->next
;
279 *previous
= instance
;
281 instance
->data
= data
;
282 instance
->args
= (args
== NULL
? NULL
: (char *) strdup(args
));
283 instance
->path
= (path
== NULL
? NULL
: (char *) strdup(path
));
284 cap_add (instance
->owner
->ihandles
, instance
);