* Contribute CGEN simulator build support code.
[external/binutils.git] / sim / common / hw-instances.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19     */
20
21
22 #include "hw-main.h"
23 #include "hw-base.h"
24
25 #include "sim-io.h"
26 #include "sim-assert.h"
27
28 struct hw_instance_data {
29   hw_finish_instance_method *to_finish;
30   struct hw_instance *instances;
31 };
32
33 static hw_finish_instance_method abort_hw_finish_instance;
34
35 void
36 create_hw_instance_data (struct hw *me)
37 {
38   me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
39   set_hw_finish_instance (me, abort_hw_finish_instance);
40 }
41
42 void
43 delete_hw_instance_data (struct hw *me)
44 {
45   /* NOP */
46 }
47
48
49 static void
50 abort_hw_finish_instance (struct hw *hw,
51                           struct hw_instance *instance)
52 {
53   hw_abort (hw, "no instance finish method");
54 }
55
56 void
57 set_hw_finish_instance (struct hw *me,
58                         hw_finish_instance_method *finish)
59 {
60   me->instances_of_hw->to_finish = finish;
61 }
62
63
64 #if 0
65 void
66 clean_hw_instances (struct hw *me)
67 {
68   struct hw_instance **instance = &me->instances;
69   while (*instance != NULL)
70     {
71       struct hw_instance *old_instance = *instance;
72       hw_instance_delete (old_instance);
73       instance = &me->instances;
74     }
75 }
76 #endif
77
78
79 void
80 hw_instance_delete (struct hw_instance *instance)
81 {
82 #if 1
83   hw_abort (hw_instance_hw (instance), "not implemented");
84 #else
85   struct hw *me = hw_instance_hw (instance);
86   if (instance->to_instance_delete == NULL)
87     hw_abort (me, "no delete method");
88   instance->method->delete(instance);
89   if (instance->args != NULL)
90     zfree (instance->args);
91   if (instance->path != NULL)
92     zfree (instance->path);
93   if (instance->child == NULL)
94     {
95       /* only remove leaf nodes */
96       struct hw_instance **curr = &me->instances;
97       while (*curr != instance)
98         {
99           ASSERT (*curr != NULL);
100           curr = &(*curr)->next;
101         }
102       *curr = instance->next;
103     }
104   else
105     {
106       /* check it isn't in the instance list */
107       struct hw_instance *curr = me->instances;
108       while (curr != NULL)
109         {
110           ASSERT(curr != instance);
111           curr = curr->next;
112         }
113       /* unlink the child */
114       ASSERT (instance->child->parent == instance);
115       instance->child->parent = NULL;
116     }
117   cap_remove (me->ihandles, instance);
118   zfree (instance);
119 #endif
120 }
121
122
123 static int
124 panic_hw_instance_read (struct hw_instance *instance,
125                         void *addr,
126                         unsigned_word len)
127 {
128   hw_abort (hw_instance_hw (instance), "no read method");
129   return -1;
130 }
131
132
133
134 static int
135 panic_hw_instance_write (struct hw_instance *instance,
136                          const void *addr,
137                          unsigned_word len)
138 {
139   hw_abort (hw_instance_hw (instance), "no write method");
140   return -1;
141 }
142
143
144 static int
145 panic_hw_instance_seek (struct hw_instance *instance,
146                         unsigned_word pos_hi,
147                         unsigned_word pos_lo)
148 {
149   hw_abort (hw_instance_hw (instance), "no seek method");
150   return -1;
151 }
152
153
154 int
155 hw_instance_call_method (struct hw_instance *instance,
156                          const char *method_name,
157                          int n_stack_args,
158                          unsigned_cell stack_args[/*n_stack_args*/],    
159                          int n_stack_returns,
160                          unsigned_cell stack_returns[/*n_stack_args*/])
161 {
162 #if 1
163   hw_abort (hw_instance_hw (instance), "not implemented");
164   return -1;
165 #else
166   struct hw *me = instance->owner;
167   const hw_instance_methods *method = instance->method->methods;
168   if (method == NULL)
169     {
170       hw_abort (me, "no methods (want %s)", method_name);
171     }
172   while (method->name != NULL)
173     {
174       if (strcmp(method->name, method_name) == 0)
175         {
176           return method->method (instance,
177                                  n_stack_args, stack_args,
178                                  n_stack_returns, stack_returns);
179         }
180       method++;
181     }
182   hw_abort (me, "no %s method", method_name);
183   return 0;
184 #endif
185 }
186
187
188 #define set_hw_instance_read(instance, method)\
189 ((instance)->to_instance_read = (method))
190
191 #define set_hw_instance_write(instance, method)\
192 ((instance)->to_instance_write = (method))
193
194 #define set_hw_instance_seek(instance, method)\
195 ((instance)->to_instance_seek = (method))
196
197
198 #if 0
199 static void
200 set_hw_instance_finish (struct hw *me,
201                         hw_instance_finish_method *method)
202 {
203   if (me->instances_of_hw == NULL)
204     me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
205   me->instances_of_hw->to_finish = method;
206 }
207 #endif
208
209
210 struct hw_instance *
211 hw_instance_create (struct hw *me,
212                     struct hw_instance *parent,
213                     const char *path,
214                     const char *args)
215 {
216   struct hw_instance *instance = ZALLOC (struct hw_instance);
217   /*instance->unit*/
218   /* link this instance into the devices list */
219   instance->hw_of_instance = me;
220   instance->parent_of_instance = NULL;
221   /* link this instance into the front of the devices instance list */
222   instance->sibling_of_instance = me->instances_of_hw->instances;
223   me->instances_of_hw->instances = instance;
224   if (parent != NULL)
225     {
226       ASSERT (parent->child_of_instance == NULL);
227       parent->child_of_instance = instance;
228       instance->parent_of_instance = parent;
229     }
230   instance->args_of_instance = hw_strdup (me, args);
231   instance->path_of_instance = hw_strdup (me, path);
232   set_hw_instance_read (instance, panic_hw_instance_read);
233   set_hw_instance_write (instance, panic_hw_instance_write);
234   set_hw_instance_seek (instance, panic_hw_instance_seek);
235   hw_handle_add_ihandle (me, instance);
236   me->instances_of_hw->to_finish (me, instance);
237   return instance;
238 }
239
240
241 struct hw_instance *
242 hw_instance_interceed (struct hw_instance *parent,
243                        const char *path,
244                        const char *args)
245 {
246 #if 1
247   return NULL;
248 #else
249   struct hw_instance *instance = ZALLOC (struct hw_instance);
250   /*instance->unit*/
251   /* link this instance into the devices list */
252   if (me != NULL)
253     {
254       ASSERT (parent == NULL);
255       instance->hw_of_instance = me;
256       instance->parent_of_instance = NULL;
257       /* link this instance into the front of the devices instance list */
258       instance->sibling_of_instance = me->instances_of_hw->instances;
259       me->instances_of_hw->instances = instance;
260     }
261   if (parent != NULL)
262     {
263       struct hw_instance **previous;
264       ASSERT (parent->child_of_instance == NULL);
265       parent->child_of_instance = instance;
266       instance->owner = parent->owner;
267       instance->parent_of_instance = parent;
268       /* in the devices instance list replace the parent instance with
269          this one */
270       instance->next = parent->next;
271       /* replace parent with this new node */
272       previous = &instance->owner->instances;
273       while (*previous != parent)
274         {
275           ASSERT (*previous != NULL);
276           previous = &(*previous)->next;
277         }
278       *previous = instance;
279     }
280   instance->data = data;
281   instance->args = (args == NULL ? NULL : (char *) strdup(args));
282   instance->path = (path == NULL ? NULL : (char *) strdup(path));
283   cap_add (instance->owner->ihandles, instance);
284   return instance;
285 #endif
286 }