GDB (API)
/home/stan/gdb/src/gdb/m32r-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for Renesas M32R, for GDB.
00002 
00003    Copyright (C) 1996-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "frame-unwind.h"
00023 #include "frame-base.h"
00024 #include "symtab.h"
00025 #include "gdbtypes.h"
00026 #include "gdbcmd.h"
00027 #include "gdbcore.h"
00028 #include "gdb_string.h"
00029 #include "value.h"
00030 #include "inferior.h"
00031 #include "symfile.h"
00032 #include "objfiles.h"
00033 #include "osabi.h"
00034 #include "language.h"
00035 #include "arch-utils.h"
00036 #include "regcache.h"
00037 #include "trad-frame.h"
00038 #include "dis-asm.h"
00039 
00040 #include "gdb_assert.h"
00041 
00042 #include "m32r-tdep.h"
00043 
00044 /* Local functions */
00045 
00046 extern void _initialize_m32r_tdep (void);
00047 
00048 static CORE_ADDR
00049 m32r_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
00050 {
00051   /* Align to the size of an instruction (so that they can safely be
00052      pushed onto the stack.  */
00053   return sp & ~3;
00054 }
00055 
00056 
00057 /* Breakpoints
00058  
00059    The little endian mode of M32R is unique.  In most of architectures,
00060    two 16-bit instructions, A and B, are placed as the following:
00061   
00062    Big endian:
00063    A0 A1 B0 B1
00064   
00065    Little endian:
00066    A1 A0 B1 B0
00067   
00068    In M32R, they are placed like this:
00069   
00070    Big endian:
00071    A0 A1 B0 B1
00072   
00073    Little endian:
00074    B1 B0 A1 A0
00075   
00076    This is because M32R always fetches instructions in 32-bit.
00077   
00078    The following functions take care of this behavior.  */
00079 
00080 static int
00081 m32r_memory_insert_breakpoint (struct gdbarch *gdbarch,
00082                                struct bp_target_info *bp_tgt)
00083 {
00084   CORE_ADDR addr = bp_tgt->placed_address;
00085   int val;
00086   gdb_byte buf[4];
00087   gdb_byte contents_cache[4];
00088   gdb_byte bp_entry[] = { 0x10, 0xf1 }; /* dpt */
00089 
00090   /* Save the memory contents.  */
00091   val = target_read_memory (addr & 0xfffffffc, contents_cache, 4);
00092   if (val != 0)
00093     return val;                 /* return error */
00094 
00095   memcpy (bp_tgt->shadow_contents, contents_cache, 4);
00096   bp_tgt->placed_size = bp_tgt->shadow_len = 4;
00097 
00098   /* Determine appropriate breakpoint contents and size for this address.  */
00099   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00100     {
00101       if ((addr & 3) == 0)
00102         {
00103           buf[0] = bp_entry[0];
00104           buf[1] = bp_entry[1];
00105           buf[2] = contents_cache[2] & 0x7f;
00106           buf[3] = contents_cache[3];
00107         }
00108       else
00109         {
00110           buf[0] = contents_cache[0];
00111           buf[1] = contents_cache[1];
00112           buf[2] = bp_entry[0];
00113           buf[3] = bp_entry[1];
00114         }
00115     }
00116   else                          /* little-endian */
00117     {
00118       if ((addr & 3) == 0)
00119         {
00120           buf[0] = contents_cache[0];
00121           buf[1] = contents_cache[1] & 0x7f;
00122           buf[2] = bp_entry[1];
00123           buf[3] = bp_entry[0];
00124         }
00125       else
00126         {
00127           buf[0] = bp_entry[1];
00128           buf[1] = bp_entry[0];
00129           buf[2] = contents_cache[2];
00130           buf[3] = contents_cache[3];
00131         }
00132     }
00133 
00134   /* Write the breakpoint.  */
00135   val = target_write_memory (addr & 0xfffffffc, buf, 4);
00136   return val;
00137 }
00138 
00139 static int
00140 m32r_memory_remove_breakpoint (struct gdbarch *gdbarch,
00141                                struct bp_target_info *bp_tgt)
00142 {
00143   CORE_ADDR addr = bp_tgt->placed_address;
00144   int val;
00145   gdb_byte buf[4];
00146   gdb_byte *contents_cache = bp_tgt->shadow_contents;
00147 
00148   buf[0] = contents_cache[0];
00149   buf[1] = contents_cache[1];
00150   buf[2] = contents_cache[2];
00151   buf[3] = contents_cache[3];
00152 
00153   /* Remove parallel bit.  */
00154   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00155     {
00156       if ((buf[0] & 0x80) == 0 && (buf[2] & 0x80) != 0)
00157         buf[2] &= 0x7f;
00158     }
00159   else                          /* little-endian */
00160     {
00161       if ((buf[3] & 0x80) == 0 && (buf[1] & 0x80) != 0)
00162         buf[1] &= 0x7f;
00163     }
00164 
00165   /* Write contents.  */
00166   val = target_write_raw_memory (addr & 0xfffffffc, buf, 4);
00167   return val;
00168 }
00169 
00170 static const gdb_byte *
00171 m32r_breakpoint_from_pc (struct gdbarch *gdbarch,
00172                          CORE_ADDR *pcptr, int *lenptr)
00173 {
00174   static gdb_byte be_bp_entry[] = {
00175     0x10, 0xf1, 0x70, 0x00
00176   };    /* dpt -> nop */
00177   static gdb_byte le_bp_entry[] = {
00178     0x00, 0x70, 0xf1, 0x10
00179   };    /* dpt -> nop */
00180   gdb_byte *bp;
00181 
00182   /* Determine appropriate breakpoint.  */
00183   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00184     {
00185       if ((*pcptr & 3) == 0)
00186         {
00187           bp = be_bp_entry;
00188           *lenptr = 4;
00189         }
00190       else
00191         {
00192           bp = be_bp_entry;
00193           *lenptr = 2;
00194         }
00195     }
00196   else
00197     {
00198       if ((*pcptr & 3) == 0)
00199         {
00200           bp = le_bp_entry;
00201           *lenptr = 4;
00202         }
00203       else
00204         {
00205           bp = le_bp_entry + 2;
00206           *lenptr = 2;
00207         }
00208     }
00209 
00210   return bp;
00211 }
00212 
00213 
00214 char *m32r_register_names[] = {
00215   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
00216   "r8", "r9", "r10", "r11", "r12", "fp", "lr", "sp",
00217   "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
00218   "evb"
00219 };
00220 
00221 static const char *
00222 m32r_register_name (struct gdbarch *gdbarch, int reg_nr)
00223 {
00224   if (reg_nr < 0)
00225     return NULL;
00226   if (reg_nr >= M32R_NUM_REGS)
00227     return NULL;
00228   return m32r_register_names[reg_nr];
00229 }
00230 
00231 
00232 /* Return the GDB type object for the "standard" data type
00233    of data in register N.  */
00234 
00235 static struct type *
00236 m32r_register_type (struct gdbarch *gdbarch, int reg_nr)
00237 {
00238   if (reg_nr == M32R_PC_REGNUM)
00239     return builtin_type (gdbarch)->builtin_func_ptr;
00240   else if (reg_nr == M32R_SP_REGNUM || reg_nr == M32R_FP_REGNUM)
00241     return builtin_type (gdbarch)->builtin_data_ptr;
00242   else
00243     return builtin_type (gdbarch)->builtin_int32;
00244 }
00245 
00246 
00247 /* Write into appropriate registers a function return value
00248    of type TYPE, given in virtual format.
00249 
00250    Things always get returned in RET1_REGNUM, RET2_REGNUM.  */
00251 
00252 static void
00253 m32r_store_return_value (struct type *type, struct regcache *regcache,
00254                          const void *valbuf)
00255 {
00256   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00257   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00258   CORE_ADDR regval;
00259   int len = TYPE_LENGTH (type);
00260 
00261   regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
00262   regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
00263 
00264   if (len > 4)
00265     {
00266       regval = extract_unsigned_integer ((gdb_byte *) valbuf + 4,
00267                                          len - 4, byte_order);
00268       regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
00269     }
00270 }
00271 
00272 /* This is required by skip_prologue.  The results of decoding a prologue
00273    should be cached because this thrashing is getting nuts.  */
00274 
00275 static int
00276 decode_prologue (struct gdbarch *gdbarch,
00277                  CORE_ADDR start_pc, CORE_ADDR scan_limit,
00278                  CORE_ADDR *pl_endptr, unsigned long *framelength)
00279 {
00280   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00281   unsigned long framesize;
00282   int insn;
00283   int op1;
00284   CORE_ADDR after_prologue = 0;
00285   CORE_ADDR after_push = 0;
00286   CORE_ADDR after_stack_adjust = 0;
00287   CORE_ADDR current_pc;
00288   LONGEST return_value;
00289 
00290   framesize = 0;
00291   after_prologue = 0;
00292 
00293   for (current_pc = start_pc; current_pc < scan_limit; current_pc += 2)
00294     {
00295       /* Check if current pc's location is readable.  */
00296       if (!safe_read_memory_integer (current_pc, 2, byte_order, &return_value))
00297         return -1;
00298 
00299       insn = read_memory_unsigned_integer (current_pc, 2, byte_order);
00300 
00301       if (insn == 0x0000)
00302         break;
00303 
00304       /* If this is a 32 bit instruction, we dont want to examine its
00305          immediate data as though it were an instruction.  */
00306       if (current_pc & 0x02)
00307         {
00308           /* Decode this instruction further.  */
00309           insn &= 0x7fff;
00310         }
00311       else
00312         {
00313           if (insn & 0x8000)
00314             {
00315               if (current_pc == scan_limit)
00316                 scan_limit += 2;        /* extend the search */
00317 
00318               current_pc += 2;  /* skip the immediate data */
00319 
00320               /* Check if current pc's location is readable.  */
00321               if (!safe_read_memory_integer (current_pc, 2, byte_order,
00322                                              &return_value))
00323                 return -1;
00324 
00325               if (insn == 0x8faf)       /* add3 sp, sp, xxxx */
00326                 /* add 16 bit sign-extended offset */
00327                 {
00328                   framesize +=
00329                     -((short) read_memory_unsigned_integer (current_pc,
00330                                                             2, byte_order));
00331                 }
00332               else
00333                 {
00334                   if (((insn >> 8) == 0xe4) /* ld24 r4, xxxxxx; sub sp, r4 */
00335                       && safe_read_memory_integer (current_pc + 2,
00336                                                    2, byte_order,
00337                                                    &return_value)
00338                       && read_memory_unsigned_integer (current_pc + 2,
00339                                                        2, byte_order)
00340                          == 0x0f24)
00341                     {
00342                       /* Subtract 24 bit sign-extended negative-offset.  */
00343                       insn = read_memory_unsigned_integer (current_pc - 2,
00344                                                            4, byte_order);
00345                       if (insn & 0x00800000)    /* sign extend */
00346                         insn |= 0xff000000;     /* negative */
00347                       else
00348                         insn &= 0x00ffffff;     /* positive */
00349                       framesize += insn;
00350                     }
00351                 }
00352               after_push = current_pc + 2;
00353               continue;
00354             }
00355         }
00356       op1 = insn & 0xf000;      /* Isolate just the first nibble.  */
00357 
00358       if ((insn & 0xf0ff) == 0x207f)
00359         {                       /* st reg, @-sp */
00360           int regno;
00361           framesize += 4;
00362           regno = ((insn >> 8) & 0xf);
00363           after_prologue = 0;
00364           continue;
00365         }
00366       if ((insn >> 8) == 0x4f)  /* addi sp, xx */
00367         /* Add 8 bit sign-extended offset.  */
00368         {
00369           int stack_adjust = (signed char) (insn & 0xff);
00370 
00371           /* there are probably two of these stack adjustments:
00372              1) A negative one in the prologue, and
00373              2) A positive one in the epilogue.
00374              We are only interested in the first one.  */
00375 
00376           if (stack_adjust < 0)
00377             {
00378               framesize -= stack_adjust;
00379               after_prologue = 0;
00380               /* A frameless function may have no "mv fp, sp".
00381                  In that case, this is the end of the prologue.  */
00382               after_stack_adjust = current_pc + 2;
00383             }
00384           continue;
00385         }
00386       if (insn == 0x1d8f)
00387         {                       /* mv fp, sp */
00388           after_prologue = current_pc + 2;
00389           break;                /* end of stack adjustments */
00390         }
00391 
00392       /* Nop looks like a branch, continue explicitly.  */
00393       if (insn == 0x7000)
00394         {
00395           after_prologue = current_pc + 2;
00396           continue;             /* nop occurs between pushes.  */
00397         }
00398       /* End of prolog if any of these are trap instructions.  */
00399       if ((insn & 0xfff0) == 0x10f0)
00400         {
00401           after_prologue = current_pc;
00402           break;
00403         }
00404       /* End of prolog if any of these are branch instructions.  */
00405       if ((op1 == 0x7000) || (op1 == 0xb000) || (op1 == 0xf000))
00406         {
00407           after_prologue = current_pc;
00408           continue;
00409         }
00410       /* Some of the branch instructions are mixed with other types.  */
00411       if (op1 == 0x1000)
00412         {
00413           int subop = insn & 0x0ff0;
00414           if ((subop == 0x0ec0) || (subop == 0x0fc0))
00415             {
00416               after_prologue = current_pc;
00417               continue;         /* jmp , jl */
00418             }
00419         }
00420     }
00421 
00422   if (framelength)
00423     *framelength = framesize;
00424 
00425   if (current_pc >= scan_limit)
00426     {
00427       if (pl_endptr)
00428         {
00429           if (after_stack_adjust != 0)
00430             /* We did not find a "mv fp,sp", but we DID find
00431                a stack_adjust.  Is it safe to use that as the
00432                end of the prologue?  I just don't know.  */
00433             {
00434               *pl_endptr = after_stack_adjust;
00435             }
00436           else if (after_push != 0)
00437             /* We did not find a "mv fp,sp", but we DID find
00438                a push.  Is it safe to use that as the
00439                end of the prologue?  I just don't know.  */
00440             {
00441               *pl_endptr = after_push;
00442             }
00443           else
00444             /* We reached the end of the loop without finding the end
00445                of the prologue.  No way to win -- we should report
00446                failure.  The way we do that is to return the original
00447                start_pc.  GDB will set a breakpoint at the start of
00448                the function (etc.)  */
00449             *pl_endptr = start_pc;
00450         }
00451       return 0;
00452     }
00453 
00454   if (after_prologue == 0)
00455     after_prologue = current_pc;
00456 
00457   if (pl_endptr)
00458     *pl_endptr = after_prologue;
00459 
00460   return 0;
00461 }                               /*  decode_prologue */
00462 
00463 /* Function: skip_prologue
00464    Find end of function prologue.  */
00465 
00466 #define DEFAULT_SEARCH_LIMIT 128
00467 
00468 static CORE_ADDR
00469 m32r_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00470 {
00471   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00472   CORE_ADDR func_addr, func_end;
00473   struct symtab_and_line sal;
00474   LONGEST return_value;
00475 
00476   /* See what the symbol table says.  */
00477 
00478   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
00479     {
00480       sal = find_pc_line (func_addr, 0);
00481 
00482       if (sal.line != 0 && sal.end <= func_end)
00483         {
00484           func_end = sal.end;
00485         }
00486       else
00487         /* Either there's no line info, or the line after the prologue is after
00488            the end of the function.  In this case, there probably isn't a
00489            prologue.  */
00490         {
00491           func_end = min (func_end, func_addr + DEFAULT_SEARCH_LIMIT);
00492         }
00493     }
00494   else
00495     func_end = pc + DEFAULT_SEARCH_LIMIT;
00496 
00497   /* If pc's location is not readable, just quit.  */
00498   if (!safe_read_memory_integer (pc, 4, byte_order, &return_value))
00499     return pc;
00500 
00501   /* Find the end of prologue.  */
00502   if (decode_prologue (gdbarch, pc, func_end, &sal.end, NULL) < 0)
00503     return pc;
00504 
00505   return sal.end;
00506 }
00507 
00508 struct m32r_unwind_cache
00509 {
00510   /* The previous frame's inner most stack address.  Used as this
00511      frame ID's stack_addr.  */
00512   CORE_ADDR prev_sp;
00513   /* The frame's base, optionally used by the high-level debug info.  */
00514   CORE_ADDR base;
00515   int size;
00516   /* How far the SP and r13 (FP) have been offset from the start of
00517      the stack frame (as defined by the previous frame's stack
00518      pointer).  */
00519   LONGEST sp_offset;
00520   LONGEST r13_offset;
00521   int uses_frame;
00522   /* Table indicating the location of each and every register.  */
00523   struct trad_frame_saved_reg *saved_regs;
00524 };
00525 
00526 /* Put here the code to store, into fi->saved_regs, the addresses of
00527    the saved registers of frame described by FRAME_INFO.  This
00528    includes special registers such as pc and fp saved in special ways
00529    in the stack frame.  sp is even more special: the address we return
00530    for it IS the sp for the next frame.  */
00531 
00532 static struct m32r_unwind_cache *
00533 m32r_frame_unwind_cache (struct frame_info *this_frame,
00534                          void **this_prologue_cache)
00535 {
00536   CORE_ADDR pc, scan_limit;
00537   ULONGEST prev_sp;
00538   ULONGEST this_base;
00539   unsigned long op;
00540   int i;
00541   struct m32r_unwind_cache *info;
00542 
00543 
00544   if ((*this_prologue_cache))
00545     return (*this_prologue_cache);
00546 
00547   info = FRAME_OBSTACK_ZALLOC (struct m32r_unwind_cache);
00548   (*this_prologue_cache) = info;
00549   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00550 
00551   info->size = 0;
00552   info->sp_offset = 0;
00553   info->uses_frame = 0;
00554 
00555   scan_limit = get_frame_pc (this_frame);
00556   for (pc = get_frame_func (this_frame);
00557        pc > 0 && pc < scan_limit; pc += 2)
00558     {
00559       if ((pc & 2) == 0)
00560         {
00561           op = get_frame_memory_unsigned (this_frame, pc, 4);
00562           if ((op & 0x80000000) == 0x80000000)
00563             {
00564               /* 32-bit instruction */
00565               if ((op & 0xffff0000) == 0x8faf0000)
00566                 {
00567                   /* add3 sp,sp,xxxx */
00568                   short n = op & 0xffff;
00569                   info->sp_offset += n;
00570                 }
00571               else if (((op >> 8) == 0xe4)
00572                        && get_frame_memory_unsigned (this_frame, pc + 2,
00573                                                      2) == 0x0f24)
00574                 {
00575                   /* ld24 r4, xxxxxx; sub sp, r4 */
00576                   unsigned long n = op & 0xffffff;
00577                   info->sp_offset += n;
00578                   pc += 2;      /* skip sub instruction */
00579                 }
00580 
00581               if (pc == scan_limit)
00582                 scan_limit += 2;        /* extend the search */
00583               pc += 2;          /* skip the immediate data */
00584               continue;
00585             }
00586         }
00587 
00588       /* 16-bit instructions */
00589       op = get_frame_memory_unsigned (this_frame, pc, 2) & 0x7fff;
00590       if ((op & 0xf0ff) == 0x207f)
00591         {
00592           /* st rn, @-sp */
00593           int regno = ((op >> 8) & 0xf);
00594           info->sp_offset -= 4;
00595           info->saved_regs[regno].addr = info->sp_offset;
00596         }
00597       else if ((op & 0xff00) == 0x4f00)
00598         {
00599           /* addi sp, xx */
00600           int n = (signed char) (op & 0xff);
00601           info->sp_offset += n;
00602         }
00603       else if (op == 0x1d8f)
00604         {
00605           /* mv fp, sp */
00606           info->uses_frame = 1;
00607           info->r13_offset = info->sp_offset;
00608           break;                /* end of stack adjustments */
00609         }
00610       else if ((op & 0xfff0) == 0x10f0)
00611         {
00612           /* End of prologue if this is a trap instruction.  */
00613           break;                /* End of stack adjustments.  */
00614         }
00615     }
00616 
00617   info->size = -info->sp_offset;
00618 
00619   /* Compute the previous frame's stack pointer (which is also the
00620      frame's ID's stack address), and this frame's base pointer.  */
00621   if (info->uses_frame)
00622     {
00623       /* The SP was moved to the FP.  This indicates that a new frame
00624          was created.  Get THIS frame's FP value by unwinding it from
00625          the next frame.  */
00626       this_base = get_frame_register_unsigned (this_frame, M32R_FP_REGNUM);
00627       /* The FP points at the last saved register.  Adjust the FP back
00628          to before the first saved register giving the SP.  */
00629       prev_sp = this_base + info->size;
00630     }
00631   else
00632     {
00633       /* Assume that the FP is this frame's SP but with that pushed
00634          stack space added back.  */
00635       this_base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
00636       prev_sp = this_base + info->size;
00637     }
00638 
00639   /* Convert that SP/BASE into real addresses.  */
00640   info->prev_sp = prev_sp;
00641   info->base = this_base;
00642 
00643   /* Adjust all the saved registers so that they contain addresses and
00644      not offsets.  */
00645   for (i = 0; i < gdbarch_num_regs (get_frame_arch (this_frame)) - 1; i++)
00646     if (trad_frame_addr_p (info->saved_regs, i))
00647       info->saved_regs[i].addr = (info->prev_sp + info->saved_regs[i].addr);
00648 
00649   /* The call instruction moves the caller's PC in the callee's LR.
00650      Since this is an unwind, do the reverse.  Copy the location of LR
00651      into PC (the address / regnum) so that a request for PC will be
00652      converted into a request for the LR.  */
00653   info->saved_regs[M32R_PC_REGNUM] = info->saved_regs[LR_REGNUM];
00654 
00655   /* The previous frame's SP needed to be computed.  Save the computed
00656      value.  */
00657   trad_frame_set_value (info->saved_regs, M32R_SP_REGNUM, prev_sp);
00658 
00659   return info;
00660 }
00661 
00662 static CORE_ADDR
00663 m32r_read_pc (struct regcache *regcache)
00664 {
00665   ULONGEST pc;
00666   regcache_cooked_read_unsigned (regcache, M32R_PC_REGNUM, &pc);
00667   return pc;
00668 }
00669 
00670 static CORE_ADDR
00671 m32r_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
00672 {
00673   return frame_unwind_register_unsigned (next_frame, M32R_SP_REGNUM);
00674 }
00675 
00676 
00677 static CORE_ADDR
00678 m32r_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
00679                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
00680                       struct value **args, CORE_ADDR sp, int struct_return,
00681                       CORE_ADDR struct_addr)
00682 {
00683   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00684   int stack_offset, stack_alloc;
00685   int argreg = ARG1_REGNUM;
00686   int argnum;
00687   struct type *type;
00688   enum type_code typecode;
00689   CORE_ADDR regval;
00690   gdb_byte *val;
00691   gdb_byte valbuf[MAX_REGISTER_SIZE];
00692   int len;
00693 
00694   /* First force sp to a 4-byte alignment.  */
00695   sp = sp & ~3;
00696 
00697   /* Set the return address.  For the m32r, the return breakpoint is
00698      always at BP_ADDR.  */
00699   regcache_cooked_write_unsigned (regcache, LR_REGNUM, bp_addr);
00700 
00701   /* If STRUCT_RETURN is true, then the struct return address (in
00702      STRUCT_ADDR) will consume the first argument-passing register.
00703      Both adjust the register count and store that value.  */
00704   if (struct_return)
00705     {
00706       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
00707       argreg++;
00708     }
00709 
00710   /* Now make sure there's space on the stack.  */
00711   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
00712     stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
00713   sp -= stack_alloc;            /* Make room on stack for args.  */
00714 
00715   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
00716     {
00717       type = value_type (args[argnum]);
00718       typecode = TYPE_CODE (type);
00719       len = TYPE_LENGTH (type);
00720 
00721       memset (valbuf, 0, sizeof (valbuf));
00722 
00723       /* Passes structures that do not fit in 2 registers by reference.  */
00724       if (len > 8
00725           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
00726         {
00727           store_unsigned_integer (valbuf, 4, byte_order,
00728                                   value_address (args[argnum]));
00729           typecode = TYPE_CODE_PTR;
00730           len = 4;
00731           val = valbuf;
00732         }
00733       else if (len < 4)
00734         {
00735           /* Value gets right-justified in the register or stack word.  */
00736           memcpy (valbuf + (register_size (gdbarch, argreg) - len),
00737                   (gdb_byte *) value_contents (args[argnum]), len);
00738           val = valbuf;
00739         }
00740       else
00741         val = (gdb_byte *) value_contents (args[argnum]);
00742 
00743       while (len > 0)
00744         {
00745           if (argreg > ARGN_REGNUM)
00746             {
00747               /* Must go on the stack.  */
00748               write_memory (sp + stack_offset, val, 4);
00749               stack_offset += 4;
00750             }
00751           else if (argreg <= ARGN_REGNUM)
00752             {
00753               /* There's room in a register.  */
00754               regval =
00755                 extract_unsigned_integer (val,
00756                                           register_size (gdbarch, argreg),
00757                                           byte_order);
00758               regcache_cooked_write_unsigned (regcache, argreg++, regval);
00759             }
00760 
00761           /* Store the value 4 bytes at a time.  This means that things
00762              larger than 4 bytes may go partly in registers and partly
00763              on the stack.  */
00764           len -= register_size (gdbarch, argreg);
00765           val += register_size (gdbarch, argreg);
00766         }
00767     }
00768 
00769   /* Finally, update the SP register.  */
00770   regcache_cooked_write_unsigned (regcache, M32R_SP_REGNUM, sp);
00771 
00772   return sp;
00773 }
00774 
00775 
00776 /* Given a return value in `regbuf' with a type `valtype', 
00777    extract and copy its value into `valbuf'.  */
00778 
00779 static void
00780 m32r_extract_return_value (struct type *type, struct regcache *regcache,
00781                            void *dst)
00782 {
00783   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00784   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00785   bfd_byte *valbuf = dst;
00786   int len = TYPE_LENGTH (type);
00787   ULONGEST tmp;
00788 
00789   /* By using store_unsigned_integer we avoid having to do
00790      anything special for small big-endian values.  */
00791   regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
00792   store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp);
00793 
00794   /* Ignore return values more than 8 bytes in size because the m32r
00795      returns anything more than 8 bytes in the stack.  */
00796   if (len > 4)
00797     {
00798       regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
00799       store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
00800     }
00801 }
00802 
00803 static enum return_value_convention
00804 m32r_return_value (struct gdbarch *gdbarch, struct value *function,
00805                    struct type *valtype, struct regcache *regcache,
00806                    gdb_byte *readbuf, const gdb_byte *writebuf)
00807 {
00808   if (TYPE_LENGTH (valtype) > 8)
00809     return RETURN_VALUE_STRUCT_CONVENTION;
00810   else
00811     {
00812       if (readbuf != NULL)
00813         m32r_extract_return_value (valtype, regcache, readbuf);
00814       if (writebuf != NULL)
00815         m32r_store_return_value (valtype, regcache, writebuf);
00816       return RETURN_VALUE_REGISTER_CONVENTION;
00817     }
00818 }
00819 
00820 
00821 
00822 static CORE_ADDR
00823 m32r_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
00824 {
00825   return frame_unwind_register_unsigned (next_frame, M32R_PC_REGNUM);
00826 }
00827 
00828 /* Given a GDB frame, determine the address of the calling function's
00829    frame.  This will be used to create a new GDB frame struct.  */
00830 
00831 static void
00832 m32r_frame_this_id (struct frame_info *this_frame,
00833                     void **this_prologue_cache, struct frame_id *this_id)
00834 {
00835   struct m32r_unwind_cache *info
00836     = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
00837   CORE_ADDR base;
00838   CORE_ADDR func;
00839   struct minimal_symbol *msym_stack;
00840   struct frame_id id;
00841 
00842   /* The FUNC is easy.  */
00843   func = get_frame_func (this_frame);
00844 
00845   /* Check if the stack is empty.  */
00846   msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
00847   if (msym_stack && info->base == SYMBOL_VALUE_ADDRESS (msym_stack))
00848     return;
00849 
00850   /* Hopefully the prologue analysis either correctly determined the
00851      frame's base (which is the SP from the previous frame), or set
00852      that base to "NULL".  */
00853   base = info->prev_sp;
00854   if (base == 0)
00855     return;
00856 
00857   id = frame_id_build (base, func);
00858   (*this_id) = id;
00859 }
00860 
00861 static struct value *
00862 m32r_frame_prev_register (struct frame_info *this_frame,
00863                           void **this_prologue_cache, int regnum)
00864 {
00865   struct m32r_unwind_cache *info
00866     = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
00867   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
00868 }
00869 
00870 static const struct frame_unwind m32r_frame_unwind = {
00871   NORMAL_FRAME,
00872   default_frame_unwind_stop_reason,
00873   m32r_frame_this_id,
00874   m32r_frame_prev_register,
00875   NULL,
00876   default_frame_sniffer
00877 };
00878 
00879 static CORE_ADDR
00880 m32r_frame_base_address (struct frame_info *this_frame, void **this_cache)
00881 {
00882   struct m32r_unwind_cache *info
00883     = m32r_frame_unwind_cache (this_frame, this_cache);
00884   return info->base;
00885 }
00886 
00887 static const struct frame_base m32r_frame_base = {
00888   &m32r_frame_unwind,
00889   m32r_frame_base_address,
00890   m32r_frame_base_address,
00891   m32r_frame_base_address
00892 };
00893 
00894 /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
00895    frame.  The frame ID's base needs to match the TOS value saved by
00896    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */
00897 
00898 static struct frame_id
00899 m32r_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
00900 {
00901   CORE_ADDR sp = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
00902   return frame_id_build (sp, get_frame_pc (this_frame));
00903 }
00904 
00905 
00906 static gdbarch_init_ftype m32r_gdbarch_init;
00907 
00908 static struct gdbarch *
00909 m32r_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
00910 {
00911   struct gdbarch *gdbarch;
00912   struct gdbarch_tdep *tdep;
00913 
00914   /* If there is already a candidate, use it.  */
00915   arches = gdbarch_list_lookup_by_info (arches, &info);
00916   if (arches != NULL)
00917     return arches->gdbarch;
00918 
00919   /* Allocate space for the new architecture.  */
00920   tdep = XMALLOC (struct gdbarch_tdep);
00921   gdbarch = gdbarch_alloc (&info, tdep);
00922 
00923   set_gdbarch_read_pc (gdbarch, m32r_read_pc);
00924   set_gdbarch_unwind_sp (gdbarch, m32r_unwind_sp);
00925 
00926   set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS);
00927   set_gdbarch_pc_regnum (gdbarch, M32R_PC_REGNUM);
00928   set_gdbarch_sp_regnum (gdbarch, M32R_SP_REGNUM);
00929   set_gdbarch_register_name (gdbarch, m32r_register_name);
00930   set_gdbarch_register_type (gdbarch, m32r_register_type);
00931 
00932   set_gdbarch_push_dummy_call (gdbarch, m32r_push_dummy_call);
00933   set_gdbarch_return_value (gdbarch, m32r_return_value);
00934 
00935   set_gdbarch_skip_prologue (gdbarch, m32r_skip_prologue);
00936   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
00937   set_gdbarch_breakpoint_from_pc (gdbarch, m32r_breakpoint_from_pc);
00938   set_gdbarch_memory_insert_breakpoint (gdbarch,
00939                                         m32r_memory_insert_breakpoint);
00940   set_gdbarch_memory_remove_breakpoint (gdbarch,
00941                                         m32r_memory_remove_breakpoint);
00942 
00943   set_gdbarch_frame_align (gdbarch, m32r_frame_align);
00944 
00945   frame_base_set_default (gdbarch, &m32r_frame_base);
00946 
00947   /* Methods for saving / extracting a dummy frame's ID.  The ID's
00948      stack address must match the SP value returned by
00949      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
00950   set_gdbarch_dummy_id (gdbarch, m32r_dummy_id);
00951 
00952   /* Return the unwound PC value.  */
00953   set_gdbarch_unwind_pc (gdbarch, m32r_unwind_pc);
00954 
00955   set_gdbarch_print_insn (gdbarch, print_insn_m32r);
00956 
00957   /* Hook in ABI-specific overrides, if they have been registered.  */
00958   gdbarch_init_osabi (info, gdbarch);
00959 
00960   /* Hook in the default unwinders.  */
00961   frame_unwind_append_unwinder (gdbarch, &m32r_frame_unwind);
00962 
00963   /* Support simple overlay manager.  */
00964   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
00965 
00966   return gdbarch;
00967 }
00968 
00969 void
00970 _initialize_m32r_tdep (void)
00971 {
00972   register_gdbarch_init (bfd_arch_m32r, m32r_gdbarch_init);
00973 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines