home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / values.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  32KB  |  1,196 lines

  1. /* Low level packing and unpacking of values for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB 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 1, or (at your option)
  9. any later version.
  10.  
  11. GDB 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 GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "value.h"
  25.  
  26. /* The value-history records all the values printed
  27.    by print commands during this session.  Each chunk
  28.    records 60 consecutive values.  The first chunk on
  29.    the chain records the most recent values.
  30.    The total number of values is in value_history_count.  */
  31.  
  32. #define VALUE_HISTORY_CHUNK 60
  33.  
  34. struct value_history_chunk
  35. {
  36.   struct value_history_chunk *next;
  37.   value values[VALUE_HISTORY_CHUNK];
  38. };
  39.  
  40. /* Chain of chunks now in use.  */
  41.  
  42. static struct value_history_chunk *value_history_chain;
  43.  
  44. static int value_history_count;    /* Abs number of last entry stored */
  45.  
  46.  
  47. /* List of all value objects currently allocated
  48.    (except for those released by calls to release_value)
  49.    This is so they can be freed after each command.  */
  50.  
  51. static value all_values;
  52.  
  53. /* Allocate a  value  that has the correct length for type TYPE.  */
  54.  
  55. value
  56. allocate_value (type)
  57.      struct type *type;
  58. {
  59.   register value val;
  60.  
  61.   check_stub_type (type);
  62.  
  63.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  64.   VALUE_NEXT (val) = all_values;
  65.   all_values = val;
  66.   VALUE_TYPE (val) = type;
  67.   VALUE_LVAL (val) = not_lval;
  68.   VALUE_ADDRESS (val) = 0;
  69.   VALUE_FRAME (val) = 0;
  70.   VALUE_OFFSET (val) = 0;
  71.   VALUE_BITPOS (val) = 0;
  72.   VALUE_BITSIZE (val) = 0;
  73.   VALUE_REPEATED (val) = 0;
  74.   VALUE_REPETITIONS (val) = 0;
  75.   VALUE_REGNO (val) = -1;
  76.   return val;
  77. }
  78.  
  79. /* Allocate a  value  that has the correct length
  80.    for COUNT repetitions type TYPE.  */
  81.  
  82. value
  83. allocate_repeat_value (type, count)
  84.      struct type *type;
  85.      int count;
  86. {
  87.   register value val;
  88.  
  89.   check_stub_type (type);
  90.  
  91.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  92.   VALUE_NEXT (val) = all_values;
  93.   all_values = val;
  94.   VALUE_TYPE (val) = type;
  95.   VALUE_LVAL (val) = not_lval;
  96.   VALUE_ADDRESS (val) = 0;
  97.   VALUE_FRAME (val) = 0;
  98.   VALUE_OFFSET (val) = 0;
  99.   VALUE_BITPOS (val) = 0;
  100.   VALUE_BITSIZE (val) = 0;
  101.   VALUE_REPEATED (val) = 1;
  102.   VALUE_REPETITIONS (val) = count;
  103.   VALUE_REGNO (val) = -1;
  104.   return val;
  105. }
  106.  
  107. /* Free all the values that have been allocated (except for those released).
  108.    Called after each command, successful or not.  */
  109.  
  110. void
  111. free_all_values ()
  112. {
  113.   register value val, next;
  114.  
  115.   for (val = all_values; val; val = next)
  116.     {
  117.       next = VALUE_NEXT (val);
  118.       free (val);
  119.     }
  120.  
  121.   all_values = 0;
  122. }
  123.  
  124. /* Remove VAL from the chain all_values
  125.    so it will not be freed automatically.  */
  126.  
  127. void
  128. release_value (val)
  129.      register value val;
  130. {
  131.   register value v;
  132.  
  133.   if (all_values == val)
  134.     {
  135.       all_values = val->next;
  136.       return;
  137.     }
  138.  
  139.   for (v = all_values; v; v = v->next)
  140.     {
  141.       if (v->next == val)
  142.     {
  143.       v->next = val->next;
  144.       break;
  145.     }
  146.     }
  147. }
  148.  
  149. /* Return a copy of the value ARG.
  150.    It contains the same contents, for same memory address,
  151.    but it's a different block of storage.  */
  152.  
  153. static value
  154. value_copy (arg)
  155.      value arg;
  156. {
  157.   register value val;
  158.   register struct type *type = VALUE_TYPE (arg);
  159.   if (VALUE_REPEATED (arg))
  160.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  161.   else
  162.     val = allocate_value (type);
  163.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  164.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  165.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  166.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  167.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  168.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  169.   bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val),
  170.      TYPE_LENGTH (VALUE_TYPE (arg))
  171.      * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  172.   return val;
  173. }
  174.  
  175. /* Access to the value history.  */
  176.  
  177. /* Record a new value in the value history.
  178.    Returns the absolute history index of the entry.  */
  179.  
  180. int
  181. record_latest_value (val)
  182.      value val;
  183. {
  184.   int i;
  185.   double foo;
  186.  
  187.   /* Check error now if about to store an invalid float.  We return -1
  188.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  189.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  190.     foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  191.     if (i) return -1;        /* Indicate value not saved in history */
  192.   }
  193.  
  194.   /* Here we treat value_history_count as origin-zero
  195.      and applying to the value being stored now.  */
  196.  
  197.   i = value_history_count % VALUE_HISTORY_CHUNK;
  198.   if (i == 0)
  199.     {
  200.       register struct value_history_chunk *new
  201.     = (struct value_history_chunk *)
  202.       xmalloc (sizeof (struct value_history_chunk));
  203.       bzero (new->values, sizeof new->values);
  204.       new->next = value_history_chain;
  205.       value_history_chain = new;
  206.     }
  207.  
  208.   value_history_chain->values[i] = val;
  209.   release_value (val);
  210.  
  211.   /* Now we regard value_history_count as origin-one
  212.      and applying to the value just stored.  */
  213.  
  214.   return ++value_history_count;
  215. }
  216.  
  217. /* Return a copy of the value in the history with sequence number NUM.  */
  218.  
  219. value
  220. access_value_history (num)
  221.      int num;
  222. {
  223.   register struct value_history_chunk *chunk;
  224.   register int i;
  225.   register int absnum = num;
  226.  
  227.   if (absnum <= 0)
  228.     absnum += value_history_count;
  229.  
  230.   if (absnum <= 0)
  231.     {
  232.       if (num == 0)
  233.     error ("The history is empty.");
  234.       else if (num == 1)
  235.     error ("There is only one value in the history.");
  236.       else
  237.     error ("History does not go back to $$%d.", -num);
  238.     }
  239.   if (absnum > value_history_count)
  240.     error ("History has not yet reached $%d.", absnum);
  241.  
  242.   absnum--;
  243.  
  244.   /* Now absnum is always absolute and origin zero.  */
  245.  
  246.   chunk = value_history_chain;
  247.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  248.        i > 0; i--)
  249.     chunk = chunk->next;
  250.  
  251.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  252. }
  253.  
  254. /* Clear the value history entirely.
  255.    Must be done when new symbol tables are loaded,
  256.    because the type pointers become invalid.  */
  257.  
  258. void
  259. clear_value_history ()
  260. {
  261.   register struct value_history_chunk *next;
  262.   register int i;
  263.   register value val;
  264.  
  265.   while (value_history_chain)
  266.     {
  267.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  268.     if (val = value_history_chain->values[i])
  269.       free (val);
  270.       next = value_history_chain->next;
  271.       free (value_history_chain);
  272.       value_history_chain = next;
  273.     }
  274.   value_history_count = 0;
  275. }
  276.  
  277. static void
  278. value_history_info (num_exp, from_tty)
  279.      char *num_exp;
  280.      int from_tty;
  281. {
  282.   register int i;
  283.   register value val;
  284.   static int num = 1;
  285.  
  286.   if (num_exp)
  287.     {
  288.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  289.     /* "info history +" should print from the stored position.  */
  290.     ;
  291.       else
  292.     /* "info history <exp>" should print around value number <exp>.  */
  293.     num = parse_and_eval_address (num_exp) - 5;
  294.     }
  295.   else
  296.     {
  297.       /* "info history" means print the last 10 values.  */
  298.       num = value_history_count - 9;
  299.     }
  300.  
  301.   if (num <= 0)
  302.     num = 1;
  303.  
  304.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  305.     {
  306.       val = access_value_history (i);
  307.       printf_filtered ("$%d = ", i);
  308.       value_print (val, stdout, 0, Val_pretty_default);
  309.       printf_filtered ("\n");
  310.     }
  311.  
  312.   /* The next "info history +" should start after what we just printed.  */
  313.   num += 10;
  314.  
  315.   /* Hitting just return after this command should do the same thing as
  316.      "info history +".  If num_exp is null, this is unnecessary, since
  317.      "info history +" is not useful after "info history".  */
  318.   if (from_tty && num_exp)
  319.     {
  320.       num_exp[0] = '+';
  321.       num_exp[1] = '\0';
  322.     }
  323. }
  324.  
  325. /* Internal variables.  These are variables within the debugger
  326.    that hold values assigned by debugger commands.
  327.    The user refers to them with a '$' prefix
  328.    that does not appear in the variable names stored internally.  */
  329.  
  330. static struct internalvar *internalvars;
  331.  
  332. /* Look up an internal variable with name NAME.  NAME should not
  333.    normally include a dollar sign.
  334.  
  335.    If the specified internal variable does not exist,
  336.    one is created, with a void value.  */
  337.  
  338. struct internalvar *
  339. lookup_internalvar (name)
  340.      char *name;
  341. {
  342.   register struct internalvar *var;
  343.  
  344.   for (var = internalvars; var; var = var->next)
  345.     if (!strcmp (var->name, name))
  346.       return var;
  347.  
  348.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  349.   var->name = concat (name, "", "");
  350.   var->value = allocate_value (builtin_type_void);
  351.   release_value (var->value);
  352.   var->next = internalvars;
  353.   internalvars = var;
  354.   return var;
  355. }
  356.  
  357. value
  358. value_of_internalvar (var)
  359.      struct internalvar *var;
  360. {
  361.   register value val;
  362.  
  363. #ifdef IS_TRAPPED_INTERNALVAR
  364.   if (IS_TRAPPED_INTERNALVAR (var->name))
  365.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  366. #endif 
  367.  
  368.   val = value_copy (var->value);
  369.   VALUE_LVAL (val) = lval_internalvar;
  370.   VALUE_INTERNALVAR (val) = var;
  371.   return val;
  372. }
  373.  
  374. void
  375. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  376.      struct internalvar *var;
  377.      int offset, bitpos, bitsize;
  378.      value newval;
  379. {
  380.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  381.  
  382. #ifdef IS_TRAPPED_INTERNALVAR
  383.   if (IS_TRAPPED_INTERNALVAR (var->name))
  384.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  385. #endif
  386.  
  387.   if (bitsize)
  388.     modify_field (addr, (int) value_as_long (newval),
  389.           bitpos, bitsize);
  390.   else
  391.     bcopy (VALUE_CONTENTS (newval), addr,
  392.        TYPE_LENGTH (VALUE_TYPE (newval)));
  393. }
  394.  
  395. void
  396. set_internalvar (var, val)
  397.      struct internalvar *var;
  398.      value val;
  399. {
  400. #ifdef IS_TRAPPED_INTERNALVAR
  401.   if (IS_TRAPPED_INTERNALVAR (var->name))
  402.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  403. #endif
  404.  
  405.   free (var->value);
  406.   var->value = value_copy (val);
  407.   release_value (var->value);
  408. }
  409.  
  410. char *
  411. internalvar_name (var)
  412.      struct internalvar *var;
  413. {
  414.   return var->name;
  415. }
  416.  
  417. /* Free all internalvars.  Done when new symtabs are loaded,
  418.    because that makes the values invalid.  */
  419.  
  420. void
  421. clear_internalvars ()
  422. {
  423.   register struct internalvar *var;
  424.  
  425.   while (internalvars)
  426.     {
  427.       var = internalvars;
  428.       internalvars = var->next;
  429.       free (var->name);
  430.       free (var->value);
  431.       free (var);
  432.     }
  433. }
  434.  
  435. static void
  436. convenience_info ()
  437. {
  438.   register struct internalvar *var;
  439.   int varseen = 0;
  440.  
  441.   for (var = internalvars; var; var = var->next)
  442.     {
  443. #ifdef IS_TRAPPED_INTERNALVAR
  444.       if (IS_TRAPPED_INTERNALVAR (var->name))
  445.     continue;
  446. #endif
  447.       if (!varseen)
  448.     {
  449.       printf ("Debugger convenience variables:\n\n");
  450.       varseen = 1;
  451.     }
  452.       printf ("$%s: ", var->name);
  453.       value_print (var->value, stdout, 0, Val_pretty_default);
  454.       printf ("\n");
  455.     }
  456.   if (!varseen)
  457.     printf ("No debugger convenience variables now defined.\n\
  458. Convenience variables have names starting with \"$\";\n\
  459. use \"set\" as in \"set $foo = 5\" to define them.\n");
  460. }
  461.  
  462. /* Extract a value as a C number (either long or double).
  463.    Knows how to convert fixed values to double, or
  464.    floating values to long.
  465.    Does not deallocate the value.  */
  466.  
  467. LONGEST
  468. value_as_long (val)
  469.      register value val;
  470. {
  471.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  472. }
  473.  
  474. double
  475. value_as_double (val)
  476.      register value val;
  477. {
  478.   double foo;
  479.   int inv;
  480.   
  481.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  482.   if (inv)
  483.     error ("Invalid floating value found in program.");
  484.   return foo;
  485. }
  486.  
  487. /* Unpack raw data (copied from debugee) at VALADDR
  488.    as a long, or as a double, assuming the raw data is described
  489.    by type TYPE.  Knows how to convert different sizes of values
  490.    and can convert between fixed and floating point.
  491.  
  492.    C++: It is assumed that the front-end has taken care of
  493.    all matters concerning pointers to members.  A pointer
  494.    to member which reaches here is considered to be equivalent
  495.    to an INT (or some size).  After all, it is only an offset.  */
  496.  
  497. LONGEST
  498. unpack_long (type, valaddr)
  499.      struct type *type;
  500.      char *valaddr;
  501. {
  502.   register enum type_code code = TYPE_CODE (type);
  503.   register int len = TYPE_LENGTH (type);
  504.   register int nosign = TYPE_UNSIGNED (type);
  505.  
  506.   if (code == TYPE_CODE_ENUM)
  507.     code = TYPE_CODE_INT;
  508.   if (code == TYPE_CODE_FLT)
  509.     {
  510.       if (len == sizeof (float))
  511.     return * (float *) valaddr;
  512.  
  513.       if (len == sizeof (double))
  514.     return * (double *) valaddr;
  515.  
  516. #ifdef LONG_DOUBLE
  517.       if (len == sizeof (long double))
  518.     return *(long double *) valaddr;
  519. #endif
  520.     }
  521.   else if (code == TYPE_CODE_INT && nosign)
  522.     {
  523.       if (len == sizeof (char))
  524.     return * (unsigned char *) valaddr;
  525.  
  526.       if (len == sizeof (short))
  527.     return * (unsigned short *) valaddr;
  528.  
  529.       if (len == sizeof (int))
  530.     return * (unsigned int *) valaddr;
  531.  
  532.       if (len == sizeof (long))
  533.     return * (unsigned long *) valaddr;
  534. #ifdef LONG_LONG
  535.       if (len == sizeof (long long))
  536.     return * (unsigned long long *) valaddr;
  537. #endif
  538.     }
  539.   else if (code == TYPE_CODE_INT)
  540.     {
  541.       if (len == sizeof (char))
  542.     return * (char *) valaddr;
  543.  
  544.       if (len == sizeof (short))
  545.     return * (short *) valaddr;
  546.  
  547.       if (len == sizeof (int))
  548.     return * (int *) valaddr;
  549.  
  550.       if (len == sizeof (long))
  551.     return * (long *) valaddr;
  552.  
  553. #ifdef LONG_LONG
  554.       if (len == sizeof (long long))
  555.     return * (long long *) valaddr;
  556. #endif
  557.     }
  558.   else if (code == TYPE_CODE_PTR
  559.        || code == TYPE_CODE_REF)
  560.     {
  561.       if (len == sizeof (char *))
  562.     return (CORE_ADDR) * (char **) valaddr;
  563.     }
  564.   else if (code == TYPE_CODE_MEMBER)
  565.     error ("not implemented: member types in unpack_long");
  566.  
  567.   error ("Value not integer or pointer.");
  568. }
  569.  
  570. /* Return a double value from the specified type and address.
  571.    INVP points to an int which is set to 0 for valid value,
  572.    1 for invalid value (bad float format).  In either case,
  573.    the returned double is OK to use.  */
  574.  
  575. double
  576. unpack_double (type, valaddr, invp)
  577.      struct type *type;
  578.      char *valaddr;
  579.      int *invp;
  580. {
  581.   register enum type_code code = TYPE_CODE (type);
  582.   register int len = TYPE_LENGTH (type);
  583.   register int nosign = TYPE_UNSIGNED (type);
  584.  
  585.   *invp = 0;            /* Assume valid.   */
  586.   if (code == TYPE_CODE_FLT)
  587.     {
  588.       if (INVALID_FLOAT (valaddr, len))
  589.     {
  590.       *invp = 1;
  591.       return 1.234567891011121314;
  592.     }
  593.  
  594.       if (len == sizeof (float))
  595.     return * (float *) valaddr;
  596.  
  597.       if (len == sizeof (double))
  598.     {
  599.       /* Some machines require doubleword alignment for doubles.
  600.          This code works on them, and on other machines.  */
  601.       double temp;
  602.       bcopy ((char *) valaddr, (char *) &temp, sizeof (double));
  603.       return temp;
  604.     }
  605.  
  606. #ifdef LONG_DOUBLE
  607.       if (len == sizeof (long double))
  608.     return *(long double *) valaddr;
  609. #endif
  610.     }
  611.   else if (code == TYPE_CODE_INT && nosign)
  612.     {
  613.       if (len == sizeof (char))
  614.     return * (unsigned char *) valaddr;
  615.  
  616.       if (len == sizeof (short))
  617.     return * (unsigned short *) valaddr;
  618.  
  619.       if (len == sizeof (int))
  620.     return * (unsigned int *) valaddr;
  621.  
  622.       if (len == sizeof (long))
  623.     return * (unsigned long *) valaddr;
  624.  
  625. #ifdef LONG_LONG
  626.       if (len == sizeof (long long))
  627.     return * (unsigned long long *) valaddr;
  628. #endif
  629.     }
  630.   else if (code == TYPE_CODE_INT)
  631.     {
  632.       if (len == sizeof (char))
  633.     return * (char *) valaddr;
  634.  
  635.       if (len == sizeof (short))
  636.     return * (short *) valaddr;
  637.  
  638.       if (len == sizeof (int))
  639.     return * (int *) valaddr;
  640.  
  641.       if (len == sizeof (long))
  642.     return * (long *) valaddr;
  643.  
  644. #ifdef LONG_LONG
  645.       if (len == sizeof (long long))
  646.     return * (long long *) valaddr;
  647. #endif
  648.     }
  649.  
  650.   error ("Value not floating number.");
  651.   /* NOTREACHED */
  652.   return (double) 0;        /* To silence compiler warning.  */
  653. }
  654.  
  655. /* Given a value ARG1 of a struct or union type,
  656.    extract and return the value of one of its fields.
  657.    FIELDNO says which field.
  658.  
  659.    For C++, must also be able to return values from static fields */
  660.  
  661. value
  662. value_field (arg1, fieldno)
  663.      register value arg1;
  664.      register int fieldno;
  665. {
  666.   register value v;
  667.   register struct type *type;
  668.   register int offset;
  669.  
  670.   check_stub_type (VALUE_TYPE (arg1));
  671.   type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno);
  672.  
  673.   /* Handle packed fields */
  674.  
  675.   offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8;
  676.   if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno))
  677.     {
  678.       v = value_from_long (type,
  679.                unpack_field_as_long (VALUE_TYPE (arg1),
  680.                          VALUE_CONTENTS (arg1),
  681.                          fieldno));
  682.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8;
  683.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno);
  684.     }
  685.   else
  686.     {
  687.       v = allocate_value (type);
  688.       bcopy (VALUE_CONTENTS (arg1) + offset,
  689.          VALUE_CONTENTS (v),
  690.          TYPE_LENGTH (type));
  691.     }
  692.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  693.   if (VALUE_LVAL (arg1) == lval_internalvar)
  694.     VALUE_LVAL (v) = lval_internalvar_component;
  695.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  696.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  697.   return v;
  698. }
  699.  
  700. value
  701. value_fn_field (arg1, fieldno, subfieldno)
  702.      register value arg1;
  703.      register int fieldno;
  704. {
  705.   register value v;
  706.   struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
  707.   register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
  708.   struct symbol *sym;
  709.  
  710.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
  711.                0, VAR_NAMESPACE, 0);
  712.   if (! sym) error ("Internal error: could not find physical method named %s",
  713.             TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
  714.   
  715.   v = allocate_value (type);
  716.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  717.   VALUE_TYPE (v) = type;
  718.   return v;
  719. }
  720.  
  721. /* Return a virtual function as a value.
  722.    ARG1 is the object which provides the virtual function
  723.    table pointer.
  724.    F is the list of member functions which contains the desired virtual
  725.    function.
  726.    J is an index into F which provides the desired virtual function.
  727.    TYPE is the basetype which first provides the virtual function table.  */
  728. value
  729. value_virtual_fn_field (arg1, f, j, type)
  730.      value arg1;
  731.      struct fn_field *f;
  732.      int j;
  733.      struct type *type;
  734. {
  735.   /* First, get the virtual function table pointer.  That comes
  736.      with a strange type, so cast it to type `pointer to long' (which
  737.      should serve just fine as a function type).  Then, index into
  738.      the table, and convert final value to appropriate function type.  */
  739.   value vfn, vtbl;
  740.   value vi = value_from_long (builtin_type_int, 
  741.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  742.   VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type);
  743.  
  744.   /* This type may have been defined before its virtual function table
  745.      was.  If so, fill in the virtual function table entry for the
  746.      type now.  */
  747.   if (TYPE_VPTR_FIELDNO (type) < 0)
  748.     TYPE_VPTR_FIELDNO (type)
  749.       = fill_in_vptr_fieldno (type);
  750.  
  751.   /* The virtual function table is now an array of structures
  752.      which have the form { int16 offset, delta; void *pfn; }.  */
  753.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type)));
  754.  
  755.   /* Index into the virtual function table.  This is hard-coded because
  756.      looking up a field is not cheap, and it may be important to save
  757.      time, e.g. if the user has set a conditional breakpoint calling
  758.      a virtual function.  */
  759.   vfn = value_field (value_subscript (vtbl, vi), 2);
  760.  
  761.   /* Reinstantiate the function pointer with the correct type.  */
  762.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  763.   return vfn;
  764. }
  765.  
  766. /* The value of a static class member does not depend
  767.    on its instance, only on its type.  If FIELDNO >= 0,
  768.    then fieldno is a valid field number and is used directly.
  769.    Otherwise, FIELDNAME is the name of the field we are
  770.    searching for.  If it is not a static field name, an
  771.    error is signaled.  TYPE is the type in which we look for the
  772.    static field member.  */
  773. value
  774. value_static_field (type, fieldname, fieldno)
  775.      register struct type *type;
  776.      char *fieldname;
  777.      register int fieldno;
  778. {
  779.   register value v;
  780.   struct symbol *sym;
  781.  
  782.   if (fieldno < 0)
  783.     {
  784.       register struct type *t = type;
  785.       /* Look for static field.  */
  786.       while (t)
  787.     {
  788.       int i;
  789.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  790.         if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
  791.           {
  792.         if (TYPE_FIELD_STATIC (t, i))
  793.           {
  794.             fieldno = i;
  795.             goto found;
  796.           }
  797.         else
  798.           error ("field `%s' is not static");
  799.           }
  800.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  801.     }
  802.  
  803.       t = type;
  804.  
  805.       if (destructor_name_p (fieldname, t))
  806.     error ("use `info method' command to print out value of destructor");
  807.  
  808.       while (t)
  809.     {
  810.       int i, j;
  811.  
  812.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
  813.         {
  814.           if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
  815.         {
  816.           error ("use `info method' command to print value of method \"%s\"", fieldname);
  817.         }
  818.         }
  819.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  820.     }
  821.       error("there is no field named %s", fieldname);
  822.     }
  823.  
  824.  found:
  825.  
  826.   sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno),
  827.                0, VAR_NAMESPACE, 0);
  828.   if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno));
  829.  
  830.   type = TYPE_FIELD_TYPE (type, fieldno);
  831.   v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  832.   return v;
  833. }
  834.  
  835. /* Compute the address of the baseclass which is
  836.    the INDEXth baseclass of TYPE.  The TYPE base
  837.    of the object is at VALADDR.  */
  838.  
  839. char *
  840. baseclass_addr (type, index, valaddr)
  841.      struct type *type;
  842.      int index;
  843.      char *valaddr;
  844. {
  845.   struct type *basetype = TYPE_BASECLASS (type, index);
  846.  
  847.   if (TYPE_VIA_VIRTUAL (basetype))
  848.     {
  849.       /* Must hunt for the pointer to this virtual baseclass.  */
  850.       register int i, len = TYPE_NFIELDS (type);
  851.       register int n_baseclasses = TYPE_N_BASECLASSES (type);
  852.       char *vbase_name, *type_name = type_name_no_tag (basetype);
  853.  
  854.       vbase_name = (char *)alloca (strlen (type_name) + 8);
  855.       sprintf (vbase_name, "_vb$%s", type_name);
  856.       /* First look for the virtual baseclass pointer
  857.      in the fields.  */
  858.       for (i = n_baseclasses; i < len; i++)
  859.     {
  860.       if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
  861.         {
  862.           value v = value_at (TYPE_MAIN_VARIANT (basetype),
  863.                   unpack_long (TYPE_FIELD_TYPE (type, i),
  864.                            valaddr + (TYPE_FIELD_BITPOS (type, i) / 8)));
  865.           return (char *) VALUE_CONTENTS (v);
  866.         }
  867.     }
  868.       /* Not in the fields, so try looking through the baseclasses.  */
  869.       for (i = index+1; i <= n_baseclasses; i++)
  870.     {
  871.       char *baddr;
  872.  
  873.       baddr = baseclass_addr (type, i, valaddr);
  874.       if (baddr)
  875.         return baddr;
  876.     }
  877.       /* Not found.  */
  878.       return 0;
  879.     }
  880.  
  881.   /* Baseclass is easily computed.  */
  882.   return valaddr + TYPE_FIELD_BITPOS (type, index-1) / 8;
  883. }
  884.  
  885. /* Ugly hack to convert method stubs into method types.  */
  886. void
  887. check_stub_method (type, i, j)
  888.      struct type *type;
  889.      int i, j;
  890. {
  891.   extern char *gdb_mangle_typename (), *strchr ();
  892.   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  893.   char *inner_name = gdb_mangle_typename (type);
  894.   char *mangled_name
  895.     = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type, i))
  896.                + strlen (inner_name)
  897.                + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
  898.                + 1);
  899.   char *demangled_name, *cplus_demangle ();
  900.   char *argname, *p;
  901.   int depth = 0, argcount = 1;
  902.   struct type **argtypes;
  903.  
  904.   if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
  905.       && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == '$')
  906.     /* Destructor */
  907.     strcpy (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
  908.   else
  909.     {
  910.       if (!strcmp (type_name_no_tag (type), TYPE_FN_FIELDLIST_NAME (type, i)))
  911.     /* Constructor */
  912.     strcpy (mangled_name, inner_name);
  913.       else
  914.     {
  915.       strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
  916.       strcat (mangled_name, inner_name);
  917.     }
  918.       strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
  919.     }
  920.   demangled_name = cplus_demangle (mangled_name, 0);
  921.  
  922.   /* Now, read in the parameters that define this type.  */
  923.   argname = strchr (demangled_name, '(') + 1;
  924.   p = argname;
  925.   while (*p)
  926.     {
  927.       if (*p == '(')
  928.     depth += 1;
  929.       else if (*p == ')')
  930.     depth -= 1;
  931.       else if (*p == ',' && depth == 0)
  932.     argcount += 1;
  933.  
  934.       p += 1;
  935.     }
  936.   if (p[-2] != '.')
  937.     argcount += 1;
  938.   argtypes = (struct type **)xmalloc (argcount * sizeof (struct type *));
  939.   p = argname;
  940.   argcount = 1;
  941.   argtypes[0] = lookup_pointer_type (type);
  942.   while (*p)
  943.     {
  944.       if (*p == '(')
  945.     depth += 1;
  946.       else if (*p == ')')
  947.     depth -= 1;
  948.  
  949.       if ((depth == 0 && *p == ',') || (depth == -1 && *p == ')'))
  950.     {
  951.       char *tmp = (char *)alloca (p - argname + 4);
  952.       value val;
  953.       if (p != argname && p[-1] != '.')
  954.         {
  955.           tmp[0] = '(';
  956.           bcopy (argname, tmp+1, p - argname);
  957.           tmp[p-argname+1] = ')';
  958.           tmp[p-argname+2] = '0';
  959.           tmp[p-argname+3] = '\0';
  960.           val = parse_and_eval (tmp);
  961.           argtypes[argcount] = VALUE_TYPE (val);
  962.         }
  963.       else
  964.         argtypes[argcount] = builtin_type_void;
  965.       argcount += 1;
  966.       argname = p + 1;
  967.     }
  968.       p += 1;
  969.     }
  970.   if (p[-2] != '.')
  971.     argtypes[argcount] = builtin_type_void;
  972.   free (demangled_name);
  973.   smash_to_method_type (TYPE_FN_FIELD_TYPE (f, j), type,
  974.             TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
  975.             argtypes);
  976.   free (TYPE_FN_FIELD_PHYSNAME (f, j));
  977.   TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
  978.   TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) &= ~TYPE_FLAG_STUB;
  979. }
  980.  
  981. long
  982. unpack_field_as_long (type, valaddr, fieldno)
  983.      struct type *type;
  984.      char *valaddr;
  985.      int fieldno;
  986. {
  987.   long val;
  988.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  989.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  990.  
  991.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  992.  
  993.   /* Extracting bits depends on endianness of the machine.  */
  994. #ifdef BITS_BIG_ENDIAN
  995.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  996. #else
  997.   val = val >> (bitpos % 8);
  998. #endif
  999.  
  1000.   val &= (1 << bitsize) - 1;
  1001.   return val;
  1002. }
  1003.  
  1004. void
  1005. modify_field (addr, fieldval, bitpos, bitsize)
  1006.      char *addr;
  1007.      int fieldval;
  1008.      int bitpos, bitsize;
  1009. {
  1010.   long oword;
  1011.  
  1012.   /* Reject values too big to fit in the field in question.
  1013.      Otherwise adjoining fields may be corrupted.  */
  1014.   if (fieldval & ~((1<<bitsize)-1))
  1015.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  1016.   
  1017.   bcopy (addr, &oword, sizeof oword);
  1018.  
  1019.   /* Shifting for bit field depends on endianness of the machine.  */
  1020. #ifdef BITS_BIG_ENDIAN
  1021.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  1022. #endif
  1023.  
  1024.   oword &= ~(((1 << bitsize) - 1) << bitpos);
  1025.   oword |= fieldval << bitpos;
  1026.   bcopy (&oword, addr, sizeof oword);
  1027. }
  1028.  
  1029. /* Convert C numbers into newly allocated values */
  1030.  
  1031. value
  1032. value_from_long (type, num)
  1033.      struct type *type;
  1034.      register LONGEST num;
  1035. {
  1036.   register value val = allocate_value (type);
  1037.   register enum type_code code = TYPE_CODE (type);
  1038.   register int len = TYPE_LENGTH (type);
  1039.  
  1040.   if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
  1041.     {
  1042.       if (len == sizeof (char))
  1043.     * (char *) VALUE_CONTENTS (val) = num;
  1044.       else if (len == sizeof (short))
  1045.     * (short *) VALUE_CONTENTS (val) = num;
  1046.       else if (len == sizeof (int))
  1047.     * (int *) VALUE_CONTENTS (val) = num;
  1048.       else if (len == sizeof (long))
  1049.     * (long *) VALUE_CONTENTS (val) = num;
  1050. #ifdef LONG_LONG
  1051.       else if (len == sizeof (long long))
  1052.     * (long long *) VALUE_CONTENTS (val) = num;
  1053. #endif
  1054.       else
  1055.     error ("Integer type encountered with unexpected data length.");
  1056.     }
  1057.   else
  1058.     error ("Unexpected type encountered for integer constant.");
  1059.  
  1060.   return val;
  1061. }
  1062.  
  1063. value
  1064. value_from_double (type, num)
  1065.      struct type *type;
  1066.      double num;
  1067. {
  1068.   register value val = allocate_value (type);
  1069.   register enum type_code code = TYPE_CODE (type);
  1070.   register int len = TYPE_LENGTH (type);
  1071.  
  1072.   if (code == TYPE_CODE_FLT)
  1073.     {
  1074.       if (len == sizeof (float))
  1075.     * (float *) VALUE_CONTENTS (val) = num;
  1076.       else if (len == sizeof (double))
  1077.     * (double *) VALUE_CONTENTS (val) = num;
  1078. #ifdef LONG_DOUBLE
  1079.       else if (len == sizeof (long double))
  1080.     *(long double *) VALUE_CONTENTS (val) = num;
  1081. #endif
  1082.       else
  1083.     error ("Floating type encountered with unexpected data length.");
  1084.     }
  1085.   else
  1086.     error ("Unexpected type encountered for floating constant.");
  1087.  
  1088.   return val;
  1089. }
  1090.  
  1091. /* Deal with the value that is "about to be returned".  */
  1092.  
  1093. /* Return the value that a function returning now
  1094.    would be returning to its caller, assuming its type is VALTYPE.
  1095.    RETBUF is where we look for what ought to be the contents
  1096.    of the registers (in raw form).  This is because it is often
  1097.    desirable to restore old values to those registers
  1098.    after saving the contents of interest, and then call
  1099.    this function using the saved values.
  1100.    struct_return is non-zero when the function in question is
  1101.    using the structure return conventions on the machine in question;
  1102.    0 when it is using the value returning conventions (this often
  1103.    means returning pointer to where structure is vs. returning value). */
  1104.  
  1105. value
  1106. value_being_returned (valtype, retbuf, struct_return)
  1107.      register struct type *valtype;
  1108.      char retbuf[REGISTER_BYTES];
  1109.      int struct_return;
  1110. {
  1111.   register value val;
  1112.  
  1113.   if (struct_return)
  1114.     return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf));
  1115.  
  1116.   val = allocate_value (valtype);
  1117.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val));
  1118.  
  1119.   return val;
  1120. }
  1121.  
  1122. /* Return true if the function specified is using the structure returning
  1123.    convention on this machine to return arguments, or 0 if it is using
  1124.    the value returning convention.  FUNCTION is the value representing
  1125.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1126.    is the type returned by the function */
  1127.  
  1128. struct block *block_for_pc ();
  1129.  
  1130. int
  1131. using_struct_return (function, funcaddr, value_type)
  1132.      value function;
  1133.      CORE_ADDR funcaddr;
  1134.      struct type *value_type;
  1135. {
  1136.   register enum type_code code = TYPE_CODE (value_type);
  1137.  
  1138.   if (code == TYPE_CODE_STRUCT ||
  1139.       code == TYPE_CODE_UNION ||
  1140.       code == TYPE_CODE_ARRAY)
  1141.     {
  1142.       struct block *b = block_for_pc (funcaddr);
  1143.  
  1144.       if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8))
  1145.     return 1;
  1146.     }
  1147.  
  1148.   return 0;
  1149. }
  1150.  
  1151. /* Store VAL so it will be returned if a function returns now.
  1152.    Does not verify that VAL's type matches what the current
  1153.    function wants to return.  */
  1154.  
  1155. void
  1156. set_return_value (val)
  1157.      value val;
  1158. {
  1159.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1160.   char regbuf[REGISTER_BYTES];
  1161.   double dbuf;
  1162.   LONGEST lbuf;
  1163.  
  1164.   if (code == TYPE_CODE_STRUCT
  1165.       || code == TYPE_CODE_UNION)
  1166.     error ("Specifying a struct or union return value is not supported.");
  1167.  
  1168.   if (code == TYPE_CODE_FLT)
  1169.     {
  1170.       dbuf = value_as_double (val);
  1171.  
  1172.       STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf);
  1173.     }
  1174.   else
  1175.     {
  1176.       lbuf = value_as_long (val);
  1177.       STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf);
  1178.     }
  1179. }
  1180.  
  1181. void
  1182. _initialize_values ()
  1183. {
  1184.   add_info ("convenience", convenience_info,
  1185.         "Debugger convenience (\"$foo\") variables.\n\
  1186. These variables are created when you assign them values;\n\
  1187. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1188. A few convenience variables are given values automatically GDB:\n\
  1189. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1190. \"$__\" holds the contents of the last address examined with \"x\".");
  1191.  
  1192.   add_info ("values", value_history_info,
  1193.         "Elements of value history (around item number IDX, or last ten).");
  1194.   add_info_alias ("history", value_history_info, 0);
  1195. }
  1196.