GDB (API)
/home/stan/gdb/src/gdb/sh64-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for Renesas Super-H, for GDB.
00002 
00003    Copyright (C) 1993-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 /* Contributed by Steve Chamberlain
00021    sac@cygnus.com.  */
00022 
00023 #include "defs.h"
00024 #include "frame.h"
00025 #include "frame-base.h"
00026 #include "frame-unwind.h"
00027 #include "dwarf2-frame.h"
00028 #include "symtab.h"
00029 #include "gdbtypes.h"
00030 #include "gdbcmd.h"
00031 #include "gdbcore.h"
00032 #include "value.h"
00033 #include "dis-asm.h"
00034 #include "inferior.h"
00035 #include "gdb_string.h"
00036 #include "gdb_assert.h"
00037 #include "arch-utils.h"
00038 #include "regcache.h"
00039 #include "osabi.h"
00040 #include "valprint.h"
00041 
00042 #include "elf-bfd.h"
00043 
00044 /* sh flags */
00045 #include "elf/sh.h"
00046 /* Register numbers shared with the simulator.  */
00047 #include "gdb/sim-sh.h"
00048 #include "language.h"
00049 #include "sh64-tdep.h"
00050 
00051 /* Information that is dependent on the processor variant.  */
00052 enum sh_abi
00053   {
00054     SH_ABI_UNKNOWN,
00055     SH_ABI_32,
00056     SH_ABI_64
00057   };
00058 
00059 struct gdbarch_tdep
00060   {
00061     enum sh_abi sh_abi;
00062   };
00063 
00064 struct sh64_frame_cache
00065 {
00066   /* Base address.  */
00067   CORE_ADDR base;
00068   LONGEST sp_offset;
00069   CORE_ADDR pc;
00070 
00071   /* Flag showing that a frame has been created in the prologue code.  */
00072   int uses_fp;
00073 
00074   int media_mode;
00075 
00076   /* Saved registers.  */
00077   CORE_ADDR saved_regs[SIM_SH64_NR_REGS];
00078   CORE_ADDR saved_sp;
00079 };
00080 
00081 /* Registers of SH5 */
00082 enum
00083   {
00084     R0_REGNUM = 0,
00085     DEFAULT_RETURN_REGNUM = 2,
00086     STRUCT_RETURN_REGNUM = 2,
00087     ARG0_REGNUM = 2,
00088     ARGLAST_REGNUM = 9,
00089     FLOAT_ARGLAST_REGNUM = 11,
00090     MEDIA_FP_REGNUM = 14,
00091     PR_REGNUM = 18,
00092     SR_REGNUM = 65,
00093     DR0_REGNUM = 141,
00094     DR_LAST_REGNUM = 172,
00095     /* FPP stands for Floating Point Pair, to avoid confusion with
00096        GDB's gdbarch_fp0_regnum, which is the number of the first Floating
00097        point register.  Unfortunately on the sh5, the floating point
00098        registers are called FR, and the floating point pairs are called FP.  */
00099     FPP0_REGNUM = 173,
00100     FPP_LAST_REGNUM = 204,
00101     FV0_REGNUM = 205,
00102     FV_LAST_REGNUM = 220,
00103     R0_C_REGNUM = 221,
00104     R_LAST_C_REGNUM = 236,
00105     PC_C_REGNUM = 237,
00106     GBR_C_REGNUM = 238,
00107     MACH_C_REGNUM = 239,
00108     MACL_C_REGNUM = 240,
00109     PR_C_REGNUM = 241,
00110     T_C_REGNUM = 242,
00111     FPSCR_C_REGNUM = 243,
00112     FPUL_C_REGNUM = 244,
00113     FP0_C_REGNUM = 245,
00114     FP_LAST_C_REGNUM = 260,
00115     DR0_C_REGNUM = 261,
00116     DR_LAST_C_REGNUM = 268,
00117     FV0_C_REGNUM = 269,
00118     FV_LAST_C_REGNUM = 272,
00119     FPSCR_REGNUM = SIM_SH64_FPCSR_REGNUM,
00120     SSR_REGNUM = SIM_SH64_SSR_REGNUM,
00121     SPC_REGNUM = SIM_SH64_SPC_REGNUM,
00122     TR7_REGNUM = SIM_SH64_TR0_REGNUM + 7,
00123     FP_LAST_REGNUM = SIM_SH64_FR0_REGNUM + SIM_SH64_NR_FP_REGS - 1
00124   };
00125 
00126 static const char *
00127 sh64_register_name (struct gdbarch *gdbarch, int reg_nr)
00128 {
00129   static char *register_names[] =
00130   {
00131     /* SH MEDIA MODE (ISA 32) */
00132     /* general registers (64-bit) 0-63 */
00133     "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
00134     "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
00135     "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
00136     "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
00137     "r32",  "r33",  "r34",  "r35",  "r36",  "r37",  "r38",  "r39",
00138     "r40",  "r41",  "r42",  "r43",  "r44",  "r45",  "r46",  "r47",
00139     "r48",  "r49",  "r50",  "r51",  "r52",  "r53",  "r54",  "r55",
00140     "r56",  "r57",  "r58",  "r59",  "r60",  "r61",  "r62",  "r63",
00141 
00142     /* pc (64-bit) 64 */
00143     "pc",   
00144 
00145     /* status reg., saved status reg., saved pc reg. (64-bit) 65-67 */
00146     "sr",  "ssr",  "spc", 
00147 
00148     /* target registers (64-bit) 68-75 */
00149     "tr0",  "tr1",  "tr2",  "tr3",  "tr4",  "tr5",  "tr6",  "tr7",
00150 
00151     /* floating point state control register (32-bit) 76 */
00152     "fpscr",
00153 
00154     /* single precision floating point registers (32-bit) 77-140 */
00155     "fr0",  "fr1",  "fr2",  "fr3",  "fr4",  "fr5",  "fr6",  "fr7",
00156     "fr8",  "fr9",  "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
00157     "fr16", "fr17", "fr18", "fr19", "fr20", "fr21", "fr22", "fr23",
00158     "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", "fr30", "fr31",
00159     "fr32", "fr33", "fr34", "fr35", "fr36", "fr37", "fr38", "fr39",
00160     "fr40", "fr41", "fr42", "fr43", "fr44", "fr45", "fr46", "fr47",
00161     "fr48", "fr49", "fr50", "fr51", "fr52", "fr53", "fr54", "fr55",
00162     "fr56", "fr57", "fr58", "fr59", "fr60", "fr61", "fr62", "fr63",
00163 
00164     /* double precision registers (pseudo) 141-172 */
00165     "dr0",  "dr2",  "dr4",  "dr6",  "dr8",  "dr10", "dr12", "dr14",
00166     "dr16", "dr18", "dr20", "dr22", "dr24", "dr26", "dr28", "dr30",
00167     "dr32", "dr34", "dr36", "dr38", "dr40", "dr42", "dr44", "dr46",
00168     "dr48", "dr50", "dr52", "dr54", "dr56", "dr58", "dr60", "dr62",
00169 
00170     /* floating point pairs (pseudo) 173-204 */
00171     "fp0",  "fp2",  "fp4",  "fp6",  "fp8",  "fp10", "fp12", "fp14",
00172     "fp16", "fp18", "fp20", "fp22", "fp24", "fp26", "fp28", "fp30",
00173     "fp32", "fp34", "fp36", "fp38", "fp40", "fp42", "fp44", "fp46",
00174     "fp48", "fp50", "fp52", "fp54", "fp56", "fp58", "fp60", "fp62",
00175 
00176     /* floating point vectors (4 floating point regs) (pseudo) 205-220 */
00177     "fv0",  "fv4",  "fv8",  "fv12", "fv16", "fv20", "fv24", "fv28",
00178     "fv32", "fv36", "fv40", "fv44", "fv48", "fv52", "fv56", "fv60",
00179 
00180     /* SH COMPACT MODE (ISA 16) (all pseudo) 221-272 */
00181     "r0_c", "r1_c", "r2_c",  "r3_c",  "r4_c",  "r5_c",  "r6_c",  "r7_c",
00182     "r8_c", "r9_c", "r10_c", "r11_c", "r12_c", "r13_c", "r14_c", "r15_c",
00183     "pc_c",
00184     "gbr_c", "mach_c", "macl_c", "pr_c", "t_c",
00185     "fpscr_c", "fpul_c",
00186     "fr0_c",  "fr1_c",  "fr2_c",  "fr3_c",
00187     "fr4_c",  "fr5_c",  "fr6_c",  "fr7_c",
00188     "fr8_c",  "fr9_c",  "fr10_c", "fr11_c",
00189     "fr12_c", "fr13_c", "fr14_c", "fr15_c",
00190     "dr0_c",  "dr2_c",  "dr4_c",  "dr6_c",
00191     "dr8_c",  "dr10_c", "dr12_c", "dr14_c",
00192     "fv0_c", "fv4_c", "fv8_c",  "fv12_c",
00193     /* FIXME!!!! XF0 XF15, XD0 XD14 ?????  */
00194   };
00195 
00196   if (reg_nr < 0)
00197     return NULL;
00198   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
00199     return NULL;
00200   return register_names[reg_nr];
00201 }
00202 
00203 #define NUM_PSEUDO_REGS_SH_MEDIA 80
00204 #define NUM_PSEUDO_REGS_SH_COMPACT 51
00205 
00206 /* Macros and functions for setting and testing a bit in a minimal
00207    symbol that marks it as 32-bit function.  The MSB of the minimal
00208    symbol's "info" field is used for this purpose.
00209 
00210    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is "special",
00211    i.e. refers to a 32-bit function, and sets a "special" bit in a
00212    minimal symbol to mark it as a 32-bit function
00213    MSYMBOL_IS_SPECIAL   tests the "special" bit in a minimal symbol  */
00214 
00215 #define MSYMBOL_IS_SPECIAL(msym) \
00216   MSYMBOL_TARGET_FLAG_1 (msym)
00217 
00218 static void
00219 sh64_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
00220 {
00221   if (msym == NULL)
00222     return;
00223 
00224   if (((elf_symbol_type *)(sym))->internal_elf_sym.st_other == STO_SH5_ISA32)
00225     {
00226       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
00227       SYMBOL_VALUE_ADDRESS (msym) |= 1;
00228     }
00229 }
00230 
00231 /* ISA32 (shmedia) function addresses are odd (bit 0 is set).  Here
00232    are some macros to test, set, or clear bit 0 of addresses.  */
00233 #define IS_ISA32_ADDR(addr)      ((addr) & 1)
00234 #define MAKE_ISA32_ADDR(addr)    ((addr) | 1)
00235 #define UNMAKE_ISA32_ADDR(addr)  ((addr) & ~1)
00236 
00237 static int
00238 pc_is_isa32 (bfd_vma memaddr)
00239 {
00240   struct bound_minimal_symbol sym;
00241 
00242   /* If bit 0 of the address is set, assume this is a
00243      ISA32 (shmedia) address.  */
00244   if (IS_ISA32_ADDR (memaddr))
00245     return 1;
00246 
00247   /* A flag indicating that this is a ISA32 function is stored by elfread.c in
00248      the high bit of the info field.  Use this to decide if the function is
00249      ISA16 or ISA32.  */
00250   sym = lookup_minimal_symbol_by_pc (memaddr);
00251   if (sym.minsym)
00252     return MSYMBOL_IS_SPECIAL (sym.minsym);
00253   else
00254     return 0;
00255 }
00256 
00257 static const unsigned char *
00258 sh64_breakpoint_from_pc (struct gdbarch *gdbarch,
00259                          CORE_ADDR *pcptr, int *lenptr)
00260 {
00261   /* The BRK instruction for shmedia is 
00262      01101111 11110101 11111111 11110000
00263      which translates in big endian mode to 0x6f, 0xf5, 0xff, 0xf0
00264      and in little endian mode to 0xf0, 0xff, 0xf5, 0x6f */
00265 
00266   /* The BRK instruction for shcompact is
00267      00000000 00111011
00268      which translates in big endian mode to 0x0, 0x3b
00269      and in little endian mode to 0x3b, 0x0 */
00270 
00271   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00272     {
00273       if (pc_is_isa32 (*pcptr))
00274         {
00275           static unsigned char big_breakpoint_media[] = {
00276             0x6f, 0xf5, 0xff, 0xf0
00277           };
00278           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
00279           *lenptr = sizeof (big_breakpoint_media);
00280           return big_breakpoint_media;
00281         }
00282       else
00283         {
00284           static unsigned char big_breakpoint_compact[] = {0x0, 0x3b};
00285           *lenptr = sizeof (big_breakpoint_compact);
00286           return big_breakpoint_compact;
00287         }
00288     }
00289   else
00290     {
00291       if (pc_is_isa32 (*pcptr))
00292         {
00293           static unsigned char little_breakpoint_media[] = {
00294             0xf0, 0xff, 0xf5, 0x6f
00295           };
00296           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
00297           *lenptr = sizeof (little_breakpoint_media);
00298           return little_breakpoint_media;
00299         }
00300       else
00301         {
00302           static unsigned char little_breakpoint_compact[] = {0x3b, 0x0};
00303           *lenptr = sizeof (little_breakpoint_compact);
00304           return little_breakpoint_compact;
00305         }
00306     }
00307 }
00308 
00309 /* Prologue looks like
00310    [mov.l       <regs>,@-r15]...
00311    [sts.l       pr,@-r15]
00312    [mov.l       r14,@-r15]
00313    [mov         r15,r14]
00314 
00315    Actually it can be more complicated than this.  For instance, with
00316    newer gcc's:
00317 
00318    mov.l   r14,@-r15
00319    add     #-12,r15
00320    mov     r15,r14
00321    mov     r4,r1
00322    mov     r5,r2
00323    mov.l   r6,@(4,r14)
00324    mov.l   r7,@(8,r14)
00325    mov.b   r1,@r14
00326    mov     r14,r1
00327    mov     r14,r1
00328    add     #2,r1
00329    mov.w   r2,@r1
00330 
00331  */
00332 
00333 /* PTABS/L Rn, TRa       0110101111110001nnnnnnl00aaa0000 
00334    with l=1 and n = 18   0110101111110001010010100aaa0000 */
00335 #define IS_PTABSL_R18(x)  (((x) & 0xffffff8f) == 0x6bf14a00)
00336 
00337 /* STS.L PR,@-r0   0100000000100010
00338    r0-4-->r0, PR-->(r0) */
00339 #define IS_STS_R0(x)            ((x) == 0x4022)
00340 
00341 /* STS PR, Rm      0000mmmm00101010
00342    PR-->Rm */
00343 #define IS_STS_PR(x)            (((x) & 0xf0ff) == 0x2a)
00344 
00345 /* MOV.L Rm,@(disp,r15)  00011111mmmmdddd
00346    Rm-->(dispx4+r15) */
00347 #define IS_MOV_TO_R15(x)              (((x) & 0xff00) == 0x1f00)
00348 
00349 /* MOV.L R14,@(disp,r15)  000111111110dddd
00350    R14-->(dispx4+r15) */
00351 #define IS_MOV_R14(x)              (((x) & 0xfff0) == 0x1fe0)
00352 
00353 /* ST.Q R14, disp, R18    101011001110dddddddddd0100100000
00354    R18-->(dispx8+R14) */
00355 #define IS_STQ_R18_R14(x)          (((x) & 0xfff003ff) == 0xace00120)
00356 
00357 /* ST.Q R15, disp, R18    101011001111dddddddddd0100100000
00358    R18-->(dispx8+R15) */
00359 #define IS_STQ_R18_R15(x)          (((x) & 0xfff003ff) == 0xacf00120)
00360 
00361 /* ST.L R15, disp, R18    101010001111dddddddddd0100100000
00362    R18-->(dispx4+R15) */
00363 #define IS_STL_R18_R15(x)          (((x) & 0xfff003ff) == 0xa8f00120)
00364 
00365 /* ST.Q R15, disp, R14    1010 1100 1111 dddd dddd dd00 1110 0000
00366    R14-->(dispx8+R15) */
00367 #define IS_STQ_R14_R15(x)          (((x) & 0xfff003ff) == 0xacf000e0)
00368 
00369 /* ST.L R15, disp, R14    1010 1000 1111 dddd dddd dd00 1110 0000
00370    R14-->(dispx4+R15) */
00371 #define IS_STL_R14_R15(x)          (((x) & 0xfff003ff) == 0xa8f000e0)
00372 
00373 /* ADDI.L R15,imm,R15     1101 0100 1111 ssss ssss ss00 1111 0000
00374    R15 + imm --> R15 */
00375 #define IS_ADDIL_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd4f000f0)
00376 
00377 /* ADDI R15,imm,R15     1101 0000 1111 ssss ssss ss00 1111 0000
00378    R15 + imm --> R15 */
00379 #define IS_ADDI_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd0f000f0)
00380 
00381 /* ADD.L R15,R63,R14    0000 0000 1111 1000 1111 1100 1110 0000 
00382    R15 + R63 --> R14 */
00383 #define IS_ADDL_SP_FP_MEDIA(x)          ((x) == 0x00f8fce0)
00384 
00385 /* ADD R15,R63,R14    0000 0000 1111 1001 1111 1100 1110 0000 
00386    R15 + R63 --> R14 */
00387 #define IS_ADD_SP_FP_MEDIA(x)   ((x) == 0x00f9fce0)
00388 
00389 #define IS_MOV_SP_FP_MEDIA(x)   \
00390   (IS_ADDL_SP_FP_MEDIA(x) || IS_ADD_SP_FP_MEDIA(x))
00391 
00392 /* MOV #imm, R0    1110 0000 ssss ssss 
00393    #imm-->R0 */
00394 #define IS_MOV_R0(x)            (((x) & 0xff00) == 0xe000)
00395 
00396 /* MOV.L @(disp,PC), R0    1101 0000 iiii iiii  */
00397 #define IS_MOVL_R0(x)           (((x) & 0xff00) == 0xd000)
00398 
00399 /* ADD r15,r0      0011 0000 1111 1100
00400    r15+r0-->r0 */
00401 #define IS_ADD_SP_R0(x)         ((x) == 0x30fc)
00402 
00403 /* MOV.L R14 @-R0  0010 0000 1110 0110
00404    R14-->(R0-4), R0-4-->R0 */
00405 #define IS_MOV_R14_R0(x)        ((x) == 0x20e6)
00406 
00407 /* ADD Rm,R63,Rn  Rm+R63-->Rn  0000 00mm mmmm 1001 1111 11nn nnnn 0000
00408    where Rm is one of r2-r9 which are the argument registers.  */
00409 /* FIXME: Recognize the float and double register moves too!  */
00410 #define IS_MEDIA_IND_ARG_MOV(x) \
00411   ((((x) & 0xfc0ffc0f) == 0x0009fc00) \
00412    && (((x) & 0x03f00000) >= 0x00200000 \
00413        && ((x) & 0x03f00000) <= 0x00900000))
00414 
00415 /* ST.Q Rn,0,Rm  Rm-->Rn+0  1010 11nn nnnn 0000 0000 00mm mmmm 0000
00416    or ST.L Rn,0,Rm  Rm-->Rn+0  1010 10nn nnnn 0000 0000 00mm mmmm 0000
00417    where Rm is one of r2-r9 which are the argument registers.  */
00418 #define IS_MEDIA_ARG_MOV(x) \
00419 (((((x) & 0xfc0ffc0f) == 0xac000000) || (((x) & 0xfc0ffc0f) == 0xa8000000)) \
00420    && (((x) & 0x000003f0) >= 0x00000020 && ((x) & 0x000003f0) <= 0x00000090))
00421 
00422 /* ST.B R14,0,Rn     Rn-->(R14+0) 1010 0000 1110 0000 0000 00nn nnnn 0000 */
00423 /* ST.W R14,0,Rn     Rn-->(R14+0) 1010 0100 1110 0000 0000 00nn nnnn 0000 */
00424 /* ST.L R14,0,Rn     Rn-->(R14+0) 1010 1000 1110 0000 0000 00nn nnnn 0000 */
00425 /* FST.S R14,0,FRn   Rn-->(R14+0) 1011 0100 1110 0000 0000 00nn nnnn 0000 */
00426 /* FST.D R14,0,DRn   Rn-->(R14+0) 1011 1100 1110 0000 0000 00nn nnnn 0000 */
00427 #define IS_MEDIA_MOV_TO_R14(x)  \
00428 ((((x) & 0xfffffc0f) == 0xa0e00000) \
00429 || (((x) & 0xfffffc0f) == 0xa4e00000) \
00430 || (((x) & 0xfffffc0f) == 0xa8e00000) \
00431 || (((x) & 0xfffffc0f) == 0xb4e00000) \
00432 || (((x) & 0xfffffc0f) == 0xbce00000))
00433 
00434 /* MOV Rm, Rn  Rm-->Rn 0110 nnnn mmmm 0011
00435    where Rm is r2-r9 */
00436 #define IS_COMPACT_IND_ARG_MOV(x) \
00437   ((((x) & 0xf00f) == 0x6003) && (((x) & 0x00f0) >= 0x0020) \
00438    && (((x) & 0x00f0) <= 0x0090))
00439 
00440 /* compact direct arg move! 
00441    MOV.L Rn, @r14     0010 1110 mmmm 0010 */
00442 #define IS_COMPACT_ARG_MOV(x) \
00443   (((((x) & 0xff0f) == 0x2e02) && (((x) & 0x00f0) >= 0x0020) \
00444     && ((x) & 0x00f0) <= 0x0090))
00445 
00446 /* MOV.B Rm, @R14     0010 1110 mmmm 0000 
00447    MOV.W Rm, @R14     0010 1110 mmmm 0001 */
00448 #define IS_COMPACT_MOV_TO_R14(x) \
00449 ((((x) & 0xff0f) == 0x2e00) || (((x) & 0xff0f) == 0x2e01))
00450 
00451 #define IS_JSR_R0(x)           ((x) == 0x400b)
00452 #define IS_NOP(x)              ((x) == 0x0009)
00453 
00454 
00455 /* MOV r15,r14     0110111011110011
00456    r15-->r14  */
00457 #define IS_MOV_SP_FP(x)         ((x) == 0x6ef3)
00458 
00459 /* ADD #imm,r15    01111111iiiiiiii
00460    r15+imm-->r15 */
00461 #define IS_ADD_SP(x)            (((x) & 0xff00) == 0x7f00)
00462 
00463 /* Skip any prologue before the guts of a function.  */
00464 
00465 /* Skip the prologue using the debug information.  If this fails we'll
00466    fall back on the 'guess' method below.  */
00467 static CORE_ADDR
00468 after_prologue (CORE_ADDR pc)
00469 {
00470   struct symtab_and_line sal;
00471   CORE_ADDR func_addr, func_end;
00472 
00473   /* If we can not find the symbol in the partial symbol table, then
00474      there is no hope we can determine the function's start address
00475      with this code.  */
00476   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
00477     return 0;
00478 
00479 
00480   /* Get the line associated with FUNC_ADDR.  */
00481   sal = find_pc_line (func_addr, 0);
00482 
00483   /* There are only two cases to consider.  First, the end of the source line
00484      is within the function bounds.  In that case we return the end of the
00485      source line.  Second is the end of the source line extends beyond the
00486      bounds of the current function.  We need to use the slow code to
00487      examine instructions in that case.  */
00488   if (sal.end < func_end)
00489     return sal.end;
00490   else
00491     return 0;
00492 }
00493 
00494 static CORE_ADDR 
00495 look_for_args_moves (struct gdbarch *gdbarch,
00496                      CORE_ADDR start_pc, int media_mode)
00497 {
00498   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00499   CORE_ADDR here, end;
00500   int w;
00501   int insn_size = (media_mode ? 4 : 2);
00502 
00503   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
00504     {
00505       if (media_mode)
00506         {
00507           w = read_memory_integer (UNMAKE_ISA32_ADDR (here),
00508                                    insn_size, byte_order);
00509           here += insn_size;
00510           if (IS_MEDIA_IND_ARG_MOV (w))
00511             {
00512               /* This must be followed by a store to r14, so the argument
00513                  is where the debug info says it is.  This can happen after
00514                  the SP has been saved, unfortunately.  */
00515          
00516               int next_insn = read_memory_integer (UNMAKE_ISA32_ADDR (here),
00517                                                    insn_size, byte_order);
00518               here += insn_size;
00519               if (IS_MEDIA_MOV_TO_R14 (next_insn))
00520                 start_pc = here;          
00521             }
00522           else if (IS_MEDIA_ARG_MOV (w))
00523             {
00524               /* These instructions store directly the argument in r14.  */
00525               start_pc = here;
00526             }
00527           else
00528             break;
00529         }
00530       else
00531         {
00532           w = read_memory_integer (here, insn_size, byte_order);
00533           w = w & 0xffff;
00534           here += insn_size;
00535           if (IS_COMPACT_IND_ARG_MOV (w))
00536             {
00537               /* This must be followed by a store to r14, so the argument
00538                  is where the debug info says it is.  This can happen after
00539                  the SP has been saved, unfortunately.  */
00540          
00541               int next_insn = 0xffff & read_memory_integer (here, insn_size,
00542                                                             byte_order);
00543               here += insn_size;
00544               if (IS_COMPACT_MOV_TO_R14 (next_insn))
00545                 start_pc = here;
00546             }
00547           else if (IS_COMPACT_ARG_MOV (w))
00548             {
00549               /* These instructions store directly the argument in r14.  */
00550               start_pc = here;
00551             }
00552           else if (IS_MOVL_R0 (w))
00553             {
00554               /* There is a function that gcc calls to get the arguments
00555                  passed correctly to the function.  Only after this
00556                  function call the arguments will be found at the place
00557                  where they are supposed to be.  This happens in case the
00558                  argument has to be stored into a 64-bit register (for
00559                  instance doubles, long longs).  SHcompact doesn't have
00560                  access to the full 64-bits, so we store the register in
00561                  stack slot and store the address of the stack slot in
00562                  the register, then do a call through a wrapper that
00563                  loads the memory value into the register.  A SHcompact
00564                  callee calls an argument decoder
00565                  (GCC_shcompact_incoming_args) that stores the 64-bit
00566                  value in a stack slot and stores the address of the
00567                  stack slot in the register.  GCC thinks the argument is
00568                  just passed by transparent reference, but this is only
00569                  true after the argument decoder is called.  Such a call
00570                  needs to be considered part of the prologue.  */
00571 
00572               /* This must be followed by a JSR @r0 instruction and by
00573                  a NOP instruction.  After these, the prologue is over!  */
00574          
00575               int next_insn = 0xffff & read_memory_integer (here, insn_size,
00576                                                             byte_order);
00577               here += insn_size;
00578               if (IS_JSR_R0 (next_insn))
00579                 {
00580                   next_insn = 0xffff & read_memory_integer (here, insn_size,
00581                                                             byte_order);
00582                   here += insn_size;
00583 
00584                   if (IS_NOP (next_insn))
00585                     start_pc = here;
00586                 }
00587             }
00588           else
00589             break;
00590         }
00591     }
00592 
00593   return start_pc;
00594 }
00595 
00596 static CORE_ADDR
00597 sh64_skip_prologue_hard_way (struct gdbarch *gdbarch, CORE_ADDR start_pc)
00598 {
00599   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00600   CORE_ADDR here, end;
00601   int updated_fp = 0;
00602   int insn_size = 4;
00603   int media_mode = 1;
00604 
00605   if (!start_pc)
00606     return 0;
00607 
00608   if (pc_is_isa32 (start_pc) == 0)
00609     {
00610       insn_size = 2;
00611       media_mode = 0;
00612     }
00613 
00614   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
00615     {
00616 
00617       if (media_mode)
00618         {
00619           int w = read_memory_integer (UNMAKE_ISA32_ADDR (here),
00620                                        insn_size, byte_order);
00621           here += insn_size;
00622           if (IS_STQ_R18_R14 (w) || IS_STQ_R18_R15 (w) || IS_STQ_R14_R15 (w)
00623               || IS_STL_R14_R15 (w) || IS_STL_R18_R15 (w)
00624               || IS_ADDIL_SP_MEDIA (w) || IS_ADDI_SP_MEDIA (w)
00625               || IS_PTABSL_R18 (w))
00626             {
00627               start_pc = here;
00628             }
00629           else if (IS_MOV_SP_FP (w) || IS_MOV_SP_FP_MEDIA(w))
00630             {
00631               start_pc = here;
00632               updated_fp = 1;
00633             }
00634           else
00635             if (updated_fp)
00636               {
00637                 /* Don't bail out yet, we may have arguments stored in
00638                    registers here, according to the debug info, so that
00639                    gdb can print the frames correctly.  */
00640                 start_pc = look_for_args_moves (gdbarch,
00641                                                 here - insn_size, media_mode);
00642                 break;
00643               }
00644         }
00645       else
00646         {
00647           int w = 0xffff & read_memory_integer (here, insn_size, byte_order);
00648           here += insn_size;
00649 
00650           if (IS_STS_R0 (w) || IS_STS_PR (w)
00651               || IS_MOV_TO_R15 (w) || IS_MOV_R14 (w) 
00652               || IS_MOV_R0 (w) || IS_ADD_SP_R0 (w) || IS_MOV_R14_R0 (w))
00653             {
00654               start_pc = here;
00655             }
00656           else if (IS_MOV_SP_FP (w))
00657             {
00658               start_pc = here;
00659               updated_fp = 1;
00660             }
00661           else
00662             if (updated_fp)
00663               {
00664                 /* Don't bail out yet, we may have arguments stored in
00665                    registers here, according to the debug info, so that
00666                    gdb can print the frames correctly.  */
00667                 start_pc = look_for_args_moves (gdbarch,
00668                                                 here - insn_size, media_mode);
00669                 break;
00670               }
00671         }
00672     }
00673 
00674   return start_pc;
00675 }
00676 
00677 static CORE_ADDR
00678 sh64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00679 {
00680   CORE_ADDR post_prologue_pc;
00681 
00682   /* See if we can determine the end of the prologue via the symbol table.
00683      If so, then return either PC, or the PC after the prologue, whichever
00684      is greater.  */
00685   post_prologue_pc = after_prologue (pc);
00686 
00687   /* If after_prologue returned a useful address, then use it.  Else
00688      fall back on the instruction skipping code.  */
00689   if (post_prologue_pc != 0)
00690     return max (pc, post_prologue_pc);
00691   else
00692     return sh64_skip_prologue_hard_way (gdbarch, pc);
00693 }
00694 
00695 /* Should call_function allocate stack space for a struct return?  */
00696 static int
00697 sh64_use_struct_convention (struct type *type)
00698 {
00699   return (TYPE_LENGTH (type) > 8);
00700 }
00701 
00702 /* For vectors of 4 floating point registers.  */
00703 static int
00704 sh64_fv_reg_base_num (struct gdbarch *gdbarch, int fv_regnum)
00705 {
00706   int fp_regnum;
00707 
00708   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fv_regnum - FV0_REGNUM) * 4;
00709   return fp_regnum;
00710 }
00711 
00712 /* For double precision floating point registers, i.e 2 fp regs.  */
00713 static int
00714 sh64_dr_reg_base_num (struct gdbarch *gdbarch, int dr_regnum)
00715 {
00716   int fp_regnum;
00717 
00718   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (dr_regnum - DR0_REGNUM) * 2;
00719   return fp_regnum;
00720 }
00721 
00722 /* For pairs of floating point registers.  */
00723 static int
00724 sh64_fpp_reg_base_num (struct gdbarch *gdbarch, int fpp_regnum)
00725 {
00726   int fp_regnum;
00727 
00728   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fpp_regnum - FPP0_REGNUM) * 2;
00729   return fp_regnum;
00730 }
00731 
00732 /* *INDENT-OFF* */
00733 /*
00734     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
00735        GDB_REGNUM  BASE_REGNUM
00736  r0_c       221      0
00737  r1_c       222      1
00738  r2_c       223      2
00739  r3_c       224      3
00740  r4_c       225      4
00741  r5_c       226      5
00742  r6_c       227      6
00743  r7_c       228      7
00744  r8_c       229      8
00745  r9_c       230      9
00746  r10_c      231      10
00747  r11_c      232      11
00748  r12_c      233      12
00749  r13_c      234      13
00750  r14_c      235      14
00751  r15_c      236      15
00752 
00753  pc_c       237      64
00754  gbr_c      238      16
00755  mach_c     239      17
00756  macl_c     240      17
00757  pr_c       241      18
00758  t_c        242      19
00759  fpscr_c    243      76
00760  fpul_c     244      109
00761 
00762  fr0_c      245      77
00763  fr1_c      246      78
00764  fr2_c      247      79
00765  fr3_c      248      80
00766  fr4_c      249      81
00767  fr5_c      250      82
00768  fr6_c      251      83
00769  fr7_c      252      84
00770  fr8_c      253      85
00771  fr9_c      254      86
00772  fr10_c     255      87
00773  fr11_c     256      88
00774  fr12_c     257      89
00775  fr13_c     258      90
00776  fr14_c     259      91
00777  fr15_c     260      92
00778 
00779  dr0_c      261      77
00780  dr2_c      262      79
00781  dr4_c      263      81
00782  dr6_c      264      83
00783  dr8_c      265      85
00784  dr10_c     266      87
00785  dr12_c     267      89
00786  dr14_c     268      91
00787 
00788  fv0_c      269      77
00789  fv4_c      270      81
00790  fv8_c      271      85
00791  fv12_c     272      91
00792 */
00793 /* *INDENT-ON* */
00794 static int
00795 sh64_compact_reg_base_num (struct gdbarch *gdbarch, int reg_nr)
00796 {
00797   int base_regnum = reg_nr;
00798 
00799   /* general register N maps to general register N */
00800   if (reg_nr >= R0_C_REGNUM 
00801       && reg_nr <= R_LAST_C_REGNUM)
00802     base_regnum = reg_nr - R0_C_REGNUM;
00803 
00804   /* floating point register N maps to floating point register N */
00805   else if (reg_nr >= FP0_C_REGNUM 
00806             && reg_nr <= FP_LAST_C_REGNUM)
00807     base_regnum = reg_nr - FP0_C_REGNUM + gdbarch_fp0_regnum (gdbarch);
00808 
00809   /* double prec register N maps to base regnum for double prec register N */
00810   else if (reg_nr >= DR0_C_REGNUM 
00811             && reg_nr <= DR_LAST_C_REGNUM)
00812     base_regnum = sh64_dr_reg_base_num (gdbarch,
00813                                         DR0_REGNUM + reg_nr - DR0_C_REGNUM);
00814 
00815   /* vector N maps to base regnum for vector register N */
00816   else if (reg_nr >= FV0_C_REGNUM 
00817             && reg_nr <= FV_LAST_C_REGNUM)
00818     base_regnum = sh64_fv_reg_base_num (gdbarch,
00819                                         FV0_REGNUM + reg_nr - FV0_C_REGNUM);
00820 
00821   else if (reg_nr == PC_C_REGNUM)
00822     base_regnum = gdbarch_pc_regnum (gdbarch);
00823 
00824   else if (reg_nr == GBR_C_REGNUM) 
00825     base_regnum = 16;
00826 
00827   else if (reg_nr == MACH_C_REGNUM
00828            || reg_nr == MACL_C_REGNUM)
00829     base_regnum = 17;
00830 
00831   else if (reg_nr == PR_C_REGNUM) 
00832     base_regnum = PR_REGNUM;
00833 
00834   else if (reg_nr == T_C_REGNUM) 
00835     base_regnum = 19;
00836 
00837   else if (reg_nr == FPSCR_C_REGNUM) 
00838     base_regnum = FPSCR_REGNUM; /*???? this register is a mess.  */
00839 
00840   else if (reg_nr == FPUL_C_REGNUM) 
00841     base_regnum = gdbarch_fp0_regnum (gdbarch) + 32;
00842   
00843   return base_regnum;
00844 }
00845 
00846 static int
00847 sign_extend (int value, int bits)
00848 {
00849   value = value & ((1 << bits) - 1);
00850   return (value & (1 << (bits - 1))
00851           ? value | (~((1 << bits) - 1))
00852           : value);
00853 }
00854 
00855 static void
00856 sh64_analyze_prologue (struct gdbarch *gdbarch,
00857                        struct sh64_frame_cache *cache,
00858                        CORE_ADDR func_pc,
00859                        CORE_ADDR current_pc)
00860 {
00861   int pc;
00862   int opc;
00863   int insn;
00864   int r0_val = 0;
00865   int insn_size;
00866   int gdb_register_number;
00867   int register_number;
00868   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00869   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00870   
00871   cache->sp_offset = 0;
00872 
00873   /* Loop around examining the prologue insns until we find something
00874      that does not appear to be part of the prologue.  But give up
00875      after 20 of them, since we're getting silly then.  */
00876 
00877   pc = func_pc;
00878 
00879   if (cache->media_mode)
00880     insn_size = 4;
00881   else
00882     insn_size = 2;
00883 
00884   opc = pc + (insn_size * 28);
00885   if (opc > current_pc)
00886     opc = current_pc;
00887   for ( ; pc <= opc; pc += insn_size)
00888     {
00889       insn = read_memory_integer (cache->media_mode ? UNMAKE_ISA32_ADDR (pc)
00890                                                     : pc,
00891                                   insn_size, byte_order);
00892 
00893       if (!cache->media_mode)
00894         {
00895           if (IS_STS_PR (insn))
00896             {
00897               int next_insn = read_memory_integer (pc + insn_size,
00898                                                    insn_size, byte_order);
00899               if (IS_MOV_TO_R15 (next_insn))
00900                 {
00901                   cache->saved_regs[PR_REGNUM]
00902                     = cache->sp_offset - ((((next_insn & 0xf) ^ 0x8)
00903                                            - 0x8) << 2);
00904                   pc += insn_size;
00905                 }
00906             }
00907 
00908           else if (IS_MOV_R14 (insn))
00909             cache->saved_regs[MEDIA_FP_REGNUM] =
00910               cache->sp_offset - ((((insn & 0xf) ^ 0x8) - 0x8) << 2);
00911 
00912           else if (IS_MOV_R0 (insn))
00913             {
00914               /* Put in R0 the offset from SP at which to store some
00915                  registers.  We are interested in this value, because it
00916                  will tell us where the given registers are stored within
00917                  the frame.  */
00918               r0_val = ((insn & 0xff) ^ 0x80) - 0x80;
00919             }
00920 
00921           else if (IS_ADD_SP_R0 (insn))
00922             {
00923               /* This instruction still prepares r0, but we don't care.
00924                  We already have the offset in r0_val.  */
00925             }
00926 
00927           else if (IS_STS_R0 (insn))
00928             {
00929               /* Store PR at r0_val-4 from SP.  Decrement r0 by 4.  */
00930               cache->saved_regs[PR_REGNUM] = cache->sp_offset - (r0_val - 4);
00931               r0_val -= 4;
00932             }
00933 
00934           else if (IS_MOV_R14_R0 (insn))
00935             {
00936               /* Store R14 at r0_val-4 from SP.  Decrement r0 by 4.  */
00937               cache->saved_regs[MEDIA_FP_REGNUM] = cache->sp_offset
00938                                                    - (r0_val - 4);
00939               r0_val -= 4;
00940             }
00941 
00942           else if (IS_ADD_SP (insn))
00943             cache->sp_offset -= ((insn & 0xff) ^ 0x80) - 0x80;
00944 
00945           else if (IS_MOV_SP_FP (insn))
00946             break;
00947         }
00948       else
00949         {
00950           if (IS_ADDIL_SP_MEDIA (insn) || IS_ADDI_SP_MEDIA (insn))
00951             cache->sp_offset -=
00952               sign_extend ((((insn & 0xffc00) ^ 0x80000) - 0x80000) >> 10, 9);
00953 
00954           else if (IS_STQ_R18_R15 (insn))
00955             cache->saved_regs[PR_REGNUM]
00956               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
00957                                                  9) << 3);
00958 
00959           else if (IS_STL_R18_R15 (insn))
00960             cache->saved_regs[PR_REGNUM]
00961               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
00962                                                  9) << 2);
00963 
00964           else if (IS_STQ_R14_R15 (insn))
00965             cache->saved_regs[MEDIA_FP_REGNUM]
00966               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
00967                                                  9) << 3);
00968 
00969           else if (IS_STL_R14_R15 (insn))
00970             cache->saved_regs[MEDIA_FP_REGNUM]
00971               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
00972                                                  9) << 2);
00973 
00974           else if (IS_MOV_SP_FP_MEDIA (insn))
00975             break;
00976         }
00977     }
00978 
00979   if (cache->saved_regs[MEDIA_FP_REGNUM] >= 0)
00980     cache->uses_fp = 1;
00981 }
00982 
00983 static CORE_ADDR
00984 sh64_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
00985 {
00986   return sp & ~7;
00987 }
00988 
00989 /* Function: push_dummy_call
00990    Setup the function arguments for calling a function in the inferior.
00991 
00992    On the Renesas SH architecture, there are four registers (R4 to R7)
00993    which are dedicated for passing function arguments.  Up to the first
00994    four arguments (depending on size) may go into these registers.
00995    The rest go on the stack.
00996 
00997    Arguments that are smaller than 4 bytes will still take up a whole
00998    register or a whole 32-bit word on the stack, and will be 
00999    right-justified in the register or the stack word.  This includes
01000    chars, shorts, and small aggregate types.
01001 
01002    Arguments that are larger than 4 bytes may be split between two or 
01003    more registers.  If there are not enough registers free, an argument
01004    may be passed partly in a register (or registers), and partly on the
01005    stack.  This includes doubles, long longs, and larger aggregates.
01006    As far as I know, there is no upper limit to the size of aggregates 
01007    that will be passed in this way; in other words, the convention of 
01008    passing a pointer to a large aggregate instead of a copy is not used.
01009 
01010    An exceptional case exists for struct arguments (and possibly other
01011    aggregates such as arrays) if the size is larger than 4 bytes but 
01012    not a multiple of 4 bytes.  In this case the argument is never split 
01013    between the registers and the stack, but instead is copied in its
01014    entirety onto the stack, AND also copied into as many registers as 
01015    there is room for.  In other words, space in registers permitting, 
01016    two copies of the same argument are passed in.  As far as I can tell,
01017    only the one on the stack is used, although that may be a function 
01018    of the level of compiler optimization.  I suspect this is a compiler
01019    bug.  Arguments of these odd sizes are left-justified within the 
01020    word (as opposed to arguments smaller than 4 bytes, which are 
01021    right-justified).
01022 
01023    If the function is to return an aggregate type such as a struct, it 
01024    is either returned in the normal return value register R0 (if its 
01025    size is no greater than one byte), or else the caller must allocate
01026    space into which the callee will copy the return value (if the size
01027    is greater than one byte).  In this case, a pointer to the return 
01028    value location is passed into the callee in register R2, which does 
01029    not displace any of the other arguments passed in via registers R4
01030    to R7.  */
01031 
01032 /* R2-R9 for integer types and integer equivalent (char, pointers) and
01033    non-scalar (struct, union) elements (even if the elements are
01034    floats).  
01035    FR0-FR11 for single precision floating point (float)
01036    DR0-DR10 for double precision floating point (double) 
01037    
01038    If a float is argument number 3 (for instance) and arguments number
01039    1,2, and 4 are integer, the mapping will be:
01040    arg1 -->R2, arg2 --> R3, arg3 -->FR0, arg4 --> R5.  I.e. R4 is not used.
01041    
01042    If a float is argument number 10 (for instance) and arguments number
01043    1 through 10 are integer, the mapping will be:
01044    arg1->R2, arg2->R3, arg3->R4, arg4->R5, arg5->R6, arg6->R7, arg7->R8,
01045    arg8->R9, arg9->(0,SP)stack(8-byte aligned), arg10->FR0,
01046    arg11->stack(16,SP).  I.e. there is hole in the stack.
01047 
01048    Different rules apply for variable arguments functions, and for functions
01049    for which the prototype is not known.  */
01050 
01051 static CORE_ADDR
01052 sh64_push_dummy_call (struct gdbarch *gdbarch,
01053                       struct value *function,
01054                       struct regcache *regcache,
01055                       CORE_ADDR bp_addr,
01056                       int nargs, struct value **args,
01057                       CORE_ADDR sp, int struct_return,
01058                       CORE_ADDR struct_addr)
01059 {
01060   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01061   int stack_offset, stack_alloc;
01062   int int_argreg;
01063   int float_argreg;
01064   int double_argreg;
01065   int float_arg_index = 0;
01066   int double_arg_index = 0;
01067   int argnum;
01068   struct type *type;
01069   CORE_ADDR regval;
01070   const gdb_byte *val;
01071   gdb_byte valbuf[8];
01072   int len;
01073   int argreg_size;
01074   int fp_args[12];
01075 
01076   memset (fp_args, 0, sizeof (fp_args));
01077 
01078   /* First force sp to a 8-byte alignment.  */
01079   sp = sh64_frame_align (gdbarch, sp);
01080 
01081   /* The "struct return pointer" pseudo-argument has its own dedicated 
01082      register.  */
01083 
01084   if (struct_return)
01085     regcache_cooked_write_unsigned (regcache, 
01086                                     STRUCT_RETURN_REGNUM, struct_addr);
01087 
01088   /* Now make sure there's space on the stack.  */
01089   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
01090     stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 7) & ~7);
01091   sp -= stack_alloc;            /* Make room on stack for args.  */
01092 
01093   /* Now load as many as possible of the first arguments into
01094      registers, and push the rest onto the stack.  There are 64 bytes
01095      in eight registers available.  Loop thru args from first to last.  */
01096 
01097   int_argreg = ARG0_REGNUM;
01098   float_argreg = gdbarch_fp0_regnum (gdbarch);
01099   double_argreg = DR0_REGNUM;
01100 
01101   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
01102     {
01103       type = value_type (args[argnum]);
01104       len = TYPE_LENGTH (type);
01105       memset (valbuf, 0, sizeof (valbuf));
01106       
01107       if (TYPE_CODE (type) != TYPE_CODE_FLT)
01108         {
01109           argreg_size = register_size (gdbarch, int_argreg);
01110 
01111           if (len < argreg_size)
01112             {
01113               /* value gets right-justified in the register or stack word.  */
01114               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01115                 memcpy (valbuf + argreg_size - len,
01116                         value_contents (args[argnum]), len);
01117               else
01118                 memcpy (valbuf, value_contents (args[argnum]), len);
01119 
01120               val = valbuf;
01121             }
01122           else
01123             val = value_contents (args[argnum]);
01124 
01125           while (len > 0)
01126             {
01127               if (int_argreg > ARGLAST_REGNUM)
01128                 {                       
01129                   /* Must go on the stack.  */
01130                   write_memory (sp + stack_offset, val, argreg_size);
01131                   stack_offset += 8;/*argreg_size;*/
01132                 }
01133               /* NOTE WELL!!!!!  This is not an "else if" clause!!!
01134                  That's because some *&^%$ things get passed on the stack
01135                  AND in the registers!   */
01136               if (int_argreg <= ARGLAST_REGNUM)
01137                 {                       
01138                   /* There's room in a register.  */
01139                   regval = extract_unsigned_integer (val, argreg_size,
01140                                                      byte_order);
01141                   regcache_cooked_write_unsigned (regcache,
01142                                                   int_argreg, regval);
01143                 }
01144               /* Store the value 8 bytes at a time.  This means that
01145                  things larger than 8 bytes may go partly in registers
01146                  and partly on the stack.  FIXME: argreg is incremented
01147                  before we use its size.  */
01148               len -= argreg_size;
01149               val += argreg_size;
01150               int_argreg++;
01151             }
01152         }
01153       else
01154         {
01155           val = value_contents (args[argnum]);
01156           if (len == 4)
01157             {
01158               /* Where is it going to be stored?  */
01159               while (fp_args[float_arg_index])
01160                 float_arg_index ++;
01161 
01162               /* Now float_argreg points to the register where it
01163                  should be stored.  Are we still within the allowed
01164                  register set?  */
01165               if (float_arg_index <= FLOAT_ARGLAST_REGNUM)
01166                 {
01167                   /* Goes in FR0...FR11 */
01168                   regcache_cooked_write (regcache,
01169                                          gdbarch_fp0_regnum (gdbarch)
01170                                          + float_arg_index,
01171                                          val);
01172                   fp_args[float_arg_index] = 1;
01173                   /* Skip the corresponding general argument register.  */
01174                   int_argreg ++;
01175                 }
01176               else 
01177                 {
01178                   /* Store it as the integers, 8 bytes at the time, if
01179                      necessary spilling on the stack.  */
01180                 }
01181             }
01182             else if (len == 8)
01183               {
01184                 /* Where is it going to be stored?  */
01185                 while (fp_args[double_arg_index])
01186                   double_arg_index += 2;
01187                 /* Now double_argreg points to the register
01188                    where it should be stored.
01189                    Are we still within the allowed register set?  */
01190                 if (double_arg_index < FLOAT_ARGLAST_REGNUM)
01191                   {
01192                     /* Goes in DR0...DR10 */
01193                     /* The numbering of the DRi registers is consecutive,
01194                        i.e. includes odd numbers.  */
01195                     int double_register_offset = double_arg_index / 2;
01196                     int regnum = DR0_REGNUM + double_register_offset;
01197                     regcache_cooked_write (regcache, regnum, val);
01198                     fp_args[double_arg_index] = 1;
01199                     fp_args[double_arg_index + 1] = 1;
01200                     /* Skip the corresponding general argument register.  */
01201                     int_argreg ++;
01202                   }
01203                 else
01204                   {
01205                     /* Store it as the integers, 8 bytes at the time, if
01206                        necessary spilling on the stack.  */
01207                   }
01208               }
01209         }
01210     }
01211   /* Store return address.  */
01212   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
01213 
01214   /* Update stack pointer.  */
01215   regcache_cooked_write_unsigned (regcache,
01216                                   gdbarch_sp_regnum (gdbarch), sp);
01217 
01218   return sp;
01219 }
01220 
01221 /* Find a function's return value in the appropriate registers (in
01222    regbuf), and copy it into valbuf.  Extract from an array REGBUF
01223    containing the (raw) register state a function return value of type
01224    TYPE, and copy that, in virtual format, into VALBUF.  */
01225 static void
01226 sh64_extract_return_value (struct type *type, struct regcache *regcache,
01227                            void *valbuf)
01228 {
01229   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01230   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01231   int len = TYPE_LENGTH (type);
01232 
01233   if (TYPE_CODE (type) == TYPE_CODE_FLT)
01234     {
01235       if (len == 4)
01236         {
01237           /* Return value stored in gdbarch_fp0_regnum.  */
01238           regcache_raw_read (regcache,
01239                              gdbarch_fp0_regnum (gdbarch), valbuf);
01240         }
01241       else if (len == 8)
01242         {
01243           /* return value stored in DR0_REGNUM.  */
01244           DOUBLEST val;
01245           gdb_byte buf[8];
01246 
01247           regcache_cooked_read (regcache, DR0_REGNUM, buf);
01248           
01249           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
01250             floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
01251                                      buf, &val);
01252           else
01253             floatformat_to_doublest (&floatformat_ieee_double_big,
01254                                      buf, &val);
01255           store_typed_floating (valbuf, type, val);
01256         }
01257     }
01258   else
01259     { 
01260       if (len <= 8)
01261         {
01262           int offset;
01263           gdb_byte buf[8];
01264           /* Result is in register 2.  If smaller than 8 bytes, it is padded 
01265              at the most significant end.  */
01266           regcache_raw_read (regcache, DEFAULT_RETURN_REGNUM, buf);
01267 
01268           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01269             offset = register_size (gdbarch, DEFAULT_RETURN_REGNUM)
01270                      - len;
01271           else
01272             offset = 0;
01273           memcpy (valbuf, buf + offset, len);
01274         }
01275       else
01276         error (_("bad size for return value"));
01277     }
01278 }
01279 
01280 /* Write into appropriate registers a function return value
01281    of type TYPE, given in virtual format.
01282    If the architecture is sh4 or sh3e, store a function's return value
01283    in the R0 general register or in the FP0 floating point register,
01284    depending on the type of the return value.  In all the other cases
01285    the result is stored in r0, left-justified.  */
01286 
01287 static void
01288 sh64_store_return_value (struct type *type, struct regcache *regcache,
01289                          const gdb_byte *valbuf)
01290 {
01291   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01292   gdb_byte buf[64];     /* more than enough...  */
01293   int len = TYPE_LENGTH (type);
01294 
01295   if (TYPE_CODE (type) == TYPE_CODE_FLT)
01296     {
01297       int i, regnum = gdbarch_fp0_regnum (gdbarch);
01298       for (i = 0; i < len; i += 4)
01299         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
01300           regcache_raw_write (regcache, regnum++,
01301                               valbuf + len - 4 - i);
01302         else
01303           regcache_raw_write (regcache, regnum++, valbuf + i);
01304     }
01305   else
01306     {
01307       int return_register = DEFAULT_RETURN_REGNUM;
01308       int offset = 0;
01309 
01310       if (len <= register_size (gdbarch, return_register))
01311         {
01312           /* Pad with zeros.  */
01313           memset (buf, 0, register_size (gdbarch, return_register));
01314           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
01315             offset = 0; /*register_size (gdbarch, 
01316                           return_register) - len;*/
01317           else
01318             offset = register_size (gdbarch, return_register) - len;
01319 
01320           memcpy (buf + offset, valbuf, len);
01321           regcache_raw_write (regcache, return_register, buf);
01322         }
01323       else
01324         regcache_raw_write (regcache, return_register, valbuf);
01325     }
01326 }
01327 
01328 static enum return_value_convention
01329 sh64_return_value (struct gdbarch *gdbarch, struct value *function,
01330                    struct type *type, struct regcache *regcache,
01331                    gdb_byte *readbuf, const gdb_byte *writebuf)
01332 {
01333   if (sh64_use_struct_convention (type))
01334     return RETURN_VALUE_STRUCT_CONVENTION;
01335   if (writebuf)
01336     sh64_store_return_value (type, regcache, writebuf);
01337   else if (readbuf)
01338     sh64_extract_return_value (type, regcache, readbuf);
01339   return RETURN_VALUE_REGISTER_CONVENTION;
01340 }
01341 
01342 /* *INDENT-OFF* */
01343 /*
01344     SH MEDIA MODE (ISA 32)
01345     general registers (64-bit) 0-63
01346 0    r0,   r1,   r2,   r3,   r4,   r5,   r6,   r7,
01347 64   r8,   r9,   r10,  r11,  r12,  r13,  r14,  r15,
01348 128  r16,  r17,  r18,  r19,  r20,  r21,  r22,  r23,
01349 192  r24,  r25,  r26,  r27,  r28,  r29,  r30,  r31,
01350 256  r32,  r33,  r34,  r35,  r36,  r37,  r38,  r39,
01351 320  r40,  r41,  r42,  r43,  r44,  r45,  r46,  r47,
01352 384  r48,  r49,  r50,  r51,  r52,  r53,  r54,  r55,
01353 448  r56,  r57,  r58,  r59,  r60,  r61,  r62,  r63,
01354 
01355     pc (64-bit) 64
01356 512  pc,
01357 
01358     status reg., saved status reg., saved pc reg. (64-bit) 65-67
01359 520  sr,  ssr,  spc,
01360 
01361     target registers (64-bit) 68-75
01362 544  tr0,  tr1,  tr2,  tr3,  tr4,  tr5,  tr6,  tr7,
01363 
01364     floating point state control register (32-bit) 76
01365 608  fpscr,
01366 
01367     single precision floating point registers (32-bit) 77-140
01368 612  fr0,  fr1,  fr2,  fr3,  fr4,  fr5,  fr6,  fr7,
01369 644  fr8,  fr9,  fr10, fr11, fr12, fr13, fr14, fr15,
01370 676  fr16, fr17, fr18, fr19, fr20, fr21, fr22, fr23,
01371 708  fr24, fr25, fr26, fr27, fr28, fr29, fr30, fr31,
01372 740  fr32, fr33, fr34, fr35, fr36, fr37, fr38, fr39,
01373 772  fr40, fr41, fr42, fr43, fr44, fr45, fr46, fr47,
01374 804  fr48, fr49, fr50, fr51, fr52, fr53, fr54, fr55,
01375 836  fr56, fr57, fr58, fr59, fr60, fr61, fr62, fr63,
01376 
01377 TOTAL SPACE FOR REGISTERS: 868 bytes
01378 
01379 From here on they are all pseudo registers: no memory allocated.
01380 REGISTER_BYTE returns the register byte for the base register.
01381 
01382     double precision registers (pseudo) 141-172
01383      dr0,  dr2,  dr4,  dr6,  dr8,  dr10, dr12, dr14,
01384      dr16, dr18, dr20, dr22, dr24, dr26, dr28, dr30,
01385      dr32, dr34, dr36, dr38, dr40, dr42, dr44, dr46,
01386      dr48, dr50, dr52, dr54, dr56, dr58, dr60, dr62,
01387  
01388     floating point pairs (pseudo) 173-204
01389      fp0,  fp2,  fp4,  fp6,  fp8,  fp10, fp12, fp14,
01390      fp16, fp18, fp20, fp22, fp24, fp26, fp28, fp30,
01391      fp32, fp34, fp36, fp38, fp40, fp42, fp44, fp46,
01392      fp48, fp50, fp52, fp54, fp56, fp58, fp60, fp62,
01393  
01394     floating point vectors (4 floating point regs) (pseudo) 205-220
01395      fv0,  fv4,  fv8,  fv12, fv16, fv20, fv24, fv28,
01396      fv32, fv36, fv40, fv44, fv48, fv52, fv56, fv60,
01397  
01398     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
01399      r0_c, r1_c, r2_c,  r3_c,  r4_c,  r5_c,  r6_c,  r7_c,
01400      r8_c, r9_c, r10_c, r11_c, r12_c, r13_c, r14_c, r15_c,
01401      pc_c,
01402      gbr_c, mach_c, macl_c, pr_c, t_c,
01403      fpscr_c, fpul_c,
01404      fr0_c, fr1_c, fr2_c,  fr3_c,  fr4_c,  fr5_c,  fr6_c,  fr7_c,
01405      fr8_c, fr9_c, fr10_c, fr11_c, fr12_c, fr13_c, fr14_c, fr15_c
01406      dr0_c, dr2_c, dr4_c,  dr6_c,  dr8_c,  dr10_c, dr12_c, dr14_c
01407      fv0_c, fv4_c, fv8_c,  fv12_c
01408 */
01409 
01410 static struct type *
01411 sh64_build_float_register_type (struct gdbarch *gdbarch, int high)
01412 {
01413   return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
01414                                   0, high);
01415 }
01416 
01417 /* Return the GDB type object for the "standard" data type
01418    of data in register REG_NR.  */
01419 static struct type *
01420 sh64_register_type (struct gdbarch *gdbarch, int reg_nr)
01421 {
01422   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
01423        && reg_nr <= FP_LAST_REGNUM)
01424       || (reg_nr >= FP0_C_REGNUM
01425           && reg_nr <= FP_LAST_C_REGNUM))
01426     return builtin_type (gdbarch)->builtin_float;
01427   else if ((reg_nr >= DR0_REGNUM 
01428             && reg_nr <= DR_LAST_REGNUM)
01429            || (reg_nr >= DR0_C_REGNUM 
01430                && reg_nr <= DR_LAST_C_REGNUM))
01431     return builtin_type (gdbarch)->builtin_double;
01432   else if  (reg_nr >= FPP0_REGNUM 
01433             && reg_nr <= FPP_LAST_REGNUM)
01434     return sh64_build_float_register_type (gdbarch, 1);
01435   else if ((reg_nr >= FV0_REGNUM
01436             && reg_nr <= FV_LAST_REGNUM)
01437            ||(reg_nr >= FV0_C_REGNUM 
01438               && reg_nr <= FV_LAST_C_REGNUM))
01439     return sh64_build_float_register_type (gdbarch, 3);
01440   else if (reg_nr == FPSCR_REGNUM)
01441     return builtin_type (gdbarch)->builtin_int;
01442   else if (reg_nr >= R0_C_REGNUM
01443            && reg_nr < FP0_C_REGNUM)
01444     return builtin_type (gdbarch)->builtin_int;
01445   else
01446     return builtin_type (gdbarch)->builtin_long_long;
01447 }
01448 
01449 static void
01450 sh64_register_convert_to_virtual (struct gdbarch *gdbarch, int regnum,
01451                                   struct type *type, gdb_byte *from, gdb_byte *to)
01452 {
01453   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
01454     {
01455       /* It is a no-op.  */
01456       memcpy (to, from, register_size (gdbarch, regnum));
01457       return;
01458     }
01459 
01460   if ((regnum >= DR0_REGNUM 
01461        && regnum <= DR_LAST_REGNUM)
01462       || (regnum >= DR0_C_REGNUM 
01463           && regnum <= DR_LAST_C_REGNUM))
01464     {
01465       DOUBLEST val;
01466       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword, 
01467                                from, &val);
01468       store_typed_floating (to, type, val);
01469     }
01470   else
01471     error (_("sh64_register_convert_to_virtual "
01472              "called with non DR register number"));
01473 }
01474 
01475 static void
01476 sh64_register_convert_to_raw (struct gdbarch *gdbarch, struct type *type,
01477                               int regnum, const void *from, void *to)
01478 {
01479   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
01480     {
01481       /* It is a no-op.  */
01482       memcpy (to, from, register_size (gdbarch, regnum));
01483       return;
01484     }
01485 
01486   if ((regnum >= DR0_REGNUM 
01487        && regnum <= DR_LAST_REGNUM)
01488       || (regnum >= DR0_C_REGNUM 
01489           && regnum <= DR_LAST_C_REGNUM))
01490     {
01491       DOUBLEST val = extract_typed_floating (from, type);
01492       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword, 
01493                                  &val, to);
01494     }
01495   else
01496     error (_("sh64_register_convert_to_raw called "
01497              "with non DR register number"));
01498 }
01499 
01500 /* Concatenate PORTIONS contiguous raw registers starting at
01501    BASE_REGNUM into BUFFER.  */
01502 
01503 static enum register_status
01504 pseudo_register_read_portions (struct gdbarch *gdbarch,
01505                                struct regcache *regcache,
01506                                int portions,
01507                                int base_regnum, gdb_byte *buffer)
01508 {
01509   int portion;
01510 
01511   for (portion = 0; portion < portions; portion++)
01512     {
01513       enum register_status status;
01514       gdb_byte *b;
01515 
01516       b = buffer + register_size (gdbarch, base_regnum) * portion;
01517       status = regcache_raw_read (regcache, base_regnum + portion, b);
01518       if (status != REG_VALID)
01519         return status;
01520     }
01521 
01522   return REG_VALID;
01523 }
01524 
01525 static enum register_status
01526 sh64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
01527                            int reg_nr, gdb_byte *buffer)
01528 {
01529   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01530   int base_regnum;
01531   int offset = 0;
01532   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
01533   enum register_status status;
01534 
01535   if (reg_nr >= DR0_REGNUM 
01536       && reg_nr <= DR_LAST_REGNUM)
01537     {
01538       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);
01539 
01540       /* Build the value in the provided buffer.  */ 
01541       /* DR regs are double precision registers obtained by
01542          concatenating 2 single precision floating point registers.  */
01543       status = pseudo_register_read_portions (gdbarch, regcache,
01544                                               2, base_regnum, temp_buffer);
01545       if (status == REG_VALID)
01546         {
01547           /* We must pay attention to the endianness.  */
01548           sh64_register_convert_to_virtual (gdbarch, reg_nr,
01549                                             register_type (gdbarch, reg_nr),
01550                                             temp_buffer, buffer);
01551         }
01552 
01553       return status;
01554     }
01555 
01556   else if (reg_nr >= FPP0_REGNUM
01557            && reg_nr <= FPP_LAST_REGNUM)
01558     {
01559       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);
01560 
01561       /* Build the value in the provided buffer.  */ 
01562       /* FPP regs are pairs of single precision registers obtained by
01563          concatenating 2 single precision floating point registers.  */
01564       return pseudo_register_read_portions (gdbarch, regcache,
01565                                             2, base_regnum, buffer);
01566     }
01567 
01568   else if (reg_nr >= FV0_REGNUM 
01569            && reg_nr <= FV_LAST_REGNUM)
01570     {
01571       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);
01572 
01573       /* Build the value in the provided buffer.  */ 
01574       /* FV regs are vectors of single precision registers obtained by
01575          concatenating 4 single precision floating point registers.  */
01576       return pseudo_register_read_portions (gdbarch, regcache,
01577                                             4, base_regnum, buffer);
01578     }
01579 
01580   /* sh compact pseudo registers.  1-to-1 with a shmedia register.  */
01581   else if (reg_nr >= R0_C_REGNUM 
01582            && reg_nr <= T_C_REGNUM)
01583     {
01584       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01585 
01586       /* Build the value in the provided buffer.  */ 
01587       status = regcache_raw_read (regcache, base_regnum, temp_buffer);
01588       if (status != REG_VALID)
01589         return status;
01590       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01591         offset = 4;
01592       memcpy (buffer,
01593               temp_buffer + offset, 4); /* get LOWER 32 bits only????  */
01594       return REG_VALID;
01595     }
01596 
01597   else if (reg_nr >= FP0_C_REGNUM
01598            && reg_nr <= FP_LAST_C_REGNUM)
01599     {
01600       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01601 
01602       /* Build the value in the provided buffer.  */ 
01603       /* Floating point registers map 1-1 to the media fp regs,
01604          they have the same size and endianness.  */
01605       return regcache_raw_read (regcache, base_regnum, buffer);
01606     }
01607 
01608   else if (reg_nr >= DR0_C_REGNUM 
01609            && reg_nr <= DR_LAST_C_REGNUM)
01610     {
01611       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01612 
01613       /* DR_C regs are double precision registers obtained by
01614          concatenating 2 single precision floating point registers.  */
01615       status = pseudo_register_read_portions (gdbarch, regcache,
01616                                               2, base_regnum, temp_buffer);
01617       if (status == REG_VALID)
01618         {
01619           /* We must pay attention to the endianness.  */
01620           sh64_register_convert_to_virtual (gdbarch, reg_nr,
01621                                             register_type (gdbarch, reg_nr),
01622                                             temp_buffer, buffer);
01623         }
01624       return status;
01625     }
01626 
01627   else if (reg_nr >= FV0_C_REGNUM 
01628            && reg_nr <= FV_LAST_C_REGNUM)
01629     {
01630       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01631 
01632       /* Build the value in the provided buffer.  */ 
01633       /* FV_C regs are vectors of single precision registers obtained by
01634          concatenating 4 single precision floating point registers.  */
01635       return pseudo_register_read_portions (gdbarch, regcache,
01636                                             4, base_regnum, buffer);
01637     }
01638 
01639   else if (reg_nr == FPSCR_C_REGNUM)
01640     {
01641       int fpscr_base_regnum;
01642       int sr_base_regnum;
01643       unsigned int fpscr_value;
01644       unsigned int sr_value;
01645       unsigned int fpscr_c_value;
01646       unsigned int fpscr_c_part1_value;
01647       unsigned int fpscr_c_part2_value;
01648 
01649       fpscr_base_regnum = FPSCR_REGNUM;
01650       sr_base_regnum = SR_REGNUM;
01651 
01652       /* Build the value in the provided buffer.  */ 
01653       /* FPSCR_C is a very weird register that contains sparse bits
01654          from the FPSCR and the SR architectural registers.
01655          Specifically: */
01656       /* *INDENT-OFF* */
01657       /*
01658          FPSRC_C bit
01659             0         Bit 0 of FPSCR
01660             1         reserved
01661             2-17      Bit 2-18 of FPSCR
01662             18-20     Bits 12,13,14 of SR
01663             21-31     reserved
01664        */
01665       /* *INDENT-ON* */
01666       /* Get FPSCR into a local buffer.  */
01667       status = regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
01668       if (status != REG_VALID)
01669         return status;
01670       /* Get value as an int.  */
01671       fpscr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
01672       /* Get SR into a local buffer */
01673       status = regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
01674       if (status != REG_VALID)
01675         return status;
01676       /* Get value as an int.  */
01677       sr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
01678       /* Build the new value.  */
01679       fpscr_c_part1_value = fpscr_value & 0x3fffd;
01680       fpscr_c_part2_value = (sr_value & 0x7000) << 6;
01681       fpscr_c_value = fpscr_c_part1_value | fpscr_c_part2_value;
01682       /* Store that in out buffer!!!  */
01683       store_unsigned_integer (buffer, 4, byte_order, fpscr_c_value);
01684       /* FIXME There is surely an endianness gotcha here.  */
01685 
01686       return REG_VALID;
01687     }
01688 
01689   else if (reg_nr == FPUL_C_REGNUM)
01690     {
01691       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01692 
01693       /* FPUL_C register is floating point register 32,
01694          same size, same endianness.  */
01695       return regcache_raw_read (regcache, base_regnum, buffer);
01696     }
01697   else
01698     gdb_assert_not_reached ("invalid pseudo register number");
01699 }
01700 
01701 static void
01702 sh64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
01703                             int reg_nr, const gdb_byte *buffer)
01704 {
01705   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01706   int base_regnum, portion;
01707   int offset;
01708   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
01709 
01710   if (reg_nr >= DR0_REGNUM
01711       && reg_nr <= DR_LAST_REGNUM)
01712     {
01713       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);
01714       /* We must pay attention to the endianness.  */
01715       sh64_register_convert_to_raw (gdbarch, register_type (gdbarch, reg_nr),
01716                                     reg_nr,
01717                                     buffer, temp_buffer);
01718 
01719       /* Write the real regs for which this one is an alias.  */
01720       for (portion = 0; portion < 2; portion++)
01721         regcache_raw_write (regcache, base_regnum + portion, 
01722                             (temp_buffer
01723                              + register_size (gdbarch,
01724                                               base_regnum) * portion));
01725     }
01726 
01727   else if (reg_nr >= FPP0_REGNUM 
01728            && reg_nr <= FPP_LAST_REGNUM)
01729     {
01730       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);
01731 
01732       /* Write the real regs for which this one is an alias.  */
01733       for (portion = 0; portion < 2; portion++)
01734         regcache_raw_write (regcache, base_regnum + portion,
01735                             (buffer + register_size (gdbarch,
01736                                                      base_regnum) * portion));
01737     }
01738 
01739   else if (reg_nr >= FV0_REGNUM
01740            && reg_nr <= FV_LAST_REGNUM)
01741     {
01742       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);
01743 
01744       /* Write the real regs for which this one is an alias.  */
01745       for (portion = 0; portion < 4; portion++)
01746         regcache_raw_write (regcache, base_regnum + portion,
01747                             (buffer + register_size (gdbarch,
01748                                                      base_regnum) * portion));
01749     }
01750 
01751   /* sh compact general pseudo registers.  1-to-1 with a shmedia
01752      register but only 4 bytes of it.  */
01753   else if (reg_nr >= R0_C_REGNUM 
01754            && reg_nr <= T_C_REGNUM)
01755     {
01756       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01757       /* reg_nr is 32 bit here, and base_regnum is 64 bits.  */
01758       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01759         offset = 4;
01760       else 
01761         offset = 0;
01762       /* Let's read the value of the base register into a temporary
01763          buffer, so that overwriting the last four bytes with the new
01764          value of the pseudo will leave the upper 4 bytes unchanged.  */
01765       regcache_raw_read (regcache, base_regnum, temp_buffer);
01766       /* Write as an 8 byte quantity.  */
01767       memcpy (temp_buffer + offset, buffer, 4);
01768       regcache_raw_write (regcache, base_regnum, temp_buffer);
01769     }
01770 
01771   /* sh floating point compact pseudo registers.  1-to-1 with a shmedia
01772      registers.  Both are 4 bytes.  */
01773   else if (reg_nr >= FP0_C_REGNUM
01774                && reg_nr <= FP_LAST_C_REGNUM)
01775     {
01776       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01777       regcache_raw_write (regcache, base_regnum, buffer);
01778     }
01779 
01780   else if (reg_nr >= DR0_C_REGNUM 
01781            && reg_nr <= DR_LAST_C_REGNUM)
01782     {
01783       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01784       for (portion = 0; portion < 2; portion++)
01785         {
01786           /* We must pay attention to the endianness.  */
01787           sh64_register_convert_to_raw (gdbarch,
01788                                         register_type (gdbarch, reg_nr),
01789                                         reg_nr,
01790                                         buffer, temp_buffer);
01791 
01792           regcache_raw_write (regcache, base_regnum + portion,
01793                               (temp_buffer
01794                                + register_size (gdbarch, 
01795                                                 base_regnum) * portion));
01796         }
01797     }
01798 
01799   else if (reg_nr >= FV0_C_REGNUM 
01800            && reg_nr <= FV_LAST_C_REGNUM)
01801     {
01802       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01803      
01804       for (portion = 0; portion < 4; portion++)
01805         {
01806           regcache_raw_write (regcache, base_regnum + portion,
01807                               (buffer
01808                                + register_size (gdbarch, 
01809                                                 base_regnum) * portion));
01810         }
01811     }
01812 
01813   else if (reg_nr == FPSCR_C_REGNUM)
01814     {      
01815       int fpscr_base_regnum;
01816       int sr_base_regnum;
01817       unsigned int fpscr_value;
01818       unsigned int sr_value;
01819       unsigned int old_fpscr_value;
01820       unsigned int old_sr_value;
01821       unsigned int fpscr_c_value;
01822       unsigned int fpscr_mask;
01823       unsigned int sr_mask;
01824 
01825       fpscr_base_regnum = FPSCR_REGNUM;
01826       sr_base_regnum = SR_REGNUM;
01827 
01828       /* FPSCR_C is a very weird register that contains sparse bits
01829          from the FPSCR and the SR architectural registers.
01830          Specifically: */
01831       /* *INDENT-OFF* */
01832       /*
01833          FPSRC_C bit
01834             0         Bit 0 of FPSCR
01835             1         reserved
01836             2-17      Bit 2-18 of FPSCR
01837             18-20     Bits 12,13,14 of SR
01838             21-31     reserved
01839        */
01840       /* *INDENT-ON* */
01841       /* Get value as an int.  */
01842       fpscr_c_value = extract_unsigned_integer (buffer, 4, byte_order);
01843 
01844       /* Build the new values.  */
01845       fpscr_mask = 0x0003fffd;
01846       sr_mask = 0x001c0000;
01847        
01848       fpscr_value = fpscr_c_value & fpscr_mask;
01849       sr_value = (fpscr_value & sr_mask) >> 6;
01850       
01851       regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
01852       old_fpscr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
01853       old_fpscr_value &= 0xfffc0002;
01854       fpscr_value |= old_fpscr_value;
01855       store_unsigned_integer (temp_buffer, 4, byte_order, fpscr_value);
01856       regcache_raw_write (regcache, fpscr_base_regnum, temp_buffer);
01857       
01858       regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
01859       old_sr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
01860       old_sr_value &= 0xffff8fff;
01861       sr_value |= old_sr_value;
01862       store_unsigned_integer (temp_buffer, 4, byte_order, sr_value);
01863       regcache_raw_write (regcache, sr_base_regnum, temp_buffer);
01864     }
01865 
01866   else if (reg_nr == FPUL_C_REGNUM)
01867     {
01868       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
01869       regcache_raw_write (regcache, base_regnum, buffer);
01870     }
01871 }
01872 
01873 /* FIXME:!! THIS SHOULD TAKE CARE OF GETTING THE RIGHT PORTION OF THE
01874    shmedia REGISTERS.  */
01875 /* Control registers, compact mode.  */
01876 static void
01877 sh64_do_cr_c_register_info (struct ui_file *file, struct frame_info *frame,
01878                             int cr_c_regnum)
01879 {
01880   switch (cr_c_regnum)
01881     {
01882     case PC_C_REGNUM:
01883       fprintf_filtered (file, "pc_c\t0x%08x\n",
01884           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01885       break;
01886     case GBR_C_REGNUM: 
01887       fprintf_filtered (file, "gbr_c\t0x%08x\n",
01888           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01889       break;
01890     case MACH_C_REGNUM: 
01891       fprintf_filtered (file, "mach_c\t0x%08x\n",
01892           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01893       break;
01894     case MACL_C_REGNUM: 
01895       fprintf_filtered (file, "macl_c\t0x%08x\n",
01896           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01897       break;
01898     case PR_C_REGNUM: 
01899       fprintf_filtered (file, "pr_c\t0x%08x\n",
01900           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01901       break;
01902     case T_C_REGNUM: 
01903       fprintf_filtered (file, "t_c\t0x%08x\n",
01904           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01905       break;
01906     case FPSCR_C_REGNUM: 
01907       fprintf_filtered (file, "fpscr_c\t0x%08x\n",
01908           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01909       break;
01910     case FPUL_C_REGNUM:
01911       fprintf_filtered (file, "fpul_c\t0x%08x\n",
01912           (int) get_frame_register_unsigned (frame, cr_c_regnum));
01913       break;
01914     }
01915 }
01916 
01917 static void
01918 sh64_do_fp_register (struct gdbarch *gdbarch, struct ui_file *file,
01919                      struct frame_info *frame, int regnum)
01920 {                               /* Do values for FP (float) regs.  */
01921   unsigned char *raw_buffer;
01922   double flt;   /* Double extracted from raw hex data.  */
01923   int inv;
01924   int j;
01925 
01926   /* Allocate space for the float.  */
01927   raw_buffer = (unsigned char *)
01928     alloca (register_size (gdbarch, gdbarch_fp0_regnum (gdbarch)));
01929 
01930   /* Get the data in raw format.  */
01931   if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
01932     error (_("can't read register %d (%s)"),
01933            regnum, gdbarch_register_name (gdbarch, regnum));
01934 
01935   /* Get the register as a number.  */ 
01936   flt = unpack_double (builtin_type (gdbarch)->builtin_float,
01937                        raw_buffer, &inv);
01938 
01939   /* Print the name and some spaces.  */
01940   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
01941   print_spaces_filtered (15 - strlen (gdbarch_register_name
01942                                         (gdbarch, regnum)), file);
01943 
01944   /* Print the value.  */
01945   if (inv)
01946     fprintf_filtered (file, "<invalid float>");
01947   else
01948     fprintf_filtered (file, "%-10.9g", flt);
01949 
01950   /* Print the fp register as hex.  */
01951   fprintf_filtered (file, "\t(raw ");
01952   print_hex_chars (file, raw_buffer,
01953                    register_size (gdbarch, regnum),
01954                    gdbarch_byte_order (gdbarch));
01955   fprintf_filtered (file, ")");
01956   fprintf_filtered (file, "\n");
01957 }
01958 
01959 static void
01960 sh64_do_pseudo_register (struct gdbarch *gdbarch, struct ui_file *file,
01961                          struct frame_info *frame, int regnum)
01962 {
01963   /* All the sh64-compact mode registers are pseudo registers.  */
01964 
01965   if (regnum < gdbarch_num_regs (gdbarch)
01966       || regnum >= gdbarch_num_regs (gdbarch)
01967                    + NUM_PSEUDO_REGS_SH_MEDIA
01968                    + NUM_PSEUDO_REGS_SH_COMPACT)
01969     internal_error (__FILE__, __LINE__,
01970                     _("Invalid pseudo register number %d\n"), regnum);
01971 
01972   else if ((regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM))
01973     {
01974       int fp_regnum = sh64_dr_reg_base_num (gdbarch, regnum);
01975       fprintf_filtered (file, "dr%d\t0x%08x%08x\n", regnum - DR0_REGNUM, 
01976           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
01977           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
01978     }
01979 
01980   else if ((regnum >= DR0_C_REGNUM && regnum <= DR_LAST_C_REGNUM))
01981     {
01982       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
01983       fprintf_filtered (file, "dr%d_c\t0x%08x%08x\n", regnum - DR0_C_REGNUM,
01984           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
01985           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
01986     }
01987 
01988   else if ((regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM))
01989     {
01990       int fp_regnum = sh64_fv_reg_base_num (gdbarch, regnum);
01991       fprintf_filtered (file, "fv%d\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n", 
01992            regnum - FV0_REGNUM, 
01993            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
01994            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
01995            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
01996            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
01997     }
01998            
01999   else if ((regnum >= FV0_C_REGNUM && regnum <= FV_LAST_C_REGNUM))
02000     {
02001       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
02002       fprintf_filtered (file, "fv%d_c\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n", 
02003            regnum - FV0_C_REGNUM, 
02004            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
02005            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
02006            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
02007            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
02008     }
02009 
02010   else if (regnum >= FPP0_REGNUM && regnum <= FPP_LAST_REGNUM)
02011     {
02012       int fp_regnum = sh64_fpp_reg_base_num (gdbarch, regnum);
02013       fprintf_filtered (file, "fpp%d\t0x%08x\t0x%08x\n", regnum - FPP0_REGNUM, 
02014           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
02015           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
02016     }
02017 
02018   else if (regnum >= R0_C_REGNUM && regnum <= R_LAST_C_REGNUM)
02019     {
02020       int c_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
02021       fprintf_filtered (file, "r%d_c\t0x%08x\n", regnum - R0_C_REGNUM, 
02022            (unsigned) get_frame_register_unsigned (frame, c_regnum));
02023     }
02024   else if (regnum >= FP0_C_REGNUM && regnum <= FP_LAST_C_REGNUM)
02025     /* This should work also for pseudoregs.  */
02026     sh64_do_fp_register (gdbarch, file, frame, regnum);
02027   else if (regnum >= PC_C_REGNUM && regnum <= FPUL_C_REGNUM)
02028     sh64_do_cr_c_register_info (file, frame, regnum);
02029 }
02030 
02031 static void
02032 sh64_do_register (struct gdbarch *gdbarch, struct ui_file *file,
02033                   struct frame_info *frame, int regnum)
02034 {
02035   unsigned char raw_buffer[MAX_REGISTER_SIZE];
02036   struct value_print_options opts;
02037 
02038   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
02039   print_spaces_filtered (15 - strlen (gdbarch_register_name
02040                                       (gdbarch, regnum)), file);
02041 
02042   /* Get the data in raw format.  */
02043   if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
02044     {
02045       fprintf_filtered (file, "*value not available*\n");
02046       return;
02047     }
02048 
02049   get_formatted_print_options (&opts, 'x');
02050   opts.deref_ref = 1;
02051   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
02052              file, 0, NULL, &opts, current_language);
02053   fprintf_filtered (file, "\t");
02054   get_formatted_print_options (&opts, 0);
02055   opts.deref_ref = 1;
02056   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
02057              file, 0, NULL, &opts, current_language);
02058   fprintf_filtered (file, "\n");
02059 }
02060 
02061 static void
02062 sh64_print_register (struct gdbarch *gdbarch, struct ui_file *file,
02063                      struct frame_info *frame, int regnum)
02064 {
02065   if (regnum < 0 || regnum >= gdbarch_num_regs (gdbarch)
02066                               + gdbarch_num_pseudo_regs (gdbarch))
02067     internal_error (__FILE__, __LINE__,
02068                     _("Invalid register number %d\n"), regnum);
02069 
02070   else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
02071     {
02072       if (TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT)
02073         sh64_do_fp_register (gdbarch, file, frame, regnum);     /* FP regs */
02074       else
02075         sh64_do_register (gdbarch, file, frame, regnum);
02076     }
02077 
02078   else if (regnum < gdbarch_num_regs (gdbarch)
02079                     + gdbarch_num_pseudo_regs (gdbarch))
02080     sh64_do_pseudo_register (gdbarch, file, frame, regnum);
02081 }
02082 
02083 static void
02084 sh64_media_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
02085                                  struct frame_info *frame, int regnum,
02086                                  int fpregs)
02087 {
02088   if (regnum != -1)             /* Do one specified register.  */
02089     {
02090       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
02091         error (_("Not a valid register for the current processor type"));
02092 
02093       sh64_print_register (gdbarch, file, frame, regnum);
02094     }
02095   else
02096     /* Do all (or most) registers.  */
02097     {
02098       regnum = 0;
02099       while (regnum < gdbarch_num_regs (gdbarch))
02100         {
02101           /* If the register name is empty, it is undefined for this
02102              processor, so don't display anything.  */
02103           if (gdbarch_register_name (gdbarch, regnum) == NULL
02104               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
02105             { 
02106               regnum++;
02107               continue;
02108             }
02109 
02110           if (TYPE_CODE (register_type (gdbarch, regnum))
02111               == TYPE_CODE_FLT)
02112             {
02113               if (fpregs)
02114                 {
02115                   /* true for "INFO ALL-REGISTERS" command.  */
02116                   sh64_do_fp_register (gdbarch, file, frame, regnum);
02117                   regnum ++;
02118                 }
02119               else
02120                 regnum += FP_LAST_REGNUM - gdbarch_fp0_regnum (gdbarch);
02121                 /* skip FP regs */
02122             }
02123           else
02124             {
02125               sh64_do_register (gdbarch, file, frame, regnum);
02126               regnum++;
02127             }
02128         }
02129 
02130       if (fpregs)
02131         while (regnum < gdbarch_num_regs (gdbarch)
02132                         + gdbarch_num_pseudo_regs (gdbarch))
02133           {
02134             sh64_do_pseudo_register (gdbarch, file, frame, regnum);
02135             regnum++;
02136           }
02137     }
02138 }
02139 
02140 static void
02141 sh64_compact_print_registers_info (struct gdbarch *gdbarch,
02142                                    struct ui_file *file,
02143                                    struct frame_info *frame, int regnum,
02144                                    int fpregs)
02145 {
02146   if (regnum != -1)             /* Do one specified register.  */
02147     {
02148       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
02149         error (_("Not a valid register for the current processor type"));
02150 
02151       if (regnum >= 0 && regnum < R0_C_REGNUM)
02152         error (_("Not a valid register for the current processor mode."));
02153 
02154       sh64_print_register (gdbarch, file, frame, regnum);
02155     }
02156   else
02157     /* Do all compact registers.  */
02158     {
02159       regnum = R0_C_REGNUM;
02160       while (regnum < gdbarch_num_regs (gdbarch)
02161                       + gdbarch_num_pseudo_regs (gdbarch))
02162         {
02163           sh64_do_pseudo_register (gdbarch, file, frame, regnum);
02164           regnum++;
02165         }
02166     }
02167 }
02168 
02169 static void
02170 sh64_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
02171                            struct frame_info *frame, int regnum, int fpregs)
02172 {
02173   if (pc_is_isa32 (get_frame_pc (frame)))
02174     sh64_media_print_registers_info (gdbarch, file, frame, regnum, fpregs);
02175   else
02176     sh64_compact_print_registers_info (gdbarch, file, frame, regnum, fpregs);
02177 }
02178 
02179 static struct sh64_frame_cache *
02180 sh64_alloc_frame_cache (void)
02181 {
02182   struct sh64_frame_cache *cache;
02183   int i;
02184 
02185   cache = FRAME_OBSTACK_ZALLOC (struct sh64_frame_cache);
02186 
02187   /* Base address.  */
02188   cache->base = 0;
02189   cache->saved_sp = 0;
02190   cache->sp_offset = 0;
02191   cache->pc = 0;
02192 
02193   /* Frameless until proven otherwise.  */
02194   cache->uses_fp = 0;
02195 
02196   /* Saved registers.  We initialize these to -1 since zero is a valid
02197      offset (that's where fp is supposed to be stored).  */
02198   for (i = 0; i < SIM_SH64_NR_REGS; i++)
02199     {
02200       cache->saved_regs[i] = -1;
02201     }
02202 
02203   return cache;
02204 }
02205 
02206 static struct sh64_frame_cache *
02207 sh64_frame_cache (struct frame_info *this_frame, void **this_cache)
02208 {
02209   struct gdbarch *gdbarch;
02210   struct sh64_frame_cache *cache;
02211   CORE_ADDR current_pc;
02212   int i;
02213 
02214   if (*this_cache)
02215     return *this_cache;
02216 
02217   gdbarch = get_frame_arch (this_frame);
02218   cache = sh64_alloc_frame_cache ();
02219   *this_cache = cache;
02220 
02221   current_pc = get_frame_pc (this_frame);
02222   cache->media_mode = pc_is_isa32 (current_pc);
02223 
02224   /* In principle, for normal frames, fp holds the frame pointer,
02225      which holds the base address for the current stack frame.
02226      However, for functions that don't need it, the frame pointer is
02227      optional.  For these "frameless" functions the frame pointer is
02228      actually the frame pointer of the calling frame.  */
02229   cache->base = get_frame_register_unsigned (this_frame, MEDIA_FP_REGNUM);
02230   if (cache->base == 0)
02231     return cache;
02232 
02233   cache->pc = get_frame_func (this_frame);
02234   if (cache->pc != 0)
02235     sh64_analyze_prologue (gdbarch, cache, cache->pc, current_pc);
02236 
02237   if (!cache->uses_fp)
02238     {
02239       /* We didn't find a valid frame, which means that CACHE->base
02240          currently holds the frame pointer for our calling frame.  If
02241          we're at the start of a function, or somewhere half-way its
02242          prologue, the function's frame probably hasn't been fully
02243          setup yet.  Try to reconstruct the base address for the stack
02244          frame by looking at the stack pointer.  For truly "frameless"
02245          functions this might work too.  */
02246       cache->base = get_frame_register_unsigned
02247                     (this_frame, gdbarch_sp_regnum (gdbarch));
02248     }
02249 
02250   /* Now that we have the base address for the stack frame we can
02251      calculate the value of sp in the calling frame.  */
02252   cache->saved_sp = cache->base + cache->sp_offset;
02253 
02254   /* Adjust all the saved registers such that they contain addresses
02255      instead of offsets.  */
02256   for (i = 0; i < SIM_SH64_NR_REGS; i++)
02257     if (cache->saved_regs[i] != -1)
02258       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i];
02259 
02260   return cache;
02261 }
02262 
02263 static struct value *
02264 sh64_frame_prev_register (struct frame_info *this_frame,
02265                           void **this_cache, int regnum)
02266 {
02267   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
02268   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02269   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02270 
02271   gdb_assert (regnum >= 0);
02272 
02273   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
02274     frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
02275 
02276   /* The PC of the previous frame is stored in the PR register of
02277      the current frame.  Frob regnum so that we pull the value from
02278      the correct place.  */
02279   if (regnum == gdbarch_pc_regnum (gdbarch))
02280     regnum = PR_REGNUM;
02281 
02282   if (regnum < SIM_SH64_NR_REGS && cache->saved_regs[regnum] != -1)
02283     {
02284       if (gdbarch_tdep (gdbarch)->sh_abi == SH_ABI_32
02285           && (regnum == MEDIA_FP_REGNUM || regnum == PR_REGNUM))
02286         {
02287           CORE_ADDR val;
02288           val = read_memory_unsigned_integer (cache->saved_regs[regnum],
02289                                               4, byte_order);
02290           return frame_unwind_got_constant (this_frame, regnum, val);
02291         }
02292 
02293       return frame_unwind_got_memory (this_frame, regnum,
02294                                       cache->saved_regs[regnum]);
02295     }
02296 
02297   return frame_unwind_got_register (this_frame, regnum, regnum);
02298 }
02299 
02300 static void
02301 sh64_frame_this_id (struct frame_info *this_frame, void **this_cache,
02302                     struct frame_id *this_id)
02303 {
02304   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
02305 
02306   /* This marks the outermost frame.  */
02307   if (cache->base == 0)
02308     return;
02309 
02310   *this_id = frame_id_build (cache->saved_sp, cache->pc);
02311 }
02312 
02313 static const struct frame_unwind sh64_frame_unwind = {
02314   NORMAL_FRAME,
02315   default_frame_unwind_stop_reason,
02316   sh64_frame_this_id,
02317   sh64_frame_prev_register,
02318   NULL,
02319   default_frame_sniffer
02320 };
02321 
02322 static CORE_ADDR
02323 sh64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
02324 {
02325   return frame_unwind_register_unsigned (next_frame,
02326                                          gdbarch_sp_regnum (gdbarch));
02327 }
02328 
02329 static CORE_ADDR
02330 sh64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
02331 {
02332   return frame_unwind_register_unsigned (next_frame,
02333                                          gdbarch_pc_regnum (gdbarch));
02334 }
02335 
02336 static struct frame_id
02337 sh64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
02338 {
02339   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
02340                                               gdbarch_sp_regnum (gdbarch));
02341   return frame_id_build (sp, get_frame_pc (this_frame));
02342 }
02343 
02344 static CORE_ADDR
02345 sh64_frame_base_address (struct frame_info *this_frame, void **this_cache)
02346 {
02347   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
02348 
02349   return cache->base;
02350 }
02351 
02352 static const struct frame_base sh64_frame_base = {
02353   &sh64_frame_unwind,
02354   sh64_frame_base_address,
02355   sh64_frame_base_address,
02356   sh64_frame_base_address
02357 };
02358 
02359 
02360 struct gdbarch *
02361 sh64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
02362 {
02363   struct gdbarch *gdbarch;
02364   struct gdbarch_tdep *tdep;
02365 
02366   /* If there is already a candidate, use it.  */
02367   arches = gdbarch_list_lookup_by_info (arches, &info);
02368   if (arches != NULL)
02369     return arches->gdbarch;
02370 
02371   /* None found, create a new architecture from the information
02372      provided.  */
02373   tdep = XMALLOC (struct gdbarch_tdep);
02374   gdbarch = gdbarch_alloc (&info, tdep);
02375 
02376   /* Determine the ABI */
02377   if (info.abfd && bfd_get_arch_size (info.abfd) == 64)
02378     {
02379       /* If the ABI is the 64-bit one, it can only be sh-media.  */
02380       tdep->sh_abi = SH_ABI_64;
02381       set_gdbarch_ptr_bit (gdbarch, 8 * TARGET_CHAR_BIT);
02382       set_gdbarch_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
02383     }
02384   else
02385     {
02386       /* If the ABI is the 32-bit one it could be either media or
02387          compact.  */
02388       tdep->sh_abi = SH_ABI_32;
02389       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02390       set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02391     }
02392 
02393   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
02394   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02395   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02396   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
02397   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02398   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
02399   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
02400 
02401   /* The number of real registers is the same whether we are in 
02402      ISA16(compact) or ISA32(media).  */
02403   set_gdbarch_num_regs (gdbarch, SIM_SH64_NR_REGS);
02404   set_gdbarch_sp_regnum (gdbarch, 15);
02405   set_gdbarch_pc_regnum (gdbarch, 64);
02406   set_gdbarch_fp0_regnum (gdbarch, SIM_SH64_FR0_REGNUM);
02407   set_gdbarch_num_pseudo_regs (gdbarch, NUM_PSEUDO_REGS_SH_MEDIA
02408                                         + NUM_PSEUDO_REGS_SH_COMPACT);
02409 
02410   set_gdbarch_register_name (gdbarch, sh64_register_name);
02411   set_gdbarch_register_type (gdbarch, sh64_register_type);
02412 
02413   set_gdbarch_pseudo_register_read (gdbarch, sh64_pseudo_register_read);
02414   set_gdbarch_pseudo_register_write (gdbarch, sh64_pseudo_register_write);
02415 
02416   set_gdbarch_breakpoint_from_pc (gdbarch, sh64_breakpoint_from_pc);
02417 
02418   set_gdbarch_print_insn (gdbarch, print_insn_sh);
02419   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);
02420 
02421   set_gdbarch_return_value (gdbarch, sh64_return_value);
02422 
02423   set_gdbarch_skip_prologue (gdbarch, sh64_skip_prologue);
02424   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
02425 
02426   set_gdbarch_push_dummy_call (gdbarch, sh64_push_dummy_call);
02427 
02428   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
02429 
02430   set_gdbarch_frame_align (gdbarch, sh64_frame_align);
02431   set_gdbarch_unwind_sp (gdbarch, sh64_unwind_sp);
02432   set_gdbarch_unwind_pc (gdbarch, sh64_unwind_pc);
02433   set_gdbarch_dummy_id (gdbarch, sh64_dummy_id);
02434   frame_base_set_default (gdbarch, &sh64_frame_base);
02435 
02436   set_gdbarch_print_registers_info (gdbarch, sh64_print_registers_info);
02437 
02438   set_gdbarch_elf_make_msymbol_special (gdbarch,
02439                                         sh64_elf_make_msymbol_special);
02440 
02441   /* Hook in ABI-specific overrides, if they have been registered.  */
02442   gdbarch_init_osabi (info, gdbarch);
02443 
02444   dwarf2_append_unwinders (gdbarch);
02445   frame_unwind_append_unwinder (gdbarch, &sh64_frame_unwind);
02446 
02447   return gdbarch;
02448 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines