gdb/v850-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.

  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-base.h"
  17. #include "trad-frame.h"
  18. #include "frame-unwind.h"
  19. #include "dwarf2-frame.h"
  20. #include "gdbtypes.h"
  21. #include "inferior.h"
  22. #include "gdbcore.h"
  23. #include "arch-utils.h"
  24. #include "regcache.h"
  25. #include "dis-asm.h"
  26. #include "osabi.h"
  27. #include "elf-bfd.h"
  28. #include "elf/v850.h"

  29. enum
  30.   {
  31.     /* General purpose registers.  */
  32.     E_R0_REGNUM,
  33.     E_R1_REGNUM,
  34.     E_R2_REGNUM,
  35.     E_R3_REGNUM, E_SP_REGNUM = E_R3_REGNUM,
  36.     E_R4_REGNUM,
  37.     E_R5_REGNUM,
  38.     E_R6_REGNUM, E_ARG0_REGNUM = E_R6_REGNUM,
  39.     E_R7_REGNUM,
  40.     E_R8_REGNUM,
  41.     E_R9_REGNUM, E_ARGLAST_REGNUM = E_R9_REGNUM,
  42.     E_R10_REGNUM, E_V0_REGNUM = E_R10_REGNUM,
  43.     E_R11_REGNUM, E_V1_REGNUM = E_R11_REGNUM,
  44.     E_R12_REGNUM,
  45.     E_R13_REGNUM,
  46.     E_R14_REGNUM,
  47.     E_R15_REGNUM,
  48.     E_R16_REGNUM,
  49.     E_R17_REGNUM,
  50.     E_R18_REGNUM,
  51.     E_R19_REGNUM,
  52.     E_R20_REGNUM,
  53.     E_R21_REGNUM,
  54.     E_R22_REGNUM,
  55.     E_R23_REGNUM,
  56.     E_R24_REGNUM,
  57.     E_R25_REGNUM,
  58.     E_R26_REGNUM,
  59.     E_R27_REGNUM,
  60.     E_R28_REGNUM,
  61.     E_R29_REGNUM, E_FP_REGNUM = E_R29_REGNUM,
  62.     E_R30_REGNUM, E_EP_REGNUM = E_R30_REGNUM,
  63.     E_R31_REGNUM, E_LP_REGNUM = E_R31_REGNUM,

  64.     /* System registers - main banks.  */
  65.     E_R32_REGNUM, E_SR0_REGNUM = E_R32_REGNUM,
  66.     E_R33_REGNUM,
  67.     E_R34_REGNUM,
  68.     E_R35_REGNUM,
  69.     E_R36_REGNUM,
  70.     E_R37_REGNUM, E_PS_REGNUM = E_R37_REGNUM,
  71.     E_R38_REGNUM,
  72.     E_R39_REGNUM,
  73.     E_R40_REGNUM,
  74.     E_R41_REGNUM,
  75.     E_R42_REGNUM,
  76.     E_R43_REGNUM,
  77.     E_R44_REGNUM,
  78.     E_R45_REGNUM,
  79.     E_R46_REGNUM,
  80.     E_R47_REGNUM,
  81.     E_R48_REGNUM,
  82.     E_R49_REGNUM,
  83.     E_R50_REGNUM,
  84.     E_R51_REGNUM,
  85.     E_R52_REGNUM, E_CTBP_REGNUM = E_R52_REGNUM,
  86.     E_R53_REGNUM,
  87.     E_R54_REGNUM,
  88.     E_R55_REGNUM,
  89.     E_R56_REGNUM,
  90.     E_R57_REGNUM,
  91.     E_R58_REGNUM,
  92.     E_R59_REGNUM,
  93.     E_R60_REGNUM,
  94.     E_R61_REGNUM,
  95.     E_R62_REGNUM,
  96.     E_R63_REGNUM,

  97.     /* PC.  */
  98.     E_R64_REGNUM, E_PC_REGNUM = E_R64_REGNUM,
  99.     E_R65_REGNUM,
  100.     E_NUM_OF_V850_REGS,
  101.     E_NUM_OF_V850E_REGS = E_NUM_OF_V850_REGS,

  102.     /* System registers - MPV (PROT00) bank.  */
  103.     E_R66_REGNUM = E_NUM_OF_V850_REGS,
  104.     E_R67_REGNUM,
  105.     E_R68_REGNUM,
  106.     E_R69_REGNUM,
  107.     E_R70_REGNUM,
  108.     E_R71_REGNUM,
  109.     E_R72_REGNUM,
  110.     E_R73_REGNUM,
  111.     E_R74_REGNUM,
  112.     E_R75_REGNUM,
  113.     E_R76_REGNUM,
  114.     E_R77_REGNUM,
  115.     E_R78_REGNUM,
  116.     E_R79_REGNUM,
  117.     E_R80_REGNUM,
  118.     E_R81_REGNUM,
  119.     E_R82_REGNUM,
  120.     E_R83_REGNUM,
  121.     E_R84_REGNUM,
  122.     E_R85_REGNUM,
  123.     E_R86_REGNUM,
  124.     E_R87_REGNUM,
  125.     E_R88_REGNUM,
  126.     E_R89_REGNUM,
  127.     E_R90_REGNUM,
  128.     E_R91_REGNUM,
  129.     E_R92_REGNUM,
  130.     E_R93_REGNUM,

  131.     /* System registers - MPU (PROT01) bank.  */
  132.     E_R94_REGNUM,
  133.     E_R95_REGNUM,
  134.     E_R96_REGNUM,
  135.     E_R97_REGNUM,
  136.     E_R98_REGNUM,
  137.     E_R99_REGNUM,
  138.     E_R100_REGNUM,
  139.     E_R101_REGNUM,
  140.     E_R102_REGNUM,
  141.     E_R103_REGNUM,
  142.     E_R104_REGNUM,
  143.     E_R105_REGNUM,
  144.     E_R106_REGNUM,
  145.     E_R107_REGNUM,
  146.     E_R108_REGNUM,
  147.     E_R109_REGNUM,
  148.     E_R110_REGNUM,
  149.     E_R111_REGNUM,
  150.     E_R112_REGNUM,
  151.     E_R113_REGNUM,
  152.     E_R114_REGNUM,
  153.     E_R115_REGNUM,
  154.     E_R116_REGNUM,
  155.     E_R117_REGNUM,
  156.     E_R118_REGNUM,
  157.     E_R119_REGNUM,
  158.     E_R120_REGNUM,
  159.     E_R121_REGNUM,

  160.     /* FPU system registers.  */
  161.     E_R122_REGNUM,
  162.     E_R123_REGNUM,
  163.     E_R124_REGNUM,
  164.     E_R125_REGNUM,
  165.     E_R126_REGNUM,
  166.     E_R127_REGNUM,
  167.     E_R128_REGNUM, E_FPSR_REGNUM = E_R128_REGNUM,
  168.     E_R129_REGNUM, E_FPEPC_REGNUM = E_R129_REGNUM,
  169.     E_R130_REGNUM, E_FPST_REGNUM = E_R130_REGNUM,
  170.     E_R131_REGNUM, E_FPCC_REGNUM = E_R131_REGNUM,
  171.     E_R132_REGNUM, E_FPCFG_REGNUM = E_R132_REGNUM,
  172.     E_R133_REGNUM,
  173.     E_R134_REGNUM,
  174.     E_R135_REGNUM,
  175.     E_R136_REGNUM,
  176.     E_R137_REGNUM,
  177.     E_R138_REGNUM,
  178.     E_R139_REGNUM,
  179.     E_R140_REGNUM,
  180.     E_R141_REGNUM,
  181.     E_R142_REGNUM,
  182.     E_R143_REGNUM,
  183.     E_R144_REGNUM,
  184.     E_R145_REGNUM,
  185.     E_R146_REGNUM,
  186.     E_R147_REGNUM,
  187.     E_R148_REGNUM,
  188.     E_R149_REGNUM,
  189.     E_NUM_OF_V850E2_REGS,

  190.     /* v850e3v5 system registers, selID 1 thru 7.  */
  191.     E_SELID_1_R0_REGNUM = E_NUM_OF_V850E2_REGS,
  192.     E_SELID_1_R31_REGNUM = E_SELID_1_R0_REGNUM + 31,

  193.     E_SELID_2_R0_REGNUM,
  194.     E_SELID_2_R31_REGNUM = E_SELID_2_R0_REGNUM + 31,

  195.     E_SELID_3_R0_REGNUM,
  196.     E_SELID_3_R31_REGNUM = E_SELID_3_R0_REGNUM + 31,

  197.     E_SELID_4_R0_REGNUM,
  198.     E_SELID_4_R31_REGNUM = E_SELID_4_R0_REGNUM + 31,

  199.     E_SELID_5_R0_REGNUM,
  200.     E_SELID_5_R31_REGNUM = E_SELID_5_R0_REGNUM + 31,

  201.     E_SELID_6_R0_REGNUM,
  202.     E_SELID_6_R31_REGNUM = E_SELID_6_R0_REGNUM + 31,

  203.     E_SELID_7_R0_REGNUM,
  204.     E_SELID_7_R31_REGNUM = E_SELID_7_R0_REGNUM + 31,

  205.     /* v850e3v5 vector registers.  */
  206.     E_VR0_REGNUM,
  207.     E_VR31_REGNUM = E_VR0_REGNUM + 31,

  208.     E_NUM_OF_V850E3V5_REGS,

  209.     /* Total number of possible registers.  */
  210.     E_NUM_REGS = E_NUM_OF_V850E3V5_REGS
  211.   };

  212. enum
  213. {
  214.   v850_reg_size = 4
  215. };

  216. /* Size of return datatype which fits into all return registers.  */
  217. enum
  218. {
  219.   E_MAX_RETTYPE_SIZE_IN_REGS = 2 * v850_reg_size
  220. };

  221. /* When v850 support was added to GCC in the late nineties, the intention
  222.    was to follow the Green Hills ABI for v850.  In fact, the authors of
  223.    that support at the time thought that they were doing so.  As far as
  224.    I can tell, the calling conventions are correct, but the return value
  225.    conventions were not quite right.  Over time, the return value code
  226.    in this file was modified to mostly reflect what GCC was actually
  227.    doing instead of to actually follow the Green Hills ABI as it did
  228.    when the code was first written.

  229.    Renesas defined the RH850 ABI which they use in their compiler.  It
  230.    is similar to the original Green Hills ABI with some minor
  231.    differences.  */

  232. enum v850_abi
  233. {
  234.   V850_ABI_GCC,
  235.   V850_ABI_RH850
  236. };

  237. /* Architecture specific data.  */

  238. struct gdbarch_tdep
  239. {
  240.   /* Fields from the ELF header.  */
  241.   int e_flags;
  242.   int e_machine;

  243.   /* Which ABI are we using?  */
  244.   enum v850_abi abi;
  245.   int eight_byte_align;
  246. };

  247. struct v850_frame_cache
  248. {
  249.   /* Base address.  */
  250.   CORE_ADDR base;
  251.   LONGEST sp_offset;
  252.   CORE_ADDR pc;

  253.   /* Flag showing that a frame has been created in the prologue code.  */
  254.   int uses_fp;

  255.   /* Saved registers.  */
  256.   struct trad_frame_saved_reg *saved_regs;
  257. };

  258. /* Info gleaned from scanning a function's prologue.  */
  259. struct pifsr                /* Info about one saved register.  */
  260. {
  261.   int offset;                /* Offset from sp or fp.  */
  262.   int cur_frameoffset;        /* Current frameoffset.  */
  263.   int reg;                /* Saved register number.  */
  264. };

  265. static const char *
  266. v850_register_name (struct gdbarch *gdbarch, int regnum)
  267. {
  268.   static const char *v850_reg_names[] =
  269.   { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  270.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  271.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  272.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  273.     "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
  274.     "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
  275.     "sr16", "sr17", "sr18", "sr19", "sr20", "sr21", "sr22", "sr23",
  276.     "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
  277.     "pc", "fp"
  278.   };
  279.   if (regnum < 0 || regnum > E_NUM_OF_V850_REGS)
  280.     return NULL;
  281.   return v850_reg_names[regnum];
  282. }

  283. static const char *
  284. v850e_register_name (struct gdbarch *gdbarch, int regnum)
  285. {
  286.   static const char *v850e_reg_names[] =
  287.   {
  288.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  289.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  290.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  291.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  292.     "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
  293.     "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
  294.     "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "sr21", "sr22", "sr23",
  295.     "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
  296.     "pc", "fp"
  297.   };
  298.   if (regnum < 0 || regnum > E_NUM_OF_V850E_REGS)
  299.     return NULL;
  300.   return v850e_reg_names[regnum];
  301. }

  302. static const char *
  303. v850e2_register_name (struct gdbarch *gdbarch, int regnum)
  304. {
  305.   static const char *v850e2_reg_names[] =
  306.   {
  307.     /* General purpose registers.  */
  308.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  309.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  310.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  311.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",

  312.     /* System registers - main banks.  */
  313.     "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "pid", "cfg",
  314.     "", "", "", "sccfg", "scbp", "eiic", "feic", "dbic",
  315.     "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "dir", "", "",
  316.     "", "", "", "", "eiwr", "fewr", "dbwr", "bsel",


  317.     /* PC.  */
  318.     "pc", "",

  319.     /* System registers - MPV (PROT00) bank.  */
  320.     "vsecr", "vstid", "vsadr", "", "vmecr", "vmtid", "vmadr", "",
  321.     "vpecr", "vptid", "vpadr", "", "", "", "", "",
  322.     "", "", "", "", "", "", "", "",
  323.     "mca", "mcs", "mcc", "mcr",

  324.     /* System registers - MPU (PROT01) bank.  */
  325.     "mpm", "mpc", "tid", "", "", "", "ipa0l", "ipa0u",
  326.     "ipa1l", "ipa1u", "ipa2l", "ipa2u", "ipa3l", "ipa3u", "ipa4l", "ipa4u",
  327.     "dpa0l", "dpa0u", "dpa1l", "dpa1u", "dpa2l", "dpa2u", "dpa3l", "dpa3u",
  328.     "dpa4l", "dpa4u", "dpa5l", "dpa5u",

  329.     /* FPU system registers.  */
  330.     "", "", "", "", "", "", "fpsr", "fpepc",
  331.     "fpst", "fpcc", "fpcfg", "fpec", "", "", "", "",
  332.     "", "", "", "", "", "", "", "",
  333.     "", "", "", "fpspc"
  334.   };
  335.   if (regnum < 0 || regnum >= E_NUM_OF_V850E2_REGS)
  336.     return NULL;
  337.   return v850e2_reg_names[regnum];
  338. }

  339. /* Implement the "register_name" gdbarch method for v850e3v5.  */

  340. static const char *
  341. v850e3v5_register_name (struct gdbarch *gdbarch, int regnum)
  342. {
  343.   static const char *v850e3v5_reg_names[] =
  344.   {
  345.     /* General purpose registers.  */
  346.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  347.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  348.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  349.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",

  350.     /* selID 0, not including FPU registers.  The FPU registers are
  351.        listed later on.  */
  352.     "eipc", "eipsw", "fepc", "fepsw",
  353.     "", "psw", "" /* fpsr */, "" /* fpepc */,
  354.     "" /* fpst */, "" /* fpcc */, "" /* fpcfg */, "" /* fpec */,
  355.     "sesr", "eiic", "feic", "",
  356.     "ctpc", "ctpsw", "", "", "ctbp", "", "", "",
  357.     "", "", "", "", "eiwr", "fewr", "", "bsel",


  358.     /* PC.  */
  359.     "pc", "",

  360.     /* v850e2 MPV bank.  */
  361.     "", "", "", "", "", "", "", "",
  362.     "", "", "", "", "", "", "", "",
  363.     "", "", "", "", "", "", "", "",
  364.     "", "", "", "",

  365.     /* Skip v850e2 MPU bank.  It's tempting to reuse these, but we need
  366.        32 entries for this bank.  */
  367.     "", "", "", "", "", "", "", "",
  368.     "", "", "", "", "", "", "", "",
  369.     "", "", "", "", "", "", "", "",
  370.     "", "", "", "",

  371.     /* FPU system registers.  These are actually in selID 0, but
  372.        are placed here to preserve register numbering compatibility
  373.        with previous architectures.  */
  374.     "", "", "", "", "", "", "fpsr", "fpepc",
  375.     "fpst", "fpcc", "fpcfg", "fpec", "", "", "", "",
  376.     "", "", "", "", "", "", "", "",
  377.     "", "", "", "",

  378.     /* selID 1.  */
  379.     "mcfg0", "mcfg1", "rbase", "ebase", "intbp", "mctl", "pid", "fpipr",
  380.     "", "", "tcsel", "sccfg", "scbp", "hvccfg", "hvcbp", "vsel",
  381.     "vmprt0", "vmprt1", "vmprt2", "", "", "", "", "vmscctl",
  382.     "vmsctbl0", "vmsctbl1", "vmsctbl2", "vmsctbl3", "", "", "", "",

  383.     /* selID 2.  */
  384.     "htcfg0", "", "", "", "", "htctl", "mea", "asid",
  385.     "mei", "ispr", "pmr", "icsr", "intcfg", "", "", "",
  386.     "tlbsch", "", "", "", "", "", "", "htscctl",
  387.     "htsctbl0", "htsctbl1", "htsctbl2", "htsctbl3",
  388.     "htsctbl4", "htsctbl5", "htsctbl6", "htsctbl7",

  389.     /* selID 3.  */
  390.     "", "", "", "", "", "", "", "",
  391.     "", "", "", "", "", "", "", "",
  392.     "", "", "", "", "", "", "", "",
  393.     "", "", "", "", "", "", "", "",

  394.     /* selID 4.  */
  395.     "tlbidx", "", "", "", "telo0", "telo1", "tehi0", "tehi1",
  396.     "", "", "tlbcfg", "", "bwerrl", "bwerrh", "brerrl", "brerrh",
  397.     "ictagl", "ictagh", "icdatl", "icdath",
  398.     "dctagl", "dctagh", "dcdatl", "dcdath",
  399.     "icctrl", "dcctrl", "iccfg", "dccfg", "icerr", "dcerr", "", "",

  400.     /* selID 5.  */
  401.     "mpm", "mprc", "", "", "mpbrgn", "mptrgn", "", "",
  402.     "mca", "mcs", "mcc", "mcr", "", "", "", "",
  403.     "", "", "", "", "mpprt0", "mpprt1", "mpprt2", "",
  404.     "", "", "", "", "", "", "", "",

  405.     /* selID 6.  */
  406.     "mpla0", "mpua0", "mpat0", "", "mpla1", "mpua1", "mpat1", "",
  407.     "mpla2", "mpua2", "mpat2", "", "mpla3", "mpua3", "mpat3", "",
  408.     "mpla4", "mpua4", "mpat4", "", "mpla5", "mpua5", "mpat5", "",
  409.     "mpla6", "mpua6", "mpat6", "", "mpla7", "mpua7", "mpat7", "",

  410.     /* selID 7.  */
  411.     "mpla8", "mpua8", "mpat8", "", "mpla9", "mpua9", "mpat9", "",
  412.     "mpla10", "mpua10", "mpat10", "", "mpla11", "mpua11", "mpat11", "",
  413.     "mpla12", "mpua12", "mpat12", "", "mpla13", "mpua13", "mpat13", "",
  414.     "mpla14", "mpua14", "mpat14", "", "mpla15", "mpua15", "mpat15", "",

  415.     /* Vector Registers */
  416.     "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
  417.     "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
  418.     "vr16", "vr17", "vr18", "vr19", "vr20", "vr21", "vr22", "vr23",
  419.     "vr24", "vr25", "vr26", "vr27", "vr28", "vr29", "vr30", "vr31",
  420.   };

  421.   if (regnum < 0 || regnum >= E_NUM_OF_V850E3V5_REGS)
  422.     return NULL;
  423.   return v850e3v5_reg_names[regnum];
  424. }

  425. /* Returns the default type for register N.  */

  426. static struct type *
  427. v850_register_type (struct gdbarch *gdbarch, int regnum)
  428. {
  429.   if (regnum == E_PC_REGNUM)
  430.     return builtin_type (gdbarch)->builtin_func_ptr;
  431.   else if (E_VR0_REGNUM <= regnum && regnum <= E_VR31_REGNUM)
  432.     return builtin_type (gdbarch)->builtin_uint64;
  433.   return builtin_type (gdbarch)->builtin_int32;
  434. }

  435. static int
  436. v850_type_is_scalar (struct type *t)
  437. {
  438.   return (TYPE_CODE (t) != TYPE_CODE_STRUCT
  439.           && TYPE_CODE (t) != TYPE_CODE_UNION
  440.           && TYPE_CODE (t) != TYPE_CODE_ARRAY);
  441. }

  442. /* Should call_function allocate stack space for a struct return?  */

  443. static int
  444. v850_use_struct_convention (struct gdbarch *gdbarch, struct type *type)
  445. {
  446.   int i;
  447.   struct type *fld_type, *tgt_type;

  448.   if (gdbarch_tdep (gdbarch)->abi == V850_ABI_RH850)
  449.     {
  450.       if (v850_type_is_scalar (type) && TYPE_LENGTH(type) <= 8)
  451.         return 0;

  452.       /* Structs are never returned in registers for this ABI.  */
  453.       return 1;
  454.     }
  455.   /* 1. The value is greater than 8 bytes -> returned by copying.  */
  456.   if (TYPE_LENGTH (type) > 8)
  457.     return 1;

  458.   /* 2. The value is a single basic type -> returned in register.  */
  459.   if (v850_type_is_scalar (type))
  460.     return 0;

  461.   /* The value is a structure or union with a single element and that
  462.      element is either a single basic type or an array of a single basic
  463.      type whose size is greater than or equal to 4 -> returned in register.  */
  464.   if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
  465.        || TYPE_CODE (type) == TYPE_CODE_UNION)
  466.        && TYPE_NFIELDS (type) == 1)
  467.     {
  468.       fld_type = TYPE_FIELD_TYPE (type, 0);
  469.       if (v850_type_is_scalar (fld_type) && TYPE_LENGTH (fld_type) >= 4)
  470.         return 0;

  471.       if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
  472.         {
  473.           tgt_type = TYPE_TARGET_TYPE (fld_type);
  474.           if (v850_type_is_scalar (tgt_type) && TYPE_LENGTH (tgt_type) >= 4)
  475.             return 0;
  476.         }
  477.     }

  478.   /* The value is a structure whose first element is an integer or a float,
  479.      and which contains no arrays of more than two elements -> returned in
  480.      register.  */
  481.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  482.       && v850_type_is_scalar (TYPE_FIELD_TYPE (type, 0))
  483.       && TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == 4)
  484.     {
  485.       for (i = 1; i < TYPE_NFIELDS (type); ++i)
  486.         {
  487.           fld_type = TYPE_FIELD_TYPE (type, 0);
  488.           if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
  489.             {
  490.               tgt_type = TYPE_TARGET_TYPE (fld_type);
  491.               if (TYPE_LENGTH (fld_type) >= 0 && TYPE_LENGTH (tgt_type) >= 0
  492.                   && TYPE_LENGTH (fld_type) / TYPE_LENGTH (tgt_type) > 2)
  493.                 return 1;
  494.             }
  495.         }
  496.       return 0;
  497.     }

  498.   /* The value is a union which contains at least one field which
  499.      would be returned in registers according to these rules ->
  500.      returned in register.  */
  501.   if (TYPE_CODE (type) == TYPE_CODE_UNION)
  502.     {
  503.       for (i = 0; i < TYPE_NFIELDS (type); ++i)
  504.         {
  505.           fld_type = TYPE_FIELD_TYPE (type, 0);
  506.           if (!v850_use_struct_convention (gdbarch, fld_type))
  507.             return 0;
  508.         }
  509.     }

  510.   return 1;
  511. }

  512. /* Structure for mapping bits in register lists to register numbers.  */

  513. struct reg_list
  514. {
  515.   long mask;
  516.   int regno;
  517. };

  518. /* Helper function for v850_scan_prologue to handle prepare instruction.  */

  519. static void
  520. v850_handle_prepare (int insn, int insn2, CORE_ADDR * current_pc_ptr,
  521.                      struct v850_frame_cache *pi, struct pifsr **pifsr_ptr)
  522. {
  523.   CORE_ADDR current_pc = *current_pc_ptr;
  524.   struct pifsr *pifsr = *pifsr_ptr;
  525.   long next = insn2 & 0xffff;
  526.   long list12 = ((insn & 1) << 16) + (next & 0xffe0);
  527.   long offset = (insn & 0x3e) << 1;
  528.   static struct reg_list reg_table[] =
  529.   {
  530.     {0x00800, 20},                /* r20 */
  531.     {0x00400, 21},                /* r21 */
  532.     {0x00200, 22},                /* r22 */
  533.     {0x00100, 23},                /* r23 */
  534.     {0x08000, 24},                /* r24 */
  535.     {0x04000, 25},                /* r25 */
  536.     {0x02000, 26},                /* r26 */
  537.     {0x01000, 27},                /* r27 */
  538.     {0x00080, 28},                /* r28 */
  539.     {0x00040, 29},                /* r29 */
  540.     {0x10000, 30},                /* ep */
  541.     {0x00020, 31},                /* lp */
  542.     {0, 0}                        /* end of table */
  543.   };
  544.   int i;

  545.   if ((next & 0x1f) == 0x0b)                /* skip imm16 argument */
  546.     current_pc += 2;
  547.   else if ((next & 0x1f) == 0x13)        /* skip imm16 argument */
  548.     current_pc += 2;
  549.   else if ((next & 0x1f) == 0x1b)        /* skip imm32 argument */
  550.     current_pc += 4;

  551.   /* Calculate the total size of the saved registers, and add it to the
  552.      immediate value used to adjust SP.  */
  553.   for (i = 0; reg_table[i].mask != 0; i++)
  554.     if (list12 & reg_table[i].mask)
  555.       offset += v850_reg_size;
  556.   pi->sp_offset -= offset;

  557.   /* Calculate the offsets of the registers relative to the value the SP
  558.      will have after the registers have been pushed and the imm5 value has
  559.      been subtracted from it.  */
  560.   if (pifsr)
  561.     {
  562.       for (i = 0; reg_table[i].mask != 0; i++)
  563.         {
  564.           if (list12 & reg_table[i].mask)
  565.             {
  566.               int reg = reg_table[i].regno;
  567.               offset -= v850_reg_size;
  568.               pifsr->reg = reg;
  569.               pifsr->offset = offset;
  570.               pifsr->cur_frameoffset = pi->sp_offset;
  571.               pifsr++;
  572.             }
  573.         }
  574.     }

  575.   /* Set result parameters.  */
  576.   *current_pc_ptr = current_pc;
  577.   *pifsr_ptr = pifsr;
  578. }


  579. /* Helper function for v850_scan_prologue to handle pushm/pushl instructions.
  580.    The SR bit of the register list is not supported.  gcc does not generate
  581.    this bit.  */

  582. static void
  583. v850_handle_pushm (int insn, int insn2, struct v850_frame_cache *pi,
  584.                    struct pifsr **pifsr_ptr)
  585. {
  586.   struct pifsr *pifsr = *pifsr_ptr;
  587.   long list12 = ((insn & 0x0f) << 16) + (insn2 & 0xfff0);
  588.   long offset = 0;
  589.   static struct reg_list pushml_reg_table[] =
  590.   {
  591.     {0x80000, E_PS_REGNUM},        /* PSW */
  592.     {0x40000, 1},                /* r1 */
  593.     {0x20000, 2},                /* r2 */
  594.     {0x10000, 3},                /* r3 */
  595.     {0x00800, 4},                /* r4 */
  596.     {0x00400, 5},                /* r5 */
  597.     {0x00200, 6},                /* r6 */
  598.     {0x00100, 7},                /* r7 */
  599.     {0x08000, 8},                /* r8 */
  600.     {0x04000, 9},                /* r9 */
  601.     {0x02000, 10},                /* r10 */
  602.     {0x01000, 11},                /* r11 */
  603.     {0x00080, 12},                /* r12 */
  604.     {0x00040, 13},                /* r13 */
  605.     {0x00020, 14},                /* r14 */
  606.     {0x00010, 15},                /* r15 */
  607.     {0, 0}                        /* end of table */
  608.   };
  609.   static struct reg_list pushmh_reg_table[] =
  610.   {
  611.     {0x80000, 16},                /* r16 */
  612.     {0x40000, 17},                /* r17 */
  613.     {0x20000, 18},                /* r18 */
  614.     {0x10000, 19},                /* r19 */
  615.     {0x00800, 20},                /* r20 */
  616.     {0x00400, 21},                /* r21 */
  617.     {0x00200, 22},                /* r22 */
  618.     {0x00100, 23},                /* r23 */
  619.     {0x08000, 24},                /* r24 */
  620.     {0x04000, 25},                /* r25 */
  621.     {0x02000, 26},                /* r26 */
  622.     {0x01000, 27},                /* r27 */
  623.     {0x00080, 28},                /* r28 */
  624.     {0x00040, 29},                /* r29 */
  625.     {0x00010, 30},                /* r30 */
  626.     {0x00020, 31},                /* r31 */
  627.     {0, 0}                        /* end of table */
  628.   };
  629.   struct reg_list *reg_table;
  630.   int i;

  631.   /* Is this a pushml or a pushmh?  */
  632.   if ((insn2 & 7) == 1)
  633.     reg_table = pushml_reg_table;
  634.   else
  635.     reg_table = pushmh_reg_table;

  636.   /* Calculate the total size of the saved registers, and add it to the
  637.      immediate value used to adjust SP.  */
  638.   for (i = 0; reg_table[i].mask != 0; i++)
  639.     if (list12 & reg_table[i].mask)
  640.       offset += v850_reg_size;
  641.   pi->sp_offset -= offset;

  642.   /* Calculate the offsets of the registers relative to the value the SP
  643.      will have after the registers have been pushed and the imm5 value is
  644.      subtracted from it.  */
  645.   if (pifsr)
  646.     {
  647.       for (i = 0; reg_table[i].mask != 0; i++)
  648.         {
  649.           if (list12 & reg_table[i].mask)
  650.             {
  651.               int reg = reg_table[i].regno;
  652.               offset -= v850_reg_size;
  653.               pifsr->reg = reg;
  654.               pifsr->offset = offset;
  655.               pifsr->cur_frameoffset = pi->sp_offset;
  656.               pifsr++;
  657.             }
  658.         }
  659.     }

  660.   /* Set result parameters.  */
  661.   *pifsr_ptr = pifsr;
  662. }

  663. /* Helper function to evaluate if register is one of the "save" registers.
  664.    This allows to simplify conditionals in v850_analyze_prologue a lot.  */

  665. static int
  666. v850_is_save_register (int reg)
  667. {
  668. /* The caller-save registers are R2, R20 - R29 and R31.  All other
  669.     registers are either special purpose (PC, SP), argument registers,
  670.     or just considered free for use in the caller.  */
  671. return reg == E_R2_REGNUM
  672.         || (reg >= E_R20_REGNUM && reg <= E_R29_REGNUM)
  673.         || reg == E_R31_REGNUM;
  674. }

  675. /* Scan the prologue of the function that contains PC, and record what
  676.    we find in PI.  Returns the pc after the prologue.  Note that the
  677.    addresses saved in frame->saved_regs are just frame relative (negative
  678.    offsets from the frame pointer).  This is because we don't know the
  679.    actual value of the frame pointer yet.  In some circumstances, the
  680.    frame pointer can't be determined till after we have scanned the
  681.    prologue.  */

  682. static CORE_ADDR
  683. v850_analyze_prologue (struct gdbarch *gdbarch,
  684.                        CORE_ADDR func_addr, CORE_ADDR pc,
  685.                        struct v850_frame_cache *pi, ULONGEST ctbp)
  686. {
  687.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  688.   CORE_ADDR prologue_end, current_pc;
  689.   struct pifsr pifsrs[E_NUM_REGS + 1];
  690.   struct pifsr *pifsr, *pifsr_tmp;
  691.   int ep_used;
  692.   int reg;
  693.   CORE_ADDR save_pc, save_end;
  694.   int regsave_func_p;
  695.   int r12_tmp;

  696.   memset (&pifsrs, 0, sizeof pifsrs);
  697.   pifsr = &pifsrs[0];

  698.   prologue_end = pc;

  699.   /* Now, search the prologue looking for instructions that setup fp, save
  700.      rp, adjust sp and such.  We also record the frame offset of any saved
  701.      registers.  */

  702.   pi->sp_offset = 0;
  703.   pi->uses_fp = 0;
  704.   ep_used = 0;
  705.   regsave_func_p = 0;
  706.   save_pc = 0;
  707.   save_end = 0;
  708.   r12_tmp = 0;

  709.   for (current_pc = func_addr; current_pc < prologue_end;)
  710.     {
  711.       int insn;
  712.       int insn2 = -1; /* dummy value */

  713.       insn = read_memory_integer (current_pc, 2, byte_order);
  714.       current_pc += 2;
  715.       if ((insn & 0x0780) >= 0x0600)        /* Four byte instruction?  */
  716.         {
  717.           insn2 = read_memory_integer (current_pc, 2, byte_order);
  718.           current_pc += 2;
  719.         }

  720.       if ((insn & 0xffc0) == ((10 << 11) | 0x0780) && !regsave_func_p)
  721.         {                        /* jarl <func>,10 */
  722.           long low_disp = insn2 & ~(long) 1;
  723.           long disp = (((((insn & 0x3f) << 16) + low_disp)
  724.                         & ~(long) 1) ^ 0x00200000) - 0x00200000;

  725.           save_pc = current_pc;
  726.           save_end = prologue_end;
  727.           regsave_func_p = 1;
  728.           current_pc += disp - 4;
  729.           prologue_end = (current_pc
  730.                           + (2 * 3)        /* moves to/from ep */
  731.                           + 4                /* addi <const>,sp,sp */
  732.                           + 2                /* jmp [r10] */
  733.                           + (2 * 12)        /* sst.w to save r2, r20-r29, r31 */
  734.                           + 20);        /* slop area */
  735.         }
  736.       else if ((insn & 0xffc0) == 0x0200 && !regsave_func_p)
  737.         {                        /* callt <imm6> */
  738.           long adr = ctbp + ((insn & 0x3f) << 1);

  739.           save_pc = current_pc;
  740.           save_end = prologue_end;
  741.           regsave_func_p = 1;
  742.           current_pc = ctbp + (read_memory_unsigned_integer (adr, 2, byte_order)
  743.                                & 0xffff);
  744.           prologue_end = (current_pc
  745.                           + (2 * 3)        /* prepare list2,imm5,sp/imm */
  746.                           + 4                /* ctret */
  747.                           + 20);        /* slop area */
  748.           continue;
  749.         }
  750.       else if ((insn & 0xffc0) == 0x0780)        /* prepare list2,imm5 */
  751.         {
  752.           v850_handle_prepare (insn, insn2, &current_pc, pi, &pifsr);
  753.           continue;
  754.         }
  755.       else if (insn == 0x07e0 && regsave_func_p && insn2 == 0x0144)
  756.         {                        /* ctret after processing register save.  */
  757.           current_pc = save_pc;
  758.           prologue_end = save_end;
  759.           regsave_func_p = 0;
  760.           continue;
  761.         }
  762.       else if ((insn & 0xfff0) == 0x07e0 && (insn2 & 5) == 1)
  763.         {                        /* pushml, pushmh */
  764.           v850_handle_pushm (insn, insn2, pi, &pifsr);
  765.           continue;
  766.         }
  767.       else if ((insn & 0xffe0) == 0x0060 && regsave_func_p)
  768.         {                        /* jmp after processing register save.  */
  769.           current_pc = save_pc;
  770.           prologue_end = save_end;
  771.           regsave_func_p = 0;
  772.           continue;
  773.         }
  774.       else if ((insn & 0x07c0) == 0x0780        /* jarl or jr */
  775.                || (insn & 0xffe0) == 0x0060        /* jmp */
  776.                || (insn & 0x0780) == 0x0580)        /* branch */
  777.         {
  778.           break;                /* Ran into end of prologue.  */
  779.         }

  780.       else if ((insn & 0xffe0) == ((E_SP_REGNUM << 11) | 0x0240))
  781.         /* add <imm>,sp */
  782.         pi->sp_offset += ((insn & 0x1f) ^ 0x10) - 0x10;
  783.       else if (insn == ((E_SP_REGNUM << 11) | 0x0600 | E_SP_REGNUM))
  784.         /* addi <imm>,sp,sp */
  785.         pi->sp_offset += insn2;
  786.       else if (insn == ((E_FP_REGNUM << 11) | 0x0000 | E_SP_REGNUM))
  787.         /* mov sp,fp */
  788.         pi->uses_fp = 1;
  789.       else if (insn == ((E_R12_REGNUM << 11) | 0x0640 | E_R0_REGNUM))
  790.         /* movhi hi(const),r0,r12 */
  791.         r12_tmp = insn2 << 16;
  792.       else if (insn == ((E_R12_REGNUM << 11) | 0x0620 | E_R12_REGNUM))
  793.         /* movea lo(const),r12,r12 */
  794.         r12_tmp += insn2;
  795.       else if (insn == ((E_SP_REGNUM << 11) | 0x01c0 | E_R12_REGNUM) && r12_tmp)
  796.         /* add r12,sp */
  797.         pi->sp_offset += r12_tmp;
  798.       else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_SP_REGNUM))
  799.         /* mov sp,ep */
  800.         ep_used = 1;
  801.       else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_R1_REGNUM))
  802.         /* mov r1,ep */
  803.         ep_used = 0;
  804.       else if (((insn & 0x07ff) == (0x0760 | E_SP_REGNUM)
  805.                 || (pi->uses_fp
  806.                     && (insn & 0x07ff) == (0x0760 | E_FP_REGNUM)))
  807.                && pifsr
  808.                && v850_is_save_register (reg = (insn >> 11) & 0x1f))
  809.         {
  810.           /* st.w <reg>,<offset>[sp] or st.w <reg>,<offset>[fp] */
  811.           pifsr->reg = reg;
  812.           pifsr->offset = insn2 & ~1;
  813.           pifsr->cur_frameoffset = pi->sp_offset;
  814.           pifsr++;
  815.         }
  816.       else if (ep_used
  817.                && ((insn & 0x0781) == 0x0501)
  818.                && pifsr
  819.                && v850_is_save_register (reg = (insn >> 11) & 0x1f))
  820.         {
  821.           /* sst.w <reg>,<offset>[ep] */
  822.           pifsr->reg = reg;
  823.           pifsr->offset = (insn & 0x007e) << 1;
  824.           pifsr->cur_frameoffset = pi->sp_offset;
  825.           pifsr++;
  826.         }
  827.     }

  828.   /* Fix up any offsets to the final offset.  If a frame pointer was created,
  829.      use it instead of the stack pointer.  */
  830.   for (pifsr_tmp = pifsrs; pifsr_tmp != pifsr; pifsr_tmp++)
  831.     {
  832.       pifsr_tmp->offset -= pi->sp_offset - pifsr_tmp->cur_frameoffset;
  833.       pi->saved_regs[pifsr_tmp->reg].addr = pifsr_tmp->offset;
  834.     }

  835.   return current_pc;
  836. }

  837. /* Return the address of the first code past the prologue of the function.  */

  838. static CORE_ADDR
  839. v850_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  840. {
  841.   CORE_ADDR func_addr, func_end;

  842.   /* See what the symbol table says.  */

  843.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  844.     {
  845.       struct symtab_and_line sal;

  846.       sal = find_pc_line (func_addr, 0);
  847.       if (sal.line != 0 && sal.end < func_end)
  848.         return sal.end;

  849.       /* Either there's no line info, or the line after the prologue is after
  850.          the end of the function.  In this case, there probably isn't a
  851.          prologue.  */
  852.       return pc;
  853.     }

  854.   /* We can't find the start of this function, so there's nothing we
  855.      can do.  */
  856.   return pc;
  857. }

  858. /* Return 1 if the data structure has any 8-byte fields that'll require
  859.    the entire data structure to be aligned.  Otherwise, return 0.  */

  860. static int
  861. v850_eight_byte_align_p (struct type *type)
  862. {
  863.   type = check_typedef (type);

  864.   if (v850_type_is_scalar (type))
  865.     return (TYPE_LENGTH (type) == 8);
  866.   else
  867.     {
  868.       int i;

  869.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  870.         {
  871.           if (v850_eight_byte_align_p (TYPE_FIELD_TYPE (type, i)))
  872.             return 1;
  873.         }
  874.     }
  875.   return 0;
  876. }

  877. static CORE_ADDR
  878. v850_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
  879. {
  880.   return sp & ~3;
  881. }

  882. /* Setup arguments and LP for a call to the target.  First four args
  883.    go in R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs
  884.    are passed by reference.  64 bit quantities (doubles and long longs)
  885.    may be split between the regs and the stack.  When calling a function
  886.    that returns a struct, a pointer to the struct is passed in as a secret
  887.    first argument (always in R6).

  888.    Stack space for the args has NOT been allocated: that job is up to us.  */

  889. static CORE_ADDR
  890. v850_push_dummy_call (struct gdbarch *gdbarch,
  891.                       struct value *function,
  892.                       struct regcache *regcache,
  893.                       CORE_ADDR bp_addr,
  894.                       int nargs,
  895.                       struct value **args,
  896.                       CORE_ADDR sp,
  897.                       int struct_return,
  898.                       CORE_ADDR struct_addr)
  899. {
  900.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  901.   int argreg;
  902.   int argnum;
  903.   int len = 0;
  904.   int stack_offset;

  905.   if (gdbarch_tdep (gdbarch)->abi == V850_ABI_RH850)
  906.     stack_offset = 0;
  907.   else
  908.   /* The offset onto the stack at which we will start copying parameters
  909.      (after the registers are used up) begins at 16 rather than at zero.
  910.      That's how the ABI is defined, though there's no indication that these
  911.      16 bytes are used for anything, not even for saving incoming
  912.      argument registers.  */
  913.   stack_offset = 16;

  914.   /* Now make space on the stack for the args.  */
  915.   for (argnum = 0; argnum < nargs; argnum++)
  916.     len += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
  917.   sp -= len + stack_offset;

  918.   argreg = E_ARG0_REGNUM;
  919.   /* The struct_return pointer occupies the first parameter register.  */
  920.   if (struct_return)
  921.     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);

  922.   /* Now load as many as possible of the first arguments into
  923.      registers, and push the rest onto the stack.  There are 16 bytes
  924.      in four registers available.  Loop thru args from first to last.  */
  925.   for (argnum = 0; argnum < nargs; argnum++)
  926.     {
  927.       int len;
  928.       gdb_byte *val;
  929.       gdb_byte valbuf[v850_reg_size];

  930.       if (!v850_type_is_scalar (value_type (*args))
  931.          && gdbarch_tdep (gdbarch)->abi == V850_ABI_GCC
  932.           && TYPE_LENGTH (value_type (*args)) > E_MAX_RETTYPE_SIZE_IN_REGS)
  933.         {
  934.           store_unsigned_integer (valbuf, 4, byte_order,
  935.                                   value_address (*args));
  936.           len = 4;
  937.           val = valbuf;
  938.         }
  939.       else
  940.         {
  941.           len = TYPE_LENGTH (value_type (*args));
  942.           val = (gdb_byte *) value_contents (*args);
  943.         }

  944.       if (gdbarch_tdep (gdbarch)->eight_byte_align
  945.           && v850_eight_byte_align_p (value_type (*args)))
  946.         {
  947.           if (argreg <= E_ARGLAST_REGNUM && (argreg & 1))
  948.             argreg++;
  949.           else if (stack_offset & 0x4)
  950.             stack_offset += 4;
  951.         }

  952.       while (len > 0)
  953.         if (argreg <= E_ARGLAST_REGNUM)
  954.           {
  955.             CORE_ADDR regval;

  956.             regval = extract_unsigned_integer (val, v850_reg_size, byte_order);
  957.             regcache_cooked_write_unsigned (regcache, argreg, regval);

  958.             len -= v850_reg_size;
  959.             val += v850_reg_size;
  960.             argreg++;
  961.           }
  962.         else
  963.           {
  964.             write_memory (sp + stack_offset, val, 4);

  965.             len -= 4;
  966.             val += 4;
  967.             stack_offset += 4;
  968.           }
  969.       args++;
  970.     }

  971.   /* Store return address.  */
  972.   regcache_cooked_write_unsigned (regcache, E_LP_REGNUM, bp_addr);

  973.   /* Update stack pointer.  */
  974.   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);

  975.   return sp;
  976. }

  977. static void
  978. v850_extract_return_value (struct type *type, struct regcache *regcache,
  979.                            gdb_byte *valbuf)
  980. {
  981.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  982.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  983.   int len = TYPE_LENGTH (type);

  984.   if (len <= v850_reg_size)
  985.     {
  986.       ULONGEST val;

  987.       regcache_cooked_read_unsigned (regcache, E_V0_REGNUM, &val);
  988.       store_unsigned_integer (valbuf, len, byte_order, val);
  989.     }
  990.   else if (len <= 2 * v850_reg_size)
  991.     {
  992.       int i, regnum = E_V0_REGNUM;
  993.       gdb_byte buf[v850_reg_size];
  994.       for (i = 0; len > 0; i += 4, len -= 4)
  995.         {
  996.           regcache_raw_read (regcache, regnum++, buf);
  997.           memcpy (valbuf + i, buf, len > 4 ? 4 : len);
  998.         }
  999.     }
  1000. }

  1001. static void
  1002. v850_store_return_value (struct type *type, struct regcache *regcache,
  1003.                          const gdb_byte *valbuf)
  1004. {
  1005.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1006.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1007.   int len = TYPE_LENGTH (type);

  1008.   if (len <= v850_reg_size)
  1009.       regcache_cooked_write_unsigned
  1010.         (regcache, E_V0_REGNUM,
  1011.          extract_unsigned_integer (valbuf, len, byte_order));
  1012.   else if (len <= 2 * v850_reg_size)
  1013.     {
  1014.       int i, regnum = E_V0_REGNUM;
  1015.       for (i = 0; i < len; i += 4)
  1016.         regcache_raw_write (regcache, regnum++, valbuf + i);
  1017.     }
  1018. }

  1019. static enum return_value_convention
  1020. v850_return_value (struct gdbarch *gdbarch, struct value *function,
  1021.                    struct type *type, struct regcache *regcache,
  1022.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  1023. {
  1024.   if (v850_use_struct_convention (gdbarch, type))
  1025.     return RETURN_VALUE_STRUCT_CONVENTION;
  1026.   if (writebuf)
  1027.     v850_store_return_value (type, regcache, writebuf);
  1028.   else if (readbuf)
  1029.     v850_extract_return_value (type, regcache, readbuf);
  1030.   return RETURN_VALUE_REGISTER_CONVENTION;
  1031. }

  1032. static const unsigned char *
  1033. v850_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  1034.                          int *lenptr)
  1035. {
  1036.   static unsigned char breakpoint[] = { 0x85, 0x05 };

  1037.   *lenptr = sizeof (breakpoint);
  1038.   return breakpoint;
  1039. }

  1040. /* Implement software breakpoints by using the dbtrap instruction.
  1041.    Older architectures had no such instruction.  For those, an
  1042.    unconditional branch to self instruction is used.  */

  1043. static const unsigned char *
  1044. v850_dbtrap_breakpoint_from_pc (struct gdbarch *gdbarch,
  1045.                                 CORE_ADDR *pcptr, int *lenptr)
  1046. {
  1047.   static unsigned char breakpoint[] = { 0x40, 0xf8 };

  1048.   *lenptr = sizeof (breakpoint);
  1049.   return breakpoint;
  1050. }

  1051. static struct v850_frame_cache *
  1052. v850_alloc_frame_cache (struct frame_info *this_frame)
  1053. {
  1054.   struct v850_frame_cache *cache;

  1055.   cache = FRAME_OBSTACK_ZALLOC (struct v850_frame_cache);
  1056.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1057.   /* Base address.  */
  1058.   cache->base = 0;
  1059.   cache->sp_offset = 0;
  1060.   cache->pc = 0;

  1061.   /* Frameless until proven otherwise.  */
  1062.   cache->uses_fp = 0;

  1063.   return cache;
  1064. }

  1065. static struct v850_frame_cache *
  1066. v850_frame_cache (struct frame_info *this_frame, void **this_cache)
  1067. {
  1068.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1069.   struct v850_frame_cache *cache;
  1070.   CORE_ADDR current_pc;
  1071.   int i;

  1072.   if (*this_cache)
  1073.     return *this_cache;

  1074.   cache = v850_alloc_frame_cache (this_frame);
  1075.   *this_cache = cache;

  1076.   /* In principle, for normal frames, fp holds the frame pointer,
  1077.      which holds the base address for the current stack frame.
  1078.      However, for functions that don't need it, the frame pointer is
  1079.      optional.  For these "frameless" functions the frame pointer is
  1080.      actually the frame pointer of the calling frame.  */
  1081.   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  1082.   if (cache->base == 0)
  1083.     return cache;

  1084.   cache->pc = get_frame_func (this_frame);
  1085.   current_pc = get_frame_pc (this_frame);
  1086.   if (cache->pc != 0)
  1087.     {
  1088.       ULONGEST ctbp;
  1089.       ctbp = get_frame_register_unsigned (this_frame, E_CTBP_REGNUM);
  1090.       v850_analyze_prologue (gdbarch, cache->pc, current_pc, cache, ctbp);
  1091.     }

  1092.   if (!cache->uses_fp)
  1093.     {
  1094.       /* We didn't find a valid frame, which means that CACHE->base
  1095.          currently holds the frame pointer for our calling frame.  If
  1096.          we're at the start of a function, or somewhere half-way its
  1097.          prologue, the function's frame probably hasn't been fully
  1098.          setup yet.  Try to reconstruct the base address for the stack
  1099.          frame by looking at the stack pointer.  For truly "frameless"
  1100.          functions this might work too.  */
  1101.       cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  1102.     }

  1103.   /* Now that we have the base address for the stack frame we can
  1104.      calculate the value of sp in the calling frame.  */
  1105.   trad_frame_set_value (cache->saved_regs, E_SP_REGNUM,
  1106.                           cache->base - cache->sp_offset);

  1107.   /* Adjust all the saved registers such that they contain addresses
  1108.      instead of offsets.  */
  1109.   for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  1110.     if (trad_frame_addr_p (cache->saved_regs, i))
  1111.       cache->saved_regs[i].addr += cache->base;

  1112.   /* The call instruction moves the caller's PC in the callee's LP.
  1113.      Since this is an unwind, do the reverse.  Copy the location of LP
  1114.      into PC (the address / regnum) so that a request for PC will be
  1115.      converted into a request for the LP.  */

  1116.   cache->saved_regs[E_PC_REGNUM] = cache->saved_regs[E_LP_REGNUM];

  1117.   return cache;
  1118. }


  1119. static struct value *
  1120. v850_frame_prev_register (struct frame_info *this_frame,
  1121.                           void **this_cache, int regnum)
  1122. {
  1123.   struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);

  1124.   gdb_assert (regnum >= 0);

  1125.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  1126. }

  1127. static void
  1128. v850_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1129.                     struct frame_id *this_id)
  1130. {
  1131.   struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);

  1132.   /* This marks the outermost frame.  */
  1133.   if (cache->base == 0)
  1134.     return;

  1135.   *this_id = frame_id_build (cache->saved_regs[E_SP_REGNUM].addr, cache->pc);
  1136. }

  1137. static const struct frame_unwind v850_frame_unwind = {
  1138.   NORMAL_FRAME,
  1139.   default_frame_unwind_stop_reason,
  1140.   v850_frame_this_id,
  1141.   v850_frame_prev_register,
  1142.   NULL,
  1143.   default_frame_sniffer
  1144. };

  1145. static CORE_ADDR
  1146. v850_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1147. {
  1148.   return frame_unwind_register_unsigned (next_frame,
  1149.                                          gdbarch_sp_regnum (gdbarch));
  1150. }

  1151. static CORE_ADDR
  1152. v850_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1153. {
  1154.   return frame_unwind_register_unsigned (next_frame,
  1155.                                          gdbarch_pc_regnum (gdbarch));
  1156. }

  1157. static struct frame_id
  1158. v850_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1159. {
  1160.   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
  1161.                                               gdbarch_sp_regnum (gdbarch));
  1162.   return frame_id_build (sp, get_frame_pc (this_frame));
  1163. }

  1164. static CORE_ADDR
  1165. v850_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1166. {
  1167.   struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);

  1168.   return cache->base;
  1169. }

  1170. static const struct frame_base v850_frame_base = {
  1171.   &v850_frame_unwind,
  1172.   v850_frame_base_address,
  1173.   v850_frame_base_address,
  1174.   v850_frame_base_address
  1175. };

  1176. static struct gdbarch *
  1177. v850_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1178. {
  1179.   struct gdbarch *gdbarch;
  1180.   struct gdbarch_tdep *tdep;
  1181.   int e_flags, e_machine;

  1182.   /* Extract the elf_flags if available.  */
  1183.   if (info.abfd != NULL
  1184.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1185.     {
  1186.       e_flags = elf_elfheader (info.abfd)->e_flags;
  1187.       e_machine = elf_elfheader (info.abfd)->e_machine;
  1188.     }
  1189.   else
  1190.     {
  1191.       e_flags = 0;
  1192.       e_machine = 0;
  1193.     }


  1194.   /* Try to find the architecture in the list of already defined
  1195.      architectures.  */
  1196.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1197.        arches != NULL;
  1198.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1199.     {
  1200.       if (gdbarch_tdep (arches->gdbarch)->e_flags != e_flags
  1201.           || gdbarch_tdep (arches->gdbarch)->e_machine != e_machine)
  1202.         continue;

  1203.       return arches->gdbarch;
  1204.     }
  1205.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  1206.   tdep->e_flags = e_flags;
  1207.   tdep->e_machine = e_machine;

  1208.   switch (tdep->e_machine)
  1209.     {
  1210.     case EM_V800:
  1211.       tdep->abi = V850_ABI_RH850;
  1212.       break;
  1213.     default:
  1214.       tdep->abi = V850_ABI_GCC;
  1215.       break;
  1216.     }

  1217.   tdep->eight_byte_align = (tdep->e_flags & EF_RH850_DATA_ALIGN8) ? 1 : 0;
  1218.   gdbarch = gdbarch_alloc (&info, tdep);

  1219.   switch (info.bfd_arch_info->mach)
  1220.     {
  1221.     case bfd_mach_v850:
  1222.       set_gdbarch_register_name (gdbarch, v850_register_name);
  1223.       set_gdbarch_num_regs (gdbarch, E_NUM_OF_V850_REGS);
  1224.       break;
  1225.     case bfd_mach_v850e:
  1226.     case bfd_mach_v850e1:
  1227.       set_gdbarch_register_name (gdbarch, v850e_register_name);
  1228.       set_gdbarch_num_regs (gdbarch, E_NUM_OF_V850E_REGS);
  1229.       break;
  1230.     case bfd_mach_v850e2:
  1231.     case bfd_mach_v850e2v3:
  1232.       set_gdbarch_register_name (gdbarch, v850e2_register_name);
  1233.       set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
  1234.       break;
  1235.     case bfd_mach_v850e3v5:
  1236.       set_gdbarch_register_name (gdbarch, v850e3v5_register_name);
  1237.       set_gdbarch_num_regs (gdbarch, E_NUM_OF_V850E3V5_REGS);
  1238.       break;
  1239.     }

  1240.   set_gdbarch_num_pseudo_regs (gdbarch, 0);
  1241.   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  1242.   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  1243.   set_gdbarch_fp0_regnum (gdbarch, -1);

  1244.   set_gdbarch_register_type (gdbarch, v850_register_type);

  1245.   set_gdbarch_char_signed (gdbarch, 1);
  1246.   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1247.   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1248.   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1249.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  1250.   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1251.   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1252.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  1253.   set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1254.   set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);

  1255.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1256.   switch (info.bfd_arch_info->mach)
  1257.     {
  1258.     case bfd_mach_v850e2:
  1259.     case bfd_mach_v850e2v3:
  1260.     case bfd_mach_v850e3v5:
  1261.       set_gdbarch_breakpoint_from_pc (gdbarch, v850_dbtrap_breakpoint_from_pc);
  1262.       break;
  1263.     default:
  1264.       set_gdbarch_breakpoint_from_pc (gdbarch, v850_breakpoint_from_pc);
  1265.       break;
  1266.     }

  1267.   set_gdbarch_return_value (gdbarch, v850_return_value);
  1268.   set_gdbarch_push_dummy_call (gdbarch, v850_push_dummy_call);
  1269.   set_gdbarch_skip_prologue (gdbarch, v850_skip_prologue);

  1270.   set_gdbarch_print_insn (gdbarch, print_insn_v850);

  1271.   set_gdbarch_frame_align (gdbarch, v850_frame_align);
  1272.   set_gdbarch_unwind_sp (gdbarch, v850_unwind_sp);
  1273.   set_gdbarch_unwind_pc (gdbarch, v850_unwind_pc);
  1274.   set_gdbarch_dummy_id (gdbarch, v850_dummy_id);
  1275.   frame_base_set_default (gdbarch, &v850_frame_base);

  1276.   /* Hook in ABI-specific overrides, if they have been registered.  */
  1277.   gdbarch_init_osabi (info, gdbarch);

  1278.   dwarf2_append_unwinders (gdbarch);
  1279.   frame_unwind_append_unwinder (gdbarch, &v850_frame_unwind);

  1280.   return gdbarch;
  1281. }

  1282. extern initialize_file_ftype _initialize_v850_tdep; /* -Wmissing-prototypes */

  1283. void
  1284. _initialize_v850_tdep (void)
  1285. {
  1286.   register_gdbarch_init (bfd_arch_v850, v850_gdbarch_init);
  1287.   register_gdbarch_init (bfd_arch_v850_rh850, v850_gdbarch_init);
  1288. }