1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
29 #include "sim-assert.h"
31 struct hw_instance_data
{
32 hw_finish_instance_method
*to_finish
;
33 struct hw_instance
*instances
;
36 static hw_finish_instance_method abort_hw_finish_instance
;
39 create_hw_instance_data (struct hw
*me
)
41 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
42 set_hw_finish_instance (me
, abort_hw_finish_instance
);
46 delete_hw_instance_data (struct hw
*me
)
53 abort_hw_finish_instance (struct hw
*hw
,
54 struct hw_instance
*instance
)
56 hw_abort (hw
, "no instance finish method");
60 set_hw_finish_instance (struct hw
*me
,
61 hw_finish_instance_method
*finish
)
63 me
->instances_of_hw
->to_finish
= finish
;
69 clean_hw_instances (struct hw
*me
)
71 struct hw_instance
**instance
= &me
->instances
;
72 while (*instance
!= NULL
)
74 struct hw_instance
*old_instance
= *instance
;
75 hw_instance_delete (old_instance
);
76 instance
= &me
->instances
;
83 hw_instance_delete (struct hw_instance
*instance
)
86 hw_abort (hw_instance_hw (instance
), "not implemented");
88 struct hw
*me
= hw_instance_hw (instance
);
89 if (instance
->to_instance_delete
== NULL
)
90 hw_abort (me
, "no delete method");
91 instance
->method
->delete(instance
);
92 if (instance
->args
!= NULL
)
93 zfree (instance
->args
);
94 if (instance
->path
!= NULL
)
95 zfree (instance
->path
);
96 if (instance
->child
== NULL
)
98 /* only remove leaf nodes */
99 struct hw_instance
**curr
= &me
->instances
;
100 while (*curr
!= instance
)
102 ASSERT (*curr
!= NULL
);
103 curr
= &(*curr
)->next
;
105 *curr
= instance
->next
;
109 /* check it isn't in the instance list */
110 struct hw_instance
*curr
= me
->instances
;
113 ASSERT(curr
!= instance
);
116 /* unlink the child */
117 ASSERT (instance
->child
->parent
== instance
);
118 instance
->child
->parent
= NULL
;
120 cap_remove (me
->ihandles
, instance
);
127 panic_hw_instance_read (struct hw_instance
*instance
,
131 hw_abort (hw_instance_hw (instance
), "no read method");
138 panic_hw_instance_write (struct hw_instance
*instance
,
142 hw_abort (hw_instance_hw (instance
), "no write method");
148 panic_hw_instance_seek (struct hw_instance
*instance
,
149 unsigned_word pos_hi
,
150 unsigned_word pos_lo
)
152 hw_abort (hw_instance_hw (instance
), "no seek method");
158 hw_instance_call_method (struct hw_instance
*instance
,
159 const char *method_name
,
161 unsigned_cell stack_args
[/*n_stack_args*/],
163 unsigned_cell stack_returns
[/*n_stack_args*/])
166 hw_abort (hw_instance_hw (instance
), "not implemented");
169 struct hw
*me
= instance
->owner
;
170 const hw_instance_methods
*method
= instance
->method
->methods
;
173 hw_abort (me
, "no methods (want %s)", method_name
);
175 while (method
->name
!= NULL
)
177 if (strcmp(method
->name
, method_name
) == 0)
179 return method
->method (instance
,
180 n_stack_args
, stack_args
,
181 n_stack_returns
, stack_returns
);
185 hw_abort (me
, "no %s method", method_name
);
191 #define set_hw_instance_read(instance, method)\
192 ((instance)->to_instance_read = (method))
194 #define set_hw_instance_write(instance, method)\
195 ((instance)->to_instance_write = (method))
197 #define set_hw_instance_seek(instance, method)\
198 ((instance)->to_instance_seek = (method))
203 set_hw_instance_finish (struct hw
*me
,
204 hw_instance_finish_method
*method
)
206 if (me
->instances_of_hw
== NULL
)
207 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
208 me
->instances_of_hw
->to_finish
= method
;
214 hw_instance_create (struct hw
*me
,
215 struct hw_instance
*parent
,
219 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
221 /* link this instance into the devices list */
222 instance
->hw_of_instance
= me
;
223 instance
->parent_of_instance
= NULL
;
224 /* link this instance into the front of the devices instance list */
225 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
226 me
->instances_of_hw
->instances
= instance
;
229 ASSERT (parent
->child_of_instance
== NULL
);
230 parent
->child_of_instance
= instance
;
231 instance
->parent_of_instance
= parent
;
233 instance
->args_of_instance
= hw_strdup (me
, args
);
234 instance
->path_of_instance
= hw_strdup (me
, path
);
235 set_hw_instance_read (instance
, panic_hw_instance_read
);
236 set_hw_instance_write (instance
, panic_hw_instance_write
);
237 set_hw_instance_seek (instance
, panic_hw_instance_seek
);
238 hw_handle_add_ihandle (me
, instance
);
239 me
->instances_of_hw
->to_finish (me
, instance
);
245 hw_instance_interceed (struct hw_instance
*parent
,
252 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
254 /* link this instance into the devices list */
257 ASSERT (parent
== NULL
);
258 instance
->hw_of_instance
= me
;
259 instance
->parent_of_instance
= NULL
;
260 /* link this instance into the front of the devices instance list */
261 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
262 me
->instances_of_hw
->instances
= instance
;
266 struct hw_instance
**previous
;
267 ASSERT (parent
->child_of_instance
== NULL
);
268 parent
->child_of_instance
= instance
;
269 instance
->owner
= parent
->owner
;
270 instance
->parent_of_instance
= parent
;
271 /* in the devices instance list replace the parent instance with
273 instance
->next
= parent
->next
;
274 /* replace parent with this new node */
275 previous
= &instance
->owner
->instances
;
276 while (*previous
!= parent
)
278 ASSERT (*previous
!= NULL
);
279 previous
= &(*previous
)->next
;
281 *previous
= instance
;
283 instance
->data
= data
;
284 instance
->args
= (args
== NULL
? NULL
: (char *) strdup(args
));
285 instance
->path
= (path
== NULL
? NULL
: (char *) strdup(path
));
286 cap_add (instance
->owner
->ihandles
, instance
);