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