2005-03-11 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 "gdbcore.h"    /* for write_memory_unsigned_integer */\r
59 #include "value.h"\r
60 #include "gdbtypes.h"\r
61 #include "frame.h"\r
62 #include "frame-unwind.h"\r
63 #include "frame-base.h"\r
64 #include "trad-frame.h"\r
65 #include "symtab.h"\r
66 #include "dwarf2-frame.h"\r
67 #include "regcache.h"\r
68 \r
69 #include "mn10300-tdep.h"\r
70 \r
71 \r
72 /* Compute the alignment required by a type.  */\r
73 \r
74 static int\r
75 mn10300_type_align (struct type *type)\r
76 {\r
77   int i, align = 1;\r
78 \r
79   switch (TYPE_CODE (type))\r
80     {\r
81     case TYPE_CODE_INT:\r
82     case TYPE_CODE_ENUM:\r
83     case TYPE_CODE_SET:\r
84     case TYPE_CODE_RANGE:\r
85     case TYPE_CODE_CHAR:\r
86     case TYPE_CODE_BOOL:\r
87     case TYPE_CODE_FLT:\r
88     case TYPE_CODE_PTR:\r
89     case TYPE_CODE_REF:\r
90       return TYPE_LENGTH (type);\r
91 \r
92     case TYPE_CODE_COMPLEX:\r
93       return TYPE_LENGTH (type) / 2;\r
94 \r
95     case TYPE_CODE_STRUCT:\r
96     case TYPE_CODE_UNION:\r
97       for (i = 0; i < TYPE_NFIELDS (type); i++)\r
98         {\r
99           int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));\r
100           while (align < falign)\r
101             align <<= 1;\r
102         }\r
103       return align;\r
104 \r
105     case TYPE_CODE_ARRAY:\r
106       /* HACK!  Structures containing arrays, even small ones, are not\r
107          elligible for returning in registers.  */\r
108       return 256;\r
109 \r
110     case TYPE_CODE_TYPEDEF:\r
111       return mn10300_type_align (check_typedef (type));\r
112 \r
113     default:\r
114       internal_error (__FILE__, __LINE__, _("bad switch"));\r
115     }\r
116 }\r
117 \r
118 /* MVS note this is deprecated.  */\r
119 /* Should call_function allocate stack space for a struct return?  */\r
120 /* gcc_p unused */\r
121 static int\r
122 mn10300_use_struct_convention (int gcc_p, struct type *type)\r
123 {\r
124   /* Structures bigger than a pair of words can't be returned in\r
125      registers.  */\r
126   if (TYPE_LENGTH (type) > 8)\r
127     return 1;\r
128 \r
129   switch (TYPE_CODE (type))\r
130     {\r
131     case TYPE_CODE_STRUCT:\r
132     case TYPE_CODE_UNION:\r
133       /* Structures with a single field are handled as the field\r
134          itself.  */\r
135       if (TYPE_NFIELDS (type) == 1)\r
136         return mn10300_use_struct_convention (gcc_p, \r
137                                               TYPE_FIELD_TYPE (type, 0));\r
138 \r
139       /* Structures with word or double-word size are passed in memory, as\r
140          long as they require at least word alignment.  */\r
141       if (mn10300_type_align (type) >= 4)\r
142         return 0;\r
143 \r
144       return 1;\r
145 \r
146       /* Arrays are addressable, so they're never returned in\r
147          registers.  This condition can only hold when the array is\r
148          the only field of a struct or union.  */\r
149     case TYPE_CODE_ARRAY:\r
150       return 1;\r
151 \r
152     case TYPE_CODE_TYPEDEF:\r
153       return mn10300_use_struct_convention (gcc_p, check_typedef (type));\r
154 \r
155     default:\r
156       return 0;\r
157     }\r
158 }\r
159 \r
160 /* MVS note this is deprecated.  */\r
161 static void\r
162 mn10300_store_return_value (struct type *type,\r
163                             struct regcache *regcache, const void *valbuf)\r
164 {\r
165   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
166   int len = TYPE_LENGTH (type);\r
167   int reg, regsz;\r
168   \r
169   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
170     reg = 4;\r
171   else\r
172     reg = 0;\r
173 \r
174   regsz = register_size (gdbarch, reg);\r
175 \r
176   if (len <= regsz)\r
177     regcache_raw_write_part (regcache, reg, 0, len, valbuf);\r
178   else if (len <= 2 * regsz)\r
179     {\r
180       regcache_raw_write (regcache, reg, valbuf);\r
181       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
182       regcache_raw_write_part (regcache, reg+1, 0,\r
183                                len - regsz, (char *) valbuf + regsz);\r
184     }\r
185   else\r
186     internal_error (__FILE__, __LINE__,\r
187                     _("Cannot store return value %d bytes long."), len);\r
188 }\r
189 \r
190 /* MVS note deprecated.  */\r
191 static void\r
192 mn10300_extract_return_value (struct type *type,\r
193                               struct regcache *regcache, void *valbuf)\r
194 {\r
195   struct gdbarch *gdbarch = get_regcache_arch (regcache);\r
196   char buf[MAX_REGISTER_SIZE];\r
197   int len = TYPE_LENGTH (type);\r
198   int reg, regsz;\r
199 \r
200   if (TYPE_CODE (type) == TYPE_CODE_PTR)\r
201     reg = 4;\r
202   else\r
203     reg = 0;\r
204 \r
205   regsz = register_size (gdbarch, reg);\r
206   if (len <= regsz)\r
207     {\r
208       regcache_raw_read (regcache, reg, buf);\r
209       memcpy (valbuf, buf, len);\r
210     }\r
211   else if (len <= 2 * regsz)\r
212     {\r
213       regcache_raw_read (regcache, reg, buf);\r
214       memcpy (valbuf, buf, regsz);\r
215       gdb_assert (regsz == register_size (gdbarch, reg + 1));\r
216       regcache_raw_read (regcache, reg + 1, buf);\r
217       memcpy ((char *) valbuf + regsz, buf, len - regsz);\r
218     }\r
219   else\r
220     internal_error (__FILE__, __LINE__,\r
221                     _("Cannot extract return value %d bytes long."), len);\r
222 }\r
223 \r
224 static char *\r
225 register_name (int reg, char **regs, long sizeof_regs)\r
226 {\r
227   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))\r
228     return NULL;\r
229   else\r
230     return regs[reg];\r
231 }\r
232 \r
233 static const char *\r
234 mn10300_generic_register_name (int reg)\r
235 {\r
236   static char *regs[] =\r
237   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
238     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",\r
239     "", "", "", "", "", "", "", "",\r
240     "", "", "", "", "", "", "", "fp"\r
241   };\r
242   return register_name (reg, regs, sizeof regs);\r
243 }\r
244 \r
245 \r
246 static const char *\r
247 am33_register_name (int reg)\r
248 {\r
249   static char *regs[] =\r
250   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",\r
251     "sp", "pc", "mdr", "psw", "lir", "lar", "",\r
252     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",\r
253     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""\r
254   };\r
255   return register_name (reg, regs, sizeof regs);\r
256 }\r
257 \r
258 \r
259 static struct type *\r
260 mn10300_register_type (struct gdbarch *gdbarch, int reg)\r
261 {\r
262   return builtin_type_int;\r
263 }\r
264 \r
265 static CORE_ADDR\r
266 mn10300_read_pc (ptid_t ptid)\r
267 {\r
268   return read_register_pid (E_PC_REGNUM, ptid);\r
269 }\r
270 \r
271 static void\r
272 mn10300_write_pc (CORE_ADDR val, ptid_t ptid)\r
273 {\r
274   return write_register_pid (E_PC_REGNUM, val, ptid);\r
275 }\r
276 \r
277 /* The breakpoint instruction must be the same size as the smallest\r
278    instruction in the instruction set.\r
279 \r
280    The Matsushita mn10x00 processors have single byte instructions\r
281    so we need a single byte breakpoint.  Matsushita hasn't defined\r
282    one, so we defined it ourselves.  */\r
283 \r
284 const static unsigned char *\r
285 mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)\r
286 {\r
287   static char breakpoint[] = {0xff};\r
288   *bp_size = 1;\r
289   return breakpoint;\r
290 }\r
291 \r
292 /* Function: skip_prologue\r
293    Return the address of the first inst past the prologue of the function.  */\r
294 \r
295 static CORE_ADDR\r
296 mn10300_skip_prologue (CORE_ADDR pc)\r
297 {\r
298   /* FIXME: not implemented.  */\r
299   /* First approximation, try simply using skip_prologue_using_sal.  */\r
300   return skip_prologue_using_sal (pc);\r
301 }\r
302 \r
303 /* Simple frame_unwind_cache.  \r
304    This finds the "extra info" for the frame.  */\r
305 struct trad_frame_cache *\r
306 mn10300_frame_unwind_cache (struct frame_info *next_frame,\r
307                             void **this_prologue_cache)\r
308 {\r
309   struct trad_frame_cache *cache;\r
310   CORE_ADDR pc;\r
311 \r
312   if (*this_prologue_cache)\r
313     return (*this_prologue_cache);\r
314 \r
315   cache = trad_frame_cache_zalloc (next_frame);\r
316   pc = gdbarch_unwind_pc (current_gdbarch, next_frame);\r
317   mn10300_analyze_prologue (next_frame, (void **) &cache, pc);\r
318 \r
319   trad_frame_set_id (cache, \r
320                      frame_id_build (trad_frame_get_this_base (cache), pc));\r
321 \r
322   (*this_prologue_cache) = cache;\r
323   return cache;\r
324 }\r
325 \r
326 /* Here is a dummy implementation.  */\r
327 static struct frame_id\r
328 mn10300_unwind_dummy_id (struct gdbarch *gdbarch,\r
329                          struct frame_info *next_frame)\r
330 {\r
331   return frame_id_build (frame_sp_unwind (next_frame), \r
332                          frame_pc_unwind (next_frame));\r
333 }\r
334 \r
335 /* Trad frame implementation.  */\r
336 static void\r
337 mn10300_frame_this_id (struct frame_info *next_frame,\r
338                        void **this_prologue_cache,\r
339                        struct frame_id *this_id)\r
340 {\r
341   struct trad_frame_cache *cache = \r
342     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
343 \r
344   trad_frame_get_id (cache, this_id);\r
345 }\r
346 \r
347 static void\r
348 mn10300_frame_prev_register (struct frame_info *next_frame,\r
349                              void **this_prologue_cache,\r
350                              int regnum, int *optimizedp,\r
351                              enum lval_type *lvalp, CORE_ADDR *addrp,\r
352                              int *realnump, void *bufferp)\r
353 {\r
354   struct trad_frame_cache *cache =\r
355     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
356 \r
357   trad_frame_get_register (cache, next_frame, regnum, optimizedp, \r
358                            lvalp, addrp, realnump, bufferp);\r
359   /* Or...\r
360   trad_frame_get_prev_register (next_frame, cache->prev_regs, regnum, \r
361                            optimizedp, lvalp, addrp, realnump, bufferp);\r
362   */\r
363 }\r
364 \r
365 static const struct frame_unwind mn10300_frame_unwind = {\r
366   NORMAL_FRAME,\r
367   mn10300_frame_this_id, \r
368   mn10300_frame_prev_register\r
369 };\r
370 \r
371 static CORE_ADDR\r
372 mn10300_frame_base_address (struct frame_info *next_frame,\r
373                             void **this_prologue_cache)\r
374 {\r
375   struct trad_frame_cache *cache = \r
376     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);\r
377 \r
378   return trad_frame_get_this_base (cache);\r
379 }\r
380 \r
381 static const struct frame_unwind *\r
382 mn10300_frame_sniffer (struct frame_info *next_frame)\r
383 {\r
384   return &mn10300_frame_unwind;\r
385 }\r
386 \r
387 static const struct frame_base mn10300_frame_base = {\r
388   &mn10300_frame_unwind, \r
389   mn10300_frame_base_address, \r
390   mn10300_frame_base_address,\r
391   mn10300_frame_base_address\r
392 };\r
393 \r
394 static CORE_ADDR\r
395 mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
396 {\r
397   ULONGEST pc;\r
398 \r
399   frame_unwind_unsigned_register (next_frame, E_PC_REGNUM, &pc);\r
400   return pc;\r
401 }\r
402 \r
403 static CORE_ADDR\r
404 mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)\r
405 {\r
406   ULONGEST sp;\r
407 \r
408   frame_unwind_unsigned_register (next_frame, E_SP_REGNUM, &sp);\r
409   return sp;\r
410 }\r
411 \r
412 static void\r
413 mn10300_frame_unwind_init (struct gdbarch *gdbarch)\r
414 {\r
415   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);\r
416   frame_unwind_append_sniffer (gdbarch, mn10300_frame_sniffer);\r
417   frame_base_set_default (gdbarch, &mn10300_frame_base);\r
418   set_gdbarch_unwind_dummy_id (gdbarch, mn10300_unwind_dummy_id);\r
419   set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);\r
420   set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);\r
421 }\r
422 \r
423 /* Function: push_dummy_call\r
424  *\r
425  * Set up machine state for a target call, including\r
426  * function arguments, stack, return address, etc.\r
427  *\r
428  */\r
429 \r
430 static CORE_ADDR\r
431 mn10300_push_dummy_call (struct gdbarch *gdbarch, \r
432                          struct value *target_func,\r
433                          struct regcache *regcache,\r
434                          CORE_ADDR bp_addr, \r
435                          int nargs, struct value **args,\r
436                          CORE_ADDR sp, \r
437                          int struct_return,\r
438                          CORE_ADDR struct_addr)\r
439 {\r
440   const int push_size = register_size (gdbarch, E_PC_REGNUM);\r
441   int regs_used = struct_return ? 1 : 0;\r
442   int len, arg_len; \r
443   int stack_offset = 0;\r
444   int argnum;\r
445   char *val;\r
446 \r
447   /* FIXME temp, don't handle struct args at all.  */\r
448   if (struct_return)\r
449     error ("Target doesn't handle struct return");\r
450 \r
451   /* This should be a nop, but align the stack just in case something\r
452      went wrong.  Stacks are four byte aligned on the mn10300.  */\r
453   sp &= ~3;\r
454 \r
455   /* Now make space on the stack for the args.\r
456 \r
457      XXX This doesn't appear to handle pass-by-invisible reference\r
458      arguments.  */\r
459   for (len = 0, argnum = 0; argnum < nargs; argnum++)\r
460     {\r
461       arg_len = (TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3;\r
462       if (TYPE_CODE (value_type (args[argnum])) == TYPE_CODE_STRUCT)\r
463         error ("Target does not handle struct args");\r
464       while (regs_used < 2 && arg_len > 0)\r
465         {\r
466           regs_used++;\r
467           arg_len -= push_size;\r
468         }\r
469       len += arg_len;\r
470     }\r
471 \r
472   /* Allocate stack space.  */\r
473   sp -= len;\r
474 \r
475   regs_used = struct_return ? 1 : 0;\r
476   /* Push all arguments onto the stack. */\r
477   for (argnum = 0; argnum < nargs; argnum++)\r
478     {\r
479       /* FIXME what about structs?  */\r
480       arg_len = TYPE_LENGTH (value_type (*args));\r
481       val = (char *) value_contents (*args);\r
482 \r
483       while (regs_used < 2 && arg_len > 0)\r
484         {\r
485           write_register (regs_used, extract_unsigned_integer (val, \r
486                                                                push_size));\r
487           val += push_size;\r
488           arg_len -= push_size;\r
489           regs_used++;\r
490         }\r
491 \r
492       while (arg_len > 0)\r
493         {\r
494           write_memory (sp + stack_offset, val, push_size);\r
495           arg_len -= push_size;\r
496           val += push_size;\r
497           stack_offset += push_size;\r
498         }\r
499 \r
500       args++;\r
501     }\r
502 \r
503   /* Make space for the flushback area.  */\r
504   sp -= 8;\r
505 \r
506   /* Push the return address that contains the magic breakpoint.  */\r
507   sp -= 4;\r
508   write_memory_unsigned_integer (sp, push_size, bp_addr);\r
509   /* Update $sp.  */\r
510   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);\r
511   return sp;\r
512 }\r
513 \r
514 \r
515 static struct gdbarch *\r
516 mn10300_gdbarch_init (struct gdbarch_info info,\r
517                       struct gdbarch_list *arches)\r
518 {\r
519   struct gdbarch *gdbarch;\r
520   struct gdbarch_tdep *tdep;\r
521 \r
522   arches = gdbarch_list_lookup_by_info (arches, &info);\r
523   if (arches != NULL)\r
524     return arches->gdbarch;\r
525 \r
526   tdep = xmalloc (sizeof (struct gdbarch_tdep));\r
527   gdbarch = gdbarch_alloc (&info, tdep);\r
528 \r
529   switch (info.bfd_arch_info->mach)\r
530     {\r
531     case 0:\r
532     case bfd_mach_mn10300:\r
533       set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);\r
534       tdep->am33_mode = 0;\r
535       break;\r
536     case bfd_mach_am33:\r
537       set_gdbarch_register_name (gdbarch, am33_register_name);\r
538       tdep->am33_mode = 1;\r
539       break;\r
540     default:\r
541       internal_error (__FILE__, __LINE__,\r
542                       _("mn10300_gdbarch_init: Unknown mn10300 variant"));\r
543       break;\r
544     }\r
545 \r
546   /* Registers.  */\r
547   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);\r
548   set_gdbarch_register_type (gdbarch, mn10300_register_type);\r
549   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);\r
550   set_gdbarch_read_pc (gdbarch, mn10300_read_pc);\r
551   set_gdbarch_write_pc (gdbarch, mn10300_write_pc);\r
552   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);\r
553   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);\r
554 \r
555   /* Stack unwinding.  */\r
556   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);\r
557   /* Breakpoints.  */\r
558   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);\r
559   /* decr_pc_after_break? */\r
560   /* Disassembly.  */\r
561   set_gdbarch_print_insn (gdbarch, print_insn_mn10300);\r
562 \r
563   /* Stage 2 */\r
564   /* MVS Note: at least the first one is deprecated!  */\r
565   set_gdbarch_deprecated_use_struct_convention (gdbarch, \r
566                                                 mn10300_use_struct_convention);\r
567   set_gdbarch_store_return_value (gdbarch, mn10300_store_return_value);\r
568   set_gdbarch_extract_return_value (gdbarch, mn10300_extract_return_value);\r
569   \r
570   /* Stage 3 -- get target calls working.  */\r
571   set_gdbarch_push_dummy_call (gdbarch, mn10300_push_dummy_call);\r
572   /* set_gdbarch_return_value (store, extract) */\r
573 \r
574 \r
575   mn10300_frame_unwind_init (gdbarch);\r
576 \r
577   return gdbarch;\r
578 }\r
579  \r
580 /* Dump out the mn10300 specific architecture information. */\r
581 \r
582 static void\r
583 mn10300_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)\r
584 {\r
585   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);\r
586   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",\r
587                       tdep->am33_mode);\r
588 }\r
589 \r
590 void\r
591 _initialize_mn10300_tdep (void)\r
592 {\r
593   gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);\r
594 }\r
595 \r