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