2005-03-04 Michael Snyder <msnyder@redhat.com>
[external/binutils.git] / gdb / mn10300-tdep.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.\r
2 \r
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free\r
4    Software Foundation, Inc.\r
5 \r
6    This file is part of GDB.\r
7 \r
8    This program is free software; you can redistribute it and/or modify\r
9    it under the terms of the GNU General Public License as published by\r
10    the Free Software Foundation; either version 2 of the License, or\r
11    (at your option) any later version.\r
12 \r
13    This program is distributed in the hope that it will be useful,\r
14    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16    GNU General Public License for more details.\r
17 \r
18    You should have received a copy of the GNU General Public License\r
19    along with this program; if not, write to the Free Software\r
20    Foundation, Inc., 59 Temple Place - Suite 330,\r
21    Boston, MA 02111-1307, USA.  */\r
22 \r
23 /* MVS Notes:\r
24 \r
25    To get from 1.1 to 1.2, add:\r
26    use_struct_convention\r
27    store_return_value\r
28    extract_return_value\r
29    extract_struct_value_address\r
30    \r
31    Make sure to use regcache.  */\r
32 \r
33 /* MVS Notes:\r
34 \r
35    Apparently cannot run without a stub placeholder for unwind_dummy_id.  \r
36 */\r
37 \r
38 /* MVS Notes:\r
39 \r
40    To get from 1.2 to 1.3, add:\r
41    read_pc, write_pc\r
42    frame_unwind_init\r
43    struct mn10300_unwind_cache\r
44    unwind_pc\r
45    unwind_dummy_id\r
46    frame_this_id\r
47    frame_prev_register\r
48    frame_sniffer (struct mn10300_frame_unwind)\r
49 */\r
50 \r
51 #include "defs.h"\r
52 #include "arch-utils.h"\r
53 #include "dis-asm.h"\r
54 #include "gdbtypes.h"\r
55 #include "regcache.h"\r
56 #include "gdb_string.h"\r
57 #include "gdb_assert.h"\r
58 #include "frame.h"\r
59 #include "frame-unwind.h"\r
60 #include "frame-base.h"\r
61 #include "trad-frame.h"\r
62 #include "symtab.h"\r
63 #include "dwarf2-frame.h"\r
64 #include "regcache.h"\r
65 \r
66 #include "mn10300-tdep.h"\r
67 \r
68 \r
69 /* Compute the alignment required by a type.  */\r
70 \r
71 static int\r
72 mn10300_type_align (struct type *type)\r
73 {\r
74   int i, align = 1;\r
75 \r
76   switch (TYPE_CODE (type))\r
77     {\r
78     case TYPE_CODE_INT:\r
79     case TYPE_CODE_ENUM:\r
80     case TYPE_CODE_SET:\r
81     case TYPE_CODE_RANGE:\r
82     case TYPE_CODE_CHAR:\r
83     case TYPE_CODE_BOOL:\r
84     case TYPE_CODE_FLT:\r
85     case TYPE_CODE_PTR:\r
86     case TYPE_CODE_REF:\r
87       return TYPE_LENGTH (type);\r
88 \r
89     case TYPE_CODE_COMPLEX:\r
90       return TYPE_LENGTH (type) / 2;\r
91 \r
92     case TYPE_CODE_STRUCT:\r
93     case TYPE_CODE_UNION:\r
94       for (i = 0; i < TYPE_NFIELDS (type); i++)\r
95         {\r
96           int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));\r
97           while (align < falign)\r
98             align <<= 1;\r
99         }\r
100       return align;\r
101 \r
102     case TYPE_CODE_ARRAY:\r
103       /* HACK!  Structures containing arrays, even small ones, are not\r
104          elligible for returning in registers.  */\r
105       return 256;\r
106 \r
107     case TYPE_CODE_TYPEDEF:\r
108       return mn10300_type_align (check_typedef (type));\r
109 \r
110     default:\r
111       internal_error (__FILE__, __LINE__, _("bad switch"));\r
112     }\r
113 }\r
114 \r
115 /* MVS note this is deprecated.  */\r
116 /* Should call_function allocate stack space for a struct return?  */\r
117 /* gcc_p unused */\r
118 static int\r
119 mn10300_use_struct_convention (int gcc_p, struct type *type)\r
120 {\r
121   /* Structures bigger than a pair of words can't be returned in\r
122      registers.  */\r
123   if (TYPE_LENGTH (type) > 8)\r
124     return 1;\r
125 \r
126   switch (TYPE_CODE (type))\r
127     {\r
128     case TYPE_CODE_STRUCT:\r
129     case TYPE_CODE_UNION:\r
130       /* Structures with a single field are handled as the field\r
131          itself.  */\r
132       if (TYPE_NFIELDS (type) == 1)\r
133         return mn10300_use_struct_convention (gcc_p, \r
134                                               TYPE_FIELD_TYPE (type, 0));\r
135 \r
136       /* Structures with word or double-word size are passed in memory, as\r
137          long as they require at least word alignment.  */\r
138       if (mn10300_type_align (type) >= 4)\r
139         return 0;\r
140 \r
141       return 1;\r
142 \r
143       /* Arrays are addressable, so they're never returned in\r
144          registers.  This condition can only hold when the array is\r
145          the only field of a struct or union.  */\r
146     case TYPE_CODE_ARRAY:\r
147       return 1;\r
148 \r
149     case TYPE_CODE_TYPEDEF:\r
150       return mn10300_use_struct_convention (gcc_p, check_typedef (type));\r
151 \r
152     default:\r
153       return 0;\r
154     }\r
155 }\r
156 \r
157 /* MVS note this is deprecated.  */\r
158 static void\r
159 mn10300_store_return_value (struct type *type,\r
160                             struct regcache *regcache, const void *valbuf)\r
161 {\r
162   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
163   int len = TYPE_LENGTH (type);\r
164   int reg, regsz;\r
165   \r
166   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
167     reg = 4;\r
168   else\r
169     reg = 0;\r
170 \r
171   regsz = register_size (gdbarch, reg);\r
172 \r
173   if (len <= regsz)\r
174     regcache_raw_write_part (regcache, reg, 0, len, valbuf);\r
175   else if (len <= 2 * regsz)\r
176     {\r
177       regcache_raw_write (regcache, reg, valbuf);\r
178       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
179       regcache_raw_write_part (regcache, reg+1, 0,\r
180                                len - regsz, (char *) valbuf + regsz);\r
181     }\r
182   else\r
183     internal_error (__FILE__, __LINE__,\r
184                     _("Cannot store return value %d bytes long."), len);\r
185 }\r
186 \r
187 /* MVS note deprecated.  */\r
188 static void\r
189 mn10300_extract_return_value (struct type *type,\r
190                               struct regcache *regcache, void *valbuf)\r
191 {\r
192   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
193   char buf[MAX_REGISTER_SIZE];\r
194   int len = TYPE_LENGTH (type);\r
195   int reg, regsz;\r
196 \r
197   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
198     reg = 4;\r
199   else\r
200     reg = 0;\r
201 \r
202   regsz = register_size (gdbarch, reg);\r
203   if (len <= regsz)\r
204     {\r
205       regcache_raw_read (regcache, reg, buf);\r
206       memcpy (valbuf, buf, len);\r
207     }\r
208   else if (len <= 2 * regsz)\r
209     {\r
210       regcache_raw_read (regcache, reg, buf);\r
211       memcpy (valbuf, buf, regsz);\r
212       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
213       regcache_raw_read (regcache, reg + 1, buf);\r
214       memcpy ((char *) valbuf + regsz, buf, len - regsz);\r
215     }\r
216   else\r
217     internal_error (__FILE__, __LINE__,\r
218                     _("Cannot extract return value %d bytes long."), len);\r
219 }\r
220 \r
221 static char *\r
222 register_name (int reg, char **regs, long sizeof_regs)\r
223 {\r
224   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))\r
225     return NULL;\r
226   else\r
227     return regs[reg];\r
228 }\r
229 \r
230 static const char *\r
231 mn10300_generic_register_name (int reg)\r
232 {\r
233   static char *regs[] =\r
234   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
235     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",\r
236     "", "", "", "", "", "", "", "",\r
237     "", "", "", "", "", "", "", "fp"\r
238   };\r
239   return register_name (reg, regs, sizeof regs);\r
240 }\r
241 \r
242 \r
243 static const char *\r
244 am33_register_name (int reg)\r
245 {\r
246   static char *regs[] =\r
247   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
248     "sp", "pc", "mdr", "psw", "lir", "lar", "",\r
249     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",\r
250     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""\r
251   };\r
252   return register_name (reg, regs, sizeof regs);\r
253 }\r
254 \r
255 \r
256 static struct type *\r
257 mn10300_register_type (struct gdbarch *gdbarch, int reg)\r
258 {\r
259   return builtin_type_int;\r
260 }\r
261 \r
262 static CORE_ADDR\r
263 mn10300_read_pc (ptid_t ptid)\r
264 {\r
265   return read_register_pid (E_PC_REGNUM, ptid);\r
266 }\r
267 \r
268 static void\r
269 mn10300_write_pc (CORE_ADDR val, ptid_t ptid)\r
270 {\r
271   return write_register_pid (E_PC_REGNUM, val, ptid);\r
272 }\r
273 \r
274 /* The breakpoint instruction must be the same size as the smallest\r
275    instruction in the instruction set.\r
276 \r
277    The Matsushita mn10x00 processors have single byte instructions\r
278    so we need a single byte breakpoint.  Matsushita hasn't defined\r
279    one, so we defined it ourselves.  */\r
280 \r
281 const static unsigned char *\r
282 mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)\r
283 {\r
284   static char breakpoint[] = {0xff};\r
285   *bp_size = 1;\r
286   return breakpoint;\r
287 }\r
288 \r
289 /* Function: skip_prologue\r
290    Return the address of the first inst past the prologue of the function.  */\r
291 \r
292 static CORE_ADDR\r
293 mn10300_skip_prologue (CORE_ADDR pc)\r
294 {\r
295   /* FIXME: not implemented.  */\r
296   /* First approximation, try simply using skip_prologue_using_sal.  */\r
297   return skip_prologue_using_sal (pc);\r
298 }\r
299 \r
300 /* Simple frame_unwind_cache.  \r
301    This finds the "extra info" for the frame.  */\r
302 struct trad_frame_cache *\r
303 mn10300_frame_unwind_cache (struct frame_info *next_frame,\r
304                             void **this_prologue_cache)\r
305 {\r
306   struct trad_frame_cache *cache;\r
307   CORE_ADDR pc;\r
308 \r
309   if (*this_prologue_cache)\r
310     return (*this_prologue_cache);\r
311 \r
312   cache = trad_frame_cache_zalloc (next_frame);\r
313   pc = gdbarch_unwind_pc (current_gdbarch, next_frame);\r
314   mn10300_analyze_prologue (next_frame, &cache, pc);\r
315 \r
316   trad_frame_set_id (cache, \r
317                      frame_id_build (trad_frame_get_this_base (cache), pc));\r
318 \r
319   (*this_prologue_cache) = cache;\r
320   return cache;\r
321 }\r
322 \r
323 /* Here is a dummy implementation.  */\r
324 static struct frame_id\r
325 mn10300_dummy_unwind_dummy_id (struct gdbarch *gdbarch,\r
326                                struct frame_info *next_frame)\r
327 {\r
328   return frame_id_build (0, 0);\r
329 }\r
330 \r
331 /* Trad frame implementation.  */\r
332 static void\r
333 mn10300_frame_this_id (struct frame_info *next_frame,\r
334                        void **this_prologue_cache,\r
335                        struct frame_id *this_id)\r
336 {\r
337   struct trad_frame_cache *cache = \r
338     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
339 \r
340   trad_frame_get_id (cache, this_id);\r
341 }\r
342 \r
343 static void\r
344 mn10300_frame_prev_register (struct frame_info *next_frame,\r
345                              void **this_prologue_cache,\r
346                              int regnum, int *optimizedp,\r
347                              enum lval_type *lvalp, CORE_ADDR *addrp,\r
348                              int *realnump, void *bufferp)\r
349 {\r
350   struct trad_frame_cache *cache =\r
351     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
352 \r
353   trad_frame_get_register (cache, next_frame, regnum, optimizedp, \r
354                            lvalp, addrp, realnump, bufferp);\r
355   /* Or...\r
356   trad_frame_get_prev_register (next_frame, cache->prev_regs, regnum, \r
357                            optimizedp, lvalp, addrp, realnump, bufferp);\r
358   */\r
359 }\r
360 \r
361 static const struct frame_unwind mn10300_frame_unwind = {\r
362   NORMAL_FRAME,\r
363   mn10300_frame_this_id, \r
364   mn10300_frame_prev_register\r
365 };\r
366 \r
367 static CORE_ADDR\r
368 mn10300_frame_base_address (struct frame_info *next_frame,\r
369                             void **this_prologue_cache)\r
370 {\r
371   struct trad_frame_cache *cache = \r
372     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
373 \r
374   return trad_frame_get_this_base (cache);\r
375 }\r
376 \r
377 static const struct frame_unwind *\r
378 mn10300_frame_sniffer (struct frame_info *next_frame)\r
379 {\r
380   return &mn10300_frame_unwind;\r
381 }\r
382 \r
383 static const struct frame_base mn10300_frame_base = {\r
384   &mn10300_frame_unwind, \r
385   mn10300_frame_base_address, \r
386   mn10300_frame_base_address,\r
387   mn10300_frame_base_address\r
388 };\r
389 \r
390 static CORE_ADDR\r
391 mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
392 {\r
393   ULONGEST pc;\r
394 \r
395   frame_unwind_unsigned_register (next_frame, E_PC_REGNUM, &pc);\r
396   return pc;\r
397 }\r
398 \r
399 static CORE_ADDR\r
400 mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
401 {\r
402   ULONGEST sp;\r
403 \r
404   frame_unwind_unsigned_register (next_frame, E_SP_REGNUM, &sp);\r
405   return sp;\r
406 }\r
407 \r
408 static void\r
409 mn10300_frame_unwind_init (struct gdbarch *gdbarch)\r
410 {\r
411   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);\r
412   frame_unwind_append_sniffer (gdbarch, mn10300_frame_sniffer);\r
413   frame_base_set_default (gdbarch, &mn10300_frame_base);\r
414   set_gdbarch_unwind_dummy_id (gdbarch, mn10300_dummy_unwind_dummy_id);\r
415   set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);\r
416   set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);\r
417 }\r
418 \r
419 /* Dump out the mn10300 specific architecture information. */\r
420 \r
421 static void\r
422 mn10300_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)\r
423 {\r
424   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);\r
425   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",\r
426                       tdep->am33_mode);\r
427 }\r
428 \r
429 static struct gdbarch *\r
430 mn10300_gdbarch_init (struct gdbarch_info info,\r
431                       struct gdbarch_list *arches)\r
432 {\r
433   struct gdbarch *gdbarch;\r
434   struct gdbarch_tdep *tdep;\r
435 \r
436   arches = gdbarch_list_lookup_by_info (arches, &info);\r
437   if (arches != NULL)\r
438     return arches->gdbarch;\r
439 \r
440   tdep = xmalloc (sizeof (struct gdbarch_tdep));\r
441   gdbarch = gdbarch_alloc (&info, tdep);\r
442 \r
443   switch (info.bfd_arch_info->mach)\r
444     {\r
445     case 0:\r
446     case bfd_mach_mn10300:\r
447       set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);\r
448       tdep->am33_mode = 0;\r
449       break;\r
450     case bfd_mach_am33:\r
451       set_gdbarch_register_name (gdbarch, am33_register_name);\r
452       tdep->am33_mode = 1;\r
453       break;\r
454     default:\r
455       internal_error (__FILE__, __LINE__,\r
456                       _("mn10300_gdbarch_init: Unknown mn10300 variant"));\r
457       break;\r
458     }\r
459 \r
460   /* Registers.  */\r
461   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);\r
462   set_gdbarch_register_type (gdbarch, mn10300_register_type);\r
463   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);\r
464   set_gdbarch_read_pc (gdbarch, mn10300_read_pc);\r
465   set_gdbarch_write_pc (gdbarch, mn10300_write_pc);\r
466   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);\r
467   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);\r
468 \r
469   /* Stack unwinding.  */\r
470   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);\r
471   /* Breakpoints.  */\r
472   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);\r
473   /* decr_pc_after_break? */\r
474   /* Disassembly.  */\r
475   set_gdbarch_print_insn (gdbarch, print_insn_mn10300);\r
476 \r
477   /* Stage 2 */\r
478   /* MVS Note: at least the first one is deprecated!  */\r
479   set_gdbarch_deprecated_use_struct_convention (gdbarch, \r
480                                                 mn10300_use_struct_convention);\r
481   set_gdbarch_store_return_value (gdbarch, mn10300_store_return_value);\r
482   set_gdbarch_extract_return_value (gdbarch, mn10300_extract_return_value);\r
483 \r
484   mn10300_frame_unwind_init (gdbarch);\r
485 \r
486   return gdbarch;\r
487 }\r
488  \r
489 void\r
490 _initialize_mn10300_tdep (void)\r
491 {\r
492   gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);\r
493 }\r
494 \r