home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / expread.y < prev    next >
Text File  |  1993-08-18  |  43KB  |  1,828 lines

  1. /* Parse C expressions for GDB.
  2.    Copyright (C) 1986, 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. /* Parse a C expression from text in a string,
  21.    and return the result as a  struct expression  pointer.
  22.    That structure contains arithmetic operations in reverse polish,
  23.    with constants represented by operations that are followed by special data.
  24.    See expression.h for the details of the format.
  25.    What is important here is that it can be built up sequentially
  26.    during the process of parsing; the lower levels of the tree always
  27.    come first in the result.  */
  28.    
  29. %{
  30. #include <stdio.h>
  31. #include "defs.h"
  32. #include "param.h"
  33. #include "symtab.h"
  34. #include "frame.h"
  35. #include "expression.h"
  36.  
  37. #ifdef atarist
  38. #include "gnu-out.h"
  39. #else
  40. #include <a.out.h>
  41. #endif
  42.  
  43. static struct expression *expout;
  44. static int expout_size;
  45. static int expout_ptr;
  46.  
  47. static int yylex ();
  48. static void yyerror ();
  49. static void write_exp_elt ();
  50. static void write_exp_elt_opcode ();
  51. static void write_exp_elt_sym ();
  52. static void write_exp_elt_longcst ();
  53. static void write_exp_elt_dblcst ();
  54. static void write_exp_elt_type ();
  55. static void write_exp_elt_intern ();
  56. static void write_exp_string ();
  57. static void start_arglist ();
  58. static int end_arglist ();
  59. static void free_funcalls ();
  60. static char *copy_name ();
  61.  
  62. /* If this is nonzero, this block is used as the lexical context
  63.    for symbol names.  */
  64.  
  65. static struct block *expression_context_block;
  66.  
  67. /* The innermost context required by the stack and register variables
  68.    we've encountered so far. */
  69. struct block *innermost_block;
  70.  
  71. /* The block in which the most recently discovered symbol was found. */
  72. struct block *block_found;
  73.  
  74. /* Number of arguments seen so far in innermost function call.  */
  75. static int arglist_len;
  76.  
  77. /* Data structure for saving values of arglist_len
  78.    for function calls whose arguments contain other function calls.  */
  79.  
  80. struct funcall
  81.   {
  82.     struct funcall *next;
  83.     int arglist_len;
  84.   };
  85.  
  86. struct funcall *funcall_chain;
  87.  
  88. /* This kind of datum is used to represent the name
  89.    of a symbol token.  */
  90.  
  91. struct stoken
  92.   {
  93.     char *ptr;
  94.     int length;
  95.   };
  96.  
  97. /* For parsing of complicated types.
  98.    An array should be preceded in the list by the size of the array.  */
  99. enum type_pieces
  100.   {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function};
  101. static enum type_pieces *type_stack;
  102. static int type_stack_depth, type_stack_size;
  103.  
  104. static void push_type ();
  105. static enum type_pieces pop_type ();
  106.  
  107. /* Allow debugging of parsing.  */
  108. #define YYDEBUG 1
  109. %}
  110.  
  111. /* Although the yacc "value" of an expression is not used,
  112.    since the result is stored in the structure being created,
  113.    other node types do have values.  */
  114.  
  115. %union
  116.   {
  117.     LONGEST lval;
  118.     unsigned LONGEST ulval;
  119.     double dval;
  120.     struct symbol *sym;
  121.     struct type *tval;
  122.     struct stoken sval;
  123.     int voidval;
  124.     struct block *bval;
  125.     enum exp_opcode opcode;
  126.     struct internalvar *ivar;
  127.  
  128.     struct type **tvec;
  129.     int *ivec;
  130.   }
  131.  
  132. %type <voidval> exp exp1 start variable
  133. %type <tval> type typebase
  134. %type <tvec> nonempty_typelist
  135. %type <bval> block
  136.  
  137. /* Fancy type parsing.  */
  138. %type <voidval> func_mod direct_abs_decl abs_decl
  139. %type <tval> ptype
  140. %type <lval> array_mod
  141.  
  142. %token <lval> INT CHAR
  143. %token <ulval> UINT
  144. %token <dval> FLOAT
  145.  
  146. /* Both NAME and TYPENAME tokens represent symbols in the input,
  147.    and both convey their data as strings.
  148.    But a TYPENAME is a string that happens to be defined as a typedef
  149.    or builtin type name (such as int or char)
  150.    and a NAME is any other symbol.
  151.  
  152.    Contexts where this distinction is not important can use the
  153.    nonterminal "name", which matches either NAME or TYPENAME.  */
  154.  
  155. %token <sval> NAME TYPENAME BLOCKNAME STRING
  156. %type <sval> name name_not_typename typename
  157.  
  158. %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
  159.  
  160. /* Special type cases, put in to allow the parser to distinguish different
  161.    legal basetypes.  */
  162. %token SIGNED LONG SHORT INT_KEYWORD DOUBLE
  163.  
  164. %token <lval> LAST REGNAME
  165.  
  166. %token <ivar> VARIABLE
  167.  
  168. %token <opcode> ASSIGN_MODIFY
  169.  
  170. /* C++ */
  171. %token THIS
  172.  
  173. %left ','
  174. %left ABOVE_COMMA
  175. %right '=' ASSIGN_MODIFY
  176. %right '?'
  177. %left OR
  178. %left AND
  179. %left '|'
  180. %left '^'
  181. %left '&'
  182. %left EQUAL NOTEQUAL
  183. %left '<' '>' LEQ GEQ
  184. %left LSH RSH
  185. %left '@'
  186. %left '+' '-'
  187. %left '*' '/' '%'
  188. %right UNARY INCREMENT DECREMENT
  189. %right ARROW '.' '[' '('
  190. %left COLONCOLON
  191.  
  192. %%
  193.  
  194. start   :    exp1
  195.     ;
  196.  
  197. /* Expressions, including the comma operator.  */
  198. exp1    :    exp
  199.     |    exp1 ',' exp
  200.             { write_exp_elt_opcode (BINOP_COMMA); }
  201.     ;
  202.  
  203. /* Expressions, not including the comma operator.  */
  204. exp    :    '*' exp    %prec UNARY
  205.             { write_exp_elt_opcode (UNOP_IND); }
  206.  
  207. exp    :    '&' exp    %prec UNARY
  208.             { write_exp_elt_opcode (UNOP_ADDR); }
  209.  
  210. exp    :    '-' exp    %prec UNARY
  211.             { write_exp_elt_opcode (UNOP_NEG); }
  212.     ;
  213.  
  214. exp    :    '!' exp    %prec UNARY
  215.             { write_exp_elt_opcode (UNOP_ZEROP); }
  216.     ;
  217.  
  218. exp    :    '~' exp    %prec UNARY
  219.             { write_exp_elt_opcode (UNOP_LOGNOT); }
  220.     ;
  221.  
  222. exp    :    INCREMENT exp    %prec UNARY
  223.             { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  224.     ;
  225.  
  226. exp    :    DECREMENT exp    %prec UNARY
  227.             { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  228.     ;
  229.  
  230. exp    :    exp INCREMENT    %prec UNARY
  231.             { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  232.     ;
  233.  
  234. exp    :    exp DECREMENT    %prec UNARY
  235.             { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  236.     ;
  237.  
  238. exp    :    SIZEOF exp       %prec UNARY
  239.             { write_exp_elt_opcode (UNOP_SIZEOF); }
  240.     ;
  241.  
  242. exp    :    exp ARROW name
  243.             { write_exp_elt_opcode (STRUCTOP_PTR);
  244.               write_exp_string ($3);
  245.               write_exp_elt_opcode (STRUCTOP_PTR); }
  246.     ;
  247.  
  248. exp    :    exp ARROW '*' exp
  249.             { write_exp_elt_opcode (STRUCTOP_MPTR); }
  250.     ;
  251.  
  252. exp    :    exp '.' name
  253.             { write_exp_elt_opcode (STRUCTOP_STRUCT);
  254.               write_exp_string ($3);
  255.               write_exp_elt_opcode (STRUCTOP_STRUCT); }
  256.     ;
  257.  
  258. exp    :    exp '.' '*' exp
  259.             { write_exp_elt_opcode (STRUCTOP_MEMBER); }
  260.     ;
  261.  
  262. exp    :    exp '[' exp1 ']'
  263.             { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  264.     ;
  265.  
  266. exp    :    exp '(' 
  267.             /* This is to save the value of arglist_len
  268.                being accumulated by an outer function call.  */
  269.             { start_arglist (); }
  270.         arglist ')'    %prec ARROW
  271.             { write_exp_elt_opcode (OP_FUNCALL);
  272.               write_exp_elt_longcst ((LONGEST) end_arglist ());
  273.               write_exp_elt_opcode (OP_FUNCALL); }
  274.     ;
  275.  
  276. arglist    :
  277.     ;
  278.  
  279. arglist    :    exp
  280.             { arglist_len = 1; }
  281.     ;
  282.  
  283. arglist    :    arglist ',' exp   %prec ABOVE_COMMA
  284.             { arglist_len++; }
  285.     ;
  286.  
  287. exp    :    '{' type '}' exp  %prec UNARY
  288.             { write_exp_elt_opcode (UNOP_MEMVAL);
  289.               write_exp_elt_type ($2);
  290.               write_exp_elt_opcode (UNOP_MEMVAL); }
  291.     ;
  292.  
  293. exp    :    '(' type ')' exp  %prec UNARY
  294.             { write_exp_elt_opcode (UNOP_CAST);
  295.               write_exp_elt_type ($2);
  296.               write_exp_elt_opcode (UNOP_CAST); }
  297.     ;
  298.  
  299. exp    :    '(' exp1 ')'
  300.             { }
  301.     ;
  302.  
  303. /* Binary operators in order of decreasing precedence.  */
  304.  
  305. exp    :    exp '@' exp
  306.             { write_exp_elt_opcode (BINOP_REPEAT); }
  307.     ;
  308.  
  309. exp    :    exp '*' exp
  310.             { write_exp_elt_opcode (BINOP_MUL); }
  311.     ;
  312.  
  313. exp    :    exp '/' exp
  314.             { write_exp_elt_opcode (BINOP_DIV); }
  315.     ;
  316.  
  317. exp    :    exp '%' exp
  318.             { write_exp_elt_opcode (BINOP_REM); }
  319.     ;
  320.  
  321. exp    :    exp '+' exp
  322.             { write_exp_elt_opcode (BINOP_ADD); }
  323.     ;
  324.  
  325. exp    :    exp '-' exp
  326.             { write_exp_elt_opcode (BINOP_SUB); }
  327.     ;
  328.  
  329. exp    :    exp LSH exp
  330.             { write_exp_elt_opcode (BINOP_LSH); }
  331.     ;
  332.  
  333. exp    :    exp RSH exp
  334.             { write_exp_elt_opcode (BINOP_RSH); }
  335.     ;
  336.  
  337. exp    :    exp EQUAL exp
  338.             { write_exp_elt_opcode (BINOP_EQUAL); }
  339.     ;
  340.  
  341. exp    :    exp NOTEQUAL exp
  342.             { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  343.     ;
  344.  
  345. exp    :    exp LEQ exp
  346.             { write_exp_elt_opcode (BINOP_LEQ); }
  347.     ;
  348.  
  349. exp    :    exp GEQ exp
  350.             { write_exp_elt_opcode (BINOP_GEQ); }
  351.     ;
  352.  
  353. exp    :    exp '<' exp
  354.             { write_exp_elt_opcode (BINOP_LESS); }
  355.     ;
  356.  
  357. exp    :    exp '>' exp
  358.             { write_exp_elt_opcode (BINOP_GTR); }
  359.     ;
  360.  
  361. exp    :    exp '&' exp
  362.             { write_exp_elt_opcode (BINOP_LOGAND); }
  363.     ;
  364.  
  365. exp    :    exp '^' exp
  366.             { write_exp_elt_opcode (BINOP_LOGXOR); }
  367.     ;
  368.  
  369. exp    :    exp '|' exp
  370.             { write_exp_elt_opcode (BINOP_LOGIOR); }
  371.     ;
  372.  
  373. exp    :    exp AND exp
  374.             { write_exp_elt_opcode (BINOP_AND); }
  375.     ;
  376.  
  377. exp    :    exp OR exp
  378.             { write_exp_elt_opcode (BINOP_OR); }
  379.     ;
  380.  
  381. exp    :    exp '?' exp ':' exp    %prec '?'
  382.             { write_exp_elt_opcode (TERNOP_COND); }
  383.     ;
  384.               
  385. exp    :    exp '=' exp
  386.             { write_exp_elt_opcode (BINOP_ASSIGN); }
  387.     ;
  388.  
  389. exp    :    exp ASSIGN_MODIFY exp
  390.             { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  391.               write_exp_elt_opcode ($2);
  392.               write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  393.     ;
  394.  
  395. exp    :    INT
  396.             { write_exp_elt_opcode (OP_LONG);
  397. #ifdef atarist
  398.               if (gcc_mshort && ($1 == (short) $1
  399.                          || $1 == (unsigned short) $1))
  400.                 write_exp_elt_type (builtin_type_short);
  401.               else
  402. #endif
  403.               if ($1 == (int) $1 || $1 == (unsigned int) $1)
  404.                 write_exp_elt_type (builtin_type_int);
  405. #ifdef LONG_LONG
  406.               else if ($1 == (long) $1 || $1 == (unsigned long) $1)
  407.                 write_exp_elt_type (builtin_type_long);
  408. #endif
  409.               else
  410.                 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
  411.               write_exp_elt_longcst ((LONGEST) $1);
  412.               write_exp_elt_opcode (OP_LONG); }
  413.     ;
  414.  
  415. exp    :    UINT
  416.             {
  417.               write_exp_elt_opcode (OP_LONG);
  418. #ifdef atarist
  419.               if (gcc_mshort && $1 == (unsigned short) $1)
  420.                 write_exp_elt_type (builtin_type_unsigned_short);
  421.               else
  422. #endif
  423.               if ($1 == (unsigned int) $1)
  424.                 write_exp_elt_type (builtin_type_unsigned_int);
  425. #ifdef LONG_LONG
  426.               else if ($1 == (unsigned long) $1)
  427.                 write_exp_elt_type (builtin_type_unsigned_long);
  428. #endif
  429.               else
  430.                 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
  431.               write_exp_elt_longcst ((LONGEST) $1);
  432.               write_exp_elt_opcode (OP_LONG);
  433.             }
  434.     ;
  435.  
  436. exp    :    CHAR
  437.             { write_exp_elt_opcode (OP_LONG);
  438.               write_exp_elt_type (builtin_type_char);
  439.               write_exp_elt_longcst ((LONGEST) $1);
  440.               write_exp_elt_opcode (OP_LONG); }
  441.     ;
  442.  
  443. exp    :    FLOAT
  444.             { write_exp_elt_opcode (OP_DOUBLE);
  445.               write_exp_elt_type (builtin_type_double);
  446.               write_exp_elt_dblcst ($1);
  447.               write_exp_elt_opcode (OP_DOUBLE); }
  448.     ;
  449.  
  450. exp    :    variable
  451.     ;
  452.  
  453. exp    :    LAST
  454.             { write_exp_elt_opcode (OP_LAST);
  455.               write_exp_elt_longcst ((LONGEST) $1);
  456.               write_exp_elt_opcode (OP_LAST); }
  457.     ;
  458.  
  459. exp    :    REGNAME
  460.             { write_exp_elt_opcode (OP_REGISTER);
  461.               write_exp_elt_longcst ((LONGEST) $1);
  462.               write_exp_elt_opcode (OP_REGISTER); }
  463.     ;
  464.  
  465. exp    :    VARIABLE
  466.             { write_exp_elt_opcode (OP_INTERNALVAR);
  467.               write_exp_elt_intern ($1);
  468.               write_exp_elt_opcode (OP_INTERNALVAR); }
  469.     ;
  470.  
  471. exp    :    SIZEOF '(' type ')'    %prec UNARY
  472.             { write_exp_elt_opcode (OP_LONG);
  473.               write_exp_elt_type (builtin_type_int);
  474.               write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  475.               write_exp_elt_opcode (OP_LONG); }
  476.     ;
  477.  
  478. exp    :    STRING
  479.             { write_exp_elt_opcode (OP_STRING);
  480.               write_exp_string ($1);
  481.               write_exp_elt_opcode (OP_STRING); }
  482.     ;
  483.  
  484. /* C++.  */
  485. exp    :    THIS
  486.             { write_exp_elt_opcode (OP_THIS);
  487.               write_exp_elt_opcode (OP_THIS); }
  488.     ;
  489.  
  490. /* end of C++.  */
  491.  
  492. block    :    BLOCKNAME
  493.             {
  494.               struct symtab *tem = lookup_symtab (copy_name ($1));
  495.               struct symbol *sym;
  496.               
  497.               if (tem)
  498.                 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
  499.               else
  500.                 {
  501.                   sym = lookup_symbol (copy_name ($1),
  502.                            expression_context_block,
  503.                            VAR_NAMESPACE, 0);
  504.                   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  505.                 $$ = SYMBOL_BLOCK_VALUE (sym);
  506.                   else
  507.                 error ("No file or function \"%s\".",
  508.                        copy_name ($1));
  509.                 }
  510.             }
  511.     ;
  512.  
  513. block    :    block COLONCOLON name
  514.             { struct symbol *tem
  515.                 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
  516.               if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  517.                 error ("No function \"%s\" in specified context.",
  518.                    copy_name ($3));
  519.               $$ = SYMBOL_BLOCK_VALUE (tem); }
  520.     ;
  521.  
  522. variable:    block COLONCOLON name
  523.             { struct symbol *sym;
  524.               sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
  525.               if (sym == 0)
  526.                 error ("No symbol \"%s\" in specified context.",
  527.                    copy_name ($3));
  528.               write_exp_elt_opcode (OP_VAR_VALUE);
  529.               write_exp_elt_sym (sym);
  530.               write_exp_elt_opcode (OP_VAR_VALUE); }
  531.     ;
  532.  
  533. variable:    typebase COLONCOLON name
  534.             {
  535.               struct type *type = $1;
  536.               if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  537.                   && TYPE_CODE (type) != TYPE_CODE_UNION)
  538.                 error ("`%s' is not defined as an aggregate type.",
  539.                    TYPE_NAME (type));
  540.  
  541.               write_exp_elt_opcode (OP_SCOPE);
  542.               write_exp_elt_type (type);
  543.               write_exp_string ($3);
  544.               write_exp_elt_opcode (OP_SCOPE);
  545.             }
  546.     |    COLONCOLON name
  547.             {
  548.               char *name = copy_name ($2);
  549.               struct symbol *sym;
  550.               int i;
  551.  
  552.               sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0);
  553.               if (sym)
  554.                 {
  555.                   write_exp_elt_opcode (OP_VAR_VALUE);
  556.                   write_exp_elt_sym (sym);
  557.                   write_exp_elt_opcode (OP_VAR_VALUE);
  558.                   break;
  559.                 }
  560.               for (i = 0; i < misc_function_count; i++)
  561.                 if (!strcmp (misc_function_vector[i].name, name))
  562.                   break;
  563.  
  564.               if (i < misc_function_count)
  565.                 {
  566.                   enum misc_function_type mft =
  567.                 (enum misc_function_type)
  568.                   misc_function_vector[i].type;
  569.                   
  570.                   write_exp_elt_opcode (OP_LONG);
  571.                   write_exp_elt_type (builtin_type_int);
  572.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  573.                   write_exp_elt_opcode (OP_LONG);
  574.                   write_exp_elt_opcode (UNOP_MEMVAL);
  575.                   if (mft == mf_data || mft == mf_bss)
  576.                 write_exp_elt_type (builtin_type_int);
  577.                   else if (mft == mf_text)
  578.                 write_exp_elt_type (lookup_function_type (builtin_type_int));
  579.                   else
  580.                 write_exp_elt_type (builtin_type_char);
  581.                   write_exp_elt_opcode (UNOP_MEMVAL);
  582.                 }
  583.               else
  584.                 if (symtab_list == 0
  585.                 && partial_symtab_list == 0)
  586.                   error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  587.                 else
  588.                   error ("No symbol \"%s\" in current context.", name);
  589.             }
  590.     ;
  591.  
  592. variable:    name_not_typename
  593.             { struct symbol *sym;
  594.               int is_a_field_of_this;
  595.  
  596.               sym = lookup_symbol (copy_name ($1),
  597.                            expression_context_block,
  598.                            VAR_NAMESPACE,
  599.                            &is_a_field_of_this);
  600.               if (sym)
  601.                 {
  602.                   switch (sym->class)
  603.                 {
  604.                 case LOC_REGISTER:
  605.                 case LOC_ARG:
  606.                 case LOC_LOCAL:
  607.                   if (innermost_block == 0 ||
  608.                       contained_in (block_found, 
  609.                             innermost_block))
  610.                     innermost_block = block_found;
  611.                 }
  612.                   write_exp_elt_opcode (OP_VAR_VALUE);
  613.                   write_exp_elt_sym (sym);
  614.                   write_exp_elt_opcode (OP_VAR_VALUE);
  615.                 }
  616.               else if (is_a_field_of_this)
  617.                 {
  618.                   /* C++: it hangs off of `this'.  Must
  619.                      not inadvertently convert from a method call
  620.                  to data ref.  */
  621.                   if (innermost_block == 0 || 
  622.                   contained_in (block_found, innermost_block))
  623.                 innermost_block = block_found;
  624.                   write_exp_elt_opcode (OP_THIS);
  625.                   write_exp_elt_opcode (OP_THIS);
  626.                   write_exp_elt_opcode (STRUCTOP_PTR);
  627.                   write_exp_string ($1);
  628.                   write_exp_elt_opcode (STRUCTOP_PTR);
  629.                 }
  630.               else
  631.                 {
  632.                   register int i;
  633.                   register char *arg = copy_name ($1);
  634.  
  635.                   for (i = 0; i < misc_function_count; i++)
  636.                 if (!strcmp (misc_function_vector[i].name, arg))
  637.                   break;
  638.  
  639.                   if (i < misc_function_count)
  640.                 {
  641.                   enum misc_function_type mft =
  642.                     (enum misc_function_type)
  643.                       misc_function_vector[i].type;
  644.                   
  645.                   write_exp_elt_opcode (OP_LONG);
  646.                   write_exp_elt_type (builtin_type_int);
  647.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  648.                   write_exp_elt_opcode (OP_LONG);
  649.                   write_exp_elt_opcode (UNOP_MEMVAL);
  650.                   if (mft == mf_data || mft == mf_bss)
  651.                     write_exp_elt_type (builtin_type_int);
  652.                   else if (mft == mf_text)
  653.                     write_exp_elt_type (lookup_function_type (builtin_type_int));
  654.                   else
  655.                     write_exp_elt_type (builtin_type_char);
  656.                   write_exp_elt_opcode (UNOP_MEMVAL);
  657.                 }
  658.                   else if (symtab_list == 0
  659.                        && partial_symtab_list == 0)
  660.                 error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  661.                   else
  662.                 error ("No symbol \"%s\" in current context.",
  663.                        copy_name ($1));
  664.                 }
  665.             }
  666.     ;
  667.  
  668.  
  669. ptype    :    typebase
  670.     |    typebase abs_decl
  671.         {
  672.           /* This is where the interesting stuff happens.  */
  673.           int done = 0;
  674.           int array_size;
  675.           struct type *follow_type = $1;
  676.           
  677.           while (!done)
  678.             switch (pop_type ())
  679.               {
  680.               case tp_end:
  681.             done = 1;
  682.             break;
  683.               case tp_pointer:
  684.             follow_type = lookup_pointer_type (follow_type);
  685.             break;
  686.               case tp_reference:
  687.             follow_type = lookup_reference_type (follow_type);
  688.             break;
  689.               case tp_array:
  690.             array_size = (int) pop_type ();
  691.             if (array_size != -1)
  692.               follow_type = create_array_type (follow_type,
  693.                                array_size);
  694.             else
  695.               follow_type = lookup_pointer_type (follow_type);
  696.             break;
  697.               case tp_function:
  698.             follow_type = lookup_function_type (follow_type);
  699.             break;
  700.               }
  701.           $$ = follow_type;
  702.         }
  703.     ;
  704.  
  705. abs_decl:    '*'
  706.             { push_type (tp_pointer); $$ = 0; }
  707.     |    '*' abs_decl
  708.             { push_type (tp_pointer); $$ = $2; }
  709.     |    direct_abs_decl
  710.     ;
  711.  
  712. direct_abs_decl: '(' abs_decl ')'
  713.             { $$ = $2; }
  714.     |    direct_abs_decl array_mod
  715.             {
  716.               push_type ((enum type_pieces) $2);
  717.               push_type (tp_array);
  718.             }
  719.     |    array_mod
  720.             {
  721.               push_type ((enum type_pieces) $1);
  722.               push_type (tp_array);
  723.               $$ = 0;
  724.             }
  725.     |     direct_abs_decl func_mod
  726.             { push_type (tp_function); }
  727.     |    func_mod
  728.             { push_type (tp_function); }
  729.     ;
  730.  
  731. array_mod:    '[' ']'
  732.             { $$ = -1; }
  733.     |    '[' INT ']'
  734.             { $$ = $2; }
  735.     ;
  736.  
  737. func_mod:    '(' ')'
  738.             { $$ = 0; }
  739.     ;
  740.  
  741. type    :    ptype
  742.     |    typebase COLONCOLON '*'
  743.             { $$ = lookup_member_type (builtin_type_int, $1); }
  744.     |    type '(' typebase COLONCOLON '*' ')'
  745.             { $$ = lookup_member_type ($1, $3); }
  746.     |    type '(' typebase COLONCOLON '*' ')' '(' ')'
  747.             { $$ = lookup_member_type
  748.                 (lookup_function_type ($1), $3); }
  749.     |    type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
  750.             { $$ = lookup_member_type
  751.                 (lookup_function_type ($1), $3);
  752.               free ($8); }
  753.     ;
  754.  
  755. typebase
  756.     :    TYPENAME
  757.             { $$ = lookup_typename (copy_name ($1),
  758.                         expression_context_block, 0); }
  759.     |    INT_KEYWORD
  760.             { $$ = builtin_type_int; }
  761.     |    LONG
  762.             { $$ = builtin_type_long; }
  763.     |    LONG LONG
  764.             {
  765. #ifdef LONG_LONG
  766.               $$ = builtin_type_long_long;
  767. #else
  768.               $$ = lookup_typename ("long long",
  769.                         expression_context_block, 0);
  770. #endif
  771.             }
  772.     |    SHORT
  773.             { $$ = builtin_type_short; }
  774.     |    LONG INT_KEYWORD
  775.             { $$ = builtin_type_long; }
  776.     |    LONG LONG INT_KEYWORD
  777.             {
  778. #ifdef LONG_LONG
  779.               $$ = builtin_type_long_long;
  780. #else
  781.               $$ = lookup_typename ("long long int",
  782.                         expression_context_block, 0);
  783. #endif
  784.             }
  785.     |    UNSIGNED LONG INT_KEYWORD
  786.             { $$ = builtin_type_unsigned_long; }
  787.     |    UNSIGNED LONG LONG INT_KEYWORD
  788.             {
  789. #ifdef LONG_LONG
  790.               $$ = builtin_type_unsigned_long_long;
  791. #else
  792.               $$ = lookup_typename ("unsigned long long int",
  793.                         expression_context_block, 0);
  794. #endif
  795.             }
  796.     |    SHORT INT_KEYWORD
  797.             { $$ = builtin_type_short; }
  798.     |    UNSIGNED SHORT INT_KEYWORD
  799.             { $$ = builtin_type_unsigned_short; }
  800.     |    DOUBLE
  801.             { $$ = builtin_type_double; }
  802.     |    LONG DOUBLE
  803.             {
  804. #ifdef LONG_DOUBLE
  805.               $$ = builtin_type_long_double;
  806. #else
  807.               $$ = lookup_typename ("long double",
  808.                         expression_context_block, 0);
  809. #endif
  810.             }
  811.     |    STRUCT name
  812.             { $$ = lookup_struct (copy_name ($2),
  813.                           expression_context_block); }
  814.     |    UNION name
  815.             { $$ = lookup_union (copy_name ($2),
  816.                          expression_context_block); }
  817.     |    ENUM name
  818.             { $$ = lookup_enum (copy_name ($2),
  819.                         expression_context_block); }
  820.     |    UNSIGNED typename
  821.             { $$ = lookup_unsigned_typename (copy_name ($2)); }
  822.     |    UNSIGNED
  823.             { $$ = builtin_type_unsigned_int; }
  824.     |    SIGNED typename
  825.             { $$ = lookup_typename (copy_name ($2),
  826.                         expression_context_block, 0); }
  827.     |    SIGNED
  828.             { $$ = builtin_type_int; }
  829.     ;
  830.  
  831. typename:    TYPENAME
  832.     |    INT_KEYWORD
  833.         {
  834.           $$.ptr = "int";
  835.           $$.length = 3;
  836.         }
  837.     |    LONG
  838.         {
  839.           $$.ptr = "long";
  840.           $$.length = 4;
  841.         }
  842.     |    SHORT
  843.         {
  844.           $$.ptr = "short";
  845.           $$.length = 5;
  846.         }
  847.     ;
  848.  
  849. nonempty_typelist
  850.     :    type
  851.         { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
  852.           $$[0] = (struct type *)0;
  853.           $$[1] = $1;
  854.         }
  855.     |    nonempty_typelist ',' type
  856.         { int len = sizeof (struct type *) * ++($<ivec>1[0]);
  857.           $$ = (struct type **)xrealloc ($1, len);
  858.           $$[$<ivec>$[0]] = $3;
  859.         }
  860.     ;
  861.  
  862. name    :    NAME
  863.     |    BLOCKNAME
  864.     |    TYPENAME
  865.     ;
  866.  
  867. name_not_typename :    NAME
  868.     |    BLOCKNAME
  869.     ;
  870.  
  871. %%
  872.  
  873. /* Begin counting arguments for a function call,
  874.    saving the data about any containing call.  */
  875.  
  876. static void
  877. start_arglist ()
  878. {
  879.   register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
  880.  
  881.   new->next = funcall_chain;
  882.   new->arglist_len = arglist_len;
  883.   arglist_len = 0;
  884.   funcall_chain = new;
  885. }
  886.  
  887. /* Return the number of arguments in a function call just terminated,
  888.    and restore the data for the containing function call.  */
  889.  
  890. static int
  891. end_arglist ()
  892. {
  893.   register int val = arglist_len;
  894.   register struct funcall *call = funcall_chain;
  895.   funcall_chain = call->next;
  896.   arglist_len = call->arglist_len;
  897.   free (call);
  898.   return val;
  899. }
  900.  
  901. /* Free everything in the funcall chain.
  902.    Used when there is an error inside parsing.  */
  903.  
  904. static void
  905. free_funcalls ()
  906. {
  907.   register struct funcall *call, *next;
  908.  
  909.   for (call = funcall_chain; call; call = next)
  910.     {
  911.       next = call->next;
  912.       free (call);
  913.     }
  914. }
  915.  
  916. /* This page contains the functions for adding data to the  struct expression
  917.    being constructed.  */
  918.  
  919. /* Add one element to the end of the expression.  */
  920.  
  921. /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
  922.    a register through here */
  923.  
  924. static void
  925. write_exp_elt (expelt)
  926.      union exp_element expelt;
  927. {
  928.   if (expout_ptr >= expout_size)
  929.     {
  930.       expout_size *= 2;
  931.       expout = (struct expression *) xrealloc (expout,
  932.                            sizeof (struct expression)
  933.                            + expout_size * sizeof (union exp_element));
  934.     }
  935.   expout->elts[expout_ptr++] = expelt;
  936. }
  937.  
  938. static void
  939. write_exp_elt_opcode (expelt)
  940.      enum exp_opcode expelt;
  941. {
  942.   union exp_element tmp;
  943.  
  944.   tmp.opcode = expelt;
  945.  
  946.   write_exp_elt (tmp);
  947. }
  948.  
  949. static void
  950. write_exp_elt_sym (expelt)
  951.      struct symbol *expelt;
  952. {
  953.   union exp_element tmp;
  954.  
  955.   tmp.symbol = expelt;
  956.  
  957.   write_exp_elt (tmp);
  958. }
  959.  
  960. static void
  961. write_exp_elt_longcst (expelt)
  962.      LONGEST expelt;
  963. {
  964.   union exp_element tmp;
  965.  
  966.   tmp.longconst = expelt;
  967.  
  968.   write_exp_elt (tmp);
  969. }
  970.  
  971. static void
  972. write_exp_elt_dblcst (expelt)
  973.      double expelt;
  974. {
  975.   union exp_element tmp;
  976.  
  977.   tmp.doubleconst = expelt;
  978.  
  979.   write_exp_elt (tmp);
  980. }
  981.  
  982. static void
  983. write_exp_elt_type (expelt)
  984.      struct type *expelt;
  985. {
  986.   union exp_element tmp;
  987.  
  988.   tmp.type = expelt;
  989.  
  990.   write_exp_elt (tmp);
  991. }
  992.  
  993. static void
  994. write_exp_elt_intern (expelt)
  995.      struct internalvar *expelt;
  996. {
  997.   union exp_element tmp;
  998.  
  999.   tmp.internalvar = expelt;
  1000.  
  1001.   write_exp_elt (tmp);
  1002. }
  1003.  
  1004. /* Add a string constant to the end of the expression.
  1005.    Follow it by its length in bytes, as a separate exp_element.  */
  1006.  
  1007. static void
  1008. write_exp_string (str)
  1009.      struct stoken str;
  1010. {
  1011.   register int len = str.length;
  1012.   register int lenelt
  1013.     = (len + sizeof (union exp_element)) / sizeof (union exp_element);
  1014.  
  1015.   expout_ptr += lenelt;
  1016.  
  1017.   if (expout_ptr >= expout_size)
  1018.     {
  1019.       expout_size = max (expout_size * 2, expout_ptr + 10);
  1020.       expout = (struct expression *)
  1021.     xrealloc (expout, (sizeof (struct expression)
  1022.                + (expout_size * sizeof (union exp_element))));
  1023.     }
  1024.   bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
  1025.   ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
  1026.   write_exp_elt_longcst ((LONGEST) len);
  1027. }
  1028.  
  1029. /* During parsing of a C expression, the pointer to the next character
  1030.    is in this variable.  */
  1031.  
  1032. static char *lexptr;
  1033.  
  1034. /* Tokens that refer to names do so with explicit pointer and length,
  1035.    so they can share the storage that lexptr is parsing.
  1036.  
  1037.    When it is necessary to pass a name to a function that expects
  1038.    a null-terminated string, the substring is copied out
  1039.    into a block of storage that namecopy points to.
  1040.  
  1041.    namecopy is allocated once, guaranteed big enough, for each parsing.  */
  1042.  
  1043. static char *namecopy;
  1044.  
  1045. /* Current depth in parentheses within the expression.  */
  1046.  
  1047. static int paren_depth;
  1048.  
  1049. /* Nonzero means stop parsing on first comma (if not within parentheses).  */
  1050.  
  1051. static int comma_terminates;
  1052.  
  1053. /* Take care of parsing a number (anything that starts with a digit).
  1054.    Set yylval and return the token type; update lexptr.
  1055.    LEN is the number of characters in it.  */
  1056.  
  1057. /*** Needs some error checking for the float case ***/
  1058.  
  1059. static int
  1060. parse_number (olen)
  1061.      int olen;
  1062. {
  1063.   register char *p = lexptr;
  1064.   register LONGEST n = 0;
  1065.   register int c;
  1066.   register int base = 10;
  1067.   register int len = olen;
  1068.   char *err_copy;
  1069.   int unsigned_p = 0;
  1070.  
  1071.   extern double atof ();
  1072.  
  1073.   for (c = 0; c < len; c++)
  1074.     if (p[c] == '.')
  1075.       {
  1076.     /* It's a float since it contains a point.  */
  1077.     yylval.dval = atof (p);
  1078.     lexptr += len;
  1079.     return FLOAT;
  1080.       }
  1081.  
  1082.   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
  1083.     {
  1084.       p += 2;
  1085.       base = 16;
  1086.       len -= 2;
  1087.     }
  1088.   else if (*p == '0')
  1089.     base = 8;
  1090.  
  1091.   while (len-- > 0)
  1092.     {
  1093.       c = *p++;
  1094.       if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
  1095.       if (c != 'l' && c != 'u')
  1096.     n *= base;
  1097.       if (c >= '0' && c <= '9')
  1098.     n += c - '0';
  1099.       else
  1100.     {
  1101.       if (base == 16 && c >= 'a' && c <= 'f')
  1102.         n += c - 'a' + 10;
  1103.       else if (len == 0 && c == 'l')
  1104.         ;
  1105.       else if (len == 0 && c == 'u')
  1106.         unsigned_p = 1;
  1107.       else if (base == 10 && len != 0 && (c == 'e' || c == 'E'))
  1108.         {
  1109.           /* Scientific notation, where we are unlucky enough not
  1110.          to have a '.' in the string.  */
  1111.           yylval.dval = atof (lexptr);
  1112.           lexptr += olen;
  1113.           return FLOAT;
  1114.         }
  1115.       else
  1116.         {
  1117.           err_copy = (char *) alloca (olen + 1);
  1118.           bcopy (lexptr, err_copy, olen);
  1119.           err_copy[olen] = 0;
  1120.           error ("Invalid number \"%s\".", err_copy);
  1121.         }
  1122.     }
  1123.     }
  1124.  
  1125.   lexptr = p;
  1126.   if (unsigned_p)
  1127.     {
  1128.       yylval.ulval = n;
  1129.       return UINT;
  1130.     }
  1131.   else
  1132.     {
  1133.       yylval.lval = n;
  1134.       return INT;
  1135.     }
  1136. }
  1137.  
  1138. struct token
  1139. {
  1140.   char *operator;
  1141.   int token;
  1142.   enum exp_opcode opcode;
  1143. };
  1144.  
  1145. static struct token tokentab3[] =
  1146.   {
  1147.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  1148.     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  1149.   };
  1150.  
  1151. static struct token tokentab2[] =
  1152.   {
  1153.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  1154.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  1155.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  1156.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  1157.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  1158.     {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
  1159.     {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
  1160.     {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
  1161.     {"++", INCREMENT, BINOP_END},
  1162.     {"--", DECREMENT, BINOP_END},
  1163.     {"->", ARROW, BINOP_END},
  1164.     {"&&", AND, BINOP_END},
  1165.     {"||", OR, BINOP_END},
  1166.     {"::", COLONCOLON, BINOP_END},
  1167.     {"<<", LSH, BINOP_END},
  1168.     {">>", RSH, BINOP_END},
  1169.     {"==", EQUAL, BINOP_END},
  1170.     {"!=", NOTEQUAL, BINOP_END},
  1171.     {"<=", LEQ, BINOP_END},
  1172.     {">=", GEQ, BINOP_END}
  1173.   };
  1174.  
  1175. /* assign machine-independent names to certain registers 
  1176.  * (unless overridden by the REGISTER_NAMES table)
  1177.  */
  1178. struct std_regs {
  1179.     char *name;
  1180.     int regnum;
  1181. } std_regs[] = {
  1182. #ifdef PC_REGNUM
  1183.     { "pc", PC_REGNUM },
  1184. #endif
  1185. #ifdef FP_REGNUM
  1186.     { "fp", FP_REGNUM },
  1187. #endif
  1188. #ifdef SP_REGNUM
  1189.     { "sp", SP_REGNUM },
  1190. #endif
  1191. #ifdef PS_REGNUM
  1192.     { "ps", PS_REGNUM },
  1193. #endif
  1194. };
  1195.  
  1196. #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
  1197.  
  1198. /* Read one token, getting characters through lexptr.  */
  1199.  
  1200. static int
  1201. yylex ()
  1202. {
  1203.   register int c;
  1204.   register int namelen;
  1205.   register int i;
  1206.   register char *tokstart;
  1207.  
  1208.  retry:
  1209.  
  1210.   tokstart = lexptr;
  1211.   /* See if it is a special token of length 3.  */
  1212.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  1213.     if (!strncmp (tokstart, tokentab3[i].operator, 3))
  1214.       {
  1215.     lexptr += 3;
  1216.     yylval.opcode = tokentab3[i].opcode;
  1217.     return tokentab3[i].token;
  1218.       }
  1219.  
  1220.   /* See if it is a special token of length 2.  */
  1221.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  1222.     if (!strncmp (tokstart, tokentab2[i].operator, 2))
  1223.       {
  1224.     lexptr += 2;
  1225.     yylval.opcode = tokentab2[i].opcode;
  1226.     return tokentab2[i].token;
  1227.       }
  1228.  
  1229.   switch (c = *tokstart)
  1230.     {
  1231.     case 0:
  1232.       return 0;
  1233.  
  1234.     case ' ':
  1235.     case '\t':
  1236.     case '\n':
  1237.       lexptr++;
  1238.       goto retry;
  1239.  
  1240.     case '\'':
  1241.       lexptr++;
  1242.       c = *lexptr++;
  1243.       if (c == '\\')
  1244.     c = parse_escape (&lexptr);
  1245.       yylval.lval = c;
  1246.       c = *lexptr++;
  1247.       if (c != '\'')
  1248.     error ("Invalid character constant.");
  1249.       return CHAR;
  1250.  
  1251.     case '(':
  1252.       paren_depth++;
  1253.       lexptr++;
  1254.       return c;
  1255.  
  1256.     case ')':
  1257.       if (paren_depth == 0)
  1258.     return 0;
  1259.       paren_depth--;
  1260.       lexptr++;
  1261.       return c;
  1262.  
  1263.     case ',':
  1264.       if (comma_terminates && paren_depth == 0)
  1265.     return 0;
  1266.       lexptr++;
  1267.       return c;
  1268.  
  1269.     case '.':
  1270.       /* Might be a floating point number.  */
  1271.       if (lexptr[1] >= '0' && lexptr[1] <= '9')
  1272.     break;            /* Falls into number code.  */
  1273.  
  1274.     case '+':
  1275.     case '-':
  1276.     case '*':
  1277.     case '/':
  1278.     case '%':
  1279.     case '|':
  1280.     case '&':
  1281.     case '^':
  1282.     case '~':
  1283.     case '!':
  1284.     case '@':
  1285.     case '<':
  1286.     case '>':
  1287.     case '[':
  1288.     case ']':
  1289.     case '?':
  1290.     case ':':
  1291.     case '=':
  1292.     case '{':
  1293.     case '}':
  1294.       lexptr++;
  1295.       return c;
  1296.  
  1297.     case '"':
  1298.       for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
  1299.     if (c == '\\')
  1300.       {
  1301.         c = tokstart[++namelen];
  1302.         if (c >= '0' && c <= '9')
  1303.           {
  1304.         c = tokstart[++namelen];
  1305.         if (c >= '0' && c <= '9')
  1306.           c = tokstart[++namelen];
  1307.           }
  1308.       }
  1309.       yylval.sval.ptr = tokstart + 1;
  1310.       yylval.sval.length = namelen - 1;
  1311.       lexptr += namelen + 1;
  1312.       return STRING;
  1313.     }
  1314.  
  1315.   /* Is it a number?  */
  1316.   /* Note:  We have already dealt with the case of the token '.'.
  1317.      See case '.' above.  */
  1318.   if ((c >= '0' && c <= '9') || c == '.')
  1319.     {
  1320.       /* It's a number.  */
  1321.       int got_dot = 0, got_e = 0;
  1322.       register char *p = tokstart;
  1323.       int hex = c == '0' && (p[1] == 'x' || p[1] == 'X');
  1324.       if (hex)
  1325.     p += 2;
  1326.       for (;; ++p)
  1327.     {
  1328.       if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1329.         got_dot = got_e = 1;
  1330.       else if (!hex && !got_dot && *p == '.')
  1331.         got_dot = 1;
  1332.       else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1333.            && (*p == '-' || *p == '+'))
  1334.         /* This is the sign of the exponent, not the end of the
  1335.            number.  */
  1336.         continue;
  1337.       else if (*p < '0' || *p > '9'
  1338.            && (!hex || ((*p < 'a' || *p > 'f')
  1339.                 && (*p < 'A' || *p > 'F'))))
  1340.         break;
  1341.     }
  1342.       return parse_number (p - tokstart);
  1343.     }
  1344.  
  1345.   if (!(c == '_' || c == '$'
  1346.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1347.     /* We must have come across a bad character (e.g. ';').  */
  1348.     error ("Invalid character '%c' in expression.", c);
  1349.  
  1350.   /* It's a name.  See how long it is.  */
  1351.   namelen = 0;
  1352.   for (c = tokstart[namelen];
  1353.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1354.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  1355.        c = tokstart[++namelen])
  1356.     ;
  1357.  
  1358.   /* The token "if" terminates the expression and is NOT 
  1359.      removed from the input stream.  */
  1360.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1361.     {
  1362.       return 0;
  1363.     }
  1364.  
  1365.   lexptr += namelen;
  1366.  
  1367.   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  1368.      and $$digits (equivalent to $<-digits> if you could type that).
  1369.      Make token type LAST, and put the number (the digits) in yylval.  */
  1370.  
  1371.   if (*tokstart == '$')
  1372.     {
  1373.       register int negate = 0;
  1374.       c = 1;
  1375.       /* Double dollar means negate the number and add -1 as well.
  1376.      Thus $$ alone means -1.  */
  1377.       if (namelen >= 2 && tokstart[1] == '$')
  1378.     {
  1379.       negate = 1;
  1380.       c = 2;
  1381.     }
  1382.       if (c == namelen)
  1383.     {
  1384.       /* Just dollars (one or two) */
  1385.       yylval.lval = - negate;
  1386.       return LAST;
  1387.     }
  1388.       /* Is the rest of the token digits?  */
  1389.       for (; c < namelen; c++)
  1390.     if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
  1391.       break;
  1392.       if (c == namelen)
  1393.     {
  1394.       yylval.lval = atoi (tokstart + 1 + negate);
  1395.       if (negate)
  1396.         yylval.lval = - yylval.lval;
  1397.       return LAST;
  1398.     }
  1399.     }
  1400.  
  1401.   /* Handle tokens that refer to machine registers:
  1402.      $ followed by a register name.  */
  1403.  
  1404.   if (*tokstart == '$') {
  1405.     for (c = 0; c < NUM_REGS; c++)
  1406.       if (namelen - 1 == strlen (reg_names[c])
  1407.       && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
  1408.     {
  1409.       yylval.lval = c;
  1410.       return REGNAME;
  1411.     }
  1412.     for (c = 0; c < NUM_STD_REGS; c++)
  1413.      if (namelen - 1 == strlen (std_regs[c].name)
  1414.      && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
  1415.        {
  1416.      yylval.lval = std_regs[c].regnum;
  1417.      return REGNAME;
  1418.        }
  1419.   }
  1420.   /* Catch specific keywords.  Should be done with a data structure.  */
  1421.   switch (namelen)
  1422.     {
  1423.     case 8:
  1424.       if (!strncmp (tokstart, "unsigned", 8))
  1425.     return UNSIGNED;
  1426.       break;
  1427.     case 6:
  1428.       if (!strncmp (tokstart, "struct", 6))
  1429.     return STRUCT;
  1430.       if (!strncmp (tokstart, "signed", 6))
  1431.     return SIGNED;
  1432.       if (!strncmp (tokstart, "sizeof", 6))      
  1433.     return SIZEOF;
  1434.       if (!strncmp (tokstart, "double", 6))
  1435.     return DOUBLE;
  1436.       break;
  1437.     case 5:
  1438.       if (!strncmp (tokstart, "union", 5))
  1439.     return UNION;
  1440.       if (!strncmp (tokstart, "short", 5))
  1441.     return SHORT;
  1442.       break;
  1443.     case 4:
  1444.       if (!strncmp (tokstart, "enum", 4))
  1445.     return ENUM;
  1446.       if (!strncmp (tokstart, "long", 4))
  1447.     return LONG;
  1448.       if (!strncmp (tokstart, "this", 4)
  1449.       && lookup_symbol ("$this", expression_context_block,
  1450.                 VAR_NAMESPACE, 0))
  1451.     return THIS;
  1452.       break;
  1453.     case 3:
  1454.       if (!strncmp (tokstart, "int", 3))
  1455.     return INT_KEYWORD;
  1456.       break;
  1457.     default:
  1458.       break;
  1459.     }
  1460.  
  1461.   yylval.sval.ptr = tokstart;
  1462.   yylval.sval.length = namelen;
  1463.  
  1464.   /* Any other names starting in $ are debugger internal variables.  */
  1465.  
  1466.   if (*tokstart == '$')
  1467.     {
  1468.       yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
  1469.       return VARIABLE;
  1470.     }
  1471.  
  1472.   /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1473.      functions or symtabs.  If this is not so, then ...
  1474.      Use token-type TYPENAME for symbols that happen to be defined
  1475.      currently as names of types; NAME for other symbols.
  1476.      The caller is not constrained to care about the distinction.  */
  1477.   {
  1478.     char *tmp = copy_name (yylval.sval);
  1479.     struct symbol *sym;
  1480.  
  1481.     if (lookup_partial_symtab (tmp))
  1482.       return BLOCKNAME;
  1483.     sym = lookup_symbol (tmp, expression_context_block,
  1484.              VAR_NAMESPACE, 0);
  1485.     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1486.       return BLOCKNAME;
  1487.     if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
  1488.       return TYPENAME;
  1489.     return NAME;
  1490.   }
  1491. }
  1492.  
  1493. static void
  1494. yyerror ()
  1495. {
  1496.   error ("Invalid syntax in expression.");
  1497. }
  1498.  
  1499. /* Return a null-terminated temporary copy of the name
  1500.    of a string token.  */
  1501.  
  1502. static char *
  1503. copy_name (token)
  1504.      struct stoken token;
  1505. {
  1506.   bcopy (token.ptr, namecopy, token.length);
  1507.   namecopy[token.length] = 0;
  1508.   return namecopy;
  1509. }
  1510.  
  1511. /* Reverse an expression from suffix form (in which it is constructed)
  1512.    to prefix form (in which we can conveniently print or execute it).  */
  1513.  
  1514. static void prefixify_subexp ();
  1515.  
  1516. static void
  1517. prefixify_expression (expr)
  1518.      register struct expression *expr;
  1519. {
  1520.   register int len = sizeof (struct expression) +
  1521.                     expr->nelts * sizeof (union exp_element);
  1522.   register struct expression *temp;
  1523.   register int inpos = expr->nelts, outpos = 0;
  1524.  
  1525.   temp = (struct expression *) alloca (len);
  1526.  
  1527.   /* Copy the original expression into temp.  */
  1528.   bcopy (expr, temp, len);
  1529.  
  1530.   prefixify_subexp (temp, expr, inpos, outpos);
  1531. }
  1532.  
  1533. /* Return the number of exp_elements in the subexpression of EXPR
  1534.    whose last exp_element is at index ENDPOS - 1 in EXPR.  */
  1535.  
  1536. static int
  1537. length_of_subexp (expr, endpos)
  1538.      register struct expression *expr;
  1539.      register int endpos;
  1540. {
  1541.   register int oplen = 1;
  1542.   register int args = 0;
  1543.   register int i;
  1544.  
  1545.   if (endpos < 0)
  1546.     error ("?error in length_of_subexp");
  1547.  
  1548.   i = (int) expr->elts[endpos - 1].opcode;
  1549.  
  1550.   switch (i)
  1551.     {
  1552.       /* C++  */
  1553.     case OP_SCOPE:
  1554.       oplen = 4 + ((expr->elts[endpos - 2].longconst
  1555.             + sizeof (union exp_element))
  1556.            / sizeof (union exp_element));
  1557.       break;
  1558.  
  1559.     case OP_LONG:
  1560.     case OP_DOUBLE:
  1561.       oplen = 4;
  1562.       break;
  1563.  
  1564.     case OP_VAR_VALUE:
  1565.     case OP_LAST:
  1566.     case OP_REGISTER:
  1567.     case OP_INTERNALVAR:
  1568.       oplen = 3;
  1569.       break;
  1570.  
  1571.     case OP_FUNCALL:
  1572.       oplen = 3;
  1573.       args = 1 + expr->elts[endpos - 2].longconst;
  1574.       break;
  1575.  
  1576.     case UNOP_CAST:
  1577.     case UNOP_MEMVAL:
  1578.       oplen = 3;
  1579.       args = 1;
  1580.       break;
  1581.  
  1582.     case STRUCTOP_STRUCT:
  1583.     case STRUCTOP_PTR:
  1584.       args = 1;
  1585.     case OP_STRING:
  1586.       oplen = 3 + ((expr->elts[endpos - 2].longconst
  1587.             + sizeof (union exp_element))
  1588.            / sizeof (union exp_element));
  1589.       break;
  1590.  
  1591.     case TERNOP_COND:
  1592.       args = 3;
  1593.       break;
  1594.  
  1595.     case BINOP_ASSIGN_MODIFY:
  1596.       oplen = 3;
  1597.       args = 2;
  1598.       break;
  1599.  
  1600.       /* C++ */
  1601.     case OP_THIS:
  1602.       oplen = 2;
  1603.       break;
  1604.  
  1605.     default:
  1606.       args = 1 + (i < (int) BINOP_END);
  1607.     }
  1608.  
  1609.   while (args > 0)
  1610.     {
  1611.       oplen += length_of_subexp (expr, endpos - oplen);
  1612.       args--;
  1613.     }
  1614.  
  1615.   return oplen;
  1616. }
  1617.  
  1618. /* Copy the subexpression ending just before index INEND in INEXPR
  1619.    into OUTEXPR, starting at index OUTBEG.
  1620.    In the process, convert it from suffix to prefix form.  */
  1621.  
  1622. static void
  1623. prefixify_subexp (inexpr, outexpr, inend, outbeg)
  1624.      register struct expression *inexpr;
  1625.      struct expression *outexpr;
  1626.      register int inend;
  1627.      int outbeg;
  1628. {
  1629.   register int oplen = 1;
  1630.   register int args = 0;
  1631.   register int i;
  1632.   int *arglens;
  1633.   enum exp_opcode opcode;
  1634.  
  1635.   /* Compute how long the last operation is (in OPLEN),
  1636.      and also how many preceding subexpressions serve as
  1637.      arguments for it (in ARGS).  */
  1638.  
  1639.   opcode = inexpr->elts[inend - 1].opcode;
  1640.   switch (opcode)
  1641.     {
  1642.       /* C++  */
  1643.     case OP_SCOPE:
  1644.       oplen = 4 + ((inexpr->elts[inend - 2].longconst
  1645.             + sizeof (union exp_element))
  1646.            / sizeof (union exp_element));
  1647.       break;
  1648.  
  1649.     case OP_LONG:
  1650.     case OP_DOUBLE:
  1651.       oplen = 4;
  1652.       break;
  1653.  
  1654.     case OP_VAR_VALUE:
  1655.     case OP_LAST:
  1656.     case OP_REGISTER:
  1657.     case OP_INTERNALVAR:
  1658.       oplen = 3;
  1659.       break;
  1660.  
  1661.     case OP_FUNCALL:
  1662.       oplen = 3;
  1663.       args = 1 + inexpr->elts[inend - 2].longconst;
  1664.       break;
  1665.  
  1666.     case UNOP_CAST:
  1667.     case UNOP_MEMVAL:
  1668.       oplen = 3;
  1669.       args = 1;
  1670.       break;
  1671.  
  1672.     case STRUCTOP_STRUCT:
  1673.     case STRUCTOP_PTR:
  1674.       args = 1;
  1675.     case OP_STRING:
  1676.       oplen = 3 + ((inexpr->elts[inend - 2].longconst
  1677.             + sizeof (union exp_element))
  1678.            / sizeof (union exp_element));
  1679.            
  1680.       break;
  1681.  
  1682.     case TERNOP_COND:
  1683.       args = 3;
  1684.       break;
  1685.  
  1686.     case BINOP_ASSIGN_MODIFY:
  1687.       oplen = 3;
  1688.       args = 2;
  1689.       break;
  1690.  
  1691.       /* C++ */
  1692.     case OP_THIS:
  1693.       oplen = 2;
  1694.       break;
  1695.  
  1696.     default:
  1697.       args = 1 + ((int) opcode < (int) BINOP_END);
  1698.     }
  1699.  
  1700.   /* Copy the final operator itself, from the end of the input
  1701.      to the beginning of the output.  */
  1702.   inend -= oplen;
  1703.   bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
  1704.      oplen * sizeof (union exp_element));
  1705.   outbeg += oplen;
  1706.  
  1707.   /* Find the lengths of the arg subexpressions.  */
  1708.   arglens = (int *) alloca (args * sizeof (int));
  1709.   for (i = args - 1; i >= 0; i--)
  1710.     {
  1711.       oplen = length_of_subexp (inexpr, inend);
  1712.       arglens[i] = oplen;
  1713.       inend -= oplen;
  1714.     }
  1715.  
  1716.   /* Now copy each subexpression, preserving the order of
  1717.      the subexpressions, but prefixifying each one.
  1718.      In this loop, inend starts at the beginning of
  1719.      the expression this level is working on
  1720.      and marches forward over the arguments.
  1721.      outbeg does similarly in the output.  */
  1722.   for (i = 0; i < args; i++)
  1723.     {
  1724.       oplen = arglens[i];
  1725.       inend += oplen;
  1726.       prefixify_subexp (inexpr, outexpr, inend, outbeg);
  1727.       outbeg += oplen;
  1728.     }
  1729. }
  1730.  
  1731. /* This page contains the two entry points to this file.  */
  1732.  
  1733. /* Read a C expression from the string *STRINGPTR points to,
  1734.    parse it, and return a pointer to a  struct expression  that we malloc.
  1735.    Use block BLOCK as the lexical context for variable names;
  1736.    if BLOCK is zero, use the block of the selected stack frame.
  1737.    Meanwhile, advance *STRINGPTR to point after the expression,
  1738.    at the first nonwhite character that is not part of the expression
  1739.    (possibly a null character).
  1740.  
  1741.    If COMMA is nonzero, stop if a comma is reached.  */
  1742.  
  1743. struct expression *
  1744. parse_c_1 (stringptr, block, comma)
  1745.      char **stringptr;
  1746.      struct block *block;
  1747. {
  1748.   struct cleanup *old_chain;
  1749.  
  1750.   lexptr = *stringptr;
  1751.  
  1752.   paren_depth = 0;
  1753.   type_stack_depth = 0;
  1754.  
  1755.   comma_terminates = comma;
  1756.  
  1757.   if (lexptr == 0 || *lexptr == 0)
  1758.     error_no_arg ("expression to compute");
  1759.  
  1760.   old_chain = make_cleanup (free_funcalls, 0);
  1761.   funcall_chain = 0;
  1762.  
  1763.   expression_context_block = block ? block : get_selected_block ();
  1764.  
  1765.   namecopy = (char *) alloca (strlen (lexptr) + 1);
  1766.   expout_size = 10;
  1767.   expout_ptr = 0;
  1768.   expout = (struct expression *)
  1769.     xmalloc (sizeof (struct expression)
  1770.          + expout_size * sizeof (union exp_element));
  1771.   make_cleanup (free_current_contents, &expout);
  1772.   if (yyparse ())
  1773.     yyerror ();
  1774.   discard_cleanups (old_chain);
  1775.   expout->nelts = expout_ptr;
  1776.   expout = (struct expression *)
  1777.     xrealloc (expout,
  1778.           sizeof (struct expression)
  1779.           + expout_ptr * sizeof (union exp_element));
  1780.   prefixify_expression (expout);
  1781.   *stringptr = lexptr;
  1782.   return expout;
  1783. }
  1784.  
  1785. /* Parse STRING as an expression, and complain if this fails
  1786.    to use up all of the contents of STRING.  */
  1787.  
  1788. struct expression *
  1789. parse_c_expression (string)
  1790.      char *string;
  1791. {
  1792.   register struct expression *exp;
  1793.   exp = parse_c_1 (&string, 0, 0);
  1794.   if (*string)
  1795.     error ("Junk after end of expression.");
  1796.   return exp;
  1797. }
  1798.  
  1799. static void 
  1800. push_type (tp)
  1801.      enum type_pieces tp;
  1802. {
  1803.   if (type_stack_depth == type_stack_size)
  1804.     {
  1805.       type_stack_size *= 2;
  1806.       type_stack = (enum type_pieces *)
  1807.     xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
  1808.     }
  1809.   type_stack[type_stack_depth++] = tp;
  1810. }
  1811.  
  1812. static enum type_pieces 
  1813. pop_type ()
  1814. {
  1815.   if (type_stack_depth)
  1816.     return type_stack[--type_stack_depth];
  1817.   return tp_end;
  1818. }
  1819.  
  1820. void
  1821. _initialize_expread ()
  1822. {
  1823.   type_stack_size = 80;
  1824.   type_stack_depth = 0;
  1825.   type_stack = (enum type_pieces *)
  1826.     xmalloc (type_stack_size * sizeof (enum type_pieces));
  1827. }
  1828.