Merge from vendor branch GCC:
[dragonfly.git] / contrib / gdb / gdb / m32r-tdep.c
1 /* Target-dependent code for the Mitsubishi m32r for GDB, the GNU debugger.
2    Copyright 1996, Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Function: m32r_use_struct_convention
32    Return nonzero if call_function should allocate stack space for a
33    struct return? */
34 int
35 m32r_use_struct_convention (gcc_p, type)
36      int gcc_p;
37      struct type *type;
38 {
39   return (TYPE_LENGTH (type) > 8);
40 }
41
42 /* Function: frame_find_saved_regs
43    Return the frame_saved_regs structure for the frame.
44    Doesn't really work for dummy frames, but it does pass back
45    an empty frame_saved_regs, so I guess that's better than total failure */
46
47 void 
48 m32r_frame_find_saved_regs (fi, regaddr)
49      struct frame_info *fi;
50      struct frame_saved_regs *regaddr;
51 {
52   memcpy(regaddr, &fi->fsr, sizeof(struct frame_saved_regs));
53 }
54
55 /* Turn this on if you want to see just how much instruction decoding
56    if being done, its quite a lot
57    */
58 #if 0
59 static void dump_insn(char * commnt,CORE_ADDR pc, int insn)
60 {
61   printf_filtered("  %s %08x %08x ",
62                   commnt,(unsigned int)pc,(unsigned int) insn);
63   (*tm_print_insn)(pc,&tm_print_insn_info);
64   printf_filtered("\n");
65 }
66 #define insn_debug(args) { printf_filtered args; }
67 #else
68 #define dump_insn(a,b,c) {}
69 #define insn_debug(args) {}
70 #endif
71
72 #define DEFAULT_SEARCH_LIMIT 44 
73
74 /* Function: scan_prologue
75    This function decodes the target function prologue to determine
76    1) the size of the stack frame, and 2) which registers are saved on it.
77    It saves the offsets of saved regs in the frame_saved_regs argument,
78    and returns the frame size.  */
79
80 /*
81   The sequence it currently generates is:
82   
83         if (varargs function) { ddi sp,#n }
84         push registers
85         if (additional stack <= 256) {  addi sp,#-stack }
86         else if (additional stack < 65k) { add3 sp,sp,#-stack
87
88         } else if (additional stack) {
89         seth sp,#(stack & 0xffff0000)
90         or3 sp,sp,#(stack & 0x0000ffff)
91         sub sp,r4
92         }
93         if (frame pointer) {
94                 mv sp,fp
95         }
96
97 These instructions are scheduled like everything else, so you should stop at
98 the first branch instruction.
99  
100 */
101
102 /* This is required by skip prologue and by m32r_init_extra_frame_info. 
103    The results of decoding a prologue should be cached because this
104    thrashing is getting nuts.
105    I am thinking of making a container class with two indexes, name and
106    address. It may be better to extend the symbol table.
107    */
108
109 static void decode_prologue (start_pc, scan_limit, 
110                              pl_endptr, framelength, 
111                              fi, fsr)
112      CORE_ADDR start_pc;
113      CORE_ADDR scan_limit;
114      CORE_ADDR * pl_endptr;  /* var parameter */
115      unsigned long * framelength;
116      struct frame_info * fi;
117      struct frame_saved_regs * fsr;
118 {
119   unsigned long framesize;
120   int insn;
121   int op1;
122   int maybe_one_more = 0;
123   CORE_ADDR after_prologue = 0;
124   CORE_ADDR after_stack_adjust = 0;
125   CORE_ADDR current_pc;
126
127
128   framesize = 0;
129   after_prologue = 0;
130   insn_debug(("rd prolog l(%d)\n",scan_limit - current_pc));
131
132   for (current_pc = start_pc; current_pc < scan_limit; current_pc += 2)
133     {
134
135       insn = read_memory_unsigned_integer (current_pc, 2);
136       dump_insn("insn-1",current_pc,insn);    /* MTZ */
137       
138        /* If this is a 32 bit instruction, we dont want to examine its
139          immediate data as though it were an instruction */
140       if (current_pc & 0x02)
141         { /* Clear the parallel execution bit from 16 bit instruction */
142           if (maybe_one_more)
143             { /* The last instruction was a branch, usually terminates
144                  the series, but if this is a parallel instruction,
145                  it may be a stack framing instruction */
146               if (! (insn & 0x8000))
147                 { insn_debug(("Really done"));
148                   break; /* nope, we are really done */
149                 }
150             }
151           insn &= 0x7fff;        /* decode this instruction further */
152         }
153       else
154         {
155           if (maybe_one_more) 
156             break; /* This isnt the one more */
157           if (insn & 0x8000)
158             {
159               insn_debug(("32 bit insn\n"));
160               if (current_pc == scan_limit)
161                 scan_limit += 2; /* extend the search */
162               current_pc += 2;   /* skip the immediate data */
163               if (insn == 0x8faf)                       /* add3 sp, sp, xxxx */
164                 /* add 16 bit sign-extended offset */
165                 { insn_debug(("stack increment\n"));
166                 framesize += -((short) read_memory_unsigned_integer (current_pc, 2));
167                 }
168               else
169                 {
170                   if (((insn >> 8) == 0xe4) && /* ld24 r4, xxxxxx; sub sp, r4 */
171                       read_memory_unsigned_integer (current_pc + 2, 2) == 0x0f24)
172                     { /* subtract 24 bit sign-extended negative-offset */
173                       dump_insn("insn-2",current_pc+2,insn);
174                       insn = read_memory_unsigned_integer (current_pc - 2, 4);
175                       dump_insn("insn-3(l4)",current_pc -2,insn);
176                       if (insn & 0x00800000)    /* sign extend */
177                         insn  |= 0xff000000;    /* negative */
178                       else
179                         insn  &= 0x00ffffff;    /* positive */
180                       framesize += insn;
181                     }
182                 }
183               after_prologue = current_pc;
184               continue;
185             }
186         }
187       op1 = insn & 0xf000; /* isolate just the first nibble */
188       
189       if ((insn & 0xf0ff) == 0x207f)
190         {               /* st reg, @-sp */
191           int regno;
192           insn_debug(("push\n"));
193 #if 0   /* No, PUSH FP is not an indication that we will use a frame pointer. */
194           if (((insn & 0xffff) == 0x2d7f) && fi) 
195             fi->using_frame_pointer = 1;
196 #endif
197           framesize  += 4;
198 #if 0 
199 /* Why should we increase the scan limit, just because we did a push? 
200    And if there is a reason, surely we would only want to do it if we
201    had already reached the scan limit... */
202           if (current_pc == scan_limit)
203             scan_limit += 2;
204 #endif
205           regno = ((insn >> 8) & 0xf);
206           if (fsr)                              /* save_regs offset */
207             fsr->regs[regno] = framesize;
208           after_prologue = 0;
209             continue;
210         }
211       if ((insn >> 8) == 0x4f)                  /* addi sp, xx */
212         /* add 8 bit sign-extended offset */
213         {
214           int stack_adjust = (char) (insn & 0xff);
215
216           /* there are probably two of these stack adjustments:
217              1) A negative one in the prologue, and
218              2) A positive one in the epilogue.
219              We are only interested in the first one.  */
220
221           if (stack_adjust < 0)
222             {
223               framesize -= stack_adjust;
224               after_prologue = 0;
225               /* A frameless function may have no "mv fp, sp".
226                  In that case, this is the end of the prologue.  */
227               after_stack_adjust = current_pc + 2;
228             }
229           continue;
230         }
231       if (insn == 0x1d8f) {     /* mv fp, sp */
232         if (fi) 
233           fi->using_frame_pointer = 1;  /* fp is now valid */
234         insn_debug(("done fp found\n"));
235         after_prologue = current_pc + 2;
236         break;                          /* end of stack adjustments */
237       }
238       if (insn ==  0x7000)  /* Nop looks like a branch, continue explicitly */
239         { insn_debug(("nop\n"));
240         after_prologue = current_pc + 2;
241         continue; /* nop occurs between pushes */
242         }
243       /* End of prolog if any of these are branch instructions */
244       if ((op1 == 0x7000)
245           || ( op1 == 0xb000)
246           || (op1 == 0x7000))
247         {
248           after_prologue = current_pc;
249           insn_debug(("Done: branch\n"));
250           maybe_one_more = 1;
251           continue;
252         }
253       /* Some of the branch instructions are mixed with other types */
254       if (op1 == 0x1000)
255         {int subop = insn & 0x0ff0;
256           if ((subop == 0x0ec0) || (subop == 0x0fc0))
257             { insn_debug(("done: jmp\n"));
258               after_prologue = current_pc;
259               maybe_one_more = 1;
260               continue; /* jmp , jl */
261             }
262         }
263     }
264
265   if (current_pc >= scan_limit)
266     {
267       if (pl_endptr) 
268 #if 1
269         if (after_stack_adjust != 0)
270           /* We did not find a "mv fp,sp", but we DID find
271              a stack_adjust.  Is it safe to use that as the
272              end of the prologue?  I just don't know. */
273           {
274             *pl_endptr = after_stack_adjust;
275             if (framelength)
276               *framelength = framesize;
277           }
278         else
279 #endif
280       /* We reached the end of the loop without finding the end
281          of the prologue.  No way to win -- we should report failure.  
282          The way we do that is to return the original start_pc.
283          GDB will set a breakpoint at the start of the function (etc.) */
284
285         *pl_endptr = start_pc;
286         
287       return;
288     }
289   if (after_prologue == 0) 
290     after_prologue = current_pc;
291
292   insn_debug((" framesize %d, firstline %08x\n",framesize,after_prologue));
293   if (framelength) 
294     *framelength = framesize;
295   if (pl_endptr) 
296     *pl_endptr = after_prologue;
297 } /*  decode_prologue */
298
299 /* Function: skip_prologue
300    Find end of function prologue */
301
302 CORE_ADDR
303 m32r_skip_prologue (pc)
304      CORE_ADDR pc;
305 {
306   CORE_ADDR func_addr, func_end;
307   struct symtab_and_line sal;
308
309   /* See what the symbol table says */
310
311   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
312     {
313       sal = find_pc_line (func_addr, 0);
314
315       if (sal.line != 0 && sal.end <= func_end)
316         {
317           
318           insn_debug(("BP after prologue %08x\n",sal.end));
319           func_end = sal.end;
320         }
321       else
322         /* Either there's no line info, or the line after the prologue is after
323            the end of the function.  In this case, there probably isn't a
324            prologue.  */
325         {
326           insn_debug(("No line info, line(%x) sal_end(%x) funcend(%x)\n",
327                       sal.line,sal.end,func_end));
328           func_end = min(func_end,func_addr + DEFAULT_SEARCH_LIMIT);
329         }
330     }
331   else 
332     func_end = pc + DEFAULT_SEARCH_LIMIT;
333   decode_prologue (pc, func_end, &sal.end, 0, 0, 0);
334   return sal.end;
335 }
336
337 static unsigned long
338 m32r_scan_prologue (fi, fsr)
339      struct frame_info *fi;
340      struct frame_saved_regs *fsr;
341 {
342   struct symtab_and_line sal;
343   CORE_ADDR prologue_start, prologue_end, current_pc;
344   unsigned long framesize;
345
346   /* this code essentially duplicates skip_prologue, 
347      but we need the start address below.  */
348
349   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
350     {
351       sal = find_pc_line (prologue_start, 0);
352
353       if (sal.line == 0)                /* no line info, use current PC */
354         if (prologue_start == entry_point_address ())
355           return 0;
356     }
357   else
358     {
359       prologue_start = fi->pc;
360       prologue_end = prologue_start + 48; /* We're in the boondocks: 
361                                               allow for 16 pushes, an add, 
362                                               and "mv fp,sp" */
363     }
364 #if 0
365   prologue_end = min (prologue_end, fi->pc);
366 #endif
367   insn_debug(("fipc(%08x) start(%08x) end(%08x)\n",
368               fi->pc,prologue_start,prologue_end));
369   prologue_end = min(prologue_end, prologue_start + DEFAULT_SEARCH_LIMIT);
370   decode_prologue (prologue_start,prologue_end,&prologue_end,&framesize,
371                    fi,fsr);
372   return framesize;
373 }
374
375 /* Function: init_extra_frame_info
376    This function actually figures out the frame address for a given pc and
377    sp.  This is tricky on the m32r because we sometimes don't use an explicit
378    frame pointer, and the previous stack pointer isn't necessarily recorded
379    on the stack.  The only reliable way to get this info is to
380    examine the prologue.  */
381
382 void
383 m32r_init_extra_frame_info (fi)
384      struct frame_info *fi;
385 {
386   int reg;
387
388   if (fi->next)
389     fi->pc = FRAME_SAVED_PC (fi->next);
390
391   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
392
393   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
394     {
395       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
396          by assuming it's always FP.  */
397       fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
398       fi->framesize = 0;
399       return;
400     }
401   else 
402     {
403       fi->using_frame_pointer = 0;
404       fi->framesize = m32r_scan_prologue (fi, &fi->fsr);
405
406       if (!fi->next)
407         if (fi->using_frame_pointer)
408           {
409             fi->frame = read_register (FP_REGNUM);
410           }
411         else
412           fi->frame = read_register (SP_REGNUM);
413       else      /* fi->next means this is not the innermost frame */
414         if (fi->using_frame_pointer)                /* we have an FP */
415           if (fi->next->fsr.regs[FP_REGNUM] != 0)   /* caller saved our FP */
416             fi->frame = read_memory_integer (fi->next->fsr.regs[FP_REGNUM], 4);
417       for (reg = 0; reg < NUM_REGS; reg++)
418         if (fi->fsr.regs[reg] != 0)
419           fi->fsr.regs[reg] = fi->frame + fi->framesize - fi->fsr.regs[reg];
420     }
421 }
422
423 /* Function: mn10300_virtual_frame_pointer
424    Return the register that the function uses for a frame pointer, 
425    plus any necessary offset to be applied to the register before
426    any frame pointer offsets.  */
427
428 void
429 m32r_virtual_frame_pointer (pc, reg, offset)
430      CORE_ADDR pc;
431      long *reg;
432      long *offset;
433 {
434   struct frame_info fi;
435
436   /* Set up a dummy frame_info. */
437   fi.next = NULL;
438   fi.prev = NULL;
439   fi.frame = 0;
440   fi.pc = pc;
441
442   /* Analyze the prolog and fill in the extra info.  */
443   m32r_init_extra_frame_info (&fi);
444
445
446   /* Results will tell us which type of frame it uses.  */
447   if (fi.using_frame_pointer)
448     {
449       *reg    = FP_REGNUM;
450       *offset = 0;
451     }
452   else
453     {
454       *reg    = SP_REGNUM;
455       *offset = 0;
456     }
457 }
458
459 /* Function: find_callers_reg
460    Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
461    we might want to do here is to check REGNUM against the clobber mask, and
462    somehow flag it as invalid if it isn't saved on the stack somewhere.  This
463    would provide a graceful failure mode when trying to get the value of
464    caller-saves registers for an inner frame.  */
465
466 CORE_ADDR
467 m32r_find_callers_reg (fi, regnum)
468      struct frame_info *fi;
469      int regnum;
470 {
471   for (; fi; fi = fi->next)
472     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
473       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
474     else if (fi->fsr.regs[regnum] != 0)
475       return read_memory_integer (fi->fsr.regs[regnum], 
476                                   REGISTER_RAW_SIZE(regnum));
477   return read_register (regnum);
478 }
479
480 /* Function: frame_chain
481    Given a GDB frame, determine the address of the calling function's frame.
482    This will be used to create a new GDB frame struct, and then
483    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
484    For m32r, we save the frame size when we initialize the frame_info.  */
485
486 CORE_ADDR
487 m32r_frame_chain (fi)
488      struct frame_info *fi;
489 {
490   CORE_ADDR fn_start, callers_pc, fp;
491
492   /* is this a dummy frame? */
493   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
494     return fi->frame;   /* dummy frame same as caller's frame */
495
496   /* is caller-of-this a dummy frame? */
497   callers_pc = FRAME_SAVED_PC(fi);  /* find out who called us: */
498   fp = m32r_find_callers_reg (fi, FP_REGNUM);
499   if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))     
500     return fp;          /* dummy frame's frame may bear no relation to ours */
501
502   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
503     if (fn_start == entry_point_address ())
504       return 0;         /* in _start fn, don't chain further */
505   if (fi->framesize == 0)
506     {
507       printf_filtered("cannot determine frame size @ %08x , pc(%08x)\n",
508                       (unsigned long) fi->frame,
509                       (unsigned long) fi->pc );
510       return 0;
511     }
512   insn_debug(("m32rx frame %08x\n",fi->frame+fi->framesize));
513   return fi->frame + fi->framesize;
514 }
515
516 /* Function: push_return_address (pc)
517    Set up the return address for the inferior function call.
518    Necessary for targets that don't actually execute a JSR/BSR instruction 
519    (ie. when using an empty CALL_DUMMY) */
520
521 CORE_ADDR
522 m32r_push_return_address (pc, sp)
523      CORE_ADDR pc;
524      CORE_ADDR sp;
525 {
526   write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
527   return sp;
528 }
529
530
531 /* Function: pop_frame
532    Discard from the stack the innermost frame,
533    restoring all saved registers.  */
534
535 struct frame_info *
536 m32r_pop_frame (frame)
537      struct frame_info *frame;
538 {
539   int regnum;
540
541   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
542     generic_pop_dummy_frame ();
543   else
544     {
545       for (regnum = 0; regnum < NUM_REGS; regnum++)
546         if (frame->fsr.regs[regnum] != 0)
547           write_register (regnum, 
548                           read_memory_integer (frame->fsr.regs[regnum], 4));
549
550       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
551       write_register (SP_REGNUM, read_register (FP_REGNUM));
552       if (read_register (PSW_REGNUM) & 0x80)
553         write_register (SPU_REGNUM, read_register (SP_REGNUM));
554       else
555         write_register (SPI_REGNUM, read_register (SP_REGNUM));
556     }
557   flush_cached_frames ();
558   return NULL;
559 }
560
561 /* Function: frame_saved_pc
562    Find the caller of this frame.  We do this by seeing if RP_REGNUM is saved
563    in the stack anywhere, otherwise we get it from the registers. */
564
565 CORE_ADDR
566 m32r_frame_saved_pc (fi)
567      struct frame_info *fi;
568 {
569   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
570     return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
571   else
572     return m32r_find_callers_reg (fi, RP_REGNUM);
573 }
574
575 /* Function: push_arguments
576    Setup the function arguments for calling a function in the inferior.
577
578    On the Mitsubishi M32R architecture, there are four registers (R0 to R3)
579    which are dedicated for passing function arguments.  Up to the first 
580    four arguments (depending on size) may go into these registers.
581    The rest go on the stack.
582
583    Arguments that are smaller than 4 bytes will still take up a whole
584    register or a whole 32-bit word on the stack, and will be
585    right-justified in the register or the stack word.  This includes
586    chars, shorts, and small aggregate types.
587  
588    Arguments of 8 bytes size are split between two registers, if 
589    available.  If only one register is available, the argument will 
590    be split between the register and the stack.  Otherwise it is
591    passed entirely on the stack.  Aggregate types with sizes between
592    4 and 8 bytes are passed entirely on the stack, and are left-justified
593    within the double-word (as opposed to aggregates smaller than 4 bytes
594    which are right-justified).
595
596    Aggregates of greater than 8 bytes are first copied onto the stack, 
597    and then a pointer to the copy is passed in the place of the normal
598    argument (either in a register if available, or on the stack).
599
600    Functions that must return an aggregate type can return it in the 
601    normal return value registers (R0 and R1) if its size is 8 bytes or
602    less.  For larger return values, the caller must allocate space for 
603    the callee to copy the return value to.  A pointer to this space is
604    passed as an implicit first argument, always in R0. */
605
606 CORE_ADDR
607 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
608      int nargs;
609      value_ptr *args;
610      CORE_ADDR sp;
611      unsigned char struct_return;
612      CORE_ADDR struct_addr;
613 {
614   int stack_offset, stack_alloc;
615   int argreg;
616   int argnum;
617   struct type *type;
618   CORE_ADDR regval;
619   char *val;
620   char valbuf[4];
621   int len;
622   int odd_sized_struct;
623
624   /* first force sp to a 4-byte alignment */
625   sp = sp & ~3;
626
627   argreg = ARG0_REGNUM;  
628   /* The "struct return pointer" pseudo-argument goes in R0 */
629   if (struct_return)
630       write_register (argreg++, struct_addr);
631  
632   /* Now make sure there's space on the stack */
633   for (argnum = 0, stack_alloc = 0;
634        argnum < nargs; argnum++)
635     stack_alloc += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
636   sp -= stack_alloc;    /* make room on stack for args */
637  
638  
639   /* Now load as many as possible of the first arguments into
640      registers, and push the rest onto the stack.  There are 16 bytes
641      in four registers available.  Loop thru args from first to last.  */
642  
643   argreg = ARG0_REGNUM;
644   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
645     {
646       type = VALUE_TYPE (args[argnum]);
647       len  = TYPE_LENGTH (type);
648       memset(valbuf, 0, sizeof(valbuf));
649       if (len < 4)
650         { /* value gets right-justified in the register or stack word */
651           memcpy(valbuf + (4 - len),
652                  (char *) VALUE_CONTENTS (args[argnum]), len);
653           val = valbuf;
654         }
655       else
656         val = (char *) VALUE_CONTENTS (args[argnum]);
657  
658       if (len > 4 && (len & 3) != 0)
659         odd_sized_struct = 1;           /* such structs go entirely on stack */
660       else
661         odd_sized_struct = 0;
662       while (len > 0)
663         {
664           if (argreg > ARGLAST_REGNUM || odd_sized_struct)
665             {                           /* must go on the stack */
666               write_memory (sp + stack_offset, val, 4);
667               stack_offset += 4;
668             }
669           /* NOTE WELL!!!!!  This is not an "else if" clause!!!
670              That's because some *&^%$ things get passed on the stack
671              AND in the registers!   */
672           if (argreg <= ARGLAST_REGNUM)
673             {                           /* there's room in a register */
674               regval = extract_address (val, REGISTER_RAW_SIZE(argreg));
675               write_register (argreg++, regval);
676             }
677           /* Store the value 4 bytes at a time.  This means that things
678              larger than 4 bytes may go partly in registers and partly
679              on the stack.  */
680           len -= REGISTER_RAW_SIZE(argreg);
681           val += REGISTER_RAW_SIZE(argreg);
682         }
683     }
684   return sp;
685 }
686
687 /* Function: fix_call_dummy 
688    If there is real CALL_DUMMY code (eg. on the stack), this function
689    has the responsability to insert the address of the actual code that
690    is the target of the target function call.  */
691
692 void
693 m32r_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
694      char *dummy;
695      CORE_ADDR pc;
696      CORE_ADDR fun;
697      int nargs;
698      value_ptr *args;
699      struct type *type;
700      int gcc_p;
701 {
702   /* ld24 r8, <(imm24) fun> */
703   *(unsigned long *) (dummy) = (fun & 0x00ffffff) | 0xe8000000;
704 }
705
706 /* Function: get_saved_register
707    Just call the generic_get_saved_register function.  */
708
709 void
710 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
711      char *raw_buffer;
712      int *optimized;
713      CORE_ADDR *addrp;
714      struct frame_info *frame;
715      int regnum;
716      enum lval_type *lval;
717 {
718   generic_get_saved_register (raw_buffer, optimized, addrp, 
719                               frame, regnum, lval);
720 }
721
722
723 /* Function: m32r_write_sp
724    Because SP is really a read-only register that mirrors either SPU or SPI,
725    we must actually write one of those two as well, depending on PSW. */
726
727 void
728 m32r_write_sp (val)
729      CORE_ADDR val;
730 {
731   unsigned long psw = read_register (PSW_REGNUM);
732
733   if (psw & 0x80)       /* stack mode: user or interrupt */
734     write_register (SPU_REGNUM, val);
735   else
736     write_register (SPI_REGNUM, val);
737   write_register (SP_REGNUM, val);
738 }
739
740 void
741 _initialize_m32r_tdep ()
742 {
743   tm_print_insn = print_insn_m32r;
744 }
745