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