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

  1. /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
  2.    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
  3.    Free Software Foundation, Inc.
  4.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  5.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
  6.  
  7. This file is part of GDB.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include "defs.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "symtab.h"
  27. #include "value.h"
  28. #include "gdbcmd.h"
  29. #include "language.h"
  30. #include "gdbcore.h"
  31. #include "symfile.h"
  32. #include "objfiles.h"
  33. #include "gdbtypes.h"
  34.  
  35. #include "opcode/mips.h"
  36.  
  37. #define VM_MIN_ADDRESS (unsigned)0x400000
  38.  
  39. /* FIXME: Put this declaration in frame.h.  */
  40. extern struct obstack frame_cache_obstack;
  41.  
  42. #if 0
  43. static int mips_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
  44. #endif
  45.  
  46. static void mips_set_fpu_command PARAMS ((char *, int,
  47.                       struct cmd_list_element *));
  48.  
  49. static void mips_show_fpu_command PARAMS ((char *, int,
  50.                        struct cmd_list_element *));
  51.  
  52. void mips_set_processor_type_command PARAMS ((char *, int));
  53.  
  54. int mips_set_processor_type PARAMS ((char *));
  55.  
  56. static void mips_show_processor_type_command PARAMS ((char *, int));
  57.  
  58. static void reinit_frame_cache_sfunc PARAMS ((char *, int,
  59.                           struct cmd_list_element *));
  60.  
  61. /* This value is the model of MIPS in use.  It is derived from the value
  62.    of the PrID register.  */
  63.  
  64. char *mips_processor_type;
  65.  
  66. char *tmp_mips_processor_type;
  67.  
  68. /* Some MIPS boards don't support floating point, so we permit the
  69.    user to turn it off.  */
  70.  
  71. enum mips_fpu_type mips_fpu;
  72.  
  73. static char *mips_fpu_string;
  74.  
  75. /* A set of original names, to be used when restoring back to generic
  76.    registers from a specific set.  */
  77.  
  78. char *mips_generic_reg_names[] = REGISTER_NAMES;
  79.  
  80. /* Names of IDT R3041 registers.  */
  81.  
  82. char *mips_r3041_reg_names[] = {
  83.     "zero",    "at",    "v0",    "v1",    "a0",    "a1",    "a2",    "a3",
  84.     "t0",    "t1",    "t2",    "t3",    "t4",    "t5",    "t6",    "t7",
  85.     "s0",    "s1",    "s2",    "s3",    "s4",    "s5",    "s6",    "s7",
  86.     "t8",    "t9",    "k0",    "k1",    "gp",    "sp",    "s8",    "ra",
  87.     "sr",    "lo",    "hi",    "bad",    "cause","pc",
  88.     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
  89.     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
  90.     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
  91.     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "f31",
  92.     "fsr",  "fir",  "fp",    "",
  93.     "",    "",    "bus",    "ccfg",    "",    "",    "",    "",
  94.     "",    "",    "port",    "cmp",    "",    "",    "epc",    "prid",
  95. };
  96.  
  97. /* Names of IDT R3051 registers.  */
  98.  
  99. char *mips_r3051_reg_names[] = {
  100.     "zero",    "at",    "v0",    "v1",    "a0",    "a1",    "a2",    "a3",
  101.     "t0",    "t1",    "t2",    "t3",    "t4",    "t5",    "t6",    "t7",
  102.     "s0",    "s1",    "s2",    "s3",    "s4",    "s5",    "s6",    "s7",
  103.     "t8",    "t9",    "k0",    "k1",    "gp",    "sp",    "s8",    "ra",
  104.     "sr",    "lo",    "hi",    "bad",    "cause","pc",
  105.     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
  106.     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
  107.     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
  108.     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "f31",
  109.     "fsr",  "fir",  "fp",    "",
  110.     "inx",    "rand",    "elo",    "",    "ctxt",    "",    "",    "",
  111.     "",    "",    "ehi",    "",    "",    "",    "epc",    "prid",
  112. };
  113.  
  114. /* Names of IDT R3081 registers.  */
  115.  
  116. char *mips_r3081_reg_names[] = {
  117.     "zero",    "at",    "v0",    "v1",    "a0",    "a1",    "a2",    "a3",
  118.     "t0",    "t1",    "t2",    "t3",    "t4",    "t5",    "t6",    "t7",
  119.     "s0",    "s1",    "s2",    "s3",    "s4",    "s5",    "s6",    "s7",
  120.     "t8",    "t9",    "k0",    "k1",    "gp",    "sp",    "s8",    "ra",
  121.     "sr",    "lo",    "hi",    "bad",    "cause","pc",
  122.     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
  123.     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
  124.     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
  125.     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "f31",
  126.     "fsr",  "fir",  "fp",    "",
  127.     "inx",    "rand",    "elo",    "cfg",    "ctxt",    "",    "",    "",
  128.     "",    "",    "ehi",    "",    "",    "",    "epc",    "prid",
  129. };
  130.  
  131. struct {
  132.   char *name;
  133.   char **regnames;
  134. } mips_processor_type_table[] = {
  135.   { "generic", mips_generic_reg_names },
  136.   { "r3041", mips_r3041_reg_names },
  137.   { "r3051", mips_r3051_reg_names },
  138.   { "r3071", mips_r3081_reg_names },
  139.   { "r3081", mips_r3081_reg_names },
  140.   { NULL, NULL }
  141. };
  142.  
  143. /* Heuristic_proc_start may hunt through the text section for a long
  144.    time across a 2400 baud serial line.  Allows the user to limit this
  145.    search.  */
  146.  
  147. static unsigned int heuristic_fence_post = 0;
  148.  
  149. #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
  150. #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
  151. #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
  152. #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
  153. #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
  154. #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
  155. #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
  156. #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
  157. #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
  158. #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
  159. #define _PROC_MAGIC_ 0x0F0F0F0F
  160. #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
  161. #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
  162.  
  163. struct linked_proc_info
  164. {
  165.   struct mips_extra_func_info info;
  166.   struct linked_proc_info *next;
  167. } *linked_proc_desc_table = NULL;
  168.  
  169.  
  170. /* Guaranteed to set fci->saved_regs to some values (it never leaves it
  171.    NULL).  */
  172.  
  173. void
  174. mips_find_saved_regs (fci)
  175.      struct frame_info *fci;
  176. {
  177.   int ireg;
  178.   CORE_ADDR reg_position;
  179.   /* r0 bit means kernel trap */
  180.   int kernel_trap;
  181.   /* What registers have been saved?  Bitmasks.  */
  182.   unsigned long gen_mask, float_mask;
  183.   mips_extra_func_info_t proc_desc;
  184.  
  185.   fci->saved_regs = (struct frame_saved_regs *)
  186.     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
  187.   memset (fci->saved_regs, 0, sizeof (struct frame_saved_regs));
  188.  
  189.   proc_desc = fci->proc_desc;
  190.   if (proc_desc == NULL)
  191.     /* I'm not sure how/whether this can happen.  Normally when we can't
  192.        find a proc_desc, we "synthesize" one using heuristic_proc_desc
  193.        and set the saved_regs right away.  */
  194.     return;
  195.  
  196.   kernel_trap = PROC_REG_MASK(proc_desc) & 1;
  197.   gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
  198.   float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
  199.  
  200.   if (/* In any frame other than the innermost, we assume that all
  201.      registers have been saved.  This assumes that all register
  202.      saves in a function happen before the first function
  203.      call.  */
  204.       fci->next == NULL
  205.  
  206.       /* In a dummy frame we know exactly where things are saved.  */
  207.       && !PROC_DESC_IS_DUMMY (proc_desc)
  208.  
  209.       /* Not sure exactly what kernel_trap means, but if it means
  210.      the kernel saves the registers without a prologue doing it,
  211.      we better not examine the prologue to see whether registers
  212.      have been saved yet.  */
  213.       && !kernel_trap)
  214.     {
  215.       /* We need to figure out whether the registers that the proc_desc
  216.      claims are saved have been saved yet.  */
  217.  
  218.       CORE_ADDR addr;
  219.       int status;
  220.       char buf[4];
  221.       unsigned long inst;
  222.  
  223.       /* Bitmasks; set if we have found a save for the register.  */
  224.       unsigned long gen_save_found = 0;
  225.       unsigned long float_save_found = 0;
  226.  
  227.       for (addr = PROC_LOW_ADDR (proc_desc);
  228.        addr < fci->pc /*&& (gen_mask != gen_save_found
  229.                   || float_mask != float_save_found)*/;
  230.        addr += 4)
  231.     {
  232.       status = read_memory_nobpt (addr, buf, 4);
  233.       if (status)
  234.         memory_error (status, addr);
  235.       inst = extract_unsigned_integer (buf, 4);
  236.       if (/* sw reg,n($sp) */
  237.           (inst & 0xffe00000) == 0xafa00000
  238.  
  239.           /* sw reg,n($r30) */
  240.           || (inst & 0xffe00000) == 0xafc00000
  241.  
  242.           /* sd reg,n($sp) */
  243.           || (inst & 0xffe00000) == 0xffa00000)
  244.         {
  245.           /* It might be possible to use the instruction to
  246.          find the offset, rather than the code below which
  247.          is based on things being in a certain order in the
  248.          frame, but figuring out what the instruction's offset
  249.          is relative to might be a little tricky.  */
  250.           int reg = (inst & 0x001f0000) >> 16;
  251.           gen_save_found |= (1 << reg);
  252.         }
  253.       else if (/* swc1 freg,n($sp) */
  254.            (inst & 0xffe00000) == 0xe7a00000
  255.  
  256.            /* swc1 freg,n($r30) */
  257.            || (inst & 0xffe00000) == 0xe7c00000
  258.  
  259.                    /* sdc1 freg,n($sp) */
  260.                    || (inst & 0xffe00000) == 0xf7a00000)
  261.  
  262.         {
  263.           int reg = ((inst & 0x001f0000) >> 16);
  264.           float_save_found |= (1 << reg);
  265.         }
  266.     }
  267.       gen_mask = gen_save_found;
  268.       float_mask = float_save_found;
  269.     }
  270.  
  271.   /* Fill in the offsets for the registers which gen_mask says
  272.      were saved.  */
  273.   reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
  274.   for (ireg= 31; gen_mask; --ireg, gen_mask <<= 1)
  275.     if (gen_mask & 0x80000000)
  276.       {
  277.     fci->saved_regs->regs[ireg] = reg_position;
  278.     reg_position -= MIPS_REGSIZE;
  279.       }
  280.   /* Fill in the offsets for the registers which float_mask says
  281.      were saved.  */
  282.   reg_position = fci->frame + PROC_FREG_OFFSET (proc_desc);
  283.  
  284.   /* The freg_offset points to where the first *double* register
  285.      is saved.  So skip to the high-order word. */
  286.   reg_position += 4;
  287.   for (ireg = 31; float_mask; --ireg, float_mask <<= 1)
  288.     if (float_mask & 0x80000000)
  289.       {
  290.     fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
  291.     reg_position -= MIPS_REGSIZE;
  292.       }
  293.  
  294.   fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  295. }
  296.  
  297. static int
  298. read_next_frame_reg(fi, regno)
  299.      struct frame_info *fi;
  300.      int regno;
  301. {
  302.   /* If it is the frame for sigtramp we have a complete sigcontext
  303.      somewhere above the frame and we get the saved registers from there.
  304.      If the stack layout for sigtramp changes we might have to change these
  305.      constants and the companion fixup_sigtramp in mdebugread.c  */
  306. #ifndef SIGFRAME_BASE
  307. /* To satisfy alignment restrictions the sigcontext is located 4 bytes
  308.    above the sigtramp frame.  */
  309. #define SIGFRAME_BASE        4
  310. #define SIGFRAME_PC_OFF        (SIGFRAME_BASE + 2 * 4)
  311. #define SIGFRAME_REGSAVE_OFF    (SIGFRAME_BASE + 3 * 4)
  312. #endif
  313. #ifndef SIGFRAME_REG_SIZE
  314. #define SIGFRAME_REG_SIZE    4
  315. #endif
  316.   for (; fi; fi = fi->next)
  317.     {
  318.       if (fi->signal_handler_caller)
  319.     {
  320.       int offset;
  321.       if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
  322.       else if (regno < 32) offset = (SIGFRAME_REGSAVE_OFF
  323.                      + regno * SIGFRAME_REG_SIZE);
  324.       else return 0;
  325.       return read_memory_integer(fi->frame + offset, MIPS_REGSIZE);
  326.     }
  327.       else if (regno == SP_REGNUM) return fi->frame;
  328.       else
  329.     {
  330.       if (fi->saved_regs == NULL)
  331.         mips_find_saved_regs (fi);
  332.       if (fi->saved_regs->regs[regno])
  333.         return read_memory_integer(fi->saved_regs->regs[regno], MIPS_REGSIZE);
  334.     }
  335.     }
  336.   return read_register (regno);
  337. }
  338.  
  339. int
  340. mips_frame_saved_pc(frame)
  341.      struct frame_info *frame;
  342. {
  343.   mips_extra_func_info_t proc_desc = frame->proc_desc;
  344.   /* We have to get the saved pc from the sigcontext
  345.      if it is a signal handler frame.  */
  346.   int pcreg = frame->signal_handler_caller ? PC_REGNUM
  347.           : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
  348.  
  349.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  350.       return read_memory_integer(frame->frame - 4, 4);
  351.  
  352.   return read_next_frame_reg(frame, pcreg);
  353. }
  354.  
  355. static struct mips_extra_func_info temp_proc_desc;
  356. static struct frame_saved_regs temp_saved_regs;
  357.  
  358. /* This fencepost looks highly suspicious to me.  Removing it also
  359.    seems suspicious as it could affect remote debugging across serial
  360.    lines.  */
  361.  
  362. static CORE_ADDR
  363. heuristic_proc_start(pc)
  364.     CORE_ADDR pc;
  365. {
  366.     CORE_ADDR start_pc = pc;
  367.     CORE_ADDR fence = start_pc - heuristic_fence_post;
  368.  
  369.     if (start_pc == 0)    return 0;
  370.  
  371.     if (heuristic_fence_post == UINT_MAX
  372.     || fence < VM_MIN_ADDRESS)
  373.       fence = VM_MIN_ADDRESS;
  374.  
  375.     /* search back for previous return */
  376.     for (start_pc -= 4; ; start_pc -= 4)
  377.     if (start_pc < fence)
  378.       {
  379.         /* It's not clear to me why we reach this point when
  380.            stop_soon_quietly, but with this test, at least we
  381.            don't print out warnings for every child forked (eg, on
  382.            decstation).  22apr93 rich@cygnus.com.  */
  383.         if (!stop_soon_quietly)
  384.           {
  385.         static int blurb_printed = 0;
  386.  
  387.         if (fence == VM_MIN_ADDRESS)
  388.           warning("Hit beginning of text section without finding");
  389.         else
  390.           warning("Hit heuristic-fence-post without finding");
  391.         
  392.         warning("enclosing function for address 0x%x", pc);
  393.         if (!blurb_printed)
  394.           {
  395.             printf_filtered ("\
  396. This warning occurs if you are debugging a function without any symbols\n\
  397. (for example, in a stripped executable).  In that case, you may wish to\n\
  398. increase the size of the search with the `set heuristic-fence-post' command.\n\
  399. \n\
  400. Otherwise, you told GDB there was a function where there isn't one, or\n\
  401. (more likely) you have encountered a bug in GDB.\n");
  402.             blurb_printed = 1;
  403.           }
  404.           }
  405.  
  406.         return 0; 
  407.       }
  408.     else if (ABOUT_TO_RETURN(start_pc))
  409.         break;
  410.  
  411.     start_pc += 8; /* skip return, and its delay slot */
  412. #if 0
  413.     /* skip nops (usually 1) 0 - is this */
  414.     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
  415.     start_pc += 4;
  416. #endif
  417.     return start_pc;
  418. }
  419.  
  420. static mips_extra_func_info_t
  421. heuristic_proc_desc(start_pc, limit_pc, next_frame)
  422.     CORE_ADDR start_pc, limit_pc;
  423.     struct frame_info *next_frame;
  424. {
  425.     CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
  426.     CORE_ADDR cur_pc;
  427.     int frame_size;
  428.     int has_frame_reg = 0;
  429.     int reg30 = 0; /* Value of $r30. Used by gcc for frame-pointer */
  430.     unsigned long reg_mask = 0;
  431.  
  432.     if (start_pc == 0) return NULL;
  433.     memset (&temp_proc_desc, '\0', sizeof(temp_proc_desc));
  434.     memset (&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
  435.     PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
  436.  
  437.     if (start_pc + 200 < limit_pc)
  438.       limit_pc = start_pc + 200;
  439.   restart:
  440.     frame_size = 0;
  441.     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
  442.         char buf[4];
  443.     unsigned long word;
  444.     int status;
  445.  
  446.     status = read_memory_nobpt (cur_pc, buf, 4); 
  447.     if (status) memory_error (status, cur_pc);
  448.     word = extract_unsigned_integer (buf, 4);
  449.  
  450.     if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
  451.         frame_size += (-word) & 0xFFFF;
  452.     else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
  453.         frame_size += (-word) & 0xFFFF;
  454.     else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
  455.         int reg = (word & 0x001F0000) >> 16;
  456.         reg_mask |= 1 << reg;
  457.         temp_saved_regs.regs[reg] = sp + (word & 0xffff);
  458.     }
  459.     else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
  460.         if ((word & 0xffff) != frame_size)
  461.         reg30 = sp + (word & 0xffff);
  462.         else if (!has_frame_reg) {
  463.         int alloca_adjust;
  464.         has_frame_reg = 1;
  465.         reg30 = read_next_frame_reg(next_frame, 30);
  466.         alloca_adjust = reg30 - (sp + (word & 0xffff));
  467.         if (alloca_adjust > 0) {
  468.             /* FP > SP + frame_size. This may be because
  469.              * of an alloca or somethings similar.
  470.              * Fix sp to "pre-alloca" value, and try again.
  471.              */
  472.             sp += alloca_adjust;
  473.             goto restart;
  474.         }
  475.         }
  476.     }
  477.     else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
  478.         int reg = (word & 0x001F0000) >> 16;
  479.         reg_mask |= 1 << reg;
  480.         temp_saved_regs.regs[reg] = reg30 + (word & 0xffff);
  481.     }
  482.     }
  483.     if (has_frame_reg) {
  484.     PROC_FRAME_REG(&temp_proc_desc) = 30;
  485.     PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
  486.     }
  487.     else {
  488.     PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
  489.     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
  490.     }
  491.     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
  492.     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
  493.     return &temp_proc_desc;
  494. }
  495.  
  496. static mips_extra_func_info_t
  497. find_proc_desc (pc, next_frame)
  498.      CORE_ADDR pc;
  499.      struct frame_info *next_frame;
  500. {
  501.   mips_extra_func_info_t proc_desc;
  502.   struct block *b = block_for_pc(pc);
  503.   struct symbol *sym;
  504.   CORE_ADDR startaddr;
  505.  
  506.   find_pc_partial_function (pc, NULL, &startaddr, NULL);
  507.   if (b == NULL)
  508.     sym = NULL;
  509.   else
  510.     {
  511.       if (startaddr > BLOCK_START (b))
  512.     /* This is the "pathological" case referred to in a comment in
  513.        print_frame_info.  It might be better to move this check into
  514.        symbol reading.  */
  515.     sym = NULL;
  516.       else
  517.     sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
  518.                  0, NULL);
  519.     }
  520.  
  521.   if (sym)
  522.     {
  523.     /* IF this is the topmost frame AND
  524.      * (this proc does not have debugging information OR
  525.      * the PC is in the procedure prologue)
  526.      * THEN create a "heuristic" proc_desc (by analyzing
  527.      * the actual code) to replace the "official" proc_desc.
  528.      */
  529.     proc_desc = (mips_extra_func_info_t) SYMBOL_VALUE (sym);
  530.     if (next_frame == NULL) {
  531.         struct symtab_and_line val;
  532.         struct symbol *proc_symbol =
  533.         PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
  534.  
  535.         if (proc_symbol) {
  536.         val = find_pc_line (BLOCK_START
  537.                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
  538.                     0);
  539.         val.pc = val.end ? val.end : pc;
  540.         }
  541.         if (!proc_symbol || pc < val.pc) {
  542.         mips_extra_func_info_t found_heuristic =
  543.           heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
  544.                        pc, next_frame);
  545.         if (found_heuristic)
  546.           proc_desc = found_heuristic;
  547.         }
  548.     }
  549.     }
  550.   else
  551.     {
  552.       /* Is linked_proc_desc_table really necessary?  It only seems to be used
  553.      by procedure call dummys.  However, the procedures being called ought
  554.      to have their own proc_descs, and even if they don't,
  555.      heuristic_proc_desc knows how to create them! */
  556.  
  557.       register struct linked_proc_info *link;
  558.  
  559.       for (link = linked_proc_desc_table; link; link = link->next)
  560.     if (PROC_LOW_ADDR(&link->info) <= pc
  561.         && PROC_HIGH_ADDR(&link->info) > pc)
  562.       return &link->info;
  563.  
  564.       if (startaddr == 0)
  565.     startaddr = heuristic_proc_start (pc);
  566.  
  567.       proc_desc =
  568.     heuristic_proc_desc (startaddr, pc, next_frame);
  569.     }
  570.   return proc_desc;
  571. }
  572.  
  573. mips_extra_func_info_t cached_proc_desc;
  574.  
  575. CORE_ADDR
  576. mips_frame_chain(frame)
  577.     struct frame_info *frame;
  578. {
  579.     mips_extra_func_info_t proc_desc;
  580.     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
  581.  
  582.     if (saved_pc == 0 || inside_entry_file (saved_pc))
  583.       return 0;
  584.  
  585.     proc_desc = find_proc_desc(saved_pc, frame);
  586.     if (!proc_desc)
  587.       return 0;
  588.  
  589.     cached_proc_desc = proc_desc;
  590.  
  591.     /* If no frame pointer and frame size is zero, we must be at end
  592.        of stack (or otherwise hosed).  If we don't check frame size,
  593.        we loop forever if we see a zero size frame.  */
  594.     if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
  595.     && PROC_FRAME_OFFSET (proc_desc) == 0
  596.     /* The previous frame from a sigtramp frame might be frameless
  597.        and have frame size zero.  */
  598.     && !frame->signal_handler_caller)
  599.       return 0;
  600.     else
  601.       return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
  602.     + PROC_FRAME_OFFSET(proc_desc);
  603. }
  604.  
  605. void
  606. init_extra_frame_info(fci)
  607.      struct frame_info *fci;
  608. {
  609.   /* Use proc_desc calculated in frame_chain */
  610.   mips_extra_func_info_t proc_desc =
  611.     fci->next ? cached_proc_desc : find_proc_desc(fci->pc, fci->next);
  612.  
  613.   fci->saved_regs = NULL;
  614.   fci->proc_desc =
  615.     proc_desc == &temp_proc_desc ? 0 : proc_desc;
  616.   if (proc_desc)
  617.     {
  618.       /* Fixup frame-pointer - only needed for top frame */
  619.       /* This may not be quite right, if proc has a real frame register.
  620.      Get the value of the frame relative sp, procedure might have been
  621.      interrupted by a signal at it's very start.  */
  622.       if (fci->pc == PROC_LOW_ADDR (proc_desc)
  623.       && !PROC_DESC_IS_DUMMY (proc_desc))
  624.     fci->frame = read_next_frame_reg (fci->next, SP_REGNUM);
  625.       else
  626.     fci->frame =
  627.       read_next_frame_reg (fci->next, PROC_FRAME_REG (proc_desc))
  628.         + PROC_FRAME_OFFSET (proc_desc);
  629.  
  630.       if (proc_desc == &temp_proc_desc)
  631.     {
  632.       fci->saved_regs = (struct frame_saved_regs*)
  633.         obstack_alloc (&frame_cache_obstack,
  634.                sizeof (struct frame_saved_regs));
  635.       *fci->saved_regs = temp_saved_regs;
  636.       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  637.     }
  638.  
  639.       /* hack: if argument regs are saved, guess these contain args */
  640.       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
  641.       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
  642.       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
  643.       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
  644.       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
  645.     }
  646. }
  647.  
  648. /* MIPS stack frames are almost impenetrable.  When execution stops,
  649.    we basically have to look at symbol information for the function
  650.    that we stopped in, which tells us *which* register (if any) is
  651.    the base of the frame pointer, and what offset from that register
  652.    the frame itself is at.  
  653.  
  654.    This presents a problem when trying to examine a stack in memory
  655.    (that isn't executing at the moment), using the "frame" command.  We
  656.    don't have a PC, nor do we have any registers except SP.
  657.  
  658.    This routine takes two arguments, SP and PC, and tries to make the
  659.    cached frames look as if these two arguments defined a frame on the
  660.    cache.  This allows the rest of info frame to extract the important
  661.    arguments without difficulty.  */
  662.  
  663. struct frame_info *
  664. setup_arbitrary_frame (argc, argv)
  665.      int argc;
  666.      CORE_ADDR *argv;
  667. {
  668.   if (argc != 2)
  669.     error ("MIPS frame specifications require two arguments: sp and pc");
  670.  
  671.   return create_new_frame (argv[0], argv[1]);
  672. }
  673.  
  674.  
  675. CORE_ADDR
  676. mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
  677.   int nargs;
  678.   value_ptr *args;
  679.   CORE_ADDR sp;
  680.   int struct_return;
  681.   CORE_ADDR struct_addr;
  682. {
  683.   register i;
  684.   int accumulate_size = struct_return ? MIPS_REGSIZE : 0;
  685.   struct mips_arg { char *contents; int len; int offset; };
  686.   struct mips_arg *mips_args =
  687.       (struct mips_arg*)alloca((nargs + 4) * sizeof(struct mips_arg));
  688.   register struct mips_arg *m_arg;
  689.   int fake_args = 0;
  690.  
  691.   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
  692.     value_ptr arg = value_arg_coerce (args[i]);
  693.     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
  694.     /* This entire mips-specific routine is because doubles must be aligned
  695.      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
  696.      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
  697.      * breaks their varargs implementation...). A correct solution
  698.      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
  699.      * in stdarg.h/varargs.h).
  700.      * On the 64 bit r4000 we always pass the first four arguments
  701.      * using eight bytes each, so that we can load them up correctly
  702.      * in CALL_DUMMY.
  703.      */
  704.     if (m_arg->len > 4)
  705.       accumulate_size = (accumulate_size + 7) & -8;
  706.     m_arg->offset = accumulate_size;
  707.     m_arg->contents = VALUE_CONTENTS(arg);
  708.     if (! GDB_TARGET_IS_MIPS64)
  709.       accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
  710.     else
  711.       {
  712.     if (accumulate_size >= 4 * MIPS_REGSIZE)
  713.       accumulate_size = (accumulate_size + m_arg->len + 3) &~ 4;
  714.     else
  715.       {
  716.         static char zeroes[8] = { 0 };
  717.         int len = m_arg->len;
  718.  
  719.         if (len < 8)
  720.           {
  721.         if (TARGET_BYTE_ORDER == BIG_ENDIAN)
  722.           m_arg->offset += 8 - len;
  723.         ++m_arg;
  724.         m_arg->len = 8 - len;
  725.         m_arg->contents = zeroes;
  726.         if (TARGET_BYTE_ORDER == BIG_ENDIAN)
  727.           m_arg->offset = accumulate_size;
  728.         else
  729.           m_arg->offset = accumulate_size + len;
  730.         ++fake_args;
  731.           }
  732.         accumulate_size = (accumulate_size + len + 7) & ~8;
  733.       }
  734.       }
  735.   }
  736.   accumulate_size = (accumulate_size + 7) & (-8);
  737.   if (accumulate_size < 4 * MIPS_REGSIZE)
  738.     accumulate_size = 4 * MIPS_REGSIZE;
  739.   sp -= accumulate_size;
  740.   for (i = nargs + fake_args; m_arg--, --i >= 0; )
  741.     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
  742.   if (struct_return)
  743.     {
  744.       char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
  745.  
  746.       store_address (buf, sizeof buf, struct_addr);
  747.       write_memory (sp, buf, sizeof buf);
  748.     }
  749.   return sp;
  750. }
  751.  
  752. /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
  753. #define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
  754.  
  755. void
  756. mips_push_dummy_frame()
  757. {
  758.   char buffer[MAX_REGISTER_RAW_SIZE];
  759.   int ireg;
  760.   struct linked_proc_info *link = (struct linked_proc_info*)
  761.       xmalloc(sizeof(struct linked_proc_info));
  762.   mips_extra_func_info_t proc_desc = &link->info;
  763.   CORE_ADDR sp = read_register (SP_REGNUM);
  764.   CORE_ADDR save_address;
  765.   link->next = linked_proc_desc_table;
  766.   linked_proc_desc_table = link;
  767. #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
  768. #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
  769. #define GEN_REG_SAVE_COUNT 22
  770. #define FLOAT_REG_SAVE_MASK MASK(0,19)
  771. #define FLOAT_REG_SAVE_COUNT 20
  772. #define FLOAT_SINGLE_REG_SAVE_MASK \
  773.   ((1<<18)|(1<<16)|(1<<14)|(1<<12)|(1<<10)|(1<<8)|(1<<6)|(1<<4)|(1<<2)|(1<<0))
  774. #define FLOAT_SINGLE_REG_SAVE_COUNT 10
  775. #define SPECIAL_REG_SAVE_COUNT 4
  776.   /*
  777.    * The registers we must save are all those not preserved across
  778.    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
  779.    * In addition, we must save the PC, and PUSH_FP_REGNUM.
  780.    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
  781.    *
  782.    * Dummy frame layout:
  783.    *  (high memory)
  784.    *     Saved PC
  785.    *    Saved MMHI, MMLO, FPC_CSR
  786.    *    Saved R31
  787.    *    Saved R28
  788.    *    ...
  789.    *    Saved R1
  790.    *    Saved D18 (i.e. F19, F18)
  791.    *    ...
  792.    *    Saved D0 (i.e. F1, F0)
  793.    *    CALL_DUMMY (subroutine stub; see tm-mips.h)
  794.    *    Parameter build area (not yet implemented)
  795.    *  (low memory)
  796.    */
  797.   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
  798.   switch (mips_fpu)
  799.     {
  800.     case MIPS_FPU_DOUBLE:
  801.       PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
  802.       break;
  803.     case MIPS_FPU_SINGLE:
  804.       PROC_FREG_MASK(proc_desc) = FLOAT_SINGLE_REG_SAVE_MASK;
  805.       break;
  806.     case MIPS_FPU_NONE:
  807.       PROC_FREG_MASK(proc_desc) = 0;
  808.       break;
  809.     }
  810.   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
  811.       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
  812.   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
  813.       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
  814.   /* save general registers */
  815.   save_address = sp + PROC_REG_OFFSET(proc_desc);
  816.   for (ireg = 32; --ireg >= 0; )
  817.     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
  818.       {
  819.     read_register_gen (ireg, buffer);
  820.  
  821.     /* Need to fix the save_address decrement below, and also make sure
  822.        that we don't run into problems with the size of the dummy frame
  823.        or any of the offsets within it.  */
  824.     if (REGISTER_RAW_SIZE (ireg) > 4)
  825.       error ("Cannot call functions on mips64");
  826.  
  827.     write_memory (save_address, buffer, REGISTER_RAW_SIZE (ireg));
  828.     save_address -= 4;
  829.       }
  830.   /* save floating-points registers starting with high order word */
  831.   save_address = sp + PROC_FREG_OFFSET(proc_desc) + 4;
  832.   for (ireg = 32; --ireg >= 0; )
  833.     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
  834.       {
  835.     read_register_gen (ireg + FP0_REGNUM, buffer);
  836.  
  837.     if (REGISTER_RAW_SIZE (ireg + FP0_REGNUM) > 4)
  838.       error ("Cannot call functions on mips64");
  839.  
  840.     write_memory (save_address, buffer,
  841.               REGISTER_RAW_SIZE (ireg + FP0_REGNUM));
  842.     save_address -= 4;
  843.       }
  844.   write_register (PUSH_FP_REGNUM, sp);
  845.   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
  846.   PROC_FRAME_OFFSET(proc_desc) = 0;
  847.   read_register_gen (PC_REGNUM, buffer);
  848.   write_memory (sp - 4, buffer, REGISTER_RAW_SIZE (PC_REGNUM));
  849.   read_register_gen (HI_REGNUM, buffer);
  850.   write_memory (sp - 8, buffer, REGISTER_RAW_SIZE (HI_REGNUM));
  851.   read_register_gen (LO_REGNUM, buffer);
  852.   write_memory (sp - 12, buffer, REGISTER_RAW_SIZE (LO_REGNUM));
  853.   if (mips_fpu != MIPS_FPU_NONE)
  854.     read_register_gen (FCRCS_REGNUM, buffer);
  855.   else
  856.     memset (buffer, 0, REGISTER_RAW_SIZE (FCRCS_REGNUM));
  857.   write_memory (sp - 16, buffer, REGISTER_RAW_SIZE (FCRCS_REGNUM));
  858.   sp -= 4 * (GEN_REG_SAVE_COUNT + SPECIAL_REG_SAVE_COUNT);
  859.   if (mips_fpu == MIPS_FPU_DOUBLE)
  860.     sp -= 4 * FLOAT_REG_SAVE_COUNT;
  861.   else if (mips_fpu == MIPS_FPU_SINGLE)
  862.     sp -= 4 * FLOAT_SINGLE_REG_SAVE_COUNT;
  863.   write_register (SP_REGNUM, sp);
  864.   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
  865.   PROC_HIGH_ADDR(proc_desc) = sp;
  866.   SET_PROC_DESC_IS_DUMMY(proc_desc);
  867.   PROC_PC_REG(proc_desc) = RA_REGNUM;
  868. }
  869.  
  870. void
  871. mips_pop_frame()
  872. {
  873.   register int regnum;
  874.   struct frame_info *frame = get_current_frame ();
  875.   CORE_ADDR new_sp = FRAME_FP (frame);
  876.  
  877.   mips_extra_func_info_t proc_desc = frame->proc_desc;
  878.  
  879.   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
  880.   if (frame->saved_regs == NULL)
  881.     mips_find_saved_regs (frame);
  882.   if (proc_desc)
  883.     {
  884.       for (regnum = 32; --regnum >= 0; )
  885.     if (PROC_REG_MASK(proc_desc) & (1 << regnum))
  886.       write_register (regnum,
  887.               read_memory_integer (frame->saved_regs->regs[regnum],
  888.                            4));
  889.       for (regnum = 32; --regnum >= 0; )
  890.     if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
  891.       write_register (regnum + FP0_REGNUM,
  892.               read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
  893.     }
  894.   write_register (SP_REGNUM, new_sp);
  895.   flush_cached_frames ();
  896.  
  897.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  898.     {
  899.       struct linked_proc_info *pi_ptr, *prev_ptr;
  900.  
  901.       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
  902.        pi_ptr != NULL;
  903.        prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
  904.     {
  905.       if (&pi_ptr->info == proc_desc)
  906.         break;
  907.     }
  908.  
  909.       if (pi_ptr == NULL)
  910.     error ("Can't locate dummy extra frame info\n");
  911.  
  912.       if (prev_ptr != NULL)
  913.     prev_ptr->next = pi_ptr->next;
  914.       else
  915.     linked_proc_desc_table = pi_ptr->next;
  916.  
  917.       free (pi_ptr);
  918.  
  919.       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
  920.       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
  921.       if (mips_fpu != MIPS_FPU_NONE)
  922.     write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
  923.     }
  924. }
  925.  
  926. static void
  927. mips_print_register (regnum, all)
  928.      int regnum, all;
  929. {
  930.   unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  931.   struct type *our_type =
  932.     init_type (TYPE_CODE_INT,
  933.            /* We will fill in the length for each register.  */
  934.            0,
  935.            TYPE_FLAG_UNSIGNED,
  936.            NULL,
  937.            NULL);
  938.  
  939.   /* Get the data in raw format.  */
  940.   if (read_relative_register_raw_bytes (regnum, raw_buffer))
  941.     {
  942.       printf_filtered ("%s: [Invalid]", reg_names[regnum]);
  943.       return;
  944.     }
  945.  
  946.   /* If an even floating pointer register, also print as double. */
  947.   if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
  948.       && !((regnum-FP0_REGNUM) & 1)) {
  949.     char dbuffer[MAX_REGISTER_RAW_SIZE]; 
  950.  
  951.     read_relative_register_raw_bytes (regnum, dbuffer);
  952.     read_relative_register_raw_bytes (regnum+1, dbuffer+4);
  953. #ifdef REGISTER_CONVERT_TO_TYPE
  954.     REGISTER_CONVERT_TO_TYPE(regnum, builtin_type_double, dbuffer);
  955. #endif
  956.     printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
  957.     val_print (builtin_type_double, dbuffer, 0,
  958.            gdb_stdout, 0, 1, 0, Val_pretty_default);
  959.     printf_filtered ("); ");
  960.   }
  961.   fputs_filtered (reg_names[regnum], gdb_stdout);
  962.  
  963.   /* The problem with printing numeric register names (r26, etc.) is that
  964.      the user can't use them on input.  Probably the best solution is to
  965.      fix it so that either the numeric or the funky (a2, etc.) names
  966.      are accepted on input.  */
  967.   if (regnum < 32)
  968.     printf_filtered ("(r%d): ", regnum);
  969.   else
  970.     printf_filtered (": ");
  971.  
  972.   /* If virtual format is floating, print it that way.  */
  973.   if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
  974.     val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
  975.            gdb_stdout, 0, 1, 0, Val_pretty_default);
  976.   /* Else print as integer in hex.  */
  977.   else
  978.     print_scalar_formatted (raw_buffer, REGISTER_VIRTUAL_TYPE (regnum),
  979.                 'x', 0, gdb_stdout);
  980. }
  981.  
  982. /* Replacement for generic do_registers_info.  */
  983.  
  984. void
  985. mips_do_registers_info (regnum, fpregs)
  986.      int regnum;
  987.      int fpregs;
  988. {
  989.   if (regnum != -1)
  990.     {
  991.       if (*(reg_names[regnum]) == '\0')
  992.     error ("Not a valid register for the current processor type");
  993.  
  994.       mips_print_register (regnum, 0);
  995.       printf_filtered ("\n");
  996.     }
  997.   else
  998.     {
  999.       int did_newline;
  1000.  
  1001.       for (regnum = 0; regnum < NUM_REGS; )
  1002.     {
  1003.       if (((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM)
  1004.           || *(reg_names[regnum]) == '\0')
  1005.         {
  1006.           regnum++;
  1007.           continue;
  1008.         }
  1009.       mips_print_register (regnum, 1);
  1010.       regnum++;
  1011.       printf_filtered ("; ");
  1012.       did_newline = 0;
  1013.       if ((regnum & 3) == 0)
  1014.         {
  1015.           printf_filtered ("\n");
  1016.           did_newline = 1;
  1017.         }
  1018.     }
  1019.       if (!did_newline)
  1020.     printf_filtered ("\n");
  1021.     }
  1022. }
  1023.  
  1024. /* Return number of args passed to a frame. described by FIP.
  1025.    Can return -1, meaning no way to tell.  */
  1026.  
  1027. int
  1028. mips_frame_num_args (frame)
  1029.     struct frame_info *frame;
  1030. {
  1031. #if 0 /* FIXME Use or lose this! */
  1032.   struct chain_info_t *p;
  1033.  
  1034.   p = mips_find_cached_frame (FRAME_FP (frame));
  1035.   if (p->valid)
  1036.     return p->the_info.numargs;
  1037. #endif
  1038.   return -1;
  1039. }
  1040.  
  1041. /* Is this a branch with a delay slot?  */
  1042.  
  1043. static int is_delayed PARAMS ((unsigned long));
  1044.  
  1045. static int
  1046. is_delayed (insn)
  1047.      unsigned long insn;
  1048. {
  1049.   int i;
  1050.   for (i = 0; i < NUMOPCODES; ++i)
  1051.     if (mips_opcodes[i].pinfo != INSN_MACRO
  1052.     && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
  1053.       break;
  1054.   return (i < NUMOPCODES
  1055.       && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
  1056.                        | INSN_COND_BRANCH_DELAY
  1057.                        | INSN_COND_BRANCH_LIKELY)));
  1058. }
  1059.  
  1060. int
  1061. mips_step_skips_delay (pc)
  1062.      CORE_ADDR pc;
  1063. {
  1064.   char buf[4];
  1065.  
  1066.   if (target_read_memory (pc, buf, 4) != 0)
  1067.     /* If error reading memory, guess that it is not a delayed branch.  */
  1068.     return 0;
  1069.   return is_delayed (extract_unsigned_integer (buf, 4));
  1070. }
  1071.  
  1072. /* To skip prologues, I use this predicate.  Returns either PC itself
  1073.    if the code at PC does not look like a function prologue; otherwise
  1074.    returns an address that (if we're lucky) follows the prologue.  If
  1075.    LENIENT, then we must skip everything which is involved in setting
  1076.    up the frame (it's OK to skip more, just so long as we don't skip
  1077.    anything which might clobber the registers which are being saved.
  1078.    We must skip more in the case where part of the prologue is in the
  1079.    delay slot of a non-prologue instruction).  */
  1080.  
  1081. CORE_ADDR
  1082. mips_skip_prologue (pc, lenient)
  1083.      CORE_ADDR pc;
  1084.      int lenient;
  1085. {
  1086.     unsigned long inst;
  1087.     int offset;
  1088.     int seen_sp_adjust = 0;
  1089.     int load_immediate_bytes = 0;
  1090.  
  1091.     /* Skip the typical prologue instructions. These are the stack adjustment
  1092.        instruction and the instructions that save registers on the stack
  1093.        or in the gcc frame.  */
  1094.     for (offset = 0; offset < 100; offset += 4)
  1095.       {
  1096.     char buf[4];
  1097.     int status;
  1098.  
  1099.     status = read_memory_nobpt (pc + offset, buf, 4);
  1100.     if (status)
  1101.       memory_error (status, pc + offset);
  1102.     inst = extract_unsigned_integer (buf, 4);
  1103.  
  1104. #if 0
  1105.     if (lenient && is_delayed (inst))
  1106.       continue;
  1107. #endif
  1108.  
  1109.     if ((inst & 0xffff0000) == 0x27bd0000)    /* addiu $sp,$sp,offset */
  1110.         seen_sp_adjust = 1;
  1111.     else if (inst == 0x03a1e823 ||             /* subu $sp,$sp,$at */
  1112.          inst == 0x03a8e823)               /* subu $sp,$sp,$t0 */
  1113.         seen_sp_adjust = 1;
  1114.     else if ((inst & 0xFFE00000) == 0xAFA00000 && (inst & 0x001F0000))
  1115.         continue;                /* sw reg,n($sp) */
  1116.                         /* reg != $zero */
  1117.     else if ((inst & 0xFFE00000) == 0xE7A00000) /* swc1 freg,n($sp) */
  1118.         continue;
  1119.     else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
  1120.                         /* sx reg,n($s8) */
  1121.         continue;                /* reg != $zero */
  1122.  
  1123.         /* move $s8,$sp.  With different versions of gas this will be either
  1124.            `addu $s8,$sp,$zero' or `or $s8,$sp,$zero'.  Accept either.  */
  1125.         else if (inst == 0x03A0F021 || inst == 0x03a0f025)
  1126.         continue;
  1127.  
  1128.     else if ((inst & 0xFF9F07FF) == 0x00800021) /* move reg,$a0-$a3 */
  1129.         continue;
  1130.     else if ((inst & 0xffff0000) == 0x3c1c0000) /* lui $gp,n */
  1131.         continue;
  1132.     else if ((inst & 0xffff0000) == 0x279c0000) /* addiu $gp,$gp,n */
  1133.         continue;
  1134.     else if (inst == 0x0399e021        /* addu $gp,$gp,$t9 */
  1135.          || inst == 0x033ce021)        /* addu $gp,$t9,$gp */
  1136.       continue;
  1137.     /* The following instructions load $at or $t0 with an immediate
  1138.        value in preparation for a stack adjustment via
  1139.        subu $sp,$sp,[$at,$t0]. These instructions could also initialize
  1140.        a local variable, so we accept them only before a stack adjustment
  1141.        instruction was seen.  */
  1142.     else if (!seen_sp_adjust)
  1143.       {
  1144.         if ((inst & 0xffff0000) == 0x3c010000 ||      /* lui $at,n */
  1145.         (inst & 0xffff0000) == 0x3c080000)      /* lui $t0,n */
  1146.           {
  1147.         load_immediate_bytes += 4;
  1148.         continue;
  1149.           }
  1150.         else if ((inst & 0xffff0000) == 0x34210000 || /* ori $at,$at,n */
  1151.              (inst & 0xffff0000) == 0x35080000 || /* ori $t0,$t0,n */
  1152.              (inst & 0xffff0000) == 0x34010000 || /* ori $at,$zero,n */
  1153.              (inst & 0xffff0000) == 0x34080000)   /* ori $t0,$zero,n */
  1154.           {
  1155.         load_immediate_bytes += 4;
  1156.         continue;
  1157.           }
  1158.         else
  1159.           break;
  1160.       }
  1161.     else
  1162.       break;
  1163.     }
  1164.  
  1165.     /* In a frameless function, we might have incorrectly
  1166.        skipped some load immediate instructions. Undo the skipping
  1167.        if the load immediate was not followed by a stack adjustment.  */
  1168.     if (load_immediate_bytes && !seen_sp_adjust)
  1169.       offset -= load_immediate_bytes;
  1170.     return pc + offset;
  1171. }
  1172.  
  1173. #if 0
  1174. /* The lenient prologue stuff should be superceded by the code in
  1175.    init_extra_frame_info which looks to see whether the stores mentioned
  1176.    in the proc_desc have actually taken place.  */
  1177.  
  1178. /* Is address PC in the prologue (loosely defined) for function at
  1179.    STARTADDR?  */
  1180.  
  1181. static int
  1182. mips_in_lenient_prologue (startaddr, pc)
  1183.      CORE_ADDR startaddr;
  1184.      CORE_ADDR pc;
  1185. {
  1186.   CORE_ADDR end_prologue = mips_skip_prologue (startaddr, 1);
  1187.   return pc >= startaddr && pc < end_prologue;
  1188. }
  1189. #endif
  1190.  
  1191. /* Given a return value in `regbuf' with a type `valtype', 
  1192.    extract and copy its value into `valbuf'.  */
  1193. void
  1194. mips_extract_return_value (valtype, regbuf, valbuf)
  1195.     struct type *valtype;
  1196.     char regbuf[REGISTER_BYTES];
  1197.     char *valbuf;
  1198. {
  1199.   int regnum;
  1200.   
  1201.   regnum = 2;
  1202.   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
  1203.        && (mips_fpu == MIPS_FPU_DOUBLE
  1204.        || (mips_fpu == MIPS_FPU_SINGLE && TYPE_LENGTH (valtype) <= 4)))
  1205.     regnum = FP0_REGNUM;
  1206.  
  1207.   memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
  1208. #ifdef REGISTER_CONVERT_TO_TYPE
  1209.   REGISTER_CONVERT_TO_TYPE(regnum, valtype, valbuf);
  1210. #endif
  1211. }
  1212.  
  1213. /* Given a return value in `regbuf' with a type `valtype', 
  1214.    write it's value into the appropriate register.  */
  1215. void
  1216. mips_store_return_value (valtype, valbuf)
  1217.     struct type *valtype;
  1218.     char *valbuf;
  1219. {
  1220.   int regnum;
  1221.   char raw_buffer[MAX_REGISTER_RAW_SIZE];
  1222.   
  1223.   regnum = 2;
  1224.   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
  1225.        && (mips_fpu == MIPS_FPU_DOUBLE
  1226.        || (mips_fpu == MIPS_FPU_SINGLE && TYPE_LENGTH (valtype) <= 4)))
  1227.     regnum = FP0_REGNUM;
  1228.  
  1229.   memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
  1230.  
  1231. #ifdef REGISTER_CONVERT_FROM_TYPE
  1232.   REGISTER_CONVERT_FROM_TYPE(regnum, valtype, raw_buffer);
  1233. #endif
  1234.  
  1235.   write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
  1236. }
  1237.  
  1238. /* These exist in mdebugread.c.  */
  1239. extern CORE_ADDR sigtramp_address, sigtramp_end;
  1240. extern void fixup_sigtramp PARAMS ((void));
  1241.  
  1242. /* Exported procedure: Is PC in the signal trampoline code */
  1243.  
  1244. int
  1245. in_sigtramp (pc, ignore)
  1246.      CORE_ADDR pc;
  1247.      char *ignore;        /* function name */
  1248. {
  1249.   if (sigtramp_address == 0)
  1250.     fixup_sigtramp ();
  1251.   return (pc >= sigtramp_address && pc < sigtramp_end);
  1252. }
  1253.  
  1254. /* Command to set FPU type.  mips_fpu_string will have been set to the
  1255.    user's argument.  Set mips_fpu based on mips_fpu_string, and then
  1256.    canonicalize mips_fpu_string.  */
  1257.  
  1258. /*ARGSUSED*/
  1259. static void
  1260. mips_set_fpu_command (args, from_tty, c)
  1261.      char *args;
  1262.      int from_tty;
  1263.      struct cmd_list_element *c;
  1264. {
  1265.   char *err = NULL;
  1266.  
  1267.   if (mips_fpu_string == NULL || *mips_fpu_string == '\0')
  1268.     mips_fpu = MIPS_FPU_DOUBLE;
  1269.   else if (strcasecmp (mips_fpu_string, "double") == 0
  1270.        || strcasecmp (mips_fpu_string, "on") == 0
  1271.        || strcasecmp (mips_fpu_string, "1") == 0
  1272.        || strcasecmp (mips_fpu_string, "yes") == 0)
  1273.     mips_fpu = MIPS_FPU_DOUBLE;
  1274.   else if (strcasecmp (mips_fpu_string, "none") == 0
  1275.        || strcasecmp (mips_fpu_string, "off") == 0
  1276.        || strcasecmp (mips_fpu_string, "0") == 0
  1277.        || strcasecmp (mips_fpu_string, "no") == 0)
  1278.     mips_fpu = MIPS_FPU_NONE;
  1279.   else if (strcasecmp (mips_fpu_string, "single") == 0)
  1280.     mips_fpu = MIPS_FPU_SINGLE;
  1281.   else
  1282.     err = strsave (mips_fpu_string);
  1283.  
  1284.   if (mips_fpu_string != NULL)
  1285.     free (mips_fpu_string);
  1286.  
  1287.   switch (mips_fpu)
  1288.     {
  1289.     case MIPS_FPU_DOUBLE:
  1290.       mips_fpu_string = strsave ("double");
  1291.       break;
  1292.     case MIPS_FPU_SINGLE:
  1293.       mips_fpu_string = strsave ("single");
  1294.       break;
  1295.     case MIPS_FPU_NONE:
  1296.       mips_fpu_string = strsave ("none");
  1297.       break;
  1298.     }
  1299.  
  1300.   if (err != NULL)
  1301.     {
  1302.       struct cleanup *cleanups = make_cleanup (free, err);
  1303.       error ("Unknown FPU type `%s'.  Use `double', `none', or `single'.",
  1304.          err);
  1305.       do_cleanups (cleanups);
  1306.     }
  1307. }
  1308.  
  1309. static void
  1310. mips_show_fpu_command (args, from_tty, c)
  1311.      char *args;
  1312.      int from_tty;
  1313.      struct cmd_list_element *c;
  1314. {
  1315. }
  1316.  
  1317. /* Command to set the processor type.  */
  1318.  
  1319. void
  1320. mips_set_processor_type_command (args, from_tty)
  1321.      char *args;
  1322.      int from_tty;
  1323. {
  1324.   int i;
  1325.  
  1326.   if (tmp_mips_processor_type == NULL || *tmp_mips_processor_type == '\0')
  1327.     {
  1328.       printf_unfiltered ("The known MIPS processor types are as follows:\n\n");
  1329.       for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
  1330.     printf_unfiltered ("%s\n", mips_processor_type_table[i].name);
  1331.  
  1332.       /* Restore the value.  */
  1333.       tmp_mips_processor_type = strsave (mips_processor_type);
  1334.  
  1335.       return;
  1336.     }
  1337.   
  1338.   if (!mips_set_processor_type (tmp_mips_processor_type))
  1339.     {
  1340.       error ("Unknown processor type `%s'.", tmp_mips_processor_type);
  1341.       /* Restore its value.  */
  1342.       tmp_mips_processor_type = strsave (mips_processor_type);
  1343.     }
  1344. }
  1345.  
  1346. static void
  1347. mips_show_processor_type_command (args, from_tty)
  1348.      char *args;
  1349.      int from_tty;
  1350. {
  1351. }
  1352.  
  1353. /* Modify the actual processor type. */
  1354.  
  1355. int
  1356. mips_set_processor_type (str)
  1357.      char *str;
  1358. {
  1359.   int i, j;
  1360.  
  1361.   if (str == NULL)
  1362.     return 0;
  1363.  
  1364.   for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
  1365.     {
  1366.       if (strcasecmp (str, mips_processor_type_table[i].name) == 0)
  1367.     {
  1368.       mips_processor_type = str;
  1369.  
  1370.       for (j = 0; j < NUM_REGS; ++j)
  1371.         reg_names[j] = mips_processor_type_table[i].regnames[j];
  1372.  
  1373.       return 1;
  1374.  
  1375.       /* FIXME tweak fpu flag too */
  1376.     }
  1377.     }
  1378.  
  1379.   return 0;
  1380. }
  1381.  
  1382. /* Attempt to identify the particular processor model by reading the
  1383.    processor id.  */
  1384.  
  1385. char *
  1386. mips_read_processor_type ()
  1387. {
  1388.   int prid;
  1389.  
  1390.   prid = read_register (PRID_REGNUM);
  1391.  
  1392.   if (prid & ~0xf == 0x700)
  1393.     return savestring ("r3041", strlen("r3041"));
  1394.  
  1395.   return NULL;
  1396. }
  1397.  
  1398. /* Just like reinit_frame_cache, but with the right arguments to be
  1399.    callable as an sfunc.  */
  1400.  
  1401. static void
  1402. reinit_frame_cache_sfunc (args, from_tty, c)
  1403.      char *args;
  1404.      int from_tty;
  1405.      struct cmd_list_element *c;
  1406. {
  1407.   reinit_frame_cache ();
  1408. }
  1409.  
  1410. int
  1411. gdb_print_insn_mips (memaddr, info)
  1412.      bfd_vma memaddr;
  1413.      disassemble_info *info;
  1414. {
  1415.   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
  1416.     return print_insn_big_mips (memaddr, info);
  1417.   else
  1418.     return print_insn_little_mips (memaddr, info);
  1419. }
  1420.  
  1421. void
  1422. _initialize_mips_tdep ()
  1423. {
  1424.   struct cmd_list_element *c;
  1425.  
  1426.   tm_print_insn = gdb_print_insn_mips;
  1427.  
  1428.   /* Let the user turn off floating point and set the fence post for
  1429.      heuristic_proc_start.  */
  1430.  
  1431.   c = add_set_cmd ("mipsfpu", class_support, var_string_noescape,
  1432.            (char *) &mips_fpu_string,
  1433.            "Set use of floating point coprocessor.\n\
  1434. Set to `none' to avoid using floating point instructions when calling\n\
  1435. functions or dealing with return values.  Set to `single' to use only\n\
  1436. single precision floating point as on the R4650.  Set to `double' for\n\
  1437. normal floating point support.",
  1438.            &setlist);
  1439.   c->function.sfunc = mips_set_fpu_command;
  1440.   c = add_show_from_set (c, &showlist);
  1441.   c->function.sfunc = mips_show_fpu_command;
  1442.  
  1443.   mips_fpu = MIPS_FPU_DOUBLE;
  1444.   mips_fpu_string = strsave ("double");
  1445.  
  1446.   c = add_set_cmd ("processor", class_support, var_string_noescape,
  1447.            (char *) &tmp_mips_processor_type,
  1448.            "Set the type of MIPS processor in use.\n\
  1449. Set this to be able to access processor-type-specific registers.\n\
  1450. ",
  1451.            &setlist);
  1452.   c->function.cfunc = mips_set_processor_type_command;
  1453.   c = add_show_from_set (c, &showlist);
  1454.   c->function.cfunc = mips_show_processor_type_command;
  1455.  
  1456.   tmp_mips_processor_type = strsave (DEFAULT_MIPS_TYPE);
  1457.   mips_set_processor_type_command (strsave (DEFAULT_MIPS_TYPE), 0);
  1458.  
  1459.   /* We really would like to have both "0" and "unlimited" work, but
  1460.      command.c doesn't deal with that.  So make it a var_zinteger
  1461.      because the user can always use "999999" or some such for unlimited.  */
  1462.   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
  1463.            (char *) &heuristic_fence_post,
  1464.            "\
  1465. Set the distance searched for the start of a function.\n\
  1466. If you are debugging a stripped executable, GDB needs to search through the\n\
  1467. program for the start of a function.  This command sets the distance of the\n\
  1468. search.  The only need to set it is when debugging a stripped executable.",
  1469.            &setlist);
  1470.   /* We need to throw away the frame cache when we set this, since it
  1471.      might change our ability to get backtraces.  */
  1472.   c->function.sfunc = reinit_frame_cache_sfunc;
  1473.   add_show_from_set (c, &showlist);
  1474. }
  1475.