1 /* Parse C expressions for GDB.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
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)
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.
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. */
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. */
35 #include "expression.h"
39 static struct expression *expout;
40 static int expout_size;
41 static int expout_ptr;
44 static void yyerror ();
45 static void write_exp_elt ();
46 static void write_exp_elt_opcode ();
47 static void write_exp_elt_sym ();
48 static void write_exp_elt_longcst ();
49 static void write_exp_elt_dblcst ();
50 static void write_exp_elt_type ();
51 static void write_exp_elt_intern ();
52 static void write_exp_string ();
53 static void start_arglist ();
54 static int end_arglist ();
55 static void free_funcalls ();
56 static char *copy_name ();
58 /* If this is nonzero, this block is used as the lexical context
61 static struct block *expression_context_block;
63 /* The innermost context required by the stack and register variables
64 we've encountered so far. */
65 struct block *innermost_block;
67 /* The block in which the most recently discovered symbol was found. */
68 struct block *block_found;
70 /* Number of arguments seen so far in innermost function call. */
71 static int arglist_len;
73 /* Data structure for saving values of arglist_len
74 for function calls whose arguments contain other function calls. */
82 struct funcall *funcall_chain;
84 /* This kind of datum is used to represent the name
93 /* For parsing of complicated types.
94 An array should be preceded in the list by the size of the array. */
96 {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function};
97 static enum type_pieces *type_stack;
98 static int type_stack_depth, type_stack_size;
100 static void push_type ();
101 static enum type_pieces pop_type ();
103 /* Allow debugging of parsing. */
107 /* Although the yacc "value" of an expression is not used,
108 since the result is stored in the structure being created,
109 other node types do have values. */
114 unsigned LONGEST ulval;
121 enum exp_opcode opcode;
122 struct internalvar *ivar;
128 %type <voidval> exp exp1 start variable
129 %type <tval> type typebase
130 %type <tvec> nonempty_typelist
133 /* Fancy type parsing. */
134 %type <voidval> func_mod direct_abs_decl abs_decl
136 %type <lval> array_mod
138 %token <lval> INT CHAR
142 /* Both NAME and TYPENAME tokens represent symbols in the input,
143 and both convey their data as strings.
144 But a TYPENAME is a string that happens to be defined as a typedef
145 or builtin type name (such as int or char)
146 and a NAME is any other symbol.
148 Contexts where this distinction is not important can use the
149 nonterminal "name", which matches either NAME or TYPENAME. */
151 %token <sval> NAME TYPENAME BLOCKNAME STRING
152 %type <sval> name name_not_typename typename
154 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
156 /* Special type cases, put in to allow the parser to distinguish different
158 %token SIGNED LONG SHORT INT_KEYWORD
160 %token <lval> LAST REGNAME
162 %token <ivar> VARIABLE
164 %token <opcode> ASSIGN_MODIFY
171 %right '=' ASSIGN_MODIFY
179 %left '<' '>' LEQ GEQ
184 %right UNARY INCREMENT DECREMENT
185 %right ARROW '.' '[' '('
193 /* Expressions, including the comma operator. */
196 { write_exp_elt_opcode (BINOP_COMMA); }
199 /* Expressions, not including the comma operator. */
200 exp : '*' exp %prec UNARY
201 { write_exp_elt_opcode (UNOP_IND); }
203 exp : '&' exp %prec UNARY
204 { write_exp_elt_opcode (UNOP_ADDR); }
206 exp : '-' exp %prec UNARY
207 { write_exp_elt_opcode (UNOP_NEG); }
210 exp : '!' exp %prec UNARY
211 { write_exp_elt_opcode (UNOP_ZEROP); }
214 exp : '~' exp %prec UNARY
215 { write_exp_elt_opcode (UNOP_LOGNOT); }
218 exp : INCREMENT exp %prec UNARY
219 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
222 exp : DECREMENT exp %prec UNARY
223 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
226 exp : exp INCREMENT %prec UNARY
227 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
230 exp : exp DECREMENT %prec UNARY
231 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
234 exp : SIZEOF exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_SIZEOF); }
239 { write_exp_elt_opcode (STRUCTOP_PTR);
240 write_exp_string ($3);
241 write_exp_elt_opcode (STRUCTOP_PTR); }
244 exp : exp ARROW '*' exp
245 { write_exp_elt_opcode (STRUCTOP_MPTR); }
249 { write_exp_elt_opcode (STRUCTOP_STRUCT);
250 write_exp_string ($3);
251 write_exp_elt_opcode (STRUCTOP_STRUCT); }
254 exp : exp '.' '*' exp
255 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
258 exp : exp '[' exp1 ']'
259 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
263 /* This is to save the value of arglist_len
264 being accumulated by an outer function call. */
265 { start_arglist (); }
266 arglist ')' %prec ARROW
267 { write_exp_elt_opcode (OP_FUNCALL);
268 write_exp_elt_longcst ((LONGEST) end_arglist ());
269 write_exp_elt_opcode (OP_FUNCALL); }
279 arglist : arglist ',' exp %prec ABOVE_COMMA
283 exp : '{' type '}' exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_MEMVAL);
285 write_exp_elt_type ($2);
286 write_exp_elt_opcode (UNOP_MEMVAL); }
289 exp : '(' type ')' exp %prec UNARY
290 { write_exp_elt_opcode (UNOP_CAST);
291 write_exp_elt_type ($2);
292 write_exp_elt_opcode (UNOP_CAST); }
299 /* Binary operators in order of decreasing precedence. */
302 { write_exp_elt_opcode (BINOP_REPEAT); }
306 { write_exp_elt_opcode (BINOP_MUL); }
310 { write_exp_elt_opcode (BINOP_DIV); }
314 { write_exp_elt_opcode (BINOP_REM); }
318 { write_exp_elt_opcode (BINOP_ADD); }
322 { write_exp_elt_opcode (BINOP_SUB); }
326 { write_exp_elt_opcode (BINOP_LSH); }
330 { write_exp_elt_opcode (BINOP_RSH); }
334 { write_exp_elt_opcode (BINOP_EQUAL); }
337 exp : exp NOTEQUAL exp
338 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
342 { write_exp_elt_opcode (BINOP_LEQ); }
346 { write_exp_elt_opcode (BINOP_GEQ); }
350 { write_exp_elt_opcode (BINOP_LESS); }
354 { write_exp_elt_opcode (BINOP_GTR); }
358 { write_exp_elt_opcode (BINOP_LOGAND); }
362 { write_exp_elt_opcode (BINOP_LOGXOR); }
366 { write_exp_elt_opcode (BINOP_LOGIOR); }
370 { write_exp_elt_opcode (BINOP_AND); }
374 { write_exp_elt_opcode (BINOP_OR); }
377 exp : exp '?' exp ':' exp %prec '?'
378 { write_exp_elt_opcode (TERNOP_COND); }
382 { write_exp_elt_opcode (BINOP_ASSIGN); }
385 exp : exp ASSIGN_MODIFY exp
386 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
387 write_exp_elt_opcode ($2);
388 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
392 { write_exp_elt_opcode (OP_LONG);
393 if ($1 == (int) $1 || $1 == (unsigned int) $1)
394 write_exp_elt_type (builtin_type_int);
396 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
397 write_exp_elt_longcst ((LONGEST) $1);
398 write_exp_elt_opcode (OP_LONG); }
403 write_exp_elt_opcode (OP_LONG);
404 if ($1 == (unsigned int) $1)
405 write_exp_elt_type (builtin_type_unsigned_int);
407 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
408 write_exp_elt_longcst ((LONGEST) $1);
409 write_exp_elt_opcode (OP_LONG);
414 { write_exp_elt_opcode (OP_LONG);
415 write_exp_elt_type (builtin_type_char);
416 write_exp_elt_longcst ((LONGEST) $1);
417 write_exp_elt_opcode (OP_LONG); }
421 { write_exp_elt_opcode (OP_DOUBLE);
422 write_exp_elt_type (builtin_type_double);
423 write_exp_elt_dblcst ($1);
424 write_exp_elt_opcode (OP_DOUBLE); }
431 { write_exp_elt_opcode (OP_LAST);
432 write_exp_elt_longcst ((LONGEST) $1);
433 write_exp_elt_opcode (OP_LAST); }
437 { write_exp_elt_opcode (OP_REGISTER);
438 write_exp_elt_longcst ((LONGEST) $1);
439 write_exp_elt_opcode (OP_REGISTER); }
443 { write_exp_elt_opcode (OP_INTERNALVAR);
444 write_exp_elt_intern ($1);
445 write_exp_elt_opcode (OP_INTERNALVAR); }
448 exp : SIZEOF '(' type ')' %prec UNARY
449 { write_exp_elt_opcode (OP_LONG);
450 write_exp_elt_type (builtin_type_int);
451 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
452 write_exp_elt_opcode (OP_LONG); }
456 { write_exp_elt_opcode (OP_STRING);
457 write_exp_string ($1);
458 write_exp_elt_opcode (OP_STRING); }
463 { write_exp_elt_opcode (OP_THIS);
464 write_exp_elt_opcode (OP_THIS); }
471 struct symtab *tem = lookup_symtab (copy_name ($1));
475 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
478 sym = lookup_symbol (copy_name ($1),
479 expression_context_block,
481 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
482 $$ = SYMBOL_BLOCK_VALUE (sym);
484 error ("No file or function \"%s\".",
490 block : block COLONCOLON name
492 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
493 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
494 error ("No function \"%s\" in specified context.",
496 $$ = SYMBOL_BLOCK_VALUE (tem); }
499 variable: block COLONCOLON name
500 { struct symbol *sym;
501 sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
503 error ("No symbol \"%s\" in specified context.",
505 write_exp_elt_opcode (OP_VAR_VALUE);
506 write_exp_elt_sym (sym);
507 write_exp_elt_opcode (OP_VAR_VALUE); }
510 variable: typebase COLONCOLON name
512 struct type *type = $1;
513 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
514 && TYPE_CODE (type) != TYPE_CODE_UNION)
515 error ("`%s' is not defined as an aggregate type.",
518 write_exp_elt_opcode (OP_SCOPE);
519 write_exp_elt_type (type);
520 write_exp_string ($3);
521 write_exp_elt_opcode (OP_SCOPE);
525 char *name = copy_name ($2);
529 sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0);
532 write_exp_elt_opcode (OP_VAR_VALUE);
533 write_exp_elt_sym (sym);
534 write_exp_elt_opcode (OP_VAR_VALUE);
537 for (i = 0; i < misc_function_count; i++)
538 if (!strcmp (misc_function_vector[i].name, name))
541 if (i < misc_function_count)
543 enum misc_function_type mft =
544 (enum misc_function_type)
545 misc_function_vector[i].type;
547 write_exp_elt_opcode (OP_LONG);
548 write_exp_elt_type (builtin_type_int);
549 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
550 write_exp_elt_opcode (OP_LONG);
551 write_exp_elt_opcode (UNOP_MEMVAL);
552 if (mft == mf_data || mft == mf_bss)
553 write_exp_elt_type (builtin_type_int);
554 else if (mft == mf_text)
555 write_exp_elt_type (lookup_function_type (builtin_type_int));
557 write_exp_elt_type (builtin_type_char);
558 write_exp_elt_opcode (UNOP_MEMVAL);
562 && partial_symtab_list == 0)
563 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
565 error ("No symbol \"%s\" in current context.", name);
569 variable: name_not_typename
570 { struct symbol *sym;
571 int is_a_field_of_this;
573 sym = lookup_symbol (copy_name ($1),
574 expression_context_block,
576 &is_a_field_of_this);
584 if (innermost_block == 0 ||
585 contained_in (block_found,
587 innermost_block = block_found;
589 write_exp_elt_opcode (OP_VAR_VALUE);
590 write_exp_elt_sym (sym);
591 write_exp_elt_opcode (OP_VAR_VALUE);
593 else if (is_a_field_of_this)
595 /* C++: it hangs off of `this'. Must
596 not inadvertently convert from a method call
598 if (innermost_block == 0 ||
599 contained_in (block_found, innermost_block))
600 innermost_block = block_found;
601 write_exp_elt_opcode (OP_THIS);
602 write_exp_elt_opcode (OP_THIS);
603 write_exp_elt_opcode (STRUCTOP_PTR);
604 write_exp_string ($1);
605 write_exp_elt_opcode (STRUCTOP_PTR);
610 register char *arg = copy_name ($1);
612 for (i = 0; i < misc_function_count; i++)
613 if (!strcmp (misc_function_vector[i].name, arg))
616 if (i < misc_function_count)
618 enum misc_function_type mft =
619 (enum misc_function_type)
620 misc_function_vector[i].type;
622 write_exp_elt_opcode (OP_LONG);
623 write_exp_elt_type (builtin_type_int);
624 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
625 write_exp_elt_opcode (OP_LONG);
626 write_exp_elt_opcode (UNOP_MEMVAL);
627 if (mft == mf_data || mft == mf_bss)
628 write_exp_elt_type (builtin_type_int);
629 else if (mft == mf_text)
630 write_exp_elt_type (lookup_function_type (builtin_type_int));
632 write_exp_elt_type (builtin_type_char);
633 write_exp_elt_opcode (UNOP_MEMVAL);
635 else if (symtab_list == 0
636 && partial_symtab_list == 0)
637 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
639 error ("No symbol \"%s\" in current context.",
649 /* This is where the interesting stuff happens. */
652 struct type *follow_type = $1;
661 follow_type = lookup_pointer_type (follow_type);
664 follow_type = lookup_reference_type (follow_type);
667 array_size = (int) pop_type ();
668 if (array_size != -1)
669 follow_type = create_array_type (follow_type,
672 follow_type = lookup_pointer_type (follow_type);
675 follow_type = lookup_function_type (follow_type);
683 { push_type (tp_pointer); $$ = 0; }
685 { push_type (tp_pointer); $$ = $2; }
689 direct_abs_decl: '(' abs_decl ')'
691 | direct_abs_decl array_mod
693 push_type ((enum type_pieces) $2);
694 push_type (tp_array);
698 push_type ((enum type_pieces) $1);
699 push_type (tp_array);
702 | direct_abs_decl func_mod
703 { push_type (tp_function); }
705 { push_type (tp_function); }
719 | typebase COLONCOLON '*'
720 { $$ = lookup_member_type (builtin_type_int, $1); }
721 | type '(' typebase COLONCOLON '*' ')'
722 { $$ = lookup_member_type ($1, $3); }
723 | type '(' typebase COLONCOLON '*' ')' '(' ')'
724 { $$ = lookup_member_type
725 (lookup_function_type ($1), $3); }
726 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
727 { $$ = lookup_member_type
728 (lookup_function_type ($1), $3);
734 { $$ = lookup_typename (copy_name ($1),
735 expression_context_block, 0); }
737 { $$ = builtin_type_int; }
739 { $$ = builtin_type_long; }
741 { $$ = builtin_type_short; }
743 { $$ = builtin_type_long; }
744 | UNSIGNED LONG INT_KEYWORD
745 { $$ = builtin_type_unsigned_long; }
747 { $$ = builtin_type_short; }
748 | UNSIGNED SHORT INT_KEYWORD
749 { $$ = builtin_type_unsigned_short; }
751 { $$ = lookup_struct (copy_name ($2),
752 expression_context_block); }
754 { $$ = lookup_union (copy_name ($2),
755 expression_context_block); }
757 { $$ = lookup_enum (copy_name ($2),
758 expression_context_block); }
760 { $$ = lookup_unsigned_typename (copy_name ($2)); }
762 { $$ = builtin_type_unsigned_int; }
764 { $$ = lookup_typename (copy_name ($2),
765 expression_context_block, 0); }
767 { $$ = builtin_type_int; }
790 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
791 $$[0] = (struct type *)0;
794 | nonempty_typelist ',' type
795 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
796 $$ = (struct type **)xrealloc ($1, len);
797 $$[$<ivec>$[0]] = $3;
806 name_not_typename : NAME
812 /* Begin counting arguments for a function call,
813 saving the data about any containing call. */
818 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
820 new->next = funcall_chain;
821 new->arglist_len = arglist_len;
826 /* Return the number of arguments in a function call just terminated,
827 and restore the data for the containing function call. */
832 register int val = arglist_len;
833 register struct funcall *call = funcall_chain;
834 funcall_chain = call->next;
835 arglist_len = call->arglist_len;
840 /* Free everything in the funcall chain.
841 Used when there is an error inside parsing. */
846 register struct funcall *call, *next;
848 for (call = funcall_chain; call; call = next)
855 /* This page contains the functions for adding data to the struct expression
856 being constructed. */
858 /* Add one element to the end of the expression. */
860 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
861 a register through here */
864 write_exp_elt (expelt)
865 union exp_element expelt;
867 if (expout_ptr >= expout_size)
870 expout = (struct expression *) xrealloc (expout,
871 sizeof (struct expression)
872 + expout_size * sizeof (union exp_element));
874 expout->elts[expout_ptr++] = expelt;
878 write_exp_elt_opcode (expelt)
879 enum exp_opcode expelt;
881 union exp_element tmp;
889 write_exp_elt_sym (expelt)
890 struct symbol *expelt;
892 union exp_element tmp;
900 write_exp_elt_longcst (expelt)
903 union exp_element tmp;
905 tmp.longconst = expelt;
911 write_exp_elt_dblcst (expelt)
914 union exp_element tmp;
916 tmp.doubleconst = expelt;
922 write_exp_elt_type (expelt)
925 union exp_element tmp;
933 write_exp_elt_intern (expelt)
934 struct internalvar *expelt;
936 union exp_element tmp;
938 tmp.internalvar = expelt;
943 /* Add a string constant to the end of the expression.
944 Follow it by its length in bytes, as a separate exp_element. */
947 write_exp_string (str)
950 register int len = str.length;
952 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
954 expout_ptr += lenelt;
956 if (expout_ptr >= expout_size)
958 expout_size = max (expout_size * 2, expout_ptr + 10);
959 expout = (struct expression *)
960 xrealloc (expout, (sizeof (struct expression)
961 + (expout_size * sizeof (union exp_element))));
963 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
964 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
965 write_exp_elt_longcst ((LONGEST) len);
968 /* During parsing of a C expression, the pointer to the next character
969 is in this variable. */
973 /* Tokens that refer to names do so with explicit pointer and length,
974 so they can share the storage that lexptr is parsing.
976 When it is necessary to pass a name to a function that expects
977 a null-terminated string, the substring is copied out
978 into a block of storage that namecopy points to.
980 namecopy is allocated once, guaranteed big enough, for each parsing. */
982 static char *namecopy;
984 /* Current depth in parentheses within the expression. */
986 static int paren_depth;
988 /* Nonzero means stop parsing on first comma (if not within parentheses). */
990 static int comma_terminates;
992 /* Take care of parsing a number (anything that starts with a digit).
993 Set yylval and return the token type; update lexptr.
994 LEN is the number of characters in it. */
996 /*** Needs some error checking for the float case ***/
1002 register char *p = lexptr;
1003 register LONGEST n = 0;
1005 register int base = 10;
1006 register int len = olen;
1010 extern double atof ();
1012 for (c = 0; c < len; c++)
1015 /* It's a float since it contains a point. */
1016 yylval.dval = atof (p);
1021 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
1033 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
1034 if (c != 'l' && c != 'u')
1036 if (c >= '0' && c <= '9')
1040 if (base == 16 && c >= 'a' && c <= 'f')
1042 else if (len == 0 && c == 'l')
1044 else if (len == 0 && c == 'u')
1046 else if (base == 10 && len != 0 && (c == 'e' || c == 'E'))
1048 /* Scientific notation, where we are unlucky enough not
1049 to have a '.' in the string. */
1050 yylval.dval = atof (lexptr);
1056 err_copy = (char *) alloca (olen + 1);
1057 bcopy (lexptr, err_copy, olen);
1059 error ("Invalid number \"%s\".", err_copy);
1081 enum exp_opcode opcode;
1084 static struct token tokentab3[] =
1086 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1087 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1090 static struct token tokentab2[] =
1092 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1093 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1094 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1095 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1096 {"%=", ASSIGN_MODIFY, BINOP_REM},
1097 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1098 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1099 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1100 {"++", INCREMENT, BINOP_END},
1101 {"--", DECREMENT, BINOP_END},
1102 {"->", ARROW, BINOP_END},
1103 {"&&", AND, BINOP_END},
1104 {"||", OR, BINOP_END},
1105 {"::", COLONCOLON, BINOP_END},
1106 {"<<", LSH, BINOP_END},
1107 {">>", RSH, BINOP_END},
1108 {"==", EQUAL, BINOP_END},
1109 {"!=", NOTEQUAL, BINOP_END},
1110 {"<=", LEQ, BINOP_END},
1111 {">=", GEQ, BINOP_END}
1114 /* assign machine-independent names to certain registers
1115 * (unless overridden by the REGISTER_NAMES table)
1122 { "pc", PC_REGNUM },
1125 { "fp", FP_REGNUM },
1128 { "sp", SP_REGNUM },
1131 { "ps", PS_REGNUM },
1135 #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
1137 /* Read one token, getting characters through lexptr. */
1143 register int namelen;
1145 register char *tokstart;
1150 /* See if it is a special token of length 3. */
1151 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1152 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1155 yylval.opcode = tokentab3[i].opcode;
1156 return tokentab3[i].token;
1159 /* See if it is a special token of length 2. */
1160 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1161 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1164 yylval.opcode = tokentab2[i].opcode;
1165 return tokentab2[i].token;
1168 switch (c = *tokstart)
1183 c = parse_escape (&lexptr);
1187 error ("Invalid character constant.");
1196 if (paren_depth == 0)
1203 if (comma_terminates && paren_depth == 0)
1209 /* Might be a floating point number. */
1210 if (lexptr[1] >= '0' && lexptr[1] <= '9')
1211 break; /* Falls into number code. */
1237 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1240 c = tokstart[++namelen];
1241 if (c >= '0' && c <= '9')
1243 c = tokstart[++namelen];
1244 if (c >= '0' && c <= '9')
1245 c = tokstart[++namelen];
1248 yylval.sval.ptr = tokstart + 1;
1249 yylval.sval.length = namelen - 1;
1250 lexptr += namelen + 1;
1254 /* Is it a number? */
1255 /* Note: We have already dealt with the case of the token '.'.
1256 See case '.' above. */
1257 if ((c >= '0' && c <= '9') || c == '.')
1259 /* It's a number. */
1260 int got_dot = 0, got_e = 0;
1261 register char *p = tokstart;
1262 int hex = c == '0' && (p[1] == 'x' || p[1] == 'X');
1267 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1268 got_dot = got_e = 1;
1269 else if (!hex && !got_dot && *p == '.')
1271 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1272 && (*p == '-' || *p == '+'))
1273 /* This is the sign of the exponent, not the end of the
1276 else if (*p < '0' || *p > '9'
1277 && (!hex || ((*p < 'a' || *p > 'f')
1278 && (*p < 'A' || *p > 'F'))))
1281 return parse_number (p - tokstart);
1284 if (!(c == '_' || c == '$'
1285 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1286 /* We must have come across a bad character (e.g. ';'). */
1287 error ("Invalid character '%c' in expression.", c);
1289 /* It's a name. See how long it is. */
1291 for (c = tokstart[namelen];
1292 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1293 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1294 c = tokstart[++namelen])
1297 /* The token "if" terminates the expression and is NOT
1298 removed from the input stream. */
1299 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1306 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1307 and $$digits (equivalent to $<-digits> if you could type that).
1308 Make token type LAST, and put the number (the digits) in yylval. */
1310 if (*tokstart == '$')
1312 register int negate = 0;
1314 /* Double dollar means negate the number and add -1 as well.
1315 Thus $$ alone means -1. */
1316 if (namelen >= 2 && tokstart[1] == '$')
1323 /* Just dollars (one or two) */
1324 yylval.lval = - negate;
1327 /* Is the rest of the token digits? */
1328 for (; c < namelen; c++)
1329 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1333 yylval.lval = atoi (tokstart + 1 + negate);
1335 yylval.lval = - yylval.lval;
1340 /* Handle tokens that refer to machine registers:
1341 $ followed by a register name. */
1343 if (*tokstart == '$') {
1344 for (c = 0; c < NUM_REGS; c++)
1345 if (namelen - 1 == strlen (reg_names[c])
1346 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1351 for (c = 0; c < NUM_STD_REGS; c++)
1352 if (namelen - 1 == strlen (std_regs[c].name)
1353 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1355 yylval.lval = std_regs[c].regnum;
1359 /* Catch specific keywords. Should be done with a data structure. */
1363 if (!strncmp (tokstart, "unsigned", 8))
1367 if (!strncmp (tokstart, "struct", 6))
1369 if (!strncmp (tokstart, "signed", 6))
1371 if (!strncmp (tokstart, "sizeof", 6))
1375 if (!strncmp (tokstart, "union", 5))
1377 if (!strncmp (tokstart, "short", 5))
1381 if (!strncmp (tokstart, "enum", 4))
1383 if (!strncmp (tokstart, "long", 4))
1385 if (!strncmp (tokstart, "this", 4)
1386 && lookup_symbol ("$this", expression_context_block,
1391 if (!strncmp (tokstart, "int", 3))
1398 yylval.sval.ptr = tokstart;
1399 yylval.sval.length = namelen;
1401 /* Any other names starting in $ are debugger internal variables. */
1403 if (*tokstart == '$')
1405 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1409 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1410 functions or symtabs. If this is not so, then ...
1411 Use token-type TYPENAME for symbols that happen to be defined
1412 currently as names of types; NAME for other symbols.
1413 The caller is not constrained to care about the distinction. */
1415 char *tmp = copy_name (yylval.sval);
1418 if (lookup_partial_symtab (tmp))
1420 sym = lookup_symbol (tmp, expression_context_block,
1422 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1424 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1433 error ("Invalid syntax in expression.");
1436 /* Return a null-terminated temporary copy of the name
1437 of a string token. */
1441 struct stoken token;
1443 bcopy (token.ptr, namecopy, token.length);
1444 namecopy[token.length] = 0;
1448 /* Reverse an expression from suffix form (in which it is constructed)
1449 to prefix form (in which we can conveniently print or execute it). */
1451 static void prefixify_subexp ();
1454 prefixify_expression (expr)
1455 register struct expression *expr;
1457 register int len = sizeof (struct expression) +
1458 expr->nelts * sizeof (union exp_element);
1459 register struct expression *temp;
1460 register int inpos = expr->nelts, outpos = 0;
1462 temp = (struct expression *) alloca (len);
1464 /* Copy the original expression into temp. */
1465 bcopy (expr, temp, len);
1467 prefixify_subexp (temp, expr, inpos, outpos);
1470 /* Return the number of exp_elements in the subexpression of EXPR
1471 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1474 length_of_subexp (expr, endpos)
1475 register struct expression *expr;
1476 register int endpos;
1478 register int oplen = 1;
1479 register int args = 0;
1483 error ("?error in length_of_subexp");
1485 i = (int) expr->elts[endpos - 1].opcode;
1491 oplen = 4 + ((expr->elts[endpos - 2].longconst
1492 + sizeof (union exp_element))
1493 / sizeof (union exp_element));
1504 case OP_INTERNALVAR:
1510 args = 1 + expr->elts[endpos - 2].longconst;
1519 case STRUCTOP_STRUCT:
1523 oplen = 3 + ((expr->elts[endpos - 2].longconst
1524 + sizeof (union exp_element))
1525 / sizeof (union exp_element));
1532 case BINOP_ASSIGN_MODIFY:
1543 args = 1 + (i < (int) BINOP_END);
1548 oplen += length_of_subexp (expr, endpos - oplen);
1555 /* Copy the subexpression ending just before index INEND in INEXPR
1556 into OUTEXPR, starting at index OUTBEG.
1557 In the process, convert it from suffix to prefix form. */
1560 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1561 register struct expression *inexpr;
1562 struct expression *outexpr;
1566 register int oplen = 1;
1567 register int args = 0;
1570 enum exp_opcode opcode;
1572 /* Compute how long the last operation is (in OPLEN),
1573 and also how many preceding subexpressions serve as
1574 arguments for it (in ARGS). */
1576 opcode = inexpr->elts[inend - 1].opcode;
1581 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1582 + sizeof (union exp_element))
1583 / sizeof (union exp_element));
1594 case OP_INTERNALVAR:
1600 args = 1 + inexpr->elts[inend - 2].longconst;
1609 case STRUCTOP_STRUCT:
1613 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1614 + sizeof (union exp_element))
1615 / sizeof (union exp_element));
1623 case BINOP_ASSIGN_MODIFY:
1634 args = 1 + ((int) opcode < (int) BINOP_END);
1637 /* Copy the final operator itself, from the end of the input
1638 to the beginning of the output. */
1640 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1641 oplen * sizeof (union exp_element));
1644 /* Find the lengths of the arg subexpressions. */
1645 arglens = (int *) alloca (args * sizeof (int));
1646 for (i = args - 1; i >= 0; i--)
1648 oplen = length_of_subexp (inexpr, inend);
1653 /* Now copy each subexpression, preserving the order of
1654 the subexpressions, but prefixifying each one.
1655 In this loop, inend starts at the beginning of
1656 the expression this level is working on
1657 and marches forward over the arguments.
1658 outbeg does similarly in the output. */
1659 for (i = 0; i < args; i++)
1663 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1668 /* This page contains the two entry points to this file. */
1670 /* Read a C expression from the string *STRINGPTR points to,
1671 parse it, and return a pointer to a struct expression that we malloc.
1672 Use block BLOCK as the lexical context for variable names;
1673 if BLOCK is zero, use the block of the selected stack frame.
1674 Meanwhile, advance *STRINGPTR to point after the expression,
1675 at the first nonwhite character that is not part of the expression
1676 (possibly a null character).
1678 If COMMA is nonzero, stop if a comma is reached. */
1681 parse_c_1 (stringptr, block, comma)
1683 struct block *block;
1685 struct cleanup *old_chain;
1687 lexptr = *stringptr;
1690 type_stack_depth = 0;
1692 comma_terminates = comma;
1694 if (lexptr == 0 || *lexptr == 0)
1695 error_no_arg ("expression to compute");
1697 old_chain = make_cleanup (free_funcalls, 0);
1700 expression_context_block = block ? block : get_selected_block ();
1702 namecopy = (char *) alloca (strlen (lexptr) + 1);
1705 expout = (struct expression *)
1706 xmalloc (sizeof (struct expression)
1707 + expout_size * sizeof (union exp_element));
1708 make_cleanup (free_current_contents, &expout);
1711 discard_cleanups (old_chain);
1712 expout->nelts = expout_ptr;
1713 expout = (struct expression *)
1715 sizeof (struct expression)
1716 + expout_ptr * sizeof (union exp_element));
1717 prefixify_expression (expout);
1718 *stringptr = lexptr;
1722 /* Parse STRING as an expression, and complain if this fails
1723 to use up all of the contents of STRING. */
1726 parse_c_expression (string)
1729 register struct expression *exp;
1730 exp = parse_c_1 (&string, 0, 0);
1732 error ("Junk after end of expression.");
1738 enum type_pieces tp;
1740 if (type_stack_depth == type_stack_size)
1742 type_stack_size *= 2;
1743 type_stack = (enum type_pieces *)
1744 xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
1746 type_stack[type_stack_depth++] = tp;
1749 static enum type_pieces
1752 if (type_stack_depth)
1753 return type_stack[--type_stack_depth];
1758 _initialize_expread ()
1760 type_stack_size = 80;
1761 type_stack_depth = 0;
1762 type_stack = (enum type_pieces *)
1763 xmalloc (type_stack_size * sizeof (enum type_pieces));