Imported Upstream version 7.9
[platform/upstream/gdb.git] / sim / common / hw-handles.c
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3    Copyright 2002-2015 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 3 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, see <http://www.gnu.org/licenses/>.  */
21
22
23 #include "hw-main.h"
24 #include "hw-base.h"
25
26
27 struct hw_handle_mapping
28 {
29   cell_word external;
30   struct hw *phandle;
31   struct hw_instance *ihandle;
32   struct hw_handle_mapping *next;
33 };
34
35
36 struct hw_handle_data
37 {
38   int nr_mappings;
39   struct hw_handle_mapping *mappings;
40 };
41
42 void
43 create_hw_handle_data (struct hw *hw)
44 {
45   if (hw_parent (hw) == NULL)
46     {
47       hw->handles_of_hw = HW_ZALLOC (hw, struct hw_handle_data);
48     }
49   else
50     {
51       hw->handles_of_hw = hw_root (hw)->handles_of_hw;
52     }
53 }
54
55 void
56 delete_hw_handle_data (struct hw *hw)
57 {
58   /* NULL */
59 }
60
61
62
63 #if 0
64 void
65 hw_handle_init (struct hw *hw)
66 {
67   struct hw_handle_mapping *current_map = db->mappings;
68   if (current_map != NULL)
69     {
70       db->nr_mappings = db->mappings->external;
71       /* verify that the mappings that were not removed are in
72          sequence down to nr 1 */
73       while (current_map->next != NULL)
74         {
75           if (current_map->external != current_map->next->external + 1)
76             error ("hw_handle: hw_handle database possibly corrupt");
77           current_map = current_map->next;
78         }
79       ASSERT (current_map->next == NULL);
80       if (current_map->external != 1)
81         error ("hw_handle: hw_handle database possibly corrupt");
82     }
83   else
84     {
85       db->nr_mappings = 0;
86     }
87 }
88 #endif
89
90
91 struct hw_instance *
92 hw_handle_ihandle2 (struct hw *hw,
93                     cell_word external)
94 {
95   struct hw_handle_data *db = hw->handles_of_hw;
96   struct hw_handle_mapping *current_map = db->mappings;
97   while (current_map != NULL)
98     {
99       if (current_map->external == external)
100         return current_map->ihandle;
101       current_map = current_map->next;
102     }
103   return (void*)0;
104 }
105
106
107 struct hw *
108 hw_handle_phandle2 (struct hw *hw,
109                     cell_word external)
110 {
111   struct hw_handle_data *db = hw->handles_of_hw;
112   struct hw_handle_mapping *current_map = db->mappings;
113   while (current_map != NULL)
114     {
115       if (current_map->external == external)
116         return current_map->phandle;
117       current_map = current_map->next;
118     }
119   return (void*)0;
120 }
121
122
123 cell_word
124 hw_handle_2ihandle (struct hw *hw,
125                     struct hw_instance *internal)
126 {
127   struct hw_handle_data *db = hw->handles_of_hw;
128   struct hw_handle_mapping *current_map = db->mappings;
129   while (current_map != NULL)
130     {
131       if (current_map->ihandle == internal)
132         return current_map->external;
133       current_map = current_map->next;
134     }
135   return 0;
136 }
137
138
139 cell_word
140 hw_handle_2phandle (struct hw *hw,
141                     struct hw *internal)
142 {
143   struct hw_handle_data *db = hw->handles_of_hw;
144   struct hw_handle_mapping *current_map = db->mappings;
145   while (current_map != NULL)
146     {
147       if (current_map->phandle == internal)
148         return current_map->external;
149       current_map = current_map->next;
150     }
151   return 0;
152 }
153
154
155 void
156 hw_handle_add_ihandle (struct hw *hw,
157                        struct hw_instance *internal)
158 {
159   struct hw_handle_data *db = hw->handles_of_hw;
160   if (hw_handle_2ihandle (hw, internal) != 0)
161     {
162       hw_abort (hw, "attempting to add an ihandle already in the data base");
163     }
164   else
165     {
166       /* insert at the front making things in decending order */
167       struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
168       new_map->next = db->mappings;
169       new_map->ihandle = internal;
170       db->nr_mappings += 1;
171       new_map->external = db->nr_mappings;
172       db->mappings = new_map;
173     }
174 }
175
176
177 void
178 hw_handle_add_phandle (struct hw *hw,
179                        struct hw *internal)
180 {
181   struct hw_handle_data *db = hw->handles_of_hw;
182   if (hw_handle_2phandle (hw, internal) != 0)
183     {
184       hw_abort (hw, "attempting to add a phandle already in the data base");
185     }
186   else
187     {
188       /* insert at the front making things in decending order */
189       struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping);
190       new_map->next = db->mappings;
191       new_map->phandle = internal;
192       db->nr_mappings += 1;
193       new_map->external = db->nr_mappings;
194       db->mappings = new_map;
195     }
196 }
197
198
199 void
200 hw_handle_remove_ihandle (struct hw *hw,
201                           struct hw_instance *internal)
202 {
203   struct hw_handle_data *db = hw->handles_of_hw;
204   struct hw_handle_mapping **current_map = &db->mappings;
205   while (*current_map != NULL)
206     {
207       if ((*current_map)->ihandle == internal)
208         {
209           struct hw_handle_mapping *delete = *current_map;
210           *current_map = delete->next;
211           free (delete);
212           return;
213         }
214       current_map = &(*current_map)->next;
215     }
216   hw_abort (hw, "attempt to remove nonexistant ihandle");
217 }
218
219
220 void
221 hw_handle_remove_phandle (struct hw *hw,
222                           struct hw *internal)
223 {
224   struct hw_handle_data *db = hw->handles_of_hw;
225   struct hw_handle_mapping **current_map = &db->mappings;
226   while (*current_map != NULL)
227     {
228       if ((*current_map)->phandle == internal)
229         {
230           struct hw_handle_mapping *delete = *current_map;
231           *current_map = delete->next;
232           free (delete);
233           return;
234         }
235       current_map = &(*current_map)->next;
236     }
237   hw_abort (hw, "attempt to remove nonexistant phandle");
238 }