* Makefile.in (mips-tdep.o, target-descriptions.o): Update.
[external/binutils.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006
4    Free Software Foundation, Inc.
5
6    Contributed by CodeSourcery.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor,
23    Boston, MA 02110-1301, USA.  */
24
25 #include "defs.h"
26 #include "arch-utils.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "vec.h"
30
31 #include "gdb_assert.h"
32
33 /* Types.  */
34
35 typedef struct property
36 {
37   const char *key;
38   const char *value;
39 } property_s;
40 DEF_VEC_O(property_s);
41
42 struct target_desc
43 {
44   /* Any architecture-specific properties specified by the target.  */
45   VEC(property_s) *properties;
46 };
47
48 /* Global state.  These variables are associated with the current
49    target; if GDB adds support for multiple simultaneous targets, then
50    these variables should become target-specific data.  */
51
52 /* A flag indicating that a description has already been fetched from
53    the current target, so it should not be queried again.  */
54
55 static int target_desc_fetched;
56
57 /* The description fetched from the current target, or NULL if the
58    current target did not supply any description.  Only valid when
59    target_desc_fetched is set.  Only the description initialization
60    code should access this; normally, the description should be
61    accessed through the gdbarch object.  */
62
63 static const struct target_desc *current_target_desc;
64
65 /* Fetch the current target's description, and switch the current
66    architecture to one which incorporates that description.  */
67
68 void
69 target_find_description (void)
70 {
71   /* If we've already fetched a description from the target, don't do
72      it again.  This allows a target to fetch the description early,
73      during its to_open or to_create_inferior, if it needs extra
74      information about the target to initialize.  */
75   if (target_desc_fetched)
76     return;
77
78   /* The current architecture should not have any target description
79      specified.  It should have been cleared, e.g. when we
80      disconnected from the previous target.  */
81   gdb_assert (gdbarch_target_desc (current_gdbarch) == NULL);
82
83   current_target_desc = target_read_description (&current_target);
84
85   /* If a non-NULL description was returned, then update the current
86      architecture.  */
87   if (current_target_desc)
88     {
89       struct gdbarch_info info;
90
91       gdbarch_info_init (&info);
92       info.target_desc = current_target_desc;
93       if (!gdbarch_update_p (info))
94         warning (_("Could not use target-supplied description"));
95     }
96
97   /* Now that we know this description is usable, record that we
98      fetched it.  */
99   target_desc_fetched = 1;
100 }
101
102 /* Discard any description fetched from the current target, and switch
103    the current architecture to one with no target description.  */
104
105 void
106 target_clear_description (void)
107 {
108   struct gdbarch_info info;
109
110   if (!target_desc_fetched)
111     return;
112
113   target_desc_fetched = 0;
114   current_target_desc = NULL;
115
116   gdbarch_info_init (&info);
117   if (!gdbarch_update_p (info))
118     internal_error (__FILE__, __LINE__,
119                     _("Could not remove target-supplied description"));
120 }
121
122 /* Return the global current target description.  This should only be
123    used by gdbarch initialization code; most access should be through
124    an existing gdbarch.  */
125
126 const struct target_desc *
127 target_current_description (void)
128 {
129   if (target_desc_fetched)
130     return current_target_desc;
131
132   return NULL;
133 }
134
135 /* Return the string value of a property named KEY, or NULL if the
136    property was not specified.  */
137
138 const char *
139 tdesc_property (const struct target_desc *target_desc, const char *key)
140 {
141   struct property *prop;
142   int ix;
143
144   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
145        ix++)
146     if (strcmp (prop->key, key) == 0)
147       return prop->value;
148
149   return NULL;
150 }
151
152 /* Methods for constructing a target description.  */
153
154 struct target_desc *
155 allocate_target_description (void)
156 {
157   return XZALLOC (struct target_desc);
158 }
159
160 void
161 set_tdesc_property (struct target_desc *target_desc,
162                     const char *key, const char *value)
163 {
164   struct property *prop, new_prop;
165   int ix;
166
167   gdb_assert (key != NULL && value != NULL);
168
169   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
170        ix++)
171     if (strcmp (prop->key, key) == 0)
172       internal_error (__FILE__, __LINE__,
173                       _("Attempted to add duplicate property \"%s\""), key);
174
175   new_prop.key = key;
176   new_prop.value = value;
177   VEC_safe_push (property_s, target_desc->properties, &new_prop);
178 }