home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / h8300-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  11.8 KB  |  479 lines

  1. /* Target-machine dependent code for Hitachi H8/300, for GDB.
  2.    Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  Contributed by Steve Chamberlain
  22.                 sac@cygnus.com
  23.  */
  24.  
  25. #include "defs.h"
  26. #include "frame.h"
  27. #include "obstack.h"
  28. #include "symtab.h"
  29. #include "dis-asm.h"
  30. #include "gdbcmd.h"
  31. #include "gdbtypes.h"
  32.  
  33. #undef NUM_REGS
  34. #define NUM_REGS 11
  35.  
  36. #define UNSIGNED_SHORT(X) ((X) & 0xffff)
  37.  
  38. /* an easy to debug H8 stack frame looks like:
  39. 0x6df6        push    r6
  40. 0x0d76      mov.w   r7,r6
  41. 0x6dfn          push    reg
  42. 0x7905 nnnn      mov.w  #n,r5    or   0x1b87  subs #2,sp
  43. 0x1957           sub.w  r5,sp
  44.  
  45.  */
  46.  
  47. #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
  48. #define IS_PUSH_FP(x) (x == 0x6df6)
  49. #define IS_MOVE_FP(x) (x == 0x0d76)
  50. #define IS_MOV_SP_FP(x) (x == 0x0d76)
  51. #define IS_SUB2_SP(x) (x==0x1b87)
  52. #define IS_MOVK_R5(x) (x==0x7905)
  53. #define IS_SUB_R5SP(x) (x==0x1957)
  54.  
  55. static CORE_ADDR examine_prologue ();
  56.  
  57. void frame_find_saved_regs ();
  58. CORE_ADDR 
  59. h8300_skip_prologue (start_pc)
  60.      CORE_ADDR start_pc;
  61. {
  62.   short int w;
  63.  
  64.   w = read_memory_unsigned_integer (start_pc, 2);
  65.   /* Skip past all push insns */
  66.   while (IS_PUSH_FP (w))
  67.     {
  68.       start_pc += 2;
  69.       w = read_memory_unsigned_integer (start_pc, 2);
  70.     }
  71.  
  72.   /* Skip past a move to FP */
  73.   if (IS_MOVE_FP (w))
  74.     {
  75.       start_pc += 2;
  76.       w = read_memory_unsigned_integer (start_pc, 2);
  77.     }
  78.  
  79.   /* Skip the stack adjust */
  80.  
  81.   if (IS_MOVK_R5 (w))
  82.     {
  83.       start_pc += 2;
  84.       w = read_memory_unsigned_integer (start_pc, 2);
  85.     }
  86.   if (IS_SUB_R5SP (w))
  87.     {
  88.       start_pc += 2;
  89.       w = read_memory_unsigned_integer (start_pc, 2);
  90.     }
  91.   while (IS_SUB2_SP (w))
  92.     {
  93.       start_pc += 2;
  94.       w = read_memory_unsigned_integer (start_pc, 2);
  95.     }
  96.  
  97.   return start_pc;
  98. }
  99.  
  100. int
  101. gdb_print_insn_h8300 (memaddr, info)
  102.      bfd_vma memaddr;
  103.      disassemble_info *info;
  104. {
  105.   if (h8300hmode)
  106.     return print_insn_h8300h (memaddr, info);
  107.   else
  108.     return print_insn_h8300 (memaddr, info);
  109. }
  110.  
  111. /* Given a GDB frame, determine the address of the calling function's frame.
  112.    This will be used to create a new GDB frame struct, and then
  113.    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
  114.  
  115.    For us, the frame address is its stack pointer value, so we look up
  116.    the function prologue to determine the caller's sp value, and return it.  */
  117.  
  118. CORE_ADDR
  119. h8300_frame_chain (thisframe)
  120.      struct frame_info *thisframe;
  121. {
  122.   frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
  123.   return thisframe->fsr->regs[SP_REGNUM];
  124. }
  125.  
  126. /* Put here the code to store, into a struct frame_saved_regs,
  127.    the addresses of the saved registers of frame described by FRAME_INFO.
  128.    This includes special registers such as pc and fp saved in special
  129.    ways in the stack frame.  sp is even more special:
  130.    the address we return for it IS the sp for the next frame.
  131.  
  132.    We cache the result of doing this in the frame_cache_obstack, since
  133.    it is fairly expensive.  */
  134.  
  135. void
  136. frame_find_saved_regs (fi, fsr)
  137.      struct frame_info *fi;
  138.      struct frame_saved_regs *fsr;
  139. {
  140.   register CORE_ADDR next_addr;
  141.   register CORE_ADDR *saved_regs;
  142.   register int regnum;
  143.   register struct frame_saved_regs *cache_fsr;
  144.   extern struct obstack frame_cache_obstack;
  145.   CORE_ADDR ip;
  146.   struct symtab_and_line sal;
  147.   CORE_ADDR limit;
  148.  
  149.   if (!fi->fsr)
  150.     {
  151.       cache_fsr = (struct frame_saved_regs *)
  152.     obstack_alloc (&frame_cache_obstack,
  153.                sizeof (struct frame_saved_regs));
  154.       memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
  155.  
  156.       fi->fsr = cache_fsr;
  157.  
  158.       /* Find the start and end of the function prologue.  If the PC
  159.      is in the function prologue, we only consider the part that
  160.      has executed already.  */
  161.  
  162.       ip = get_pc_function_start (fi->pc);
  163.       sal = find_pc_line (ip, 0);
  164.       limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
  165.  
  166.       /* This will fill in fields in *fi as well as in cache_fsr.  */
  167.       examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
  168.     }
  169.  
  170.   if (fsr)
  171.     *fsr = *fi->fsr;
  172. }
  173.  
  174. /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
  175.    is not the address of a valid instruction, the address of the next
  176.    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
  177.    of the instruction.*/
  178.  
  179. CORE_ADDR
  180. NEXT_PROLOGUE_INSN (addr, lim, pword1)
  181.      CORE_ADDR addr;
  182.      CORE_ADDR lim;
  183.      INSN_WORD *pword1;
  184. {
  185.   char buf[2];
  186.   if (addr < lim + 8)
  187.     {
  188.       read_memory (addr, buf, 2);
  189.       *pword1 = extract_signed_integer (buf, 2);
  190.  
  191.       return addr + 2;
  192.     }
  193.   return 0;
  194. }
  195.  
  196. /* Examine the prologue of a function.  `ip' points to the first instruction.
  197.    `limit' is the limit of the prologue (e.g. the addr of the first
  198.    linenumber, or perhaps the program counter if we're stepping through).
  199.    `frame_sp' is the stack pointer value in use in this frame.
  200.    `fsr' is a pointer to a frame_saved_regs structure into which we put
  201.    info about the registers saved by this frame.
  202.    `fi' is a struct frame_info pointer; we fill in various fields in it
  203.    to reflect the offsets of the arg pointer and the locals pointer.  */
  204.  
  205. static CORE_ADDR
  206. examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
  207.      register CORE_ADDR ip;
  208.      register CORE_ADDR limit;
  209.      CORE_ADDR after_prolog_fp;
  210.      struct frame_saved_regs *fsr;
  211.      struct frame_info *fi;
  212. {
  213.   register CORE_ADDR next_ip;
  214.   int r;
  215.   int i;
  216.   int have_fp = 0;
  217.   register int src;
  218.   register struct pic_prologue_code *pcode;
  219.   INSN_WORD insn_word;
  220.   int size, offset;
  221.   /* Number of things pushed onto stack, starts at 2/4, 'cause the
  222.      PC is already there */
  223.   unsigned int reg_save_depth = h8300hmode ? 4 : 2;
  224.  
  225.   unsigned int auto_depth = 0;    /* Number of bytes of autos */
  226.  
  227.   char in_frame[11];        /* One for each reg */
  228.  
  229.   memset (in_frame, 1, 11);
  230.   for (r = 0; r < 8; r++)
  231.     {
  232.       fsr->regs[r] = 0;
  233.     }
  234.   if (after_prolog_fp == 0)
  235.     {
  236.       after_prolog_fp = read_register (SP_REGNUM);
  237.     }
  238.   if (ip == 0 || ip & (h8300hmode ? ~0xffff : ~0xffff))
  239.     return 0;
  240.  
  241.   next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  242.  
  243.   /* Skip over any fp push instructions */
  244.   fsr->regs[6] = after_prolog_fp;
  245.   while (next_ip && IS_PUSH_FP (insn_word))
  246.     {
  247.       ip = next_ip;
  248.  
  249.       in_frame[insn_word & 0x7] = reg_save_depth;
  250.       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  251.       reg_save_depth += 2;
  252.     }
  253.  
  254.   /* Is this a move into the fp */
  255.   if (next_ip && IS_MOV_SP_FP (insn_word))
  256.     {
  257.       ip = next_ip;
  258.       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  259.       have_fp = 1;
  260.     }
  261.  
  262.   /* Skip over any stack adjustment, happens either with a number of
  263.      sub#2,sp or a mov #x,r5 sub r5,sp */
  264.  
  265.   if (next_ip && IS_SUB2_SP (insn_word))
  266.     {
  267.       while (next_ip && IS_SUB2_SP (insn_word))
  268.     {
  269.       auto_depth += 2;
  270.       ip = next_ip;
  271.       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  272.     }
  273.     }
  274.   else
  275.     {
  276.       if (next_ip && IS_MOVK_R5 (insn_word))
  277.     {
  278.       ip = next_ip;
  279.       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  280.       auto_depth += insn_word;
  281.  
  282.       next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
  283.       auto_depth += insn_word;
  284.     }
  285.     }
  286.   /* Work out which regs are stored where */
  287.   while (next_ip && IS_PUSH (insn_word))
  288.     {
  289.       ip = next_ip;
  290.       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  291.       fsr->regs[r] = after_prolog_fp + auto_depth;
  292.       auto_depth += 2;
  293.     }
  294.  
  295.   /* The args are always reffed based from the stack pointer */
  296.   fi->args_pointer = after_prolog_fp;
  297.   /* Locals are always reffed based from the fp */
  298.   fi->locals_pointer = after_prolog_fp;
  299.   /* The PC is at a known place */
  300.   fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
  301.  
  302.   /* Rememeber any others too */
  303.   in_frame[PC_REGNUM] = 0;
  304.  
  305.   if (have_fp)
  306.     /* We keep the old FP in the SP spot */
  307.     fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
  308.   else
  309.     fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
  310.  
  311.   return (ip);
  312. }
  313.  
  314. void
  315. init_extra_frame_info (fromleaf, fi)
  316.      int fromleaf;
  317.      struct frame_info *fi;
  318. {
  319.   fi->fsr = 0;            /* Not yet allocated */
  320.   fi->args_pointer = 0;        /* Unknown */
  321.   fi->locals_pointer = 0;    /* Unknown */
  322.   fi->from_pc = 0;
  323. }
  324.  
  325. /* Return the saved PC from this frame.
  326.  
  327.    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
  328.    just use the register SRP_REGNUM itself.  */
  329.  
  330. CORE_ADDR
  331. frame_saved_pc (frame)
  332.      struct frame_info *frame;
  333. {
  334.   return frame->from_pc;
  335. }
  336.  
  337. CORE_ADDR
  338. frame_locals_address (fi)
  339.      struct frame_info *fi;
  340. {
  341.   if (!fi->locals_pointer)
  342.     {
  343.       struct frame_saved_regs ignore;
  344.  
  345.       get_frame_saved_regs (fi, &ignore);
  346.  
  347.     }
  348.   return fi->locals_pointer;
  349. }
  350.  
  351. /* Return the address of the argument block for the frame
  352.    described by FI.  Returns 0 if the address is unknown.  */
  353.  
  354. CORE_ADDR
  355. frame_args_address (fi)
  356.      struct frame_info *fi;
  357. {
  358.   if (!fi->args_pointer)
  359.     {
  360.       struct frame_saved_regs ignore;
  361.  
  362.       get_frame_saved_regs (fi, &ignore);
  363.  
  364.     }
  365.  
  366.   return fi->args_pointer;
  367. }
  368.  
  369. void 
  370. h8300_pop_frame ()
  371. {
  372.   unsigned regnum;
  373.   struct frame_saved_regs fsr;
  374.   struct frame_info *frame = get_current_frame ();
  375.  
  376.   get_frame_saved_regs (frame, &fsr);
  377.  
  378.   for (regnum = 0; regnum < 8; regnum++)
  379.     {
  380.       if (fsr.regs[regnum])
  381.     write_register (regnum, read_memory_integer(fsr.regs[regnum]), BINWORD);
  382.  
  383.       flush_cached_frames ();
  384.     }
  385. }
  386.  
  387.  
  388. struct cmd_list_element *setmemorylist;
  389.  
  390. static void
  391. h8300_command(args, from_tty)
  392. {
  393.   extern int h8300hmode;
  394.   h8300hmode = 0;
  395. }
  396.  
  397. static void
  398. h8300h_command(args, from_tty)
  399. {
  400.   extern int h8300hmode;
  401.   h8300hmode = 1;
  402. }
  403.  
  404. static void 
  405. set_machine (args, from_tty)
  406.      char *args;
  407.      int from_tty;
  408. {
  409.   printf_unfiltered ("\"set machine\" must be followed by h8300 or h8300h.\n");
  410.   help_list (setmemorylist, "set memory ", -1, gdb_stdout);
  411. }
  412.  
  413. void
  414. _initialize_h8300m ()
  415. {
  416.   add_prefix_cmd ("machine", no_class, set_machine,
  417.           "set the machine type", &setmemorylist, "set machine ", 0,
  418.           &setlist);
  419.  
  420.   add_cmd ("h8300", class_support, h8300_command,
  421.        "Set machine to be H8/300.", &setmemorylist);
  422.  
  423.   add_cmd ("h8300h", class_support, h8300h_command,
  424.        "Set machine to be H8/300H.", &setmemorylist);
  425. }
  426.  
  427.  
  428.  
  429. void
  430. print_register_hook (regno)
  431. {
  432.   if (regno == 8)
  433.     {
  434.       /* CCR register */
  435.       int C, Z, N, V;
  436.       unsigned char b[4];
  437.       unsigned char l;
  438.       read_relative_register_raw_bytes (regno, b);
  439.       l = b[REGISTER_VIRTUAL_SIZE(8) -1];
  440.       printf_unfiltered ("\t");
  441.       printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
  442.       printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
  443.       N = (l & 0x8) != 0;
  444.       Z = (l & 0x4) != 0;
  445.       V = (l & 0x2) != 0;
  446.       C = (l & 0x1) != 0;
  447.       printf_unfiltered ("N-%d ", N);
  448.       printf_unfiltered ("Z-%d ", Z);
  449.       printf_unfiltered ("V-%d ", V);
  450.       printf_unfiltered ("C-%d ", C);
  451.       if ((C | Z) == 0)
  452.     printf_unfiltered ("u> ");
  453.       if ((C | Z) == 1)
  454.     printf_unfiltered ("u<= ");
  455.       if ((C == 0))
  456.     printf_unfiltered ("u>= ");
  457.       if (C == 1)
  458.     printf_unfiltered ("u< ");
  459.       if (Z == 0)
  460.     printf_unfiltered ("!= ");
  461.       if (Z == 1)
  462.     printf_unfiltered ("== ");
  463.       if ((N ^ V) == 0)
  464.     printf_unfiltered (">= ");
  465.       if ((N ^ V) == 1)
  466.     printf_unfiltered ("< ");
  467.       if ((Z | (N ^ V)) == 0)
  468.     printf_unfiltered ("> ");
  469.       if ((Z | (N ^ V)) == 1)
  470.     printf_unfiltered ("<= ");
  471.     }
  472. }
  473.  
  474. void
  475. _initialize_h8300_tdep ()
  476. {
  477.   tm_print_insn = gdb_print_insn_h8300;
  478. }
  479.