gdb/rl78-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for the Renesas RL78 for GDB, the GNU debugger.

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

  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "prologue-value.h"
  18. #include "target.h"
  19. #include "regcache.h"
  20. #include "opcode/rl78.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2-frame.h"
  29. #include "reggroups.h"

  30. #include "elf/rl78.h"
  31. #include "elf-bfd.h"

  32. /* Register Banks.  */

  33. enum
  34. {
  35.   RL78_BANK0 = 0,
  36.   RL78_BANK1 = 1,
  37.   RL78_BANK2 = 2,
  38.   RL78_BANK3 = 3,
  39.   RL78_NUMBANKS = 4,
  40.   RL78_REGS_PER_BANK = 8
  41. };

  42. /* Register Numbers.  */

  43. enum
  44. {
  45.   /* All general purpose registers are 8 bits wide.  */
  46.   RL78_RAW_BANK0_R0_REGNUM = 0,
  47.   RL78_RAW_BANK0_R1_REGNUM,
  48.   RL78_RAW_BANK0_R2_REGNUM,
  49.   RL78_RAW_BANK0_R3_REGNUM,
  50.   RL78_RAW_BANK0_R4_REGNUM,
  51.   RL78_RAW_BANK0_R5_REGNUM,
  52.   RL78_RAW_BANK0_R6_REGNUM,
  53.   RL78_RAW_BANK0_R7_REGNUM,

  54.   RL78_RAW_BANK1_R0_REGNUM,
  55.   RL78_RAW_BANK1_R1_REGNUM,
  56.   RL78_RAW_BANK1_R2_REGNUM,
  57.   RL78_RAW_BANK1_R3_REGNUM,
  58.   RL78_RAW_BANK1_R4_REGNUM,
  59.   RL78_RAW_BANK1_R5_REGNUM,
  60.   RL78_RAW_BANK1_R6_REGNUM,
  61.   RL78_RAW_BANK1_R7_REGNUM,

  62.   RL78_RAW_BANK2_R0_REGNUM,
  63.   RL78_RAW_BANK2_R1_REGNUM,
  64.   RL78_RAW_BANK2_R2_REGNUM,
  65.   RL78_RAW_BANK2_R3_REGNUM,
  66.   RL78_RAW_BANK2_R4_REGNUM,
  67.   RL78_RAW_BANK2_R5_REGNUM,
  68.   RL78_RAW_BANK2_R6_REGNUM,
  69.   RL78_RAW_BANK2_R7_REGNUM,

  70.   RL78_RAW_BANK3_R0_REGNUM,
  71.   RL78_RAW_BANK3_R1_REGNUM,
  72.   RL78_RAW_BANK3_R2_REGNUM,
  73.   RL78_RAW_BANK3_R3_REGNUM,
  74.   RL78_RAW_BANK3_R4_REGNUM,
  75.   RL78_RAW_BANK3_R5_REGNUM,
  76.   RL78_RAW_BANK3_R6_REGNUM,
  77.   RL78_RAW_BANK3_R7_REGNUM,

  78.   RL78_PSW_REGNUM,        /* 8 bits */
  79.   RL78_ES_REGNUM,        /* 8 bits */
  80.   RL78_CS_REGNUM,        /* 8 bits */
  81.   RL78_RAW_PC_REGNUM,        /* 20 bits; we'll use 32 bits for it.  */

  82.   /* Fixed address SFRs (some of those above are SFRs too.) */
  83.   RL78_SPL_REGNUM,        /* 8 bits; lower half of SP */
  84.   RL78_SPH_REGNUM,        /* 8 bits; upper half of SP */
  85.   RL78_PMC_REGNUM,        /* 8 bits */
  86.   RL78_MEM_REGNUM,        /* 8 bits ?? */

  87.   RL78_NUM_REGS,

  88.   /* Pseudo registers.  */
  89.   RL78_PC_REGNUM = RL78_NUM_REGS,
  90.   RL78_SP_REGNUM,

  91.   RL78_X_REGNUM,
  92.   RL78_A_REGNUM,
  93.   RL78_C_REGNUM,
  94.   RL78_B_REGNUM,
  95.   RL78_E_REGNUM,
  96.   RL78_D_REGNUM,
  97.   RL78_L_REGNUM,
  98.   RL78_H_REGNUM,

  99.   RL78_AX_REGNUM,
  100.   RL78_BC_REGNUM,
  101.   RL78_DE_REGNUM,
  102.   RL78_HL_REGNUM,

  103.   RL78_BANK0_R0_REGNUM,
  104.   RL78_BANK0_R1_REGNUM,
  105.   RL78_BANK0_R2_REGNUM,
  106.   RL78_BANK0_R3_REGNUM,
  107.   RL78_BANK0_R4_REGNUM,
  108.   RL78_BANK0_R5_REGNUM,
  109.   RL78_BANK0_R6_REGNUM,
  110.   RL78_BANK0_R7_REGNUM,

  111.   RL78_BANK1_R0_REGNUM,
  112.   RL78_BANK1_R1_REGNUM,
  113.   RL78_BANK1_R2_REGNUM,
  114.   RL78_BANK1_R3_REGNUM,
  115.   RL78_BANK1_R4_REGNUM,
  116.   RL78_BANK1_R5_REGNUM,
  117.   RL78_BANK1_R6_REGNUM,
  118.   RL78_BANK1_R7_REGNUM,

  119.   RL78_BANK2_R0_REGNUM,
  120.   RL78_BANK2_R1_REGNUM,
  121.   RL78_BANK2_R2_REGNUM,
  122.   RL78_BANK2_R3_REGNUM,
  123.   RL78_BANK2_R4_REGNUM,
  124.   RL78_BANK2_R5_REGNUM,
  125.   RL78_BANK2_R6_REGNUM,
  126.   RL78_BANK2_R7_REGNUM,

  127.   RL78_BANK3_R0_REGNUM,
  128.   RL78_BANK3_R1_REGNUM,
  129.   RL78_BANK3_R2_REGNUM,
  130.   RL78_BANK3_R3_REGNUM,
  131.   RL78_BANK3_R4_REGNUM,
  132.   RL78_BANK3_R5_REGNUM,
  133.   RL78_BANK3_R6_REGNUM,
  134.   RL78_BANK3_R7_REGNUM,

  135.   RL78_BANK0_RP0_REGNUM,
  136.   RL78_BANK0_RP1_REGNUM,
  137.   RL78_BANK0_RP2_REGNUM,
  138.   RL78_BANK0_RP3_REGNUM,

  139.   RL78_BANK1_RP0_REGNUM,
  140.   RL78_BANK1_RP1_REGNUM,
  141.   RL78_BANK1_RP2_REGNUM,
  142.   RL78_BANK1_RP3_REGNUM,

  143.   RL78_BANK2_RP0_REGNUM,
  144.   RL78_BANK2_RP1_REGNUM,
  145.   RL78_BANK2_RP2_REGNUM,
  146.   RL78_BANK2_RP3_REGNUM,

  147.   RL78_BANK3_RP0_REGNUM,
  148.   RL78_BANK3_RP1_REGNUM,
  149.   RL78_BANK3_RP2_REGNUM,
  150.   RL78_BANK3_RP3_REGNUM,

  151.   /* These are the same as the above 16 registers, but have
  152.      a pointer type for use as base registers in expression
  153.      evaluation.  These are not user visible registers.  */
  154.   RL78_BANK0_RP0_PTR_REGNUM,
  155.   RL78_BANK0_RP1_PTR_REGNUM,
  156.   RL78_BANK0_RP2_PTR_REGNUM,
  157.   RL78_BANK0_RP3_PTR_REGNUM,

  158.   RL78_BANK1_RP0_PTR_REGNUM,
  159.   RL78_BANK1_RP1_PTR_REGNUM,
  160.   RL78_BANK1_RP2_PTR_REGNUM,
  161.   RL78_BANK1_RP3_PTR_REGNUM,

  162.   RL78_BANK2_RP0_PTR_REGNUM,
  163.   RL78_BANK2_RP1_PTR_REGNUM,
  164.   RL78_BANK2_RP2_PTR_REGNUM,
  165.   RL78_BANK2_RP3_PTR_REGNUM,

  166.   RL78_BANK3_RP0_PTR_REGNUM,
  167.   RL78_BANK3_RP1_PTR_REGNUM,
  168.   RL78_BANK3_RP2_PTR_REGNUM,
  169.   RL78_BANK3_RP3_PTR_REGNUM,

  170.   RL78_NUM_TOTAL_REGS,
  171.   RL78_NUM_PSEUDO_REGS = RL78_NUM_TOTAL_REGS - RL78_NUM_REGS
  172. };

  173. /* Architecture specific data.  */

  174. struct gdbarch_tdep
  175. {
  176.   /* The ELF header flags specify the multilib used.  */
  177.   int elf_flags;

  178.   struct type *rl78_void,
  179.               *rl78_uint8,
  180.               *rl78_int8,
  181.               *rl78_uint16,
  182.               *rl78_int16,
  183.               *rl78_uint32,
  184.               *rl78_int32,
  185.               *rl78_data_pointer,
  186.               *rl78_code_pointer;
  187. };

  188. /* This structure holds the results of a prologue analysis.  */

  189. struct rl78_prologue
  190. {
  191.   /* The offset from the frame base to the stack pointer --- always
  192.      zero or negative.

  193.      Calling this a "size" is a bit misleading, but given that the
  194.      stack grows downwards, using offsets for everything keeps one
  195.      from going completely sign-crazy: you never change anything's
  196.      sign for an ADD instruction; always change the second operand's
  197.      sign for a SUB instruction; and everything takes care of
  198.      itself.  */
  199.   int frame_size;

  200.   /* Non-zero if this function has initialized the frame pointer from
  201.      the stack pointer, zero otherwise.  */
  202.   int has_frame_ptr;

  203.   /* If has_frame_ptr is non-zero, this is the offset from the frame
  204.      base to where the frame pointer points.  This is always zero or
  205.      negative.  */
  206.   int frame_ptr_offset;

  207.   /* The address of the first instruction at which the frame has been
  208.      set up and the arguments are where the debug info says they are
  209.      --- as best as we can tell.  */
  210.   CORE_ADDR prologue_end;

  211.   /* reg_offset[R] is the offset from the CFA at which register R is
  212.      saved, or 1 if register R has not been saved.  (Real values are
  213.      always zero or negative.)  */
  214.   int reg_offset[RL78_NUM_TOTAL_REGS];
  215. };

  216. /* Implement the "register_type" gdbarch method.  */

  217. static struct type *
  218. rl78_register_type (struct gdbarch *gdbarch, int reg_nr)
  219. {
  220.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  221.   if (reg_nr == RL78_PC_REGNUM)
  222.     return tdep->rl78_code_pointer;
  223.   else if (reg_nr == RL78_RAW_PC_REGNUM)
  224.     return tdep->rl78_uint32;
  225.   else if (reg_nr <= RL78_MEM_REGNUM
  226.            || (RL78_X_REGNUM <= reg_nr && reg_nr <= RL78_H_REGNUM)
  227.            || (RL78_BANK0_R0_REGNUM <= reg_nr
  228.                && reg_nr <= RL78_BANK3_R7_REGNUM))
  229.     return tdep->rl78_int8;
  230.   else if (reg_nr == RL78_SP_REGNUM
  231.            || (RL78_BANK0_RP0_PTR_REGNUM <= reg_nr
  232.                && reg_nr <= RL78_BANK3_RP3_PTR_REGNUM))
  233.     return tdep->rl78_data_pointer;
  234.   else
  235.     return tdep->rl78_int16;
  236. }

  237. /* Implement the "register_name" gdbarch method.  */

  238. static const char *
  239. rl78_register_name (struct gdbarch *gdbarch, int regnr)
  240. {
  241.   static const char *const reg_names[] =
  242.   {
  243.     "",                /* bank0_r0 */
  244.     "",                /* bank0_r1 */
  245.     "",                /* bank0_r2 */
  246.     "",                /* bank0_r3 */
  247.     "",                /* bank0_r4 */
  248.     "",                /* bank0_r5 */
  249.     "",                /* bank0_r6 */
  250.     "",                /* bank0_r7 */

  251.     "",                /* bank1_r0 */
  252.     "",                /* bank1_r1 */
  253.     "",                /* bank1_r2 */
  254.     "",                /* bank1_r3 */
  255.     "",                /* bank1_r4 */
  256.     "",                /* bank1_r5 */
  257.     "",                /* bank1_r6 */
  258.     "",                /* bank1_r7 */

  259.     "",                /* bank2_r0 */
  260.     "",                /* bank2_r1 */
  261.     "",                /* bank2_r2 */
  262.     "",                /* bank2_r3 */
  263.     "",                /* bank2_r4 */
  264.     "",                /* bank2_r5 */
  265.     "",                /* bank2_r6 */
  266.     "",                /* bank2_r7 */

  267.     "",                /* bank3_r0 */
  268.     "",                /* bank3_r1 */
  269.     "",                /* bank3_r2 */
  270.     "",                /* bank3_r3 */
  271.     "",                /* bank3_r4 */
  272.     "",                /* bank3_r5 */
  273.     "",                /* bank3_r6 */
  274.     "",                /* bank3_r7 */

  275.     "psw",
  276.     "es",
  277.     "cs",
  278.     "",

  279.     "",                /* spl */
  280.     "",                /* sph */
  281.     "pmc",
  282.     "mem",

  283.     "pc",
  284.     "sp",

  285.     "x",
  286.     "a",
  287.     "c",
  288.     "b",
  289.     "e",
  290.     "d",
  291.     "l",
  292.     "h",

  293.     "ax",
  294.     "bc",
  295.     "de",
  296.     "hl",

  297.     "bank0_r0",
  298.     "bank0_r1",
  299.     "bank0_r2",
  300.     "bank0_r3",
  301.     "bank0_r4",
  302.     "bank0_r5",
  303.     "bank0_r6",
  304.     "bank0_r7",

  305.     "bank1_r0",
  306.     "bank1_r1",
  307.     "bank1_r2",
  308.     "bank1_r3",
  309.     "bank1_r4",
  310.     "bank1_r5",
  311.     "bank1_r6",
  312.     "bank1_r7",

  313.     "bank2_r0",
  314.     "bank2_r1",
  315.     "bank2_r2",
  316.     "bank2_r3",
  317.     "bank2_r4",
  318.     "bank2_r5",
  319.     "bank2_r6",
  320.     "bank2_r7",

  321.     "bank3_r0",
  322.     "bank3_r1",
  323.     "bank3_r2",
  324.     "bank3_r3",
  325.     "bank3_r4",
  326.     "bank3_r5",
  327.     "bank3_r6",
  328.     "bank3_r7",

  329.     "bank0_rp0",
  330.     "bank0_rp1",
  331.     "bank0_rp2",
  332.     "bank0_rp3",

  333.     "bank1_rp0",
  334.     "bank1_rp1",
  335.     "bank1_rp2",
  336.     "bank1_rp3",

  337.     "bank2_rp0",
  338.     "bank2_rp1",
  339.     "bank2_rp2",
  340.     "bank2_rp3",

  341.     "bank3_rp0",
  342.     "bank3_rp1",
  343.     "bank3_rp2",
  344.     "bank3_rp3",

  345.     /* The 16 register slots would be named
  346.        bank0_rp0_ptr_regnum ... bank3_rp3_ptr_regnum, but we don't
  347.        want these to be user visible registers.  */
  348.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
  349.   };

  350.   return reg_names[regnr];
  351. }

  352. /* Implement the "register_name" gdbarch method for the g10 variant.  */

  353. static const char *
  354. rl78_g10_register_name (struct gdbarch *gdbarch, int regnr)
  355. {
  356.   static const char *const reg_names[] =
  357.   {
  358.     "",                /* bank0_r0 */
  359.     "",                /* bank0_r1 */
  360.     "",                /* bank0_r2 */
  361.     "",                /* bank0_r3 */
  362.     "",                /* bank0_r4 */
  363.     "",                /* bank0_r5 */
  364.     "",                /* bank0_r6 */
  365.     "",                /* bank0_r7 */

  366.     "",                /* bank1_r0 */
  367.     "",                /* bank1_r1 */
  368.     "",                /* bank1_r2 */
  369.     "",                /* bank1_r3 */
  370.     "",                /* bank1_r4 */
  371.     "",                /* bank1_r5 */
  372.     "",                /* bank1_r6 */
  373.     "",                /* bank1_r7 */

  374.     "",                /* bank2_r0 */
  375.     "",                /* bank2_r1 */
  376.     "",                /* bank2_r2 */
  377.     "",                /* bank2_r3 */
  378.     "",                /* bank2_r4 */
  379.     "",                /* bank2_r5 */
  380.     "",                /* bank2_r6 */
  381.     "",                /* bank2_r7 */

  382.     "",                /* bank3_r0 */
  383.     "",                /* bank3_r1 */
  384.     "",                /* bank3_r2 */
  385.     "",                /* bank3_r3 */
  386.     "",                /* bank3_r4 */
  387.     "",                /* bank3_r5 */
  388.     "",                /* bank3_r6 */
  389.     "",                /* bank3_r7 */

  390.     "psw",
  391.     "es",
  392.     "cs",
  393.     "",

  394.     "",                /* spl */
  395.     "",                /* sph */
  396.     "pmc",
  397.     "mem",

  398.     "pc",
  399.     "sp",

  400.     "x",
  401.     "a",
  402.     "c",
  403.     "b",
  404.     "e",
  405.     "d",
  406.     "l",
  407.     "h",

  408.     "ax",
  409.     "bc",
  410.     "de",
  411.     "hl",

  412.     "bank0_r0",
  413.     "bank0_r1",
  414.     "bank0_r2",
  415.     "bank0_r3",
  416.     "bank0_r4",
  417.     "bank0_r5",
  418.     "bank0_r6",
  419.     "bank0_r7",

  420.     "",
  421.     "",
  422.     "",
  423.     "",
  424.     "",
  425.     "",
  426.     "",
  427.     "",

  428.     "",
  429.     "",
  430.     "",
  431.     "",
  432.     "",
  433.     "",
  434.     "",
  435.     "",

  436.     "",
  437.     "",
  438.     "",
  439.     "",
  440.     "",
  441.     "",
  442.     "",
  443.     "",

  444.     "bank0_rp0",
  445.     "bank0_rp1",
  446.     "bank0_rp2",
  447.     "bank0_rp3",

  448.     "",
  449.     "",
  450.     "",
  451.     "",

  452.     "",
  453.     "",
  454.     "",
  455.     "",

  456.     "",
  457.     "",
  458.     "",
  459.     "",

  460.     /* The 16 register slots would be named
  461.        bank0_rp0_ptr_regnum ... bank3_rp3_ptr_regnum, but we don't
  462.        want these to be user visible registers.  */
  463.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
  464.   };

  465.   return reg_names[regnr];
  466. }

  467. /* Implement the "register_reggroup_p" gdbarch method.  */

  468. static int
  469. rl78_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  470.                           struct reggroup *group)
  471. {
  472.   if (group == all_reggroup)
  473.     return 1;

  474.   /* All other registers are saved and restored.  */
  475.   if (group == save_reggroup || group == restore_reggroup)
  476.     {
  477.       if ((regnum < RL78_NUM_REGS
  478.            && regnum != RL78_SPL_REGNUM
  479.            && regnum != RL78_SPH_REGNUM
  480.            && regnum != RL78_RAW_PC_REGNUM)
  481.           || regnum == RL78_SP_REGNUM
  482.           || regnum == RL78_PC_REGNUM)
  483.         return 1;
  484.       else
  485.         return 0;
  486.     }

  487.   if ((RL78_BANK0_R0_REGNUM <= regnum && regnum <= RL78_BANK3_R7_REGNUM)
  488.       || regnum == RL78_ES_REGNUM
  489.       || regnum == RL78_CS_REGNUM
  490.       || regnum == RL78_SPL_REGNUM
  491.       || regnum == RL78_SPH_REGNUM
  492.       || regnum == RL78_PMC_REGNUM
  493.       || regnum == RL78_MEM_REGNUM
  494.       || regnum == RL78_RAW_PC_REGNUM
  495.       || (RL78_BANK0_RP0_REGNUM <= regnum && regnum <= RL78_BANK3_RP3_REGNUM))
  496.     return group == system_reggroup;

  497.   return group == general_reggroup;
  498. }

  499. /* Strip bits to form an instruction address.  (When fetching a
  500.    32-bit address from the stack, the high eight bits are garbage.
  501.    This function strips off those unused bits.)  */

  502. static CORE_ADDR
  503. rl78_make_instruction_address (CORE_ADDR addr)
  504. {
  505.   return addr & 0xffffff;
  506. }

  507. /* Set / clear bits necessary to make a data address.  */

  508. static CORE_ADDR
  509. rl78_make_data_address (CORE_ADDR addr)
  510. {
  511.   return (addr & 0xffff) | 0xf0000;
  512. }

  513. /* Implement the "pseudo_register_read" gdbarch method.  */

  514. static enum register_status
  515. rl78_pseudo_register_read (struct gdbarch *gdbarch,
  516.                            struct regcache *regcache,
  517.                            int reg, gdb_byte *buffer)
  518. {
  519.   enum register_status status;

  520.   if (RL78_BANK0_R0_REGNUM <= reg && reg <= RL78_BANK3_R7_REGNUM)
  521.     {
  522.       int raw_regnum = RL78_RAW_BANK0_R0_REGNUM
  523.                        + (reg - RL78_BANK0_R0_REGNUM);

  524.       status = regcache_raw_read (regcache, raw_regnum, buffer);
  525.     }
  526.   else if (RL78_BANK0_RP0_REGNUM <= reg && reg <= RL78_BANK3_RP3_REGNUM)
  527.     {
  528.       int raw_regnum = 2 * (reg - RL78_BANK0_RP0_REGNUM)
  529.                        + RL78_RAW_BANK0_R0_REGNUM;

  530.       status = regcache_raw_read (regcache, raw_regnum, buffer);
  531.       if (status == REG_VALID)
  532.         status = regcache_raw_read (regcache, raw_regnum + 1, buffer + 1);
  533.     }
  534.   else if (RL78_BANK0_RP0_PTR_REGNUM <= reg && reg <= RL78_BANK3_RP3_PTR_REGNUM)
  535.     {
  536.       int raw_regnum = 2 * (reg - RL78_BANK0_RP0_PTR_REGNUM)
  537.                        + RL78_RAW_BANK0_R0_REGNUM;

  538.       status = regcache_raw_read (regcache, raw_regnum, buffer);
  539.       if (status == REG_VALID)
  540.         status = regcache_raw_read (regcache, raw_regnum + 1, buffer + 1);
  541.     }
  542.   else if (reg == RL78_SP_REGNUM)
  543.     {
  544.       status = regcache_raw_read (regcache, RL78_SPL_REGNUM, buffer);
  545.       if (status == REG_VALID)
  546.         status = regcache_raw_read (regcache, RL78_SPH_REGNUM, buffer + 1);
  547.     }
  548.   else if (reg == RL78_PC_REGNUM)
  549.     {
  550.       gdb_byte rawbuf[4];

  551.       status = regcache_raw_read (regcache, RL78_RAW_PC_REGNUM, rawbuf);
  552.       memcpy (buffer, rawbuf, 3);
  553.     }
  554.   else if (RL78_X_REGNUM <= reg && reg <= RL78_H_REGNUM)
  555.     {
  556.       ULONGEST psw;

  557.       status = regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  558.       if (status == REG_VALID)
  559.         {
  560.           /* RSB0 is at bit 3; RSBS1 is at bit 5.  */
  561.           int bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  562.           int raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  563.                            + (reg - RL78_X_REGNUM);
  564.           status = regcache_raw_read (regcache, raw_regnum, buffer);
  565.         }
  566.     }
  567.   else if (RL78_AX_REGNUM <= reg && reg <= RL78_HL_REGNUM)
  568.     {
  569.       ULONGEST psw;

  570.       status = regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  571.       if (status == REG_VALID)
  572.         {
  573.           /* RSB0 is at bit 3; RSBS1 is at bit 5.  */
  574.           int bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  575.           int raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  576.                            + 2 * (reg - RL78_AX_REGNUM);
  577.           status = regcache_raw_read (regcache, raw_regnum, buffer);
  578.           if (status == REG_VALID)
  579.             status = regcache_raw_read (regcache, raw_regnum + 1,
  580.                                         buffer + 1);
  581.         }
  582.     }
  583.   else
  584.     gdb_assert_not_reached ("invalid pseudo register number");
  585.   return status;
  586. }

  587. /* Implement the "pseudo_register_write" gdbarch method.  */

  588. static void
  589. rl78_pseudo_register_write (struct gdbarch *gdbarch,
  590.                             struct regcache *regcache,
  591.                             int reg, const gdb_byte *buffer)
  592. {
  593.   if (RL78_BANK0_R0_REGNUM <= reg && reg <= RL78_BANK3_R7_REGNUM)
  594.     {
  595.       int raw_regnum = RL78_RAW_BANK0_R0_REGNUM
  596.                        + (reg - RL78_BANK0_R0_REGNUM);

  597.       regcache_raw_write (regcache, raw_regnum, buffer);
  598.     }
  599.   else if (RL78_BANK0_RP0_REGNUM <= reg && reg <= RL78_BANK3_RP3_REGNUM)
  600.     {
  601.       int raw_regnum = 2 * (reg - RL78_BANK0_RP0_REGNUM)
  602.                        + RL78_RAW_BANK0_R0_REGNUM;

  603.       regcache_raw_write (regcache, raw_regnum, buffer);
  604.       regcache_raw_write (regcache, raw_regnum + 1, buffer + 1);
  605.     }
  606.   else if (RL78_BANK0_RP0_PTR_REGNUM <= reg && reg <= RL78_BANK3_RP3_PTR_REGNUM)
  607.     {
  608.       int raw_regnum = 2 * (reg - RL78_BANK0_RP0_PTR_REGNUM)
  609.                        + RL78_RAW_BANK0_R0_REGNUM;

  610.       regcache_raw_write (regcache, raw_regnum, buffer);
  611.       regcache_raw_write (regcache, raw_regnum + 1, buffer + 1);
  612.     }
  613.   else if (reg == RL78_SP_REGNUM)
  614.     {
  615.       regcache_raw_write (regcache, RL78_SPL_REGNUM, buffer);
  616.       regcache_raw_write (regcache, RL78_SPH_REGNUM, buffer + 1);
  617.     }
  618.   else if (reg == RL78_PC_REGNUM)
  619.     {
  620.       gdb_byte rawbuf[4];

  621.       memcpy (rawbuf, buffer, 3);
  622.       rawbuf[3] = 0;
  623.       regcache_raw_write (regcache, RL78_RAW_PC_REGNUM, rawbuf);
  624.     }
  625.   else if (RL78_X_REGNUM <= reg && reg <= RL78_H_REGNUM)
  626.     {
  627.       ULONGEST psw;
  628.       int bank;
  629.       int raw_regnum;

  630.       regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  631.       bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  632.       /* RSB0 is at bit 3; RSBS1 is at bit 5.  */
  633.       raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  634.                    + (reg - RL78_X_REGNUM);
  635.       regcache_raw_write (regcache, raw_regnum, buffer);
  636.     }
  637.   else if (RL78_AX_REGNUM <= reg && reg <= RL78_HL_REGNUM)
  638.     {
  639.       ULONGEST psw;
  640.       int bank, raw_regnum;

  641.       regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  642.       bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  643.       /* RSB0 is at bit 3; RSBS1 is at bit 5.  */
  644.       raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  645.                    + 2 * (reg - RL78_AX_REGNUM);
  646.       regcache_raw_write (regcache, raw_regnum, buffer);
  647.       regcache_raw_write (regcache, raw_regnum + 1, buffer + 1);
  648.     }
  649.   else
  650.     gdb_assert_not_reached ("invalid pseudo register number");
  651. }

  652. /* Implement the "breakpoint_from_pc" gdbarch method.  */

  653. static const gdb_byte *
  654. rl78_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  655.                          int *lenptr)
  656. {
  657.   /* The documented BRK instruction is actually a two byte sequence,
  658.      {0x61, 0xcc}, but instructions may be as short as one byte.
  659.      Correspondence with Renesas revealed that the one byte sequence
  660.      0xff is used when a one byte breakpoint instruction is required.  */
  661.   static gdb_byte breakpoint[] = { 0xff };

  662.   *lenptr = sizeof breakpoint;
  663.   return breakpoint;
  664. }

  665. /* Define a "handle" struct for fetching the next opcode.  */

  666. struct rl78_get_opcode_byte_handle
  667. {
  668.   CORE_ADDR pc;
  669. };

  670. /* Fetch a byte on behalf of the opcode decoder.  HANDLE contains
  671.    the memory address of the next byte to fetch.  If successful,
  672.    the address in the handle is updated and the byte fetched is
  673.    returned as the value of the function.  If not successful, -1
  674.    is returned.  */

  675. static int
  676. rl78_get_opcode_byte (void *handle)
  677. {
  678.   struct rl78_get_opcode_byte_handle *opcdata = handle;
  679.   int status;
  680.   gdb_byte byte;

  681.   status = target_read_memory (opcdata->pc, &byte, 1);
  682.   if (status == 0)
  683.     {
  684.       opcdata->pc += 1;
  685.       return byte;
  686.     }
  687.   else
  688.     return -1;
  689. }

  690. /* Function for finding saved registers in a 'struct pv_area'; this
  691.    function is passed to pv_area_scan.

  692.    If VALUE is a saved register, ADDR says it was saved at a constant
  693.    offset from the frame base, and SIZE indicates that the whole
  694.    register was saved, record its offset.  */

  695. static void
  696. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size,
  697.                  pv_t value)
  698. {
  699.   struct rl78_prologue *result = (struct rl78_prologue *) result_untyped;

  700.   if (value.kind == pvk_register
  701.       && value.k == 0
  702.       && pv_is_register (addr, RL78_SP_REGNUM)
  703.       && size == register_size (target_gdbarch (), value.reg))
  704.     result->reg_offset[value.reg] = addr.k;
  705. }

  706. /* Analyze a prologue starting at START_PC, going no further than
  707.    LIMIT_PC.  Fill in RESULT as appropriate.  */

  708. static void
  709. rl78_analyze_prologue (CORE_ADDR start_pc,
  710.                        CORE_ADDR limit_pc, struct rl78_prologue *result)
  711. {
  712.   CORE_ADDR pc, next_pc;
  713.   int rn;
  714.   pv_t reg[RL78_NUM_TOTAL_REGS];
  715.   struct pv_area *stack;
  716.   struct cleanup *back_to;
  717.   CORE_ADDR after_last_frame_setup_insn = start_pc;
  718.   int bank = 0;

  719.   memset (result, 0, sizeof (*result));

  720.   for (rn = 0; rn < RL78_NUM_TOTAL_REGS; rn++)
  721.     {
  722.       reg[rn] = pv_register (rn, 0);
  723.       result->reg_offset[rn] = 1;
  724.     }

  725.   stack = make_pv_area (RL78_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
  726.   back_to = make_cleanup_free_pv_area (stack);

  727.   /* The call instruction has saved the return address on the stack.  */
  728.   reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -4);
  729.   pv_area_store (stack, reg[RL78_SP_REGNUM], 4, reg[RL78_PC_REGNUM]);

  730.   pc = start_pc;
  731.   while (pc < limit_pc)
  732.     {
  733.       int bytes_read;
  734.       struct rl78_get_opcode_byte_handle opcode_handle;
  735.       RL78_Opcode_Decoded opc;

  736.       opcode_handle.pc = pc;
  737.       bytes_read = rl78_decode_opcode (pc, &opc, rl78_get_opcode_byte,
  738.                                      &opcode_handle);
  739.       next_pc = pc + bytes_read;

  740.       if (opc.id == RLO_sel)
  741.         {
  742.           bank = opc.op[1].addend;
  743.         }
  744.       else if (opc.id == RLO_mov
  745.                && opc.op[0].type == RL78_Operand_PreDec
  746.                && opc.op[0].reg == RL78_Reg_SP
  747.                && opc.op[1].type == RL78_Operand_Register)
  748.         {
  749.           int rsrc = (bank * RL78_REGS_PER_BANK)
  750.                    + 2 * (opc.op[1].reg - RL78_Reg_AX);

  751.           reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -1);
  752.           pv_area_store (stack, reg[RL78_SP_REGNUM], 1, reg[rsrc]);
  753.           reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -1);
  754.           pv_area_store (stack, reg[RL78_SP_REGNUM], 1, reg[rsrc + 1]);
  755.           after_last_frame_setup_insn = next_pc;
  756.         }
  757.       else if (opc.id == RLO_sub
  758.                && opc.op[0].type == RL78_Operand_Register
  759.                && opc.op[0].reg == RL78_Reg_SP
  760.                && opc.op[1].type == RL78_Operand_Immediate)
  761.         {
  762.           int addend = opc.op[1].addend;

  763.           reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM],
  764.                                                  -addend);
  765.           after_last_frame_setup_insn = next_pc;
  766.         }
  767.       else
  768.         {
  769.           /* Terminate the prologue scan.  */
  770.           break;
  771.         }

  772.       pc = next_pc;
  773.     }

  774.   /* Is the frame size (offset, really) a known constant?  */
  775.   if (pv_is_register (reg[RL78_SP_REGNUM], RL78_SP_REGNUM))
  776.     result->frame_size = reg[RL78_SP_REGNUM].k;

  777.   /* Record where all the registers were saved.  */
  778.   pv_area_scan (stack, check_for_saved, (void *) result);

  779.   result->prologue_end = after_last_frame_setup_insn;

  780.   do_cleanups (back_to);
  781. }

  782. /* Implement the "addr_bits_remove" gdbarch method.  */

  783. static CORE_ADDR
  784. rl78_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  785. {
  786.   return addr & 0xffffff;
  787. }

  788. /* Implement the "address_to_pointer" gdbarch method.  */

  789. static void
  790. rl78_address_to_pointer (struct gdbarch *gdbarch,
  791.                          struct type *type, gdb_byte *buf, CORE_ADDR addr)
  792. {
  793.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  794.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  795.                           addr & 0xffffff);
  796. }

  797. /* Implement the "pointer_to_address" gdbarch method.  */

  798. static CORE_ADDR
  799. rl78_pointer_to_address (struct gdbarch *gdbarch,
  800.                          struct type *type, const gdb_byte *buf)
  801. {
  802.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  803.   CORE_ADDR addr
  804.     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);

  805.   /* Is it a code address?  */
  806.   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
  807.       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
  808.       || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type))
  809.       || TYPE_LENGTH (type) == 4)
  810.     return rl78_make_instruction_address (addr);
  811.   else
  812.     return rl78_make_data_address (addr);
  813. }

  814. /* Implement the "skip_prologue" gdbarch method.  */

  815. static CORE_ADDR
  816. rl78_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  817. {
  818.   const char *name;
  819.   CORE_ADDR func_addr, func_end;
  820.   struct rl78_prologue p;

  821.   /* Try to find the extent of the function that contains PC.  */
  822.   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  823.     return pc;

  824.   rl78_analyze_prologue (pc, func_end, &p);
  825.   return p.prologue_end;
  826. }

  827. /* Implement the "unwind_pc" gdbarch method.  */

  828. static CORE_ADDR
  829. rl78_unwind_pc (struct gdbarch *arch, struct frame_info *next_frame)
  830. {
  831.   return rl78_addr_bits_remove
  832.            (arch, frame_unwind_register_unsigned (next_frame,
  833.                                                   RL78_PC_REGNUM));
  834. }

  835. /* Implement the "unwind_sp" gdbarch method.  */

  836. static CORE_ADDR
  837. rl78_unwind_sp (struct gdbarch *arch, struct frame_info *next_frame)
  838. {
  839.   return frame_unwind_register_unsigned (next_frame, RL78_SP_REGNUM);
  840. }

  841. /* Given a frame described by THIS_FRAME, decode the prologue of its
  842.    associated function if there is not cache entry as specified by
  843.    THIS_PROLOGUE_CACHE.  Save the decoded prologue in the cache and
  844.    return that struct as the value of this function.  */

  845. static struct rl78_prologue *
  846. rl78_analyze_frame_prologue (struct frame_info *this_frame,
  847.                            void **this_prologue_cache)
  848. {
  849.   if (!*this_prologue_cache)
  850.     {
  851.       CORE_ADDR func_start, stop_addr;

  852.       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rl78_prologue);

  853.       func_start = get_frame_func (this_frame);
  854.       stop_addr = get_frame_pc (this_frame);

  855.       /* If we couldn't find any function containing the PC, then
  856.          just initialize the prologue cache, but don't do anything.  */
  857.       if (!func_start)
  858.         stop_addr = func_start;

  859.       rl78_analyze_prologue (func_start, stop_addr, *this_prologue_cache);
  860.     }

  861.   return *this_prologue_cache;
  862. }

  863. /* Given a frame and a prologue cache, return this frame's base.  */

  864. static CORE_ADDR
  865. rl78_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  866. {
  867.   struct rl78_prologue *p
  868.     = rl78_analyze_frame_prologue (this_frame, this_prologue_cache);
  869.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, RL78_SP_REGNUM);

  870.   return rl78_make_data_address (sp - p->frame_size);
  871. }

  872. /* Implement the "frame_this_id" method for unwinding frames.  */

  873. static void
  874. rl78_this_id (struct frame_info *this_frame,
  875.               void **this_prologue_cache, struct frame_id *this_id)
  876. {
  877.   *this_id = frame_id_build (rl78_frame_base (this_frame,
  878.                                               this_prologue_cache),
  879.                              get_frame_func (this_frame));
  880. }

  881. /* Implement the "frame_prev_register" method for unwinding frames.  */

  882. static struct value *
  883. rl78_prev_register (struct frame_info *this_frame,
  884.                     void **this_prologue_cache, int regnum)
  885. {
  886.   struct rl78_prologue *p
  887.     = rl78_analyze_frame_prologue (this_frame, this_prologue_cache);
  888.   CORE_ADDR frame_base = rl78_frame_base (this_frame, this_prologue_cache);

  889.   if (regnum == RL78_SP_REGNUM)
  890.     return frame_unwind_got_constant (this_frame, regnum, frame_base);

  891.   else if (regnum == RL78_SPL_REGNUM)
  892.     return frame_unwind_got_constant (this_frame, regnum,
  893.                                       (frame_base & 0xff));

  894.   else if (regnum == RL78_SPH_REGNUM)
  895.     return frame_unwind_got_constant (this_frame, regnum,
  896.                                       ((frame_base >> 8) & 0xff));

  897.   /* If prologue analysis says we saved this register somewhere,
  898.      return a description of the stack slot holding it.  */
  899.   else if (p->reg_offset[regnum] != 1)
  900.     {
  901.       struct value *rv =
  902.         frame_unwind_got_memory (this_frame, regnum,
  903.                                  frame_base + p->reg_offset[regnum]);

  904.       if (regnum == RL78_PC_REGNUM)
  905.         {
  906.           ULONGEST pc = rl78_make_instruction_address (value_as_long (rv));

  907.           return frame_unwind_got_constant (this_frame, regnum, pc);
  908.         }
  909.       return rv;
  910.     }

  911.   /* Otherwise, presume we haven't changed the value of this
  912.      register, and get it from the next frame.  */
  913.   else
  914.     return frame_unwind_got_register (this_frame, regnum, regnum);
  915. }

  916. static const struct frame_unwind rl78_unwind =
  917. {
  918.   NORMAL_FRAME,
  919.   default_frame_unwind_stop_reason,
  920.   rl78_this_id,
  921.   rl78_prev_register,
  922.   NULL,
  923.   default_frame_sniffer
  924. };

  925. /* Implement the "dwarf_reg_to_regnum" gdbarch method.  */

  926. static int
  927. rl78_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  928. {
  929.   if (0 <= reg && reg <= 31)
  930.     {
  931.       if ((reg & 1) == 0)
  932.         /* Map even registers to their 16-bit counterparts which have a
  933.            pointer type.  This is usually what is required from the DWARF
  934.            info.  */
  935.         return (reg >> 1) + RL78_BANK0_RP0_PTR_REGNUM;
  936.       else
  937.         return reg;
  938.     }
  939.   else if (reg == 32)
  940.     return RL78_SP_REGNUM;
  941.   else if (reg == 33)
  942.     return -1;                        /* ap */
  943.   else if (reg == 34)
  944.     return RL78_PSW_REGNUM;
  945.   else if (reg == 35)
  946.     return RL78_ES_REGNUM;
  947.   else if (reg == 36)
  948.     return RL78_CS_REGNUM;
  949.   else if (reg == 37)
  950.     return RL78_PC_REGNUM;
  951.   else
  952.     internal_error (__FILE__, __LINE__,
  953.                     _("Undefined dwarf2 register mapping of reg %d"),
  954.                     reg);
  955. }

  956. /* Implement the `register_sim_regno' gdbarch method.  */

  957. static int
  958. rl78_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  959. {
  960.   gdb_assert (regnum < RL78_NUM_REGS);

  961.   /* So long as regnum is in [0, RL78_NUM_REGS), it's valid.  We
  962.      just want to override the default here which disallows register
  963.      numbers which have no names.  */
  964.   return regnum;
  965. }

  966. /* Implement the "return_value" gdbarch method.  */

  967. static enum return_value_convention
  968. rl78_return_value (struct gdbarch *gdbarch,
  969.                    struct value *function,
  970.                    struct type *valtype,
  971.                    struct regcache *regcache,
  972.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  973. {
  974.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  975.   ULONGEST valtype_len = TYPE_LENGTH (valtype);
  976.   int is_g10 = gdbarch_tdep (gdbarch)->elf_flags & E_FLAG_RL78_G10;

  977.   if (valtype_len > 8)
  978.     return RETURN_VALUE_STRUCT_CONVENTION;

  979.   if (readbuf)
  980.     {
  981.       ULONGEST u;
  982.       int argreg = RL78_RAW_BANK1_R0_REGNUM;
  983.       CORE_ADDR g10_raddr = 0xffec8;
  984.       int offset = 0;

  985.       while (valtype_len > 0)
  986.         {
  987.           if (is_g10)
  988.             u = read_memory_integer (g10_raddr, 1,
  989.                                      gdbarch_byte_order (gdbarch));
  990.           else
  991.             regcache_cooked_read_unsigned (regcache, argreg, &u);
  992.           store_unsigned_integer (readbuf + offset, 1, byte_order, u);
  993.           valtype_len -= 1;
  994.           offset += 1;
  995.           argreg++;
  996.           g10_raddr++;
  997.         }
  998.     }

  999.   if (writebuf)
  1000.     {
  1001.       ULONGEST u;
  1002.       int argreg = RL78_RAW_BANK1_R0_REGNUM;
  1003.       CORE_ADDR g10_raddr = 0xffec8;
  1004.       int offset = 0;

  1005.       while (valtype_len > 0)
  1006.         {
  1007.           u = extract_unsigned_integer (writebuf + offset, 1, byte_order);
  1008.           if (is_g10) {
  1009.             gdb_byte b = u & 0xff;
  1010.             write_memory (g10_raddr, &b, 1);
  1011.           }
  1012.           else
  1013.             regcache_cooked_write_unsigned (regcache, argreg, u);
  1014.           valtype_len -= 1;
  1015.           offset += 1;
  1016.           argreg++;
  1017.           g10_raddr++;
  1018.         }
  1019.     }

  1020.   return RETURN_VALUE_REGISTER_CONVENTION;
  1021. }


  1022. /* Implement the "frame_align" gdbarch method.  */

  1023. static CORE_ADDR
  1024. rl78_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1025. {
  1026.   return rl78_make_data_address (align_down (sp, 2));
  1027. }


  1028. /* Implement the "dummy_id" gdbarch method.  */

  1029. static struct frame_id
  1030. rl78_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1031. {
  1032.   return
  1033.     frame_id_build (rl78_make_data_address
  1034.                       (get_frame_register_unsigned
  1035.                         (this_frame, RL78_SP_REGNUM)),
  1036.                     get_frame_pc (this_frame));
  1037. }


  1038. /* Implement the "push_dummy_call" gdbarch method.  */

  1039. static CORE_ADDR
  1040. rl78_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1041.                       struct regcache *regcache, CORE_ADDR bp_addr,
  1042.                       int nargs, struct value **args, CORE_ADDR sp,
  1043.                       int struct_return, CORE_ADDR struct_addr)
  1044. {
  1045.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1046.   gdb_byte buf[4];
  1047.   int i;

  1048.   /* Push arguments in reverse order.  */
  1049.   for (i = nargs - 1; i >= 0; i--)
  1050.     {
  1051.       struct type *value_type = value_enclosing_type (args[i]);
  1052.       int len = TYPE_LENGTH (value_type);
  1053.       int container_len = (len + 1) & ~1;

  1054.       sp -= container_len;
  1055.       write_memory (rl78_make_data_address (sp),
  1056.                     value_contents_all (args[i]), len);
  1057.     }

  1058.   /* Store struct value address.  */
  1059.   if (struct_return)
  1060.     {
  1061.       store_unsigned_integer (buf, 2, byte_order, struct_addr);
  1062.       sp -= 2;
  1063.       write_memory (rl78_make_data_address (sp), buf, 2);
  1064.     }

  1065.   /* Store return address.  */
  1066.   sp -= 4;
  1067.   store_unsigned_integer (buf, 4, byte_order, bp_addr);
  1068.   write_memory (rl78_make_data_address (sp), buf, 4);

  1069.   /* Finally, update the stack pointer...  */
  1070.   regcache_cooked_write_unsigned (regcache, RL78_SP_REGNUM, sp);

  1071.   /* DWARF2/GCC uses the stack address *before* the function call as a
  1072.      frame's CFA.  */
  1073.   return rl78_make_data_address (sp + 4);
  1074. }

  1075. /* Allocate and initialize a gdbarch object.  */

  1076. static struct gdbarch *
  1077. rl78_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1078. {
  1079.   struct gdbarch *gdbarch;
  1080.   struct gdbarch_tdep *tdep;
  1081.   int elf_flags;

  1082.   /* Extract the elf_flags if available.  */
  1083.   if (info.abfd != NULL
  1084.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1085.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  1086.   else
  1087.     elf_flags = 0;


  1088.   /* Try to find the architecture in the list of already defined
  1089.      architectures.  */
  1090.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1091.        arches != NULL;
  1092.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1093.     {
  1094.       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
  1095.         continue;

  1096.       return arches->gdbarch;
  1097.     }

  1098.   /* None found, create a new architecture from the information
  1099.      provided.  */
  1100.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  1101.   gdbarch = gdbarch_alloc (&info, tdep);
  1102.   tdep->elf_flags = elf_flags;

  1103.   /* Initialize types.  */
  1104.   tdep->rl78_void = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
  1105.   tdep->rl78_uint8 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
  1106.   tdep->rl78_int8 = arch_integer_type (gdbarch, 8, 0, "int8_t");
  1107.   tdep->rl78_uint16 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
  1108.   tdep->rl78_int16 = arch_integer_type (gdbarch, 16, 0, "int16_t");
  1109.   tdep->rl78_uint32 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
  1110.   tdep->rl78_int32 = arch_integer_type (gdbarch, 32, 0, "int32_t");

  1111.   tdep->rl78_data_pointer
  1112.     = arch_type (gdbarch, TYPE_CODE_PTR, 16 / TARGET_CHAR_BIT,
  1113.                  xstrdup ("rl78_data_addr_t"));
  1114.   TYPE_TARGET_TYPE (tdep->rl78_data_pointer) = tdep->rl78_void;
  1115.   TYPE_UNSIGNED (tdep->rl78_data_pointer) = 1;

  1116.   tdep->rl78_code_pointer
  1117.     = arch_type (gdbarch, TYPE_CODE_PTR, 32 / TARGET_CHAR_BIT,
  1118.                  xstrdup ("rl78_code_addr_t"));
  1119.   TYPE_TARGET_TYPE (tdep->rl78_code_pointer) = tdep->rl78_void;
  1120.   TYPE_UNSIGNED (tdep->rl78_code_pointer) = 1;

  1121.   /* Registers.  */
  1122.   set_gdbarch_num_regs (gdbarch, RL78_NUM_REGS);
  1123.   set_gdbarch_num_pseudo_regs (gdbarch, RL78_NUM_PSEUDO_REGS);
  1124.   if (tdep->elf_flags & E_FLAG_RL78_G10)
  1125.     set_gdbarch_register_name (gdbarch, rl78_g10_register_name);
  1126.   else
  1127.     set_gdbarch_register_name (gdbarch, rl78_register_name);
  1128.   set_gdbarch_register_type (gdbarch, rl78_register_type);
  1129.   set_gdbarch_pc_regnum (gdbarch, RL78_PC_REGNUM);
  1130.   set_gdbarch_sp_regnum (gdbarch, RL78_SP_REGNUM);
  1131.   set_gdbarch_pseudo_register_read (gdbarch, rl78_pseudo_register_read);
  1132.   set_gdbarch_pseudo_register_write (gdbarch, rl78_pseudo_register_write);
  1133.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rl78_dwarf_reg_to_regnum);
  1134.   set_gdbarch_register_reggroup_p (gdbarch, rl78_register_reggroup_p);
  1135.   set_gdbarch_register_sim_regno (gdbarch, rl78_register_sim_regno);

  1136.   /* Data types.  */
  1137.   set_gdbarch_char_signed (gdbarch, 0);
  1138.   set_gdbarch_short_bit (gdbarch, 16);
  1139.   set_gdbarch_int_bit (gdbarch, 16);
  1140.   set_gdbarch_long_bit (gdbarch, 32);
  1141.   set_gdbarch_long_long_bit (gdbarch, 64);
  1142.   set_gdbarch_ptr_bit (gdbarch, 16);
  1143.   set_gdbarch_addr_bit (gdbarch, 32);
  1144.   set_gdbarch_dwarf2_addr_size (gdbarch, 4);
  1145.   set_gdbarch_float_bit (gdbarch, 32);
  1146.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1147.   set_gdbarch_double_bit (gdbarch, 32);
  1148.   set_gdbarch_long_double_bit (gdbarch, 64);
  1149.   set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1150.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  1151.   set_gdbarch_pointer_to_address (gdbarch, rl78_pointer_to_address);
  1152.   set_gdbarch_address_to_pointer (gdbarch, rl78_address_to_pointer);
  1153.   set_gdbarch_addr_bits_remove (gdbarch, rl78_addr_bits_remove);

  1154.   /* Breakpoints.  */
  1155.   set_gdbarch_breakpoint_from_pc (gdbarch, rl78_breakpoint_from_pc);
  1156.   set_gdbarch_decr_pc_after_break (gdbarch, 1);

  1157.   /* Disassembly.  */
  1158.   set_gdbarch_print_insn (gdbarch, print_insn_rl78);

  1159.   /* Frames, prologues, etc.  */
  1160.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1161.   set_gdbarch_skip_prologue (gdbarch, rl78_skip_prologue);
  1162.   set_gdbarch_unwind_pc (gdbarch, rl78_unwind_pc);
  1163.   set_gdbarch_unwind_sp (gdbarch, rl78_unwind_sp);
  1164.   set_gdbarch_frame_align (gdbarch, rl78_frame_align);

  1165.   dwarf2_append_unwinders (gdbarch);
  1166.   frame_unwind_append_unwinder (gdbarch, &rl78_unwind);

  1167.   /* Dummy frames, return values.  */
  1168.   set_gdbarch_dummy_id (gdbarch, rl78_dummy_id);
  1169.   set_gdbarch_push_dummy_call (gdbarch, rl78_push_dummy_call);
  1170.   set_gdbarch_return_value (gdbarch, rl78_return_value);

  1171.   /* Virtual tables.  */
  1172.   set_gdbarch_vbit_in_delta (gdbarch, 1);

  1173.   return gdbarch;
  1174. }

  1175. /* -Wmissing-prototypes */
  1176. extern initialize_file_ftype _initialize_rl78_tdep;

  1177. /* Register the above initialization routine.  */

  1178. void
  1179. _initialize_rl78_tdep (void)
  1180. {
  1181.   register_gdbarch_init (bfd_arch_rl78, rl78_gdbarch_init);
  1182. }