2004-12-10 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 enum {\r
69   E_D0_REGNUM = 0,\r
70   E_D1_REGNUM = 1,\r
71   E_D2_REGNUM = 2,\r
72   E_D3_REGNUM = 3,\r
73   E_A0_REGNUM = 4,\r
74   E_A1_REGNUM = 5,\r
75   E_A2_REGNUM = 6,\r
76   E_A3_REGNUM = 7,\r
77   E_SP_REGNUM = 8,\r
78   E_PC_REGNUM = 9,\r
79   E_MDR_REGNUM = 10,\r
80   E_PSW_REGNUM = 11,\r
81   E_LIR_REGNUM = 12,\r
82   E_LAR_REGNUM = 13,\r
83   E_MDRQ_REGNUM = 14,\r
84   E_E0_REGNUM = 15,\r
85   E_MCRH_REGNUM = 26,\r
86   E_MCRL_REGNUM = 27,\r
87   E_MCVF_REGNUM = 28,\r
88   E_NUM_REGS = 32\r
89 };\r
90 \r
91 \r
92 /* Compute the alignment required by a type.  */\r
93 \r
94 static int\r
95 mn10300_type_align (struct type *type)\r
96 {\r
97   int i, align = 1;\r
98 \r
99   switch (TYPE_CODE (type))\r
100     {\r
101     case TYPE_CODE_INT:\r
102     case TYPE_CODE_ENUM:\r
103     case TYPE_CODE_SET:\r
104     case TYPE_CODE_RANGE:\r
105     case TYPE_CODE_CHAR:\r
106     case TYPE_CODE_BOOL:\r
107     case TYPE_CODE_FLT:\r
108     case TYPE_CODE_PTR:\r
109     case TYPE_CODE_REF:\r
110       return TYPE_LENGTH (type);\r
111 \r
112     case TYPE_CODE_COMPLEX:\r
113       return TYPE_LENGTH (type) / 2;\r
114 \r
115     case TYPE_CODE_STRUCT:\r
116     case TYPE_CODE_UNION:\r
117       for (i = 0; i < TYPE_NFIELDS (type); i++)\r
118         {\r
119           int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));\r
120           while (align < falign)\r
121             align <<= 1;\r
122         }\r
123       return align;\r
124 \r
125     case TYPE_CODE_ARRAY:\r
126       /* HACK!  Structures containing arrays, even small ones, are not\r
127          elligible for returning in registers.  */\r
128       return 256;\r
129 \r
130     case TYPE_CODE_TYPEDEF:\r
131       return mn10300_type_align (check_typedef (type));\r
132 \r
133     default:\r
134       internal_error (__FILE__, __LINE__, "bad switch");\r
135     }\r
136 }\r
137 \r
138 /* MVS note this is deprecated.  */\r
139 /* Should call_function allocate stack space for a struct return?  */\r
140 /* gcc_p unused */\r
141 static int\r
142 mn10300_use_struct_convention (int gcc_p, struct type *type)\r
143 {\r
144   /* Structures bigger than a pair of words can't be returned in\r
145      registers.  */\r
146   if (TYPE_LENGTH (type) > 8)\r
147     return 1;\r
148 \r
149   switch (TYPE_CODE (type))\r
150     {\r
151     case TYPE_CODE_STRUCT:\r
152     case TYPE_CODE_UNION:\r
153       /* Structures with a single field are handled as the field\r
154          itself.  */\r
155       if (TYPE_NFIELDS (type) == 1)\r
156         return mn10300_use_struct_convention (gcc_p, \r
157                                               TYPE_FIELD_TYPE (type, 0));\r
158 \r
159       /* Structures with word or double-word size are passed in memory, as\r
160          long as they require at least word alignment.  */\r
161       if (mn10300_type_align (type) >= 4)\r
162         return 0;\r
163 \r
164       return 1;\r
165 \r
166       /* Arrays are addressable, so they're never returned in\r
167          registers.  This condition can only hold when the array is\r
168          the only field of a struct or union.  */\r
169     case TYPE_CODE_ARRAY:\r
170       return 1;\r
171 \r
172     case TYPE_CODE_TYPEDEF:\r
173       return mn10300_use_struct_convention (gcc_p, check_typedef (type));\r
174 \r
175     default:\r
176       return 0;\r
177     }\r
178 }\r
179 \r
180 /* MVS note this is deprecated.  */\r
181 static void\r
182 mn10300_store_return_value (struct type *type,\r
183                             struct regcache *regcache, const void *valbuf)\r
184 {\r
185   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
186   int len = TYPE_LENGTH (type);\r
187   int reg, regsz;\r
188   \r
189   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
190     reg = 4;\r
191   else\r
192     reg = 0;\r
193 \r
194   regsz = register_size (gdbarch, reg);\r
195 \r
196   if (len <= regsz)\r
197     regcache_raw_write_part (regcache, reg, 0, len, valbuf);\r
198   else if (len <= 2 * regsz)\r
199     {\r
200       regcache_raw_write (regcache, reg, valbuf);\r
201       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
202       regcache_raw_write_part (regcache, reg+1, 0,\r
203                                len - regsz, (char *) valbuf + regsz);\r
204     }\r
205   else\r
206     internal_error (__FILE__, __LINE__,\r
207                     "Cannot store return value %d bytes long.", len);\r
208 }\r
209 \r
210 /* MVS note deprecated.  */\r
211 static void\r
212 mn10300_extract_return_value (struct type *type,\r
213                               struct regcache *regcache, void *valbuf)\r
214 {\r
215   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
216   char buf[MAX_REGISTER_SIZE];\r
217   int len = TYPE_LENGTH (type);\r
218   int reg, regsz;\r
219 \r
220   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
221     reg = 4;\r
222   else\r
223     reg = 0;\r
224 \r
225   regsz = register_size (gdbarch, reg);\r
226   if (len <= regsz)\r
227     {\r
228       regcache_raw_read (regcache, reg, buf);\r
229       memcpy (valbuf, buf, len);\r
230     }\r
231   else if (len <= 2 * regsz)\r
232     {\r
233       regcache_raw_read (regcache, reg, buf);\r
234       memcpy (valbuf, buf, regsz);\r
235       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
236       regcache_raw_read (regcache, reg + 1, buf);\r
237       memcpy ((char *) valbuf + regsz, buf, len - regsz);\r
238     }\r
239   else\r
240     internal_error (__FILE__, __LINE__,\r
241                     "Cannot extract return value %d bytes long.", len);\r
242 }\r
243 \r
244 static char *\r
245 register_name (int reg, char **regs, long sizeof_regs)\r
246 {\r
247   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))\r
248     return NULL;\r
249   else\r
250     return regs[reg];\r
251 }\r
252 \r
253 static const char *\r
254 mn10300_generic_register_name (int reg)\r
255 {\r
256   static char *regs[] =\r
257   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
258     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",\r
259     "", "", "", "", "", "", "", "",\r
260     "", "", "", "", "", "", "", "fp"\r
261   };\r
262   return register_name (reg, regs, sizeof regs);\r
263 }\r
264 \r
265 \r
266 static const char *\r
267 am33_register_name (int reg)\r
268 {\r
269   static char *regs[] =\r
270   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
271     "sp", "pc", "mdr", "psw", "lir", "lar", "",\r
272     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",\r
273     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""\r
274   };\r
275   return register_name (reg, regs, sizeof regs);\r
276 }\r
277 \r
278 \r
279 static struct type *\r
280 mn10300_register_type (struct gdbarch *gdbarch, int reg)\r
281 {\r
282   return builtin_type_int;\r
283 }\r
284 \r
285 static CORE_ADDR\r
286 mn10300_read_pc (ptid_t ptid)\r
287 {\r
288   return read_register_pid (E_PC_REGNUM, ptid);\r
289 }\r
290 \r
291 static void\r
292 mn10300_write_pc (CORE_ADDR val, ptid_t ptid)\r
293 {\r
294   return write_register_pid (E_PC_REGNUM, val, ptid);\r
295 }\r
296 \r
297 /* The breakpoint instruction must be the same size as the smallest\r
298    instruction in the instruction set.\r
299 \r
300    The Matsushita mn10x00 processors have single byte instructions\r
301    so we need a single byte breakpoint.  Matsushita hasn't defined\r
302    one, so we defined it ourselves.  */\r
303 \r
304 const static unsigned char *\r
305 mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)\r
306 {\r
307   static char breakpoint[] = {0xff};\r
308   *bp_size = 1;\r
309   return breakpoint;\r
310 }\r
311 \r
312 /* Function: skip_prologue\r
313    Return the address of the first inst past the prologue of the function.  */\r
314 \r
315 static CORE_ADDR\r
316 mn10300_skip_prologue (CORE_ADDR pc)\r
317 {\r
318   /* FIXME: not implemented.  */\r
319   /* First approximation, try simply using skip_prologue_using_sal.  */\r
320   return skip_prologue_using_sal (pc);\r
321 }\r
322 \r
323 /* Simple frame_unwind_cache.  \r
324    This finds the "extra info" for the frame.  */\r
325 struct trad_frame_cache *\r
326 mn10300_frame_unwind_cache (struct frame_info *next_frame,\r
327                             void **this_prologue_cache)\r
328 {\r
329   struct trad_frame_cache *cache;\r
330 \r
331   if (*this_prologue_cache)\r
332     return (*this_prologue_cache);\r
333 \r
334   cache = trad_frame_cache_zalloc (next_frame);\r
335   trad_frame_set_id (cache, \r
336                      frame_id_build (gdbarch_unwind_sp (current_gdbarch,\r
337                                                         next_frame),\r
338                                      gdbarch_unwind_pc (current_gdbarch,\r
339                                                         next_frame)));\r
340 \r
341   /* FIXME: The SP isn't the frame base, so this is 0th approximation.  */\r
342   /* FIXME: The A3 reg isn't always the frame register either, so this\r
343      is 1st approximation.  */\r
344   trad_frame_set_this_base (cache, \r
345                             frame_unwind_register_signed (next_frame, \r
346                                                           E_A3_REGNUM));\r
347   (*this_prologue_cache) = cache;\r
348   return cache;\r
349 }\r
350 \r
351 /* Here is a dummy implementation.  */\r
352 static struct frame_id\r
353 mn10300_dummy_unwind_dummy_id (struct gdbarch *gdbarch,\r
354                                struct frame_info *next_frame)\r
355 {\r
356   return frame_id_build (0, 0);\r
357 }\r
358 \r
359 /* Trad frame implementation.  */\r
360 static void\r
361 mn10300_frame_this_id (struct frame_info *next_frame,\r
362                        void **this_prologue_cache,\r
363                        struct frame_id *this_id)\r
364 {\r
365   struct trad_frame_cache *cache = \r
366     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
367 \r
368   trad_frame_get_id (cache, this_id);\r
369 }\r
370 \r
371 static void\r
372 mn10300_frame_prev_register (struct frame_info *next_frame,\r
373                              void **this_prologue_cache,\r
374                              int regnum, int *optimizedp,\r
375                              enum lval_type *lvalp, CORE_ADDR *addrp,\r
376                              int *realnump, void *bufferp)\r
377 {\r
378   struct trad_frame_cache *cache =\r
379     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
380 \r
381   trad_frame_get_register (cache, next_frame, regnum, optimizedp, \r
382                            lvalp, addrp, realnump, bufferp);\r
383   /* Or...\r
384   trad_frame_get_prev_register (next_frame, cache->prev_regs, regnum, \r
385                            optimizedp, lvalp, addrp, realnump, bufferp);\r
386   */\r
387 }\r
388 \r
389 static const struct frame_unwind mn10300_frame_unwind = {\r
390   NORMAL_FRAME,\r
391   mn10300_frame_this_id, \r
392   mn10300_frame_prev_register\r
393 };\r
394 \r
395 static CORE_ADDR\r
396 mn10300_frame_base_address (struct frame_info *next_frame,\r
397                             void **this_prologue_cache)\r
398 {\r
399   struct trad_frame_cache *cache = \r
400     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
401 \r
402   return trad_frame_get_this_base (cache);\r
403 }\r
404 \r
405 static const struct frame_unwind *\r
406 mn10300_frame_sniffer (struct frame_info *next_frame)\r
407 {\r
408   return &mn10300_frame_unwind;\r
409 }\r
410 \r
411 static const struct frame_base mn10300_frame_base = {\r
412   &mn10300_frame_unwind, \r
413   mn10300_frame_base_address, \r
414   mn10300_frame_base_address,\r
415   mn10300_frame_base_address\r
416 };\r
417 \r
418 static CORE_ADDR\r
419 mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
420 {\r
421   ULONGEST pc;\r
422 \r
423   frame_unwind_unsigned_register (next_frame, E_PC_REGNUM, &pc);\r
424   return pc;\r
425 }\r
426 \r
427 static CORE_ADDR\r
428 mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
429 {\r
430   ULONGEST sp;\r
431 \r
432   frame_unwind_unsigned_register (next_frame, E_SP_REGNUM, &sp);\r
433   return sp;\r
434 }\r
435 \r
436 static void\r
437 mn10300_frame_unwind_init (struct gdbarch *gdbarch)\r
438 {\r
439   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);\r
440   frame_unwind_append_sniffer (gdbarch, mn10300_frame_sniffer);\r
441   frame_base_set_default (gdbarch, &mn10300_frame_base);\r
442   set_gdbarch_unwind_dummy_id (gdbarch, mn10300_dummy_unwind_dummy_id);\r
443   set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);\r
444   set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);\r
445 }\r
446 \r
447 /* Dump out the mn10300 specific architecture information. */\r
448 \r
449 static void\r
450 mn10300_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)\r
451 {\r
452   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);\r
453   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",\r
454                       tdep->am33_mode);\r
455 }\r
456 \r
457 static struct gdbarch *\r
458 mn10300_gdbarch_init (struct gdbarch_info info,\r
459                       struct gdbarch_list *arches)\r
460 {\r
461   struct gdbarch *gdbarch;\r
462   struct gdbarch_tdep *tdep;\r
463 \r
464   arches = gdbarch_list_lookup_by_info (arches, &info);\r
465   if (arches != NULL)\r
466     return arches->gdbarch;\r
467 \r
468   tdep = xmalloc (sizeof (struct gdbarch_tdep));\r
469   gdbarch = gdbarch_alloc (&info, tdep);\r
470 \r
471   switch (info.bfd_arch_info->mach)\r
472     {\r
473     case 0:\r
474     case bfd_mach_mn10300:\r
475       set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);\r
476       tdep->am33_mode = 0;\r
477       break;\r
478     case bfd_mach_am33:\r
479       set_gdbarch_register_name (gdbarch, am33_register_name);\r
480       tdep->am33_mode = 1;\r
481       break;\r
482     default:\r
483       internal_error (__FILE__, __LINE__,\r
484                       "mn10300_gdbarch_init: Unknown mn10300 variant");\r
485       break;\r
486     }\r
487 \r
488   /* Registers.  */\r
489   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);\r
490   set_gdbarch_register_type (gdbarch, mn10300_register_type);\r
491   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);\r
492   set_gdbarch_read_pc (gdbarch, mn10300_read_pc);\r
493   set_gdbarch_write_pc (gdbarch, mn10300_write_pc);\r
494   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);\r
495   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);\r
496 \r
497   /* Stack unwinding.  */\r
498   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);\r
499   /* Breakpoints.  */\r
500   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);\r
501   /* decr_pc_after_break? */\r
502   /* Disassembly.  */\r
503   set_gdbarch_print_insn (gdbarch, print_insn_mn10300);\r
504 \r
505   /* Stage 2 */\r
506   /* MVS Note: at least the first one is deprecated!  */\r
507   set_gdbarch_deprecated_use_struct_convention (gdbarch, \r
508                                                 mn10300_use_struct_convention);\r
509   set_gdbarch_store_return_value (gdbarch, mn10300_store_return_value);\r
510   set_gdbarch_extract_return_value (gdbarch, mn10300_extract_return_value);\r
511 \r
512   mn10300_frame_unwind_init (gdbarch);\r
513 \r
514   return gdbarch;\r
515 }\r
516  \r
517 void\r
518 _initialize_mn10300_tdep (void)\r
519 {\r
520   gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);\r
521 }\r
522 \r