* stack.c (print_frame_args): Fix typos in comments.
[deliverable/binutils-gdb.git] / gdb / m32r-tdep.c
CommitLineData
d95a8903
AC
1/* Target-dependent code for Renesas M32R, for GDB.
2
9b254dd1
DJ
3 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008 Free Software Foundation, Inc.
d95a8903
AC
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
d95a8903
AC
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d95a8903
AC
20
21#include "defs.h"
22#include "frame.h"
23#include "frame-unwind.h"
24#include "frame-base.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
29#include "gdb_string.h"
30#include "value.h"
31#include "inferior.h"
32#include "symfile.h"
33#include "objfiles.h"
c46b0409 34#include "osabi.h"
d95a8903
AC
35#include "language.h"
36#include "arch-utils.h"
37#include "regcache.h"
38#include "trad-frame.h"
73e8eb51 39#include "dis-asm.h"
d95a8903
AC
40
41#include "gdb_assert.h"
42
9b32d526 43#include "m32r-tdep.h"
d95a8903
AC
44
45/* Local functions */
46
47extern void _initialize_m32r_tdep (void);
48
49static CORE_ADDR
50m32r_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
51{
52 /* Align to the size of an instruction (so that they can safely be
53 pushed onto the stack. */
54 return sp & ~3;
55}
56
d95a8903 57
9f0b0322
KI
58/* Breakpoints
59
60 The little endian mode of M32R is unique. In most of architectures,
61 two 16-bit instructions, A and B, are placed as the following:
62
63 Big endian:
64 A0 A1 B0 B1
65
66 Little endian:
67 A1 A0 B1 B0
68
69 In M32R, they are placed like this:
70
71 Big endian:
72 A0 A1 B0 B1
73
74 Little endian:
75 B1 B0 A1 A0
76
77 This is because M32R always fetches instructions in 32-bit.
78
79 The following functions take care of this behavior. */
d95a8903
AC
80
81static int
ae4b2284
MD
82m32r_memory_insert_breakpoint (struct gdbarch *gdbarch,
83 struct bp_target_info *bp_tgt)
d95a8903 84{
8181d85f 85 CORE_ADDR addr = bp_tgt->placed_address;
d95a8903 86 int val;
16ac4ab5 87 gdb_byte buf[4];
8181d85f 88 gdb_byte *contents_cache = bp_tgt->shadow_contents;
16ac4ab5 89 gdb_byte bp_entry[] = { 0x10, 0xf1 }; /* dpt */
d95a8903
AC
90
91 /* Save the memory contents. */
9f0b0322 92 val = target_read_memory (addr & 0xfffffffc, contents_cache, 4);
d95a8903
AC
93 if (val != 0)
94 return val; /* return error */
95
8181d85f
DJ
96 bp_tgt->placed_size = bp_tgt->shadow_len = 4;
97
d95a8903 98 /* Determine appropriate breakpoint contents and size for this address. */
ae4b2284 99 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
d95a8903 100 {
9f0b0322 101 if ((addr & 3) == 0)
d95a8903 102 {
9f0b0322
KI
103 buf[0] = bp_entry[0];
104 buf[1] = bp_entry[1];
105 buf[2] = contents_cache[2] & 0x7f;
106 buf[3] = contents_cache[3];
d95a8903
AC
107 }
108 else
109 {
9f0b0322
KI
110 buf[0] = contents_cache[0];
111 buf[1] = contents_cache[1];
112 buf[2] = bp_entry[0];
113 buf[3] = bp_entry[1];
d95a8903
AC
114 }
115 }
9f0b0322
KI
116 else /* little-endian */
117 {
118 if ((addr & 3) == 0)
d95a8903 119 {
9f0b0322
KI
120 buf[0] = contents_cache[0];
121 buf[1] = contents_cache[1] & 0x7f;
122 buf[2] = bp_entry[1];
123 buf[3] = bp_entry[0];
d95a8903
AC
124 }
125 else
126 {
9f0b0322
KI
127 buf[0] = bp_entry[1];
128 buf[1] = bp_entry[0];
129 buf[2] = contents_cache[2];
130 buf[3] = contents_cache[3];
d95a8903
AC
131 }
132 }
133
134 /* Write the breakpoint. */
9f0b0322 135 val = target_write_memory (addr & 0xfffffffc, buf, 4);
d95a8903
AC
136 return val;
137}
138
139static int
ae4b2284
MD
140m32r_memory_remove_breakpoint (struct gdbarch *gdbarch,
141 struct bp_target_info *bp_tgt)
d95a8903 142{
8181d85f 143 CORE_ADDR addr = bp_tgt->placed_address;
d95a8903 144 int val;
16ac4ab5 145 gdb_byte buf[4];
8181d85f 146 gdb_byte *contents_cache = bp_tgt->shadow_contents;
d95a8903 147
9f0b0322
KI
148 buf[0] = contents_cache[0];
149 buf[1] = contents_cache[1];
150 buf[2] = contents_cache[2];
151 buf[3] = contents_cache[3];
152
153 /* Remove parallel bit. */
ae4b2284 154 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
d95a8903 155 {
9f0b0322
KI
156 if ((buf[0] & 0x80) == 0 && (buf[2] & 0x80) != 0)
157 buf[2] &= 0x7f;
d95a8903 158 }
9f0b0322 159 else /* little-endian */
d95a8903 160 {
9f0b0322
KI
161 if ((buf[3] & 0x80) == 0 && (buf[1] & 0x80) != 0)
162 buf[1] &= 0x7f;
d95a8903
AC
163 }
164
165 /* Write contents. */
9f0b0322 166 val = target_write_memory (addr & 0xfffffffc, buf, 4);
d95a8903
AC
167 return val;
168}
169
16ac4ab5 170static const gdb_byte *
67d57894 171m32r_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
d95a8903 172{
16ac4ab5
KI
173 static gdb_byte be_bp_entry[] = { 0x10, 0xf1, 0x70, 0x00 }; /* dpt -> nop */
174 static gdb_byte le_bp_entry[] = { 0x00, 0x70, 0xf1, 0x10 }; /* dpt -> nop */
175 gdb_byte *bp;
d95a8903
AC
176
177 /* Determine appropriate breakpoint. */
67d57894 178 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
d95a8903
AC
179 {
180 if ((*pcptr & 3) == 0)
181 {
9f0b0322
KI
182 bp = be_bp_entry;
183 *lenptr = 4;
d95a8903
AC
184 }
185 else
186 {
9f0b0322
KI
187 bp = be_bp_entry;
188 *lenptr = 2;
d95a8903
AC
189 }
190 }
191 else
192 {
193 if ((*pcptr & 3) == 0)
194 {
9f0b0322
KI
195 bp = le_bp_entry;
196 *lenptr = 4;
d95a8903
AC
197 }
198 else
199 {
9f0b0322
KI
200 bp = le_bp_entry + 2;
201 *lenptr = 2;
d95a8903
AC
202 }
203 }
204
205 return bp;
206}
207
208
209char *m32r_register_names[] = {
210 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
211 "r8", "r9", "r10", "r11", "r12", "fp", "lr", "sp",
212 "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
213 "evb"
214};
215
d95a8903 216static const char *
d93859e2 217m32r_register_name (struct gdbarch *gdbarch, int reg_nr)
d95a8903
AC
218{
219 if (reg_nr < 0)
220 return NULL;
9b32d526 221 if (reg_nr >= M32R_NUM_REGS)
d95a8903
AC
222 return NULL;
223 return m32r_register_names[reg_nr];
224}
225
226
227/* Return the GDB type object for the "standard" data type
228 of data in register N. */
229
230static struct type *
231m32r_register_type (struct gdbarch *gdbarch, int reg_nr)
232{
233 if (reg_nr == M32R_PC_REGNUM)
0dfff4cb 234 return builtin_type (gdbarch)->builtin_func_ptr;
d95a8903 235 else if (reg_nr == M32R_SP_REGNUM || reg_nr == M32R_FP_REGNUM)
0dfff4cb 236 return builtin_type (gdbarch)->builtin_data_ptr;
d95a8903
AC
237 else
238 return builtin_type_int32;
239}
240
241
242/* Write into appropriate registers a function return value
243 of type TYPE, given in virtual format.
244
245 Things always get returned in RET1_REGNUM, RET2_REGNUM. */
246
247static void
248m32r_store_return_value (struct type *type, struct regcache *regcache,
249 const void *valbuf)
250{
251 CORE_ADDR regval;
252 int len = TYPE_LENGTH (type);
253
254 regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len);
255 regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
256
257 if (len > 4)
258 {
16ac4ab5 259 regval = extract_unsigned_integer ((gdb_byte *) valbuf + 4, len - 4);
d95a8903
AC
260 regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
261 }
262}
263
d95a8903
AC
264/* This is required by skip_prologue. The results of decoding a prologue
265 should be cached because this thrashing is getting nuts. */
266
cea15572 267static int
d95a8903 268decode_prologue (CORE_ADDR start_pc, CORE_ADDR scan_limit,
cea15572 269 CORE_ADDR *pl_endptr, unsigned long *framelength)
d95a8903
AC
270{
271 unsigned long framesize;
272 int insn;
273 int op1;
d95a8903 274 CORE_ADDR after_prologue = 0;
cea15572 275 CORE_ADDR after_push = 0;
d95a8903
AC
276 CORE_ADDR after_stack_adjust = 0;
277 CORE_ADDR current_pc;
cea15572 278 LONGEST return_value;
d95a8903
AC
279
280 framesize = 0;
281 after_prologue = 0;
282
283 for (current_pc = start_pc; current_pc < scan_limit; current_pc += 2)
284 {
cea15572
KI
285 /* Check if current pc's location is readable. */
286 if (!safe_read_memory_integer (current_pc, 2, &return_value))
287 return -1;
288
d95a8903
AC
289 insn = read_memory_unsigned_integer (current_pc, 2);
290
cea15572
KI
291 if (insn == 0x0000)
292 break;
293
d95a8903
AC
294 /* If this is a 32 bit instruction, we dont want to examine its
295 immediate data as though it were an instruction */
296 if (current_pc & 0x02)
297 {
d95a8903
AC
298 /* decode this instruction further */
299 insn &= 0x7fff;
300 }
301 else
302 {
d95a8903
AC
303 if (insn & 0x8000)
304 {
305 if (current_pc == scan_limit)
306 scan_limit += 2; /* extend the search */
cea15572 307
d95a8903 308 current_pc += 2; /* skip the immediate data */
cea15572
KI
309
310 /* Check if current pc's location is readable. */
311 if (!safe_read_memory_integer (current_pc, 2, &return_value))
312 return -1;
313
d95a8903
AC
314 if (insn == 0x8faf) /* add3 sp, sp, xxxx */
315 /* add 16 bit sign-extended offset */
316 {
317 framesize +=
318 -((short) read_memory_unsigned_integer (current_pc, 2));
319 }
320 else
321 {
7e3dd49e 322 if (((insn >> 8) == 0xe4) /* ld24 r4, xxxxxx; sub sp, r4 */
cea15572
KI
323 && safe_read_memory_integer (current_pc + 2, 2,
324 &return_value)
7e3dd49e
AC
325 && read_memory_unsigned_integer (current_pc + 2,
326 2) == 0x0f24)
d95a8903
AC
327 /* subtract 24 bit sign-extended negative-offset */
328 {
329 insn = read_memory_unsigned_integer (current_pc - 2, 4);
330 if (insn & 0x00800000) /* sign extend */
331 insn |= 0xff000000; /* negative */
332 else
333 insn &= 0x00ffffff; /* positive */
334 framesize += insn;
335 }
336 }
cea15572 337 after_push = current_pc + 2;
d95a8903
AC
338 continue;
339 }
340 }
341 op1 = insn & 0xf000; /* isolate just the first nibble */
342
343 if ((insn & 0xf0ff) == 0x207f)
344 { /* st reg, @-sp */
345 int regno;
346 framesize += 4;
347 regno = ((insn >> 8) & 0xf);
348 after_prologue = 0;
349 continue;
350 }
351 if ((insn >> 8) == 0x4f) /* addi sp, xx */
352 /* add 8 bit sign-extended offset */
353 {
9ffbf372 354 int stack_adjust = (signed char) (insn & 0xff);
d95a8903
AC
355
356 /* there are probably two of these stack adjustments:
357 1) A negative one in the prologue, and
358 2) A positive one in the epilogue.
359 We are only interested in the first one. */
360
361 if (stack_adjust < 0)
362 {
363 framesize -= stack_adjust;
364 after_prologue = 0;
365 /* A frameless function may have no "mv fp, sp".
366 In that case, this is the end of the prologue. */
367 after_stack_adjust = current_pc + 2;
368 }
369 continue;
370 }
371 if (insn == 0x1d8f)
372 { /* mv fp, sp */
373 after_prologue = current_pc + 2;
374 break; /* end of stack adjustments */
375 }
cea15572 376
d95a8903
AC
377 /* Nop looks like a branch, continue explicitly */
378 if (insn == 0x7000)
379 {
380 after_prologue = current_pc + 2;
381 continue; /* nop occurs between pushes */
382 }
cea15572
KI
383 /* End of prolog if any of these are trap instructions */
384 if ((insn & 0xfff0) == 0x10f0)
385 {
386 after_prologue = current_pc;
387 break;
388 }
d95a8903
AC
389 /* End of prolog if any of these are branch instructions */
390 if ((op1 == 0x7000) || (op1 == 0xb000) || (op1 == 0xf000))
391 {
392 after_prologue = current_pc;
d95a8903
AC
393 continue;
394 }
395 /* Some of the branch instructions are mixed with other types */
396 if (op1 == 0x1000)
397 {
398 int subop = insn & 0x0ff0;
399 if ((subop == 0x0ec0) || (subop == 0x0fc0))
400 {
401 after_prologue = current_pc;
d95a8903
AC
402 continue; /* jmp , jl */
403 }
404 }
405 }
406
cea15572
KI
407 if (framelength)
408 *framelength = framesize;
409
d95a8903
AC
410 if (current_pc >= scan_limit)
411 {
412 if (pl_endptr)
413 {
414 if (after_stack_adjust != 0)
415 /* We did not find a "mv fp,sp", but we DID find
416 a stack_adjust. Is it safe to use that as the
417 end of the prologue? I just don't know. */
418 {
419 *pl_endptr = after_stack_adjust;
420 }
cea15572
KI
421 else if (after_push != 0)
422 /* We did not find a "mv fp,sp", but we DID find
423 a push. Is it safe to use that as the
424 end of the prologue? I just don't know. */
425 {
426 *pl_endptr = after_push;
427 }
d95a8903
AC
428 else
429 /* We reached the end of the loop without finding the end
430 of the prologue. No way to win -- we should report failure.
431 The way we do that is to return the original start_pc.
432 GDB will set a breakpoint at the start of the function (etc.) */
433 *pl_endptr = start_pc;
434 }
cea15572 435 return 0;
d95a8903 436 }
cea15572 437
d95a8903
AC
438 if (after_prologue == 0)
439 after_prologue = current_pc;
440
441 if (pl_endptr)
442 *pl_endptr = after_prologue;
cea15572
KI
443
444 return 0;
d95a8903
AC
445} /* decode_prologue */
446
447/* Function: skip_prologue
448 Find end of function prologue */
449
cea15572 450#define DEFAULT_SEARCH_LIMIT 128
d95a8903
AC
451
452CORE_ADDR
6093d2eb 453m32r_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
d95a8903
AC
454{
455 CORE_ADDR func_addr, func_end;
456 struct symtab_and_line sal;
cea15572 457 LONGEST return_value;
d95a8903
AC
458
459 /* See what the symbol table says */
460
461 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
462 {
463 sal = find_pc_line (func_addr, 0);
464
465 if (sal.line != 0 && sal.end <= func_end)
466 {
467 func_end = sal.end;
468 }
469 else
470 /* Either there's no line info, or the line after the prologue is after
471 the end of the function. In this case, there probably isn't a
472 prologue. */
473 {
474 func_end = min (func_end, func_addr + DEFAULT_SEARCH_LIMIT);
475 }
476 }
477 else
478 func_end = pc + DEFAULT_SEARCH_LIMIT;
cea15572
KI
479
480 /* If pc's location is not readable, just quit. */
481 if (!safe_read_memory_integer (pc, 4, &return_value))
482 return pc;
483
484 /* Find the end of prologue. */
485 if (decode_prologue (pc, func_end, &sal.end, NULL) < 0)
486 return pc;
487
d95a8903
AC
488 return sal.end;
489}
490
d95a8903
AC
491struct m32r_unwind_cache
492{
493 /* The previous frame's inner most stack address. Used as this
494 frame ID's stack_addr. */
495 CORE_ADDR prev_sp;
496 /* The frame's base, optionally used by the high-level debug info. */
497 CORE_ADDR base;
498 int size;
499 /* How far the SP and r13 (FP) have been offset from the start of
500 the stack frame (as defined by the previous frame's stack
501 pointer). */
502 LONGEST sp_offset;
503 LONGEST r13_offset;
504 int uses_frame;
505 /* Table indicating the location of each and every register. */
506 struct trad_frame_saved_reg *saved_regs;
507};
508
509/* Put here the code to store, into fi->saved_regs, the addresses of
510 the saved registers of frame described by FRAME_INFO. This
511 includes special registers such as pc and fp saved in special ways
512 in the stack frame. sp is even more special: the address we return
513 for it IS the sp for the next frame. */
514
515static struct m32r_unwind_cache *
94afd7a6 516m32r_frame_unwind_cache (struct frame_info *this_frame,
d95a8903
AC
517 void **this_prologue_cache)
518{
cea15572 519 CORE_ADDR pc, scan_limit;
d95a8903
AC
520 ULONGEST prev_sp;
521 ULONGEST this_base;
cea15572 522 unsigned long op, op2;
d95a8903
AC
523 int i;
524 struct m32r_unwind_cache *info;
525
cea15572 526
d95a8903
AC
527 if ((*this_prologue_cache))
528 return (*this_prologue_cache);
529
530 info = FRAME_OBSTACK_ZALLOC (struct m32r_unwind_cache);
531 (*this_prologue_cache) = info;
94afd7a6 532 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
d95a8903
AC
533
534 info->size = 0;
535 info->sp_offset = 0;
d95a8903 536 info->uses_frame = 0;
cea15572 537
94afd7a6
UW
538 scan_limit = get_frame_pc (this_frame);
539 for (pc = get_frame_func (this_frame);
cea15572 540 pc > 0 && pc < scan_limit; pc += 2)
d95a8903
AC
541 {
542 if ((pc & 2) == 0)
543 {
94afd7a6 544 op = get_frame_memory_unsigned (this_frame, pc, 4);
d95a8903
AC
545 if ((op & 0x80000000) == 0x80000000)
546 {
547 /* 32-bit instruction */
548 if ((op & 0xffff0000) == 0x8faf0000)
549 {
550 /* add3 sp,sp,xxxx */
551 short n = op & 0xffff;
552 info->sp_offset += n;
553 }
cea15572 554 else if (((op >> 8) == 0xe4)
94afd7a6 555 && get_frame_memory_unsigned (this_frame, pc + 2,
7e3dd49e 556 2) == 0x0f24)
d95a8903 557 {
cea15572 558 /* ld24 r4, xxxxxx; sub sp, r4 */
d95a8903
AC
559 unsigned long n = op & 0xffffff;
560 info->sp_offset += n;
cea15572 561 pc += 2; /* skip sub instruction */
d95a8903 562 }
d95a8903 563
cea15572
KI
564 if (pc == scan_limit)
565 scan_limit += 2; /* extend the search */
566 pc += 2; /* skip the immediate data */
d95a8903
AC
567 continue;
568 }
569 }
570
571 /* 16-bit instructions */
94afd7a6 572 op = get_frame_memory_unsigned (this_frame, pc, 2) & 0x7fff;
d95a8903
AC
573 if ((op & 0xf0ff) == 0x207f)
574 {
575 /* st rn, @-sp */
576 int regno = ((op >> 8) & 0xf);
577 info->sp_offset -= 4;
578 info->saved_regs[regno].addr = info->sp_offset;
579 }
580 else if ((op & 0xff00) == 0x4f00)
581 {
582 /* addi sp, xx */
9ffbf372 583 int n = (signed char) (op & 0xff);
d95a8903
AC
584 info->sp_offset += n;
585 }
586 else if (op == 0x1d8f)
587 {
588 /* mv fp, sp */
589 info->uses_frame = 1;
590 info->r13_offset = info->sp_offset;
cea15572
KI
591 break; /* end of stack adjustments */
592 }
593 else if ((op & 0xfff0) == 0x10f0)
594 {
595 /* end of prologue if this is a trap instruction */
596 break; /* end of stack adjustments */
d95a8903 597 }
d95a8903
AC
598 }
599
600 info->size = -info->sp_offset;
601
602 /* Compute the previous frame's stack pointer (which is also the
603 frame's ID's stack address), and this frame's base pointer. */
604 if (info->uses_frame)
605 {
606 /* The SP was moved to the FP. This indicates that a new frame
607 was created. Get THIS frame's FP value by unwinding it from
608 the next frame. */
94afd7a6 609 this_base = get_frame_register_unsigned (this_frame, M32R_FP_REGNUM);
d95a8903
AC
610 /* The FP points at the last saved register. Adjust the FP back
611 to before the first saved register giving the SP. */
612 prev_sp = this_base + info->size;
613 }
614 else
615 {
616 /* Assume that the FP is this frame's SP but with that pushed
617 stack space added back. */
94afd7a6 618 this_base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
d95a8903
AC
619 prev_sp = this_base + info->size;
620 }
621
622 /* Convert that SP/BASE into real addresses. */
623 info->prev_sp = prev_sp;
624 info->base = this_base;
625
626 /* Adjust all the saved registers so that they contain addresses and
627 not offsets. */
94afd7a6 628 for (i = 0; i < gdbarch_num_regs (get_frame_arch (this_frame)) - 1; i++)
d95a8903
AC
629 if (trad_frame_addr_p (info->saved_regs, i))
630 info->saved_regs[i].addr = (info->prev_sp + info->saved_regs[i].addr);
631
632 /* The call instruction moves the caller's PC in the callee's LR.
633 Since this is an unwind, do the reverse. Copy the location of LR
634 into PC (the address / regnum) so that a request for PC will be
635 converted into a request for the LR. */
636 info->saved_regs[M32R_PC_REGNUM] = info->saved_regs[LR_REGNUM];
637
638 /* The previous frame's SP needed to be computed. Save the computed
639 value. */
640 trad_frame_set_value (info->saved_regs, M32R_SP_REGNUM, prev_sp);
641
642 return info;
643}
644
645static CORE_ADDR
61a1198a 646m32r_read_pc (struct regcache *regcache)
d95a8903 647{
7e3dd49e 648 ULONGEST pc;
61a1198a 649 regcache_cooked_read_unsigned (regcache, M32R_PC_REGNUM, &pc);
d95a8903
AC
650 return pc;
651}
652
653static void
61a1198a 654m32r_write_pc (struct regcache *regcache, CORE_ADDR val)
d95a8903 655{
61a1198a 656 regcache_cooked_write_unsigned (regcache, M32R_PC_REGNUM, val);
d95a8903
AC
657}
658
659static CORE_ADDR
660m32r_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
661{
7e3dd49e 662 return frame_unwind_register_unsigned (next_frame, M32R_SP_REGNUM);
d95a8903
AC
663}
664
665
666static CORE_ADDR
7d9b040b 667m32r_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
d95a8903
AC
668 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
669 struct value **args, CORE_ADDR sp, int struct_return,
670 CORE_ADDR struct_addr)
671{
672 int stack_offset, stack_alloc;
673 int argreg = ARG1_REGNUM;
674 int argnum;
675 struct type *type;
676 enum type_code typecode;
677 CORE_ADDR regval;
16ac4ab5
KI
678 gdb_byte *val;
679 gdb_byte valbuf[MAX_REGISTER_SIZE];
d95a8903
AC
680 int len;
681 int odd_sized_struct;
682
683 /* first force sp to a 4-byte alignment */
684 sp = sp & ~3;
685
686 /* Set the return address. For the m32r, the return breakpoint is
687 always at BP_ADDR. */
688 regcache_cooked_write_unsigned (regcache, LR_REGNUM, bp_addr);
689
690 /* If STRUCT_RETURN is true, then the struct return address (in
691 STRUCT_ADDR) will consume the first argument-passing register.
692 Both adjust the register count and store that value. */
693 if (struct_return)
694 {
695 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
696 argreg++;
697 }
698
699 /* Now make sure there's space on the stack */
700 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
4991999e 701 stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
d95a8903
AC
702 sp -= stack_alloc; /* make room on stack for args */
703
704 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
705 {
4991999e 706 type = value_type (args[argnum]);
d95a8903
AC
707 typecode = TYPE_CODE (type);
708 len = TYPE_LENGTH (type);
709
710 memset (valbuf, 0, sizeof (valbuf));
711
712 /* Passes structures that do not fit in 2 registers by reference. */
713 if (len > 8
714 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
715 {
716 store_unsigned_integer (valbuf, 4, VALUE_ADDRESS (args[argnum]));
717 typecode = TYPE_CODE_PTR;
718 len = 4;
719 val = valbuf;
720 }
721 else if (len < 4)
722 {
723 /* value gets right-justified in the register or stack word */
7e3dd49e 724 memcpy (valbuf + (register_size (gdbarch, argreg) - len),
16ac4ab5 725 (gdb_byte *) value_contents (args[argnum]), len);
d95a8903
AC
726 val = valbuf;
727 }
728 else
16ac4ab5 729 val = (gdb_byte *) value_contents (args[argnum]);
d95a8903
AC
730
731 while (len > 0)
732 {
733 if (argreg > ARGN_REGNUM)
734 {
735 /* must go on the stack */
736 write_memory (sp + stack_offset, val, 4);
737 stack_offset += 4;
738 }
739 else if (argreg <= ARGN_REGNUM)
740 {
741 /* there's room in a register */
742 regval =
7e3dd49e
AC
743 extract_unsigned_integer (val,
744 register_size (gdbarch, argreg));
d95a8903
AC
745 regcache_cooked_write_unsigned (regcache, argreg++, regval);
746 }
747
748 /* Store the value 4 bytes at a time. This means that things
749 larger than 4 bytes may go partly in registers and partly
750 on the stack. */
7e3dd49e
AC
751 len -= register_size (gdbarch, argreg);
752 val += register_size (gdbarch, argreg);
d95a8903
AC
753 }
754 }
755
756 /* Finally, update the SP register. */
757 regcache_cooked_write_unsigned (regcache, M32R_SP_REGNUM, sp);
758
759 return sp;
760}
761
762
763/* Given a return value in `regbuf' with a type `valtype',
764 extract and copy its value into `valbuf'. */
765
766static void
767m32r_extract_return_value (struct type *type, struct regcache *regcache,
768 void *dst)
769{
770 bfd_byte *valbuf = dst;
771 int len = TYPE_LENGTH (type);
772 ULONGEST tmp;
773
774 /* By using store_unsigned_integer we avoid having to do
775 anything special for small big-endian values. */
776 regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
777 store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), tmp);
778
779 /* Ignore return values more than 8 bytes in size because the m32r
780 returns anything more than 8 bytes in the stack. */
781 if (len > 4)
782 {
783 regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
784 store_unsigned_integer (valbuf + len - 4, 4, tmp);
785 }
786}
787
14588880 788enum return_value_convention
c055b101
CV
789m32r_return_value (struct gdbarch *gdbarch, struct type *func_type,
790 struct type *valtype, struct regcache *regcache,
791 gdb_byte *readbuf, const gdb_byte *writebuf)
14588880
KI
792{
793 if (TYPE_LENGTH (valtype) > 8)
794 return RETURN_VALUE_STRUCT_CONVENTION;
795 else
796 {
797 if (readbuf != NULL)
798 m32r_extract_return_value (valtype, regcache, readbuf);
799 if (writebuf != NULL)
800 m32r_store_return_value (valtype, regcache, writebuf);
801 return RETURN_VALUE_REGISTER_CONVENTION;
802 }
803}
804
805
d95a8903
AC
806
807static CORE_ADDR
808m32r_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
809{
7e3dd49e 810 return frame_unwind_register_unsigned (next_frame, M32R_PC_REGNUM);
d95a8903
AC
811}
812
813/* Given a GDB frame, determine the address of the calling function's
814 frame. This will be used to create a new GDB frame struct. */
815
816static void
94afd7a6 817m32r_frame_this_id (struct frame_info *this_frame,
d95a8903
AC
818 void **this_prologue_cache, struct frame_id *this_id)
819{
820 struct m32r_unwind_cache *info
94afd7a6 821 = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
d95a8903
AC
822 CORE_ADDR base;
823 CORE_ADDR func;
824 struct minimal_symbol *msym_stack;
825 struct frame_id id;
826
827 /* The FUNC is easy. */
94afd7a6 828 func = get_frame_func (this_frame);
d95a8903 829
d95a8903
AC
830 /* Check if the stack is empty. */
831 msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
832 if (msym_stack && info->base == SYMBOL_VALUE_ADDRESS (msym_stack))
833 return;
834
835 /* Hopefully the prologue analysis either correctly determined the
836 frame's base (which is the SP from the previous frame), or set
837 that base to "NULL". */
838 base = info->prev_sp;
839 if (base == 0)
840 return;
841
842 id = frame_id_build (base, func);
d95a8903
AC
843 (*this_id) = id;
844}
845
94afd7a6
UW
846static struct value *
847m32r_frame_prev_register (struct frame_info *this_frame,
848 void **this_prologue_cache, int regnum)
d95a8903
AC
849{
850 struct m32r_unwind_cache *info
94afd7a6
UW
851 = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
852 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
d95a8903
AC
853}
854
855static const struct frame_unwind m32r_frame_unwind = {
856 NORMAL_FRAME,
857 m32r_frame_this_id,
94afd7a6
UW
858 m32r_frame_prev_register,
859 NULL,
860 default_frame_sniffer
d95a8903
AC
861};
862
d95a8903 863static CORE_ADDR
94afd7a6 864m32r_frame_base_address (struct frame_info *this_frame, void **this_cache)
d95a8903
AC
865{
866 struct m32r_unwind_cache *info
94afd7a6 867 = m32r_frame_unwind_cache (this_frame, this_cache);
d95a8903
AC
868 return info->base;
869}
870
871static const struct frame_base m32r_frame_base = {
872 &m32r_frame_unwind,
873 m32r_frame_base_address,
874 m32r_frame_base_address,
875 m32r_frame_base_address
876};
877
94afd7a6
UW
878/* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
879 frame. The frame ID's base needs to match the TOS value saved by
880 save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint. */
d95a8903
AC
881
882static struct frame_id
94afd7a6 883m32r_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
d95a8903 884{
94afd7a6
UW
885 CORE_ADDR sp = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
886 return frame_id_build (sp, get_frame_pc (this_frame));
d95a8903
AC
887}
888
889
890static gdbarch_init_ftype m32r_gdbarch_init;
891
892static struct gdbarch *
893m32r_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
894{
895 struct gdbarch *gdbarch;
896 struct gdbarch_tdep *tdep;
897
898 /* If there is already a candidate, use it. */
899 arches = gdbarch_list_lookup_by_info (arches, &info);
900 if (arches != NULL)
901 return arches->gdbarch;
902
903 /* Allocate space for the new architecture. */
904 tdep = XMALLOC (struct gdbarch_tdep);
905 gdbarch = gdbarch_alloc (&info, tdep);
906
907 set_gdbarch_read_pc (gdbarch, m32r_read_pc);
908 set_gdbarch_write_pc (gdbarch, m32r_write_pc);
909 set_gdbarch_unwind_sp (gdbarch, m32r_unwind_sp);
910
9b32d526 911 set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS);
d95a8903
AC
912 set_gdbarch_sp_regnum (gdbarch, M32R_SP_REGNUM);
913 set_gdbarch_register_name (gdbarch, m32r_register_name);
914 set_gdbarch_register_type (gdbarch, m32r_register_type);
915
d95a8903 916 set_gdbarch_push_dummy_call (gdbarch, m32r_push_dummy_call);
14588880 917 set_gdbarch_return_value (gdbarch, m32r_return_value);
d95a8903
AC
918
919 set_gdbarch_skip_prologue (gdbarch, m32r_skip_prologue);
920 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
d95a8903
AC
921 set_gdbarch_breakpoint_from_pc (gdbarch, m32r_breakpoint_from_pc);
922 set_gdbarch_memory_insert_breakpoint (gdbarch,
923 m32r_memory_insert_breakpoint);
924 set_gdbarch_memory_remove_breakpoint (gdbarch,
925 m32r_memory_remove_breakpoint);
926
d95a8903
AC
927 set_gdbarch_frame_align (gdbarch, m32r_frame_align);
928
d95a8903
AC
929 frame_base_set_default (gdbarch, &m32r_frame_base);
930
931 /* Methods for saving / extracting a dummy frame's ID. The ID's
932 stack address must match the SP value returned by
933 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
94afd7a6 934 set_gdbarch_dummy_id (gdbarch, m32r_dummy_id);
d95a8903
AC
935
936 /* Return the unwound PC value. */
937 set_gdbarch_unwind_pc (gdbarch, m32r_unwind_pc);
938
939 set_gdbarch_print_insn (gdbarch, print_insn_m32r);
940
c46b0409
KI
941 /* Hook in ABI-specific overrides, if they have been registered. */
942 gdbarch_init_osabi (info, gdbarch);
943
944 /* Hook in the default unwinders. */
94afd7a6 945 frame_unwind_append_unwinder (gdbarch, &m32r_frame_unwind);
c46b0409 946
1c772458
UW
947 /* Support simple overlay manager. */
948 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
949
d95a8903
AC
950 return gdbarch;
951}
952
953void
954_initialize_m32r_tdep (void)
955{
956 register_gdbarch_init (bfd_arch_m32r, m32r_gdbarch_init);
957}
This page took 0.463707 seconds and 4 git commands to generate.