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