OSDN Git Service

2002-11-22 Andrew Cagney <ac131313@redhat.com>
[pf3gnuchains/pf3gnuchains3x.git] / sim / common / hw-instances.c
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3    Copyright 2002 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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24
25 #include "hw-main.h"
26 #include "hw-base.h"
27
28 #include "sim-io.h"
29 #include "sim-assert.h"
30
31 struct hw_instance_data {
32   hw_finish_instance_method *to_finish;
33   struct hw_instance *instances;
34 };
35
36 static hw_finish_instance_method abort_hw_finish_instance;
37
38 void
39 create_hw_instance_data (struct hw *me)
40 {
41   me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
42   set_hw_finish_instance (me, abort_hw_finish_instance);
43 }
44
45 void
46 delete_hw_instance_data (struct hw *me)
47 {
48   /* NOP */
49 }
50
51
52 static void
53 abort_hw_finish_instance (struct hw *hw,
54                           struct hw_instance *instance)
55 {
56   hw_abort (hw, "no instance finish method");
57 }
58
59 void
60 set_hw_finish_instance (struct hw *me,
61                         hw_finish_instance_method *finish)
62 {
63   me->instances_of_hw->to_finish = finish;
64 }
65
66
67 #if 0
68 void
69 clean_hw_instances (struct hw *me)
70 {
71   struct hw_instance **instance = &me->instances;
72   while (*instance != NULL)
73     {
74       struct hw_instance *old_instance = *instance;
75       hw_instance_delete (old_instance);
76       instance = &me->instances;
77     }
78 }
79 #endif
80
81
82 void
83 hw_instance_delete (struct hw_instance *instance)
84 {
85 #if 1
86   hw_abort (hw_instance_hw (instance), "not implemented");
87 #else
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)
97     {
98       /* only remove leaf nodes */
99       struct hw_instance **curr = &me->instances;
100       while (*curr != instance)
101         {
102           ASSERT (*curr != NULL);
103           curr = &(*curr)->next;
104         }
105       *curr = instance->next;
106     }
107   else
108     {
109       /* check it isn't in the instance list */
110       struct hw_instance *curr = me->instances;
111       while (curr != NULL)
112         {
113           ASSERT(curr != instance);
114           curr = curr->next;
115         }
116       /* unlink the child */
117       ASSERT (instance->child->parent == instance);
118       instance->child->parent = NULL;
119     }
120   cap_remove (me->ihandles, instance);
121   zfree (instance);
122 #endif
123 }
124
125
126 static int
127 panic_hw_instance_read (struct hw_instance *instance,
128                         void *addr,
129                         unsigned_word len)
130 {
131   hw_abort (hw_instance_hw (instance), "no read method");
132   return -1;
133 }
134
135
136
137 static int
138 panic_hw_instance_write (struct hw_instance *instance,
139                          const void *addr,
140                          unsigned_word len)
141 {
142   hw_abort (hw_instance_hw (instance), "no write method");
143   return -1;
144 }
145
146
147 static int
148 panic_hw_instance_seek (struct hw_instance *instance,
149                         unsigned_word pos_hi,
150                         unsigned_word pos_lo)
151 {
152   hw_abort (hw_instance_hw (instance), "no seek method");
153   return -1;
154 }
155
156
157 int
158 hw_instance_call_method (struct hw_instance *instance,
159                          const char *method_name,
160                          int n_stack_args,
161                          unsigned_cell stack_args[/*n_stack_args*/],    
162                          int n_stack_returns,
163                          unsigned_cell stack_returns[/*n_stack_args*/])
164 {
165 #if 1
166   hw_abort (hw_instance_hw (instance), "not implemented");
167   return -1;
168 #else
169   struct hw *me = instance->owner;
170   const hw_instance_methods *method = instance->method->methods;
171   if (method == NULL)
172     {
173       hw_abort (me, "no methods (want %s)", method_name);
174     }
175   while (method->name != NULL)
176     {
177       if (strcmp(method->name, method_name) == 0)
178         {
179           return method->method (instance,
180                                  n_stack_args, stack_args,
181                                  n_stack_returns, stack_returns);
182         }
183       method++;
184     }
185   hw_abort (me, "no %s method", method_name);
186   return 0;
187 #endif
188 }
189
190
191 #define set_hw_instance_read(instance, method)\
192 ((instance)->to_instance_read = (method))
193
194 #define set_hw_instance_write(instance, method)\
195 ((instance)->to_instance_write = (method))
196
197 #define set_hw_instance_seek(instance, method)\
198 ((instance)->to_instance_seek = (method))
199
200
201 #if 0
202 static void
203 set_hw_instance_finish (struct hw *me,
204                         hw_instance_finish_method *method)
205 {
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;
209 }
210 #endif
211
212
213 struct hw_instance *
214 hw_instance_create (struct hw *me,
215                     struct hw_instance *parent,
216                     const char *path,
217                     const char *args)
218 {
219   struct hw_instance *instance = ZALLOC (struct hw_instance);
220   /*instance->unit*/
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;
227   if (parent != NULL)
228     {
229       ASSERT (parent->child_of_instance == NULL);
230       parent->child_of_instance = instance;
231       instance->parent_of_instance = parent;
232     }
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);
240   return instance;
241 }
242
243
244 struct hw_instance *
245 hw_instance_interceed (struct hw_instance *parent,
246                        const char *path,
247                        const char *args)
248 {
249 #if 1
250   return NULL;
251 #else
252   struct hw_instance *instance = ZALLOC (struct hw_instance);
253   /*instance->unit*/
254   /* link this instance into the devices list */
255   if (me != NULL)
256     {
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;
263     }
264   if (parent != NULL)
265     {
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
272          this one */
273       instance->next = parent->next;
274       /* replace parent with this new node */
275       previous = &instance->owner->instances;
276       while (*previous != parent)
277         {
278           ASSERT (*previous != NULL);
279           previous = &(*previous)->next;
280         }
281       *previous = instance;
282     }
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);
287   return instance;
288 #endif
289 }